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 | |
Jiawei Shao | 8e4b355 | 2017-08-30 14:20:58 +0800 | [diff] [blame] | 26 | namespace |
| 27 | { |
| 28 | |
| 29 | bool 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 Etuaho | 99bd5f4 | 2016-11-07 12:44:29 +0000 | [diff] [blame] | 44 | float 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 | |
| 184 | bool 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 Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 203 | GLenum GLVariableType(const TType &type) |
| 204 | { |
| 205 | if (type.getBasicType() == EbtFloat) |
| 206 | { |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 207 | if (type.isVector()) |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 208 | { |
| 209 | switch (type.getNominalSize()) |
| 210 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 211 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 219 | #if !UNREACHABLE_IS_NORETURN |
| 220 | return GL_NONE; |
| 221 | #endif |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | else if (type.isMatrix()) |
| 225 | { |
| 226 | switch (type.getCols()) |
| 227 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 228 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 239 | #if !UNREACHABLE_IS_NORETURN |
| 240 | return GL_NONE; |
| 241 | #endif |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 242 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 243 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 244 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 255 | #if !UNREACHABLE_IS_NORETURN |
| 256 | return GL_NONE; |
| 257 | #endif |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 258 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 259 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 260 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 271 | #if !UNREACHABLE_IS_NORETURN |
| 272 | return GL_NONE; |
| 273 | #endif |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 274 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 275 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 276 | default: |
| 277 | UNREACHABLE(); |
Nico Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 278 | #if !UNREACHABLE_IS_NORETURN |
| 279 | return GL_NONE; |
| 280 | #endif |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 281 | } |
| 282 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 283 | else |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 284 | { |
| 285 | return GL_FLOAT; |
| 286 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 287 | } |
| 288 | else if (type.getBasicType() == EbtInt) |
| 289 | { |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 290 | if (type.isVector()) |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 291 | { |
| 292 | switch (type.getNominalSize()) |
| 293 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 294 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 302 | #if !UNREACHABLE_IS_NORETURN |
| 303 | return GL_NONE; |
| 304 | #endif |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 305 | } |
| 306 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 307 | else |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 308 | { |
| 309 | ASSERT(!type.isMatrix()); |
| 310 | return GL_INT; |
| 311 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 312 | } |
| 313 | else if (type.getBasicType() == EbtUInt) |
| 314 | { |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 315 | if (type.isVector()) |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 316 | { |
| 317 | switch (type.getNominalSize()) |
| 318 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 319 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 327 | #if !UNREACHABLE_IS_NORETURN |
| 328 | return GL_NONE; |
| 329 | #endif |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 330 | } |
| 331 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 332 | else |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 333 | { |
| 334 | ASSERT(!type.isMatrix()); |
| 335 | return GL_UNSIGNED_INT; |
| 336 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 337 | } |
| 338 | else if (type.getBasicType() == EbtBool) |
| 339 | { |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 340 | if (type.isVector()) |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 341 | { |
| 342 | switch (type.getNominalSize()) |
| 343 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 344 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 352 | #if !UNREACHABLE_IS_NORETURN |
| 353 | return GL_NONE; |
| 354 | #endif |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 355 | } |
| 356 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 357 | else |
Olli Etuaho | 37d96cc | 2017-07-11 14:14:03 +0300 | [diff] [blame] | 358 | { |
| 359 | ASSERT(!type.isMatrix()); |
| 360 | return GL_BOOL; |
| 361 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | switch (type.getBasicType()) |
| 365 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 366 | 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 Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 374 | case EbtSamplerExternal2DY2YEXT: |
| 375 | return GL_SAMPLER_EXTERNAL_2D_Y2Y_EXT; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 376 | case EbtSampler2DRect: |
Corentin Wallez | 13c0dd4 | 2017-07-04 18:27:01 -0400 | [diff] [blame] | 377 | return GL_SAMPLER_2D_RECT_ANGLE; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 378 | case EbtSampler2DArray: |
| 379 | return GL_SAMPLER_2D_ARRAY; |
JiangYizhou | 4021932 | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 380 | case EbtSampler2DMS: |
| 381 | return GL_SAMPLER_2D_MULTISAMPLE; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 382 | 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; |
JiangYizhou | 4021932 | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 390 | case EbtISampler2DMS: |
| 391 | return GL_INT_SAMPLER_2D_MULTISAMPLE; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 392 | 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; |
JiangYizhou | 4021932 | 2016-12-09 09:50:51 +0800 | [diff] [blame] | 400 | case EbtUSampler2DMS: |
| 401 | return GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 402 | 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; |
jchen10 | 4cdac9e | 2017-05-08 11:01:20 +0800 | [diff] [blame] | 432 | case EbtAtomicCounter: |
| 433 | return GL_UNSIGNED_INT_ATOMIC_COUNTER; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 434 | default: |
| 435 | UNREACHABLE(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | return GL_NONE; |
| 439 | } |
| 440 | |
| 441 | GLenum GLVariablePrecision(const TType &type) |
| 442 | { |
| 443 | if (type.getBasicType() == EbtFloat) |
| 444 | { |
| 445 | switch (type.getPrecision()) |
| 446 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 447 | 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 Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | else if (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt) |
| 460 | { |
| 461 | switch (type.getPrecision()) |
| 462 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 463 | 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 Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | // Other types (boolean, sampler) don't have a precision |
| 477 | return GL_NONE; |
| 478 | } |
| 479 | |
| 480 | TString ArrayString(const TType &type) |
| 481 | { |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 482 | TStringStream arrayString; |
Kai Ninomiya | 57ea533 | 2017-11-22 14:04:48 -0800 | [diff] [blame] | 483 | if (!type.isArray()) |
| 484 | return arrayString.str(); |
| 485 | |
| 486 | const TVector<unsigned int> &arraySizes = *type.getArraySizes(); |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 487 | for (auto arraySizeIter = arraySizes.rbegin(); arraySizeIter != arraySizes.rend(); |
| 488 | ++arraySizeIter) |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 489 | { |
Jiawei Shao | 8e4b355 | 2017-08-30 14:20:58 +0800 | [diff] [blame] | 490 | arrayString << "["; |
| 491 | if (*arraySizeIter > 0) |
| 492 | { |
| 493 | arrayString << (*arraySizeIter); |
| 494 | } |
| 495 | arrayString << "]"; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 496 | } |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 497 | return arrayString.str(); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 500 | ImmutableString GetTypeName(const TType &type, ShHashFunction64 hashFunction, NameMap *nameMap) |
Jamie Madill | 6276b92 | 2017-09-25 02:35:57 -0400 | [diff] [blame] | 501 | { |
| 502 | if (type.getBasicType() == EbtStruct) |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 503 | return HashName(type.getStruct(), hashFunction, nameMap); |
Jamie Madill | 6276b92 | 2017-09-25 02:35:57 -0400 | [diff] [blame] | 504 | else |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 505 | return ImmutableString(type.getBuiltInTypeNameString()); |
Jamie Madill | 6276b92 | 2017-09-25 02:35:57 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 508 | bool IsVaryingOut(TQualifier qualifier) |
| 509 | { |
| 510 | switch (qualifier) |
| 511 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 512 | case EvqVaryingOut: |
| 513 | case EvqSmoothOut: |
| 514 | case EvqFlatOut: |
| 515 | case EvqCentroidOut: |
| 516 | case EvqVertexOut: |
Jiawei Shao | 8e4b355 | 2017-08-30 14:20:58 +0800 | [diff] [blame] | 517 | case EvqGeometryOut: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 518 | return true; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 519 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 520 | default: |
| 521 | break; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | bool IsVaryingIn(TQualifier qualifier) |
| 528 | { |
| 529 | switch (qualifier) |
| 530 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 531 | case EvqVaryingIn: |
| 532 | case EvqSmoothIn: |
| 533 | case EvqFlatIn: |
| 534 | case EvqCentroidIn: |
| 535 | case EvqFragmentIn: |
Jiawei Shao | 8e4b355 | 2017-08-30 14:20:58 +0800 | [diff] [blame] | 536 | case EvqGeometryIn: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 537 | return true; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 538 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 539 | default: |
| 540 | break; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | bool IsVarying(TQualifier qualifier) |
| 547 | { |
| 548 | return IsVaryingIn(qualifier) || IsVaryingOut(qualifier); |
| 549 | } |
| 550 | |
Jiawei Shao | 8e4b355 | 2017-08-30 14:20:58 +0800 | [diff] [blame] | 551 | bool IsGeometryShaderInput(GLenum shaderType, TQualifier qualifier) |
| 552 | { |
| 553 | return (qualifier == EvqGeometryIn) || |
Jiawei Shao | bd924af | 2017-11-16 15:28:04 +0800 | [diff] [blame] | 554 | ((shaderType == GL_GEOMETRY_SHADER_EXT) && IsInterpolationIn(qualifier)); |
Jiawei Shao | 8e4b355 | 2017-08-30 14:20:58 +0800 | [diff] [blame] | 555 | } |
| 556 | |
Jamie Madill | f257598 | 2014-06-25 16:04:54 -0400 | [diff] [blame] | 557 | InterpolationType GetInterpolationType(TQualifier qualifier) |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 558 | { |
| 559 | switch (qualifier) |
| 560 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 561 | case EvqFlatIn: |
| 562 | case EvqFlatOut: |
| 563 | return INTERPOLATION_FLAT; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 564 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 565 | case EvqSmoothIn: |
| 566 | case EvqSmoothOut: |
| 567 | case EvqVertexOut: |
| 568 | case EvqFragmentIn: |
| 569 | case EvqVaryingIn: |
| 570 | case EvqVaryingOut: |
Jiawei Shao | 8e4b355 | 2017-08-30 14:20:58 +0800 | [diff] [blame] | 571 | case EvqGeometryIn: |
| 572 | case EvqGeometryOut: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 573 | return INTERPOLATION_SMOOTH; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 574 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 575 | case EvqCentroidIn: |
| 576 | case EvqCentroidOut: |
| 577 | return INTERPOLATION_CENTROID; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 578 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 579 | default: |
| 580 | UNREACHABLE(); |
Nico Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 581 | #if !UNREACHABLE_IS_NORETURN |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 582 | return INTERPOLATION_SMOOTH; |
Nico Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 583 | #endif |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 587 | TType GetShaderVariableBasicType(const sh::ShaderVariable &var) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 588 | { |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 589 | switch (var.type) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 590 | { |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 591 | 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 Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 599 | 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 Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 643 | #if !UNREACHABLE_IS_NORETURN |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 644 | return TType(); |
Nico Weber | b5db2b4 | 2018-02-12 15:31:56 -0500 | [diff] [blame^] | 645 | #endif |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Martin Radev | 70866b8 | 2016-07-22 15:27:42 +0300 | [diff] [blame] | 649 | // GLSL ES 1.0.17 4.6.1 The Invariant Qualifier |
| 650 | bool CanBeInvariantESSL1(TQualifier qualifier) |
| 651 | { |
| 652 | return IsVaryingIn(qualifier) || IsVaryingOut(qualifier) || |
| 653 | IsBuiltinOutputVariable(qualifier) || |
| 654 | (IsBuiltinFragmentInputVariable(qualifier) && qualifier != EvqFrontFacing); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 655 | } |
Martin Radev | 70866b8 | 2016-07-22 15:27:42 +0300 | [diff] [blame] | 656 | |
| 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 |
| 659 | bool CanBeInvariantESSL3OrGreater(TQualifier qualifier) |
| 660 | { |
| 661 | return IsVaryingOut(qualifier) || qualifier == EvqFragmentOut || |
| 662 | IsBuiltinOutputVariable(qualifier); |
| 663 | } |
| 664 | |
| 665 | bool 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 | |
| 684 | bool 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 Radev | e469de8 | 2017-07-04 11:58:35 +0300 | [diff] [blame] | 697 | |
| 698 | bool IsOutputESSL(ShShaderOutput output) |
| 699 | { |
| 700 | return output == SH_ESSL_OUTPUT; |
| 701 | } |
| 702 | |
| 703 | bool 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 | } |
| 724 | bool 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 | } |
| 737 | bool IsOutputVulkan(ShShaderOutput output) |
| 738 | { |
| 739 | return output == SH_GLSL_VULKAN_OUTPUT; |
| 740 | } |
| 741 | |
Martin Radev | 70866b8 | 2016-07-22 15:27:42 +0300 | [diff] [blame] | 742 | } // namespace sh |