using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#if NET6_0_OR_GREATER
using RuntimeHelpers = System.Runtime.CompilerServices.RuntimeHelpers;
#else
using RuntimeHelpers = ComputeSharp.D2D1.NetStandard.System.Runtime.CompilerServices.RuntimeHelpers;
using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute;
#endif
namespace ComputeSharp.D2D1.Interop.Effects;
///
/// A simple and implementation for a given pixel shader.
///
internal unsafe partial struct PixelShaderEffect
{
///
/// A wrapper for an effect factory.
///
/// The resulting effect factory.
/// The HRESULT for the operation.
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int FactoryDelegate(IUnknown** effectImpl);
#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);
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int GetConstantBufferDelegate(IUnknown* effect, byte* data, uint dataSize, uint* actualSize);
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int SetConstantBufferDelegate(IUnknown* effect, byte* data, uint dataSize);
///
/// A cached instance wrapping .
///
public static readonly GetConstantBufferDelegate GetConstantBufferWrapper = GetConstantBuffer;
///
/// A cached instance wrapping .
///
public static readonly SetConstantBufferDelegate SetConstantBufferWrapper = SetConstantBuffer;
#endif
///
/// The shared vtable pointer for instance, for .
///
private static readonly void** VtblForID2D1EffectImpl;
///
/// The shared vtable pointer for instance, for .
///
private static readonly void** VtblForID2D1DrawTransform;
///
/// Initializes the shared state for .
///
static PixelShaderEffect()
{
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
VtblForID2D1EffectImpl = lpVtbl;
VtblForID2D1DrawTransform = &lpVtbl[6];
}
///
/// 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 constant buffer data, if set.
///
private byte* constantBuffer;
///
/// The size of , if set.
///
private int constantBufferSize;
///
/// The for the shader.
///
private Guid shaderId;
///
/// The number of inputs for the shader.
///
private int inputCount;
///
/// The buffer with the types of inputs for the shader.
///
///
/// This buffer is shared among effect instances, so it should not be released. It is
/// owned by , which will release it when the target type is unloaded.
///
private D2D1PixelShaderInputType* inputTypes;
///
/// The number of available input descriptions.
///
private int inputDescriptionCount;
///
/// The buffer with the available input descriptions for the shader.
///
///
/// This buffer is also shared among effect instances, so it should not be released. It is
/// owned by , which will release it when the target type is unloaded.
///
private D2D1InputDescription* inputDescriptions;
///
/// The shader bytecode.
///
private byte* bytecode;
///
/// The size of .
///
private int bytecodeSize;
///
/// The buffer precision for the resulting output buffer.
///
private D2D1BufferPrecision bufferPrecision;
///
/// The channel depth for the resulting output buffer.
///
private D2D1ChannelDepth channelDepth;
///
/// The handle for the instance in use, if any.
///
private GCHandle d2D1TransformMapperHandle;
///
/// The instance currently in use.
///
private ID2D1DrawInfo* d2D1DrawInfo;
///
/// The factory method for .
///
/// The for the shader.
/// The number of inputs for the shader.
/// The buffer with the types of inputs for the shader.
/// The number of available input descriptions.
/// The buffer with the available input descriptions for the shader.
/// The shader bytecode.
/// The size of .
/// The buffer precision for the resulting output buffer.
/// The channel depth for the resulting output buffer.
/// The instance to use for the effect.
/// The resulting effect instance.
/// This always returns 0.
private static int Factory(
Guid shaderId,
int inputCount,
D2D1PixelShaderInputType* inputTypes,
int inputDescriptionCount,
D2D1InputDescription* inputDescriptions,
byte* bytecode,
int bytecodeSize,
D2D1BufferPrecision bufferPrecision,
D2D1ChannelDepth channelDepth,
D2D1TransformMapper? d2D1TransformMapper,
IUnknown** effectImpl)
{
PixelShaderEffect* @this = (PixelShaderEffect*)NativeMemory.Alloc((nuint)sizeof(PixelShaderEffect));
*@this = default;
@this->lpVtblForID2D1EffectImpl = VtblForID2D1EffectImpl;
@this->lpVtblForID2D1DrawTransform = VtblForID2D1DrawTransform;
@this->referenceCount = 1;
@this->shaderId = shaderId;
@this->inputCount = inputCount;
@this->inputTypes = inputTypes;
@this->inputDescriptionCount = inputDescriptionCount;
@this->inputDescriptions = inputDescriptions;
@this->bytecode = bytecode;
@this->bytecodeSize = bytecodeSize;
@this->bufferPrecision = bufferPrecision;
@this->channelDepth = channelDepth;
@this->d2D1TransformMapperHandle = GCHandle.Alloc(d2D1TransformMapper);
*effectImpl = (IUnknown*)@this;
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;
}
///
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)
{
if (this.constantBuffer is not null)
{
NativeMemory.Free(this.constantBuffer);
}
this.d2D1TransformMapperHandle.Free();
NativeMemory.Free(Unsafe.AsPointer(ref this));
}
return referenceCount;
}
}