diff --git a/editor/src/UI/ImGuiBackendBridge.h b/editor/src/UI/ImGuiBackendBridge.h index 12171f7e..a0df1e81 100644 --- a/editor/src/UI/ImGuiBackendBridge.h +++ b/editor/src/UI/ImGuiBackendBridge.h @@ -6,6 +6,10 @@ #include #include +#include +#include +#include +#include #include #include #include @@ -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(outGpuHandle->ptr); + return true; + } + void FreeTextureDescriptor( D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle, D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle) { diff --git a/editor/src/Viewport/ViewportHostService.h b/editor/src/Viewport/ViewportHostService.h index f9d8ee2f..75659c3c 100644 --- a/editor/src/Viewport/ViewportHostService.h +++ b/editor/src/Viewport/ViewportHostService.h @@ -10,8 +10,7 @@ #include #include -#include -#include +#include #include #include #include @@ -127,7 +126,7 @@ public: void Initialize(UI::ImGuiBackendBridge& backend, RHI::RHIDevice* device) { Shutdown(); m_backend = &backend; - m_device = device ? static_cast(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(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(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 m_sceneRenderer; Rendering::RenderContext m_sceneViewLastRenderContext = {}; std::array m_entries = {};