From 53f447a8c5cbe26adc483a75414b2fcd6bb4f88b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 10 Apr 2025 22:59:20 -0700 Subject: [PATCH 1/4] Add 'ID2D1PixelShaderDescriptor.EffectFactory' --- .../ID2D1PixelShaderDescriptor{T}.cs | 13 +++++++ .../Shaders/Interop/D2D1PixelShaderEffect.cs | 28 +++++++++++++- .../Effects/PixelShaderEffect.Globals{T}.cs | 38 +------------------ .../Interop/Effects/PixelShaderEffect.cs | 2 +- 4 files changed, 41 insertions(+), 40 deletions(-) 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; From 80b6f556d79a23b3a61b1f928ebb0de13de1110b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 10 Apr 2025 23:13:28 -0700 Subject: [PATCH 2/4] Update generator to emit 'EffectFactory' property --- ...escriptorGenerator.EffectFactory.Syntax.cs | 46 +++++++++++++++++++ .../D2DPixelShaderDescriptorGenerator.cs | 1 + 2 files changed, 47 insertions(+) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectFactory.Syntax.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectFactory.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectFactory.Syntax.cs new file mode 100644 index 00000000..c69a6f24 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.EffectFactory.Syntax.cs @@ -0,0 +1,46 @@ +using ComputeSharp.D2D1.SourceGenerators.Models; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; + +namespace ComputeSharp.D2D1.SourceGenerators; + +/// +partial class D2DPixelShaderDescriptorGenerator +{ + /// + /// A helper with all logic to generate the EffectFactory property. + /// + private static class EffectFactory + { + /// + /// Writes the EffectFactory property. + /// + /// The input instance with gathered shader info. + /// The instance to write into. + public static void WriteSyntax(D2D1ShaderInfo info, IndentedTextWriter writer) + { + writer.WriteLine("/// "); + writer.WriteGeneratedAttributes(GeneratorName); + writer.WriteLine($"static unsafe nint global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor<{info.Hierarchy.Hierarchy[0].QualifiedName}>.EffectFactory"); + + using (writer.WriteBlock()) + { + writer.WriteLine("[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]"); + writer.WriteLine("get"); + + using (writer.WriteBlock()) + { + writer.WriteLine($$""" + [global::System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = [typeof(global::System.Runtime.CompilerServices.CallConvStdcall)])] + static int EffectFactory(void** effectImpl) + { + return global::ComputeSharp.D2D1.Interop.D2D1PixelShaderEffect.CreateEffectUnsafe<{{info.Hierarchy.Hierarchy[0].QualifiedName}}>(effectImpl); + } + + return (nint)(delegate* unmanaged[Stdcall])&EffectFactory; + """, isMultiline: true); + } + } + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs index 40f6ca7a..1d145909 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.cs @@ -225,6 +225,7 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene declaredMembers.Add(EffectMetadata.WriteEffectDescriptionSyntax); declaredMembers.Add(EffectMetadata.WriteEffectCategorySyntax); declaredMembers.Add(EffectMetadata.WriteEffectAuthorSyntax); + declaredMembers.Add(EffectFactory.WriteSyntax); declaredMembers.Add(NumericProperties.WriteConstantBufferSizeSyntax); declaredMembers.Add(NumericProperties.WriteInputCountSyntax); declaredMembers.Add(NumericProperties.WriteResourceTextureCountSyntax); From bf354d5abe9d64eaa11aefd4b9cf16eeda9fe0c6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 10 Apr 2025 23:30:39 -0700 Subject: [PATCH 3/4] Update source generator unit tests --- .../Test_D2DPixelShaderDescriptorGenerator.cs | 114 ++++++++++++++++++ ...ixelShaderDescriptorGenerator_Analyzers.cs | 2 + 2 files changed, 116 insertions(+) diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs index ec2379b3..a0731c5f 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator.cs @@ -102,6 +102,25 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] static string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectAuthor => null; + /// + [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] + [global::System.Diagnostics.DebuggerNonUserCode] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + static unsafe nint global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectFactory + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + [global::System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = [typeof(global::System.Runtime.CompilerServices.CallConvStdcall)])] + static int EffectFactory(void** effectImpl) + { + return global::ComputeSharp.D2D1.Interop.D2D1PixelShaderEffect.CreateEffectUnsafe(effectImpl); + } + + return (nint)(delegate* unmanaged[Stdcall])&EffectFactory; + } + } + /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] @@ -402,6 +421,25 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] static string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectAuthor => null; + /// + [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] + [global::System.Diagnostics.DebuggerNonUserCode] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + static unsafe nint global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectFactory + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + [global::System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = [typeof(global::System.Runtime.CompilerServices.CallConvStdcall)])] + static int EffectFactory(void** effectImpl) + { + return global::ComputeSharp.D2D1.Interop.D2D1PixelShaderEffect.CreateEffectUnsafe(effectImpl); + } + + return (nint)(delegate* unmanaged[Stdcall])&EffectFactory; + } + } + /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] @@ -754,6 +792,25 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] static string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectAuthor => "ComputeSharp.D2D1"; + /// + [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] + [global::System.Diagnostics.DebuggerNonUserCode] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + static unsafe nint global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectFactory + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + [global::System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = [typeof(global::System.Runtime.CompilerServices.CallConvStdcall)])] + static int EffectFactory(void** effectImpl) + { + return global::ComputeSharp.D2D1.Interop.D2D1PixelShaderEffect.CreateEffectUnsafe(effectImpl); + } + + return (nint)(delegate* unmanaged[Stdcall])&EffectFactory; + } + } + /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] @@ -1052,6 +1109,25 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] static string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectAuthor => null; + /// + [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] + [global::System.Diagnostics.DebuggerNonUserCode] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + static unsafe nint global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectFactory + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + [global::System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = [typeof(global::System.Runtime.CompilerServices.CallConvStdcall)])] + static int EffectFactory(void** effectImpl) + { + return global::ComputeSharp.D2D1.Interop.D2D1PixelShaderEffect.CreateEffectUnsafe(effectImpl); + } + + return (nint)(delegate* unmanaged[Stdcall])&EffectFactory; + } + } + /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] @@ -1347,6 +1423,25 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] static string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectAuthor => null; + /// + [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] + [global::System.Diagnostics.DebuggerNonUserCode] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + static unsafe nint global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectFactory + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + [global::System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = [typeof(global::System.Runtime.CompilerServices.CallConvStdcall)])] + static int EffectFactory(void** effectImpl) + { + return global::ComputeSharp.D2D1.Interop.D2D1PixelShaderEffect.CreateEffectUnsafe(effectImpl); + } + + return (nint)(delegate* unmanaged[Stdcall])&EffectFactory; + } + } + /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] @@ -1630,6 +1725,25 @@ public class Test_D2DPixelShaderDescriptorGenerator [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] static string? global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectAuthor => null; + /// + [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] + [global::System.Diagnostics.DebuggerNonUserCode] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + static unsafe nint global::ComputeSharp.D2D1.Descriptors.ID2D1PixelShaderDescriptor.EffectFactory + { + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + get + { + [global::System.Runtime.InteropServices.UnmanagedCallersOnly(CallConvs = [typeof(global::System.Runtime.CompilerServices.CallConvStdcall)])] + static int EffectFactory(void** effectImpl) + { + return global::ComputeSharp.D2D1.Interop.D2D1PixelShaderEffect.CreateEffectUnsafe(effectImpl); + } + + return (nint)(delegate* unmanaged[Stdcall])&EffectFactory; + } + } + /// [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.D2D1.D2DPixelShaderDescriptorGenerator", )] [global::System.Diagnostics.DebuggerNonUserCode] diff --git a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs index cc3c4a79..42268da0 100644 --- a/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs +++ b/tests/ComputeSharp.D2D1.Tests.SourceGenerators/Test_D2DPixelShaderDescriptorGenerator_Analyzers.cs @@ -69,6 +69,8 @@ public class Test_D2DPixelShaderDescriptorGenerator_Analyzers public static string? EffectAuthor => throw new NotImplementedException(); + public static nint EffectFactory => throw new NotImplementedException(); + public static int ConstantBufferSize => throw new NotImplementedException(); public static int InputCount => throw new NotImplementedException(); From 7a7d4e75c1f9c95cb3622fb6af56986e411a625b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 12 Apr 2025 15:41:15 -0700 Subject: [PATCH 4/4] Bump version to 3.2.0 --- build/Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/Directory.Build.props b/build/Directory.Build.props index cb5ae82b..4ec16389 100644 --- a/build/Directory.Build.props +++ b/build/Directory.Build.props @@ -72,7 +72,7 @@ As such, this needs to be changed before a new release as well. --> - 3.1.0 + 3.2.0 false