alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 1 | // |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/BuiltInFunctionEmulator.h" |
| 8 | #include "compiler/translator/DetectCallDepth.h" |
| 9 | #include "compiler/translator/ForLoopUnroll.h" |
| 10 | #include "compiler/translator/Initialize.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 11 | #include "compiler/translator/InitializeParseContext.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 12 | #include "compiler/translator/InitializeVariables.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 13 | #include "compiler/translator/ParseContext.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 14 | #include "compiler/translator/RenameFunction.h" |
| 15 | #include "compiler/translator/ShHandle.h" |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 16 | #include "compiler/translator/UnfoldShortCircuitAST.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 17 | #include "compiler/translator/ValidateLimitations.h" |
| 18 | #include "compiler/translator/ValidateOutputs.h" |
| 19 | #include "compiler/translator/VariablePacker.h" |
| 20 | #include "compiler/translator/depgraph/DependencyGraph.h" |
| 21 | #include "compiler/translator/depgraph/DependencyGraphOutput.h" |
| 22 | #include "compiler/translator/timing/RestrictFragmentShaderTiming.h" |
| 23 | #include "compiler/translator/timing/RestrictVertexShaderTiming.h" |
shannon.woods@transgaming.com | da1ed36 | 2013-01-25 21:54:57 +0000 | [diff] [blame] | 24 | #include "third_party/compiler/ArrayBoundsClamper.h" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 25 | |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 26 | bool IsWebGLBasedSpec(ShShaderSpec spec) |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 27 | { |
| 28 | return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC; |
| 29 | } |
| 30 | |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 31 | size_t GetGlobalMaxTokenSize() |
| 32 | { |
| 33 | TParseContext *parseContext = GetGlobalParseContext(); |
| 34 | // WebGL defines a max token legnth of 256, while ES2 leaves max token |
| 35 | // size undefined. ES3 defines a max size of 1024 characters. |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 36 | if (IsWebGLBasedSpec(parseContext->shaderSpec)) |
Jamie Madill | 88f6e94 | 2014-02-19 10:27:53 -0500 | [diff] [blame] | 37 | { |
| 38 | return 256; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | return 1024; |
| 43 | } |
| 44 | } |
| 45 | |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 46 | namespace { |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 47 | class TScopedPoolAllocator |
| 48 | { |
| 49 | public: |
| 50 | TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator) |
| 51 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 52 | mAllocator->push(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 53 | SetGlobalPoolAllocator(mAllocator); |
| 54 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 55 | ~TScopedPoolAllocator() |
| 56 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 57 | SetGlobalPoolAllocator(NULL); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 58 | mAllocator->pop(); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 61 | private: |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 62 | TPoolAllocator* mAllocator; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 63 | }; |
| 64 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 65 | class TScopedSymbolTableLevel |
| 66 | { |
| 67 | public: |
| 68 | TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table) |
| 69 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 70 | ASSERT(mTable->atBuiltInLevel()); |
| 71 | mTable->push(); |
| 72 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 73 | ~TScopedSymbolTableLevel() |
| 74 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 75 | while (!mTable->atBuiltInLevel()) |
| 76 | mTable->pop(); |
| 77 | } |
| 78 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 79 | private: |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 80 | TSymbolTable* mTable; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 81 | }; |
| 82 | } // namespace |
| 83 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 84 | TShHandleBase::TShHandleBase() |
| 85 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 86 | allocator.push(); |
| 87 | SetGlobalPoolAllocator(&allocator); |
| 88 | } |
| 89 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 90 | TShHandleBase::~TShHandleBase() |
| 91 | { |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 92 | SetGlobalPoolAllocator(NULL); |
| 93 | allocator.popAll(); |
| 94 | } |
| 95 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 96 | TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec) |
| 97 | : shaderType(type), |
zmo@google.com | f420c42 | 2011-09-12 18:27:59 +0000 | [diff] [blame] | 98 | shaderSpec(spec), |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 99 | maxUniformVectors(0), |
| 100 | maxExpressionComplexity(0), |
| 101 | maxCallStackDepth(0), |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 102 | fragmentPrecisionHigh(false), |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 103 | clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC), |
zmo@google.com | 9996b8e | 2012-01-19 01:43:55 +0000 | [diff] [blame] | 104 | builtInFunctionEmulator(type) |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 105 | { |
| 106 | } |
| 107 | |
| 108 | TCompiler::~TCompiler() |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | bool TCompiler::Init(const ShBuiltInResources& resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 113 | { |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 114 | shaderVersion = 100; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 115 | maxUniformVectors = (shaderType == SH_VERTEX_SHADER) ? |
| 116 | resources.MaxVertexUniformVectors : |
| 117 | resources.MaxFragmentUniformVectors; |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 118 | maxExpressionComplexity = resources.MaxExpressionComplexity; |
| 119 | maxCallStackDepth = resources.MaxCallStackDepth; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 120 | |
| 121 | SetGlobalPoolAllocator(&allocator); |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 122 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 123 | // Generate built-in symbol table. |
| 124 | if (!InitBuiltInSymbolTable(resources)) |
| 125 | return false; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 126 | InitExtensionBehavior(resources, extensionBehavior); |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 127 | fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1; |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 128 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 129 | arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy); |
| 130 | clampingStrategy = resources.ArrayIndexClampingStrategy; |
| 131 | |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 132 | hashFunction = resources.HashFunction; |
| 133 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool TCompiler::compile(const char* const shaderStrings[], |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 138 | size_t numStrings, |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 139 | int compileOptions) |
| 140 | { |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 141 | TScopedPoolAllocator scopedAlloc(&allocator); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 142 | clearResults(); |
| 143 | |
| 144 | if (numStrings == 0) |
| 145 | return true; |
| 146 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 147 | // If compiling for WebGL, validate loop and indexing as well. |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 148 | if (IsWebGLBasedSpec(shaderSpec)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 149 | compileOptions |= SH_VALIDATE_LOOP_INDEXING; |
alokp@chromium.org | 1f29954 | 2010-11-12 15:50:23 +0000 | [diff] [blame] | 150 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 151 | // First string is path of source file if flag is set. The actual source follows. |
| 152 | const char* sourcePath = NULL; |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 153 | size_t firstSource = 0; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 154 | if (compileOptions & SH_SOURCE_PATH) |
| 155 | { |
| 156 | sourcePath = shaderStrings[0]; |
| 157 | ++firstSource; |
| 158 | } |
| 159 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 160 | TIntermediate intermediate(infoSink); |
| 161 | TParseContext parseContext(symbolTable, extensionBehavior, intermediate, |
zmo@google.com | dc4b4f8 | 2011-06-17 00:42:53 +0000 | [diff] [blame] | 162 | shaderType, shaderSpec, compileOptions, true, |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 163 | sourcePath, infoSink); |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 164 | parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh; |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 165 | SetGlobalParseContext(&parseContext); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 166 | |
| 167 | // We preserve symbols at the built-in level from compile-to-compile. |
| 168 | // Start pushing the user-defined symbols at global level. |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 169 | TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 170 | |
| 171 | // Parse shader. |
| 172 | bool success = |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 173 | (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) && |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 174 | (parseContext.treeRoot != NULL); |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 175 | |
shannon.woods%transgaming.com@gtempaccount.com | 5524db0 | 2013-04-13 03:38:16 +0000 | [diff] [blame] | 176 | shaderVersion = parseContext.getShaderVersion(); |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 177 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 178 | if (success) |
| 179 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 180 | TIntermNode* root = parseContext.treeRoot; |
| 181 | success = intermediate.postProcess(root); |
| 182 | |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 183 | // Disallow expressions deemed too complex. |
| 184 | if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY)) |
| 185 | success = limitExpressionComplexity(root); |
| 186 | |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 187 | if (success) |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 188 | success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0); |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 189 | |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 190 | if (success && shaderVersion == 300 && shaderType == SH_FRAGMENT_SHADER) |
| 191 | success = validateOutputs(root); |
| 192 | |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 193 | if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING)) |
| 194 | success = validateLimitations(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 195 | |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 196 | if (success && (compileOptions & SH_TIMING_RESTRICTIONS)) |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 197 | success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 198 | |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 199 | if (success && shaderSpec == SH_CSS_SHADERS_SPEC) |
| 200 | rewriteCSSShader(root); |
| 201 | |
zmo@google.com | 0c6bb7a | 2011-08-17 19:39:58 +0000 | [diff] [blame] | 202 | // Unroll for-loop markup needs to happen after validateLimitations pass. |
| 203 | if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX)) |
Zhenyao Mo | 3cdfcce | 2014-03-07 13:00:08 -0800 | [diff] [blame] | 204 | { |
| 205 | ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 206 | root->traverse(&marker); |
| 207 | } |
| 208 | if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX)) |
Zhenyao Mo | 3cdfcce | 2014-03-07 13:00:08 -0800 | [diff] [blame] | 209 | { |
| 210 | ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex); |
Zhenyao Mo | 550c600 | 2014-02-26 15:40:48 -0800 | [diff] [blame] | 211 | root->traverse(&marker); |
| 212 | if (marker.samplerArrayIndexIsFloatLoopIndex()) |
| 213 | { |
| 214 | infoSink.info.prefix(EPrefixError); |
| 215 | infoSink.info << "sampler array index is float loop index"; |
| 216 | success = false; |
| 217 | } |
| 218 | } |
zmo@google.com | 0c6bb7a | 2011-08-17 19:39:58 +0000 | [diff] [blame] | 219 | |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 220 | // Built-in function emulation needs to happen after validateLimitations pass. |
| 221 | if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS)) |
| 222 | builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root); |
| 223 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 224 | // Clamping uniform array bounds needs to happen after validateLimitations pass. |
| 225 | if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)) |
| 226 | arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root); |
| 227 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 228 | if (success && shaderType == SH_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION)) |
| 229 | initializeGLPosition(root); |
Zhenyao Mo | ac44cd2 | 2013-09-23 14:57:09 -0400 | [diff] [blame] | 230 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 231 | if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)) |
| 232 | { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 233 | UnfoldShortCircuitAST unfoldShortCircuit; |
| 234 | root->traverse(&unfoldShortCircuit); |
| 235 | unfoldShortCircuit.updateTree(); |
| 236 | } |
| 237 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 238 | if (success && (compileOptions & SH_VARIABLES)) |
| 239 | { |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 240 | collectVariables(root); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 241 | if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) |
| 242 | { |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 243 | success = enforcePackingRestrictions(); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 244 | if (!success) |
| 245 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 246 | infoSink.info.prefix(EPrefixError); |
| 247 | infoSink.info << "too many uniforms"; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 250 | if (success && shaderType == SH_VERTEX_SHADER && |
| 251 | (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE)) |
| 252 | initializeVaryingsWithoutStaticUse(root); |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 253 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 254 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 255 | if (success && (compileOptions & SH_INTERMEDIATE_TREE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 256 | intermediate.outputTree(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 257 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 258 | if (success && (compileOptions & SH_OBJECT_CODE)) |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 259 | translate(root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // Cleanup memory. |
| 263 | intermediate.remove(parseContext.treeRoot); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 264 | |
| 265 | return success; |
| 266 | } |
| 267 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 268 | bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 269 | { |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 270 | compileResources = resources; |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 271 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 272 | assert(symbolTable.isEmpty()); |
| 273 | symbolTable.push(); // COMMON_BUILTINS |
| 274 | symbolTable.push(); // ESSL1_BUILTINS |
| 275 | symbolTable.push(); // ESSL3_BUILTINS |
shannonwoods@chromium.org | 2ac0be9 | 2013-05-30 00:02:27 +0000 | [diff] [blame] | 276 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 277 | TPublicType integer; |
| 278 | integer.type = EbtInt; |
| 279 | integer.primarySize = 1; |
| 280 | integer.secondarySize = 1; |
| 281 | integer.array = false; |
| 282 | |
| 283 | TPublicType floatingPoint; |
| 284 | floatingPoint.type = EbtFloat; |
| 285 | floatingPoint.primarySize = 1; |
| 286 | floatingPoint.secondarySize = 1; |
| 287 | floatingPoint.array = false; |
| 288 | |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 289 | TPublicType sampler; |
| 290 | sampler.primarySize = 1; |
| 291 | sampler.secondarySize = 1; |
| 292 | sampler.array = false; |
| 293 | |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 294 | switch(shaderType) |
| 295 | { |
| 296 | case SH_FRAGMENT_SHADER: |
| 297 | symbolTable.setDefaultPrecision(integer, EbpMedium); |
| 298 | break; |
| 299 | case SH_VERTEX_SHADER: |
| 300 | symbolTable.setDefaultPrecision(integer, EbpHigh); |
| 301 | symbolTable.setDefaultPrecision(floatingPoint, EbpHigh); |
| 302 | break; |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 303 | default: |
| 304 | assert(false && "Language not supported"); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 305 | } |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 306 | // We set defaults for all the sampler types, even those that are |
| 307 | // only available if an extension exists. |
| 308 | for (int samplerType = EbtGuardSamplerBegin + 1; |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 309 | samplerType < EbtGuardSamplerEnd; ++samplerType) |
| 310 | { |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 311 | sampler.type = static_cast<TBasicType>(samplerType); |
| 312 | symbolTable.setDefaultPrecision(sampler, EbpLow); |
| 313 | } |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 314 | |
Jamie Madill | 1b45214 | 2013-07-12 14:51:11 -0400 | [diff] [blame] | 315 | InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 316 | |
| 317 | IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable); |
| 318 | |
| 319 | return true; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void TCompiler::clearResults() |
| 323 | { |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 324 | arrayBoundsClamper.Cleanup(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 325 | infoSink.info.erase(); |
| 326 | infoSink.obj.erase(); |
| 327 | infoSink.debug.erase(); |
| 328 | |
| 329 | attribs.clear(); |
| 330 | uniforms.clear(); |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 331 | varyings.clear(); |
zmo@google.com | a3b4ab4 | 2011-09-16 00:53:26 +0000 | [diff] [blame] | 332 | |
| 333 | builtInFunctionEmulator.Cleanup(); |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 334 | |
| 335 | nameMap.clear(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 338 | bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth) |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 339 | { |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 340 | DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth); |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 341 | root->traverse(&detect); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 342 | switch (detect.detectCallDepth()) |
| 343 | { |
| 344 | case DetectCallDepth::kErrorNone: |
| 345 | return true; |
| 346 | case DetectCallDepth::kErrorMissingMain: |
| 347 | infoSink.info.prefix(EPrefixError); |
| 348 | infoSink.info << "Missing main()"; |
| 349 | return false; |
| 350 | case DetectCallDepth::kErrorRecursion: |
| 351 | infoSink.info.prefix(EPrefixError); |
| 352 | infoSink.info << "Function recursion detected"; |
| 353 | return false; |
| 354 | case DetectCallDepth::kErrorMaxDepthExceeded: |
| 355 | infoSink.info.prefix(EPrefixError); |
| 356 | infoSink.info << "Function call stack too deep"; |
| 357 | return false; |
| 358 | default: |
| 359 | UNREACHABLE(); |
| 360 | return false; |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 364 | bool TCompiler::validateOutputs(TIntermNode* root) |
| 365 | { |
| 366 | ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers); |
| 367 | root->traverse(&validateOutputs); |
| 368 | return (validateOutputs.numErrors() == 0); |
| 369 | } |
| 370 | |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 371 | void TCompiler::rewriteCSSShader(TIntermNode* root) |
| 372 | { |
| 373 | RenameFunction renamer("main(", "css_main("); |
| 374 | root->traverse(&renamer); |
| 375 | } |
| 376 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 377 | bool TCompiler::validateLimitations(TIntermNode* root) |
| 378 | { |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 379 | ValidateLimitations validate(shaderType, infoSink.info); |
| 380 | root->traverse(&validate); |
| 381 | return validate.numErrors() == 0; |
| 382 | } |
| 383 | |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 384 | bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph) |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 385 | { |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 386 | if (shaderSpec != SH_WEBGL_SPEC) |
| 387 | { |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 388 | infoSink.info << "Timing restrictions must be enforced under the WebGL spec."; |
| 389 | return false; |
| 390 | } |
| 391 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 392 | if (shaderType == SH_FRAGMENT_SHADER) |
| 393 | { |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 394 | TDependencyGraph graph(root); |
| 395 | |
| 396 | // Output any errors first. |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 397 | bool success = enforceFragmentShaderTimingRestrictions(graph); |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 398 | |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 399 | // Then, output the dependency graph. |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 400 | if (outputGraph) |
| 401 | { |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 402 | TDependencyGraphOutput output(infoSink.info); |
| 403 | output.outputAllSpanningTrees(graph); |
| 404 | } |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 405 | |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 406 | return success; |
| 407 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 408 | else |
| 409 | { |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 410 | return enforceVertexShaderTimingRestrictions(root); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 414 | bool TCompiler::limitExpressionComplexity(TIntermNode* root) |
| 415 | { |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 416 | TMaxDepthTraverser traverser(maxExpressionComplexity+1); |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 417 | root->traverse(&traverser); |
Jamie Madill | 6654bc9 | 2014-03-26 14:01:57 -0400 | [diff] [blame] | 418 | |
| 419 | if (traverser.getMaxDepth() > maxExpressionComplexity) |
| 420 | { |
| 421 | infoSink.info << "Expression too complex."; |
| 422 | return false; |
| 423 | } |
| 424 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 425 | TDependencyGraph graph(root); |
| 426 | |
| 427 | for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls(); |
| 428 | iter != graph.endUserDefinedFunctionCalls(); |
| 429 | ++iter) |
| 430 | { |
| 431 | TGraphFunctionCall* samplerSymbol = *iter; |
| 432 | TDependencyGraphTraverser graphTraverser; |
| 433 | samplerSymbol->traverse(&graphTraverser); |
| 434 | } |
| 435 | |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 439 | bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph) |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 440 | { |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 441 | RestrictFragmentShaderTiming restrictor(infoSink.info); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 442 | restrictor.enforceRestrictions(graph); |
| 443 | return restrictor.numErrors() == 0; |
| 444 | } |
| 445 | |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 446 | bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root) |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 447 | { |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 448 | RestrictVertexShaderTiming restrictor(infoSink.info); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 449 | restrictor.enforceRestrictions(root); |
| 450 | return restrictor.numErrors() == 0; |
| 451 | } |
| 452 | |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 453 | void TCompiler::collectVariables(TIntermNode* root) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 454 | { |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 455 | CollectVariables collect(attribs, uniforms, varyings, hashFunction); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 456 | root->traverse(&collect); |
| 457 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 458 | |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 459 | bool TCompiler::enforcePackingRestrictions() |
| 460 | { |
| 461 | VariablePacker packer; |
| 462 | return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms); |
| 463 | } |
| 464 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 465 | void TCompiler::initializeGLPosition(TIntermNode* root) |
| 466 | { |
| 467 | InitializeVariables::InitVariableInfoList variables; |
| 468 | InitializeVariables::InitVariableInfo var( |
| 469 | "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4)); |
| 470 | variables.push_back(var); |
| 471 | InitializeVariables initializer(variables); |
| 472 | root->traverse(&initializer); |
| 473 | } |
| 474 | |
| 475 | void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root) |
| 476 | { |
| 477 | InitializeVariables::InitVariableInfoList variables; |
| 478 | for (size_t ii = 0; ii < varyings.size(); ++ii) |
| 479 | { |
| 480 | const TVariableInfo& varying = varyings[ii]; |
| 481 | if (varying.staticUse) |
| 482 | continue; |
| 483 | unsigned char size = 0; |
| 484 | bool matrix = false; |
| 485 | switch (varying.type) |
| 486 | { |
| 487 | case SH_FLOAT: |
| 488 | size = 1; |
| 489 | break; |
| 490 | case SH_FLOAT_VEC2: |
| 491 | size = 2; |
| 492 | break; |
| 493 | case SH_FLOAT_VEC3: |
| 494 | size = 3; |
| 495 | break; |
| 496 | case SH_FLOAT_VEC4: |
| 497 | size = 4; |
| 498 | break; |
| 499 | case SH_FLOAT_MAT2: |
| 500 | size = 2; |
| 501 | matrix = true; |
| 502 | break; |
| 503 | case SH_FLOAT_MAT3: |
| 504 | size = 3; |
| 505 | matrix = true; |
| 506 | break; |
| 507 | case SH_FLOAT_MAT4: |
| 508 | size = 4; |
| 509 | matrix = true; |
| 510 | break; |
| 511 | default: |
| 512 | ASSERT(false); |
| 513 | } |
| 514 | TType type(EbtFloat, EbpUndefined, EvqVaryingOut, size, matrix, varying.isArray); |
| 515 | TString name = varying.name.c_str(); |
| 516 | if (varying.isArray) |
| 517 | { |
| 518 | type.setArraySize(varying.size); |
| 519 | name = name.substr(0, name.find_first_of('[')); |
| 520 | } |
| 521 | |
| 522 | InitializeVariables::InitVariableInfo var(name, type); |
| 523 | variables.push_back(var); |
| 524 | } |
| 525 | InitializeVariables initializer(variables); |
| 526 | root->traverse(&initializer); |
| 527 | } |
| 528 | |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 529 | const TExtensionBehavior& TCompiler::getExtensionBehavior() const |
| 530 | { |
| 531 | return extensionBehavior; |
| 532 | } |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 533 | |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 534 | const ShBuiltInResources& TCompiler::getResources() const |
| 535 | { |
| 536 | return compileResources; |
| 537 | } |
| 538 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 539 | const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const |
| 540 | { |
| 541 | return arrayBoundsClamper; |
| 542 | } |
| 543 | |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 544 | ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const |
| 545 | { |
| 546 | return clampingStrategy; |
| 547 | } |
| 548 | |
| 549 | const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const |
| 550 | { |
| 551 | return builtInFunctionEmulator; |
| 552 | } |