diff --git a/src/ComputeSharp.D2D1.NetStandard/System/Text/EncodingExtensions.cs b/src/ComputeSharp.D2D1.NetStandard/System/Text/EncodingExtensions.cs index 2beda15a..4f9f5f47 100644 --- a/src/ComputeSharp.D2D1.NetStandard/System/Text/EncodingExtensions.cs +++ b/src/ComputeSharp.D2D1.NetStandard/System/Text/EncodingExtensions.cs @@ -24,4 +24,18 @@ internal static class EncodingExtensions return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); } } + + /// + /// Decodes a text from a sequence of bytes. + /// + /// The input instance to use. + /// The byte span that holds the encoded bytes. + /// The resulting text. + public static unsafe string GetString(this Encoding encoding, ReadOnlySpan bytes) + { + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) + { + return encoding.GetString(bytesPtr, bytes.Length); + } + } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs new file mode 100644 index 00000000..ceebb020 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs @@ -0,0 +1,215 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; +#if !NET6_0_OR_GREATER +using ComputeSharp.D2D1.NetStandard.System.Text; +#endif + +namespace ComputeSharp.D2D1.Interop; + +/// +/// A helper type used to read and validate effect registration blobs produced by +/// . +/// +public unsafe readonly struct D2D1EffectRegistrationData +{ + /// + /// Creates a new instance with the specified parameters. + /// + /// The effect id. + /// The number of inputs for the effect. + /// The XML text that can be used to register the effect. + /// The values for the effect. + /// A pointer to the effect factory callback. + private D2D1EffectRegistrationData( + Guid effectId, + int numberOfInputs, + string xml, + PropertyBinding[] propertyBindings, + nint effectFactory) + { + EffectId = effectId; + NumberOfInputs = numberOfInputs; + Xml = xml; + PropertyBindings = propertyBindings; + EffectFactory = (void*)effectFactory; + } + + /// + /// Gets the id to use to register the effect. + /// + public Guid EffectId { get; } + + /// + /// Gets the number of inputs for the effect. + /// + public int NumberOfInputs { get; } + + /// + /// Gets the XML text with the effect description, that can be used to register it. + /// + public string Xml { get; } + + /// + /// Gets the sequence of values for the effect. + /// + public ReadOnlyMemory PropertyBindings { get; } + + /// + /// Gets a callback to an effect factory (a ). + /// + public void* EffectFactory { get; } + + /// + /// Tries to load a instance from the input binary blob. + /// + /// The input binary blob to deserialize. + /// The resulting instance, if successful. + /// Whether or not the input blob has been correctly deserialized (if it was not malformed). + /// + /// The input blob should have been created by a call to any of the overloads of + /// . + /// + public static unsafe bool TryLoad(ReadOnlyMemory blob, out D2D1EffectRegistrationData data) + { + data = default; + + if (blob.IsEmpty) + { + return false; + } + + ReadOnlySpan span = blob.Span; + + // Effect id + if (!MemoryMarshal.TryRead(span, out Guid effectId)) + { + return false; + } + + span = span.Slice(sizeof(Guid)); + + // Number of inputs + if (!MemoryMarshal.TryRead(span, out int numberOfInputs) || + numberOfInputs < 0) + { + return false; + } + + span = span.Slice(sizeof(int)); + + // Effect XML + int lengthOfXml = span.IndexOf((byte)'\0'); + + if (lengthOfXml == -1) + { + return false; + } + + string xml = Encoding.UTF8.GetString(span.Slice(0, lengthOfXml)); + + span = span.Slice(lengthOfXml + 1); + + // Number of bindings + if (!MemoryMarshal.TryRead(span, out int numberOfBindings) || + numberOfBindings < 0) + { + return false; + } + + span = span.Slice(sizeof(int)); + + PropertyBinding[] propertyBindings = new PropertyBinding[numberOfBindings]; + + // Property bindings + for (int i = 0; i < numberOfBindings; i++) + { + // Property name + int lengthOfName = span.IndexOf((byte)'\0'); + + if (lengthOfName == -1) + { + return false; + } + + string name = Encoding.UTF8.GetString(span.Slice(0, lengthOfName)); + + span = span.Slice(lengthOfName + 1); + + // Property getter + if (!MemoryMarshal.TryRead(span, out nint getter)) + { + return false; + } + + span = span.Slice(sizeof(nint)); + + // Property setter + if (!MemoryMarshal.TryRead(span, out nint setter)) + { + return false; + } + + span = span.Slice(sizeof(nint)); + + propertyBindings[i] = new PropertyBinding(name, getter, setter); + } + + // Effect factory + if (!MemoryMarshal.TryRead(span, out nint effectFactory)) + { + return false; + } + + span = span.Slice(sizeof(nint)); + + // If the buffer is bigger than expected, also consider it malformed + if (span.Length > 0) + { + return false; + } + + data = new D2D1EffectRegistrationData( + effectId, + numberOfInputs, + xml, + propertyBindings, + effectFactory); + + return true; + } + + /// + /// A model representing a property binding for an effect. + /// + public readonly struct PropertyBinding + { + /// + /// Creates a new instance with the specified parameters. + /// + /// The property name. + /// A pointer to the property getter callback + /// A pointer to the property setter callback + internal PropertyBinding(string name, nint getter, nint setter) + { + Name = name; + Getter = (void*)getter; + Setter = (void*)setter; + } + + /// + /// Gets the name of the property. + /// + public string Name { get; } + + /// + /// Gets the property getter (a ). + /// + public void* Getter { get; } + + /// + /// Gets the property setter (a ). + /// + public void* Setter { get; } + } +} diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs index f7242758..2ec05819 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1InteropServices.Effects.cs @@ -155,6 +155,14 @@ unsafe partial class D2D1InteropServices /// 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 ). @@ -169,6 +177,9 @@ unsafe partial class D2D1InteropServices /// 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 . /// ///