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