From e36afbfca6b95ce735e2f27366ea5e4099f1065b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 22 Feb 2021 15:55:01 +0100 Subject: [PATCH] Implemented swap chain animation --- .../ComputeSharp.Sample.SwapChain/Program.cs | 189 +++++++++++------- 1 file changed, 117 insertions(+), 72 deletions(-) diff --git a/samples/ComputeSharp.Sample.SwapChain/Program.cs b/samples/ComputeSharp.Sample.SwapChain/Program.cs index 853bb58d..04f4b087 100644 --- a/samples/ComputeSharp.Sample.SwapChain/Program.cs +++ b/samples/ComputeSharp.Sample.SwapChain/Program.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using ComputeSharp.Interop; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; @@ -34,7 +35,17 @@ namespace ComputeSharp.Sample.SwapChain /// /// The next fence value for graphics operations using . /// - //private ulong nextD3D12FenceValue = 1; + private ulong nextD3D12FenceValue = 1; + + /// + /// The object to create command lists. + /// + private ComPtr d3D12CommandAllocator; + + /// + /// The instance used to copy data to the back buffers. + /// + private ComPtr d3D12GraphicsCommandList; /// /// The instance used to display content onto the target window. @@ -56,6 +67,11 @@ namespace ComputeSharp.Sample.SwapChain /// private uint currentBufferIndex; + /// + /// The instance used to prepare frames to display. + /// + private ReadWriteTexture2D texture = null!; + public override string Title => "Fractal tiles"; public override unsafe void OnInitialize(Size size, HWND hwnd) @@ -127,90 +143,119 @@ namespace ComputeSharp.Sample.SwapChain _ = dxgiSwapChain1.Get()->GetBuffer(1, FX.__uuidof(), (void**)d3D12Resource1); } - // Get the index of the current buffer - using ComPtr dxgiSwapChain3 = default; + // Get the index of the initial back buffer + using (ComPtr dxgiSwapChain3 = default) + { + _ = this.dxgiSwapChain1.CopyTo(dxgiSwapChain3.GetAddressOf()); - _ = this.dxgiSwapChain1.CopyTo(dxgiSwapChain3.GetAddressOf()); + this.currentBufferIndex = dxgiSwapChain3.Get()->GetCurrentBackBufferIndex(); + } - this.currentBufferIndex = dxgiSwapChain3.Get()->GetCurrentBackBufferIndex(); + D3D12_RESOURCE_DESC d3D12Resource0Description = this.d3D12Resource0.Get()->GetDesc(); - // ============== - // SAMPLE - // ============== + // Create the command allocator to use + fixed (ID3D12CommandAllocator** d3D12CommandAllocator = this.d3D12CommandAllocator) + { + this.d3D12Device.Get()->CreateCommandAllocator( + D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT, + FX.__uuidof(), + (void**)d3D12CommandAllocator); + } - var desc = this.d3D12Resource0.Get()->GetDesc(); + // Create the reusable command list to copy data to the back buffers + fixed (ID3D12GraphicsCommandList** d3D12GraphicsCommandList = this.d3D12GraphicsCommandList) + { + this.d3D12Device.Get()->CreateCommandList( + 0, + D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT, + d3D12CommandAllocator, + null, + FX.__uuidof(), + (void**)d3D12GraphicsCommandList); + } - int width = (int)desc.Width; - int height = (int)desc.Height; + // Close the command list to prepare it for future use + this.d3D12GraphicsCommandList.Get()->Close(); - using ReadWriteTexture2D texture = Gpu.Default.AllocateReadWriteTexture2D(width, height); - - Gpu.Default.For(texture.Width, texture.Height, new FractalTiling(texture, 0)); - - ID3D12Resource* resource; - - _ = InteropServices.TryGetID3D12Resource(texture, FX.__uuidof(), (void**)&resource); - - using ComPtr d3D12CommandAllocator = default; - - this.d3D12Device.Get()->CreateCommandAllocator( - D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT, - FX.__uuidof(), - (void**)d3D12CommandAllocator.GetAddressOf()); - - using ComPtr d3D12GraphicsCommandList = default; - - this.d3D12Device.Get()->CreateCommandList( - 0, - D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT, - d3D12CommandAllocator, - null, - FX.__uuidof(), - (void**)d3D12GraphicsCommandList.GetAddressOf()); - - D3D12_RESOURCE_BARRIER d3D12ResourceBarrierBefore = D3D12_RESOURCE_BARRIER.InitTransition( - resource, - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS, - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE); - - d3D12GraphicsCommandList.Get()->ResourceBarrier(1, &d3D12ResourceBarrierBefore); - - D3D12_RESOURCE_BARRIER d3D12ResourceBarrierBefore0 = D3D12_RESOURCE_BARRIER.InitTransition( - this.d3D12Resource0.Get(), - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON, - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST); - - d3D12GraphicsCommandList.Get()->ResourceBarrier(1, &d3D12ResourceBarrierBefore0); - - d3D12GraphicsCommandList.Get()->CopyResource(this.d3D12Resource0.Get(), resource); - - D3D12_RESOURCE_BARRIER d3D12ResourceBarrierAfter = D3D12_RESOURCE_BARRIER.InitTransition( - resource, - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE, - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS); - - d3D12GraphicsCommandList.Get()->ResourceBarrier(1, &d3D12ResourceBarrierAfter); - - D3D12_RESOURCE_BARRIER d3D12ResourceBarrierAfter0 = D3D12_RESOURCE_BARRIER.InitTransition( - this.d3D12Resource0.Get(), - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST, - D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON); - - d3D12GraphicsCommandList.Get()->ResourceBarrier(1, &d3D12ResourceBarrierAfter0); - - d3D12GraphicsCommandList.Get()->Close(); - - this.d3D12CommandQueue.Get()->ExecuteCommandLists(1, (ID3D12CommandList**)d3D12GraphicsCommandList.GetAddressOf()); - - this.dxgiSwapChain1.Get()->Present(0, 0); + // Create the 2D texture to use to generate frames to display + this.texture = Gpu.Default.AllocateReadWriteTexture2D( + (int)d3D12Resource0Description.Width, + (int)d3D12Resource0Description.Height); } public override void OnResize(Size size) { } - public override void OnUpdate(TimeSpan time) + public override unsafe void OnUpdate(TimeSpan time) { + // Generate the new frame + Gpu.Default.For(texture.Width, texture.Height, new FractalTiling(texture, (float)time.TotalSeconds)); + + using ComPtr d3D12Resource = default; + + // Get the underlying ID3D12Resource pointer for the texture + _ = InteropServices.TryGetID3D12Resource(texture, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf()); + + // Get the target back buffer to update + ID3D12Resource* d3D12ResourceBackBuffer = this.currentBufferIndex switch + { + 0 => this.d3D12Resource0.Get(), + 1 => this.d3D12Resource1.Get(), + _ => null + }; + + this.currentBufferIndex ^= 1; + + // Reset the command list to reuse + this.d3D12GraphicsCommandList.Get()->Reset(this.d3D12CommandAllocator.Get(), null); + + D3D12_RESOURCE_BARRIER* d3D12ResourceBarriers = stackalloc D3D12_RESOURCE_BARRIER[] + { + D3D12_RESOURCE_BARRIER.InitTransition( + d3D12Resource.Get(), + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE), + D3D12_RESOURCE_BARRIER.InitTransition( + d3D12ResourceBackBuffer, + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON, + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST) + }; + + // Transition the resources to COPY_DEST and COPY_SOURCE respectively + d3D12GraphicsCommandList.Get()->ResourceBarrier(2, d3D12ResourceBarriers); + + // Copy the generated frame to the target back buffer + d3D12GraphicsCommandList.Get()->CopyResource(d3D12ResourceBackBuffer, d3D12Resource.Get()); + + d3D12ResourceBarriers[0] = D3D12_RESOURCE_BARRIER.InitTransition( + d3D12Resource.Get(), + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + + d3D12ResourceBarriers[1] = D3D12_RESOURCE_BARRIER.InitTransition( + d3D12ResourceBackBuffer, + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COMMON); + + // Transition the resources back to COMMON and UNORDERED_ACCESS respectively + d3D12GraphicsCommandList.Get()->ResourceBarrier(2, d3D12ResourceBarriers); + + d3D12GraphicsCommandList.Get()->Close(); + + // Execute the command list to perform the copy + this.d3D12CommandQueue.Get()->ExecuteCommandLists(1, (ID3D12CommandList**)d3D12GraphicsCommandList.GetAddressOf()); + this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue); + + if (this.nextD3D12FenceValue > this.d3D12Fence.Get()->GetCompletedValue()) + { + this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default); + } + + this.nextD3D12FenceValue++; + + // Present the new frame + this.dxgiSwapChain1.Get()->Present(0, 0); } public override void Dispose()