Make D2D1PixelShaderEffect.RegisterForD2D1Factory1 formatting zero-alloc
このコミットが含まれているのは:
1 親
90c32d4fd3
コミット
876d194078
3ファイル変更
+142
-74
検索条件に一致するファイルはありません
@@ -33,20 +33,54 @@ internal static class ArrayPoolBinaryWriterExtensions
|
||||
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 in 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(this in ArrayPoolBufferWriter<byte> writer, scoped ReadOnlySpan<byte> data)
|
||||
public static void WriteRaw<T>(this in ArrayPoolBufferWriter<T> writer, scoped ReadOnlySpan<T> data)
|
||||
where T : unmanaged
|
||||
{
|
||||
Span<byte> span = writer.GetSpan(data.Length);
|
||||
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 in 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>
|
||||
@@ -82,4 +116,24 @@ internal static class ArrayPoolBinaryWriterExtensions
|
||||
|
||||
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 in 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
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,7 @@ internal ref struct ArrayPoolBufferWriter<T>
|
||||
/// </summary>
|
||||
/// <param name="sizeHint">The minimum number of items to ensure space for in <see cref="array"/>.</param>
|
||||
[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>
|
||||
|
||||
T[] newArray = ArrayPool<T>.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<T>.Shared.Return(this.array!);
|
||||
|
||||
|
||||
@@ -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<T>.Initialize(mapperFactory);
|
||||
|
||||
// Setup the input string
|
||||
StringBuilder effectInputsBuilder = new();
|
||||
using ArrayPoolBufferWriter<char> writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize);
|
||||
|
||||
// Build the XML text
|
||||
writer.WriteRaw("""
|
||||
<?xml version='1.0'?>
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='
|
||||
""");
|
||||
writer.WriteRaw(typeof(T).FullName!);
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
<Property name='Author' type='string' value='ComputeSharp.D2D1'/>
|
||||
<Property name='Category' type='string' value='Stylize'/>
|
||||
<Property name='Description' type='string' value='A custom D2D1 effect using a pixel shader'/>
|
||||
<Inputs>
|
||||
|
||||
""");
|
||||
|
||||
// Add the input nodes
|
||||
for (int i = 0; i < PixelShaderEffect.For<T>.InputCount; i++)
|
||||
{
|
||||
effectInputsBuilder.Append("<Input name='Source");
|
||||
effectInputsBuilder.Append(i);
|
||||
effectInputsBuilder.Append("'/>");
|
||||
writer.WriteRaw(" <Input name='Source");
|
||||
writer.WriteAsUnicode(i);
|
||||
writer.WriteRaw("""
|
||||
'/>
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
// Prepare the XML with a variable number of inputs
|
||||
string xml = @$"<?xml version='1.0'?>
|
||||
<Effect>
|
||||
<Property name='DisplayName' type='string' value='{typeof(T).FullName}'/>
|
||||
<Property name='Author' type='string' value='ComputeSharp.D2D1'/>
|
||||
<Property name='Category' type='string' value='Stylize'/>
|
||||
<Property name='Description' type='string' value='A custom D2D1 effect using a pixel shader'/>
|
||||
<Inputs>
|
||||
{effectInputsBuilder}
|
||||
</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>
|
||||
</Effect>";
|
||||
// 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>
|
||||
</Effect>
|
||||
""");
|
||||
|
||||
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")
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする