From 5899a6667af956782020ba3c4be5c70f4b43cc5f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 17 Apr 2022 22:05:51 +0200 Subject: [PATCH] Minor code refactoring, centralize effect id management --- .../Shaders/Interop/D2D1InteropServices.Effects.cs | 6 ++---- .../Shaders/Interop/Effects/PixelShaderEffect.cs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs index 3be601a8..e7daf335 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs @@ -109,17 +109,15 @@ unsafe partial class D2D1InteropServices (void*)Marshal.GetFunctionPointerForDelegate(PixelShaderEffect.SetConstantBufferWrapper); #endif - Guid shaderId = typeof(T).GUID; - // Register the effect ((ID2D1Factory1*)d2D1Factory1)->RegisterEffectFromString( - classId: &shaderId, + classId: PixelShaderEffect.For.Id, propertyXml: (ushort*)pXml, bindings: &d2D1PropertyBinding, bindingsCount: 1, effectFactory: PixelShaderEffect.For.Factory).Assert(); - effectId = shaderId; + effectId = *PixelShaderEffect.For.Id; } } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs index c5d6ebc0..a33c0d17 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/PixelShaderEffect.cs @@ -79,6 +79,7 @@ internal unsafe partial struct PixelShaderEffect /// /// The for the shader. /// + [FixedAddressValueType] private static Guid shaderId; /// @@ -125,7 +126,7 @@ internal unsafe partial struct PixelShaderEffect else { // Load all shader properties - Guid guid = Guid.NewGuid(); + Guid guid = typeof(T).GUID; int inputCount = ((D2DInputCountAttribute[])typeof(T).GetCustomAttributes(typeof(D2DInputCountAttribute), false))[0].Count; ReadOnlyMemory buffer = D2D1InteropServices.LoadShaderBytecode(); byte* typeAssociatedMemory = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), buffer.Length); @@ -145,11 +146,21 @@ internal unsafe partial struct PixelShaderEffect } } + /// + /// Gets a pointer to the id of the effect. + /// + public static Guid* Id + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => (Guid*)Unsafe.AsPointer(ref shaderId); + } + /// /// Gets the factory for the current effect. /// public static delegate* unmanaged[Stdcall] Factory { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get => (delegate* unmanaged[Stdcall])Marshal.GetFunctionPointerForDelegate(EffectFactory); }