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