Line data Source code
1 : //
2 : // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 : //
6 :
7 : #ifndef COMPILER_TRANSLATOR_COMPILER_H_
8 : #define COMPILER_TRANSLATOR_COMPILER_H_
9 :
10 : //
11 : // Machine independent part of the compiler private objects
12 : // sent as ShHandle to the driver.
13 : //
14 : // This should not be included by driver code.
15 : //
16 :
17 : #include "compiler/translator/BuiltInFunctionEmulator.h"
18 : #include "compiler/translator/CallDAG.h"
19 : #include "compiler/translator/ExtensionBehavior.h"
20 : #include "compiler/translator/HashNames.h"
21 : #include "compiler/translator/InfoSink.h"
22 : #include "compiler/translator/Pragma.h"
23 : #include "compiler/translator/SymbolTable.h"
24 : #include "compiler/translator/VariableInfo.h"
25 : #include "third_party/compiler/ArrayBoundsClamper.h"
26 :
27 : namespace sh
28 : {
29 :
30 : class TCompiler;
31 : #ifdef ANGLE_ENABLE_HLSL
32 : class TranslatorHLSL;
33 : #endif // ANGLE_ENABLE_HLSL
34 :
35 : //
36 : // Helper function to identify specs that are based on the WebGL spec.
37 : //
38 : bool IsWebGLBasedSpec(ShShaderSpec spec);
39 :
40 : //
41 : // Helper function to check if the shader type is GLSL.
42 : //
43 : bool IsGLSL130OrNewer(ShShaderOutput output);
44 : bool IsGLSL420OrNewer(ShShaderOutput output);
45 : bool IsGLSL410OrOlder(ShShaderOutput output);
46 :
47 : //
48 : // Helper function to check if the invariant qualifier can be removed.
49 : //
50 : bool RemoveInvariant(sh::GLenum shaderType,
51 : int shaderVersion,
52 : ShShaderOutput outputType,
53 : ShCompileOptions compileOptions);
54 :
55 : //
56 : // The base class used to back handles returned to the driver.
57 : //
58 : class TShHandleBase {
59 : public:
60 : TShHandleBase();
61 : virtual ~TShHandleBase();
62 0 : virtual TCompiler* getAsCompiler() { return 0; }
63 : #ifdef ANGLE_ENABLE_HLSL
64 0 : virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
65 : #endif // ANGLE_ENABLE_HLSL
66 :
67 : protected:
68 : // Memory allocator. Allocates and tracks memory required by the compiler.
69 : // Deallocates all memory when compiler is destructed.
70 : TPoolAllocator allocator;
71 : };
72 :
73 : //
74 : // The base class for the machine dependent compiler to derive from
75 : // for managing object code from the compile.
76 : //
77 : class TCompiler : public TShHandleBase
78 : {
79 : public:
80 : TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
81 : ~TCompiler() override;
82 0 : TCompiler *getAsCompiler() override { return this; }
83 :
84 : bool Init(const ShBuiltInResources& resources);
85 :
86 : // compileTreeForTesting should be used only when tests require access to
87 : // the AST. Users of this function need to manually manage the global pool
88 : // allocator. Returns nullptr whenever there are compilation errors.
89 : TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
90 : size_t numStrings,
91 : ShCompileOptions compileOptions);
92 :
93 : bool compile(const char *const shaderStrings[],
94 : size_t numStrings,
95 : ShCompileOptions compileOptions);
96 :
97 : // Get results of the last compilation.
98 0 : int getShaderVersion() const { return shaderVersion; }
99 0 : TInfoSink& getInfoSink() { return infoSink; }
100 :
101 0 : bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
102 0 : const sh::WorkGroupSize &getComputeShaderLocalSize() { return mComputeShaderLocalSize; }
103 :
104 : // Clears the results from the previous compilation.
105 : void clearResults();
106 :
107 0 : const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
108 0 : const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
109 0 : const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
110 0 : const std::vector<sh::Varying> &getVaryings() const { return varyings; }
111 0 : const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
112 :
113 0 : ShHashFunction64 getHashFunction() const { return hashFunction; }
114 0 : NameMap& getNameMap() { return nameMap; }
115 0 : TSymbolTable& getSymbolTable() { return symbolTable; }
116 : ShShaderSpec getShaderSpec() const { return shaderSpec; }
117 0 : ShShaderOutput getOutputType() const { return outputType; }
118 0 : const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
119 :
120 : bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
121 :
122 : // Get the resources set by InitBuiltInSymbolTable
123 : const ShBuiltInResources& getResources() const;
124 :
125 : protected:
126 0 : sh::GLenum getShaderType() const { return shaderType; }
127 : // Initialize symbol-table with built-in symbols.
128 : bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
129 : // Compute the string representation of the built-in resources
130 : void setResourceString();
131 : // Return false if the call depth is exceeded.
132 : bool checkCallDepth();
133 : // Returns true if a program has no conflicting or missing fragment outputs
134 : bool validateOutputs(TIntermNode* root);
135 : // Returns true if the given shader does not exceed the minimum
136 : // functionality mandated in GLSL 1.0 spec Appendix A.
137 : bool validateLimitations(TIntermNode* root);
138 : // Add emulated functions to the built-in function emulator.
139 0 : virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
140 0 : ShCompileOptions compileOptions){};
141 : // Translate to object code.
142 : virtual void translate(TIntermNode *root, ShCompileOptions compileOptions) = 0;
143 : // Returns true if, after applying the packing rules in the GLSL 1.017 spec
144 : // Appendix A, section 7, the shader does not use too many uniforms.
145 : bool enforcePackingRestrictions();
146 : // Insert statements to reference all members in unused uniform blocks with standard and shared
147 : // layout. This is to work around a Mac driver that treats unused standard/shared
148 : // uniform blocks as inactive.
149 : void useAllMembersInUnusedStandardAndSharedBlocks(TIntermNode *root);
150 : // Insert statements to initialize output variables in the beginning of main().
151 : // This is to avoid undefined behaviors.
152 : void initializeOutputVariables(TIntermNode *root);
153 : // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
154 : // It is to work around a Linux driver bug where missing this causes compile failure
155 : // while spec says it is allowed.
156 : // This function should only be applied to vertex shaders.
157 : void initializeGLPosition(TIntermNode* root);
158 : // Return true if the maximum expression complexity is below the limit.
159 : bool limitExpressionComplexity(TIntermNode* root);
160 : // Get built-in extensions with default behavior.
161 : const TExtensionBehavior& getExtensionBehavior() const;
162 : const char *getSourcePath() const;
163 0 : const TPragma& getPragma() const { return mPragma; }
164 : void writePragma(ShCompileOptions compileOptions);
165 0 : unsigned int *getTemporaryIndex() { return &mTemporaryIndex; }
166 : // Relies on collectVariables having been called.
167 : bool isVaryingDefined(const char *varyingName);
168 :
169 : const ArrayBoundsClamper& getArrayBoundsClamper() const;
170 : ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
171 : const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
172 :
173 : virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
174 : virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
175 :
176 : bool wereVariablesCollected() const;
177 : std::vector<sh::Attribute> attributes;
178 : std::vector<sh::OutputVariable> outputVariables;
179 : std::vector<sh::Uniform> uniforms;
180 : std::vector<sh::ShaderVariable> expandedUniforms;
181 : std::vector<sh::Varying> varyings;
182 : std::vector<sh::InterfaceBlock> interfaceBlocks;
183 :
184 : private:
185 : // Creates the function call DAG for further analysis, returning false if there is a recursion
186 : bool initCallDag(TIntermNode *root);
187 : // Return false if "main" doesn't exist
188 : bool tagUsedFunctions();
189 : void internalTagUsedFunction(size_t index);
190 :
191 : void initSamplerDefaultPrecision(TBasicType samplerType);
192 :
193 : // Collect info for all attribs, uniforms, varyings.
194 : void collectVariables(TIntermNode *root);
195 :
196 : bool variablesCollected;
197 :
198 : // Removes unused function declarations and prototypes from the AST
199 : class UnusedPredicate;
200 : bool pruneUnusedFunctions(TIntermBlock *root);
201 :
202 : TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
203 : size_t numStrings,
204 : const ShCompileOptions compileOptions);
205 :
206 : sh::GLenum shaderType;
207 : ShShaderSpec shaderSpec;
208 : ShShaderOutput outputType;
209 :
210 : struct FunctionMetadata
211 : {
212 0 : FunctionMetadata()
213 0 : : used(false)
214 : {
215 0 : }
216 : bool used;
217 : };
218 :
219 : CallDAG mCallDag;
220 : std::vector<FunctionMetadata> functionMetadata;
221 :
222 : int maxUniformVectors;
223 : int maxExpressionComplexity;
224 : int maxCallStackDepth;
225 : int maxFunctionParameters;
226 :
227 : ShBuiltInResources compileResources;
228 : std::string builtInResourcesString;
229 :
230 : // Built-in symbol table for the given language, spec, and resources.
231 : // It is preserved from compile-to-compile.
232 : TSymbolTable symbolTable;
233 : // Built-in extensions with default behavior.
234 : TExtensionBehavior extensionBehavior;
235 : bool fragmentPrecisionHigh;
236 :
237 : ArrayBoundsClamper arrayBoundsClamper;
238 : ShArrayIndexClampingStrategy clampingStrategy;
239 : BuiltInFunctionEmulator builtInFunctionEmulator;
240 :
241 : // Results of compilation.
242 : int shaderVersion;
243 : TInfoSink infoSink; // Output sink.
244 : const char *mSourcePath; // Path of source file or NULL
245 :
246 : // compute shader local group size
247 : bool mComputeShaderLocalSizeDeclared;
248 : sh::WorkGroupSize mComputeShaderLocalSize;
249 :
250 : // name hashing.
251 : ShHashFunction64 hashFunction;
252 : NameMap nameMap;
253 :
254 : TPragma mPragma;
255 :
256 : unsigned int mTemporaryIndex;
257 : };
258 :
259 : //
260 : // This is the interface between the machine independent code
261 : // and the machine dependent code.
262 : //
263 : // The machine dependent code should derive from the classes
264 : // above. Then Construct*() and Delete*() will create and
265 : // destroy the machine dependent objects, which contain the
266 : // above machine independent information.
267 : //
268 : TCompiler* ConstructCompiler(
269 : sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
270 : void DeleteCompiler(TCompiler*);
271 :
272 : } // namespace sh
273 :
274 : #endif // COMPILER_TRANSLATOR_COMPILER_H_
|