using System;
using System.Runtime.CompilerServices;
#if !NET6_0_OR_GREATER
using System.Runtime.InteropServices;
#endif
using System.Text;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.Interop.Effects;
using ComputeSharp.D2D1.Shaders.Dispatching;
using ComputeSharp.D2D1.Shaders.Interop.Buffers;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.Interop;
///
unsafe partial class D2D1InteropServices
{
///
/// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString.
///
/// The type of D2D1 pixel shader to register.
/// A pointer to the ID2D1Factory1 instance to use.
/// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect.
/// For more info, see .
public static unsafe void RegisterPixelShaderEffectForD2D1Factory1(void* d2D1Factory1, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
{
RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory1, null, out effectId);
}
///
/// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString.
///
/// The type of D2D1 pixel shader to register.
/// The type of implementation to register.
/// A pointer to the ID2D1Factory1 instance to use.
/// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect.
/// For more info, see .
public static unsafe void RegisterPixelShaderEffectForD2D1Factory1(void* d2D1Factory1, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
where TMapper : class, ID2D1TransformMapper, new()
{
RegisterPixelShaderEffectForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId);
}
///
/// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString.
///
/// The type of D2D1 pixel shader to register.
/// A pointer to the ID2D1Factory1 instance to use.
/// An optional factory of instances to use for the transform.
/// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect.
/// For more info, see .
public static unsafe void RegisterPixelShaderEffectForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
{
effectId = default;
PixelShaderEffect.For.Initialize(mapperFactory);
// Setup the input string
StringBuilder effectInputsBuilder = new();
for (int i = 0; i < PixelShaderEffect.For.NumberOfInputs; i++)
{
effectInputsBuilder.Append("");
}
// Prepare the XML with a variable number of inputs
string xml = @$"
{effectInputsBuilder}
";
fixed (char* pXml = xml)
fixed (char* pPropertyName = "Buffer")
{
// Prepare the effect binding functions
D2D1_PROPERTY_BINDING d2D1PropertyBinding;
d2D1PropertyBinding.propertyName = (ushort*)pPropertyName;
d2D1PropertyBinding.getFunction =
(delegate* unmanaged[Stdcall])
#if NET6_0_OR_GREATER
(delegate* unmanaged)
&PixelShaderEffect.GetConstantBuffer;
#else
(void*)Marshal.GetFunctionPointerForDelegate(PixelShaderEffect.GetConstantBufferWrapper);
#endif
d2D1PropertyBinding.setFunction =
(delegate* unmanaged[Stdcall])
#if NET6_0_OR_GREATER
(delegate* unmanaged)
&PixelShaderEffect.SetConstantBuffer;
#else
(void*)Marshal.GetFunctionPointerForDelegate(PixelShaderEffect.SetConstantBufferWrapper);
#endif
// Register the effect
((ID2D1Factory1*)d2D1Factory1)->RegisterEffectFromString(
classId: PixelShaderEffect.For.Id,
propertyXml: (ushort*)pXml,
bindings: &d2D1PropertyBinding,
bindingsCount: 1,
effectFactory: PixelShaderEffect.For.Factory).Assert();
effectId = *PixelShaderEffect.For.Id;
}
}
///
/// Gets a binary blob containing serialized information that can be used to register an effect, by using ID2D1Factory1::RegisterEffectFromString.
///
/// The type of D2D1 pixel shader to register.
/// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect.
/// For more info and for details on the binary format, see .
public static unsafe ReadOnlyMemory GetPixelShaderEffectRegistrationBlob(out Guid effectId)
where T : unmanaged, ID2D1PixelShader
{
return GetPixelShaderEffectRegistrationBlob(null, out effectId);
}
///
/// Gets a binary blob containing serialized information that can be used to register an effect, by using ID2D1Factory1::RegisterEffectFromString.
///
/// The type of D2D1 pixel shader to register.
/// The type of implementation to register.
/// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect.
/// For more info and for details on the binary format, see .
public static unsafe ReadOnlyMemory GetPixelShaderEffectRegistrationBlob(out Guid effectId)
where T : unmanaged, ID2D1PixelShader
where TMapper : class, ID2D1TransformMapper, new()
{
return GetPixelShaderEffectRegistrationBlob(static () => new TMapper(), out effectId);
}
///
/// Gets a binary blob containing serialized information that can be used to register an effect, by using ID2D1Factory1::RegisterEffectFromString.
///
/// The type of D2D1 pixel shader to get the registration blob for.
/// An optional factory of instances to use for the transform.
/// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect.
///
///
/// This blob can be useful to marshal registration data across an app domain boundary without having assembly references cross this boundary as well.
///
///
/// For instance, an application could allow plugins to create effects through the ComputeSharp.D2D1 APIs, and to then create an effect registration
/// blob to pass to the main application, which could then use it to register the effect. This way, even if a plugin was loaded with a separate assembly load
/// context and using a different (potentially incompatible) version of ComputeSharp.D2D1, the two could still work side by side with no issues.
///
///
/// The binary blob contains information with the following format:
///
/// The effect id (a ).
/// The number of inputs for the effect (an ).
/// The effect XML description (as null-terminated UTF8 text).
/// The number of property bindings (an ).
/// The property name (as null-terminated UTF8 text).
/// The property getter (a ).
/// The property setter (a ).
/// The effect factory (a ).
///
/// The property name, getter and setter are grouped together after the number of bindings.
///
///
/// To make the deserialization easier, the type can be used to read and validate the returned blob.
///
///
/// For more info, see .
///
///
public static unsafe ReadOnlyMemory GetPixelShaderEffectRegistrationBlob(Func>? mapperFactory, out Guid effectId)
where T : unmanaged, ID2D1PixelShader
{
effectId = default;
PixelShaderEffect.For.Initialize(mapperFactory);
using ArrayPoolBinaryWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize);
// Effect id and number of inputs
writer.Write(*PixelShaderEffect.For.Id);
writer.Write(PixelShaderEffect.For.NumberOfInputs);
// Build the XML text
writer.WriteAsUtf8(@"
");
// Add the input nodes
for (int i = 0; i < PixelShaderEffect.For.NumberOfInputs; i++)
{
writer.WriteAsUtf8("");
}
// Write the last part of the XML (including the buffer property)
writer.WriteAsUtf8(@"
");
// Null terminator for the text
writer.Write((byte)'\0');
// Property accessors
#if NET6_0_OR_GREATER
nint getBufferPointer = (nint)(delegate* unmanaged)&PixelShaderEffect.GetConstantBuffer;
nint setBufferPointer = (nint)(delegate* unmanaged)&PixelShaderEffect.SetConstantBuffer;
#else
nint getBufferPointer = Marshal.GetFunctionPointerForDelegate(PixelShaderEffect.GetConstantBufferWrapper);
nint setBufferPointer = Marshal.GetFunctionPointerForDelegate(PixelShaderEffect.SetConstantBufferWrapper);
#endif
// Bindings
writer.Write(1);
writer.WriteAsUtf8("Buffer");
writer.Write((byte)'\0');
writer.Write(getBufferPointer);
writer.Write(setBufferPointer);
// Effect factory
writer.Write((nint)PixelShaderEffect.For.Factory);
effectId = *PixelShaderEffect.For.Id;
return writer.WrittenSpan.ToArray();
}
///
/// Creates an effect wrapping an input D2D1 pixel shader, by calling ID2D1DeviceContext::CreateEffect.
///
/// The type of D2D1 pixel shader to create an effect for.
/// A pointer to the ID2D1DeviceContext instance to use.
/// A pointer to the resulting ID2D1Effect* pointer to produce.
/// For more info, see .
public static unsafe void CreatePixelShaderEffectFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect)
where T : unmanaged, ID2D1PixelShader
{
Guid shaderId = typeof(T).GUID;
((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect(
effectId: &shaderId,
effect: (ID2D1Effect**)d2D1Effect).Assert();
}
///
/// Sets the constant buffer from an input D2D1 effect, by calling ID2D1Effect::SetValue.
///
/// The type of D2D1 pixel shader to set the constant buffer for.
/// The input D2D1 pixel shader to set the contant buffer for.
/// A pointer to the ID2D1Effect instance to use.
/// For more info, see .
public static unsafe void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect)
where T : unmanaged, ID2D1PixelShader
{
D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect);
Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader);
}
}