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