Merge pull request #307 from Sergio0694/dev/span-get-constant-buffer

Add zero-alloc D2D1PixelShader.GetConstantBuffer APIs
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2022-06-17 16:40:04 +02:00
コミット f5d6136b65
6ファイル変更
+339 -2

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

+51
ファイルの表示
@@ -263,6 +263,57 @@ public static class D2D1PixelShader
return dataLoader.GetResultingDispatchData();
}
/// <summary>
/// Gets the size of the constant buffer for a D2D1 pixel shader of a specified type.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to retrieve info for.</typeparam>
/// <returns>The size of the constant buffer for a D2D1 pixel shader of type <typeparamref name="T"/>.</returns>
public static int GetConstantBufferSize<T>()
where T : unmanaged, ID2D1PixelShader
{
return D2D1BufferSizeDispatchDataLoader.For<T>.ConstantBufferSize;
}
/// <summary>
/// Gets the constant buffer from an input D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to retrieve info for.</typeparam>
/// <param name="shader">The input D2D1 pixel shader to retrieve info for.</param>
/// <param name="span">The target <see cref="Span{T}"/> to write the constant buffer to.</param>
/// <returns>The number of bytes written into <paramref name="span"/>.</returns>
/// <exception cref="ArgumentException">Thrown if <paramref name="span"/> is not large enough to contain the constant buffer.</exception>
public static int GetConstantBuffer<T>(in T shader, Span<byte> span)
where T : unmanaged, ID2D1PixelShader
{
if (!TryGetConstantBuffer(in shader, span, out int writtenBytes))
{
ThrowHelper.ThrowArgumentException(nameof(span), "The destination span is too short.");
}
return writtenBytes;
}
/// <summary>
/// Tries to get the constant buffer from an input D2D1 pixel shader.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to retrieve info for.</typeparam>
/// <param name="shader">The input D2D1 pixel shader to retrieve info for.</param>
/// <param name="span">The target <see cref="Span{T}"/> to write the constant buffer to.</param>
/// <param name="bytesWritten">The number of bytes written into <paramref name="span"/>.</param>
/// <returns>Whether or not the constant buffer was retrieved successfully.</returns>
public static unsafe bool TryGetConstantBuffer<T>(in T shader, Span<byte> span, out int bytesWritten)
where T : unmanaged, ID2D1PixelShader
{
fixed (byte* buffer = span)
{
D2D1ByteBufferDispatchDataLoader dataLoader = new(buffer, span.Length);
Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader);
return dataLoader.TryGetWrittenBytes(out bytesWritten);
}
}
/// <summary>
/// Sets the constant buffer from an input D2D1 pixel shader, by calling <c>ID2D1DrawInfo::SetPixelShaderConstantBuffer</c>.
/// </summary>
+63
ファイルの表示
@@ -0,0 +1,63 @@
using System;
using System.Runtime.CompilerServices;
using ComputeSharp.D2D1.__Internals;
using TerraFX.Interop.DirectX;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Loaders;
/// <summary>
/// A data loader for D2D1 pixel shaders dispatched via <see cref="ID2D1DrawInfo"/>, to just retrieve the constant buffer size.
/// </summary>
internal unsafe struct D2D1BufferSizeDispatchDataLoader : ID2D1DispatchDataLoader
{
/// <summary>
/// The size of the constant buffer.
/// </summary>
private int size;
/// <summary>
/// Gets the size of the constant buffer.
/// </summary>
/// <returns>The size of the constant buffer.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetConstantBufferSize()
{
return this.size;
}
/// <inheritdoc/>
void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan<uint> data)
{
this.size = data.Length * sizeof(uint);
}
/// <summary>
/// A helper class caching computed constant buffer sizes for generic shaders.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to retrieve info for.</typeparam>
public static class For<T>
where T : unmanaged, ID2D1PixelShader
{
/// <summary>
/// Gets the constant buffer size for a shader of type <typeparamref name="T"/>.
/// </summary>
public static int ConstantBufferSize { get; } = GetConstantBufferSize();
/// <summary>
/// Calculates the constant buffer size for a shader of type <typeparamref name="T"/>.
/// </summary>
/// <returns>The constant buffer size for a shader of type <typeparamref name="T"/>.</returns>
private static int GetConstantBufferSize()
{
D2D1BufferSizeDispatchDataLoader dataLoader = default;
Unsafe.SkipInit(out T shader);
shader.LoadDispatchData(ref dataLoader);
return dataLoader.GetConstantBufferSize();
}
}
}
+1 -1
ファイルの表示
@@ -21,7 +21,7 @@ internal struct D2D1ByteArrayDispatchDataLoader : ID2D1DispatchDataLoader
/// <summary>
/// Gets the resulting pixel shader constant buffer.
/// </summary>
/// <remarks>A <see cref="byte"/> array with the constant buffer for the current shader.</remarks>
/// <returns>A <see cref="byte"/> array with the constant buffer for the current shader.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte[] GetResultingDispatchData()
{
+1 -1
ファイルの表示
@@ -22,7 +22,7 @@ internal struct D2D1ByteArrayInputDescriptionsLoader : ID2D1InputDescriptionsLoa
/// <summary>
/// Gets the resulting input descriptions.
/// </summary>
/// <remarks>A <see cref="D2D1InputDescription"/> array with the available input descriptions.</remarks>
/// <returns>A <see cref="D2D1InputDescription"/> array with the available input descriptions.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public D2D1InputDescription[] GetResultingInputDescriptions()
{
+65
ファイルの表示
@@ -0,0 +1,65 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.__Internals;
using TerraFX.Interop.DirectX;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Shaders.Loaders;
/// <summary>
/// A data loader for D2D1 pixel shaders dispatched via <see cref="ID2D1DrawInfo"/>.
/// </summary>
internal unsafe struct D2D1ByteBufferDispatchDataLoader : ID2D1DispatchDataLoader
{
/// <summary>
/// The target buffer to write data to.
/// </summary>
private readonly byte* buffer;
/// <summary>
/// The length of the buffer pointed to by <see cref="buffer"/>.
/// </summary>
private readonly int length;
/// <summary>
/// The number of written bytes.
/// </summary>
private int writtenBytes;
/// <summary>
/// Creates a new <see cref="D2D1ByteBufferDispatchDataLoader"/> instance.
/// </summary>
/// <param name="buffer">The target buffer to write data to.</param>
/// <param name="length">The length of the buffer pointed to by <see cref="buffer"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal D2D1ByteBufferDispatchDataLoader(byte* buffer, int length)
{
this.buffer = buffer;
this.length = length;
this.writtenBytes = 0;
}
/// <summary>
/// Gets the number of written bytes, or <c>0</c> in case of failure.
/// </summary>
/// <param name="writtenBytes">The number of written bytes, of <c>0</c> in case of failure.</param>
/// <returns>Whether or not the constant buffer was retrieved successfully.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetWrittenBytes(out int writtenBytes)
{
return (writtenBytes = this.writtenBytes) != 0;
}
/// <inheritdoc/>
void ID2D1DispatchDataLoader.LoadConstantBuffer(ReadOnlySpan<uint> data)
{
ReadOnlySpan<byte> constantBuffer = MemoryMarshal.Cast<uint, byte>(data);
if (constantBuffer.TryCopyTo(new Span<byte>(this.buffer, this.length)))
{
this.writtenBytes = constantBuffer.Length;
}
}
}
+158
ファイルの表示
@@ -275,4 +275,162 @@ public partial class D2D1PixelShaderTests
return result;
}
}
[TestMethod]
public unsafe void GetConstantBufferSize()
{
int size = D2D1PixelShader.GetConstantBufferSize<ShaderWithScalarVectorAndMatrixTypes>();
Assert.AreEqual(size, 124);
// Repeated calls always return the same result
size = D2D1PixelShader.GetConstantBufferSize<ShaderWithScalarVectorAndMatrixTypes>();
Assert.AreEqual(size, 124);
}
[TestMethod]
public unsafe void GetConstantBuffer_ReadOnlyMemory()
{
GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
ReadOnlyMemory<byte> constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader);
ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(constantBuffer.Span);
}
[TestMethod]
public unsafe void GetConstantBuffer_Span()
{
GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
byte[] buffer = new byte[1024];
int bytesWritten = D2D1PixelShader.GetConstantBuffer(in shader, buffer);
Assert.IsTrue(bytesWritten > 0);
Assert.IsTrue(bytesWritten <= buffer.Length);
ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten));
}
[TestMethod]
[DataRow(0)]
[DataRow(4)]
[DataRow(27)]
[ExpectedException(typeof(ArgumentException))]
public unsafe void GetConstantBuffer_Span_DestinationTooShort(int size)
{
GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
byte[] buffer = new byte[size];
_ = D2D1PixelShader.GetConstantBuffer(in shader, buffer);
Assert.Fail();
}
[TestMethod]
public unsafe void GetConstantBuffer_TryGetSpan()
{
GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
byte[] buffer = new byte[1024];
bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten);
Assert.IsTrue(success);
Assert.IsTrue(bytesWritten > 0);
Assert.IsTrue(bytesWritten <= buffer.Length);
ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(buffer.AsSpan(0, bytesWritten));
}
[TestMethod]
[DataRow(0)]
[DataRow(4)]
[DataRow(27)]
public unsafe void GetConstantBuffer_TryGetSpan_DestinationTooShort(int size)
{
GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader);
byte[] buffer = new byte[size];
bool success = D2D1PixelShader.TryGetConstantBuffer(in shader, buffer, out int bytesWritten);
Assert.IsFalse(success);
Assert.AreEqual(bytesWritten, 0);
}
// Gets a ShaderWithScalarVectorAndMatrixTypes instance with test values
private void GetShaderWithScalarVectorAndMatrixTypes(out ShaderWithScalarVectorAndMatrixTypes shader)
{
shader = new ShaderWithScalarVectorAndMatrixTypes(
x: 111,
y: 222,
z: 333,
f2x3: new(55, 44, 888, 111, 222, 333),
a: 22,
i1x3: new(1, 2, 3),
d2: new(3.14, 6.28),
c: 42,
i1x2: new(111, 222),
i2x2: new(11, 22, 33, 44),
d: 9999);
}
// Validates the constant buffer for the test ShaderWithScalarVectorAndMatrixTypes instance
private unsafe void ValidateShaderWithScalarVectorAndMatrixTypesConstantBuffer(ReadOnlySpan<byte> span)
{
Assert.AreEqual(31 * sizeof(uint), span.Length);
fixed (byte* buffer = span)
{
Assert.AreEqual(111, *(int*)&buffer[0]);
Assert.AreEqual(222, *(int*)&buffer[4]);
Assert.AreEqual(333, *(int*)&buffer[8]);
Assert.AreEqual(55, *(float*)&buffer[16]);
Assert.AreEqual(44, *(float*)&buffer[20]);
Assert.AreEqual(888, *(float*)&buffer[24]);
Assert.AreEqual(111, *(float*)&buffer[32]);
Assert.AreEqual(222, *(float*)&buffer[36]);
Assert.AreEqual(333, *(float*)&buffer[40]);
Assert.AreEqual(22, *(int*)&buffer[44]);
Assert.AreEqual(1, *(int*)&buffer[48]);
Assert.AreEqual(2, *(int*)&buffer[52]);
Assert.AreEqual(3, *(int*)&buffer[56]);
Assert.AreEqual(3.14, *(double*)&buffer[64]);
Assert.AreEqual(6.28, *(double*)&buffer[72]);
Assert.AreEqual(42, *(int*)&buffer[80]);
Assert.AreEqual(111, *(int*)&buffer[84]);
Assert.AreEqual(222, *(int*)&buffer[88]);
Assert.AreEqual(11, *(int*)&buffer[96]);
Assert.AreEqual(22, *(int*)&buffer[100]);
Assert.AreEqual(33, *(int*)&buffer[112]);
Assert.AreEqual(44, *(int*)&buffer[116]);
Assert.AreEqual(9999, *(int*)&buffer[120]);
}
}
[D2DInputCount(0)]
[AutoConstructor]
private readonly partial struct ShaderWithScalarVectorAndMatrixTypes : ID2D1PixelShader
{
public readonly int x;
public readonly int y;
public readonly int z;
public readonly float2x3 f2x3;
public readonly int a;
public readonly Int1x3 i1x3;
public readonly double2 d2;
public readonly int c;
public readonly Int1x2 i1x2;
public readonly int2x2 i2x2;
public readonly int d;
public float4 Execute()
{
return 0;
}
}
}