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