Code refactoring to the DispatchData struct

このコミットが含まれているのは:
Sergio Pedri committed 2019-09-19 03:09:16 +02:00
1 親 85cce1db3c
コミット d91c2be519
3ファイル変更
+85 -44

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

+1 -1
ファイルの表示
@@ -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<GraphicsResource> resources = dispatchData.Resources;
+82
ファイルの表示
@@ -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
{
/// <summary>
/// A <see langword="struct"/> that contains all the captured data to dispatch a shader
/// </summary>
internal readonly ref struct DispatchData
{
/// <summary>
/// The <see cref="GraphicsResource"/> array with the captured buffers
/// </summary>
private readonly GraphicsResource[] ResourcesArray;
/// <summary>
/// The number of <see cref="GraphicsResource"/> instances in <see cref="ResourcesArray"/>
/// </summary>
private readonly int ResourcesCount;
/// <summary>
/// The <see cref="byte"/> array with all the captured variables, with proper padding
/// </summary>
private readonly byte[] VariablesArray;
/// <summary>
/// The actual size in bytes to use from <see cref="VariablesArray"/>
/// </summary>
private readonly int VariablesByteSize;
/// <summary>
/// Creates a new <see cref="DispatchData"/> instance with the specified parameters
/// </summary>
/// <param name="resourcesArray">The <see cref="GraphicsResource"/> array with the captured buffers</param>
/// <param name="resourcesCount">The number of <see cref="GraphicsResource"/> instances in <see cref="ResourcesArray"/></param>
/// <param name="variablesArray">The <see cref="byte"/> array with all the captured variables, with proper padding</param>
/// <param name="variablesByteSize">The actual size in bytes to use from <see cref="VariablesArray"/></param>
public DispatchData(GraphicsResource[] resourcesArray, int resourcesCount, byte[] variablesArray, int variablesByteSize)
{
ResourcesArray = resourcesArray;
VariablesArray = variablesArray;
ResourcesCount = resourcesCount;
VariablesByteSize = variablesByteSize;
}
/// <summary>
/// Gets a <see cref="Span{T}"/> with all the captured buffers
/// </summary>
public Span<GraphicsResource> Resources
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ResourcesArray.AsSpan(0, ResourcesCount);
}
/// <summary>
/// Gets a <see cref="Span{T}"/> with the padded data representing all the captured variables
/// </summary>
public Span<Int4> Variables
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ref Int4 r = ref Unsafe.As<byte, Int4>(ref VariablesArray[0]);
bool mod = (VariablesByteSize & 15) > 0;
int length = VariablesByteSize / 4 + Unsafe.As<bool, byte>(ref mod);
return MemoryMarshal.CreateSpan(ref r, length);
}
}
/// <summary>
/// Implements the <see cref="IDisposable.Dispose"/> method and returns the rented buffers
/// </summary>
public void Dispose()
{
ArrayPool<GraphicsResource>.Shared.Return(ResourcesArray);
ArrayPool<byte>.Shared.Return(VariablesArray);
}
}
}
+2 -43
ファイルの表示
@@ -35,7 +35,7 @@ namespace ComputeSharp.Shaders.Translation
private MembersLoader? _MembersLoader;
[Pure]
public ShaderDispatchData GetDispatchData(Action<ThreadIds> action, int x, int y, int z)
public DispatchData GetDispatchData(Action<ThreadIds> 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<GraphicsResource> Resources
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ResourcesArray.AsSpan(0, ResourcesCount);
}
public Span<Int4> Variables
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ref Int4 r = ref Unsafe.As<byte, Int4>(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<GraphicsResource>.Shared.Return(ResourcesArray);
ArrayPool<byte>.Shared.Return(VariablesArray);
}
return new DispatchData(resources, _ResourcesCount, variables, _VariablesByteSize);
}
}
}