blob: 3f22bd6b6d6777f7f1403a5678ac62a76190f48d [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;
Daniel Bratell73941de2015-02-25 14:34:49 +010028#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000029class TranslatorHLSL;
Daniel Bratell73941de2015-02-25 14:34:49 +010030#endif // ANGLE_ENABLE_HLSL
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
32//
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080033// Helper function to identify specs that are based on the WebGL spec.
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000034//
Jamie Madill5508f392014-02-20 13:31:36 -050035bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000036
37//
Qingqing Dengad0d0792015-04-08 14:25:06 -070038// Helper function to check if the shader type is GLSL.
39//
40bool IsGLSL130OrNewer(ShShaderOutput output);
41
42//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043// The base class used to back handles returned to the driver.
44//
45class TShHandleBase {
46public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000047 TShHandleBase();
48 virtual ~TShHandleBase();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000049 virtual TCompiler* getAsCompiler() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010050#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000051 virtual TranslatorHLSL* getAsTranslatorHLSL() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010052#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000053
54protected:
55 // Memory allocator. Allocates and tracks memory required by the compiler.
56 // Deallocates all memory when compiler is destructed.
57 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058};
59
60//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061// The base class for the machine dependent compiler to derive from
62// for managing object code from the compile.
63//
Jamie Madilla718c1e2014-07-02 15:31:22 -040064class TCompiler : public TShHandleBase
65{
66 public:
Jamie Madill183bde52014-07-02 15:31:19 -040067 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
Corentin Walleze5a1f272015-08-21 02:58:25 +020068 ~TCompiler() override;
69 TCompiler *getAsCompiler() override { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000070
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000071 bool Init(const ShBuiltInResources& resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020072
73 // compileTreeForTesting should be used only when tests require access to
74 // the AST. Users of this function need to manually manage the global pool
75 // allocator. Returns NULL whenever there are compilation errors.
76 TIntermNode *compileTreeForTesting(const char* const shaderStrings[],
77 size_t numStrings, int compileOptions);
78
alokp@chromium.org07620a52010-09-23 17:53:56 +000079 bool compile(const char* const shaderStrings[],
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020080 size_t numStrings, int compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000081
82 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000083 int getShaderVersion() const { return shaderVersion; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000084 TInfoSink& getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -040085
Martin Radev802abe02016-08-04 17:48:32 +030086 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Martin Radev4c4c8e72016-08-04 12:25:34 +030087 const sh::WorkGroupSize &getComputeShaderLocalSize() { return mComputeShaderLocalSize; }
Martin Radev802abe02016-08-04 17:48:32 +030088
Dmitry Skiba2539fff2015-06-16 17:56:09 -070089 // Clears the results from the previous compilation.
90 void clearResults();
91
Jamie Madilled27c722014-07-02 15:31:23 -040092 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
Jamie Madilla0a9e122015-09-02 15:54:30 -040093 const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -040094 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
95 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
Jamie Madilled27c722014-07-02 15:31:23 -040096 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +000097
daniel@transgaming.comc23f4612012-11-28 19:42:57 +000098 ShHashFunction64 getHashFunction() const { return hashFunction; }
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +000099 NameMap& getNameMap() { return nameMap; }
100 TSymbolTable& getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700101 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -0400102 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -0700103 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000104
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300105 bool shouldRunLoopAndIndexingValidation(int compileOptions) const;
106
Jamie Madill54ad4f82014-09-03 09:40:46 -0400107 // Get the resources set by InitBuiltInSymbolTable
108 const ShBuiltInResources& getResources() const;
109
Jamie Madilla718c1e2014-07-02 15:31:22 -0400110 protected:
Jamie Madill183bde52014-07-02 15:31:19 -0400111 sh::GLenum getShaderType() const { return shaderType; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000112 // Initialize symbol-table with built-in symbols.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000113 bool InitBuiltInSymbolTable(const ShBuiltInResources& resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400114 // Compute the string representation of the built-in resources
115 void setResourceString();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800116 // Return false if the call depth is exceeded.
117 bool checkCallDepth();
Jamie Madill05a80ce2013-06-20 11:55:49 -0400118 // Returns true if a program has no conflicting or missing fragment outputs
119 bool validateOutputs(TIntermNode* root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000120 // Returns true if the given shader does not exceed the minimum
121 // functionality mandated in GLSL 1.0 spec Appendix A.
122 bool validateLimitations(TIntermNode* root);
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400123 // Collect info for all attribs, uniforms, varyings.
124 void collectVariables(TIntermNode* root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200125 // Add emulated functions to the built-in function emulator.
126 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu, int compileOptions) {};
alokp@chromium.org07620a52010-09-23 17:53:56 +0000127 // Translate to object code.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200128 virtual void translate(TIntermNode *root, int compileOptions) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +0000129 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
130 // Appendix A, section 7, the shader does not use too many uniforms.
131 bool enforcePackingRestrictions();
Zhenyao Mo72111912016-07-20 17:45:56 -0700132 // Insert statements to initialize output variables in the beginning of main().
133 // This is to avoid undefined behaviors.
Olli Etuaho27776e32016-07-22 14:00:56 +0300134 void initializeOutputVariables(TIntermNode *root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800135 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
136 // It is to work around a Linux driver bug where missing this causes compile failure
137 // while spec says it is allowed.
138 // This function should only be applied to vertex shaders.
139 void initializeGLPosition(TIntermNode* root);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400140 // Return true if the maximum expression complexity is below the limit.
141 bool limitExpressionComplexity(TIntermNode* root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000142 // Get built-in extensions with default behavior.
143 const TExtensionBehavior& getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200144 const char *getSourcePath() const;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700145 const TPragma& getPragma() const { return mPragma; }
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700146 void writePragma(int compileOptions);
Corentin Wallezd4b50542015-09-28 12:19:26 -0700147 unsigned int *getTemporaryIndex() { return &mTemporaryIndex; }
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700148 // Relies on collectVariables having been called.
149 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000150
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000151 const ArrayBoundsClamper& getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000152 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200153 const BuiltInFunctionEmulator& getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000154
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700155 virtual bool shouldCollectVariables(int compileOptions)
156 {
157 return (compileOptions & SH_VARIABLES) != 0;
158 }
159
160 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
161
Jamie Madilled27c722014-07-02 15:31:23 -0400162 std::vector<sh::Attribute> attributes;
Jamie Madilla0a9e122015-09-02 15:54:30 -0400163 std::vector<sh::OutputVariable> outputVariables;
Jamie Madilled27c722014-07-02 15:31:23 -0400164 std::vector<sh::Uniform> uniforms;
Jamie Madill42bcf322014-08-25 16:20:46 -0400165 std::vector<sh::ShaderVariable> expandedUniforms;
Jamie Madilled27c722014-07-02 15:31:23 -0400166 std::vector<sh::Varying> varyings;
167 std::vector<sh::InterfaceBlock> interfaceBlocks;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700168 bool variablesCollected;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300169
Jamie Madilla718c1e2014-07-02 15:31:22 -0400170 private:
Corentin Wallez71d147f2015-02-11 11:15:24 -0800171 // Creates the function call DAG for further analysis, returning false if there is a recursion
172 bool initCallDag(TIntermNode *root);
173 // Return false if "main" doesn't exist
174 bool tagUsedFunctions();
175 void internalTagUsedFunction(size_t index);
176
Olli Etuaho183d7e22015-11-20 15:59:09 +0200177 void initSamplerDefaultPrecision(TBasicType samplerType);
178
Corentin Walleza094a8a2015-04-07 11:53:06 -0700179 // Removes unused function declarations and prototypes from the AST
180 class UnusedPredicate;
181 bool pruneUnusedFunctions(TIntermNode *root);
182
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300183 TIntermNode *compileTreeImpl(const char *const shaderStrings[],
184 size_t numStrings,
185 const int compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200186
Jamie Madill183bde52014-07-02 15:31:19 -0400187 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000188 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400189 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000190
Corentin Wallez71d147f2015-02-11 11:15:24 -0800191 struct FunctionMetadata
192 {
193 FunctionMetadata()
194 : used(false)
195 {
196 }
197 bool used;
198 };
199
200 CallDAG mCallDag;
201 std::vector<FunctionMetadata> functionMetadata;
202
gman@chromium.org8d804792012-10-17 21:33:48 +0000203 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400204 int maxExpressionComplexity;
205 int maxCallStackDepth;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200206 int maxFunctionParameters;
gman@chromium.org8d804792012-10-17 21:33:48 +0000207
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000208 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400209 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000210
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000211 // Built-in symbol table for the given language, spec, and resources.
212 // It is preserved from compile-to-compile.
213 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000214 // Built-in extensions with default behavior.
215 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000216 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000217
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000218 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000219 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200220 BuiltInFunctionEmulator builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000221
alokp@chromium.org07620a52010-09-23 17:53:56 +0000222 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000223 int shaderVersion;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000224 TInfoSink infoSink; // Output sink.
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200225 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000226
Martin Radev802abe02016-08-04 17:48:32 +0300227 // compute shader local group size
228 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300229 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300230
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000231 // name hashing.
232 ShHashFunction64 hashFunction;
233 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700234
235 TPragma mPragma;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700236
237 unsigned int mTemporaryIndex;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238};
239
240//
241// This is the interface between the machine independent code
242// and the machine dependent code.
243//
244// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400245// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246// destroy the machine dependent objects, which contain the
247// above machine independent information.
248//
zmo@google.com5601ea02011-06-10 18:23:25 +0000249TCompiler* ConstructCompiler(
Jamie Madill183bde52014-07-02 15:31:19 -0400250 sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251void DeleteCompiler(TCompiler*);
252
Geoff Lang0a73dd82014-11-19 16:18:08 -0500253#endif // COMPILER_TRANSLATOR_COMPILER_H_