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