diff --git a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs
index 3a19393f..012eca14 100644
--- a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs
+++ b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs
@@ -8,6 +8,18 @@ namespace ComputeSharp.D2D1.Helpers;
///
internal static partial class ThrowHelper
{
+ ///
+ /// Throws a new .
+ ///
+ /// The argument name.
+ /// The message to include in the exception.
+ /// Thrown with the specified parameters.
+ [DoesNotReturn]
+ public static void ThrowArgumentException(string? name, string? message)
+ {
+ throw new ArgumentException(message, name);
+ }
+
///
/// Throws a new .
///
diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs
index fc345dca..cba3bc9b 100644
--- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs
+++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1EffectRegistrationData.cs
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
+using ComputeSharp.D2D1.Helpers;
#if !NET6_0_OR_GREATER
using ComputeSharp.D2D1.NetStandard.System.Text;
#endif
@@ -79,6 +80,54 @@ public static class D2D1EffectRegistrationData
///
public void* EffectFactory { get; }
+ ///
+ /// Loads a instance from the input binary blob.
+ ///
+ /// The input binary blob to deserialize.
+ /// The resulting instance.
+ /// Thrown if the loading failed.
+ ///
+ /// The input blob should have been created by a call to any of the overloads
+ /// of .
+ ///
+ public static V1 Load(ReadOnlyMemory blob)
+ {
+ switch (TryLoadAndGetResult(blob, out V1 data))
+ {
+ case LoadingResult.Success:
+ return data;
+ case LoadingResult.EmptyBuffer:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is empty.");
+ break;
+ case LoadingResult.BufferTooShort:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is too short.");
+ break;
+ case LoadingResult.BufferTooLong:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob is too long.");
+ break;
+ case LoadingResult.MismatchedBlobVersionId:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The blob version id from the input blob does not match.");
+ break;
+ case LoadingResult.InvalidNumberOfInputs:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob has an invalid number of inputs.");
+ break;
+ case LoadingResult.InvalidPropertyXml:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The property XML value in the input blob is invalid.");
+ break;
+ case LoadingResult.InvalidNumberOfBindings:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob declares an invalid number of bindings.");
+ break;
+ case LoadingResult.InvalidBindingPropertyName:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob has an invalid property name for one of its property bindings.");
+ break;
+ default:
+ ThrowHelper.ThrowArgumentException(nameof(blob), "The input blob failed to be loaded for an unknown reason.");
+ break;
+ }
+
+ return data;
+ }
+
///
/// Tries to load a instance from the input binary blob.
///
@@ -89,22 +138,38 @@ public static class D2D1EffectRegistrationData
/// The input blob should have been created by a call to any of the overloads
/// of .
///
- public static unsafe bool TryLoad(ReadOnlyMemory blob, out V1 data)
+ public static bool TryLoad(ReadOnlyMemory blob, out V1 data)
+ {
+ return TryLoadAndGetResult(blob, out data) is LoadingResult.Success;
+ }
+
+ ///
+ /// Tries to load a instance from the input binary blob and returns a result.
+ ///
+ /// The input binary blob to deserialize.
+ /// The resulting instance, if successful.
+ /// The result of the loading operation.
+ private static LoadingResult TryLoadAndGetResult(ReadOnlyMemory blob, out V1 data)
{
data = default;
if (blob.IsEmpty)
{
- return false;
+ return LoadingResult.EmptyBuffer;
}
ReadOnlySpan span = blob.Span;
// Blob id
- if (!MemoryMarshal.TryRead(span, out Guid blobId) ||
- blobId != BlobId)
+ if (!MemoryMarshal.TryRead(span, out Guid blobId))
{
- return false;
+ return LoadingResult.BufferTooShort;
+ }
+
+ // Validate the blob id
+ if (blobId != BlobId)
+ {
+ return LoadingResult.MismatchedBlobVersionId;
}
span = span.Slice(sizeof(Guid));
@@ -112,16 +177,21 @@ public static class D2D1EffectRegistrationData
// Effect class id
if (!MemoryMarshal.TryRead(span, out Guid effectId))
{
- return false;
+ return LoadingResult.BufferTooShort;
}
span = span.Slice(sizeof(Guid));
// Number of inputs
- if (!MemoryMarshal.TryRead(span, out int numberOfInputs) ||
- numberOfInputs < 0)
+ if (!MemoryMarshal.TryRead(span, out int numberOfInputs))
{
- return false;
+ return LoadingResult.BufferTooShort;
+ }
+
+ // Validate the number of inputs
+ if (numberOfInputs < 0)
+ {
+ return LoadingResult.InvalidNumberOfInputs;
}
span = span.Slice(sizeof(int));
@@ -131,7 +201,7 @@ public static class D2D1EffectRegistrationData
if (lengthOfXml == -1)
{
- return false;
+ return LoadingResult.InvalidPropertyXml;
}
string xml = Encoding.UTF8.GetString(span.Slice(0, lengthOfXml));
@@ -139,10 +209,15 @@ public static class D2D1EffectRegistrationData
span = span.Slice(lengthOfXml + 1);
// Number of bindings
- if (!MemoryMarshal.TryRead(span, out int numberOfBindings) ||
- numberOfBindings < 0)
+ if (!MemoryMarshal.TryRead(span, out int numberOfBindings))
{
- return false;
+ return LoadingResult.BufferTooShort;
+ }
+
+ // Validate the number of bindings
+ if (numberOfBindings < 0)
+ {
+ return LoadingResult.InvalidNumberOfBindings;
}
span = span.Slice(sizeof(int));
@@ -157,7 +232,7 @@ public static class D2D1EffectRegistrationData
if (lengthOfName == -1)
{
- return false;
+ return LoadingResult.InvalidBindingPropertyName;
}
string name = Encoding.UTF8.GetString(span.Slice(0, lengthOfName));
@@ -167,7 +242,7 @@ public static class D2D1EffectRegistrationData
// Property get function
if (!MemoryMarshal.TryRead(span, out nint getFunction))
{
- return false;
+ return LoadingResult.BufferTooShort;
}
span = span.Slice(sizeof(nint));
@@ -175,7 +250,7 @@ public static class D2D1EffectRegistrationData
// Property set function
if (!MemoryMarshal.TryRead(span, out nint setFunction))
{
- return false;
+ return LoadingResult.BufferTooShort;
}
span = span.Slice(sizeof(nint));
@@ -186,7 +261,7 @@ public static class D2D1EffectRegistrationData
// Effect factory
if (!MemoryMarshal.TryRead(span, out nint effectFactory))
{
- return false;
+ return LoadingResult.BufferTooShort;
}
span = span.Slice(sizeof(nint));
@@ -194,7 +269,7 @@ public static class D2D1EffectRegistrationData
// If the buffer is bigger than expected, also consider it malformed
if (span.Length > 0)
{
- return false;
+ return LoadingResult.BufferTooLong;
}
data = new V1(
@@ -204,7 +279,23 @@ public static class D2D1EffectRegistrationData
propertyBindings,
effectFactory);
- return true;
+ return LoadingResult.Success;
+ }
+
+ ///
+ /// Indicates the result of a blob loading operation.
+ ///
+ private enum LoadingResult
+ {
+ Success,
+ EmptyBuffer,
+ BufferTooShort,
+ BufferTooLong,
+ MismatchedBlobVersionId,
+ InvalidNumberOfInputs,
+ InvalidPropertyXml,
+ InvalidNumberOfBindings,
+ InvalidBindingPropertyName
}
}
}