xref: /aosp_15_r20/external/angle/src/common/utilities.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2002 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker 
7*8975f5c5SAndroid Build Coastguard Worker // utilities.h: Conversion functions and other utility routines.
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker #ifndef COMMON_UTILITIES_H_
10*8975f5c5SAndroid Build Coastguard Worker #define COMMON_UTILITIES_H_
11*8975f5c5SAndroid Build Coastguard Worker 
12*8975f5c5SAndroid Build Coastguard Worker #include <EGL/egl.h>
13*8975f5c5SAndroid Build Coastguard Worker #include <EGL/eglext.h>
14*8975f5c5SAndroid Build Coastguard Worker #include <GLSLANG/ShaderLang.h>
15*8975f5c5SAndroid Build Coastguard Worker 
16*8975f5c5SAndroid Build Coastguard Worker #include <math.h>
17*8975f5c5SAndroid Build Coastguard Worker #include <string>
18*8975f5c5SAndroid Build Coastguard Worker #include <vector>
19*8975f5c5SAndroid Build Coastguard Worker 
20*8975f5c5SAndroid Build Coastguard Worker #include "angle_gl.h"
21*8975f5c5SAndroid Build Coastguard Worker 
22*8975f5c5SAndroid Build Coastguard Worker #include "common/PackedEnums.h"
23*8975f5c5SAndroid Build Coastguard Worker #include "common/mathutil.h"
24*8975f5c5SAndroid Build Coastguard Worker #include "common/platform.h"
25*8975f5c5SAndroid Build Coastguard Worker 
26*8975f5c5SAndroid Build Coastguard Worker namespace sh
27*8975f5c5SAndroid Build Coastguard Worker {
28*8975f5c5SAndroid Build Coastguard Worker struct ShaderVariable;
29*8975f5c5SAndroid Build Coastguard Worker }
30*8975f5c5SAndroid Build Coastguard Worker 
31*8975f5c5SAndroid Build Coastguard Worker namespace gl
32*8975f5c5SAndroid Build Coastguard Worker {
33*8975f5c5SAndroid Build Coastguard Worker 
34*8975f5c5SAndroid Build Coastguard Worker int VariableComponentCount(GLenum type);
35*8975f5c5SAndroid Build Coastguard Worker GLenum VariableComponentType(GLenum type);
36*8975f5c5SAndroid Build Coastguard Worker size_t VariableComponentSize(GLenum type);
37*8975f5c5SAndroid Build Coastguard Worker size_t VariableInternalSize(GLenum type);
38*8975f5c5SAndroid Build Coastguard Worker size_t VariableExternalSize(GLenum type);
39*8975f5c5SAndroid Build Coastguard Worker int VariableRowCount(GLenum type);
40*8975f5c5SAndroid Build Coastguard Worker int VariableColumnCount(GLenum type);
41*8975f5c5SAndroid Build Coastguard Worker bool IsSamplerType(GLenum type);
42*8975f5c5SAndroid Build Coastguard Worker bool IsSamplerCubeType(GLenum type);
43*8975f5c5SAndroid Build Coastguard Worker bool IsSamplerYUVType(GLenum type);
44*8975f5c5SAndroid Build Coastguard Worker bool IsImageType(GLenum type);
45*8975f5c5SAndroid Build Coastguard Worker bool IsImage2DType(GLenum type);
46*8975f5c5SAndroid Build Coastguard Worker bool IsAtomicCounterType(GLenum type);
47*8975f5c5SAndroid Build Coastguard Worker bool IsOpaqueType(GLenum type);
48*8975f5c5SAndroid Build Coastguard Worker bool IsMatrixType(GLenum type);
49*8975f5c5SAndroid Build Coastguard Worker GLenum TransposeMatrixType(GLenum type);
50*8975f5c5SAndroid Build Coastguard Worker int VariableRegisterCount(GLenum type);
51*8975f5c5SAndroid Build Coastguard Worker int MatrixRegisterCount(GLenum type, bool isRowMajorMatrix);
52*8975f5c5SAndroid Build Coastguard Worker int MatrixComponentCount(GLenum type, bool isRowMajorMatrix);
53*8975f5c5SAndroid Build Coastguard Worker int VariableSortOrder(GLenum type);
54*8975f5c5SAndroid Build Coastguard Worker GLenum VariableBoolVectorType(GLenum type);
55*8975f5c5SAndroid Build Coastguard Worker std::string GetGLSLTypeString(GLenum type);
56*8975f5c5SAndroid Build Coastguard Worker 
57*8975f5c5SAndroid Build Coastguard Worker int AllocateFirstFreeBits(unsigned int *bits, unsigned int allocationSize, unsigned int bitsSize);
58*8975f5c5SAndroid Build Coastguard Worker 
59*8975f5c5SAndroid Build Coastguard Worker // Parse the base resource name and array indices. Returns the base name of the resource.
60*8975f5c5SAndroid Build Coastguard Worker // If the provided name doesn't index an array, the outSubscripts vector will be empty.
61*8975f5c5SAndroid Build Coastguard Worker // If the provided name indexes an array, the outSubscripts vector will contain indices with
62*8975f5c5SAndroid Build Coastguard Worker // outermost array indices in the back. If an array index is invalid, GL_INVALID_INDEX is added to
63*8975f5c5SAndroid Build Coastguard Worker // outSubscripts.
64*8975f5c5SAndroid Build Coastguard Worker std::string ParseResourceName(const std::string &name, std::vector<unsigned int> *outSubscripts);
65*8975f5c5SAndroid Build Coastguard Worker 
66*8975f5c5SAndroid Build Coastguard Worker bool IsBuiltInName(const char *name);
IsBuiltInName(const std::string & name)67*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool IsBuiltInName(const std::string &name)
68*8975f5c5SAndroid Build Coastguard Worker {
69*8975f5c5SAndroid Build Coastguard Worker     return IsBuiltInName(name.c_str());
70*8975f5c5SAndroid Build Coastguard Worker }
71*8975f5c5SAndroid Build Coastguard Worker 
72*8975f5c5SAndroid Build Coastguard Worker // Strips only the last array index from a resource name.
73*8975f5c5SAndroid Build Coastguard Worker std::string StripLastArrayIndex(const std::string &name);
74*8975f5c5SAndroid Build Coastguard Worker 
75*8975f5c5SAndroid Build Coastguard Worker bool SamplerNameContainsNonZeroArrayElement(const std::string &name);
76*8975f5c5SAndroid Build Coastguard Worker 
77*8975f5c5SAndroid Build Coastguard Worker // Find the range of index values in the provided indices pointer.  Primitive restart indices are
78*8975f5c5SAndroid Build Coastguard Worker // only counted in the range if primitive restart is disabled.
79*8975f5c5SAndroid Build Coastguard Worker IndexRange ComputeIndexRange(DrawElementsType indexType,
80*8975f5c5SAndroid Build Coastguard Worker                              const GLvoid *indices,
81*8975f5c5SAndroid Build Coastguard Worker                              size_t count,
82*8975f5c5SAndroid Build Coastguard Worker                              bool primitiveRestartEnabled);
83*8975f5c5SAndroid Build Coastguard Worker 
84*8975f5c5SAndroid Build Coastguard Worker // Get the primitive restart index value for the given index type.
85*8975f5c5SAndroid Build Coastguard Worker GLuint GetPrimitiveRestartIndex(DrawElementsType indexType);
86*8975f5c5SAndroid Build Coastguard Worker 
87*8975f5c5SAndroid Build Coastguard Worker // Get the primitive restart index value with the given C++ type.
88*8975f5c5SAndroid Build Coastguard Worker template <typename T>
GetPrimitiveRestartIndexFromType()89*8975f5c5SAndroid Build Coastguard Worker constexpr T GetPrimitiveRestartIndexFromType()
90*8975f5c5SAndroid Build Coastguard Worker {
91*8975f5c5SAndroid Build Coastguard Worker     return std::numeric_limits<T>::max();
92*8975f5c5SAndroid Build Coastguard Worker }
93*8975f5c5SAndroid Build Coastguard Worker 
94*8975f5c5SAndroid Build Coastguard Worker static_assert(GetPrimitiveRestartIndexFromType<uint8_t>() == 0xFF,
95*8975f5c5SAndroid Build Coastguard Worker               "verify restart index for uint8_t values");
96*8975f5c5SAndroid Build Coastguard Worker static_assert(GetPrimitiveRestartIndexFromType<uint16_t>() == 0xFFFF,
97*8975f5c5SAndroid Build Coastguard Worker               "verify restart index for uint8_t values");
98*8975f5c5SAndroid Build Coastguard Worker static_assert(GetPrimitiveRestartIndexFromType<uint32_t>() == 0xFFFFFFFF,
99*8975f5c5SAndroid Build Coastguard Worker               "verify restart index for uint8_t values");
100*8975f5c5SAndroid Build Coastguard Worker 
101*8975f5c5SAndroid Build Coastguard Worker bool IsTriangleMode(PrimitiveMode drawMode);
102*8975f5c5SAndroid Build Coastguard Worker bool IsPolygonMode(PrimitiveMode mode);
103*8975f5c5SAndroid Build Coastguard Worker 
104*8975f5c5SAndroid Build Coastguard Worker namespace priv
105*8975f5c5SAndroid Build Coastguard Worker {
106*8975f5c5SAndroid Build Coastguard Worker extern const angle::PackedEnumMap<PrimitiveMode, bool> gLineModes;
107*8975f5c5SAndroid Build Coastguard Worker }  // namespace priv
108*8975f5c5SAndroid Build Coastguard Worker 
IsLineMode(PrimitiveMode primitiveMode)109*8975f5c5SAndroid Build Coastguard Worker ANGLE_INLINE bool IsLineMode(PrimitiveMode primitiveMode)
110*8975f5c5SAndroid Build Coastguard Worker {
111*8975f5c5SAndroid Build Coastguard Worker     return priv::gLineModes[primitiveMode];
112*8975f5c5SAndroid Build Coastguard Worker }
113*8975f5c5SAndroid Build Coastguard Worker 
114*8975f5c5SAndroid Build Coastguard Worker bool IsIntegerFormat(GLenum unsizedFormat);
115*8975f5c5SAndroid Build Coastguard Worker 
116*8975f5c5SAndroid Build Coastguard Worker // Returns the product of the sizes in the vector, or 1 if the vector is empty. Doesn't currently
117*8975f5c5SAndroid Build Coastguard Worker // perform overflow checks.
118*8975f5c5SAndroid Build Coastguard Worker unsigned int ArraySizeProduct(const std::vector<unsigned int> &arraySizes);
119*8975f5c5SAndroid Build Coastguard Worker // Returns the product of the sizes in the vector except for the outermost dimension, or 1 if the
120*8975f5c5SAndroid Build Coastguard Worker // vector is empty.
121*8975f5c5SAndroid Build Coastguard Worker unsigned int InnerArraySizeProduct(const std::vector<unsigned int> &arraySizes);
122*8975f5c5SAndroid Build Coastguard Worker // Returns the outermost array dimension, or 1 if the vector is empty.
123*8975f5c5SAndroid Build Coastguard Worker unsigned int OutermostArraySize(const std::vector<unsigned int> &arraySizes);
124*8975f5c5SAndroid Build Coastguard Worker 
125*8975f5c5SAndroid Build Coastguard Worker // Return the array index at the end of name, and write the length of name before the final array
126*8975f5c5SAndroid Build Coastguard Worker // index into nameLengthWithoutArrayIndexOut. In case name doesn't include an array index, return
127*8975f5c5SAndroid Build Coastguard Worker // GL_INVALID_INDEX and write the length of the original string.
128*8975f5c5SAndroid Build Coastguard Worker unsigned int ParseArrayIndex(const std::string &name, size_t *nameLengthWithoutArrayIndexOut);
129*8975f5c5SAndroid Build Coastguard Worker 
130*8975f5c5SAndroid Build Coastguard Worker enum class SamplerFormat : uint8_t
131*8975f5c5SAndroid Build Coastguard Worker {
132*8975f5c5SAndroid Build Coastguard Worker     Float    = 0,
133*8975f5c5SAndroid Build Coastguard Worker     Unsigned = 1,
134*8975f5c5SAndroid Build Coastguard Worker     Signed   = 2,
135*8975f5c5SAndroid Build Coastguard Worker     Shadow   = 3,
136*8975f5c5SAndroid Build Coastguard Worker 
137*8975f5c5SAndroid Build Coastguard Worker     InvalidEnum = 4,
138*8975f5c5SAndroid Build Coastguard Worker     EnumCount   = 4,
139*8975f5c5SAndroid Build Coastguard Worker };
140*8975f5c5SAndroid Build Coastguard Worker 
141*8975f5c5SAndroid Build Coastguard Worker struct UniformTypeInfo final : angle::NonCopyable
142*8975f5c5SAndroid Build Coastguard Worker {
143*8975f5c5SAndroid Build Coastguard Worker     inline constexpr UniformTypeInfo(GLenum type,
144*8975f5c5SAndroid Build Coastguard Worker                                      GLenum componentType,
145*8975f5c5SAndroid Build Coastguard Worker                                      GLenum textureType,
146*8975f5c5SAndroid Build Coastguard Worker                                      GLenum transposedMatrixType,
147*8975f5c5SAndroid Build Coastguard Worker                                      GLenum boolVectorType,
148*8975f5c5SAndroid Build Coastguard Worker                                      SamplerFormat samplerFormat,
149*8975f5c5SAndroid Build Coastguard Worker                                      int rowCount,
150*8975f5c5SAndroid Build Coastguard Worker                                      int columnCount,
151*8975f5c5SAndroid Build Coastguard Worker                                      int componentCount,
152*8975f5c5SAndroid Build Coastguard Worker                                      size_t componentSize,
153*8975f5c5SAndroid Build Coastguard Worker                                      size_t internalSize,
154*8975f5c5SAndroid Build Coastguard Worker                                      size_t externalSize,
155*8975f5c5SAndroid Build Coastguard Worker                                      bool isSampler,
156*8975f5c5SAndroid Build Coastguard Worker                                      bool isMatrixType,
157*8975f5c5SAndroid Build Coastguard Worker                                      bool isImageType);
158*8975f5c5SAndroid Build Coastguard Worker 
159*8975f5c5SAndroid Build Coastguard Worker     GLenum type;
160*8975f5c5SAndroid Build Coastguard Worker     GLenum componentType;
161*8975f5c5SAndroid Build Coastguard Worker     GLenum textureType;
162*8975f5c5SAndroid Build Coastguard Worker     GLenum transposedMatrixType;
163*8975f5c5SAndroid Build Coastguard Worker     GLenum boolVectorType;
164*8975f5c5SAndroid Build Coastguard Worker     SamplerFormat samplerFormat;
165*8975f5c5SAndroid Build Coastguard Worker     int rowCount;
166*8975f5c5SAndroid Build Coastguard Worker     int columnCount;
167*8975f5c5SAndroid Build Coastguard Worker     int componentCount;
168*8975f5c5SAndroid Build Coastguard Worker     size_t componentSize;
169*8975f5c5SAndroid Build Coastguard Worker     size_t internalSize;
170*8975f5c5SAndroid Build Coastguard Worker     size_t externalSize;
171*8975f5c5SAndroid Build Coastguard Worker     bool isSampler;
172*8975f5c5SAndroid Build Coastguard Worker     bool isMatrixType;
173*8975f5c5SAndroid Build Coastguard Worker     bool isImageType;
174*8975f5c5SAndroid Build Coastguard Worker };
175*8975f5c5SAndroid Build Coastguard Worker 
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)176*8975f5c5SAndroid Build Coastguard Worker inline constexpr UniformTypeInfo::UniformTypeInfo(GLenum type,
177*8975f5c5SAndroid Build Coastguard Worker                                                   GLenum componentType,
178*8975f5c5SAndroid Build Coastguard Worker                                                   GLenum textureType,
179*8975f5c5SAndroid Build Coastguard Worker                                                   GLenum transposedMatrixType,
180*8975f5c5SAndroid Build Coastguard Worker                                                   GLenum boolVectorType,
181*8975f5c5SAndroid Build Coastguard Worker                                                   SamplerFormat samplerFormat,
182*8975f5c5SAndroid Build Coastguard Worker                                                   int rowCount,
183*8975f5c5SAndroid Build Coastguard Worker                                                   int columnCount,
184*8975f5c5SAndroid Build Coastguard Worker                                                   int componentCount,
185*8975f5c5SAndroid Build Coastguard Worker                                                   size_t componentSize,
186*8975f5c5SAndroid Build Coastguard Worker                                                   size_t internalSize,
187*8975f5c5SAndroid Build Coastguard Worker                                                   size_t externalSize,
188*8975f5c5SAndroid Build Coastguard Worker                                                   bool isSampler,
189*8975f5c5SAndroid Build Coastguard Worker                                                   bool isMatrixType,
190*8975f5c5SAndroid Build Coastguard Worker                                                   bool isImageType)
191*8975f5c5SAndroid Build Coastguard Worker     : type(type),
192*8975f5c5SAndroid Build Coastguard Worker       componentType(componentType),
193*8975f5c5SAndroid Build Coastguard Worker       textureType(textureType),
194*8975f5c5SAndroid Build Coastguard Worker       transposedMatrixType(transposedMatrixType),
195*8975f5c5SAndroid Build Coastguard Worker       boolVectorType(boolVectorType),
196*8975f5c5SAndroid Build Coastguard Worker       samplerFormat(samplerFormat),
197*8975f5c5SAndroid Build Coastguard Worker       rowCount(rowCount),
198*8975f5c5SAndroid Build Coastguard Worker       columnCount(columnCount),
199*8975f5c5SAndroid Build Coastguard Worker       componentCount(componentCount),
200*8975f5c5SAndroid Build Coastguard Worker       componentSize(componentSize),
201*8975f5c5SAndroid Build Coastguard Worker       internalSize(internalSize),
202*8975f5c5SAndroid Build Coastguard Worker       externalSize(externalSize),
203*8975f5c5SAndroid Build Coastguard Worker       isSampler(isSampler),
204*8975f5c5SAndroid Build Coastguard Worker       isMatrixType(isMatrixType),
205*8975f5c5SAndroid Build Coastguard Worker       isImageType(isImageType)
206*8975f5c5SAndroid Build Coastguard Worker {}
207*8975f5c5SAndroid Build Coastguard Worker 
208*8975f5c5SAndroid Build Coastguard Worker struct UniformTypeIndex
209*8975f5c5SAndroid Build Coastguard Worker {
210*8975f5c5SAndroid Build Coastguard Worker     uint16_t value;
211*8975f5c5SAndroid Build Coastguard Worker };
212*8975f5c5SAndroid Build Coastguard Worker const UniformTypeInfo &GetUniformTypeInfo(GLenum uniformType);
213*8975f5c5SAndroid Build Coastguard Worker UniformTypeIndex GetUniformTypeIndex(GLenum uniformType);
214*8975f5c5SAndroid Build Coastguard Worker const UniformTypeInfo &GetUniformTypeInfoFromIndex(UniformTypeIndex index);
215*8975f5c5SAndroid Build Coastguard Worker 
216*8975f5c5SAndroid Build Coastguard Worker const char *GetGenericErrorMessage(GLenum error);
217*8975f5c5SAndroid Build Coastguard Worker 
218*8975f5c5SAndroid Build Coastguard Worker unsigned int ElementTypeSize(GLenum elementType);
219*8975f5c5SAndroid Build Coastguard Worker 
220*8975f5c5SAndroid Build Coastguard Worker bool IsMipmapFiltered(GLenum minFilterMode);
221*8975f5c5SAndroid Build Coastguard Worker 
222*8975f5c5SAndroid Build Coastguard Worker template <typename T>
GetClampedVertexCount(size_t vertexCount)223*8975f5c5SAndroid Build Coastguard Worker T GetClampedVertexCount(size_t vertexCount)
224*8975f5c5SAndroid Build Coastguard Worker {
225*8975f5c5SAndroid Build Coastguard Worker     static constexpr size_t kMax = static_cast<size_t>(std::numeric_limits<T>::max());
226*8975f5c5SAndroid Build Coastguard Worker     return static_cast<T>(vertexCount > kMax ? kMax : vertexCount);
227*8975f5c5SAndroid Build Coastguard Worker }
228*8975f5c5SAndroid Build Coastguard Worker 
229*8975f5c5SAndroid Build Coastguard Worker enum class PipelineType
230*8975f5c5SAndroid Build Coastguard Worker {
231*8975f5c5SAndroid Build Coastguard Worker     GraphicsPipeline = 0,
232*8975f5c5SAndroid Build Coastguard Worker     ComputePipeline  = 1,
233*8975f5c5SAndroid Build Coastguard Worker };
234*8975f5c5SAndroid Build Coastguard Worker 
235*8975f5c5SAndroid Build Coastguard Worker PipelineType GetPipelineType(ShaderType shaderType);
236*8975f5c5SAndroid Build Coastguard Worker 
237*8975f5c5SAndroid Build Coastguard Worker // For use with KHR_debug.
238*8975f5c5SAndroid Build Coastguard Worker const char *GetDebugMessageSourceString(GLenum source);
239*8975f5c5SAndroid Build Coastguard Worker const char *GetDebugMessageTypeString(GLenum type);
240*8975f5c5SAndroid Build Coastguard Worker const char *GetDebugMessageSeverityString(GLenum severity);
241*8975f5c5SAndroid Build Coastguard Worker 
242*8975f5c5SAndroid Build Coastguard Worker // For use with EXT_texture_sRGB_decode
243*8975f5c5SAndroid Build Coastguard Worker // A texture may be forced to skip decoding to a linear colorspace even if its format
244*8975f5c5SAndroid Build Coastguard Worker // is in sRGB colorspace.
245*8975f5c5SAndroid Build Coastguard Worker //
246*8975f5c5SAndroid Build Coastguard Worker // Default - decode data according to the image's format's colorspace
247*8975f5c5SAndroid Build Coastguard Worker // Skip - data is not decoded during sampling
248*8975f5c5SAndroid Build Coastguard Worker enum class SrgbDecode
249*8975f5c5SAndroid Build Coastguard Worker {
250*8975f5c5SAndroid Build Coastguard Worker     Default = 0,
251*8975f5c5SAndroid Build Coastguard Worker     Skip
252*8975f5c5SAndroid Build Coastguard Worker };
253*8975f5c5SAndroid Build Coastguard Worker 
254*8975f5c5SAndroid Build Coastguard Worker // For use with EXT_texture_format_sRGB_override
255*8975f5c5SAndroid Build Coastguard Worker // A texture may be forced to decode data to linear colorspace even if its format
256*8975f5c5SAndroid Build Coastguard Worker // is in linear colorspace.
257*8975f5c5SAndroid Build Coastguard Worker //
258*8975f5c5SAndroid Build Coastguard Worker // Default - decode data according to the image's format's colorspace
259*8975f5c5SAndroid Build Coastguard Worker // SRGB - data will be decoded to linear colorspace irrespective of texture's format
260*8975f5c5SAndroid Build Coastguard Worker enum class SrgbOverride
261*8975f5c5SAndroid Build Coastguard Worker {
262*8975f5c5SAndroid Build Coastguard Worker     Default = 0,
263*8975f5c5SAndroid Build Coastguard Worker     SRGB
264*8975f5c5SAndroid Build Coastguard Worker };
265*8975f5c5SAndroid Build Coastguard Worker 
266*8975f5c5SAndroid Build Coastguard Worker // For use with EXT_sRGB_write_control
267*8975f5c5SAndroid Build Coastguard Worker // A framebuffer may be forced to not encode data to sRGB colorspace even if its format
268*8975f5c5SAndroid Build Coastguard Worker // is in sRGB colorspace.
269*8975f5c5SAndroid Build Coastguard Worker //
270*8975f5c5SAndroid Build Coastguard Worker // Default - encode data according to the image's format's colorspace
271*8975f5c5SAndroid Build Coastguard Worker // Linear - data will not be encoded into sRGB colorspace
272*8975f5c5SAndroid Build Coastguard Worker enum class SrgbWriteControlMode
273*8975f5c5SAndroid Build Coastguard Worker {
274*8975f5c5SAndroid Build Coastguard Worker     Default = 0,
275*8975f5c5SAndroid Build Coastguard Worker     Linear  = 1
276*8975f5c5SAndroid Build Coastguard Worker };
277*8975f5c5SAndroid Build Coastguard Worker 
278*8975f5c5SAndroid Build Coastguard Worker // For use with EXT_YUV_target
279*8975f5c5SAndroid Build Coastguard Worker // A sampler of external YUV textures may either implicitly perform RGB conversion (regular
280*8975f5c5SAndroid Build Coastguard Worker // samplerExternalOES) or skip the conversion and sample raw YUV values (__samplerExternal2DY2Y).
281*8975f5c5SAndroid Build Coastguard Worker enum class YuvSamplingMode
282*8975f5c5SAndroid Build Coastguard Worker {
283*8975f5c5SAndroid Build Coastguard Worker     Default = 0,
284*8975f5c5SAndroid Build Coastguard Worker     Y2Y     = 1
285*8975f5c5SAndroid Build Coastguard Worker };
286*8975f5c5SAndroid Build Coastguard Worker 
287*8975f5c5SAndroid Build Coastguard Worker ShaderType GetShaderTypeFromBitfield(size_t singleShaderType);
288*8975f5c5SAndroid Build Coastguard Worker GLbitfield GetBitfieldFromShaderType(ShaderType shaderType);
289*8975f5c5SAndroid Build Coastguard Worker bool ShaderTypeSupportsTransformFeedback(ShaderType shaderType);
290*8975f5c5SAndroid Build Coastguard Worker // Given a set of shader stages, returns the last vertex processing stage.  This is the stage that
291*8975f5c5SAndroid Build Coastguard Worker // interfaces the fragment shader.
292*8975f5c5SAndroid Build Coastguard Worker ShaderType GetLastPreFragmentStage(ShaderBitSet shaderTypes);
293*8975f5c5SAndroid Build Coastguard Worker 
294*8975f5c5SAndroid Build Coastguard Worker }  // namespace gl
295*8975f5c5SAndroid Build Coastguard Worker 
296*8975f5c5SAndroid Build Coastguard Worker namespace egl
297*8975f5c5SAndroid Build Coastguard Worker {
298*8975f5c5SAndroid Build Coastguard Worker // For use with EGL_EXT_image_gl_colorspace
299*8975f5c5SAndroid Build Coastguard Worker // An EGLImage can be created with attributes that override the color space of underlying image data
300*8975f5c5SAndroid Build Coastguard Worker // when rendering to the image, or sampling from the image. The possible values are -
301*8975f5c5SAndroid Build Coastguard Worker //  Default - EGLImage source's colorspace should be preserved
302*8975f5c5SAndroid Build Coastguard Worker //  sRGB - EGLImage targets will assume sRGB colorspace
303*8975f5c5SAndroid Build Coastguard Worker //  Linear - EGLImage targets will assume linear colorspace
304*8975f5c5SAndroid Build Coastguard Worker enum class ImageColorspace
305*8975f5c5SAndroid Build Coastguard Worker {
306*8975f5c5SAndroid Build Coastguard Worker     Default = 0,
307*8975f5c5SAndroid Build Coastguard Worker     SRGB,
308*8975f5c5SAndroid Build Coastguard Worker     Linear
309*8975f5c5SAndroid Build Coastguard Worker };
310*8975f5c5SAndroid Build Coastguard Worker 
311*8975f5c5SAndroid Build Coastguard Worker static const EGLenum FirstCubeMapTextureTarget = EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
312*8975f5c5SAndroid Build Coastguard Worker static const EGLenum LastCubeMapTextureTarget  = EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR;
313*8975f5c5SAndroid Build Coastguard Worker bool IsCubeMapTextureTarget(EGLenum target);
314*8975f5c5SAndroid Build Coastguard Worker size_t CubeMapTextureTargetToLayerIndex(EGLenum target);
315*8975f5c5SAndroid Build Coastguard Worker EGLenum LayerIndexToCubeMapTextureTarget(size_t index);
316*8975f5c5SAndroid Build Coastguard Worker bool IsTextureTarget(EGLenum target);
317*8975f5c5SAndroid Build Coastguard Worker bool IsRenderbufferTarget(EGLenum target);
318*8975f5c5SAndroid Build Coastguard Worker bool IsExternalImageTarget(EGLenum target);
319*8975f5c5SAndroid Build Coastguard Worker 
320*8975f5c5SAndroid Build Coastguard Worker const char *GetGenericErrorMessage(EGLint error);
321*8975f5c5SAndroid Build Coastguard Worker }  // namespace egl
322*8975f5c5SAndroid Build Coastguard Worker 
323*8975f5c5SAndroid Build Coastguard Worker namespace egl_gl
324*8975f5c5SAndroid Build Coastguard Worker {
325*8975f5c5SAndroid Build Coastguard Worker GLuint EGLClientBufferToGLObjectHandle(EGLClientBuffer buffer);
326*8975f5c5SAndroid Build Coastguard Worker }
327*8975f5c5SAndroid Build Coastguard Worker 
328*8975f5c5SAndroid Build Coastguard Worker namespace gl_egl
329*8975f5c5SAndroid Build Coastguard Worker {
330*8975f5c5SAndroid Build Coastguard Worker EGLenum GLComponentTypeToEGLColorComponentType(GLenum glComponentType);
331*8975f5c5SAndroid Build Coastguard Worker EGLClientBuffer GLObjectHandleToEGLClientBuffer(GLuint handle);
332*8975f5c5SAndroid Build Coastguard Worker }  // namespace gl_egl
333*8975f5c5SAndroid Build Coastguard Worker 
334*8975f5c5SAndroid Build Coastguard Worker namespace angle
335*8975f5c5SAndroid Build Coastguard Worker {
336*8975f5c5SAndroid Build Coastguard Worker 
337*8975f5c5SAndroid Build Coastguard Worker // All state that modify attachment's colorspace
338*8975f5c5SAndroid Build Coastguard Worker struct ColorspaceState
339*8975f5c5SAndroid Build Coastguard Worker {
340*8975f5c5SAndroid Build Coastguard Worker   public:
ColorspaceStateColorspaceState341*8975f5c5SAndroid Build Coastguard Worker     ColorspaceState() { reset(); }
resetColorspaceState342*8975f5c5SAndroid Build Coastguard Worker     void reset()
343*8975f5c5SAndroid Build Coastguard Worker     {
344*8975f5c5SAndroid Build Coastguard Worker         hasStaticTexelFetchAccess = false;
345*8975f5c5SAndroid Build Coastguard Worker         srgbDecode                = gl::SrgbDecode::Default;
346*8975f5c5SAndroid Build Coastguard Worker         srgbOverride              = gl::SrgbOverride::Default;
347*8975f5c5SAndroid Build Coastguard Worker         srgbWriteControl          = gl::SrgbWriteControlMode::Default;
348*8975f5c5SAndroid Build Coastguard Worker         eglImageColorspace        = egl::ImageColorspace::Default;
349*8975f5c5SAndroid Build Coastguard Worker     }
350*8975f5c5SAndroid Build Coastguard Worker 
351*8975f5c5SAndroid Build Coastguard Worker     // States that affect read operations
352*8975f5c5SAndroid Build Coastguard Worker     bool hasStaticTexelFetchAccess;
353*8975f5c5SAndroid Build Coastguard Worker     gl::SrgbDecode srgbDecode;
354*8975f5c5SAndroid Build Coastguard Worker     gl::SrgbOverride srgbOverride;
355*8975f5c5SAndroid Build Coastguard Worker 
356*8975f5c5SAndroid Build Coastguard Worker     // States that affect write operations
357*8975f5c5SAndroid Build Coastguard Worker     gl::SrgbWriteControlMode srgbWriteControl;
358*8975f5c5SAndroid Build Coastguard Worker 
359*8975f5c5SAndroid Build Coastguard Worker     // States that affect both read and write operations
360*8975f5c5SAndroid Build Coastguard Worker     egl::ImageColorspace eglImageColorspace;
361*8975f5c5SAndroid Build Coastguard Worker };
362*8975f5c5SAndroid Build Coastguard Worker 
363*8975f5c5SAndroid Build Coastguard Worker template <typename T>
ConstStrLen(T s)364*8975f5c5SAndroid Build Coastguard Worker constexpr size_t ConstStrLen(T s)
365*8975f5c5SAndroid Build Coastguard Worker {
366*8975f5c5SAndroid Build Coastguard Worker     if (s == nullptr)
367*8975f5c5SAndroid Build Coastguard Worker     {
368*8975f5c5SAndroid Build Coastguard Worker         return 0;
369*8975f5c5SAndroid Build Coastguard Worker     }
370*8975f5c5SAndroid Build Coastguard Worker     return std::char_traits<char>::length(s);
371*8975f5c5SAndroid Build Coastguard Worker }
372*8975f5c5SAndroid Build Coastguard Worker 
373*8975f5c5SAndroid Build Coastguard Worker bool IsDrawEntryPoint(EntryPoint entryPoint);
374*8975f5c5SAndroid Build Coastguard Worker bool IsDispatchEntryPoint(EntryPoint entryPoint);
375*8975f5c5SAndroid Build Coastguard Worker bool IsClearEntryPoint(EntryPoint entryPoint);
376*8975f5c5SAndroid Build Coastguard Worker bool IsQueryEntryPoint(EntryPoint entryPoint);
377*8975f5c5SAndroid Build Coastguard Worker 
378*8975f5c5SAndroid Build Coastguard Worker template <typename T>
FillWithNullptr(T * array)379*8975f5c5SAndroid Build Coastguard Worker void FillWithNullptr(T *array)
380*8975f5c5SAndroid Build Coastguard Worker {
381*8975f5c5SAndroid Build Coastguard Worker     // std::array::fill(nullptr) yields unoptimized, unrolled loop over array items
382*8975f5c5SAndroid Build Coastguard Worker     memset(array->data(), 0, array->size() * sizeof(*array->data()));
383*8975f5c5SAndroid Build Coastguard Worker     // sanity check for non-0 nullptr
384*8975f5c5SAndroid Build Coastguard Worker     ASSERT(array->data()[0] == nullptr);
385*8975f5c5SAndroid Build Coastguard Worker }
386*8975f5c5SAndroid Build Coastguard Worker }  // namespace angle
387*8975f5c5SAndroid Build Coastguard Worker 
388*8975f5c5SAndroid Build Coastguard Worker void writeFile(const char *path, const void *data, size_t size);
389*8975f5c5SAndroid Build Coastguard Worker 
390*8975f5c5SAndroid Build Coastguard Worker // Get the underlying type. Useful for indexing into arrays with enum values by avoiding the clutter
391*8975f5c5SAndroid Build Coastguard Worker // of the extraneous static_cast<>() calls.
392*8975f5c5SAndroid Build Coastguard Worker // https://stackoverflow.com/a/8357462
393*8975f5c5SAndroid Build Coastguard Worker template <typename E>
ToUnderlying(E e)394*8975f5c5SAndroid Build Coastguard Worker constexpr typename std::underlying_type<E>::type ToUnderlying(E e) noexcept
395*8975f5c5SAndroid Build Coastguard Worker {
396*8975f5c5SAndroid Build Coastguard Worker     return static_cast<typename std::underlying_type<E>::type>(e);
397*8975f5c5SAndroid Build Coastguard Worker }
398*8975f5c5SAndroid Build Coastguard Worker 
399*8975f5c5SAndroid Build Coastguard Worker #endif  // COMMON_UTILITIES_H_
400