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