blob: 436e49f4a1a0102b4203d682f27af19d0d7d1d05 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Nicolas Capens6407fe82015-02-10 16:27:49 -05007#include "Compiler.h"
8
Nicolas Capenscc863da2015-01-21 15:50:55 -05009#include "AnalyzeCallDepth.h"
10#include "Initialize.h"
11#include "InitializeParseContext.h"
Nicolas Capens6407fe82015-02-10 16:27:49 -050012#include "InitializeGlobals.h"
Nicolas Capenscc863da2015-01-21 15:50:55 -050013#include "ParseHelper.h"
Nicolas Capenscc863da2015-01-21 15:50:55 -050014#include "ValidateLimitations.h"
John Bauman66b8ab22014-05-06 15:57:45 -040015
John Baumand4ae8632014-05-06 16:18:33 -040016namespace
John Bauman66b8ab22014-05-06 15:57:45 -040017{
John Bauman66b8ab22014-05-06 15:57:45 -040018class TScopedPoolAllocator {
19public:
20 TScopedPoolAllocator(TPoolAllocator* allocator, bool pushPop)
21 : mAllocator(allocator), mPushPopAllocator(pushPop) {
22 if (mPushPopAllocator) mAllocator->push();
23 SetGlobalPoolAllocator(mAllocator);
24 }
25 ~TScopedPoolAllocator() {
26 SetGlobalPoolAllocator(NULL);
27 if (mPushPopAllocator) mAllocator->pop();
28 }
29
30private:
31 TPoolAllocator* mAllocator;
32 bool mPushPopAllocator;
33};
34} // namespace
35
Nicolas Capens6407fe82015-02-10 16:27:49 -050036//
37// Initialize built-in resources with minimum expected values.
38//
39ShBuiltInResources::ShBuiltInResources()
40{
41 // Constants.
42 MaxVertexAttribs = 8;
43 MaxVertexUniformVectors = 128;
44 MaxVaryingVectors = 8;
45 MaxVertexTextureImageUnits = 0;
46 MaxCombinedTextureImageUnits = 8;
47 MaxTextureImageUnits = 8;
48 MaxFragmentUniformVectors = 16;
49 MaxDrawBuffers = 1;
50
51 // Extensions.
52 OES_standard_derivatives = 0;
53 OES_fragment_precision_high = 0;
54 OES_EGL_image_external = 0;
55
56 MaxCallStackDepth = UINT_MAX;
57}
58
John Bauman66b8ab22014-05-06 15:57:45 -040059TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
60 : shaderType(type),
John Baumand4ae8632014-05-06 16:18:33 -040061 shaderSpec(spec),
62 maxCallStackDepth(UINT_MAX)
John Bauman66b8ab22014-05-06 15:57:45 -040063{
Nicolas Capens7a8ccc42015-02-10 15:42:31 -050064 allocator.push();
65 SetGlobalPoolAllocator(&allocator);
John Bauman66b8ab22014-05-06 15:57:45 -040066}
67
68TCompiler::~TCompiler()
69{
Nicolas Capens7a8ccc42015-02-10 15:42:31 -050070 SetGlobalPoolAllocator(NULL);
71 allocator.popAll();
John Bauman66b8ab22014-05-06 15:57:45 -040072}
73
74bool TCompiler::Init(const ShBuiltInResources& resources)
75{
John Baumand4ae8632014-05-06 16:18:33 -040076 maxCallStackDepth = resources.MaxCallStackDepth;
John Bauman66b8ab22014-05-06 15:57:45 -040077 TScopedPoolAllocator scopedAlloc(&allocator, false);
78
79 // Generate built-in symbol table.
80 if (!InitBuiltInSymbolTable(resources))
81 return false;
82 InitExtensionBehavior(resources, extensionBehavior);
83
84 return true;
85}
86
87bool TCompiler::compile(const char* const shaderStrings[],
88 const int numStrings,
89 int compileOptions)
90{
91 TScopedPoolAllocator scopedAlloc(&allocator, true);
92 clearResults();
93
94 if (numStrings == 0)
95 return true;
96
97 // If compiling for WebGL, validate loop and indexing as well.
98 if (shaderSpec == SH_WEBGL_SPEC)
99 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
100
101 // First string is path of source file if flag is set. The actual source follows.
102 const char* sourcePath = NULL;
103 int firstSource = 0;
104 if (compileOptions & SH_SOURCE_PATH)
105 {
106 sourcePath = shaderStrings[0];
107 ++firstSource;
108 }
109
110 TIntermediate intermediate(infoSink);
111 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
112 shaderType, shaderSpec, compileOptions, true,
113 sourcePath, infoSink);
Nicolas Capens978ddc52014-11-11 12:42:08 -0500114 SetGlobalParseContext(&parseContext);
John Bauman66b8ab22014-05-06 15:57:45 -0400115
116 // We preserve symbols at the built-in level from compile-to-compile.
117 // Start pushing the user-defined symbols at global level.
118 symbolTable.push();
119 if (!symbolTable.atGlobalLevel())
120 infoSink.info.message(EPrefixInternalError, "Wrong symbol table level");
121
122 // Parse shader.
123 bool success =
124 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
125 (parseContext.treeRoot != NULL);
126 if (success) {
127 TIntermNode* root = parseContext.treeRoot;
128 success = intermediate.postProcess(root);
129
130 if (success)
John Baumand4ae8632014-05-06 16:18:33 -0400131 success = validateCallDepth(root, infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400132
133 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
134 success = validateLimitations(root);
135
John Bauman66b8ab22014-05-06 15:57:45 -0400136 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
137 intermediate.outputTree(root);
138
139 if (success && (compileOptions & SH_OBJECT_CODE))
Nicolas Capens014b9a62014-10-15 10:28:29 -0400140 success = translate(root);
John Bauman66b8ab22014-05-06 15:57:45 -0400141 }
142
John Bauman66b8ab22014-05-06 15:57:45 -0400143 // Ensure symbol table is returned to the built-in level,
144 // throwing away all but the built-ins.
145 while (!symbolTable.atBuiltInLevel())
146 symbolTable.pop();
147
148 return success;
149}
150
John Baumand4ae8632014-05-06 16:18:33 -0400151bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
John Bauman66b8ab22014-05-06 15:57:45 -0400152{
John Baumand4ae8632014-05-06 16:18:33 -0400153 assert(symbolTable.isEmpty());
154
155 //
156 // Push the symbol table to give it an initial scope. This
157 // push should not have a corresponding pop, so that built-ins
158 // are preserved, and the test for an empty table fails.
159 //
160 symbolTable.push();
John Bauman66b8ab22014-05-06 15:57:45 -0400161
John Baumand4ae8632014-05-06 16:18:33 -0400162 TPublicType integer;
163 integer.type = EbtInt;
164 integer.size = 1;
165 integer.matrix = false;
166 integer.array = false;
167
168 TPublicType floatingPoint;
169 floatingPoint.type = EbtFloat;
170 floatingPoint.size = 1;
171 floatingPoint.matrix = false;
172 floatingPoint.array = false;
173
174 switch(shaderType)
175 {
176 case SH_FRAGMENT_SHADER:
177 symbolTable.setDefaultPrecision(integer, EbpMedium);
178 break;
179 case SH_VERTEX_SHADER:
180 symbolTable.setDefaultPrecision(integer, EbpHigh);
181 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
182 break;
183 default: assert(false && "Language not supported");
184 }
185
186 InsertBuiltInFunctions(shaderType, resources, symbolTable);
187
188 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
189
190 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400191}
192
193void TCompiler::clearResults()
194{
195 infoSink.info.erase();
196 infoSink.obj.erase();
197 infoSink.debug.erase();
John Bauman66b8ab22014-05-06 15:57:45 -0400198}
199
John Baumand4ae8632014-05-06 16:18:33 -0400200bool TCompiler::validateCallDepth(TIntermNode *root, TInfoSink &infoSink)
John Bauman66b8ab22014-05-06 15:57:45 -0400201{
John Baumand4ae8632014-05-06 16:18:33 -0400202 AnalyzeCallDepth validator(root);
203
204 unsigned int depth = validator.analyzeCallDepth();
205
206 if(depth == 0)
207 {
208 infoSink.info.prefix(EPrefixError);
209 infoSink.info << "Missing main()";
210 return false;
211 }
212 else if(depth == UINT_MAX)
213 {
214 infoSink.info.prefix(EPrefixError);
215 infoSink.info << "Function recursion detected";
216 return false;
217 }
218 else if(depth > maxCallStackDepth)
219 {
220 infoSink.info.prefix(EPrefixError);
221 infoSink.info << "Function call stack too deep";
222 return false;
223 }
224
225 return true;
John Bauman66b8ab22014-05-06 15:57:45 -0400226}
227
228bool TCompiler::validateLimitations(TIntermNode* root) {
229 ValidateLimitations validate(shaderType, infoSink.info);
230 root->traverse(&validate);
231 return validate.numErrors() == 0;
232}
233
John Bauman66b8ab22014-05-06 15:57:45 -0400234const TExtensionBehavior& TCompiler::getExtensionBehavior() const
235{
236 return extensionBehavior;
237}
Nicolas Capens6407fe82015-02-10 16:27:49 -0500238
239bool InitCompilerGlobals()
240{
241 if(!InitializePoolIndex())
242 {
243 assert(0 && "InitCompilerGlobals(): Failed to initalize global pool");
244 return false;
245 }
246
247 if(!InitializeParseContextIndex())
248 {
249 assert(0 && "InitCompilerGlobals(): Failed to initalize parse context");
250 return false;
251 }
252
253 return true;
254}
255
256void FreeCompilerGlobals()
257{
258 FreeParseContextIndex();
259 FreePoolIndex();
260}