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