diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ID3D12DeviceExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs
similarity index 64%
rename from src/ComputeSharp.D2D1.Uwp/Extensions/ID3D12DeviceExtensions.cs
rename to src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1DeviceContextExtensions.cs
index 5e6b46fb..c2304839 100644
--- a/src/ComputeSharp.D2D1.Uwp/Extensions/ID3D12DeviceExtensions.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/Extensions/ID2D1EffectExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ID2D1EffectExtensions.cs
index 88822c3c..e937b79c 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)
@@ -48,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}.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 _);
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
{