diff --git a/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs b/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs index 8dfddd6e..7cd7cbbc 100644 --- a/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs +++ b/src/ComputeSharp.D2D1/Interfaces/Descriptors/ID2D1PixelShaderDescriptor{T}.cs @@ -52,6 +52,19 @@ public interface ID2D1PixelShaderDescriptor /// static abstract string? EffectAuthor { get; } + /// + /// Gets a pointer to the effect factory for the current shader. + /// + /// + /// + /// This only applies to effects created from . + /// + /// + /// The returned function pointer should have a signature matching PD2D1_EFFECT_FACTORY. + /// + /// + static abstract nint EffectFactory { get; } + /// /// Gets the size in bytes of the constant buffer for the current shader. /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index f4ff4a8d..b4bf83a2 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -72,6 +72,30 @@ public static unsafe class D2D1PixelShaderEffect return T.EffectAuthor; } + /// + /// Creates a D2D effect instance for a given pixel shader type. + /// + /// The type of D2D1 pixel shader to create a D2D effect instance for. + /// The effect implementation returned by the factory. + /// The error code for the operation. + /// + /// + /// This method provides the PD2D1_EFFECT_FACTORY + /// implementation for a given pixel shader type . 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. + /// + /// + /// 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 is to produce the factory for + /// ID2D1Factory1::RegisterEffectFromString. + /// + /// + public static int CreateEffectUnsafe(void** effectImpl) + where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor + { + return PixelShaderEffect.Factory(PixelShaderEffect.Globals.Instance, (IUnknown**)effectImpl); + } + /// /// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString. /// @@ -184,7 +208,7 @@ public static unsafe class D2D1PixelShaderEffect propertyXml: (ushort*)pXml, bindings: d2D1PropertyBinding, bindingsCount: (uint)(D2D1PixelShaderEffectProperty.NumberOfAlwaysAvailableProperties + T.ResourceTextureCount), - effectFactory: PixelShaderEffect.Globals.Instance.Factory).Assert(); + effectFactory: (delegate* unmanaged)T.EffectFactory).Assert(); } effectId = T.EffectId; @@ -376,7 +400,7 @@ public static unsafe class D2D1PixelShaderEffect } // Effect factory - writer.Write((nint)PixelShaderEffect.Globals.Instance.Factory); + writer.Write(T.EffectFactory); byte[] registrationBlob = writer.WrittenSpan.ToArray(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs index 93264977..0f92fed0 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.Globals{T}.cs @@ -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; /// internal unsafe partial struct PixelShaderEffect { - /// - /// A wrapper for an effect factory. - /// - /// The resulting effect factory. - /// The HRESULT for the operation. - /// - /// The return type is intentionally * because delegate targets are considered - /// visible for reflection, so not using avoids metadata for it being rooted unnecessarily. - /// - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate int FactoryDelegate(void** effectImpl); - /// /// A base type with global values for pixel shader effects. /// - /// The wrapper for the shader factory. - public abstract class Globals(FactoryDelegate effectFactory) + public abstract class Globals { - /// - /// Gets the wrapper for the shader factory. - /// - private readonly FactoryDelegate effectFactory = effectFactory; - /// public abstract ref readonly Guid EffectId { get; } @@ -66,15 +46,6 @@ internal unsafe partial struct PixelShaderEffect /// public abstract ReadOnlyMemory HlslBytecode { get; } - - /// - /// Gets the factory for the current effect. - /// - public delegate* unmanaged Factory - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(this.effectFactory); - } } /// @@ -93,7 +64,6 @@ internal unsafe partial struct PixelShaderEffect /// Creates a new instance for shaders of type . /// private Globals() - : base(CreateEffect) { } @@ -147,11 +117,5 @@ internal unsafe partial struct PixelShaderEffect return this.hlslBytecode = D2D1PixelShader.LoadBytecode(); } } - - /// - private static int CreateEffect(void** effectImpl) - { - return PixelShaderEffect.Factory(Instance, (IUnknown**)effectImpl); - } } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index 740026c5..c39259db 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -103,7 +103,7 @@ internal unsafe partial struct PixelShaderEffect /// The instance to use. /// The resulting effect instance. /// The for the operation. - private static int Factory(Globals globals, IUnknown** effectImpl) + public static int Factory(Globals globals, IUnknown** effectImpl) { PixelShaderEffect* @this = null; GCHandle globalsHandle = default;