Fix ComputeContext.Clear API for ReadWriteBuffer<T>

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-29 17:07:30 +01:00
1 親 8c75a71580
コミット f7e486eae2
3ファイル変更
+96 -2

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

+31
ファイルの表示
@@ -458,6 +458,37 @@ internal static unsafe class ID3D12DeviceExtensions
d3D12Device.CreateUnorderedAccessView(d3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle);
}
/// <summary>
/// Creates a view for a buffer that will need to be cleared.
/// </summary>
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> instance in use.</param>
/// <param name="d3D12Resource">The <see cref="ID3D12Resource"/> to create a view for.</param>
/// <param name="dxgiFormat">The <see cref="DXGI_FORMAT"/> value to use.</param>
/// <param name="bufferSize">The size of the target resource.</param>
/// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
/// <param name="d3D12CpuDescriptorHandleNonShaderVisible">The non shader visible<see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> instance for the current resource.</param>
public static void CreateUnorderedAccessViewForClear(
this ref ID3D12Device d3D12Device,
ID3D12Resource* d3D12Resource,
DXGI_FORMAT dxgiFormat,
uint bufferSize,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible)
{
D3D12_UNORDERED_ACCESS_VIEW_DESC d3D12UnorderedAccessViewDescription = default;
d3D12UnorderedAccessViewDescription.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
d3D12UnorderedAccessViewDescription.Format = dxgiFormat;
d3D12UnorderedAccessViewDescription.Buffer.NumElements = bufferSize;
d3D12Device.CreateUnorderedAccessView(d3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandleNonShaderVisible);
d3D12Device.CopyDescriptorsSimple(
NumDescriptors: 1,
DestDescriptorRangeStart: d3D12CpuDescriptorHandle,
SrcDescriptorRangeStart: d3D12CpuDescriptorHandleNonShaderVisible,
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
}
/// <summary>
/// Creates a new <see cref="ID3D12CommandAllocator"/> for a given device.
/// </summary>
+17 -2
ファイルの表示
@@ -10,6 +10,7 @@ using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static TerraFX.Interop.DirectX.D3D12_FEATURE;
using static TerraFX.Interop.DirectX.DXGI_FORMAT;
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
#if NET6_0_OR_GREATER
using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal;
@@ -43,6 +44,11 @@ public unsafe abstract class Buffer<T> : NativeObject
/// </summary>
private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles;
/// <summary>
/// The <see cref="ID3D12ResourceDescriptorHandles"/> instance for the current resource, when a typed UAV is needed.
/// </summary>
private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView;
/// <summary>
/// The size in bytes of the current buffer (this value is never negative).
/// </summary>
@@ -92,6 +98,7 @@ public unsafe abstract class Buffer<T> : NativeObject
device.RegisterAllocatedResource();
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView);
switch (resourceType)
{
@@ -103,7 +110,12 @@ public unsafe abstract class Buffer<T> : NativeObject
break;
case ResourceType.ReadWrite:
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
device.D3D12Device->CreateUnorderedAccessViewForClear(
this.d3D12Resource.Get(),
DXGI_FORMAT_R32_UINT,
(uint)(usableSizeInBytes / sizeof(uint)),
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandle,
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
break;
}
@@ -201,6 +213,7 @@ public unsafe abstract class Buffer<T> : NativeObject
{
device.UnregisterAllocatedResource();
device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles);
device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView);
}
}
@@ -222,7 +235,9 @@ public unsafe abstract class Buffer<T> : NativeObject
ThrowIfDisposed();
ThrowIfDeviceMismatch(device);
return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible);
return (
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle,
this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
}
/// <inheritdoc cref="__Internals.GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice)"/>
+48
ファイルの表示
@@ -15,6 +15,54 @@ namespace ComputeSharp.Tests;
[TestCategory("ComputeContext")]
public partial class ComputeContextTests
{
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(int))]
[Data(typeof(Int2))]
[Data(typeof(Int3))]
[Data(typeof(Int4))]
[Data(typeof(uint))]
[Data(typeof(UInt2))]
[Data(typeof(UInt3))]
[Data(typeof(UInt4))]
[Data(typeof(float))]
[Data(typeof(Float2))]
[Data(typeof(Float3))]
[Data(typeof(Float4))]
[Data(typeof(Bgra32))]
[Data(typeof(Rgba32))]
[Data(typeof(Rgba64))]
[Data(typeof(R8))]
[Data(typeof(R16))]
[Data(typeof(Rg16))]
[Data(typeof(Rg32))]
public void Clear_ReadWriteBuffer(Device device, Type type)
{
static void Test<T>(Device device)
where T : unmanaged
{
T[] array = new T[128];
new Random().NextBytes(array.AsSpan().AsBytes());
using ReadWriteBuffer<T> texture = device.Get().AllocateReadWriteBuffer<T>(array);
using (ComputeContext context = device.Get().CreateComputeContext())
{
context.Clear(texture);
}
texture.CopyTo(array);
foreach (byte value in array.AsSpan().AsBytes())
{
Assert.AreEqual(value, 0);
}
}
TestHelper.Run(Test<int>, type, device);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(int))]