refactor: move viewport texture bridge into imgui backend

This commit is contained in:
2026-04-01 19:17:44 +08:00
parent 55b3021159
commit d58962ad5e
2 changed files with 52 additions and 22 deletions

View File

@@ -6,6 +6,10 @@
#include <d3d12.h>
#include <dxgi1_6.h>
#include <XCEngine/RHI/D3D12/D3D12Device.h>
#include <XCEngine/RHI/D3D12/D3D12Texture.h>
#include <XCEngine/RHI/RHIDevice.h>
#include <XCEngine/RHI/RHITexture.h>
#include <imgui.h>
#include <imgui_impl_dx12.h>
#include <imgui_impl_win32.h>
@@ -98,6 +102,45 @@ public:
AllocateSrvDescriptorInternal(outCpuHandle, outGpuHandle);
}
bool CreateTextureDescriptor(
::XCEngine::RHI::RHIDevice* device,
::XCEngine::RHI::RHITexture* texture,
D3D12_CPU_DESCRIPTOR_HANDLE* outCpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE* outGpuHandle,
ImTextureID* outTextureId) {
if (device == nullptr ||
texture == nullptr ||
outCpuHandle == nullptr ||
outGpuHandle == nullptr ||
outTextureId == nullptr) {
return false;
}
auto* nativeDevice = dynamic_cast<::XCEngine::RHI::D3D12Device*>(device);
auto* nativeTexture = dynamic_cast<::XCEngine::RHI::D3D12Texture*>(texture);
if (nativeDevice == nullptr || nativeTexture == nullptr || nativeTexture->GetResource() == nullptr) {
return false;
}
AllocateTextureDescriptor(outCpuHandle, outGpuHandle);
if (outCpuHandle->ptr == 0 || outGpuHandle->ptr == 0) {
return false;
}
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
nativeDevice->GetDevice()->CreateShaderResourceView(
nativeTexture->GetResource(),
&srvDesc,
*outCpuHandle);
*outTextureId = static_cast<ImTextureID>(outGpuHandle->ptr);
return true;
}
void FreeTextureDescriptor(
D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle) {

View File

@@ -10,8 +10,7 @@
#include <XCEngine/Components/CameraComponent.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/RHI/D3D12/D3D12Device.h>
#include <XCEngine/RHI/D3D12/D3D12Texture.h>
#include <XCEngine/RHI/RHIDevice.h>
#include <XCEngine/RHI/RHIEnums.h>
#include <XCEngine/RHI/RHIResourceView.h>
#include <XCEngine/RHI/RHITexture.h>
@@ -127,7 +126,7 @@ public:
void Initialize(UI::ImGuiBackendBridge& backend, RHI::RHIDevice* device) {
Shutdown();
m_backend = &backend;
m_device = device ? static_cast<RHI::D3D12Device*>(device) : nullptr;
m_device = device;
}
void Shutdown() {
@@ -497,24 +496,12 @@ private:
}
bool CreateViewportTextureDescriptor(ViewportEntry& entry) {
m_backend->AllocateTextureDescriptor(&entry.imguiCpuHandle, &entry.imguiGpuHandle);
if (entry.imguiCpuHandle.ptr == 0 || entry.imguiGpuHandle.ptr == 0) {
return false;
}
auto* nativeTexture = static_cast<RHI::D3D12Texture*>(entry.colorTexture);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = 1;
m_device->GetDevice()->CreateShaderResourceView(
nativeTexture->GetResource(),
&srvDesc,
entry.imguiCpuHandle);
entry.textureId = static_cast<ImTextureID>(entry.imguiGpuHandle.ptr);
return true;
return m_backend->CreateTextureDescriptor(
m_device,
entry.colorTexture,
&entry.imguiCpuHandle,
&entry.imguiGpuHandle,
&entry.textureId);
}
bool EnsureViewportResources(ViewportEntry& entry) {
@@ -1016,7 +1003,7 @@ private:
}
UI::ImGuiBackendBridge* m_backend = nullptr;
RHI::D3D12Device* m_device = nullptr;
RHI::RHIDevice* m_device = nullptr;
std::unique_ptr<Rendering::SceneRenderer> m_sceneRenderer;
Rendering::RenderContext m_sceneViewLastRenderContext = {};
std::array<ViewportEntry, 2> m_entries = {};