diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs
index 19b8e0dc..88822ea7 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1ResourceTextureManager.cs
@@ -71,7 +71,9 @@ namespace ComputeSharp.D2D1.Interop;
/// [uuid(5CBB1024-8EA1-4689-81BF-8AD190B5EF5D)]
/// interface ID2D1ResourceTextureManager : IUnknown
/// {
-/// HRESULT SetEffectContext([in] ID2D1EffectContext *effectContext);
+/// HRESULT Initialize(
+/// [in] ID2D1EffectContext *effectContext,
+/// [in, optional] const UINT32 *dimensions);
///
/// HRESULT GetResourceTexture([out] ID2D1ResourceTexture **resourceTexture);
/// };
@@ -79,13 +81,19 @@ namespace ComputeSharp.D2D1.Interop;
///
///
/// Note that due to the fact ID2D1EffectContext and ID2D1ResourceTexture are not thread safe, they are only
-/// safe to use without additional synchronization when SetEffectContext and GetResourceTexture are called.
+/// safe to use without additional synchronization when Initialize and GetResourceTexture are called.
/// This happens from the internal ID2D1EffectImpl object, which is invoked by D2D, which will handle taking the
/// necessary lock before invoking these APIs. Due to the fact the context has to be stored internally, custom implementations
/// have to make sure to retrieve the right ID2D1Multithread instance from the input context and use that to synchronize
/// all accesses to those two objects from all APIs (including those exposed by ID2D1ResourceTextureManager). The
/// built-in implementation of these two interfaces provided by takes care of all this.
///
+///
+/// The dimensions parameter in ID2D1ResourceTextureManager::Initialize is optional, and can be passed by an ID2D1Effect
+/// when initializing an input resurce texture manager, if the dimensions of the resource texture at the target index are known in advance.
+/// This can allow the manager to perform additional validation when the resource texture is initialized. Not providing a value is not an
+/// error, but if a resource texture with invalid size is used the effect might fail to render later on and be more difficult to troubleshoot.
+///
///
public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
{
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
index 8459d558..9eeaac8a 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Properties.cs
@@ -140,7 +140,7 @@ unsafe partial struct PixelShaderEffect
/// The for the operation.
private int GetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize, uint* actualSize)
{
- if (!IsResourceTextureManagerIndexValid(index))
+ if (!IsResourceTextureManagerIndexValid(index, out _))
{
return E.E_INVALIDARG;
}
@@ -173,7 +173,7 @@ unsafe partial struct PixelShaderEffect
/// The for the operation.
private int SetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize)
{
- if (!IsResourceTextureManagerIndexValid(index))
+ if (!IsResourceTextureManagerIndexValid(index, out uint dimensions))
{
return E.E_INVALIDARG;
}
@@ -219,7 +219,7 @@ unsafe partial struct PixelShaderEffect
// Initialize the resource texture manager, if an effect context is available
if (this.d2D1EffectContext is not null)
{
- resourceTextureManagerInternal.Get()->SetEffectContext(this.d2D1EffectContext);
+ resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions);
}
// Store the resource texture manager into the buffer
@@ -232,17 +232,22 @@ unsafe partial struct PixelShaderEffect
/// Checks whether a given index for a resource texture manager is valid for the current effect.
///
/// The resource texture manager index to validate.
+ /// The number of dimensions for the resource texture at the gven index.
/// Whether or not is valid for the current effect.
- private bool IsResourceTextureManagerIndexValid(int index)
+ private bool IsResourceTextureManagerIndexValid(int index, out uint dimensions)
{
foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan(this.resourceTextureDescriptions, this.resourceTextureDescriptionCount))
{
if (resourceTextureDescription.Index == index)
{
+ dimensions = (uint)resourceTextureDescription.Dimensions;
+
return true;
}
}
+ dimensions = 0;
+
return false;
}
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs
index df6c6e02..e48e170e 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManager.cs
@@ -29,6 +29,15 @@ unsafe partial struct D2D1ResourceTextureManagerImpl
return E.E_NOT_VALID_STATE;
}
+ // If the manager has been assigned to an effect and the dimensions are available, validate them.
+ // Check and ignore if the input properties are null (validation for that is done afterwards).
+ if (@this->expectedDimensions != 0 &&
+ resourceTextureProperties is not null &&
+ resourceTextureProperties->dimensions != @this->expectedDimensions)
+ {
+ return E.E_INVALIDARG;
+ }
+
// If the method has already been called, just forward the call
if (@this->d2D1EffectContext is not null)
{
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 42c59e3c..f9fc316e 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.ID2D1ResourceTextureManagerInternal.cs
@@ -19,9 +19,9 @@ partial struct D2D1ResourceTextureManagerImpl
private unsafe static class ID2D1ResourceTextureManagerInternalMethods
{
#if !NET6_0_OR_GREATER
- ///
+ ///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
- public delegate int SetEffectContextDelegate(D2D1ResourceTextureManagerImpl* @this, ID2D1EffectContext* effectContext);
+ public delegate int InitializeDelegate(D2D1ResourceTextureManagerImpl* @this, ID2D1EffectContext* effectContext, uint* dimensions);
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
@@ -43,9 +43,9 @@ partial struct D2D1ResourceTextureManagerImpl
public static readonly ReleaseDelegate ReleaseWrapper = Release;
///
- /// A cached instance wrapping .
+ /// A cached instance wrapping .
///
- public static readonly SetEffectContextDelegate SetEffectContextWrapper = SetEffectContext;
+ public static readonly InitializeDelegate InitializeWrapper = Initialize;
///
/// A cached instance wrapping .
@@ -80,9 +80,9 @@ partial struct D2D1ResourceTextureManagerImpl
return @this->Release();
}
- ///
+ ///
[UnmanagedCallersOnly]
- public static int SetEffectContext(D2D1ResourceTextureManagerImpl* @this, ID2D1EffectContext* effectContext)
+ public static int Initialize(D2D1ResourceTextureManagerImpl* @this, ID2D1EffectContext* effectContext, uint* dimensions)
{
@this = (D2D1ResourceTextureManagerImpl*)&((void**)@this)[-1];
@@ -93,6 +93,20 @@ partial struct D2D1ResourceTextureManagerImpl
lock (@this->lockHandle.Target!)
{
+ // If the dimensions are available, validate them
+ if (dimensions is not null)
+ {
+ uint expectedDimensions = *dimensions;
+
+ if (@this->resourceTextureProperties.extents is not null &&
+ @this->resourceTextureProperties.dimensions != expectedDimensions)
+ {
+ return E.E_INVALIDARG;
+ }
+
+ @this->expectedDimensions = expectedDimensions;
+ }
+
// If this is the first time the method is called, just store the context.
// Before doing so, get an ID2D1Multithread instance, to ensure thread safety.
// For additional info, see docs on ID2D1EffectFactoryExtensions.GetD2D1Multithread.
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs
index df88f646..952d28bd 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/D2D1ResourceTextureManagerImpl.cs
@@ -66,13 +66,13 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
lpVtbl[5 + 0] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.QueryInterface;
lpVtbl[5 + 1] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.AddRef;
lpVtbl[5 + 2] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.Release;
- lpVtbl[5 + 3] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.SetEffectContext;
+ lpVtbl[5 + 3] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.Initialize;
lpVtbl[5 + 4] = (delegate* unmanaged)&ID2D1ResourceTextureManagerInternalMethods.GetResourceTexture;
#else
lpVtbl[5 + 0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.QueryInterfaceWrapper);
lpVtbl[5 + 1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.AddRefWrapper);
lpVtbl[5 + 2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.ReleaseWrapper);
- lpVtbl[5 + 3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.SetEffectContextWrapper);
+ lpVtbl[5 + 3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.InitializeWrapper);
lpVtbl[5 + 4] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1ResourceTextureManagerInternalMethods.GetResourceTextureWrapper);
#endif
@@ -146,6 +146,12 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
///
private GCHandle lockHandle;
+ ///
+ /// The expected dimensions for the slot the manager has been assigned to in an effect.
+ ///
+ /// If 0, it has not been set, so it can be ignored.
+ private uint expectedDimensions;
+
///
/// The factory method for instances.
///
@@ -168,6 +174,7 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
@this->strides = null;
@this->dataSize = 0;
@this->lockHandle = GCHandle.Alloc(new object());
+ @this->expectedDimensions = 0;
*resourceTextureManager = @this;
}
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ID2D1ResourceTextureManagerInternal.cs
index d89d79fb..ac43cacb 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ID2D1ResourceTextureManagerInternal.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ID2D1ResourceTextureManagerInternal.cs
@@ -47,13 +47,15 @@ internal unsafe struct ID2D1ResourceTextureManagerInternal
/// Sets the for the current ID2D1ResourceTextureManagerInternal instance.
///
/// The input for the manager.
+ /// A pointer to an optional amount of dimensions for the target resource texture, for validation.
/// An for the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public int SetEffectContext(ID2D1EffectContext* effectContext)
+ public int Initialize(ID2D1EffectContext* effectContext, uint* dimensions)
{
- return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])(
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])(
(ID2D1ResourceTextureManagerInternal*)Unsafe.AsPointer(ref this),
- effectContext);
+ effectContext,
+ dimensions);
}
///