Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/src/compiler/translator/Compiler.h
1693 views
1
//
2
// Copyright 2002 The ANGLE Project Authors. All rights reserved.
3
// Use of this source code is governed by a BSD-style license that can be
4
// found in the LICENSE file.
5
//
6
7
#ifndef COMPILER_TRANSLATOR_COMPILER_H_
8
#define COMPILER_TRANSLATOR_COMPILER_H_
9
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
17
#include <GLSLANG/ShaderVars.h>
18
19
#include "common/PackedEnums.h"
20
#include "compiler/translator/BuiltInFunctionEmulator.h"
21
#include "compiler/translator/CallDAG.h"
22
#include "compiler/translator/Diagnostics.h"
23
#include "compiler/translator/ExtensionBehavior.h"
24
#include "compiler/translator/HashNames.h"
25
#include "compiler/translator/InfoSink.h"
26
#include "compiler/translator/Pragma.h"
27
#include "compiler/translator/SymbolTable.h"
28
#include "compiler/translator/ValidateAST.h"
29
30
namespace sh
31
{
32
33
class TCompiler;
34
class TParseContext;
35
#ifdef ANGLE_ENABLE_HLSL
36
class TranslatorHLSL;
37
#endif // ANGLE_ENABLE_HLSL
38
#ifdef ANGLE_ENABLE_METAL
39
class TranslatorMetalDirect;
40
#endif // ANGLE_ENABLE_METAL
41
42
using SpecConstUsageBits = angle::PackedEnumBitSet<vk::SpecConstUsage, uint32_t>;
43
44
//
45
// Helper function to check if the shader type is GLSL.
46
//
47
bool IsGLSL130OrNewer(ShShaderOutput output);
48
bool IsGLSL420OrNewer(ShShaderOutput output);
49
bool IsGLSL410OrOlder(ShShaderOutput output);
50
51
//
52
// Helper function to check if the invariant qualifier can be removed.
53
//
54
bool RemoveInvariant(sh::GLenum shaderType,
55
int shaderVersion,
56
ShShaderOutput outputType,
57
ShCompileOptions compileOptions);
58
59
//
60
// The base class used to back handles returned to the driver.
61
//
62
class TShHandleBase
63
{
64
public:
65
TShHandleBase();
66
virtual ~TShHandleBase();
67
virtual TCompiler *getAsCompiler() { return 0; }
68
#ifdef ANGLE_ENABLE_HLSL
69
virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }
70
#endif // ANGLE_ENABLE_HLSL
71
#ifdef ANGLE_ENABLE_METAL
72
virtual TranslatorMetalDirect *getAsTranslatorMetalDirect() { return nullptr; }
73
#endif // ANGLE_ENABLE_METAL
74
75
protected:
76
// Memory allocator. Allocates and tracks memory required by the compiler.
77
// Deallocates all memory when compiler is destructed.
78
angle::PoolAllocator allocator;
79
};
80
81
//
82
// The base class for the machine dependent compiler to derive from
83
// for managing object code from the compile.
84
//
85
class TCompiler : public TShHandleBase
86
{
87
public:
88
TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
89
~TCompiler() override;
90
TCompiler *getAsCompiler() override { return this; }
91
92
bool Init(const ShBuiltInResources &resources);
93
94
// compileTreeForTesting should be used only when tests require access to
95
// the AST. Users of this function need to manually manage the global pool
96
// allocator. Returns nullptr whenever there are compilation errors.
97
TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],
98
size_t numStrings,
99
ShCompileOptions compileOptions);
100
101
bool compile(const char *const shaderStrings[],
102
size_t numStrings,
103
ShCompileOptions compileOptions);
104
105
// Get results of the last compilation.
106
int getShaderVersion() const { return mShaderVersion; }
107
TInfoSink &getInfoSink() { return mInfoSink; }
108
109
bool isEarlyFragmentTestsSpecified() const { return mEarlyFragmentTestsSpecified; }
110
bool isEarlyFragmentTestsOptimized() const { return mEarlyFragmentTestsOptimized; }
111
SpecConstUsageBits getSpecConstUsageBits() const { return mSpecConstUsageBits; }
112
113
bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }
114
const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; }
115
int getNumViews() const { return mNumViews; }
116
117
// Clears the results from the previous compilation.
118
void clearResults();
119
120
const std::vector<sh::ShaderVariable> &getAttributes() const { return mAttributes; }
121
const std::vector<sh::ShaderVariable> &getOutputVariables() const { return mOutputVariables; }
122
const std::vector<sh::ShaderVariable> &getUniforms() const { return mUniforms; }
123
const std::vector<sh::ShaderVariable> &getInputVaryings() const { return mInputVaryings; }
124
const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; }
125
const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; }
126
const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; }
127
const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const
128
{
129
return mShaderStorageBlocks;
130
}
131
132
ShHashFunction64 getHashFunction() const { return mResources.HashFunction; }
133
NameMap &getNameMap() { return mNameMap; }
134
TSymbolTable &getSymbolTable() { return mSymbolTable; }
135
ShShaderSpec getShaderSpec() const { return mShaderSpec; }
136
ShShaderOutput getOutputType() const { return mOutputType; }
137
const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; }
138
139
bool isHighPrecisionSupported() const;
140
141
bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;
142
bool shouldLimitTypeSizes() const;
143
144
// Get the resources set by InitBuiltInSymbolTable
145
const ShBuiltInResources &getResources() const;
146
147
const TPragma &getPragma() const { return mPragma; }
148
149
int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }
150
int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; }
151
TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const
152
{
153
return mGeometryShaderInputPrimitiveType;
154
}
155
TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const
156
{
157
return mGeometryShaderOutputPrimitiveType;
158
}
159
160
unsigned int getStructSize(const ShaderVariable &var) const;
161
162
int getTessControlShaderOutputVertices() const { return mTessControlShaderOutputVertices; }
163
TLayoutTessEvaluationType getTessEvaluationShaderInputPrimitiveType() const
164
{
165
return mTessEvaluationShaderInputPrimitiveType;
166
}
167
TLayoutTessEvaluationType getTessEvaluationShaderInputVertexSpacingType() const
168
{
169
return mTessEvaluationShaderInputVertexSpacingType;
170
}
171
TLayoutTessEvaluationType getTessEvaluationShaderInputOrderingType() const
172
{
173
return mTessEvaluationShaderInputOrderingType;
174
}
175
TLayoutTessEvaluationType getTessEvaluationShaderInputPointType() const
176
{
177
return mTessEvaluationShaderInputPointType;
178
}
179
180
bool hasAnyPreciseType() const { return mHasAnyPreciseType; }
181
182
unsigned int getSharedMemorySize() const;
183
184
sh::GLenum getShaderType() const { return mShaderType; }
185
186
// Validate the AST and produce errors if it is inconsistent.
187
bool validateAST(TIntermNode *root);
188
// Some transformations may need to temporarily disable validation until they are complete. A
189
// set of disable/enable helpers are used for this purpose.
190
bool disableValidateFunctionCall();
191
void restoreValidateFunctionCall(bool enable);
192
bool disableValidateVariableReferences();
193
void restoreValidateVariableReferences(bool enable);
194
// When the AST is post-processed (such as to determine precise-ness of intermediate nodes),
195
// it's expected to no longer transform.
196
void enableValidateNoMoreTransformations();
197
198
protected:
199
// Add emulated functions to the built-in function emulator.
200
virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,
201
ShCompileOptions compileOptions)
202
{}
203
// Translate to object code. May generate performance warnings through the diagnostics.
204
ANGLE_NO_DISCARD virtual bool translate(TIntermBlock *root,
205
ShCompileOptions compileOptions,
206
PerformanceDiagnostics *perfDiagnostics) = 0;
207
// Get built-in extensions with default behavior.
208
const TExtensionBehavior &getExtensionBehavior() const;
209
const char *getSourcePath() const;
210
// Relies on collectVariables having been called.
211
bool isVaryingDefined(const char *varyingName);
212
213
const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;
214
215
virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;
216
virtual bool shouldCollectVariables(ShCompileOptions compileOptions);
217
218
bool wereVariablesCollected() const;
219
std::vector<sh::ShaderVariable> mAttributes;
220
std::vector<sh::ShaderVariable> mOutputVariables;
221
std::vector<sh::ShaderVariable> mUniforms;
222
std::vector<sh::ShaderVariable> mInputVaryings;
223
std::vector<sh::ShaderVariable> mOutputVaryings;
224
std::vector<sh::ShaderVariable> mSharedVariables;
225
std::vector<sh::InterfaceBlock> mInterfaceBlocks;
226
std::vector<sh::InterfaceBlock> mUniformBlocks;
227
std::vector<sh::InterfaceBlock> mShaderStorageBlocks;
228
229
// Track what should be validated given passes currently applied.
230
ValidateASTOptions mValidateASTOptions;
231
232
// Specialization constant usage bits
233
SpecConstUsageBits mSpecConstUsageBits;
234
235
private:
236
// Initialize symbol-table with built-in symbols.
237
bool initBuiltInSymbolTable(const ShBuiltInResources &resources);
238
// Compute the string representation of the built-in resources
239
void setResourceString();
240
// Return false if the call depth is exceeded.
241
bool checkCallDepth();
242
// Insert statements to reference all members in unused uniform blocks with standard and shared
243
// layout. This is to work around a Mac driver that treats unused standard/shared
244
// uniform blocks as inactive.
245
ANGLE_NO_DISCARD bool useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);
246
// Insert statements to initialize output variables in the beginning of main().
247
// This is to avoid undefined behaviors.
248
ANGLE_NO_DISCARD bool initializeOutputVariables(TIntermBlock *root);
249
// Insert gl_Position = vec4(0,0,0,0) to the beginning of main().
250
// It is to work around a Linux driver bug where missing this causes compile failure
251
// while spec says it is allowed.
252
// This function should only be applied to vertex shaders.
253
ANGLE_NO_DISCARD bool initializeGLPosition(TIntermBlock *root);
254
// Return true if the maximum expression complexity is below the limit.
255
bool limitExpressionComplexity(TIntermBlock *root);
256
// Creates the function call DAG for further analysis, returning false if there is a recursion
257
bool initCallDag(TIntermNode *root);
258
// Return false if "main" doesn't exist
259
bool tagUsedFunctions();
260
void internalTagUsedFunction(size_t index);
261
262
void collectInterfaceBlocks();
263
264
bool mVariablesCollected;
265
266
bool mGLPositionInitialized;
267
268
// Removes unused function declarations and prototypes from the AST
269
class UnusedPredicate;
270
void pruneUnusedFunctions(TIntermBlock *root);
271
272
TIntermBlock *compileTreeImpl(const char *const shaderStrings[],
273
size_t numStrings,
274
const ShCompileOptions compileOptions);
275
276
// Fetches and stores shader metadata that is not stored within the AST itself, such as shader
277
// version.
278
void setASTMetadata(const TParseContext &parseContext);
279
280
// Check if shader version meets the requirement.
281
bool checkShaderVersion(TParseContext *parseContext);
282
283
// Does checks that need to be run after parsing is complete and returns true if they pass.
284
bool checkAndSimplifyAST(TIntermBlock *root,
285
const TParseContext &parseContext,
286
ShCompileOptions compileOptions);
287
288
sh::GLenum mShaderType;
289
ShShaderSpec mShaderSpec;
290
ShShaderOutput mOutputType;
291
292
struct FunctionMetadata
293
{
294
FunctionMetadata() : used(false) {}
295
bool used;
296
};
297
298
CallDAG mCallDag;
299
std::vector<FunctionMetadata> mFunctionMetadata;
300
301
ShBuiltInResources mResources;
302
std::string mBuiltInResourcesString;
303
304
// Built-in symbol table for the given language, spec, and resources.
305
// It is preserved from compile-to-compile.
306
TSymbolTable mSymbolTable;
307
// Built-in extensions with default behavior.
308
TExtensionBehavior mExtensionBehavior;
309
310
BuiltInFunctionEmulator mBuiltInFunctionEmulator;
311
312
// Results of compilation.
313
int mShaderVersion;
314
TInfoSink mInfoSink; // Output sink.
315
TDiagnostics mDiagnostics;
316
const char *mSourcePath; // Path of source file or NULL
317
318
// fragment shader early fragment tests
319
bool mEarlyFragmentTestsSpecified;
320
bool mEarlyFragmentTestsOptimized;
321
322
// compute shader local group size
323
bool mComputeShaderLocalSizeDeclared;
324
sh::WorkGroupSize mComputeShaderLocalSize;
325
326
// GL_OVR_multiview num_views.
327
int mNumViews;
328
329
// geometry shader parameters.
330
int mGeometryShaderMaxVertices;
331
int mGeometryShaderInvocations;
332
TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;
333
TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;
334
335
// tesssellation shader parameters
336
int mTessControlShaderOutputVertices;
337
TLayoutTessEvaluationType mTessEvaluationShaderInputPrimitiveType;
338
TLayoutTessEvaluationType mTessEvaluationShaderInputVertexSpacingType;
339
TLayoutTessEvaluationType mTessEvaluationShaderInputOrderingType;
340
TLayoutTessEvaluationType mTessEvaluationShaderInputPointType;
341
342
bool mHasAnyPreciseType;
343
344
// name hashing.
345
NameMap mNameMap;
346
347
TPragma mPragma;
348
349
ShCompileOptions mCompileOptions;
350
};
351
352
//
353
// This is the interface between the machine independent code
354
// and the machine dependent code.
355
//
356
// The machine dependent code should derive from the classes
357
// above. Then Construct*() and Delete*() will create and
358
// destroy the machine dependent objects, which contain the
359
// above machine independent information.
360
//
361
TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);
362
void DeleteCompiler(TCompiler *);
363
364
} // namespace sh
365
366
#endif // COMPILER_TRANSLATOR_COMPILER_H_
367
368