From 151204d5c1a7bd9a860f424b1b5960fdc81691b3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Jan 2023 17:55:27 +0100 Subject: [PATCH] Fix exception when setting constant buffer for empty type --- .../Extensions/ID2D1EffectExtensions.cs | 21 +++++++++++++++++++ ...PixelShaderEffect{T}.Properties.Interop.cs | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs index 3c25d334..e937b79c 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -57,6 +57,27 @@ internal static unsafe class ID2D1EffectExtensions return shader; } + /// + /// Sets the constant buffer of type from a given object. + /// + /// The type of shader being used. + /// The input instance. + /// The constant buffer value to set. + public static void SetConstantBuffer(this ref ID2D1Effect d2D1Effect, in T value) + where T : unmanaged, ID2D1PixelShader + { + // Same as above, simply do nothing for empty types. This case is still handled to + // avoid crashing when someone's just setting an empty constant buffer, which is + // unnecessary but still valid (eg. a stateless shader). This could be the case if + // someone was eg. binding or using some other automated system to set constant buffer. + if (D2D1PixelShader.GetConstantBufferSize() == 0) + { + return; + } + + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect((ID2D1Effect*)Unsafe.AsPointer(ref d2D1Effect), in value); + } + /// /// Gets the instance from a given object. /// diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs index 0e5c3bd3..88862939 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.cs @@ -40,7 +40,7 @@ unsafe partial class PixelShaderEffect // If the effect is realized, set the property on the underlying ID2D1Effect object if (this.d2D1Effect.Get() is not null) { - D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(this.d2D1Effect.Get(), in value); + this.d2D1Effect.Get()->SetConstantBuffer(in value); } else {