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