1 #ifndef _GLUSHADERUTIL_HPP
2 #define _GLUSHADERUTIL_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program OpenGL ES Utilities
5 * ------------------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Shader utilities.
24 *//*--------------------------------------------------------------------*/
25
26 #include "tcuDefs.hpp"
27 #include "deInt32.h"
28 #include "gluRenderContext.hpp"
29 #include "tcuVector.hpp"
30 #include "tcuVector.hpp"
31 #include "tcuMatrix.hpp"
32
33 namespace glu
34 {
35
36 // ShadingLanguageVersion
37
38 enum GLSLVersion
39 {
40 GLSL_VERSION_100_ES = 0, //!< GLSL ES 1.0
41 GLSL_VERSION_300_ES, //!< GLSL ES 3.0
42 GLSL_VERSION_310_ES, //!< GLSL ES 3.1
43 GLSL_VERSION_320_ES, //!< GLSL ES 3.2
44
45 GLSL_VERSION_130, //!< GLSL 1.3
46 GLSL_VERSION_140, //!< GLSL 1.4
47 GLSL_VERSION_150, //!< GLSL 1.5
48 GLSL_VERSION_330, //!< GLSL 3.0
49 GLSL_VERSION_400, //!< GLSL 4.0
50 GLSL_VERSION_410, //!< GLSL 4.1
51 GLSL_VERSION_420, //!< GLSL 4.2
52 GLSL_VERSION_430, //!< GLSL 4.3
53 GLSL_VERSION_440, //!< GLSL 4.4
54 GLSL_VERSION_450, //!< GLSL 4.5
55 GLSL_VERSION_460, //!< GLSL 4.6
56
57 GLSL_VERSION_LAST
58 };
59
60 const char* getGLSLVersionName (GLSLVersion version);
61 const char* getGLSLVersionDeclaration (GLSLVersion version);
62 bool glslVersionUsesInOutQualifiers (GLSLVersion version);
63 bool glslVersionIsES (GLSLVersion version);
64 bool isGLSLVersionSupported (ContextType type, GLSLVersion version);
65 GLSLVersion getContextTypeGLSLVersion (ContextType type);
66
67 // ShaderType
68
69 enum ShaderType
70 {
71 SHADERTYPE_VERTEX = 0,
72 SHADERTYPE_FRAGMENT,
73 SHADERTYPE_GEOMETRY,
74 SHADERTYPE_TESSELLATION_CONTROL,
75 SHADERTYPE_TESSELLATION_EVALUATION,
76 SHADERTYPE_COMPUTE,
77
78 SHADERTYPE_LAST
79 };
80
81 const char* getShaderTypeName (ShaderType shaderType);
82
83 // Precision
84
85 enum Precision
86 {
87 PRECISION_LOWP = 0,
88 PRECISION_MEDIUMP,
89 PRECISION_HIGHP,
90
91 PRECISION_LAST
92 };
93
94 const char* getPrecisionName (Precision precision);
95
96 // DataType
97
98 enum DataType
99 {
100 TYPE_INVALID = 0,
101
102 TYPE_FLOAT,
103 TYPE_FLOAT_VEC2,
104 TYPE_FLOAT_VEC3,
105 TYPE_FLOAT_VEC4,
106 TYPE_FLOAT_MAT2,
107 TYPE_FLOAT_MAT2X3,
108 TYPE_FLOAT_MAT2X4,
109 TYPE_FLOAT_MAT3X2,
110 TYPE_FLOAT_MAT3,
111 TYPE_FLOAT_MAT3X4,
112 TYPE_FLOAT_MAT4X2,
113 TYPE_FLOAT_MAT4X3,
114 TYPE_FLOAT_MAT4,
115
116 TYPE_DOUBLE,
117 TYPE_DOUBLE_VEC2,
118 TYPE_DOUBLE_VEC3,
119 TYPE_DOUBLE_VEC4,
120 TYPE_DOUBLE_MAT2,
121 TYPE_DOUBLE_MAT2X3,
122 TYPE_DOUBLE_MAT2X4,
123 TYPE_DOUBLE_MAT3X2,
124 TYPE_DOUBLE_MAT3,
125 TYPE_DOUBLE_MAT3X4,
126 TYPE_DOUBLE_MAT4X2,
127 TYPE_DOUBLE_MAT4X3,
128 TYPE_DOUBLE_MAT4,
129
130 TYPE_INT,
131 TYPE_INT_VEC2,
132 TYPE_INT_VEC3,
133 TYPE_INT_VEC4,
134
135 TYPE_UINT,
136 TYPE_UINT_VEC2,
137 TYPE_UINT_VEC3,
138 TYPE_UINT_VEC4,
139
140 TYPE_BOOL,
141 TYPE_BOOL_VEC2,
142 TYPE_BOOL_VEC3,
143 TYPE_BOOL_VEC4,
144
145 TYPE_SAMPLER_1D,
146 TYPE_SAMPLER_2D,
147 TYPE_SAMPLER_CUBE,
148 TYPE_SAMPLER_1D_ARRAY,
149 TYPE_SAMPLER_2D_ARRAY,
150 TYPE_SAMPLER_3D,
151 TYPE_SAMPLER_CUBE_ARRAY,
152
153 TYPE_SAMPLER_1D_SHADOW,
154 TYPE_SAMPLER_2D_SHADOW,
155 TYPE_SAMPLER_CUBE_SHADOW,
156 TYPE_SAMPLER_1D_ARRAY_SHADOW,
157 TYPE_SAMPLER_2D_ARRAY_SHADOW,
158 TYPE_SAMPLER_CUBE_ARRAY_SHADOW,
159
160 TYPE_INT_SAMPLER_1D,
161 TYPE_INT_SAMPLER_2D,
162 TYPE_INT_SAMPLER_CUBE,
163 TYPE_INT_SAMPLER_1D_ARRAY,
164 TYPE_INT_SAMPLER_2D_ARRAY,
165 TYPE_INT_SAMPLER_3D,
166 TYPE_INT_SAMPLER_CUBE_ARRAY,
167
168 TYPE_UINT_SAMPLER_1D,
169 TYPE_UINT_SAMPLER_2D,
170 TYPE_UINT_SAMPLER_CUBE,
171 TYPE_UINT_SAMPLER_1D_ARRAY,
172 TYPE_UINT_SAMPLER_2D_ARRAY,
173 TYPE_UINT_SAMPLER_3D,
174 TYPE_UINT_SAMPLER_CUBE_ARRAY,
175
176 TYPE_SAMPLER_2D_MULTISAMPLE,
177 TYPE_INT_SAMPLER_2D_MULTISAMPLE,
178 TYPE_UINT_SAMPLER_2D_MULTISAMPLE,
179
180 TYPE_IMAGE_2D,
181 TYPE_IMAGE_CUBE,
182 TYPE_IMAGE_2D_ARRAY,
183 TYPE_IMAGE_3D,
184 TYPE_IMAGE_CUBE_ARRAY,
185
186 TYPE_INT_IMAGE_2D,
187 TYPE_INT_IMAGE_CUBE,
188 TYPE_INT_IMAGE_2D_ARRAY,
189 TYPE_INT_IMAGE_3D,
190 TYPE_INT_IMAGE_CUBE_ARRAY,
191
192 TYPE_UINT_IMAGE_2D,
193 TYPE_UINT_IMAGE_CUBE,
194 TYPE_UINT_IMAGE_2D_ARRAY,
195 TYPE_UINT_IMAGE_3D,
196 TYPE_UINT_IMAGE_CUBE_ARRAY,
197
198 TYPE_UINT_ATOMIC_COUNTER,
199
200 TYPE_SAMPLER_BUFFER,
201 TYPE_INT_SAMPLER_BUFFER,
202 TYPE_UINT_SAMPLER_BUFFER,
203
204 TYPE_SAMPLER_2D_MULTISAMPLE_ARRAY,
205 TYPE_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
206 TYPE_UINT_SAMPLER_2D_MULTISAMPLE_ARRAY,
207
208 TYPE_IMAGE_BUFFER,
209 TYPE_INT_IMAGE_BUFFER,
210 TYPE_UINT_IMAGE_BUFFER,
211
212 TYPE_UINT8,
213 TYPE_UINT8_VEC2,
214 TYPE_UINT8_VEC3,
215 TYPE_UINT8_VEC4,
216
217 TYPE_INT8,
218 TYPE_INT8_VEC2,
219 TYPE_INT8_VEC3,
220 TYPE_INT8_VEC4,
221
222 TYPE_UINT16,
223 TYPE_UINT16_VEC2,
224 TYPE_UINT16_VEC3,
225 TYPE_UINT16_VEC4,
226
227 TYPE_INT16,
228 TYPE_INT16_VEC2,
229 TYPE_INT16_VEC3,
230 TYPE_INT16_VEC4,
231
232 TYPE_FLOAT16,
233 TYPE_FLOAT16_VEC2,
234 TYPE_FLOAT16_VEC3,
235 TYPE_FLOAT16_VEC4,
236 TYPE_FLOAT16_MAT2,
237 TYPE_FLOAT16_MAT2X3,
238 TYPE_FLOAT16_MAT2X4,
239 TYPE_FLOAT16_MAT3X2,
240 TYPE_FLOAT16_MAT3,
241 TYPE_FLOAT16_MAT3X4,
242 TYPE_FLOAT16_MAT4X2,
243 TYPE_FLOAT16_MAT4X3,
244 TYPE_FLOAT16_MAT4,
245
246 TYPE_LAST
247 };
248
249 const char* getDataTypeName (DataType dataType);
250 int getDataTypeScalarSize (DataType dataType);
251 DataType getDataTypeScalarType (DataType dataType);
252 DataType getDataTypeFloat16Scalars (DataType dataType);
253 DataType getDataTypeFloatScalars (DataType dataType);
254 DataType getDataTypeDoubleScalars (DataType dataType);
255 DataType getDataTypeVector (DataType scalarType, int size);
256 DataType getDataTypeFloatVec (int vecSize);
257 DataType getDataTypeIntVec (int vecSize);
258 DataType getDataTypeUintVec (int vecSize);
259 DataType getDataTypeBoolVec (int vecSize);
260 DataType getDataTypeMatrix (int numCols, int numRows);
261 DataType getDataTypeFromGLType (deUint32 glType);
262
isDataTypeFloat16OrVec(DataType dataType)263 inline bool isDataTypeFloat16OrVec (DataType dataType) { return (dataType >= TYPE_FLOAT16) && (dataType <= TYPE_FLOAT16_MAT4); }
isDataTypeFloatOrVec(DataType dataType)264 inline bool isDataTypeFloatOrVec (DataType dataType) { return (dataType >= TYPE_FLOAT) && (dataType <= TYPE_FLOAT_VEC4); }
isDataTypeFloatType(DataType dataType)265 inline bool isDataTypeFloatType (DataType dataType) { return (dataType >= TYPE_FLOAT) && (dataType <= TYPE_FLOAT_MAT4); }
isDataTypeDoubleOrDVec(DataType dataType)266 inline bool isDataTypeDoubleOrDVec (DataType dataType) { return (dataType >= TYPE_DOUBLE) && (dataType <= TYPE_DOUBLE_VEC4); }
isDataTypeMatrix(DataType dataType)267 inline bool isDataTypeMatrix (DataType dataType) {
268 return ((dataType >= TYPE_FLOAT_MAT2) && (dataType <= TYPE_FLOAT_MAT4))
269 || ((dataType >= TYPE_DOUBLE_MAT2) && (dataType <= TYPE_DOUBLE_MAT4))
270 || ((dataType >= TYPE_FLOAT16_MAT2) && (dataType <= TYPE_FLOAT16_MAT4))
271 ;
272 }
isDataTypeIntOrIVec(DataType dataType)273 inline bool isDataTypeIntOrIVec (DataType dataType) { return (dataType >= TYPE_INT) && (dataType <= TYPE_INT_VEC4); }
isDataTypeUintOrUVec(DataType dataType)274 inline bool isDataTypeUintOrUVec (DataType dataType) { return (dataType >= TYPE_UINT) && (dataType <= TYPE_UINT_VEC4); }
isDataTypeBoolOrBVec(DataType dataType)275 inline bool isDataTypeBoolOrBVec (DataType dataType) { return (dataType >= TYPE_BOOL) && (dataType <= TYPE_BOOL_VEC4); }
isDataTypeScalar(DataType dataType)276 inline bool isDataTypeScalar (DataType dataType) {
277 return (dataType == TYPE_FLOAT)
278 || (dataType == TYPE_DOUBLE)
279 || (dataType == TYPE_INT)
280 || (dataType == TYPE_UINT)
281 || (dataType == TYPE_BOOL)
282 || (dataType == TYPE_UINT8)
283 || (dataType == TYPE_INT8)
284 || (dataType == TYPE_UINT16)
285 || (dataType == TYPE_INT16)
286 || (dataType == TYPE_FLOAT16)
287 ;
288 }
isDataTypeVector(DataType dataType)289 inline bool isDataTypeVector (DataType dataType) {
290 return deInRange32(dataType, TYPE_FLOAT_VEC2, TYPE_FLOAT_VEC4)
291 || deInRange32(dataType, TYPE_DOUBLE_VEC2, TYPE_DOUBLE_VEC4)
292 || deInRange32(dataType, TYPE_INT_VEC2, TYPE_INT_VEC4)
293 || deInRange32(dataType, TYPE_UINT_VEC2, TYPE_UINT_VEC4)
294 || deInRange32(dataType, TYPE_BOOL_VEC2, TYPE_BOOL_VEC4)
295 || deInRange32(dataType, TYPE_UINT8_VEC2, TYPE_UINT8_VEC4)
296 || deInRange32(dataType, TYPE_INT8_VEC2, TYPE_INT8_VEC4)
297 || deInRange32(dataType, TYPE_UINT16_VEC2, TYPE_UINT16_VEC4)
298 || deInRange32(dataType, TYPE_INT16_VEC2, TYPE_INT16_VEC4)
299 || deInRange32(dataType, TYPE_FLOAT16_VEC2, TYPE_FLOAT16_VEC4)
300 ;
301 }
isDataTypeScalarOrVector(DataType dataType)302 inline bool isDataTypeScalarOrVector (DataType dataType) {
303 return deInRange32(dataType, TYPE_FLOAT, TYPE_FLOAT_VEC4)
304 || deInRange32(dataType, TYPE_DOUBLE, TYPE_DOUBLE_VEC4)
305 || deInRange32(dataType, TYPE_INT, TYPE_INT_VEC4)
306 || deInRange32(dataType, TYPE_UINT, TYPE_UINT_VEC4)
307 || deInRange32(dataType, TYPE_BOOL, TYPE_BOOL_VEC4)
308 || deInRange32(dataType, TYPE_UINT8, TYPE_UINT8_VEC4)
309 || deInRange32(dataType, TYPE_INT8, TYPE_INT8_VEC4)
310 || deInRange32(dataType, TYPE_UINT16, TYPE_UINT16_VEC4)
311 || deInRange32(dataType, TYPE_INT16, TYPE_INT16_VEC4)
312 || deInRange32(dataType, TYPE_FLOAT16, TYPE_FLOAT16_VEC4)
313 ;
314 }
isDataTypeSampler(DataType dataType)315 inline bool isDataTypeSampler (DataType dataType) { return (dataType >= TYPE_SAMPLER_1D) && (dataType <= TYPE_UINT_SAMPLER_2D_MULTISAMPLE); }
isDataTypeImage(DataType dataType)316 inline bool isDataTypeImage (DataType dataType) { return (dataType >= TYPE_IMAGE_2D) && (dataType <= TYPE_UINT_IMAGE_3D); }
isDataTypeSamplerMultisample(DataType dataType)317 inline bool isDataTypeSamplerMultisample (DataType dataType) { return (dataType >= TYPE_SAMPLER_2D_MULTISAMPLE) && (dataType <= TYPE_UINT_SAMPLER_2D_MULTISAMPLE); }
isDataTypeAtomicCounter(DataType dataType)318 inline bool isDataTypeAtomicCounter (DataType dataType) { return dataType == TYPE_UINT_ATOMIC_COUNTER; }
isDataTypeSamplerBuffer(DataType dataType)319 inline bool isDataTypeSamplerBuffer (DataType dataType) { return (dataType >= TYPE_SAMPLER_BUFFER) && (dataType <= TYPE_UINT_SAMPLER_BUFFER); }
isDataTypeSamplerMSArray(DataType dataType)320 inline bool isDataTypeSamplerMSArray (DataType dataType) { return (dataType >= TYPE_SAMPLER_2D_MULTISAMPLE_ARRAY) && (dataType <= TYPE_UINT_SAMPLER_2D_MULTISAMPLE_ARRAY); }
isDataTypeImageBuffer(DataType dataType)321 inline bool isDataTypeImageBuffer (DataType dataType) { return (dataType >= TYPE_IMAGE_BUFFER) && (dataType <= TYPE_UINT_IMAGE_BUFFER); }
isDataTypeExplicitPrecision(DataType dataType)322 inline bool isDataTypeExplicitPrecision (DataType dataType) {
323 return deInRange32(dataType, TYPE_UINT8, TYPE_UINT8_VEC4)
324 || deInRange32(dataType, TYPE_INT8, TYPE_INT8_VEC4)
325 || deInRange32(dataType, TYPE_UINT16, TYPE_UINT16_VEC4)
326 || deInRange32(dataType, TYPE_INT16, TYPE_INT16_VEC4)
327 || deInRange32(dataType, TYPE_FLOAT16, TYPE_FLOAT16_VEC4)
328 ;
329 }
dataTypeSupportsPrecisionModifier(DataType dataType)330 inline bool dataTypeSupportsPrecisionModifier (DataType dataType) { return !isDataTypeBoolOrBVec(dataType) && !isDataTypeExplicitPrecision(dataType); }
331
332 int getDataTypeMatrixNumRows (DataType dataType);
333 int getDataTypeMatrixNumColumns (DataType dataType);
334
335 int getDataTypeNumLocations (DataType dataType);
336 int getDataTypeNumComponents (DataType dataType);
337
338 template <typename T>
339 struct DataTypeTraits;
340
341 template <> struct DataTypeTraits<deUint16> { enum { DATATYPE = TYPE_FLOAT16 }; };
342 template <> struct DataTypeTraits<float> { enum { DATATYPE = TYPE_FLOAT }; };
343 template <> struct DataTypeTraits<bool> { enum { DATATYPE = TYPE_BOOL }; };
344 template <> struct DataTypeTraits<int> { enum { DATATYPE = TYPE_INT }; };
345 template <> struct DataTypeTraits<deUint32> { enum { DATATYPE = TYPE_UINT }; };
346 template <> struct DataTypeTraits<tcu::Mat2> { enum { DATATYPE = TYPE_FLOAT_MAT2 }; };
347 template <> struct DataTypeTraits<tcu::Mat2x3> { enum { DATATYPE = TYPE_FLOAT_MAT2X3 }; };
348 template <> struct DataTypeTraits<tcu::Mat2x4> { enum { DATATYPE = TYPE_FLOAT_MAT2X4 }; };
349 template <> struct DataTypeTraits<tcu::Mat3x2> { enum { DATATYPE = TYPE_FLOAT_MAT3X2 }; };
350 template <> struct DataTypeTraits<tcu::Mat3> { enum { DATATYPE = TYPE_FLOAT_MAT3 }; };
351 template <> struct DataTypeTraits<tcu::Mat3x4> { enum { DATATYPE = TYPE_FLOAT_MAT3X4 }; };
352 template <> struct DataTypeTraits<tcu::Mat4x2> { enum { DATATYPE = TYPE_FLOAT_MAT4X2 }; };
353 template <> struct DataTypeTraits<tcu::Mat4x3> { enum { DATATYPE = TYPE_FLOAT_MAT4X3 }; };
354 template <> struct DataTypeTraits<tcu::Mat4> { enum { DATATYPE = TYPE_FLOAT_MAT4 }; };
355 template <> struct DataTypeTraits<tcu::Mat2_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT2 }; };
356 template <> struct DataTypeTraits<tcu::Mat2x3_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT2X3 }; };
357 template <> struct DataTypeTraits<tcu::Mat2x4_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT2X4 }; };
358 template <> struct DataTypeTraits<tcu::Mat3x2_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT3X2 }; };
359 template <> struct DataTypeTraits<tcu::Mat3_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT3 }; };
360 template <> struct DataTypeTraits<tcu::Mat3x4_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT3X4 }; };
361 template <> struct DataTypeTraits<tcu::Mat4x2_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT4X2 }; };
362 template <> struct DataTypeTraits<tcu::Mat4x3_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT4X3 }; };
363 template <> struct DataTypeTraits<tcu::Mat4_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT4 }; };
364
365 template <typename T, int Size>
366 struct DataTypeTraits<tcu::Vector<T, Size> >
367 {
368 DE_STATIC_ASSERT(TYPE_FLOAT_VEC4 == TYPE_FLOAT + 3);
369 DE_STATIC_ASSERT(TYPE_INT_VEC4 == TYPE_INT + 3);
370 DE_STATIC_ASSERT(TYPE_UINT_VEC4 == TYPE_UINT + 3);
371 DE_STATIC_ASSERT(TYPE_BOOL_VEC4 == TYPE_BOOL + 3);
372 enum { DATATYPE = DataTypeTraits<T>::DATATYPE + Size - 1 };
373 };
374
375 template <typename T>
dataTypeOf(void)376 inline DataType dataTypeOf (void) { return DataType(DataTypeTraits<T>::DATATYPE); }
377
378 } // glu
379
380 #endif // _GLUSHADERUTIL_HPP
381