blob: a93d7e63354127f6f297de690503867daf63acd8 [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"
Zhenyao Moe740add2014-07-18 17:01:01 -070015#include "compiler/translator/RegenerateStructNames.h"
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/RenameFunction.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070017#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070018#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/ValidateLimitations.h"
20#include "compiler/translator/ValidateOutputs.h"
21#include "compiler/translator/VariablePacker.h"
22#include "compiler/translator/depgraph/DependencyGraph.h"
23#include "compiler/translator/depgraph/DependencyGraphOutput.h"
24#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
25#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000026#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040027#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040028#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000029
Jamie Madill5508f392014-02-20 13:31:36 -050030bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000031{
32 return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
33}
34
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070035size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050036{
Jamie Madill88f6e942014-02-19 10:27:53 -050037 // WebGL defines a max token legnth of 256, while ES2 leaves max token
38 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070039 if (IsWebGLBasedSpec(spec))
Jamie Madill88f6e942014-02-19 10:27:53 -050040 {
41 return 256;
42 }
43 else
44 {
45 return 1024;
46 }
47}
48
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000049namespace {
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080050class TScopedPoolAllocator
51{
52 public:
53 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
54 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040055 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000056 SetGlobalPoolAllocator(mAllocator);
57 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080058 ~TScopedPoolAllocator()
59 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000060 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040061 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000062 }
63
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080064 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000065 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040066};
67
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080068class TScopedSymbolTableLevel
69{
70 public:
71 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
72 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040073 ASSERT(mTable->atBuiltInLevel());
74 mTable->push();
75 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080076 ~TScopedSymbolTableLevel()
77 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040078 while (!mTable->atBuiltInLevel())
79 mTable->pop();
80 }
81
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080082 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040083 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000084};
85} // namespace
86
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080087TShHandleBase::TShHandleBase()
88{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000089 allocator.push();
90 SetGlobalPoolAllocator(&allocator);
91}
92
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080093TShHandleBase::~TShHandleBase()
94{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000095 SetGlobalPoolAllocator(NULL);
96 allocator.popAll();
97}
98
Jamie Madill183bde52014-07-02 15:31:19 -040099TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000100 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000101 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400102 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400103 maxUniformVectors(0),
104 maxExpressionComplexity(0),
105 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000106 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000107 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000108 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000109{
110}
111
112TCompiler::~TCompiler()
113{
114}
115
116bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000117{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000118 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400119 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000120 resources.MaxVertexUniformVectors :
121 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400122 maxExpressionComplexity = resources.MaxExpressionComplexity;
123 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400124
125 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000126
alokp@chromium.org07620a52010-09-23 17:53:56 +0000127 // Generate built-in symbol table.
128 if (!InitBuiltInSymbolTable(resources))
129 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000130 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000131 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000132
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000133 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
134 clampingStrategy = resources.ArrayIndexClampingStrategy;
135
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000136 hashFunction = resources.HashFunction;
137
alokp@chromium.org07620a52010-09-23 17:53:56 +0000138 return true;
139}
140
141bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000142 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000143 int compileOptions)
144{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400145 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000146 clearResults();
147
148 if (numStrings == 0)
149 return true;
150
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000151 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500152 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000153 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000154
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000155 // First string is path of source file if flag is set. The actual source follows.
156 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000157 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000158 if (compileOptions & SH_SOURCE_PATH)
159 {
160 sourcePath = shaderStrings[0];
161 ++firstSource;
162 }
163
alokp@chromium.org07620a52010-09-23 17:53:56 +0000164 TIntermediate intermediate(infoSink);
165 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000166 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000167 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000168 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400169 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000170
171 // We preserve symbols at the built-in level from compile-to-compile.
172 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400173 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000174
175 // Parse shader.
176 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000177 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000178 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000179
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000180 shaderVersion = parseContext.getShaderVersion();
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000181
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800182 if (success)
183 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700184 mPragma = parseContext.pragma();
185 if (mPragma.stdgl.invariantAll)
186 {
187 symbolTable.setGlobalInvariant();
188 }
189
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000190 TIntermNode* root = parseContext.treeRoot;
191 success = intermediate.postProcess(root);
192
Jamie Madill6654bc92014-03-26 14:01:57 -0400193 // Disallow expressions deemed too complex.
194 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
195 success = limitExpressionComplexity(root);
196
zmo@google.comb1762df2011-07-30 02:04:23 +0000197 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400198 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000199
Jamie Madill183bde52014-07-02 15:31:19 -0400200 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400201 success = validateOutputs(root);
202
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000203 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
204 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000205
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000206 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000207 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000208
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000209 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
210 rewriteCSSShader(root);
211
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000212 // Unroll for-loop markup needs to happen after validateLimitations pass.
213 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800214 {
215 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800216 root->traverse(&marker);
217 }
218 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800219 {
220 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800221 root->traverse(&marker);
222 if (marker.samplerArrayIndexIsFloatLoopIndex())
223 {
224 infoSink.info.prefix(EPrefixError);
225 infoSink.info << "sampler array index is float loop index";
226 success = false;
227 }
228 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000229
zmo@google.com32e97312011-08-24 01:03:11 +0000230 // Built-in function emulation needs to happen after validateLimitations pass.
231 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
232 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
233
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000234 // Clamping uniform array bounds needs to happen after validateLimitations pass.
235 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
236 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
237
Jamie Madill183bde52014-07-02 15:31:19 -0400238 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800239 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400240
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800241 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
242 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700243 UnfoldShortCircuitAST unfoldShortCircuit;
244 root->traverse(&unfoldShortCircuit);
245 unfoldShortCircuit.updateTree();
246 }
247
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800248 if (success && (compileOptions & SH_VARIABLES))
249 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400250 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800251 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
252 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000253 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800254 if (!success)
255 {
Jamie Madill075edd82013-07-08 13:30:19 -0400256 infoSink.info.prefix(EPrefixError);
257 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000258 }
259 }
Jamie Madill183bde52014-07-02 15:31:19 -0400260 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800261 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
262 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000263 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000264
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700265 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
266 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700267 ScalarizeVecAndMatConstructorArgs scalarizer(
268 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700269 root->traverse(&scalarizer);
270 }
271
Zhenyao Moe740add2014-07-18 17:01:01 -0700272 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
273 {
274 RegenerateStructNames gen(symbolTable, shaderVersion);
275 root->traverse(&gen);
276 }
277
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000278 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000279 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000280
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000281 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000282 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000283 }
284
285 // Cleanup memory.
286 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700287 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000288 return success;
289}
290
Nicolas Capens49a88872013-06-20 09:54:03 -0400291bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000292{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000293 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400294 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000295
Nicolas Capens49a88872013-06-20 09:54:03 -0400296 assert(symbolTable.isEmpty());
297 symbolTable.push(); // COMMON_BUILTINS
298 symbolTable.push(); // ESSL1_BUILTINS
299 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000300
Nicolas Capens49a88872013-06-20 09:54:03 -0400301 TPublicType integer;
302 integer.type = EbtInt;
303 integer.primarySize = 1;
304 integer.secondarySize = 1;
305 integer.array = false;
306
307 TPublicType floatingPoint;
308 floatingPoint.type = EbtFloat;
309 floatingPoint.primarySize = 1;
310 floatingPoint.secondarySize = 1;
311 floatingPoint.array = false;
312
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400313 TPublicType sampler;
314 sampler.primarySize = 1;
315 sampler.secondarySize = 1;
316 sampler.array = false;
317
Nicolas Capens49a88872013-06-20 09:54:03 -0400318 switch(shaderType)
319 {
Jamie Madill183bde52014-07-02 15:31:19 -0400320 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400321 symbolTable.setDefaultPrecision(integer, EbpMedium);
322 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400323 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400324 symbolTable.setDefaultPrecision(integer, EbpHigh);
325 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
326 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800327 default:
328 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400329 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400330 // We set defaults for all the sampler types, even those that are
331 // only available if an extension exists.
332 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800333 samplerType < EbtGuardSamplerEnd; ++samplerType)
334 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400335 sampler.type = static_cast<TBasicType>(samplerType);
336 symbolTable.setDefaultPrecision(sampler, EbpLow);
337 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400338
Jamie Madill1b452142013-07-12 14:51:11 -0400339 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400340
341 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
342
343 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000344}
345
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400346void TCompiler::setResourceString()
347{
348 std::ostringstream strstream;
349 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
350 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
351 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
352 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
353 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
354 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
355 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
356 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
357 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
358 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
359 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
360 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
361 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
362 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
363 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
364 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
365 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
366 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
367 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
368 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300369 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
370 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400371
372 builtInResourcesString = strstream.str();
373}
374
alokp@chromium.org07620a52010-09-23 17:53:56 +0000375void TCompiler::clearResults()
376{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000377 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000378 infoSink.info.erase();
379 infoSink.obj.erase();
380 infoSink.debug.erase();
381
Jamie Madilled27c722014-07-02 15:31:23 -0400382 attributes.clear();
383 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000384 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400385 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400386 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400387 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000388
389 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000390
391 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000392}
393
Jamie Madilleb1a0102013-07-08 13:31:38 -0400394bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000395{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400396 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000397 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800398 switch (detect.detectCallDepth())
399 {
400 case DetectCallDepth::kErrorNone:
401 return true;
402 case DetectCallDepth::kErrorMissingMain:
403 infoSink.info.prefix(EPrefixError);
404 infoSink.info << "Missing main()";
405 return false;
406 case DetectCallDepth::kErrorRecursion:
407 infoSink.info.prefix(EPrefixError);
408 infoSink.info << "Function recursion detected";
409 return false;
410 case DetectCallDepth::kErrorMaxDepthExceeded:
411 infoSink.info.prefix(EPrefixError);
412 infoSink.info << "Function call stack too deep";
413 return false;
414 default:
415 UNREACHABLE();
416 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000417 }
418}
419
Jamie Madill05a80ce2013-06-20 11:55:49 -0400420bool TCompiler::validateOutputs(TIntermNode* root)
421{
422 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
423 root->traverse(&validateOutputs);
424 return (validateOutputs.numErrors() == 0);
425}
426
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000427void TCompiler::rewriteCSSShader(TIntermNode* root)
428{
429 RenameFunction renamer("main(", "css_main(");
430 root->traverse(&renamer);
431}
432
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800433bool TCompiler::validateLimitations(TIntermNode* root)
434{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000435 ValidateLimitations validate(shaderType, infoSink.info);
436 root->traverse(&validate);
437 return validate.numErrors() == 0;
438}
439
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000440bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000441{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800442 if (shaderSpec != SH_WEBGL_SPEC)
443 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000444 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
445 return false;
446 }
447
Jamie Madill183bde52014-07-02 15:31:19 -0400448 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800449 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000450 TDependencyGraph graph(root);
451
452 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000453 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500454
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000455 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800456 if (outputGraph)
457 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000458 TDependencyGraphOutput output(infoSink.info);
459 output.outputAllSpanningTrees(graph);
460 }
Jamie Madill5508f392014-02-20 13:31:36 -0500461
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000462 return success;
463 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800464 else
465 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000466 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000467 }
468}
469
Jamie Madilleb1a0102013-07-08 13:31:38 -0400470bool TCompiler::limitExpressionComplexity(TIntermNode* root)
471{
Jamie Madill6654bc92014-03-26 14:01:57 -0400472 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400473 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400474
475 if (traverser.getMaxDepth() > maxExpressionComplexity)
476 {
477 infoSink.info << "Expression too complex.";
478 return false;
479 }
480
Jamie Madilleb1a0102013-07-08 13:31:38 -0400481 TDependencyGraph graph(root);
482
483 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
484 iter != graph.endUserDefinedFunctionCalls();
485 ++iter)
486 {
487 TGraphFunctionCall* samplerSymbol = *iter;
488 TDependencyGraphTraverser graphTraverser;
489 samplerSymbol->traverse(&graphTraverser);
490 }
491
Jamie Madilleb1a0102013-07-08 13:31:38 -0400492 return true;
493}
494
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000495bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000496{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000497 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000498 restrictor.enforceRestrictions(graph);
499 return restrictor.numErrors() == 0;
500}
501
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000502bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000503{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000504 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000505 restrictor.enforceRestrictions(root);
506 return restrictor.numErrors() == 0;
507}
508
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400509void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000510{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400511 sh::CollectVariables collect(&attributes,
512 &outputVariables,
513 &uniforms,
514 &varyings,
515 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700516 hashFunction,
517 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000518 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400519
Zhenyao Mo409078f2014-10-28 13:23:18 -0700520 // This is for enforcePackingRestriction().
521 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000522}
zmo@google.comfd747b82011-04-23 01:30:07 +0000523
gman@chromium.org8d804792012-10-17 21:33:48 +0000524bool TCompiler::enforcePackingRestrictions()
525{
526 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400527 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000528}
529
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800530void TCompiler::initializeGLPosition(TIntermNode* root)
531{
532 InitializeVariables::InitVariableInfoList variables;
533 InitializeVariables::InitVariableInfo var(
534 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
535 variables.push_back(var);
536 InitializeVariables initializer(variables);
537 root->traverse(&initializer);
538}
539
540void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
541{
542 InitializeVariables::InitVariableInfoList variables;
543 for (size_t ii = 0; ii < varyings.size(); ++ii)
544 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400545 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800546 if (varying.staticUse)
547 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400548 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
549 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400550 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800551 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400552 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800553 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400554 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800555 name = name.substr(0, name.find_first_of('['));
556 }
557
558 InitializeVariables::InitVariableInfo var(name, type);
559 variables.push_back(var);
560 }
561 InitializeVariables initializer(variables);
562 root->traverse(&initializer);
563}
564
zmo@google.com5601ea02011-06-10 18:23:25 +0000565const TExtensionBehavior& TCompiler::getExtensionBehavior() const
566{
567 return extensionBehavior;
568}
zmo@google.com32e97312011-08-24 01:03:11 +0000569
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000570const ShBuiltInResources& TCompiler::getResources() const
571{
572 return compileResources;
573}
574
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000575const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
576{
577 return arrayBoundsClamper;
578}
579
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000580ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
581{
582 return clampingStrategy;
583}
584
585const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
586{
587 return builtInFunctionEmulator;
588}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700589
590void TCompiler::writePragma()
591{
592 TInfoSinkBase &sink = infoSink.obj;
593 if (mPragma.stdgl.invariantAll)
594 sink << "#pragma STDGL invariant(all)\n";
595}