From 8f594c8ebd11f0e5f8a0c6369c3fe7383d250cbe Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 1 Dec 2025 08:19:49 -0800 Subject: Enable debug layer; add missing transitionon on depth/stencil buffer --- hello/main.c | 115 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 65 insertions(+), 50 deletions(-) (limited to 'hello/main.c') diff --git a/hello/main.c b/hello/main.c index 1f0b4ed..309a8c9 100644 --- a/hello/main.c +++ b/hello/main.c @@ -40,6 +40,34 @@ typedef struct D3D { UINT cbv_descriptor_size; } D3D; +static D3D12_CPU_DESCRIPTOR_HANDLE d3d_get_current_back_buffer_view(const D3D* d3d) { + assert(d3d); + assert(d3d->pSwapChain); + assert(d3d->pRtvHeap); + assert(d3d->rtv_descriptor_size > 0); + D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle; + d3d->pRtvHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(d3d->pRtvHeap, &rtv_handle); + return CD3DX12_CPU_DESCRIPTOR_HANDLE( + rtv_handle, + d3d->pSwapChain->lpVtbl->GetCurrentBackBufferIndex(d3d->pSwapChain), + d3d->rtv_descriptor_size); +} + +static ID3D12Resource* d3d_get_current_back_buffer(const D3D* d3d) { + assert(d3d); + assert(d3d->pSwapChain); + assert(d3d->pSwapChainBuffer); + return d3d->pSwapChainBuffer[d3d->pSwapChain->lpVtbl->GetCurrentBackBufferIndex(d3d->pSwapChain)]; +} + +static D3D12_CPU_DESCRIPTOR_HANDLE d3d_get_depth_stencil_view(D3D* d3d) { + assert(d3d); + assert(d3d->pDsvHeap); + D3D12_CPU_DESCRIPTOR_HANDLE handle; + d3d->pDsvHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(d3d->pDsvHeap, &handle); + return handle; +} + /// Creates the application's swap chain. /// /// This method can be called multiple times to re-create the swap chain. @@ -100,14 +128,6 @@ static void d3d_create_swap_chain_buffer_render_target_views(D3D* d3d) { } } -static D3D12_CPU_DESCRIPTOR_HANDLE d3d_get_depth_stencil_view(D3D* d3d) { - assert(d3d); - assert(d3d->pDsvHeap); - D3D12_CPU_DESCRIPTOR_HANDLE handle; - d3d->pDsvHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(d3d->pDsvHeap, &handle); - return handle; -} - /// Creates a depth/stencil buffer and its view. static void d3d_create_depth_stencil_buffer_and_view(D3D* d3d) { assert(d3d); @@ -195,10 +215,10 @@ static void d3d_init(D3D* d3d, Window* pWindow, const D3DSettings* pSettings) { d3d->settings = *pSettings; UINT dxgiFactoryFlags = 0; -#ifdef DEBUG - ID3D12Debug* debug = 0; - D3D12GetDebugInterface(&IID_ID3D12Debug, (&debug)); - debug->EnableDebugLayer(); +#ifndef NDEBUG + ID3D12Debug* pDebug = NULL; + TrapIfFailed(D3D12GetDebugInterface(&IID_ID3D12Debug, (&pDebug))); + pDebug->lpVtbl->EnableDebugLayer(pDebug); dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG; #endif TrapIfFailed(CreateDXGIFactory2( @@ -249,7 +269,7 @@ static void d3d_init(D3D* d3d, Window* pWindow, const D3DSettings* pSettings) { /*nodeMask=*/0, queue_desc.Type, d3d->pCommandAllocator, - /*pInitialState=*/0, // Pipeline state. + /*pInitialState=*/NULL, // Pipeline state. &IID_ID3D12CommandList, &d3d->pCommandList)); @@ -272,7 +292,7 @@ static void d3d_init(D3D* d3d, Window* pWindow, const D3DSettings* pSettings) { &IID_ID3D12Fence, &d3d->pFence)); - if ((d3d->fence_event = CreateEvent(0, FALSE, FALSE, 0)) == 0) { + if ((d3d->fence_event = CreateEvent(NULL, FALSE, FALSE, NULL)) == NULL) { TrapIfFailed(HRESULT_FROM_WIN32(GetLastError())); } } @@ -290,26 +310,7 @@ void d3d_destroy(D3D* d3d) { SafeRelease(d3d->pFence); SafeRelease(d3d->pDevice); SafeRelease(d3d->pDxgiFactory); -} - -static D3D12_CPU_DESCRIPTOR_HANDLE d3d_get_current_back_buffer_view(const D3D* d3d) { - assert(d3d); - assert(d3d->pSwapChain); - assert(d3d->pRtvHeap); - assert(d3d->rtv_descriptor_size > 0); - D3D12_CPU_DESCRIPTOR_HANDLE rtv_handle; - d3d->pRtvHeap->lpVtbl->GetCPUDescriptorHandleForHeapStart(d3d->pRtvHeap, &rtv_handle); - return CD3DX12_CPU_DESCRIPTOR_HANDLE( - rtv_handle, - d3d->pSwapChain->lpVtbl->GetCurrentBackBufferIndex(d3d->pSwapChain), - d3d->rtv_descriptor_size); -} - -static ID3D12Resource* d3d_get_current_back_buffer(const D3D* d3d) { - assert(d3d); - assert(d3d->pSwapChain); - assert(d3d->pSwapChainBuffer); - return d3d->pSwapChainBuffer[d3d->pSwapChain->lpVtbl->GetCurrentBackBufferIndex(d3d->pSwapChain)]; + CloseHandle(d3d->fence_event); } static void d3d_populate_command_list(D3D* d3d) { @@ -339,14 +340,21 @@ static void d3d_populate_command_list(D3D* d3d) { TrapIfFailed(d3d->pCommandList->lpVtbl->Reset( d3d->pCommandList, d3d->pCommandAllocator, - /*pInitialState=*/0)); - - // Indicate that we intend to use the back buffer as a render target. - const D3D12_RESOURCE_BARRIER render_barrier = CD3DX12_RESOURCE_BARRIER_Transition( - d3d_get_current_back_buffer(d3d), - D3D12_RESOURCE_STATE_PRESENT, - D3D12_RESOURCE_STATE_RENDER_TARGET); - d3d->pCommandList->lpVtbl->ResourceBarrier(d3d->pCommandList, 1, &render_barrier); + /*pInitialState=*/NULL)); + + // Indicate that we intend to use the back buffer as a render target and + // depth/stencil for writing. + const D3D12_RESOURCE_BARRIER render_barriers[] = { + CD3DX12_RESOURCE_BARRIER_Transition( + d3d_get_current_back_buffer(d3d), + D3D12_RESOURCE_STATE_PRESENT, + D3D12_RESOURCE_STATE_RENDER_TARGET), + CD3DX12_RESOURCE_BARRIER_Transition( + d3d->pDepthStencilBuffer, + D3D12_RESOURCE_STATE_PRESENT, + D3D12_RESOURCE_STATE_DEPTH_WRITE) + }; + d3d->pCommandList->lpVtbl->ResourceBarrier(d3d->pCommandList, COUNTOF(render_barriers), render_barriers); // Record commands. const float clear_colour[] = { 0.0f, 0.502f, 0.494f, 0.0f }; @@ -366,12 +374,19 @@ static void d3d_populate_command_list(D3D* d3d) { 0, // Number of rectangles in the following array. 0); // No rectangles; clear the entire resource view. - // Indicate that we now intend to use the back buffer to present. - const D3D12_RESOURCE_BARRIER present_barrier = CD3DX12_RESOURCE_BARRIER_Transition( - d3d_get_current_back_buffer(d3d), - D3D12_RESOURCE_STATE_RENDER_TARGET, - D3D12_RESOURCE_STATE_PRESENT); - d3d->pCommandList->lpVtbl->ResourceBarrier(d3d->pCommandList, 1, &present_barrier); + // Indicate that we now intend to use the back buffer and depth/stencil + // buffer to present. + const D3D12_RESOURCE_BARRIER present_barriers[] = { + CD3DX12_RESOURCE_BARRIER_Transition( + d3d_get_current_back_buffer(d3d), + D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_PRESENT), + CD3DX12_RESOURCE_BARRIER_Transition( + d3d->pDepthStencilBuffer, + D3D12_RESOURCE_STATE_DEPTH_WRITE, + D3D12_RESOURCE_STATE_PRESENT) + }; + d3d->pCommandList->lpVtbl->ResourceBarrier(d3d->pCommandList, COUNTOF(present_barriers), present_barriers); // A command list must be closed before it can be executed. TrapIfFailed(d3d->pCommandList->lpVtbl->Close(d3d->pCommandList)); @@ -385,8 +400,8 @@ static void d3d_wait_for_previous_frame(D3D* d3d) { // Advance the fence value to mark commands up to this fence point. d3d->fence_value++; - // The command queue will signal the new fence value when all commands - // up to this point have finished execution. + // The command queue will signal the new fence value when all commands up to + // this point have finished execution. TrapIfFailed(d3d->pCommandQueue->lpVtbl->Signal( d3d->pCommandQueue, d3d->pFence, d3d->fence_value)); -- cgit v1.2.3