blob: 2eef38741649c338648e6c00f9dba109b8ca05d4 [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 Etuaho78ed6cd2017-08-09 16:19:00 +030017#include <GLSLANG/ShaderVars.h>
18
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020019#include "compiler/translator/BuiltInFunctionEmulator.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080020#include "compiler/translator/CallDAG.h"
Olli Etuaho77ba4082016-12-16 12:01:18 +000021#include "compiler/translator/Diagnostics.h"
Geoff Lang17732822013-08-29 13:46:49 -040022#include "compiler/translator/ExtensionBehavior.h"
23#include "compiler/translator/HashNames.h"
24#include "compiler/translator/InfoSink.h"
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070025#include "compiler/translator/Pragma.h"
Geoff Lang17732822013-08-29 13:46:49 -040026#include "compiler/translator/SymbolTable.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000027#include "third_party/compiler/ArrayBoundsClamper.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028
Jamie Madillacb4b812016-11-07 13:50:29 -050029namespace sh
30{
31
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032class TCompiler;
Olli Etuahod10cf692017-11-02 11:06:14 +020033class TParseContext;
Daniel Bratell73941de2015-02-25 14:34:49 +010034#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000035class TranslatorHLSL;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050036#endif // ANGLE_ENABLE_HLSL
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037
38//
Qingqing Dengad0d0792015-04-08 14:25:06 -070039// Helper function to check if the shader type is GLSL.
40//
41bool IsGLSL130OrNewer(ShShaderOutput output);
Qiankun Miao705a9192016-08-29 10:05:27 +080042bool IsGLSL420OrNewer(ShShaderOutput output);
Zhenyao Mob7bf7422016-11-08 14:44:05 -080043bool IsGLSL410OrOlder(ShShaderOutput output);
Qingqing Dengad0d0792015-04-08 14:25:06 -070044
45//
Qiankun Miao89dd8f32016-11-09 12:59:30 +000046// Helper function to check if the invariant qualifier can be removed.
47//
48bool RemoveInvariant(sh::GLenum shaderType,
49 int shaderVersion,
50 ShShaderOutput outputType,
51 ShCompileOptions compileOptions);
52
53//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054// The base class used to back handles returned to the driver.
55//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050056class TShHandleBase
57{
58 public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000059 TShHandleBase();
60 virtual ~TShHandleBase();
Jamie Madilld7b1ab52016-12-12 14:42:19 -050061 virtual TCompiler *getAsCompiler() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010062#ifdef ANGLE_ENABLE_HLSL
Jamie Madilld7b1ab52016-12-12 14:42:19 -050063 virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }
64#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000065
Jamie Madilld7b1ab52016-12-12 14:42:19 -050066 protected:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000067 // Memory allocator. Allocates and tracks memory required by the compiler.
68 // Deallocates all memory when compiler is destructed.
Tobin Ehlis47ca1b22019-01-23 16:11:41 +000069 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070};
71
72//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073// The base class for the machine dependent compiler to derive from
74// for managing object code from the compile.
75//
Jamie Madilla718c1e2014-07-02 15:31:22 -040076class TCompiler : public TShHandleBase
77{
78 public:
Jamie Madill183bde52014-07-02 15:31:19 -040079 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
Corentin Walleze5a1f272015-08-21 02:58:25 +020080 ~TCompiler() override;
81 TCompiler *getAsCompiler() override { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000082
Jamie Madilld7b1ab52016-12-12 14:42:19 -050083 bool Init(const ShBuiltInResources &resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020084
85 // compileTreeForTesting should be used only when tests require access to
86 // the AST. Users of this function need to manually manage the global pool
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010087 // allocator. Returns nullptr whenever there are compilation errors.
88 TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
89 size_t numStrings,
90 ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020091
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080092 bool compile(const char *const shaderStrings[],
93 size_t numStrings,
94 ShCompileOptions compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000095
96 // Get results of the last compilation.
Olli Etuaho6f591c92018-09-21 11:20:47 +030097 int getShaderVersion() const { return mShaderVersion; }
98 TInfoSink &getInfoSink() { return mInfoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -040099
Martin Radev802abe02016-08-04 17:48:32 +0300100 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Olli Etuaho09b04a22016-12-15 13:30:26 +0000101 const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; }
102 int getNumViews() const { return mNumViews; }
Martin Radev802abe02016-08-04 17:48:32 +0300103
Dmitry Skiba2539fff2015-06-16 17:56:09 -0700104 // Clears the results from the previous compilation.
105 void clearResults();
106
Olli Etuaho6f591c92018-09-21 11:20:47 +0300107 const std::vector<sh::Attribute> &getAttributes() const { return mAttributes; }
108 const std::vector<sh::OutputVariable> &getOutputVariables() const { return mOutputVariables; }
109 const std::vector<sh::Uniform> &getUniforms() const { return mUniforms; }
110 const std::vector<sh::Varying> &getInputVaryings() const { return mInputVaryings; }
111 const std::vector<sh::Varying> &getOutputVaryings() const { return mOutputVaryings; }
112 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; }
113 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; }
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800114 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const
115 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300116 return mShaderStorageBlocks;
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800117 }
Olli Etuaho6f591c92018-09-21 11:20:47 +0300118 const std::vector<sh::InterfaceBlock> &getInBlocks() const { return mInBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000119
Olli Etuaho6f591c92018-09-21 11:20:47 +0300120 ShHashFunction64 getHashFunction() const { return mResources.HashFunction; }
121 NameMap &getNameMap() { return mNameMap; }
122 TSymbolTable &getSymbolTable() { return mSymbolTable; }
123 ShShaderSpec getShaderSpec() const { return mShaderSpec; }
124 ShShaderOutput getOutputType() const { return mOutputType; }
125 const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000126
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800127 bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300128
Jamie Madill54ad4f82014-09-03 09:40:46 -0400129 // Get the resources set by InitBuiltInSymbolTable
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500130 const ShBuiltInResources &getResources() const;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400131
Shaob5cc1192017-07-06 10:47:20 +0800132 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }
133 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; }
134 TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const
135 {
136 return mGeometryShaderInputPrimitiveType;
137 }
138 TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const
139 {
140 return mGeometryShaderOutputPrimitiveType;
141 }
142
Olli Etuaho6f591c92018-09-21 11:20:47 +0300143 sh::GLenum getShaderType() const { return mShaderType; }
Shaob5cc1192017-07-06 10:47:20 +0800144
145 protected:
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200146 // Add emulated functions to the built-in function emulator.
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800147 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
148 ShCompileOptions compileOptions){};
Olli Etuaho89a69a02017-10-23 12:20:45 +0300149 // Translate to object code. May generate performance warnings through the diagnostics.
150 virtual void translate(TIntermBlock *root,
151 ShCompileOptions compileOptions,
152 PerformanceDiagnostics *perfDiagnostics) = 0;
zmo@google.com5601ea02011-06-10 18:23:25 +0000153 // Get built-in extensions with default behavior.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500154 const TExtensionBehavior &getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200155 const char *getSourcePath() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500156 const TPragma &getPragma() const { return mPragma; }
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800157 void writePragma(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700158 // Relies on collectVariables having been called.
159 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000160
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500161 const ArrayBoundsClamper &getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000162 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500163 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000164
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700165 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
Corentin Wallez1df16022016-10-27 08:16:56 -0400166 virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700167
Corentin Wallez1df16022016-10-27 08:16:56 -0400168 bool wereVariablesCollected() const;
Olli Etuaho6f591c92018-09-21 11:20:47 +0300169 std::vector<sh::Attribute> mAttributes;
170 std::vector<sh::OutputVariable> mOutputVariables;
171 std::vector<sh::Uniform> mUniforms;
172 std::vector<sh::Varying> mInputVaryings;
173 std::vector<sh::Varying> mOutputVaryings;
174 std::vector<sh::InterfaceBlock> mInterfaceBlocks;
175 std::vector<sh::InterfaceBlock> mUniformBlocks;
176 std::vector<sh::InterfaceBlock> mShaderStorageBlocks;
177 std::vector<sh::InterfaceBlock> mInBlocks;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300178
Jamie Madilla718c1e2014-07-02 15:31:22 -0400179 private:
Olli Etuaho6f591c92018-09-21 11:20:47 +0300180 // Initialize symbol-table with built-in symbols.
181 bool initBuiltInSymbolTable(const ShBuiltInResources &resources);
182 // Compute the string representation of the built-in resources
183 void setResourceString();
184 // Return false if the call depth is exceeded.
185 bool checkCallDepth();
186 // Insert statements to reference all members in unused uniform blocks with standard and shared
187 // layout. This is to work around a Mac driver that treats unused standard/shared
188 // uniform blocks as inactive.
189 void useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
190 // Insert statements to initialize output variables in the beginning of main().
191 // This is to avoid undefined behaviors.
192 void initializeOutputVariables(TIntermBlock *root);
193 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
194 // It is to work around a Linux driver bug where missing this causes compile failure
195 // while spec says it is allowed.
196 // This function should only be applied to vertex shaders.
197 void initializeGLPosition(TIntermBlock *root);
198 // Return true if the maximum expression complexity is below the limit.
199 bool limitExpressionComplexity(TIntermBlock *root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800200 // Creates the function call DAG for further analysis, returning false if there is a recursion
201 bool initCallDag(TIntermNode *root);
202 // Return false if "main" doesn't exist
203 bool tagUsedFunctions();
204 void internalTagUsedFunction(size_t index);
205
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800206 void collectInterfaceBlocks();
207
Olli Etuaho6f591c92018-09-21 11:20:47 +0300208 bool mVariablesCollected;
Corentin Wallez1df16022016-10-27 08:16:56 -0400209
Olli Etuahob12040c2017-06-27 14:20:45 +0300210 bool mGLPositionInitialized;
211
Corentin Walleza094a8a2015-04-07 11:53:06 -0700212 // Removes unused function declarations and prototypes from the AST
213 class UnusedPredicate;
Olli Etuahod10cf692017-11-02 11:06:14 +0200214 void pruneUnusedFunctions(TIntermBlock *root);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700215
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100216 TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
217 size_t numStrings,
218 const ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200219
Olli Etuahod10cf692017-11-02 11:06:14 +0200220 // Fetches and stores shader metadata that is not stored within the AST itself, such as shader
221 // version.
222 void setASTMetadata(const TParseContext &parseContext);
223
Jiawei Shaoe41df652018-02-09 14:31:39 +0800224 // Check if shader version meets the requirement.
225 bool checkShaderVersion(TParseContext *parseContext);
226
Olli Etuahod10cf692017-11-02 11:06:14 +0200227 // Does checks that need to be run after parsing is complete and returns true if they pass.
228 bool checkAndSimplifyAST(TIntermBlock *root,
229 const TParseContext &parseContext,
230 ShCompileOptions compileOptions);
231
Olli Etuaho6f591c92018-09-21 11:20:47 +0300232 sh::GLenum mShaderType;
233 ShShaderSpec mShaderSpec;
234 ShShaderOutput mOutputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000235
Corentin Wallez71d147f2015-02-11 11:15:24 -0800236 struct FunctionMetadata
237 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500238 FunctionMetadata() : used(false) {}
Corentin Wallez71d147f2015-02-11 11:15:24 -0800239 bool used;
240 };
241
242 CallDAG mCallDag;
Olli Etuaho6f591c92018-09-21 11:20:47 +0300243 std::vector<FunctionMetadata> mFunctionMetadata;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800244
Olli Etuaho6f591c92018-09-21 11:20:47 +0300245 ShBuiltInResources mResources;
246 std::string mBuiltInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000247
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000248 // Built-in symbol table for the given language, spec, and resources.
249 // It is preserved from compile-to-compile.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300250 TSymbolTable mSymbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000251 // Built-in extensions with default behavior.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300252 TExtensionBehavior mExtensionBehavior;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000253
Olli Etuaho6f591c92018-09-21 11:20:47 +0300254 ArrayBoundsClamper mArrayBoundsClamper;
255 BuiltInFunctionEmulator mBuiltInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000256
alokp@chromium.org07620a52010-09-23 17:53:56 +0000257 // Results of compilation.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300258 int mShaderVersion;
259 TInfoSink mInfoSink; // Output sink.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000260 TDiagnostics mDiagnostics;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000262
Martin Radev802abe02016-08-04 17:48:32 +0300263 // compute shader local group size
264 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300265 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300266
Olli Etuaho09b04a22016-12-15 13:30:26 +0000267 // GL_OVR_multiview num_views.
268 int mNumViews;
269
Shaob5cc1192017-07-06 10:47:20 +0800270 // geometry shader parameters.
271 int mGeometryShaderMaxVertices;
272 int mGeometryShaderInvocations;
273 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
274 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
275
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000276 // name hashing.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300277 NameMap mNameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700278
279 TPragma mPragma;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280};
281
282//
283// This is the interface between the machine independent code
284// and the machine dependent code.
285//
286// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400287// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288// destroy the machine dependent objects, which contain the
289// above machine independent information.
290//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500291TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
292void DeleteCompiler(TCompiler *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293
Jamie Madillacb4b812016-11-07 13:50:29 -0500294} // namespace sh
295
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296#endif // COMPILER_TRANSLATOR_COMPILER_H_