From 251dddc17c7eacc6b9db815447944c5568505281 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 5 Dec 2020 17:01:33 +0100 Subject: [PATCH] Initial Texture3D get/set support for whole volume --- .../Buffers/Abstract/Texture3D{T}.cs | 149 +++++++++++++++++- .../Extensions/ID3D12DeviceExtensions.cs | 30 ++++ .../ID3D12GraphicsCommandListExtensions.cs | 4 +- .../Helpers/MemoryHelper.cs | 96 ++++++++++- 4 files changed, 274 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs index f7ed889c..36fe6f9b 100644 --- a/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp.Graphics/Buffers/Abstract/Texture3D{T}.cs @@ -1,11 +1,16 @@ -using System.Runtime.CompilerServices; +using System; +using System.Runtime.CompilerServices; using ComputeSharp.Core.Interop; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Buffers.Enums; +using ComputeSharp.Graphics.Buffers.Interop; +using ComputeSharp.Graphics.Commands; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; +using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; +using static TerraFX.Interop.D3D12_RESOURCE_STATES; using static TerraFX.Interop.D3D12_SRV_DIMENSION; using static TerraFX.Interop.D3D12_UAV_DIMENSION; @@ -101,6 +106,148 @@ namespace ComputeSharp.Graphics.Buffers.Abstract /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Reads the contents of the specified range from the current instance and writes them into a target . + /// + /// The input 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. + public void GetData(Span destination, int x, int y, int z, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + 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, 0, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 0, Depth, nameof(depth)); + Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); + Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + Guard.HasSizeGreaterThanOrEqualTo(destination, width * height * depth, nameof(destination)); + + 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 _, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.ReadBack, totalSizeInBytes); + + using (CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE)) + { + copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_SOURCE); + + copyCommandList.CopyTextureRegion( + d3D12Resource.Get(), + (uint)Unsafe.SizeOf(), + D3D12Resource, + DXGIFormatHelper.GetForType(), + (uint)x, + (uint)y, + (ushort)z, + (uint)width, + (uint)height, + (ushort)depth); + + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12ResourceState); + copyCommandList.ExecuteAndWaitForCompletion(); + } + + using ID3D12ResourceMap resource = d3D12Resource.Get()->Map(); + + MemoryHelper.Copy( + resource.Pointer, + width, + height, + depth, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height, + destination); + } + + /// + /// Writes the contents of a given to a specified area of the current instance. + /// + /// The input 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. + public void SetData(ReadOnlySpan source, int x, int y, int z, int width, int height, int depth) + { + GraphicsDevice.ThrowIfDisposed(); + + 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, 0, Width, nameof(width)); + Guard.IsBetweenOrEqualTo(height, 0, Height, nameof(height)); + Guard.IsBetweenOrEqualTo(depth, 0, Depth, nameof(depth)); + Guard.IsLessThanOrEqualTo(x + width, Width, nameof(x)); + Guard.IsLessThanOrEqualTo(y + height, Height, nameof(y)); + Guard.IsLessThanOrEqualTo(z + depth, Depth, nameof(z)); + Guard.HasSizeGreaterThanOrEqualTo(source, width * height * depth, nameof(source)); + + 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 _, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes); + + using ComPtr d3D12Resource = GraphicsDevice.D3D12Device->CreateCommittedResource(ResourceType.Upload, totalSizeInBytes); + + using (ID3D12ResourceMap resource = d3D12Resource.Get()->Map()) + { + MemoryHelper.Copy( + source, + resource.Pointer, + width, + height, + depth, + rowSizeInBytes, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch, + d3D12PlacedSubresourceFootprint.Footprint.RowPitch * (uint)height); + } + + using CommandList copyCommandList = new(GraphicsDevice, D3D12_COMMAND_LIST_TYPE_COMPUTE); + + copyCommandList.ResourceBarrier(D3D12Resource, D3D12ResourceState, D3D12_RESOURCE_STATE_COPY_DEST); + + copyCommandList.CopyTextureRegion( + D3D12Resource, + DXGIFormatHelper.GetForType(), + (uint)x, + (uint)y, + (ushort)z, + (uint)width, + (uint)height, + (ushort)depth, + d3D12Resource.Get(), + (uint)Unsafe.SizeOf()); + + copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState); + copyCommandList.ExecuteAndWaitForCompletion(); + } + /// protected override void OnDispose() { diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs index d0efc1d7..0112a51f 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12DeviceExtensions.cs @@ -402,6 +402,36 @@ namespace ComputeSharp.Graphics.Extensions return d3D12GraphicsCommandList.Move(); } + /// + /// Gets the layout data for a target resource. + /// + /// The target to use to get the layout info. + /// The value for the target resource. + /// The resulting layout info for the resource. + /// The number of rows in the resource. + /// The size in bytes of each row in the resource. + /// The total number of bytes for the resource. + public static void GetCopyableFootprint( + this ref ID3D12Device d3D12Device, + D3D12_RESOURCE_DESC* d3D12ResourceDesc, + out D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint, + out uint numRows, + out ulong rowSizeInBytes, + out ulong totalSizeInBytes) + { + uint a; + ulong b, c; + + fixed (D3D12_PLACED_SUBRESOURCE_FOOTPRINT* p = &d3D12PlacedSubresourceFootprint) + { + d3D12Device.GetCopyableFootprints(d3D12ResourceDesc, 0, 1, 0, p, &a, &b, &c); + } + + numRows = a; + rowSizeInBytes = b; + totalSizeInBytes = c; + } + /// /// Checks the feature support of a given type for a given device. /// diff --git a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs index 1c52d59e..5b0e14af 100644 --- a/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp.Graphics/Extensions/ID3D12GraphicsCommandListExtensions.cs @@ -41,7 +41,7 @@ namespace ComputeSharp.Graphics.Extensions width, height, depth, - (elementSizeInBytes * width * depth + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; d3D12PlacedSubresourceFootprint.Offset = 0; d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; @@ -84,7 +84,7 @@ namespace ComputeSharp.Graphics.Extensions width, height, depth, - (elementSizeInBytes * width * depth + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); + (elementSizeInBytes * width + FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1) & ~((uint)FX.D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1)); D3D12_PLACED_SUBRESOURCE_FOOTPRINT d3D12PlacedSubresourceFootprint; d3D12PlacedSubresourceFootprint.Offset = 0; d3D12PlacedSubresourceFootprint.Footprint = d3D12SubresourceFootprint; diff --git a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs index 9115977c..a9a4c4fb 100644 --- a/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/MemoryHelper.cs @@ -42,8 +42,8 @@ namespace ComputeSharp.Graphics.Helpers /// The type of values to read. /// The destination to read from. /// The pointer that indicates the 2D memory area to write to. - /// The width of the 2D memory area to read. - /// The height of the 2D memory area to read. + /// The width of the 2D memory area to write to. + /// The height of the 2D memory area to write to. /// The width of the memory area in bytes. /// The pitch (padded width) of the memory area in bytes. [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -107,5 +107,97 @@ namespace ComputeSharp.Graphics.Helpers } } } + + /// + /// Copies the content of a to the 3D area pointed by an input pointer. + /// + /// The type of values to read. + /// The destination to read from. + /// The pointer that indicates the 3D memory area to write to. + /// The width of the 3D memory area to write to. + /// The height of the 3D memory area to write to. + /// The depth of the 3D memory area to write to. + /// The width of the memory area in bytes. + /// The pitch (padded width) of the memory area in bytes. + /// The size in bytes of each 2D slice within the target 3D memory area. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy( + ReadOnlySpan source, + void* destination, + int width, + int height, + int depth, + ulong widthInBytes, + ulong pitchInBytes, + ulong sliceInBytes) + where T : unmanaged + { + if (widthInBytes == pitchInBytes) + { + source.CopyTo(new Span(destination, width * height * depth)); + } + else + { + for (int z = 0; z < depth; z++) + { + int sourceZOffset = width * height * z; + ulong destinationZOffset = sliceInBytes * (uint)z; + + for (int i = 0; i < height; i++) + { + ReadOnlySpan sourceRow = source.Slice(sourceZOffset + i * width, width); + Span destinationRow = new((byte*)destination + destinationZOffset + (uint)i * pitchInBytes, width); + + sourceRow.CopyTo(destinationRow); + } + } + } + } + + /// + /// Copies a 3D memory area pointed by a pointer value to a target . + /// + /// The type of values to read. + /// The pointer that indicates the memory area to read from. + /// The width of the 3D memory area to read. + /// The height of the 3D memory area to read. + /// The depth of the 3D memory area to read. + /// The width of the memory area in bytes. + /// The pitch (padded width) of the memory area in bytes. + /// The size in bytes of each 2D slice within the source 3D memory area. + /// The destination to write. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static unsafe void Copy( + void* source, + int width, + int height, + int depth, + ulong widthInBytes, + ulong pitchInBytes, + ulong sliceInBytes, + Span destination) + where T : unmanaged + { + if (widthInBytes == pitchInBytes) + { + new Span(source, width * height * depth).CopyTo(destination); + } + else + { + for (int z = 0; z < depth; z++) + { + ulong sourceZOffset = sliceInBytes * (uint)z; + int destinationZOffset = width * height * z; + + for (int i = 0; i < height; i++) + { + ReadOnlySpan sourceRow = new((byte*)source + sourceZOffset + (uint)i * pitchInBytes, width); + Span destinationRow = destination.Slice(destinationZOffset + i * width, width); + + sourceRow.CopyTo(destinationRow); + } + } + } + } } }