Path: blob/main_old/src/compiler/translator/Compiler.h
1693 views
//1// Copyright 2002 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56#ifndef COMPILER_TRANSLATOR_COMPILER_H_7#define COMPILER_TRANSLATOR_COMPILER_H_89//10// Machine independent part of the compiler private objects11// sent as ShHandle to the driver.12//13// This should not be included by driver code.14//1516#include <GLSLANG/ShaderVars.h>1718#include "common/PackedEnums.h"19#include "compiler/translator/BuiltInFunctionEmulator.h"20#include "compiler/translator/CallDAG.h"21#include "compiler/translator/Diagnostics.h"22#include "compiler/translator/ExtensionBehavior.h"23#include "compiler/translator/HashNames.h"24#include "compiler/translator/InfoSink.h"25#include "compiler/translator/Pragma.h"26#include "compiler/translator/SymbolTable.h"27#include "compiler/translator/ValidateAST.h"2829namespace sh30{3132class TCompiler;33class TParseContext;34#ifdef ANGLE_ENABLE_HLSL35class TranslatorHLSL;36#endif // ANGLE_ENABLE_HLSL37#ifdef ANGLE_ENABLE_METAL38class TranslatorMetalDirect;39#endif // ANGLE_ENABLE_METAL4041using SpecConstUsageBits = angle::PackedEnumBitSet<vk::SpecConstUsage, uint32_t>;4243//44// Helper function to check if the shader type is GLSL.45//46bool IsGLSL130OrNewer(ShShaderOutput output);47bool IsGLSL420OrNewer(ShShaderOutput output);48bool IsGLSL410OrOlder(ShShaderOutput output);4950//51// Helper function to check if the invariant qualifier can be removed.52//53bool RemoveInvariant(sh::GLenum shaderType,54int shaderVersion,55ShShaderOutput outputType,56ShCompileOptions compileOptions);5758//59// The base class used to back handles returned to the driver.60//61class TShHandleBase62{63public:64TShHandleBase();65virtual ~TShHandleBase();66virtual TCompiler *getAsCompiler() { return 0; }67#ifdef ANGLE_ENABLE_HLSL68virtual TranslatorHLSL *getAsTranslatorHLSL() { return 0; }69#endif // ANGLE_ENABLE_HLSL70#ifdef ANGLE_ENABLE_METAL71virtual TranslatorMetalDirect *getAsTranslatorMetalDirect() { return nullptr; }72#endif // ANGLE_ENABLE_METAL7374protected:75// Memory allocator. Allocates and tracks memory required by the compiler.76// Deallocates all memory when compiler is destructed.77angle::PoolAllocator allocator;78};7980//81// The base class for the machine dependent compiler to derive from82// for managing object code from the compile.83//84class TCompiler : public TShHandleBase85{86public:87TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);88~TCompiler() override;89TCompiler *getAsCompiler() override { return this; }9091bool Init(const ShBuiltInResources &resources);9293// compileTreeForTesting should be used only when tests require access to94// the AST. Users of this function need to manually manage the global pool95// allocator. Returns nullptr whenever there are compilation errors.96TIntermBlock *compileTreeForTesting(const char *const shaderStrings[],97size_t numStrings,98ShCompileOptions compileOptions);99100bool compile(const char *const shaderStrings[],101size_t numStrings,102ShCompileOptions compileOptions);103104// Get results of the last compilation.105int getShaderVersion() const { return mShaderVersion; }106TInfoSink &getInfoSink() { return mInfoSink; }107108bool isEarlyFragmentTestsSpecified() const { return mEarlyFragmentTestsSpecified; }109bool isEarlyFragmentTestsOptimized() const { return mEarlyFragmentTestsOptimized; }110SpecConstUsageBits getSpecConstUsageBits() const { return mSpecConstUsageBits; }111112bool isComputeShaderLocalSizeDeclared() const { return mComputeShaderLocalSizeDeclared; }113const sh::WorkGroupSize &getComputeShaderLocalSize() const { return mComputeShaderLocalSize; }114int getNumViews() const { return mNumViews; }115116// Clears the results from the previous compilation.117void clearResults();118119const std::vector<sh::ShaderVariable> &getAttributes() const { return mAttributes; }120const std::vector<sh::ShaderVariable> &getOutputVariables() const { return mOutputVariables; }121const std::vector<sh::ShaderVariable> &getUniforms() const { return mUniforms; }122const std::vector<sh::ShaderVariable> &getInputVaryings() const { return mInputVaryings; }123const std::vector<sh::ShaderVariable> &getOutputVaryings() const { return mOutputVaryings; }124const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const { return mInterfaceBlocks; }125const std::vector<sh::InterfaceBlock> &getUniformBlocks() const { return mUniformBlocks; }126const std::vector<sh::InterfaceBlock> &getShaderStorageBlocks() const127{128return mShaderStorageBlocks;129}130131ShHashFunction64 getHashFunction() const { return mResources.HashFunction; }132NameMap &getNameMap() { return mNameMap; }133TSymbolTable &getSymbolTable() { return mSymbolTable; }134ShShaderSpec getShaderSpec() const { return mShaderSpec; }135ShShaderOutput getOutputType() const { return mOutputType; }136const std::string &getBuiltInResourcesString() const { return mBuiltInResourcesString; }137138bool isHighPrecisionSupported() const;139140bool shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const;141bool shouldLimitTypeSizes() const;142143// Get the resources set by InitBuiltInSymbolTable144const ShBuiltInResources &getResources() const;145146const TPragma &getPragma() const { return mPragma; }147148int getGeometryShaderMaxVertices() const { return mGeometryShaderMaxVertices; }149int getGeometryShaderInvocations() const { return mGeometryShaderInvocations; }150TLayoutPrimitiveType getGeometryShaderInputPrimitiveType() const151{152return mGeometryShaderInputPrimitiveType;153}154TLayoutPrimitiveType getGeometryShaderOutputPrimitiveType() const155{156return mGeometryShaderOutputPrimitiveType;157}158159unsigned int getStructSize(const ShaderVariable &var) const;160161int getTessControlShaderOutputVertices() const { return mTessControlShaderOutputVertices; }162TLayoutTessEvaluationType getTessEvaluationShaderInputPrimitiveType() const163{164return mTessEvaluationShaderInputPrimitiveType;165}166TLayoutTessEvaluationType getTessEvaluationShaderInputVertexSpacingType() const167{168return mTessEvaluationShaderInputVertexSpacingType;169}170TLayoutTessEvaluationType getTessEvaluationShaderInputOrderingType() const171{172return mTessEvaluationShaderInputOrderingType;173}174TLayoutTessEvaluationType getTessEvaluationShaderInputPointType() const175{176return mTessEvaluationShaderInputPointType;177}178179bool hasAnyPreciseType() const { return mHasAnyPreciseType; }180181unsigned int getSharedMemorySize() const;182183sh::GLenum getShaderType() const { return mShaderType; }184185// Validate the AST and produce errors if it is inconsistent.186bool validateAST(TIntermNode *root);187// Some transformations may need to temporarily disable validation until they are complete. A188// set of disable/enable helpers are used for this purpose.189bool disableValidateFunctionCall();190void restoreValidateFunctionCall(bool enable);191bool disableValidateVariableReferences();192void restoreValidateVariableReferences(bool enable);193// When the AST is post-processed (such as to determine precise-ness of intermediate nodes),194// it's expected to no longer transform.195void enableValidateNoMoreTransformations();196197protected:198// Add emulated functions to the built-in function emulator.199virtual void initBuiltInFunctionEmulator(BuiltInFunctionEmulator *emu,200ShCompileOptions compileOptions)201{}202// Translate to object code. May generate performance warnings through the diagnostics.203ANGLE_NO_DISCARD virtual bool translate(TIntermBlock *root,204ShCompileOptions compileOptions,205PerformanceDiagnostics *perfDiagnostics) = 0;206// Get built-in extensions with default behavior.207const TExtensionBehavior &getExtensionBehavior() const;208const char *getSourcePath() const;209// Relies on collectVariables having been called.210bool isVaryingDefined(const char *varyingName);211212const BuiltInFunctionEmulator &getBuiltInFunctionEmulator() const;213214virtual bool shouldFlattenPragmaStdglInvariantAll() = 0;215virtual bool shouldCollectVariables(ShCompileOptions compileOptions);216217bool wereVariablesCollected() const;218std::vector<sh::ShaderVariable> mAttributes;219std::vector<sh::ShaderVariable> mOutputVariables;220std::vector<sh::ShaderVariable> mUniforms;221std::vector<sh::ShaderVariable> mInputVaryings;222std::vector<sh::ShaderVariable> mOutputVaryings;223std::vector<sh::ShaderVariable> mSharedVariables;224std::vector<sh::InterfaceBlock> mInterfaceBlocks;225std::vector<sh::InterfaceBlock> mUniformBlocks;226std::vector<sh::InterfaceBlock> mShaderStorageBlocks;227228// Track what should be validated given passes currently applied.229ValidateASTOptions mValidateASTOptions;230231// Specialization constant usage bits232SpecConstUsageBits mSpecConstUsageBits;233234private:235// Initialize symbol-table with built-in symbols.236bool initBuiltInSymbolTable(const ShBuiltInResources &resources);237// Compute the string representation of the built-in resources238void setResourceString();239// Return false if the call depth is exceeded.240bool checkCallDepth();241// Insert statements to reference all members in unused uniform blocks with standard and shared242// layout. This is to work around a Mac driver that treats unused standard/shared243// uniform blocks as inactive.244ANGLE_NO_DISCARD bool useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root);245// Insert statements to initialize output variables in the beginning of main().246// This is to avoid undefined behaviors.247ANGLE_NO_DISCARD bool initializeOutputVariables(TIntermBlock *root);248// Insert gl_Position = vec4(0,0,0,0) to the beginning of main().249// It is to work around a Linux driver bug where missing this causes compile failure250// while spec says it is allowed.251// This function should only be applied to vertex shaders.252ANGLE_NO_DISCARD bool initializeGLPosition(TIntermBlock *root);253// Return true if the maximum expression complexity is below the limit.254bool limitExpressionComplexity(TIntermBlock *root);255// Creates the function call DAG for further analysis, returning false if there is a recursion256bool initCallDag(TIntermNode *root);257// Return false if "main" doesn't exist258bool tagUsedFunctions();259void internalTagUsedFunction(size_t index);260261void collectInterfaceBlocks();262263bool mVariablesCollected;264265bool mGLPositionInitialized;266267// Removes unused function declarations and prototypes from the AST268class UnusedPredicate;269void pruneUnusedFunctions(TIntermBlock *root);270271TIntermBlock *compileTreeImpl(const char *const shaderStrings[],272size_t numStrings,273const ShCompileOptions compileOptions);274275// Fetches and stores shader metadata that is not stored within the AST itself, such as shader276// version.277void setASTMetadata(const TParseContext &parseContext);278279// Check if shader version meets the requirement.280bool checkShaderVersion(TParseContext *parseContext);281282// Does checks that need to be run after parsing is complete and returns true if they pass.283bool checkAndSimplifyAST(TIntermBlock *root,284const TParseContext &parseContext,285ShCompileOptions compileOptions);286287sh::GLenum mShaderType;288ShShaderSpec mShaderSpec;289ShShaderOutput mOutputType;290291struct FunctionMetadata292{293FunctionMetadata() : used(false) {}294bool used;295};296297CallDAG mCallDag;298std::vector<FunctionMetadata> mFunctionMetadata;299300ShBuiltInResources mResources;301std::string mBuiltInResourcesString;302303// Built-in symbol table for the given language, spec, and resources.304// It is preserved from compile-to-compile.305TSymbolTable mSymbolTable;306// Built-in extensions with default behavior.307TExtensionBehavior mExtensionBehavior;308309BuiltInFunctionEmulator mBuiltInFunctionEmulator;310311// Results of compilation.312int mShaderVersion;313TInfoSink mInfoSink; // Output sink.314TDiagnostics mDiagnostics;315const char *mSourcePath; // Path of source file or NULL316317// fragment shader early fragment tests318bool mEarlyFragmentTestsSpecified;319bool mEarlyFragmentTestsOptimized;320321// compute shader local group size322bool mComputeShaderLocalSizeDeclared;323sh::WorkGroupSize mComputeShaderLocalSize;324325// GL_OVR_multiview num_views.326int mNumViews;327328// geometry shader parameters.329int mGeometryShaderMaxVertices;330int mGeometryShaderInvocations;331TLayoutPrimitiveType mGeometryShaderInputPrimitiveType;332TLayoutPrimitiveType mGeometryShaderOutputPrimitiveType;333334// tesssellation shader parameters335int mTessControlShaderOutputVertices;336TLayoutTessEvaluationType mTessEvaluationShaderInputPrimitiveType;337TLayoutTessEvaluationType mTessEvaluationShaderInputVertexSpacingType;338TLayoutTessEvaluationType mTessEvaluationShaderInputOrderingType;339TLayoutTessEvaluationType mTessEvaluationShaderInputPointType;340341bool mHasAnyPreciseType;342343// name hashing.344NameMap mNameMap;345346TPragma mPragma;347348ShCompileOptions mCompileOptions;349};350351//352// This is the interface between the machine independent code353// and the machine dependent code.354//355// The machine dependent code should derive from the classes356// above. Then Construct*() and Delete*() will create and357// destroy the machine dependent objects, which contain the358// above machine independent information.359//360TCompiler *ConstructCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output);361void DeleteCompiler(TCompiler *);362363} // namespace sh364365#endif // COMPILER_TRANSLATOR_COMPILER_H_366367368