diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs
index 8710954a..7241354d 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs
@@ -116,6 +116,17 @@ public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
/// The filter for the resource to create.
/// The extend modes for the resource to create.
/// Thrown if creating or initializing the instance failed.
+ ///
+ ///
+ /// Since has a finalizer, which can be invoked at any point on the finalizer
+ /// thread, it can only be used in effects associated with a device context using a factory that supports multithreaded
+ /// use (ie. the underlying ID2D1Factory has to be created using D2D1_FACTORY_TYPE_MULTI_THREADED ).
+ /// Not doing so will cause the resource texture manager to fail to be initialized when used in an effect.
+ ///
+ ///
+ /// For more info, see .
+ ///
+ ///
public D2D1ResourceTextureManager(
ReadOnlySpan extents,
D2D1BufferPrecision bufferPrecision,
@@ -144,6 +155,17 @@ public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
/// The filter for the resource to create.
/// The extend modes for the resource to create.
/// Thrown if creating or initializing the instance failed.
+ ///
+ ///
+ /// Since has a finalizer, which can be invoked at any point on the finalizer
+ /// thread, it can only be used in effects associated with a device context using a factory that supports multithreaded
+ /// use (ie. the underlying ID2D1Factory has to be created using D2D1_FACTORY_TYPE_MULTI_THREADED ).
+ /// Not doing so will cause the resource texture manager to fail to be initialized when used in an effect.
+ ///
+ ///
+ /// For more info, see .
+ ///
+ ///
public D2D1ResourceTextureManager(
Guid resourceId,
ReadOnlySpan extents,
@@ -174,6 +196,17 @@ public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
/// The data to load in the resource to create.
/// The strides for the data supplied for the resource to create.
/// Thrown if creating or initializing the instance failed.
+ ///
+ ///
+ /// Since has a finalizer, which can be invoked at any point on the finalizer
+ /// thread, it can only be used in effects associated with a device context using a factory that supports multithreaded
+ /// use (ie. the underlying ID2D1Factory has to be created using D2D1_FACTORY_TYPE_MULTI_THREADED ).
+ /// Not doing so will cause the resource texture manager to fail to be initialized when used in an effect.
+ ///
+ ///
+ /// For more info, see .
+ ///
+ ///
public D2D1ResourceTextureManager(
ReadOnlySpan extents,
D2D1BufferPrecision bufferPrecision,
@@ -206,6 +239,17 @@ public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
/// The data to load in the resource to create.
/// The strides for the data supplied for the resource to create.
/// Thrown if creating or initializing the instance failed.
+ ///
+ ///
+ /// Since has a finalizer, which can be invoked at any point on the finalizer
+ /// thread, it can only be used in effects associated with a device context using a factory that supports multithreaded
+ /// use (ie. the underlying ID2D1Factory has to be created using D2D1_FACTORY_TYPE_MULTI_THREADED ).
+ /// Not doing so will cause the resource texture manager to fail to be initialized when used in an effect.
+ ///
+ ///
+ /// For more info, see .
+ ///
+ ///
public D2D1ResourceTextureManager(
Guid resourceId,
ReadOnlySpan extents,
@@ -263,7 +307,7 @@ public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
fixed (D2D1ResourceTextureManagerImpl** d2D1ResourceManagerImpl = &this.d2D1ResourceManagerImpl)
{
- D2D1ResourceTextureManagerImpl.Factory(d2D1ResourceManagerImpl);
+ D2D1ResourceTextureManagerImpl.Factory(d2D1ResourceManagerImpl, requiresMultithread: true);
}
fixed (uint* pExtents = extents)
@@ -382,9 +426,13 @@ public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
/// Creates a new ID2D1ResourceTextureManager instance.
///
/// A pointer to the resulting ID2D1ResourceTextureManager instance.
+ ///
+ /// The resulting ID2D1ResourceTextureManager instance does not require multithread support for the factory that is
+ /// assigned to it when initialized from an effect. Callers have the responsibility of ensuring thread safety when using it.
+ ///
public static void Create(void** resourceTextureManager)
{
- D2D1ResourceTextureManagerImpl.Factory((D2D1ResourceTextureManagerImpl**)resourceTextureManager);
+ D2D1ResourceTextureManagerImpl.Factory((D2D1ResourceTextureManagerImpl**)resourceTextureManager, requiresMultithread: false);
}
///
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs
index 4396bd1d..9d4cdb7d 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs
@@ -116,9 +116,18 @@ partial struct D2D1ResourceTextureManagerImpl
int hresult = effectContext->GetD2D1Multithread(d2D1Multithread.GetAddressOf());
- // If an ID2D1Multithread object is available, we can safely store the context
+ // If an ID2D1Multithread object is available, we can safely store the context. That
+ // is, under the condition that the required multithread support is also available.
if (Windows.SUCCEEDED(hresult))
{
+ if (@this->requiresMultithread > d2D1Multithread.Get()->GetMultithreadProtected())
+ {
+ return E.E_INVALIDARG;
+ }
+
+ // Now, the effect context can actually be stored safely while holding the lock.
+ // This is guaranteed to be the case here, as this method is only called (as per
+ // contact of the COM interface) from ID2D1EffectImpl, which holds the D2D lock.
_ = effectContext->AddRef();
@this->d2D1EffectContext = effectContext;
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs
index f575d791..bf17571c 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs
@@ -95,6 +95,15 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
///
private volatile int referenceCount;
+ ///
+ /// Indicates whether the current instance requires multithread support to work safely.
+ ///
+ ///
+ /// This is used when the object is wrapped in a RCW with a finalizer, which could run at any time.
+ /// In that case, multithread support is required, and using a single thread factory is an error.
+ ///
+ private int requiresMultithread;
+
///
/// The object currently wrapped by the current instance.
///
@@ -156,7 +165,8 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
/// The factory method for instances.
///
/// The resulting resource texture manager instance.
- public static void Factory(D2D1ResourceTextureManagerImpl** resourceTextureManager)
+ /// Indicates whether the current instance requires multithread support to work safely.
+ public static void Factory(D2D1ResourceTextureManagerImpl** resourceTextureManager, bool requiresMultithread)
{
D2D1ResourceTextureManagerImpl* @this = (D2D1ResourceTextureManagerImpl*)NativeMemory.Alloc((nuint)sizeof(D2D1ResourceTextureManagerImpl));
@@ -168,6 +178,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
@this->d2D1EffectContext = null;
@this->d2D1Multithread = null;
@this->d2D1ResourceTexture = null;
+ @this->requiresMultithread = requiresMultithread ? 1 : 0;
@this->resourceId = null;
@this->resourceTextureProperties = default;
@this->data = null;
diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Multithread.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Multithread.cs
index 93cc3351..d728980d 100644
--- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Multithread.cs
+++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/ID2D1Multithread.cs
@@ -45,6 +45,13 @@ namespace TerraFX.Interop.DirectX
return ((delegate* unmanaged[Stdcall])(lpVtbl[2]))((ID2D1Multithread*)Unsafe.AsPointer(ref this));
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(3)]
+ public int GetMultithreadProtected()
+ {
+ return ((delegate* unmanaged[Stdcall])(lpVtbl[3]))((ID2D1Multithread*)Unsafe.AsPointer(ref this));
+ }
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(4)]
public void Enter()
diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs
index 0195201e..cb2431f1 100644
--- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs
+++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1Helper.cs
@@ -13,8 +13,9 @@ internal static class D2D1Helper
///
/// Creates an instance.
///
+ /// Indicates whether or not the factory should be created as single threaded.
/// A new instance.
- public static unsafe ComPtr CreateD2D1Factory2()
+ public static unsafe ComPtr CreateD2D1Factory2(bool singleThreaded = false)
{
using ComPtr d2D1Factory2 = default;
@@ -22,7 +23,7 @@ internal static class D2D1Helper
// Create a Direct2D factory
DirectX.D2D1CreateFactory(
- factoryType: D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED,
+ factoryType: singleThreaded ? D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_SINGLE_THREADED : D2D1_FACTORY_TYPE.D2D1_FACTORY_TYPE_MULTI_THREADED,
riid: Windows.__uuidof(),
pFactoryOptions: &d2D1FactoryOptions,
ppIFactory: (void**)d2D1Factory2.GetAddressOf()).Assert();