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