1 //
2 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // utilities.h: Conversion functions and other utility routines.
8 
9 #ifndef COMMON_UTILITIES_H_
10 #define COMMON_UTILITIES_H_
11 
12 #include <EGL/egl.h>
13 #include <EGL/eglext.h>
14 
15 #include "angle_gl.h"
16 #include <string>
17 #include <math.h>
18 
19 #include "common/mathutil.h"
20 
21 namespace gl
22 {
23 
24 int VariableComponentCount(GLenum type);
25 GLenum VariableComponentType(GLenum type);
26 size_t VariableComponentSize(GLenum type);
27 size_t VariableInternalSize(GLenum type);
28 size_t VariableExternalSize(GLenum type);
29 GLenum VariableBoolVectorType(GLenum type);
30 int VariableRowCount(GLenum type);
31 int VariableColumnCount(GLenum type);
32 bool IsSamplerType(GLenum type);
33 GLenum SamplerTypeToTextureType(GLenum samplerType);
34 bool IsMatrixType(GLenum type);
35 GLenum TransposeMatrixType(GLenum type);
36 int VariableRegisterCount(GLenum type);
37 int MatrixRegisterCount(GLenum type, bool isRowMajorMatrix);
38 int MatrixComponentCount(GLenum type, bool isRowMajorMatrix);
39 int VariableSortOrder(GLenum type);
40 
41 int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsigned int bitsSize);
42 
43 static const GLenum FirstCubeMapTextureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
44 static const GLenum LastCubeMapTextureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
45 bool IsCubeMapTextureTarget(GLenum target);
46 size_t CubeMapTextureTargetToLayerIndex(GLenum target);
47 GLenum LayerIndexToCubeMapTextureTarget(size_t index);
48 
49 // Parse the base uniform name and array index.  Returns the base name of the uniform. outSubscript is
50 // set to GL_INVALID_INDEX if the provided name is not an array or the array index is invalid.
51 std::string ParseUniformName(const std::string &name, size_t *outSubscript);
52 
53 // Find the range of index values in the provided indices pointer.  Primitive restart indices are
54 // only counted in the range if primitive restart is disabled.
55 IndexRange ComputeIndexRange(GLenum indexType,
56                              const GLvoid *indices,
57                              size_t count,
58                              bool primitiveRestartEnabled);
59 
60 // Get the primitive restart index value for the given index type.
61 GLuint GetPrimitiveRestartIndex(GLenum indexType);
62 
63 bool IsTriangleMode(GLenum drawMode);
64 
65 // [OpenGL ES 3.0.2] Section 2.3.1 page 14
66 // Data Conversion For State-Setting Commands
67 // Floating-point values are rounded to the nearest integer, instead of truncated, as done by static_cast.
iround(GLfloat value)68 template <typename outT> outT iround(GLfloat value) { return static_cast<outT>(value > 0.0f ? floor(value + 0.5f) : ceil(value - 0.5f)); }
uiround(GLfloat value)69 template <typename outT> outT uiround(GLfloat value) { return static_cast<outT>(value + 0.5f); }
70 
71 // Helper for converting arbitrary GL types to other GL types used in queries and state setting
72 template <typename ParamType>
ConvertToGLuint(ParamType param)73 GLuint ConvertToGLuint(ParamType param)
74 {
75     return static_cast<GLuint>(param);
76 }
77 template <>
78 GLuint ConvertToGLuint(GLfloat param);
79 
80 template <typename ParamType>
ConvertToGLint(ParamType param)81 GLint ConvertToGLint(ParamType param)
82 {
83     return static_cast<GLint>(param);
84 }
85 template <>
86 GLint ConvertToGLint(GLfloat param);
87 
88 // Same conversion as uint
89 template <typename ParamType>
ConvertToGLenum(ParamType param)90 GLenum ConvertToGLenum(ParamType param)
91 {
92     return static_cast<GLenum>(ConvertToGLuint(param));
93 }
94 
95 template <typename ParamType>
ConvertToGLfloat(ParamType param)96 GLfloat ConvertToGLfloat(ParamType param)
97 {
98     return static_cast<GLfloat>(param);
99 }
100 
101 template <typename ParamType>
ConvertFromGLfloat(GLfloat param)102 ParamType ConvertFromGLfloat(GLfloat param)
103 {
104     return static_cast<ParamType>(param);
105 }
106 template <>
107 GLint ConvertFromGLfloat(GLfloat param);
108 template <>
109 GLuint ConvertFromGLfloat(GLfloat param);
110 
111 template <typename ParamType>
ConvertFromGLenum(GLenum param)112 ParamType ConvertFromGLenum(GLenum param)
113 {
114     return static_cast<ParamType>(param);
115 }
116 
117 template <typename ParamType>
ConvertFromGLuint(GLuint param)118 ParamType ConvertFromGLuint(GLuint param)
119 {
120     return static_cast<ParamType>(param);
121 }
122 
123 template <typename ParamType>
ConvertFromGLint(GLint param)124 ParamType ConvertFromGLint(GLint param)
125 {
126     return static_cast<ParamType>(param);
127 }
128 
129 template <typename ParamType>
ConvertFromGLboolean(GLboolean param)130 ParamType ConvertFromGLboolean(GLboolean param)
131 {
132     return static_cast<ParamType>(param ? GL_TRUE : GL_FALSE);
133 }
134 
135 template <typename ParamType>
ConvertFromGLint64(GLint64 param)136 ParamType ConvertFromGLint64(GLint64 param)
137 {
138     return clampCast<ParamType>(param);
139 }
140 
141 unsigned int ParseAndStripArrayIndex(std::string *name);
142 
143 }  // namespace gl
144 
145 namespace egl
146 {
147 static const EGLenum FirstCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
148 static const EGLenum LastCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR;
149 bool IsCubeMapTextureTarget(EGLenum target);
150 size_t CubeMapTextureTargetToLayerIndex(EGLenum target);
151 EGLenum LayerIndexToCubeMapTextureTarget(size_t index);
152 bool IsTextureTarget(EGLenum target);
153 bool IsRenderbufferTarget(EGLenum target);
154 }
155 
156 namespace egl_gl
157 {
158 GLenum EGLCubeMapTargetToGLCubeMapTarget(EGLenum eglTarget);
159 GLenum EGLImageTargetToGLTextureTarget(EGLenum eglTarget);
160 GLuint EGLClientBufferToGLObjectHandle(EGLClientBuffer buffer);
161 }
162 
163 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
164 std::string getTempPath();
165 void writeFile(const char* path, const void* data, size_t size);
166 #endif
167 
168 #if defined (ANGLE_PLATFORM_WINDOWS)
169 void ScheduleYield();
170 #endif
171 
172 #endif  // COMMON_UTILITIES_H_
173