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.
7 #ifndef _SHHANDLE_INCLUDED_
8 #define _SHHANDLE_INCLUDED_
11 // Machine independent part of the compiler private objects
12 // sent as ShHandle to the driver.
14 // This should not be included by driver code.
17 #include "compiler/translator/BuiltInFunctionEmulator.h"
18 #include "compiler/translator/ExtensionBehavior.h"
19 #include "compiler/translator/HashNames.h"
20 #include "compiler/translator/InfoSink.h"
21 #include "compiler/translator/Pragma.h"
22 #include "compiler/translator/SymbolTable.h"
23 #include "compiler/translator/VariableInfo.h"
24 #include "third_party/compiler/ArrayBoundsClamper.h"
27 class TDependencyGraph;
31 // Helper function to identify specs that are based on the WebGL spec,
32 // like the CSS Shaders spec.
34 bool IsWebGLBasedSpec(ShShaderSpec spec);
37 // The base class used to back handles returned to the driver.
42 virtual ~TShHandleBase();
43 virtual TCompiler* getAsCompiler() { return 0; }
44 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
47 // Memory allocator. Allocates and tracks memory required by the compiler.
48 // Deallocates all memory when compiler is destructed.
49 TPoolAllocator allocator;
53 // The base class for the machine dependent compiler to derive from
54 // for managing object code from the compile.
56 class TCompiler : public TShHandleBase
59 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
61 virtual TCompiler* getAsCompiler() { return this; }
63 bool Init(const ShBuiltInResources& resources);
64 bool compile(const char* const shaderStrings[],
68 // Get results of the last compilation.
69 int getShaderVersion() const { return shaderVersion; }
70 TInfoSink& getInfoSink() { return infoSink; }
72 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
73 const std::vector<sh::Attribute> &getOutputVariables() const { return outputVariables; }
74 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
75 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
76 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
78 ShHashFunction64 getHashFunction() const { return hashFunction; }
79 NameMap& getNameMap() { return nameMap; }
80 TSymbolTable& getSymbolTable() { return symbolTable; }
81 ShShaderSpec getShaderSpec() const { return shaderSpec; }
82 ShShaderOutput getOutputType() const { return outputType; }
83 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
85 // Get the resources set by InitBuiltInSymbolTable
86 const ShBuiltInResources& getResources() const;
89 sh::GLenum getShaderType() const { return shaderType; }
90 // Initialize symbol-table with built-in symbols.
91 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
92 // Compute the string representation of the built-in resources
93 void setResourceString();
94 // Clears the results from the previous compilation.
96 // Return true if function recursion is detected or call depth exceeded.
97 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth);
98 // Returns true if a program has no conflicting or missing fragment outputs
99 bool validateOutputs(TIntermNode* root);
100 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
101 void rewriteCSSShader(TIntermNode* root);
102 // Returns true if the given shader does not exceed the minimum
103 // functionality mandated in GLSL 1.0 spec Appendix A.
104 bool validateLimitations(TIntermNode* root);
105 // Collect info for all attribs, uniforms, varyings.
106 void collectVariables(TIntermNode* root);
107 // Translate to object code.
108 virtual void translate(TIntermNode* root) = 0;
109 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
110 // Appendix A, section 7, the shader does not use too many uniforms.
111 bool enforcePackingRestrictions();
112 // Insert statements to initialize varyings without static use in the beginning
113 // of main(). It is to work around a Mac driver where such varyings in a vertex
114 // shader may be optimized out incorrectly at compile time, causing a link failure.
115 // This function should only be applied to vertex shaders.
116 void initializeVaryingsWithoutStaticUse(TIntermNode* root);
117 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
118 // It is to work around a Linux driver bug where missing this causes compile failure
119 // while spec says it is allowed.
120 // This function should only be applied to vertex shaders.
121 void initializeGLPosition(TIntermNode* root);
122 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
123 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
124 // Returns true if the shader does not use samplers.
125 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
126 // Returns true if the shader does not use sampler dependent values to affect control
127 // flow or in operations whose time can depend on the input values.
128 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
129 // Return true if the maximum expression complexity is below the limit.
130 bool limitExpressionComplexity(TIntermNode* root);
131 // Get built-in extensions with default behavior.
132 const TExtensionBehavior& getExtensionBehavior() const;
133 const TPragma& getPragma() const { return mPragma; }
136 const ArrayBoundsClamper& getArrayBoundsClamper() const;
137 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
138 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
140 std::vector<sh::Attribute> attributes;
141 std::vector<sh::Attribute> outputVariables;
142 std::vector<sh::Uniform> uniforms;
143 std::vector<sh::ShaderVariable> expandedUniforms;
144 std::vector<sh::Varying> varyings;
145 std::vector<sh::InterfaceBlock> interfaceBlocks;
148 sh::GLenum shaderType;
149 ShShaderSpec shaderSpec;
150 ShShaderOutput outputType;
152 int maxUniformVectors;
153 int maxExpressionComplexity;
154 int maxCallStackDepth;
156 ShBuiltInResources compileResources;
157 std::string builtInResourcesString;
159 // Built-in symbol table for the given language, spec, and resources.
160 // It is preserved from compile-to-compile.
161 TSymbolTable symbolTable;
162 // Built-in extensions with default behavior.
163 TExtensionBehavior extensionBehavior;
164 bool fragmentPrecisionHigh;
166 ArrayBoundsClamper arrayBoundsClamper;
167 ShArrayIndexClampingStrategy clampingStrategy;
168 BuiltInFunctionEmulator builtInFunctionEmulator;
170 // Results of compilation.
172 TInfoSink infoSink; // Output sink.
175 ShHashFunction64 hashFunction;
182 // This is the interface between the machine independent code
183 // and the machine dependent code.
185 // The machine dependent code should derive from the classes
186 // above. Then Construct*() and Delete*() will create and
187 // destroy the machine dependent objects, which contain the
188 // above machine independent information.
190 TCompiler* ConstructCompiler(
191 sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
192 void DeleteCompiler(TCompiler*);
194 #endif // _SHHANDLE_INCLUDED_