using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using ComputeSharp.D2D1.Interop; using ComputeSharp.Win32; namespace ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; /// /// An implementation of the and interfaces. /// internal unsafe partial struct D2D1DrawInfoUpdateContextImpl { /// /// The shared vtable pointer for instance, for . /// private static readonly void** VtblForID2D1DrawInfoUpdateContext = InitVtblForID2D1DrawInfoUpdateContextAndID2D1DrawInfoUpdateContextInternal(); /// /// The shared vtable pointer for instance, for . /// private static readonly void** VtblForID2D1DrawInfoUpdateContextInternal = &VtblForID2D1DrawInfoUpdateContext[5]; /// /// Initializes the combined vtable for and . /// /// The combined vtable for and . private static void** InitVtblForID2D1DrawInfoUpdateContextAndID2D1DrawInfoUpdateContextInternal() { void** lpVtbl = (void**)D2D1AssemblyAssociatedMemory.Allocate(sizeof(void*) * 10); // ID2D1ResourceTextureManager lpVtbl[0] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextMethods.QueryInterface; lpVtbl[1] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextMethods.AddRef; lpVtbl[2] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextMethods.Release; lpVtbl[3] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextMethods.GetConstantBufferSize; lpVtbl[4] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextMethods.GetConstantBuffer; lpVtbl[5] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextMethods.SetConstantBuffer; // ID2D1ResourceTextureManagerInternal lpVtbl[6 + 0] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextInternalMethods.QueryInterface; lpVtbl[6 + 1] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextInternalMethods.AddRef; lpVtbl[6 + 2] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextInternalMethods.Release; lpVtbl[6 + 3] = (delegate* unmanaged[MemberFunction])&ID2D1DrawInfoUpdateContextInternalMethods.Close; return lpVtbl; } /// /// The vtable pointer for the current instance, for . /// private void** lpVtblForID2D1DrawInfoUpdateContext; /// /// The vtable pointer for the current instance, for . /// private void** lpVtblForID2D1DrawInfoUpdateContextInternal; /// /// The current reference count for the object (from ). /// private volatile int referenceCount; /// /// The constant buffer data, if set. /// private byte* constantBuffer; /// /// The size of . /// private int constantBufferSize; /// /// The instance currently in use. /// private ComPtr d2D1DrawInfo; /// /// The factory method for instances. /// /// The resulting draw info update context instance. /// The constant buffer data, if set. /// The size of . /// The instance currently in use. /// This always returns 0. public static HRESULT Factory( D2D1DrawInfoUpdateContextImpl** drawInfoUpdateContext, byte* constantBuffer, int constantBufferSize, ID2D1DrawInfo* d2D1DrawInfo) { D2D1DrawInfoUpdateContextImpl* @this; try { @this = (D2D1DrawInfoUpdateContextImpl*)NativeMemory.Alloc((nuint)sizeof(D2D1DrawInfoUpdateContextImpl)); } catch (OutOfMemoryException) { *drawInfoUpdateContext = null; return E.E_OUTOFMEMORY; } @this->lpVtblForID2D1DrawInfoUpdateContext = VtblForID2D1DrawInfoUpdateContext; @this->lpVtblForID2D1DrawInfoUpdateContextInternal = VtblForID2D1DrawInfoUpdateContextInternal; @this->referenceCount = 1; @this->constantBuffer = constantBuffer; @this->constantBufferSize = constantBufferSize; // This ID2D1DrawInfo manager instance is short lived, and the contract guarantees that it will not remain // alive after the underlying ID2D1DrawInfo instance has been invalidated. As such, we can optimize the // initialization by skipping the AddRef/Release calls on the ID2D1DrawInfo object, and simply storing the // pointer to the object. When this instance is closed, that will just be set to null again. @this->d2D1DrawInfo = default; @this->d2D1DrawInfo.Attach(d2D1DrawInfo); *drawInfoUpdateContext = @this; return S.S_OK; } /// public int QueryInterface(Guid* riid, void** ppvObject) { if (ppvObject is null) { return E.E_POINTER; } // ID2D1DrawInfoUpdateContext if (riid->Equals(Windows.__uuidof()) || riid->Equals(Windows.__uuidof())) { _ = Interlocked.Increment(ref this.referenceCount); *ppvObject = Unsafe.AsPointer(ref this); return S.S_OK; } // ID2D1DrawInfoUpdateContextInternal if (riid->Equals(Windows.__uuidof())) { _ = Interlocked.Increment(ref this.referenceCount); *ppvObject = (void**)Unsafe.AsPointer(ref this) + 1; return S.S_OK; } *ppvObject = null; return E.E_NOINTERFACE; } /// public uint AddRef() { return (uint)Interlocked.Increment(ref this.referenceCount); } /// public uint Release() { uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount); if (referenceCount == 0) { NativeMemory.Free(Unsafe.AsPointer(ref this)); } return referenceCount; } }