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