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/SymbolTable.h"
22 #include "compiler/translator/VariableInfo.h"
23 #include "third_party/compiler/ArrayBoundsClamper.h"
26 class TDependencyGraph;
30 // Helper function to identify specs that are based on the WebGL spec,
31 // like the CSS Shaders spec.
33 bool IsWebGLBasedSpec(ShShaderSpec spec);
36 // The base class used to back handles returned to the driver.
41 virtual ~TShHandleBase();
42 virtual TCompiler* getAsCompiler() { return 0; }
43 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
46 // Memory allocator. Allocates and tracks memory required by the compiler.
47 // Deallocates all memory when compiler is destructed.
48 TPoolAllocator allocator;
52 // The base class for the machine dependent compiler to derive from
53 // for managing object code from the compile.
55 class TCompiler : public TShHandleBase
58 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
60 virtual TCompiler* getAsCompiler() { return this; }
62 bool Init(const ShBuiltInResources& resources);
63 bool compile(const char* const shaderStrings[],
67 // Get results of the last compilation.
68 int getShaderVersion() const { return shaderVersion; }
69 TInfoSink& getInfoSink() { return infoSink; }
71 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
72 const std::vector<sh::Attribute> &getOutputVariables() const { return outputVariables; }
73 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
74 const std::vector<sh::ShaderVariable> &getExpandedUniforms() const { return expandedUniforms; }
75 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
76 const std::vector<sh::ShaderVariable> &getExpandedVaryings() const { return expandedVaryings; }
77 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
79 ShHashFunction64 getHashFunction() const { return hashFunction; }
80 NameMap& getNameMap() { return nameMap; }
81 TSymbolTable& getSymbolTable() { return symbolTable; }
82 ShShaderSpec getShaderSpec() const { return shaderSpec; }
83 ShShaderOutput getOutputType() const { return outputType; }
84 std::string getBuiltInResourcesString() const { return builtInResourcesString; }
86 // Get the resources set by InitBuiltInSymbolTable
87 const ShBuiltInResources& getResources() const;
90 sh::GLenum getShaderType() const { return shaderType; }
91 // Initialize symbol-table with built-in symbols.
92 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
93 // Compute the string representation of the built-in resources
94 void setResourceString();
95 // Clears the results from the previous compilation.
97 // Return true if function recursion is detected or call depth exceeded.
98 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth);
99 // Returns true if a program has no conflicting or missing fragment outputs
100 bool validateOutputs(TIntermNode* root);
101 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
102 void rewriteCSSShader(TIntermNode* root);
103 // Returns true if the given shader does not exceed the minimum
104 // functionality mandated in GLSL 1.0 spec Appendix A.
105 bool validateLimitations(TIntermNode* root);
106 // Collect info for all attribs, uniforms, varyings.
107 void collectVariables(TIntermNode* root);
108 // Translate to object code.
109 virtual void translate(TIntermNode* root) = 0;
110 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
111 // Appendix A, section 7, the shader does not use too many uniforms.
112 bool enforcePackingRestrictions();
113 // Insert statements to initialize varyings without static use in the beginning
114 // of main(). It is to work around a Mac driver where such varyings in a vertex
115 // shader may be optimized out incorrectly at compile time, causing a link failure.
116 // This function should only be applied to vertex shaders.
117 void initializeVaryingsWithoutStaticUse(TIntermNode* root);
118 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
119 // It is to work around a Linux driver bug where missing this causes compile failure
120 // while spec says it is allowed.
121 // This function should only be applied to vertex shaders.
122 void initializeGLPosition(TIntermNode* root);
123 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
124 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
125 // Returns true if the shader does not use samplers.
126 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
127 // Returns true if the shader does not use sampler dependent values to affect control
128 // flow or in operations whose time can depend on the input values.
129 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
130 // Return true if the maximum expression complexity is below the limit.
131 bool limitExpressionComplexity(TIntermNode* root);
132 // Get built-in extensions with default behavior.
133 const TExtensionBehavior& getExtensionBehavior() const;
135 const ArrayBoundsClamper& getArrayBoundsClamper() const;
136 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
137 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
139 std::vector<sh::Attribute> attributes;
140 std::vector<sh::Attribute> outputVariables;
141 std::vector<sh::Uniform> uniforms;
142 std::vector<sh::ShaderVariable> expandedUniforms;
143 std::vector<sh::Varying> varyings;
144 std::vector<sh::ShaderVariable> expandedVaryings;
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;
180 // This is the interface between the machine independent code
181 // and the machine dependent code.
183 // The machine dependent code should derive from the classes
184 // above. Then Construct*() and Delete*() will create and
185 // destroy the machine dependent objects, which contain the
186 // above machine independent information.
188 TCompiler* ConstructCompiler(
189 sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
190 void DeleteCompiler(TCompiler*);
192 #endif // _SHHANDLE_INCLUDED_