1 //
2 // Copyright 2002 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 <math.h>
16 #include <string>
17 #include <vector>
18
19 #include "angle_gl.h"
20
21 #include "common/PackedEnums.h"
22 #include "common/mathutil.h"
23 #include "common/platform.h"
24
25 namespace sh
26 {
27 struct ShaderVariable;
28 }
29
30 namespace gl
31 {
32
33 int VariableComponentCount(GLenum type);
34 GLenum VariableComponentType(GLenum type);
35 size_t VariableComponentSize(GLenum type);
36 size_t VariableInternalSize(GLenum type);
37 size_t VariableExternalSize(GLenum type);
38 int VariableRowCount(GLenum type);
39 int VariableColumnCount(GLenum type);
40 bool IsSamplerType(GLenum type);
41 bool IsSamplerCubeType(GLenum type);
42 bool IsImageType(GLenum type);
43 bool IsImage2DType(GLenum type);
44 bool IsAtomicCounterType(GLenum type);
45 bool IsOpaqueType(GLenum type);
46 bool IsMatrixType(GLenum type);
47 GLenum TransposeMatrixType(GLenum type);
48 int VariableRegisterCount(GLenum type);
49 int MatrixRegisterCount(GLenum type, bool isRowMajorMatrix);
50 int MatrixComponentCount(GLenum type, bool isRowMajorMatrix);
51 int VariableSortOrder(GLenum type);
52 GLenum VariableBoolVectorType(GLenum type);
53
54 int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsigned int bitsSize);
55
56 // Parse the base resource name and array indices. Returns the base name of the resource.
57 // If the provided name doesn't index an array, the outSubscripts vector will be empty.
58 // If the provided name indexes an array, the outSubscripts vector will contain indices with
59 // outermost array indices in the back. If an array index is invalid, GL_INVALID_INDEX is added to
60 // outSubscripts.
61 std::string ParseResourceName(const std::string &name, std::vector<unsigned int> *outSubscripts);
62
63 // Strips only the last array index from a resource name.
64 std::string StripLastArrayIndex(const std::string &name);
65
66 // Find the child field which matches 'fullName' == var.name + "." + field.name.
67 // Return nullptr if not found.
68 const sh::ShaderVariable *FindShaderVarField(const sh::ShaderVariable &var,
69 const std::string &fullName,
70 GLuint *fieldIndexOut);
71
72 // Find the range of index values in the provided indices pointer. Primitive restart indices are
73 // only counted in the range if primitive restart is disabled.
74 IndexRange ComputeIndexRange(DrawElementsType indexType,
75 const GLvoid *indices,
76 size_t count,
77 bool primitiveRestartEnabled);
78
79 // Get the primitive restart index value for the given index type.
80 GLuint GetPrimitiveRestartIndex(DrawElementsType indexType);
81
82 // Get the primitive restart index value with the given C++ type.
83 template <typename T>
GetPrimitiveRestartIndexFromType()84 constexpr T GetPrimitiveRestartIndexFromType()
85 {
86 return std::numeric_limits<T>::max();
87 }
88
89 static_assert(GetPrimitiveRestartIndexFromType<uint8_t>() == 0xFF,
90 "verify restart index for uint8_t values");
91 static_assert(GetPrimitiveRestartIndexFromType<uint16_t>() == 0xFFFF,
92 "verify restart index for uint8_t values");
93 static_assert(GetPrimitiveRestartIndexFromType<uint32_t>() == 0xFFFFFFFF,
94 "verify restart index for uint8_t values");
95
96 bool IsTriangleMode(PrimitiveMode drawMode);
97
98 namespace priv
99 {
100 extern const angle::PackedEnumMap<PrimitiveMode, bool> gLineModes;
101 } // namespace priv
102
IsLineMode(PrimitiveMode primitiveMode)103 ANGLE_INLINE bool IsLineMode(PrimitiveMode primitiveMode)
104 {
105 return priv::gLineModes[primitiveMode];
106 }
107
108 bool IsIntegerFormat(GLenum unsizedFormat);
109
110 // Returns the product of the sizes in the vector, or 1 if the vector is empty. Doesn't currently
111 // perform overflow checks.
112 unsigned int ArraySizeProduct(const std::vector<unsigned int> &arraySizes);
113
114 // Return the array index at the end of name, and write the length of name before the final array
115 // index into nameLengthWithoutArrayIndexOut. In case name doesn't include an array index, return
116 // GL_INVALID_INDEX and write the length of the original string.
117 unsigned int ParseArrayIndex(const std::string &name, size_t *nameLengthWithoutArrayIndexOut);
118
119 enum class SamplerFormat : uint8_t
120 {
121 Float = 0,
122 Unsigned = 1,
123 Signed = 2,
124 Shadow = 3,
125
126 InvalidEnum = 4,
127 EnumCount = 4,
128 };
129
130 struct UniformTypeInfo final : angle::NonCopyable
131 {
132 inline constexpr UniformTypeInfo(GLenum type,
133 GLenum componentType,
134 GLenum textureType,
135 GLenum transposedMatrixType,
136 GLenum boolVectorType,
137 SamplerFormat samplerFormat,
138 int rowCount,
139 int columnCount,
140 int componentCount,
141 size_t componentSize,
142 size_t internalSize,
143 size_t externalSize,
144 bool isSampler,
145 bool isMatrixType,
146 bool isImageType,
147 const char *glslAsFloat);
148
149 GLenum type;
150 GLenum componentType;
151 GLenum textureType;
152 GLenum transposedMatrixType;
153 GLenum boolVectorType;
154 SamplerFormat samplerFormat;
155 int rowCount;
156 int columnCount;
157 int componentCount;
158 size_t componentSize;
159 size_t internalSize;
160 size_t externalSize;
161 bool isSampler;
162 bool isMatrixType;
163 bool isImageType;
164 const char *glslAsFloat;
165 };
166
UniformTypeInfo(GLenum type,GLenum componentType,GLenum textureType,GLenum transposedMatrixType,GLenum boolVectorType,SamplerFormat samplerFormat,int rowCount,int columnCount,int componentCount,size_t componentSize,size_t internalSize,size_t externalSize,bool isSampler,bool isMatrixType,bool isImageType,const char * glslAsFloat)167 inline constexpr UniformTypeInfo::UniformTypeInfo(GLenum type,
168 GLenum componentType,
169 GLenum textureType,
170 GLenum transposedMatrixType,
171 GLenum boolVectorType,
172 SamplerFormat samplerFormat,
173 int rowCount,
174 int columnCount,
175 int componentCount,
176 size_t componentSize,
177 size_t internalSize,
178 size_t externalSize,
179 bool isSampler,
180 bool isMatrixType,
181 bool isImageType,
182 const char *glslAsFloat)
183 : type(type),
184 componentType(componentType),
185 textureType(textureType),
186 transposedMatrixType(transposedMatrixType),
187 boolVectorType(boolVectorType),
188 samplerFormat(samplerFormat),
189 rowCount(rowCount),
190 columnCount(columnCount),
191 componentCount(componentCount),
192 componentSize(componentSize),
193 internalSize(internalSize),
194 externalSize(externalSize),
195 isSampler(isSampler),
196 isMatrixType(isMatrixType),
197 isImageType(isImageType),
198 glslAsFloat(glslAsFloat)
199 {}
200
201 const UniformTypeInfo &GetUniformTypeInfo(GLenum uniformType);
202
203 const char *GetGenericErrorMessage(GLenum error);
204
205 unsigned int ElementTypeSize(GLenum elementType);
206
207 template <typename T>
GetClampedVertexCount(size_t vertexCount)208 T GetClampedVertexCount(size_t vertexCount)
209 {
210 static constexpr size_t kMax = static_cast<size_t>(std::numeric_limits<T>::max());
211 return static_cast<T>(vertexCount > kMax ? kMax : vertexCount);
212 }
213
214 enum class PipelineType
215 {
216 GraphicsPipeline = 0,
217 ComputePipeline = 1,
218 };
219
220 PipelineType GetPipelineType(ShaderType shaderType);
221 } // namespace gl
222
223 namespace egl
224 {
225 static const EGLenum FirstCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
226 static const EGLenum LastCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR;
227 bool IsCubeMapTextureTarget(EGLenum target);
228 size_t CubeMapTextureTargetToLayerIndex(EGLenum target);
229 EGLenum LayerIndexToCubeMapTextureTarget(size_t index);
230 bool IsTextureTarget(EGLenum target);
231 bool IsRenderbufferTarget(EGLenum target);
232 bool IsExternalImageTarget(EGLenum target);
233
234 const char *GetGenericErrorMessage(EGLint error);
235 } // namespace egl
236
237 namespace egl_gl
238 {
239 GLuint EGLClientBufferToGLObjectHandle(EGLClientBuffer buffer);
240 }
241
242 namespace gl_egl
243 {
244 EGLenum GLComponentTypeToEGLColorComponentType(GLenum glComponentType);
245 EGLClientBuffer GLObjectHandleToEGLClientBuffer(GLuint handle);
246 } // namespace gl_egl
247
248 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
249 std::string getTempPath();
250 void writeFile(const char *path, const void *data, size_t size);
251 #endif
252
253 #if defined(ANGLE_PLATFORM_WINDOWS)
254 void ScheduleYield();
255 #endif
256
257 #endif // COMMON_UTILITIES_H_
258