Optimized ToString() overloads for HLSL primitive types

このコミットが含まれているのは:
Sergio Pedri committed 2021-10-30 15:11:44 +02:00
1 親 6fe06ea4b2
コミット 2a7163ba92
18ファイル変更
+550 -277

検索条件に一致するファイルはありません

+297
ファイルの表示
@@ -0,0 +1,297 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Globalization;
using System.Runtime.InteropServices;
namespace System.Runtime.CompilerServices;
/// <summary>
/// Provides a handler used by the language compiler to process interpolated strings into <see cref="string"/> instances.
/// </summary>
[InterpolatedStringHandler]
public ref struct FormatInterpolatedStringHandler
{
/// <summary>
/// Minimum size array to rent from the pool.
/// </summary>
private const int MinimumArrayPoolLength = 256;
/// <summary>
/// The format to use to print individual values to the target buffer.
/// </summary>
private readonly string? format;
/// <summary>
/// Optional provider to pass to <see cref="IFormattable.ToString(string?, IFormatProvider?)"/> or <see cref="ISpanFormattable.TryFormat(Span{char}, out int, ReadOnlySpan{char}, IFormatProvider?)"/> calls.
/// </summary>
private readonly IFormatProvider? formatProvider;
/// <summary>
/// Array rented from the array pool and used to back <see cref="characters"/>.
/// </summary>
private char[]? arrayToReturnToPool;
/// <summary>
/// The span to write into.
/// </summary>
private Span<char> characters;
/// <summary>
/// Position at which to write the next character.
/// </summary>
private int position;
/// <summary>
/// Whether <see cref="formatProvider"/> provides an <see cref="ICustomFormatter"/>.
/// </summary>
private readonly bool hasCustomFormatter;
/// <summary>
/// Creates a handler used to translate an interpolated string into a <see cref="string"/>.
/// </summary>
/// <param name="literalLength">The number of constant characters outside of interpolation expressions in the interpolated string.</param>
/// <param name="formattedCount">The number of interpolation expressions in the interpolated string.</param>
/// <param name="format">The format to use, if available.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <param name="initialBuffer">A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten.</param>
/// <remarks>This is intended to be called only by compiler-generated code. Arguments are not validated as they'd otherwise be for members intended to be used directly.</remarks>
public FormatInterpolatedStringHandler(int literalLength, int formattedCount, string? format, IFormatProvider? formatProvider, Span<char> initialBuffer)
{
this.format = format;
this.formatProvider = formatProvider;
characters = initialBuffer;
arrayToReturnToPool = null;
position = 0;
hasCustomFormatter = formatProvider is not null && HasCustomFormatter(formatProvider);
}
/// <summary>
/// Creates a new <see cref="string"/> with the specified arguments.
/// </summary>
/// <param name="format">The format to use, if available.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <param name="initialBuffer">A buffer temporarily transferred to the handler for use as part of its formatting. Contents may be overwritten.</param>
/// <param name="handler">The input <see cref="FormatInterpolatedStringHandler"/> instance responsible for formatting the resulting <see cref="string"/>.</param>
/// <returns>The resulting formatted <see cref="string"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Create(
string? format,
IFormatProvider? formatProvider,
Span<char> initialBuffer,
[InterpolatedStringHandlerArgument("format", "formatProvider", "initialBuffer")] ref FormatInterpolatedStringHandler handler)
{
return handler.ToStringAndClear();
}
/// <summary>Gets the built <see cref="string"/> and clears the handler.</summary>
/// <returns>The built string.</returns>
/// <remarks>
/// This releases any resources used by the handler. The method should be invoked only
/// once and as the last thing performed on the handler. Subsequent use is erroneous, ill-defined,
/// and may destabilize the process, as may using any other copies of the handler after ToStringAndClear
/// is called on any one of them.
/// </remarks>
public string ToStringAndClear()
{
string result = new(characters.Slice(0, position));
Clear();
return result;
}
/// <summary>
/// Clears the handler, returning any rented array to the pool.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Clear()
{
char[]? toReturn = arrayToReturnToPool;
this = default;
if (toReturn is not null)
{
ArrayPool<char>.Shared.Return(toReturn);
}
}
/// <summary>
/// Writes the specified string to the handler.
/// </summary>
/// <param name="value">The string to write.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLiteral(string value)
{
if (value.Length == 1)
{
Span<char> chars = characters;
int pos = position;
if ((uint)pos < (uint)chars.Length)
{
chars[pos] = value[0];
position = pos + 1;
}
else
{
GrowThenCopyString(value);
}
return;
}
if (value.Length == 2)
{
Span<char> chars = characters;
int pos = position;
if ((uint)pos < chars.Length - 1)
{
Unsafe.WriteUnaligned(
ref Unsafe.As<char, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(chars), pos)),
Unsafe.ReadUnaligned<int>(ref Unsafe.As<char, byte>(ref Unsafe.AsRef(in value.GetPinnableReference()))));
position = pos + 2;
}
else
{
GrowThenCopyString(value);
}
return;
}
AppendStringDirect(value);
}
/// <summary>
/// Writes the specified string to the handler.
/// </summary>
/// <param name="value">The string to write.</param>
private void AppendStringDirect(string value)
{
if (value.TryCopyTo(characters.Slice(position)))
{
position += value.Length;
}
else
{
GrowThenCopyString(value);
}
}
/// <summary>
/// Writes the specified value to the handler.
/// </summary>
/// <param name="value">The value to write.</param>
public void AppendFormatted(string value)
{
AppendLiteral(value);
}
/// <summary>
/// Writes the specified value to the handler.
/// </summary>
/// <param name="value">The value to write.</param>
public void AppendFormatted<T>(T value)
where T : ISpanFormattable
{
if (hasCustomFormatter)
{
AppendCustomFormatter(value, format);
return;
}
int charsWritten;
while (!value.TryFormat(characters.Slice(position), out charsWritten, format, formatProvider))
{
Grow();
}
position += charsWritten;
}
/// <summary>
/// Gets whether the provider provides a custom formatter.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool HasCustomFormatter(IFormatProvider provider)
{
return
provider.GetType() != typeof(CultureInfo) &&
provider.GetFormat(typeof(ICustomFormatter)) != null;
}
/// <summary>
/// Formats the value using the custom formatter from the provider.
/// </summary>
/// <param name="value">The value to write.</param>
/// <param name="format">The format string.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private void AppendCustomFormatter<T>(T value, string? format)
where T : ISpanFormattable
{
ICustomFormatter? formatter = (ICustomFormatter?)formatProvider?.GetFormat(typeof(ICustomFormatter));
if (formatter is not null && formatter.Format(format, value, formatProvider) is string customFormatted)
{
AppendStringDirect(customFormatted);
}
}
/// <summary>
/// Fallback for fast path in <see cref="AppendStringDirect"/> when there's not enough space in the destination.
/// </summary>
/// <param name="value">The string to write.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private void GrowThenCopyString(string value)
{
Grow(value.Length);
value.CopyTo(characters.Slice(position));
position += value.Length;
}
/// <summary>
/// Grows <see cref="characters"/> to have the capacity to store at least <paramref name="additionalChars"/> beyond <see cref="position"/>.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private void Grow(int additionalChars)
{
GrowCore((uint)position + (uint)additionalChars);
}
/// <summary>
/// Grows the size of <see cref="characters"/>.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private void Grow()
{
GrowCore((uint)characters.Length + 1);
}
/// <summary>
/// Grow the size of <see cref="characters"/> to at least the specified <paramref name="requiredMinCapacity"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void GrowCore(uint requiredMinCapacity)
{
uint newCapacity = Math.Max(requiredMinCapacity, Math.Min((uint)characters.Length * 2, 0x3FFFFFDF));
int arraySize = (int)Math.Clamp(newCapacity, MinimumArrayPoolLength, int.MaxValue);
char[] newArray = ArrayPool<char>.Shared.Rent(arraySize);
characters.Slice(0, position).CopyTo(newArray);
char[]? toReturn = arrayToReturnToPool;
characters = arrayToReturnToPool = newArray;
if (toReturn is not null)
{
ArrayPool<char>.Shared.Return(toReturn);
}
}
}
+18 -1
ファイルの表示
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System;
using System.Runtime.InteropServices;
namespace ComputeSharp;
@@ -41,6 +42,22 @@ public readonly struct Bool
return Value.ToString();
}
/// <inheritdoc cref="bool.ToString(IFormatProvider?)"/>
public string ToString(IFormatProvider? formatProvider)
{
return Value.ToString();
}
#if !SOURCE_GENERATOR
/// <inheritdoc cref="bool.TryFormat(Span{char}, out int)"/>
public bool TryFormat(Span<char> destination, out int charsWritten)
{
return Value.TryFormat(destination, out charsWritten);
}
#endif
/// <summary>
/// Checks whether or not two <see cref="Bool"/> instances represent the same <see cref="bool"/> value.
/// </summary>
+5 -10
ファイルの表示
@@ -1,6 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -388,20 +387,16 @@ public unsafe partial struct Bool2
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Bool4 GGGG => ref *(Bool4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
StringBuilder sb = new();
sb.Append('<');
sb.Append(this.x);
sb.Append(", ");
sb.Append(this.y);
sb.Append('>');
return sb.ToString();
return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}>");
}
#endif
/// <summary>
/// Negates a <see cref="Bool2"/> value.
/// </summary>
+5 -12
ファイルの表示
@@ -1,6 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -1469,22 +1468,16 @@ public unsafe partial struct Bool3
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Bool4 BBBB => ref *(Bool4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
StringBuilder sb = new();
sb.Append('<');
sb.Append(this.x);
sb.Append(", ");
sb.Append(this.y);
sb.Append(", ");
sb.Append(this.z);
sb.Append('>');
return sb.ToString();
return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}, {this.z}>");
}
#endif
/// <summary>
/// Negates a <see cref="Bool3"/> value.
/// </summary>
+5 -14
ファイルの表示
@@ -1,6 +1,5 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -4110,24 +4109,16 @@ public unsafe partial struct Bool4
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Bool4 AAAA => ref *(Bool4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
StringBuilder sb = new();
sb.Append('<');
sb.Append(this.x);
sb.Append(", ");
sb.Append(this.y);
sb.Append(", ");
sb.Append(this.z);
sb.Append(", ");
sb.Append(this.w);
sb.Append('>');
return sb.ToString();
return string.Create(null, stackalloc char[32], $"<{this.x}, {this.y}, {this.z}, {this.w}>");
}
#endif
/// <summary>
/// Negates a <see cref="Bool4"/> value.
/// </summary>
+16 -13
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Double2"/>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 8)]
public unsafe partial struct Double2 : IFormattable
public unsafe partial struct Double2
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -390,29 +392,30 @@ public unsafe partial struct Double2 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Double4 GGGG => ref *(Double4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}>");
}
#endif
/// <summary>
/// Negates a <see cref="Double2"/> value.
/// </summary>
+16 -16
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Double3"/>
[StructLayout(LayoutKind.Explicit, Size = 24, Pack = 8)]
public unsafe partial struct Double3 : IFormattable
public unsafe partial struct Double3
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -1471,32 +1473,30 @@ public unsafe partial struct Double3 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Double4 BBBB => ref *(Double4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
#endif
/// <summary>
/// Negates a <see cref="Double3"/> value.
/// </summary>
+16 -19
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Double4"/>
[StructLayout(LayoutKind.Explicit, Size = 32, Pack = 8)]
public unsafe partial struct Double4 : IFormattable
public unsafe partial struct Double4
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -4112,35 +4114,30 @@ public unsafe partial struct Double4 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Double4 AAAA => ref *(Double4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.w.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
#endif
/// <summary>
/// Negates a <see cref="Double4"/> value.
/// </summary>
+16 -13
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Float2"/>
[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]
public unsafe partial struct Float2 : IFormattable
public unsafe partial struct Float2
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -390,29 +392,30 @@ public unsafe partial struct Float2 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Float4 GGGG => ref *(Float4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}>");
}
#endif
/// <summary>
/// Negates a <see cref="Float2"/> value.
/// </summary>
+16 -16
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Float3"/>
[StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
public unsafe partial struct Float3 : IFormattable
public unsafe partial struct Float3
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -1471,32 +1473,30 @@ public unsafe partial struct Float3 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Float4 BBBB => ref *(Float4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
#endif
/// <summary>
/// Negates a <see cref="Float3"/> value.
/// </summary>
+16 -19
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Float4"/>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)]
public unsafe partial struct Float4 : IFormattable
public unsafe partial struct Float4
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -4112,35 +4114,30 @@ public unsafe partial struct Float4 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Float4 AAAA => ref *(Float4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.w.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
#endif
/// <summary>
/// Negates a <see cref="Float4"/> value.
/// </summary>
+16 -13
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Int2"/>
[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]
public unsafe partial struct Int2 : IFormattable
public unsafe partial struct Int2
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -390,29 +392,30 @@ public unsafe partial struct Int2 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Int4 GGGG => ref *(Int4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}>");
}
#endif
/// <summary>
/// Negates a <see cref="Int2"/> value.
/// </summary>
+16 -16
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Int3"/>
[StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
public unsafe partial struct Int3 : IFormattable
public unsafe partial struct Int3
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -1471,32 +1473,30 @@ public unsafe partial struct Int3 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Int4 BBBB => ref *(Int4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
#endif
/// <summary>
/// Negates a <see cref="Int3"/> value.
/// </summary>
+16 -19
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="Int4"/>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)]
public unsafe partial struct Int4 : IFormattable
public unsafe partial struct Int4
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -4112,35 +4114,30 @@ public unsafe partial struct Int4 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly Int4 AAAA => ref *(Int4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.w.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
#endif
/// <summary>
/// Negates a <see cref="Int4"/> value.
/// </summary>
+16 -13
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="UInt2"/>
[StructLayout(LayoutKind.Explicit, Size = 8, Pack = 4)]
public unsafe partial struct UInt2 : IFormattable
public unsafe partial struct UInt2
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -390,29 +392,30 @@ public unsafe partial struct UInt2 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly UInt4 GGGG => ref *(UInt4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}>");
}
#endif
/// <summary>
/// Sums two <see cref="UInt2"/> values.
/// </summary>
+16 -16
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="UInt3"/>
[StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
public unsafe partial struct UInt3 : IFormattable
public unsafe partial struct UInt3
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -1471,32 +1473,30 @@ public unsafe partial struct UInt3 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly UInt4 BBBB => ref *(UInt4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}>");
}
#endif
/// <summary>
/// Sums two <see cref="UInt3"/> values.
/// </summary>
+16 -19
ファイルの表示
@@ -2,7 +2,6 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -14,7 +13,10 @@ namespace ComputeSharp;
/// <inheritdoc cref="UInt4"/>
[StructLayout(LayoutKind.Explicit, Size = 16, Pack = 4)]
public unsafe partial struct UInt4 : IFormattable
public unsafe partial struct UInt4
#if !SOURCE_GENERATOR
: IFormattable
#endif
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -4112,35 +4114,30 @@ public unsafe partial struct UInt4 : IFormattable
/// <remarks>This method is an intrinsic and can only be used within a shader on the GPU. Using it on the CPU is undefined behavior.</remarks>
public readonly ref readonly UInt4 AAAA => ref *(UInt4*)UndefinedData;
#if !SOURCE_GENERATOR
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
sb.Append(this.x.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.y.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.z.ToString(format, formatProvider));
sb.Append(separator);
sb.Append(' ');
sb.Append(this.w.ToString(format, formatProvider));
sb.Append('>');
return sb.ToString();
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<{this.x}{separator} {this.y}{separator} {this.z}{separator} {this.w}>");
}
#endif
/// <summary>
/// Sums two <see cref="UInt4"/> values.
/// </summary>
+28 -48
ファイルの表示
@@ -50,7 +50,7 @@ void GenerateVectorProperties(string typeName, int elementSize)
string elementTypeName = Regex.Match(typeName, @"^[A-Za-z]+").Value;
string hlslElementTypeName = elementTypeName.ToLower();
int i = int.Parse(Regex.Match(typeName, @"\d+$").Value);
string formattable = elementTypeName == "Bool" ? "" : " : IFormattable";
string formattable = elementTypeName == "Bool" ? "" : "IFormattable";
if (elementTypeName != "Bool")
{
@@ -60,7 +60,6 @@ void GenerateVectorProperties(string typeName, int elementSize)
#>
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if !NET6_0
using RuntimeHelpers = ComputeSharp.SourceGenerators.Helpers.RuntimeHelpers;
using MemoryMarshal = ComputeSharp.SourceGenerators.Helpers.MemoryMarshal;
@@ -72,7 +71,17 @@ namespace ComputeSharp;
/// <inheritdoc cref="<#=typeName#>"/>
[StructLayout(LayoutKind.Explicit, Size = <#=(elementSize * i)#>, Pack = <#=elementSize#>)]
public unsafe partial struct <#=typeName#><#=formattable#>
public unsafe partial struct <#=typeName#>
<#
if (formattable != "")
{
#>
#if !SOURCE_GENERATOR
: <#=formattable#>
#endif
<#
}
#>
{
/// <summary>
/// A private buffer to which the undefined properties will point to.
@@ -147,35 +156,18 @@ public unsafe partial struct <#=typeName#><#=formattable#>
PopIndent();
WriteLine("");
#>
#if !SOURCE_GENERATOR
<#
if (elementTypeName == "Bool")
{
#>
/// <inheritdoc/>
public override readonly string ToString()
{
StringBuilder sb = new();
sb.Append('<');
<#
PushIndent(" ");
PushIndent(" ");
for (int j = 0; j < i; j++)
{
WriteLine($"sb.Append(this.{"xyzw"[j]});");
if (j < i - 1)
{
WriteLine("sb.Append(\", \");");
}
}
PopIndent();
PopIndent();
#>
sb.Append('>');
return sb.ToString();
return string.Create(null, stackalloc char[32], $"<<#=string.Join(", ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>");
}
<#
}
@@ -185,41 +177,29 @@ public unsafe partial struct <#=typeName#><#=formattable#>
/// <inheritdoc/>
public override readonly string ToString()
{
return ToString("G", CultureInfo.CurrentCulture);
string separator = NumberFormatInfo.CurrentInfo.NumberGroupSeparator;
return string.Create(null, stackalloc char[64], $"<<#=string.Join("{separator} ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>");
}
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
StringBuilder sb = new();
string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
sb.Append('<');
return FormatInterpolatedStringHandler.Create(
format,
formatProvider,
stackalloc char[64],
$"<<#=string.Join("{separator} ", "xyzw".Take(i).Select(c => $"{{this.{c}}}"))#>>");
}
<#
PushIndent(" ");
PushIndent(" ");
for (int j = 0; j < i; j++)
{
WriteLine($"sb.Append(this.{"xyzw"[j]}.ToString(format, formatProvider));");
if (j < i - 1)
{
WriteLine("sb.Append(separator);");
WriteLine("sb.Append(' ');");
}
}
PopIndent();
PopIndent();
#>
sb.Append('>');
return sb.ToString();
}
#endif
<#
}
// Generate the negation operator
if (elementTypeName != "UInt")