Separate viewport target and descriptor ownership

This commit is contained in:
2026-04-13 19:57:25 +08:00
parent f3fc34898a
commit 5b89c2bb76
10 changed files with 534 additions and 433 deletions

View File

@@ -8,11 +8,8 @@ namespace XCEngine::UI::Editor::Host {
using ::XCEngine::RHI::CommandListDesc;
using ::XCEngine::RHI::CommandQueueDesc;
using ::XCEngine::RHI::DescriptorHeapType;
using ::XCEngine::RHI::DescriptorPoolDesc;
using ::XCEngine::RHI::D3D12CommandList;
using ::XCEngine::RHI::D3D12CommandQueue;
using ::XCEngine::RHI::D3D12DescriptorHeap;
using ::XCEngine::RHI::D3D12Device;
using ::XCEngine::RHI::D3D12SwapChain;
using ::XCEngine::RHI::D3D12Texture;
@@ -101,20 +98,6 @@ bool D3D12WindowRenderer::Initialize(HWND hwnd, int width, int height) {
return false;
}
DescriptorPoolDesc srvPoolDesc = {};
srvPoolDesc.type = DescriptorHeapType::CBV_SRV_UAV;
srvPoolDesc.descriptorCount = kSrvDescriptorCount;
srvPoolDesc.shaderVisible = true;
m_srvPool = m_device->CreateDescriptorPool(srvPoolDesc);
m_srvHeap = dynamic_cast<D3D12DescriptorHeap*>(m_srvPool);
if (m_srvPool == nullptr || m_srvHeap == nullptr) {
m_lastError = "Failed to create the D3D12 SRV descriptor heap.";
Shutdown();
return false;
}
m_srvDescriptorSize = m_srvHeap->GetDescriptorSize();
m_srvUsage.assign(kSrvDescriptorCount, false);
if (!RecreateBackBufferViews()) {
m_lastError = "Failed to create swap chain back buffer views.";
Shutdown();
@@ -137,14 +120,6 @@ void D3D12WindowRenderer::Shutdown() {
ReleaseFrameCompletionFence();
ReleaseBackBufferViews();
if (m_srvPool != nullptr) {
m_srvPool->Shutdown();
delete m_srvPool;
m_srvPool = nullptr;
}
m_srvHeap = nullptr;
m_srvUsage.clear();
if (m_swapChain != nullptr) {
m_swapChain->Shutdown();
delete m_swapChain;
@@ -175,7 +150,6 @@ void D3D12WindowRenderer::Shutdown() {
m_width = 0;
m_height = 0;
m_activeBackBufferIndex = 0u;
m_srvDescriptorSize = 0;
m_lastError.clear();
}
@@ -250,8 +224,8 @@ bool D3D12WindowRenderer::Resize(int width, int height) {
}
bool D3D12WindowRenderer::BeginFrame() {
if (m_swapChain == nullptr || m_srvHeap == nullptr) {
m_lastError = "BeginFrame requires a swap chain, command list, and SRV heap.";
if (m_swapChain == nullptr) {
m_lastError = "BeginFrame requires an initialized swap chain.";
return false;
}
@@ -355,10 +329,6 @@ ID3D12Device* D3D12WindowRenderer::GetDevice() const {
return device != nullptr ? device->GetDevice() : nullptr;
}
ID3D12DescriptorHeap* D3D12WindowRenderer::GetSrvHeap() const {
return m_srvHeap != nullptr ? m_srvHeap->GetDescriptorHeap() : nullptr;
}
ID3D12CommandQueue* D3D12WindowRenderer::GetCommandQueue() const {
const D3D12CommandQueue* queue = GetD3D12CommandQueue();
return queue != nullptr ? queue->GetCommandQueue() : nullptr;
@@ -368,68 +338,6 @@ const std::string& D3D12WindowRenderer::GetLastError() const {
return m_lastError;
}
void D3D12WindowRenderer::AllocateShaderResourceDescriptor(
D3D12_CPU_DESCRIPTOR_HANDLE* outCpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE* outGpuHandle) {
AllocateShaderResourceDescriptorInternal(outCpuHandle, outGpuHandle);
}
bool D3D12WindowRenderer::CreateShaderResourceTextureDescriptor(
RHIDevice* device,
::XCEngine::RHI::RHITexture* texture,
D3D12_CPU_DESCRIPTOR_HANDLE* outCpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE* outGpuHandle) {
if (device == nullptr ||
texture == nullptr ||
outCpuHandle == nullptr ||
outGpuHandle == nullptr) {
return false;
}
*outCpuHandle = {};
*outGpuHandle = {};
auto* nativeDevice = dynamic_cast<D3D12Device*>(device);
auto* nativeTexture = dynamic_cast<D3D12Texture*>(texture);
if (nativeDevice == nullptr ||
nativeTexture == nullptr ||
nativeTexture->GetResource() == nullptr) {
return false;
}
AllocateShaderResourceDescriptorInternal(outCpuHandle, outGpuHandle);
if (outCpuHandle->ptr == 0 || outGpuHandle->ptr == 0) {
*outCpuHandle = {};
*outGpuHandle = {};
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);
return true;
}
void D3D12WindowRenderer::FreeShaderResourceDescriptor(
D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle) {
FreeShaderResourceDescriptorInternal(cpuHandle, gpuHandle);
}
UINT D3D12WindowRenderer::GetSrvDescriptorSize() const {
return m_srvDescriptorSize;
}
UINT D3D12WindowRenderer::GetSrvDescriptorCount() const {
return kSrvDescriptorCount;
}
RHIDevice* D3D12WindowRenderer::GetRHIDevice() const {
return m_device;
}
@@ -504,55 +412,6 @@ D3D12SwapChain* D3D12WindowRenderer::GetD3D12SwapChain() const {
: nullptr;
}
void D3D12WindowRenderer::AllocateShaderResourceDescriptorInternal(
D3D12_CPU_DESCRIPTOR_HANDLE* outCpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE* outGpuHandle) {
if (outCpuHandle == nullptr || outGpuHandle == nullptr) {
return;
}
*outCpuHandle = {};
*outGpuHandle = {};
if (m_srvHeap == nullptr ||
m_srvDescriptorSize == 0 ||
m_srvUsage.empty()) {
return;
}
for (std::size_t index = 0; index < m_srvUsage.size(); ++index) {
if (m_srvUsage[index]) {
continue;
}
m_srvUsage[index] = true;
outCpuHandle->ptr =
m_srvHeap->GetCPUDescriptorHandleForHeapStart().ptr +
static_cast<SIZE_T>(index) * m_srvDescriptorSize;
outGpuHandle->ptr =
m_srvHeap->GetGPUDescriptorHandleForHeapStart().ptr +
static_cast<UINT64>(index) * m_srvDescriptorSize;
return;
}
}
void D3D12WindowRenderer::FreeShaderResourceDescriptorInternal(
D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle,
D3D12_GPU_DESCRIPTOR_HANDLE) {
if (m_srvHeap == nullptr ||
m_srvDescriptorSize == 0 ||
cpuHandle.ptr < m_srvHeap->GetCPUDescriptorHandleForHeapStart().ptr) {
return;
}
const SIZE_T offset =
cpuHandle.ptr - m_srvHeap->GetCPUDescriptorHandleForHeapStart().ptr;
const std::size_t index =
static_cast<std::size_t>(offset / m_srvDescriptorSize);
if (index < m_srvUsage.size()) {
m_srvUsage[index] = false;
}
}
bool D3D12WindowRenderer::InitializeFrameCompletionFence() {
ReleaseFrameCompletionFence();