From e6371069fb9da1aecd381ccac3cd47b0a9676f34 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Mon, 29 Sep 2025 05:30:32 -0400 Subject: [PATCH] Fixes ImGui Texture --- src/engine/assets/texture/Texture.cpp | 28 ++++++++++++++++++--------- src/engine/assets/texture/Texture.hpp | 7 +++++-- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/engine/assets/texture/Texture.cpp b/src/engine/assets/texture/Texture.cpp index 2f98107..9b35925 100644 --- a/src/engine/assets/texture/Texture.cpp +++ b/src/engine/assets/texture/Texture.cpp @@ -76,7 +76,7 @@ namespace fgl::engine const ImVec2 imgui_size { static_cast< float >( extent.width ), static_cast< float >( extent.height ) }; - ImGui::Image( getImGuiDescriptorSet(), imgui_size ); + ImGui::Image( getImGuiTexture(), imgui_size ); } bool Texture::drawImGuiButton( vk::Extent2D extent ) @@ -98,7 +98,7 @@ namespace fgl::engine const ImVec2 imgui_size { static_cast< float >( extent.width ), static_cast< float >( extent.height ) }; - return ImGui::ImageButton( m_name.c_str(), getImGuiDescriptorSet(), imgui_size ); + return ImGui::ImageButton( m_name.c_str(), getImGuiTexture(), imgui_size ); } Texture::Texture( std::tuple< std::vector< std::byte >, int, int, vk::Format, Sampler > tuple ) : @@ -176,6 +176,23 @@ namespace fgl::engine return m_image_view->descriptorInfo( vk::ImageLayout::eGeneral ); } + ImTextureRef Texture::createImGuiTexture() const + { + const ImTextureRef ref { m_imgui_set }; + + return ref; + } + + ImTextureRef Texture::getImGuiTexture() const + { + if ( !m_imgui_texture.has_value() ) + { + m_imgui_texture = createImGuiTexture(); + } + + return m_imgui_texture.value(); + } + vk::Extent2D Texture::getExtent() const { return m_image_view->getExtent(); @@ -208,13 +225,6 @@ namespace fgl::engine m_imgui_set = ImGui_ImplVulkan_AddTexture( vk_sampler, vk_view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL ); } - vk::DescriptorSet& Texture::getImGuiDescriptorSet() - { - assert( ready() ); - assert( m_imgui_set != VK_NULL_HANDLE ); - return m_imgui_set; - } - Texture::Texture( const std::shared_ptr< Image >& image, Sampler&& sampler ) : m_texture_id( texture_id_pool.getID() ), m_image( image ), diff --git a/src/engine/assets/texture/Texture.hpp b/src/engine/assets/texture/Texture.hpp index 8290a53..59abc29 100644 --- a/src/engine/assets/texture/Texture.hpp +++ b/src/engine/assets/texture/Texture.hpp @@ -61,7 +61,8 @@ namespace fgl::engine vk::Extent2D m_extent; //! Descriptor set used for displaying the texture in ImGui - vk::DescriptorSet m_imgui_set { VK_NULL_HANDLE }; + mutable vk::DescriptorSet m_imgui_set { VK_NULL_HANDLE }; + mutable std::optional< ImTextureRef > m_imgui_texture { std::nullopt }; std::string m_name; @@ -111,7 +112,9 @@ namespace fgl::engine const std::string& getName() const { return m_name; } [[nodiscard]] vk::DescriptorImageInfo getDescriptor() const; - [[nodiscard]] vk::DescriptorSet& getImGuiDescriptorSet(); + [[nodiscard]] ImTextureRef createImGuiTexture() const; + // [[nodiscard]] vk::DescriptorSet& getImGuiDescriptorSet(); + [[nodiscard]] ImTextureRef getImGuiTexture() const; [[nodiscard]] vk::Extent2D getExtent() const;