blob: 86ef6c35bdf3b8d68fe2a08cf5876382c4d13689 [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
Jamie Madilled27c722014-07-02 15:31:23 -0400360 attributes.clear();
361 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000362 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400363 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400364 varyings.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400365 expandedVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400366 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000367
368 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000369
370 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000371}
372
Jamie Madilleb1a0102013-07-08 13:31:38 -0400373bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000374{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400375 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000376 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800377 switch (detect.detectCallDepth())
378 {
379 case DetectCallDepth::kErrorNone:
380 return true;
381 case DetectCallDepth::kErrorMissingMain:
382 infoSink.info.prefix(EPrefixError);
383 infoSink.info << "Missing main()";
384 return false;
385 case DetectCallDepth::kErrorRecursion:
386 infoSink.info.prefix(EPrefixError);
387 infoSink.info << "Function recursion detected";
388 return false;
389 case DetectCallDepth::kErrorMaxDepthExceeded:
390 infoSink.info.prefix(EPrefixError);
391 infoSink.info << "Function call stack too deep";
392 return false;
393 default:
394 UNREACHABLE();
395 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000396 }
397}
398
Jamie Madill05a80ce2013-06-20 11:55:49 -0400399bool TCompiler::validateOutputs(TIntermNode* root)
400{
401 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
402 root->traverse(&validateOutputs);
403 return (validateOutputs.numErrors() == 0);
404}
405
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000406void TCompiler::rewriteCSSShader(TIntermNode* root)
407{
408 RenameFunction renamer("main(", "css_main(");
409 root->traverse(&renamer);
410}
411
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800412bool TCompiler::validateLimitations(TIntermNode* root)
413{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000414 ValidateLimitations validate(shaderType, infoSink.info);
415 root->traverse(&validate);
416 return validate.numErrors() == 0;
417}
418
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000419bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000420{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800421 if (shaderSpec != SH_WEBGL_SPEC)
422 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000423 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
424 return false;
425 }
426
Jamie Madill183bde52014-07-02 15:31:19 -0400427 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800428 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000429 TDependencyGraph graph(root);
430
431 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000432 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500433
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000434 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800435 if (outputGraph)
436 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000437 TDependencyGraphOutput output(infoSink.info);
438 output.outputAllSpanningTrees(graph);
439 }
Jamie Madill5508f392014-02-20 13:31:36 -0500440
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000441 return success;
442 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800443 else
444 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000445 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000446 }
447}
448
Jamie Madilleb1a0102013-07-08 13:31:38 -0400449bool TCompiler::limitExpressionComplexity(TIntermNode* root)
450{
Jamie Madill6654bc92014-03-26 14:01:57 -0400451 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400452 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400453
454 if (traverser.getMaxDepth() > maxExpressionComplexity)
455 {
456 infoSink.info << "Expression too complex.";
457 return false;
458 }
459
Jamie Madilleb1a0102013-07-08 13:31:38 -0400460 TDependencyGraph graph(root);
461
462 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
463 iter != graph.endUserDefinedFunctionCalls();
464 ++iter)
465 {
466 TGraphFunctionCall* samplerSymbol = *iter;
467 TDependencyGraphTraverser graphTraverser;
468 samplerSymbol->traverse(&graphTraverser);
469 }
470
Jamie Madilleb1a0102013-07-08 13:31:38 -0400471 return true;
472}
473
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000474bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000475{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000476 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000477 restrictor.enforceRestrictions(graph);
478 return restrictor.numErrors() == 0;
479}
480
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000481bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000482{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000483 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000484 restrictor.enforceRestrictions(root);
485 return restrictor.numErrors() == 0;
486}
487
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400488void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000489{
Jamie Madilld5512cd2014-07-10 17:50:08 -0400490 CollectVariables collect(&attributes,
491 &outputVariables,
492 &uniforms,
493 &varyings,
494 &interfaceBlocks,
495 hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000496 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400497
498 // For backwards compatiblity with ShGetVariableInfo, expand struct
499 // uniforms and varyings into separate variables for each field.
500 ExpandVariables(uniforms, &expandedUniforms);
501 ExpandVariables(varyings, &expandedVaryings);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000502}
zmo@google.comfd747b82011-04-23 01:30:07 +0000503
gman@chromium.org8d804792012-10-17 21:33:48 +0000504bool TCompiler::enforcePackingRestrictions()
505{
506 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400507 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000508}
509
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800510void TCompiler::initializeGLPosition(TIntermNode* root)
511{
512 InitializeVariables::InitVariableInfoList variables;
513 InitializeVariables::InitVariableInfo var(
514 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
515 variables.push_back(var);
516 InitializeVariables initializer(variables);
517 root->traverse(&initializer);
518}
519
520void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
521{
522 InitializeVariables::InitVariableInfoList variables;
523 for (size_t ii = 0; ii < varyings.size(); ++ii)
524 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400525 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800526 if (varying.staticUse)
527 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400528 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
529 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400530 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800531 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400532 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800533 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400534 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800535 name = name.substr(0, name.find_first_of('['));
536 }
537
538 InitializeVariables::InitVariableInfo var(name, type);
539 variables.push_back(var);
540 }
541 InitializeVariables initializer(variables);
542 root->traverse(&initializer);
543}
544
zmo@google.com5601ea02011-06-10 18:23:25 +0000545const TExtensionBehavior& TCompiler::getExtensionBehavior() const
546{
547 return extensionBehavior;
548}
zmo@google.com32e97312011-08-24 01:03:11 +0000549
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000550const ShBuiltInResources& TCompiler::getResources() const
551{
552 return compileResources;
553}
554
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000555const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
556{
557 return arrayBoundsClamper;
558}
559
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000560ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
561{
562 return clampingStrategy;
563}
564
565const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
566{
567 return builtInFunctionEmulator;
568}