From 03bbbe7724ad2f3c16cb641dd631570fb17094db Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 7 Dec 2020 03:12:28 +0100 Subject: [PATCH] Enabled copy command list for readonly textures --- .../Buffers/Abstract/Texture2D{T}.cs | 39 +++++++++++++++---- .../Buffers/Abstract/Texture3D{T}.cs | 39 +++++++++++++++---- .../Commands/CommandList.cs | 2 +- .../Extensions/ID3D12DeviceExtensions.cs | 4 +- src/ComputeSharp.Graphics/GraphicsDevice.cs | 2 +- 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs index a3c9cee6..dfd28d3f 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture2D{T}.cs @@ -36,7 +36,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The default value for the current resource. /// - private readonly D3D12_RESOURCE_STATES D3D12ResourceState; + private readonly D3D12_RESOURCE_STATES d3D12ResourceState; + + /// + /// The value to use for copy operations. + /// + private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType; /// /// Creates a new instance with the specified parameters. @@ -61,7 +66,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract DXGIFormatHelper.GetForType(), (uint)width, (uint)height, - out D3D12ResourceState); + out this.d3D12ResourceState); + + this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON + ? D3D12_COMMAND_LIST_TYPE_COPY + : D3D12_COMMAND_LIST_TYPE_COMPUTE; device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); @@ -130,9 +139,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); - using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) + using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + } copyCommandList.CopyTextureRegion( d3D12Resource.Get(), @@ -145,7 +157,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract (uint)height, 1); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } @@ -207,9 +223,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12PlacedSubresourceFootprint.Footprint.RowPitch); } - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } copyCommandList.CopyTextureRegion( D3D12Resource, @@ -219,7 +238,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12Resource.Get(), &d3D12PlacedSubresourceFootprint); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index adcb4106..837d6f55 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -36,7 +36,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// /// The default value for the current resource. /// - private readonly D3D12_RESOURCE_STATES D3D12ResourceState; + private readonly D3D12_RESOURCE_STATES d3D12ResourceState; + + /// + /// The value to use for copy operations. + /// + private readonly D3D12_COMMAND_LIST_TYPE d3D12CommandListType; /// /// Creates a new instance with the specified parameters. @@ -65,7 +70,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract (uint)width, (uint)height, (ushort)depth, - out D3D12ResourceState); + out this.d3D12ResourceState); + + this.d3D12CommandListType = this.d3D12ResourceState == D3D12_RESOURCE_STATE_COMMON + ? D3D12_COMMAND_LIST_TYPE_COPY + : D3D12_COMMAND_LIST_TYPE_COMPUTE; device.AllocateShaderResourceViewDescriptorHandles(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); @@ -144,9 +153,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); - using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) + using (CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType)) { - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + } copyCommandList.CopyTextureRegion( d3D12Resource.Get(), @@ -159,7 +171,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract (uint)height, (ushort)depth); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } @@ -230,9 +246,12 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); } - using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } copyCommandList.CopyTextureRegion( D3D12Resource, @@ -242,7 +261,11 @@ namespace ComputeSharp.Graphics.Buffers.Abstract d3D12Resource.Get(), &d3D12PlacedSubresourceFootprint); - copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + copyCommandList.ExecuteAndWaitForCompletion(); } diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 3be6492e..13bc89bc 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -55,7 +55,7 @@ namespace ComputeSharp.Graphics.Commands /// /// Gets the command list type being used by the current instance. /// - public readonly D3D12_COMMAND_LIST_TYPE D3d12CommandListType => this.d3D12CommandListType; + public readonly D3D12_COMMAND_LIST_TYPE D3D12CommandListType => this.d3D12CommandListType; /// /// Detaches the object in use by the current instance. diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index c22ef9ac..3576f368 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -163,7 +163,7 @@ namespace ComputeSharp.Graphics.Extensions (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch { - ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE), + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; @@ -214,7 +214,7 @@ namespace ComputeSharp.Graphics.Extensions (d3D12ResourceFlags, d3D12ResourceStates) = resourceType switch { - ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE), + ResourceType.ReadOnly => (D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COMMON), ResourceType.ReadWrite => (D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS), _ => ThrowHelper.ThrowArgumentException<(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_STATES)>() }; diff --git a/src/ComputeSharp.Graphics/GraphicsDevice.cs b/src/ComputeSharp.Graphics/GraphicsDevice.cs index e49cee8f..cdfad124 100644 --- a/src/ComputeSharp.Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp.Graphics/GraphicsDevice.cs @@ -168,7 +168,7 @@ namespace ComputeSharp.Graphics ulong d3D12FenceValue; // Get the target command queue, fence and pool for the list type - switch (commandList.D3d12CommandListType) + switch (commandList.D3D12CommandListType) { case D3D12_COMMAND_LIST_TYPE_COMPUTE: commandAllocatorPool = ref this.computeCommandAllocatorPool;