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