From 81da4482001ad7b41f5238d142f4dde16025a89c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 28 Dec 2021 20:42:20 +0100 Subject: [PATCH 1/8] Add IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles API --- .../__Internals/GraphicsResourceHelper.cs | 37 ++++++++++++++----- .../Graphics/Resources/Abstract/Buffer{T}.cs | 22 ++++++++--- .../Resources/Abstract/StructuredBuffer{T}.cs | 9 +++++ .../Resources/Abstract/Texture2D{T}.cs | 35 +++++++++++++++--- .../Resources/Abstract/Texture3D{T}.cs | 35 +++++++++++++++--- 5 files changed, 112 insertions(+), 26 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs index 48df0aa0..b88ac3b4 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -26,6 +26,13 @@ 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. + /// + /// The target instance in use. + /// The GPU and CPU descriptor handles for the resource. + (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device); + /// /// Validates the given resource for usage with a specified device, and retrieves the underlying object. /// @@ -42,13 +49,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 +68,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 +87,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 +148,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 +167,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..5da1fd60 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -40,12 +40,12 @@ public unsafe abstract class Buffer : NativeObject /// /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; /// /// The instance for the current resource. /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; /// /// The size in bytes of the current buffer (this value is never negative). @@ -95,18 +95,18 @@ public unsafe abstract class Buffer : NativeObject #endif device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, D3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.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.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.d3D12CpuDescriptorHandle); break; } @@ -137,6 +137,16 @@ 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.d3D12GpuDescriptorHandle; + + /// + /// Gets the instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index d9dd43dd..659a8981 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -305,6 +305,15 @@ public abstract class StructuredBuffer : Buffer } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + } + /// internal unsafe ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index ad3ad054..4a109c27 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -41,12 +41,12 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; /// /// The instance for the current resource. /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; /// /// The default value for the current resource. @@ -120,15 +120,15 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); 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.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.d3D12CpuDescriptorHandle); break; } @@ -155,6 +155,16 @@ 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.d3D12GpuDescriptorHandle; + + /// + /// Gets the instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -552,6 +562,15 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + } + /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { @@ -570,6 +589,12 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + return ValidateAndGetGpuAndCpuDescriptorHandles(device); + } + /// 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..23bddb66 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -41,12 +41,12 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// /// The instance for the current resource. /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; /// /// The instance for the current resource. /// - internal readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle; + private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; /// /// The default value for the current resource. @@ -125,15 +125,15 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); 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.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.d3D12CpuDescriptorHandle); break; } @@ -165,6 +165,16 @@ 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.d3D12GpuDescriptorHandle; + + /// + /// Gets the instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -605,6 +615,15 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + ThrowIfDisposed(); + ThrowIfDeviceMismatch(device); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + } + /// internal ID3D12Resource* ValidateAndGetID3D12Resource(GraphicsDevice device) { @@ -623,6 +642,12 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + { + return ValidateAndGetGpuAndCpuDescriptorHandles(device); + } + /// ID3D12Resource* GraphicsResourceHelper.IGraphicsResource.ValidateAndGetID3D12Resource(GraphicsDevice device) { From 2ceb3b83f6f825941ed819a853806f7fde201a70 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 28 Dec 2021 21:14:49 +0100 Subject: [PATCH 2/8] Add initial prototype of Clear APIs --- .../ID3D12GraphicsCommandListExtensions.cs | 50 ++++++++++- src/ComputeSharp/Shaders/ComputeContext.cs | 28 +++++- .../Extensions/ComputeContextExtensions.cs | 90 +++++++++++++++++++ .../um/d3d12/ID3D12GraphicsCommandList.cs | 14 +++ 4 files changed, 177 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index 987fbf90..ad2664be 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,58 @@ 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. + public static unsafe void ClearUnorderedAccessView( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) + where T : unmanaged + { + if (typeof(T) == typeof(uint)) + { + UInt4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewUint( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (uint*)&values, + NumRects: 0, + pRects: null); + } + else if (typeof(T) == typeof(float)) + { + Float4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewFloat( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (float*)&values, + NumRects: 0, + pRects: null); + } + else + { + ThrowHelper.ThrowArgumentException("Invalid clear type"); + } + } + /// /// Binds an input constant buffer to the first root parameter. /// diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 17b0dcd4..cca1e2cb 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -42,6 +42,25 @@ public ref struct ComputeContext /// public GraphicsDevice GraphicsDevice => this.device; + /// + /// Clears a specific resource. + /// + /// The to clear. + /// The value for the target resource. + /// The value for the target resource. + internal readonly unsafe void Clear( + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle) + where T : unmanaged + { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + + ref CommandList commandList = ref GetCommandList(in this, null); + + commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle); + } + /// /// Inserts a resource barrier for a specific resource. /// @@ -52,7 +71,7 @@ public ref struct ComputeContext ref CommandList commandList = ref GetCommandList(in this); - commandList.D3D12GraphicsCommandList->UAVBarrier(d3D12Resource); + commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); } /// @@ -253,7 +272,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..6e880332 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -98,6 +98,96 @@ 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.ValidateAndGetGpuAndCpuDescriptorHandles(context.GraphicsDevice); + + context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu); + } + + /// + /// 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 + { + throw new System.NotImplementedException(); + } + + /// + /// 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 + { + throw new System.NotImplementedException(); + } + + /// + /// 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 + { + throw new System.NotImplementedException(); + } + + /// + /// 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 + { + throw new System.NotImplementedException(); + } + + /// + /// 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 + { + throw new System.NotImplementedException(); + } + + /// + /// 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 + { + throw new System.NotImplementedException(); + } + /// /// 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); + } } } From a9f50c1e468577aee3d213499ecc9bf09280e049 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 13:12:01 +0100 Subject: [PATCH 3/8] Add non shader visible descriptor heap setup --- .../ID3D12DescriptorHandleAllocator.cs | 50 ++++++++++++++----- .../Interop/ID3D12DeviceExtensions.cs | 5 +- src/ComputeSharp/Graphics/GraphicsDevice.cs | 6 ++- .../Graphics/Resources/Abstract/Buffer{T}.cs | 15 +++++- .../Resources/Abstract/StructuredBuffer{T}.cs | 2 +- .../Resources/Abstract/Texture2D{T}.cs | 17 +++++-- .../Resources/Abstract/Texture3D{T}.cs | 10 +++- 7 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index 7fd7ca5b..cf4b905d 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 D3D12DescriptorHandleBundle[] d3D12DescriptorHandlePairs; /// /// The head index for the queue. @@ -51,20 +56,23 @@ 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 d3D12CpuDescriptorHandleNonShaderVisible = this.d3D12DescriptorHeapNonShaderVisible.Get()->GetCPUDescriptorHandleForHeapStart(); D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart(); 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( + this.d3D12DescriptorHandlePairs[i] = new D3D12DescriptorHandleBundle( in d3D12CpuDescriptorHandle, + in d3D12CpuDescriptorHandleNonShaderVisible, in d3D12GpuDescriptorHandleStart, i, descriptorIncrementSize); @@ -80,18 +88,21 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// Rents a new CPU and GPU handle pair to use in a memory buffer. /// /// The resulting value. + /// The resulting non shader visible value. /// The resulting value. public void Rent( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsGreaterThan(this.size, 0, nameof(size)); - ref readonly D3D12DescriptorHandlePair d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; + ref readonly D3D12DescriptorHandleBundle d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; d3D12CpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandle; + d3D12CpuDescriptorHandleNonShaderVisible = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandleNonShaderVisible; d3D12GpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12GpuDescriptorhandle; if (this.head == DescriptorsPerHeap) @@ -107,16 +118,18 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// Returns a CPU and GPU handle pair for later use. /// /// The returned value. + /// The returned non shader visible value. /// The returned value. public void Return( D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { lock (this.d3D12DescriptorHandlePairs) { Guard.IsLessThan(this.size, DescriptorsPerHeap, nameof(size)); - this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandlePair(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); + this.d3D12DescriptorHandlePairs[this.tail++] = new D3D12DescriptorHandleBundle(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonShaderVisible, d3D12GpuDescriptorHandle); if (this.tail == DescriptorsPerHeap) { @@ -134,47 +147,58 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable } /// - /// A type representing a pair of reusable descriptor handles. + /// A type representing a bundle of reusable descriptor handles. /// - private readonly struct D3D12DescriptorHandlePair + private readonly struct D3D12DescriptorHandleBundle { /// /// The value for the current entry. /// public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; + /// + /// The value for the current entry, non shader visible. + /// + public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible; + /// /// The value for the current entry. /// public readonly D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorhandle; /// - /// Creates a new instance with the given parameters. + /// Creates a new instance with the given parameters. /// /// The value to wrap. + /// The non shader visible value to wrap. /// The value to wrap. - public D3D12DescriptorHandlePair( + public D3D12DescriptorHandleBundle( D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle; + D3D12CpuDescriptorHandleNonShaderVisible = d3D12CpuDescriptorHandleNonShaderVisible; D3D12GpuDescriptorhandle = d3D12GpuDescriptorHandle; } /// - /// Creates a new instance with the given parameters. + /// Creates a new instance with the given parameters. /// /// The base value. + /// The base non shader visible value. /// The base value. /// The offset for the new pair of handles to compute. /// The increment size for each consecutive handles pair. - public D3D12DescriptorHandlePair( + public D3D12DescriptorHandleBundle( in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart, + in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, in D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart, int offset, uint descriptorIncrementSize) { D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, offset, descriptorIncrementSize); + D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandleNonShaderVisible, in d3D12CpuDescriptorHandleNonShaderVisible, offset, descriptorIncrementSize); D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12GpuDescriptorhandle, in d3D12GpuDescriptorHandleStart, offset, descriptorIncrementSize); } } diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12DeviceExtensions.cs index 477f68c1..2752ec6d 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( diff --git a/src/ComputeSharp/Graphics/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1ea769fe..1c7b4e7c 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -286,18 +286,20 @@ public sealed unsafe partial class GraphicsDevice : NativeObject [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void RentShaderResourceViewDescriptorHandles( out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonGpuVisible, out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { - this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12CpuDescriptorHandleNonGpuVisible, out d3D12GpuDescriptorHandle); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void ReturnShaderResourceViewDescriptorHandles( D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonGpuVisible, D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) { - this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonGpuVisible, d3D12GpuDescriptorHandle); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 5da1fd60..51000b13 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -42,6 +42,11 @@ public unsafe abstract class Buffer : NativeObject /// private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; + /// + /// The non shader visible instance for the current resource. + /// + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; + /// /// The instance for the current resource. /// @@ -95,7 +100,7 @@ public unsafe abstract class Buffer : NativeObject #endif device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); switch (resourceType) { @@ -107,6 +112,7 @@ public unsafe abstract class Buffer : NativeObject break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -147,6 +153,11 @@ public unsafe abstract class Buffer : NativeObject /// internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// + /// Gets the non shader visible instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -208,7 +219,7 @@ public unsafe abstract class Buffer : NativeObject if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); } } diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 659a8981..33e29398 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -311,7 +311,7 @@ public abstract class StructuredBuffer : Buffer ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 4a109c27..802db69b 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -43,6 +43,11 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; + /// + /// The non shader visible instance for the current resource. + /// + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; + /// /// The instance for the current resource. /// @@ -120,7 +125,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); switch (resourceType) { @@ -129,6 +134,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -165,6 +171,11 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// + /// Gets the non shader visible instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -546,7 +557,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); } } @@ -568,7 +579,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 23bddb66..87327438 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -43,6 +43,11 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle; + /// + /// The non shader visible instance for the current resource. + /// + private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; + /// /// The instance for the current resource. /// @@ -125,7 +130,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); switch (resourceType) { @@ -134,6 +139,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper break; case ResourceType.ReadWrite: device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -599,7 +605,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); } } From 1d138d8aaee8c34e0a49f63f499adfa2da30a5fb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 15:26:00 +0100 Subject: [PATCH 4/8] Implement texture clear APIs --- .../ID3D12GraphicsCommandListExtensions.cs | 32 ++++++-------- .../Graphics/Helpers/DXGIFormatHelper.cs | 43 +++++++++++++++++++ .../__Internals/GraphicsResourceHelper.cs | 5 ++- .../Graphics/Resources/Abstract/Buffer{T}.cs | 2 +- .../Resources/Abstract/StructuredBuffer{T}.cs | 6 +-- .../Resources/Abstract/Texture2D{T}.cs | 14 +++--- .../Resources/Abstract/Texture3D{T}.cs | 19 +++++--- src/ComputeSharp/Shaders/ComputeContext.cs | 19 +++++--- .../Extensions/ComputeContextExtensions.cs | 28 ++++++++---- 9 files changed, 119 insertions(+), 49 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index ad2664be..16bb50b1 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -222,31 +222,19 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions /// /// Clears a target UAV resource. /// - /// /// The instance in use. /// The to clear. /// The value for the target resource. /// The value for the target resource. - public static unsafe void ClearUnorderedAccessView( + /// 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) - where T : unmanaged + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + bool isNormalized) { - if (typeof(T) == typeof(uint)) - { - UInt4 values = default; - - d3D12GraphicsCommandList.ClearUnorderedAccessViewUint( - ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, - ViewCPUHandle: d3D12CpuDescriptorHandle, - pResource: d3D12Resource, - Values: (uint*)&values, - NumRects: 0, - pRects: null); - } - else if (typeof(T) == typeof(float)) + if (isNormalized) { Float4 values = default; @@ -260,7 +248,15 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions } else { - ThrowHelper.ThrowArgumentException("Invalid clear type"); + UInt4 values = default; + + d3D12GraphicsCommandList.ClearUnorderedAccessViewUint( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (uint*)&values, + NumRects: 0, + pRects: null); } } 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 b88ac3b4..195862ad 100644 --- a/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/__Internals/GraphicsResourceHelper.cs @@ -27,11 +27,12 @@ public static class GraphicsResourceHelper 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. + /// 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) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device); + (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. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 51000b13..40b7db1a 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs @@ -156,7 +156,7 @@ public unsafe abstract class Buffer : NativeObject /// /// Gets the non shader visible instance for the current resource. /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs index 33e29398..c0c96b40 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -305,13 +305,13 @@ public abstract class StructuredBuffer : Buffer } } - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) { ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs index 802db69b..c8e0f27f 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture2D{T}.cs @@ -174,7 +174,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// Gets the non shader visible instance for the current resource. /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonGpuVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. @@ -573,13 +573,15 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } } - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonGpuVisible); + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); } /// @@ -601,9 +603,9 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper } /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { - return ValidateAndGetGpuAndCpuDescriptorHandles(device); + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index 87327438..ab71fe6e 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs @@ -181,6 +181,11 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; + /// + /// Gets the non shader visible instance for the current resource. + /// + internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + /// /// Reads the contents of the specified range from the current instance and writes them into a target memory area. /// @@ -621,13 +626,15 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper } } - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { ThrowIfDisposed(); ThrowIfDeviceMismatch(device); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandle); + isNormalized = DXGIFormatHelper.IsNormalizedType(); + + return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); } /// @@ -648,10 +655,10 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper return D3D12GpuDescriptorHandle; } - /// - (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandles(GraphicsDevice device) + /// + (D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE) GraphicsResourceHelper.IGraphicsResource.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device, out bool isNormalized) { - return ValidateAndGetGpuAndCpuDescriptorHandles(device); + return ValidateAndGetGpuAndCpuDescriptorHandlesForClear(device, out isNormalized); } /// diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index cca1e2cb..3ffb9935 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -40,7 +40,15 @@ 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. @@ -48,17 +56,18 @@ public ref struct ComputeContext /// The to clear. /// The value for the target resource. /// The value for the target resource. - internal readonly unsafe void Clear( + /// 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) - where T : unmanaged + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + bool isNormalized) { ThrowInvalidOperationExceptionIfDeviceIsNull(); ref CommandList commandList = ref GetCommandList(in this, null); - commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle); + commandList.D3D12GraphicsCommandList->ClearUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, isNormalized); } /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index 6e880332..ac2019ed 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -107,9 +107,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteBuffer buffer) where T : unmanaged { - var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandles(context.GraphicsDevice); + var handles = buffer.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice); - context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu); + context.Clear(buffer.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized: false); } /// @@ -121,7 +121,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture2D texture) where T : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -133,7 +135,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, ReadWriteTexture3D texture) where T : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -147,7 +151,9 @@ public static partial class ComputeContextExtensions where T : unmanaged, IUnorm where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -161,7 +167,9 @@ public static partial class ComputeContextExtensions where T : unmanaged, IUnorm where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); } /// @@ -173,7 +181,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture2D texture) where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); } /// @@ -185,7 +195,9 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture3D texture) where TPixel : unmanaged { - throw new System.NotImplementedException(); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); } /// From e2faab5aedc4652cd8798ac2c4aa487153827c46 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 15:28:09 +0100 Subject: [PATCH 5/8] Add unit tests for normal texture clearing --- .../ComputeSharp.Tests/ComputeContextTests.cs | 132 +++++++++++++++++- .../ComputeSharp.Tests/Helpers/TestHelper.cs | 34 +++++ 2 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 tests/ComputeSharp.Tests/Helpers/TestHelper.cs diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index c0b48044..9e747b73 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -2,6 +2,8 @@ using System.Linq; using ComputeSharp.Tests.Attributes; using ComputeSharp.Tests.Extensions; +using ComputeSharp.Tests.Helpers; +using Microsoft.Toolkit.HighPerformance; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ComputeSharp.Tests; @@ -10,6 +12,112 @@ 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_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] public void For_Batched(Device device) @@ -108,7 +216,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 +231,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 +243,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 +270,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 +283,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..3601bd20 --- /dev/null +++ b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs @@ -0,0 +1,34 @@ +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; + } + } +} From 26c6f86d9f4b0492548749a6b63c3320b1b72f96 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 15:36:09 +0100 Subject: [PATCH 6/8] Add remaining unit tests for clearing all texture types --- .../ComputeSharp.Tests/ComputeContextTests.cs | 171 ++++++++++++++++++ .../ComputeSharp.Tests/Helpers/TestHelper.cs | 24 +++ 2 files changed, 195 insertions(+) diff --git a/tests/ComputeSharp.Tests/ComputeContextTests.cs b/tests/ComputeSharp.Tests/ComputeContextTests.cs index 9e747b73..a6ba37df 100644 --- a/tests/ComputeSharp.Tests/ComputeContextTests.cs +++ b/tests/ComputeSharp.Tests/ComputeContextTests.cs @@ -1,11 +1,14 @@ 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] @@ -118,6 +121,174 @@ public partial class ComputeContextTests 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) diff --git a/tests/ComputeSharp.Tests/Helpers/TestHelper.cs b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs index 3601bd20..4d2a447c 100644 --- a/tests/ComputeSharp.Tests/Helpers/TestHelper.cs +++ b/tests/ComputeSharp.Tests/Helpers/TestHelper.cs @@ -31,4 +31,28 @@ public static class TestHelper 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; + } + } } From 8c75a715807a5ed92721592e72162d26d734b37d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 16:08:26 +0100 Subject: [PATCH 7/8] Minor code refactoring --- .../ID3D12DescriptorHandleAllocator.cs | 109 ++++-------------- .../ID3D12ResourceDescriptorHandles.cs | 40 +++++++ src/ComputeSharp/Graphics/GraphicsDevice.cs | 14 +-- .../Graphics/Resources/Abstract/Buffer{T}.cs | 57 +++++---- .../Resources/Abstract/StructuredBuffer{T}.cs | 18 --- .../Resources/Abstract/Texture2D{T}.cs | 39 ++----- .../Resources/Abstract/Texture3D{T}.cs | 39 ++----- 7 files changed, 112 insertions(+), 204 deletions(-) create mode 100644 src/ComputeSharp/Graphics/Commands/Interop/ID3D12ResourceDescriptorHandles.cs diff --git a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs index cf4b905d..30363d12 100644 --- a/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs +++ b/src/ComputeSharp/Graphics/Commands/Interop/ID3D12DescriptorHandleAllocator.cs @@ -31,9 +31,9 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable private ComPtr d3D12DescriptorHeapNonShaderVisible; /// - /// The array of items with the available descriptor handles. + /// The array of items with the available descriptor handles. /// - private readonly D3D12DescriptorHandleBundle[] d3D12DescriptorHandlePairs; + private readonly ID3D12ResourceDescriptorHandles[] d3D12DescriptorHandlePairs; /// /// The head index for the queue. @@ -58,24 +58,26 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: true); this.d3D12DescriptorHeapNonShaderVisible = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: false); - this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray((int)DescriptorsPerHeap); + 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 d3D12CpuDescriptorHandleNonShaderVisible = this.d3D12DescriptorHeapNonShaderVisible.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 D3D12DescriptorHandleBundle( - in d3D12CpuDescriptorHandle, - in d3D12CpuDescriptorHandleNonShaderVisible, - 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); } } @@ -85,25 +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 non shader visible value. - /// The resulting value. - public void Rent( - out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, - 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 D3D12DescriptorHandleBundle d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++]; - - d3D12CpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandle; - d3D12CpuDescriptorHandleNonShaderVisible = d3D12DescriptorHandlePair.D3D12CpuDescriptorHandleNonShaderVisible; - d3D12GpuDescriptorHandle = d3D12DescriptorHandlePair.D3D12GpuDescriptorhandle; + d3D12ResourceDescriptorHandles = this.d3D12DescriptorHandlePairs[this.head++]; if (this.head == DescriptorsPerHeap) { @@ -117,19 +110,14 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable /// /// Returns a CPU and GPU handle pair for later use. /// - /// The returned value. - /// The returned non shader visible value. - /// The returned value. - public void Return( - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, - 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 D3D12DescriptorHandleBundle(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonShaderVisible, d3D12GpuDescriptorHandle); + this.d3D12DescriptorHandlePairs[this.tail++] = d3D12ResourceDescriptorHandles; if (this.tail == DescriptorsPerHeap) { @@ -145,61 +133,4 @@ internal unsafe struct ID3D12DescriptorHandleAllocator : IDisposable { this.d3D12DescriptorHeap.Dispose(); } - - /// - /// A type representing a bundle of reusable descriptor handles. - /// - private readonly struct D3D12DescriptorHandleBundle - { - /// - /// The value for the current entry. - /// - public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle; - - /// - /// The value for the current entry, non shader visible. - /// - public readonly D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible; - - /// - /// 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 non shader visible value to wrap. - /// The value to wrap. - public D3D12DescriptorHandleBundle( - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) - { - D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle; - D3D12CpuDescriptorHandleNonShaderVisible = d3D12CpuDescriptorHandleNonShaderVisible; - D3D12GpuDescriptorhandle = d3D12GpuDescriptorHandle; - } - - /// - /// Creates a new instance with the given parameters. - /// - /// The base value. - /// The base non shader visible value. - /// The base value. - /// The offset for the new pair of handles to compute. - /// The increment size for each consecutive handles pair. - public D3D12DescriptorHandleBundle( - in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart, - in D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, - in D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart, - int offset, - uint descriptorIncrementSize) - { - D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, offset, descriptorIncrementSize); - D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12CpuDescriptorHandleNonShaderVisible, in d3D12CpuDescriptorHandleNonShaderVisible, 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/GraphicsDevice.cs b/src/ComputeSharp/Graphics/GraphicsDevice.cs index 1c7b4e7c..fd87c436 100644 --- a/src/ComputeSharp/Graphics/GraphicsDevice.cs +++ b/src/ComputeSharp/Graphics/GraphicsDevice.cs @@ -284,22 +284,16 @@ public sealed unsafe partial class GraphicsDevice : NativeObject /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void RentShaderResourceViewDescriptorHandles( - out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonGpuVisible, - out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + internal void RentShaderResourceViewDescriptorHandles(out ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { - this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12CpuDescriptorHandleNonGpuVisible, out d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Rent(out d3D12ResourceDescriptorHandles); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal void ReturnShaderResourceViewDescriptorHandles( - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, - D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonGpuVisible, - D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle) + internal void ReturnShaderResourceViewDescriptorHandles(in ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles) { - this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12CpuDescriptorHandleNonGpuVisible, d3D12GpuDescriptorHandle); + this.shaderResourceViewDescriptorAllocator.Return(in d3D12ResourceDescriptorHandles); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Buffer{T}.cs index 40b7db1a..9c815e7f 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; @@ -38,19 +39,9 @@ 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; - - /// - /// The non shader visible instance for the current resource. - /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; - - /// - /// The instance for the current resource. - /// - private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The size in bytes of the current buffer (this value is never negative). @@ -100,19 +91,19 @@ public unsafe abstract class Buffer : NativeObject #endif device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.Constant: - device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12CpuDescriptorHandle); + device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); break; case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.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, this.d3D12CpuDescriptorHandle); - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12CpuDescriptorHandleNonShaderVisible); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle); + device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); break; } @@ -146,17 +137,7 @@ public unsafe abstract class Buffer : NativeObject /// /// Gets the instance for the current resource. /// - internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12GpuDescriptorHandle; - - /// - /// Gets the instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; - - /// - /// Gets the non shader visible instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + 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. @@ -219,7 +200,7 @@ public unsafe abstract class Buffer : NativeObject if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -234,4 +215,22 @@ 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.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.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 c0c96b40..5b30dad3 100644 --- a/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs +++ b/src/ComputeSharp/Graphics/Resources/Abstract/StructuredBuffer{T}.cs @@ -304,22 +304,4 @@ public abstract class StructuredBuffer : Buffer copyCommandList.ExecuteAndWaitForCompletion(); } } - - /// - internal (D3D12_GPU_DESCRIPTOR_HANDLE Gpu, D3D12_CPU_DESCRIPTOR_HANDLE Cpu) ValidateAndGetGpuAndCpuDescriptorHandlesForClear(GraphicsDevice device) - { - ThrowIfDisposed(); - ThrowIfDeviceMismatch(device); - - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); - } - - /// - 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 c8e0f27f..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,19 +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 non shader visible instance for the current resource. - /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; - - /// - /// The instance for the current resource. - /// - private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The default value for the current resource. @@ -125,16 +116,16 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE2D, this.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, this.d3D12CpuDescriptorHandle); - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE2D, this.d3D12CpuDescriptorHandleNonShaderVisible); + 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; } @@ -164,17 +155,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper /// /// Gets the instance for the current resource. /// - internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12GpuDescriptorHandle; - - /// - /// Gets the instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; - - /// - /// Gets the non shader visible instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + 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. @@ -557,7 +538,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -581,7 +562,7 @@ public unsafe abstract class Texture2D : NativeObject, GraphicsResourceHelper isNormalized = DXGIFormatHelper.IsNormalizedType(); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } /// diff --git a/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs b/src/ComputeSharp/Graphics/Resources/Abstract/Texture3D{T}.cs index ab71fe6e..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,19 +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 non shader visible instance for the current resource. - /// - private readonly D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible; - - /// - /// The instance for the current resource. - /// - private readonly D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle; + private readonly ID3D12ResourceDescriptorHandles d3D12ResourceDescriptorHandles; /// /// The default value for the current resource. @@ -130,16 +121,16 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper out _); device.RegisterAllocatedResource(); - device.RentShaderResourceViewDescriptorHandles(out this.d3D12CpuDescriptorHandle, out this.d3D12CpuDescriptorHandleNonShaderVisible, out this.d3D12GpuDescriptorHandle); + device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles); switch (resourceType) { case ResourceType.ReadOnly: - device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_SRV_DIMENSION_TEXTURE3D, this.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, this.d3D12CpuDescriptorHandle); - device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), DXGIFormatHelper.GetForType(), D3D12_UAV_DIMENSION_TEXTURE3D, this.d3D12CpuDescriptorHandleNonShaderVisible); + 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; } @@ -174,17 +165,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper /// /// Gets the instance for the current resource. /// - internal D3D12_GPU_DESCRIPTOR_HANDLE D3D12GpuDescriptorHandle => this.d3D12GpuDescriptorHandle; - - /// - /// Gets the instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandle => this.d3D12CpuDescriptorHandle; - - /// - /// Gets the non shader visible instance for the current resource. - /// - internal D3D12_CPU_DESCRIPTOR_HANDLE D3D12CpuDescriptorHandleNonShaderVisible => this.d3D12CpuDescriptorHandleNonShaderVisible; + 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. @@ -610,7 +591,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper if (GraphicsDevice is GraphicsDevice device) { device.UnregisterAllocatedResource(); - device.ReturnShaderResourceViewDescriptorHandles(D3D12CpuDescriptorHandle, this.d3D12CpuDescriptorHandleNonShaderVisible, D3D12GpuDescriptorHandle); + device.ReturnShaderResourceViewDescriptorHandles(in this.d3D12ResourceDescriptorHandles); } } @@ -634,7 +615,7 @@ public unsafe abstract class Texture3D : NativeObject, GraphicsResourceHelper isNormalized = DXGIFormatHelper.IsNormalizedType(); - return (D3D12GpuDescriptorHandle, D3D12CpuDescriptorHandleNonShaderVisible); + return (this.d3D12ResourceDescriptorHandles.D3D12GpuDescriptorHandle, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandleNonShaderVisible); } /// From f7e486eae2dc903964308ed2e0c5679452a25e51 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 29 Dec 2021 17:07:30 +0100 Subject: [PATCH 8/8] Fix ComputeContext.Clear API for ReadWriteBuffer --- .../Interop/ID3D12DeviceExtensions.cs | 31 ++++++++++++ .../Graphics/Resources/Abstract/Buffer{T}.cs | 19 +++++++- .../ComputeSharp.Tests/ComputeContextTests.cs | 48 +++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) 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))]