diff --git a/src/ComputeSharp.Graphics/Commands/CommandList.cs b/src/ComputeSharp.Graphics/Commands/CommandList.cs index 58bd809a..3514e5d4 100644 --- a/src/ComputeSharp.Graphics/Commands/CommandList.cs +++ b/src/ComputeSharp.Graphics/Commands/CommandList.cs @@ -154,6 +154,18 @@ namespace ComputeSharp.Graphics.Commands this.d3D12GraphicsCommandList.Get()->ResourceBarrier(1, &d3D12ResourceBarrier); } + /// + /// Binds an input constant buffer to the first root parameter. + /// + /// The input buffer with the constant data to bind. + public readonly unsafe void SetComputeRoot32BitConstants(ReadOnlySpan data) + { + fixed (uint* p = data) + { + this.d3D12GraphicsCommandList.Get()->SetComputeRoot32BitConstants(0, (uint)data.Length, p, 0); + } + } + /// /// Binds an input value to a specified root parameter. /// diff --git a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs index 88f88aac..da70278e 100644 --- a/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs +++ b/src/ComputeSharp.Shaders/Extensions/ID3D12DeviceExtensions.cs @@ -28,18 +28,22 @@ namespace ComputeSharp.Shaders.Extensions fixed (D3D12_DESCRIPTOR_RANGE1* d3D12DescriptorRange1 = d3D12DescriptorRanges1) { - D3D12_ROOT_PARAMETER1* d3D12RootParameters1 = stackalloc D3D12_ROOT_PARAMETER1[d3D12DescriptorRanges1.Length]; + D3D12_ROOT_PARAMETER1* d3D12RootParameters1 = stackalloc D3D12_ROOT_PARAMETER1[d3D12DescriptorRanges1.Length + 1]; + + // The initial constant buffer is initialized with 32 bit root constants. + // This avoids having to manage an extra buffer for each shader dispatch. + D3D12_ROOT_PARAMETER1.InitAsConstants(out *d3D12RootParameters1, 3, 0); // TODO: fix length // Pack each descriptor range into a root parameter with that single range as content for (int i = 0; i < d3D12DescriptorRanges1.Length; i++) { - D3D12_ROOT_PARAMETER1.InitAsDescriptorTable(out d3D12RootParameters1[i], 1, d3D12DescriptorRange1 + i); + D3D12_ROOT_PARAMETER1.InitAsDescriptorTable(out d3D12RootParameters1[i + 1], 1, d3D12DescriptorRange1 + i); } // Root signature description wrapping the packed collection of root parameters D3D12_VERSIONED_ROOT_SIGNATURE_DESC.Init_1_1( out D3D12_VERSIONED_ROOT_SIGNATURE_DESC d3D12VersionedRootSignatureDescription, - (uint)d3D12DescriptorRanges1.Length, + (uint)d3D12DescriptorRanges1.Length + 1, d3D12RootParameters1); // Serialize the root signature from the data just computed. When this is done diff --git a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs index 57accca0..891b1d50 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner{T}.cs @@ -15,32 +15,6 @@ using static TerraFX.Interop.D3D12_COMMAND_LIST_TYPE; namespace ComputeSharp.Shaders { - /// - /// A with helper methods to support . - /// This type is primarily needed to avoid having fields being repeated per generic instantiation. - /// - internal static class ShaderRunner - { - /// - /// A thread-safe map of reusable GPU buffers for captured locals. - /// - [ThreadStatic] - private static ConditionalWeakTable>? variablesBuffers; - - /// - /// Gets the reusable instance for a shader invocation. - /// - /// The to use to run the shader. - /// The reusable instance to populate. - [Pure] - public static ConstantBuffer GetVariablesBuffer(GraphicsDevice device) - { - ConditionalWeakTable> map = variablesBuffers ??= new(); - - return map.GetValue(device, static gpu => new(gpu, 256, AllocationMode.Default)); - } - } - /// /// A responsible for performing all the necessary operations to compile and run a compute shader. /// @@ -126,22 +100,17 @@ namespace ComputeSharp.Shaders // Extract the dispatch data for the shader invocation using DispatchData dispatchData = shaderData.Loader.GetDispatchData(device, in shader, x, y, z); - // Load the captured buffers + // Initialize the loop targets and the captured values + commandList.SetComputeRoot32BitConstants(dispatchData.Variables); + ReadOnlySpan resources = dispatchData.Resources; for (int i = 0; i < resources.Length; i++) { + // Load the captured buffers commandList.SetComputeRootDescriptorTable(i + 1, resources[i]); } - // Initialize the loop targets and the captured values - ConstantBuffer variablesBuffer = ShaderRunner.GetVariablesBuffer(device); - ReadOnlySpan variables = dispatchData.Variables; - - variablesBuffer.SetData(ref MemoryMarshal.GetReference(variables), variables.Length, 0); - - commandList.SetComputeRootDescriptorTable(0, variablesBuffer.D3D12GpuDescriptorHandle); - // Calculate the dispatch values int groupsX = Math.DivRem(x, threadsX, out int modX) + (modX == 0 ? 0 : 1), diff --git a/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs index fac76157..c47050ba 100644 --- a/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs +++ b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs @@ -70,21 +70,21 @@ namespace ComputeSharp.Shaders.Translation.Models /// /// Gets a with the padded data representing all the captured variables. /// - public unsafe ReadOnlySpan Variables + public unsafe ReadOnlySpan Variables { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { #if NET5_0 ref byte r0 = ref MemoryMarshal.GetArrayDataReference(this.variablesArray); - ref Int4 r1 = ref Unsafe.As(ref r0); - int length = (int)((uint)this.variablesByteSize / (uint)sizeof(Int4)); + ref uint r1 = ref Unsafe.As(ref r0); + int length = (int)((uint)this.variablesByteSize / sizeof(uint)); return MemoryMarshal.CreateReadOnlySpan(ref r1, length); #else Span span = this.variablesArray.AsSpan(0, this.variablesByteSize); - return MemoryMarshal.Cast(span); + return MemoryMarshal.Cast(span); #endif } } diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs index db6b8bef..11d015ef 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.DispatchData.cs @@ -58,7 +58,7 @@ namespace ComputeSharp.Shaders.Translation { // Resources and variables buffers ulong[] resources = ArrayPool.Shared.Rent(this.totalResourceCount); - byte[] variables = ArrayPool.Shared.Rent(4096); + byte[] variables = ArrayPool.Shared.Rent(256); ref ulong r0 = ref MemoryMarshal.GetArrayDataReference(resources); ref byte r1 = ref MemoryMarshal.GetArrayDataReference(variables); diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs index 1d6f6257..22422117 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs @@ -164,11 +164,6 @@ namespace ComputeSharp.Shaders.Translation List d3D12DescriptorRanges1 = new(); - // Descriptor for the buffer for captured scalar/vector variables - D3D12_DESCRIPTOR_RANGE1 d3D12DescriptorRange1 = new(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, this.constantBuffersCount++); - - d3D12DescriptorRanges1.Add(d3D12DescriptorRange1); - // Inspect the captured fields foreach (FieldInfo fieldInfo in shaderFields) { diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs index 834fb2d9..97a13f72 100644 --- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs @@ -163,12 +163,6 @@ namespace ComputeSharp.SourceGenerators } } - // After all the captured fields have been processed, ansure the reported byte size for - // the local variables is padded to the standard alignment for constant buffers. This is - // necessary to enable loading all the dispatch data after reinterpreting it to a sequence - // of values of size 16 bytes (in particular, Int4 is used), without reading out of bounds. - rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16); - yield return ReturnStatement(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(rawDataOffset))); } }