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