KhronosGroup / SPIRV-Tools

Apache License 2.0
1.09k stars 559 forks source link

How can I implement the following feature: I want to override the Process_vkCreateShaderModule function in VulkanConsumer and modify the pCode (SPIR-V code) in the ShaderModule, then apply these modifications to a new file. #5816

Closed PjLovePb closed 2 months ago

PjLovePb commented 2 months ago
GFXRECON_BEGIN_NAMESPACE(decode)

class VulkanShaderOptimizeCosumer : public VulkanConsumer
{
  public:
    VulkanShaderOptimizeCosumer(gfxrecon::encode::CaptureEncoder* encoder) : encoder_(encoder) {}

    virtual void
    Process_vkCreateShaderModule(const gfxrecon::decode::ApiCallInfo&                                      call_info,
                                 VkResult                                                                  return_value,
                                 gfxrecon::format::HandleId                                                device,
                                 gfxrecon::decode::StructPointerDecoder<Decoded_VkShaderModuleCreateInfo>* pCreateInfo,
                                 gfxrecon::decode::StructPointerDecoder<Decoded_VkAllocationCallbacks>*    pAllocator,
                                 gfxrecon::decode::HandlePointerDecoder<VkShaderModule>* pShaderModule) override
    {
        if (pCreateInfo && pCreateInfo->GetPointer())
        {
            auto create_info = pCreateInfo->GetPointer();
            if (create_info->pCode)
            {
                //read the old code ,then apply some change
                std::vector<uint32_t> spirv_code(create_info->pCode,
                                                 create_info->pCode + create_info->codeSize / sizeof(uint32_t));

                  modified_spirv_code_  = std::move(spirv_code);
                   create_info->pCode    = modified_spirv_code_.data();
                   create_info->codeSize = modified_spirv_code_.size() * sizeof(uint32_t);

                //todo: How can I implement the following feature: I want to override the Process_vkCreateShaderModule function in VulkanConsumer and modify the pCode (SPIR-V code) in the ShaderModule, then apply these modifications to a new file.
            }
        }

    }

    private:
    gfxrecon::encode::CaptureEncoder* encoder_;
    std::vector<uint32_t>             modified_spirv_code_;

};

GFXRECON_END_NAMESPACE(decode)