blob: 5464ec51821b91478b11cf97e341278c5195ddc1 [file] [log] [blame]
daniel@transgaming.com91ed1492010-10-29 03:11:43 +00001//
2// Copyright (c) 2010 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
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/util.h"
daniel@transgaming.com91ed1492010-10-29 03:11:43 +00008
Zhenyao Mof1d723c2013-09-23 14:57:07 -04009#include <limits>
10
Olli Etuaho99bd5f42016-11-07 12:44:29 +000011#include "common/utilities.h"
Zhenyao Mocc4ec642013-09-23 14:57:10 -040012#include "compiler/preprocessor/numeric_lex.h"
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070013#include "compiler/translator/SymbolTable.h"
Zhenyao Mof1d723c2013-09-23 14:57:07 -040014
Olli Etuahof541f522015-10-13 12:21:01 +030015bool atoi_clamp(const char *str, unsigned int *value)
Zhenyao Mof1d723c2013-09-23 14:57:07 -040016{
Zhenyao Mocc4ec642013-09-23 14:57:10 -040017 bool success = pp::numeric_lex_int(str, value);
18 if (!success)
Olli Etuahof541f522015-10-13 12:21:01 +030019 *value = std::numeric_limits<unsigned int>::max();
Zhenyao Mocc4ec642013-09-23 14:57:10 -040020 return success;
Zhenyao Mof1d723c2013-09-23 14:57:07 -040021}
22
Jamie Madill033dae62014-06-18 12:56:28 -040023namespace sh
24{
25
Jiawei Shao8e4b3552017-08-30 14:20:58 +080026namespace
27{
28
29bool IsInterpolationIn(TQualifier qualifier)
30{
31 switch (qualifier)
32 {
33 case EvqSmoothIn:
34 case EvqFlatIn:
35 case EvqCentroidIn:
36 return true;
37 default:
38 return false;
39 }
40}
41
42} // anonymous namespace
43
Olli Etuaho99bd5f42016-11-07 12:44:29 +000044float NumericLexFloat32OutOfRangeToInfinity(const std::string &str)
45{
46 // Parses a decimal string using scientific notation into a floating point number.
47 // Out-of-range values are converted to infinity. Values that are too small to be
48 // represented are converted to zero.
49
50 // The mantissa in decimal scientific notation. The magnitude of the mantissa integer does not
51 // matter.
52 unsigned int decimalMantissa = 0;
53 size_t i = 0;
54 bool decimalPointSeen = false;
55 bool nonZeroSeenInMantissa = false;
56
57 // The exponent offset reflects the position of the decimal point.
58 int exponentOffset = -1;
59 while (i < str.length())
60 {
61 const char c = str[i];
62 if (c == 'e' || c == 'E')
63 {
64 break;
65 }
66 if (c == '.')
67 {
68 decimalPointSeen = true;
69 ++i;
70 continue;
71 }
72
73 unsigned int digit = static_cast<unsigned int>(c - '0');
74 ASSERT(digit < 10u);
75 if (digit != 0u)
76 {
77 nonZeroSeenInMantissa = true;
78 }
79 if (nonZeroSeenInMantissa)
80 {
81 // Add bits to the mantissa until space runs out in 32-bit int. This should be
82 // enough precision to make the resulting binary mantissa accurate to 1 ULP.
83 if (decimalMantissa <= (std::numeric_limits<unsigned int>::max() - 9u) / 10u)
84 {
85 decimalMantissa = decimalMantissa * 10u + digit;
86 }
87 if (!decimalPointSeen)
88 {
89 ++exponentOffset;
90 }
91 }
92 else if (decimalPointSeen)
93 {
94 --exponentOffset;
95 }
96 ++i;
97 }
98 if (decimalMantissa == 0)
99 {
100 return 0.0f;
101 }
102 int exponent = 0;
103 if (i < str.length())
104 {
105 ASSERT(str[i] == 'e' || str[i] == 'E');
106 ++i;
107 bool exponentOutOfRange = false;
108 bool negativeExponent = false;
109 if (str[i] == '-')
110 {
111 negativeExponent = true;
112 ++i;
113 }
114 else if (str[i] == '+')
115 {
116 ++i;
117 }
118 while (i < str.length())
119 {
120 const char c = str[i];
121 unsigned int digit = static_cast<unsigned int>(c - '0');
122 ASSERT(digit < 10u);
123 if (exponent <= (std::numeric_limits<int>::max() - 9) / 10)
124 {
125 exponent = exponent * 10 + digit;
126 }
127 else
128 {
129 exponentOutOfRange = true;
130 }
131 ++i;
132 }
133 if (negativeExponent)
134 {
135 exponent = -exponent;
136 }
137 if (exponentOutOfRange)
138 {
139 if (negativeExponent)
140 {
141 return 0.0f;
142 }
143 else
144 {
145 return std::numeric_limits<float>::infinity();
146 }
147 }
148 }
149 // Do the calculation in 64-bit to avoid overflow.
150 long long exponentLong =
151 static_cast<long long>(exponent) + static_cast<long long>(exponentOffset);
152 if (exponentLong > std::numeric_limits<float>::max_exponent10)
153 {
154 return std::numeric_limits<float>::infinity();
155 }
156 else if (exponentLong < std::numeric_limits<float>::min_exponent10)
157 {
158 return 0.0f;
159 }
160 // The exponent is in range, so we need to actually evaluate the float.
161 exponent = static_cast<int>(exponentLong);
162 double value = decimalMantissa;
163
164 // Calculate the exponent offset to normalize the mantissa.
165 int normalizationExponentOffset = 0;
166 while (decimalMantissa >= 10u)
167 {
168 --normalizationExponentOffset;
169 decimalMantissa /= 10u;
170 }
171 // Apply the exponent.
172 value *= std::pow(10.0, static_cast<double>(exponent + normalizationExponentOffset));
173 if (value > static_cast<double>(std::numeric_limits<float>::max()))
174 {
175 return std::numeric_limits<float>::infinity();
176 }
177 if (value < static_cast<double>(std::numeric_limits<float>::min()))
178 {
179 return 0.0f;
180 }
181 return static_cast<float>(value);
182}
183
184bool strtof_clamp(const std::string &str, float *value)
185{
186 // Try the standard float parsing path first.
187 bool success = pp::numeric_lex_float(str, value);
188
189 // If the standard path doesn't succeed, take the path that can handle the following corner
190 // cases:
191 // 1. The decimal mantissa is very small but the exponent is very large, putting the resulting
192 // number inside the float range.
193 // 2. The decimal mantissa is very large but the exponent is very small, putting the resulting
194 // number inside the float range.
195 // 3. The value is out-of-range and should be evaluated as infinity.
196 // 4. The value is too small and should be evaluated as zero.
197 // See ESSL 3.00.6 section 4.1.4 for the relevant specification.
198 if (!success)
199 *value = NumericLexFloat32OutOfRangeToInfinity(str);
200 return !gl::isInf(*value);
201}
202
Jamie Madill033dae62014-06-18 12:56:28 -0400203GLenum GLVariableType(const TType &type)
204{
205 if (type.getBasicType() == EbtFloat)
206 {
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300207 if (type.isVector())
Jamie Madill033dae62014-06-18 12:56:28 -0400208 {
209 switch (type.getNominalSize())
210 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500211 case 2:
212 return GL_FLOAT_VEC2;
213 case 3:
214 return GL_FLOAT_VEC3;
215 case 4:
216 return GL_FLOAT_VEC4;
217 default:
218 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500219#if !UNREACHABLE_IS_NORETURN
220 return GL_NONE;
221#endif
Jamie Madill033dae62014-06-18 12:56:28 -0400222 }
223 }
224 else if (type.isMatrix())
225 {
226 switch (type.getCols())
227 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500228 case 2:
229 switch (type.getRows())
230 {
231 case 2:
232 return GL_FLOAT_MAT2;
233 case 3:
234 return GL_FLOAT_MAT2x3;
235 case 4:
236 return GL_FLOAT_MAT2x4;
237 default:
238 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500239#if !UNREACHABLE_IS_NORETURN
240 return GL_NONE;
241#endif
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500242 }
Jamie Madill033dae62014-06-18 12:56:28 -0400243
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500244 case 3:
245 switch (type.getRows())
246 {
247 case 2:
248 return GL_FLOAT_MAT3x2;
249 case 3:
250 return GL_FLOAT_MAT3;
251 case 4:
252 return GL_FLOAT_MAT3x4;
253 default:
254 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500255#if !UNREACHABLE_IS_NORETURN
256 return GL_NONE;
257#endif
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500258 }
Jamie Madill033dae62014-06-18 12:56:28 -0400259
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500260 case 4:
261 switch (type.getRows())
262 {
263 case 2:
264 return GL_FLOAT_MAT4x2;
265 case 3:
266 return GL_FLOAT_MAT4x3;
267 case 4:
268 return GL_FLOAT_MAT4;
269 default:
270 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500271#if !UNREACHABLE_IS_NORETURN
272 return GL_NONE;
273#endif
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500274 }
Jamie Madill033dae62014-06-18 12:56:28 -0400275
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500276 default:
277 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500278#if !UNREACHABLE_IS_NORETURN
279 return GL_NONE;
280#endif
Jamie Madill033dae62014-06-18 12:56:28 -0400281 }
282 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500283 else
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300284 {
285 return GL_FLOAT;
286 }
Jamie Madill033dae62014-06-18 12:56:28 -0400287 }
288 else if (type.getBasicType() == EbtInt)
289 {
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300290 if (type.isVector())
Jamie Madill033dae62014-06-18 12:56:28 -0400291 {
292 switch (type.getNominalSize())
293 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500294 case 2:
295 return GL_INT_VEC2;
296 case 3:
297 return GL_INT_VEC3;
298 case 4:
299 return GL_INT_VEC4;
300 default:
301 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500302#if !UNREACHABLE_IS_NORETURN
303 return GL_NONE;
304#endif
Jamie Madill033dae62014-06-18 12:56:28 -0400305 }
306 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500307 else
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300308 {
309 ASSERT(!type.isMatrix());
310 return GL_INT;
311 }
Jamie Madill033dae62014-06-18 12:56:28 -0400312 }
313 else if (type.getBasicType() == EbtUInt)
314 {
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300315 if (type.isVector())
Jamie Madill033dae62014-06-18 12:56:28 -0400316 {
317 switch (type.getNominalSize())
318 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500319 case 2:
320 return GL_UNSIGNED_INT_VEC2;
321 case 3:
322 return GL_UNSIGNED_INT_VEC3;
323 case 4:
324 return GL_UNSIGNED_INT_VEC4;
325 default:
326 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500327#if !UNREACHABLE_IS_NORETURN
328 return GL_NONE;
329#endif
Jamie Madill033dae62014-06-18 12:56:28 -0400330 }
331 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500332 else
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300333 {
334 ASSERT(!type.isMatrix());
335 return GL_UNSIGNED_INT;
336 }
Jamie Madill033dae62014-06-18 12:56:28 -0400337 }
338 else if (type.getBasicType() == EbtBool)
339 {
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300340 if (type.isVector())
Jamie Madill033dae62014-06-18 12:56:28 -0400341 {
342 switch (type.getNominalSize())
343 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500344 case 2:
345 return GL_BOOL_VEC2;
346 case 3:
347 return GL_BOOL_VEC3;
348 case 4:
349 return GL_BOOL_VEC4;
350 default:
351 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500352#if !UNREACHABLE_IS_NORETURN
353 return GL_NONE;
354#endif
Jamie Madill033dae62014-06-18 12:56:28 -0400355 }
356 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500357 else
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300358 {
359 ASSERT(!type.isMatrix());
360 return GL_BOOL;
361 }
Jamie Madill033dae62014-06-18 12:56:28 -0400362 }
363
364 switch (type.getBasicType())
365 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500366 case EbtSampler2D:
367 return GL_SAMPLER_2D;
368 case EbtSampler3D:
369 return GL_SAMPLER_3D;
370 case EbtSamplerCube:
371 return GL_SAMPLER_CUBE;
372 case EbtSamplerExternalOES:
373 return GL_SAMPLER_EXTERNAL_OES;
Andrei Volykhina5527072017-03-22 16:46:30 +0300374 case EbtSamplerExternal2DY2YEXT:
375 return GL_SAMPLER_EXTERNAL_2D_Y2Y_EXT;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500376 case EbtSampler2DRect:
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400377 return GL_SAMPLER_2D_RECT_ANGLE;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500378 case EbtSampler2DArray:
379 return GL_SAMPLER_2D_ARRAY;
JiangYizhou40219322016-12-09 09:50:51 +0800380 case EbtSampler2DMS:
381 return GL_SAMPLER_2D_MULTISAMPLE;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500382 case EbtISampler2D:
383 return GL_INT_SAMPLER_2D;
384 case EbtISampler3D:
385 return GL_INT_SAMPLER_3D;
386 case EbtISamplerCube:
387 return GL_INT_SAMPLER_CUBE;
388 case EbtISampler2DArray:
389 return GL_INT_SAMPLER_2D_ARRAY;
JiangYizhou40219322016-12-09 09:50:51 +0800390 case EbtISampler2DMS:
391 return GL_INT_SAMPLER_2D_MULTISAMPLE;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500392 case EbtUSampler2D:
393 return GL_UNSIGNED_INT_SAMPLER_2D;
394 case EbtUSampler3D:
395 return GL_UNSIGNED_INT_SAMPLER_3D;
396 case EbtUSamplerCube:
397 return GL_UNSIGNED_INT_SAMPLER_CUBE;
398 case EbtUSampler2DArray:
399 return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
JiangYizhou40219322016-12-09 09:50:51 +0800400 case EbtUSampler2DMS:
401 return GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500402 case EbtSampler2DShadow:
403 return GL_SAMPLER_2D_SHADOW;
404 case EbtSamplerCubeShadow:
405 return GL_SAMPLER_CUBE_SHADOW;
406 case EbtSampler2DArrayShadow:
407 return GL_SAMPLER_2D_ARRAY_SHADOW;
408 case EbtImage2D:
409 return GL_IMAGE_2D;
410 case EbtIImage2D:
411 return GL_INT_IMAGE_2D;
412 case EbtUImage2D:
413 return GL_UNSIGNED_INT_IMAGE_2D;
414 case EbtImage2DArray:
415 return GL_IMAGE_2D_ARRAY;
416 case EbtIImage2DArray:
417 return GL_INT_IMAGE_2D_ARRAY;
418 case EbtUImage2DArray:
419 return GL_UNSIGNED_INT_IMAGE_2D_ARRAY;
420 case EbtImage3D:
421 return GL_IMAGE_3D;
422 case EbtIImage3D:
423 return GL_INT_IMAGE_3D;
424 case EbtUImage3D:
425 return GL_UNSIGNED_INT_IMAGE_3D;
426 case EbtImageCube:
427 return GL_IMAGE_CUBE;
428 case EbtIImageCube:
429 return GL_INT_IMAGE_CUBE;
430 case EbtUImageCube:
431 return GL_UNSIGNED_INT_IMAGE_CUBE;
jchen104cdac9e2017-05-08 11:01:20 +0800432 case EbtAtomicCounter:
433 return GL_UNSIGNED_INT_ATOMIC_COUNTER;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500434 default:
435 UNREACHABLE();
Jamie Madill033dae62014-06-18 12:56:28 -0400436 }
437
438 return GL_NONE;
439}
440
441GLenum GLVariablePrecision(const TType &type)
442{
443 if (type.getBasicType() == EbtFloat)
444 {
445 switch (type.getPrecision())
446 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500447 case EbpHigh:
448 return GL_HIGH_FLOAT;
449 case EbpMedium:
450 return GL_MEDIUM_FLOAT;
451 case EbpLow:
452 return GL_LOW_FLOAT;
453 case EbpUndefined:
454 // Should be defined as the default precision by the parser
455 default:
456 UNREACHABLE();
Jamie Madill033dae62014-06-18 12:56:28 -0400457 }
458 }
459 else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt)
460 {
461 switch (type.getPrecision())
462 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500463 case EbpHigh:
464 return GL_HIGH_INT;
465 case EbpMedium:
466 return GL_MEDIUM_INT;
467 case EbpLow:
468 return GL_LOW_INT;
469 case EbpUndefined:
470 // Should be defined as the default precision by the parser
471 default:
472 UNREACHABLE();
Jamie Madill033dae62014-06-18 12:56:28 -0400473 }
474 }
475
476 // Other types (boolean, sampler) don't have a precision
477 return GL_NONE;
478}
479
480TString ArrayString(const TType &type)
481{
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300482 TStringStream arrayString;
Kai Ninomiya57ea5332017-11-22 14:04:48 -0800483 if (!type.isArray())
484 return arrayString.str();
485
486 const TVector<unsigned int> &arraySizes = *type.getArraySizes();
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300487 for (auto arraySizeIter = arraySizes.rbegin(); arraySizeIter != arraySizes.rend();
488 ++arraySizeIter)
Jamie Madill033dae62014-06-18 12:56:28 -0400489 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800490 arrayString << "[";
491 if (*arraySizeIter > 0)
492 {
493 arrayString << (*arraySizeIter);
494 }
495 arrayString << "]";
Jamie Madill033dae62014-06-18 12:56:28 -0400496 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300497 return arrayString.str();
Jamie Madill033dae62014-06-18 12:56:28 -0400498}
499
Olli Etuahofbb1c792018-01-19 16:26:59 +0200500ImmutableString GetTypeName(const TType &type, ShHashFunction64 hashFunction, NameMap *nameMap)
Jamie Madill6276b922017-09-25 02:35:57 -0400501{
502 if (type.getBasicType() == EbtStruct)
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200503 return HashName(type.getStruct(), hashFunction, nameMap);
Jamie Madill6276b922017-09-25 02:35:57 -0400504 else
Olli Etuahofbb1c792018-01-19 16:26:59 +0200505 return ImmutableString(type.getBuiltInTypeNameString());
Jamie Madill6276b922017-09-25 02:35:57 -0400506}
507
Jamie Madill033dae62014-06-18 12:56:28 -0400508bool IsVaryingOut(TQualifier qualifier)
509{
510 switch (qualifier)
511 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500512 case EvqVaryingOut:
513 case EvqSmoothOut:
514 case EvqFlatOut:
515 case EvqCentroidOut:
516 case EvqVertexOut:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800517 case EvqGeometryOut:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500518 return true;
Jamie Madill033dae62014-06-18 12:56:28 -0400519
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500520 default:
521 break;
Jamie Madill033dae62014-06-18 12:56:28 -0400522 }
523
524 return false;
525}
526
527bool IsVaryingIn(TQualifier qualifier)
528{
529 switch (qualifier)
530 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500531 case EvqVaryingIn:
532 case EvqSmoothIn:
533 case EvqFlatIn:
534 case EvqCentroidIn:
535 case EvqFragmentIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800536 case EvqGeometryIn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500537 return true;
Jamie Madill033dae62014-06-18 12:56:28 -0400538
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500539 default:
540 break;
Jamie Madill033dae62014-06-18 12:56:28 -0400541 }
542
543 return false;
544}
545
546bool IsVarying(TQualifier qualifier)
547{
548 return IsVaryingIn(qualifier) || IsVaryingOut(qualifier);
549}
550
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800551bool IsGeometryShaderInput(GLenum shaderType, TQualifier qualifier)
552{
553 return (qualifier == EvqGeometryIn) ||
Jiawei Shaobd924af2017-11-16 15:28:04 +0800554 ((shaderType == GL_GEOMETRY_SHADER_EXT) && IsInterpolationIn(qualifier));
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800555}
556
Jamie Madillf2575982014-06-25 16:04:54 -0400557InterpolationType GetInterpolationType(TQualifier qualifier)
Jamie Madill033dae62014-06-18 12:56:28 -0400558{
559 switch (qualifier)
560 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500561 case EvqFlatIn:
562 case EvqFlatOut:
563 return INTERPOLATION_FLAT;
Jamie Madill033dae62014-06-18 12:56:28 -0400564
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500565 case EvqSmoothIn:
566 case EvqSmoothOut:
567 case EvqVertexOut:
568 case EvqFragmentIn:
569 case EvqVaryingIn:
570 case EvqVaryingOut:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800571 case EvqGeometryIn:
572 case EvqGeometryOut:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500573 return INTERPOLATION_SMOOTH;
Jamie Madill033dae62014-06-18 12:56:28 -0400574
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500575 case EvqCentroidIn:
576 case EvqCentroidOut:
577 return INTERPOLATION_CENTROID;
Jamie Madill033dae62014-06-18 12:56:28 -0400578
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500579 default:
580 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500581#if !UNREACHABLE_IS_NORETURN
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500582 return INTERPOLATION_SMOOTH;
Nico Weberb5db2b42018-02-12 15:31:56 -0500583#endif
Jamie Madill033dae62014-06-18 12:56:28 -0400584 }
585}
586
Corentin Wallez509e4562016-08-25 14:55:44 -0400587TType GetShaderVariableBasicType(const sh::ShaderVariable &var)
Zhenyao Mo72111912016-07-20 17:45:56 -0700588{
Corentin Wallez509e4562016-08-25 14:55:44 -0400589 switch (var.type)
Zhenyao Mo72111912016-07-20 17:45:56 -0700590 {
Qin Jiajia7835b522016-10-08 11:20:17 +0800591 case GL_BOOL:
592 return TType(EbtBool);
593 case GL_BOOL_VEC2:
594 return TType(EbtBool, 2);
595 case GL_BOOL_VEC3:
596 return TType(EbtBool, 3);
597 case GL_BOOL_VEC4:
598 return TType(EbtBool, 4);
Zhenyao Mo72111912016-07-20 17:45:56 -0700599 case GL_FLOAT:
600 return TType(EbtFloat);
601 case GL_FLOAT_VEC2:
602 return TType(EbtFloat, 2);
603 case GL_FLOAT_VEC3:
604 return TType(EbtFloat, 3);
605 case GL_FLOAT_VEC4:
606 return TType(EbtFloat, 4);
607 case GL_FLOAT_MAT2:
608 return TType(EbtFloat, 2, 2);
609 case GL_FLOAT_MAT3:
610 return TType(EbtFloat, 3, 3);
611 case GL_FLOAT_MAT4:
612 return TType(EbtFloat, 4, 4);
613 case GL_FLOAT_MAT2x3:
614 return TType(EbtFloat, 2, 3);
615 case GL_FLOAT_MAT2x4:
616 return TType(EbtFloat, 2, 4);
617 case GL_FLOAT_MAT3x2:
618 return TType(EbtFloat, 3, 2);
619 case GL_FLOAT_MAT3x4:
620 return TType(EbtFloat, 3, 4);
621 case GL_FLOAT_MAT4x2:
622 return TType(EbtFloat, 4, 2);
623 case GL_FLOAT_MAT4x3:
624 return TType(EbtFloat, 4, 3);
625 case GL_INT:
626 return TType(EbtInt);
627 case GL_INT_VEC2:
628 return TType(EbtInt, 2);
629 case GL_INT_VEC3:
630 return TType(EbtInt, 3);
631 case GL_INT_VEC4:
632 return TType(EbtInt, 4);
633 case GL_UNSIGNED_INT:
634 return TType(EbtUInt);
635 case GL_UNSIGNED_INT_VEC2:
636 return TType(EbtUInt, 2);
637 case GL_UNSIGNED_INT_VEC3:
638 return TType(EbtUInt, 3);
639 case GL_UNSIGNED_INT_VEC4:
640 return TType(EbtUInt, 4);
641 default:
642 UNREACHABLE();
Nico Weberb5db2b42018-02-12 15:31:56 -0500643#if !UNREACHABLE_IS_NORETURN
Zhenyao Mo72111912016-07-20 17:45:56 -0700644 return TType();
Nico Weberb5db2b42018-02-12 15:31:56 -0500645#endif
Zhenyao Mo72111912016-07-20 17:45:56 -0700646 }
647}
648
Martin Radev70866b82016-07-22 15:27:42 +0300649// GLSL ES 1.0.17 4.6.1 The Invariant Qualifier
650bool CanBeInvariantESSL1(TQualifier qualifier)
651{
652 return IsVaryingIn(qualifier) || IsVaryingOut(qualifier) ||
653 IsBuiltinOutputVariable(qualifier) ||
654 (IsBuiltinFragmentInputVariable(qualifier) && qualifier != EvqFrontFacing);
Jamie Madill033dae62014-06-18 12:56:28 -0400655}
Martin Radev70866b82016-07-22 15:27:42 +0300656
657// GLSL ES 3.00 Revision 6, 4.6.1 The Invariant Qualifier
658// GLSL ES 3.10 Revision 4, 4.8.1 The Invariant Qualifier
659bool CanBeInvariantESSL3OrGreater(TQualifier qualifier)
660{
661 return IsVaryingOut(qualifier) || qualifier == EvqFragmentOut ||
662 IsBuiltinOutputVariable(qualifier);
663}
664
665bool IsBuiltinOutputVariable(TQualifier qualifier)
666{
667 switch (qualifier)
668 {
669 case EvqPosition:
670 case EvqPointSize:
671 case EvqFragDepth:
672 case EvqFragDepthEXT:
673 case EvqFragColor:
674 case EvqSecondaryFragColorEXT:
675 case EvqFragData:
676 case EvqSecondaryFragDataEXT:
677 return true;
678 default:
679 break;
680 }
681 return false;
682}
683
684bool IsBuiltinFragmentInputVariable(TQualifier qualifier)
685{
686 switch (qualifier)
687 {
688 case EvqFragCoord:
689 case EvqPointCoord:
690 case EvqFrontFacing:
691 return true;
692 default:
693 break;
694 }
695 return false;
696}
Martin Radeve469de82017-07-04 11:58:35 +0300697
698bool IsOutputESSL(ShShaderOutput output)
699{
700 return output == SH_ESSL_OUTPUT;
701}
702
703bool IsOutputGLSL(ShShaderOutput output)
704{
705 switch (output)
706 {
707 case SH_GLSL_130_OUTPUT:
708 case SH_GLSL_140_OUTPUT:
709 case SH_GLSL_150_CORE_OUTPUT:
710 case SH_GLSL_330_CORE_OUTPUT:
711 case SH_GLSL_400_CORE_OUTPUT:
712 case SH_GLSL_410_CORE_OUTPUT:
713 case SH_GLSL_420_CORE_OUTPUT:
714 case SH_GLSL_430_CORE_OUTPUT:
715 case SH_GLSL_440_CORE_OUTPUT:
716 case SH_GLSL_450_CORE_OUTPUT:
717 case SH_GLSL_COMPATIBILITY_OUTPUT:
718 return true;
719 default:
720 break;
721 }
722 return false;
723}
724bool IsOutputHLSL(ShShaderOutput output)
725{
726 switch (output)
727 {
728 case SH_HLSL_3_0_OUTPUT:
729 case SH_HLSL_4_1_OUTPUT:
730 case SH_HLSL_4_0_FL9_3_OUTPUT:
731 return true;
732 default:
733 break;
734 }
735 return false;
736}
737bool IsOutputVulkan(ShShaderOutput output)
738{
739 return output == SH_GLSL_VULKAN_OUTPUT;
740}
741
Martin Radev70866b82016-07-22 15:27:42 +0300742} // namespace sh