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