From aae13e1c3e4097b0d6f5c3cef513147483c834aa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 1 Oct 2023 00:55:43 +0200 Subject: [PATCH] Use centralized XML writer for effect registration --- .../Shaders/Interop/D2D1PixelShaderEffect.cs | 223 ++---------------- 1 file changed, 19 insertions(+), 204 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 71ac7af4..5966c1f2 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.GetOrAllocateBuffer()) 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(); } /// @@ -355,108 +253,25 @@ public static unsafe class D2D1PixelShaderEffect writer.WriteRaw(PixelShaderEffect.For.Instance.Id); writer.WriteRaw(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.GetOrAllocateBuffer(); + 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.WriteRaw(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);