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; /// /// Gets the instance currently in use. /// public ID2D1DrawInfo* D2D1DrawInfo => this.d2D1DrawInfo; /// /// Creates and initializes a new instance. /// /// A new instance. public static PixelShaderTransform* Create() { PixelShaderTransform* @this = (PixelShaderTransform*)NativeMemory.Alloc((nuint)sizeof(PixelShaderTransform)); @this->lpVtbl = Vtbl; @this->referenceCount = 1; @this->inputRect = default; @this->d2D1DrawInfo = null; 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 1; } /// [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((Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(typeof(InvertShader).GUID))); } } }