blob: 4402298aab1885a92fbfee211a26f4b4064a62e7 [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
Jamie Madill88f6e942014-02-19 10:27:53 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madilld4a3a312014-06-25 16:04:56 -04008#include "compiler/translator/Compiler.h"
Geoff Lang17732822013-08-29 13:46:49 -04009#include "compiler/translator/DetectCallDepth.h"
10#include "compiler/translator/ForLoopUnroll.h"
11#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080013#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040014#include "compiler/translator/ParseContext.h"
Geoff Lang17732822013-08-29 13:46:49 -040015#include "compiler/translator/RenameFunction.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070016#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040017#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.comda1ed362013-01-25 21:54:57 +000024#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040025#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040026#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000027
Jamie Madill5508f392014-02-20 13:31:36 -050028bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000029{
30 return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
31}
32
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070033size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050034{
Jamie Madill88f6e942014-02-19 10:27:53 -050035 // WebGL defines a max token legnth of 256, while ES2 leaves max token
36 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070037 if (IsWebGLBasedSpec(spec))
Jamie Madill88f6e942014-02-19 10:27:53 -050038 {
39 return 256;
40 }
41 else
42 {
43 return 1024;
44 }
45}
46
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000047namespace {
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080048class TScopedPoolAllocator
49{
50 public:
51 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
52 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040053 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000054 SetGlobalPoolAllocator(mAllocator);
55 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080056 ~TScopedPoolAllocator()
57 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000058 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040059 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000060 }
61
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080062 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000063 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040064};
65
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080066class TScopedSymbolTableLevel
67{
68 public:
69 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
70 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040071 ASSERT(mTable->atBuiltInLevel());
72 mTable->push();
73 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080074 ~TScopedSymbolTableLevel()
75 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040076 while (!mTable->atBuiltInLevel())
77 mTable->pop();
78 }
79
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080080 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040081 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000082};
83} // namespace
84
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080085TShHandleBase::TShHandleBase()
86{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000087 allocator.push();
88 SetGlobalPoolAllocator(&allocator);
89}
90
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080091TShHandleBase::~TShHandleBase()
92{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000093 SetGlobalPoolAllocator(NULL);
94 allocator.popAll();
95}
96
Jamie Madill183bde52014-07-02 15:31:19 -040097TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000098 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +000099 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400100 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400101 maxUniformVectors(0),
102 maxExpressionComplexity(0),
103 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000104 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000105 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000106 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000107{
108}
109
110TCompiler::~TCompiler()
111{
112}
113
114bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000115{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000116 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400117 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000118 resources.MaxVertexUniformVectors :
119 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400120 maxExpressionComplexity = resources.MaxExpressionComplexity;
121 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400122
123 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000124
alokp@chromium.org07620a52010-09-23 17:53:56 +0000125 // Generate built-in symbol table.
126 if (!InitBuiltInSymbolTable(resources))
127 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000128 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000129 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000130
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000131 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
132 clampingStrategy = resources.ArrayIndexClampingStrategy;
133
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000134 hashFunction = resources.HashFunction;
135
alokp@chromium.org07620a52010-09-23 17:53:56 +0000136 return true;
137}
138
139bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000140 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000141 int compileOptions)
142{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400143 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000144 clearResults();
145
146 if (numStrings == 0)
147 return true;
148
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000149 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500150 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000151 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000152
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000153 // First string is path of source file if flag is set. The actual source follows.
154 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000155 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000156 if (compileOptions & SH_SOURCE_PATH)
157 {
158 sourcePath = shaderStrings[0];
159 ++firstSource;
160 }
161
alokp@chromium.org07620a52010-09-23 17:53:56 +0000162 TIntermediate intermediate(infoSink);
163 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000164 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000165 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000166 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400167 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000168
169 // We preserve symbols at the built-in level from compile-to-compile.
170 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400171 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000172
173 // Parse shader.
174 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000175 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000176 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000177
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000178 shaderVersion = parseContext.getShaderVersion();
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000179
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800180 if (success)
181 {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000182 TIntermNode* root = parseContext.treeRoot;
183 success = intermediate.postProcess(root);
184
Jamie Madill6654bc92014-03-26 14:01:57 -0400185 // Disallow expressions deemed too complex.
186 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
187 success = limitExpressionComplexity(root);
188
zmo@google.comb1762df2011-07-30 02:04:23 +0000189 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400190 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000191
Jamie Madill183bde52014-07-02 15:31:19 -0400192 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400193 success = validateOutputs(root);
194
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000195 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
196 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000197
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000198 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000199 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000200
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000201 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
202 rewriteCSSShader(root);
203
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000204 // Unroll for-loop markup needs to happen after validateLimitations pass.
205 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800206 {
207 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800208 root->traverse(&marker);
209 }
210 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800211 {
212 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800213 root->traverse(&marker);
214 if (marker.samplerArrayIndexIsFloatLoopIndex())
215 {
216 infoSink.info.prefix(EPrefixError);
217 infoSink.info << "sampler array index is float loop index";
218 success = false;
219 }
220 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000221
zmo@google.com32e97312011-08-24 01:03:11 +0000222 // Built-in function emulation needs to happen after validateLimitations pass.
223 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
224 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
225
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000226 // Clamping uniform array bounds needs to happen after validateLimitations pass.
227 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
228 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
229
Jamie Madill183bde52014-07-02 15:31:19 -0400230 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800231 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400232
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800233 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
234 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700235 UnfoldShortCircuitAST unfoldShortCircuit;
236 root->traverse(&unfoldShortCircuit);
237 unfoldShortCircuit.updateTree();
238 }
239
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800240 if (success && (compileOptions & SH_VARIABLES))
241 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400242 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800243 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
244 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000245 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800246 if (!success)
247 {
Jamie Madill075edd82013-07-08 13:30:19 -0400248 infoSink.info.prefix(EPrefixError);
249 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000250 }
251 }
Jamie Madill183bde52014-07-02 15:31:19 -0400252 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800253 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
254 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000255 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000256
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000257 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000258 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000259
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000260 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000261 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000262 }
263
264 // Cleanup memory.
265 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700266 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000267 return success;
268}
269
Nicolas Capens49a88872013-06-20 09:54:03 -0400270bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000271{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000272 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400273 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000274
Nicolas Capens49a88872013-06-20 09:54:03 -0400275 assert(symbolTable.isEmpty());
276 symbolTable.push(); // COMMON_BUILTINS
277 symbolTable.push(); // ESSL1_BUILTINS
278 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000279
Nicolas Capens49a88872013-06-20 09:54:03 -0400280 TPublicType integer;
281 integer.type = EbtInt;
282 integer.primarySize = 1;
283 integer.secondarySize = 1;
284 integer.array = false;
285
286 TPublicType floatingPoint;
287 floatingPoint.type = EbtFloat;
288 floatingPoint.primarySize = 1;
289 floatingPoint.secondarySize = 1;
290 floatingPoint.array = false;
291
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400292 TPublicType sampler;
293 sampler.primarySize = 1;
294 sampler.secondarySize = 1;
295 sampler.array = false;
296
Nicolas Capens49a88872013-06-20 09:54:03 -0400297 switch(shaderType)
298 {
Jamie Madill183bde52014-07-02 15:31:19 -0400299 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400300 symbolTable.setDefaultPrecision(integer, EbpMedium);
301 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400302 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400303 symbolTable.setDefaultPrecision(integer, EbpHigh);
304 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
305 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800306 default:
307 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400308 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400309 // We set defaults for all the sampler types, even those that are
310 // only available if an extension exists.
311 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800312 samplerType < EbtGuardSamplerEnd; ++samplerType)
313 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400314 sampler.type = static_cast<TBasicType>(samplerType);
315 symbolTable.setDefaultPrecision(sampler, EbpLow);
316 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400317
Jamie Madill1b452142013-07-12 14:51:11 -0400318 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400319
320 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
321
322 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000323}
324
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400325void TCompiler::setResourceString()
326{
327 std::ostringstream strstream;
328 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
329 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
330 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
331 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
332 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
333 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
334 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
335 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
336 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
337 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
338 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
339 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
340 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
341 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
342 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
343 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
344 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
345 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
346 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
347 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
348 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset;
349
350 builtInResourcesString = strstream.str();
351}
352
alokp@chromium.org07620a52010-09-23 17:53:56 +0000353void TCompiler::clearResults()
354{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000355 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000356 infoSink.info.erase();
357 infoSink.obj.erase();
358 infoSink.debug.erase();
359
360 attribs.clear();
361 uniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400362 varyings.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000363
364 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000365
366 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000367}
368
Jamie Madilleb1a0102013-07-08 13:31:38 -0400369bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000370{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400371 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000372 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800373 switch (detect.detectCallDepth())
374 {
375 case DetectCallDepth::kErrorNone:
376 return true;
377 case DetectCallDepth::kErrorMissingMain:
378 infoSink.info.prefix(EPrefixError);
379 infoSink.info << "Missing main()";
380 return false;
381 case DetectCallDepth::kErrorRecursion:
382 infoSink.info.prefix(EPrefixError);
383 infoSink.info << "Function recursion detected";
384 return false;
385 case DetectCallDepth::kErrorMaxDepthExceeded:
386 infoSink.info.prefix(EPrefixError);
387 infoSink.info << "Function call stack too deep";
388 return false;
389 default:
390 UNREACHABLE();
391 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000392 }
393}
394
Jamie Madill05a80ce2013-06-20 11:55:49 -0400395bool TCompiler::validateOutputs(TIntermNode* root)
396{
397 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
398 root->traverse(&validateOutputs);
399 return (validateOutputs.numErrors() == 0);
400}
401
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000402void TCompiler::rewriteCSSShader(TIntermNode* root)
403{
404 RenameFunction renamer("main(", "css_main(");
405 root->traverse(&renamer);
406}
407
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800408bool TCompiler::validateLimitations(TIntermNode* root)
409{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000410 ValidateLimitations validate(shaderType, infoSink.info);
411 root->traverse(&validate);
412 return validate.numErrors() == 0;
413}
414
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000415bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000416{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800417 if (shaderSpec != SH_WEBGL_SPEC)
418 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000419 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
420 return false;
421 }
422
Jamie Madill183bde52014-07-02 15:31:19 -0400423 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800424 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000425 TDependencyGraph graph(root);
426
427 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000428 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500429
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000430 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800431 if (outputGraph)
432 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000433 TDependencyGraphOutput output(infoSink.info);
434 output.outputAllSpanningTrees(graph);
435 }
Jamie Madill5508f392014-02-20 13:31:36 -0500436
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000437 return success;
438 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800439 else
440 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000441 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000442 }
443}
444
Jamie Madilleb1a0102013-07-08 13:31:38 -0400445bool TCompiler::limitExpressionComplexity(TIntermNode* root)
446{
Jamie Madill6654bc92014-03-26 14:01:57 -0400447 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400448 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400449
450 if (traverser.getMaxDepth() > maxExpressionComplexity)
451 {
452 infoSink.info << "Expression too complex.";
453 return false;
454 }
455
Jamie Madilleb1a0102013-07-08 13:31:38 -0400456 TDependencyGraph graph(root);
457
458 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
459 iter != graph.endUserDefinedFunctionCalls();
460 ++iter)
461 {
462 TGraphFunctionCall* samplerSymbol = *iter;
463 TDependencyGraphTraverser graphTraverser;
464 samplerSymbol->traverse(&graphTraverser);
465 }
466
Jamie Madilleb1a0102013-07-08 13:31:38 -0400467 return true;
468}
469
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000470bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000471{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000472 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000473 restrictor.enforceRestrictions(graph);
474 return restrictor.numErrors() == 0;
475}
476
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000477bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000478{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000479 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000480 restrictor.enforceRestrictions(root);
481 return restrictor.numErrors() == 0;
482}
483
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400484void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000485{
Jamie Madilla718c1e2014-07-02 15:31:22 -0400486 CollectVariables collect(&attribs, &uniforms, &varyings, hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000487 root->traverse(&collect);
488}
zmo@google.comfd747b82011-04-23 01:30:07 +0000489
gman@chromium.org8d804792012-10-17 21:33:48 +0000490bool TCompiler::enforcePackingRestrictions()
491{
492 VariablePacker packer;
493 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
494}
495
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800496void TCompiler::initializeGLPosition(TIntermNode* root)
497{
498 InitializeVariables::InitVariableInfoList variables;
499 InitializeVariables::InitVariableInfo var(
500 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
501 variables.push_back(var);
502 InitializeVariables initializer(variables);
503 root->traverse(&initializer);
504}
505
506void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
507{
508 InitializeVariables::InitVariableInfoList variables;
509 for (size_t ii = 0; ii < varyings.size(); ++ii)
510 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400511 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800512 if (varying.staticUse)
513 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400514 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
515 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400516 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800517 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400518 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800519 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400520 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800521 name = name.substr(0, name.find_first_of('['));
522 }
523
524 InitializeVariables::InitVariableInfo var(name, type);
525 variables.push_back(var);
526 }
527 InitializeVariables initializer(variables);
528 root->traverse(&initializer);
529}
530
zmo@google.com5601ea02011-06-10 18:23:25 +0000531const TExtensionBehavior& TCompiler::getExtensionBehavior() const
532{
533 return extensionBehavior;
534}
zmo@google.com32e97312011-08-24 01:03:11 +0000535
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000536const ShBuiltInResources& TCompiler::getResources() const
537{
538 return compileResources;
539}
540
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000541const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
542{
543 return arrayBoundsClamper;
544}
545
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000546ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
547{
548 return clampingStrategy;
549}
550
551const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
552{
553 return builtInFunctionEmulator;
554}