using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #if !NET6_0_OR_GREATER using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers; #endif namespace ComputeSharp.D2D1.Interop.Effects; /// /// A simple and implementation for a given pixel shader. /// internal unsafe partial struct PixelShaderEffect { #if !NET6_0_OR_GREATER /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate int QueryInterfaceDelegate(PixelShaderEffect* @this, Guid* riid, void** ppvObject); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate uint AddRefDelegate(PixelShaderEffect* @this); /// [UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate uint ReleaseDelegate(PixelShaderEffect* @this); #endif /// /// The shared vtable pointer for instance, for . /// private static readonly void** VtblForID2D1EffectImpl = InitVtblForID2D1EffectImplAndID2D1DrawTransform(); /// /// The shared vtable pointer for instance, for . /// private static readonly void** VtblForID2D1DrawTransform = &VtblForID2D1EffectImpl[6]; /// /// Initializes the combined vtable for and . /// /// The combined vtable for and . private static void** InitVtblForID2D1EffectImplAndID2D1DrawTransform() { void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(PixelShaderEffect), sizeof(void*) * 14); // ID2D1EffectImpl #if NET6_0_OR_GREATER 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; #else lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.QueryInterfaceWrapper); lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.AddRefWrapper); lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.ReleaseWrapper); lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.InitializeWrapper); lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.PrepareForRenderWrapper); lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.SetGraphWrapper); #endif // ID2D1DrawTransform #if NET6_0_OR_GREATER 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; #else lpVtbl[6 + 0] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.QueryInterfaceWrapper); lpVtbl[6 + 1] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.AddRefWrapper); lpVtbl[6 + 2] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.ReleaseWrapper); lpVtbl[6 + 3] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.GetInputCountWrapper); lpVtbl[6 + 4] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.MapOutputRectToInputRectsWrapper); lpVtbl[6 + 5] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.MapInputRectsToOutputRectWrapper); lpVtbl[6 + 6] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.MapInvalidRectWrapper); lpVtbl[6 + 7] = (void*)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.SetDrawInfoWrapper); #endif return lpVtbl; } /// /// The vtable pointer for the current instance, for . /// 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 for the instance in use. /// private GCHandle globalsHandle; /// /// The constant buffer data, if set. /// private byte* constantBuffer; /// /// The instance to use, if any. /// private ID2D1TransformMapper* d2D1TransformMapper; /// /// The instance currently in use. /// private ID2D1DrawInfo* d2D1DrawInfo; /// /// The instance currently in use. /// private ID2D1EffectContext* d2D1EffectContext; /// /// The resource texture managers for the current instance. /// private ResourceTextureManagerBuffer resourceTextureManagerBuffer; /// /// The factory method for . /// /// The instance to use. /// The resulting effect instance. /// The for the operation. private static int Factory(Globals globals, IUnknown** effectImpl) { PixelShaderEffect* @this = null; GCHandle globalsHandle = default; try { @this = (PixelShaderEffect*)NativeMemory.Alloc((nuint)sizeof(PixelShaderEffect)); globalsHandle = GCHandle.Alloc(globals); @this->lpVtblForID2D1EffectImpl = VtblForID2D1EffectImpl; @this->lpVtblForID2D1DrawTransform = VtblForID2D1DrawTransform; @this->referenceCount = 1; @this->globalsHandle = globalsHandle; @this->constantBuffer = null; @this->d2D1TransformMapper = null; @this->d2D1DrawInfo = null; @this->d2D1EffectContext = null; @this->resourceTextureManagerBuffer = default; *effectImpl = (IUnknown*)@this; return S.S_OK; } catch (Exception e) { // Free the effect, if we have one NativeMemory.Free(@this); // Free the handle, if we have one if (globalsHandle.IsAllocated) { globalsHandle.Free(); } *effectImpl = null; return Marshal.GetHRForException(e); } } /// 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); *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; } *ppvObject = null; return E.E_NOINTERFACE; } /// private uint AddRef() { return (uint)Interlocked.Increment(ref this.referenceCount); } /// private uint Release() { uint referenceCount = (uint)Interlocked.Decrement(ref this.referenceCount); if (referenceCount == 0) { this.globalsHandle.Free(); NativeMemory.Free(this.constantBuffer); ComPtr.Release(this.d2D1TransformMapper); ComPtr.Release(this.d2D1DrawInfo); ComPtr.Release(this.d2D1EffectContext); // Retrieve all possible resource texture managers in use and release the ones that had been // assigned (from one of the property bindings). We just hardcode 16 here and dont access // the globals, as technically invoking APIs on it might throw an exception. for (int i = 0; i < 16; i++) { ComPtr.Release(this.resourceTextureManagerBuffer[i]); } NativeMemory.Free(Unsafe.AsPointer(ref this)); } return referenceCount; } /// /// Gets the instance for the current effect. /// /// The instance for the current effect. [MethodImpl(MethodImplOptions.AggressiveInlining)] private Globals GetGlobals() { return Unsafe.As(this.globalsHandle.Target!); } }