Remove unnecessary ArrayPoolStringBuilder type

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-13 12:11:55 +02:00
1 親 8e0aa437bc
コミット d797f14771
4ファイル変更
+13 -242

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

+3 -5
ファイルの表示
@@ -104,11 +104,9 @@ public static class ReflectionServices
private static unsafe ShaderInfo GetNonGenericShaderInfo<T>(in T shader)
where T : struct, IShader
{
Unsafe.AsRef(in shader).BuildHlslSource(out ArrayPoolStringBuilder shaderSource, 1, 1, 1);
Unsafe.AsRef(in shader).BuildHlslSource(out string hlslSource);
using ComPtr<IDxcBlob> dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.Compile(shaderSource.WrittenSpan);
shaderSource.Dispose();
using ComPtr<IDxcBlob> dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.Compile(hlslSource);
using ComPtr<IDxcUtils> dxcUtils = default;
@@ -134,7 +132,7 @@ public static class ReflectionServices
return new(
CompilerVersion: new string(d3D12ShaderDescription.Creator),
HlslSource: shaderSource.WrittenSpan.ToString(),
HlslSource: hlslSource,
ConstantBufferCount: d3D12ShaderDescription.ConstantBuffers,
BoundResourceCount: d3D12ShaderDescription.BoundResources,
InstructionCount: d3D12ShaderDescription.InstructionCount,
+8 -69
ファイルの表示
@@ -1,4 +1,3 @@
using System.Collections.Immutable;
using ComputeSharp.__Internals;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGenerators.Models;
@@ -25,14 +24,14 @@ partial class IShaderGenerator
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth)
{
// Generate the necessary body statements depending on whether dynamic shaders are supported
ImmutableArray<StatementSyntax> bodyStatements = GenerateRenderMethodBody(hlslSourceInfo, hierarchyDepth);
// The indentation level is the current depth + 2 (the method, and the return expression)
SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSourceInfo.HlslSource, hierarchyDepth + 2);
// This code produces a method declaration as follows:
//
// readonly void global::ComputeSharp.__Internals.IShader.BuildHlslSource(out global::ComputeSharp.__Internals.ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ)
// readonly void global::ComputeSharp.__Internals.IShader.BuildHlslSource(out string hlslSource)
// {
// <BODY>
// hlslSource = <HLSL_SOURCE>;
// }
return
MethodDeclaration(
@@ -41,73 +40,13 @@ partial class IShaderGenerator
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.__Internals.{nameof(IShader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.AddParameterListParameters(
Parameter(Identifier("builder")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder")),
Parameter(Identifier("threadsX")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
Parameter(Identifier("threadsY")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))),
Parameter(Identifier("threadsZ")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
.WithBody(Block(bodyStatements));
}
/// <summary>
/// Produces the series of statements for the empty fallback method.
/// </summary>
/// <returns>A series of statements for when dynamic shaders are not supported.</returns>
private static ImmutableArray<StatementSyntax> GenerateEmptyMethodBody()
{
// builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(0);
return
ImmutableArray.Create<StatementSyntax>(
Parameter(Identifier("hlslSource")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.StringKeyword))))
.WithBody(Block(
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("builder"),
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder"),
IdentifierName("Create")))
.AddArgumentListArguments(
Argument(LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(0)))))));
}
/// <summary>
/// Produces the series of statements to build the current HLSL source.
/// </summary>
/// <param name="hlslSourceInfo">The input <see cref="HlslShaderSourceInfo"/> instance to use.</param>
/// <param name="hierarchyDepth">The depth of the hierarchy for this type (used to calculate the right indentation).</param>
/// <returns>The series of statements to build the HLSL source to compile to execute the current shader.</returns>
private static ImmutableArray<StatementSyntax> GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth)
{
using ImmutableArrayBuilder<StatementSyntax> statements = new();
// builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(<SIZE_HINT>);
statements.Add(
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("builder"),
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("global::ComputeSharp.__Internals.ArrayPoolStringBuilder"),
IdentifierName("Create")))
.AddArgumentListArguments(
Argument(LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(hlslSourceInfo.HlslSource.Length)))))));
// The indentation level is the current depth + 2 (the method, and the return expression)
SyntaxToken hlslSourceLiteralExpression = SyntaxTokenHelper.CreateRawMultilineStringLiteral(hlslSourceInfo.HlslSource, hierarchyDepth + 2);
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName("builder"), IdentifierName("Append")))
.AddArgumentListArguments(Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression)))));
return statements.ToImmutable();
IdentifierName("hlslSource"),
LiteralExpression(SyntaxKind.StringLiteralExpression, hlslSourceLiteralExpression)))));
}
}
}
+2 -5
ファイルの表示
@@ -38,13 +38,10 @@ public interface IShader
/// <summary>
/// Builds the HLSL source code for the current shader instance.
/// </summary>
/// <param name="builder">The resulting <see cref="ArrayPoolStringBuilder"/> instance containing the HLSL source.</param>
/// <param name="threadsX">The number of threads in each thread group for the X axis.</param>
/// <param name="threadsY">The number of threads in each thread group for the Y axis.</param>
/// <param name="threadsZ">The number of threads in each thread group for the Z axis.</param>
/// <param name="hlslSource">The resulting HLSL source.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is not intended to be called directly by user code")]
void BuildHlslSource(out ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ);
void BuildHlslSource(out string hlslSource);
/// <summary>
/// Loads the bytecode for the current shader.
-163
ファイルの表示
@@ -1,163 +0,0 @@
using System;
using System.Buffers;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace ComputeSharp.__Internals;
/// <summary>
/// A helper type that implements a pooled buffer writer for <see cref="char"/> values.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This type is not intended to be used directly by user code")]
[DebuggerTypeProxy(typeof(DebugView))]
public ref struct ArrayPoolStringBuilder
{
/// <summary>
/// The underlying <see cref="char"/> array.
/// </summary>
private char[] array;
/// <summary>
/// The starting offset within <see cref="array"/>.
/// </summary>
private int index;
/// <summary>
/// Creates a new <see cref="ArrayPoolStringBuilder"/> instance ready to use.
/// </summary>
/// <param name="sizeHint">The size hint for the internal buffer to allocate.</param>
/// <returns>A new <see cref="ArrayPoolStringBuilder"/> instance with default values.</returns>
public static ArrayPoolStringBuilder Create(int sizeHint)
{
ArrayPoolStringBuilder builder;
builder.array = ArrayPool<char>.Shared.Rent(sizeHint);
builder.index = 0;
return builder;
}
/// <summary>
/// Gets the data written to the underlying buffer so far, as a <see cref="ReadOnlySpan{T}"/>.
/// </summary>
[UnscopedRef]
public readonly ReadOnlySpan<char> WrittenSpan
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(this.array, 0, this.index);
}
/// <summary>
/// Appends the input character to the current buffer.
/// </summary>
/// <param name="value">The input character to write.</param>
public void Append(char value)
{
EnsureCapacity(1);
this.array[this.index++] = value;
}
/// <summary>
/// Appends the input sequence of characters to the current buffer.
/// </summary>
/// <param name="value">The input characters to write.</param>
public void Append(string value)
{
EnsureCapacity(value.Length);
#if NET6_0_OR_GREATER
value.CopyTo(this.array.AsSpan(this.index));
#else
value.AsSpan().CopyTo(this.array.AsSpan(this.index));
#endif
this.index += value.Length;
}
/// <summary>
/// Appends the text representation of the input value to the current buffer.
/// </summary>
/// <param name="value">The input value to write.</param>
public void Append(int value)
{
#if NET6_0_OR_GREATER
int charsWritten;
while (!value.TryFormat(this.array.AsSpan(this.index), out charsWritten))
{
EnsureCapacity(10);
}
this.index += charsWritten;
#else
Append(value.ToString());
#endif
}
/// <summary>
/// Ensures that <see cref="array"/> has enough free space to contain a given number of new items.
/// </summary>
/// <param name="requestedSize">The minimum number of items to ensure space for in <see cref="array"/>.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EnsureCapacity(int requestedSize)
{
if (requestedSize > this.array.Length - this.index)
{
ResizeBuffer(requestedSize);
}
}
/// <summary>
/// Resizes <see cref="array"/> to ensure it can fit the specified number of new items.
/// </summary>
/// <param name="sizeHint">The minimum number of items to ensure space for in <see cref="array"/>.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private void ResizeBuffer(int sizeHint)
{
int minimumSize = this.index + sizeHint;
// Rent a new array with the specified size, and copy as many items from the current array
// as possible to the new array. This mirrors the behavior of the Array.Resize API from
// the BCL: if the new size is greater than the length of the current array, copy all the
// items from the original array into the new one. Otherwise, copy as many items as possible,
// until the new array is completely filled, and ignore the remaining items in the first array.
char[] newArray = ArrayPool<char>.Shared.Rent(minimumSize);
int itemsToCopy = Math.Min(this.array.Length, minimumSize);
Array.Copy(this.array, 0, newArray, 0, itemsToCopy);
ArrayPool<char>.Shared.Return(this.array);
this.array = newArray;
}
/// <inheritdoc/>
public readonly void Dispose()
{
ArrayPool<char>.Shared.Return(this.array);
}
/// <summary>
/// A debug proxy used for displaying debug info.
/// </summary>
internal sealed class DebugView
{
/// <summary>
/// Initializes a new instance of the <see cref="DebugView"/> class with the specified parameters.
/// </summary>
/// <param name="builder">The input <see cref="ArrayPoolStringBuilder"/> instance to display.</param>
public DebugView(ArrayPoolStringBuilder builder)
{
Text = builder.WrittenSpan.ToString();
}
/// <summary>
/// Gets the text to display for the current instance.
/// </summary>
public string Text { get; }
}
}