| 1 | // |
| 2 | // Copyright (c) 2002-2014 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 | |
| 7 | #include "compiler/translator/Compiler.h" |
| 8 | |
| 9 | #include <sstream> |
| 10 | |
| 11 | #include "angle_gl.h" |
| 12 | #include "common/utilities.h" |
| 13 | #include "compiler/translator/CallDAG.h" |
| 14 | #include "compiler/translator/CollectVariables.h" |
| 15 | #include "compiler/translator/Initialize.h" |
| 16 | #include "compiler/translator/IsASTDepthBelowLimit.h" |
| 17 | #include "compiler/translator/OutputTree.h" |
| 18 | #include "compiler/translator/ParseContext.h" |
| 19 | #include "compiler/translator/ValidateLimitations.h" |
| 20 | #include "compiler/translator/ValidateMaxParameters.h" |
| 21 | #include "compiler/translator/ValidateOutputs.h" |
| 22 | #include "compiler/translator/ValidateVaryingLocations.h" |
| 23 | #include "compiler/translator/VariablePacker.h" |
| 24 | #include "compiler/translator/tree_ops/AddAndTrueToLoopCondition.h" |
| 25 | #include "compiler/translator/tree_ops/ClampFragDepth.h" |
| 26 | #include "compiler/translator/tree_ops/ClampPointSize.h" |
| 27 | #include "compiler/translator/tree_ops/DeclareAndInitBuiltinsForInstancedMultiview.h" |
| 28 | #include "compiler/translator/tree_ops/DeferGlobalInitializers.h" |
| 29 | #include "compiler/translator/tree_ops/EmulateGLDrawID.h" |
| 30 | #include "compiler/translator/tree_ops/EmulateGLFragColorBroadcast.h" |
| 31 | #include "compiler/translator/tree_ops/EmulatePrecision.h" |
| 32 | #include "compiler/translator/tree_ops/FoldExpressions.h" |
| 33 | #include "compiler/translator/tree_ops/InitializeVariables.h" |
| 34 | #include "compiler/translator/tree_ops/PruneEmptyCases.h" |
| 35 | #include "compiler/translator/tree_ops/PruneNoOps.h" |
| 36 | #include "compiler/translator/tree_ops/RegenerateStructNames.h" |
| 37 | #include "compiler/translator/tree_ops/RemoveArrayLengthMethod.h" |
| 38 | #include "compiler/translator/tree_ops/RemoveInvariantDeclaration.h" |
| 39 | #include "compiler/translator/tree_ops/RemovePow.h" |
| 40 | #include "compiler/translator/tree_ops/RemoveUnreferencedVariables.h" |
| 41 | #include "compiler/translator/tree_ops/RewriteDoWhile.h" |
| 42 | #include "compiler/translator/tree_ops/RewriteRepeatedAssignToSwizzled.h" |
| 43 | #include "compiler/translator/tree_ops/ScalarizeVecAndMatConstructorArgs.h" |
| 44 | #include "compiler/translator/tree_ops/SeparateDeclarations.h" |
| 45 | #include "compiler/translator/tree_ops/SimplifyLoopConditions.h" |
| 46 | #include "compiler/translator/tree_ops/SplitSequenceOperator.h" |
| 47 | #include "compiler/translator/tree_ops/UnfoldShortCircuitAST.h" |
| 48 | #include "compiler/translator/tree_ops/UseInterfaceBlockFields.h" |
| 49 | #include "compiler/translator/tree_ops/VectorizeVectorScalarArithmetic.h" |
| 50 | #include "compiler/translator/tree_util/BuiltIn_autogen.h" |
| 51 | #include "compiler/translator/tree_util/IntermNodePatternMatcher.h" |
| 52 | #include "compiler/translator/util.h" |
| 53 | #include "third_party/compiler/ArrayBoundsClamper.h" |
| 54 | |
| 55 | namespace sh |
| 56 | { |
| 57 | |
| 58 | namespace |
| 59 | { |
| 60 | |
| 61 | #if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 62 | void DumpFuzzerCase(char const *const *shaderStrings, |
| 63 | size_t numStrings, |
| 64 | uint32_t type, |
| 65 | uint32_t spec, |
| 66 | uint32_t output, |
| 67 | uint64_t options) |
| 68 | { |
| 69 | static int fileIndex = 0; |
| 70 | |
| 71 | std::ostringstream o = sh::InitializeStream<std::ostringstream>(); |
| 72 | o << "corpus/" << fileIndex++ << ".sample" ; |
| 73 | std::string s = o.str(); |
| 74 | |
| 75 | // Must match the input format of the fuzzer |
| 76 | FILE *f = fopen(s.c_str(), "w" ); |
| 77 | fwrite(&type, sizeof(type), 1, f); |
| 78 | fwrite(&spec, sizeof(spec), 1, f); |
| 79 | fwrite(&output, sizeof(output), 1, f); |
| 80 | fwrite(&options, sizeof(options), 1, f); |
| 81 | |
| 82 | char zero[128 - 20] = {0}; |
| 83 | fwrite(&zero, 128 - 20, 1, f); |
| 84 | |
| 85 | for (size_t i = 0; i < numStrings; i++) |
| 86 | { |
| 87 | fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f); |
| 88 | } |
| 89 | fwrite(&zero, 1, 1, f); |
| 90 | |
| 91 | fclose(f); |
| 92 | } |
| 93 | #endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 94 | } // anonymous namespace |
| 95 | |
| 96 | bool IsGLSL130OrNewer(ShShaderOutput output) |
| 97 | { |
| 98 | return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT || |
| 99 | output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT || |
| 100 | output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT || |
| 101 | output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT || |
| 102 | output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT); |
| 103 | } |
| 104 | |
| 105 | bool IsGLSL420OrNewer(ShShaderOutput output) |
| 106 | { |
| 107 | return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT || |
| 108 | output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT); |
| 109 | } |
| 110 | |
| 111 | bool IsGLSL410OrOlder(ShShaderOutput output) |
| 112 | { |
| 113 | return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT || |
| 114 | output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT || |
| 115 | output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT); |
| 116 | } |
| 117 | |
| 118 | bool RemoveInvariant(sh::GLenum shaderType, |
| 119 | int shaderVersion, |
| 120 | ShShaderOutput outputType, |
| 121 | ShCompileOptions compileOptions) |
| 122 | { |
| 123 | if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 && |
| 124 | shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType)) |
| 125 | return true; |
| 126 | |
| 127 | if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 && |
| 128 | shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER) |
| 129 | return true; |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | size_t GetGlobalMaxTokenSize(ShShaderSpec spec) |
| 135 | { |
| 136 | // WebGL defines a max token length of 256, while ES2 leaves max token |
| 137 | // size undefined. ES3 defines a max size of 1024 characters. |
| 138 | switch (spec) |
| 139 | { |
| 140 | case SH_WEBGL_SPEC: |
| 141 | return 256; |
| 142 | default: |
| 143 | return 1024; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources) |
| 148 | { |
| 149 | switch (shaderType) |
| 150 | { |
| 151 | case GL_VERTEX_SHADER: |
| 152 | return resources.MaxVertexUniformVectors; |
| 153 | case GL_FRAGMENT_SHADER: |
| 154 | return resources.MaxFragmentUniformVectors; |
| 155 | |
| 156 | // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting |
| 157 | case GL_COMPUTE_SHADER: |
| 158 | return resources.MaxComputeUniformComponents / 4; |
| 159 | case GL_GEOMETRY_SHADER_EXT: |
| 160 | return resources.MaxGeometryUniformComponents / 4; |
| 161 | default: |
| 162 | UNREACHABLE(); |
| 163 | return -1; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | namespace |
| 168 | { |
| 169 | |
| 170 | class TScopedPoolAllocator |
| 171 | { |
| 172 | public: |
| 173 | TScopedPoolAllocator(angle::PoolAllocator *allocator) : mAllocator(allocator) |
| 174 | { |
| 175 | mAllocator->push(); |
| 176 | SetGlobalPoolAllocator(mAllocator); |
| 177 | } |
| 178 | ~TScopedPoolAllocator() |
| 179 | { |
| 180 | SetGlobalPoolAllocator(nullptr); |
| 181 | mAllocator->pop(); |
| 182 | } |
| 183 | |
| 184 | private: |
| 185 | angle::PoolAllocator *mAllocator; |
| 186 | }; |
| 187 | |
| 188 | class TScopedSymbolTableLevel |
| 189 | { |
| 190 | public: |
| 191 | TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table) |
| 192 | { |
| 193 | ASSERT(mTable->isEmpty()); |
| 194 | mTable->push(); |
| 195 | } |
| 196 | ~TScopedSymbolTableLevel() |
| 197 | { |
| 198 | while (!mTable->isEmpty()) |
| 199 | mTable->pop(); |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | TSymbolTable *mTable; |
| 204 | }; |
| 205 | |
| 206 | int MapSpecToShaderVersion(ShShaderSpec spec) |
| 207 | { |
| 208 | switch (spec) |
| 209 | { |
| 210 | case SH_GLES2_SPEC: |
| 211 | case SH_WEBGL_SPEC: |
| 212 | return 100; |
| 213 | case SH_GLES3_SPEC: |
| 214 | case SH_WEBGL2_SPEC: |
| 215 | return 300; |
| 216 | case SH_GLES3_1_SPEC: |
| 217 | case SH_WEBGL3_SPEC: |
| 218 | return 310; |
| 219 | default: |
| 220 | UNREACHABLE(); |
| 221 | return 0; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | bool ValidateFragColorAndFragData(GLenum shaderType, |
| 226 | int shaderVersion, |
| 227 | const TSymbolTable &symbolTable, |
| 228 | TDiagnostics *diagnostics) |
| 229 | { |
| 230 | if (shaderVersion > 100 || shaderType != GL_FRAGMENT_SHADER) |
| 231 | { |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | bool usesFragColor = false; |
| 236 | bool usesFragData = false; |
| 237 | // This validation is a bit stricter than the spec - it's only an error to write to |
| 238 | // both FragData and FragColor. But because it's better not to have reads from undefined |
| 239 | // variables, we always return an error if they are both referenced, rather than only if they |
| 240 | // are written. |
| 241 | if (symbolTable.isStaticallyUsed(*BuiltInVariable::gl_FragColor()) || |
| 242 | symbolTable.isStaticallyUsed(*BuiltInVariable::gl_SecondaryFragColorEXT())) |
| 243 | { |
| 244 | usesFragColor = true; |
| 245 | } |
| 246 | // Extension variables may not always be initialized (saves some time at symbol table init). |
| 247 | bool secondaryFragDataUsed = |
| 248 | symbolTable.gl_SecondaryFragDataEXT() != nullptr && |
| 249 | symbolTable.isStaticallyUsed(*symbolTable.gl_SecondaryFragDataEXT()); |
| 250 | if (symbolTable.isStaticallyUsed(*symbolTable.gl_FragData()) || secondaryFragDataUsed) |
| 251 | { |
| 252 | usesFragData = true; |
| 253 | } |
| 254 | if (usesFragColor && usesFragData) |
| 255 | { |
| 256 | const char *errorMessage = "cannot use both gl_FragData and gl_FragColor" ; |
| 257 | if (symbolTable.isStaticallyUsed(*BuiltInVariable::gl_SecondaryFragColorEXT()) || |
| 258 | secondaryFragDataUsed) |
| 259 | { |
| 260 | errorMessage = |
| 261 | "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)" |
| 262 | " and (gl_FragColor, gl_SecondaryFragColorEXT)" ; |
| 263 | } |
| 264 | diagnostics->globalError(errorMessage); |
| 265 | return false; |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | } // namespace |
| 271 | |
| 272 | TShHandleBase::TShHandleBase() |
| 273 | { |
| 274 | allocator.push(); |
| 275 | SetGlobalPoolAllocator(&allocator); |
| 276 | } |
| 277 | |
| 278 | TShHandleBase::~TShHandleBase() |
| 279 | { |
| 280 | SetGlobalPoolAllocator(nullptr); |
| 281 | allocator.popAll(); |
| 282 | } |
| 283 | |
| 284 | TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output) |
| 285 | : mVariablesCollected(false), |
| 286 | mGLPositionInitialized(false), |
| 287 | mShaderType(type), |
| 288 | mShaderSpec(spec), |
| 289 | mOutputType(output), |
| 290 | mBuiltInFunctionEmulator(), |
| 291 | mDiagnostics(mInfoSink.info), |
| 292 | mSourcePath(nullptr), |
| 293 | mComputeShaderLocalSizeDeclared(false), |
| 294 | mComputeShaderLocalSize(1), |
| 295 | mGeometryShaderMaxVertices(-1), |
| 296 | mGeometryShaderInvocations(0), |
| 297 | mGeometryShaderInputPrimitiveType(EptUndefined), |
| 298 | mGeometryShaderOutputPrimitiveType(EptUndefined) |
| 299 | {} |
| 300 | |
| 301 | TCompiler::~TCompiler() {} |
| 302 | |
| 303 | bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const |
| 304 | { |
| 305 | // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API, |
| 306 | // validate loop and indexing as well (to verify that the shader only uses minimal functionality |
| 307 | // of ESSL 1.00 as in Appendix A of the spec). |
| 308 | return (IsWebGLBasedSpec(mShaderSpec) && mShaderVersion == 100) || |
| 309 | (compileOptions & SH_VALIDATE_LOOP_INDEXING); |
| 310 | } |
| 311 | |
| 312 | bool TCompiler::Init(const ShBuiltInResources &resources) |
| 313 | { |
| 314 | SetGlobalPoolAllocator(&allocator); |
| 315 | |
| 316 | // Generate built-in symbol table. |
| 317 | if (!initBuiltInSymbolTable(resources)) |
| 318 | return false; |
| 319 | |
| 320 | mResources = resources; |
| 321 | setResourceString(); |
| 322 | |
| 323 | InitExtensionBehavior(resources, mExtensionBehavior); |
| 324 | mArrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy); |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[], |
| 329 | size_t numStrings, |
| 330 | ShCompileOptions compileOptions) |
| 331 | { |
| 332 | return compileTreeImpl(shaderStrings, numStrings, compileOptions); |
| 333 | } |
| 334 | |
| 335 | TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[], |
| 336 | size_t numStrings, |
| 337 | const ShCompileOptions compileOptions) |
| 338 | { |
| 339 | clearResults(); |
| 340 | |
| 341 | ASSERT(numStrings > 0); |
| 342 | ASSERT(GetGlobalPoolAllocator()); |
| 343 | |
| 344 | // Reset the extension behavior for each compilation unit. |
| 345 | ResetExtensionBehavior(mExtensionBehavior); |
| 346 | |
| 347 | // If gl_DrawID is not supported, remove it from the available extensions |
| 348 | // Currently we only allow emulation of gl_DrawID |
| 349 | const bool glDrawIDSupported = (compileOptions & SH_EMULATE_GL_DRAW_ID) != 0u; |
| 350 | if (!glDrawIDSupported) |
| 351 | { |
| 352 | auto it = mExtensionBehavior.find(TExtension::ANGLE_multi_draw); |
| 353 | if (it != mExtensionBehavior.end()) |
| 354 | { |
| 355 | mExtensionBehavior.erase(it); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // First string is path of source file if flag is set. The actual source follows. |
| 360 | size_t firstSource = 0; |
| 361 | if (compileOptions & SH_SOURCE_PATH) |
| 362 | { |
| 363 | mSourcePath = shaderStrings[0]; |
| 364 | ++firstSource; |
| 365 | } |
| 366 | |
| 367 | TParseContext parseContext(mSymbolTable, mExtensionBehavior, mShaderType, mShaderSpec, |
| 368 | compileOptions, true, &mDiagnostics, getResources()); |
| 369 | |
| 370 | parseContext.setFragmentPrecisionHighOnESSL1(mResources.FragmentPrecisionHigh == 1); |
| 371 | |
| 372 | // We preserve symbols at the built-in level from compile-to-compile. |
| 373 | // Start pushing the user-defined symbols at global level. |
| 374 | TScopedSymbolTableLevel globalLevel(&mSymbolTable); |
| 375 | ASSERT(mSymbolTable.atGlobalLevel()); |
| 376 | |
| 377 | // Parse shader. |
| 378 | if (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, |
| 379 | &parseContext) != 0) |
| 380 | { |
| 381 | return nullptr; |
| 382 | } |
| 383 | |
| 384 | if (parseContext.getTreeRoot() == nullptr) |
| 385 | { |
| 386 | return nullptr; |
| 387 | } |
| 388 | |
| 389 | setASTMetadata(parseContext); |
| 390 | |
| 391 | if (!checkShaderVersion(&parseContext)) |
| 392 | { |
| 393 | return nullptr; |
| 394 | } |
| 395 | |
| 396 | TIntermBlock *root = parseContext.getTreeRoot(); |
| 397 | if (!checkAndSimplifyAST(root, parseContext, compileOptions)) |
| 398 | { |
| 399 | return nullptr; |
| 400 | } |
| 401 | |
| 402 | return root; |
| 403 | } |
| 404 | |
| 405 | bool TCompiler::checkShaderVersion(TParseContext *parseContext) |
| 406 | { |
| 407 | if (MapSpecToShaderVersion(mShaderSpec) < mShaderVersion) |
| 408 | { |
| 409 | mDiagnostics.globalError("unsupported shader version" ); |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | ASSERT(parseContext); |
| 414 | switch (mShaderType) |
| 415 | { |
| 416 | case GL_COMPUTE_SHADER: |
| 417 | if (mShaderVersion < 310) |
| 418 | { |
| 419 | mDiagnostics.globalError("Compute shader is not supported in this shader version." ); |
| 420 | return false; |
| 421 | } |
| 422 | break; |
| 423 | |
| 424 | case GL_GEOMETRY_SHADER_EXT: |
| 425 | if (mShaderVersion < 310) |
| 426 | { |
| 427 | mDiagnostics.globalError( |
| 428 | "Geometry shader is not supported in this shader version." ); |
| 429 | return false; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | ASSERT(mShaderVersion == 310); |
| 434 | if (!parseContext->checkCanUseExtension(sh::TSourceLoc(), |
| 435 | TExtension::EXT_geometry_shader)) |
| 436 | { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | break; |
| 441 | |
| 442 | default: |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | return true; |
| 447 | } |
| 448 | |
| 449 | void TCompiler::setASTMetadata(const TParseContext &parseContext) |
| 450 | { |
| 451 | mShaderVersion = parseContext.getShaderVersion(); |
| 452 | |
| 453 | mPragma = parseContext.pragma(); |
| 454 | mSymbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll); |
| 455 | |
| 456 | mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared(); |
| 457 | mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize(); |
| 458 | |
| 459 | mNumViews = parseContext.getNumViews(); |
| 460 | |
| 461 | if (mShaderType == GL_GEOMETRY_SHADER_EXT) |
| 462 | { |
| 463 | mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType(); |
| 464 | mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType(); |
| 465 | mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices(); |
| 466 | mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations(); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | bool TCompiler::checkAndSimplifyAST(TIntermBlock *root, |
| 471 | const TParseContext &parseContext, |
| 472 | ShCompileOptions compileOptions) |
| 473 | { |
| 474 | // Disallow expressions deemed too complex. |
| 475 | if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root)) |
| 476 | { |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | if (shouldRunLoopAndIndexingValidation(compileOptions) && |
| 481 | !ValidateLimitations(root, mShaderType, &mSymbolTable, &mDiagnostics)) |
| 482 | { |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | if (!ValidateFragColorAndFragData(mShaderType, mShaderVersion, mSymbolTable, &mDiagnostics)) |
| 487 | { |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | // Fold expressions that could not be folded before validation that was done as a part of |
| 492 | // parsing. |
| 493 | FoldExpressions(root, &mDiagnostics); |
| 494 | // Folding should only be able to generate warnings. |
| 495 | ASSERT(mDiagnostics.numErrors() == 0); |
| 496 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 497 | { |
| 498 | return false; |
| 499 | } |
| 500 | |
| 501 | // We prune no-ops to work around driver bugs and to keep AST processing and output simple. |
| 502 | // The following kinds of no-ops are pruned: |
| 503 | // 1. Empty declarations "int;". |
| 504 | // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision |
| 505 | // for float, so float literal statements would end up with no precision which is |
| 506 | // invalid ESSL. |
| 507 | // After this empty declarations are not allowed in the AST. |
| 508 | PruneNoOps(root, &mSymbolTable); |
| 509 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 510 | { |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | // Create the function DAG and check there is no recursion |
| 515 | if (!initCallDag(root)) |
| 516 | { |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth()) |
| 521 | { |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | // Checks which functions are used and if "main" exists |
| 526 | mFunctionMetadata.clear(); |
| 527 | mFunctionMetadata.resize(mCallDag.size()); |
| 528 | if (!tagUsedFunctions()) |
| 529 | { |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS)) |
| 534 | { |
| 535 | pruneUnusedFunctions(root); |
| 536 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 537 | { |
| 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (mShaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, mShaderType)) |
| 543 | { |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | if (mShaderVersion >= 300 && mShaderType == GL_FRAGMENT_SHADER && |
| 548 | !ValidateOutputs(root, getExtensionBehavior(), mResources.MaxDrawBuffers, &mDiagnostics)) |
| 549 | { |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | // Fail compilation if precision emulation not supported. |
| 554 | if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision && |
| 555 | !EmulatePrecision::SupportedInLanguage(mOutputType)) |
| 556 | { |
| 557 | mDiagnostics.globalError("Precision emulation not supported for this output type." ); |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | // Clamping uniform array bounds needs to happen after validateLimitations pass. |
| 562 | if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS) |
| 563 | { |
| 564 | mArrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root); |
| 565 | } |
| 566 | |
| 567 | if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) && |
| 568 | parseContext.isExtensionEnabled(TExtension::OVR_multiview2) && |
| 569 | getShaderType() != GL_COMPUTE_SHADER) |
| 570 | { |
| 571 | DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, mShaderType, compileOptions, |
| 572 | mOutputType, &mSymbolTable); |
| 573 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 574 | { |
| 575 | return false; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // This pass might emit short circuits so keep it before the short circuit unfolding |
| 580 | if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS) |
| 581 | { |
| 582 | RewriteDoWhile(root, &mSymbolTable); |
| 583 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 584 | { |
| 585 | return false; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION) |
| 590 | { |
| 591 | AddAndTrueToLoopCondition(root); |
| 592 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 593 | { |
| 594 | return false; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT) |
| 599 | { |
| 600 | UnfoldShortCircuitAST(root); |
| 601 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 602 | { |
| 603 | return false; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT) |
| 608 | { |
| 609 | RemovePow(root, &mSymbolTable); |
| 610 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 611 | { |
| 612 | return false; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (compileOptions & SH_REGENERATE_STRUCT_NAMES) |
| 617 | { |
| 618 | RegenerateStructNames gen(&mSymbolTable); |
| 619 | root->traverse(&gen); |
| 620 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 621 | { |
| 622 | return false; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (mShaderType == GL_VERTEX_SHADER && |
| 627 | IsExtensionEnabled(mExtensionBehavior, TExtension::ANGLE_multi_draw)) |
| 628 | { |
| 629 | if ((compileOptions & SH_EMULATE_GL_DRAW_ID) != 0) |
| 630 | { |
| 631 | EmulateGLDrawID(root, &mSymbolTable, &mUniforms, |
| 632 | shouldCollectVariables(compileOptions)); |
| 633 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 634 | { |
| 635 | return false; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if (mShaderType == GL_FRAGMENT_SHADER && mShaderVersion == 100 && mResources.EXT_draw_buffers && |
| 641 | mResources.MaxDrawBuffers > 1 && |
| 642 | IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers)) |
| 643 | { |
| 644 | EmulateGLFragColorBroadcast(root, mResources.MaxDrawBuffers, &mOutputVariables, |
| 645 | &mSymbolTable, mShaderVersion); |
| 646 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 647 | { |
| 648 | return false; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | int simplifyScalarized = (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS) |
| 653 | ? IntermNodePatternMatcher::kScalarizedVecOrMatConstructor |
| 654 | : 0; |
| 655 | |
| 656 | // Split multi declarations and remove calls to array length(). |
| 657 | // Note that SimplifyLoopConditions needs to be run before any other AST transformations |
| 658 | // that may need to generate new statements from loop conditions or loop expressions. |
| 659 | SimplifyLoopConditions(root, |
| 660 | IntermNodePatternMatcher::kMultiDeclaration | |
| 661 | IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized, |
| 662 | &getSymbolTable()); |
| 663 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 664 | { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | // Note that separate declarations need to be run before other AST transformations that |
| 669 | // generate new statements from expressions. |
| 670 | SeparateDeclarations(root); |
| 671 | mValidateASTOptions.validateMultiDeclarations = true; |
| 672 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 673 | { |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized, |
| 678 | &getSymbolTable()); |
| 679 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 680 | { |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | RemoveArrayLengthMethod(root); |
| 685 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 686 | { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | RemoveUnreferencedVariables(root, &mSymbolTable); |
| 691 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 692 | { |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | // In case the last case inside a switch statement is a certain type of no-op, GLSL compilers in |
| 697 | // drivers may not accept it. In this case we clean up the dead code from the end of switch |
| 698 | // statements. This is also required because PruneNoOps or RemoveUnreferencedVariables may have |
| 699 | // left switch statements that only contained an empty declaration inside the final case in an |
| 700 | // invalid state. Relies on that PruneNoOps and RemoveUnreferencedVariables have already been |
| 701 | // run. |
| 702 | PruneEmptyCases(root); |
| 703 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 704 | { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | // Built-in function emulation needs to happen after validateLimitations pass. |
| 709 | // TODO(jmadill): Remove global pool allocator. |
| 710 | GetGlobalPoolAllocator()->lock(); |
| 711 | initBuiltInFunctionEmulator(&mBuiltInFunctionEmulator, compileOptions); |
| 712 | GetGlobalPoolAllocator()->unlock(); |
| 713 | mBuiltInFunctionEmulator.markBuiltInFunctionsForEmulation(root); |
| 714 | |
| 715 | bool highPrecisionSupported = mShaderVersion > 100 || mShaderType != GL_FRAGMENT_SHADER || |
| 716 | mResources.FragmentPrecisionHigh == 1; |
| 717 | if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS) |
| 718 | { |
| 719 | ScalarizeVecAndMatConstructorArgs(root, mShaderType, highPrecisionSupported, &mSymbolTable); |
| 720 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 721 | { |
| 722 | return false; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | if (shouldCollectVariables(compileOptions)) |
| 727 | { |
| 728 | ASSERT(!mVariablesCollected); |
| 729 | CollectVariables(root, &mAttributes, &mOutputVariables, &mUniforms, &mInputVaryings, |
| 730 | &mOutputVaryings, &mUniformBlocks, &mShaderStorageBlocks, &mInBlocks, |
| 731 | mResources.HashFunction, &mSymbolTable, mShaderType, mExtensionBehavior); |
| 732 | collectInterfaceBlocks(); |
| 733 | mVariablesCollected = true; |
| 734 | if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS) |
| 735 | { |
| 736 | useAllMembersInUnusedStandardAndSharedBlocks(root); |
| 737 | } |
| 738 | if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) |
| 739 | { |
| 740 | int maxUniformVectors = GetMaxUniformVectorsForShaderType(mShaderType, mResources); |
| 741 | // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec |
| 742 | // Appendix A, section 7, the shader does not use too many uniforms. |
| 743 | if (!CheckVariablesInPackingLimits(maxUniformVectors, mUniforms)) |
| 744 | { |
| 745 | mDiagnostics.globalError("too many uniforms" ); |
| 746 | return false; |
| 747 | } |
| 748 | } |
| 749 | if ((compileOptions & SH_INIT_OUTPUT_VARIABLES) && (mShaderType != GL_COMPUTE_SHADER)) |
| 750 | { |
| 751 | initializeOutputVariables(root); |
| 752 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 753 | { |
| 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Removing invariant declarations must be done after collecting variables. |
| 760 | // Otherwise, built-in invariant declarations don't apply. |
| 761 | if (RemoveInvariant(mShaderType, mShaderVersion, mOutputType, compileOptions)) |
| 762 | { |
| 763 | RemoveInvariantDeclaration(root); |
| 764 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 765 | { |
| 766 | return false; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // gl_Position is always written in compatibility output mode. |
| 771 | // It may have been already initialized among other output variables, in that case we don't |
| 772 | // need to initialize it twice. |
| 773 | if (mShaderType == GL_VERTEX_SHADER && !mGLPositionInitialized && |
| 774 | ((compileOptions & SH_INIT_GL_POSITION) || (mOutputType == SH_GLSL_COMPATIBILITY_OUTPUT))) |
| 775 | { |
| 776 | initializeGLPosition(root); |
| 777 | mGLPositionInitialized = true; |
| 778 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 779 | { |
| 780 | return false; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // DeferGlobalInitializers needs to be run before other AST transformations that generate new |
| 785 | // statements from expressions. But it's fine to run DeferGlobalInitializers after the above |
| 786 | // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST |
| 787 | // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00. |
| 788 | bool initializeLocalsAndGlobals = |
| 789 | (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType()); |
| 790 | bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES); |
| 791 | DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize, |
| 792 | highPrecisionSupported, &mSymbolTable); |
| 793 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 794 | { |
| 795 | return false; |
| 796 | } |
| 797 | |
| 798 | if (initializeLocalsAndGlobals) |
| 799 | { |
| 800 | // Initialize uninitialized local variables. |
| 801 | // In some cases initializing can generate extra statements in the parent block, such as |
| 802 | // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case |
| 803 | // we need to first simplify loop conditions. We've already separated declarations |
| 804 | // earlier, which is also required. If we don't follow the Appendix A limitations, loop |
| 805 | // init statements can declare arrays or nameless structs and have multiple |
| 806 | // declarations. |
| 807 | |
| 808 | if (!shouldRunLoopAndIndexingValidation(compileOptions)) |
| 809 | { |
| 810 | SimplifyLoopConditions(root, |
| 811 | IntermNodePatternMatcher::kArrayDeclaration | |
| 812 | IntermNodePatternMatcher::kNamelessStructDeclaration, |
| 813 | &getSymbolTable()); |
| 814 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 815 | { |
| 816 | return false; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize, |
| 821 | highPrecisionSupported, &getSymbolTable()); |
| 822 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 823 | { |
| 824 | return false; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE)) |
| 829 | { |
| 830 | ClampPointSize(root, mResources.MaxPointSize, &getSymbolTable()); |
| 831 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 832 | { |
| 833 | return false; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | if (getShaderType() == GL_FRAGMENT_SHADER && (compileOptions & SH_CLAMP_FRAG_DEPTH)) |
| 838 | { |
| 839 | ClampFragDepth(root, &getSymbolTable()); |
| 840 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 841 | { |
| 842 | return false; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | if (compileOptions & SH_REWRITE_REPEATED_ASSIGN_TO_SWIZZLED) |
| 847 | { |
| 848 | sh::RewriteRepeatedAssignToSwizzled(root); |
| 849 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 850 | { |
| 851 | return false; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC) |
| 856 | { |
| 857 | VectorizeVectorScalarArithmetic(root, &getSymbolTable()); |
| 858 | if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions)) |
| 859 | { |
| 860 | return false; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | bool TCompiler::compile(const char *const shaderStrings[], |
| 868 | size_t numStrings, |
| 869 | ShCompileOptions compileOptionsIn) |
| 870 | { |
| 871 | #if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 872 | DumpFuzzerCase(shaderStrings, numStrings, mShaderType, mShaderSpec, mOutputType, |
| 873 | compileOptionsIn); |
| 874 | #endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 875 | |
| 876 | if (numStrings == 0) |
| 877 | return true; |
| 878 | |
| 879 | ShCompileOptions compileOptions = compileOptionsIn; |
| 880 | |
| 881 | // Apply key workarounds. |
| 882 | if (shouldFlattenPragmaStdglInvariantAll()) |
| 883 | { |
| 884 | // This should be harmless to do in all cases, but for the moment, do it only conditionally. |
| 885 | compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL; |
| 886 | } |
| 887 | |
| 888 | TScopedPoolAllocator scopedAlloc(&allocator); |
| 889 | TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions); |
| 890 | |
| 891 | if (root) |
| 892 | { |
| 893 | if (compileOptions & SH_INTERMEDIATE_TREE) |
| 894 | OutputTree(root, mInfoSink.info); |
| 895 | |
| 896 | if (compileOptions & SH_OBJECT_CODE) |
| 897 | { |
| 898 | PerformanceDiagnostics perfDiagnostics(&mDiagnostics); |
| 899 | translate(root, compileOptions, &perfDiagnostics); |
| 900 | } |
| 901 | |
| 902 | if (mShaderType == GL_VERTEX_SHADER && |
| 903 | IsExtensionEnabled(mExtensionBehavior, TExtension::ANGLE_multi_draw)) |
| 904 | { |
| 905 | if ((compileOptions & SH_EMULATE_GL_DRAW_ID) != 0) |
| 906 | { |
| 907 | for (auto &uniform : mUniforms) |
| 908 | { |
| 909 | if (uniform.name == "angle_DrawID" && uniform.mappedName == "angle_DrawID" ) |
| 910 | { |
| 911 | uniform.name = "gl_DrawID" ; |
| 912 | break; |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | // The IntermNode tree doesn't need to be deleted here, since the |
| 919 | // memory will be freed in a big chunk by the PoolAllocator. |
| 920 | return true; |
| 921 | } |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | bool TCompiler::initBuiltInSymbolTable(const ShBuiltInResources &resources) |
| 926 | { |
| 927 | if (resources.MaxDrawBuffers < 1) |
| 928 | { |
| 929 | return false; |
| 930 | } |
| 931 | if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1) |
| 932 | { |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | mSymbolTable.initializeBuiltIns(mShaderType, mShaderSpec, resources); |
| 937 | |
| 938 | return true; |
| 939 | } |
| 940 | |
| 941 | void TCompiler::setResourceString() |
| 942 | { |
| 943 | std::ostringstream strstream = sh::InitializeStream<std::ostringstream>(); |
| 944 | |
| 945 | // clang-format off |
| 946 | strstream << ":MaxVertexAttribs:" << mResources.MaxVertexAttribs |
| 947 | << ":MaxVertexUniformVectors:" << mResources.MaxVertexUniformVectors |
| 948 | << ":MaxVaryingVectors:" << mResources.MaxVaryingVectors |
| 949 | << ":MaxVertexTextureImageUnits:" << mResources.MaxVertexTextureImageUnits |
| 950 | << ":MaxCombinedTextureImageUnits:" << mResources.MaxCombinedTextureImageUnits |
| 951 | << ":MaxTextureImageUnits:" << mResources.MaxTextureImageUnits |
| 952 | << ":MaxFragmentUniformVectors:" << mResources.MaxFragmentUniformVectors |
| 953 | << ":MaxDrawBuffers:" << mResources.MaxDrawBuffers |
| 954 | << ":OES_standard_derivatives:" << mResources.OES_standard_derivatives |
| 955 | << ":OES_EGL_image_external:" << mResources.OES_EGL_image_external |
| 956 | << ":OES_EGL_image_external_essl3:" << mResources.OES_EGL_image_external_essl3 |
| 957 | << ":NV_EGL_stream_consumer_external:" << mResources.NV_EGL_stream_consumer_external |
| 958 | << ":ARB_texture_rectangle:" << mResources.ARB_texture_rectangle |
| 959 | << ":EXT_draw_buffers:" << mResources.EXT_draw_buffers |
| 960 | << ":FragmentPrecisionHigh:" << mResources.FragmentPrecisionHigh |
| 961 | << ":MaxExpressionComplexity:" << mResources.MaxExpressionComplexity |
| 962 | << ":MaxCallStackDepth:" << mResources.MaxCallStackDepth |
| 963 | << ":MaxFunctionParameters:" << mResources.MaxFunctionParameters |
| 964 | << ":EXT_blend_func_extended:" << mResources.EXT_blend_func_extended |
| 965 | << ":EXT_frag_depth:" << mResources.EXT_frag_depth |
| 966 | << ":EXT_shader_texture_lod:" << mResources.EXT_shader_texture_lod |
| 967 | << ":EXT_shader_framebuffer_fetch:" << mResources.EXT_shader_framebuffer_fetch |
| 968 | << ":NV_shader_framebuffer_fetch:" << mResources.NV_shader_framebuffer_fetch |
| 969 | << ":ARM_shader_framebuffer_fetch:" << mResources.ARM_shader_framebuffer_fetch |
| 970 | << ":OVR_multiview2:" << mResources.OVR_multiview2 |
| 971 | << ":EXT_YUV_target:" << mResources.EXT_YUV_target |
| 972 | << ":EXT_geometry_shader:" << mResources.EXT_geometry_shader |
| 973 | << ":MaxVertexOutputVectors:" << mResources.MaxVertexOutputVectors |
| 974 | << ":MaxFragmentInputVectors:" << mResources.MaxFragmentInputVectors |
| 975 | << ":MinProgramTexelOffset:" << mResources.MinProgramTexelOffset |
| 976 | << ":MaxProgramTexelOffset:" << mResources.MaxProgramTexelOffset |
| 977 | << ":MaxDualSourceDrawBuffers:" << mResources.MaxDualSourceDrawBuffers |
| 978 | << ":MaxViewsOVR:" << mResources.MaxViewsOVR |
| 979 | << ":NV_draw_buffers:" << mResources.NV_draw_buffers |
| 980 | << ":WEBGL_debug_shader_precision:" << mResources.WEBGL_debug_shader_precision |
| 981 | << ":ANGLE_multi_draw:" << mResources.ANGLE_multi_draw |
| 982 | << ":MinProgramTextureGatherOffset:" << mResources.MinProgramTextureGatherOffset |
| 983 | << ":MaxProgramTextureGatherOffset:" << mResources.MaxProgramTextureGatherOffset |
| 984 | << ":MaxImageUnits:" << mResources.MaxImageUnits |
| 985 | << ":MaxVertexImageUniforms:" << mResources.MaxVertexImageUniforms |
| 986 | << ":MaxFragmentImageUniforms:" << mResources.MaxFragmentImageUniforms |
| 987 | << ":MaxComputeImageUniforms:" << mResources.MaxComputeImageUniforms |
| 988 | << ":MaxCombinedImageUniforms:" << mResources.MaxCombinedImageUniforms |
| 989 | << ":MaxCombinedShaderOutputResources:" << mResources.MaxCombinedShaderOutputResources |
| 990 | << ":MaxComputeWorkGroupCountX:" << mResources.MaxComputeWorkGroupCount[0] |
| 991 | << ":MaxComputeWorkGroupCountY:" << mResources.MaxComputeWorkGroupCount[1] |
| 992 | << ":MaxComputeWorkGroupCountZ:" << mResources.MaxComputeWorkGroupCount[2] |
| 993 | << ":MaxComputeWorkGroupSizeX:" << mResources.MaxComputeWorkGroupSize[0] |
| 994 | << ":MaxComputeWorkGroupSizeY:" << mResources.MaxComputeWorkGroupSize[1] |
| 995 | << ":MaxComputeWorkGroupSizeZ:" << mResources.MaxComputeWorkGroupSize[2] |
| 996 | << ":MaxComputeUniformComponents:" << mResources.MaxComputeUniformComponents |
| 997 | << ":MaxComputeTextureImageUnits:" << mResources.MaxComputeTextureImageUnits |
| 998 | << ":MaxComputeAtomicCounters:" << mResources.MaxComputeAtomicCounters |
| 999 | << ":MaxComputeAtomicCounterBuffers:" << mResources.MaxComputeAtomicCounterBuffers |
| 1000 | << ":MaxVertexAtomicCounters:" << mResources.MaxVertexAtomicCounters |
| 1001 | << ":MaxFragmentAtomicCounters:" << mResources.MaxFragmentAtomicCounters |
| 1002 | << ":MaxCombinedAtomicCounters:" << mResources.MaxCombinedAtomicCounters |
| 1003 | << ":MaxAtomicCounterBindings:" << mResources.MaxAtomicCounterBindings |
| 1004 | << ":MaxVertexAtomicCounterBuffers:" << mResources.MaxVertexAtomicCounterBuffers |
| 1005 | << ":MaxFragmentAtomicCounterBuffers:" << mResources.MaxFragmentAtomicCounterBuffers |
| 1006 | << ":MaxCombinedAtomicCounterBuffers:" << mResources.MaxCombinedAtomicCounterBuffers |
| 1007 | << ":MaxAtomicCounterBufferSize:" << mResources.MaxAtomicCounterBufferSize |
| 1008 | << ":MaxGeometryUniformComponents:" << mResources.MaxGeometryUniformComponents |
| 1009 | << ":MaxGeometryUniformBlocks:" << mResources.MaxGeometryUniformBlocks |
| 1010 | << ":MaxGeometryInputComponents:" << mResources.MaxGeometryInputComponents |
| 1011 | << ":MaxGeometryOutputComponents:" << mResources.MaxGeometryOutputComponents |
| 1012 | << ":MaxGeometryOutputVertices:" << mResources.MaxGeometryOutputVertices |
| 1013 | << ":MaxGeometryTotalOutputComponents:" << mResources.MaxGeometryTotalOutputComponents |
| 1014 | << ":MaxGeometryTextureImageUnits:" << mResources.MaxGeometryTextureImageUnits |
| 1015 | << ":MaxGeometryAtomicCounterBuffers:" << mResources.MaxGeometryAtomicCounterBuffers |
| 1016 | << ":MaxGeometryAtomicCounters:" << mResources.MaxGeometryAtomicCounters |
| 1017 | << ":MaxGeometryShaderStorageBlocks:" << mResources.MaxGeometryShaderStorageBlocks |
| 1018 | << ":MaxGeometryShaderInvocations:" << mResources.MaxGeometryShaderInvocations |
| 1019 | << ":MaxGeometryImageUniforms:" << mResources.MaxGeometryImageUniforms; |
| 1020 | // clang-format on |
| 1021 | |
| 1022 | mBuiltInResourcesString = strstream.str(); |
| 1023 | } |
| 1024 | |
| 1025 | void TCompiler::collectInterfaceBlocks() |
| 1026 | { |
| 1027 | ASSERT(mInterfaceBlocks.empty()); |
| 1028 | mInterfaceBlocks.reserve(mUniformBlocks.size() + mShaderStorageBlocks.size() + |
| 1029 | mInBlocks.size()); |
| 1030 | mInterfaceBlocks.insert(mInterfaceBlocks.end(), mUniformBlocks.begin(), mUniformBlocks.end()); |
| 1031 | mInterfaceBlocks.insert(mInterfaceBlocks.end(), mShaderStorageBlocks.begin(), |
| 1032 | mShaderStorageBlocks.end()); |
| 1033 | mInterfaceBlocks.insert(mInterfaceBlocks.end(), mInBlocks.begin(), mInBlocks.end()); |
| 1034 | } |
| 1035 | |
| 1036 | void TCompiler::clearResults() |
| 1037 | { |
| 1038 | mArrayBoundsClamper.Cleanup(); |
| 1039 | mInfoSink.info.erase(); |
| 1040 | mInfoSink.obj.erase(); |
| 1041 | mInfoSink.debug.erase(); |
| 1042 | mDiagnostics.resetErrorCount(); |
| 1043 | |
| 1044 | mAttributes.clear(); |
| 1045 | mOutputVariables.clear(); |
| 1046 | mUniforms.clear(); |
| 1047 | mInputVaryings.clear(); |
| 1048 | mOutputVaryings.clear(); |
| 1049 | mInterfaceBlocks.clear(); |
| 1050 | mUniformBlocks.clear(); |
| 1051 | mShaderStorageBlocks.clear(); |
| 1052 | mInBlocks.clear(); |
| 1053 | mVariablesCollected = false; |
| 1054 | mGLPositionInitialized = false; |
| 1055 | |
| 1056 | mNumViews = -1; |
| 1057 | |
| 1058 | mGeometryShaderInputPrimitiveType = EptUndefined; |
| 1059 | mGeometryShaderOutputPrimitiveType = EptUndefined; |
| 1060 | mGeometryShaderInvocations = 0; |
| 1061 | mGeometryShaderMaxVertices = -1; |
| 1062 | |
| 1063 | mBuiltInFunctionEmulator.cleanup(); |
| 1064 | |
| 1065 | mNameMap.clear(); |
| 1066 | |
| 1067 | mSourcePath = nullptr; |
| 1068 | |
| 1069 | mSymbolTable.clearCompilationResults(); |
| 1070 | } |
| 1071 | |
| 1072 | bool TCompiler::initCallDag(TIntermNode *root) |
| 1073 | { |
| 1074 | mCallDag.clear(); |
| 1075 | |
| 1076 | switch (mCallDag.init(root, &mDiagnostics)) |
| 1077 | { |
| 1078 | case CallDAG::INITDAG_SUCCESS: |
| 1079 | return true; |
| 1080 | case CallDAG::INITDAG_RECURSION: |
| 1081 | case CallDAG::INITDAG_UNDEFINED: |
| 1082 | // Error message has already been written out. |
| 1083 | ASSERT(mDiagnostics.numErrors() > 0); |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | UNREACHABLE(); |
| 1088 | return true; |
| 1089 | } |
| 1090 | |
| 1091 | bool TCompiler::checkCallDepth() |
| 1092 | { |
| 1093 | std::vector<int> depths(mCallDag.size()); |
| 1094 | |
| 1095 | for (size_t i = 0; i < mCallDag.size(); i++) |
| 1096 | { |
| 1097 | int depth = 0; |
| 1098 | const CallDAG::Record &record = mCallDag.getRecordFromIndex(i); |
| 1099 | |
| 1100 | for (const int &calleeIndex : record.callees) |
| 1101 | { |
| 1102 | depth = std::max(depth, depths[calleeIndex] + 1); |
| 1103 | } |
| 1104 | |
| 1105 | depths[i] = depth; |
| 1106 | |
| 1107 | if (depth >= mResources.MaxCallStackDepth) |
| 1108 | { |
| 1109 | // Trace back the function chain to have a meaningful info log. |
| 1110 | std::stringstream errorStream = sh::InitializeStream<std::stringstream>(); |
| 1111 | errorStream << "Call stack too deep (larger than " << mResources.MaxCallStackDepth |
| 1112 | << ") with the following call chain: " |
| 1113 | << record.node->getFunction()->name(); |
| 1114 | |
| 1115 | int currentFunction = static_cast<int>(i); |
| 1116 | int currentDepth = depth; |
| 1117 | |
| 1118 | while (currentFunction != -1) |
| 1119 | { |
| 1120 | errorStream |
| 1121 | << " -> " |
| 1122 | << mCallDag.getRecordFromIndex(currentFunction).node->getFunction()->name(); |
| 1123 | |
| 1124 | int nextFunction = -1; |
| 1125 | for (const int &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees) |
| 1126 | { |
| 1127 | if (depths[calleeIndex] == currentDepth - 1) |
| 1128 | { |
| 1129 | currentDepth--; |
| 1130 | nextFunction = calleeIndex; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | currentFunction = nextFunction; |
| 1135 | } |
| 1136 | |
| 1137 | std::string errorStr = errorStream.str(); |
| 1138 | mDiagnostics.globalError(errorStr.c_str()); |
| 1139 | |
| 1140 | return false; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | bool TCompiler::tagUsedFunctions() |
| 1148 | { |
| 1149 | // Search from main, starting from the end of the DAG as it usually is the root. |
| 1150 | for (size_t i = mCallDag.size(); i-- > 0;) |
| 1151 | { |
| 1152 | if (mCallDag.getRecordFromIndex(i).node->getFunction()->isMain()) |
| 1153 | { |
| 1154 | internalTagUsedFunction(i); |
| 1155 | return true; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | mDiagnostics.globalError("Missing main()" ); |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
| 1163 | void TCompiler::internalTagUsedFunction(size_t index) |
| 1164 | { |
| 1165 | if (mFunctionMetadata[index].used) |
| 1166 | { |
| 1167 | return; |
| 1168 | } |
| 1169 | |
| 1170 | mFunctionMetadata[index].used = true; |
| 1171 | |
| 1172 | for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees) |
| 1173 | { |
| 1174 | internalTagUsedFunction(calleeIndex); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | // A predicate for the stl that returns if a top-level node is unused |
| 1179 | class TCompiler::UnusedPredicate |
| 1180 | { |
| 1181 | public: |
| 1182 | UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas) |
| 1183 | : mCallDag(callDag), mMetadatas(metadatas) |
| 1184 | {} |
| 1185 | |
| 1186 | bool operator()(TIntermNode *node) |
| 1187 | { |
| 1188 | const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode(); |
| 1189 | const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition(); |
| 1190 | |
| 1191 | const TFunction *func = nullptr; |
| 1192 | |
| 1193 | if (asFunctionDefinition) |
| 1194 | { |
| 1195 | func = asFunctionDefinition->getFunction(); |
| 1196 | } |
| 1197 | else if (asFunctionPrototype) |
| 1198 | { |
| 1199 | func = asFunctionPrototype->getFunction(); |
| 1200 | } |
| 1201 | if (func == nullptr) |
| 1202 | { |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | size_t callDagIndex = mCallDag->findIndex(func->uniqueId()); |
| 1207 | if (callDagIndex == CallDAG::InvalidIndex) |
| 1208 | { |
| 1209 | // This happens only for unimplemented prototypes which are thus unused |
| 1210 | ASSERT(asFunctionPrototype); |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | ASSERT(callDagIndex < mMetadatas->size()); |
| 1215 | return !(*mMetadatas)[callDagIndex].used; |
| 1216 | } |
| 1217 | |
| 1218 | private: |
| 1219 | const CallDAG *mCallDag; |
| 1220 | const std::vector<FunctionMetadata> *mMetadatas; |
| 1221 | }; |
| 1222 | |
| 1223 | void TCompiler::pruneUnusedFunctions(TIntermBlock *root) |
| 1224 | { |
| 1225 | UnusedPredicate isUnused(&mCallDag, &mFunctionMetadata); |
| 1226 | TIntermSequence *sequence = root->getSequence(); |
| 1227 | |
| 1228 | if (!sequence->empty()) |
| 1229 | { |
| 1230 | sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), |
| 1231 | sequence->end()); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | bool TCompiler::limitExpressionComplexity(TIntermBlock *root) |
| 1236 | { |
| 1237 | if (!IsASTDepthBelowLimit(root, mResources.MaxExpressionComplexity)) |
| 1238 | { |
| 1239 | mDiagnostics.globalError("Expression too complex." ); |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
| 1243 | if (!ValidateMaxParameters(root, mResources.MaxFunctionParameters)) |
| 1244 | { |
| 1245 | mDiagnostics.globalError("Function has too many parameters." ); |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
| 1252 | bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions) |
| 1253 | { |
| 1254 | return (compileOptions & SH_VARIABLES) != 0; |
| 1255 | } |
| 1256 | |
| 1257 | bool TCompiler::wereVariablesCollected() const |
| 1258 | { |
| 1259 | return mVariablesCollected; |
| 1260 | } |
| 1261 | |
| 1262 | void TCompiler::initializeGLPosition(TIntermBlock *root) |
| 1263 | { |
| 1264 | InitVariableList list; |
| 1265 | sh::ShaderVariable var(GL_FLOAT_VEC4); |
| 1266 | var.name = "gl_Position" ; |
| 1267 | list.push_back(var); |
| 1268 | InitializeVariables(root, list, &mSymbolTable, mShaderVersion, mExtensionBehavior, false, |
| 1269 | false); |
| 1270 | } |
| 1271 | |
| 1272 | void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root) |
| 1273 | { |
| 1274 | sh::InterfaceBlockList list; |
| 1275 | |
| 1276 | for (const sh::InterfaceBlock &block : mUniformBlocks) |
| 1277 | { |
| 1278 | if (!block.staticUse && |
| 1279 | (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED)) |
| 1280 | { |
| 1281 | list.push_back(block); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | sh::UseInterfaceBlockFields(root, list, mSymbolTable); |
| 1286 | } |
| 1287 | |
| 1288 | void TCompiler::initializeOutputVariables(TIntermBlock *root) |
| 1289 | { |
| 1290 | InitVariableList list; |
| 1291 | if (mShaderType == GL_VERTEX_SHADER || mShaderType == GL_GEOMETRY_SHADER_EXT) |
| 1292 | { |
| 1293 | for (const sh::Varying &var : mOutputVaryings) |
| 1294 | { |
| 1295 | list.push_back(var); |
| 1296 | if (var.name == "gl_Position" ) |
| 1297 | { |
| 1298 | ASSERT(!mGLPositionInitialized); |
| 1299 | mGLPositionInitialized = true; |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | else |
| 1304 | { |
| 1305 | ASSERT(mShaderType == GL_FRAGMENT_SHADER); |
| 1306 | for (const sh::OutputVariable &var : mOutputVariables) |
| 1307 | { |
| 1308 | list.push_back(var); |
| 1309 | } |
| 1310 | } |
| 1311 | InitializeVariables(root, list, &mSymbolTable, mShaderVersion, mExtensionBehavior, false, |
| 1312 | false); |
| 1313 | } |
| 1314 | |
| 1315 | const TExtensionBehavior &TCompiler::getExtensionBehavior() const |
| 1316 | { |
| 1317 | return mExtensionBehavior; |
| 1318 | } |
| 1319 | |
| 1320 | const char *TCompiler::getSourcePath() const |
| 1321 | { |
| 1322 | return mSourcePath; |
| 1323 | } |
| 1324 | |
| 1325 | const ShBuiltInResources &TCompiler::getResources() const |
| 1326 | { |
| 1327 | return mResources; |
| 1328 | } |
| 1329 | |
| 1330 | const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const |
| 1331 | { |
| 1332 | return mArrayBoundsClamper; |
| 1333 | } |
| 1334 | |
| 1335 | ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const |
| 1336 | { |
| 1337 | return mResources.ArrayIndexClampingStrategy; |
| 1338 | } |
| 1339 | |
| 1340 | const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const |
| 1341 | { |
| 1342 | return mBuiltInFunctionEmulator; |
| 1343 | } |
| 1344 | |
| 1345 | void TCompiler::writePragma(ShCompileOptions compileOptions) |
| 1346 | { |
| 1347 | if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL)) |
| 1348 | { |
| 1349 | TInfoSinkBase &sink = mInfoSink.obj; |
| 1350 | if (mPragma.stdgl.invariantAll) |
| 1351 | sink << "#pragma STDGL invariant(all)\n" ; |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | bool TCompiler::isVaryingDefined(const char *varyingName) |
| 1356 | { |
| 1357 | ASSERT(mVariablesCollected); |
| 1358 | for (size_t ii = 0; ii < mInputVaryings.size(); ++ii) |
| 1359 | { |
| 1360 | if (mInputVaryings[ii].name == varyingName) |
| 1361 | { |
| 1362 | return true; |
| 1363 | } |
| 1364 | } |
| 1365 | for (size_t ii = 0; ii < mOutputVaryings.size(); ++ii) |
| 1366 | { |
| 1367 | if (mOutputVaryings[ii].name == varyingName) |
| 1368 | { |
| 1369 | return true; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
| 1376 | } // namespace sh |
| 1377 | |