#include "spirv.hpp"
#include "GlslangToSpv.h"
#include "SpvBuilder.h"
#include "SpvTools.h"
namespace spv {
#include "GLSL.std.450.h"
#include "GLSL.ext.KHR.h"
#include "GLSL.ext.EXT.h"
#include "GLSL.ext.AMD.h"
#include "GLSL.ext.NV.h"
#include "GLSL.ext.ARM.h"
#include "GLSL.ext.QCOM.h"
#include "NonSemanticDebugPrintf.h"
}
#include "../glslang/MachineIndependent/localintermediate.h"
#include "../glslang/MachineIndependent/SymbolTable.h"
#include "../glslang/Include/Common.h"
#include "glslang/build_info.h"
#include <fstream>
#include <iomanip>
#include <list>
#include <map>
#include <optional>
#include <stack>
#include <string>
#include <vector>
namespace {
namespace {
class SpecConstantOpModeGuard { … };
struct OpDecorations { … };
}
class TGlslangToSpvTraverser : public glslang::TIntermTraverser { … };
spv::SourceLanguage TranslateSourceLanguage(glslang::EShSource source, EProfile profile)
{ … }
spv::ExecutionModel TranslateExecutionModel(EShLanguage stage, bool isMeshShaderEXT = false)
{ … }
spv::Dim TranslateDimensionality(const glslang::TSampler& sampler)
{ … }
spv::Decoration TranslatePrecisionDecoration(glslang::TPrecisionQualifier glslangPrecision)
{ … }
spv::Decoration TranslatePrecisionDecoration(const glslang::TType& type)
{ … }
spv::Decoration TranslateBlockDecoration(const glslang::TStorageQualifier storage, bool useStorageBuffer)
{ … }
void TranslateMemoryDecoration(const glslang::TQualifier& qualifier, std::vector<spv::Decoration>& memory,
bool useVulkanMemoryModel)
{ … }
spv::Decoration TranslateLayoutDecoration(const glslang::TType& type, glslang::TLayoutMatrix matrixLayout)
{ … }
spv::Decoration TGlslangToSpvTraverser::TranslateInterpolationDecoration(const glslang::TQualifier& qualifier)
{ … }
spv::Decoration TGlslangToSpvTraverser::TranslateAuxiliaryStorageDecoration(const glslang::TQualifier& qualifier)
{ … }
spv::Decoration TranslateInvariantDecoration(const glslang::TQualifier& qualifier)
{ … }
spv::Decoration TranslateNoContractionDecoration(const glslang::TQualifier& qualifier)
{ … }
spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(const glslang::TQualifier& qualifier)
{ … }
spv::Decoration TGlslangToSpvTraverser::TranslateNonUniformDecoration(
const spv::Builder::AccessChain::CoherentFlags& coherentFlags)
{ … }
spv::MemoryAccessMask TGlslangToSpvTraverser::TranslateMemoryAccess(
const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
{ … }
spv::ImageOperandsMask TGlslangToSpvTraverser::TranslateImageOperands(
const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
{ … }
spv::Builder::AccessChain::CoherentFlags TGlslangToSpvTraverser::TranslateCoherent(const glslang::TType& type)
{ … }
spv::Scope TGlslangToSpvTraverser::TranslateMemoryScope(
const spv::Builder::AccessChain::CoherentFlags &coherentFlags)
{ … }
spv::BuiltIn TGlslangToSpvTraverser::TranslateBuiltInDecoration(glslang::TBuiltInVariable builtIn,
bool memberDeclaration)
{ … }
spv::ImageFormat TGlslangToSpvTraverser::TranslateImageFormat(const glslang::TType& type)
{ … }
spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSelectionControl(
const glslang::TIntermSelection& selectionNode) const
{ … }
spv::SelectionControlMask TGlslangToSpvTraverser::TranslateSwitchControl(const glslang::TIntermSwitch& switchNode)
const
{ … }
spv::LoopControlMask TGlslangToSpvTraverser::TranslateLoopControl(const glslang::TIntermLoop& loopNode,
std::vector<unsigned int>& operands) const
{ … }
spv::StorageClass TGlslangToSpvTraverser::TranslateStorageClass(const glslang::TType& type)
{ … }
void TGlslangToSpvTraverser::TranslateLiterals(const glslang::TVector<const glslang::TIntermConstantUnion*>& constants,
std::vector<unsigned>& literals) const
{ … }
void TGlslangToSpvTraverser::addIndirectionIndexCapabilities(const glslang::TType& baseType,
const glslang::TType& indexType)
{ … }
bool IsDescriptorResource(const glslang::TType& type)
{ … }
void InheritQualifiers(glslang::TQualifier& child, const glslang::TQualifier& parent)
{ … }
bool HasNonLayoutQualifiers(const glslang::TType& type, const glslang::TQualifier& qualifier)
{ … }
TGlslangToSpvTraverser::TGlslangToSpvTraverser(unsigned int spvVersion,
const glslang::TIntermediate* glslangIntermediate,
spv::SpvBuildLogger* buildLogger, glslang::SpvOptions& options) : … { … }
void TGlslangToSpvTraverser::finishSpv(bool compileOnly)
{ … }
void TGlslangToSpvTraverser::dumpSpv(std::vector<unsigned int>& out)
{ … }
void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
{ … }
bool TGlslangToSpvTraverser::visitBinary(glslang::TVisit , glslang::TIntermBinary* node)
{ … }
spv::Id TGlslangToSpvTraverser::convertLoadedBoolInUniformToUint(const glslang::TType& type,
spv::Id nominalTypeId,
spv::Id loadedId)
{ … }
std::pair<spv::Id, spv::Id> TGlslangToSpvTraverser::getForcedType(glslang::TBuiltInVariable glslangBuiltIn,
const glslang::TType& glslangType)
{ … }
spv::Id TGlslangToSpvTraverser::translateForcedType(spv::Id object)
{ … }
bool TGlslangToSpvTraverser::visitUnary(glslang::TVisit , glslang::TIntermUnary* node)
{ … }
spv::Id TGlslangToSpvTraverser::createCompositeConstruct(spv::Id resultTypeId, std::vector<spv::Id> constituents)
{ … }
bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TIntermAggregate* node)
{ … }
bool TGlslangToSpvTraverser::visitSelection(glslang::TVisit , glslang::TIntermSelection* node)
{ … }
bool TGlslangToSpvTraverser::visitSwitch(glslang::TVisit , glslang::TIntermSwitch* node)
{ … }
void TGlslangToSpvTraverser::visitConstantUnion(glslang::TIntermConstantUnion* node)
{ … }
bool TGlslangToSpvTraverser::visitLoop(glslang::TVisit , glslang::TIntermLoop* node)
{ … }
bool TGlslangToSpvTraverser::visitBranch(glslang::TVisit , glslang::TIntermBranch* node)
{ … }
spv::Id TGlslangToSpvTraverser::createSpvVariable(const glslang::TIntermSymbol* node, spv::Id forcedType)
{ … }
spv::Id TGlslangToSpvTraverser::getSampledType(const glslang::TSampler& sampler)
{ … }
spv::Id TGlslangToSpvTraverser::getInvertedSwizzleType(const glslang::TIntermTyped& node)
{ … }
spv::Id TGlslangToSpvTraverser::createInvertedSwizzle(spv::Decoration precision, const glslang::TIntermTyped& node,
spv::Id parentResult)
{ … }
void TGlslangToSpvTraverser::convertSwizzle(const glslang::TIntermAggregate& node, std::vector<unsigned>& swizzle)
{ … }
spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type, bool forwardReferenceOnly)
{ … }
spv::LinkageType TGlslangToSpvTraverser::convertGlslangLinkageToSpv(glslang::TLinkType linkType)
{ … }
spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& type,
glslang::TLayoutPacking explicitLayout, const glslang::TQualifier& qualifier,
bool lastBufferBlockMember, bool forwardReferenceOnly)
{ … }
void TGlslangToSpvTraverser::applySpirvDecorate(const glslang::TType& type, spv::Id id, std::optional<int> member)
{ … }
bool TGlslangToSpvTraverser::filterMember(const glslang::TType& member)
{
auto& extensions = glslangIntermediate->getRequestedExtensions();
if (member.getFieldName() == "gl_SecondaryViewportMaskNV" &&
extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
return true;
if (member.getFieldName() == "gl_SecondaryPositionNV" &&
extensions.find("GL_NV_stereo_view_rendering") == extensions.end())
return true;
if (glslangIntermediate->getStage() == EShLangMesh) {
if (member.getFieldName() == "gl_PrimitiveShadingRateEXT" &&
extensions.find("GL_EXT_fragment_shading_rate") == extensions.end())
return true;
}
if (glslangIntermediate->getStage() != EShLangMesh) {
if (member.getFieldName() == "gl_ViewportMask" &&
extensions.find("GL_NV_viewport_array2") == extensions.end())
return true;
if (member.getFieldName() == "gl_PositionPerViewNV" &&
extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
return true;
if (member.getFieldName() == "gl_ViewportMaskPerViewNV" &&
extensions.find("GL_NVX_multiview_per_view_attributes") == extensions.end())
return true;
}
return false;
};
spv::Id TGlslangToSpvTraverser::convertGlslangStructToSpvType(const glslang::TType& type,
const glslang::TTypeList* glslangMembers,
glslang::TLayoutPacking explicitLayout,
const glslang::TQualifier& qualifier)
{ … }
void TGlslangToSpvTraverser::decorateStructType(const glslang::TType& type,
const glslang::TTypeList* glslangMembers,
glslang::TLayoutPacking explicitLayout,
const glslang::TQualifier& qualifier,
spv::Id spvType,
const std::vector<spv::Id>& spvMembers)
{ … }
spv::Id TGlslangToSpvTraverser::makeArraySizeId(const glslang::TArraySizes& arraySizes, int dim, bool allowZero)
{ … }
spv::Id TGlslangToSpvTraverser::accessChainLoad(const glslang::TType& type)
{ … }
void TGlslangToSpvTraverser::accessChainStore(const glslang::TType& type, spv::Id rvalue)
{ … }
void TGlslangToSpvTraverser::multiTypeStore(const glslang::TType& type, spv::Id rValue)
{ … }
glslang::TLayoutPacking TGlslangToSpvTraverser::getExplicitLayout(const glslang::TType& type) const
{ … }
int TGlslangToSpvTraverser::getArrayStride(const glslang::TType& arrayType, glslang::TLayoutPacking explicitLayout,
glslang::TLayoutMatrix matrixLayout)
{ … }
int TGlslangToSpvTraverser::getMatrixStride(const glslang::TType& matrixType, glslang::TLayoutPacking explicitLayout,
glslang::TLayoutMatrix matrixLayout)
{ … }
void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType, const glslang::TType& memberType,
int& currentOffset, int& nextOffset, glslang::TLayoutPacking explicitLayout, glslang::TLayoutMatrix matrixLayout)
{ … }
void TGlslangToSpvTraverser::declareUseOfStructMember(const glslang::TTypeList& members, int glslangMember)
{ … }
bool TGlslangToSpvTraverser::isShaderEntryPoint(const glslang::TIntermAggregate* node)
{ … }
bool TGlslangToSpvTraverser::writableParam(glslang::TStorageQualifier qualifier) const
{ … }
bool TGlslangToSpvTraverser::originalParam(glslang::TStorageQualifier qualifier, const glslang::TType& paramType,
bool implicitThisParam)
{ … }
void TGlslangToSpvTraverser::makeFunctions(const glslang::TIntermSequence& glslFunctions)
{ … }
void TGlslangToSpvTraverser::makeGlobalInitializers(const glslang::TIntermSequence& initializers)
{ … }
void TGlslangToSpvTraverser::collectRayTracingLinkerObjects()
{ … }
void TGlslangToSpvTraverser::visitFunctions(const glslang::TIntermSequence& glslFunctions)
{ … }
void TGlslangToSpvTraverser::handleFunctionEntry(const glslang::TIntermAggregate* node)
{ … }
void TGlslangToSpvTraverser::translateArguments(const glslang::TIntermAggregate& node, std::vector<spv::Id>& arguments,
spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags)
{ … }
void TGlslangToSpvTraverser::translateArguments(glslang::TIntermUnary& node, std::vector<spv::Id>& arguments)
{ … }
spv::Id TGlslangToSpvTraverser::createImageTextureFunctionCall(glslang::TIntermOperator* node)
{ … }
spv::Id TGlslangToSpvTraverser::handleUserFunctionCall(const glslang::TIntermAggregate* node)
{ … }
spv::Id TGlslangToSpvTraverser::createBinaryOperation(glslang::TOperator op, OpDecorations& decorations,
spv::Id typeId, spv::Id left, spv::Id right,
glslang::TBasicType typeProxy, bool reduceComparison)
{ … }
spv::Id TGlslangToSpvTraverser::createBinaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
spv::Id left, spv::Id right)
{ … }
spv::Id TGlslangToSpvTraverser::createUnaryOperation(glslang::TOperator op, OpDecorations& decorations, spv::Id typeId,
spv::Id operand, glslang::TBasicType typeProxy, const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags,
const glslang::TType &opType)
{ … }
spv::Id TGlslangToSpvTraverser::createUnaryMatrixOperation(spv::Op op, OpDecorations& decorations, spv::Id typeId,
spv::Id operand, glslang::TBasicType )
{ … }
spv::Id TGlslangToSpvTraverser::createIntWidthConversion(glslang::TOperator op, spv::Id operand, int vectorSize, spv::Id destType)
{ … }
spv::Id TGlslangToSpvTraverser::createConversion(glslang::TOperator op, OpDecorations& decorations, spv::Id destType,
spv::Id operand, glslang::TBasicType typeProxy)
{ … }
spv::Id TGlslangToSpvTraverser::makeSmearedConstant(spv::Id constant, int vectorSize)
{ … }
spv::Id TGlslangToSpvTraverser::createAtomicOperation(glslang::TOperator op, spv::Decoration ,
spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy,
const spv::Builder::AccessChain::CoherentFlags &lvalueCoherentFlags, const glslang::TType &opType)
{ … }
spv::Id TGlslangToSpvTraverser::createInvocationsOperation(glslang::TOperator op, spv::Id typeId,
std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
{ … }
spv::Id TGlslangToSpvTraverser::CreateInvocationsVectorOperation(spv::Op op, spv::GroupOperation groupOperation,
spv::Id typeId, std::vector<spv::Id>& operands)
{ … }
spv::Id TGlslangToSpvTraverser::createSubgroupOperation(glslang::TOperator op, spv::Id typeId,
std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
{ … }
spv::Id TGlslangToSpvTraverser::createMiscOperation(glslang::TOperator op, spv::Decoration precision,
spv::Id typeId, std::vector<spv::Id>& operands, glslang::TBasicType typeProxy)
{ … }
spv::Id TGlslangToSpvTraverser::createNoArgOperation(glslang::TOperator op, spv::Decoration precision, spv::Id typeId)
{ … }
spv::Id TGlslangToSpvTraverser::getSymbolId(const glslang::TIntermSymbol* symbol)
{ … }
void TGlslangToSpvTraverser::addMeshNVDecoration(spv::Id id, int member, const glslang::TQualifier& qualifier)
{ … }
bool TGlslangToSpvTraverser::hasQCOMImageProceessingDecoration(spv::Id id, spv::Decoration decor)
{ … }
void TGlslangToSpvTraverser::addImageProcessingQCOMDecoration(spv::Id id, spv::Decoration decor)
{ … }
void TGlslangToSpvTraverser::addImageProcessing2QCOMDecoration(spv::Id id, bool isForGather)
{ … }
spv::Id TGlslangToSpvTraverser::createSpvConstant(const glslang::TIntermTyped& node)
{ … }
spv::Id TGlslangToSpvTraverser::createSpvConstantFromConstUnionArray(const glslang::TType& glslangType,
const glslang::TConstUnionArray& consts, int& nextConst, bool specConstant)
{ … }
bool TGlslangToSpvTraverser::isTrivialLeaf(const glslang::TIntermTyped* node)
{ … }
bool TGlslangToSpvTraverser::isTrivial(const glslang::TIntermTyped* node)
{ … }
spv::Id TGlslangToSpvTraverser::createShortCircuit(glslang::TOperator op, glslang::TIntermTyped& left,
glslang::TIntermTyped& right)
{ … }
spv::Id TGlslangToSpvTraverser::getExtBuiltins(const char* name)
{ … }
};
namespace glslang {
void GetSpirvVersion(std::string& version)
{ … }
int GetSpirvGeneratorVersion()
{ … }
bool OutputSpvBin(const std::vector<unsigned int>& spirv, const char* baseName)
{ … }
bool OutputSpvHex(const std::vector<unsigned int>& spirv, const char* baseName, const char* varName)
{ … }
void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv, SpvOptions* options)
{ … }
void GlslangToSpv(const TIntermediate& intermediate, std::vector<unsigned int>& spirv,
spv::SpvBuildLogger* logger, SpvOptions* options)
{ … }
};