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