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 int getMappedNameMaxLength() const; 74 75 ShHashFunction64 getHashFunction() const { return hashFunction; } 76 NameMap& getNameMap() { return nameMap; } 77 TSymbolTable& getSymbolTable() { return symbolTable; } 78 79 protected: 80 ShShaderType getShaderType() const { return shaderType; } 81 ShShaderSpec getShaderSpec() const { return shaderSpec; } 82 // Initialize symbol-table with built-in symbols. 83 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources); 84 // Clears the results from the previous compilation. 85 void clearResults(); 86 // Return true if function recursion is detected or call depth exceeded. 87 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth); 88 // Rewrites a shader's intermediate tree according to the CSS Shaders spec. 89 void rewriteCSSShader(TIntermNode* root); 90 // Returns true if the given shader does not exceed the minimum 91 // functionality mandated in GLSL 1.0 spec Appendix A. 92 bool validateLimitations(TIntermNode* root); 93 // Collect info for all attribs and uniforms. 94 void collectAttribsUniforms(TIntermNode* root); 95 // Map long variable names into shorter ones. 96 void mapLongVariableNames(TIntermNode* root); 97 // Translate to object code. 98 virtual void translate(TIntermNode* root) = 0; 99 // Returns true if, after applying the packing rules in the GLSL 1.017 spec 100 // Appendix A, section 7, the shader does not use too many uniforms. 101 bool enforcePackingRestrictions(); 102 // Returns true if the shader passes the restrictions that aim to prevent timing attacks. 103 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph); 104 // Returns true if the shader does not use samplers. 105 bool enforceVertexShaderTimingRestrictions(TIntermNode* root); 106 // Returns true if the shader does not use sampler dependent values to affect control 107 // flow or in operations whose time can depend on the input values. 108 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph); 109 // Return true if the maximum expression complexity below the limit. 110 bool limitExpressionComplexity(TIntermNode* root); 111 // Get built-in extensions with default behavior. 112 const TExtensionBehavior& getExtensionBehavior() const; 113 // Get the resources set by InitBuiltInSymbolTable 114 const ShBuiltInResources& getResources() const; 115 116 const ArrayBoundsClamper& getArrayBoundsClamper() const; 117 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const; 118 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const; 119 120 private: 121 ShShaderType shaderType; 122 ShShaderSpec shaderSpec; 123 124 int maxUniformVectors; 125 int maxExpressionComplexity; 126 int maxCallStackDepth; 127 128 ShBuiltInResources compileResources; 129 130 // Built-in symbol table for the given language, spec, and resources. 131 // It is preserved from compile-to-compile. 132 TSymbolTable symbolTable; 133 // Built-in extensions with default behavior. 134 TExtensionBehavior extensionBehavior; 135 bool fragmentPrecisionHigh; 136 137 ArrayBoundsClamper arrayBoundsClamper; 138 ShArrayIndexClampingStrategy clampingStrategy; 139 BuiltInFunctionEmulator builtInFunctionEmulator; 140 141 // Results of compilation. 142 TInfoSink infoSink; // Output sink. 143 TVariableInfoList attribs; // Active attributes in the compiled shader. 144 TVariableInfoList uniforms; // Active uniforms in the compiled shader. 145 146 // Cached copy of the ref-counted singleton. 147 LongNameMap* longNameMap; 148 149 // name hashing. 150 ShHashFunction64 hashFunction; 151 NameMap nameMap; 152 }; 153 154 // 155 // This is the interface between the machine independent code 156 // and the machine dependent code. 157 // 158 // The machine dependent code should derive from the classes 159 // above. Then Construct*() and Delete*() will create and 160 // destroy the machine dependent objects, which contain the 161 // above machine independent information. 162 // 163 TCompiler* ConstructCompiler( 164 ShShaderType type, ShShaderSpec spec, ShShaderOutput output); 165 void DeleteCompiler(TCompiler*); 166 167 #endif // _SHHANDLE_INCLUDED_ 168