using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#if NET6_0_OR_GREATER
using MemoryMarshal2 = System.Runtime.InteropServices.MemoryMarshal;
#else
using MemoryMarshal2 = ComputeSharp.NetStandard.MemoryMarshal;
using RuntimeHelpers = ComputeSharp.NetStandard.RuntimeHelpers;
#endif
namespace ComputeSharp.D2D1.Shaders.Translation;
///
partial class D3DCompiler
{
///
/// A custom fallback implementation to use on systems with no support for it.
///
private unsafe struct ID3DIncludeForD2DHelpers
{
#if !NET6_0_OR_GREATER
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int OpenDelegate(ID3DIncludeForD2DHelpers* @this, D3D_INCLUDE_TYPE IncludeType, sbyte* pFileName, void* pParentData, void** ppData, uint* pBytes);
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int CloseDelegate(ID3DIncludeForD2DHelpers* @this, void* pData);
///
/// A cached instance wrapping .
///
private static readonly OpenDelegate OpenWrapper = Open;
///
/// A cached instance wrapping .
///
private static readonly CloseDelegate CloseWrapper = Close;
#endif
///
/// The shared method table pointer for all instances.
///
private static readonly void** Vtbl = InitVtbl();
///
/// Builds the custom method table pointer for .
///
/// The method table pointer for .
private static void** InitVtbl()
{
void** lpVtbl = (void**)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ID3DIncludeForD2DHelpers), sizeof(void*) * 2);
#if NET6_0_OR_GREATER
lpVtbl[0] = (delegate* unmanaged)&Open;
lpVtbl[1] = (delegate* unmanaged)&Close;
#else
lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(OpenWrapper);
lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(CloseWrapper);
#endif
return lpVtbl;
}
///
/// The method table pointer for the current instance.
///
private void** lpVtbl;
///
/// Creates and initializes a new instance.
///
/// A pointer to a new instance.
public static ID3DIncludeForD2DHelpers* Create()
{
ID3DIncludeForD2DHelpers* @this = (ID3DIncludeForD2DHelpers*)NativeMemory.Alloc((nuint)sizeof(ID3DIncludeForD2DHelpers));
@this->lpVtbl = Vtbl;
return @this;
}
///
[UnmanagedCallersOnly]
public static int Open(ID3DIncludeForD2DHelpers* @this, D3D_INCLUDE_TYPE IncludeType, sbyte* pFileName, void* pParentData, void** ppData, uint* pBytes)
{
ReadOnlySpan fileName = MemoryMarshal2.CreateReadOnlySpanFromNullTerminated((byte*)pFileName);
if (fileName.SequenceEqual("d2d1effecthelpers.hlsli"u8))
{
*ppData = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(D2D1EffectHelpers.TextUtf8));
*pBytes = (uint)D2D1EffectHelpers.TextUtf8.Length;
return S.S_OK;
}
return S.S_FALSE;
}
///
[UnmanagedCallersOnly]
public static int Close(ID3DIncludeForD2DHelpers* @this, void* pData)
{
return S.S_OK;
}
}
}