blob: 2dc0fe63f715bbc0a4d047753f9e4d42cf544502 [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.
69 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.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +000097 int getShaderVersion() const { return shaderVersion; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050098 TInfoSink &getInfoSink() { return infoSink; }
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
Jamie Madilled27c722014-07-02 15:31:23 -0400107 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
Jamie Madilla0a9e122015-09-02 15:54:30 -0400108 const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -0400109 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800110 const std::vector<sh::Varying> &getInputVaryings() const { return inputVaryings; }
111 const std::vector<sh::Varying> &getOutputVaryings() const { return outputVaryings; }
Jamie Madilled27c722014-07-02 15:31:23 -0400112 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800113 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return uniformBlocks; }
114 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const
115 {
116 return shaderStorageBlocks;
117 }
Jiawei Shaod8105a02017-08-08 09:54:36 +0800118 const std::vector<sh::InterfaceBlock> &getInBlocks() const { return inBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000119
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000120 ShHashFunction64 getHashFunction() const { return hashFunction; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500121 NameMap &getNameMap() { return nameMap; }
122 TSymbolTable &getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700123 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -0400124 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -0700125 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
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
Jamie Madill183bde52014-07-02 15:31:19 -0400143 sh::GLenum getShaderType() const { return shaderType; }
Shaob5cc1192017-07-06 10:47:20 +0800144
145 protected:
alokp@chromium.org07620a52010-09-23 17:53:56 +0000146 // Initialize symbol-table with built-in symbols.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500147 bool InitBuiltInSymbolTable(const ShBuiltInResources &resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400148 // Compute the string representation of the built-in resources
149 void setResourceString();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800150 // Return false if the call depth is exceeded.
151 bool checkCallDepth();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200152 // Add emulated functions to the built-in function emulator.
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800153 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
154 ShCompileOptions compileOptions){};
Olli Etuaho89a69a02017-10-23 12:20:45 +0300155 // Translate to object code. May generate performance warnings through the diagnostics.
156 virtual void translate(TIntermBlock *root,
157 ShCompileOptions compileOptions,
158 PerformanceDiagnostics *perfDiagnostics) = 0;
Qin Jiajia7835b522016-10-08 11:20:17 +0800159 // Insert statements to reference all members in unused uniform blocks with standard and shared
160 // layout. This is to work around a Mac driver that treats unused standard/shared
161 // uniform blocks as inactive.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300162 void useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700163 // Insert statements to initialize output variables in the beginning of main().
164 // This is to avoid undefined behaviors.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300165 void initializeOutputVariables(TIntermBlock *root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800166 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
167 // It is to work around a Linux driver bug where missing this causes compile failure
168 // while spec says it is allowed.
169 // This function should only be applied to vertex shaders.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300170 void initializeGLPosition(TIntermBlock *root);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400171 // Return true if the maximum expression complexity is below the limit.
Olli Etuahob4279202017-05-18 15:08:24 +0300172 bool limitExpressionComplexity(TIntermBlock *root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000173 // Get built-in extensions with default behavior.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500174 const TExtensionBehavior &getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200175 const char *getSourcePath() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500176 const TPragma &getPragma() const { return mPragma; }
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800177 void writePragma(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700178 // Relies on collectVariables having been called.
179 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000180
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500181 const ArrayBoundsClamper &getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000182 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500183 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000184
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700185 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
Corentin Wallez1df16022016-10-27 08:16:56 -0400186 virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700187
Corentin Wallez1df16022016-10-27 08:16:56 -0400188 bool wereVariablesCollected() const;
Jamie Madilled27c722014-07-02 15:31:23 -0400189 std::vector<sh::Attribute> attributes;
Jamie Madilla0a9e122015-09-02 15:54:30 -0400190 std::vector<sh::OutputVariable> outputVariables;
Jamie Madilled27c722014-07-02 15:31:23 -0400191 std::vector<sh::Uniform> uniforms;
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800192 std::vector<sh::Varying> inputVaryings;
193 std::vector<sh::Varying> outputVaryings;
Jamie Madilled27c722014-07-02 15:31:23 -0400194 std::vector<sh::InterfaceBlock> interfaceBlocks;
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800195 std::vector<sh::InterfaceBlock> uniformBlocks;
196 std::vector<sh::InterfaceBlock> shaderStorageBlocks;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800197 std::vector<sh::InterfaceBlock> inBlocks;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300198
Jamie Madilla718c1e2014-07-02 15:31:22 -0400199 private:
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
Olli Etuaho183d7e22015-11-20 15:59:09 +0200206 void initSamplerDefaultPrecision(TBasicType samplerType);
207
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800208 void collectInterfaceBlocks();
209
Corentin Wallez1df16022016-10-27 08:16:56 -0400210 bool variablesCollected;
211
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
Jamie Madill183bde52014-07-02 15:31:19 -0400234 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000235 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400236 ShShaderOutput outputType;
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;
245 std::vector<FunctionMetadata> functionMetadata;
246
gman@chromium.org8d804792012-10-17 21:33:48 +0000247 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400248 int maxExpressionComplexity;
249 int maxCallStackDepth;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200250 int maxFunctionParameters;
gman@chromium.org8d804792012-10-17 21:33:48 +0000251
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000252 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400253 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000254
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000255 // Built-in symbol table for the given language, spec, and resources.
256 // It is preserved from compile-to-compile.
257 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000258 // Built-in extensions with default behavior.
259 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000260 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000261
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000262 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000263 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200264 BuiltInFunctionEmulator builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000265
alokp@chromium.org07620a52010-09-23 17:53:56 +0000266 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000267 int shaderVersion;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500268 TInfoSink infoSink; // Output sink.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000269 TDiagnostics mDiagnostics;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500270 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000271
Martin Radev802abe02016-08-04 17:48:32 +0300272 // compute shader local group size
273 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300274 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300275
Olli Etuaho09b04a22016-12-15 13:30:26 +0000276 // GL_OVR_multiview num_views.
277 int mNumViews;
278
Shaob5cc1192017-07-06 10:47:20 +0800279 // geometry shader parameters.
280 int mGeometryShaderMaxVertices;
281 int mGeometryShaderInvocations;
282 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
283 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
284
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000285 // name hashing.
286 ShHashFunction64 hashFunction;
287 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700288
289 TPragma mPragma;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290};
291
292//
293// This is the interface between the machine independent code
294// and the machine dependent code.
295//
296// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400297// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298// destroy the machine dependent objects, which contain the
299// above machine independent information.
300//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500301TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
302void DeleteCompiler(TCompiler *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
Jamie Madillacb4b812016-11-07 13:50:29 -0500304} // namespace sh
305
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500306#endif // COMPILER_TRANSLATOR_COMPILER_H_