blob: 9c9f9e70ab05edfeaa64a6a95a9ae2867e6a2aad [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
zmo@google.comb9f64aa2012-01-20 00:35:15 +00002// Copyright (c) 2002-2012 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
zmo@google.com32e97312011-08-24 01:03:11 +00007#include "compiler/BuiltInFunctionEmulator.h"
zmo@google.comb1762df2011-07-30 02:04:23 +00008#include "compiler/DetectRecursion.h"
zmo@google.com0c6bb7a2011-08-17 19:39:58 +00009#include "compiler/ForLoopUnroll.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000010#include "compiler/Initialize.h"
alokp@chromium.org8b851c62012-06-15 16:25:11 +000011#include "compiler/InitializeParseContext.h"
zmo@google.comb9f64aa2012-01-20 00:35:15 +000012#include "compiler/MapLongVariableNames.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000013#include "compiler/ParseHelper.h"
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000014#include "compiler/RenameFunction.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000015#include "compiler/ShHandle.h"
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000016#include "compiler/ValidateLimitations.h"
gman@chromium.org8d804792012-10-17 21:33:48 +000017#include "compiler/VariablePacker.h"
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000018#include "compiler/depgraph/DependencyGraph.h"
19#include "compiler/depgraph/DependencyGraphOutput.h"
20#include "compiler/timing/RestrictFragmentShaderTiming.h"
21#include "compiler/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000022#include "third_party/compiler/ArrayBoundsClamper.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000023
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000024bool isWebGLBasedSpec(ShShaderSpec spec)
25{
26 return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
27}
28
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000029namespace {
30bool InitializeSymbolTable(
31 const TBuiltInStrings& builtInStrings,
32 ShShaderType type, ShShaderSpec spec, const ShBuiltInResources& resources,
33 TInfoSink& infoSink, TSymbolTable& symbolTable)
alokp@chromium.org07620a52010-09-23 17:53:56 +000034{
35 TIntermediate intermediate(infoSink);
36 TExtensionBehavior extBehavior;
zmo@google.com09c323a2011-08-12 18:22:25 +000037 InitExtensionBehavior(resources, extBehavior);
zmo@google.comdc4b4f82011-06-17 00:42:53 +000038 // The builtins deliberately don't specify precisions for the function
39 // arguments and return types. For that reason we don't try to check them.
40 TParseContext parseContext(symbolTable, extBehavior, intermediate, type, spec, 0, false, NULL, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +000041 parseContext.fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +000042
43 GlobalParseContext = &parseContext;
44
alokp@chromium.org07620a52010-09-23 17:53:56 +000045 assert(symbolTable.isEmpty());
46 //
47 // Parse the built-ins. This should only happen once per
48 // language symbol table.
49 //
50 // Push the symbol table to give it an initial scope. This
51 // push should not have a corresponding pop, so that built-ins
52 // are preserved, and the test for an empty table fails.
53 //
54 symbolTable.push();
alokp@chromium.org07620a52010-09-23 17:53:56 +000055
56 for (TBuiltInStrings::const_iterator i = builtInStrings.begin(); i != builtInStrings.end(); ++i)
57 {
alokp@chromium.org570bfc72010-09-24 17:19:25 +000058 const char* builtInShaders = i->c_str();
59 int builtInLengths = static_cast<int>(i->size());
60 if (builtInLengths <= 0)
61 continue;
alokp@chromium.org07620a52010-09-23 17:53:56 +000062
alokp@chromium.org044a5cf2010-11-12 15:42:16 +000063 if (PaParseStrings(1, &builtInShaders, &builtInLengths, &parseContext) != 0)
alokp@chromium.org07620a52010-09-23 17:53:56 +000064 {
65 infoSink.info.message(EPrefixInternalError, "Unable to parse built-ins");
66 return false;
67 }
68 }
69
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000070 IdentifyBuiltIns(type, spec, resources, symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +000071
alokp@chromium.org07620a52010-09-23 17:53:56 +000072 return true;
73}
74
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000075class TScopedPoolAllocator {
76public:
77 TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop)
78 : mAllocator(allocator), mPushPopAllocator(pushPop) {
79 if (mPushPopAllocator) mAllocator->push();
80 SetGlobalPoolAllocator(mAllocator);
81 }
82 ~TScopedPoolAllocator() {
83 SetGlobalPoolAllocator(NULL);
84 if (mPushPopAllocator) mAllocator->pop();
85 }
86
87private:
88 TPoolAllocator* mAllocator;
89 bool mPushPopAllocator;
90};
91} // namespace
92
93TShHandleBase::TShHandleBase() {
94 allocator.push();
95 SetGlobalPoolAllocator(&allocator);
96}
97
98TShHandleBase::~TShHandleBase() {
99 SetGlobalPoolAllocator(NULL);
100 allocator.popAll();
101}
102
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000103TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
104 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000105 shaderSpec(spec),
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{
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000110 longNameMap = LongNameMap::GetInstance();
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000111}
112
113TCompiler::~TCompiler()
114{
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000115 ASSERT(longNameMap);
116 longNameMap->Release();
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000117}
118
119bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000120{
gman@chromium.org8d804792012-10-17 21:33:48 +0000121 maxUniformVectors = (shaderType == SH_VERTEX_SHADER) ?
122 resources.MaxVertexUniformVectors :
123 resources.MaxFragmentUniformVectors;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000124 TScopedPoolAllocator scopedAlloc(&allocator, false);
125
alokp@chromium.org07620a52010-09-23 17:53:56 +0000126 // Generate built-in symbol table.
127 if (!InitBuiltInSymbolTable(resources))
128 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000129 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000130 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000131
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000132 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
133 clampingStrategy = resources.ArrayIndexClampingStrategy;
134
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000135 hashFunction = resources.HashFunction;
136
alokp@chromium.org07620a52010-09-23 17:53:56 +0000137 return true;
138}
139
140bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000141 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000142 int compileOptions)
143{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000144 TScopedPoolAllocator scopedAlloc(&allocator, true);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000145 clearResults();
146
147 if (numStrings == 0)
148 return true;
149
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000150 // If compiling for WebGL, validate loop and indexing as well.
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000151 if (isWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000152 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000153
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000154 // First string is path of source file if flag is set. The actual source follows.
155 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000156 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000157 if (compileOptions & SH_SOURCE_PATH)
158 {
159 sourcePath = shaderStrings[0];
160 ++firstSource;
161 }
162
alokp@chromium.org07620a52010-09-23 17:53:56 +0000163 TIntermediate intermediate(infoSink);
164 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000165 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000166 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000167 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000168 GlobalParseContext = &parseContext;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000169
170 // We preserve symbols at the built-in level from compile-to-compile.
171 // Start pushing the user-defined symbols at global level.
172 symbolTable.push();
173 if (!symbolTable.atGlobalLevel())
174 infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
175
176 // Parse shader.
177 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000178 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000179 (parseContext.treeRoot != NULL);
180 if (success) {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000181 TIntermNode* root = parseContext.treeRoot;
182 success = intermediate.postProcess(root);
183
zmo@google.comb1762df2011-07-30 02:04:23 +0000184 if (success)
185 success = detectRecursion(root);
186
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000187 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
188 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000189
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000190 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000191 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000192
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000193 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
194 rewriteCSSShader(root);
195
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000196 // Unroll for-loop markup needs to happen after validateLimitations pass.
197 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
198 ForLoopUnroll::MarkForLoopsWithIntegerIndicesForUnrolling(root);
199
zmo@google.com32e97312011-08-24 01:03:11 +0000200 // Built-in function emulation needs to happen after validateLimitations pass.
201 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
202 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
203
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000204 // Clamping uniform array bounds needs to happen after validateLimitations pass.
205 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
206 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
207
zmo@google.comfd747b82011-04-23 01:30:07 +0000208 // Call mapLongVariableNames() before collectAttribsUniforms() so in
209 // collectAttribsUniforms() we already have the mapped symbol names and
210 // we could composite mapped and original variable names.
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000211 // Also, if we hash all the names, then no need to do this for long names.
212 if (success && (compileOptions & SH_MAP_LONG_VARIABLE_NAMES) && hashFunction == NULL)
zmo@google.comfd747b82011-04-23 01:30:07 +0000213 mapLongVariableNames(root);
214
gman@chromium.org8d804792012-10-17 21:33:48 +0000215 if (success && (compileOptions & SH_ATTRIBUTES_UNIFORMS)) {
zmo@google.comfd747b82011-04-23 01:30:07 +0000216 collectAttribsUniforms(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000217 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS) {
218 success = enforcePackingRestrictions();
219 if (!success) {
220 infoSink.info.message(EPrefixError, "too many uniforms");
221 }
222 }
223 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000224
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000225 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000226 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000227
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000228 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000229 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000230 }
231
232 // Cleanup memory.
233 intermediate.remove(parseContext.treeRoot);
234 // Ensure symbol table is returned to the built-in level,
235 // throwing away all but the built-ins.
236 while (!symbolTable.atBuiltInLevel())
237 symbolTable.pop();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000238
239 return success;
240}
241
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000242bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000243{
244 TBuiltIns builtIns;
245
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000246 builtIns.initialize(shaderType, shaderSpec, resources);
247 return InitializeSymbolTable(builtIns.getBuiltInStrings(),
248 shaderType, shaderSpec, resources, infoSink, symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000249}
250
251void TCompiler::clearResults()
252{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000253 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000254 infoSink.info.erase();
255 infoSink.obj.erase();
256 infoSink.debug.erase();
257
258 attribs.clear();
259 uniforms.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000260
261 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000262
263 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000264}
265
zmo@google.comb1762df2011-07-30 02:04:23 +0000266bool TCompiler::detectRecursion(TIntermNode* root)
267{
268 DetectRecursion detect;
269 root->traverse(&detect);
270 switch (detect.detectRecursion()) {
271 case DetectRecursion::kErrorNone:
272 return true;
273 case DetectRecursion::kErrorMissingMain:
274 infoSink.info.message(EPrefixError, "Missing main()");
275 return false;
276 case DetectRecursion::kErrorRecursion:
277 infoSink.info.message(EPrefixError, "Function recursion detected");
278 return false;
279 default:
280 UNREACHABLE();
281 return false;
282 }
283}
284
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000285void TCompiler::rewriteCSSShader(TIntermNode* root)
286{
287 RenameFunction renamer("main(", "css_main(");
288 root->traverse(&renamer);
289}
290
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000291bool TCompiler::validateLimitations(TIntermNode* root) {
292 ValidateLimitations validate(shaderType, infoSink.info);
293 root->traverse(&validate);
294 return validate.numErrors() == 0;
295}
296
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000297bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000298{
299 if (shaderSpec != SH_WEBGL_SPEC) {
300 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
301 return false;
302 }
303
304 if (shaderType == SH_FRAGMENT_SHADER) {
305 TDependencyGraph graph(root);
306
307 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000308 bool success = enforceFragmentShaderTimingRestrictions(graph);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000309
310 // Then, output the dependency graph.
311 if (outputGraph) {
312 TDependencyGraphOutput output(infoSink.info);
313 output.outputAllSpanningTrees(graph);
314 }
315
316 return success;
317 }
318 else {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000319 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000320 }
321}
322
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000323bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000324{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000325 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000326 restrictor.enforceRestrictions(graph);
327 return restrictor.numErrors() == 0;
328}
329
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000330bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000331{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000332 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000333 restrictor.enforceRestrictions(root);
334 return restrictor.numErrors() == 0;
335}
336
alokp@chromium.org07620a52010-09-23 17:53:56 +0000337void TCompiler::collectAttribsUniforms(TIntermNode* root)
338{
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000339 CollectAttribsUniforms collect(attribs, uniforms, hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000340 root->traverse(&collect);
341}
zmo@google.comfd747b82011-04-23 01:30:07 +0000342
gman@chromium.org8d804792012-10-17 21:33:48 +0000343bool TCompiler::enforcePackingRestrictions()
344{
345 VariablePacker packer;
346 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
347}
348
zmo@google.comfd747b82011-04-23 01:30:07 +0000349void TCompiler::mapLongVariableNames(TIntermNode* root)
350{
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000351 ASSERT(longNameMap);
352 MapLongVariableNames map(longNameMap);
zmo@google.com9996b8e2012-01-19 01:43:55 +0000353 root->traverse(&map);
zmo@google.comfd747b82011-04-23 01:30:07 +0000354}
355
356int TCompiler::getMappedNameMaxLength() const
357{
kbr@chromium.org22152112011-10-26 01:18:28 +0000358 return MAX_SHORTENED_IDENTIFIER_SIZE + 1;
zmo@google.comfd747b82011-04-23 01:30:07 +0000359}
zmo@google.com5601ea02011-06-10 18:23:25 +0000360
361const TExtensionBehavior& TCompiler::getExtensionBehavior() const
362{
363 return extensionBehavior;
364}
zmo@google.com32e97312011-08-24 01:03:11 +0000365
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000366const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
367{
368 return arrayBoundsClamper;
369}
370
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000371ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
372{
373 return clampingStrategy;
374}
375
376const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
377{
378 return builtInFunctionEmulator;
379}