blob: cd09eef0d368981118a1aca77172b20a6fd1f412 [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//
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080039// Helper function to identify specs that are based on the WebGL spec.
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000040//
Jamie Madill5508f392014-02-20 13:31:36 -050041bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000042
43//
Qingqing Dengad0d0792015-04-08 14:25:06 -070044// Helper function to check if the shader type is GLSL.
45//
46bool IsGLSL130OrNewer(ShShaderOutput output);
Qiankun Miao705a9192016-08-29 10:05:27 +080047bool IsGLSL420OrNewer(ShShaderOutput output);
Zhenyao Mob7bf7422016-11-08 14:44:05 -080048bool IsGLSL410OrOlder(ShShaderOutput output);
Qingqing Dengad0d0792015-04-08 14:25:06 -070049
50//
Qiankun Miao89dd8f32016-11-09 12:59:30 +000051// Helper function to check if the invariant qualifier can be removed.
52//
53bool RemoveInvariant(sh::GLenum shaderType,
54 int shaderVersion,
55 ShShaderOutput outputType,
56 ShCompileOptions compileOptions);
57
58//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059// The base class used to back handles returned to the driver.
60//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050061class TShHandleBase
62{
63 public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000064 TShHandleBase();
65 virtual ~TShHandleBase();
Jamie Madilld7b1ab52016-12-12 14:42:19 -050066 virtual TCompiler *getAsCompiler() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010067#ifdef ANGLE_ENABLE_HLSL
Jamie Madilld7b1ab52016-12-12 14:42:19 -050068 virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }
69#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000070
Jamie Madilld7b1ab52016-12-12 14:42:19 -050071 protected:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000072 // Memory allocator. Allocates and tracks memory required by the compiler.
73 // Deallocates all memory when compiler is destructed.
74 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075};
76
77//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078// The base class for the machine dependent compiler to derive from
79// for managing object code from the compile.
80//
Jamie Madilla718c1e2014-07-02 15:31:22 -040081class TCompiler : public TShHandleBase
82{
83 public:
Jamie Madill183bde52014-07-02 15:31:19 -040084 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
Corentin Walleze5a1f272015-08-21 02:58:25 +020085 ~TCompiler() override;
86 TCompiler *getAsCompiler() override { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000087
Jamie Madilld7b1ab52016-12-12 14:42:19 -050088 bool Init(const ShBuiltInResources &resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020089
90 // compileTreeForTesting should be used only when tests require access to
91 // the AST. Users of this function need to manually manage the global pool
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010092 // allocator. Returns nullptr whenever there are compilation errors.
93 TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
94 size_t numStrings,
95 ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020096
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080097 bool compile(const char *const shaderStrings[],
98 size_t numStrings,
99 ShCompileOptions compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000100
101 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000102 int getShaderVersion() const { return shaderVersion; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500103 TInfoSink &getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -0400104
Martin Radev802abe02016-08-04 17:48:32 +0300105 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Olli Etuaho09b04a22016-12-15 13:30:26 +0000106 const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; }
107 int getNumViews() const { return mNumViews; }
Martin Radev802abe02016-08-04 17:48:32 +0300108
Dmitry Skiba2539fff2015-06-16 17:56:09 -0700109 // Clears the results from the previous compilation.
110 void clearResults();
111
Jamie Madilled27c722014-07-02 15:31:23 -0400112 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
Jamie Madilla0a9e122015-09-02 15:54:30 -0400113 const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -0400114 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800115 const std::vector<sh::Varying> &getInputVaryings() const { return inputVaryings; }
116 const std::vector<sh::Varying> &getOutputVaryings() const { return outputVaryings; }
Jamie Madilled27c722014-07-02 15:31:23 -0400117 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800118 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return uniformBlocks; }
119 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const
120 {
121 return shaderStorageBlocks;
122 }
Jiawei Shaod8105a02017-08-08 09:54:36 +0800123 const std::vector<sh::InterfaceBlock> &getInBlocks() const { return inBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000124
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000125 ShHashFunction64 getHashFunction() const { return hashFunction; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500126 NameMap &getNameMap() { return nameMap; }
127 TSymbolTable &getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700128 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -0400129 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -0700130 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000131
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800132 bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300133
Jamie Madill54ad4f82014-09-03 09:40:46 -0400134 // Get the resources set by InitBuiltInSymbolTable
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500135 const ShBuiltInResources &getResources() const;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400136
Shaob5cc1192017-07-06 10:47:20 +0800137 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }
138 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; }
139 TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const
140 {
141 return mGeometryShaderInputPrimitiveType;
142 }
143 TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const
144 {
145 return mGeometryShaderOutputPrimitiveType;
146 }
147
Jamie Madill183bde52014-07-02 15:31:19 -0400148 sh::GLenum getShaderType() const { return shaderType; }
Shaob5cc1192017-07-06 10:47:20 +0800149
150 protected:
alokp@chromium.org07620a52010-09-23 17:53:56 +0000151 // Initialize symbol-table with built-in symbols.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500152 bool InitBuiltInSymbolTable(const ShBuiltInResources &resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400153 // Compute the string representation of the built-in resources
154 void setResourceString();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800155 // Return false if the call depth is exceeded.
156 bool checkCallDepth();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200157 // Add emulated functions to the built-in function emulator.
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800158 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
159 ShCompileOptions compileOptions){};
Olli Etuaho89a69a02017-10-23 12:20:45 +0300160 // Translate to object code. May generate performance warnings through the diagnostics.
161 virtual void translate(TIntermBlock *root,
162 ShCompileOptions compileOptions,
163 PerformanceDiagnostics *perfDiagnostics) = 0;
Qin Jiajia7835b522016-10-08 11:20:17 +0800164 // Insert statements to reference all members in unused uniform blocks with standard and shared
165 // layout. This is to work around a Mac driver that treats unused standard/shared
166 // uniform blocks as inactive.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300167 void useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700168 // Insert statements to initialize output variables in the beginning of main().
169 // This is to avoid undefined behaviors.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300170 void initializeOutputVariables(TIntermBlock *root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800171 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
172 // It is to work around a Linux driver bug where missing this causes compile failure
173 // while spec says it is allowed.
174 // This function should only be applied to vertex shaders.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300175 void initializeGLPosition(TIntermBlock *root);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400176 // Return true if the maximum expression complexity is below the limit.
Olli Etuahob4279202017-05-18 15:08:24 +0300177 bool limitExpressionComplexity(TIntermBlock *root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000178 // Get built-in extensions with default behavior.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500179 const TExtensionBehavior &getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200180 const char *getSourcePath() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500181 const TPragma &getPragma() const { return mPragma; }
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800182 void writePragma(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700183 // Relies on collectVariables having been called.
184 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000185
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500186 const ArrayBoundsClamper &getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000187 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500188 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000189
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700190 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
Corentin Wallez1df16022016-10-27 08:16:56 -0400191 virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300192 virtual bool needToInitializeGlobalsInAST() const { return IsWebGLBasedSpec(shaderSpec); }
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700193
Corentin Wallez1df16022016-10-27 08:16:56 -0400194 bool wereVariablesCollected() const;
Jamie Madilled27c722014-07-02 15:31:23 -0400195 std::vector<sh::Attribute> attributes;
Jamie Madilla0a9e122015-09-02 15:54:30 -0400196 std::vector<sh::OutputVariable> outputVariables;
Jamie Madilled27c722014-07-02 15:31:23 -0400197 std::vector<sh::Uniform> uniforms;
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800198 std::vector<sh::Varying> inputVaryings;
199 std::vector<sh::Varying> outputVaryings;
Jamie Madilled27c722014-07-02 15:31:23 -0400200 std::vector<sh::InterfaceBlock> interfaceBlocks;
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800201 std::vector<sh::InterfaceBlock> uniformBlocks;
202 std::vector<sh::InterfaceBlock> shaderStorageBlocks;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800203 std::vector<sh::InterfaceBlock> inBlocks;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300204
Jamie Madilla718c1e2014-07-02 15:31:22 -0400205 private:
Corentin Wallez71d147f2015-02-11 11:15:24 -0800206 // Creates the function call DAG for further analysis, returning false if there is a recursion
207 bool initCallDag(TIntermNode *root);
208 // Return false if "main" doesn't exist
209 bool tagUsedFunctions();
210 void internalTagUsedFunction(size_t index);
211
Olli Etuaho183d7e22015-11-20 15:59:09 +0200212 void initSamplerDefaultPrecision(TBasicType samplerType);
213
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800214 void collectInterfaceBlocks();
215
Corentin Wallez1df16022016-10-27 08:16:56 -0400216 bool variablesCollected;
217
Olli Etuahob12040c2017-06-27 14:20:45 +0300218 bool mGLPositionInitialized;
219
Corentin Walleza094a8a2015-04-07 11:53:06 -0700220 // Removes unused function declarations and prototypes from the AST
221 class UnusedPredicate;
Olli Etuahod10cf692017-11-02 11:06:14 +0200222 void pruneUnusedFunctions(TIntermBlock *root);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700223
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100224 TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
225 size_t numStrings,
226 const ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200227
Olli Etuahod10cf692017-11-02 11:06:14 +0200228 // Fetches and stores shader metadata that is not stored within the AST itself, such as shader
229 // version.
230 void setASTMetadata(const TParseContext &parseContext);
231
232 // Does checks that need to be run after parsing is complete and returns true if they pass.
233 bool checkAndSimplifyAST(TIntermBlock *root,
234 const TParseContext &parseContext,
235 ShCompileOptions compileOptions);
236
Jamie Madill183bde52014-07-02 15:31:19 -0400237 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000238 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400239 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000240
Corentin Wallez71d147f2015-02-11 11:15:24 -0800241 struct FunctionMetadata
242 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500243 FunctionMetadata() : used(false) {}
Corentin Wallez71d147f2015-02-11 11:15:24 -0800244 bool used;
245 };
246
247 CallDAG mCallDag;
248 std::vector<FunctionMetadata> functionMetadata;
249
gman@chromium.org8d804792012-10-17 21:33:48 +0000250 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400251 int maxExpressionComplexity;
252 int maxCallStackDepth;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200253 int maxFunctionParameters;
gman@chromium.org8d804792012-10-17 21:33:48 +0000254
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000255 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400256 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000257
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000258 // Built-in symbol table for the given language, spec, and resources.
259 // It is preserved from compile-to-compile.
260 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000261 // Built-in extensions with default behavior.
262 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000263 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000264
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000265 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000266 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200267 BuiltInFunctionEmulator builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000268
alokp@chromium.org07620a52010-09-23 17:53:56 +0000269 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000270 int shaderVersion;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500271 TInfoSink infoSink; // Output sink.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000272 TDiagnostics mDiagnostics;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500273 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000274
Martin Radev802abe02016-08-04 17:48:32 +0300275 // compute shader local group size
276 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300277 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300278
Olli Etuaho09b04a22016-12-15 13:30:26 +0000279 // GL_OVR_multiview num_views.
280 int mNumViews;
281
Shaob5cc1192017-07-06 10:47:20 +0800282 // geometry shader parameters.
283 int mGeometryShaderMaxVertices;
284 int mGeometryShaderInvocations;
285 TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
286 TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
287
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000288 // name hashing.
289 ShHashFunction64 hashFunction;
290 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700291
292 TPragma mPragma;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293};
294
295//
296// This is the interface between the machine independent code
297// and the machine dependent code.
298//
299// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400300// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301// destroy the machine dependent objects, which contain the
302// above machine independent information.
303//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500304TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
305void DeleteCompiler(TCompiler *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Jamie Madillacb4b812016-11-07 13:50:29 -0500307} // namespace sh
308
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500309#endif // COMPILER_TRANSLATOR_COMPILER_H_