From 631f20b31a6c863d3b322b7f23cb03cc5f8dbaf1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 4 Feb 2021 14:19:17 +0100 Subject: [PATCH] Added internal transfer Texture3D copy methods --- .../Resources/Abstract/Texture3D{T}.cs | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs index 2bccef01..4459aea0 100644 --- a/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Resources/Abstract/Texture3D{T}.cs @@ -210,6 +210,73 @@ namespace ComputeSharp.Resources } } + /// + /// Reads the contents of the specified range from the current instance and writes them into a instance. + /// + /// The target instance to write data to. + /// The size of the memory area to write data to. + /// The horizontal offset in the source texture. + /// The vertical offset in the source texture. + /// The depthwise offset in the source texture. + /// The width of the memory area to copy. + /// The height of the memory area to copy. + /// The depth of the memory area to copy. + internal void CopyTo(ReadBackTexture3D destination, int size, int x, int y, int z, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + destination.ThrowIfDeviceMismatch(GraphicsDevice); + destination.ThrowIfDisposed(); + + Guard.IsInRange(x, 0, Width, nameof(x)); + Guard.IsInRange(y, 0, Height, nameof(y)); + Guard.IsInRange(z, 0, Depth, nameof(z)); + Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(width, 1, destination.Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, destination.Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, destination.Depth, nameof(depth)); + Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); + Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + &d3D12ResourceDescription, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + } + + copyCommandList.CopyTextureRegion( + destination.D3D12Resource, + &d3D12PlacedSubresourceFootprint, + D3D12Resource, + (uint)x, + (uint)y, + (ushort)z, + (uint)width, + (uint)height, + (ushort)depth); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, this.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// /// Writes the contents of a given memory area to a specified area of the current instance. /// @@ -284,6 +351,70 @@ namespace ComputeSharp.Resources copyCommandList.ExecuteAndWaitForCompletion(); } + /// + /// Writes the contents of a given instance to a specified area of the current instance. + /// + /// The input instance to read data from. + /// The size of the memory area to read data from. + /// The horizontal offset in the destination texture. + /// The vertical offset in the destination texture. + /// The depthwise offseet in the destination texture. + /// The width of the memory area to write to. + /// The height of the memory area to write to. + /// The depth of the memory area to write to. + internal void CopyFrom(ReadBackTexture3D source, int size, int x, int y, int z, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + ThrowIfDisposed(); + + source.ThrowIfDeviceMismatch(GraphicsDevice); + source.ThrowIfDisposed(); + + Guard.IsInRange(x, 0, Width, nameof(x)); + Guard.IsInRange(y, 0, Height, nameof(y)); + Guard.IsInRange(z, 0, Depth, nameof(z)); + Guard.IsBetweenOrEqualTo(width, 1, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, Depth, nameof(depth)); + Guard.IsBetweenOrEqualTo(width, 1, source.Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 1, source.Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 1, source.Depth, nameof(depth)); + Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); + Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + + D3D12_RESOURCE_DESC d3D12ResourceDescription = D3D12_RESOURCE_DESC.Tex3D(DXGIFormatHelper.GetForType(), (ulong)width, (uint)height, (ushort)depth); + + GraphicsDevice.D3D12Device->GetCopyableFootprint( + &d3D12ResourceDescription, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + using CommandList copyCommandList = new(GraphicsDevice, this.d3D12CommandListType); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, this.d3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + } + + copyCommandList.CopyTextureRegion( + D3D12Resource, + (uint)x, + (uint)y, + (ushort)z, + source.D3D12Resource, + &d3D12PlacedSubresourceFootprint); + + if (copyCommandList.D3D12CommandListType == D3D12_COMMAND_LIST_TYPE_COMPUTE) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, this.d3D12ResourceState); + } + + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// protected override bool OnDispose() {