From a28303c4bd1f4444691e86441057408d9d45faa8 Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Tue, 10 Apr 2018 12:48:46 +0200 Subject: [PATCH] [dxvk] Optimize binding ID remapping We don't need to iterate over the full shader code when creating a new shader module. This optimization may slightly reduce the initial pipeline creation time. --- src/dxvk/dxvk_shader.cpp | 25 +++++++++++++------------ src/dxvk/dxvk_shader.h | 1 + 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/dxvk/dxvk_shader.cpp b/src/dxvk/dxvk_shader.cpp index f104078da..cbc1558e1 100644 --- a/src/dxvk/dxvk_shader.cpp +++ b/src/dxvk/dxvk_shader.cpp @@ -49,6 +49,15 @@ namespace dxvk { : m_stage(stage), m_code(code), m_interface(iface) { for (uint32_t i = 0; i < slotCount; i++) m_slots.push_back(slotInfos[i]); + + // Gather the offsets where the binding IDs + // are stored so we can quickly remap them. + for (auto ins : m_code) { + if (ins.opCode() == spv::OpDecorate + && ((ins.arg(2) == spv::DecorationBinding) + || (ins.arg(2) == spv::DecorationSpecId))) + m_idOffsets.push_back(ins.offset() + 3); + } } @@ -81,20 +90,12 @@ namespace dxvk { Rc DxvkShader::createShaderModule( const Rc& vkd, const DxvkDescriptorSlotMapping& mapping) const { - // Iterate over the code and replace every resource slot - // index with the corresponding mapped binding index. SpirvCodeBuffer spirvCode = m_code; - for (auto ins : spirvCode) { - if (ins.opCode() == spv::OpDecorate - && ((ins.arg(2) == spv::DecorationBinding) - || (ins.arg(2) == spv::DecorationSpecId))) { - - const uint32_t oldBinding = ins.arg(3); - const uint32_t newBinding = mapping.getBindingId(oldBinding); - ins.setArg(3, newBinding); - } - } + // Remap resource binding IDs + uint32_t* code = spirvCode.data(); + for (uint32_t ofs : m_idOffsets) + code[ofs] = mapping.getBindingId(code[ofs]); return new DxvkShaderModule(vkd, m_stage, spirvCode, m_debugName); } diff --git a/src/dxvk/dxvk_shader.h b/src/dxvk/dxvk_shader.h index ce64624aa..06ff35de8 100644 --- a/src/dxvk/dxvk_shader.h +++ b/src/dxvk/dxvk_shader.h @@ -176,6 +176,7 @@ namespace dxvk { SpirvCodeBuffer m_code; std::vector m_slots; + std::vector m_idOffsets; DxvkInterfaceSlots m_interface; std::string m_debugName;