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