diff --git a/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs index 66f3cc9b..9283fc99 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/EndToEndTests.cs @@ -127,15 +127,19 @@ public class EndToEndTests { D2D1_PROPERTY_BINDING d2D1PropertyBinding; d2D1PropertyBinding.propertyName = (ushort*)pPropertyName; - d2D1PropertyBinding.getFunction = (delegate* unmanaged)(delegate* unmanaged)&PixelShaderEffect.GetConstantBuffer; - d2D1PropertyBinding.setFunction = (delegate* unmanaged)(delegate* unmanaged)&PixelShaderEffect.SetConstantBuffer; + d2D1PropertyBinding.getFunction = + (delegate* unmanaged) + (delegate* unmanaged)&PixelShaderEffect.GetConstantBuffer; + d2D1PropertyBinding.setFunction = + (delegate* unmanaged) + (delegate* unmanaged)&PixelShaderEffect.SetConstantBuffer; d2D1Factory2.Get()->RegisterEffectFromString( classId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)), propertyXml: (ushort*)pXml, bindings: &d2D1PropertyBinding, bindingsCount: 1, - effectFactory: (delegate* unmanaged)(delegate* unmanaged)&PixelShaderEffect.Factory).Assert(); + effectFactory: PixelShaderEffect.For.Factory).Assert(); } using ComPtr d2D1Effect = default; @@ -145,6 +149,10 @@ public class EndToEndTests effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)), effect: d2D1Effect.GetAddressOf()).Assert(); + float number = 1; + + d2D1Effect.Get()->SetValue(0, D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB, (byte*)&number, sizeof(float)).Assert(); + d2D1Effect.Get()->SetInput(0, (ID2D1Image*)d2D1BitmapSource.Get()); d2D1DeviceContext.Get()->BeginDraw(); @@ -199,12 +207,14 @@ public class EndToEndTests [D2DInputCount(1)] [D2DInputSimple(0)] [D2DEmbeddedBytecode(D2D1ShaderProfile.PixelShader50)] -public readonly partial struct InvertShader : ID2D1PixelShader +public partial struct InvertShader : ID2D1PixelShader { + public float number; + public float4 Execute() { float4 color = D2D1.GetInput(0); - float3 rgb = Hlsl.Saturate(1 - color.RGB); + float3 rgb = Hlsl.Saturate(this.number - color.RGB); return new(rgb, 1); } diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.Transform.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.Transform.cs index 94e509b5..4b2836a1 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.Transform.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.Transform.cs @@ -60,6 +60,16 @@ partial struct PixelShaderEffect /// private ID2D1DrawInfo* d2D1DrawInfo; + /// + /// The for the shader. + /// + private Guid shaderId; + + /// + /// The number of inputs for the shader. + /// + private int numberOfInputs; + /// /// Gets the instance currently in use. /// @@ -68,8 +78,10 @@ partial struct PixelShaderEffect /// /// Creates and initializes a new instance. /// + /// The for the shader. + /// The number of inputs for the shader. /// A new instance. - public static PixelShaderTransform* Create() + public static PixelShaderTransform* Create(Guid shaderId, int numberOfInpputs) { PixelShaderTransform* @this = (PixelShaderTransform*)NativeMemory.Alloc((nuint)sizeof(PixelShaderTransform)); @@ -77,6 +89,8 @@ partial struct PixelShaderEffect @this->referenceCount = 1; @this->inputRect = default; @this->d2D1DrawInfo = null; + @this->shaderId = shaderId; + @this->numberOfInputs = numberOfInpputs; return @this; } @@ -132,7 +146,7 @@ partial struct PixelShaderEffect [UnmanagedCallersOnly] private static uint GetInputCount(PixelShaderTransform* @this) { - return 1; + return (uint)@this->numberOfInputs; } /// @@ -182,7 +196,7 @@ partial struct PixelShaderEffect { @this->d2D1DrawInfo = drawInfo; - return drawInfo->SetPixelShader((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID))); + return drawInfo->SetPixelShader(&@this->shaderId); } } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs index 8f29d0a7..2165b1b8 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs @@ -12,6 +12,71 @@ namespace ComputeSharp.D2D1Interop.Tests.Helpers; /// internal unsafe partial struct PixelShaderEffect { + /// + /// A generic pixel shader implementation. + /// + /// The type of shader. + public static class For + where T : unmanaged, ID2D1PixelShader + { + /// + /// The for the shader. + /// + private static readonly Guid shaderId; + + /// + /// The shader bytecode. + /// + private static readonly byte* bytecode; + + /// + /// The size of . + /// + private static readonly int bytecodeSize; + + /// + /// The number of inputs for the shader. + /// + private static readonly int numberOfInputs; + + /// + /// The wrapper for the shader factory. + /// + private static readonly FactoryDelegate EffectFactory = CreateEffect; + + /// + /// The static constructor for . + /// + static For() + { + shaderId = typeof(T).GUID; + + ReadOnlyMemory buffer = D2D1InteropServices.LoadShaderBytecode(); + + bytecode = (byte*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For), buffer.Length); + + buffer.Span.CopyTo(new Span(bytecode, buffer.Length)); + + bytecodeSize = buffer.Length; + + numberOfInputs = ((D2DInputCountAttribute[])typeof(T).GetCustomAttributes(typeof(D2DInputCountAttribute), false))[0].Count; + } + + /// + /// Gets the factory for the current effect. + /// + public static delegate* unmanaged Factory + { + get => (delegate* unmanaged)Marshal.GetFunctionPointerForDelegate(EffectFactory); + } + + /// + private static int CreateEffect(IUnknown** effectImpl) + { + return PixelShaderEffect.Factory(shaderId, bytecode, bytecodeSize, numberOfInputs, effectImpl); + } + } + /// /// The shared vtable pointer for instances. /// @@ -60,6 +125,26 @@ internal unsafe partial struct PixelShaderEffect /// private int constantBufferSize; + /// + /// The for the shader. + /// + private Guid shaderId; + + /// + /// The shader bytecode. + /// + private byte* bytecode; + + /// + /// The size of . + /// + private int bytecodeSize; + + /// + /// The number of inputs for the shader. + /// + private int numberOfInputs; + /// /// Creates and initializes a new instance. /// @@ -77,18 +162,33 @@ internal unsafe partial struct PixelShaderEffect return @this; } + /// + /// A wrapper for an effect factory. + /// + /// The resulting effect factory. + /// The HRESULT for the operation. + [UnmanagedFunctionPointer(CallingConvention.Winapi)] + private delegate int FactoryDelegate(IUnknown** effectImpl); + /// /// The factory method for . /// + /// The for the shader. + /// The shader bytecode. /// The resulting effect instance. + /// The size of . + /// The number of inputs for the shader. /// This always returns 0. - [UnmanagedCallersOnly] - public static int Factory(IUnknown** effectImpl) + private static int Factory(Guid shaderId, byte* bytecode, int bytecodeSize, int numberOfInpputs, IUnknown** effectImpl) { PixelShaderEffect* pixelShaderEffect = Create(); - PixelShaderTransform* pixelShaderTransform = PixelShaderTransform.Create(); + PixelShaderTransform* pixelShaderTransform = PixelShaderTransform.Create(shaderId, numberOfInpputs); pixelShaderEffect->pixelShaderTransform = pixelShaderTransform; + pixelShaderEffect->shaderId = shaderId; + pixelShaderEffect->bytecode = bytecode; + pixelShaderEffect->bytecodeSize = bytecodeSize; + pixelShaderEffect->numberOfInputs = numberOfInpputs; *effectImpl = (IUnknown*)pixelShaderEffect; @@ -194,17 +294,10 @@ internal unsafe partial struct PixelShaderEffect [UnmanagedCallersOnly] private static int Initialize(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph) { - ReadOnlyMemory bytecode = D2D1InteropServices.LoadShaderBytecode(); - - int hresult; - - fixed (byte* bytecodePtr = bytecode.Span) - { - hresult = effectContext->LoadPixelShader( - shaderId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID)), - shaderBuffer: bytecodePtr, - shaderBufferCount: (uint)bytecode.Length); - } + int hresult = effectContext->LoadPixelShader( + shaderId: &@this->shaderId, + shaderBuffer: @this->bytecode, + shaderBufferCount: (uint)@this->bytecodeSize); if (Windows.SUCCEEDED(hresult)) {