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