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