ADD: Texture cache system improvement 2

This commit is contained in:
2025-12-25 20:08:25 +09:00
parent fae718e9b3
commit 0172996e12
3 changed files with 884 additions and 47 deletions

View File

@@ -16,9 +16,12 @@
#include <glm/gtx/matrix_decompose.hpp>
#include <glm/gtx/quaternion.hpp>
#include <cstdint>
#include <filesystem>
// ImGui integration for texture display
#include "imgui.h"
#include "imgui_impl_vulkan.h"
namespace GameAPI
{
@@ -332,6 +335,49 @@ void Engine::unload_texture(TextureHandle handle)
_engine->_textureCache->unload(cacheHandle);
}
void* Engine::create_imgui_texture(TextureHandle handle, void* sampler)
{
if (!_engine || !_engine->_textureCache)
{
return nullptr;
}
auto cacheHandle = static_cast<TextureCache::TextureHandle>(handle);
VkImageView imageView = _engine->_textureCache->image_view(cacheHandle);
if (imageView == VK_NULL_HANDLE)
{
return nullptr;
}
// Use provided sampler or default linear sampler
VkSampler vkSampler = reinterpret_cast<VkSampler>(sampler);
if (vkSampler == VK_NULL_HANDLE && _engine->_context && _engine->_context->samplers)
{
vkSampler = _engine->_context->samplers->defaultLinear();
}
// Create ImGui descriptor set using ImGui_ImplVulkan
VkDescriptorSet descriptorSet = ImGui_ImplVulkan_AddTexture(
vkSampler,
imageView,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
);
return reinterpret_cast<void*>(descriptorSet);
}
void Engine::free_imgui_texture(void* imgui_texture_id)
{
if (imgui_texture_id == nullptr)
{
return;
}
VkDescriptorSet descriptorSet = reinterpret_cast<VkDescriptorSet>(imgui_texture_id);
ImGui_ImplVulkan_RemoveTexture(descriptorSet);
}
// ----------------------------------------------------------------------------
// Shadows
// ----------------------------------------------------------------------------

View File

@@ -383,6 +383,15 @@ public:
// This is optional - the cache manages memory automatically
void unload_texture(TextureHandle handle);
// Create an ImGui descriptor set for a texture (for use with ImGui::Image())
// Returns ImTextureID (actually VkDescriptorSet) that can be used in ImGui
// The returned descriptor set is managed by ImGui and valid until cleanup
// sampler: VK_NULL_HANDLE uses default linear sampler
void* create_imgui_texture(TextureHandle handle, void* sampler = nullptr);
// Free an ImGui descriptor set created by create_imgui_texture()
void free_imgui_texture(void* imgui_texture_id);
// ------------------------------------------------------------------------
// Shadows
// ------------------------------------------------------------------------