daniel@transgaming.com | 91ed149 | 2010-10-29 03:11:43 +0000 | [diff] [blame] | 1 | // |
| 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 Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/util.h" |
daniel@transgaming.com | 91ed149 | 2010-10-29 03:11:43 +0000 | [diff] [blame] | 8 | |
Zhenyao Mo | f1d723c | 2013-09-23 14:57:07 -0400 | [diff] [blame] | 9 | #include <limits> |
| 10 | |
Olli Etuaho | 99bd5f4 | 2016-11-07 12:44:29 +0000 | [diff] [blame] | 11 | #include "common/utilities.h" |
Zhenyao Mo | cc4ec64 | 2013-09-23 14:57:10 -0400 | [diff] [blame] | 12 | #include "compiler/preprocessor/numeric_lex.h" |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 13 | #include "compiler/translator/SymbolTable.h" |
Zhenyao Mo | f1d723c | 2013-09-23 14:57:07 -0400 | [diff] [blame] | 14 | |
Olli Etuaho | f541f52 | 2015-10-13 12:21:01 +0300 | [diff] [blame] | 15 | bool atoi_clamp(const char *str, unsigned int *value) |
Zhenyao Mo | f1d723c | 2013-09-23 14:57:07 -0400 | [diff] [blame] | 16 | { |
Zhenyao Mo | cc4ec64 | 2013-09-23 14:57:10 -0400 | [diff] [blame] | 17 | bool success = pp::numeric_lex_int(str, value); |
| 18 | if (!success) |
Olli Etuaho | f541f52 | 2015-10-13 12:21:01 +0300 | [diff] [blame] | 19 | *value = std::numeric_limits<unsigned int>::max(); |
Zhenyao Mo | cc4ec64 | 2013-09-23 14:57:10 -0400 | [diff] [blame] | 20 | return success; |
Zhenyao Mo | f1d723c | 2013-09-23 14:57:07 -0400 | [diff] [blame] | 21 | } |
| 22 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 23 | namespace sh |
| 24 | { |
| 25 | |
Olli Etuaho | 99bd5f4 | 2016-11-07 12:44:29 +0000 | [diff] [blame] | 26 | float NumericLexFloat32OutOfRangeToInfinity(const std::string &str) |
| 27 | { |
| 28 | // Parses a decimal string using scientific notation into a floating point number. |
| 29 | // Out-of-range values are converted to infinity. Values that are too small to be |
| 30 | // represented are converted to zero. |
| 31 | |
| 32 | // The mantissa in decimal scientific notation. The magnitude of the mantissa integer does not |
| 33 | // matter. |
| 34 | unsigned int decimalMantissa = 0; |
| 35 | size_t i = 0; |
| 36 | bool decimalPointSeen = false; |
| 37 | bool nonZeroSeenInMantissa = false; |
| 38 | |
| 39 | // The exponent offset reflects the position of the decimal point. |
| 40 | int exponentOffset = -1; |
| 41 | while (i < str.length()) |
| 42 | { |
| 43 | const char c = str[i]; |
| 44 | if (c == 'e' || c == 'E') |
| 45 | { |
| 46 | break; |
| 47 | } |
| 48 | if (c == '.') |
| 49 | { |
| 50 | decimalPointSeen = true; |
| 51 | ++i; |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | unsigned int digit = static_cast<unsigned int>(c - '0'); |
| 56 | ASSERT(digit < 10u); |
| 57 | if (digit != 0u) |
| 58 | { |
| 59 | nonZeroSeenInMantissa = true; |
| 60 | } |
| 61 | if (nonZeroSeenInMantissa) |
| 62 | { |
| 63 | // Add bits to the mantissa until space runs out in 32-bit int. This should be |
| 64 | // enough precision to make the resulting binary mantissa accurate to 1 ULP. |
| 65 | if (decimalMantissa <= (std::numeric_limits<unsigned int>::max() - 9u) / 10u) |
| 66 | { |
| 67 | decimalMantissa = decimalMantissa * 10u + digit; |
| 68 | } |
| 69 | if (!decimalPointSeen) |
| 70 | { |
| 71 | ++exponentOffset; |
| 72 | } |
| 73 | } |
| 74 | else if (decimalPointSeen) |
| 75 | { |
| 76 | --exponentOffset; |
| 77 | } |
| 78 | ++i; |
| 79 | } |
| 80 | if (decimalMantissa == 0) |
| 81 | { |
| 82 | return 0.0f; |
| 83 | } |
| 84 | int exponent = 0; |
| 85 | if (i < str.length()) |
| 86 | { |
| 87 | ASSERT(str[i] == 'e' || str[i] == 'E'); |
| 88 | ++i; |
| 89 | bool exponentOutOfRange = false; |
| 90 | bool negativeExponent = false; |
| 91 | if (str[i] == '-') |
| 92 | { |
| 93 | negativeExponent = true; |
| 94 | ++i; |
| 95 | } |
| 96 | else if (str[i] == '+') |
| 97 | { |
| 98 | ++i; |
| 99 | } |
| 100 | while (i < str.length()) |
| 101 | { |
| 102 | const char c = str[i]; |
| 103 | unsigned int digit = static_cast<unsigned int>(c - '0'); |
| 104 | ASSERT(digit < 10u); |
| 105 | if (exponent <= (std::numeric_limits<int>::max() - 9) / 10) |
| 106 | { |
| 107 | exponent = exponent * 10 + digit; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | exponentOutOfRange = true; |
| 112 | } |
| 113 | ++i; |
| 114 | } |
| 115 | if (negativeExponent) |
| 116 | { |
| 117 | exponent = -exponent; |
| 118 | } |
| 119 | if (exponentOutOfRange) |
| 120 | { |
| 121 | if (negativeExponent) |
| 122 | { |
| 123 | return 0.0f; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | return std::numeric_limits<float>::infinity(); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | // Do the calculation in 64-bit to avoid overflow. |
| 132 | long long exponentLong = |
| 133 | static_cast<long long>(exponent) + static_cast<long long>(exponentOffset); |
| 134 | if (exponentLong > std::numeric_limits<float>::max_exponent10) |
| 135 | { |
| 136 | return std::numeric_limits<float>::infinity(); |
| 137 | } |
| 138 | else if (exponentLong < std::numeric_limits<float>::min_exponent10) |
| 139 | { |
| 140 | return 0.0f; |
| 141 | } |
| 142 | // The exponent is in range, so we need to actually evaluate the float. |
| 143 | exponent = static_cast<int>(exponentLong); |
| 144 | double value = decimalMantissa; |
| 145 | |
| 146 | // Calculate the exponent offset to normalize the mantissa. |
| 147 | int normalizationExponentOffset = 0; |
| 148 | while (decimalMantissa >= 10u) |
| 149 | { |
| 150 | --normalizationExponentOffset; |
| 151 | decimalMantissa /= 10u; |
| 152 | } |
| 153 | // Apply the exponent. |
| 154 | value *= std::pow(10.0, static_cast<double>(exponent + normalizationExponentOffset)); |
| 155 | if (value > static_cast<double>(std::numeric_limits<float>::max())) |
| 156 | { |
| 157 | return std::numeric_limits<float>::infinity(); |
| 158 | } |
| 159 | if (value < static_cast<double>(std::numeric_limits<float>::min())) |
| 160 | { |
| 161 | return 0.0f; |
| 162 | } |
| 163 | return static_cast<float>(value); |
| 164 | } |
| 165 | |
| 166 | bool strtof_clamp(const std::string &str, float *value) |
| 167 | { |
| 168 | // Try the standard float parsing path first. |
| 169 | bool success = pp::numeric_lex_float(str, value); |
| 170 | |
| 171 | // If the standard path doesn't succeed, take the path that can handle the following corner |
| 172 | // cases: |
| 173 | // 1. The decimal mantissa is very small but the exponent is very large, putting the resulting |
| 174 | // number inside the float range. |
| 175 | // 2. The decimal mantissa is very large but the exponent is very small, putting the resulting |
| 176 | // number inside the float range. |
| 177 | // 3. The value is out-of-range and should be evaluated as infinity. |
| 178 | // 4. The value is too small and should be evaluated as zero. |
| 179 | // See ESSL 3.00.6 section 4.1.4 for the relevant specification. |
| 180 | if (!success) |
| 181 | *value = NumericLexFloat32OutOfRangeToInfinity(str); |
| 182 | return !gl::isInf(*value); |
| 183 | } |
| 184 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 185 | GLenum GLVariableType(const TType &type) |
| 186 | { |
| 187 | if (type.getBasicType() == EbtFloat) |
| 188 | { |
| 189 | if (type.isScalar()) |
| 190 | { |
| 191 | return GL_FLOAT; |
| 192 | } |
| 193 | else if (type.isVector()) |
| 194 | { |
| 195 | switch (type.getNominalSize()) |
| 196 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 197 | case 2: |
| 198 | return GL_FLOAT_VEC2; |
| 199 | case 3: |
| 200 | return GL_FLOAT_VEC3; |
| 201 | case 4: |
| 202 | return GL_FLOAT_VEC4; |
| 203 | default: |
| 204 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | else if (type.isMatrix()) |
| 208 | { |
| 209 | switch (type.getCols()) |
| 210 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 211 | case 2: |
| 212 | switch (type.getRows()) |
| 213 | { |
| 214 | case 2: |
| 215 | return GL_FLOAT_MAT2; |
| 216 | case 3: |
| 217 | return GL_FLOAT_MAT2x3; |
| 218 | case 4: |
| 219 | return GL_FLOAT_MAT2x4; |
| 220 | default: |
| 221 | UNREACHABLE(); |
| 222 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 223 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 224 | case 3: |
| 225 | switch (type.getRows()) |
| 226 | { |
| 227 | case 2: |
| 228 | return GL_FLOAT_MAT3x2; |
| 229 | case 3: |
| 230 | return GL_FLOAT_MAT3; |
| 231 | case 4: |
| 232 | return GL_FLOAT_MAT3x4; |
| 233 | default: |
| 234 | UNREACHABLE(); |
| 235 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 236 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 237 | case 4: |
| 238 | switch (type.getRows()) |
| 239 | { |
| 240 | case 2: |
| 241 | return GL_FLOAT_MAT4x2; |
| 242 | case 3: |
| 243 | return GL_FLOAT_MAT4x3; |
| 244 | case 4: |
| 245 | return GL_FLOAT_MAT4; |
| 246 | default: |
| 247 | UNREACHABLE(); |
| 248 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 249 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 250 | default: |
| 251 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 254 | else |
| 255 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 256 | } |
| 257 | else if (type.getBasicType() == EbtInt) |
| 258 | { |
| 259 | if (type.isScalar()) |
| 260 | { |
| 261 | return GL_INT; |
| 262 | } |
| 263 | else if (type.isVector()) |
| 264 | { |
| 265 | switch (type.getNominalSize()) |
| 266 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 267 | case 2: |
| 268 | return GL_INT_VEC2; |
| 269 | case 3: |
| 270 | return GL_INT_VEC3; |
| 271 | case 4: |
| 272 | return GL_INT_VEC4; |
| 273 | default: |
| 274 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 275 | } |
| 276 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 277 | else |
| 278 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 279 | } |
| 280 | else if (type.getBasicType() == EbtUInt) |
| 281 | { |
| 282 | if (type.isScalar()) |
| 283 | { |
| 284 | return GL_UNSIGNED_INT; |
| 285 | } |
| 286 | else if (type.isVector()) |
| 287 | { |
| 288 | switch (type.getNominalSize()) |
| 289 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 290 | case 2: |
| 291 | return GL_UNSIGNED_INT_VEC2; |
| 292 | case 3: |
| 293 | return GL_UNSIGNED_INT_VEC3; |
| 294 | case 4: |
| 295 | return GL_UNSIGNED_INT_VEC4; |
| 296 | default: |
| 297 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 298 | } |
| 299 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 300 | else |
| 301 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 302 | } |
| 303 | else if (type.getBasicType() == EbtBool) |
| 304 | { |
| 305 | if (type.isScalar()) |
| 306 | { |
| 307 | return GL_BOOL; |
| 308 | } |
| 309 | else if (type.isVector()) |
| 310 | { |
| 311 | switch (type.getNominalSize()) |
| 312 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 313 | case 2: |
| 314 | return GL_BOOL_VEC2; |
| 315 | case 3: |
| 316 | return GL_BOOL_VEC3; |
| 317 | case 4: |
| 318 | return GL_BOOL_VEC4; |
| 319 | default: |
| 320 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 321 | } |
| 322 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 323 | else |
| 324 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | switch (type.getBasicType()) |
| 328 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 329 | case EbtSampler2D: |
| 330 | return GL_SAMPLER_2D; |
| 331 | case EbtSampler3D: |
| 332 | return GL_SAMPLER_3D; |
| 333 | case EbtSamplerCube: |
| 334 | return GL_SAMPLER_CUBE; |
| 335 | case EbtSamplerExternalOES: |
| 336 | return GL_SAMPLER_EXTERNAL_OES; |
| 337 | case EbtSampler2DRect: |
| 338 | return GL_SAMPLER_2D_RECT_ARB; |
| 339 | case EbtSampler2DArray: |
| 340 | return GL_SAMPLER_2D_ARRAY; |
JiangYizhou | 4021932 | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 341 | case EbtSampler2DMS: |
| 342 | return GL_SAMPLER_2D_MULTISAMPLE; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 343 | case EbtISampler2D: |
| 344 | return GL_INT_SAMPLER_2D; |
| 345 | case EbtISampler3D: |
| 346 | return GL_INT_SAMPLER_3D; |
| 347 | case EbtISamplerCube: |
| 348 | return GL_INT_SAMPLER_CUBE; |
| 349 | case EbtISampler2DArray: |
| 350 | return GL_INT_SAMPLER_2D_ARRAY; |
JiangYizhou | 4021932 | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 351 | case EbtISampler2DMS: |
| 352 | return GL_INT_SAMPLER_2D_MULTISAMPLE; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 353 | case EbtUSampler2D: |
| 354 | return GL_UNSIGNED_INT_SAMPLER_2D; |
| 355 | case EbtUSampler3D: |
| 356 | return GL_UNSIGNED_INT_SAMPLER_3D; |
| 357 | case EbtUSamplerCube: |
| 358 | return GL_UNSIGNED_INT_SAMPLER_CUBE; |
| 359 | case EbtUSampler2DArray: |
| 360 | return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY; |
JiangYizhou | 4021932 | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 361 | case EbtUSampler2DMS: |
| 362 | return GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 363 | case EbtSampler2DShadow: |
| 364 | return GL_SAMPLER_2D_SHADOW; |
| 365 | case EbtSamplerCubeShadow: |
| 366 | return GL_SAMPLER_CUBE_SHADOW; |
| 367 | case EbtSampler2DArrayShadow: |
| 368 | return GL_SAMPLER_2D_ARRAY_SHADOW; |
| 369 | case EbtImage2D: |
| 370 | return GL_IMAGE_2D; |
| 371 | case EbtIImage2D: |
| 372 | return GL_INT_IMAGE_2D; |
| 373 | case EbtUImage2D: |
| 374 | return GL_UNSIGNED_INT_IMAGE_2D; |
| 375 | case EbtImage2DArray: |
| 376 | return GL_IMAGE_2D_ARRAY; |
| 377 | case EbtIImage2DArray: |
| 378 | return GL_INT_IMAGE_2D_ARRAY; |
| 379 | case EbtUImage2DArray: |
| 380 | return GL_UNSIGNED_INT_IMAGE_2D_ARRAY; |
| 381 | case EbtImage3D: |
| 382 | return GL_IMAGE_3D; |
| 383 | case EbtIImage3D: |
| 384 | return GL_INT_IMAGE_3D; |
| 385 | case EbtUImage3D: |
| 386 | return GL_UNSIGNED_INT_IMAGE_3D; |
| 387 | case EbtImageCube: |
| 388 | return GL_IMAGE_CUBE; |
| 389 | case EbtIImageCube: |
| 390 | return GL_INT_IMAGE_CUBE; |
| 391 | case EbtUImageCube: |
| 392 | return GL_UNSIGNED_INT_IMAGE_CUBE; |
| 393 | default: |
| 394 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return GL_NONE; |
| 398 | } |
| 399 | |
| 400 | GLenum GLVariablePrecision(const TType &type) |
| 401 | { |
| 402 | if (type.getBasicType() == EbtFloat) |
| 403 | { |
| 404 | switch (type.getPrecision()) |
| 405 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 406 | case EbpHigh: |
| 407 | return GL_HIGH_FLOAT; |
| 408 | case EbpMedium: |
| 409 | return GL_MEDIUM_FLOAT; |
| 410 | case EbpLow: |
| 411 | return GL_LOW_FLOAT; |
| 412 | case EbpUndefined: |
| 413 | // Should be defined as the default precision by the parser |
| 414 | default: |
| 415 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt) |
| 419 | { |
| 420 | switch (type.getPrecision()) |
| 421 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 422 | case EbpHigh: |
| 423 | return GL_HIGH_INT; |
| 424 | case EbpMedium: |
| 425 | return GL_MEDIUM_INT; |
| 426 | case EbpLow: |
| 427 | return GL_LOW_INT; |
| 428 | case EbpUndefined: |
| 429 | // Should be defined as the default precision by the parser |
| 430 | default: |
| 431 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | // Other types (boolean, sampler) don't have a precision |
| 436 | return GL_NONE; |
| 437 | } |
| 438 | |
| 439 | TString ArrayString(const TType &type) |
| 440 | { |
| 441 | if (!type.isArray()) |
| 442 | { |
| 443 | return ""; |
| 444 | } |
| 445 | |
| 446 | return "[" + str(type.getArraySize()) + "]"; |
| 447 | } |
| 448 | |
| 449 | bool IsVaryingOut(TQualifier qualifier) |
| 450 | { |
| 451 | switch (qualifier) |
| 452 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 453 | case EvqVaryingOut: |
| 454 | case EvqSmoothOut: |
| 455 | case EvqFlatOut: |
| 456 | case EvqCentroidOut: |
| 457 | case EvqVertexOut: |
| 458 | return true; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 459 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 460 | default: |
| 461 | break; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | bool IsVaryingIn(TQualifier qualifier) |
| 468 | { |
| 469 | switch (qualifier) |
| 470 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 471 | case EvqVaryingIn: |
| 472 | case EvqSmoothIn: |
| 473 | case EvqFlatIn: |
| 474 | case EvqCentroidIn: |
| 475 | case EvqFragmentIn: |
| 476 | return true; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 477 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 478 | default: |
| 479 | break; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | bool IsVarying(TQualifier qualifier) |
| 486 | { |
| 487 | return IsVaryingIn(qualifier) || IsVaryingOut(qualifier); |
| 488 | } |
| 489 | |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 490 | InterpolationType GetInterpolationType(TQualifier qualifier) |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 491 | { |
| 492 | switch (qualifier) |
| 493 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 494 | case EvqFlatIn: |
| 495 | case EvqFlatOut: |
| 496 | return INTERPOLATION_FLAT; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 497 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 498 | case EvqSmoothIn: |
| 499 | case EvqSmoothOut: |
| 500 | case EvqVertexOut: |
| 501 | case EvqFragmentIn: |
| 502 | case EvqVaryingIn: |
| 503 | case EvqVaryingOut: |
| 504 | return INTERPOLATION_SMOOTH; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 505 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 506 | case EvqCentroidIn: |
| 507 | case EvqCentroidOut: |
| 508 | return INTERPOLATION_CENTROID; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 509 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 510 | default: |
| 511 | UNREACHABLE(); |
| 512 | return INTERPOLATION_SMOOTH; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 516 | TType GetShaderVariableBasicType(const sh::ShaderVariable &var) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 517 | { |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 518 | switch (var.type) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 519 | { |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 520 | case GL_BOOL: |
| 521 | return TType(EbtBool); |
| 522 | case GL_BOOL_VEC2: |
| 523 | return TType(EbtBool, 2); |
| 524 | case GL_BOOL_VEC3: |
| 525 | return TType(EbtBool, 3); |
| 526 | case GL_BOOL_VEC4: |
| 527 | return TType(EbtBool, 4); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 528 | case GL_FLOAT: |
| 529 | return TType(EbtFloat); |
| 530 | case GL_FLOAT_VEC2: |
| 531 | return TType(EbtFloat, 2); |
| 532 | case GL_FLOAT_VEC3: |
| 533 | return TType(EbtFloat, 3); |
| 534 | case GL_FLOAT_VEC4: |
| 535 | return TType(EbtFloat, 4); |
| 536 | case GL_FLOAT_MAT2: |
| 537 | return TType(EbtFloat, 2, 2); |
| 538 | case GL_FLOAT_MAT3: |
| 539 | return TType(EbtFloat, 3, 3); |
| 540 | case GL_FLOAT_MAT4: |
| 541 | return TType(EbtFloat, 4, 4); |
| 542 | case GL_FLOAT_MAT2x3: |
| 543 | return TType(EbtFloat, 2, 3); |
| 544 | case GL_FLOAT_MAT2x4: |
| 545 | return TType(EbtFloat, 2, 4); |
| 546 | case GL_FLOAT_MAT3x2: |
| 547 | return TType(EbtFloat, 3, 2); |
| 548 | case GL_FLOAT_MAT3x4: |
| 549 | return TType(EbtFloat, 3, 4); |
| 550 | case GL_FLOAT_MAT4x2: |
| 551 | return TType(EbtFloat, 4, 2); |
| 552 | case GL_FLOAT_MAT4x3: |
| 553 | return TType(EbtFloat, 4, 3); |
| 554 | case GL_INT: |
| 555 | return TType(EbtInt); |
| 556 | case GL_INT_VEC2: |
| 557 | return TType(EbtInt, 2); |
| 558 | case GL_INT_VEC3: |
| 559 | return TType(EbtInt, 3); |
| 560 | case GL_INT_VEC4: |
| 561 | return TType(EbtInt, 4); |
| 562 | case GL_UNSIGNED_INT: |
| 563 | return TType(EbtUInt); |
| 564 | case GL_UNSIGNED_INT_VEC2: |
| 565 | return TType(EbtUInt, 2); |
| 566 | case GL_UNSIGNED_INT_VEC3: |
| 567 | return TType(EbtUInt, 3); |
| 568 | case GL_UNSIGNED_INT_VEC4: |
| 569 | return TType(EbtUInt, 4); |
| 570 | default: |
| 571 | UNREACHABLE(); |
| 572 | return TType(); |
| 573 | } |
| 574 | } |
| 575 | |
Geoff Lang | 156d719 | 2016-07-21 16:11:00 -0400 | [diff] [blame] | 576 | TOperator TypeToConstructorOperator(const TType &type) |
| 577 | { |
| 578 | switch (type.getBasicType()) |
| 579 | { |
| 580 | case EbtFloat: |
| 581 | if (type.isMatrix()) |
| 582 | { |
| 583 | switch (type.getCols()) |
| 584 | { |
| 585 | case 2: |
| 586 | switch (type.getRows()) |
| 587 | { |
| 588 | case 2: |
| 589 | return EOpConstructMat2; |
| 590 | case 3: |
| 591 | return EOpConstructMat2x3; |
| 592 | case 4: |
| 593 | return EOpConstructMat2x4; |
| 594 | default: |
| 595 | break; |
| 596 | } |
| 597 | break; |
| 598 | |
| 599 | case 3: |
| 600 | switch (type.getRows()) |
| 601 | { |
| 602 | case 2: |
| 603 | return EOpConstructMat3x2; |
| 604 | case 3: |
| 605 | return EOpConstructMat3; |
| 606 | case 4: |
| 607 | return EOpConstructMat3x4; |
| 608 | default: |
| 609 | break; |
| 610 | } |
| 611 | break; |
| 612 | |
| 613 | case 4: |
| 614 | switch (type.getRows()) |
| 615 | { |
| 616 | case 2: |
| 617 | return EOpConstructMat4x2; |
| 618 | case 3: |
| 619 | return EOpConstructMat4x3; |
| 620 | case 4: |
| 621 | return EOpConstructMat4; |
| 622 | default: |
| 623 | break; |
| 624 | } |
| 625 | break; |
| 626 | } |
| 627 | } |
| 628 | else |
| 629 | { |
| 630 | switch (type.getNominalSize()) |
| 631 | { |
| 632 | case 1: |
| 633 | return EOpConstructFloat; |
| 634 | case 2: |
| 635 | return EOpConstructVec2; |
| 636 | case 3: |
| 637 | return EOpConstructVec3; |
| 638 | case 4: |
| 639 | return EOpConstructVec4; |
| 640 | default: |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | break; |
| 645 | |
| 646 | case EbtInt: |
| 647 | switch (type.getNominalSize()) |
| 648 | { |
| 649 | case 1: |
| 650 | return EOpConstructInt; |
| 651 | case 2: |
| 652 | return EOpConstructIVec2; |
| 653 | case 3: |
| 654 | return EOpConstructIVec3; |
| 655 | case 4: |
| 656 | return EOpConstructIVec4; |
| 657 | default: |
| 658 | break; |
| 659 | } |
| 660 | break; |
| 661 | |
| 662 | case EbtUInt: |
| 663 | switch (type.getNominalSize()) |
| 664 | { |
| 665 | case 1: |
| 666 | return EOpConstructUInt; |
| 667 | case 2: |
| 668 | return EOpConstructUVec2; |
| 669 | case 3: |
| 670 | return EOpConstructUVec3; |
| 671 | case 4: |
| 672 | return EOpConstructUVec4; |
| 673 | default: |
| 674 | break; |
| 675 | } |
| 676 | break; |
| 677 | |
| 678 | case EbtBool: |
| 679 | switch (type.getNominalSize()) |
| 680 | { |
| 681 | case 1: |
| 682 | return EOpConstructBool; |
| 683 | case 2: |
| 684 | return EOpConstructBVec2; |
| 685 | case 3: |
| 686 | return EOpConstructBVec3; |
| 687 | case 4: |
| 688 | return EOpConstructBVec4; |
| 689 | default: |
| 690 | break; |
| 691 | } |
| 692 | break; |
| 693 | |
| 694 | case EbtStruct: |
| 695 | return EOpConstructStruct; |
| 696 | |
| 697 | default: |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | return EOpNull; |
| 702 | } |
| 703 | |
Martin Radev | 70866b8 | 2016-07-22 15:27:42 +0300 | [diff] [blame] | 704 | // GLSL ES 1.0.17 4.6.1 The Invariant Qualifier |
| 705 | bool CanBeInvariantESSL1(TQualifier qualifier) |
| 706 | { |
| 707 | return IsVaryingIn(qualifier) || IsVaryingOut(qualifier) || |
| 708 | IsBuiltinOutputVariable(qualifier) || |
| 709 | (IsBuiltinFragmentInputVariable(qualifier) && qualifier != EvqFrontFacing); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 710 | } |
Martin Radev | 70866b8 | 2016-07-22 15:27:42 +0300 | [diff] [blame] | 711 | |
| 712 | // GLSL ES 3.00 Revision 6, 4.6.1 The Invariant Qualifier |
| 713 | // GLSL ES 3.10 Revision 4, 4.8.1 The Invariant Qualifier |
| 714 | bool CanBeInvariantESSL3OrGreater(TQualifier qualifier) |
| 715 | { |
| 716 | return IsVaryingOut(qualifier) || qualifier == EvqFragmentOut || |
| 717 | IsBuiltinOutputVariable(qualifier); |
| 718 | } |
| 719 | |
| 720 | bool IsBuiltinOutputVariable(TQualifier qualifier) |
| 721 | { |
| 722 | switch (qualifier) |
| 723 | { |
| 724 | case EvqPosition: |
| 725 | case EvqPointSize: |
| 726 | case EvqFragDepth: |
| 727 | case EvqFragDepthEXT: |
| 728 | case EvqFragColor: |
| 729 | case EvqSecondaryFragColorEXT: |
| 730 | case EvqFragData: |
| 731 | case EvqSecondaryFragDataEXT: |
| 732 | return true; |
| 733 | default: |
| 734 | break; |
| 735 | } |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | bool IsBuiltinFragmentInputVariable(TQualifier qualifier) |
| 740 | { |
| 741 | switch (qualifier) |
| 742 | { |
| 743 | case EvqFragCoord: |
| 744 | case EvqPointCoord: |
| 745 | case EvqFrontFacing: |
| 746 | return true; |
| 747 | default: |
| 748 | break; |
| 749 | } |
| 750 | return false; |
| 751 | } |
| 752 | } // namespace sh |