diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs index 1e843687..632a49d2 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslStringMethod.cs @@ -84,7 +84,7 @@ namespace ComputeSharp.SourceGenerators Identifier("BuildHlslString")) .AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.ReadOnlyKeyword)) .AddParameterListParameters( - Parameter(Identifier("builder")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("ArrayPoolStringBuilder")), + Parameter(Identifier("builder")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(IdentifierName("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)))) @@ -463,6 +463,22 @@ namespace ComputeSharp.SourceGenerators IEnumerable processedMethods, string executeMethod) { + // builder = ArrayPoolStringBuilder.Create(1024); + yield return + ExpressionStatement( + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + IdentifierName("builder"), + InvocationExpression( + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName("ArrayPoolStringBuilder"), + IdentifierName("Create"))) + .AddArgumentListArguments( + Argument(LiteralExpression( + SyntaxKind.NumericLiteralExpression, + Literal(1024)))))); + // Header yield return ParseStatement("builder.AppendLine(\"// ================================================\");"); yield return ParseStatement("builder.AppendLine(\"// AUTO GENERATED\");"); diff --git a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs b/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs index e2bfda10..fee6f8aa 100644 --- a/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs +++ b/src/ComputeSharp/Core/Interfaces/__Internals/IShader{TSelf}.cs @@ -48,12 +48,12 @@ namespace ComputeSharp.__Internals /// /// Builds the HLSL source code for the current shader instance. /// - /// The target instance to write the HLSL source to. + /// 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. [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This method is not intended to be called directly by user code")] - void BuildHlslString(ref ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ); + void BuildHlslString(out ArrayPoolStringBuilder builder, int threadsX, int threadsY, int threadsZ); } } diff --git a/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs index 332654f8..4b8e7194 100644 --- a/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs +++ b/src/ComputeSharp/Shaders/Interop/ReflectionServices.cs @@ -102,11 +102,12 @@ namespace ComputeSharp.Interop private static unsafe void GetNonGenericShaderInfo(in T shader, out ShaderInfo shaderInfo) where T : struct, IShader { - ArrayPoolStringBuilder shaderSource = ArrayPoolStringBuilder.Create(); - - shader.BuildHlslString(ref shaderSource, 1, 1, 1); + shader.BuildHlslString(out ArrayPoolStringBuilder shaderSource, 1, 1, 1); using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(shaderSource.WrittenSpan); + + shaderSource.Dispose(); + using ComPtr dxcUtils = default; Guid dxcLibraryClsid = FX.CLSID_DxcLibrary; diff --git a/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs b/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs index a7922b49..cda9f57a 100644 --- a/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs +++ b/src/ComputeSharp/Shaders/Renderer/__Internals/ArrayPoolStringBuilder.cs @@ -25,13 +25,14 @@ namespace ComputeSharp.__Internals /// /// Creates a new instance ready to use. /// + /// The size hint for the internal buffer to allocate. /// A new instance with default values. [Pure] - public static ArrayPoolStringBuilder Create() + public static ArrayPoolStringBuilder Create(int sizeHint) { ArrayPoolStringBuilder builder; - builder.array = ArrayPool.Shared.Rent(1024); + builder.array = ArrayPool.Shared.Rent(sizeHint); builder.index = 0; return builder; diff --git a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs index 15a0c699..9fdcf83f 100644 --- a/src/ComputeSharp/Shaders/ShaderRunner{T}.cs +++ b/src/ComputeSharp/Shaders/ShaderRunner{T}.cs @@ -237,9 +237,7 @@ namespace ComputeSharp.Shaders [MethodImpl(MethodImplOptions.NoInlining)] private static unsafe void LoadShader(int threadsX, int threadsY, int threadsZ, in T shader, out CachedShader shaderData) { - ArrayPoolStringBuilder builder = ArrayPoolStringBuilder.Create(); - - shader.BuildHlslString(ref builder, threadsX, threadsY, threadsZ); + shader.BuildHlslString(out ArrayPoolStringBuilder builder, threadsX, threadsY, threadsZ); using ComPtr dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(builder.WrittenSpan);