diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index 7fd7ca5b..30363d12 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -26,9 +26,14 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable private ComPtr d3D12DescriptorHeap; /// - /// The array of items with the available descriptor handles. + /// The non shader visible in use for the current instance. /// - private readonly D3D12DescriptorHandlePair[] d3D12DescriptorHandlePairs; + private ComPtr d3D12DescriptorHeapNonShaderVisible; + + /// + /// The array of items with the available descriptor handles. + /// + private readonly ID3D12ResourceDescriptorHandles[] d3D12DescriptorHandlePairs; /// /// The head index for the queue. @@ -51,23 +56,28 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// The instance to use. public ID3D12DescriptorHandleAllocator(ID3D12Device* device) { - this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap); - this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray((int)DescriptorsPerHeap); + this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: true); + this.d3D12DescriptorHeapNonShaderVisible = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: false); + this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray((int)DescriptorsPerHeap); this.head = 0; this.tail = 0; this.size = (int)DescriptorsPerHeap; - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart(); + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart(); D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart(); + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisibleStart = this.d3D12DescriptorHeapNonShaderVisible.Get()->GetCPUDescriptorHandleForHeapStart(); uint descriptorIncrementSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); for (int i = 0; i < this.d3D12DescriptorHandlePairs.Length; i++) { - this.d3D12DescriptorHandlePairs[i] = new D3D12DescriptorHandlePair( - in d3D12CpuDescriptorHandle, - in d3D12GpuDescriptorHandleStart, - i, - descriptorIncrementSize); + D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, i, descriptorIncrementSize); + D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, in d3D12GpuDescriptorHandleStart, i, descriptorIncrementSize); + D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, in d3D12CpuDescriptorHandleNonShaderVisibleStart, i, descriptorIncrementSize); + + this.d3D12DescriptorHandlePairs[i] = new ID3D12ResourceDescriptorHandles( + d3D12CpuDescriptorHandle, + d3D12GpuDescriptorHandle, + d3D12CpuDescriptorHandleNonShaderVisible); } } @@ -77,22 +87,16 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable public ID3D12DescriptorHeap* D3D12DescriptorHeap => this.d3D12DescriptorHeap; /// - /// Rents a new CPU and GPU handle pair to use in a memory buffer. + /// Rents a new bundle of CPU and GPU handles to use in a resource. /// - /// The resulting value. - /// The resulting value. - public void Rent( - out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + /// The resulting value. + public void Rent(out ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsGreaterThan(this.size, 0, nameof(size)); - ref readonly D3D12DescriptorHandlePair d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; - - d3D12CpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandle; - d3D12GpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12GpuDescriptorhandle; + d3D12ResourceDescriptorHandles = this.d3D12DescriptorHandlePairs[this.head++]; if (this.head == DescriptorsPerHeap) { @@ -106,17 +110,14 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// /// Returns a CPU and GPU handle pair for later use. /// - /// The returned value. - /// The returned value. - public void Return( - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + /// The returned value. + public void Return(in ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsLessThan(this.size, DescriptorsPerHeap, nameof(size)); - this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandlePair(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); + this.d3D12DescriptorHandlePairs[this.tail++] = d3D12ResourceDescriptorHandles; if (this.tail == DescriptorsPerHeap) { @@ -132,50 +133,4 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { this.d3D12DescriptorHeap.Dispose(); } - - /// - /// A type representing a pair of reusable descriptor handles. - /// - private readonly struct D3D12DescriptorHandlePair - { - /// - /// The value for the current entry. - /// - public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; - - /// - /// The value for the current entry. - /// - public readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorhandle; - - /// - /// Creates a new instance with the given parameters. - /// - /// The value to wrap. - /// The value to wrap. - public D3D12DescriptorHandlePair( - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) - { - D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle; - D3D12GpuDescriptorhandle = d3D12GpuDescriptorHandle; - } - - /// - /// Creates a new instance with the given parameters. - /// - /// The base value. - /// The base value. - /// The offset for the new pair of handles to compute. - /// The increment size for each consecutive handles pair. - public D3D12DescriptorHandlePair( - in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart, - in D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart, - int offset, - uint descriptorIncrementSize) - { - D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, offset, descriptorIncrementSize); - D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12GpuDescriptorhandle, in d3D12GpuDescriptorHandleStart, offset, descriptorIncrementSize); - } - } } diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12ResourceDescriptorHandles.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12ResourceDescriptorHandles.cs new file mode 100644 index 00000000..e416d833 --- /dev/null +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12ResourceDescriptorHandles.cs @@ -0,0 +1,40 @@ +using TerraFX.Interop.DirectX; + +namespace ComputeSharp.Graphics.Commands.Interop; + +/// +/// A type representing a bundle of reusable resource descriptor handles. +/// +internal readonly struct ID3D12ResourceDescriptorHandles +{ + /// + /// The value for the current entry. + /// + public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + + /// + /// The value for the current entry. + /// + public readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + + /// + /// The value for the current entry, non shader visible. + /// + public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible; + + /// + /// Creates a new instance with the given parameters. + /// + /// The value to wrap. + /// The value to wrap. + /// The non shader visible value to wrap. + public ID3D12ResourceDescriptorHandles( + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible) + { + D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle; + D3D12GpuDescriptorHandle = d3D12GpuDescriptorHandle; + D3D12CpuDescriptorHandleNonShaderVisible = d3D12CpuDescriptorHandleNonShaderVisible; + } +} diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 477f68c1..8be3ec80 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs @@ -321,16 +321,17 @@ internal static unsafe class ID3D12DeviceExtensions /// /// The target to use to create the descriptor heap. /// The number of descriptors to allocate. + /// Whether or not the descriptor heap should be shader visible. /// A pointer to the newly allocated instance. /// Thrown when the creation of the command queue fails. - public static ComPtr CreateDescriptorHeap(this ref ID3D12Device d3D12Device, uint descriptorsCount) + public static ComPtr CreateDescriptorHeap(this ref ID3D12Device d3D12Device, uint descriptorsCount, bool isShaderVisible) { using ComPtr d3D12DescriptorHeap = default; D3D12_DESCRIPTOR_HEAP_DESC d3D12DescriptorHeapDesc; d3D12DescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; d3D12DescriptorHeapDesc.NumDescriptors = descriptorsCount; - d3D12DescriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + d3D12DescriptorHeapDesc.Flags = isShaderVisible ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE : D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_NONE; d3D12DescriptorHeapDesc.NodeMask = 0; d3D12Device.CreateDescriptorHeap( @@ -457,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/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index 987fbf90..16bb50b1 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -1,7 +1,6 @@ using System; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop.DirectX; -using static TerraFX.Interop.DirectX.D3D12_RESOURCE_BARRIER_FLAGS; -using static TerraFX.Interop.DirectX.D3D12_RESOURCE_BARRIER_TYPE; namespace ComputeSharp.Graphics.Extensions; @@ -213,13 +212,54 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions /// /// The instance in use. /// The to insert the barrier for. - public static void UAVBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource) + public static void UnorderedAccessViewBarrier(this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, ID3D12Resource* d3D12Resource) { D3D12_RESOURCE_BARRIER d3D12ResourceBarrier = D3D12_RESOURCE_BARRIER.InitUAV(d3D12Resource); d3D12GraphicsCommandList.ResourceBarrier(1, &d3D12ResourceBarrier); } + /// + /// Clears a target UAV resource. + /// + /// The instance in use. + /// The to clear. + /// The value for the target resource. + /// The value for the target resource. + /// Indicates whether the target resource uses a normalized format. + public static unsafe void ClearUnorderedAccessView( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + bool isNormalized) + { + if (isNormalized) + { + Float4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewFloat( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (float*)&values, + NumRects: 0, + pRects: null); + } + else + { + UInt4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewUint( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (uint*)&values, + NumRects: 0, + pRects: null); + } + } + /// /// Binds an input constant buffer to the first root parameter. /// diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1ea769fe..fd87c436 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -284,20 +284,16 @@ public sealed unsafe partial class GraphicsDevice : NativeObject /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void RentShaderResourceViewDescriptorHandles( - out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + internal void RentShaderResourceViewDescriptorHandles(out ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { - this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Rent(out d3D12ResourceDescriptorHandles); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void ReturnShaderResourceViewDescriptorHandles( - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + internal void ReturnShaderResourceViewDescriptorHandles(in ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { - this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Return(in d3D12ResourceDescriptorHandles); } /// diff --git a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs index dec9ee3e..6fd25e28 100644 --- a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs @@ -45,4 +45,47 @@ internal static class DXGIFormatHelper else if (typeof(T) == typeof(Rg32)) return DXGI_FORMAT_R16G16_UNORM; else return ThrowHelper.ThrowArgumentException("Invalid texture type"); } + + /// + /// Gets whether or not the input type corresponds to a normalized format. + /// + /// The input type argument to check. + /// TWhether or not the input type corresponds to a normalized format.. + /// Thrown when the input type is not supported. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool IsNormalizedType() + where T : unmanaged + { + if (typeof(T) == typeof(int) || + typeof(T) == typeof(Int2) || + typeof(T) == typeof(Int3) || + typeof(T) == typeof(Int4) || + typeof(T) == typeof(uint) || + typeof(T) == typeof(UInt2) || + typeof(T) == typeof(UInt3) || + typeof(T) == typeof(UInt4) || + typeof(T) == typeof(float) || + typeof(T) == typeof(Float2) || + typeof(T) == typeof(Float3) || + typeof(T) == typeof(Float4) || + typeof(T) == typeof(Vector2) || + typeof(T) == typeof(Vector3) || + typeof(T) == typeof(Vector4)) + { + return false; + } + + if (typeof(T) == typeof(Bgra32) || + typeof(T) == typeof(Rgba32) || + typeof(T) == typeof(Rgba64) || + typeof(T) == typeof(R8) || + typeof(T) == typeof(R16) || + typeof(T) == typeof(Rg16) || + typeof(T) == typeof(Rg32)) + { + return true; + } + + return ThrowHelper.ThrowArgumentException("Invalid texture type"); + } } diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 48df0aa0..195862ad 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -26,6 +26,14 @@ public static class GraphicsResourceHelper /// The GPU descriptor handle for the resource. D3D12_GPU_DESCRIPTOR_HANDLE ValidateAndGetGpuDescriptorHandle(GraphicsDevice device); + /// + /// Validates the given resource for usage with a specified device, and retrieves its GPU and CPU descriptor handles for a clear operation. + /// + /// The target instance in use. + /// Indicates whether the current resource uses a normalized format. + /// The GPU and CPU descriptor handles for the resource. + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized); + /// /// Validates the given resource for usage with a specified device, and retrieves the underlying object. /// @@ -42,13 +50,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the buffer. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(Buffer buffer, GraphicsDevice device) where T : unmanaged { buffer.ThrowIfDisposed(); buffer.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in buffer.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = buffer.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -59,13 +69,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture2D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -76,13 +88,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture2D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture2D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -135,13 +149,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadOnlyTexture3D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// @@ -152,13 +168,15 @@ public static class GraphicsResourceHelper /// The target instance in use. /// The GPU descriptor handle for the texture. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture3D texture, GraphicsDevice device) + public static unsafe ulong ValidateAndGetGpuDescriptorHandle(ReadWriteTexture3D texture, GraphicsDevice device) where T : unmanaged { texture.ThrowIfDisposed(); texture.ThrowIfDeviceMismatch(device); - return Unsafe.As(ref Unsafe.AsRef(in texture.D3D12GpuDescriptorHandle)); + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = texture.D3D12GpuDescriptorHandle; + + return *(ulong*)&d3D12GpuDescriptorHandle; } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index c9fd0889..06992782 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -2,6 +2,7 @@ using System.Runtime.CompilerServices; using ComputeSharp.Core.Helpers; using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Interop; @@ -9,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; @@ -38,14 +40,14 @@ public unsafe abstract class Buffer : NativeObject private ComPtr d3D12Resource; /// - /// The instance for the current resource. + /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// - /// The instance for the current resource. + /// The instance for the current resource, when a typed UAV is needed. /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView; /// /// The size in bytes of the current buffer (this value is never negative). @@ -95,18 +97,25 @@ public unsafe abstract class Buffer : NativeObject #endif device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView); switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessViewForClear( + this.d3D12Resource.Get(), + DXGI_FORMAT_R32_UINT, + (uint)(usableSizeInBytes / sizeof(uint)), + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandle, + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -137,6 +146,11 @@ public unsafe abstract class Buffer : NativeObject /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Gets the instance for the current resource. + /// + internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -198,7 +212,8 @@ public unsafe abstract class Buffer : NativeObject if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView); } } @@ -213,4 +228,24 @@ public unsafe abstract class Buffer : NativeObject GraphicsDeviceMismatchException.Throw(this, device); } } + + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return ( + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12GpuDescriptorHandle, + this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible); + } + + /// + internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return D3D12Resource; + } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index d9dd43dd..5b30dad3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -304,13 +304,4 @@ public abstract class StructuredBuffer : Buffer copyCommandList.ExecuteAndWaitForCompletion(); } } - - /// - internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) - { - ThrowIfDisposed(); - ThrowIfDeviceMismatch(device); - - return D3D12Resource; - } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index ad3ad054..3b828db5 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -2,6 +2,7 @@ using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; @@ -39,14 +40,9 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper private ComPtr d3D12Resource; /// - /// The instance for the current resource. + /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; - - /// - /// The instance for the current resource. - /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The default value for the current resource. @@ -120,15 +116,16 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -155,6 +152,11 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Gets the instance for the current resource. + /// + internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -536,7 +538,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -552,6 +554,17 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + } + /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { @@ -570,6 +583,12 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); + } + /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 8cd5ef65..55d913df 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -2,6 +2,7 @@ using ComputeSharp.__Internals; using ComputeSharp.Exceptions; using ComputeSharp.Graphics.Commands; +using ComputeSharp.Graphics.Commands.Interop; using ComputeSharp.Graphics.Extensions; using ComputeSharp.Graphics.Helpers; using ComputeSharp.Graphics.Resources.Interop; @@ -39,14 +40,9 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper private ComPtr d3D12Resource; /// - /// The instance for the current resource. + /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; - - /// - /// The instance for the current resource. - /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The default value for the current resource. @@ -125,15 +121,16 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); break; case ResourceType.ReadWrite: - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -165,6 +162,11 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// internal ID3D12Resource* D3D12Resource => this.d3D12Resource; + /// + /// Gets the instance for the current resource. + /// + internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -589,7 +591,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -605,6 +607,17 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); + } + /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { @@ -623,6 +636,12 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) + { + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); + } + /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 17b0dcd4..3ffb9935 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -40,7 +40,35 @@ public ref struct ComputeContext /// /// Gets the associated with the current instance. /// - public GraphicsDevice GraphicsDevice => this.device; + public GraphicsDevice GraphicsDevice + { + get + { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + + return this.device; + } + } + + /// + /// Clears a specific resource. + /// + /// The to clear. + /// The value for the target resource. + /// The value for the target resource. + /// Indicates whether the target resource uses a normalized format. + internal readonly unsafe void Clear( + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + bool isNormalized) + { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + + ref CommandList commandList = ref GetCommandList(in this, null); + + commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, isNormalized); + } /// /// Inserts a resource barrier for a specific resource. @@ -52,7 +80,7 @@ public ref struct ComputeContext ref CommandList commandList = ref GetCommandList(in this); - commandList.D3D12GraphicsCommandList->UAVBarrier(d3D12Resource); + commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); } /// @@ -253,7 +281,12 @@ public ref struct ComputeContext if (context.commandList.IsAllocated) { - context.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); + // Skip setting the pipeline state if the new state is null. This is the case when the upcoming + // operation is not a shader dispatch, but just a resource clear. In this case there is no state. + if (pipelineState is not null) + { + context.commandList.D3D12GraphicsCommandList->SetPipelineState(pipelineState); + } } else { diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 3fd3e302..ac2019ed 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -98,6 +98,108 @@ public static partial class ComputeContextExtensions context.Barrier(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice)); } + /// + /// Clears a specific resource. + /// + /// The type of items stored on the buffer. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) + where T : unmanaged + { + var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice); + + context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized: false); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of pixels stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture2D texture) + where TPixel : unmanaged + { + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); + } + + /// + /// Clears a specific resource, with a specified value. + /// + /// The type of pixels stored on the texture. + /// The to use to clear the resource. + /// The input instance to clear. + public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture3D texture) + where TPixel : unmanaged + { + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); + } + /// /// Runs the input shader on a target instance, with the specified parameters. /// diff --git a/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs index cace23fe..ffc02277 100644 --- a/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs +++ b/src/TerraFX.Interop.Windows/DirectX/um/d3d12/ID3D12GraphicsCommandList.cs @@ -298,5 +298,19 @@ namespace TerraFX.Interop.DirectX { ((delegate* unmanaged[Stdcall])(lpVtbl[42]))((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref this), RootParameterIndex, BufferLocation); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(49)] + public void ClearUnorderedAccessViewUint(D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap, D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle, ID3D12Resource* pResource, [NativeTypeName("const UINT [4]")] uint* Values, uint NumRects, [NativeTypeName("const D3D12_RECT *")] void* pRects) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[49]))((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref this), ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(50)] + public void ClearUnorderedAccessViewFloat(D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap, D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle, ID3D12Resource* pResource, [NativeTypeName("const FLOAT [4]")] float* Values, uint NumRects, [NativeTypeName("const D3D12_RECT *")] void* pRects) + { + ((delegate* unmanaged[Stdcall])(lpVtbl[50]))((ID3D12GraphicsCommandList*)Unsafe.AsPointer(ref this), ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects); + } } } diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index c0b48044..3ddaf53f 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -1,15 +1,342 @@ using System; using System.Linq; +using ComputeSharp.__Internals; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; +using ComputeSharp.Tests.Helpers; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; +#pragma warning disable CS0618 + namespace ComputeSharp.Tests; [TestClass] [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))] + [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_ReadWriteTexture2D(Device device, Type type) + { + static void Test(Device device) + where T : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(array, 128, 128); + + 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))] + [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_ReadWriteTexture3D(Device device, Type type) + { + static void Test(Device device) + where T : unmanaged + { + if (!device.Get().IsReadWriteTexture3DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128 * 3]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(array, 128, 128, 3); + + 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(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture2D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(array, 128, 128); + + 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, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture3D_WithPixel(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128 * 3]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(array, 128, 128, 3); + + 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, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture2D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(array, 128, 128); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear((IReadWriteTexture2D)texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + + [CombinatorialTestMethod] + [AllDevices] + [Data(typeof(Bgra32), typeof(Float4))] + [Data(typeof(Rgba32), typeof(Float4))] + [Data(typeof(Rgba64), typeof(Float4))] + [Data(typeof(R8), typeof(float))] + [Data(typeof(R16), typeof(float))] + [Data(typeof(Rg16), typeof(Float2))] + [Data(typeof(Rg32), typeof(Float2))] + public void Clear_ReadWriteTexture3D_WithPixel_AsNormalizedTexture(Device device, Type cpuType, Type gpuType) + { + static void Test(Device device) + where T : unmanaged, IUnorm + where TPixel : unmanaged + { + if (!device.Get().IsReadWriteTexture2DSupportedForType()) + { + Assert.Inconclusive(); + } + + T[] array = new T[128 * 128 * 3]; + + new Random().NextBytes(array.AsSpan().AsBytes()); + + using ReadWriteTexture3D texture = device.Get().AllocateReadWriteTexture3D(array, 128, 128, 3); + + using (ComputeContext context = device.Get().CreateComputeContext()) + { + context.Clear((IReadWriteTexture3D)texture); + } + + texture.CopyTo(array); + + foreach (byte value in array.AsSpan().AsBytes()) + { + Assert.AreEqual(value, 0); + } + } + + TestHelper.Run(Test, cpuType, gpuType, device); + } + [CombinatorialTestMethod] [AllDevices] public void For_Batched(Device device) @@ -108,7 +435,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void Barrier_WithoutPreviousDispatches_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); @@ -123,7 +450,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_Barrier_ThrowsException(Device device) { using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); @@ -135,8 +462,22 @@ public partial class ComputeContextTests Assert.Fail(); } + [CombinatorialTestMethod] + [AllDevices] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] + public void DefaultContext_Clear_ThrowsException(Device device) + { + using ReadWriteBuffer buffer = device.Get().AllocateReadWriteBuffer(64); + + using ComputeContext context = default; + + context.Clear(buffer); + + Assert.Fail(); + } + [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_For_ThrowsException() { using ComputeContext context = default; @@ -148,7 +489,7 @@ public partial class ComputeContextTests [CombinatorialTestMethod] [AllDevices] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_ForEach_ThrowsException(Device device) { using ReadWriteTexture2D texture = device.Get().AllocateReadWriteTexture2D(128, 128); @@ -161,7 +502,7 @@ public partial class ComputeContextTests } [TestMethod] - [ExpectedException(typeof(InvalidOperationException))] + [ExpectedException(typeof(InvalidOperationException), AllowDerivedTypes = false)] public void DefaultContext_Dispose_ThrowsException() { using ComputeContext context = default; diff --git a/tests/ComputeSharp.Tests/Helpers/TestHelper.cs b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs new file mode 100644 index 00000000..4d2a447c --- /dev/null +++ b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs @@ -0,0 +1,58 @@ +using System; +using System.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.Tests.Helpers; + +/// +/// A helper class for test methods. +/// +public static class TestHelper +{ + /// + /// Runs a generic test methods with the specified arguments. + /// + /// A instance wrapping the target method. + /// The type argument to use to invoke . + /// The arguments to use to invoke . + public static void Run(Delegate @delegate, Type type, params object[] arguments) + { + if (@delegate.Method is not { IsStatic: true, IsGenericMethod: true }) + { + Assert.Fail("The input delegate must point to a generic static method"); + } + + try + { + @delegate.Method.GetGenericMethodDefinition().MakeGenericMethod(type).Invoke(null, arguments); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } + + /// + /// Runs a generic test methods with the specified arguments. + /// + /// A instance wrapping the target method. + /// The first type argument to use to invoke . + /// The second type argument to use to invoke . + /// The arguments to use to invoke . + public static void Run(Delegate @delegate, Type type1, Type type2, params object[] arguments) + { + if (@delegate.Method is not { IsStatic: true, IsGenericMethod: true }) + { + Assert.Fail("The input delegate must point to a generic static method"); + } + + try + { + @delegate.Method.GetGenericMethodDefinition().MakeGenericMethod(type1, type2).Invoke(null, arguments); + } + catch (TargetInvocationException e) when (e.InnerException is not null) + { + throw e.InnerException; + } + } +}