blob: da522f164ad3b9886eabcb040516eb46d76e2624 [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
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000017#include "GLSLANG/ShaderLang.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/BuiltInFunctionEmulator.h"
20#include "compiler/translator/ExtensionBehavior.h"
21#include "compiler/translator/HashNames.h"
22#include "compiler/translator/InfoSink.h"
23#include "compiler/translator/SymbolTable.h"
24#include "compiler/translator/VariableInfo.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000025#include "third_party/compiler/ArrayBoundsClamper.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026
27class TCompiler;
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +000028class TDependencyGraph;
daniel@transgaming.com043da132012-12-20 21:12:22 +000029class TranslatorHLSL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
31//
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000032// Helper function to identify specs that are based on the WebGL spec,
33// like the CSS Shaders spec.
34//
Jamie Madill5508f392014-02-20 13:31:36 -050035bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000036
37//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038// The base class used to back handles returned to the driver.
39//
40class TShHandleBase {
41public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000042 TShHandleBase();
43 virtual ~TShHandleBase();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044 virtual TCompiler* getAsCompiler() { return 0; }
daniel@transgaming.com043da132012-12-20 21:12:22 +000045 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000046
47protected:
48 // Memory allocator. Allocates and tracks memory required by the compiler.
49 // Deallocates all memory when compiler is destructed.
50 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051};
52
53//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054// The base class for the machine dependent compiler to derive from
55// for managing object code from the compile.
56//
57class TCompiler : public TShHandleBase {
58public:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000059 TCompiler(ShShaderType type, ShShaderSpec spec);
60 virtual ~TCompiler();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 virtual TCompiler* getAsCompiler() { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000062
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000063 bool Init(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000064 bool compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000065 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +000066 int compileOptions);
67
68 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000069 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000070 TInfoSink& getInfoSink() { return infoSink; }
71 const TVariableInfoList& getAttribs() const { return attribs; }
72 const TVariableInfoList& getUniforms() const { return uniforms; }
Zhenyao Mo74da9f22013-09-23 14:57:01 -040073 const TVariableInfoList& getVaryings() const { return varyings; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000074
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000075 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000076 NameMap& getNameMap() { return nameMap; }
77 TSymbolTable& getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070078 ShShaderSpec getShaderSpec() const { return shaderSpec; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000079
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080protected:
alokp@chromium.org9ecf3952010-10-13 19:28:25 +000081 ShShaderType getShaderType() const { return shaderType; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000082 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000083 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
alokp@chromium.org07620a52010-09-23 17:53:56 +000084 // Clears the results from the previous compilation.
85 void clearResults();
Jamie Madilleb1a0102013-07-08 13:31:38 -040086 // Return true if function recursion is detected or call depth exceeded.
87 bool detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth);
Jamie Madill05a80ce2013-06-20 11:55:49 -040088 // Returns true if a program has no conflicting or missing fragment outputs
89 bool validateOutputs(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000090 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
91 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +000092 // Returns true if the given shader does not exceed the minimum
93 // functionality mandated in GLSL 1.0 spec Appendix A.
94 bool validateLimitations(TIntermNode* root);
Zhenyao Mo74da9f22013-09-23 14:57:01 -040095 // Collect info for all attribs, uniforms, varyings.
96 void collectVariables(TIntermNode* root);
alokp@chromium.org07620a52010-09-23 17:53:56 +000097 // Translate to object code.
98 virtual void translate(TIntermNode* root) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +000099 // 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();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800102 // Insert statements to initialize varyings without static use in the beginning
103 // of main(). It is to work around a Mac driver where such varyings in a vertex
104 // shader may be optimized out incorrectly at compile time, causing a link failure.
105 // This function should only be applied to vertex shaders.
106 void initializeVaryingsWithoutStaticUse(TIntermNode* root);
107 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
108 // It is to work around a Linux driver bug where missing this causes compile failure
109 // while spec says it is allowed.
110 // This function should only be applied to vertex shaders.
111 void initializeGLPosition(TIntermNode* root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000112 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000113 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
114 // Returns true if the shader does not use samplers.
115 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
116 // Returns true if the shader does not use sampler dependent values to affect control
117 // flow or in operations whose time can depend on the input values.
118 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400119 // Return true if the maximum expression complexity is below the limit.
120 bool limitExpressionComplexity(TIntermNode* root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000121 // Get built-in extensions with default behavior.
122 const TExtensionBehavior& getExtensionBehavior() const;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000123 // Get the resources set by InitBuiltInSymbolTable
124 const ShBuiltInResources& getResources() const;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000125
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000126 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000127 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000128 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
129
alokp@chromium.org07620a52010-09-23 17:53:56 +0000130private:
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000131 ShShaderType shaderType;
132 ShShaderSpec shaderSpec;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000133
gman@chromium.org8d804792012-10-17 21:33:48 +0000134 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400135 int maxExpressionComplexity;
136 int maxCallStackDepth;
gman@chromium.org8d804792012-10-17 21:33:48 +0000137
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000138 ShBuiltInResources compileResources;
139
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000140 // Built-in symbol table for the given language, spec, and resources.
141 // It is preserved from compile-to-compile.
142 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000143 // Built-in extensions with default behavior.
144 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000145 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000146
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000147 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000148 ShArrayIndexClampingStrategy clampingStrategy;
zmo@google.com32e97312011-08-24 01:03:11 +0000149 BuiltInFunctionEmulator builtInFunctionEmulator;
150
alokp@chromium.org07620a52010-09-23 17:53:56 +0000151 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000152 int shaderVersion;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000153 TInfoSink infoSink; // Output sink.
154 TVariableInfoList attribs; // Active attributes in the compiled shader.
155 TVariableInfoList uniforms; // Active uniforms in the compiled shader.
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400156 TVariableInfoList varyings; // Varyings in the compiled shader.
zmo@google.com24c08c42011-05-27 17:40:48 +0000157
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000158 // name hashing.
159 ShHashFunction64 hashFunction;
160 NameMap nameMap;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161};
162
163//
164// This is the interface between the machine independent code
165// and the machine dependent code.
166//
167// The machine dependent code should derive from the classes
168// above. Then Construct*() and Delete*() will create and
169// destroy the machine dependent objects, which contain the
170// above machine independent information.
171//
zmo@google.com5601ea02011-06-10 18:23:25 +0000172TCompiler* ConstructCompiler(
173 ShShaderType type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174void DeleteCompiler(TCompiler*);
175
daniel@transgaming.com5cb728c2011-05-17 18:34:24 +0000176#endif // _SHHANDLE_INCLUDED_