blob: 22278199da4da94024349dc35410baba6f4b261f [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,
Jamie Madillc09ae152019-02-01 14:16:32 -0500149 ShCompileOptions compileOptions)
150 {}
Olli Etuaho89a69a02017-10-23 12:20:45 +0300151 // Translate to object code. May generate performance warnings through the diagnostics.
152 virtual void translate(TIntermBlock *root,
153 ShCompileOptions compileOptions,
154 PerformanceDiagnostics *perfDiagnostics) = 0;
zmo@google.com5601ea02011-06-10 18:23:25 +0000155 // Get built-in extensions with default behavior.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500156 const TExtensionBehavior &getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200157 const char *getSourcePath() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500158 const TPragma &getPragma() const { return mPragma; }
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800159 void writePragma(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700160 // Relies on collectVariables having been called.
161 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000162
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500163 const ArrayBoundsClamper &getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000164 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500165 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000166
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700167 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
Corentin Wallez1df16022016-10-27 08:16:56 -0400168 virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700169
Corentin Wallez1df16022016-10-27 08:16:56 -0400170 bool wereVariablesCollected() const;
Olli Etuaho6f591c92018-09-21 11:20:47 +0300171 std::vector<sh::Attribute> mAttributes;
172 std::vector<sh::OutputVariable> mOutputVariables;
173 std::vector<sh::Uniform> mUniforms;
174 std::vector<sh::Varying> mInputVaryings;
175 std::vector<sh::Varying> mOutputVaryings;
176 std::vector<sh::InterfaceBlock> mInterfaceBlocks;
177 std::vector<sh::InterfaceBlock> mUniformBlocks;
178 std::vector<sh::InterfaceBlock> mShaderStorageBlocks;
179 std::vector<sh::InterfaceBlock> mInBlocks;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300180
Jamie Madilla718c1e2014-07-02 15:31:22 -0400181 private:
Olli Etuaho6f591c92018-09-21 11:20:47 +0300182 // Initialize symbol-table with built-in symbols.
183 bool initBuiltInSymbolTable(const ShBuiltInResources &resources);
184 // Compute the string representation of the built-in resources
185 void setResourceString();
186 // Return false if the call depth is exceeded.
187 bool checkCallDepth();
188 // Insert statements to reference all members in unused uniform blocks with standard and shared
189 // layout. This is to work around a Mac driver that treats unused standard/shared
190 // uniform blocks as inactive.
191 void useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
192 // Insert statements to initialize output variables in the beginning of main().
193 // This is to avoid undefined behaviors.
194 void initializeOutputVariables(TIntermBlock *root);
195 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
196 // It is to work around a Linux driver bug where missing this causes compile failure
197 // while spec says it is allowed.
198 // This function should only be applied to vertex shaders.
199 void initializeGLPosition(TIntermBlock *root);
200 // Return true if the maximum expression complexity is below the limit.
201 bool limitExpressionComplexity(TIntermBlock *root);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800202 // Creates the function call DAG for further analysis, returning false if there is a recursion
203 bool initCallDag(TIntermNode *root);
204 // Return false if "main" doesn't exist
205 bool tagUsedFunctions();
206 void internalTagUsedFunction(size_t index);
207
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800208 void collectInterfaceBlocks();
209
Olli Etuaho6f591c92018-09-21 11:20:47 +0300210 bool mVariablesCollected;
Corentin Wallez1df16022016-10-27 08:16:56 -0400211
Olli Etuahob12040c2017-06-27 14:20:45 +0300212 bool mGLPositionInitialized;
213
Corentin Walleza094a8a2015-04-07 11:53:06 -0700214 // Removes unused function declarations and prototypes from the AST
215 class UnusedPredicate;
Olli Etuahod10cf692017-11-02 11:06:14 +0200216 void pruneUnusedFunctions(TIntermBlock *root);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700217
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100218 TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
219 size_t numStrings,
220 const ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200221
Olli Etuahod10cf692017-11-02 11:06:14 +0200222 // Fetches and stores shader metadata that is not stored within the AST itself, such as shader
223 // version.
224 void setASTMetadata(const TParseContext &parseContext);
225
Jiawei Shaoe41df652018-02-09 14:31:39 +0800226 // Check if shader version meets the requirement.
227 bool checkShaderVersion(TParseContext *parseContext);
228
Olli Etuahod10cf692017-11-02 11:06:14 +0200229 // Does checks that need to be run after parsing is complete and returns true if they pass.
230 bool checkAndSimplifyAST(TIntermBlock *root,
231 const TParseContext &parseContext,
232 ShCompileOptions compileOptions);
233
Olli Etuaho6f591c92018-09-21 11:20:47 +0300234 sh::GLenum mShaderType;
235 ShShaderSpec mShaderSpec;
236 ShShaderOutput mOutputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000237
Corentin Wallez71d147f2015-02-11 11:15:24 -0800238 struct FunctionMetadata
239 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500240 FunctionMetadata() : used(false) {}
Corentin Wallez71d147f2015-02-11 11:15:24 -0800241 bool used;
242 };
243
244 CallDAG mCallDag;
Olli Etuaho6f591c92018-09-21 11:20:47 +0300245 std::vector<FunctionMetadata> mFunctionMetadata;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800246
Olli Etuaho6f591c92018-09-21 11:20:47 +0300247 ShBuiltInResources mResources;
248 std::string mBuiltInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000249
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000250 // Built-in symbol table for the given language, spec, and resources.
251 // It is preserved from compile-to-compile.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300252 TSymbolTable mSymbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000253 // Built-in extensions with default behavior.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300254 TExtensionBehavior mExtensionBehavior;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000255
Olli Etuaho6f591c92018-09-21 11:20:47 +0300256 ArrayBoundsClamper mArrayBoundsClamper;
257 BuiltInFunctionEmulator mBuiltInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000258
alokp@chromium.org07620a52010-09-23 17:53:56 +0000259 // Results of compilation.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300260 int mShaderVersion;
261 TInfoSink mInfoSink; // Output sink.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000262 TDiagnostics mDiagnostics;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500263 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000264
Martin Radev802abe02016-08-04 17:48:32 +0300265 // compute shader local group size
266 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300267 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300268
Olli Etuaho09b04a22016-12-15 13:30:26 +0000269 // GL_OVR_multiview num_views.
270 int mNumViews;
271
Shaob5cc1192017-07-06 10:47:20 +0800272 // geometry shader parameters.
273 int mGeometryShaderMaxVertices;
274 int mGeometryShaderInvocations;
275 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
276 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
277
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000278 // name hashing.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300279 NameMap mNameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700280
281 TPragma mPragma;
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400282
283 // Track what should be validated given passes currently applied.
284 ValidateASTOptions mValidateASTOptions;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285};
286
287//
288// This is the interface between the machine independent code
289// and the machine dependent code.
290//
291// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400292// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293// destroy the machine dependent objects, which contain the
294// above machine independent information.
295//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
297void DeleteCompiler(TCompiler *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
Jamie Madillacb4b812016-11-07 13:50:29 -0500299} // namespace sh
300
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500301#endif // COMPILER_TRANSLATOR_COMPILER_H_