using System; using System.Globalization; using System.Runtime.CompilerServices; #if !NET6_0_OR_GREATER using System.Runtime.InteropServices; #endif using System.Text; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Helpers; 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; /// /// An implementation of a D2D1 pixel shader effect that can be used to instantiate ID2D1Effect objects. /// public static unsafe class D2D1PixelShaderEffect { /// /// 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. /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader { RegisterForD2D1Factory1(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. /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static unsafe void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { RegisterForD2D1Factory1(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. /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static unsafe void RegisterForD2D1Factory1(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 fixed (Guid* pGuid = &PixelShaderEffect.For.Id) { // Register the effect ((ID2D1Factory1*)d2D1Factory1)->RegisterEffectFromString( classId: pGuid, 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. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader { return GetRegistrationBlob(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. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . public static unsafe ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { return GetRegistrationBlob(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. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// /// /// 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 blob version id (a ). /// 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. /// The leading blob id will determine what subtype should be used to deserialize the blob (eg. ). /// /// /// For more info, see . /// /// public static unsafe ReadOnlyMemory GetRegistrationBlob(Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; PixelShaderEffect.For.Initialize(mapperFactory); using ArrayPoolBinaryWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); // Blob id writer.Write(D2D1EffectRegistrationData.V1.BlobId); // 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. /// /// Thrown if this method is called before initializing a shader effect for the shader type . /// To do so, make sure to call either (or an overload), or /// to use (or an overload), and use that blob to register an effect. /// /// For more info, see . public static unsafe void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) where T : unmanaged, ID2D1PixelShader { if (!PixelShaderEffect.For.TryGetId(out Guid id)) { ThrowHelper.ThrowInvalidOperationException("The effect for the input shader type has not been initialized yet."); } ((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect( effectId: &id, 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); } }