daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 17 | #include "GLSLANG/ShaderLang.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 18 | |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 19 | #include "compiler/BuiltInFunctionEmulator.h" |
alokp@chromium.org | ad771eb | 2010-09-07 17:36:23 +0000 | [diff] [blame] | 20 | #include "compiler/ExtensionBehavior.h" |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 21 | #include "compiler/HashNames.h" |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 22 | #include "compiler/InfoSink.h" |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 23 | #include "compiler/SymbolTable.h" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 24 | #include "compiler/VariableInfo.h" |
shannon.woods@transgaming.com | da1ed36 | 2013-01-25 21:54:57 +0000 | [diff] [blame] | 25 | #include "third_party/compiler/ArrayBoundsClamper.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 26 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 27 | class LongNameMap; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 28 | class TCompiler; |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 29 | class TDependencyGraph; |
daniel@transgaming.com | 043da13 | 2012-12-20 21:12:22 +0000 | [diff] [blame] | 30 | class TranslatorHLSL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 31 | |
| 32 | // |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 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 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | // The base class used to back handles returned to the driver. |
| 40 | // |
| 41 | class TShHandleBase { |
| 42 | public: |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 43 | TShHandleBase(); |
| 44 | virtual ~TShHandleBase(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 45 | virtual TCompiler* getAsCompiler() { return 0; } |
daniel@transgaming.com | 043da13 | 2012-12-20 21:12:22 +0000 | [diff] [blame] | 46 | virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; } |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 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; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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: |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 60 | TCompiler(ShShaderType type, ShShaderSpec spec); |
| 61 | virtual ~TCompiler(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 62 | virtual TCompiler* getAsCompiler() { return this; } |
alokp@chromium.org | 76b8208 | 2010-03-24 17:59:39 +0000 | [diff] [blame] | 63 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 64 | bool Init(const ShBuiltInResources& resources); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 65 | bool compile(const char* const shaderStrings[], |
| 66 | const int 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; } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 73 | int getMappedNameMaxLength() const; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 74 | |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 75 | ShHashFunction64 getHashFunction() const { return hashFunction; } |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 76 | NameMap& getNameMap() { return nameMap; } |
| 77 | TSymbolTable& getSymbolTable() { return symbolTable; } |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 78 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | protected: |
alokp@chromium.org | 9ecf395 | 2010-10-13 19:28:25 +0000 | [diff] [blame] | 80 | ShShaderType getShaderType() const { return shaderType; } |
| 81 | ShShaderSpec getShaderSpec() const { return shaderSpec; } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 82 | // Initialize symbol-table with built-in symbols. |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 83 | bool InitBuiltInSymbolTable(const ShBuiltInResources& resources); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 84 | // Clears the results from the previous compilation. |
| 85 | void clearResults(); |
zmo@google.com | b1762df | 2011-07-30 02:04:23 +0000 | [diff] [blame] | 86 | // Return true if function recursion is detected. |
| 87 | bool detectRecursion(TIntermNode* root); |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 88 | // Rewrites a shader's intermediate tree according to the CSS Shaders spec. |
| 89 | void rewriteCSSShader(TIntermNode* root); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 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); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 93 | // Collect info for all attribs and uniforms. |
| 94 | void collectAttribsUniforms(TIntermNode* root); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 95 | // Map long variable names into shorter ones. |
| 96 | void mapLongVariableNames(TIntermNode* root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 97 | // Translate to object code. |
| 98 | virtual void translate(TIntermNode* root) = 0; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 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(); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 102 | // Returns true if the shader passes the restrictions that aim to prevent timing attacks. |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 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); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 109 | // Get built-in extensions with default behavior. |
| 110 | const TExtensionBehavior& getExtensionBehavior() const; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 111 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 112 | const ArrayBoundsClamper& getArrayBoundsClamper() const; |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame^] | 113 | ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const; |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 114 | const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const; |
| 115 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 116 | private: |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 117 | ShShaderType shaderType; |
| 118 | ShShaderSpec shaderSpec; |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 119 | |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 120 | int maxUniformVectors; |
| 121 | |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 122 | // Built-in symbol table for the given language, spec, and resources. |
| 123 | // It is preserved from compile-to-compile. |
| 124 | TSymbolTable symbolTable; |
alokp@chromium.org | ad771eb | 2010-09-07 17:36:23 +0000 | [diff] [blame] | 125 | // Built-in extensions with default behavior. |
| 126 | TExtensionBehavior extensionBehavior; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 127 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 128 | ArrayBoundsClamper arrayBoundsClamper; |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame^] | 129 | ShArrayIndexClampingStrategy clampingStrategy; |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 130 | BuiltInFunctionEmulator builtInFunctionEmulator; |
| 131 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 132 | // Results of compilation. |
| 133 | TInfoSink infoSink; // Output sink. |
| 134 | TVariableInfoList attribs; // Active attributes in the compiled shader. |
| 135 | TVariableInfoList uniforms; // Active uniforms in the compiled shader. |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 136 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 137 | // Cached copy of the ref-counted singleton. |
| 138 | LongNameMap* longNameMap; |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 139 | |
| 140 | // name hashing. |
| 141 | ShHashFunction64 hashFunction; |
| 142 | NameMap nameMap; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | // |
| 146 | // This is the interface between the machine independent code |
| 147 | // and the machine dependent code. |
| 148 | // |
| 149 | // The machine dependent code should derive from the classes |
| 150 | // above. Then Construct*() and Delete*() will create and |
| 151 | // destroy the machine dependent objects, which contain the |
| 152 | // above machine independent information. |
| 153 | // |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 154 | TCompiler* ConstructCompiler( |
| 155 | ShShaderType type, ShShaderSpec spec, ShShaderOutput output); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 156 | void DeleteCompiler(TCompiler*); |
| 157 | |
daniel@transgaming.com | 5cb728c | 2011-05-17 18:34:24 +0000 | [diff] [blame] | 158 | #endif // _SHHANDLE_INCLUDED_ |