diff --git a/src/ComputeSharp/Graphics/Commands/CommandList.cs b/src/ComputeSharp/Graphics/Commands/CommandList.cs index 993aad79..9d265909 100644 --- a/src/ComputeSharp/Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp/Graphics/Commands/CommandList.cs @@ -56,6 +56,28 @@ namespace ComputeSharp.Graphics.Commands } } + /// + /// Creates a new instance with the specified parameters. + /// + /// The target instance to use. + /// The instance to use for the new command list. + public CommandList(GraphicsDevice device, ID3D12PipelineState* d3D12PipelineState) + { + this.device = device; + this.d3D12CommandListType = D3D12_COMMAND_LIST_TYPE_COMPUTE; + + Unsafe.SkipInit(out this.d3D12GraphicsCommandList); + Unsafe.SkipInit(out this.d3D12CommandAllocator); + + device.GetCommandListAndAllocator( + d3D12PipelineState, + out *(ID3D12GraphicsCommandList**)Unsafe.AsPointer(ref this.d3D12GraphicsCommandList), + out *(ID3D12CommandAllocator**)Unsafe.AsPointer(ref this.d3D12CommandAllocator)); + + // Set the heap descriptor for the command list + device.SetDescriptorHeapForCommandList(this.d3D12GraphicsCommandList); + } + /// /// Gets the command list type being used by the current instance. /// diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs index 5c74e0df..565b3fc8 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12CommandListPool.cs @@ -36,16 +36,17 @@ namespace ComputeSharp.Graphics.Commands.Interop /// Rents a and pair. /// /// The renting the command list. + /// The instance to use for the new command list. /// The resulting value. /// The resulting value. - public void Rent(ID3D12Device* d3D12Device, out ID3D12GraphicsCommandList* d3D12CommandList, out ID3D12CommandAllocator* d3D12CommandAllocator) + public void Rent(ID3D12Device* d3D12Device, ID3D12PipelineState* d3D12PipelineState, out ID3D12GraphicsCommandList* d3D12CommandList, out ID3D12CommandAllocator* d3D12CommandAllocator) { lock (this.d3D12CommandListBundleQueue) { if (this.d3D12CommandListBundleQueue.TryDequeue(out D3D12CommandListBundle d3D12CommandListBundle)) { d3D12CommandListBundle.D3D12CommandAllocator->Reset().Assert(); - d3D12CommandListBundle.D3D12CommandList->Reset(d3D12CommandListBundle.D3D12CommandAllocator, null).Assert(); + d3D12CommandListBundle.D3D12CommandList->Reset(d3D12CommandListBundle.D3D12CommandAllocator, d3D12PipelineState).Assert(); d3D12CommandList = d3D12CommandListBundle.D3D12CommandList; d3D12CommandAllocator = d3D12CommandListBundle.D3D12CommandAllocator; @@ -53,7 +54,7 @@ namespace ComputeSharp.Graphics.Commands.Interop return; } - CreateCommandListAndAllocator(d3D12Device, out d3D12CommandList, out d3D12CommandAllocator); + CreateCommandListAndAllocator(d3D12Device, d3D12PipelineState, out d3D12CommandList, out d3D12CommandAllocator); } } @@ -89,13 +90,14 @@ namespace ComputeSharp.Graphics.Commands.Interop /// Creates a new and pair. /// /// The renting the command list. + /// The instance to use for the new command list. /// The resulting value. /// The resulting value. [MethodImpl(MethodImplOptions.NoInlining)] - private void CreateCommandListAndAllocator(ID3D12Device* d3D12Device, out ID3D12GraphicsCommandList* d3D12CommandList, out ID3D12CommandAllocator* d3D12CommandAllocator) + private void CreateCommandListAndAllocator(ID3D12Device* d3D12Device, ID3D12PipelineState* d3D12PipelineState, out ID3D12GraphicsCommandList* d3D12CommandList, out ID3D12CommandAllocator* d3D12CommandAllocator) { using ComPtr d3D12CommandAllocatorComPtr = d3D12Device->CreateCommandAllocator(this.d3D12CommandListType); - using ComPtr d3D12CommandListComPtr = d3D12Device->CreateCommandList(this.d3D12CommandListType, d3D12CommandAllocatorComPtr.Get()); + using ComPtr d3D12CommandListComPtr = d3D12Device->CreateCommandList(this.d3D12CommandListType, d3D12CommandAllocatorComPtr.Get(), d3D12PipelineState); fixed (ID3D12GraphicsCommandList** d3D12CommandListPtr = &d3D12CommandList) fixed (ID3D12CommandAllocator** d3D12CommandAllocatorPtr = &d3D12CommandAllocator) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 7d0f7dbf..9287ed3f 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -260,12 +260,14 @@ namespace ComputeSharp.Graphics.Extensions /// The target to use to create the command list. /// The type of command list to create. /// The command allocator to use to create the command list. + /// The initial object, if present. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command list fails. public static ComPtr CreateCommandList( this ref ID3D12Device d3D12Device, D3D12_COMMAND_LIST_TYPE d3D12CommandListType, - ID3D12CommandAllocator* d3D12CommandAllocator) + ID3D12CommandAllocator* d3D12CommandAllocator, + ID3D12PipelineState* d3D12PipelineState) { using ComPtr d3D12GraphicsCommandList = default; @@ -273,7 +275,7 @@ namespace ComputeSharp.Graphics.Extensions 0, d3D12CommandListType, d3D12CommandAllocator, - null, + d3D12PipelineState, FX.__uuidof(), d3D12GraphicsCommandList.GetVoidAddressOf()).Assert(); diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 4d6900ed..484dd902 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -252,6 +252,7 @@ namespace ComputeSharp } /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void RentShaderResourceViewDescriptorHandles( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) @@ -260,6 +261,7 @@ namespace ComputeSharp } /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void ReturnShaderResourceViewDescriptorHandles( D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) @@ -271,6 +273,7 @@ namespace ComputeSharp /// The type of command allocator to rent. /// The resulting value. /// The resulting value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void GetCommandListAndAllocator( D3D12_COMMAND_LIST_TYPE d3D12CommandListType, out ID3D12GraphicsCommandList* d3D12CommandList, @@ -279,10 +282,10 @@ namespace ComputeSharp switch (d3D12CommandListType) { case D3D12_COMMAND_LIST_TYPE_COMPUTE: - this.computeCommandListPool.Rent(this.d3D12Device.Get(), out d3D12CommandList, out d3D12CommandAllocator); + this.computeCommandListPool.Rent(this.d3D12Device.Get(), null, out d3D12CommandList, out d3D12CommandAllocator); break; case D3D12_COMMAND_LIST_TYPE_COPY: - this.copyCommandListPool.Rent(this.d3D12Device.Get(), out d3D12CommandList, out d3D12CommandAllocator); + this.copyCommandListPool.Rent(this.d3D12Device.Get(), null, out d3D12CommandList, out d3D12CommandAllocator); break; default: ThrowHelper.ThrowArgumentException>(); @@ -292,10 +295,24 @@ namespace ComputeSharp } } + /// + /// The instance to use for the new command list. + /// The resulting value. + /// The resulting value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal void GetCommandListAndAllocator( + ID3D12PipelineState* d3D12PipelineState, + out ID3D12GraphicsCommandList* d3D12CommandList, + out ID3D12CommandAllocator* d3D12CommandAllocator) + { + this.computeCommandListPool.Rent(this.d3D12Device.Get(), d3D12PipelineState, out d3D12CommandList, out d3D12CommandAllocator); + } + /// /// Sets the descriptor heap for a given instance. /// /// The input instance to use. + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void SetDescriptorHeapForCommandList(ID3D12GraphicsCommandList* d3D12GraphicsCommandList) { ID3D12DescriptorHeap* d3D12DescriptorHeap = this.shaderResourceViewDescriptorAllocator.D3D12DescriptorHeap; diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index f545ff33..4dc9f032 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -10,7 +10,6 @@ using ComputeSharp.__Internals; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; -using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; #pragma warning disable CS0618 @@ -148,10 +147,9 @@ namespace ComputeSharp.Shaders } } - using CommandList commandList = new(device, D3D12_COMMAND_LIST_TYPE_COMPUTE); + using CommandList commandList = new(device, pipelineData.D3D12PipelineState); commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); - commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineData.D3D12PipelineState); ComputeShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); @@ -172,7 +170,6 @@ namespace ComputeSharp.Shaders { device.ThrowIfDisposed(); - // Calculate the dispatch values int x = texture.Width, y = texture.Height, @@ -184,7 +181,6 @@ namespace ComputeSharp.Shaders Guard.IsBetweenOrEqualTo(groupsX, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); Guard.IsBetweenOrEqualTo(groupsY, 1, FX.D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION, nameof(groupsX)); - // Create the shader key ShaderKey key = new(shader.GetDispatchId(), threadsX, threadsY, 1); PipelineData? pipelineData; @@ -206,11 +202,9 @@ namespace ComputeSharp.Shaders } } - // Create the commands list and set the pipeline state - using CommandList commandList = new(device, D3D12_COMMAND_LIST_TYPE_COMPUTE); + using CommandList commandList = new(device, pipelineData.D3D12PipelineState); commandList.D3D12GraphicsCommandList->SetComputeRootSignature(pipelineData.D3D12RootSignature); - commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineData.D3D12PipelineState); PixelShaderDispatchDataLoader dataLoader = new(commandList.D3D12GraphicsCommandList); @@ -221,7 +215,6 @@ namespace ComputeSharp.Shaders 1, ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuDescriptorHandle(device)); - // Dispatch and wait for completion commandList.D3D12GraphicsCommandList->Dispatch((uint)groupsX, (uint)groupsY, 1); commandList.ExecuteAndWaitForCompletion(); }