Add ID2D1ResourceTextureManager::Initialize dimensions parameter
このコミットが含まれているのは:
1 親
e29a3fad04
コミット
3c2de6c285
6ファイル変更
+62
-17
検索条件に一致するファイルはありません
@@ -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;
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Note that due to the fact <c>ID2D1EffectContext</c> and <c>ID2D1ResourceTexture</c> are not thread safe, they are only
|
||||
/// safe to use without additional synchronization when <c>SetEffectContext</c> and <c>GetResourceTexture</c> are called.
|
||||
/// safe to use without additional synchronization when <c>Initialize</c> and <c>GetResourceTexture</c> are called.
|
||||
/// This happens from the internal <c>ID2D1EffectImpl</c> 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 <c>ID2D1Multithread</c> instance from the input context and use that to synchronize
|
||||
/// all accesses to those two objects from all APIs (including those exposed by <c>ID2D1ResourceTextureManager</c>). The
|
||||
/// built-in implementation of these two interfaces provided by <see cref="D2D1ResourceTextureManager"/> takes care of all this.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The <c>dimensions</c> parameter in <c>ID2D1ResourceTextureManager::Initialize</c> is optional, and can be passed by an <c>ID2D1Effect</c>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed unsafe class D2D1ResourceTextureManager : ICustomQueryInterface
|
||||
{
|
||||
|
||||
@@ -140,7 +140,7 @@ unsafe partial struct PixelShaderEffect
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
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
|
||||
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
|
||||
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.
|
||||
/// </summary>
|
||||
/// <param name="index">The resource texture manager index to validate.</param>
|
||||
/// <param name="dimensions">The number of dimensions for the resource texture at the gven index.</param>
|
||||
/// <returns>Whether or not <paramref name="index"/> is valid for the current effect.</returns>
|
||||
private bool IsResourceTextureManagerIndexValid(int index)
|
||||
private bool IsResourceTextureManagerIndexValid(int index, out uint dimensions)
|
||||
{
|
||||
foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan<D2D1ResourceTextureDescription>(this.resourceTextureDescriptions, this.resourceTextureDescriptionCount))
|
||||
{
|
||||
if (resourceTextureDescription.Index == index)
|
||||
{
|
||||
dimensions = (uint)resourceTextureDescription.Dimensions;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
dimensions = 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+9
@@ -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)
|
||||
{
|
||||
|
||||
+20
-6
@@ -19,9 +19,9 @@ partial struct D2D1ResourceTextureManagerImpl
|
||||
private unsafe static class ID2D1ResourceTextureManagerInternalMethods
|
||||
{
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <inheritdoc cref="SetEffectContext"/>
|
||||
/// <inheritdoc cref="Initialize"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
public delegate int SetEffectContextDelegate(D2D1ResourceTextureManagerImpl* @this, ID2D1EffectContext* effectContext);
|
||||
public delegate int InitializeDelegate(D2D1ResourceTextureManagerImpl* @this, ID2D1EffectContext* effectContext, uint* dimensions);
|
||||
|
||||
/// <inheritdoc cref="GetResourceTexture"/>
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
||||
@@ -43,9 +43,9 @@ partial struct D2D1ResourceTextureManagerImpl
|
||||
public static readonly ReleaseDelegate ReleaseWrapper = Release;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="SetEffectContextDelegate"/> instance wrapping <see cref="SetEffectContext"/>.
|
||||
/// A cached <see cref="InitializeDelegate"/> instance wrapping <see cref="Initialize"/>.
|
||||
/// </summary>
|
||||
public static readonly SetEffectContextDelegate SetEffectContextWrapper = SetEffectContext;
|
||||
public static readonly InitializeDelegate InitializeWrapper = Initialize;
|
||||
|
||||
/// <summary>
|
||||
/// A cached <see cref="GetResourceTextureDelegate"/> instance wrapping <see cref="GetResourceTexture"/>.
|
||||
@@ -80,9 +80,9 @@ partial struct D2D1ResourceTextureManagerImpl
|
||||
return @this->Release();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ID2D1ResourceTextureManagerInternal.SetEffectContext"/>
|
||||
/// <inheritdoc cref="ID2D1ResourceTextureManagerInternal.Initialize"/>
|
||||
[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.
|
||||
|
||||
+9
-2
@@ -66,13 +66,13 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
|
||||
lpVtbl[5 + 0] = (delegate* unmanaged<D2D1ResourceTextureManagerImpl*, Guid*, void**, int>)&ID2D1ResourceTextureManagerInternalMethods.QueryInterface;
|
||||
lpVtbl[5 + 1] = (delegate* unmanaged<D2D1ResourceTextureManagerImpl*, uint>)&ID2D1ResourceTextureManagerInternalMethods.AddRef;
|
||||
lpVtbl[5 + 2] = (delegate* unmanaged<D2D1ResourceTextureManagerImpl*, uint>)&ID2D1ResourceTextureManagerInternalMethods.Release;
|
||||
lpVtbl[5 + 3] = (delegate* unmanaged<D2D1ResourceTextureManagerImpl*, ID2D1EffectContext*, int>)&ID2D1ResourceTextureManagerInternalMethods.SetEffectContext;
|
||||
lpVtbl[5 + 3] = (delegate* unmanaged<D2D1ResourceTextureManagerImpl*, ID2D1EffectContext*, uint*, int>)&ID2D1ResourceTextureManagerInternalMethods.Initialize;
|
||||
lpVtbl[5 + 4] = (delegate* unmanaged<D2D1ResourceTextureManagerImpl*, ID2D1ResourceTexture**, int>)&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
|
||||
/// </summary>
|
||||
private GCHandle lockHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The expected dimensions for the slot the manager has been assigned to in an effect.
|
||||
/// </summary>
|
||||
/// <remarks>If <c>0</c>, it has not been set, so it can be ignored.</remarks>
|
||||
private uint expectedDimensions;
|
||||
|
||||
/// <summary>
|
||||
/// The factory method for <see cref="D2D1ResourceTextureManagerImpl"/> instances.
|
||||
/// </summary>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+5
-3
@@ -47,13 +47,15 @@ internal unsafe struct ID2D1ResourceTextureManagerInternal
|
||||
/// Sets the <see cref="ID2D1EffectContext"/> for the current <c>ID2D1ResourceTextureManagerInternal</c> instance.
|
||||
/// </summary>
|
||||
/// <param name="effectContext">The input <see cref="ID2D1EffectContext"/> for the manager.</param>
|
||||
/// <param name="dimensions">A pointer to an optional amount of dimensions for the target resource texture, for validation.</param>
|
||||
/// <returns>An <see cref="HRESULT"/> for the operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int SetEffectContext(ID2D1EffectContext* effectContext)
|
||||
public int Initialize(ID2D1EffectContext* effectContext, uint* dimensions)
|
||||
{
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1ResourceTextureManagerInternal*, ID2D1EffectContext*, int>)this.lpVtbl[3])(
|
||||
return ((delegate* unmanaged[Stdcall]<ID2D1ResourceTextureManagerInternal*, ID2D1EffectContext*, uint*, int>)this.lpVtbl[3])(
|
||||
(ID2D1ResourceTextureManagerInternal*)Unsafe.AsPointer(ref this),
|
||||
effectContext);
|
||||
effectContext,
|
||||
dimensions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする