Update PipelineDataLoader<T> to use new descriptor ranges

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-14 11:38:47 +02:00
1 親 4f53b3e686
コミット 4f872c661e
4ファイル変更
+54 -84

検索条件に一致するファイルはありません

-20
ファイルの表示
@@ -1,20 +0,0 @@
using System;
using System.ComponentModel;
namespace ComputeSharp.__Internals;
/// <summary>
/// A base <see langword="interface"/> representing a metadatadata loader for a shader being dispatched.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This interface is not intended to be used directly by user code")]
public interface IDispatchMetadataLoader
{
/// <summary>
/// Loads an opaque metadata handle from the given shader metadata.
/// </summary>
/// <param name="serializedMetadata">The serialized metadata for the current shader.</param>
/// <param name="resourceDescriptors">The sequence of resource descriptors for the current shader.</param>
/// <param name="result">The resulting opaque metadata handle.</param>
void LoadMetadataHandle(ReadOnlySpan<byte> serializedMetadata, ReadOnlySpan<ResourceDescriptorRange> resourceDescriptors, out IntPtr result);
}
-11
ファイルの表示
@@ -72,15 +72,4 @@ public interface IShader
[Obsolete("This method is not intended to be called directly by user code")]
void LoadDispatchData<TLoader>(ref TLoader loader, GraphicsDevice device, int x, int y, int z)
where TLoader : struct, IDispatchDataLoader;
/// <summary>
/// Loads an opaque metadata handle from the metadata of the current shader.
/// </summary>
/// <typeparam name="TLoader">The type of data loader being used.</typeparam>
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the data.</param>
/// <param name="result">The resulting opaque metadata handle.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be called directly by user code")]
void LoadDispatchMetadata<TLoader>(ref TLoader loader, out IntPtr result)
where TLoader : struct, IDispatchMetadataLoader;
}
-46
ファイルの表示
@@ -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;
/// <summary>
/// A data loader for compute shaders.
/// </summary>
internal readonly unsafe struct ShaderDispatchMetadataLoader : IDispatchMetadataLoader
{
/// <summary>
/// The <see cref="ID3D12Device"/> object in use.
/// </summary>
private readonly ID3D12Device* d3D12Device;
/// <summary>
/// Creates a new <see cref="ShaderDispatchMetadataLoader"/> instance.
/// </summary>
/// <param name="d3D12Device">The <see cref="ID3D12Device"/> object to use.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ShaderDispatchMetadataLoader(ID3D12Device* d3D12Device)
{
this.d3D12Device = d3D12Device;
}
/// <inheritdoc/>
public unsafe void LoadMetadataHandle(ReadOnlySpan<byte> serializedMetadata, ReadOnlySpan<ResourceDescriptorRange> resourceDescriptors, out IntPtr result)
{
default(ArgumentException).ThrowIf(serializedMetadata.Length != 5, nameof(serializedMetadata));
fixed (IntPtr* p = &result)
{
*(ComPtr<ID3D12RootSignature>*)p = this.d3D12Device->CreateRootSignature(
MemoryMarshal.Read<int>(serializedMetadata),
MemoryMarshal.Cast<ResourceDescriptorRange, D3D12_DESCRIPTOR_RANGE1>(resourceDescriptors),
MemoryMarshal.Read<bool>(serializedMetadata.Slice(4)));
}
}
}
+54 -7
ファイルの表示
@@ -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 <see langword="class"/> responsible for performing all the necessary operations to compile and load shader data.
/// </summary>
/// <typeparam name="T">The type of compute shader to load.</typeparam>
internal static class PipelineDataLoader<T>
internal static unsafe class PipelineDataLoader<T>
where T : struct, IShader
{
/// <summary>
@@ -37,19 +39,16 @@ internal static class PipelineDataLoader<T>
/// </summary>
/// <param name="device">The <see cref="GraphicsDevice"/> to use to run the shader.</param>
/// <returns>The resulting <see cref="PipelineData"/> instance to use to run the shader.</returns>
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe PipelineData CreatePipelineData(GraphicsDevice device)
private static PipelineData CreatePipelineData(GraphicsDevice device)
{
PipelineData pipelineData;
using (ComPtr<ID3D12RootSignature> 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<byte> hlslBytecode = shader.HlslBytecode;
fixed (byte* hlslBytecodePtr = hlslBytecode.Span)
@@ -70,4 +69,52 @@ internal static class PipelineDataLoader<T>
return pipelineData;
}
/// <summary>
/// Creates a D3D12 root signature for the current shader type.
/// </summary>
/// <param name="d3D12Device">The target <see cref="ID3D12Device"/> to use to create the root signature.</param>
/// <param name="d3D12RootSignature">The allocated <see cref="ID3D12RootSignature"/> instance.</param>
private static void CreateD3D12RootSignature(ID3D12Device* d3D12Device, ID3D12RootSignature** d3D12RootSignature)
{
Unsafe.SkipInit(out T shader);
ReadOnlySpan<ResourceDescriptorRange> resourceDescriptorRanges = shader.ResourceDescriptorRanges.Span;
D3D12_DESCRIPTOR_RANGE1[]? d3D12DescriptorRangesArray = null;
// Create the span and optional array of D3D12_DESCRIPTOR_RANGE1 objects
Span<D3D12_DESCRIPTOR_RANGE1> d3D12DescriptorRanges = resourceDescriptorRanges.Length <= 8
? stackalloc D3D12_DESCRIPTOR_RANGE1[8]
: d3D12DescriptorRangesArray = ArrayPool<D3D12_DESCRIPTOR_RANGE1>.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<D3D12_DESCRIPTOR_RANGE1>.Shared.Return(d3D12DescriptorRangesArray);
}
}
}