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..a832e566 --- /dev/null +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/D2D1EffectXmlFactory.cs @@ -0,0 +1,183 @@ +using System; +using System.Text; +using System.Threading; +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 assume that the typical use of these APIs is not to ever try to concurrently register multiple + // effects, also because D2D factories are not even thread-safe anyway. So we can just serialize calls + // to the registration APIs and get away with a very simple and fast caching scheme, where we simply + // use a single StringBuilder instance as temporary buffer to write the XML text to. The builder is + // initialized with a size of 4096, which should be more than enough to hold pretty much all XML values. + // When the returned Buffer value is disposed, the lock will be released. + Monitor.Enter(builder); + + // Guard the XML building logic in a try/finally to avoid hanging + try + { + _ = XmlBuilder.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'); + } + catch + { + // Something went wrong building the XML, so we have to release the lock early + Monitor.Exit(builder); + } + + return new(builder); + } + + /// + /// A simple buffer for an effect XML blob. + /// + public readonly ref struct EffectXml + { + /// + /// The instance holding the registration blob. + /// + private readonly StringBuilder builder; + + /// + /// 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) + { + this.builder = builder; + } + + /// + /// Gets a for the buffer (pretty much always with no allocation). + /// + /// The value with the effect XML blob. + public ReadOnlySpan GetOrAllocateBuffer() + { +#if NET6_0_OR_GREATER + StringBuilder.ChunkEnumerator enumerator = this.builder.GetChunks(); + + if (enumerator.MoveNext()) + { + ReadOnlyMemory segment = enumerator.Current; + + if (!enumerator.MoveNext()) + { + return segment.Span; + } + } +#endif + + return this.builder.ToString().AsSpan(); + } + + /// + public void Dispose() + { + Monitor.Exit(this.builder); + } + } +} \ No newline at end of file