diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
index e3e3d1f3..43584367 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.ID2D1EffectImplMethods.cs
@@ -128,11 +128,27 @@ internal unsafe partial struct PixelShaderEffect
}
}
+ // If loading the bytecode succeeded, set the transform node
if (Windows.SUCCEEDED(hresult))
{
hresult = transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform);
}
+ // If the transform node was set, also store the effect context
+ if (Windows.SUCCEEDED(hresult))
+ {
+ // Free the previous ID2D1EffectContext object, if present
+ if (@this->d2D1EffectContext is not null)
+ {
+ _ = @this->d2D1EffectContext->Release();
+ }
+
+ // Store the new ID2D1EffectContext object
+ _ = effectContext->AddRef();
+
+ @this->d2D1EffectContext = effectContext;
+ }
+
return hresult;
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
index 35922e2d..15564664 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs
@@ -191,6 +191,11 @@ internal unsafe partial struct PixelShaderEffect
///
private ID2D1DrawInfo* d2D1DrawInfo;
+ ///
+ /// The instance currently in use.
+ ///
+ private ID2D1EffectContext* d2D1EffectContext;
+
///
/// The factory method for .
///
@@ -250,8 +255,10 @@ internal unsafe partial struct PixelShaderEffect
@this->bufferPrecision = bufferPrecision;
@this->channelDepth = channelDepth;
@this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper);
+ @this->d2D1DrawInfo = null;
+ @this->d2D1EffectContext = null;
- *effectImpl = (IUnknown*)@this;
+ * effectImpl = (IUnknown*)@this;
return S.S_OK;
}
@@ -317,6 +324,11 @@ internal unsafe partial struct PixelShaderEffect
_ = this.d2D1DrawInfo->Release();
}
+ if (this.d2D1EffectContext is not null)
+ {
+ _ = this.d2D1EffectContext->Release();
+ }
+
NativeMemory.Free(Unsafe.AsPointer(ref this));
}