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 <math.h>
16 #include <string>
17 #include <vector>
18 #include "angle_gl.h"
19 
20 #include "common/mathutil.h"
21 
22 namespace gl
23 {
24 
25 int VariableComponentCount(GLenum type);
26 GLenum VariableComponentType(GLenum type);
27 size_t VariableComponentSize(GLenum type);
28 size_t VariableInternalSize(GLenum type);
29 size_t VariableExternalSize(GLenum type);
30 int VariableRowCount(GLenum type);
31 int VariableColumnCount(GLenum type);
32 bool IsSamplerType(GLenum type);
33 bool IsImageType(GLenum type);
34 bool IsAtomicCounterType(GLenum type);
35 bool IsOpaqueType(GLenum type);
36 GLenum SamplerTypeToTextureType(GLenum samplerType);
37 bool IsMatrixType(GLenum type);
38 GLenum TransposeMatrixType(GLenum type);
39 int VariableRegisterCount(GLenum type);
40 int MatrixRegisterCount(GLenum type, bool isRowMajorMatrix);
41 int MatrixComponentCount(GLenum type, bool isRowMajorMatrix);
42 int VariableSortOrder(GLenum type);
43 GLenum VariableBoolVectorType(GLenum type);
44 
45 int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsigned int bitsSize);
46 
47 static const GLenum FirstCubeMapTextureTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
48 static const GLenum LastCubeMapTextureTarget = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
49 bool IsCubeMapTextureTarget(GLenum target);
50 size_t CubeMapTextureTargetToLayerIndex(GLenum target);
51 GLenum LayerIndexToCubeMapTextureTarget(size_t index);
52 
53 // Parse the base resource name and array indices. Returns the base name of the resource.
54 // If the provided name doesn't index an array, the outSubscripts vector will be empty.
55 // If the provided name indexes an array, the outSubscripts vector will contain indices with
56 // outermost array indices in the back. If an array index is invalid, GL_INVALID_INDEX is added to
57 // outSubscripts.
58 std::string ParseResourceName(const std::string &name, std::vector<unsigned int> *outSubscripts);
59 
60 // Find the range of index values in the provided indices pointer.  Primitive restart indices are
61 // only counted in the range if primitive restart is disabled.
62 IndexRange ComputeIndexRange(GLenum indexType,
63                              const GLvoid *indices,
64                              size_t count,
65                              bool primitiveRestartEnabled);
66 
67 // Get the primitive restart index value for the given index type.
68 GLuint GetPrimitiveRestartIndex(GLenum indexType);
69 
70 bool IsTriangleMode(GLenum drawMode);
71 bool IsIntegerFormat(GLenum unsizedFormat);
72 
73 // Returns the product of the sizes in the vector, or 1 if the vector is empty. Doesn't currently
74 // perform overflow checks.
75 unsigned int ArraySizeProduct(const std::vector<unsigned int> &arraySizes);
76 
77 // Return the array index at the end of name, and write the length of name before the final array
78 // index into nameLengthWithoutArrayIndexOut. In case name doesn't include an array index, return
79 // GL_INVALID_INDEX and write the length of the original string.
80 unsigned int ParseArrayIndex(const std::string &name, size_t *nameLengthWithoutArrayIndexOut);
81 
82 struct UniformTypeInfo final : angle::NonCopyable
83 {
UniformTypeInfofinal84     constexpr UniformTypeInfo(GLenum type,
85                               GLenum componentType,
86                               GLenum samplerTextureType,
87                               GLenum transposedMatrixType,
88                               GLenum boolVectorType,
89                               int rowCount,
90                               int columnCount,
91                               int componentCount,
92                               size_t componentSize,
93                               size_t internalSize,
94                               size_t externalSize,
95                               bool isSampler,
96                               bool isMatrixType,
97                               bool isImageType)
98         : type(type),
99           componentType(componentType),
100           samplerTextureType(samplerTextureType),
101           transposedMatrixType(transposedMatrixType),
102           boolVectorType(boolVectorType),
103           rowCount(rowCount),
104           columnCount(columnCount),
105           componentCount(componentCount),
106           componentSize(componentSize),
107           internalSize(internalSize),
108           externalSize(externalSize),
109           isSampler(isSampler),
110           isMatrixType(isMatrixType),
111           isImageType(isImageType)
112     {
113     }
114 
115     GLenum type;
116     GLenum componentType;
117     GLenum samplerTextureType;
118     GLenum transposedMatrixType;
119     GLenum boolVectorType;
120     int rowCount;
121     int columnCount;
122     int componentCount;
123     size_t componentSize;
124     size_t internalSize;
125     size_t externalSize;
126     bool isSampler;
127     bool isMatrixType;
128     bool isImageType;
129 };
130 
131 const UniformTypeInfo &GetUniformTypeInfo(GLenum uniformType);
132 
133 const char *GetGenericErrorMessage(GLenum error);
134 
135 unsigned int ElementTypeSize(GLenum elementType);
136 
137 }  // namespace gl
138 
139 namespace egl
140 {
141 static const EGLenum FirstCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
142 static const EGLenum LastCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR;
143 bool IsCubeMapTextureTarget(EGLenum target);
144 size_t CubeMapTextureTargetToLayerIndex(EGLenum target);
145 EGLenum LayerIndexToCubeMapTextureTarget(size_t index);
146 bool IsTextureTarget(EGLenum target);
147 bool IsRenderbufferTarget(EGLenum target);
148 
149 const char *GetGenericErrorMessage(EGLint error);
150 }  // namespace egl
151 
152 namespace egl_gl
153 {
154 GLenum EGLCubeMapTargetToGLCubeMapTarget(EGLenum eglTarget);
155 GLenum EGLImageTargetToGLTextureTarget(EGLenum eglTarget);
156 GLuint EGLClientBufferToGLObjectHandle(EGLClientBuffer buffer);
157 }
158 
159 namespace gl_egl
160 {
161 EGLenum GLComponentTypeToEGLColorComponentType(GLenum glComponentType);
162 }  // namespace gl_egl
163 
164 #if !defined(ANGLE_ENABLE_WINDOWS_STORE)
165 std::string getTempPath();
166 void writeFile(const char* path, const void* data, size_t size);
167 #endif
168 
169 #if defined (ANGLE_PLATFORM_WINDOWS)
170 void ScheduleYield();
171 #endif
172 
173 #endif  // COMMON_UTILITIES_H_
174