From c5d4822e9f2efbdff60b18133cb41560a2cca331 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 8 Jan 2022 15:25:37 +0100 Subject: [PATCH] Add ComputeContext.Fill APIs --- .../ID3D12GraphicsCommandListExtensions.cs | 24 ++++ .../Graphics/Helpers/DXGIFormatHelper.cs | 106 +++++++++++++++++- src/ComputeSharp/Shaders/ComputeContext.cs | 30 ++++- .../Extensions/ComputeContextExtensions.cs | 101 ++++++++++++++--- 4 files changed, 241 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs index 56b4548a..ba806042 100644 --- a/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs +++ b/src/ComputeSharp/Graphics/Extensions/Interop/ID3D12GraphicsCommandListExtensions.cs @@ -259,6 +259,30 @@ internal static unsafe class ID3D12GraphicsCommandListExtensions } } + /// + /// Fills a target UAV resource. + /// + /// The instance in use. + /// The to fill. + /// The value for the target resource. + /// The value for the target resource. + /// The value to use to fill the view. + public static unsafe void FillUnorderedAccessView( + this ref ID3D12GraphicsCommandList d3D12GraphicsCommandList, + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + Float4 value) + { + d3D12GraphicsCommandList.ClearUnorderedAccessViewFloat( + ViewGPUHandleInCurrentHeap: d3D12GpuDescriptorHandle, + ViewCPUHandle: d3D12CpuDescriptorHandle, + pResource: d3D12Resource, + Values: (float*)&value, + NumRects: 0, + pRects: null); + } + /// /// Binds an input constant buffer to the first root parameter. /// diff --git a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs index 6fd25e28..e14a142a 100644 --- a/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/DXGIFormatHelper.cs @@ -50,7 +50,7 @@ internal static class DXGIFormatHelper /// 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.. + /// Whether 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() @@ -88,4 +88,108 @@ internal static class DXGIFormatHelper return ThrowHelper.ThrowArgumentException("Invalid texture type"); } + + /// + /// Extends a given pixel type to its equivalent. + /// + /// The input pixel value to convert. + /// The equivalent value for . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Float4 ExtendToNormalizedValue(ref T value) + where T : unmanaged + { + if (typeof(T) == typeof(Float4)) + { + return Unsafe.As(ref value); + } + + Float4 result = default; + + Unsafe.As(ref result) = value; + + return result; + } + + /// + /// Converts a given value to a normalized value. + /// + /// The input value to convert. + /// The normalized value for . + /// Thrown when the input type is not supported. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Float4 ConvertToNormalizedValue(ref T value) + where T : unmanaged + { + if (typeof(T) == typeof(Bgra32)) + { + ref Bgra32 bgra = ref Unsafe.As(ref value); + + Vector4 linear = new(bgra.R, bgra.G, bgra.B, bgra.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rgba32)) + { + ref Rgba32 rgba = ref Unsafe.As(ref value); + + Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rgba64)) + { + ref Rgba64 rgba = ref Unsafe.As(ref value); + + Vector4 linear = new(rgba.R, rgba.G, rgba.B, rgba.A); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(R8)) + { + ref R8 r = ref Unsafe.As(ref value); + + Vector4 linear = new(r.R, 0, 0, 0); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(R16)) + { + ref R16 r = ref Unsafe.As(ref value); + + Vector4 linear = new(r.R, 0, 0, 0); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rg16)) + { + ref Rg16 rg = ref Unsafe.As(ref value); + + Vector4 linear = new(rg.R, rg.G, 0, 0); + Vector4 normalized = linear / byte.MaxValue; + + return normalized; + } + + if (typeof(T) == typeof(Rg32)) + { + ref Rg32 rg = ref Unsafe.As(ref value); + + Vector4 linear = new(rg.R, rg.G, 0, 0); + Vector4 normalized = linear / ushort.MaxValue; + + return normalized; + } + + return ThrowHelper.ThrowArgumentException("Invalid pixel type"); + } } diff --git a/src/ComputeSharp/Shaders/ComputeContext.cs b/src/ComputeSharp/Shaders/ComputeContext.cs index 12e884ad..aa28245a 100644 --- a/src/ComputeSharp/Shaders/ComputeContext.cs +++ b/src/ComputeSharp/Shaders/ComputeContext.cs @@ -52,6 +52,19 @@ public struct ComputeContext : IDisposable, IAsyncDisposable } } + /// + /// Inserts a resource barrier for a specific resource. + /// + /// The to insert the barrier for. + internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource) + { + ThrowInvalidOperationExceptionIfDeviceIsNull(); + + ref CommandList commandList = ref GetCommandList(in this); + + commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); + } + /// /// Clears a specific resource. /// @@ -73,16 +86,23 @@ public struct ComputeContext : IDisposable, IAsyncDisposable } /// - /// Inserts a resource barrier for a specific resource. + /// Fills a specific resource. /// - /// The to insert the barrier for. - internal readonly unsafe void Barrier(ID3D12Resource* d3D12Resource) + /// The to fill. + /// The value for the target resource. + /// The value for the target resource. + /// The value to use to fill the resource. + internal readonly unsafe void Fill( + ID3D12Resource* d3D12Resource, + D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, + D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, + Float4 value) { ThrowInvalidOperationExceptionIfDeviceIsNull(); - ref CommandList commandList = ref GetCommandList(in this); + ref CommandList commandList = ref GetCommandList(in this, null); - commandList.D3D12GraphicsCommandList->UnorderedAccessViewBarrier(d3D12Resource); + commandList.D3D12GraphicsCommandList->FillUnorderedAccessView(d3D12Resource, d3D12GpuDescriptorHandle, d3D12CpuDescriptorHandle, value); } /// diff --git a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs index d7da40f7..131c035d 100644 --- a/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs +++ b/src/ComputeSharp/Shaders/Extensions/ComputeContextExtensions.cs @@ -1,5 +1,6 @@ using System.Runtime.CompilerServices; using ComputeSharp.__Internals; +using ComputeSharp.Graphics.Helpers; #pragma warning disable CS0618 @@ -113,7 +114,7 @@ public static partial class ComputeContextExtensions } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The to use to clear the resource. @@ -127,7 +128,7 @@ public static partial class ComputeContextExtensions } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The to use to clear the resource. @@ -141,7 +142,7 @@ public static partial class ComputeContextExtensions } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The type of pixels used on the GPU side. @@ -151,13 +152,13 @@ public static partial class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true); } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of items stored on the texture. /// The type of pixels used on the GPU side. @@ -167,13 +168,13 @@ public static partial class ComputeContextExtensions where T : unmanaged, IPixel where TPixel : unmanaged { - var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, isNormalized); + context.Clear(texture.D3D12Resource, handles.Gpu, handles.Cpu, true); } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of pixels stored on the texture. /// The to use to clear the resource. @@ -181,13 +182,13 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture2D texture) where TPixel : unmanaged { - var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true); } /// - /// Clears a specific resource, with a specified value. + /// Clears a specific resource. /// /// The type of pixels stored on the texture. /// The to use to clear the resource. @@ -195,9 +196,81 @@ public static partial class ComputeContextExtensions public static unsafe void Clear(this in ComputeContext context, IReadWriteTexture3D texture) where TPixel : unmanaged { - var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out bool isNormalized); + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); - context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, isNormalized); + context.Clear(((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), handles.Gpu, handles.Cpu, true); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, ReadWriteTexture2D texture, T value) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of items stored on the texture. + /// The type of pixels used on the GPU side. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, ReadWriteTexture3D texture, T value) + where T : unmanaged, IPixel + where TPixel : unmanaged + { + var handles = texture.ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill(texture.D3D12Resource, handles.Gpu, handles.Cpu, DXGIFormatHelper.ConvertToNormalizedValue(ref value)); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of pixels stored on the texture. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, IReadWriteTexture2D texture, TPixel value) + where TPixel : unmanaged + { + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill( + ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), + handles.Gpu, + handles.Cpu, + DXGIFormatHelper.ExtendToNormalizedValue(ref value)); + } + + /// + /// Fills a specific texture with a given value. + /// + /// The type of pixels stored on the texture. + /// The to use to fill the resource. + /// The input instance to fill. + /// The value to use to fill . + public static unsafe void Fill(this in ComputeContext context, IReadWriteTexture3D texture, TPixel value) + where TPixel : unmanaged + { + var handles = ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetGpuAndCpuDescriptorHandlesForClear(context.GraphicsDevice, out _); + + context.Fill( + ((GraphicsResourceHelper.IGraphicsResource)texture).ValidateAndGetID3D12Resource(context.GraphicsDevice), + handles.Gpu, + handles.Cpu, + DXGIFormatHelper.ExtendToNormalizedValue(ref value)); } ///