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" |
Qiankun Miao | 09cfac6 | 2016-09-06 17:25:16 +0800 | [diff] [blame] | 13 | #include "compiler/translator/AddAndTrueToLoopCondition.h" |
Dmitry Skiba | 0197111 | 2015-07-10 14:54:00 -0400 | [diff] [blame] | 14 | #include "compiler/translator/Cache.h" |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 15 | #include "compiler/translator/CallDAG.h" |
Martin Radev | 69056a1 | 2017-05-18 11:14:50 +0300 | [diff] [blame] | 16 | #include "compiler/translator/DeclareAndInitBuiltinsForInstancedMultiview.h" |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 17 | #include "compiler/translator/DeferGlobalInitializers.h" |
Zhenyao Mo | 4e94fea | 2016-08-09 14:31:37 -0700 | [diff] [blame] | 18 | #include "compiler/translator/EmulateGLFragColorBroadcast.h" |
Jamie Madill | d569619 | 2016-10-06 11:09:24 -0400 | [diff] [blame] | 19 | #include "compiler/translator/EmulatePrecision.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 20 | #include "compiler/translator/Initialize.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 21 | #include "compiler/translator/InitializeVariables.h" |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 22 | #include "compiler/translator/IntermNodePatternMatcher.h" |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 23 | #include "compiler/translator/IsASTDepthBelowLimit.h" |
| 24 | #include "compiler/translator/OutputTree.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 25 | #include "compiler/translator/ParseContext.h" |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 26 | #include "compiler/translator/PruneEmptyDeclarations.h" |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 27 | #include "compiler/translator/RegenerateStructNames.h" |
Qiankun Miao | 705a919 | 2016-08-29 10:05:27 +0800 | [diff] [blame] | 28 | #include "compiler/translator/RemoveInvariantDeclaration.h" |
Olli Etuaho | 5c407bb | 2015-06-01 12:20:39 +0300 | [diff] [blame] | 29 | #include "compiler/translator/RemovePow.h" |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 30 | #include "compiler/translator/RewriteDoWhile.h" |
Zhenyao Mo | cd68fe7 | 2014-07-11 10:45:44 -0700 | [diff] [blame] | 31 | #include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h" |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 32 | #include "compiler/translator/SeparateDeclarations.h" |
| 33 | #include "compiler/translator/SimplifyLoopConditions.h" |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 34 | #include "compiler/translator/UnfoldShortCircuitAST.h" |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 35 | #include "compiler/translator/UseInterfaceBlockFields.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 36 | #include "compiler/translator/ValidateLimitations.h" |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 37 | #include "compiler/translator/ValidateMaxParameters.h" |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 38 | #include "compiler/translator/ValidateMultiviewWebGL.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 39 | #include "compiler/translator/ValidateOutputs.h" |
| 40 | #include "compiler/translator/VariablePacker.h" |
shannon.woods@transgaming.com | da1ed36 | 2013-01-25 21:54:57 +0000 | [diff] [blame] | 41 | #include "third_party/compiler/ArrayBoundsClamper.h" |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 42 | |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 43 | namespace sh |
| 44 | { |
| 45 | |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 46 | namespace |
| 47 | { |
| 48 | |
| 49 | #if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 50 | void DumpFuzzerCase(char const *const *shaderStrings, |
| 51 | size_t numStrings, |
| 52 | uint32_t type, |
| 53 | uint32_t spec, |
| 54 | uint32_t output, |
| 55 | uint64_t options) |
| 56 | { |
| 57 | static int fileIndex = 0; |
| 58 | |
| 59 | std::ostringstream o; |
| 60 | o << "corpus/" << fileIndex++ << ".sample"; |
| 61 | std::string s = o.str(); |
| 62 | |
| 63 | // Must match the input format of the fuzzer |
| 64 | FILE *f = fopen(s.c_str(), "w"); |
| 65 | fwrite(&type, sizeof(type), 1, f); |
| 66 | fwrite(&spec, sizeof(spec), 1, f); |
| 67 | fwrite(&output, sizeof(output), 1, f); |
| 68 | fwrite(&options, sizeof(options), 1, f); |
| 69 | |
| 70 | char zero[128 - 20] = {0}; |
| 71 | fwrite(&zero, 128 - 20, 1, f); |
| 72 | |
| 73 | for (size_t i = 0; i < numStrings; i++) |
| 74 | { |
| 75 | fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f); |
| 76 | } |
| 77 | fwrite(&zero, 1, 1, f); |
| 78 | |
| 79 | fclose(f); |
| 80 | } |
| 81 | #endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 82 | } // anonymous namespace |
| 83 | |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 84 | bool IsWebGLBasedSpec(ShShaderSpec spec) |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 85 | { |
Qiankun Miao | c2c5fc4 | 2016-08-31 15:24:22 +0800 | [diff] [blame] | 86 | 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] | 87 | } |
| 88 | |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 89 | bool IsGLSL130OrNewer(ShShaderOutput output) |
| 90 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 91 | return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT || |
| 92 | output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT || |
| 93 | output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT || |
| 94 | output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT || |
| 95 | output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT); |
Qingqing Deng | ad0d079 | 2015-04-08 14:25:06 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Qiankun Miao | 705a919 | 2016-08-29 10:05:27 +0800 | [diff] [blame] | 98 | bool IsGLSL420OrNewer(ShShaderOutput output) |
| 99 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 100 | return (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); |
Qiankun Miao | 705a919 | 2016-08-29 10:05:27 +0800 | [diff] [blame] | 102 | } |
| 103 | |
Zhenyao Mo | b7bf742 | 2016-11-08 14:44:05 -0800 | [diff] [blame] | 104 | bool IsGLSL410OrOlder(ShShaderOutput output) |
| 105 | { |
| 106 | return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT || |
| 107 | output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT || |
| 108 | output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT); |
| 109 | } |
| 110 | |
Qiankun Miao | 89dd8f3 | 2016-11-09 12:59:30 +0000 | [diff] [blame] | 111 | bool RemoveInvariant(sh::GLenum shaderType, |
| 112 | int shaderVersion, |
| 113 | ShShaderOutput outputType, |
| 114 | ShCompileOptions compileOptions) |
| 115 | { |
| 116 | if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 && |
| 117 | shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType)) |
| 118 | return true; |
| 119 | |
| 120 | if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 && |
Qiankun Miao | 41f9f67 | 2016-11-16 17:04:36 +0800 | [diff] [blame] | 121 | shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER) |
Qiankun Miao | 89dd8f3 | 2016-11-09 12:59:30 +0000 | [diff] [blame] | 122 | return true; |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
Zhenyao Mo | 7faf1a1 | 2014-04-25 18:03:56 -0700 | [diff] [blame] | 127 | size_t GetGlobalMaxTokenSize(ShShaderSpec spec) |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 128 | { |
He Yunchao | 29ab9ff | 2015-08-06 16:58:30 +0800 | [diff] [blame] | 129 | // 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] | 130 | // size undefined. ES3 defines a max size of 1024 characters. |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 131 | switch (spec) |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 132 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 133 | case SH_WEBGL_SPEC: |
| 134 | return 256; |
| 135 | default: |
| 136 | return 1024; |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 140 | int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources) |
| 141 | { |
| 142 | switch (shaderType) |
| 143 | { |
| 144 | case GL_VERTEX_SHADER: |
| 145 | return resources.MaxVertexUniformVectors; |
| 146 | case GL_FRAGMENT_SHADER: |
| 147 | return resources.MaxFragmentUniformVectors; |
| 148 | case GL_COMPUTE_SHADER: |
| 149 | // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting |
| 150 | return resources.MaxComputeUniformComponents / 4; |
| 151 | default: |
| 152 | UNIMPLEMENTED(); |
| 153 | return -1; |
| 154 | } |
| 155 | } |
| 156 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 157 | namespace |
| 158 | { |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 159 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 160 | class TScopedPoolAllocator |
| 161 | { |
| 162 | public: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 163 | TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 164 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 165 | mAllocator->push(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 166 | SetGlobalPoolAllocator(mAllocator); |
| 167 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 168 | ~TScopedPoolAllocator() |
| 169 | { |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 170 | SetGlobalPoolAllocator(nullptr); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 171 | mAllocator->pop(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 174 | private: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 175 | TPoolAllocator *mAllocator; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 176 | }; |
| 177 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 178 | class TScopedSymbolTableLevel |
| 179 | { |
| 180 | public: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 181 | TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 182 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 183 | ASSERT(mTable->atBuiltInLevel()); |
| 184 | mTable->push(); |
| 185 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 186 | ~TScopedSymbolTableLevel() |
| 187 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 188 | while (!mTable->atBuiltInLevel()) |
| 189 | mTable->pop(); |
| 190 | } |
| 191 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 192 | private: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 193 | TSymbolTable *mTable; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 194 | }; |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 195 | |
| 196 | int MapSpecToShaderVersion(ShShaderSpec spec) |
| 197 | { |
| 198 | switch (spec) |
| 199 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 200 | case SH_GLES2_SPEC: |
| 201 | case SH_WEBGL_SPEC: |
| 202 | return 100; |
| 203 | case SH_GLES3_SPEC: |
| 204 | case SH_WEBGL2_SPEC: |
| 205 | return 300; |
| 206 | case SH_GLES3_1_SPEC: |
| 207 | case SH_WEBGL3_SPEC: |
| 208 | return 310; |
| 209 | default: |
| 210 | UNREACHABLE(); |
| 211 | return 0; |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 215 | } // namespace |
| 216 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 217 | TShHandleBase::TShHandleBase() |
| 218 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 219 | allocator.push(); |
| 220 | SetGlobalPoolAllocator(&allocator); |
| 221 | } |
| 222 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 223 | TShHandleBase::~TShHandleBase() |
| 224 | { |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 225 | SetGlobalPoolAllocator(nullptr); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 226 | allocator.popAll(); |
| 227 | } |
| 228 | |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 229 | TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output) |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 230 | : variablesCollected(false), |
Olli Etuaho | b12040c | 2017-06-27 14:20:45 +0300 | [diff] [blame] | 231 | mGLPositionInitialized(false), |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 232 | shaderType(type), |
zmo@google.com | f420c42 | 2011-09-12 18:27:59 +0000 | [diff] [blame] | 233 | shaderSpec(spec), |
Jamie Madill | 68fe74a | 2014-05-27 12:56:01 -0400 | [diff] [blame] | 234 | outputType(output), |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 235 | maxUniformVectors(0), |
| 236 | maxExpressionComplexity(0), |
| 237 | maxCallStackDepth(0), |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 238 | maxFunctionParameters(0), |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 239 | fragmentPrecisionHigh(false), |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 240 | clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC), |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 241 | builtInFunctionEmulator(), |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 242 | mDiagnostics(infoSink.info), |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 243 | mSourcePath(nullptr), |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 244 | mComputeShaderLocalSizeDeclared(false) |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 245 | { |
Martin Radev | 802abe0 | 2016-08-04 17:48:32 +0300 | [diff] [blame] | 246 | mComputeShaderLocalSize.fill(1); |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | TCompiler::~TCompiler() |
| 250 | { |
| 251 | } |
| 252 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 253 | bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const |
Olli Etuaho | 5d91dda | 2015-06-18 15:47:46 +0300 | [diff] [blame] | 254 | { |
| 255 | // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API, |
| 256 | // validate loop and indexing as well (to verify that the shader only uses minimal functionality |
| 257 | // of ESSL 1.00 as in Appendix A of the spec). |
| 258 | return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) || |
| 259 | (compileOptions & SH_VALIDATE_LOOP_INDEXING); |
| 260 | } |
| 261 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 262 | bool TCompiler::Init(const ShBuiltInResources &resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 263 | { |
Shao | 77891c0 | 2017-06-23 16:30:17 +0800 | [diff] [blame] | 264 | shaderVersion = 100; |
| 265 | |
| 266 | maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources); |
| 267 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 268 | maxExpressionComplexity = resources.MaxExpressionComplexity; |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 269 | maxCallStackDepth = resources.MaxCallStackDepth; |
| 270 | maxFunctionParameters = resources.MaxFunctionParameters; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 271 | |
| 272 | SetGlobalPoolAllocator(&allocator); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 273 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 274 | // Generate built-in symbol table. |
| 275 | if (!InitBuiltInSymbolTable(resources)) |
| 276 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 277 | InitExtensionBehavior(resources, extensionBehavior); |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 278 | fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 279 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 280 | arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy); |
| 281 | clampingStrategy = resources.ArrayIndexClampingStrategy; |
| 282 | |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 283 | hashFunction = resources.HashFunction; |
| 284 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 288 | TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[], |
| 289 | size_t numStrings, |
| 290 | ShCompileOptions compileOptions) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 291 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 292 | return compileTreeImpl(shaderStrings, numStrings, compileOptions); |
| 293 | } |
| 294 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 295 | TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[], |
| 296 | size_t numStrings, |
| 297 | const ShCompileOptions compileOptions) |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 298 | { |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 299 | clearResults(); |
| 300 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 301 | ASSERT(numStrings > 0); |
| 302 | ASSERT(GetGlobalPoolAllocator()); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 303 | |
David Yen | 0fbd128 | 2015-02-02 14:46:09 -0800 | [diff] [blame] | 304 | // Reset the extension behavior for each compilation unit. |
| 305 | ResetExtensionBehavior(extensionBehavior); |
| 306 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 307 | // 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] | 308 | size_t firstSource = 0; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 309 | if (compileOptions & SH_SOURCE_PATH) |
| 310 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 311 | mSourcePath = shaderStrings[0]; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 312 | ++firstSource; |
| 313 | } |
| 314 | |
Olli Etuaho | f119a26 | 2016-08-19 15:54:22 +0300 | [diff] [blame] | 315 | TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec, |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 316 | compileOptions, true, &mDiagnostics, getResources()); |
Olli Etuaho | 853dc1a | 2014-11-06 17:25:48 +0200 | [diff] [blame] | 317 | |
Olli Etuaho | a699668 | 2015-10-12 14:32:30 +0300 | [diff] [blame] | 318 | parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 319 | |
| 320 | // We preserve symbols at the built-in level from compile-to-compile. |
| 321 | // Start pushing the user-defined symbols at global level. |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 322 | TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 323 | |
| 324 | // Parse shader. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 325 | bool success = (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, |
| 326 | &parseContext) == 0) && |
| 327 | (parseContext.getTreeRoot() != nullptr); |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 328 | |
shannon.woods%transgaming.com@gtempaccount.com | 5524db0 | 2013-04-13 03:38:16 +0000 | [diff] [blame] | 329 | shaderVersion = parseContext.getShaderVersion(); |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 330 | if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion) |
| 331 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 332 | mDiagnostics.globalError("unsupported shader version"); |
Zhenyao Mo | db9b40b | 2014-10-29 15:00:04 -0700 | [diff] [blame] | 333 | success = false; |
| 334 | } |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 335 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 336 | TIntermBlock *root = nullptr; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 337 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 338 | if (success) |
| 339 | { |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 340 | mPragma = parseContext.pragma(); |
Kenneth Russell | 8bad46d | 2016-07-01 19:52:52 -0700 | [diff] [blame] | 341 | symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll); |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 342 | |
Martin Radev | 802abe0 | 2016-08-04 17:48:32 +0300 | [diff] [blame] | 343 | mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared(); |
| 344 | mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize(); |
| 345 | |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 346 | mNumViews = parseContext.getNumViews(); |
| 347 | |
Jamie Madill | 6e06b1f | 2015-05-14 10:01:17 -0400 | [diff] [blame] | 348 | root = parseContext.getTreeRoot(); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 349 | |
Olli Etuaho | a699668 | 2015-10-12 14:32:30 +0300 | [diff] [blame] | 350 | // Highp might have been auto-enabled based on shader version |
| 351 | fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh(); |
| 352 | |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 353 | if (success && (IsWebGLBasedSpec(shaderSpec) && |
| 354 | IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") && |
| 355 | IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2"))) |
| 356 | { |
| 357 | // Can't enable both extensions at the same time. |
| 358 | mDiagnostics.globalError("Can't enable both OVR_multiview and OVR_multiview2"); |
| 359 | success = false; |
| 360 | } |
| 361 | |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 362 | // Disallow expressions deemed too complex. |
| 363 | if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY)) |
| 364 | success = limitExpressionComplexity(root); |
| 365 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 366 | // Create the function DAG and check there is no recursion |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 367 | if (success) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 368 | success = initCallDag(root); |
| 369 | |
| 370 | if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH)) |
| 371 | success = checkCallDepth(); |
| 372 | |
| 373 | // Checks which functions are used and if "main" exists |
| 374 | if (success) |
| 375 | { |
| 376 | functionMetadata.clear(); |
| 377 | functionMetadata.resize(mCallDag.size()); |
| 378 | success = tagUsedFunctions(); |
| 379 | } |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 380 | |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 381 | if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS)) |
| 382 | success = pruneUnusedFunctions(root); |
| 383 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 384 | // Prune empty declarations to work around driver bugs and to keep declaration output |
| 385 | // simple. |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 386 | if (success) |
| 387 | PruneEmptyDeclarations(root); |
| 388 | |
Andrei Volykhin | 73fa4b8 | 2017-05-05 18:45:06 +0300 | [diff] [blame] | 389 | if (success && shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER) |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 390 | { |
| 391 | success = ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers, |
| 392 | &mDiagnostics); |
| 393 | } |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 394 | |
Olli Etuaho | 5d91dda | 2015-06-18 15:47:46 +0300 | [diff] [blame] | 395 | if (success && shouldRunLoopAndIndexingValidation(compileOptions)) |
Olli Etuaho | 9ec7939 | 2017-03-31 23:04:23 +0300 | [diff] [blame] | 396 | success = |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 397 | ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 398 | |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 399 | bool multiview2 = IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2"); |
| 400 | if (success && compileResources.OVR_multiview && IsWebGLBasedSpec(shaderSpec) && |
| 401 | (IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") || multiview2)) |
Olli Etuaho | 01d0ad0 | 2017-01-22 14:51:23 -0800 | [diff] [blame] | 402 | success = ValidateMultiviewWebGL(root, shaderType, symbolTable, shaderVersion, |
| 403 | multiview2, &mDiagnostics); |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 404 | |
Jamie Madill | d569619 | 2016-10-06 11:09:24 -0400 | [diff] [blame] | 405 | // Fail compilation if precision emulation not supported. |
| 406 | if (success && getResources().WEBGL_debug_shader_precision && |
| 407 | getPragma().debugShaderPrecision) |
| 408 | { |
| 409 | if (!EmulatePrecision::SupportedInLanguage(outputType)) |
| 410 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 411 | mDiagnostics.globalError("Precision emulation not supported for this output type."); |
Jamie Madill | d569619 | 2016-10-06 11:09:24 -0400 | [diff] [blame] | 412 | success = false; |
| 413 | } |
| 414 | } |
| 415 | |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 416 | // Built-in function emulation needs to happen after validateLimitations pass. |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 417 | if (success) |
| 418 | { |
Jamie Madill | 438dbcf | 2016-06-17 14:20:05 -0400 | [diff] [blame] | 419 | // TODO(jmadill): Remove global pool allocator. |
| 420 | GetGlobalPoolAllocator()->lock(); |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 421 | initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions); |
Jamie Madill | 438dbcf | 2016-06-17 14:20:05 -0400 | [diff] [blame] | 422 | GetGlobalPoolAllocator()->unlock(); |
Olli Etuaho | dfa75e8 | 2017-01-23 09:43:06 -0800 | [diff] [blame] | 423 | builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root); |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 424 | } |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 425 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 426 | // Clamping uniform array bounds needs to happen after validateLimitations pass. |
| 427 | if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)) |
| 428 | arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root); |
| 429 | |
Martin Radev | 69056a1 | 2017-05-18 11:14:50 +0300 | [diff] [blame] | 430 | if (success && (compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) && |
Martin Radev | 7ef89a4 | 2017-07-05 14:23:06 +0300 | [diff] [blame] | 431 | parseContext.isMultiviewExtensionEnabled() && getShaderType() != GL_COMPUTE_SHADER) |
Martin Radev | 69056a1 | 2017-05-18 11:14:50 +0300 | [diff] [blame] | 432 | { |
Martin Radev | c39a19a | 2017-07-07 18:52:09 +0300 | [diff] [blame] | 433 | DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions, |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 434 | outputType, &symbolTable); |
Martin Radev | 69056a1 | 2017-05-18 11:14:50 +0300 | [diff] [blame] | 435 | } |
| 436 | |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 437 | // This pass might emit short circuits so keep it before the short circuit unfolding |
| 438 | if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)) |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 439 | RewriteDoWhile(root, &symbolTable); |
Corentin Wallez | d4b5054 | 2015-09-28 12:19:26 -0700 | [diff] [blame] | 440 | |
Qiankun Miao | 09cfac6 | 2016-09-06 17:25:16 +0800 | [diff] [blame] | 441 | if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)) |
| 442 | sh::AddAndTrueToLoopCondition(root); |
| 443 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 444 | if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)) |
| 445 | { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 446 | UnfoldShortCircuitAST unfoldShortCircuit; |
| 447 | root->traverse(&unfoldShortCircuit); |
| 448 | unfoldShortCircuit.updateTree(); |
| 449 | } |
| 450 | |
Olli Etuaho | 5c407bb | 2015-06-01 12:20:39 +0300 | [diff] [blame] | 451 | if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)) |
| 452 | { |
| 453 | RemovePow(root); |
| 454 | } |
| 455 | |
Olli Etuaho | 4dfe809 | 2015-08-21 17:44:35 +0300 | [diff] [blame] | 456 | if (success && shouldCollectVariables(compileOptions)) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 457 | { |
Olli Etuaho | 1951501 | 2017-06-26 18:00:17 +0300 | [diff] [blame] | 458 | ASSERT(!variablesCollected); |
| 459 | CollectVariables(root, &attributes, &outputVariables, &uniforms, &varyings, |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 460 | &interfaceBlocks, hashFunction, &symbolTable, shaderVersion, |
Olli Etuaho | 1951501 | 2017-06-26 18:00:17 +0300 | [diff] [blame] | 461 | extensionBehavior); |
| 462 | variablesCollected = true; |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 463 | if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS) |
| 464 | { |
| 465 | useAllMembersInUnusedStandardAndSharedBlocks(root); |
| 466 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 467 | if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) |
| 468 | { |
Olli Etuaho | 1951501 | 2017-06-26 18:00:17 +0300 | [diff] [blame] | 469 | std::vector<sh::ShaderVariable> expandedUniforms; |
| 470 | sh::ExpandUniforms(uniforms, &expandedUniforms); |
| 471 | VariablePacker packer; |
| 472 | // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec |
| 473 | // Appendix A, section 7, the shader does not use too many uniforms. |
| 474 | success = |
| 475 | packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 476 | if (!success) |
| 477 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 478 | mDiagnostics.globalError("too many uniforms"); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 481 | if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES)) |
| 482 | { |
Olli Etuaho | 27776e3 | 2016-07-22 14:00:56 +0300 | [diff] [blame] | 483 | initializeOutputVariables(root); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 484 | } |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 485 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 486 | |
Olli Etuaho | b12040c | 2017-06-27 14:20:45 +0300 | [diff] [blame] | 487 | // gl_Position is always written in compatibility output mode. |
| 488 | // It may have been already initialized among other output variables, in that case we don't |
| 489 | // need to initialize it twice. |
| 490 | if (success && shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized && |
| 491 | ((compileOptions & SH_INIT_GL_POSITION) || |
| 492 | (outputType == SH_GLSL_COMPATIBILITY_OUTPUT))) |
| 493 | { |
| 494 | initializeGLPosition(root); |
| 495 | mGLPositionInitialized = true; |
| 496 | } |
| 497 | |
Yuly Novikov | 817232e | 2017-02-22 18:36:10 -0500 | [diff] [blame] | 498 | // Removing invariant declarations must be done after collecting variables. |
| 499 | // Otherwise, built-in invariant declarations don't apply. |
| 500 | if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions)) |
| 501 | sh::RemoveInvariantDeclaration(root); |
| 502 | |
Zhenyao Mo | cd68fe7 | 2014-07-11 10:45:44 -0700 | [diff] [blame] | 503 | if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)) |
| 504 | { |
Olli Etuaho | b990b55 | 2016-10-27 12:29:17 +0100 | [diff] [blame] | 505 | ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh, |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 506 | &symbolTable); |
Zhenyao Mo | cd68fe7 | 2014-07-11 10:45:44 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 509 | if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES)) |
| 510 | { |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 511 | RegenerateStructNames gen(&symbolTable, shaderVersion); |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 512 | root->traverse(&gen); |
| 513 | } |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 514 | |
Zhenyao Mo | 4e94fea | 2016-08-09 14:31:37 -0700 | [diff] [blame] | 515 | if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 && |
| 516 | compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 && |
| 517 | IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers")) |
| 518 | { |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 519 | EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables, |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 520 | &symbolTable, shaderVersion); |
Zhenyao Mo | 4e94fea | 2016-08-09 14:31:37 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 523 | if (success) |
| 524 | { |
Olli Etuaho | 0ffc441 | 2017-05-19 14:18:55 +0300 | [diff] [blame] | 525 | DeferGlobalInitializers(root, needToInitializeGlobalsInAST()); |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 526 | } |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 527 | |
| 528 | if (success && (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && getOutputType()) |
| 529 | { |
| 530 | // Initialize uninitialized local variables. |
| 531 | // In some cases initializing can generate extra statements in the parent block, such as |
| 532 | // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case |
| 533 | // we need to first simplify loop conditions and separate declarations. If we don't |
| 534 | // follow the Appendix A limitations, loop init statements can declare arrays or |
| 535 | // nameless structs and have multiple declarations. |
| 536 | |
| 537 | if (!shouldRunLoopAndIndexingValidation(compileOptions)) |
| 538 | { |
| 539 | SimplifyLoopConditions(root, |
| 540 | IntermNodePatternMatcher::kMultiDeclaration | |
| 541 | IntermNodePatternMatcher::kArrayDeclaration | |
| 542 | IntermNodePatternMatcher::kNamelessStructDeclaration, |
Olli Etuaho | a5e693a | 2017-07-13 16:07:26 +0300 | [diff] [blame^] | 543 | &getSymbolTable(), getShaderVersion()); |
Olli Etuaho | 9733cee | 2017-05-11 19:14:35 +0300 | [diff] [blame] | 544 | } |
| 545 | // We only really need to separate array declarations and nameless struct declarations, |
| 546 | // but it's simpler to just use the regular SeparateDeclarations. |
| 547 | SeparateDeclarations(root); |
| 548 | InitializeUninitializedLocals(root, getShaderVersion()); |
| 549 | } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 552 | if (success) |
| 553 | return root; |
| 554 | |
Yunchao He | f81ce4a | 2017-04-24 10:49:17 +0800 | [diff] [blame] | 555 | return nullptr; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 558 | bool TCompiler::compile(const char *const shaderStrings[], |
| 559 | size_t numStrings, |
| 560 | ShCompileOptions compileOptionsIn) |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 561 | { |
Corentin Wallez | 28b6528 | 2016-06-16 07:24:50 -0700 | [diff] [blame] | 562 | #if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 563 | DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn); |
| 564 | #endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT) |
| 565 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 566 | if (numStrings == 0) |
| 567 | return true; |
| 568 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 569 | ShCompileOptions compileOptions = compileOptionsIn; |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 570 | |
| 571 | // Apply key workarounds. |
| 572 | if (shouldFlattenPragmaStdglInvariantAll()) |
| 573 | { |
| 574 | // This should be harmless to do in all cases, but for the moment, do it only conditionally. |
| 575 | compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL; |
| 576 | } |
| 577 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 578 | TScopedPoolAllocator scopedAlloc(&allocator); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 579 | TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 580 | |
| 581 | if (root) |
| 582 | { |
| 583 | if (compileOptions & SH_INTERMEDIATE_TREE) |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 584 | OutputTree(root, infoSink.info); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 585 | |
| 586 | if (compileOptions & SH_OBJECT_CODE) |
| 587 | translate(root, compileOptions); |
| 588 | |
| 589 | // The IntermNode tree doesn't need to be deleted here, since the |
| 590 | // memory will be freed in a big chunk by the PoolAllocator. |
| 591 | return true; |
| 592 | } |
| 593 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 596 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 597 | { |
Olli Etuaho | 28cb036 | 2016-11-22 15:42:37 +0000 | [diff] [blame] | 598 | if (resources.MaxDrawBuffers < 1) |
| 599 | { |
| 600 | return false; |
| 601 | } |
| 602 | if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1) |
| 603 | { |
| 604 | return false; |
| 605 | } |
| 606 | |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 607 | compileResources = resources; |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 608 | setResourceString(); |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 609 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 610 | assert(symbolTable.isEmpty()); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 611 | symbolTable.push(); // COMMON_BUILTINS |
| 612 | symbolTable.push(); // ESSL1_BUILTINS |
| 613 | symbolTable.push(); // ESSL3_BUILTINS |
| 614 | symbolTable.push(); // ESSL3_1_BUILTINS |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 615 | |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 616 | switch (shaderType) |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 617 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 618 | case GL_FRAGMENT_SHADER: |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 619 | symbolTable.setDefaultPrecision(EbtInt, EbpMedium); |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 620 | break; |
| 621 | case GL_VERTEX_SHADER: |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 622 | symbolTable.setDefaultPrecision(EbtInt, EbpHigh); |
| 623 | symbolTable.setDefaultPrecision(EbtFloat, EbpHigh); |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 624 | break; |
| 625 | case GL_COMPUTE_SHADER: |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 626 | symbolTable.setDefaultPrecision(EbtInt, EbpHigh); |
| 627 | symbolTable.setDefaultPrecision(EbtFloat, EbpHigh); |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 628 | break; |
| 629 | default: |
| 630 | assert(false && "Language not supported"); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 631 | } |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 632 | // Set defaults for sampler types that have default precision, even those that are |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 633 | // only available if an extension exists. |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 634 | // New sampler types in ESSL3 don't have default precision. ESSL1 types do. |
| 635 | initSamplerDefaultPrecision(EbtSampler2D); |
| 636 | initSamplerDefaultPrecision(EbtSamplerCube); |
| 637 | // SamplerExternalOES is specified in the extension to have default precision. |
| 638 | initSamplerDefaultPrecision(EbtSamplerExternalOES); |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 639 | // SamplerExternal2DY2YEXT is specified in the extension to have default precision. |
| 640 | initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT); |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 641 | // It isn't specified whether Sampler2DRect has default precision. |
| 642 | initSamplerDefaultPrecision(EbtSampler2DRect); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 643 | |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 644 | symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh); |
jchen10 | 4cdac9e | 2017-05-08 11:01:20 +0800 | [diff] [blame] | 645 | |
Jamie Madill | 1b45214 | 2013-07-12 14:51:11 -0400 | [diff] [blame] | 646 | InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 647 | |
| 648 | IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable); |
| 649 | |
| 650 | return true; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 653 | void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType) |
| 654 | { |
| 655 | ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd); |
Olli Etuaho | cce8965 | 2017-06-19 16:04:09 +0300 | [diff] [blame] | 656 | symbolTable.setDefaultPrecision(samplerType, EbpLow); |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 659 | void TCompiler::setResourceString() |
| 660 | { |
| 661 | std::ostringstream strstream; |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 662 | |
| 663 | // clang-format off |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 664 | strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 665 | << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors |
| 666 | << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors |
| 667 | << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits |
| 668 | << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits |
| 669 | << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits |
| 670 | << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors |
| 671 | << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers |
| 672 | << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives |
| 673 | << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external |
| 674 | << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3 |
| 675 | << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external |
| 676 | << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle |
| 677 | << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers |
| 678 | << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh |
| 679 | << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity |
| 680 | << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth |
| 681 | << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters |
| 682 | << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended |
| 683 | << ":EXT_frag_depth:" << compileResources.EXT_frag_depth |
| 684 | << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod |
| 685 | << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch |
| 686 | << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch |
| 687 | << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch |
Martin Radev | 318f9aa | 2017-05-17 17:47:28 +0300 | [diff] [blame] | 688 | << ":OVR_multiview:" << compileResources.OVR_multiview |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 689 | << ":EXT_YUV_target:" << compileResources.EXT_YUV_target |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 690 | << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors |
| 691 | << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors |
| 692 | << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset |
| 693 | << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset |
| 694 | << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers |
Martin Radev | 318f9aa | 2017-05-17 17:47:28 +0300 | [diff] [blame] | 695 | << ":MaxViewsOVR:" << compileResources.MaxViewsOVR |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 696 | << ":NV_draw_buffers:" << compileResources.NV_draw_buffers |
| 697 | << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision |
| 698 | << ":MaxImageUnits:" << compileResources.MaxImageUnits |
| 699 | << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms |
| 700 | << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms |
| 701 | << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms |
| 702 | << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms |
| 703 | << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources |
| 704 | << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0] |
| 705 | << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1] |
| 706 | << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2] |
| 707 | << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0] |
| 708 | << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1] |
| 709 | << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2] |
| 710 | << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents |
| 711 | << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits |
| 712 | << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters |
| 713 | << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers |
| 714 | << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters |
| 715 | << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters |
| 716 | << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters |
| 717 | << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings |
| 718 | << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers |
| 719 | << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers |
| 720 | << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers |
| 721 | << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize; |
Geoff Lang | b66a909 | 2016-05-16 15:59:14 -0400 | [diff] [blame] | 722 | // clang-format on |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 723 | |
| 724 | builtInResourcesString = strstream.str(); |
| 725 | } |
| 726 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 727 | void TCompiler::clearResults() |
| 728 | { |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 729 | arrayBoundsClamper.Cleanup(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 730 | infoSink.info.erase(); |
| 731 | infoSink.obj.erase(); |
| 732 | infoSink.debug.erase(); |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 733 | mDiagnostics.resetErrorCount(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 734 | |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 735 | attributes.clear(); |
| 736 | outputVariables.clear(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 737 | uniforms.clear(); |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 738 | varyings.clear(); |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 739 | interfaceBlocks.clear(); |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 740 | variablesCollected = false; |
Olli Etuaho | b12040c | 2017-06-27 14:20:45 +0300 | [diff] [blame] | 741 | mGLPositionInitialized = false; |
zmo@google.com | a3b4ab4 | 2011-09-16 00:53:26 +0000 | [diff] [blame] | 742 | |
Olli Etuaho | 09b04a2 | 2016-12-15 13:30:26 +0000 | [diff] [blame] | 743 | mNumViews = -1; |
| 744 | |
Olli Etuaho | dfa75e8 | 2017-01-23 09:43:06 -0800 | [diff] [blame] | 745 | builtInFunctionEmulator.cleanup(); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 746 | |
| 747 | nameMap.clear(); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 748 | |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 749 | mSourcePath = nullptr; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 752 | bool TCompiler::initCallDag(TIntermNode *root) |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 753 | { |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 754 | mCallDag.clear(); |
| 755 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 756 | switch (mCallDag.init(root, &mDiagnostics)) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 757 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 758 | case CallDAG::INITDAG_SUCCESS: |
| 759 | return true; |
| 760 | case CallDAG::INITDAG_RECURSION: |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 761 | case CallDAG::INITDAG_UNDEFINED: |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 762 | // Error message has already been written out. |
| 763 | ASSERT(mDiagnostics.numErrors() > 0); |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 764 | return false; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | UNREACHABLE(); |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | bool TCompiler::checkCallDepth() |
| 772 | { |
| 773 | std::vector<int> depths(mCallDag.size()); |
| 774 | |
| 775 | for (size_t i = 0; i < mCallDag.size(); i++) |
| 776 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 777 | int depth = 0; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 778 | auto &record = mCallDag.getRecordFromIndex(i); |
| 779 | |
| 780 | for (auto &calleeIndex : record.callees) |
| 781 | { |
| 782 | depth = std::max(depth, depths[calleeIndex] + 1); |
| 783 | } |
| 784 | |
| 785 | depths[i] = depth; |
| 786 | |
| 787 | if (depth >= maxCallStackDepth) |
| 788 | { |
| 789 | // Trace back the function chain to have a meaningful info log. |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 790 | std::stringstream errorStream; |
| 791 | errorStream << "Call stack too deep (larger than " << maxCallStackDepth |
| 792 | << ") with the following call chain: " << record.name; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 793 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 794 | int currentFunction = static_cast<int>(i); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 795 | int currentDepth = depth; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 796 | |
| 797 | while (currentFunction != -1) |
| 798 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 799 | errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name; |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 800 | |
| 801 | int nextFunction = -1; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 802 | for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 803 | { |
| 804 | if (depths[calleeIndex] == currentDepth - 1) |
| 805 | { |
| 806 | currentDepth--; |
| 807 | nextFunction = calleeIndex; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | currentFunction = nextFunction; |
| 812 | } |
| 813 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 814 | std::string errorStr = errorStream.str(); |
| 815 | mDiagnostics.globalError(errorStr.c_str()); |
| 816 | |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 817 | return false; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | return true; |
| 822 | } |
| 823 | |
| 824 | bool TCompiler::tagUsedFunctions() |
| 825 | { |
| 826 | // 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] | 827 | for (size_t i = mCallDag.size(); i-- > 0;) |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 828 | { |
Olli Etuaho | ec9232b | 2017-03-27 17:01:37 +0300 | [diff] [blame] | 829 | if (mCallDag.getRecordFromIndex(i).name == "main") |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 830 | { |
| 831 | internalTagUsedFunction(i); |
| 832 | return true; |
| 833 | } |
| 834 | } |
| 835 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 836 | mDiagnostics.globalError("Missing main()"); |
Corentin Wallez | 71d147f | 2015-02-11 11:15:24 -0800 | [diff] [blame] | 837 | return false; |
| 838 | } |
| 839 | |
| 840 | void TCompiler::internalTagUsedFunction(size_t index) |
| 841 | { |
| 842 | if (functionMetadata[index].used) |
| 843 | { |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | functionMetadata[index].used = true; |
| 848 | |
| 849 | for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees) |
| 850 | { |
| 851 | internalTagUsedFunction(calleeIndex); |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 855 | // A predicate for the stl that returns if a top-level node is unused |
| 856 | class TCompiler::UnusedPredicate |
| 857 | { |
| 858 | public: |
| 859 | UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas) |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 860 | : mCallDag(callDag), mMetadatas(metadatas) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 861 | { |
| 862 | } |
| 863 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 864 | bool operator()(TIntermNode *node) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 865 | { |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 866 | const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode(); |
| 867 | const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition(); |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 868 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 869 | const TFunctionSymbolInfo *functionInfo = nullptr; |
| 870 | |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 871 | if (asFunctionDefinition) |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 872 | { |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 873 | functionInfo = asFunctionDefinition->getFunctionSymbolInfo(); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 874 | } |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 875 | else if (asFunctionPrototype) |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 876 | { |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 877 | functionInfo = asFunctionPrototype->getFunctionSymbolInfo(); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 878 | } |
| 879 | if (functionInfo == nullptr) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 880 | { |
| 881 | return false; |
| 882 | } |
| 883 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 884 | size_t callDagIndex = mCallDag->findIndex(functionInfo); |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 885 | if (callDagIndex == CallDAG::InvalidIndex) |
| 886 | { |
| 887 | // This happens only for unimplemented prototypes which are thus unused |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 888 | ASSERT(asFunctionPrototype); |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 889 | return true; |
| 890 | } |
| 891 | |
| 892 | ASSERT(callDagIndex < mMetadatas->size()); |
| 893 | return !(*mMetadatas)[callDagIndex].used; |
| 894 | } |
| 895 | |
| 896 | private: |
| 897 | const CallDAG *mCallDag; |
| 898 | const std::vector<FunctionMetadata> *mMetadatas; |
| 899 | }; |
| 900 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 901 | bool TCompiler::pruneUnusedFunctions(TIntermBlock *root) |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 902 | { |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 903 | UnusedPredicate isUnused(&mCallDag, &functionMetadata); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 904 | TIntermSequence *sequence = root->getSequence(); |
Corentin Wallez | b081e78 | 2015-07-20 05:40:04 -0700 | [diff] [blame] | 905 | |
| 906 | if (!sequence->empty()) |
| 907 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 908 | sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), |
| 909 | sequence->end()); |
Corentin Wallez | b081e78 | 2015-07-20 05:40:04 -0700 | [diff] [blame] | 910 | } |
Corentin Wallez | a094a8a | 2015-04-07 11:53:06 -0700 | [diff] [blame] | 911 | |
| 912 | return true; |
| 913 | } |
| 914 | |
Olli Etuaho | b427920 | 2017-05-18 15:08:24 +0300 | [diff] [blame] | 915 | bool TCompiler::limitExpressionComplexity(TIntermBlock *root) |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 916 | { |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 917 | if (!IsASTDepthBelowLimit(root, maxExpressionComplexity)) |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 918 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 919 | mDiagnostics.globalError("Expression too complex."); |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 920 | return false; |
| 921 | } |
| 922 | |
Olli Etuaho | b427920 | 2017-05-18 15:08:24 +0300 | [diff] [blame] | 923 | if (!ValidateMaxParameters(root, maxFunctionParameters)) |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 924 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 925 | mDiagnostics.globalError("Function has too many parameters."); |
Olli Etuaho | 19d1dc9 | 2016-03-08 17:18:46 +0200 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 929 | return true; |
| 930 | } |
| 931 | |
Corentin Wallez | 1df1602 | 2016-10-27 08:16:56 -0400 | [diff] [blame] | 932 | bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions) |
| 933 | { |
| 934 | return (compileOptions & SH_VARIABLES) != 0; |
| 935 | } |
| 936 | |
| 937 | bool TCompiler::wereVariablesCollected() const |
| 938 | { |
| 939 | return variablesCollected; |
| 940 | } |
| 941 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 942 | void TCompiler::initializeGLPosition(TIntermBlock *root) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 943 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 944 | InitVariableList list; |
| 945 | sh::ShaderVariable var(GL_FLOAT_VEC4, 0); |
| 946 | var.name = "gl_Position"; |
| 947 | list.push_back(var); |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 948 | InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 951 | void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root) |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 952 | { |
| 953 | sh::InterfaceBlockList list; |
| 954 | |
| 955 | for (auto block : interfaceBlocks) |
| 956 | { |
| 957 | if (!block.staticUse && |
| 958 | (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED)) |
| 959 | { |
| 960 | list.push_back(block); |
| 961 | } |
| 962 | } |
| 963 | |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 964 | sh::UseInterfaceBlockFields(root, list, symbolTable); |
Qin Jiajia | 7835b52 | 2016-10-08 11:20:17 +0800 | [diff] [blame] | 965 | } |
| 966 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame] | 967 | void TCompiler::initializeOutputVariables(TIntermBlock *root) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 968 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 969 | InitVariableList list; |
| 970 | if (shaderType == GL_VERTEX_SHADER) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 971 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 972 | for (auto var : varyings) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 973 | { |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 974 | list.push_back(var); |
Olli Etuaho | b12040c | 2017-06-27 14:20:45 +0300 | [diff] [blame] | 975 | if (var.name == "gl_Position") |
| 976 | { |
| 977 | ASSERT(!mGLPositionInitialized); |
| 978 | mGLPositionInitialized = true; |
| 979 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 980 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 981 | } |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 982 | else |
| 983 | { |
| 984 | ASSERT(shaderType == GL_FRAGMENT_SHADER); |
| 985 | for (auto var : outputVariables) |
| 986 | { |
| 987 | list.push_back(var); |
| 988 | } |
| 989 | } |
Olli Etuaho | daaff1c | 2017-07-05 18:03:26 +0300 | [diff] [blame] | 990 | InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 993 | const TExtensionBehavior &TCompiler::getExtensionBehavior() const |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 994 | { |
| 995 | return extensionBehavior; |
| 996 | } |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 997 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 998 | const char *TCompiler::getSourcePath() const |
| 999 | { |
| 1000 | return mSourcePath; |
| 1001 | } |
| 1002 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1003 | const ShBuiltInResources &TCompiler::getResources() const |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 1004 | { |
| 1005 | return compileResources; |
| 1006 | } |
| 1007 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1008 | const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 1009 | { |
| 1010 | return arrayBoundsClamper; |
| 1011 | } |
| 1012 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 1013 | ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const |
| 1014 | { |
| 1015 | return clampingStrategy; |
| 1016 | } |
| 1017 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1018 | const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 1019 | { |
| 1020 | return builtInFunctionEmulator; |
| 1021 | } |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 1022 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 1023 | void TCompiler::writePragma(ShCompileOptions compileOptions) |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 1024 | { |
Kenneth Russell | bccc65d | 2016-07-19 16:48:43 -0700 | [diff] [blame] | 1025 | if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL)) |
| 1026 | { |
| 1027 | TInfoSinkBase &sink = infoSink.obj; |
| 1028 | if (mPragma.stdgl.invariantAll) |
| 1029 | sink << "#pragma STDGL invariant(all)\n"; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | bool TCompiler::isVaryingDefined(const char *varyingName) |
| 1034 | { |
| 1035 | ASSERT(variablesCollected); |
| 1036 | for (size_t ii = 0; ii < varyings.size(); ++ii) |
| 1037 | { |
| 1038 | if (varyings[ii].name == varyingName) |
| 1039 | { |
| 1040 | return true; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | return false; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 1045 | } |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 1046 | |
| 1047 | } // namespace sh |