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