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