daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 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 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 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" |
shannon.woods@transgaming.com | da1ed36 | 2013-01-25 21:54:57 +0000 | [diff] [blame] | 23 | #include "third_party/compiler/ArrayBoundsClamper.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 24 | |
| 25 | class TCompiler; |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 26 | class TDependencyGraph; |
daniel@transgaming.com | 043da13 | 2012-12-20 21:12:22 +0000 | [diff] [blame] | 27 | class TranslatorHLSL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 28 | |
| 29 | // |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 30 | // Helper function to identify specs that are based on the WebGL spec, |
| 31 | // like the CSS Shaders spec. |
| 32 | // |
Jamie Madill | 5508f39 | 2014-02-20 13:31:36 -0500 | [diff] [blame] | 33 | bool IsWebGLBasedSpec(ShShaderSpec spec); |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 34 | |
| 35 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 36 | // The base class used to back handles returned to the driver. |
| 37 | // |
| 38 | class TShHandleBase { |
| 39 | public: |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 40 | TShHandleBase(); |
| 41 | virtual ~TShHandleBase(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 42 | virtual TCompiler* getAsCompiler() { return 0; } |
daniel@transgaming.com | 043da13 | 2012-12-20 21:12:22 +0000 | [diff] [blame] | 43 | virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; } |
alokp@chromium.org | bafcbaa | 2010-11-23 19:07:43 +0000 | [diff] [blame] | 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; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 52 | // The base class for the machine dependent compiler to derive from |
| 53 | // for managing object code from the compile. |
| 54 | // |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 55 | class TCompiler : public TShHandleBase |
| 56 | { |
| 57 | public: |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 58 | TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 59 | virtual ~TCompiler(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 60 | virtual TCompiler* getAsCompiler() { return this; } |
alokp@chromium.org | 76b8208 | 2010-03-24 17:59:39 +0000 | [diff] [blame] | 61 | |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 62 | bool Init(const ShBuiltInResources& resources); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 63 | bool compile(const char* const shaderStrings[], |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 64 | size_t numStrings, |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 65 | int compileOptions); |
| 66 | |
| 67 | // Get results of the last compilation. |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 68 | int getShaderVersion() const { return shaderVersion; } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 69 | TInfoSink& getInfoSink() { return infoSink; } |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 70 | |
| 71 | const std::vector<sh::Attribute> &getAttributes() const { return attributes; } |
| 72 | const std::vector<sh::Attribute> &getOutputVariables() const { return outputVariables; } |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 73 | const std::vector<sh::Uniform> &getUniforms() const { return uniforms; } |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 74 | const std::vector<sh::ShaderVariable> &getExpandedUniforms() const { return expandedUniforms; } |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 75 | const std::vector<sh::Varying> &getVaryings() const { return varyings; } |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 76 | const std::vector<sh::ShaderVariable> &getExpandedVaryings() const { return expandedVaryings; } |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 77 | const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 78 | |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 79 | ShHashFunction64 getHashFunction() const { return hashFunction; } |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 80 | NameMap& getNameMap() { return nameMap; } |
| 81 | TSymbolTable& getSymbolTable() { return symbolTable; } |
Zhenyao Mo | 7faf1a1 | 2014-04-25 18:03:56 -0700 | [diff] [blame] | 82 | ShShaderSpec getShaderSpec() const { return shaderSpec; } |
Jamie Madill | 68fe74a | 2014-05-27 12:56:01 -0400 | [diff] [blame] | 83 | ShShaderOutput getOutputType() const { return outputType; } |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 84 | std::string getBuiltInResourcesString() const { return builtInResourcesString; } |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 85 | |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 86 | // Get the resources set by InitBuiltInSymbolTable |
| 87 | const ShBuiltInResources& getResources() const; |
| 88 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 89 | protected: |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 90 | sh::GLenum getShaderType() const { return shaderType; } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 91 | // Initialize symbol-table with built-in symbols. |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 92 | bool InitBuiltInSymbolTable(const ShBuiltInResources& resources); |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 93 | // Compute the string representation of the built-in resources |
| 94 | void setResourceString(); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 95 | // Clears the results from the previous compilation. |
| 96 | void clearResults(); |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 97 | // Return true if function recursion is detected or call depth exceeded. |
| 98 | bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth); |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 99 | // Returns true if a program has no conflicting or missing fragment outputs |
| 100 | bool validateOutputs(TIntermNode* root); |
maxvujovic@gmail.com | 430f5e0 | 2012-06-08 17:47:59 +0000 | [diff] [blame] | 101 | // Rewrites a shader's intermediate tree according to the CSS Shaders spec. |
| 102 | void rewriteCSSShader(TIntermNode* root); |
alokp@chromium.org | b59a778 | 2010-11-24 18:38:33 +0000 | [diff] [blame] | 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); |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 106 | // Collect info for all attribs, uniforms, varyings. |
| 107 | void collectVariables(TIntermNode* root); |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 108 | // Translate to object code. |
| 109 | virtual void translate(TIntermNode* root) = 0; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 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(); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 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); |
maxvujovic@gmail.com | 66ebd01 | 2012-05-30 22:18:11 +0000 | [diff] [blame] | 123 | // 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] | 124 | bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph); |
| 125 | // Returns true if the shader does not use samplers. |
| 126 | bool enforceVertexShaderTimingRestrictions(TIntermNode* root); |
Jamie Madill | d4a3a31 | 2014-06-25 16:04:56 -0400 | [diff] [blame] | 127 | // Returns true if the shader does not use sampler dependent values to affect control |
maxvujovic@gmail.com | 77222c9 | 2012-06-04 21:06:05 +0000 | [diff] [blame] | 128 | // flow or in operations whose time can depend on the input values. |
| 129 | bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph); |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 130 | // Return true if the maximum expression complexity is below the limit. |
| 131 | bool limitExpressionComplexity(TIntermNode* root); |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 132 | // Get built-in extensions with default behavior. |
| 133 | const TExtensionBehavior& getExtensionBehavior() const; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 134 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 135 | const ArrayBoundsClamper& getArrayBoundsClamper() const; |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 136 | ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const; |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 137 | const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const; |
| 138 | |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 139 | std::vector<sh::Attribute> attributes; |
| 140 | std::vector<sh::Attribute> outputVariables; |
| 141 | std::vector<sh::Uniform> uniforms; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 142 | std::vector<sh::ShaderVariable> expandedUniforms; |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 143 | std::vector<sh::Varying> varyings; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 144 | std::vector<sh::ShaderVariable> expandedVaryings; |
Jamie Madill | ed27c72 | 2014-07-02 15:31:23 -0400 | [diff] [blame] | 145 | std::vector<sh::InterfaceBlock> interfaceBlocks; |
| 146 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 147 | private: |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 148 | sh::GLenum shaderType; |
alokp@chromium.org | 4888ceb | 2010-10-01 21:13:12 +0000 | [diff] [blame] | 149 | ShShaderSpec shaderSpec; |
Jamie Madill | 68fe74a | 2014-05-27 12:56:01 -0400 | [diff] [blame] | 150 | ShShaderOutput outputType; |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 151 | |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 152 | int maxUniformVectors; |
Jamie Madill | eb1a010 | 2013-07-08 13:31:38 -0400 | [diff] [blame] | 153 | int maxExpressionComplexity; |
| 154 | int maxCallStackDepth; |
gman@chromium.org | 8d80479 | 2012-10-17 21:33:48 +0000 | [diff] [blame] | 155 | |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 156 | ShBuiltInResources compileResources; |
Shannon Woods | 2d76e5f | 2014-05-16 17:46:41 -0400 | [diff] [blame] | 157 | std::string builtInResourcesString; |
shannon.woods%transgaming.com@gtempaccount.com | 18b4c4b | 2013-04-13 03:31:40 +0000 | [diff] [blame] | 158 | |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 159 | // Built-in symbol table for the given language, spec, and resources. |
| 160 | // It is preserved from compile-to-compile. |
| 161 | TSymbolTable symbolTable; |
alokp@chromium.org | ad771eb | 2010-09-07 17:36:23 +0000 | [diff] [blame] | 162 | // Built-in extensions with default behavior. |
| 163 | TExtensionBehavior extensionBehavior; |
shannon.woods%transgaming.com@gtempaccount.com | cbb6b6a | 2013-04-13 03:27:47 +0000 | [diff] [blame] | 164 | bool fragmentPrecisionHigh; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 165 | |
daniel@transgaming.com | 4167cc9 | 2013-01-11 04:11:53 +0000 | [diff] [blame] | 166 | ArrayBoundsClamper arrayBoundsClamper; |
shannon.woods@transgaming.com | 1d432bb | 2013-01-25 21:57:28 +0000 | [diff] [blame] | 167 | ShArrayIndexClampingStrategy clampingStrategy; |
zmo@google.com | 32e9731 | 2011-08-24 01:03:11 +0000 | [diff] [blame] | 168 | BuiltInFunctionEmulator builtInFunctionEmulator; |
| 169 | |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 170 | // Results of compilation. |
shannon.woods%transgaming.com@gtempaccount.com | 0bbed38 | 2013-04-13 03:38:07 +0000 | [diff] [blame] | 171 | int shaderVersion; |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 172 | TInfoSink infoSink; // Output sink. |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 173 | |
daniel@transgaming.com | c23f461 | 2012-11-28 19:42:57 +0000 | [diff] [blame] | 174 | // name hashing. |
| 175 | ShHashFunction64 hashFunction; |
| 176 | NameMap nameMap; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | // |
| 180 | // This is the interface between the machine independent code |
| 181 | // and the machine dependent code. |
| 182 | // |
| 183 | // The machine dependent code should derive from the classes |
Jamie Madill | d4a3a31 | 2014-06-25 16:04:56 -0400 | [diff] [blame] | 184 | // above. Then Construct*() and Delete*() will create and |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 185 | // destroy the machine dependent objects, which contain the |
| 186 | // above machine independent information. |
| 187 | // |
zmo@google.com | 5601ea0 | 2011-06-10 18:23:25 +0000 | [diff] [blame] | 188 | TCompiler* ConstructCompiler( |
Jamie Madill | 183bde5 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 189 | sh::GLenum type, ShShaderSpec spec, ShShaderOutput output); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 190 | void DeleteCompiler(TCompiler*); |
| 191 | |
daniel@transgaming.com | 5cb728c | 2011-05-17 18:34:24 +0000 | [diff] [blame] | 192 | #endif // _SHHANDLE_INCLUDED_ |