From 4f872c661ed95d574245279550c516e5c51d6afc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 14 Oct 2023 11:38:47 +0200 Subject: [PATCH] Update PipelineDataLoader to use new descriptor ranges --- .../__Internals/IDispatchMetadataLoader.cs | 20 ------ .../Core/Interfaces/__Internals/IShader.cs | 11 ---- .../ShaderDispatchMetadataLoader.cs | 46 -------------- .../Shaders/Loading/PipelineDataLoader{T}.cs | 61 ++++++++++++++++--- 4 files changed, 54 insertions(+), 84 deletions(-) delete mode 100644 src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs delete mode 100644 src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs deleted file mode 100644 index 0ae91753..00000000 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IDispatchMetadataLoader.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.ComponentModel; - -namespace ComputeSharp.__Internals; - -/// -/// A base representing a metadatadata loader for a shader being dispatched. -/// -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete("This interface is not intended to be used directly by user code")] -public interface IDispatchMetadataLoader -{ - /// - /// Loads an opaque metadata handle from the given shader metadata. - /// - /// The serialized metadata for the current shader. - /// The sequence of resource descriptors for the current shader. - /// The resulting opaque metadata handle. - void LoadMetadataHandle(ReadOnlySpan serializedMetadata, ReadOnlySpan resourceDescriptors, out IntPtr result); -} \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index 2d00eb1b..df79288c 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -72,15 +72,4 @@ public interface IShader [Obsolete("This method is not intended to be called directly by user code")] void LoadDispatchData(ref TLoader loader, GraphicsDevice device, int x, int y, int z) where TLoader : struct, IDispatchDataLoader; - - /// - /// Loads an opaque metadata handle from the metadata of the current shader. - /// - /// The type of data loader being used. - /// The instance to use to load the data. - /// The resulting opaque metadata handle. - [EditorBrowsable(EditorBrowsableState.Never)] - [Obsolete("This method is not intended to be called directly by user code")] - void LoadDispatchMetadata(ref TLoader loader, out IntPtr result) - where TLoader : struct, IDispatchMetadataLoader; } \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs b/src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs deleted file mode 100644 index c43be70d..00000000 --- a/src/ComputeSharp/Shaders/Dispatching/ShaderDispatchMetadataLoader.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using ComputeSharp.Shaders.Extensions; -using ComputeSharp.__Internals; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; - -#pragma warning disable CS0618 - -namespace ComputeSharp.Shaders.Dispatching; - -/// -/// A data loader for compute shaders. -/// -internal readonly unsafe struct ShaderDispatchMetadataLoader : IDispatchMetadataLoader -{ - /// - /// The object in use. - /// - private readonly ID3D12Device* d3D12Device; - - /// - /// Creates a new instance. - /// - /// The object to use. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal ShaderDispatchMetadataLoader(ID3D12Device* d3D12Device) - { - this.d3D12Device = d3D12Device; - } - - /// - public unsafe void LoadMetadataHandle(ReadOnlySpan serializedMetadata, ReadOnlySpan resourceDescriptors, out IntPtr result) - { - default(ArgumentException).ThrowIf(serializedMetadata.Length != 5, nameof(serializedMetadata)); - - fixed (IntPtr* p = &result) - { - *(ComPtr*)p = this.d3D12Device->CreateRootSignature( - MemoryMarshal.Read(serializedMetadata), - MemoryMarshal.Cast(resourceDescriptors), - MemoryMarshal.Read(serializedMetadata.Slice(4))); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs index 59846b79..b9883cf6 100644 --- a/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs +++ b/src/ComputeSharp/Shaders/Loading/PipelineDataLoader{T}.cs @@ -1,10 +1,12 @@ using System; +using System.Buffers; using System.Runtime.CompilerServices; -using ComputeSharp.Shaders.Dispatching; using ComputeSharp.Shaders.Extensions; +using ComputeSharp.Interop; using ComputeSharp.__Internals; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; +using static TerraFX.Interop.DirectX.D3D12_DESCRIPTOR_RANGE_FLAGS; #pragma warning disable CS0618 @@ -14,7 +16,7 @@ namespace ComputeSharp.Shaders.Loading; /// A responsible for performing all the necessary operations to compile and load shader data. /// /// The type of compute shader to load. -internal static class PipelineDataLoader +internal static unsafe class PipelineDataLoader where T : struct, IShader { /// @@ -37,19 +39,16 @@ internal static class PipelineDataLoader /// /// The to use to run the shader. /// The resulting instance to use to run the shader. - [MethodImpl(MethodImplOptions.NoInlining)] - private static unsafe PipelineData CreatePipelineData(GraphicsDevice device) + private static PipelineData CreatePipelineData(GraphicsDevice device) { PipelineData pipelineData; using (ComPtr d3D12RootSignature = default) { - ShaderDispatchMetadataLoader metadataLoader = new(device.D3D12Device); + CreateD3D12RootSignature(device.D3D12Device, d3D12RootSignature.GetAddressOf()); Unsafe.SkipInit(out T shader); - shader.LoadDispatchMetadata(ref metadataLoader, out *(IntPtr*)&d3D12RootSignature); - ReadOnlyMemory hlslBytecode = shader.HlslBytecode; fixed (byte* hlslBytecodePtr = hlslBytecode.Span) @@ -70,4 +69,52 @@ internal static class PipelineDataLoader return pipelineData; } + + /// + /// Creates a D3D12 root signature for the current shader type. + /// + /// The target to use to create the root signature. + /// The allocated instance. + private static void CreateD3D12RootSignature(ID3D12Device* d3D12Device, ID3D12RootSignature** d3D12RootSignature) + { + Unsafe.SkipInit(out T shader); + + ReadOnlySpan resourceDescriptorRanges = shader.ResourceDescriptorRanges.Span; + + D3D12_DESCRIPTOR_RANGE1[]? d3D12DescriptorRangesArray = null; + + // Create the span and optional array of D3D12_DESCRIPTOR_RANGE1 objects + Span d3D12DescriptorRanges = resourceDescriptorRanges.Length <= 8 + ? stackalloc D3D12_DESCRIPTOR_RANGE1[8] + : d3D12DescriptorRangesArray = ArrayPool.Shared.Rent(resourceDescriptorRanges.Length); + + // Ensure the size has the right length to match the available descriptor ranges + d3D12DescriptorRanges = d3D12DescriptorRanges.Slice(0, resourceDescriptorRanges.Length); + + // Initialize the D3D12_DESCRIPTOR_RANGE1 values from the shader descriptor ranges + for (int i = 0; i < resourceDescriptorRanges.Length; i++) + { + ref readonly ResourceDescriptorRange resourceDescriptorRange = ref resourceDescriptorRanges[i]; + ref D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange = ref d3D12DescriptorRanges[i]; + + d3D12DescriptorRange.RangeType = (D3D12_DESCRIPTOR_RANGE_TYPE)resourceDescriptorRange.Type; + d3D12DescriptorRange.NumDescriptors = 1; + d3D12DescriptorRange.BaseShaderRegister = resourceDescriptorRange.Register; + d3D12DescriptorRange.RegisterSpace = 0; + d3D12DescriptorRange.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE; + d3D12DescriptorRange.OffsetInDescriptorsFromTableStart = D3D12.D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND; + } + + // Create the D3D12 root signature + *d3D12RootSignature = d3D12Device->CreateRootSignature( + shader.ConstantBufferSize, + d3D12DescriptorRanges, + shader.IsStaticSamplerRequired).Get(); + + // Return the array to the pool if no exceptions have been thrown + if (d3D12DescriptorRangesArray is not null) + { + ArrayPool.Shared.Return(d3D12DescriptorRangesArray); + } + } } \ No newline at end of file