diff --git a/src/ComputeSharp.Core/Extensions/ComPtrExtensions.cs b/src/ComputeSharp.Core/Extensions/ComPtrExtensions.cs index 9c781ace..c426d3ed 100644 --- a/src/ComputeSharp.Core/Extensions/ComPtrExtensions.cs +++ b/src/ComputeSharp.Core/Extensions/ComPtrExtensions.cs @@ -69,15 +69,11 @@ namespace ComputeSharp /// [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe ComPtr Upcast(this in ComPtr ptr) + public static unsafe ref readonly ComPtr Upcast(this in ComPtr ptr) where T : unmanaged where U : unmanaged { - ComPtr copy = default; - - *(T**)Unsafe.AsPointer(ref copy) = ptr.Get(); - - return copy; + return ref Unsafe.As, ComPtr>(ref Unsafe.AsRef(in ptr)); } } } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList2.cs b/src/ComputeSharp.Graphics/Commands/CommandList2.cs index 95a5409b..c60432f2 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList2.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList2.cs @@ -1,4 +1,6 @@ using System; +using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; using ComputeSharp.Graphics.Buffers.Abstract; using ComputeSharp.Graphics.Extensions; using TerraFX.Interop; @@ -9,7 +11,7 @@ namespace ComputeSharp.Graphics.Commands /// /// A command list to set and execute operaations on the GPU. /// - internal readonly unsafe ref struct CommandList2 + internal unsafe ref struct CommandList2 { /// /// The instance associated with the current command list. @@ -24,12 +26,12 @@ namespace ComputeSharp.Graphics.Commands /// /// The object in use by the current instance. /// - private readonly ComPtr d3D12CommandAllocator; + private ComPtr d3D12CommandAllocator; /// /// The object in use by the current instance. /// - private readonly ComPtr d3D12GraphicsCommandList; + private ComPtr d3D12GraphicsCommandList; /// /// Creates a new instance with the specified parameters. @@ -50,6 +52,32 @@ namespace ComputeSharp.Graphics.Commands } } + /// + /// Gets the command list type being used by the current instance. + /// + public readonly D3D12_COMMAND_LIST_TYPE D3d12CommandListType => this.d3d12CommandListType; + + /// + /// Detaches the object in use by the current instance. + /// + /// The object in use, with ownership. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ComPtr DetachD3D12CommandAllocator() + { + return this.d3D12CommandAllocator.Move(); + } + + /// + /// Gets a pointer to the object in use by the current instance. + /// + /// A double pointer to the current object to execute. + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly ID3D12CommandList** GetD3D12CommandListAddressOf() + { + return this.d3D12GraphicsCommandList.Upcast().GetAddressOf(); + } + /// /// Copies a memory region from one resource to another. /// @@ -58,7 +86,7 @@ namespace ComputeSharp.Graphics.Commands /// The destination to write to. /// The starting offset to write the destination resource from. /// The total number of bytes to copy from one resource to another. - public void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, int sourceOffset, ID3D12Resource* d3d12ResourceDestination, int destinationOffset, int numBytes) + public readonly void CopyBufferRegion(ID3D12Resource* d3D12ResourceSource, int sourceOffset, ID3D12Resource* d3d12ResourceDestination, int destinationOffset, int numBytes) { this.d3D12GraphicsCommandList.Get()->CopyBufferRegion(d3d12ResourceDestination, (uint)destinationOffset, d3D12ResourceSource, (uint)sourceOffset, (uint)numBytes); } @@ -68,16 +96,16 @@ namespace ComputeSharp.Graphics.Commands /// /// The root parameter index to bind to the input resource. /// The input instance to bind. - public void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + public readonly void SetComputeRootDescriptorTable(int rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { this.d3D12GraphicsCommandList.Get()->SetComputeRootDescriptorTable((uint)rootParameterIndex, d3D12GpuDescriptorHandle); } /// - /// Sets a given object ready to be executed. + /// Sets a given object ready to be executed. /// - /// The input to setup. - public void SetPipelineData(PipelineData pipelineData) + /// The input to setup. + public readonly void SetPipelineData(PipelineData pipelineData) { this.d3D12GraphicsCommandList.Get()->SetComputeRootSignature(pipelineData.D3D12RootSignature); this.d3D12GraphicsCommandList.Get()->SetPipelineState(pipelineData.D3D12PipelineState); @@ -89,7 +117,7 @@ namespace ComputeSharp.Graphics.Commands /// The number of thread groups to schedule for the X axis. /// The number of thread groups to schedule for the Y axis. /// The number of thread groups to schedule for the Z axis. - public void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ) + public readonly void Dispatch(int threadGroupCountX, int threadGroupCountY, int threadGroupCountZ) { this.d3D12GraphicsCommandList.Get()->Dispatch((uint)threadGroupCountX, (uint)threadGroupCountY, (uint)threadGroupCountZ); } @@ -101,14 +129,13 @@ namespace ComputeSharp.Graphics.Commands { this.d3D12GraphicsCommandList.Get()->Close(); - // TODO: GraphicsDevice.ExecuteCommandList(this); + this.device.ExecuteCommandList(ref this); } /// public void Dispose() { - this.device.EnqueueCommandAllocator(this.d3D12CommandAllocator.Move(), this.d3d12CommandListType); - + this.d3D12CommandAllocator.Dispose(); this.d3D12GraphicsCommandList.Dispose(); } } diff --git a/src/ComputeSharp.Graphics/GraphicsDevice2.cs b/src/ComputeSharp.Graphics/GraphicsDevice2.cs index c18713ed..6e2e6a18 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice2.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice2.cs @@ -1,6 +1,8 @@ using ComputeSharp.Core.Interop; using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; +using ComputeSharp.Graphics.Helpers; +using System.Runtime.CompilerServices; using TerraFX.Interop; using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; using static TerraFX.Interop.D3D12_FEATURE; @@ -121,22 +123,6 @@ namespace ComputeSharp.Graphics this.shaderResourceViewDescriptorAllocator.Allocate(out d3d12CpuDescriptorHandle, out d3d12GpuDescriptorHandle); } - /// - /// The instance to return to the pool. - /// The type of command allocator to return to the pool. - internal void EnqueueCommandAllocator(ComPtr d3D12CommandAllocator, D3D12_COMMAND_LIST_TYPE d3d12CommandListType) - { - switch (d3d12CommandListType) - { - case D3D12_COMMAND_LIST_TYPE_COMPUTE: - this.computeCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12ComputeFenceValue); - break; - case D3D12_COMMAND_LIST_TYPE_COPY: - this.copyCommandAllocatorPool.Enqueue(d3D12CommandAllocator, this.nextD3D12CopyFenceValue); - break; - }; - } - /// /// The type of command allocator to rent. internal ComPtr GetCommandAllocator(D3D12_COMMAND_LIST_TYPE d3d12CommandListType) @@ -160,6 +146,54 @@ namespace ComputeSharp.Graphics d3D12GraphicsCommandList->SetDescriptorHeaps(1, &d3D12DescriptorHeap); } + /// + /// Executes a given command list and waits for the operation to be completed. + /// + /// The input to execute. + internal void ExecuteCommandList(ref CommandList2 commandList) + { + ref readonly CommandAllocatorPool2 commandAllocatorPool = ref Unsafe.NullRef(); + ID3D12CommandQueue* d3D12CommandQueue; + ID3D12Fence* d3D12Fence; + ulong d3D12FenceValue; + + // Get the target command queue, fence and pool for the list type + switch (commandList.D3d12CommandListType) + { + case D3D12_COMMAND_LIST_TYPE_COMPUTE: + commandAllocatorPool = ref this.computeCommandAllocatorPool; + d3D12CommandQueue = this.d3D12ComputeCommandQueue; + d3D12Fence = this.d3D12ComputeFence; + d3D12FenceValue = this.nextD3D12ComputeFenceValue++; + break; + case D3D12_COMMAND_LIST_TYPE_COPY: + commandAllocatorPool = ref this.copyCommandAllocatorPool; + d3D12CommandQueue = this.d3D12CopyCommandQueue; + d3D12Fence = this.d3D12CopyFence; + d3D12FenceValue = this.nextD3D12CopyFenceValue++; + break; + default: throw null!; + } + + // Enqueue the command allocator pool so that it can be reused later + commandAllocatorPool.Enqueue(commandList.DetachD3D12CommandAllocator(), d3D12FenceValue); + + // Execute the command list and signal to the target fence + d3D12CommandQueue->ExecuteCommandLists(1, commandList.GetD3D12CommandListAddressOf()); + + int result = d3D12CommandQueue->Signal(d3D12Fence, d3D12FenceValue); + + ThrowHelper.ThrowIfFailed(result); + + // If the fence value hasn't been reached, wait until the operation completes + if (d3D12FenceValue > d3D12Fence->GetCompletedValue()) + { + result = d3D12Fence->SetEventOnCompletion(d3D12FenceValue, default); + + ThrowHelper.ThrowIfFailed(result); + } + } + /// protected override void Dispose(bool disposing) {