Add registration blob version id
このコミットが含まれているのは:
1 親
cab4986cfc
コミット
e6507f23b6
2ファイル変更
+162
-137
検索条件に一致するファイルはありません
@@ -20,171 +20,191 @@ namespace ComputeSharp.D2D1.Interop;
|
||||
/// For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public unsafe readonly struct D2D1EffectRegistrationData
|
||||
public static class D2D1EffectRegistrationData
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="D2D1EffectRegistrationData"/> instance with the specified parameters.
|
||||
/// The containing type for deserialized version 1 registration blobs.
|
||||
/// </summary>
|
||||
/// <param name="classId">The effect class id.</param>
|
||||
/// <param name="numberOfInputs">The number of inputs for the effect.</param>
|
||||
/// <param name="propertyXml">The XML text that can be used to register the effect.</param>
|
||||
/// <param name="propertyBindings">The <see cref="D2D1PropertyBinding"/> values for the effect.</param>
|
||||
/// <param name="effectFactory">A pointer to the effect factory callback.</param>
|
||||
private D2D1EffectRegistrationData(
|
||||
Guid classId,
|
||||
int numberOfInputs,
|
||||
string propertyXml,
|
||||
D2D1PropertyBinding[] propertyBindings,
|
||||
nint effectFactory)
|
||||
public unsafe readonly struct V1
|
||||
{
|
||||
ClassId = classId;
|
||||
NumberOfInputs = numberOfInputs;
|
||||
PropertyXml = propertyXml;
|
||||
PropertyBindings = propertyBindings;
|
||||
EffectFactory = (void*)effectFactory;
|
||||
}
|
||||
/// <summary>
|
||||
/// The blob id for V1 registration data.
|
||||
/// </summary>
|
||||
internal static readonly Guid BlobId = new(0xA1B4A55E, 0x4C78, 0x413F, 0xA2, 0xE6, 0x0A, 0x02, 0xE6, 0x0A, 0x76, 0x18);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the id to use to register the effect.
|
||||
/// </summary>
|
||||
public Guid ClassId { 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 PropertyXml { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sequence of <see cref="D2D1PropertyBinding"/> values for the effect.
|
||||
/// </summary>
|
||||
public ReadOnlyMemory<D2D1PropertyBinding> PropertyBindings { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a callback to an effect factory (a <see langword="delegate* unmanaged[Stdcall]<IUnknown**, HRESULT>"/>).
|
||||
/// </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="D2D1PixelShaderEffect.GetRegistrationBlob{T}(out Guid)"/>.
|
||||
/// </remarks>
|
||||
public static unsafe bool TryLoad(ReadOnlyMemory<byte> blob, out D2D1EffectRegistrationData data)
|
||||
{
|
||||
data = default;
|
||||
|
||||
if (blob.IsEmpty)
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="V1"/> instance with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="classId">The effect class id.</param>
|
||||
/// <param name="numberOfInputs">The number of inputs for the effect.</param>
|
||||
/// <param name="propertyXml">The XML text that can be used to register the effect.</param>
|
||||
/// <param name="propertyBindings">The <see cref="D2D1PropertyBinding"/> values for the effect.</param>
|
||||
/// <param name="effectFactory">A pointer to the effect factory callback.</param>
|
||||
private V1(
|
||||
Guid classId,
|
||||
int numberOfInputs,
|
||||
string propertyXml,
|
||||
D2D1PropertyBinding[] propertyBindings,
|
||||
nint effectFactory)
|
||||
{
|
||||
return false;
|
||||
ClassId = classId;
|
||||
NumberOfInputs = numberOfInputs;
|
||||
PropertyXml = propertyXml;
|
||||
PropertyBindings = propertyBindings;
|
||||
EffectFactory = (void*)effectFactory;
|
||||
}
|
||||
|
||||
ReadOnlySpan<byte> span = blob.Span;
|
||||
/// <summary>
|
||||
/// Gets the id to use to register the effect.
|
||||
/// </summary>
|
||||
public Guid ClassId { get; }
|
||||
|
||||
// Effect class id
|
||||
if (!MemoryMarshal.TryRead(span, out Guid effectId))
|
||||
/// <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 PropertyXml { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sequence of <see cref="D2D1PropertyBinding"/> values for the effect.
|
||||
/// </summary>
|
||||
public ReadOnlyMemory<D2D1PropertyBinding> PropertyBindings { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a callback to an effect factory (a <see langword="delegate* unmanaged[Stdcall]<IUnknown**, HRESULT>"/>).
|
||||
/// </summary>
|
||||
public void* EffectFactory { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Tries to load a <see cref="V1"/> instance from the input binary blob.
|
||||
/// </summary>
|
||||
/// <param name="blob">The input binary blob to deserialize.</param>
|
||||
/// <param name="data">The resulting <see cref="V1"/> 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="D2D1PixelShaderEffect.GetRegistrationBlob{T}(out Guid)"/>.
|
||||
/// </remarks>
|
||||
public static unsafe bool TryLoad(ReadOnlyMemory<byte> blob, out V1 data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
data = default;
|
||||
|
||||
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 property 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));
|
||||
|
||||
D2D1PropertyBinding[] propertyBindings = new D2D1PropertyBinding[numberOfBindings];
|
||||
|
||||
// Property bindings
|
||||
for (int i = 0; i < numberOfBindings; i++)
|
||||
{
|
||||
// Property name
|
||||
int lengthOfName = span.IndexOf((byte)'\0');
|
||||
|
||||
if (lengthOfName == -1)
|
||||
if (blob.IsEmpty)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string name = Encoding.UTF8.GetString(span.Slice(0, lengthOfName));
|
||||
ReadOnlySpan<byte> span = blob.Span;
|
||||
|
||||
span = span.Slice(lengthOfName + 1);
|
||||
// Blob id
|
||||
if (!MemoryMarshal.TryRead(span, out Guid blobId) ||
|
||||
blobId != BlobId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Property get function
|
||||
if (!MemoryMarshal.TryRead(span, out nint getFunction))
|
||||
span = span.Slice(sizeof(Guid));
|
||||
|
||||
// Effect class 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 property 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));
|
||||
|
||||
D2D1PropertyBinding[] propertyBindings = new D2D1PropertyBinding[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 get function
|
||||
if (!MemoryMarshal.TryRead(span, out nint getFunction))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(nint));
|
||||
|
||||
// Property set function
|
||||
if (!MemoryMarshal.TryRead(span, out nint setFunction))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(nint));
|
||||
|
||||
propertyBindings[i] = new D2D1PropertyBinding(name, (void*)getFunction, (void*)setFunction);
|
||||
}
|
||||
|
||||
// Effect factory
|
||||
if (!MemoryMarshal.TryRead(span, out nint effectFactory))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(nint));
|
||||
|
||||
// Property set function
|
||||
if (!MemoryMarshal.TryRead(span, out nint setFunction))
|
||||
// If the buffer is bigger than expected, also consider it malformed
|
||||
if (span.Length > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
span = span.Slice(sizeof(nint));
|
||||
data = new V1(
|
||||
effectId,
|
||||
numberOfInputs,
|
||||
xml,
|
||||
propertyBindings,
|
||||
effectFactory);
|
||||
|
||||
propertyBindings[i] = new D2D1PropertyBinding(name, (void*)getFunction, (void*)setFunction);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@@ -170,6 +170,7 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
/// <para>
|
||||
/// The binary blob contains information with the following format:
|
||||
/// <list type="bullet">
|
||||
/// <item>The blob version id (a <see cref="Guid"/>).</item>
|
||||
/// <item>The effect id (a <see cref="Guid"/>).</item>
|
||||
/// <item>The number of inputs for the effect (an <see cref="int"/>).</item>
|
||||
/// <item>The effect XML description (as null-terminated UTF8 text).</item>
|
||||
@@ -183,6 +184,7 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// To make the deserialization easier, the <see cref="D2D1EffectRegistrationData"/> 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. <see cref="D2D1EffectRegistrationData.V1"/>).
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For more info, see <see href="https://docs.microsoft.com/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1factory1-registereffectfromstring"/>.
|
||||
@@ -197,6 +199,9 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
|
||||
using ArrayPoolBinaryWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize);
|
||||
|
||||
// Blob id
|
||||
writer.Write(D2D1EffectRegistrationData.V1.BlobId);
|
||||
|
||||
// Effect id and number of inputs
|
||||
writer.Write(PixelShaderEffect.For<T>.Id);
|
||||
writer.Write(PixelShaderEffect.For<T>.NumberOfInputs);
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする