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