blob: 86590b31272eedc7e3558c2d25ead80864183080 [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"
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -040027#include "compiler/translator/ValidateAST.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000028#include "third_party/compiler/ArrayBoundsClamper.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029
Jamie Madillacb4b812016-11-07 13:50:29 -050030namespace sh
31{
32
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033class TCompiler;
Olli Etuahod10cf692017-11-02 11:06:14 +020034class TParseContext;
Daniel Bratell73941de2015-02-25 14:34:49 +010035#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000036class TranslatorHLSL;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050037#endif // ANGLE_ENABLE_HLSL
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +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);
Qiankun Miao705a9192016-08-29 10:05:27 +080043bool IsGLSL420OrNewer(ShShaderOutput output);
Zhenyao Mob7bf7422016-11-08 14:44:05 -080044bool IsGLSL410OrOlder(ShShaderOutput output);
Qingqing Dengad0d0792015-04-08 14:25:06 -070045
46//
Qiankun Miao89dd8f32016-11-09 12:59:30 +000047// Helper function to check if the invariant qualifier can be removed.
48//
49bool RemoveInvariant(sh::GLenum shaderType,
50 int shaderVersion,
51 ShShaderOutput outputType,
52 ShCompileOptions compileOptions);
53
54//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055// The base class used to back handles returned to the driver.
56//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050057class TShHandleBase
58{
59 public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000060 TShHandleBase();
61 virtual ~TShHandleBase();
Jamie Madilld7b1ab52016-12-12 14:42:19 -050062 virtual TCompiler *getAsCompiler() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010063#ifdef ANGLE_ENABLE_HLSL
Jamie Madilld7b1ab52016-12-12 14:42:19 -050064 virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }
65#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000066
Jamie Madilld7b1ab52016-12-12 14:42:19 -050067 protected:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000068 // Memory allocator. Allocates and tracks memory required by the compiler.
69 // Deallocates all memory when compiler is destructed.
Tobin Ehlis47ca1b22019-01-23 16:11:41 +000070 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000071};
72
73//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074// The base class for the machine dependent compiler to derive from
75// for managing object code from the compile.
76//
Jamie Madilla718c1e2014-07-02 15:31:22 -040077class TCompiler : public TShHandleBase
78{
79 public:
Jamie Madill183bde52014-07-02 15:31:19 -040080 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
Corentin Walleze5a1f272015-08-21 02:58:25 +020081 ~TCompiler() override;
82 TCompiler *getAsCompiler() override { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000083
Jamie Madilld7b1ab52016-12-12 14:42:19 -050084 bool Init(const ShBuiltInResources &resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020085
86 // compileTreeForTesting should be used only when tests require access to
87 // the AST. Users of this function need to manually manage the global pool
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010088 // allocator. Returns nullptr whenever there are compilation errors.
89 TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
90 size_t numStrings,
91 ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020092
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080093 bool compile(const char *const shaderStrings[],
94 size_t numStrings,
95 ShCompileOptions compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000096
97 // Get results of the last compilation.
Olli Etuaho6f591c92018-09-21 11:20:47 +030098 int getShaderVersion() const { return mShaderVersion; }
99 TInfoSink &getInfoSink() { return mInfoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -0400100
Martin Radev802abe02016-08-04 17:48:32 +0300101 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Olli Etuaho09b04a22016-12-15 13:30:26 +0000102 const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; }
103 int getNumViews() const { return mNumViews; }
Martin Radev802abe02016-08-04 17:48:32 +0300104
Dmitry Skiba2539fff2015-06-16 17:56:09 -0700105 // Clears the results from the previous compilation.
106 void clearResults();
107
Olli Etuaho6f591c92018-09-21 11:20:47 +0300108 const std::vector<sh::Attribute> &getAttributes() const { return mAttributes; }
109 const std::vector<sh::OutputVariable> &getOutputVariables() const { return mOutputVariables; }
110 const std::vector<sh::Uniform> &getUniforms() const { return mUniforms; }
111 const std::vector<sh::Varying> &getInputVaryings() const { return mInputVaryings; }
112 const std::vector<sh::Varying> &getOutputVaryings() const { return mOutputVaryings; }
113 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; }
114 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; }
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800115 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const
116 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300117 return mShaderStorageBlocks;
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800118 }
Olli Etuaho6f591c92018-09-21 11:20:47 +0300119 const std::vector<sh::InterfaceBlock> &getInBlocks() const { return mInBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000120
Olli Etuaho6f591c92018-09-21 11:20:47 +0300121 ShHashFunction64 getHashFunction() const { return mResources.HashFunction; }
122 NameMap &getNameMap() { return mNameMap; }
123 TSymbolTable &getSymbolTable() { return mSymbolTable; }
124 ShShaderSpec getShaderSpec() const { return mShaderSpec; }
125 ShShaderOutput getOutputType() const { return mOutputType; }
126 const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000127
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800128 bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300129
Jamie Madill54ad4f82014-09-03 09:40:46 -0400130 // Get the resources set by InitBuiltInSymbolTable
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500131 const ShBuiltInResources &getResources() const;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400132
Shaob5cc1192017-07-06 10:47:20 +0800133 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }
134 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; }
135 TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const
136 {
137 return mGeometryShaderInputPrimitiveType;
138 }
139 TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const
140 {
141 return mGeometryShaderOutputPrimitiveType;
142 }
143
Olli Etuaho6f591c92018-09-21 11:20:47 +0300144 sh::GLenum getShaderType() const { return mShaderType; }
Shaob5cc1192017-07-06 10:47:20 +0800145
146 protected:
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200147 // Add emulated functions to the built-in function emulator.
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800148 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
149 ShCompileOptions compileOptions){};
Olli Etuaho89a69a02017-10-23 12:20:45 +0300150 // Translate to object code. May generate performance warnings through the diagnostics.
151 virtual void translate(TIntermBlock *root,
152 ShCompileOptions compileOptions,
153 PerformanceDiagnostics *perfDiagnostics) = 0;
zmo@google.com5601ea02011-06-10 18:23:25 +0000154 // Get built-in extensions with default behavior.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500155 const TExtensionBehavior &getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200156 const char *getSourcePath() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500157 const TPragma &getPragma() const { return mPragma; }
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800158 void writePragma(ShCompileOptions compileOptions);
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
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500162 const ArrayBoundsClamper &getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000163 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500164 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000165
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700166 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
Corentin Wallez1df16022016-10-27 08:16:56 -0400167 virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700168
Corentin Wallez1df16022016-10-27 08:16:56 -0400169 bool wereVariablesCollected() const;
Olli Etuaho6f591c92018-09-21 11:20:47 +0300170 std::vector<sh::Attribute> mAttributes;
171 std::vector<sh::OutputVariable> mOutputVariables;
172 std::vector<sh::Uniform> mUniforms;
173 std::vector<sh::Varying> mInputVaryings;
174 std::vector<sh::Varying> mOutputVaryings;
175 std::vector<sh::InterfaceBlock> mInterfaceBlocks;
176 std::vector<sh::InterfaceBlock> mUniformBlocks;
177 std::vector<sh::InterfaceBlock> mShaderStorageBlocks;
178 std::vector<sh::InterfaceBlock> mInBlocks;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300179
Jamie Madilla718c1e2014-07-02 15:31:22 -0400180 private:
Olli Etuaho6f591c92018-09-21 11:20:47 +0300181 // Initialize symbol-table with built-in symbols.
182 bool initBuiltInSymbolTable(const ShBuiltInResources &resources);
183 // Compute the string representation of the built-in resources
184 void setResourceString();
185 // Return false if the call depth is exceeded.
186 bool checkCallDepth();
187 // Insert statements to reference all members in unused uniform blocks with standard and shared
188 // layout. This is to work around a Mac driver that treats unused standard/shared
189 // uniform blocks as inactive.
190 void useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
191 // Insert statements to initialize output variables in the beginning of main().
192 // This is to avoid undefined behaviors.
193 void initializeOutputVariables(TIntermBlock *root);
194 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
195 // It is to work around a Linux driver bug where missing this causes compile failure
196 // while spec says it is allowed.
197 // This function should only be applied to vertex shaders.
198 void initializeGLPosition(TIntermBlock *root);
199 // Return true if the maximum expression complexity is below the limit.
200 bool limitExpressionComplexity(TIntermBlock *root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800201 // Creates the function call DAG for further analysis, returning false if there is a recursion
202 bool initCallDag(TIntermNode *root);
203 // Return false if "main" doesn't exist
204 bool tagUsedFunctions();
205 void internalTagUsedFunction(size_t index);
206
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800207 void collectInterfaceBlocks();
208
Olli Etuaho6f591c92018-09-21 11:20:47 +0300209 bool mVariablesCollected;
Corentin Wallez1df16022016-10-27 08:16:56 -0400210
Olli Etuahob12040c2017-06-27 14:20:45 +0300211 bool mGLPositionInitialized;
212
Corentin Walleza094a8a2015-04-07 11:53:06 -0700213 // Removes unused function declarations and prototypes from the AST
214 class UnusedPredicate;
Olli Etuahod10cf692017-11-02 11:06:14 +0200215 void pruneUnusedFunctions(TIntermBlock *root);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700216
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100217 TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
218 size_t numStrings,
219 const ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200220
Olli Etuahod10cf692017-11-02 11:06:14 +0200221 // Fetches and stores shader metadata that is not stored within the AST itself, such as shader
222 // version.
223 void setASTMetadata(const TParseContext &parseContext);
224
Jiawei Shaoe41df652018-02-09 14:31:39 +0800225 // Check if shader version meets the requirement.
226 bool checkShaderVersion(TParseContext *parseContext);
227
Olli Etuahod10cf692017-11-02 11:06:14 +0200228 // Does checks that need to be run after parsing is complete and returns true if they pass.
229 bool checkAndSimplifyAST(TIntermBlock *root,
230 const TParseContext &parseContext,
231 ShCompileOptions compileOptions);
232
Olli Etuaho6f591c92018-09-21 11:20:47 +0300233 sh::GLenum mShaderType;
234 ShShaderSpec mShaderSpec;
235 ShShaderOutput mOutputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000236
Corentin Wallez71d147f2015-02-11 11:15:24 -0800237 struct FunctionMetadata
238 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500239 FunctionMetadata() : used(false) {}
Corentin Wallez71d147f2015-02-11 11:15:24 -0800240 bool used;
241 };
242
243 CallDAG mCallDag;
Olli Etuaho6f591c92018-09-21 11:20:47 +0300244 std::vector<FunctionMetadata> mFunctionMetadata;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800245
Olli Etuaho6f591c92018-09-21 11:20:47 +0300246 ShBuiltInResources mResources;
247 std::string mBuiltInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000248
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000249 // Built-in symbol table for the given language, spec, and resources.
250 // It is preserved from compile-to-compile.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300251 TSymbolTable mSymbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000252 // Built-in extensions with default behavior.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300253 TExtensionBehavior mExtensionBehavior;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000254
Olli Etuaho6f591c92018-09-21 11:20:47 +0300255 ArrayBoundsClamper mArrayBoundsClamper;
256 BuiltInFunctionEmulator mBuiltInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000257
alokp@chromium.org07620a52010-09-23 17:53:56 +0000258 // Results of compilation.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300259 int mShaderVersion;
260 TInfoSink mInfoSink; // Output sink.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000261 TDiagnostics mDiagnostics;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500262 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000263
Martin Radev802abe02016-08-04 17:48:32 +0300264 // compute shader local group size
265 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300266 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300267
Olli Etuaho09b04a22016-12-15 13:30:26 +0000268 // GL_OVR_multiview num_views.
269 int mNumViews;
270
Shaob5cc1192017-07-06 10:47:20 +0800271 // geometry shader parameters.
272 int mGeometryShaderMaxVertices;
273 int mGeometryShaderInvocations;
274 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
275 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
276
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000277 // name hashing.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300278 NameMap mNameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700279
280 TPragma mPragma;
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400281
282 // Track what should be validated given passes currently applied.
283 ValidateASTOptions mValidateASTOptions;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284};
285
286//
287// This is the interface between the machine independent code
288// and the machine dependent code.
289//
290// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400291// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292// destroy the machine dependent objects, which contain the
293// above machine independent information.
294//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500295TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
296void DeleteCompiler(TCompiler *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
Jamie Madillacb4b812016-11-07 13:50:29 -0500298} // namespace sh
299
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500300#endif // COMPILER_TRANSLATOR_COMPILER_H_