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 _SHHANDLE_INCLUDED_ 8 #define _SHHANDLE_INCLUDED_ 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/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" 24 25 class TCompiler; 26 class TDependencyGraph; 27 class TranslatorHLSL; 28 29 // 30 // Helper function to identify specs that are based on the WebGL spec, 31 // like the CSS Shaders spec. 32 // 33 bool IsWebGLBasedSpec(ShShaderSpec spec); 34 35 // 36 // The base class used to back handles returned to the driver. 37 // 38 class TShHandleBase { 39 public: 40 TShHandleBase(); 41 virtual ~TShHandleBase(); getAsCompiler()42 virtual TCompiler* getAsCompiler() { return 0; } getAsTranslatorHLSL()43 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; } 44 45 protected: 46 // Memory allocator. Allocates and tracks memory required by the compiler. 47 // Deallocates all memory when compiler is destructed. 48 TPoolAllocator allocator; 49 }; 50 51 // 52 // The base class for the machine dependent compiler to derive from 53 // for managing object code from the compile. 54 // 55 class TCompiler : public TShHandleBase { 56 public: 57 TCompiler(ShShaderType type, ShShaderSpec spec, ShShaderOutput output); 58 virtual ~TCompiler(); getAsCompiler()59 virtual TCompiler* getAsCompiler() { return this; } 60 61 bool Init(const ShBuiltInResources& resources); 62 bool compile(const char* const shaderStrings[], 63 size_t numStrings, 64 int compileOptions); 65 66 // Get results of the last compilation. getShaderVersion()67 int getShaderVersion() const { return shaderVersion; } getInfoSink()68 TInfoSink& getInfoSink() { return infoSink; } getAttribs()69 const TVariableInfoList& getAttribs() const { return attribs; } getUniforms()70 const TVariableInfoList& getUniforms() const { return uniforms; } getVaryings()71 const TVariableInfoList& getVaryings() const { return varyings; } 72 getHashFunction()73 ShHashFunction64 getHashFunction() const { return hashFunction; } getNameMap()74 NameMap& getNameMap() { return nameMap; } getSymbolTable()75 TSymbolTable& getSymbolTable() { return symbolTable; } getShaderSpec()76 ShShaderSpec getShaderSpec() const { return shaderSpec; } getOutputType()77 ShShaderOutput getOutputType() const { return outputType; } getBuiltInResourcesString()78 std::string getBuiltInResourcesString() const { return builtInResourcesString; } 79 80 protected: getShaderType()81 ShShaderType getShaderType() const { return shaderType; } 82 // Initialize symbol-table with built-in symbols. 83 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources); 84 // Compute the string representation of the built-in resources 85 void setResourceString(); 86 // Clears the results from the previous compilation. 87 void clearResults(); 88 // Return true if function recursion is detected or call depth exceeded. 89 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth); 90 // Returns true if a program has no conflicting or missing fragment outputs 91 bool validateOutputs(TIntermNode* root); 92 // Rewrites a shader's intermediate tree according to the CSS Shaders spec. 93 void rewriteCSSShader(TIntermNode* root); 94 // Returns true if the given shader does not exceed the minimum 95 // functionality mandated in GLSL 1.0 spec Appendix A. 96 bool validateLimitations(TIntermNode* root); 97 // Collect info for all attribs, uniforms, varyings. 98 void collectVariables(TIntermNode* root); 99 // Translate to object code. 100 virtual void translate(TIntermNode* root) = 0; 101 // Returns true if, after applying the packing rules in the GLSL 1.017 spec 102 // Appendix A, section 7, the shader does not use too many uniforms. 103 bool enforcePackingRestrictions(); 104 // Insert statements to initialize varyings without static use in the beginning 105 // of main(). It is to work around a Mac driver where such varyings in a vertex 106 // shader may be optimized out incorrectly at compile time, causing a link failure. 107 // This function should only be applied to vertex shaders. 108 void initializeVaryingsWithoutStaticUse(TIntermNode* root); 109 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main(). 110 // It is to work around a Linux driver bug where missing this causes compile failure 111 // while spec says it is allowed. 112 // This function should only be applied to vertex shaders. 113 void initializeGLPosition(TIntermNode* root); 114 // Returns true if the shader passes the restrictions that aim to prevent timing attacks. 115 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph); 116 // Returns true if the shader does not use samplers. 117 bool enforceVertexShaderTimingRestrictions(TIntermNode* root); 118 // Returns true if the shader does not use sampler dependent values to affect control 119 // flow or in operations whose time can depend on the input values. 120 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph); 121 // Return true if the maximum expression complexity is below the limit. 122 bool limitExpressionComplexity(TIntermNode* root); 123 // Get built-in extensions with default behavior. 124 const TExtensionBehavior& getExtensionBehavior() const; 125 // Get the resources set by InitBuiltInSymbolTable 126 const ShBuiltInResources& getResources() const; 127 128 const ArrayBoundsClamper& getArrayBoundsClamper() const; 129 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const; 130 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const; 131 132 private: 133 ShShaderType shaderType; 134 ShShaderSpec shaderSpec; 135 ShShaderOutput outputType; 136 137 int maxUniformVectors; 138 int maxExpressionComplexity; 139 int maxCallStackDepth; 140 141 ShBuiltInResources compileResources; 142 std::string builtInResourcesString; 143 144 // Built-in symbol table for the given language, spec, and resources. 145 // It is preserved from compile-to-compile. 146 TSymbolTable symbolTable; 147 // Built-in extensions with default behavior. 148 TExtensionBehavior extensionBehavior; 149 bool fragmentPrecisionHigh; 150 151 ArrayBoundsClamper arrayBoundsClamper; 152 ShArrayIndexClampingStrategy clampingStrategy; 153 BuiltInFunctionEmulator builtInFunctionEmulator; 154 155 // Results of compilation. 156 int shaderVersion; 157 TInfoSink infoSink; // Output sink. 158 TVariableInfoList attribs; // Active attributes in the compiled shader. 159 TVariableInfoList uniforms; // Active uniforms in the compiled shader. 160 TVariableInfoList varyings; // Varyings in the compiled shader. 161 162 // name hashing. 163 ShHashFunction64 hashFunction; 164 NameMap nameMap; 165 }; 166 167 // 168 // This is the interface between the machine independent code 169 // and the machine dependent code. 170 // 171 // The machine dependent code should derive from the classes 172 // above. Then Construct*() and Delete*() will create and 173 // destroy the machine dependent objects, which contain the 174 // above machine independent information. 175 // 176 TCompiler* ConstructCompiler( 177 ShShaderType type, ShShaderSpec spec, ShShaderOutput output); 178 void DeleteCompiler(TCompiler*); 179 180 #endif // _SHHANDLE_INCLUDED_ 181