diff --git a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs index 8b8e0ced..b31d8c38 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs @@ -104,11 +104,9 @@ public static class ReflectionServices private static unsafe ShaderInfo GetNonGenericShaderInfo(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 dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.Compile(shaderSource.WrittenSpan); - - shaderSource.Dispose(); + using ComPtr dxcBlobBytecode = Shaders.Translation.ShaderCompiler.Instance.Compile(hlslSource); using ComPtr 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, diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs index c1e55e3c..cf9d323a 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.Syntax.cs @@ -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 /// The resulting instance for the BuildHlslSource method. public static MethodDeclarationSyntax GetSyntax(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth) { - // Generate the necessary body statements depending on whether dynamic shaders are supported - ImmutableArray 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) // { - // + // hlslSource = ; // } 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)); - } - - /// - /// Produces the series of statements for the empty fallback method. - /// - /// A series of statements for when dynamic shaders are not supported. - private static ImmutableArray GenerateEmptyMethodBody() - { - // builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(0); - return - ImmutableArray.Create( + 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))))))); - } - - /// - /// Produces the series of statements to build the current HLSL source. - /// - /// The input instance to use. - /// The depth of the hierarchy for this type (used to calculate the right indentation). - /// The series of statements to build the HLSL source to compile to execute the current shader. - private static ImmutableArray GenerateRenderMethodBody(HlslShaderSourceInfo hlslSourceInfo, int hierarchyDepth) - { - using ImmutableArrayBuilder statements = new(); - - // builder = global::ComputeSharp.__Internals.ArrayPoolStringBuilder.Create(); - 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))))); } } } \ No newline at end of file diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs index 35878d67..d39d9234 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader.cs @@ -38,13 +38,10 @@ public interface IShader /// /// Builds the HLSL source code for the current shader instance. /// - /// The resulting instance containing the HLSL source. - /// The number of threads in each thread group for the X axis. - /// The number of threads in each thread group for the Y axis. - /// The number of threads in each thread group for the Z axis. + /// The resulting HLSL source. [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); /// /// Loads the bytecode for the current shader. diff --git a/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs b/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs deleted file mode 100644 index cb503640..00000000 --- a/src/ComputeSharp/Shaders/Dispatching/__Internals/ArrayPoolStringBuilder.cs +++ /dev/null @@ -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; - -/// -/// A helper type that implements a pooled buffer writer for values. -/// -[EditorBrowsable(EditorBrowsableState.Never)] -[Obsolete("This type is not intended to be used directly by user code")] -[DebuggerTypeProxy(typeof(DebugView))] -public ref struct ArrayPoolStringBuilder -{ - /// - /// The underlying array. - /// - private char[] array; - - /// - /// The starting offset within . - /// - private int index; - - /// - /// Creates a new instance ready to use. - /// - /// The size hint for the internal buffer to allocate. - /// A new instance with default values. - public static ArrayPoolStringBuilder Create(int sizeHint) - { - ArrayPoolStringBuilder builder; - - builder.array = ArrayPool.Shared.Rent(sizeHint); - builder.index = 0; - - return builder; - } - - /// - /// Gets the data written to the underlying buffer so far, as a . - /// - [UnscopedRef] - public readonly ReadOnlySpan WrittenSpan - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => new(this.array, 0, this.index); - } - - /// - /// Appends the input character to the current buffer. - /// - /// The input character to write. - public void Append(char value) - { - EnsureCapacity(1); - - this.array[this.index++] = value; - } - - /// - /// Appends the input sequence of characters to the current buffer. - /// - /// The input characters to write. - 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; - } - - /// - /// Appends the text representation of the input value to the current buffer. - /// - /// The input value to write. - 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 - } - - /// - /// Ensures that has enough free space to contain a given number of new items. - /// - /// The minimum number of items to ensure space for in . - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void EnsureCapacity(int requestedSize) - { - if (requestedSize > this.array.Length - this.index) - { - ResizeBuffer(requestedSize); - } - } - - /// - /// Resizes to ensure it can fit the specified number of new items. - /// - /// The minimum number of items to ensure space for in . - [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.Shared.Rent(minimumSize); - int itemsToCopy = Math.Min(this.array.Length, minimumSize); - - Array.Copy(this.array, 0, newArray, 0, itemsToCopy); - - ArrayPool.Shared.Return(this.array); - - this.array = newArray; - } - - /// - public readonly void Dispose() - { - ArrayPool.Shared.Return(this.array); - } - - /// - /// A debug proxy used for displaying debug info. - /// - internal sealed class DebugView - { - /// - /// Initializes a new instance of the class with the specified parameters. - /// - /// The input instance to display. - public DebugView(ArrayPoolStringBuilder builder) - { - Text = builder.WrittenSpan.ToString(); - } - - /// - /// Gets the text to display for the current instance. - /// - public string Text { get; } - } -} \ No newline at end of file