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_RAYGEN,
79 SHADERTYPE_ANY_HIT,
80 SHADERTYPE_CLOSEST_HIT,
81 SHADERTYPE_MISS,
82 SHADERTYPE_INTERSECTION,
83 SHADERTYPE_CALLABLE,
84
85 SHADERTYPE_LAST
86 };
87
88 const char* getShaderTypeName (ShaderType shaderType);
89 std::string getShaderTypePostfix (ShaderType shaderType);
90
91 // Precision
92
93 enum Precision
94 {
95 PRECISION_LOWP = 0,
96 PRECISION_MEDIUMP,
97 PRECISION_HIGHP,
98
99 PRECISION_LAST
100 };
101
102 const char* getPrecisionName (Precision precision);
103 std::string getPrecisionPostfix (Precision precision);
104
105 // DataType
106
107 enum DataType
108 {
109 TYPE_INVALID = 0,
110
111 TYPE_FLOAT,
112 TYPE_FLOAT_VEC2,
113 TYPE_FLOAT_VEC3,
114 TYPE_FLOAT_VEC4,
115 TYPE_FLOAT_MAT2,
116 TYPE_FLOAT_MAT2X3,
117 TYPE_FLOAT_MAT2X4,
118 TYPE_FLOAT_MAT3X2,
119 TYPE_FLOAT_MAT3,
120 TYPE_FLOAT_MAT3X4,
121 TYPE_FLOAT_MAT4X2,
122 TYPE_FLOAT_MAT4X3,
123 TYPE_FLOAT_MAT4,
124
125 TYPE_DOUBLE,
126 TYPE_DOUBLE_VEC2,
127 TYPE_DOUBLE_VEC3,
128 TYPE_DOUBLE_VEC4,
129 TYPE_DOUBLE_MAT2,
130 TYPE_DOUBLE_MAT2X3,
131 TYPE_DOUBLE_MAT2X4,
132 TYPE_DOUBLE_MAT3X2,
133 TYPE_DOUBLE_MAT3,
134 TYPE_DOUBLE_MAT3X4,
135 TYPE_DOUBLE_MAT4X2,
136 TYPE_DOUBLE_MAT4X3,
137 TYPE_DOUBLE_MAT4,
138
139 TYPE_INT,
140 TYPE_INT_VEC2,
141 TYPE_INT_VEC3,
142 TYPE_INT_VEC4,
143
144 TYPE_UINT,
145 TYPE_UINT_VEC2,
146 TYPE_UINT_VEC3,
147 TYPE_UINT_VEC4,
148
149 TYPE_BOOL,
150 TYPE_BOOL_VEC2,
151 TYPE_BOOL_VEC3,
152 TYPE_BOOL_VEC4,
153
154 TYPE_SAMPLER_1D,
155 TYPE_SAMPLER_2D,
156 TYPE_SAMPLER_CUBE,
157 TYPE_SAMPLER_1D_ARRAY,
158 TYPE_SAMPLER_2D_ARRAY,
159 TYPE_SAMPLER_3D,
160 TYPE_SAMPLER_CUBE_ARRAY,
161
162 TYPE_SAMPLER_1D_SHADOW,
163 TYPE_SAMPLER_2D_SHADOW,
164 TYPE_SAMPLER_CUBE_SHADOW,
165 TYPE_SAMPLER_1D_ARRAY_SHADOW,
166 TYPE_SAMPLER_2D_ARRAY_SHADOW,
167 TYPE_SAMPLER_CUBE_ARRAY_SHADOW,
168
169 TYPE_INT_SAMPLER_1D,
170 TYPE_INT_SAMPLER_2D,
171 TYPE_INT_SAMPLER_CUBE,
172 TYPE_INT_SAMPLER_1D_ARRAY,
173 TYPE_INT_SAMPLER_2D_ARRAY,
174 TYPE_INT_SAMPLER_3D,
175 TYPE_INT_SAMPLER_CUBE_ARRAY,
176
177 TYPE_UINT_SAMPLER_1D,
178 TYPE_UINT_SAMPLER_2D,
179 TYPE_UINT_SAMPLER_CUBE,
180 TYPE_UINT_SAMPLER_1D_ARRAY,
181 TYPE_UINT_SAMPLER_2D_ARRAY,
182 TYPE_UINT_SAMPLER_3D,
183 TYPE_UINT_SAMPLER_CUBE_ARRAY,
184
185 TYPE_SAMPLER_2D_MULTISAMPLE,
186 TYPE_INT_SAMPLER_2D_MULTISAMPLE,
187 TYPE_UINT_SAMPLER_2D_MULTISAMPLE,
188
189 TYPE_IMAGE_2D,
190 TYPE_IMAGE_CUBE,
191 TYPE_IMAGE_2D_ARRAY,
192 TYPE_IMAGE_3D,
193 TYPE_IMAGE_CUBE_ARRAY,
194
195 TYPE_INT_IMAGE_2D,
196 TYPE_INT_IMAGE_CUBE,
197 TYPE_INT_IMAGE_2D_ARRAY,
198 TYPE_INT_IMAGE_3D,
199 TYPE_INT_IMAGE_CUBE_ARRAY,
200
201 TYPE_UINT_IMAGE_2D,
202 TYPE_UINT_IMAGE_CUBE,
203 TYPE_UINT_IMAGE_2D_ARRAY,
204 TYPE_UINT_IMAGE_3D,
205 TYPE_UINT_IMAGE_CUBE_ARRAY,
206
207 TYPE_UINT_ATOMIC_COUNTER,
208
209 TYPE_SAMPLER_BUFFER,
210 TYPE_INT_SAMPLER_BUFFER,
211 TYPE_UINT_SAMPLER_BUFFER,
212
213 TYPE_SAMPLER_2D_MULTISAMPLE_ARRAY,
214 TYPE_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
215 TYPE_UINT_SAMPLER_2D_MULTISAMPLE_ARRAY,
216
217 TYPE_IMAGE_BUFFER,
218 TYPE_INT_IMAGE_BUFFER,
219 TYPE_UINT_IMAGE_BUFFER,
220
221 TYPE_UINT8,
222 TYPE_UINT8_VEC2,
223 TYPE_UINT8_VEC3,
224 TYPE_UINT8_VEC4,
225
226 TYPE_INT8,
227 TYPE_INT8_VEC2,
228 TYPE_INT8_VEC3,
229 TYPE_INT8_VEC4,
230
231 TYPE_UINT16,
232 TYPE_UINT16_VEC2,
233 TYPE_UINT16_VEC3,
234 TYPE_UINT16_VEC4,
235
236 TYPE_INT16,
237 TYPE_INT16_VEC2,
238 TYPE_INT16_VEC3,
239 TYPE_INT16_VEC4,
240
241 TYPE_FLOAT16,
242 TYPE_FLOAT16_VEC2,
243 TYPE_FLOAT16_VEC3,
244 TYPE_FLOAT16_VEC4,
245 TYPE_FLOAT16_MAT2,
246 TYPE_FLOAT16_MAT2X3,
247 TYPE_FLOAT16_MAT2X4,
248 TYPE_FLOAT16_MAT3X2,
249 TYPE_FLOAT16_MAT3,
250 TYPE_FLOAT16_MAT3X4,
251 TYPE_FLOAT16_MAT4X2,
252 TYPE_FLOAT16_MAT4X3,
253 TYPE_FLOAT16_MAT4,
254
255 TYPE_LAST
256 };
257
258 const char* getDataTypeName (DataType dataType);
259 int getDataTypeScalarSize (DataType dataType);
260 DataType getDataTypeScalarType (DataType dataType);
261 DataType getDataTypeFloat16Scalars (DataType dataType);
262 DataType getDataTypeFloatScalars (DataType dataType);
263 DataType getDataTypeDoubleScalars (DataType dataType);
264 DataType getDataTypeVector (DataType scalarType, int size);
265 DataType getDataTypeFloatVec (int vecSize);
266 DataType getDataTypeIntVec (int vecSize);
267 DataType getDataTypeUintVec (int vecSize);
268 DataType getDataTypeBoolVec (int vecSize);
269 DataType getDataTypeMatrix (int numCols, int numRows);
270 DataType getDataTypeFromGLType (deUint32 glType);
271
isDataTypeFloat16OrVec(DataType dataType)272 inline bool isDataTypeFloat16OrVec (DataType dataType) { return (dataType >= TYPE_FLOAT16) && (dataType <= TYPE_FLOAT16_MAT4); }
isDataTypeFloatOrVec(DataType dataType)273 inline bool isDataTypeFloatOrVec (DataType dataType) { return (dataType >= TYPE_FLOAT) && (dataType <= TYPE_FLOAT_VEC4); }
isDataTypeFloatType(DataType dataType)274 inline bool isDataTypeFloatType (DataType dataType) { return (dataType >= TYPE_FLOAT) && (dataType <= TYPE_FLOAT_MAT4); }
isDataTypeDoubleType(DataType dataType)275 inline bool isDataTypeDoubleType (DataType dataType) { return (dataType >= TYPE_DOUBLE) && (dataType <= TYPE_DOUBLE_MAT4); }
isDataTypeDoubleOrDVec(DataType dataType)276 inline bool isDataTypeDoubleOrDVec (DataType dataType) { return (dataType >= TYPE_DOUBLE) && (dataType <= TYPE_DOUBLE_VEC4); }
isDataTypeMatrix(DataType dataType)277 inline bool isDataTypeMatrix (DataType dataType) {
278 return ((dataType >= TYPE_FLOAT_MAT2) && (dataType <= TYPE_FLOAT_MAT4))
279 || ((dataType >= TYPE_DOUBLE_MAT2) && (dataType <= TYPE_DOUBLE_MAT4))
280 || ((dataType >= TYPE_FLOAT16_MAT2) && (dataType <= TYPE_FLOAT16_MAT4))
281 ;
282 }
isDataTypeIntOrIVec(DataType dataType)283 inline bool isDataTypeIntOrIVec (DataType dataType) { return (dataType >= TYPE_INT) && (dataType <= TYPE_INT_VEC4); }
isDataTypeUintOrUVec(DataType dataType)284 inline bool isDataTypeUintOrUVec (DataType dataType) { return (dataType >= TYPE_UINT) && (dataType <= TYPE_UINT_VEC4); }
isDataTypeBoolOrBVec(DataType dataType)285 inline bool isDataTypeBoolOrBVec (DataType dataType) { return (dataType >= TYPE_BOOL) && (dataType <= TYPE_BOOL_VEC4); }
isDataTypeScalar(DataType dataType)286 inline bool isDataTypeScalar (DataType dataType) {
287 return (dataType == TYPE_FLOAT)
288 || (dataType == TYPE_DOUBLE)
289 || (dataType == TYPE_INT)
290 || (dataType == TYPE_UINT)
291 || (dataType == TYPE_BOOL)
292 || (dataType == TYPE_UINT8)
293 || (dataType == TYPE_INT8)
294 || (dataType == TYPE_UINT16)
295 || (dataType == TYPE_INT16)
296 || (dataType == TYPE_FLOAT16)
297 ;
298 }
isDataTypeVector(DataType dataType)299 inline bool isDataTypeVector (DataType dataType) {
300 return deInRange32(dataType, TYPE_FLOAT_VEC2, TYPE_FLOAT_VEC4)
301 || deInRange32(dataType, TYPE_DOUBLE_VEC2, TYPE_DOUBLE_VEC4)
302 || deInRange32(dataType, TYPE_INT_VEC2, TYPE_INT_VEC4)
303 || deInRange32(dataType, TYPE_UINT_VEC2, TYPE_UINT_VEC4)
304 || deInRange32(dataType, TYPE_BOOL_VEC2, TYPE_BOOL_VEC4)
305 || deInRange32(dataType, TYPE_UINT8_VEC2, TYPE_UINT8_VEC4)
306 || deInRange32(dataType, TYPE_INT8_VEC2, TYPE_INT8_VEC4)
307 || deInRange32(dataType, TYPE_UINT16_VEC2, TYPE_UINT16_VEC4)
308 || deInRange32(dataType, TYPE_INT16_VEC2, TYPE_INT16_VEC4)
309 || deInRange32(dataType, TYPE_FLOAT16_VEC2, TYPE_FLOAT16_VEC4)
310 ;
311 }
isDataTypeScalarOrVector(DataType dataType)312 inline bool isDataTypeScalarOrVector (DataType dataType) {
313 return deInRange32(dataType, TYPE_FLOAT, TYPE_FLOAT_VEC4)
314 || deInRange32(dataType, TYPE_DOUBLE, TYPE_DOUBLE_VEC4)
315 || deInRange32(dataType, TYPE_INT, TYPE_INT_VEC4)
316 || deInRange32(dataType, TYPE_UINT, TYPE_UINT_VEC4)
317 || deInRange32(dataType, TYPE_BOOL, TYPE_BOOL_VEC4)
318 || deInRange32(dataType, TYPE_UINT8, TYPE_UINT8_VEC4)
319 || deInRange32(dataType, TYPE_INT8, TYPE_INT8_VEC4)
320 || deInRange32(dataType, TYPE_UINT16, TYPE_UINT16_VEC4)
321 || deInRange32(dataType, TYPE_INT16, TYPE_INT16_VEC4)
322 || deInRange32(dataType, TYPE_FLOAT16, TYPE_FLOAT16_VEC4)
323 ;
324 }
isDataTypeSampler(DataType dataType)325 inline bool isDataTypeSampler (DataType dataType) { return (dataType >= TYPE_SAMPLER_1D) && (dataType <= TYPE_UINT_SAMPLER_2D_MULTISAMPLE); }
isDataTypeImage(DataType dataType)326 inline bool isDataTypeImage (DataType dataType) { return (dataType >= TYPE_IMAGE_2D) && (dataType <= TYPE_UINT_IMAGE_3D); }
isDataTypeSamplerMultisample(DataType dataType)327 inline bool isDataTypeSamplerMultisample (DataType dataType) { return (dataType >= TYPE_SAMPLER_2D_MULTISAMPLE) && (dataType <= TYPE_UINT_SAMPLER_2D_MULTISAMPLE); }
isDataTypeAtomicCounter(DataType dataType)328 inline bool isDataTypeAtomicCounter (DataType dataType) { return dataType == TYPE_UINT_ATOMIC_COUNTER; }
isDataTypeSamplerBuffer(DataType dataType)329 inline bool isDataTypeSamplerBuffer (DataType dataType) { return (dataType >= TYPE_SAMPLER_BUFFER) && (dataType <= TYPE_UINT_SAMPLER_BUFFER); }
isDataTypeSamplerMSArray(DataType dataType)330 inline bool isDataTypeSamplerMSArray (DataType dataType) { return (dataType >= TYPE_SAMPLER_2D_MULTISAMPLE_ARRAY) && (dataType <= TYPE_UINT_SAMPLER_2D_MULTISAMPLE_ARRAY); }
isDataTypeImageBuffer(DataType dataType)331 inline bool isDataTypeImageBuffer (DataType dataType) { return (dataType >= TYPE_IMAGE_BUFFER) && (dataType <= TYPE_UINT_IMAGE_BUFFER); }
isDataTypeExplicitPrecision(DataType dataType)332 inline bool isDataTypeExplicitPrecision (DataType dataType) {
333 return deInRange32(dataType, TYPE_UINT8, TYPE_UINT8_VEC4)
334 || deInRange32(dataType, TYPE_INT8, TYPE_INT8_VEC4)
335 || deInRange32(dataType, TYPE_UINT16, TYPE_UINT16_VEC4)
336 || deInRange32(dataType, TYPE_INT16, TYPE_INT16_VEC4)
337 || deInRange32(dataType, TYPE_FLOAT16, TYPE_FLOAT16_MAT4)
338 || deInRange32(dataType, TYPE_DOUBLE, TYPE_DOUBLE_MAT4)
339 ;
340 }
dataTypeSupportsPrecisionModifier(DataType dataType)341 inline bool dataTypeSupportsPrecisionModifier (DataType dataType) { return !isDataTypeBoolOrBVec(dataType) && !isDataTypeExplicitPrecision(dataType); }
342
343 int getDataTypeMatrixNumRows (DataType dataType);
344 int getDataTypeMatrixNumColumns (DataType dataType);
345
346 int getDataTypeNumLocations (DataType dataType);
347 int getDataTypeNumComponents (DataType dataType);
348
349 template <typename T>
350 struct DataTypeTraits;
351
352 template <> struct DataTypeTraits<deUint16> { enum { DATATYPE = TYPE_FLOAT16 }; };
353 template <> struct DataTypeTraits<float> { enum { DATATYPE = TYPE_FLOAT }; };
354 template <> struct DataTypeTraits<bool> { enum { DATATYPE = TYPE_BOOL }; };
355 template <> struct DataTypeTraits<int> { enum { DATATYPE = TYPE_INT }; };
356 template <> struct DataTypeTraits<deUint32> { enum { DATATYPE = TYPE_UINT }; };
357 template <> struct DataTypeTraits<tcu::Mat2> { enum { DATATYPE = TYPE_FLOAT_MAT2 }; };
358 template <> struct DataTypeTraits<tcu::Mat2x3> { enum { DATATYPE = TYPE_FLOAT_MAT2X3 }; };
359 template <> struct DataTypeTraits<tcu::Mat2x4> { enum { DATATYPE = TYPE_FLOAT_MAT2X4 }; };
360 template <> struct DataTypeTraits<tcu::Mat3x2> { enum { DATATYPE = TYPE_FLOAT_MAT3X2 }; };
361 template <> struct DataTypeTraits<tcu::Mat3> { enum { DATATYPE = TYPE_FLOAT_MAT3 }; };
362 template <> struct DataTypeTraits<tcu::Mat3x4> { enum { DATATYPE = TYPE_FLOAT_MAT3X4 }; };
363 template <> struct DataTypeTraits<tcu::Mat4x2> { enum { DATATYPE = TYPE_FLOAT_MAT4X2 }; };
364 template <> struct DataTypeTraits<tcu::Mat4x3> { enum { DATATYPE = TYPE_FLOAT_MAT4X3 }; };
365 template <> struct DataTypeTraits<tcu::Mat4> { enum { DATATYPE = TYPE_FLOAT_MAT4 }; };
366 template <> struct DataTypeTraits<tcu::Mat2_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT2 }; };
367 template <> struct DataTypeTraits<tcu::Mat2x3_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT2X3 }; };
368 template <> struct DataTypeTraits<tcu::Mat2x4_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT2X4 }; };
369 template <> struct DataTypeTraits<tcu::Mat3x2_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT3X2 }; };
370 template <> struct DataTypeTraits<tcu::Mat3_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT3 }; };
371 template <> struct DataTypeTraits<tcu::Mat3x4_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT3X4 }; };
372 template <> struct DataTypeTraits<tcu::Mat4x2_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT4X2 }; };
373 template <> struct DataTypeTraits<tcu::Mat4x3_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT4X3 }; };
374 template <> struct DataTypeTraits<tcu::Mat4_16b> { enum { DATATYPE = TYPE_FLOAT16_MAT4 }; };
375
376 template <typename T, int Size>
377 struct DataTypeTraits<tcu::Vector<T, Size> >
378 {
379 DE_STATIC_ASSERT(TYPE_FLOAT_VEC4 == TYPE_FLOAT + 3);
380 DE_STATIC_ASSERT(TYPE_INT_VEC4 == TYPE_INT + 3);
381 DE_STATIC_ASSERT(TYPE_UINT_VEC4 == TYPE_UINT + 3);
382 DE_STATIC_ASSERT(TYPE_BOOL_VEC4 == TYPE_BOOL + 3);
383 enum { DATATYPE = DataTypeTraits<T>::DATATYPE + Size - 1 };
384 };
385
386 template <typename T>
dataTypeOf(void)387 inline DataType dataTypeOf (void) { return DataType(DataTypeTraits<T>::DATATYPE); }
388
389 } // glu
390
391 #endif // _GLUSHADERUTIL_HPP
392