1 // 2 // Copyright (c) 2002-2012 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 "GLSLANG/ShaderLang.h" 18 19 #include "compiler/BuiltInFunctionEmulator.h" 20 #include "compiler/ExtensionBehavior.h" 21 #include "compiler/HashNames.h" 22 #include "compiler/InfoSink.h" 23 #include "compiler/SymbolTable.h" 24 #include "compiler/VariableInfo.h" 25 #include "third_party/compiler/ArrayBoundsClamper.h" 26 27 class LongNameMap; 28 class TCompiler; 29 class TDependencyGraph; 30 class TranslatorHLSL; 31 32 // 33 // Helper function to identify specs that are based on the WebGL spec, 34 // like the CSS Shaders spec. 35 // 36 bool isWebGLBasedSpec(ShShaderSpec spec); 37 38 // 39 // The base class used to back handles returned to the driver. 40 // 41 class TShHandleBase { 42 public: 43 TShHandleBase(); 44 virtual ~TShHandleBase(); 45 virtual TCompiler* getAsCompiler() { return 0; } 46 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; } 47 48 protected: 49 // Memory allocator. Allocates and tracks memory required by the compiler. 50 // Deallocates all memory when compiler is destructed. 51 TPoolAllocator allocator; 52 }; 53 54 // 55 // The base class for the machine dependent compiler to derive from 56 // for managing object code from the compile. 57 // 58 class TCompiler : public TShHandleBase { 59 public: 60 TCompiler(ShShaderType type, ShShaderSpec spec); 61 virtual ~TCompiler(); 62 virtual TCompiler* getAsCompiler() { return this; } 63 64 bool Init(const ShBuiltInResources& resources); 65 bool compile(const char* const shaderStrings[], 66 size_t numStrings, 67 int compileOptions); 68 69 // Get results of the last compilation. 70 TInfoSink& getInfoSink() { return infoSink; } 71 const TVariableInfoList& getAttribs() const { return attribs; } 72 const TVariableInfoList& getUniforms() const { return uniforms; } 73 const TVariableInfoList& getVaryings() const { return varyings; } 74 int getMappedNameMaxLength() const; 75 76 ShHashFunction64 getHashFunction() const { return hashFunction; } 77 NameMap& getNameMap() { return nameMap; } 78 TSymbolTable& getSymbolTable() { return symbolTable; } 79 80 protected: 81 ShShaderType getShaderType() const { return shaderType; } 82 ShShaderSpec getShaderSpec() const { return shaderSpec; } 83 // Initialize symbol-table with built-in symbols. 84 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources); 85 // Clears the results from the previous compilation. 86 void clearResults(); 87 // Return true if function recursion is detected or call depth exceeded. 88 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth); 89 // Rewrites a shader's intermediate tree according to the CSS Shaders spec. 90 void rewriteCSSShader(TIntermNode* root); 91 // Returns true if the given shader does not exceed the minimum 92 // functionality mandated in GLSL 1.0 spec Appendix A. 93 bool validateLimitations(TIntermNode* root); 94 // Collect info for all attribs, uniforms, varyings. 95 void collectVariables(TIntermNode* root); 96 // Map long variable names into shorter ones. 97 void mapLongVariableNames(TIntermNode* root); 98 // Translate to object code. 99 virtual void translate(TIntermNode* root) = 0; 100 // Returns true if, after applying the packing rules in the GLSL 1.017 spec 101 // Appendix A, section 7, the shader does not use too many uniforms. 102 bool enforcePackingRestrictions(); 103 // Returns true if the shader passes the restrictions that aim to prevent timing attacks. 104 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph); 105 // Returns true if the shader does not use samplers. 106 bool enforceVertexShaderTimingRestrictions(TIntermNode* root); 107 // Returns true if the shader does not use sampler dependent values to affect control 108 // flow or in operations whose time can depend on the input values. 109 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph); 110 // Return true if the maximum expression complexity below the limit. 111 bool limitExpressionComplexity(TIntermNode* root); 112 // Get built-in extensions with default behavior. 113 const TExtensionBehavior& getExtensionBehavior() const; 114 // Get the resources set by InitBuiltInSymbolTable 115 const ShBuiltInResources& getResources() const; 116 117 const ArrayBoundsClamper& getArrayBoundsClamper() const; 118 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const; 119 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const; 120 121 private: 122 ShShaderType shaderType; 123 ShShaderSpec shaderSpec; 124 125 int maxUniformVectors; 126 int maxExpressionComplexity; 127 int maxCallStackDepth; 128 129 ShBuiltInResources compileResources; 130 131 // Built-in symbol table for the given language, spec, and resources. 132 // It is preserved from compile-to-compile. 133 TSymbolTable symbolTable; 134 // Built-in extensions with default behavior. 135 TExtensionBehavior extensionBehavior; 136 bool fragmentPrecisionHigh; 137 138 ArrayBoundsClamper arrayBoundsClamper; 139 ShArrayIndexClampingStrategy clampingStrategy; 140 BuiltInFunctionEmulator builtInFunctionEmulator; 141 142 // Results of compilation. 143 TInfoSink infoSink; // Output sink. 144 TVariableInfoList attribs; // Active attributes in the compiled shader. 145 TVariableInfoList uniforms; // Active uniforms in the compiled shader. 146 TVariableInfoList varyings; // Varyings in the compiled shader. 147 148 // Cached copy of the ref-counted singleton. 149 LongNameMap* longNameMap; 150 151 // name hashing. 152 ShHashFunction64 hashFunction; 153 NameMap nameMap; 154 }; 155 156 // 157 // This is the interface between the machine independent code 158 // and the machine dependent code. 159 // 160 // The machine dependent code should derive from the classes 161 // above. Then Construct*() and Delete*() will create and 162 // destroy the machine dependent objects, which contain the 163 // above machine independent information. 164 // 165 TCompiler* ConstructCompiler( 166 ShShaderType type, ShShaderSpec spec, ShShaderOutput output); 167 void DeleteCompiler(TCompiler*); 168 169 #endif // _SHHANDLE_INCLUDED_ 170