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; /// /// A simple implementation for a given pixel shader. /// internal unsafe partial struct PixelShaderEffect { /// /// 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(PixelShaderEffect), sizeof(void*) * 6); 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; 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 linked instance currently in use. /// private PixelShaderTransform* pixelShaderTransform; /// /// The constant buffer data, if set. /// private byte* constantBuffer; /// /// The size of , if set. /// private int constantBufferSize; /// /// Creates and initializes a new instance. /// /// 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; } /// /// The factory method for . /// /// The resulting effect instance. /// This always returns 0. [UnmanagedCallersOnly] public static int Factory(IUnknown** effectImpl) { PixelShaderEffect* pixelShaderEffect = Create(); PixelShaderTransform* pixelShaderTransform = PixelShaderTransform.Create(); pixelShaderEffect->pixelShaderTransform = pixelShaderTransform; *effectImpl = (IUnknown*)pixelShaderEffect; return S.S_OK; } /// [UnmanagedCallersOnly] public static int GetConstantBuffer(IUnknown* effect, byte* data, uint dataSize, uint* actualSize) { PixelShaderEffect* @this = (PixelShaderEffect*)effect; if (@this->constantBufferSize == 0) { *actualSize = 0; } else { int bytesToCopy = Math.Min((int)dataSize, @this->constantBufferSize); Buffer.MemoryCopy(@this->constantBuffer, data, dataSize, bytesToCopy); *actualSize = (uint)bytesToCopy; } return S.S_OK; } /// [UnmanagedCallersOnly] public static int SetConstantBuffer(IUnknown* effect, byte* data, uint dataSize) { PixelShaderEffect* @this = (PixelShaderEffect*)effect; if (@this->constantBuffer is not null) { NativeMemory.Free(@this->constantBuffer); } void* buffer = NativeMemory.Alloc(dataSize); Buffer.MemoryCopy(data, buffer, dataSize, dataSize); @this->constantBuffer = (byte*)buffer; @this->constantBufferSize = (int)dataSize; return S.S_OK; } /// [UnmanagedCallersOnly] private static int QueryInterface(PixelShaderEffect* @this, Guid* riid, void** ppvObject) { if (ppvObject is null) { return E.E_POINTER; } if (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(PixelShaderEffect* @this) { return (uint)Interlocked.Increment(ref @this->referenceCount); } /// [UnmanagedCallersOnly] private static uint Release(PixelShaderEffect* @this) { uint referenceCount = (uint)Interlocked.Decrement(ref @this->referenceCount); if (referenceCount == 0) { ((IUnknown*)@this->pixelShaderTransform)->Release(); if (@this->constantBuffer is not null) { NativeMemory.Free(@this->constantBuffer); } NativeMemory.Free(@this); } return referenceCount; } /// [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); } 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; } }