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