diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs similarity index 69% rename from src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs rename to src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs index ba7c3cf5..c7a95502 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter.cs @@ -5,32 +5,27 @@ using System.Diagnostics.CodeAnalysis; using System.Numerics; #endif using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; #if !NET6_0_OR_GREATER using BitOperations = ComputeSharp.NetStandard.BitOperations; #endif namespace ComputeSharp.D2D1.Shaders.Interop.Buffers; -/// -internal static class ArrayPoolBinaryWriter +/// +/// Represents a heap-based, array-backed output sink into which binary data can be written. +/// +internal ref struct ArrayPoolBinaryWriter { /// /// The default buffer size to use to expand empty arrays. /// public const int DefaultInitialBufferSize = 256; -} -/// -/// Represents a heap-based, array-backed output sink into which data can be written. -/// -/// The type of values to write. -internal ref struct ArrayPoolBufferWriter - where T : unmanaged -{ /// - /// The underlying array. + /// The underlying array. /// - private T[]? array; + private byte[]? array; /// /// The starting offset within . @@ -38,11 +33,11 @@ internal ref struct ArrayPoolBufferWriter private int index; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the type. /// - public ArrayPoolBufferWriter(int capacity) + public ArrayPoolBinaryWriter(int capacity) { - this.array = ArrayPool.Shared.Rent(capacity); + this.array = ArrayPool.Shared.Rent(capacity); this.index = 0; } @@ -50,12 +45,12 @@ internal ref struct ArrayPoolBufferWriter /// Gets the data written to the underlying buffer so far, as a . /// [UnscopedRef] - public readonly ReadOnlySpan WrittenSpan + public readonly ReadOnlySpan WrittenSpan { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - T[]? array = this.array; + byte[]? array = this.array; default(ObjectDisposedException).ThrowIfNull(array); @@ -63,17 +58,59 @@ internal ref struct ArrayPoolBufferWriter } } + /// + /// Writes the raw value of type into the buffer. + /// + /// The type of value to write. + /// The value to write. + /// This method will just blit the data of into the target buffer. + public unsafe void Write(scoped in T value) + { + Span span = GetSpan(sizeof(T)); + + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(span), value); + + Advance(sizeof(T)); + } + + /// + /// Writes the raw data from the input into the buffer. + /// + /// The data to write. + public void Write(scoped ReadOnlySpan data) + { + Span span = GetSpan(data.Length); + + data.CopyTo(span); + + Advance(data.Length); + } + + /// + public void Dispose() + { + byte[]? array = this.array; + + if (array is null) + { + return; + } + + this.array = null; + + ArrayPool.Shared.Return(array); + } + /// /// Advances the current writer. /// /// The amount to advance. /// Must be called after . - internal void Advance(int count) + private void Advance(int count) { - T[]? array = this.array; + byte[]? array = this.array; default(ObjectDisposedException).ThrowIfNull(array); - default(ArgumentOutOfRangeException).ThrowIfNegative(count); default(ArgumentException).ThrowIf(this.index > array.Length - count); this.index += count; @@ -85,7 +122,7 @@ internal ref struct ArrayPoolBufferWriter /// The capacity to request. /// A to write data to. [UnscopedRef] - internal Span GetSpan(int sizeHint = 0) + private Span GetSpan(int sizeHint = 0) { CheckBufferAndEnsureCapacity(sizeHint); @@ -99,7 +136,7 @@ internal ref struct ArrayPoolBufferWriter [MethodImpl(MethodImplOptions.AggressiveInlining)] private void CheckBufferAndEnsureCapacity(int sizeHint) { - T[]? array = this.array; + byte[]? array = this.array; default(ObjectDisposedException).ThrowIfNull(array); default(ArgumentOutOfRangeException).ThrowIfNegative(sizeHint); @@ -134,27 +171,12 @@ internal ref struct ArrayPoolBufferWriter minimumSize = BitOperations.RoundUpToPowerOf2(minimumSize); } - T[] newArray = ArrayPool.Shared.Rent((int)minimumSize); + byte[] newArray = ArrayPool.Shared.Rent((int)minimumSize); - Buffer.BlockCopy(this.array!, 0, newArray, 0, this.index * sizeof(T)); + Buffer.BlockCopy(this.array!, 0, newArray, 0, this.index); - ArrayPool.Shared.Return(this.array!); + ArrayPool.Shared.Return(this.array!); this.array = newArray; } - - /// - public void Dispose() - { - T[]? array = this.array; - - if (array is null) - { - return; - } - - this.array = null; - - ArrayPool.Shared.Return(array); - } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs deleted file mode 100644 index a4d08422..00000000 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -#if NET6_0_OR_GREATER -using System.Buffers.Text; -#endif -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; - -namespace ComputeSharp.D2D1.Shaders.Interop.Buffers; - -/// -/// Helpers for the type. -/// -internal static class ArrayPoolBinaryWriterExtensions -{ - /// - /// Writes the raw value of type into the buffer. - /// - /// The type of value to write. - /// The target instance to write data to. - /// The value to write. - /// This method will just blit the data of into the target buffer. - public static unsafe void WriteRaw(this ref ArrayPoolBufferWriter writer, scoped in T value) - where T : unmanaged - { - Span span = writer.GetSpan(sizeof(T)); - - Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(span), value); - - writer.Advance(sizeof(T)); - } - - /// - /// Writes the raw value of type into the buffer. - /// - /// The type of value to write. - /// The target instance to write data to. - /// The value to write. - public static unsafe void WriteRaw(this ref ArrayPoolBufferWriter writer, scoped in T value) - where T : unmanaged - { - Span span = writer.GetSpan(1); - - MemoryMarshal.GetReference(span) = value; - - writer.Advance(1); - } - - /// - /// Writes the raw data from the input into the buffer. - /// - /// The type of values in the target writer. - /// The target instance to write data to. - /// The data to write. - public static void WriteRaw(this ref ArrayPoolBufferWriter writer, scoped ReadOnlySpan data) - where T : unmanaged - { - Span span = writer.GetSpan(data.Length); - - data.CopyTo(span); - - writer.Advance(data.Length); - } - -#if !NET6_0_OR_GREATER - /// - /// Writes the raw data from the input into the buffer. - /// - /// The target instance to write data to. - /// The data to write. - public static void WriteRaw(this ref ArrayPoolBufferWriter writer, string data) - { - Span span = writer.GetSpan(data.Length); - - data.AsSpan().CopyTo(span); - - writer.Advance(data.Length); - } -#endif - - /// - /// Writes an value as UTF8 bytes into the buffer. - /// - /// The target instance to write data to. - /// The value to write. - public static void WriteAsUtf8(this ref ArrayPoolBufferWriter writer, int value) - { -#if NET6_0_OR_GREATER - // Get a span of at least 10 elements (10 is the length of int.MaxValue as text) - Span span = writer.GetSpan(10); - - _ = Utf8Formatter.TryFormat(value, span, out int bytesWritten); - - writer.Advance(bytesWritten); -#else - // Just accept the allocation on .NET Standard 2.0 - writer.WriteAsUtf8(value.ToString()); -#endif - } - - /// - /// Writes text as UTF8 bytes into the buffer. - /// - /// The target instance to write data to. - /// The text to write. - public static void WriteAsUtf8(this ref ArrayPoolBufferWriter writer, string text) - { - int maximumByteSize = Encoding.UTF8.GetMaxByteCount(text.Length); - - Span span = writer.GetSpan(maximumByteSize); - - int writtenBytes = Encoding.UTF8.GetBytes(text.AsSpan(), span); - - writer.Advance(writtenBytes); - } - - /// - /// Writes an value as Unicode bytes into the buffer. - /// - /// The target instance to write data to. - /// The value to write. - public static void WriteAsUnicode(this ref ArrayPoolBufferWriter writer, int value) - { -#if NET6_0_OR_GREATER - // Get a span of at least 10 elements (10 is the length of int.MaxValue as text) - Span span = writer.GetSpan(10); - - _ = value.TryFormat(span, out int charsWritten); - - writer.Advance(charsWritten); -#else - // Just accept the allocation on .NET Standard 2.0 - writer.WriteRaw(value.ToString().AsSpan()); -#endif - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs deleted file mode 100644 index 72f366df..00000000 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/PinnedBufferMemoryManager.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Buffers; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace ComputeSharp.D2D1.Shaders.Interop.Buffers; - -/// -/// A custom that wraps a pinned memory buffer. -/// -internal sealed unsafe class PinnedBufferMemoryManager : MemoryManager -{ - /// - /// A pointer to the buffer. - /// - private readonly byte* buffer; - - /// - /// The length of the buffer. - /// - private readonly int length; - - /// - /// Initializes a new instance of the class with the specified parameters. - /// - /// A wrapping the input pinned buffer. - public PinnedBufferMemoryManager(ReadOnlySpan span) - { - this.buffer = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span)); - this.length = span.Length; - } - - /// - public override Span GetSpan() - { - return new(this.buffer, this.length); - } - - /// - public override unsafe MemoryHandle Pin(int elementIndex = 0) - { - default(ArgumentOutOfRangeException).ThrowIfNotInRange(elementIndex, 0, this.length); - - return new(this.buffer + elementIndex); - } - - /// - public override void Unpin() - { - } - - /// - protected override void Dispose(bool disposing) - { - } -} \ No newline at end of file diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 71ac7af4..a248c778 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -1,5 +1,7 @@ using System; +using System.Buffers; using System.Runtime.CompilerServices; +using System.Text; using ComputeSharp.D2D1.Descriptors; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Interop.Effects; @@ -101,111 +103,9 @@ public static unsafe class D2D1PixelShaderEffect effectId = default; - ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); + using D2D1EffectXmlFactory.EffectXml effectXml = D2D1EffectXmlFactory.GetXmlBuffer(); - // Build the XML text - writer.WriteRaw(""" - - - - - - - - - """); - - // Add the input nodes - for (int i = 0; i < PixelShaderEffect.For.Instance.InputCount; i++) - { - writer.WriteRaw(" - - """); - } - - // Write the last part of the XML (including the buffer property) - writer.WriteRaw(""" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - """); - - // Null terminator for the text - writer.WriteRaw('\0'); - - fixed (char* pXml = writer.WrittenSpan) + fixed (char* pXml = effectXml.GetBuffer()) fixed (char* pBufferPropertyName = nameof(D2D1PixelShaderEffectProperty.ConstantBuffer)) fixed (char* pResourceTextureManager0PropertyName = nameof(D2D1PixelShaderEffectProperty.ResourceTextureManager0)) fixed (char* pResourceTextureManager1PropertyName = nameof(D2D1PixelShaderEffectProperty.ResourceTextureManager1)) @@ -301,8 +201,6 @@ public static unsafe class D2D1PixelShaderEffect effectId = PixelShaderEffect.For.Instance.Id; } - - writer.Dispose(); } /// @@ -346,194 +244,111 @@ public static unsafe class D2D1PixelShaderEffect public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor { - ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); + ArrayPoolBinaryWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); // Blob id - writer.WriteRaw(D2D1EffectRegistrationData.V1.BlobId); + writer.Write(D2D1EffectRegistrationData.V1.BlobId); // Effect id and number of inputs - writer.WriteRaw(PixelShaderEffect.For.Instance.Id); - writer.WriteRaw(PixelShaderEffect.For.Instance.InputCount); + writer.Write(PixelShaderEffect.For.Instance.Id); + writer.Write(PixelShaderEffect.For.Instance.InputCount); - // Build the XML text - writer.WriteRaw(""" - - - - - - - - - """u8); - - // Add the input nodes - for (int i = 0; i < PixelShaderEffect.For.Instance.InputCount; i++) + // Retrieve the effect XML and include it into the registration blob + using (D2D1EffectXmlFactory.EffectXml effectXml = D2D1EffectXmlFactory.GetXmlBuffer()) { - writer.WriteRaw(" + ReadOnlySpan registrationText = effectXml.GetBuffer(); + int maxRegistrationTextByteCount = Encoding.UTF8.GetMaxByteCount(registrationText.Length); + byte[] registrationTextUtf8 = ArrayPool.Shared.Rent(maxRegistrationTextByteCount); - """u8); + // Encode the effect XML into UTF8 to write into the registration blob. + // This is marginally slower than building it inline here directly in + // UTF8 format, but the registration blob is not the primary scenario. + // Besides, the overhead is just a matter of a few more microseconds. + int registrationTextByteCount = Encoding.UTF8.GetBytes(registrationText, registrationTextUtf8); + + // Build the XML text + writer.Write(registrationTextUtf8.AsSpan(0, registrationTextByteCount)); + + ArrayPool.Shared.Return(registrationTextUtf8); } - // Write the last part of the XML (including the buffer property) - writer.WriteRaw(""" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - """u8); - - // Null terminator for the text - writer.WriteRaw((byte)'\0'); - // Bindings - writer.WriteRaw(D2D1PixelShaderEffectProperty.NumberOfProperties); - writer.WriteRaw("ConstantBuffer"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetConstantBuffer); - writer.WriteRaw((nint)PixelShaderEffect.SetConstantBuffer); - writer.WriteRaw("ResourceTextureManager0"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager0); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager0); - writer.WriteRaw("ResourceTextureManager1"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager1); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager1); - writer.WriteRaw("ResourceTextureManager2"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager2); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager2); - writer.WriteRaw("ResourceTextureManager3"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager3); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager3); - writer.WriteRaw("ResourceTextureManager4"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager4); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager4); - writer.WriteRaw("ResourceTextureManager5"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager5); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager5); - writer.WriteRaw("ResourceTextureManager6"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager6); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager6); - writer.WriteRaw("ResourceTextureManager7"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager7); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager7); - writer.WriteRaw("ResourceTextureManager8"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager8); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager8); - writer.WriteRaw("ResourceTextureManager9"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager9); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager9); - writer.WriteRaw("ResourceTextureManager10"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager10); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager10); - writer.WriteRaw("ResourceTextureManager11"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager11); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager11); - writer.WriteRaw("ResourceTextureManager12"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager12); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager12); - writer.WriteRaw("ResourceTextureManager13"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager13); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager13); - writer.WriteRaw("ResourceTextureManager14"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager14); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager14); - writer.WriteRaw("ResourceTextureManager15"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetResourceTextureManager15); - writer.WriteRaw((nint)PixelShaderEffect.SetResourceTextureManager15); - writer.WriteRaw("TransformMapper"u8); - writer.WriteRaw((byte)'\0'); - writer.WriteRaw((nint)PixelShaderEffect.GetTransformMapper); - writer.WriteRaw((nint)PixelShaderEffect.SetTransformMapper); + writer.Write(D2D1PixelShaderEffectProperty.NumberOfProperties); + writer.Write("ConstantBuffer"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetConstantBuffer); + writer.Write((nint)PixelShaderEffect.SetConstantBuffer); + writer.Write("ResourceTextureManager0"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager0); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager0); + writer.Write("ResourceTextureManager1"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager1); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager1); + writer.Write("ResourceTextureManager2"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager2); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager2); + writer.Write("ResourceTextureManager3"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager3); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager3); + writer.Write("ResourceTextureManager4"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager4); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager4); + writer.Write("ResourceTextureManager5"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager5); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager5); + writer.Write("ResourceTextureManager6"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager6); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager6); + writer.Write("ResourceTextureManager7"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager7); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager7); + writer.Write("ResourceTextureManager8"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager8); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager8); + writer.Write("ResourceTextureManager9"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager9); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager9); + writer.Write("ResourceTextureManager10"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager10); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager10); + writer.Write("ResourceTextureManager11"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager11); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager11); + writer.Write("ResourceTextureManager12"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager12); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager12); + writer.Write("ResourceTextureManager13"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager13); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager13); + writer.Write("ResourceTextureManager14"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager14); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager14); + writer.Write("ResourceTextureManager15"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetResourceTextureManager15); + writer.Write((nint)PixelShaderEffect.SetResourceTextureManager15); + writer.Write("TransformMapper"u8); + writer.Write((byte)'\0'); + writer.Write((nint)PixelShaderEffect.GetTransformMapper); + writer.Write((nint)PixelShaderEffect.SetTransformMapper); // Effect factory - writer.WriteRaw((nint)PixelShaderEffect.For.Instance.Factory); + writer.Write((nint)PixelShaderEffect.For.Instance.Factory); byte[] registrationBlob = writer.WrittenSpan.ToArray(); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs new file mode 100644 index 00000000..36f6bc56 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs @@ -0,0 +1,170 @@ +using System; +using System.Buffers; +using System.Text; +using ComputeSharp.D2D1.Descriptors; + +#pragma warning disable CS0618 + +namespace ComputeSharp.D2D1.Interop; + +/// +/// A factory type to produce effect registration XML blobs. +/// +internal static unsafe class D2D1EffectXmlFactory +{ + /// + /// The shared instance to use to build effect registration XML blobs. + /// + private static readonly StringBuilder XmlBuilder = new(4096); + + /// + /// Gets a instance with the effect registration XML blob for a given shader type. + /// + /// The type of shader type to build the XML for. + /// The instance with the effect registration XML blob. + public static EffectXml GetXmlBuffer() + where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor + { + StringBuilder builder = XmlBuilder; + + // We use a simple caching schema here with a single StringBuilder instance shared across all threads. + // It is only used to write the effect XML blob and copy it, which is extremely fast. The lock itself + // is not taken during the actual effect registration, but only to build the effect XML text. The builder + // is initialized with a size of 4096, which should be more than enough to hold pretty much all XML values. + lock (builder) + { + _ = builder.Clear(); + + // Write the metadata properties for the effect + _ = builder.Append($""" + + + + + + + + """); + + // Add the input nodes, if any + for (int i = 0; i < D2D1PixelShader.GetInputCount(); i++) + { + _ = builder.Append('\n'); + _ = builder.Append($" "); + } + + // Close the inputs tag and add all effect properties + _ = builder.Append('\n'); + _ = builder.Append(""" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + """); + + // Null terminator for the text + _ = builder.Append('\0'); + + return new(builder); + } + } + + /// + /// A simple buffer for an effect XML blob. + /// + public readonly ref struct EffectXml + { + /// + /// The buffer with the effect XML blob. + /// + private readonly char[] buffer; + + /// + /// The length of the effect XML blob. + /// + private readonly int length; + + /// + /// Creates a new value with the specified parameters. + /// + /// The instance with the effect XML. + [Obsolete($"This should only be used by {nameof(D2D1EffectXmlFactory)}.")] + public EffectXml(StringBuilder builder) + { + // Rent a buffer from the pool with the current builder size + this.buffer = ArrayPool.Shared.Rent(builder.Length); + this.length = builder.Length; + + // Copy the current builder content with the effect XML blob into the + // rented buffer. This allows the builder to be reused for other effects. + builder.CopyTo(0, this.buffer, 0, builder.Length); + } + + /// + /// Gets a for the effect XML blob. + /// + /// The value with the effect XML blob. + public ReadOnlySpan GetBuffer() + { + return this.buffer.AsSpan(0, this.length); + } + + /// + public void Dispose() + { + ArrayPool.Shared.Return(this.buffer); + } + } +} \ No newline at end of file