From a6209491c9c85ec08261448f54008c5bad06e2bc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 24 Jul 2022 13:27:46 +0200 Subject: [PATCH] Fix D2D1PixelShader.GetConstantBuffer with stateless shaders --- .../D2D1ByteBufferDispatchDataLoader.cs | 17 +++++- .../D2D1PixelShaderTests.cs | 52 ++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs index 1471514a..e10f2d0d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Loaders/D2D1ByteBufferDispatchDataLoader.cs @@ -38,7 +38,7 @@ internal unsafe struct D2D1ByteBufferDispatchDataLoader : ID2D1DispatchDataLoade { this.buffer = buffer; this.length = length; - this.writtenBytes = 0; + this.writtenBytes = -1; } /// @@ -49,7 +49,20 @@ internal unsafe struct D2D1ByteBufferDispatchDataLoader : ID2D1DispatchDataLoade [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryGetWrittenBytes(out int writtenBytes) { - return (writtenBytes = this.writtenBytes) != 0; + // If the -1 marker is present, it means copying to the target span failed. + // In this case, the returned number of written bytes is still reported as 0. + // But, the actual load operation is marked as failed. This allows the data loader + // to both handle calls with a destination too short and with an empty source buffer. + if (this.writtenBytes == -1) + { + writtenBytes = 0; + + return false; + } + + writtenBytes = this.writtenBytes; + + return true; } /// diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 437e235d..a3dad56d 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -276,6 +276,56 @@ public partial class D2D1PixelShaderTests } } + [TestMethod] + public unsafe void GetConstantBufferSize_Empty() + { + int size = D2D1PixelShader.GetConstantBufferSize(); + + Assert.AreEqual(size, 0); + + // Repeated calls always return the same result + size = D2D1PixelShader.GetConstantBufferSize(); + + Assert.AreEqual(size, 0); + } + + [TestMethod] + public unsafe void GetConstantBuffer_Empty_ReadOnlyMemory() + { + ShaderWithNoCapturedValues shader = default; + + ReadOnlyMemory constantBuffer = D2D1PixelShader.GetConstantBuffer(in shader); + + Assert.AreEqual(constantBuffer.Length, 0); + } + + // See https://github.com/Sergio0694/ComputeSharp/issues/323 + [TestMethod] + public unsafe void GetConstantBuffer_Empty_Span() + { + ShaderWithNoCapturedValues shader = default; + + byte[] buffer = Array.Empty(); + + int bytesWritten = D2D1PixelShader.GetConstantBuffer(in shader, buffer); + + Assert.AreEqual(bytesWritten, 0); + } + + [D2DInputCount(1)] + [D2DInputSimple(0)] + [D2DInputDescription(0, D2D1Filter.MinMagMipPoint)] + [D2DPixelOptions(D2D1PixelOptions.TrivialSampling)] + private readonly partial struct ShaderWithNoCapturedValues : ID2D1PixelShader + { + public float4 Execute() + { + float4 color = D2D.GetInput(0); + + return color * color; + } + } + [TestMethod] public unsafe void GetConstantBufferSize() { @@ -283,7 +333,7 @@ public partial class D2D1PixelShaderTests Assert.AreEqual(size, 124); - // Repeated calls always return the same result + // Same as above: repeated calls always return the same result size = D2D1PixelShader.GetConstantBufferSize(); Assert.AreEqual(size, 124);