From d91c2be519174d9a117bf55f9e1e33caff9604f0 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 19 Sep 2019 03:09:16 +0200 Subject: [PATCH] Code refactoring to the DispatchData struct --- src/ComputeSharp.Shaders/ShaderRunner.cs | 2 +- .../Translation/Models/DispatchData.cs | 82 +++++++++++++++++++ .../Translation/ShaderLoader.IL.cs | 45 +--------- 3 files changed, 85 insertions(+), 44 deletions(-) create mode 100644 src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs diff --git a/src/ComputeSharp.Shaders/ShaderRunner.cs b/src/ComputeSharp.Shaders/ShaderRunner.cs index 944a72cd..f96dac8c 100644 --- a/src/ComputeSharp.Shaders/ShaderRunner.cs +++ b/src/ComputeSharp.Shaders/ShaderRunner.cs @@ -104,7 +104,7 @@ namespace ComputeSharp.Shaders commandList.SetPipelineState(pipelineState); // Extract the dispatch data for the shader invocation - using var dispatchData = shaderData.Loader.GetDispatchData(action, x, y, z); + using DispatchData dispatchData = shaderData.Loader.GetDispatchData(action, x, y, z); // Load the captured buffers Span resources = dispatchData.Resources; diff --git a/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs new file mode 100644 index 00000000..e1d579f1 --- /dev/null +++ b/src/ComputeSharp.Shaders/Translation/Models/DispatchData.cs @@ -0,0 +1,82 @@ +using System; +using System.Buffers; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using ComputeSharp.Graphics.Buffers.Abstract; + +namespace ComputeSharp.Shaders.Translation.Models +{ + /// + /// A that contains all the captured data to dispatch a shader + /// + internal readonly ref struct DispatchData + { + /// + /// The array with the captured buffers + /// + private readonly GraphicsResource[] ResourcesArray; + + /// + /// The number of instances in + /// + private readonly int ResourcesCount; + + /// + /// The array with all the captured variables, with proper padding + /// + private readonly byte[] VariablesArray; + + /// + /// The actual size in bytes to use from + /// + private readonly int VariablesByteSize; + + /// + /// Creates a new instance with the specified parameters + /// + /// The array with the captured buffers + /// The number of instances in + /// The array with all the captured variables, with proper padding + /// The actual size in bytes to use from + public DispatchData(GraphicsResource[] resourcesArray, int resourcesCount, byte[] variablesArray, int variablesByteSize) + { + ResourcesArray = resourcesArray; + VariablesArray = variablesArray; + ResourcesCount = resourcesCount; + VariablesByteSize = variablesByteSize; + } + + /// + /// Gets a with all the captured buffers + /// + public Span Resources + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => ResourcesArray.AsSpan(0, ResourcesCount); + } + + /// + /// Gets a with the padded data representing all the captured variables + /// + public Span Variables + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ref Int4 r = ref Unsafe.As(ref VariablesArray[0]); + bool mod = (VariablesByteSize & 15) > 0; + int length = VariablesByteSize / 4 + Unsafe.As(ref mod); + return MemoryMarshal.CreateSpan(ref r, length); + } + } + + /// + /// Implements the method and returns the rented buffers + /// + public void Dispose() + { + ArrayPool.Shared.Return(ResourcesArray); + ArrayPool.Shared.Return(VariablesArray); + } + } +} diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.IL.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.IL.cs index 1c8c32d8..49f225aa 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.IL.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.IL.cs @@ -35,7 +35,7 @@ namespace ComputeSharp.Shaders.Translation private MembersLoader? _MembersLoader; [Pure] - public ShaderDispatchData GetDispatchData(Action action, int x, int y, int z) + public DispatchData GetDispatchData(Action action, int x, int y, int z) { if (_MembersLoader == null) { @@ -150,48 +150,7 @@ namespace ComputeSharp.Shaders.Translation // Invoke the dynamic method to extract the captured data _MembersLoader(action.Target, ref r0, ref r1); - return new ShaderDispatchData(resources, _ResourcesCount, variables, _VariablesByteSize); - } - - public readonly ref struct ShaderDispatchData - { - private readonly GraphicsResource[] ResourcesArray; - - private readonly byte[] VariablesArray; - - private readonly int ResourcesCount; - - private readonly int VariablesInt4Size; - - public Span Resources - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => ResourcesArray.AsSpan(0, ResourcesCount); - } - - public Span Variables - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - ref Int4 r = ref Unsafe.As(ref VariablesArray[0]); - return MemoryMarshal.CreateSpan(ref r, VariablesInt4Size); - } - } - - public ShaderDispatchData(GraphicsResource[] resourcesArray, int resourcesCount, byte[] variablesArray, int variablesInt4Size) - { - ResourcesArray = resourcesArray; - VariablesArray = variablesArray; - ResourcesCount = resourcesCount; - VariablesInt4Size = variablesInt4Size; - } - - public void Dispose() - { - ArrayPool.Shared.Return(ResourcesArray); - ArrayPool.Shared.Return(VariablesArray); - } + return new DispatchData(resources, _ResourcesCount, variables, _VariablesByteSize); } } }