From ec057ce7e420825323d650151cf145f5d029da87 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 15 Aug 2022 14:14:33 +0200 Subject: [PATCH] Add validation for constant buffer initialization and unit test --- ...lShaderEffect.ID2D1DrawTransformMethods.cs | 1 - ...ixelShaderEffect.ID2D1EffectImplMethods.cs | 7 ++++ .../D2D1PixelShaderEffectTests.cs | 38 ++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs index 8ea1cfc6..4b9995ff 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -200,7 +200,6 @@ partial struct PixelShaderEffect opaqueInputs[i] = inputOpaqueSubRects[i].ToRectangle(); } - // TODO: shouldn't there be a check for the buffer being null? ReadOnlySpan buffer = new(@this->constantBuffer, @this->constantBufferSize); Rectangle output; diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs index 130eb816..0509e163 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -159,6 +159,13 @@ internal unsafe partial struct PixelShaderEffect { int hresult = S.S_OK; + // Validate the constant buffer + if (@this->constantBufferSize > 0 && + @this->constantBuffer is null) + { + return E.E_NOT_VALID_STATE; + } + // First, set the constant buffer, if available if (@this->constantBuffer is not null) { diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs index 83a3dc65..ab67ec3a 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -1,13 +1,17 @@ using System; +using System.ComponentModel; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; +using ComputeSharp.D2D1.Tests.Helpers; using Microsoft.VisualStudio.TestTools.UnitTesting; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1.Tests; [TestClass] [TestCategory("D2D1PixelShaderEffect")] -public class D2D1PixelShaderEffectTests +public partial class D2D1PixelShaderEffectTests { [TestMethod] [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] @@ -85,4 +89,36 @@ public class D2D1PixelShaderEffectTests { D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect((void*)1, (D2D1ResourceTextureManager)null!, 0); } + + [TestMethod] + [ExpectedException(typeof(Win32Exception))] + public unsafe void NullConstantBuffer_DrawImageFails() + { + 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()); + + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), 128, 128); + + D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); + } + + [D2DInputCount(0)] + [D2DRequiresScenePosition] + [AutoConstructor] + private partial struct NullConstantBufferShader : ID2D1PixelShader + { + private float dummy; + + public float4 Execute() + { + return dummy; + } + } } \ No newline at end of file