From d0490ed524ca8923c37c641d89f21cdb56a5f4a7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Jan 2023 17:13:10 +0100 Subject: [PATCH 1/4] Fix ID2D1DeviceContextExtensions file name --- ...{ID3D12DeviceExtensions.cs => ID2D1DeviceContextExtensions.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/ComputeSharp.D2D1.Uwp/Extensions/{ID3D12DeviceExtensions.cs => ID2D1DeviceContextExtensions.cs} (100%) diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs similarity index 100% rename from src/ComputeSharp.D2D1.Uwp/Extensions/ID3D12DeviceExtensions.cs rename to src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs From c33877d3826a98291ee5592d00565c31ffb386a8 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Jan 2023 17:18:44 +0100 Subject: [PATCH 2/4] Defer getting D2D1 factory to when realization fails --- .../ID2D1DeviceContextExtensions.cs | 20 +++++++++++++++++++ ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 15 +++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs index 5e6b46fb..c2304839 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs @@ -57,4 +57,24 @@ internal static unsafe class ID2D1DeviceContextExtensions return dpiX; } + + /// + /// Gets an object from a given . + /// + /// The input instance. + /// The resulting object. + /// The for the operation. + /// + /// The APIs specifically need an (as is used). + /// + public static HRESULT GetFactory1(this ref ID2D1DeviceContext deviceContext, ID2D1Factory1** d2D1Factory1) + { + using ComPtr d2D1Factory = default; + + // Get the underlying ID2D1Factory from the input context, which can be used to register effects + deviceContext.GetFactory(d2D1Factory.GetAddressOf()); + + // QueryInterface for the ID2D1Factory1 object (which should always be present) + return d2D1Factory.CopyTo(d2D1Factory1); + } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index 048b8c97..33d14648 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -224,16 +224,6 @@ unsafe partial class PixelShaderEffect /// Whether the effect was realized correctly. private bool Realize(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext) { - using ComPtr d2D1Factory = default; - - // Get the underlying ID2D1Factory from the input context, so we can register and create the effect - deviceContext->GetFactory(d2D1Factory.GetAddressOf()); - - using ComPtr d2D1Factory1 = default; - - // D2D1PixelShaderEffect APIs specifically need an ID2D1Factory1 (as ID2D1Factory1::RegisterEffectFromString is used) - d2D1Factory.CopyTo(d2D1Factory1.GetAddressOf()).Assert(); - fixed (ID2D1Effect** d2D1Effect = this.d2D1Effect) { Guid effectId = PixelShaderEffect.For.Instance.Id; @@ -248,6 +238,11 @@ unsafe partial class PixelShaderEffect if (hresult == D2DERR.D2DERR_EFFECT_IS_NOT_REGISTERED || hresult == E.E_NOTFOUND) { + using ComPtr d2D1Factory1 = default; + + // Get the ID2D1Factory1 object to register the effect (required by D2D1PixelShaderEffect) + deviceContext->GetFactory1(d2D1Factory1.GetAddressOf()).Assert(); + // Register the effect with the factory (pass the same D2D1 draw transform mapper factory that was used before) D2D1PixelShaderEffect.RegisterForD2D1Factory1(d2D1Factory1.Get(), out _); From d304a0ff96000832b3d03d0012075db2a8d48e8f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Jan 2023 17:43:33 +0100 Subject: [PATCH 3/4] Fix exception when getting constant buffer for empty type --- .../Extensions/ID2D1EffectExtensions.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs index 88822c3c..3c25d334 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs @@ -29,6 +29,15 @@ internal static unsafe class ID2D1EffectExtensions where T : unmanaged, ID2D1PixelShader { int constantBufferSize = D2D1PixelShader.GetConstantBufferSize(); + + // Special cases the constant buffer type being empty. If that's the case, there is no + // need to try to retrieve the constant buffer from the underlying effect anyway, and + // most importantly, the ID2D1EffectImpl from ComputeSharp.D2D1 will return E_INVALIDARG. + if (constantBufferSize == 0) + { + return default; + } + byte[] buffer = ArrayPool.Shared.Rent(constantBufferSize); fixed (byte* p = buffer) From 151204d5c1a7bd9a860f424b1b5960fdc81691b3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 10 Jan 2023 17:55:27 +0100 Subject: [PATCH 4/4] 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 {