Add D2D1EffectRegistrationData type

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:31 +02:00
1 親 f6282f53ff
コミット a914c30cfb
3ファイル変更
+240

検索条件に一致するファイルはありません

+14
ファイルの表示
@@ -24,4 +24,18 @@ internal static class EncodingExtensions
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
}
}
/// <summary>
/// Decodes a text from a sequence of bytes.
/// </summary>
/// <param name="encoding">The input <see cref="Encoding"/> instance to use.</param>
/// <param name="bytes">The byte span that holds the encoded bytes.</param>
/// <returns>The resulting text.</returns>
public static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
{
fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes))
{
return encoding.GetString(bytesPtr, bytes.Length);
}
}
}
+215
ファイルの表示
@@ -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;
/// <summary>
/// A helper type used to read and validate effect registration blobs produced by
/// <see cref="D2D1InteropServices.GetPixelShaderEffectRegistrationBlob{T}(out Guid)"/>.
/// </summary>
public unsafe readonly struct D2D1EffectRegistrationData
{
/// <summary>
/// Creates a new <see cref="D2D1EffectRegistrationData"/> instance with the specified parameters.
/// </summary>
/// <param name="effectId">The effect id.</param>
/// <param name="numberOfInputs">The number of inputs for the effect.</param>
/// <param name="xml">The XML text that can be used to register the effect.</param>
/// <param name="propertyBindings">The <see cref="PropertyBinding"/> values for the effect.</param>
/// <param name="effectFactory">A pointer to the effect factory callback.</param>
private D2D1EffectRegistrationData(
Guid effectId,
int numberOfInputs,
string xml,
PropertyBinding[] propertyBindings,
nint effectFactory)
{
EffectId = effectId;
NumberOfInputs = numberOfInputs;
Xml = xml;
PropertyBindings = propertyBindings;
EffectFactory = (void*)effectFactory;
}
/// <summary>
/// Gets the id to use to register the effect.
/// </summary>
public Guid EffectId { get; }
/// <summary>
/// Gets the number of inputs for the effect.
/// </summary>
public int NumberOfInputs { get; }
/// <summary>
/// Gets the XML text with the effect description, that can be used to register it.
/// </summary>
public string Xml { get; }
/// <summary>
/// Gets the sequence of <see cref="PropertyBinding"/> values for the effect.
/// </summary>
public ReadOnlyMemory<PropertyBinding> PropertyBindings { get; }
/// <summary>
/// Gets a callback to an effect factory (a <see langword="delegate* unmanaged[Stdcall]&lt;IUnknown**, byte*, uint, HRESULT&gt;"/>).
/// </summary>
public void* EffectFactory { get; }
/// <summary>
/// Tries to load a <see cref="D2D1EffectRegistrationData"/> instance from the input binary blob.
/// </summary>
/// <param name="blob">The input binary blob to deserialize.</param>
/// <param name="data">The resulting <see cref="D2D1EffectRegistrationData"/> instance, if successful.</param>
/// <returns>Whether or not the input blob has been correctly deserialized (if it was not malformed).</returns>
/// <remarks>
/// The input blob should have been created by a call to any of the overloads of
/// <see cref="D2D1InteropServices.GetPixelShaderEffectRegistrationBlob{T}(out Guid)"/>.
/// </remarks>
public static unsafe bool TryLoad(ReadOnlyMemory<byte> blob, out D2D1EffectRegistrationData data)
{
data = default;
if (blob.IsEmpty)
{
return false;
}
ReadOnlySpan<byte> 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;
}
/// <summary>
/// A model representing a property binding for an effect.
/// </summary>
public readonly struct PropertyBinding
{
/// <summary>
/// Creates a new <see cref="PropertyBinding"/> instance with the specified parameters.
/// </summary>
/// <param name="name">The property name.</param>
/// <param name="getter">A pointer to the property getter callback</param>
/// <param name="setter">A pointer to the property setter callback</param>
internal PropertyBinding(string name, nint getter, nint setter)
{
Name = name;
Getter = (void*)getter;
Setter = (void*)setter;
}
/// <summary>
/// Gets the name of the property.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the property getter (a <see langword="delegate* unmanaged[Stdcall]&lt;IUnknown*, byte*, uint, uint*, HRESULT&gt;"/>).
/// </summary>
public void* Getter { get; }
/// <summary>
/// Gets the property setter (a <see langword="delegate* unmanaged[Stdcall]&lt;IUnknown*, byte*, uint, HRESULT&gt;"/>).
/// </summary>
public void* Setter { get; }
}
}
+11
ファイルの表示
@@ -155,6 +155,14 @@ unsafe partial class D2D1InteropServices
/// <param name="effectId">The <see cref="Guid"/> of the registered effect, which can be used to call <c>ID2D1DeviceContext::CreateEffect</c>.</param>
/// <remarks>
/// <para>
/// This blob can be useful to marshal registration data across an app domain boundary without having assembly references cross this boundary as well.
/// </para>
/// <para>
/// For instance, an application could allow plugins to create effects through the <c>ComputeSharp.D2D1</c> 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 <c>ComputeSharp.D2D1</c>, the two could still work side by side with no issues.
/// </para>
/// <para>
/// The binary blob contains information with the following format:
/// <list type="bullet">
/// <item>The effect id (a <see cref="Guid"/>).</item>
@@ -169,6 +177,9 @@ unsafe partial class D2D1InteropServices
/// The property name, getter and setter are grouped together after the number of bindings.
/// </para>
/// <para>
/// To make the deserialization easier, the <see cref="D2D1EffectRegistrationData"/> type can be used to read and validate the returned blob.
/// </para>
/// <para>
/// For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.
/// </para>
/// </remarks>