diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.ID2D1DrawTransformMethods.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.ID2D1DrawTransformMethods.cs new file mode 100644 index 00000000..99b64e9d --- /dev/null +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.ID2D1DrawTransformMethods.cs @@ -0,0 +1,110 @@ +using System; +using System.Runtime.InteropServices; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1Interop.Tests.Helpers; + +/// +partial struct PixelShaderEffect +{ + /// + /// The implementation for . + /// + private unsafe static class ID2D1DrawTransformMethods + { + /// + [UnmanagedCallersOnly] + public static int QueryInterface(PixelShaderEffect* @this, Guid* riid, void** ppvObject) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + return @this->QueryInterface(riid, ppvObject); + } + + /// + [UnmanagedCallersOnly] + public static uint AddRef(PixelShaderEffect* @this) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + return @this->AddRef(); + } + + /// + [UnmanagedCallersOnly] + public static uint Release(PixelShaderEffect* @this) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + return @this->Release(); + } + + /// + [UnmanagedCallersOnly] + public static uint GetInputCount(PixelShaderEffect* @this) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + return (uint)@this->numberOfInputs; + } + + /// + [UnmanagedCallersOnly] + public static int MapOutputRectToInputRects(PixelShaderEffect* @this, RECT* outputRect, RECT* inputRects, uint inputRectsCount) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + if (inputRectsCount != 1) + { + return E.E_INVALIDARG; + } + + inputRects[0] = *outputRect; + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly] + public static int MapInputRectsToOutputRect(PixelShaderEffect* @this, RECT* inputRects, RECT* inputOpaqueSubRects, uint inputRectCount, RECT* outputRect, RECT* outputOpaqueSubRect) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + if (inputRectCount != 1) + { + return E.E_INVALIDARG; + } + + *outputRect = inputRects[0]; + + @this->inputRect = inputRects[0]; + + *outputOpaqueSubRect = default; + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly] + public static int MapInvalidRect(PixelShaderEffect* @this, uint inputIndex, RECT invalidInputRect, RECT* invalidOutputRect) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + *invalidOutputRect = @this->inputRect; + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly] + public static int SetDrawInfo(PixelShaderEffect* @this, ID2D1DrawInfo* drawInfo) + { + @this = (PixelShaderEffect*)&((void**)@this)[-1]; + + @this->d2D1DrawInfo = drawInfo; + + return drawInfo->SetPixelShader(&@this->shaderId); + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.ID2D1EffectImplMethods.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.ID2D1EffectImplMethods.cs new file mode 100644 index 00000000..0ff6cb33 --- /dev/null +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.ID2D1EffectImplMethods.cs @@ -0,0 +1,77 @@ +using System; +using System.Runtime.InteropServices; +using TerraFX.Interop.DirectX; +using TerraFX.Interop.Windows; + +namespace ComputeSharp.D2D1Interop.Tests.Helpers; + +/// +/// A simple and implementation for a given pixel shader. +/// +internal unsafe partial struct PixelShaderEffect +{ + /// + /// The implementation for . + /// + private static class ID2D1EffectImplMethods + { + /// + [UnmanagedCallersOnly] + public static int QueryInterface(PixelShaderEffect* @this, Guid* riid, void** ppvObject) + { + return @this->QueryInterface(riid, ppvObject); + } + + /// + [UnmanagedCallersOnly] + public static uint AddRef(PixelShaderEffect* @this) + { + return @this->AddRef(); + } + + /// + [UnmanagedCallersOnly] + public static uint Release(PixelShaderEffect* @this) + { + return @this->Release(); + } + + /// + [UnmanagedCallersOnly] + public static int Initialize(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph) + { + int hresult = effectContext->LoadPixelShader( + shaderId: &@this->shaderId, + shaderBuffer: @this->bytecode, + shaderBufferCount: (uint)@this->bytecodeSize); + + if (Windows.SUCCEEDED(hresult)) + { + hresult = transformGraph->SetSingleTransformNode((ID2D1TransformNode*)&@this->lpVtblForID2D1DrawTransform); + } + + return hresult; + } + + /// + [UnmanagedCallersOnly] + public static int PrepareForRender(PixelShaderEffect* @this, D2D1_CHANGE_TYPE changeType) + { + if (@this->constantBuffer is not null) + { + return @this->d2D1DrawInfo->SetPixelShaderConstantBuffer( + buffer: @this->constantBuffer, + bufferCount: (uint)@this->constantBufferSize); + } + + return S.S_OK; + } + + /// + [UnmanagedCallersOnly] + public static int SetGraph(PixelShaderEffect* @this, ID2D1TransformGraph* transformGraph) + { + return E.E_NOTIMPL; + } + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.Transform.cs b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.Transform.cs deleted file mode 100644 index 4b2836a1..00000000 --- a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.Transform.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Threading; -using TerraFX.Interop.DirectX; -using TerraFX.Interop.Windows; - -namespace ComputeSharp.D2D1Interop.Tests.Helpers; - -/// -partial struct PixelShaderEffect -{ - /// - /// A simple implementation. - /// - private unsafe struct PixelShaderTransform - { - /// - /// The shared vtable pointer for instances. - /// - private static readonly void** Vtbl = InitVtbl(); - - /// - /// Setups the vtable pointer for . - /// - /// The initialized vtable pointer for . - private static void** InitVtbl() - { - void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(PixelShaderTransform), sizeof(void*) * 8); - - lpVtbl[0] = (delegate* unmanaged)&QueryInterface; - lpVtbl[1] = (delegate* unmanaged)&AddRef; - lpVtbl[2] = (delegate* unmanaged)&Release; - lpVtbl[3] = (delegate* unmanaged)&GetInputCount; - lpVtbl[4] = (delegate* unmanaged)&MapOutputRectToInputRects; - lpVtbl[5] = (delegate* unmanaged)&MapInputRectsToOutputRect; - lpVtbl[6] = (delegate* unmanaged)&MapInvalidRect; - lpVtbl[7] = (delegate* unmanaged)&SetDrawInfo; - - return lpVtbl; - } - - /// - /// The vtable pointer for the current instance. - /// - private void** lpVtbl; - - /// - /// The current reference count for the object (from IUnknown). - /// - private volatile int referenceCount; - - /// - /// The current input rectangle. - /// - private RECT inputRect; - - /// - /// The instance currently in use. - /// - 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. - /// - public ID2D1DrawInfo* D2D1DrawInfo => this.d2D1DrawInfo; - - /// - /// Creates and initializes a new instance. - /// - /// The for the shader. - /// The number of inputs for the shader. - /// A new instance. - public static PixelShaderTransform* Create(Guid shaderId, int numberOfInpputs) - { - PixelShaderTransform* @this = (PixelShaderTransform*)NativeMemory.Alloc((nuint)sizeof(PixelShaderTransform)); - - @this->lpVtbl = Vtbl; - @this->referenceCount = 1; - @this->inputRect = default; - @this->d2D1DrawInfo = null; - @this->shaderId = shaderId; - @this->numberOfInputs = numberOfInpputs; - - return @this; - } - - /// - [UnmanagedCallersOnly] - private static int QueryInterface(PixelShaderTransform* @this, Guid* riid, void** ppvObject) - { - if (ppvObject is null) - { - return E.E_POINTER; - } - - if (riid->Equals(Windows.__uuidof()) || - riid->Equals(Windows.__uuidof()) || - riid->Equals(Windows.__uuidof()) || - riid->Equals(Windows.__uuidof())) - { - _ = Interlocked.Increment(ref @this->referenceCount); - - *ppvObject = @this; - - return S.S_OK; - } - - *ppvObject = null; - - return E.E_NOINTERFACE; - } - - /// - [UnmanagedCallersOnly] - private static uint AddRef(PixelShaderTransform* @this) - { - return (uint)Interlocked.Increment(ref @this->referenceCount); - } - - /// - [UnmanagedCallersOnly] - private static uint Release(PixelShaderTransform* @this) - { - uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount); - - if (referenceCount == 0) - { - NativeMemory.Free(@this); - } - - return referenceCount; - } - - /// - [UnmanagedCallersOnly] - private static uint GetInputCount(PixelShaderTransform* @this) - { - return (uint)@this->numberOfInputs; - } - - /// - [UnmanagedCallersOnly] - private static int MapOutputRectToInputRects(PixelShaderTransform* @this, RECT* outputRect, RECT* inputRects, uint inputRectsCount) - { - if (inputRectsCount != 1) - { - return E.E_INVALIDARG; - } - - inputRects[0] = *outputRect; - - return S.S_OK; - } - - /// - [UnmanagedCallersOnly] - private static int MapInputRectsToOutputRect(PixelShaderTransform* @this, RECT* inputRects, RECT* inputOpaqueSubRects, uint inputRectCount, RECT* outputRect, RECT* outputOpaqueSubRect) - { - if (inputRectCount != 1) - { - return E.E_INVALIDARG; - } - - *outputRect = inputRects[0]; - - @this->inputRect = inputRects[0]; - - *outputOpaqueSubRect = default; - - return S.S_OK; - } - - /// - [UnmanagedCallersOnly] - private static int MapInvalidRect(PixelShaderTransform* @this, uint inputIndex, RECT invalidInputRect, RECT* invalidOutputRect) - { - *invalidOutputRect = @this->inputRect; - - return S.S_OK; - } - - /// - [UnmanagedCallersOnly] - private static int SetDrawInfo(PixelShaderTransform* @this, ID2D1DrawInfo* drawInfo) - { - @this->d2D1DrawInfo = drawInfo; - - 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 c663bfb7..7bc9d786 100644 --- a/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs +++ b/tests/ComputeSharp.D2D1Interop.Tests/Helpers/PixelShaderEffect.cs @@ -8,10 +8,18 @@ using TerraFX.Interop.Windows; namespace ComputeSharp.D2D1Interop.Tests.Helpers; /// -/// A simple implementation for a given pixel shader. +/// A simple and implementation for a given pixel shader. /// internal unsafe partial struct PixelShaderEffect { + /// + /// A wrapper for an effect factory. + /// + /// The resulting effect factory. + /// The HRESULT for the operation. + [UnmanagedFunctionPointer(CallingConvention.Winapi)] + private delegate int FactoryDelegate(IUnknown** effectImpl); + /// /// A generic pixel shader implementation. /// @@ -83,43 +91,59 @@ internal unsafe partial struct PixelShaderEffect } /// - /// The shared vtable pointer for instances. + /// The shared vtable pointer for instance, for . /// - private static readonly void** Vtbl = InitVtbl(); + private static readonly void** VtblForID2D1EffectImpl; /// - /// Setups the vtable pointer for . + /// The shared vtable pointer for instance, for . /// - /// The initialized vtable pointer for . - private static void** InitVtbl() + private static readonly void** VtblForID2D1DrawTransform; + + /// + /// Initializes the shared state for . + /// + static PixelShaderEffect() { - void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(PixelShaderEffect), sizeof(void*) * 6); + void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(PixelShaderEffect), sizeof(void*) * 14); - lpVtbl[0] = (delegate* unmanaged)&QueryInterface; - lpVtbl[1] = (delegate* unmanaged)&AddRef; - lpVtbl[2] = (delegate* unmanaged)&Release; - lpVtbl[3] = (delegate* unmanaged)&Initialize; - lpVtbl[4] = (delegate* unmanaged)&PrepareForRender; - lpVtbl[5] = (delegate* unmanaged)&SetGraph; + // ID2D1EffectImpl + lpVtbl[0] = (delegate* unmanaged)&ID2D1EffectImplMethods.QueryInterface; + lpVtbl[1] = (delegate* unmanaged)&ID2D1EffectImplMethods.AddRef; + lpVtbl[2] = (delegate* unmanaged)&ID2D1EffectImplMethods.Release; + lpVtbl[3] = (delegate* unmanaged)&ID2D1EffectImplMethods.Initialize; + lpVtbl[4] = (delegate* unmanaged)&ID2D1EffectImplMethods.PrepareForRender; + lpVtbl[5] = (delegate* unmanaged)&ID2D1EffectImplMethods.SetGraph; - return lpVtbl; + // ID2D1DrawTransform + lpVtbl[6 + 0] = (delegate* unmanaged)&ID2D1DrawTransformMethods.QueryInterface; + lpVtbl[6 + 1] = (delegate* unmanaged)&ID2D1DrawTransformMethods.AddRef; + lpVtbl[6 + 2] = (delegate* unmanaged)&ID2D1DrawTransformMethods.Release; + lpVtbl[6 + 3] = (delegate* unmanaged)&ID2D1DrawTransformMethods.GetInputCount; + lpVtbl[6 + 4] = (delegate* unmanaged)&ID2D1DrawTransformMethods.MapOutputRectToInputRects; + lpVtbl[6 + 5] = (delegate* unmanaged)&ID2D1DrawTransformMethods.MapInputRectsToOutputRect; + lpVtbl[6 + 6] = (delegate* unmanaged)&ID2D1DrawTransformMethods.MapInvalidRect; + lpVtbl[6 + 7] = (delegate* unmanaged)&ID2D1DrawTransformMethods.SetDrawInfo; + + VtblForID2D1EffectImpl = lpVtbl; + VtblForID2D1DrawTransform = &lpVtbl[6]; } /// - /// The vtable pointer for the current instance. + /// The vtable pointer for the current instance, for . /// - private void** lpVtbl; + private void** lpVtblForID2D1EffectImpl; + + /// + /// The vtable pointer for the current instance, for . + /// + private void** lpVtblForID2D1DrawTransform; /// /// The current reference count for the object (from IUnknown). /// private volatile int referenceCount; - /// - /// The linked instance currently in use. - /// - private PixelShaderTransform* pixelShaderTransform; - /// /// The constant buffer data, if set. /// @@ -151,29 +175,14 @@ internal unsafe partial struct PixelShaderEffect private int numberOfInputs; /// - /// Creates and initializes a new instance. + /// The current input rectangle. /// - /// A new instance. - private static PixelShaderEffect* Create() - { - PixelShaderEffect* @this = (PixelShaderEffect*)NativeMemory.Alloc((nuint)sizeof(PixelShaderEffect)); - - @this->lpVtbl = Vtbl; - @this->referenceCount = 1; - @this->pixelShaderTransform = null; - @this->constantBuffer = null; - @this->constantBufferSize = 0; - - return @this; - } + private RECT inputRect; /// - /// A wrapper for an effect factory. + /// The instance currently in use. /// - /// The resulting effect factory. - /// The HRESULT for the operation. - [UnmanagedFunctionPointer(CallingConvention.Winapi)] - private delegate int FactoryDelegate(IUnknown** effectImpl); + private ID2D1DrawInfo* d2D1DrawInfo; /// /// The factory method for . @@ -182,20 +191,23 @@ internal unsafe partial struct PixelShaderEffect /// The shader bytecode. /// The resulting effect instance. /// The size of . - /// The number of inputs for the shader. + /// The number of inputs for the shader. /// This always returns 0. - private static int Factory(Guid shaderId, byte* bytecode, int bytecodeSize, int numberOfInpputs, IUnknown** effectImpl) + private static int Factory(Guid shaderId, byte* bytecode, int bytecodeSize, int numberOfInputs, IUnknown** effectImpl) { - PixelShaderEffect* pixelShaderEffect = Create(); - PixelShaderTransform* pixelShaderTransform = PixelShaderTransform.Create(shaderId, numberOfInpputs); + PixelShaderEffect* @this = (PixelShaderEffect*)NativeMemory.Alloc((nuint)sizeof(PixelShaderEffect)); - pixelShaderEffect->pixelShaderTransform = pixelShaderTransform; - pixelShaderEffect->shaderId = shaderId; - pixelShaderEffect->bytecode = bytecode; - pixelShaderEffect->bytecodeSize = bytecodeSize; - pixelShaderEffect->numberOfInputs = numberOfInpputs; + *@this = default; - *effectImpl = (IUnknown*)pixelShaderEffect; + @this->lpVtblForID2D1EffectImpl = VtblForID2D1EffectImpl; + @this->lpVtblForID2D1DrawTransform = VtblForID2D1DrawTransform; + @this->referenceCount = 1; + @this->shaderId = shaderId; + @this->bytecode = bytecode; + @this->bytecodeSize = bytecodeSize; + @this->numberOfInputs = numberOfInputs; + + *effectImpl = (IUnknown*)@this; return S.S_OK; } @@ -244,20 +256,32 @@ internal unsafe partial struct PixelShaderEffect } /// - [UnmanagedCallersOnly] - private static int QueryInterface(PixelShaderEffect* @this, Guid* riid, void** ppvObject) + private int QueryInterface(Guid* riid, void** ppvObject) { if (ppvObject is null) { return E.E_POINTER; } + // ID2D1EffectImpl if (riid->Equals(Windows.__uuidof()) || riid->Equals(Windows.__uuidof())) { - _ = Interlocked.Increment(ref @this->referenceCount); + _ = Interlocked.Increment(ref this.referenceCount); - *ppvObject = @this; + *ppvObject = Unsafe.AsPointer(ref this); + + return S.S_OK; + } + + // ID2D1DrawTransform + if (riid->Equals(Windows.__uuidof()) || + riid->Equals(Windows.__uuidof()) || + riid->Equals(Windows.__uuidof())) + { + _ = Interlocked.Increment(ref this.referenceCount); + + *ppvObject = (void**)Unsafe.AsPointer(ref this) + 1; return S.S_OK; } @@ -268,68 +292,26 @@ internal unsafe partial struct PixelShaderEffect } /// - [UnmanagedCallersOnly] - private static uint AddRef(PixelShaderEffect* @this) + private uint AddRef() { - return (uint)Interlocked.Increment(ref @this->referenceCount); + return (uint)Interlocked.Increment(ref this.referenceCount); } /// - [UnmanagedCallersOnly] - private static uint Release(PixelShaderEffect* @this) + private uint Release() { - uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount); + uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount); if (referenceCount == 0) { - ((IUnknown*)@this->pixelShaderTransform)->Release(); - - if (@this->constantBuffer is not null) + if (this.constantBuffer is not null) { - NativeMemory.Free(@this->constantBuffer); + NativeMemory.Free(this.constantBuffer); } - NativeMemory.Free(@this); + NativeMemory.Free(Unsafe.AsPointer(ref this)); } return referenceCount; } - - /// - [UnmanagedCallersOnly] - private static int Initialize(PixelShaderEffect* @this, ID2D1EffectContext* effectContext, ID2D1TransformGraph* transformGraph) - { - int hresult = effectContext->LoadPixelShader( - shaderId: &@this->shaderId, - shaderBuffer: @this->bytecode, - shaderBufferCount: (uint)@this->bytecodeSize); - - if (Windows.SUCCEEDED(hresult)) - { - hresult = transformGraph->SetSingleTransformNode((ID2D1TransformNode*)@this->pixelShaderTransform); - } - - return hresult; - } - - /// - [UnmanagedCallersOnly] - private static int PrepareForRender(PixelShaderEffect* @this, D2D1_CHANGE_TYPE changeType) - { - if (@this->constantBuffer is not null) - { - return @this->pixelShaderTransform->D2D1DrawInfo->SetPixelShaderConstantBuffer( - buffer: @this->constantBuffer, - bufferCount: (uint)@this->constantBufferSize); - } - - return S.S_OK; - } - - /// - [UnmanagedCallersOnly] - private static int SetGraph(PixelShaderEffect* @this, ID2D1TransformGraph* transformGraph) - { - return E.E_NOTIMPL; - } } \ No newline at end of file