Enhance XCUI demo text editing and host bridge

This commit is contained in:
2026-04-05 05:58:05 +08:00
parent a7662a1d43
commit d1cb0c874b
11 changed files with 479 additions and 24 deletions

View File

@@ -182,7 +182,8 @@ public:
void Render(
UI::ImGuiBackendBridge& imguiBackend,
const float clearColor[4],
const RenderCallback& beforeUiRender = {}) {
const RenderCallback& beforeUiRender = {},
const RenderCallback& afterUiRender = {}) {
auto* d3d12Queue = GetD3D12CommandQueue();
auto* d3d12CommandList = GetD3D12CommandList();
if (m_swapChain == nullptr ||
@@ -222,6 +223,11 @@ public:
d3d12CommandList->SetDescriptorHeaps(1, descriptorHeaps);
imguiBackend.RenderDrawData(d3d12CommandList->GetCommandList());
if (afterUiRender) {
d3d12CommandList->SetRenderTargets(1, &renderTargetView, nullptr);
afterUiRender(GetRenderContext(), renderSurface);
}
d3d12CommandList->TransitionBarrier(
renderTargetView,
RHI::ResourceStates::RenderTarget,

View File

@@ -7,8 +7,10 @@
#include <d3d12.h>
#include <dxgi1_6.h>
#include <XCEngine/RHI/D3D12/D3D12Device.h>
#include <XCEngine/RHI/D3D12/D3D12ResourceView.h>
#include <XCEngine/RHI/D3D12/D3D12Texture.h>
#include <XCEngine/RHI/RHIDevice.h>
#include <XCEngine/RHI/RHIResourceView.h>
#include <XCEngine/RHI/RHITexture.h>
#include <imgui.h>
#include <imgui_impl_dx12.h>
@@ -168,6 +170,63 @@ public:
return true;
}
bool CreateTextureDescriptor(
::XCEngine::RHI::RHIDevice* device,
::XCEngine::RHI::RHIResourceView* shaderResourceView,
D3D12_CPU_DESCRIPTOR_HANDLE* outCpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE* outGpuHandle,
ImTextureID* outTextureId) {
if (device == nullptr ||
shaderResourceView == nullptr ||
outCpuHandle == nullptr ||
outGpuHandle == nullptr ||
outTextureId == nullptr) {
return false;
}
*outCpuHandle = {};
*outGpuHandle = {};
*outTextureId = {};
auto* nativeDevice = dynamic_cast<::XCEngine::RHI::D3D12Device*>(device);
auto* nativeView = dynamic_cast<::XCEngine::RHI::D3D12ResourceView*>(shaderResourceView);
if (nativeDevice == nullptr ||
nativeView == nullptr ||
!nativeView->IsValid() ||
nativeView->GetViewType() != ::XCEngine::RHI::ResourceViewType::ShaderResource) {
return false;
}
if (m_srvHeap == nullptr ||
m_srvDescriptorSize == 0 ||
m_srvUsage.empty() ||
std::find(m_srvUsage.begin(), m_srvUsage.end(), false) == m_srvUsage.end()) {
return false;
}
const D3D12_CPU_DESCRIPTOR_HANDLE sourceCpuHandle = nativeView->GetCPUHandle();
if (sourceCpuHandle.ptr == 0) {
return false;
}
AllocateTextureDescriptor(outCpuHandle, outGpuHandle);
if (outCpuHandle->ptr == 0 || outGpuHandle->ptr == 0) {
*outCpuHandle = {};
*outGpuHandle = {};
return false;
}
// ImGui samples from its own shader-visible heap, so copy the existing SRV into an ImGui-owned slot.
nativeDevice->GetDevice()->CopyDescriptorsSimple(
1,
*outCpuHandle,
sourceCpuHandle,
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
*outTextureId = static_cast<ImTextureID>(outGpuHandle->ptr);
return true;
}
void FreeTextureDescriptor(
D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle) {