From 8b988912c2e808cc19faccdc784f4e3a888bd29f Mon Sep 17 00:00:00 2001 From: hachem Date: Thu, 20 Aug 2026 16:51:57 +0200 Subject: chore: move ImGui HDRI switching to Vulkan --- src/platform/vulkan/vulkan_renderer.cpp | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/platform/vulkan/vulkan_renderer.cpp') diff --git a/src/platform/vulkan/vulkan_renderer.cpp b/src/platform/vulkan/vulkan_renderer.cpp index 30eed3b..f88bbea 100644 --- a/src/platform/vulkan/vulkan_renderer.cpp +++ b/src/platform/vulkan/vulkan_renderer.cpp @@ -94,6 +94,7 @@ namespace Donut bool left_was_down = false; VkImage cube_image = VK_NULL_HANDLE; VkDeviceMemory cube_mem = VK_NULL_HANDLE; VkImageView cube_view = VK_NULL_HANDLE; VkSampler cube_sampler = VK_NULL_HANDLE; + std::string hdri_path; VkDescriptorSetLayout geo_set_layout = VK_NULL_HANDLE; VkDescriptorPool geo_pool = VK_NULL_HANDLE; VkDescriptorSet geo_set = VK_NULL_HANDLE; @@ -125,6 +126,7 @@ namespace Donut auto create_sync_objects() -> bool; auto create_geodesic_resources() -> bool; auto create_hdri_cubemap(const char* path) -> bool; + auto rebuild_hdri_cubemap(const char* path) -> void; auto create_present_resources() -> bool; auto process_input() -> void; auto update_geodesic_uniforms() -> void; @@ -590,6 +592,7 @@ namespace Donut auto VulkanRenderer::Impl::create_hdri_cubemap(const char* path) -> bool { + hdri_path = path; const VkMemoryPropertyFlags host_vis = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; const uint32_t FACE = 1024; const VkFormat cube_fmt = VK_FORMAT_R16G16B16A16_SFLOAT; @@ -832,6 +835,26 @@ namespace Donut return true; } + auto VulkanRenderer::Impl::rebuild_hdri_cubemap(const char* path) -> void + { + vkDeviceWaitIdle(device); + + if (cube_sampler) vkDestroySampler(device, cube_sampler, nullptr); + if (cube_view) vkDestroyImageView(device, cube_view, nullptr); + if (cube_image) vkDestroyImage(device, cube_image, nullptr); + if (cube_mem) vkFreeMemory(device, cube_mem, nullptr); + cube_sampler = VK_NULL_HANDLE; cube_view = VK_NULL_HANDLE; + cube_image = VK_NULL_HANDLE; cube_mem = VK_NULL_HANDLE; + + create_hdri_cubemap(path); + + VkDescriptorImageInfo cube_info{ cube_sampler, cube_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL }; + VkWriteDescriptorSet write{ VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET }; + write.dstSet = geo_set; write.dstBinding = 4; write.descriptorCount = 1; + write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; write.pImageInfo = &cube_info; + vkUpdateDescriptorSets(device, 1, &write, 0, nullptr); + } + auto VulkanRenderer::Impl::create_present_resources() -> bool { VkSamplerCreateInfo smci{ VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO }; @@ -1116,6 +1139,19 @@ namespace Donut m_impl->width = width; m_impl->height = height; } + auto VulkanRenderer::set_hdri(const std::string& path) -> void + { + Impl& v = *m_impl; + if (v.device == VK_NULL_HANDLE || v.geo_set == VK_NULL_HANDLE) return; + if (v.hdri_path == path) return; + v.rebuild_hdri_cubemap(path.c_str()); + } + + auto VulkanRenderer::current_hdri() const -> const std::string& + { + return m_impl->hdri_path; + } + auto VulkanRenderer::draw_frame(const glm::vec4& clear_color, const std::function& build_ui) -> void { Impl& v = *m_impl; -- cgit v1.3