Initial Texture3D<T> get/set support for whole volume

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-05 17:01:33 +01:00
1 親 eef8e41899
コミット 251dddc17c
4ファイル変更
+274 -5

検索条件に一致するファイルはありません

+148 -1
ファイルの表示
@@ -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
/// </summary>
internal ID3D12Resource* D3D12Resource => this.d3D12Resource;
/// <summary>
/// Reads the contents of the specified range from the current <see cref="Texture3D{T}"/> instance and writes them into a target <see cref="Span{T}"/>.
/// </summary>
/// <param name="destination">The input <see cref="Span{T}"/> to write data to.</param>
/// <param name="x">The horizontal offset in the source texture.</param>
/// <param name="y">The vertical offset in the source texture.</param>
/// <param name="z">The depthwise offset in the source texture.</param>
/// <param name="width">The width of the memory area to copy.</param>
/// <param name="height">The height of the memory area to copy.</param>
/// <param name="depth">The depth of the memory area to copy.</param>
public void GetData(Span<T> 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<T>(), (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<ID3D12Resource> 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<T>(),
D3D12Resource,
DXGIFormatHelper.GetForType<T>(),
(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);
}
/// <summary>
/// Writes the contents of a given <see cref="ReadOnlySpan{T}"/> to a specified area of the current <see cref="Texture2D{T}"/> instance.
/// </summary>
/// <param name="source">The input <see cref="ReadOnlySpan{T}"/> to read data from.</param>
/// <param name="x">The horizontal offset in the destination texture.</param>
/// <param name="y">The vertical offset in the destination texture.</param>
/// <param name="z">The depthwise offseet in the destination texture.</param>
/// <param name="width">The width of the memory area to write to.</param>
/// <param name="height">The height of the memory area to write to.</param>
/// <param name="depth">The depth of the memory area to write to.</param>
public void SetData(ReadOnlySpan<T> 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<T>(), (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<ID3D12Resource> 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<T>(),
(uint)x,
(uint)y,
(ushort)z,
(uint)width,
(uint)height,
(ushort)depth,
d3D12Resource.Get(),
(uint)Unsafe.SizeOf<T>());
copyCommandList.ResourceBarrier(D3D12Resource, D3D12_RESOURCE_STATE_COPY_DEST, D3D12ResourceState);
copyCommandList.ExecuteAndWaitForCompletion();
}
/// <inheritdoc/>
protected override void OnDispose()
{
+30
ファイルの表示
@@ -402,6 +402,36 @@ namespace ComputeSharp.Graphics.Extensions
return d3D12GraphicsCommandList.Move();
}
/// <summary>
/// Gets the layout data for a target resource.
/// </summary>
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> to use to get the layout info.</param>
/// <param name="d3D12ResourceDesc">The <see cref="D3D12_RESOURCE_DESC"/> value for the target resource.</param>
/// <param name="d3D12PlacedSubresourceFootprint">The resulting layout info for the resource.</param>
/// <param name="numRows">The number of rows in the resource.</param>
/// <param name="rowSizeInBytes">The size in bytes of each row in the resource.</param>
/// <param name="totalSizeInBytes">The total number of bytes for the resource.</param>
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;
}
/// <summary>
/// Checks the feature support of a given type for a given device.
/// </summary>
+2 -2
ファイルの表示
@@ -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;
+94 -2
ファイルの表示
@@ -42,8 +42,8 @@ namespace ComputeSharp.Graphics.Helpers
/// <typeparam name="T">The type of values to read.</typeparam>
/// <param name="source">The destination <see cref="Span{T}"/> to read from.</param>
/// <param name="destination">The pointer that indicates the 2D memory area to write to.</param>
/// <param name="width">The width of the 2D memory area to read.</param>
/// <param name="height">The height of the 2D memory area to read.</param>
/// <param name="width">The width of the 2D memory area to write to.</param>
/// <param name="height">The height of the 2D memory area to write to.</param>
/// <param name="widthInBytes">The width of the memory area in bytes.</param>
/// <param name="pitchInBytes">The pitch (padded width) of the memory area in bytes.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -107,5 +107,97 @@ namespace ComputeSharp.Graphics.Helpers
}
}
}
/// <summary>
/// Copies the content of a <see cref="ReadOnlySpan{T}"/> to the 3D area pointed by an input pointer.
/// </summary>
/// <typeparam name="T">The type of values to read.</typeparam>
/// <param name="source">The destination <see cref="Span{T}"/> to read from.</param>
/// <param name="destination">The pointer that indicates the 3D memory area to write to.</param>
/// <param name="width">The width of the 3D memory area to write to.</param>
/// <param name="height">The height of the 3D memory area to write to.</param>
/// <param name="depth">The depth of the 3D memory area to write to.</param>
/// <param name="widthInBytes">The width of the memory area in bytes.</param>
/// <param name="pitchInBytes">The pitch (padded width) of the memory area in bytes.</param>
/// <param name="sliceInBytes">The size in bytes of each 2D slice within the target 3D memory area.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(
ReadOnlySpan<T> 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<T>(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<T> sourceRow = source.Slice(sourceZOffset + i * width, width);
Span<T> destinationRow = new((byte*)destination + destinationZOffset + (uint)i * pitchInBytes, width);
sourceRow.CopyTo(destinationRow);
}
}
}
}
/// <summary>
/// Copies a 3D memory area pointed by a pointer value to a target <see cref="Span{T}"/>.
/// </summary>
/// <typeparam name="T">The type of values to read.</typeparam>
/// <param name="source">The pointer that indicates the memory area to read from.</param>
/// <param name="width">The width of the 3D memory area to read.</param>
/// <param name="height">The height of the 3D memory area to read.</param>
/// <param name="depth">The depth of the 3D memory area to read.</param>
/// <param name="widthInBytes">The width of the memory area in bytes.</param>
/// <param name="pitchInBytes">The pitch (padded width) of the memory area in bytes.</param>
/// <param name="sliceInBytes">The size in bytes of each 2D slice within the source 3D memory area.</param>
/// <param name="destination">The destination <see cref="Span{T}"/> to write.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy<T>(
void* source,
int width,
int height,
int depth,
ulong widthInBytes,
ulong pitchInBytes,
ulong sliceInBytes,
Span<T> destination)
where T : unmanaged
{
if (widthInBytes == pitchInBytes)
{
new Span<T>(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<T> sourceRow = new((byte*)source + sourceZOffset + (uint)i * pitchInBytes, width);
Span<T> destinationRow = destination.Slice(destinationZOffset + i * width, width);
sourceRow.CopyTo(destinationRow);
}
}
}
}
}
}