blob: 6b682374b29a2a44e05184e1944e384954d69d0a [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
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_COMPILER_H_
8#define COMPILER_TRANSLATOR_COMPILER_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
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
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020017#include "compiler/translator/BuiltInFunctionEmulator.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080018#include "compiler/translator/CallDAG.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/ExtensionBehavior.h"
20#include "compiler/translator/HashNames.h"
21#include "compiler/translator/InfoSink.h"
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070022#include "compiler/translator/Pragma.h"
Geoff Lang17732822013-08-29 13:46:49 -040023#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 Bratell73941de2015-02-25 14:34:49 +010029#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000030class TranslatorHLSL;
Daniel Bratell73941de2015-02-25 14:34:49 +010031#endif // ANGLE_ENABLE_HLSL
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
33//
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000034// Helper function to identify specs that are based on the WebGL spec,
35// like the CSS Shaders spec.
36//
Jamie Madill5508f392014-02-20 13:31:36 -050037bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000038
39//
Qingqing Dengad0d0792015-04-08 14:25:06 -070040// Helper function to check if the shader type is GLSL.
41//
42bool IsGLSL130OrNewer(ShShaderOutput output);
43
44//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045// The base class used to back handles returned to the driver.
46//
47class TShHandleBase {
48public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000049 TShHandleBase();
50 virtual ~TShHandleBase();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051 virtual TCompiler* getAsCompiler() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010052#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000053 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010054#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000055
56protected:
57 // Memory allocator. Allocates and tracks memory required by the compiler.
58 // Deallocates all memory when compiler is destructed.
59 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000060};
61
62//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063// The base class for the machine dependent compiler to derive from
64// for managing object code from the compile.
65//
Jamie Madilla718c1e2014-07-02 15:31:22 -040066class TCompiler : public TShHandleBase
67{
68 public:
Jamie Madill183bde52014-07-02 15:31:19 -040069 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
Corentin Walleze5a1f272015-08-21 02:58:25 +020070 ~TCompiler() override;
71 TCompiler *getAsCompiler() override { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000072
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000073 bool Init(const ShBuiltInResources& resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020074
75 // compileTreeForTesting should be used only when tests require access to
76 // the AST. Users of this function need to manually manage the global pool
77 // allocator. Returns NULL whenever there are compilation errors.
78 TIntermNode *compileTreeForTesting(const char* const shaderStrings[],
79 size_t numStrings, int compileOptions);
80
alokp@chromium.org07620a52010-09-23 17:53:56 +000081 bool compile(const char* const shaderStrings[],
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020082 size_t numStrings, int compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000083
84 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000085 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000086 TInfoSink& getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -040087
Martin Radev802abe02016-08-04 17:48:32 +030088 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Martin Radev4c4c8e72016-08-04 12:25:34 +030089 const sh::WorkGroupSize &getComputeShaderLocalSize() { return mComputeShaderLocalSize; }
Martin Radev802abe02016-08-04 17:48:32 +030090
Dmitry Skiba2539fff2015-06-16 17:56:09 -070091 // Clears the results from the previous compilation.
92 void clearResults();
93
Jamie Madilled27c722014-07-02 15:31:23 -040094 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
Jamie Madilla0a9e122015-09-02 15:54:30 -040095 const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -040096 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
97 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
Jamie Madilled27c722014-07-02 15:31:23 -040098 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000099
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000100 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000101 NameMap& getNameMap() { return nameMap; }
102 TSymbolTable& getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700103 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -0400104 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -0700105 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000106
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300107 bool shouldRunLoopAndIndexingValidation(int compileOptions) const;
108
Jamie Madill54ad4f82014-09-03 09:40:46 -0400109 // Get the resources set by InitBuiltInSymbolTable
110 const ShBuiltInResources& getResources() const;
111
Jamie Madilla718c1e2014-07-02 15:31:22 -0400112 protected:
Jamie Madill183bde52014-07-02 15:31:19 -0400113 sh::GLenum getShaderType() const { return shaderType; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000114 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000115 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400116 // Compute the string representation of the built-in resources
117 void setResourceString();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800118 // Return false if the call depth is exceeded.
119 bool checkCallDepth();
Jamie Madill05a80ce2013-06-20 11:55:49 -0400120 // Returns true if a program has no conflicting or missing fragment outputs
121 bool validateOutputs(TIntermNode* root);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000122 // Rewrites a shader's intermediate tree according to the CSS Shaders spec.
123 void rewriteCSSShader(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000124 // Returns true if the given shader does not exceed the minimum
125 // functionality mandated in GLSL 1.0 spec Appendix A.
126 bool validateLimitations(TIntermNode* root);
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400127 // Collect info for all attribs, uniforms, varyings.
128 void collectVariables(TIntermNode* root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200129 // Add emulated functions to the built-in function emulator.
130 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) {};
alokp@chromium.org07620a52010-09-23 17:53:56 +0000131 // Translate to object code.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200132 virtual void translate(TIntermNode *root, int compileOptions) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +0000133 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
134 // Appendix A, section 7, the shader does not use too many uniforms.
135 bool enforcePackingRestrictions();
Zhenyao Mo72111912016-07-20 17:45:56 -0700136 // Insert statements to initialize output variables in the beginning of main().
137 // This is to avoid undefined behaviors.
Olli Etuaho27776e32016-07-22 14:00:56 +0300138 void initializeOutputVariables(TIntermNode *root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800139 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
140 // It is to work around a Linux driver bug where missing this causes compile failure
141 // while spec says it is allowed.
142 // This function should only be applied to vertex shaders.
143 void initializeGLPosition(TIntermNode* root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000144 // Returns true if the shader passes the restrictions that aim to prevent timing attacks.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000145 bool enforceTimingRestrictions(TIntermNode* root, bool outputGraph);
146 // Returns true if the shader does not use samplers.
147 bool enforceVertexShaderTimingRestrictions(TIntermNode* root);
Jamie Madilld4a3a312014-06-25 16:04:56 -0400148 // Returns true if the shader does not use sampler dependent values to affect control
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000149 // flow or in operations whose time can depend on the input values.
150 bool enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400151 // Return true if the maximum expression complexity is below the limit.
152 bool limitExpressionComplexity(TIntermNode* root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000153 // Get built-in extensions with default behavior.
154 const TExtensionBehavior& getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200155 const char *getSourcePath() const;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700156 const TPragma& getPragma() const { return mPragma; }
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700157 void writePragma(int compileOptions);
Corentin Wallezd4b50542015-09-28 12:19:26 -0700158 unsigned int *getTemporaryIndex() { return &mTemporaryIndex; }
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700159 // Relies on collectVariables having been called.
160 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000161
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000162 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000163 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200164 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000165
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700166 virtual bool shouldCollectVariables(int compileOptions)
167 {
168 return (compileOptions & SH_VARIABLES) != 0;
169 }
170
171 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
172
Jamie Madilled27c722014-07-02 15:31:23 -0400173 std::vector<sh::Attribute> attributes;
Jamie Madilla0a9e122015-09-02 15:54:30 -0400174 std::vector<sh::OutputVariable> outputVariables;
Jamie Madilled27c722014-07-02 15:31:23 -0400175 std::vector<sh::Uniform> uniforms;
Jamie Madill42bcf322014-08-25 16:20:46 -0400176 std::vector<sh::ShaderVariable> expandedUniforms;
Jamie Madilled27c722014-07-02 15:31:23 -0400177 std::vector<sh::Varying> varyings;
178 std::vector<sh::InterfaceBlock> interfaceBlocks;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700179 bool variablesCollected;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300180
Jamie Madilla718c1e2014-07-02 15:31:22 -0400181 private:
Corentin Wallez71d147f2015-02-11 11:15:24 -0800182 // Creates the function call DAG for further analysis, returning false if there is a recursion
183 bool initCallDag(TIntermNode *root);
184 // Return false if "main" doesn't exist
185 bool tagUsedFunctions();
186 void internalTagUsedFunction(size_t index);
187
Olli Etuaho183d7e22015-11-20 15:59:09 +0200188 void initSamplerDefaultPrecision(TBasicType samplerType);
189
Corentin Walleza094a8a2015-04-07 11:53:06 -0700190 // Removes unused function declarations and prototypes from the AST
191 class UnusedPredicate;
192 bool pruneUnusedFunctions(TIntermNode *root);
193
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300194 TIntermNode *compileTreeImpl(const char *const shaderStrings[],
195 size_t numStrings,
196 const int compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200197
Jamie Madill183bde52014-07-02 15:31:19 -0400198 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000199 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400200 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000201
Corentin Wallez71d147f2015-02-11 11:15:24 -0800202 struct FunctionMetadata
203 {
204 FunctionMetadata()
205 : used(false)
206 {
207 }
208 bool used;
209 };
210
211 CallDAG mCallDag;
212 std::vector<FunctionMetadata> functionMetadata;
213
gman@chromium.org8d804792012-10-17 21:33:48 +0000214 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400215 int maxExpressionComplexity;
216 int maxCallStackDepth;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200217 int maxFunctionParameters;
gman@chromium.org8d804792012-10-17 21:33:48 +0000218
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000219 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400220 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000221
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000222 // Built-in symbol table for the given language, spec, and resources.
223 // It is preserved from compile-to-compile.
224 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000225 // Built-in extensions with default behavior.
226 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000227 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000228
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000229 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000230 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200231 BuiltInFunctionEmulator builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000232
alokp@chromium.org07620a52010-09-23 17:53:56 +0000233 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000234 int shaderVersion;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000235 TInfoSink infoSink; // Output sink.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200236 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000237
Martin Radev802abe02016-08-04 17:48:32 +0300238 // compute shader local group size
239 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300240 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300241
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000242 // name hashing.
243 ShHashFunction64 hashFunction;
244 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700245
246 TPragma mPragma;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700247
248 unsigned int mTemporaryIndex;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249};
250
251//
252// This is the interface between the machine independent code
253// and the machine dependent code.
254//
255// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400256// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257// destroy the machine dependent objects, which contain the
258// above machine independent information.
259//
zmo@google.com5601ea02011-06-10 18:23:25 +0000260TCompiler* ConstructCompiler(
Jamie Madill183bde52014-07-02 15:31:19 -0400261 sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262void DeleteCompiler(TCompiler*);
263
Geoff Lang0a73dd82014-11-19 16:18:08 -0500264#endif // COMPILER_TRANSLATOR_COMPILER_H_