Non-functional: Rationalize some existing use of SPIRV-Tools.

This commit is contained in:
John Kessenich 2018-08-22 17:12:46 -06:00
parent 1323bf8e39
commit e7df8e0b76
3 changed files with 86 additions and 70 deletions

View File

@ -3219,7 +3219,7 @@ void TGlslangToSpvTraverser::updateMemberOffset(const glslang::TType& structType
// adjusting this late means inconsistencies with earlier code, which for reflection is an issue // adjusting this late means inconsistencies with earlier code, which for reflection is an issue
// Until reflection is brought in sync with these adjustments, don't apply to $Global, // Until reflection is brought in sync with these adjustments, don't apply to $Global,
// which is the most likely to rely on reflection, and least likely to rely implicit layouts // which is the most likely to rely on reflection, and least likely to rely implicit layouts
if (glslangIntermediate->usingHlslOFfsets() && if (glslangIntermediate->usingHlslOffsets() &&
! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) { ! memberType.isArray() && memberType.isVector() && structType.getTypeName().compare("$Global") != 0) {
int dummySize; int dummySize;
int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize); int componentAlignment = glslangIntermediate->getBaseAlignmentScalar(memberType, dummySize);
@ -7003,31 +7003,12 @@ void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsign
GlslangToSpv(intermediate, spirv, &logger, options); GlslangToSpv(intermediate, spirv, &logger, options);
} }
void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
spv::SpvBuildLogger* logger, SpvOptions* options)
{
TIntermNode* root = intermediate.getTreeRoot();
if (root == 0)
return;
glslang::SpvOptions defaultOptions;
if (options == nullptr)
options = &defaultOptions;
glslang::GetThreadPoolAllocator().push();
TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options);
root->traverse(&it);
it.finishSpv();
it.dumpSpv(spirv);
#if ENABLE_OPT #if ENABLE_OPT
// If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
// eg. forward and remove memory writes of opaque types. // Apply the SPIRV-Tools validator to generated SPIR-V.
if ((intermediate.getSource() == EShSourceHlsl || void SpirvToolsLegalize(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
options->optimizeSize) && spv::SpvBuildLogger* logger, const SpvOptions* options)
!options->disableOptimizer) { {
spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2; spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
spvtools::Optimizer optimizer(target_env); spvtools::Optimizer optimizer(target_env);
@ -7091,8 +7072,36 @@ void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsign
optimizer.RegisterPass(CreateAggressiveDCEPass()); optimizer.RegisterPass(CreateAggressiveDCEPass());
optimizer.RegisterPass(CreateCFGCleanupPass()); optimizer.RegisterPass(CreateCFGCleanupPass());
if (!optimizer.Run(spirv.data(), spirv.size(), &spirv)) optimizer.Run(spirv.data(), spirv.size(), &spirv);
}
#endif
void GlslangToSpv(const glslang::TIntermediate& intermediate, std::vector<unsigned int>& spirv,
spv::SpvBuildLogger* logger, SpvOptions* options)
{
TIntermNode* root = intermediate.getTreeRoot();
if (root == 0)
return; return;
glslang::SpvOptions defaultOptions;
if (options == nullptr)
options = &defaultOptions;
glslang::GetThreadPoolAllocator().push();
TGlslangToSpvTraverser it(intermediate.getSpv().spv, &intermediate, logger, *options);
root->traverse(&it);
it.finishSpv();
it.dumpSpv(spirv);
#if ENABLE_OPT
// If from HLSL, run spirv-opt to "legalize" the SPIR-V for Vulkan
// eg. forward and remove memory writes of opaque types.
if ((intermediate.getSource() == EShSourceHlsl || options->optimizeSize) &&
!options->disableOptimizer) {
SpirvToolsLegalize(intermediate, spirv, logger, options);
} }
#endif #endif

View File

@ -723,16 +723,23 @@ void Disassemble(std::ostream& out, const std::vector<unsigned int>& stream)
// Use the SPIRV-Tools disassembler to print SPIR-V. // Use the SPIRV-Tools disassembler to print SPIR-V.
void SpirvToolsDisassemble(std::ostream& out, const std::vector<unsigned int>& spirv) void SpirvToolsDisassemble(std::ostream& out, const std::vector<unsigned int>& spirv)
{ {
// disassemble
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_3); spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_3);
spv_text text; spv_text text;
spv_diagnostic diagnostic = nullptr; spv_diagnostic diagnostic = nullptr;
spvBinaryToText(context, &spirv.front(), spirv.size(), spvBinaryToText(context, spirv.data(), spirv.size(),
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES | SPV_BINARY_TO_TEXT_OPTION_INDENT, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES | SPV_BINARY_TO_TEXT_OPTION_INDENT,
&text, &diagnostic); &text, &diagnostic);
// dump
if (diagnostic == nullptr) if (diagnostic == nullptr)
out << text->str; out << text->str;
else else
spvDiagnosticPrint(diagnostic); spvDiagnosticPrint(diagnostic);
// teardown
spvDiagnosticDestroy(diagnostic);
spvContextDestroy(context);
} }
#endif #endif

View File

@ -349,7 +349,7 @@ public:
if (hlslOffsets) if (hlslOffsets)
processes.addProcess("hlsl-offsets"); processes.addProcess("hlsl-offsets");
} }
bool usingHlslOFfsets() const { return hlslOffsets; } bool usingHlslOffsets() const { return hlslOffsets; }
void setUseStorageBuffer() void setUseStorageBuffer()
{ {
useStorageBuffer = true; useStorageBuffer = true;