From 876d1940789279627bbe7d0b5a91b936ef2906fc Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 17 Aug 2022 15:54:06 +0200 Subject: [PATCH] Make D2D1PixelShaderEffect.RegisterForD2D1Factory1 formatting zero-alloc --- .../ArrayPoolBinaryWriterExtensions.cs | 58 ++++++- .../Buffers/ArrayPoolBinaryWriter{T}.cs | 4 +- .../Shaders/Interop/D2D1PixelShaderEffect.cs | 154 ++++++++++-------- 3 files changed, 142 insertions(+), 74 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs index a187cc4a..b3e28d2c 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriterExtensions.cs @@ -33,20 +33,54 @@ internal static class ArrayPoolBinaryWriterExtensions 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 in 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 in ArrayPoolBufferWriter writer, scoped ReadOnlySpan data) + public static void WriteRaw(this in ArrayPoolBufferWriter writer, scoped ReadOnlySpan data) + where T : unmanaged { - Span span = writer.GetSpan(data.Length); + 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 in 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. /// @@ -82,4 +116,24 @@ internal static class ArrayPoolBinaryWriterExtensions 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 in 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/ArrayPoolBinaryWriter{T}.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs index c59ef460..58e3ee65 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Buffers/ArrayPoolBinaryWriter{T}.cs @@ -143,7 +143,7 @@ internal ref struct ArrayPoolBufferWriter /// /// The minimum number of items to ensure space for in . [MethodImpl(MethodImplOptions.NoInlining)] - private readonly void ResizeBuffer(int sizeHint) + private readonly unsafe void ResizeBuffer(int sizeHint) { uint minimumSize = (uint)this.index + (uint)sizeHint; @@ -159,7 +159,7 @@ internal ref struct ArrayPoolBufferWriter T[] newArray = ArrayPool.Shared.Rent((int)minimumSize); - Buffer.BlockCopy(this.array!, 0, newArray, 0, this.index); + Buffer.BlockCopy(this.array!, 0, newArray, 0, this.index * sizeof(T)); ArrayPool.Shared.Return(this.array!); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index cd92376b..d79fcfb4 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -1,7 +1,5 @@ using System; -using System.Globalization; using System.Runtime.CompilerServices; -using System.Text; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop.Effects; @@ -84,80 +82,96 @@ public static unsafe class D2D1PixelShaderEffect PixelShaderEffect.For.Initialize(mapperFactory); - // Setup the input string - StringBuilder effectInputsBuilder = new(); + using ArrayPoolBufferWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); + // Build the XML text + writer.WriteRaw(""" + + + + + + + + + """); + + // Add the input nodes for (int i = 0; i < PixelShaderEffect.For.InputCount; i++) { - effectInputsBuilder.Append(""); + writer.WriteRaw(" + + """); } - // Prepare the XML with a variable number of inputs - string xml = @$" - - - - - - - {effectInputsBuilder} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"; + // Write the last part of the XML (including the buffer property) + writer.WriteRaw(""" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + """); - fixed (char* pXml = xml) + // Null terminator for the text + writer.WriteRaw('\0'); + + fixed (char* pXml = writer.WrittenSpan) fixed (char* pBufferPropertyName = "ConstantBuffer") fixed (char* pResourceTextureManager0PropertyName = "ResourceTextureManager0") fixed (char* pResourceTextureManager1PropertyName = "ResourceTextureManager1")