diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 2752ec6d..8be3ec80 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -458,6 +458,37 @@ internal static unsafe class ID3D12DeviceExtensions d3D12Device.CreateUnorderedAccessView(d3D12Resource, null, &d3D12UnorderedAccessViewDescription, d3D12CpuDescriptorHandle); } + /// + /// Creates a view for a buffer that will need to be cleared. + /// + /// The target instance in use. + /// The to create a view for. + /// The value to use. + /// The size of the target resource. + /// The instance for the current resource. + /// The non shader visible instance for the current resource. + 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); + } + /// /// Creates a new for a given device. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 9c815e7f..06992782 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -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 : NativeObject /// private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; + /// + /// The instance for the current resource, when a typed UAV is needed. + /// + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView; + /// /// The size in bytes of the current buffer (this value is never negative). /// @@ -92,6 +98,7 @@ public unsafe abstract class Buffer : NativeObject device.RegisterAllocatedResource(); device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView); switch (resourceType) { @@ -103,7 +110,12 @@ public unsafe abstract class Buffer : 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 : NativeObject { device.UnregisterAllocatedResource(); device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView); } } @@ -222,7 +235,9 @@ public unsafe abstract class Buffer : NativeObject ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + return ( + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle, + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible); } /// diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index a6ba37df..3ddaf53f 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -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(Device device) + where T : unmanaged + { + T[] array = new T[128]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteBuffer texture = device.Get().AllocateReadWriteBuffer(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, type, device); + } + [CombinatorialTestMethod] [AllDevices] [Data(typeof(int))]