D3D12: Fix texture ownership semantics and remove GetSwapChain() exposure

This commit fixes the D3D12 texture architecture issues:

1. D3D12Texture ownership semantics:
   - Add m_ownsResource member to track resource ownership
   - InitializeFromExisting() now takes ownsResource parameter (default false)
   - Shutdown() only releases resource if ownsResource is true
   - Initialize() sets m_ownsResource = true for created resources

2. D3D12SwapChain changes:
   - Remove GetSwapChain() method (was exposing native D3D12 API)
   - Change GetBackBuffer() to return reference instead of pointer
   - Back buffers initialized with ownsResource = false

3. minimal/main.cpp updates:
   - Remove gColorRTs[2] array (was duplicating back buffer wrapping)
   - Direct use of gSwapChain.GetBackBuffer(i) instead
   - All references updated to use encapsulated API

4. Documentation:
   - Update TEST_SPEC.md v1.3
   - Remove known limitation 7.2 (minimal GetBuffer issue fixed)
   - Add D3D12_Texture_Architecture_Fix_Plan.md design document
This commit is contained in:
2026-03-20 17:58:27 +08:00
parent 0017388498
commit 34c04af6cb
7 changed files with 433 additions and 38 deletions

View File

@@ -44,7 +44,6 @@ D3D12CommandAllocator gCommandAllocator;
D3D12CommandList gCommandList;
// Render targets
D3D12Texture gColorRTs[2];
D3D12Texture gDepthStencil;
D3D12DescriptorHeap gRTVHeap;
D3D12DescriptorHeap gDSVHeap;
@@ -124,13 +123,11 @@ bool InitD3D12() {
// Create RTVs for back buffers using encapsulated interface
for (int i = 0; i < 2; i++) {
ID3D12Resource* buffer = nullptr;
gSwapChain.GetSwapChain()->GetBuffer(i, IID_PPV_ARGS(&buffer));
gColorRTs[i].InitializeFromExisting(buffer);
D3D12Texture& backBuffer = gSwapChain.GetBackBuffer(i);
CPUDescriptorHandle rtvCpuHandle = gRTVHeap.GetCPUDescriptorHandle(i);
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = { rtvCpuHandle.ptr };
gRTVs[i].InitializeAt(device, gColorRTs[i].GetResource(), rtvHandle, nullptr);
gRTVs[i].InitializeAt(device, backBuffer.GetResource(), rtvHandle, nullptr);
}
// Create DSV
@@ -164,7 +161,8 @@ void BeginRender() {
gCurrentRTIndex = gSwapChain.GetCurrentBackBufferIndex();
// Transition render target
gCommandList.TransitionBarrier(gColorRTs[gCurrentRTIndex].GetResource(),
D3D12Texture& currentBackBuffer = gSwapChain.GetBackBuffer(gCurrentRTIndex);
gCommandList.TransitionBarrier(currentBackBuffer.GetResource(),
ResourceStates::Present, ResourceStates::RenderTarget);
// Set render targets using encapsulated interface
@@ -189,7 +187,8 @@ void BeginRender() {
// End rendering
void EndRender() {
gCommandList.TransitionBarrier(gColorRTs[gCurrentRTIndex].GetResource(),
D3D12Texture& currentBackBuffer = gSwapChain.GetBackBuffer(gCurrentRTIndex);
gCommandList.TransitionBarrier(currentBackBuffer.GetResource(),
ResourceStates::RenderTarget, ResourceStates::Present);
}
@@ -275,7 +274,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
bool screenshotResult = D3D12Screenshot::Capture(
gDevice,
gCommandQueue,
gColorRTs[gCurrentRTIndex],
gSwapChain.GetBackBuffer(gCurrentRTIndex),
"minimal.ppm"
);
if (screenshotResult) {