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