alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 1 | // |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 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 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/BuiltInFunctionEmulator.h" |
| 8 | #include "compiler/translator/DetectCallDepth.h" |
| 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" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 13 | #include "compiler/translator/MapLongVariableNames.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 14 | #include "compiler/translator/ParseContext.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 15 | #include "compiler/translator/RenameFunction.h" |
| 16 | #include "compiler/translator/ShHandle.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" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 26 | |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 27 | bool isWebGLBasedSpec(ShShaderSpec spec) |
| 28 | { |
| 29 | return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC; |
| 30 | } |
| 31 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 32 | namespace { |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 33 | class TScopedPoolAllocator |
| 34 | { |
| 35 | public: |
| 36 | TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator) |
| 37 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 38 | mAllocator->push(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 39 | SetGlobalPoolAllocator(mAllocator); |
| 40 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 41 | ~TScopedPoolAllocator() |
| 42 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 43 | SetGlobalPoolAllocator(NULL); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 44 | mAllocator->pop(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 47 | private: |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 48 | TPoolAllocator* mAllocator; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 51 | class TScopedSymbolTableLevel |
| 52 | { |
| 53 | public: |
| 54 | TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table) |
| 55 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 56 | ASSERT(mTable->atBuiltInLevel()); |
| 57 | mTable->push(); |
| 58 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 59 | ~TScopedSymbolTableLevel() |
| 60 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 61 | while (!mTable->atBuiltInLevel()) |
| 62 | mTable->pop(); |
| 63 | } |
| 64 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 65 | private: |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 66 | TSymbolTable* mTable; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 67 | }; |
| 68 | } // namespace |
| 69 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 70 | TShHandleBase::TShHandleBase() |
| 71 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 72 | allocator.push(); |
| 73 | SetGlobalPoolAllocator(&allocator); |
| 74 | } |
| 75 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 76 | TShHandleBase::~TShHandleBase() |
| 77 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 78 | SetGlobalPoolAllocator(NULL); |
| 79 | allocator.popAll(); |
| 80 | } |
| 81 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 82 | TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec) |
| 83 | : shaderType(type), |
zmo@google.com | f420c42 | 2011-09-12 18:27:59 +0000 | [diff] [blame] | 84 | shaderSpec(spec), |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 85 | maxUniformVectors(0), |
| 86 | maxExpressionComplexity(0), |
| 87 | maxCallStackDepth(0), |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 88 | fragmentPrecisionHigh(false), |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 89 | clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC), |
zmo@google.com | 9996b8e | 2012-01-19 01:43:55 +0000 | [diff] [blame] | 90 | builtInFunctionEmulator(type) |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 91 | { |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 92 | longNameMap = LongNameMap::GetInstance(); |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TCompiler::~TCompiler() |
| 96 | { |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 97 | ASSERT(longNameMap); |
| 98 | longNameMap->Release(); |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool TCompiler::Init(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 102 | { |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 103 | shaderVersion = 100; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 104 | maxUniformVectors = (shaderType == SH_VERTEX_SHADER) ? |
| 105 | resources.MaxVertexUniformVectors : |
| 106 | resources.MaxFragmentUniformVectors; |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 107 | maxExpressionComplexity = resources.MaxExpressionComplexity; |
| 108 | maxCallStackDepth = resources.MaxCallStackDepth; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 109 | |
| 110 | SetGlobalPoolAllocator(&allocator); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 111 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 112 | // Generate built-in symbol table. |
| 113 | if (!InitBuiltInSymbolTable(resources)) |
| 114 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 115 | InitExtensionBehavior(resources, extensionBehavior); |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 116 | fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 117 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 118 | arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy); |
| 119 | clampingStrategy = resources.ArrayIndexClampingStrategy; |
| 120 | |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 121 | hashFunction = resources.HashFunction; |
| 122 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool TCompiler::compile(const char* const shaderStrings[], |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 127 | size_t numStrings, |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 128 | int compileOptions) |
| 129 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 130 | TScopedPoolAllocator scopedAlloc(&allocator); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 131 | clearResults(); |
| 132 | |
| 133 | if (numStrings == 0) |
| 134 | return true; |
| 135 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 136 | // If compiling for WebGL, validate loop and indexing as well. |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 137 | if (isWebGLBasedSpec(shaderSpec)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 138 | compileOptions |= SH_VALIDATE_LOOP_INDEXING; |
alokp@chromium.org | 1f29954 | 2010-11-12 15:50:23 +0000 | [diff] [blame] | 139 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 140 | // First string is path of source file if flag is set. The actual source follows. |
| 141 | const char* sourcePath = NULL; |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 142 | size_t firstSource = 0; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 143 | if (compileOptions & SH_SOURCE_PATH) |
| 144 | { |
| 145 | sourcePath = shaderStrings[0]; |
| 146 | ++firstSource; |
| 147 | } |
| 148 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 149 | TIntermediate intermediate(infoSink); |
| 150 | TParseContext parseContext(symbolTable, extensionBehavior, intermediate, |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 151 | shaderType, shaderSpec, compileOptions, true, |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 152 | sourcePath, infoSink); |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 153 | parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh; |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 154 | SetGlobalParseContext(&parseContext); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 155 | |
| 156 | // We preserve symbols at the built-in level from compile-to-compile. |
| 157 | // Start pushing the user-defined symbols at global level. |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 158 | TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 159 | |
| 160 | // Parse shader. |
| 161 | bool success = |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 162 | (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) && |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 163 | (parseContext.treeRoot != NULL); |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 164 | |
shannon.woods%transgaming.com@gtempaccount.com | 5524db0 | 2013-04-13 03:38:16 +0000 | [diff] [blame] | 165 | shaderVersion = parseContext.getShaderVersion(); |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 166 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 167 | if (success) |
| 168 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 169 | TIntermNode* root = parseContext.treeRoot; |
| 170 | success = intermediate.postProcess(root); |
| 171 | |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 172 | if (success) |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 173 | success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0); |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 174 | |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 175 | if (success && shaderVersion == 300 && shaderType == SH_FRAGMENT_SHADER) |
| 176 | success = validateOutputs(root); |
| 177 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 178 | if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING)) |
| 179 | success = validateLimitations(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 180 | |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 181 | if (success && (compileOptions & SH_TIMING_RESTRICTIONS)) |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 182 | success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 183 | |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 184 | if (success && shaderSpec == SH_CSS_SHADERS_SPEC) |
| 185 | rewriteCSSShader(root); |
| 186 | |
zmo@google.com | 0c6bb7a | 2011-08-17 19:39:58 +0000 | [diff] [blame] | 187 | // Unroll for-loop markup needs to happen after validateLimitations pass. |
| 188 | if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX)) |
| 189 | ForLoopUnroll::MarkForLoopsWithIntegerIndicesForUnrolling(root); |
| 190 | |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 191 | // Built-in function emulation needs to happen after validateLimitations pass. |
| 192 | if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS)) |
| 193 | builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root); |
| 194 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 195 | // Clamping uniform array bounds needs to happen after validateLimitations pass. |
| 196 | if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)) |
| 197 | arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root); |
| 198 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 199 | // Disallow expressions deemed too complex. |
| 200 | if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY)) |
| 201 | success = limitExpressionComplexity(root); |
| 202 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 203 | // Call mapLongVariableNames() before collectAttribsUniforms() so in |
| 204 | // collectAttribsUniforms() we already have the mapped symbol names and |
| 205 | // we could composite mapped and original variable names. |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 206 | // Also, if we hash all the names, then no need to do this for long names. |
| 207 | if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES) && hashFunction == NULL) |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 208 | mapLongVariableNames(root); |
| 209 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 210 | if (success && shaderType == SH_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION)) |
| 211 | initializeGLPosition(root); |
Zhenyao Mo | ac44cd2 | 2013-09-23 14:57:09 -0400 | [diff] [blame] | 212 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 213 | if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)) |
| 214 | { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 215 | UnfoldShortCircuitAST unfoldShortCircuit; |
| 216 | root->traverse(&unfoldShortCircuit); |
| 217 | unfoldShortCircuit.updateTree(); |
| 218 | } |
| 219 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 220 | if (success && (compileOptions & SH_VARIABLES)) |
| 221 | { |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 222 | collectVariables(root); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 223 | if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) |
| 224 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 225 | success = enforcePackingRestrictions(); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 226 | if (!success) |
| 227 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 228 | infoSink.info.prefix(EPrefixError); |
| 229 | infoSink.info << "too many uniforms"; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 232 | if (success && shaderType == SH_VERTEX_SHADER && |
| 233 | (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE)) |
| 234 | initializeVaryingsWithoutStaticUse(root); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 235 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 236 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 237 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 238 | intermediate.outputTree(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 239 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 240 | if (success && (compileOptions & SH_OBJECT_CODE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 241 | translate(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Cleanup memory. |
| 245 | intermediate.remove(parseContext.treeRoot); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 246 | |
| 247 | return success; |
| 248 | } |
| 249 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 250 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 251 | { |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 252 | compileResources = resources; |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 253 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 254 | assert(symbolTable.isEmpty()); |
| 255 | symbolTable.push(); // COMMON_BUILTINS |
| 256 | symbolTable.push(); // ESSL1_BUILTINS |
| 257 | symbolTable.push(); // ESSL3_BUILTINS |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 258 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 259 | TPublicType integer; |
| 260 | integer.type = EbtInt; |
| 261 | integer.primarySize = 1; |
| 262 | integer.secondarySize = 1; |
| 263 | integer.array = false; |
| 264 | |
| 265 | TPublicType floatingPoint; |
| 266 | floatingPoint.type = EbtFloat; |
| 267 | floatingPoint.primarySize = 1; |
| 268 | floatingPoint.secondarySize = 1; |
| 269 | floatingPoint.array = false; |
| 270 | |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 271 | TPublicType sampler; |
| 272 | sampler.primarySize = 1; |
| 273 | sampler.secondarySize = 1; |
| 274 | sampler.array = false; |
| 275 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 276 | switch(shaderType) |
| 277 | { |
| 278 | case SH_FRAGMENT_SHADER: |
| 279 | symbolTable.setDefaultPrecision(integer, EbpMedium); |
| 280 | break; |
| 281 | case SH_VERTEX_SHADER: |
| 282 | symbolTable.setDefaultPrecision(integer, EbpHigh); |
| 283 | symbolTable.setDefaultPrecision(floatingPoint, EbpHigh); |
| 284 | break; |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 285 | default: |
| 286 | assert(false && "Language not supported"); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 287 | } |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 288 | // We set defaults for all the sampler types, even those that are |
| 289 | // only available if an extension exists. |
| 290 | for (int samplerType = EbtGuardSamplerBegin + 1; |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 291 | samplerType < EbtGuardSamplerEnd; ++samplerType) |
| 292 | { |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 293 | sampler.type = static_cast<TBasicType>(samplerType); |
| 294 | symbolTable.setDefaultPrecision(sampler, EbpLow); |
| 295 | } |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 296 | |
Jamie Madill | 1b45214 | 2013-07-12 14:51:11 -0400 | [diff] [blame] | 297 | InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 298 | |
| 299 | IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable); |
| 300 | |
| 301 | return true; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void TCompiler::clearResults() |
| 305 | { |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 306 | arrayBoundsClamper.Cleanup(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 307 | infoSink.info.erase(); |
| 308 | infoSink.obj.erase(); |
| 309 | infoSink.debug.erase(); |
| 310 | |
| 311 | attribs.clear(); |
| 312 | uniforms.clear(); |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 313 | varyings.clear(); |
zmo@google.com | a3b4ab4 | 2011-09-16 00:53:26 +0000 | [diff] [blame] | 314 | |
| 315 | builtInFunctionEmulator.Cleanup(); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 316 | |
| 317 | nameMap.clear(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 320 | bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth) |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 321 | { |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 322 | DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth); |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 323 | root->traverse(&detect); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 324 | switch (detect.detectCallDepth()) |
| 325 | { |
| 326 | case DetectCallDepth::kErrorNone: |
| 327 | return true; |
| 328 | case DetectCallDepth::kErrorMissingMain: |
| 329 | infoSink.info.prefix(EPrefixError); |
| 330 | infoSink.info << "Missing main()"; |
| 331 | return false; |
| 332 | case DetectCallDepth::kErrorRecursion: |
| 333 | infoSink.info.prefix(EPrefixError); |
| 334 | infoSink.info << "Function recursion detected"; |
| 335 | return false; |
| 336 | case DetectCallDepth::kErrorMaxDepthExceeded: |
| 337 | infoSink.info.prefix(EPrefixError); |
| 338 | infoSink.info << "Function call stack too deep"; |
| 339 | return false; |
| 340 | default: |
| 341 | UNREACHABLE(); |
| 342 | return false; |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 346 | bool TCompiler::validateOutputs(TIntermNode* root) |
| 347 | { |
| 348 | ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers); |
| 349 | root->traverse(&validateOutputs); |
| 350 | return (validateOutputs.numErrors() == 0); |
| 351 | } |
| 352 | |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 353 | void TCompiler::rewriteCSSShader(TIntermNode* root) |
| 354 | { |
| 355 | RenameFunction renamer("main(", "css_main("); |
| 356 | root->traverse(&renamer); |
| 357 | } |
| 358 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 359 | bool TCompiler::validateLimitations(TIntermNode* root) |
| 360 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 361 | ValidateLimitations validate(shaderType, infoSink.info); |
| 362 | root->traverse(&validate); |
| 363 | return validate.numErrors() == 0; |
| 364 | } |
| 365 | |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 366 | bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph) |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 367 | { |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 368 | if (shaderSpec != SH_WEBGL_SPEC) |
| 369 | { |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 370 | infoSink.info << "Timing restrictions must be enforced under the WebGL spec."; |
| 371 | return false; |
| 372 | } |
| 373 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 374 | if (shaderType == SH_FRAGMENT_SHADER) |
| 375 | { |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 376 | TDependencyGraph graph(root); |
| 377 | |
| 378 | // Output any errors first. |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 379 | bool success = enforceFragmentShaderTimingRestrictions(graph); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 380 | |
| 381 | // Then, output the dependency graph. |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 382 | if (outputGraph) |
| 383 | { |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 384 | TDependencyGraphOutput output(infoSink.info); |
| 385 | output.outputAllSpanningTrees(graph); |
| 386 | } |
| 387 | |
| 388 | return success; |
| 389 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 390 | else |
| 391 | { |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 392 | return enforceVertexShaderTimingRestrictions(root); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 396 | bool TCompiler::limitExpressionComplexity(TIntermNode* root) |
| 397 | { |
| 398 | TIntermTraverser traverser; |
| 399 | root->traverse(&traverser); |
| 400 | TDependencyGraph graph(root); |
| 401 | |
| 402 | for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls(); |
| 403 | iter != graph.endUserDefinedFunctionCalls(); |
| 404 | ++iter) |
| 405 | { |
| 406 | TGraphFunctionCall* samplerSymbol = *iter; |
| 407 | TDependencyGraphTraverser graphTraverser; |
| 408 | samplerSymbol->traverse(&graphTraverser); |
| 409 | } |
| 410 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 411 | if (traverser.getMaxDepth() > maxExpressionComplexity) |
| 412 | { |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 413 | infoSink.info << "Expression too complex."; |
| 414 | return false; |
| 415 | } |
| 416 | return true; |
| 417 | } |
| 418 | |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 419 | bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph) |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 420 | { |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 421 | RestrictFragmentShaderTiming restrictor(infoSink.info); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 422 | restrictor.enforceRestrictions(graph); |
| 423 | return restrictor.numErrors() == 0; |
| 424 | } |
| 425 | |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 426 | bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root) |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 427 | { |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 428 | RestrictVertexShaderTiming restrictor(infoSink.info); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 429 | restrictor.enforceRestrictions(root); |
| 430 | return restrictor.numErrors() == 0; |
| 431 | } |
| 432 | |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 433 | void TCompiler::collectVariables(TIntermNode* root) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 434 | { |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 435 | CollectVariables collect(attribs, uniforms, varyings, hashFunction); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 436 | root->traverse(&collect); |
| 437 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 438 | |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 439 | bool TCompiler::enforcePackingRestrictions() |
| 440 | { |
| 441 | VariablePacker packer; |
| 442 | return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms); |
| 443 | } |
| 444 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 445 | void TCompiler::initializeGLPosition(TIntermNode* root) |
| 446 | { |
| 447 | InitializeVariables::InitVariableInfoList variables; |
| 448 | InitializeVariables::InitVariableInfo var( |
| 449 | "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4)); |
| 450 | variables.push_back(var); |
| 451 | InitializeVariables initializer(variables); |
| 452 | root->traverse(&initializer); |
| 453 | } |
| 454 | |
| 455 | void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root) |
| 456 | { |
| 457 | InitializeVariables::InitVariableInfoList variables; |
| 458 | for (size_t ii = 0; ii < varyings.size(); ++ii) |
| 459 | { |
| 460 | const TVariableInfo& varying = varyings[ii]; |
| 461 | if (varying.staticUse) |
| 462 | continue; |
| 463 | unsigned char size = 0; |
| 464 | bool matrix = false; |
| 465 | switch (varying.type) |
| 466 | { |
| 467 | case SH_FLOAT: |
| 468 | size = 1; |
| 469 | break; |
| 470 | case SH_FLOAT_VEC2: |
| 471 | size = 2; |
| 472 | break; |
| 473 | case SH_FLOAT_VEC3: |
| 474 | size = 3; |
| 475 | break; |
| 476 | case SH_FLOAT_VEC4: |
| 477 | size = 4; |
| 478 | break; |
| 479 | case SH_FLOAT_MAT2: |
| 480 | size = 2; |
| 481 | matrix = true; |
| 482 | break; |
| 483 | case SH_FLOAT_MAT3: |
| 484 | size = 3; |
| 485 | matrix = true; |
| 486 | break; |
| 487 | case SH_FLOAT_MAT4: |
| 488 | size = 4; |
| 489 | matrix = true; |
| 490 | break; |
| 491 | default: |
| 492 | ASSERT(false); |
| 493 | } |
| 494 | TType type(EbtFloat, EbpUndefined, EvqVaryingOut, size, matrix, varying.isArray); |
| 495 | TString name = varying.name.c_str(); |
| 496 | if (varying.isArray) |
| 497 | { |
| 498 | type.setArraySize(varying.size); |
| 499 | name = name.substr(0, name.find_first_of('[')); |
| 500 | } |
| 501 | |
| 502 | InitializeVariables::InitVariableInfo var(name, type); |
| 503 | variables.push_back(var); |
| 504 | } |
| 505 | InitializeVariables initializer(variables); |
| 506 | root->traverse(&initializer); |
| 507 | } |
| 508 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 509 | void TCompiler::mapLongVariableNames(TIntermNode* root) |
| 510 | { |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 511 | ASSERT(longNameMap); |
| 512 | MapLongVariableNames map(longNameMap); |
zmo@google.com | 9996b8e | 2012-01-19 01:43:55 +0000 | [diff] [blame] | 513 | root->traverse(&map); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | int TCompiler::getMappedNameMaxLength() const |
| 517 | { |
kbr@chromium.org | 2215211 | 2011-10-26 01:18:28 +0000 | [diff] [blame] | 518 | return MAX_SHORTENED_IDENTIFIER_SIZE + 1; |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 519 | } |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 520 | |
| 521 | const TExtensionBehavior& TCompiler::getExtensionBehavior() const |
| 522 | { |
| 523 | return extensionBehavior; |
| 524 | } |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 525 | |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 526 | const ShBuiltInResources& TCompiler::getResources() const |
| 527 | { |
| 528 | return compileResources; |
| 529 | } |
| 530 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 531 | const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const |
| 532 | { |
| 533 | return arrayBoundsClamper; |
| 534 | } |
| 535 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 536 | ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const |
| 537 | { |
| 538 | return clampingStrategy; |
| 539 | } |
| 540 | |
| 541 | const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const |
| 542 | { |
| 543 | return builtInFunctionEmulator; |
| 544 | } |