Merge pull request #585 from Sergio0694/dev/shared-effect-xml
Unify effect XML building logic, optimize on .NET 6
このコミットが含まれているのは:
5ファイル変更
+329
-514
検索条件に一致するファイルはありません
+62
-40
@@ -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;
|
||||
|
||||
/// <inheritdoc cref="ArrayPoolBufferWriter{T}"/>
|
||||
internal static class ArrayPoolBinaryWriter
|
||||
/// <summary>
|
||||
/// Represents a heap-based, array-backed output sink into which binary data can be written.
|
||||
/// </summary>
|
||||
internal ref struct ArrayPoolBinaryWriter
|
||||
{
|
||||
/// <summary>
|
||||
/// The default buffer size to use to expand empty arrays.
|
||||
/// </summary>
|
||||
public const int DefaultInitialBufferSize = 256;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a heap-based, array-backed output sink into which data can be written.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of values to write.</typeparam>
|
||||
internal ref struct ArrayPoolBufferWriter<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
/// <summary>
|
||||
/// The underlying <typeparamref name="T"/> array.
|
||||
/// The underlying <see cref="byte"/> array.
|
||||
/// </summary>
|
||||
private T[]? array;
|
||||
private byte[]? array;
|
||||
|
||||
/// <summary>
|
||||
/// The starting offset within <see cref="array"/>.
|
||||
@@ -38,11 +33,11 @@ internal ref struct ArrayPoolBufferWriter<T>
|
||||
private int index;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ArrayPoolBufferWriter{T}"/> class.
|
||||
/// Initializes a new instance of the <see cref="ArrayPoolBinaryWriter"/> type.
|
||||
/// </summary>
|
||||
public ArrayPoolBufferWriter(int capacity)
|
||||
public ArrayPoolBinaryWriter(int capacity)
|
||||
{
|
||||
this.array = ArrayPool<T>.Shared.Rent(capacity);
|
||||
this.array = ArrayPool<byte>.Shared.Rent(capacity);
|
||||
this.index = 0;
|
||||
}
|
||||
|
||||
@@ -50,12 +45,12 @@ internal ref struct ArrayPoolBufferWriter<T>
|
||||
/// Gets the data written to the underlying buffer so far, as a <see cref="ReadOnlySpan{T}"/>.
|
||||
/// </summary>
|
||||
[UnscopedRef]
|
||||
public readonly ReadOnlySpan<T> WrittenSpan
|
||||
public readonly ReadOnlySpan<byte> 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<T>
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the raw value of type <typeparamref name="T"/> into the buffer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to write.</typeparam>
|
||||
/// <param name="value">The value to write.</param>
|
||||
/// <remarks>This method will just blit the data of <paramref name="value"/> into the target buffer.</remarks>
|
||||
public unsafe void Write<T>(scoped in T value)
|
||||
{
|
||||
Span<byte> span = GetSpan(sizeof(T));
|
||||
|
||||
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(span), value);
|
||||
|
||||
Advance(sizeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the raw data from the input <see cref="ReadOnlySpan{T}"/> into the buffer.
|
||||
/// </summary>
|
||||
/// <param name="data">The data to write.</param>
|
||||
public void Write(scoped ReadOnlySpan<byte> data)
|
||||
{
|
||||
Span<byte> span = GetSpan(data.Length);
|
||||
|
||||
data.CopyTo(span);
|
||||
|
||||
Advance(data.Length);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
byte[]? array = this.array;
|
||||
|
||||
if (array is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.array = null;
|
||||
|
||||
ArrayPool<byte>.Shared.Return(array);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Advances the current writer.
|
||||
/// </summary>
|
||||
/// <param name="count">The amount to advance.</param>
|
||||
/// <remarks>Must be called after <see cref="GetSpan"/>.</remarks>
|
||||
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<T>
|
||||
/// <param name="sizeHint">The capacity to request.</param>
|
||||
/// <returns>A <see cref="Span{T}"/> to write data to.</returns>
|
||||
[UnscopedRef]
|
||||
internal Span<T> GetSpan(int sizeHint = 0)
|
||||
private Span<byte> GetSpan(int sizeHint = 0)
|
||||
{
|
||||
CheckBufferAndEnsureCapacity(sizeHint);
|
||||
|
||||
@@ -99,7 +136,7 @@ internal ref struct ArrayPoolBufferWriter<T>
|
||||
[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<T>
|
||||
minimumSize = BitOperations.RoundUpToPowerOf2(minimumSize);
|
||||
}
|
||||
|
||||
T[] newArray = ArrayPool<T>.Shared.Rent((int)minimumSize);
|
||||
byte[] newArray = ArrayPool<byte>.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<T>.Shared.Return(this.array!);
|
||||
ArrayPool<byte>.Shared.Return(this.array!);
|
||||
|
||||
this.array = newArray;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
T[]? array = this.array;
|
||||
|
||||
if (array is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this.array = null;
|
||||
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Helpers for the <see cref="ArrayPoolBufferWriter{T}"/> type.
|
||||
/// </summary>
|
||||
internal static class ArrayPoolBinaryWriterExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Writes the raw value of type <typeparamref name="T"/> into the buffer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to write.</typeparam>
|
||||
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance to write data to.</param>
|
||||
/// <param name="value">The value to write.</param>
|
||||
/// <remarks>This method will just blit the data of <paramref name="value"/> into the target buffer.</remarks>
|
||||
public static unsafe void WriteRaw<T>(this ref ArrayPoolBufferWriter<byte> writer, scoped in T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
Span<byte> span = writer.GetSpan(sizeof(T));
|
||||
|
||||
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(span), value);
|
||||
|
||||
writer.Advance(sizeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the raw value of type <typeparamref name="T"/> into the buffer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of value to write.</typeparam>
|
||||
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance to write data to.</param>
|
||||
/// <param name="value">The value to write.</param>
|
||||
public static unsafe void WriteRaw<T>(this ref ArrayPoolBufferWriter<T> writer, scoped in T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
Span<T> span = writer.GetSpan(1);
|
||||
|
||||
MemoryMarshal.GetReference(span) = value;
|
||||
|
||||
writer.Advance(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the raw data from the input <see cref="ReadOnlySpan{T}"/> into the buffer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of values in the target writer.</typeparam>
|
||||
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance to write data to.</param>
|
||||
/// <param name="data">The data to write.</param>
|
||||
public static void WriteRaw<T>(this ref ArrayPoolBufferWriter<T> writer, scoped ReadOnlySpan<T> data)
|
||||
where T : unmanaged
|
||||
{
|
||||
Span<T> span = writer.GetSpan(data.Length);
|
||||
|
||||
data.CopyTo(span);
|
||||
|
||||
writer.Advance(data.Length);
|
||||
}
|
||||
|
||||
#if !NET6_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// Writes the raw data from the input <see cref="ReadOnlySpan{T}"/> into the buffer.
|
||||
/// </summary>
|
||||
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance to write data to.</param>
|
||||
/// <param name="data">The data to write.</param>
|
||||
public static void WriteRaw(this ref ArrayPoolBufferWriter<char> writer, string data)
|
||||
{
|
||||
Span<char> span = writer.GetSpan(data.Length);
|
||||
|
||||
data.AsSpan().CopyTo(span);
|
||||
|
||||
writer.Advance(data.Length);
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Writes an <see langword="int"/> value as UTF8 bytes into the buffer.
|
||||
/// </summary>
|
||||
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance to write data to.</param>
|
||||
/// <param name="value">The value to write.</param>
|
||||
public static void WriteAsUtf8(this ref ArrayPoolBufferWriter<byte> 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<byte> 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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes text as UTF8 bytes into the buffer.
|
||||
/// </summary>
|
||||
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance to write data to.</param>
|
||||
/// <param name="text">The text to write.</param>
|
||||
public static void WriteAsUtf8(this ref ArrayPoolBufferWriter<byte> writer, string text)
|
||||
{
|
||||
int maximumByteSize = Encoding.UTF8.GetMaxByteCount(text.Length);
|
||||
|
||||
Span<byte> span = writer.GetSpan(maximumByteSize);
|
||||
|
||||
int writtenBytes = Encoding.UTF8.GetBytes(text.AsSpan(), span);
|
||||
|
||||
writer.Advance(writtenBytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes an <see langword="int"/> value as Unicode bytes into the buffer.
|
||||
/// </summary>
|
||||
/// <param name="writer">The target <see cref="ArrayPoolBufferWriter{T}"/> instance to write data to.</param>
|
||||
/// <param name="value">The value to write.</param>
|
||||
public static void WriteAsUnicode(this ref ArrayPoolBufferWriter<char> 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<char> 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
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Interop.Buffers;
|
||||
|
||||
/// <summary>
|
||||
/// A custom <see cref="MemoryManager{T}"/> that wraps a pinned memory buffer.
|
||||
/// </summary>
|
||||
internal sealed unsafe class PinnedBufferMemoryManager : MemoryManager<byte>
|
||||
{
|
||||
/// <summary>
|
||||
/// A pointer to the buffer.
|
||||
/// </summary>
|
||||
private readonly byte* buffer;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the buffer.
|
||||
/// </summary>
|
||||
private readonly int length;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PinnedBufferMemoryManager"/> class with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="span">A <see cref="ReadOnlySpan{T}"/> wrapping the input pinned buffer.</param>
|
||||
public PinnedBufferMemoryManager(ReadOnlySpan<byte> span)
|
||||
{
|
||||
this.buffer = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span));
|
||||
this.length = span.Length;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Span<byte> GetSpan()
|
||||
{
|
||||
return new(this.buffer, this.length);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override unsafe MemoryHandle Pin(int elementIndex = 0)
|
||||
{
|
||||
default(ArgumentOutOfRangeException).ThrowIfNotInRange(elementIndex, 0, this.length);
|
||||
|
||||
return new(this.buffer + elementIndex);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Unpin()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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<char> writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize);
|
||||
using D2D1EffectXmlFactory.EffectXml effectXml = D2D1EffectXmlFactory.GetXmlBuffer<T>();
|
||||
|
||||
// Build the XML text
|
||||
writer.WriteRaw("""
|
||||
<?xml version='1.0'?>
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectDisplayName<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Description' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectDescription<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Category' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectCategory<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Author' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(GetEffectAuthor<T>().AsSpan());
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Inputs>
|
||||
|
||||
""");
|
||||
|
||||
// Add the input nodes
|
||||
for (int i = 0; i < PixelShaderEffect.For<T>.Instance.InputCount; i++)
|
||||
{
|
||||
writer.WriteRaw(" <Input name='Source");
|
||||
writer.WriteAsUnicode(i);
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
// Write the last part of the XML (including the buffer property)
|
||||
writer.WriteRaw("""
|
||||
</Inputs>
|
||||
<Property name='ConstantBuffer' type='blob'>
|
||||
<Property name='DisplayName' type='string' value='ConstantBuffer'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager0' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager0'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager1' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager1'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager2' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager2'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager3' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager3'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager4' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager4'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager5' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager5'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager6' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager6'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager7' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager7'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager8' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager8'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager9' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager9'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager10' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager10'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager11' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager11'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager12' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager12'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager13' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager13'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager14' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager14'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager15' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager15'/>
|
||||
</Property>
|
||||
<Property name='TransformMapper' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='TransformMapper'/>
|
||||
</Property>
|
||||
</Effect>
|
||||
""");
|
||||
|
||||
// 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<T>.Instance.Id;
|
||||
}
|
||||
|
||||
writer.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -346,194 +244,111 @@ public static unsafe class D2D1PixelShaderEffect
|
||||
public static ReadOnlyMemory<byte> GetRegistrationBlob<T>(out Guid effectId)
|
||||
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
|
||||
{
|
||||
ArrayPoolBufferWriter<byte> 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<T>.Instance.Id);
|
||||
writer.WriteRaw(PixelShaderEffect.For<T>.Instance.InputCount);
|
||||
writer.Write(PixelShaderEffect.For<T>.Instance.Id);
|
||||
writer.Write(PixelShaderEffect.For<T>.Instance.InputCount);
|
||||
|
||||
// Build the XML text
|
||||
writer.WriteRaw("""
|
||||
<?xml version='1.0'?>
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectDisplayName<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Description' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectDescription<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Category' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectCategory<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Author' type='string' value='
|
||||
"""u8);
|
||||
writer.WriteAsUtf8(GetEffectAuthor<T>() ?? "");
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Inputs>
|
||||
|
||||
"""u8);
|
||||
|
||||
// Add the input nodes
|
||||
for (int i = 0; i < PixelShaderEffect.For<T>.Instance.InputCount; i++)
|
||||
// Retrieve the effect XML and include it into the registration blob
|
||||
using (D2D1EffectXmlFactory.EffectXml effectXml = D2D1EffectXmlFactory.GetXmlBuffer<T>())
|
||||
{
|
||||
writer.WriteRaw(" <Input name='Source"u8);
|
||||
writer.WriteAsUtf8(i);
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
ReadOnlySpan<char> registrationText = effectXml.GetBuffer();
|
||||
int maxRegistrationTextByteCount = Encoding.UTF8.GetMaxByteCount(registrationText.Length);
|
||||
byte[] registrationTextUtf8 = ArrayPool<byte>.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<byte>.Shared.Return(registrationTextUtf8);
|
||||
}
|
||||
|
||||
// Write the last part of the XML (including the buffer property)
|
||||
writer.WriteRaw("""
|
||||
</Inputs>
|
||||
<Property name='ConstantBuffer' type='blob'>
|
||||
<Property name='DisplayName' type='string' value='ConstantBuffer'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager0' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager0'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager1' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager1'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager2' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager2'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager3' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager3'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager4' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager4'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager5' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager5'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager6' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager6'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager7' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager7'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager8' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager8'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager9' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager9'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager10' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager10'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager11' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager11'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager12' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager12'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager13' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager13'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager14' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager14'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager15' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager15'/>
|
||||
</Property>
|
||||
<Property name='TransformMapper' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='TransformMapper'/>
|
||||
</Property>
|
||||
</Effect>
|
||||
"""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<T>.Instance.Factory);
|
||||
writer.Write((nint)PixelShaderEffect.For<T>.Instance.Factory);
|
||||
|
||||
byte[] registrationBlob = writer.WrittenSpan.ToArray();
|
||||
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Text;
|
||||
using ComputeSharp.D2D1.Descriptors;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// A factory type to produce effect registration XML blobs.
|
||||
/// </summary>
|
||||
internal static unsafe class D2D1EffectXmlFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// The shared <see cref="StringBuilder"/> instance to use to build effect registration XML blobs.
|
||||
/// </summary>
|
||||
private static readonly StringBuilder XmlBuilder = new(4096);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="EffectXml"/> instance with the effect registration XML blob for a given shader type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader type to build the XML for.</typeparam>
|
||||
/// <returns>The <see cref="EffectXml"/> instance with the effect registration XML blob.</returns>
|
||||
public static EffectXml GetXmlBuffer<T>()
|
||||
where T : unmanaged, ID2D1PixelShader, ID2D1PixelShaderDescriptor<T>
|
||||
{
|
||||
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($"""
|
||||
<?xml version='1.0'?>
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='{D2D1PixelShaderEffect.GetEffectDisplayName<T>()}'/>
|
||||
<Property name='Description' type='string' value='{D2D1PixelShaderEffect.GetEffectDescription<T>()}'/>
|
||||
<Property name='Category' type='string' value='{D2D1PixelShaderEffect.GetEffectCategory<T>()}'/>
|
||||
<Property name='Author' type='string' value='{D2D1PixelShaderEffect.GetEffectAuthor<T>()}'/>
|
||||
<Inputs>
|
||||
""");
|
||||
|
||||
// Add the input nodes, if any
|
||||
for (int i = 0; i < D2D1PixelShader.GetInputCount<T>(); i++)
|
||||
{
|
||||
_ = builder.Append('\n');
|
||||
_ = builder.Append($" <Input name='Source{i}'/>");
|
||||
}
|
||||
|
||||
// Close the inputs tag and add all effect properties
|
||||
_ = builder.Append('\n');
|
||||
_ = builder.Append("""
|
||||
</Inputs>
|
||||
<Property name='ConstantBuffer' type='blob'>
|
||||
<Property name='DisplayName' type='string' value='ConstantBuffer'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager0' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager0'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager1' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager1'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager2' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager2'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager3' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager3'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager4' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager4'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager5' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager5'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager6' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager6'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager7' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager7'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager8' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager8'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager9' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager9'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager10' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager10'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager11' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager11'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager12' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager12'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager13' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager13'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager14' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager14'/>
|
||||
</Property>
|
||||
<Property name='ResourceTextureManager15' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='ResourceTextureManager15'/>
|
||||
</Property>
|
||||
<Property name='TransformMapper' type='iunknown'>
|
||||
<Property name='DisplayName' type='string' value='TransformMapper'/>
|
||||
</Property>
|
||||
</Effect>
|
||||
""");
|
||||
|
||||
// Null terminator for the text
|
||||
_ = builder.Append('\0');
|
||||
|
||||
return new(builder);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A simple buffer for an effect XML blob.
|
||||
/// </summary>
|
||||
public readonly ref struct EffectXml
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="char"/> buffer with the effect XML blob.
|
||||
/// </summary>
|
||||
private readonly char[] buffer;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the effect XML blob.
|
||||
/// </summary>
|
||||
private readonly int length;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="EffectXml"/> value with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="StringBuilder"/> instance with the effect XML.</param>
|
||||
[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<char>.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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="ReadOnlySpan{T}"/> for the effect XML blob.
|
||||
/// </summary>
|
||||
/// <returns>The <see cref="ReadOnlySpan{T}"/> value with the effect XML blob.</returns>
|
||||
public ReadOnlySpan<char> GetBuffer()
|
||||
{
|
||||
return this.buffer.AsSpan(0, this.length);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IDisposable.Dispose"/>
|
||||
public void Dispose()
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(this.buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする