Add 'ID2D1PixelShaderDescriptor<T>.EffectFactory'

このコミットが含まれているのは:
Sergio Pedri committed 2025-04-12 13:46:20 -07:00
1 親 2bd64c93d7
コミット 53f447a8c5
4ファイル変更
+41 -40

検索条件に一致するファイルはありません

+13
ファイルの表示
@@ -52,6 +52,19 @@ public interface ID2D1PixelShaderDescriptor<T>
/// </remarks>
static abstract string? EffectAuthor { get; }
/// <summary>
/// Gets a pointer to the effect factory for the current shader.
/// </summary>
/// <remarks>
/// <para>
/// This only applies to effects created from <see cref="D2D1PixelShaderEffect"/>.
/// </para>
/// <para>
/// The returned function pointer should have a signature matching <a href="https://learn.microsoft.com/windows/win32/api/d2d1_1/nc-d2d1_1-pd2d1_effect_factory"><c>PD2D1_EFFECT_FACTORY</c></a>.
/// </para>
/// </remarks>
static abstract nint EffectFactory { get; }
/// <summary>
/// Gets the size in bytes of the constant buffer for the current shader.
/// </summary>
+26 -2
ファイルの表示
@@ -72,6 +72,30 @@ public static unsafe class D2D1PixelShaderEffect
return T.EffectAuthor;
}
/// <summary>
/// Creates a D2D effect instance for a given pixel shader type.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader to create a D2D effect instance for.</typeparam>
/// <param name="effectImpl">The effect implementation returned by the factory.</param>
/// <returns>The error code for the operation.</returns>
/// <remarks>
/// <para>
/// This method provides the <a href="https://learn.microsoft.com/windows/win32/api/d2d1_1/nc-d2d1_1-pd2d1_effect_factory"><c>PD2D1_EFFECT_FACTORY</c></a>
/// implementation for a given pixel shader type <typeparamref name="T"/>. It is only meant to be used to register an effect with this shader type, and
/// should only ever be used implicitly by the ComputeSharp infrastructure, or in advanced scenarios where effects are registered manually.
/// </para>
/// <para>
/// This method is guaranteed to never throw an exception, and can be used directly in a method exported to native code.
/// The main use case scenario for calling <see cref="CreateEffectUnsafe"/> is to produce the factory for
/// <a href="https://learn.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"><c>ID2D1Factory1::RegisterEffectFromString</c></a>.
/// </para>
/// </remarks>
public static int CreateEffectUnsafe<T>(void** effectImpl)
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
{
return PixelShaderEffect.Factory(PixelShaderEffect.Globals<T>.Instance, (IUnknown**)effectImpl);
}
/// <summary>
/// Registers an effect from an input D2D1 pixel shader, by calling <c>ID2D1Factory1::RegisterEffectFromString</c>.
/// </summary>
@@ -184,7 +208,7 @@ public static unsafe class D2D1PixelShaderEffect
propertyXml: (ushort*)pXml,
bindings: d2D1PropertyBinding,
bindingsCount: (uint)(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + T.ResourceTextureCount),
effectFactory: PixelShaderEffect.Globals<T>.Instance.Factory).Assert();
effectFactory: (delegate* unmanaged<IUnknown**, HRESULT>)T.EffectFactory).Assert();
}
effectId = T.EffectId;
@@ -376,7 +400,7 @@ public static unsafe class D2D1PixelShaderEffect
}
// Effect factory
writer.Write((nint)PixelShaderEffect.Globals<T>.Instance.Factory);
writer.Write(T.EffectFactory);
byte[] registrationBlob = writer.WrittenSpan.ToArray();
+1 -37
ファイルの表示
@@ -1,6 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.Descriptors;
using ComputeSharp.Win32;
@@ -11,29 +9,11 @@ namespace ComputeSharp.D2D1.Interop.Effects;
/// </summary>
internal unsafe partial struct PixelShaderEffect
{
/// <summary>
/// A wrapper for an effect factory.
/// </summary>
/// <param name="effectImpl">The resulting effect factory.</param>
/// <returns>The <c>HRESULT</c> for the operation.</returns>
/// <remarks>
/// The return type is intentionally <see langword="void"/><c>*</c> because delegate targets are considered
/// visible for reflection, so not using <see cref="IUnknown"/> avoids metadata for it being rooted unnecessarily.
/// </remarks>
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int FactoryDelegate(void** effectImpl);
/// <summary>
/// A base type with global values for pixel shader effects.
/// </summary>
/// <param name="effectFactory">The <see cref="FactoryDelegate"/> wrapper for the shader factory.</param>
public abstract class Globals(FactoryDelegate effectFactory)
public abstract class Globals
{
/// <summary>
/// Gets the <see cref="FactoryDelegate"/> wrapper for the shader factory.
/// </summary>
private readonly FactoryDelegate effectFactory = effectFactory;
/// <inheritdoc cref="ID2D1PixelShaderDescriptor{T}.EffectId"/>
public abstract ref readonly Guid EffectId { get; }
@@ -66,15 +46,6 @@ internal unsafe partial struct PixelShaderEffect
/// <inheritdoc cref="ID2D1PixelShaderDescriptor{T}.HlslBytecode"/>
public abstract ReadOnlyMemory<byte> HlslBytecode { get; }
/// <summary>
/// Gets the factory for the current effect.
/// </summary>
public delegate* unmanaged<IUnknown**, HRESULT> Factory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (delegate* unmanaged<IUnknown**, HRESULT>)Marshal.GetFunctionPointerForDelegate(this.effectFactory);
}
}
/// <summary>
@@ -93,7 +64,6 @@ internal unsafe partial struct PixelShaderEffect
/// Creates a new <see cref="Globals"/> instance for shaders of type <typeparamref name="T"/>.
/// </summary>
private Globals()
: base(CreateEffect)
{
}
@@ -147,11 +117,5 @@ internal unsafe partial struct PixelShaderEffect
return this.hlslBytecode = D2D1PixelShader.LoadBytecode<T>();
}
}
/// <inheritdoc cref="FactoryDelegate"/>
private static int CreateEffect(void** effectImpl)
{
return PixelShaderEffect.Factory(Instance, (IUnknown**)effectImpl);
}
}
}
+1 -1
ファイルの表示
@@ -103,7 +103,7 @@ internal unsafe partial struct PixelShaderEffect
/// <param name="globals">The <see cref="Globals"/> instance to use.</param>
/// <param name="effectImpl">The resulting effect instance.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
private static int Factory(Globals globals, IUnknown** effectImpl)
public static int Factory(Globals globals, IUnknown** effectImpl)
{
PixelShaderEffect* @this = null;
GCHandle globalsHandle = default;