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