diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs index 9addce80..4016dd5e 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.Syntax.cs @@ -23,6 +23,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.IncreaseIndent(); writer.WriteLine("\"\"\""); writer.Write(info.HlslInfoKey.HlslSource, isMultiline: true); + writer.WriteLine(skipIfPresent: true); writer.WriteLine("\"\"\";"); writer.DecreaseIndent(); } diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index 150cc289..05af11da 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Linq; using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; @@ -471,124 +470,101 @@ partial class D2DPixelShaderDescriptorGenerator ImmutableArray inputComplexIndices, bool requiresScenePosition) { - using ImmutableArrayBuilder hlslBuilder = new(); - - void AppendLF() - { - hlslBuilder.Add('\n'); - } - - void AppendLineAndLF(string text) - { - hlslBuilder.AddRange(text.AsSpan()); - hlslBuilder.Add('\n'); - } + using IndentedTextWriter writer = new(); // Header - AppendLineAndLF("// ================================================"); - AppendLineAndLF("// AUTO GENERATED"); - AppendLineAndLF("// ================================================"); - AppendLineAndLF("// This shader was created by ComputeSharp."); - AppendLineAndLF("// See: https://github.com/Sergio0694/ComputeSharp."); - AppendLF(); + writer.WriteLine(""" + // ================================================ + // AUTO GENERATED + // ================================================ + // This shader was created by ComputeSharp. + // See: https://github.com/Sergio0694/ComputeSharp. + """, isMultiline: true); // Shader metadata - AppendLineAndLF($"#define D2D_INPUT_COUNT {inputCount}"); + writer.WriteLine(); + writer.WriteLine($"#define D2D_INPUT_COUNT {inputCount}"); foreach (int simpleInput in inputSimpleIndices) { - AppendLineAndLF($"#define D2D_INPUT{simpleInput}_SIMPLE"); + writer.WriteLine($"#define D2D_INPUT{simpleInput}_SIMPLE"); } foreach (int complexInput in inputComplexIndices) { - AppendLineAndLF($"#define D2D_INPUT{complexInput}_COMPLEX"); + writer.WriteLine($"#define D2D_INPUT{complexInput}_COMPLEX"); } - if (requiresScenePosition) - { - AppendLineAndLF("#define D2D_REQUIRES_SCENE_POSITION"); - } + writer.WriteLineIf(requiresScenePosition, "#define D2D_REQUIRES_SCENE_POSITION"); // Add the "d2d1effecthelpers.hlsli" header - AppendLF(); - AppendLineAndLF("#include \"d2d1effecthelpers.hlsli\""); + writer.WriteLine(); + writer.WriteLine("#include \"d2d1effecthelpers.hlsli\""); + writer.WriteLine(); // Define declarations - if (definedConstants.Any()) + foreach ((string name, string value) in definedConstants) { - AppendLF(); - - foreach ((string name, string value) in definedConstants) - { - AppendLineAndLF($"#define {name} {value}"); - } + writer.WriteLine($"#define {name} {value}"); } - // Static fields - if (staticFields.Any()) - { - AppendLF(); + writer.WriteLine(skipIfPresent: true); - foreach ((string Name, string TypeDeclaration, string? Assignment) field in staticFields) + // Static fields + foreach ((string Name, string TypeDeclaration, string? Assignment) field in staticFields) + { + if (field.Assignment is string assignment) { - if (field.Assignment is string assignment) - { - AppendLineAndLF($"{field.TypeDeclaration} {field.Name} = {assignment};"); - } - else - { - AppendLineAndLF($"{field.TypeDeclaration} {field.Name};"); - } + writer.WriteLine($"{field.TypeDeclaration} {field.Name} = {assignment};"); + } + else + { + writer.WriteLine($"{field.TypeDeclaration} {field.Name};"); } } // Declared types foreach ((string _, string typeDefinition) in declaredTypes) { - AppendLF(); - AppendLineAndLF(typeDefinition); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(typeDefinition); } - // Captured variables - if (valueFields.Any()) - { - AppendLF(); + writer.WriteLine(skipIfPresent: true); - foreach ((string fieldName, string fieldType) in valueFields) - { - AppendLineAndLF($"{fieldType} {fieldName};"); - } + // Captured variables + foreach ((string fieldName, string fieldType) in valueFields) + { + writer.WriteLine($"{fieldType} {fieldName};"); } // Resource textures foreach ((string fieldName, string fieldType, int index) in resourceTextureFields) { - AppendLF(); - - AppendLineAndLF($"{fieldType} {fieldName} : register(t{index});"); - AppendLineAndLF($"SamplerState __sampler__{fieldName} : register(s{index});"); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine($"{fieldType} {fieldName} : register(t{index});"); + writer.WriteLine($"SamplerState __sampler__{fieldName} : register(s{index});"); } // Forward declarations foreach ((string forwardDeclaration, string _) in processedMethods) { - AppendLF(); - AppendLineAndLF(forwardDeclaration); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(forwardDeclaration); } // Captured methods foreach ((string _, string method) in processedMethods) { - AppendLF(); - AppendLineAndLF(method); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(method); } // Entry point - AppendLF(); - AppendLineAndLF(executeMethod); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(executeMethod); - return hlslBuilder.ToString(); + return writer.ToString(); } } } \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs index 1dd1c909..6ff0b05d 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.Syntax.cs @@ -23,6 +23,7 @@ partial class ComputeShaderDescriptorGenerator writer.IncreaseIndent(); writer.WriteLine("\"\"\""); writer.Write(info.HlslInfoKey.HlslSource, isMultiline: true); + writer.WriteLine(skipIfPresent: true); writer.WriteLine("\"\"\";"); writer.DecreaseIndent(); } diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index 6c60560f..ba24e672 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -512,98 +512,68 @@ partial class ComputeShaderDescriptorGenerator bool isSamplerUsed, string executeMethod) { - using ImmutableArrayBuilder hlslBuilder = new(); - - void AppendLF() - { - hlslBuilder.Add('\n'); - } - - void AppendLineAndLF(string text) - { - hlslBuilder.AddRange(text.AsSpan()); - hlslBuilder.Add('\n'); - } - - void AppendCharacterAndLF(char c) - { - hlslBuilder.Add(c); - hlslBuilder.Add('\n'); - } - - string FlushText() - { - string text = hlslBuilder.ToString(); - - hlslBuilder.Clear(); - - return text; - } + using IndentedTextWriter writer = new(); // Header - AppendLineAndLF("// ================================================"); - AppendLineAndLF("// AUTO GENERATED"); - AppendLineAndLF("// ================================================"); - AppendLineAndLF("// This shader was created by ComputeSharp."); - AppendLineAndLF("// See: https://github.com/Sergio0694/ComputeSharp."); + writer.WriteLine(""" + // ================================================ + // AUTO GENERATED + // ================================================ + // This shader was created by ComputeSharp. + // See: https://github.com/Sergio0694/ComputeSharp. + """, isMultiline: true); // Group size constants - AppendLF(); - AppendLineAndLF($"#define __GroupSize__get_X {threadsX}"); - AppendLineAndLF($"#define __GroupSize__get_Y {threadsY}"); - AppendLineAndLF($"#define __GroupSize__get_Z {threadsZ}"); + writer.WriteLine(); + writer.WriteLine($"#define __GroupSize__get_X {threadsX}"); + writer.WriteLine($"#define __GroupSize__get_Y {threadsY}"); + writer.WriteLine($"#define __GroupSize__get_Z {threadsZ}"); // Define declarations foreach ((string name, string value) in definedConstants) { - AppendLineAndLF($"#define {name} {value}"); + writer.WriteLine($"#define {name} {value}"); } - // Static fields - if (staticFields.Any()) - { - AppendLF(); + writer.WriteLine(); - foreach ((string Name, string TypeDeclaration, string? Assignment) field in staticFields) + // Static fields + foreach ((string Name, string TypeDeclaration, string? Assignment) field in staticFields) + { + if (field.Assignment is string assignment) { - if (field.Assignment is string assignment) - { - AppendLineAndLF($"{field.TypeDeclaration} {field.Name} = {assignment};"); - } - else - { - AppendLineAndLF($"{field.TypeDeclaration} {field.Name};"); - } + writer.WriteLine($"{field.TypeDeclaration} {field.Name} = {assignment};"); + } + else + { + writer.WriteLine($"{field.TypeDeclaration} {field.Name};"); } } // Declared types foreach ((string _, string typeDefinition) in declaredTypes) { - AppendLF(); - AppendLineAndLF(typeDefinition); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(typeDefinition); } + writer.WriteLine(skipIfPresent: true); + writer.WriteLine("cbuffer _ : register(b0)"); + // Captured variables - AppendLF(); - AppendLineAndLF("cbuffer _ : register(b0)"); - AppendCharacterAndLF('{'); - AppendLineAndLF(" uint __x;"); - AppendLineAndLF(" uint __y;"); - - if (isComputeShader) + using (writer.WriteBlock()) { - AppendLineAndLF(" uint __z;"); - } + writer.WriteLine("uint __x;"); + writer.WriteLine("uint __y;"); + writer.WriteLineIf(isComputeShader, "uint __z;"); - // User-defined values - foreach ((string fieldName, string fieldType) in valueFields) - { - AppendLineAndLF($" {fieldType} {fieldName};"); + // User-defined values + foreach ((string fieldName, string fieldType) in valueFields) + { + writer.WriteLine($"{fieldType} {fieldName};"); + } } - AppendCharacterAndLF('}'); - int constantBuffersCount = 1; int readOnlyBuffersCount = 0; int readWriteBuffersCount = 0; @@ -611,15 +581,15 @@ partial class ComputeShaderDescriptorGenerator // Optional implicit texture field if (!isComputeShader) { - AppendLF(); - AppendLineAndLF($"{implicitTextureType} __outputTexture : register(u{readWriteBuffersCount++});"); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine($"{implicitTextureType} __outputTexture : register(u{readWriteBuffersCount++});"); } // Optional sampler field if (isSamplerUsed) { - AppendLF(); - AppendLineAndLF("SamplerState __sampler : register(s);"); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine("SamplerState __sampler : register(s);"); } // Resources @@ -627,21 +597,23 @@ partial class ComputeShaderDescriptorGenerator { if (HlslKnownTypes.IsConstantBufferType(metadataName)) { - AppendLF(); - AppendLineAndLF($"cbuffer _{fieldName} : register(b{constantBuffersCount++})"); - AppendCharacterAndLF('{'); - AppendLineAndLF($" {fieldType} {fieldName}[2];"); - AppendCharacterAndLF('}'); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine($"cbuffer _{fieldName} : register(b{constantBuffersCount++})"); + + using (writer.WriteBlock()) + { + writer.WriteLine($"{fieldType} {fieldName}[2];"); + } } else if (HlslKnownTypes.IsReadOnlyTypedResourceType(metadataName)) { - AppendLF(); - AppendLineAndLF($"{fieldType} {fieldName} : register(t{readOnlyBuffersCount++});"); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine($"{fieldType} {fieldName} : register(t{readOnlyBuffersCount++});"); } else if (HlslKnownTypes.IsReadWriteTypedResourceType(metadataName)) { - AppendLF(); - AppendLineAndLF($"{fieldType} {fieldName} : register(u{readWriteBuffersCount++});"); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine($"{fieldType} {fieldName} : register(u{readWriteBuffersCount++});"); } } @@ -650,30 +622,30 @@ partial class ComputeShaderDescriptorGenerator { object count = (object?)bufferCount ?? "__GroupSize__get_X * __GroupSize__get_Y * __GroupSize__get_Z"; - AppendLF(); - AppendLineAndLF($"groupshared {bufferType} {bufferName} [{count}];"); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine($"groupshared {bufferType} {bufferName} [{count}];"); } // Forward declarations foreach ((string forwardDeclaration, string _) in processedMethods) { - AppendLF(); - AppendLineAndLF(forwardDeclaration); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(forwardDeclaration); } // Captured methods foreach ((string _, string method) in processedMethods) { - AppendLF(); - AppendLineAndLF(method); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(method); } // Entry point - AppendLF(); - AppendLineAndLF("[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]"); - AppendLineAndLF(executeMethod); + writer.WriteLine(skipIfPresent: true); + writer.WriteLine("[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]"); + writer.WriteLine(executeMethod); - return FlushText(); + return writer.ToString(); } } } \ No newline at end of file