From 1cc48c919cc47895ea5ae23199c20dfe909b4e4c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 12 Mar 2022 00:31:27 +0200 Subject: [PATCH] Set debug names for shader modules on Vulkan --- source/backends/opengl/module_backend.h | 2 ++ source/backends/vulkan/module_backend.cpp | 28 ++++++++++++++++++++++ source/backends/vulkan/module_backend.h | 4 ++++ source/backends/vulkan/program_backend.cpp | 20 +++++++++++++++- source/backends/vulkan/program_backend.h | 3 ++- source/core/module.h | 6 +++++ source/resources/resources.cpp | 3 ++- 7 files changed, 63 insertions(+), 3 deletions(-) diff --git a/source/backends/opengl/module_backend.h b/source/backends/opengl/module_backend.h index bb56c55c..656b2025 100644 --- a/source/backends/opengl/module_backend.h +++ b/source/backends/opengl/module_backend.h @@ -14,6 +14,8 @@ protected: ~OpenGLSpirVModule() = default; void create() { } + + void set_debug_name(const std::string &) { } }; using SpirVModuleBackend = OpenGLSpirVModule; diff --git a/source/backends/vulkan/module_backend.cpp b/source/backends/vulkan/module_backend.cpp index 759497ab..7ec198e0 100644 --- a/source/backends/vulkan/module_backend.cpp +++ b/source/backends/vulkan/module_backend.cpp @@ -39,6 +39,34 @@ void VulkanSpirVModule::create() module_info.pCode = code.data(); vk.CreateShaderModule(module_info, handle); + + if(!debug_name.empty()) + set_vulkan_object_name(); +} + +void VulkanSpirVModule::set_debug_name(const string &name) +{ +#ifdef DEBUG + debug_name = name; + if(handle) + set_vulkan_object_name(); +#else + (void)name; +#endif +} + +void VulkanSpirVModule::set_vulkan_object_name() const +{ +#ifdef DEBUG + const VulkanFunctions &vk = device.get_functions(); + + VkDebugUtilsObjectNameInfoEXT name_info = { }; + name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT; + name_info.objectType = VK_OBJECT_TYPE_SHADER_MODULE; + name_info.objectHandle = reinterpret_cast(handle); + name_info.pObjectName = debug_name.c_str(); + vk.SetDebugUtilsObjectName(name_info); +#endif } diff --git a/source/backends/vulkan/module_backend.h b/source/backends/vulkan/module_backend.h index 12cbe45f..c4729976 100644 --- a/source/backends/vulkan/module_backend.h +++ b/source/backends/vulkan/module_backend.h @@ -15,12 +15,16 @@ class VulkanSpirVModule protected: Device &device; VkShaderModule handle = 0; + std::string debug_name; VulkanSpirVModule(); VulkanSpirVModule(VulkanSpirVModule &&); ~VulkanSpirVModule(); void create(); + + void set_debug_name(const std::string &); + void set_vulkan_object_name() const; }; using SpirVModuleBackend = VulkanSpirVModule; diff --git a/source/backends/vulkan/program_backend.cpp b/source/backends/vulkan/program_backend.cpp index 68ade90c..5af7d67a 100644 --- a/source/backends/vulkan/program_backend.cpp +++ b/source/backends/vulkan/program_backend.cpp @@ -22,7 +22,8 @@ VulkanProgram::VulkanProgram(VulkanProgram &&other): stage_flags(other.stage_flags), creation_info(move(other.creation_info)), desc_set_layout_handles(move(other.desc_set_layout_handles)), - layout_handle(other.layout_handle) + layout_handle(other.layout_handle), + debug_name(move(other.debug_name)) { other.desc_set_layout_handles.clear(); other.layout_handle = 0; @@ -99,6 +100,12 @@ void VulkanProgram::add_spirv_stages(const SpirVModule &mod, const map(this)->specialized_spirv) + spirv->set_debug_name(debug_name); +#endif } void VulkanProgram::finalize_uniforms() @@ -160,5 +167,16 @@ void VulkanProgram::finalize_uniforms() vk.CreatePipelineLayout(layout_info, layout_handle); } +void VulkanProgram::set_debug_name(const string &name) +{ +#ifdef DEBUG + debug_name = name; + if(SpirVModule *spirv = static_cast(this)->specialized_spirv) + spirv->set_debug_name(debug_name); +#else + (void)name; +#endif +} + } // namespace GL } // namespace Msp diff --git a/source/backends/vulkan/program_backend.h b/source/backends/vulkan/program_backend.h index 1454b40b..ff39aa4e 100644 --- a/source/backends/vulkan/program_backend.h +++ b/source/backends/vulkan/program_backend.h @@ -26,6 +26,7 @@ protected: std::vector creation_info; std::vector desc_set_layout_handles; VkPipelineLayout layout_handle = 0; + std::string debug_name; VulkanProgram(); VulkanProgram(VulkanProgram &&); @@ -37,7 +38,7 @@ protected: void finalize_uniforms(); - void set_debug_name(const std::string &) { } + void set_debug_name(const std::string &); }; using ProgramBackend = VulkanProgram; diff --git a/source/core/module.h b/source/core/module.h index 51924d60..26895ee0 100644 --- a/source/core/module.h +++ b/source/core/module.h @@ -61,6 +61,9 @@ private: virtual void compile(SL::Compiler &) = 0; SL::Features create_features() const; + +public: + virtual void set_debug_name(const std::string &) { } }; /** @@ -278,6 +281,9 @@ public: private: std::vector collect_visited_blocks(const std::map &) const; void collect_visited_blocks(unsigned, std::vector &) const; + +public: + void set_debug_name(const std::string &n) { SpirVModuleBackend::set_debug_name(n); } }; } // namespace GL diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 6a9c4f89..ab4b0a8d 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -63,7 +63,8 @@ Resources::Resources(bool set_as_global): .creator([this](const string &n){ return create_mesh(n); }) .notify(&set_debug_name); add_type().suffix(".glsl").suffix(".spv") - .creator([this](const string &n){ return create_module(n); }); + .creator([this](const string &n){ return create_module(n); }) + .notify(&set_debug_name); add_type().base().keyword("object"); add_type().base().base().suffix(".scene") .creator([this](const string &n) -> OccludedScene * { create_generic(n); return 0; }); -- 2.43.0