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