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