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 { #if !NET6_0_OR_GREATER /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int QueryInterfaceDelegate(D2D1DrawInfoUpdateContextImpl* @this, Guid* riid, void** ppvObject); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate uint AddRefDelegate(D2D1DrawInfoUpdateContextImpl* @this); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate uint ReleaseDelegate(D2D1DrawInfoUpdateContextImpl* @this); #endif /// /// 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 #if NET6_0_OR_GREATER lpVtbl[0] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextMethods.QueryInterface; lpVtbl[1] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextMethods.AddRef; lpVtbl[2] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextMethods.Release; lpVtbl[3] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextMethods.GetConstantBufferSize; lpVtbl[4] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextMethods.GetConstantBuffer; lpVtbl[5] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextMethods.SetConstantBuffer; #else lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextMethods.QueryInterfaceWrapper); lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextMethods.AddRefWrapper); lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextMethods.ReleaseWrapper); lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextMethods.GetConstantBufferSizeWrapper); lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextMethods.GetConstantBufferWrapper); lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextMethods.SetConstantBufferWrapper); #endif // ID2D1ResourceTextureManagerInternal #if NET6_0_OR_GREATER lpVtbl[6 + 0] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextInternalMethods.QueryInterface; lpVtbl[6 + 1] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextInternalMethods.AddRef; lpVtbl[6 + 2] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextInternalMethods.Release; lpVtbl[6 + 3] = (delegate* unmanaged)&ID2D1DrawInfoUpdateContextInternalMethods.Close; #else lpVtbl[6 + 0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextInternalMethods.QueryInterfaceWrapper); lpVtbl[6 + 1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextInternalMethods.AddRefWrapper); lpVtbl[6 + 2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextInternalMethods.ReleaseWrapper); lpVtbl[6 + 3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawInfoUpdateContextInternalMethods.CloseWrapper); #endif 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 ID2D1DrawInfo* 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->d2D1DrawInfo = 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(ID2D1DrawInfoUpdateContext.Guid)) { _ = Interlocked.Increment(ref this.referenceCount); *ppvObject = Unsafe.AsPointer(ref this); return S.S_OK; } // ID2D1DrawInfoUpdateContextInternal if (riid->Equals(ID2D1DrawInfoUpdateContextInternal.Guid)) { _ = 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; } }