blob: ca0c157884fd54d5f707773044a3afd4cebfb2ee [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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 Lang17732822013-08-29 13:46:49 -040017#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.comda1ed362013-01-25 21:54:57 +000023#include "third_party/compiler/ArrayBoundsClamper.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024
25class TCompiler;
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000026class TDependencyGraph;
daniel@transgaming.com043da132012-12-20 21:12:22 +000027class TranslatorHLSL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028
29//
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000030// Helper function to identify specs that are based on the WebGL spec,
31// like the CSS Shaders spec.
32//
Jamie Madill5508f392014-02-20 13:31:36 -050033bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000034
35//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036// The base class used to back handles returned to the driver.
37//
38class TShHandleBase {
39public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000040 TShHandleBase();
41 virtual ~TShHandleBase();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042 virtual TCompiler* getAsCompiler() { return 0; }
daniel@transgaming.com043da132012-12-20 21:12:22 +000043 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000044
45protected:
46 // Memory allocator. Allocates and tracks memory required by the compiler.
47 // Deallocates all memory when compiler is destructed.
48 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049};
50
51//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052// The base class for the machine dependent compiler to derive from
53// for managing object code from the compile.
54//
Jamie Madilla718c1e2014-07-02 15:31:22 -040055class TCompiler : public TShHandleBase
56{
57 public:
Jamie Madill183bde52014-07-02 15:31:19 -040058 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000059 virtual ~TCompiler();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000060 virtual TCompiler* getAsCompiler() { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000061
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000062 bool Init(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000063 bool compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000064 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +000065 int compileOptions);
66
67 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000068 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000069 TInfoSink& getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -040070
71 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
72 const std::vector<sh::Attribute> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -040073 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
Jamie Madill42bcf322014-08-25 16:20:46 -040074 const std::vector<sh::ShaderVariable> &getExpandedUniforms() const { return expandedUniforms; }
Jamie Madilla718c1e2014-07-02 15:31:22 -040075 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
Jamie Madill42bcf322014-08-25 16:20:46 -040076 const std::vector<sh::ShaderVariable> &getExpandedVaryings() const { return expandedVaryings; }
Jamie Madilled27c722014-07-02 15:31:23 -040077 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000078
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000079 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000080 NameMap& getNameMap() { return nameMap; }
81 TSymbolTable& getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070082 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -040083 ShShaderOutput getOutputType() const { return outputType; }
Shannon Woods2d76e5f2014-05-16 17:46:41 -040084 std::string getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000085
Jamie Madill54ad4f82014-09-03 09:40:46 -040086 // Get the resources set by InitBuiltInSymbolTable
87 const ShBuiltInResources& getResources() const;
88
Jamie Madilla718c1e2014-07-02 15:31:22 -040089 protected:
Jamie Madill183bde52014-07-02 15:31:19 -040090 sh::GLenum getShaderType() const { return shaderType; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000091 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000092 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -040093 // Compute the string representation of the built-in resources
94 void setResourceString();
alokp@chromium.org07620a52010-09-23 17:53:56 +000095 // Clears the results from the previous compilation.
96 void clearResults();
Jamie Madilleb1a0102013-07-08 13:31:38 -040097 // Return true if function recursion is detected or call depth exceeded.
98 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth);
Jamie Madill05a80ce2013-06-20 11:55:49 -040099 // Returns true if a program has no conflicting or missing fragment outputs
100 bool validateOutputs(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000101 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
102 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000103 // 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 Mo74da9f22013-09-23 14:57:01 -0400106 // Collect info for all attribs, uniforms, varyings.
107 void collectVariables(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000108 // Translate to object code.
109 virtual void translate(TIntermNode* root) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +0000110 // 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 Mo4a667fe2014-02-11 12:35:01 -0800113 // 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.com66ebd012012-05-30 22:18:11 +0000123 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000124 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
125 // Returns true if the shader does not use samplers.
126 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
Jamie Madilld4a3a312014-06-25 16:04:56 -0400127 // Returns true if the shader does not use sampler dependent values to affect control
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000128 // flow or in operations whose time can depend on the input values.
129 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400130 // Return true if the maximum expression complexity is below the limit.
131 bool limitExpressionComplexity(TIntermNode* root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000132 // Get built-in extensions with default behavior.
133 const TExtensionBehavior& getExtensionBehavior() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000134
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000135 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000136 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000137 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
138
Jamie Madilled27c722014-07-02 15:31:23 -0400139 std::vector<sh::Attribute> attributes;
140 std::vector<sh::Attribute> outputVariables;
141 std::vector<sh::Uniform> uniforms;
Jamie Madill42bcf322014-08-25 16:20:46 -0400142 std::vector<sh::ShaderVariable> expandedUniforms;
Jamie Madilled27c722014-07-02 15:31:23 -0400143 std::vector<sh::Varying> varyings;
Jamie Madill42bcf322014-08-25 16:20:46 -0400144 std::vector<sh::ShaderVariable> expandedVaryings;
Jamie Madilled27c722014-07-02 15:31:23 -0400145 std::vector<sh::InterfaceBlock> interfaceBlocks;
146
Jamie Madilla718c1e2014-07-02 15:31:22 -0400147 private:
Jamie Madill183bde52014-07-02 15:31:19 -0400148 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000149 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400150 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000151
gman@chromium.org8d804792012-10-17 21:33:48 +0000152 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400153 int maxExpressionComplexity;
154 int maxCallStackDepth;
gman@chromium.org8d804792012-10-17 21:33:48 +0000155
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000156 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400157 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000158
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000159 // Built-in symbol table for the given language, spec, and resources.
160 // It is preserved from compile-to-compile.
161 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000162 // Built-in extensions with default behavior.
163 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000164 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000165
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000166 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000167 ShArrayIndexClampingStrategy clampingStrategy;
zmo@google.com32e97312011-08-24 01:03:11 +0000168 BuiltInFunctionEmulator builtInFunctionEmulator;
169
alokp@chromium.org07620a52010-09-23 17:53:56 +0000170 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000171 int shaderVersion;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000172 TInfoSink infoSink; // Output sink.
zmo@google.com24c08c42011-05-27 17:40:48 +0000173
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000174 // name hashing.
175 ShHashFunction64 hashFunction;
176 NameMap nameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177};
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 Madilld4a3a312014-06-25 16:04:56 -0400184// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185// destroy the machine dependent objects, which contain the
186// above machine independent information.
187//
zmo@google.com5601ea02011-06-10 18:23:25 +0000188TCompiler* ConstructCompiler(
Jamie Madill183bde52014-07-02 15:31:19 -0400189 sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190void DeleteCompiler(TCompiler*);
191
daniel@transgaming.com5cb728c2011-05-17 18:34:24 +0000192#endif // _SHHANDLE_INCLUDED_