blob: 933869fd594b93ac50cac447a9169286f0a33c78 [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 Etuaho8efc5ad2015-03-03 17:21:10 +020017#include "compiler/translator/BuiltInFunctionEmulator.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080018#include "compiler/translator/CallDAG.h"
Olli Etuaho77ba4082016-12-16 12:01:18 +000019#include "compiler/translator/Diagnostics.h"
Geoff Lang17732822013-08-29 13:46:49 -040020#include "compiler/translator/ExtensionBehavior.h"
21#include "compiler/translator/HashNames.h"
22#include "compiler/translator/InfoSink.h"
Zhenyao Mo94ac7b72014-10-15 18:22:08 -070023#include "compiler/translator/Pragma.h"
Geoff Lang17732822013-08-29 13:46:49 -040024#include "compiler/translator/SymbolTable.h"
25#include "compiler/translator/VariableInfo.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000026#include "third_party/compiler/ArrayBoundsClamper.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027
Jamie Madillacb4b812016-11-07 13:50:29 -050028namespace sh
29{
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031class TCompiler;
Daniel Bratell73941de2015-02-25 14:34:49 +010032#ifdef ANGLE_ENABLE_HLSL
daniel@transgaming.com043da132012-12-20 21:12:22 +000033class TranslatorHLSL;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050034#endif // ANGLE_ENABLE_HLSL
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035
36//
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080037// Helper function to identify specs that are based on the WebGL spec.
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000038//
Jamie Madill5508f392014-02-20 13:31:36 -050039bool IsWebGLBasedSpec(ShShaderSpec spec);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000040
41//
Qingqing Dengad0d0792015-04-08 14:25:06 -070042// Helper function to check if the shader type is GLSL.
43//
44bool IsGLSL130OrNewer(ShShaderOutput output);
Qiankun Miao705a9192016-08-29 10:05:27 +080045bool IsGLSL420OrNewer(ShShaderOutput output);
Zhenyao Mob7bf7422016-11-08 14:44:05 -080046bool IsGLSL410OrOlder(ShShaderOutput output);
Qingqing Dengad0d0792015-04-08 14:25:06 -070047
48//
Qiankun Miao89dd8f32016-11-09 12:59:30 +000049// Helper function to check if the invariant qualifier can be removed.
50//
51bool RemoveInvariant(sh::GLenum shaderType,
52 int shaderVersion,
53 ShShaderOutput outputType,
54 ShCompileOptions compileOptions);
55
56//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057// The base class used to back handles returned to the driver.
58//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050059class TShHandleBase
60{
61 public:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000062 TShHandleBase();
63 virtual ~TShHandleBase();
Jamie Madilld7b1ab52016-12-12 14:42:19 -050064 virtual TCompiler *getAsCompiler() { return 0; }
Daniel Bratell73941de2015-02-25 14:34:49 +010065#ifdef ANGLE_ENABLE_HLSL
Jamie Madilld7b1ab52016-12-12 14:42:19 -050066 virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }
67#endif // ANGLE_ENABLE_HLSL
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000068
Jamie Madilld7b1ab52016-12-12 14:42:19 -050069 protected:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000070 // Memory allocator. Allocates and tracks memory required by the compiler.
71 // Deallocates all memory when compiler is destructed.
72 TPoolAllocator allocator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073};
74
75//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076// The base class for the machine dependent compiler to derive from
77// for managing object code from the compile.
78//
Jamie Madilla718c1e2014-07-02 15:31:22 -040079class TCompiler : public TShHandleBase
80{
81 public:
Jamie Madill183bde52014-07-02 15:31:19 -040082 TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
Corentin Walleze5a1f272015-08-21 02:58:25 +020083 ~TCompiler() override;
84 TCompiler *getAsCompiler() override { return this; }
alokp@chromium.org76b82082010-03-24 17:59:39 +000085
Jamie Madilld7b1ab52016-12-12 14:42:19 -050086 bool Init(const ShBuiltInResources &resources);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020087
88 // compileTreeForTesting should be used only when tests require access to
89 // the AST. Users of this function need to manually manage the global pool
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010090 // allocator. Returns nullptr whenever there are compilation errors.
91 TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
92 size_t numStrings,
93 ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020094
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080095 bool compile(const char *const shaderStrings[],
96 size_t numStrings,
97 ShCompileOptions compileOptions);
alokp@chromium.org07620a52010-09-23 17:53:56 +000098
99 // Get results of the last compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000100 int getShaderVersion() const { return shaderVersion; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500101 TInfoSink &getInfoSink() { return infoSink; }
Jamie Madilled27c722014-07-02 15:31:23 -0400102
Martin Radev802abe02016-08-04 17:48:32 +0300103 bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300104 const sh::WorkGroupSize &getComputeShaderLocalSize() { return mComputeShaderLocalSize; }
Martin Radev802abe02016-08-04 17:48:32 +0300105
Dmitry Skiba2539fff2015-06-16 17:56:09 -0700106 // Clears the results from the previous compilation.
107 void clearResults();
108
Jamie Madilled27c722014-07-02 15:31:23 -0400109 const std::vector<sh::Attribute> &getAttributes() const { return attributes; }
Jamie Madilla0a9e122015-09-02 15:54:30 -0400110 const std::vector<sh::OutputVariable> &getOutputVariables() const { return outputVariables; }
Jamie Madilla718c1e2014-07-02 15:31:22 -0400111 const std::vector<sh::Uniform> &getUniforms() const { return uniforms; }
112 const std::vector<sh::Varying> &getVaryings() const { return varyings; }
Jamie Madilled27c722014-07-02 15:31:23 -0400113 const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return interfaceBlocks; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000114
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000115 ShHashFunction64 getHashFunction() const { return hashFunction; }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500116 NameMap &getNameMap() { return nameMap; }
117 TSymbolTable &getSymbolTable() { return symbolTable; }
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700118 ShShaderSpec getShaderSpec() const { return shaderSpec; }
Jamie Madill68fe74a2014-05-27 12:56:01 -0400119 ShShaderOutput getOutputType() const { return outputType; }
Zhenyao Mo4de44cb2014-10-29 18:03:46 -0700120 const std::string &getBuiltInResourcesString() const { return builtInResourcesString; }
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000121
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800122 bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300123
Jamie Madill54ad4f82014-09-03 09:40:46 -0400124 // Get the resources set by InitBuiltInSymbolTable
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500125 const ShBuiltInResources &getResources() const;
Jamie Madill54ad4f82014-09-03 09:40:46 -0400126
Jamie Madilla718c1e2014-07-02 15:31:22 -0400127 protected:
Jamie Madill183bde52014-07-02 15:31:19 -0400128 sh::GLenum getShaderType() const { return shaderType; }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000129 // Initialize symbol-table with built-in symbols.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500130 bool InitBuiltInSymbolTable(const ShBuiltInResources &resources);
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400131 // Compute the string representation of the built-in resources
132 void setResourceString();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800133 // Return false if the call depth is exceeded.
134 bool checkCallDepth();
Jamie Madill05a80ce2013-06-20 11:55:49 -0400135 // Returns true if a program has no conflicting or missing fragment outputs
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500136 bool validateOutputs(TIntermNode *root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000137 // Returns true if the given shader does not exceed the minimum
138 // functionality mandated in GLSL 1.0 spec Appendix A.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500139 bool validateLimitations(TIntermNode *root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200140 // Add emulated functions to the built-in function emulator.
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800141 virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
142 ShCompileOptions compileOptions){};
alokp@chromium.org07620a52010-09-23 17:53:56 +0000143 // Translate to object code.
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800144 virtual void translate(TIntermNode *root, ShCompileOptions compileOptions) = 0;
gman@chromium.org8d804792012-10-17 21:33:48 +0000145 // Returns true if, after applying the packing rules in the GLSL 1.017 spec
146 // Appendix A, section 7, the shader does not use too many uniforms.
147 bool enforcePackingRestrictions();
Qin Jiajia7835b522016-10-08 11:20:17 +0800148 // Insert statements to reference all members in unused uniform blocks with standard and shared
149 // layout. This is to work around a Mac driver that treats unused standard/shared
150 // uniform blocks as inactive.
151 void useAllMembersInUnusedStandardAndSharedBlocks(TIntermNode *root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700152 // Insert statements to initialize output variables in the beginning of main().
153 // This is to avoid undefined behaviors.
Olli Etuaho27776e32016-07-22 14:00:56 +0300154 void initializeOutputVariables(TIntermNode *root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800155 // Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
156 // It is to work around a Linux driver bug where missing this causes compile failure
157 // while spec says it is allowed.
158 // This function should only be applied to vertex shaders.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500159 void initializeGLPosition(TIntermNode *root);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400160 // Return true if the maximum expression complexity is below the limit.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500161 bool limitExpressionComplexity(TIntermNode *root);
zmo@google.com5601ea02011-06-10 18:23:25 +0000162 // Get built-in extensions with default behavior.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500163 const TExtensionBehavior &getExtensionBehavior() const;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200164 const char *getSourcePath() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500165 const TPragma &getPragma() const { return mPragma; }
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800166 void writePragma(ShCompileOptions compileOptions);
Corentin Wallezd4b50542015-09-28 12:19:26 -0700167 unsigned int *getTemporaryIndex() { return &mTemporaryIndex; }
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700168 // Relies on collectVariables having been called.
169 bool isVaryingDefined(const char *varyingName);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000170
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500171 const ArrayBoundsClamper &getArrayBoundsClamper() const;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000172 ShArrayIndexClampingStrategy getArrayIndexClampingStrategy() const;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500173 const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
zmo@google.com32e97312011-08-24 01:03:11 +0000174
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700175 virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
Corentin Wallez1df16022016-10-27 08:16:56 -0400176 virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700177
Corentin Wallez1df16022016-10-27 08:16:56 -0400178 bool wereVariablesCollected() const;
Jamie Madilled27c722014-07-02 15:31:23 -0400179 std::vector<sh::Attribute> attributes;
Jamie Madilla0a9e122015-09-02 15:54:30 -0400180 std::vector<sh::OutputVariable> outputVariables;
Jamie Madilled27c722014-07-02 15:31:23 -0400181 std::vector<sh::Uniform> uniforms;
Jamie Madill42bcf322014-08-25 16:20:46 -0400182 std::vector<sh::ShaderVariable> expandedUniforms;
Jamie Madilled27c722014-07-02 15:31:23 -0400183 std::vector<sh::Varying> varyings;
184 std::vector<sh::InterfaceBlock> interfaceBlocks;
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300185
Jamie Madilla718c1e2014-07-02 15:31:22 -0400186 private:
Corentin Wallez71d147f2015-02-11 11:15:24 -0800187 // Creates the function call DAG for further analysis, returning false if there is a recursion
188 bool initCallDag(TIntermNode *root);
189 // Return false if "main" doesn't exist
190 bool tagUsedFunctions();
191 void internalTagUsedFunction(size_t index);
192
Olli Etuaho183d7e22015-11-20 15:59:09 +0200193 void initSamplerDefaultPrecision(TBasicType samplerType);
194
Corentin Wallez1df16022016-10-27 08:16:56 -0400195 // Collect info for all attribs, uniforms, varyings.
196 void collectVariables(TIntermNode *root);
197
198 bool variablesCollected;
199
Corentin Walleza094a8a2015-04-07 11:53:06 -0700200 // Removes unused function declarations and prototypes from the AST
201 class UnusedPredicate;
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100202 bool pruneUnusedFunctions(TIntermBlock *root);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700203
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100204 TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
205 size_t numStrings,
206 const ShCompileOptions compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200207
Jamie Madill183bde52014-07-02 15:31:19 -0400208 sh::GLenum shaderType;
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000209 ShShaderSpec shaderSpec;
Jamie Madill68fe74a2014-05-27 12:56:01 -0400210 ShShaderOutput outputType;
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000211
Corentin Wallez71d147f2015-02-11 11:15:24 -0800212 struct FunctionMetadata
213 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500214 FunctionMetadata() : used(false) {}
Corentin Wallez71d147f2015-02-11 11:15:24 -0800215 bool used;
216 };
217
218 CallDAG mCallDag;
219 std::vector<FunctionMetadata> functionMetadata;
220
gman@chromium.org8d804792012-10-17 21:33:48 +0000221 int maxUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400222 int maxExpressionComplexity;
223 int maxCallStackDepth;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200224 int maxFunctionParameters;
gman@chromium.org8d804792012-10-17 21:33:48 +0000225
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000226 ShBuiltInResources compileResources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400227 std::string builtInResourcesString;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000228
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000229 // Built-in symbol table for the given language, spec, and resources.
230 // It is preserved from compile-to-compile.
231 TSymbolTable symbolTable;
alokp@chromium.orgad771eb2010-09-07 17:36:23 +0000232 // Built-in extensions with default behavior.
233 TExtensionBehavior extensionBehavior;
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000234 bool fragmentPrecisionHigh;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000235
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000236 ArrayBoundsClamper arrayBoundsClamper;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000237 ShArrayIndexClampingStrategy clampingStrategy;
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200238 BuiltInFunctionEmulator builtInFunctionEmulator;
zmo@google.com32e97312011-08-24 01:03:11 +0000239
alokp@chromium.org07620a52010-09-23 17:53:56 +0000240 // Results of compilation.
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000241 int shaderVersion;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500242 TInfoSink infoSink; // Output sink.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000243 TDiagnostics mDiagnostics;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500244 const char *mSourcePath; // Path of source file or NULL
zmo@google.com24c08c42011-05-27 17:40:48 +0000245
Martin Radev802abe02016-08-04 17:48:32 +0300246 // compute shader local group size
247 bool mComputeShaderLocalSizeDeclared;
Martin Radev4c4c8e72016-08-04 12:25:34 +0300248 sh::WorkGroupSize mComputeShaderLocalSize;
Martin Radev802abe02016-08-04 17:48:32 +0300249
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000250 // name hashing.
251 ShHashFunction64 hashFunction;
252 NameMap nameMap;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700253
254 TPragma mPragma;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700255
256 unsigned int mTemporaryIndex;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257};
258
259//
260// This is the interface between the machine independent code
261// and the machine dependent code.
262//
263// The machine dependent code should derive from the classes
Jamie Madilld4a3a312014-06-25 16:04:56 -0400264// above. Then Construct*() and Delete*() will create and
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265// destroy the machine dependent objects, which contain the
266// above machine independent information.
267//
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500268TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
269void DeleteCompiler(TCompiler *);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270
Jamie Madillacb4b812016-11-07 13:50:29 -0500271} // namespace sh
272
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500273#endif // COMPILER_TRANSLATOR_COMPILER_H_