blob: 085b7a93cc9e7a60eb2ebf3745f0b9e82659e4e7 [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;
Daniel Bratell73941de2015-02-25 14:34:49 +010033#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000034class TranslatorHLSL;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050035#endif // ANGLE_ENABLE_HLSL
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036
37//
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080038// Helper function to identify specs that are based on the WebGL spec.
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000039//
Jamie Madill5508f392014-02-20 13:31:36 -050040bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000041
42//
Qingqing Dengad0d0792015-04-08 14:25:06 -070043// Helper function to check if the shader type is GLSL.
44//
45bool IsGLSL130OrNewer(ShShaderOutput output);
Qiankun Miao705a9192016-08-29 10:05:27 +080046bool IsGLSL420OrNewer(ShShaderOutput output);
Zhenyao Mob7bf7422016-11-08 14:44:05 -080047bool IsGLSL410OrOlder(ShShaderOutput output);
Qingqing Dengad0d0792015-04-08 14:25:06 -070048
49//
Qiankun Miao89dd8f32016-11-09 12:59:30 +000050// Helper function to check if the invariant qualifier can be removed.
51//
52bool RemoveInvariant(sh::GLenum shaderType,
53 int shaderVersion,
54 ShShaderOutput outputType,
55 ShCompileOptions compileOptions);
56
57//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058// The base class used to back handles returned to the driver.
59//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050060class TShHandleBase
61{
62 public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000063 TShHandleBase();
64 virtual ~TShHandleBase();
Jamie Madilld7b1ab52016-12-12 14:42:19 -050065 virtual TCompiler *getAsCompiler() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010066#ifdef ANGLE_ENABLE_HLSL
Jamie Madilld7b1ab52016-12-12 14:42:19 -050067 virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }
68#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000069
Jamie Madilld7b1ab52016-12-12 14:42:19 -050070 protected:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000071 // Memory allocator. Allocates and tracks memory required by the compiler.
72 // Deallocates all memory when compiler is destructed.
73 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074};
75
76//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077// The base class for the machine dependent compiler to derive from
78// for managing object code from the compile.
79//
Jamie Madilla718c1e2014-07-02 15:31:22 -040080class TCompiler : public TShHandleBase
81{
82 public:
Jamie Madill183bde52014-07-02 15:31:19 -040083 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
Corentin Walleze5a1f272015-08-21 02:58:25 +020084 ~TCompiler() override;
85 TCompiler *getAsCompiler() override { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000086
Jamie Madilld7b1ab52016-12-12 14:42:19 -050087 bool Init(const ShBuiltInResources &resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020088
89 // compileTreeForTesting should be used only when tests require access to
90 // the AST. Users of this function need to manually manage the global pool
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010091 // allocator. Returns nullptr whenever there are compilation errors.
92 TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
93 size_t numStrings,
94 ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020095
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080096 bool compile(const char *const shaderStrings[],
97 size_t numStrings,
98 ShCompileOptions compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000099
100 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000101 int getShaderVersion() const { return shaderVersion; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500102 TInfoSink &getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -0400103
Martin Radev802abe02016-08-04 17:48:32 +0300104 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Olli Etuaho09b04a22016-12-15 13:30:26 +0000105 const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; }
106 int getNumViews() const { return mNumViews; }
Martin Radev802abe02016-08-04 17:48:32 +0300107
Dmitry Skiba2539fff2015-06-16 17:56:09 -0700108 // Clears the results from the previous compilation.
109 void clearResults();
110
Jamie Madilled27c722014-07-02 15:31:23 -0400111 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
Jamie Madilla0a9e122015-09-02 15:54:30 -0400112 const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -0400113 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800114 const std::vector<sh::Varying> &getInputVaryings() const { return inputVaryings; }
115 const std::vector<sh::Varying> &getOutputVaryings() const { return outputVaryings; }
Jamie Madilled27c722014-07-02 15:31:23 -0400116 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800117 const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return uniformBlocks; }
118 const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const
119 {
120 return shaderStorageBlocks;
121 }
Jiawei Shaod8105a02017-08-08 09:54:36 +0800122 const std::vector<sh::InterfaceBlock> &getInBlocks() const { return inBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000123
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000124 ShHashFunction64 getHashFunction() const { return hashFunction; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500125 NameMap &getNameMap() { return nameMap; }
126 TSymbolTable &getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700127 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -0400128 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -0700129 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000130
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800131 bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300132
Jamie Madill54ad4f82014-09-03 09:40:46 -0400133 // Get the resources set by InitBuiltInSymbolTable
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500134 const ShBuiltInResources &getResources() const;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400135
Shaob5cc1192017-07-06 10:47:20 +0800136 int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }
137 int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; }
138 TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const
139 {
140 return mGeometryShaderInputPrimitiveType;
141 }
142 TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const
143 {
144 return mGeometryShaderOutputPrimitiveType;
145 }
146
Jamie Madill183bde52014-07-02 15:31:19 -0400147 sh::GLenum getShaderType() const { return shaderType; }
Shaob5cc1192017-07-06 10:47:20 +0800148
149 protected:
alokp@chromium.org07620a52010-09-23 17:53:56 +0000150 // Initialize symbol-table with built-in symbols.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500151 bool InitBuiltInSymbolTable(const ShBuiltInResources &resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400152 // Compute the string representation of the built-in resources
153 void setResourceString();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800154 // Return false if the call depth is exceeded.
155 bool checkCallDepth();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200156 // Add emulated functions to the built-in function emulator.
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800157 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
158 ShCompileOptions compileOptions){};
alokp@chromium.org07620a52010-09-23 17:53:56 +0000159 // Translate to object code.
Olli Etuaho5df2b9d2017-05-18 12:15:28 +0300160 virtual void translate(TIntermBlock *root, ShCompileOptions compileOptions) = 0;
Qin Jiajia7835b522016-10-08 11:20:17 +0800161 // Insert statements to reference all members in unused uniform blocks with standard and shared
162 // layout. This is to work around a Mac driver that treats unused standard/shared
163 // uniform blocks as inactive.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300164 void useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700165 // Insert statements to initialize output variables in the beginning of main().
166 // This is to avoid undefined behaviors.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300167 void initializeOutputVariables(TIntermBlock *root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800168 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
169 // It is to work around a Linux driver bug where missing this causes compile failure
170 // while spec says it is allowed.
171 // This function should only be applied to vertex shaders.
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300172 void initializeGLPosition(TIntermBlock *root);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400173 // Return true if the maximum expression complexity is below the limit.
Olli Etuahob4279202017-05-18 15:08:24 +0300174 bool limitExpressionComplexity(TIntermBlock *root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000175 // Get built-in extensions with default behavior.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500176 const TExtensionBehavior &getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200177 const char *getSourcePath() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500178 const TPragma &getPragma() const { return mPragma; }
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800179 void writePragma(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700180 // Relies on collectVariables having been called.
181 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000182
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500183 const ArrayBoundsClamper &getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000184 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500185 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000186
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700187 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
Corentin Wallez1df16022016-10-27 08:16:56 -0400188 virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300189 virtual bool needToInitializeGlobalsInAST() const { return IsWebGLBasedSpec(shaderSpec); }
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700190
Corentin Wallez1df16022016-10-27 08:16:56 -0400191 bool wereVariablesCollected() const;
Jamie Madilled27c722014-07-02 15:31:23 -0400192 std::vector<sh::Attribute> attributes;
Jamie Madilla0a9e122015-09-02 15:54:30 -0400193 std::vector<sh::OutputVariable> outputVariables;
Jamie Madilled27c722014-07-02 15:31:23 -0400194 std::vector<sh::Uniform> uniforms;
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800195 std::vector<sh::Varying> inputVaryings;
196 std::vector<sh::Varying> outputVaryings;
Jamie Madilled27c722014-07-02 15:31:23 -0400197 std::vector<sh::InterfaceBlock> interfaceBlocks;
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800198 std::vector<sh::InterfaceBlock> uniformBlocks;
199 std::vector<sh::InterfaceBlock> shaderStorageBlocks;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800200 std::vector<sh::InterfaceBlock> inBlocks;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300201
Jamie Madilla718c1e2014-07-02 15:31:22 -0400202 private:
Corentin Wallez71d147f2015-02-11 11:15:24 -0800203 // Creates the function call DAG for further analysis, returning false if there is a recursion
204 bool initCallDag(TIntermNode *root);
205 // Return false if "main" doesn't exist
206 bool tagUsedFunctions();
207 void internalTagUsedFunction(size_t index);
208
Olli Etuaho183d7e22015-11-20 15:59:09 +0200209 void initSamplerDefaultPrecision(TBasicType samplerType);
210
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800211 void collectInterfaceBlocks();
212
Corentin Wallez1df16022016-10-27 08:16:56 -0400213 bool variablesCollected;
214
Olli Etuahob12040c2017-06-27 14:20:45 +0300215 bool mGLPositionInitialized;
216
Corentin Walleza094a8a2015-04-07 11:53:06 -0700217 // Removes unused function declarations and prototypes from the AST
218 class UnusedPredicate;
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100219 bool pruneUnusedFunctions(TIntermBlock *root);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700220
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100221 TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
222 size_t numStrings,
223 const ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200224
Jamie Madill183bde52014-07-02 15:31:19 -0400225 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000226 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400227 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000228
Corentin Wallez71d147f2015-02-11 11:15:24 -0800229 struct FunctionMetadata
230 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500231 FunctionMetadata() : used(false) {}
Corentin Wallez71d147f2015-02-11 11:15:24 -0800232 bool used;
233 };
234
235 CallDAG mCallDag;
236 std::vector<FunctionMetadata> functionMetadata;
237
gman@chromium.org8d804792012-10-17 21:33:48 +0000238 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400239 int maxExpressionComplexity;
240 int maxCallStackDepth;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200241 int maxFunctionParameters;
gman@chromium.org8d804792012-10-17 21:33:48 +0000242
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000243 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400244 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000245
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000246 // Built-in symbol table for the given language, spec, and resources.
247 // It is preserved from compile-to-compile.
248 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000249 // Built-in extensions with default behavior.
250 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000251 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000252
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000253 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000254 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200255 BuiltInFunctionEmulator builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000256
alokp@chromium.org07620a52010-09-23 17:53:56 +0000257 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000258 int shaderVersion;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500259 TInfoSink infoSink; // 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.
277 ShHashFunction64 hashFunction;
278 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700279
280 TPragma mPragma;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281};
282
283//
284// This is the interface between the machine independent code
285// and the machine dependent code.
286//
287// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400288// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289// destroy the machine dependent objects, which contain the
290// above machine independent information.
291//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500292TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
293void DeleteCompiler(TCompiler *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
Jamie Madillacb4b812016-11-07 13:50:29 -0500295} // namespace sh
296
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500297#endif // COMPILER_TRANSLATOR_COMPILER_H_