From ea2b314267345b3d0e923e6695652192d1637936 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 16 Aug 2022 14:05:25 +0200 Subject: [PATCH] Support ID2D1Effect::GetValueSize for constant buffer, add unit test --- .../Effects/PixelShaderEffect.Properties.cs | 14 ++++++-- .../D2D1PixelShaderEffectTests.cs | 36 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs index fb5c9f63..856bb492 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs @@ -69,13 +69,23 @@ unsafe partial struct PixelShaderEffect [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] private static int GetConstantBufferImpl(IUnknown* effect, byte* data, uint dataSize, uint* actualSize) { + PixelShaderEffect* @this = (PixelShaderEffect*)effect; + + // This is a call likely from ID2D1Effect::GetValueSize, to query the property size + if (data is null && + dataSize == 0 && + actualSize is not null) + { + *actualSize = (uint)@this->constantBufferSize; + + return S.S_FALSE; + } + if (data is null) { return E.E_POINTER; } - PixelShaderEffect* @this = (PixelShaderEffect*)effect; - if (dataSize < @this->constantBufferSize) { return E.E_NOT_SUFFICIENT_BUFFER; diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index ab67ec3a..6d2c504b 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Runtime.InteropServices; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; @@ -121,4 +122,39 @@ public partial class D2D1PixelShaderEffectTests return dummy; } } + + [TestMethod] + public unsafe void GetValueSize_ConstantBuffer() + { + using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2(); + using ComPtr d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get()); + using ComPtr d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get()); + + D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory2.Get(), null, out _); + + using ComPtr d2D1Effect = default; + + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf()); + + uint size = d2D1Effect.Get()->GetValueSize(D2D1PixelShaderEffectProperty.ConstantBuffer); + + Assert.AreEqual(D2D1PixelShader.GetConstantBufferSize(), (int)size); + } + + [D2DInputCount(0)] + [D2DRequiresScenePosition] + [AutoConstructor] + private partial struct ConstantBufferSizeTestShader : ID2D1PixelShader + { + private float a; + private float b; + private float3 c; + private int d; + private int e; + + public float4 Execute() + { + return a + b + c.X + d + e; + } + } } \ No newline at end of file