From 98b8d410168e526dba6fe1950df111a631e6a8de Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sun, 15 Apr 2018 21:00:08 +0200 Subject: [PATCH] [dxbc] Write shader name to the generated SPIR-V Might help identifying shaders in debugging tools such as Renderdoc. --- src/d3d11/d3d11_shader.cpp | 2 +- src/dxbc/dxbc_compiler.cpp | 7 +++++++ src/dxbc/dxbc_compiler.h | 1 + src/dxbc/dxbc_module.cpp | 7 +++++-- src/dxbc/dxbc_module.h | 8 ++++++-- src/spirv/spirv_module.cpp | 25 +++++++++++++++++++++++++ src/spirv/spirv_module.h | 9 +++++++++ tests/dxbc/test_dxbc_compiler.cpp | 5 +++-- 8 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/d3d11/d3d11_shader.cpp b/src/d3d11/d3d11_shader.cpp index 4b08d72f3..732b86fd9 100644 --- a/src/d3d11/d3d11_shader.cpp +++ b/src/d3d11/d3d11_shader.cpp @@ -68,7 +68,7 @@ namespace dxvk { std::ios_base::binary | std::ios_base::trunc)); } - m_shader = module.compile(*pDxbcOptions); + m_shader = module.compile(*pDxbcOptions, m_name); m_shader->setDebugName(m_name); if (dumpPath.size() != 0) { diff --git a/src/dxbc/dxbc_compiler.cpp b/src/dxbc/dxbc_compiler.cpp index 7ce5de499..dbb38d6b4 100644 --- a/src/dxbc/dxbc_compiler.cpp +++ b/src/dxbc/dxbc_compiler.cpp @@ -9,6 +9,7 @@ namespace dxvk { constexpr uint32_t PushConstant_InstanceId = 0; DxbcCompiler::DxbcCompiler( + const std::string& fileName, const DxbcOptions& options, const DxbcProgramVersion& version, const Rc& isgn, @@ -23,6 +24,12 @@ namespace dxvk { // initialization phase where the execution mode is set. m_entryPointId = m_module.allocateId(); + // Set the shader name so that we recognize it in renderdoc + m_module.setDebugSource( + spv::SourceLanguageUnknown, 0, + m_module.addDebugString(fileName.c_str()), + ""); + // Set the memory model. This is the same for all shaders. m_module.setMemoryModel( spv::AddressingModelLogical, diff --git a/src/dxbc/dxbc_compiler.h b/src/dxbc/dxbc_compiler.h index 98e91912e..5532efbb4 100644 --- a/src/dxbc/dxbc_compiler.h +++ b/src/dxbc/dxbc_compiler.h @@ -311,6 +311,7 @@ namespace dxvk { public: DxbcCompiler( + const std::string& fileName, const DxbcOptions& options, const DxbcProgramVersion& version, const Rc& isgn, diff --git a/src/dxbc/dxbc_module.cpp b/src/dxbc/dxbc_module.cpp index 5ab1ba856..f70ab68ef 100644 --- a/src/dxbc/dxbc_module.cpp +++ b/src/dxbc/dxbc_module.cpp @@ -41,7 +41,9 @@ namespace dxvk { } - Rc DxbcModule::compile(const DxbcOptions& options) const { + Rc DxbcModule::compile( + const DxbcOptions& options, + const std::string& fileName) const { if (m_shexChunk == nullptr) throw DxvkError("DxbcModule::compile: No SHDR/SHEX chunk"); @@ -52,7 +54,8 @@ namespace dxvk { m_isgnChunk, m_osgnChunk, analysisInfo); - DxbcCompiler compiler(options, + DxbcCompiler compiler( + fileName, options, m_shexChunk->version(), m_isgnChunk, m_osgnChunk, analysisInfo); diff --git a/src/dxbc/dxbc_module.h b/src/dxbc/dxbc_module.h index c4a792369..1c3bfa30a 100644 --- a/src/dxbc/dxbc_module.h +++ b/src/dxbc/dxbc_module.h @@ -52,13 +52,17 @@ namespace dxvk { * \brief Compiles DXBC shader to SPIR-V module * * \param [in] options DXBC compiler options + * \param [in] fileName File name, will be added to + * the compiled SPIR-V for debugging purposes. * \returns The compiled shader object */ - Rc compile(const DxbcOptions& options) const; + Rc compile( + const DxbcOptions& options, + const std::string& fileName) const; private: - DxbcHeader m_header; + DxbcHeader m_header; Rc m_isgnChunk; Rc m_osgnChunk; diff --git a/src/spirv/spirv_module.cpp b/src/spirv/spirv_module.cpp index 2b01a5a6e..a14e4cad4 100644 --- a/src/spirv/spirv_module.cpp +++ b/src/spirv/spirv_module.cpp @@ -116,6 +116,31 @@ namespace dxvk { } + uint32_t SpirvModule::addDebugString( + const char* string) { + uint32_t resultId = this->allocateId(); + + m_debugNames.putIns (spv::OpString, + 2 + m_debugNames.strLen(string)); + m_debugNames.putWord(resultId); + m_debugNames.putStr (string); + return resultId; + } + + + void SpirvModule::setDebugSource( + spv::SourceLanguage language, + uint32_t version, + uint32_t file, + const char* source) { + m_debugNames.putIns (spv::OpSource, + 4 + m_debugNames.strLen(source)); + m_debugNames.putWord(language); + m_debugNames.putWord(version); + m_debugNames.putWord(file); + m_debugNames.putStr (source); + } + void SpirvModule::setDebugName( uint32_t expressionId, const char* debugName) { diff --git a/src/spirv/spirv_module.h b/src/spirv/spirv_module.h index 9819c9c8d..93da6952a 100644 --- a/src/spirv/spirv_module.h +++ b/src/spirv/spirv_module.h @@ -89,6 +89,15 @@ namespace dxvk { uint32_t entryPointId, uint32_t vertexCount); + uint32_t addDebugString( + const char* string); + + void setDebugSource( + spv::SourceLanguage language, + uint32_t version, + uint32_t file, + const char* source); + void setDebugName( uint32_t expressionId, const char* debugName); diff --git a/tests/dxbc/test_dxbc_compiler.cpp b/tests/dxbc/test_dxbc_compiler.cpp index e5d8c2d66..668981c3c 100644 --- a/tests/dxbc/test_dxbc_compiler.cpp +++ b/tests/dxbc/test_dxbc_compiler.cpp @@ -28,7 +28,8 @@ int WINAPI WinMain(HINSTANCE hInstance, } try { - std::ifstream ifile(str::fromws(argv[1]), std::ios::binary); + std::string ifileName = str::fromws(argv[1]); + std::ifstream ifile(ifileName, std::ios::binary); ifile.ignore(std::numeric_limits::max()); std::streamsize length = ifile.gcount(); ifile.clear(); @@ -40,7 +41,7 @@ int WINAPI WinMain(HINSTANCE hInstance, DxbcReader reader(dxbcCode.data(), dxbcCode.size()); DxbcModule module(reader); - Rc shader = module.compile(DxbcOptions()); + Rc shader = module.compile(DxbcOptions(), ifileName); std::ofstream ofile(str::fromws(argv[2]), std::ios::binary); shader->dump(ofile); return 0;