diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs
index 93b2f1d9..3b4a1f00 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs
@@ -451,13 +451,13 @@ partial class D2DPixelShaderDescriptorGenerator
///
/// Produces the series of statements to build the current HLSL source.
///
- /// The sequence of defined constants for the shader.
- /// The sequence of value instance fields for the current shader.
+ ///
+ ///
/// The sequence of captured resource textures for the current shader.
- /// The sequence of static fields referenced by the shader.
- /// The sequence of processed methods used by the shader.
- /// The collection of declarations of all custom types.
- /// The collection of implementations of all methods in all custom types.
+ ///
+ ///
+ ///
+ ///
/// The body of the entry point of the shader.
/// The number of shader inputs to declare.
/// The indicess of the simple shader inputs.
@@ -480,17 +480,9 @@ partial class D2DPixelShaderDescriptorGenerator
{
using IndentedTextWriter writer = new();
- // Header
- writer.WriteLine("""
- // ================================================
- // AUTO GENERATED
- // ================================================
- // This shader was created by ComputeSharp.
- // See: https://github.com/Sergio0694/ComputeSharp.
- """, isMultiline: true);
+ HlslSourceHelper.WriteHeader(writer);
// Shader metadata
- writer.WriteLine();
writer.WriteLine($"#define D2D_INPUT_COUNT {inputCount}");
foreach (int simpleInput in inputSimpleIndices)
@@ -510,52 +502,16 @@ partial class D2DPixelShaderDescriptorGenerator
writer.WriteLine("#include \"d2d1effecthelpers.hlsli\"");
writer.WriteLine();
- // Define declarations
- foreach (HlslConstant constant in definedConstants)
- {
- writer.WriteLine($"#define {constant.Name} {constant.Value}");
- }
+ // The FXC compiler does not support type forward declarations
+ HlslSourceHelper.WriteTopDeclarations(
+ writer,
+ definedConstants,
+ staticFields,
+ processedMethods,
+ typeDeclarations,
+ includeTypeForwardDeclarations: false);
- writer.WriteLine(skipIfPresent: true);
-
- // Forward declarations of shader/static methods
- foreach (HlslMethod method in processedMethods)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(method.Signature);
- }
-
- writer.WriteLine(skipIfPresent: true);
-
- // Static fields
- foreach (HlslStaticField field in staticFields)
- {
- if (field.Assignment is string assignment)
- {
- writer.WriteLine($"{field.TypeDeclaration} {field.Name} = {assignment};");
- }
- else
- {
- writer.WriteLine($"{field.TypeDeclaration} {field.Name};");
- }
- }
-
- writer.WriteLine(skipIfPresent: true);
-
- // Declared types
- foreach (HlslUserType userType in typeDeclarations)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(userType.Definition);
- }
-
- writer.WriteLine(skipIfPresent: true);
-
- // Captured variables
- foreach (HlslValueField valueField in valueFields)
- {
- writer.WriteLine($"{valueField.Type} {valueField.Name};");
- }
+ HlslSourceHelper.WriteCapturedFields(writer, valueFields);
// Resource textures
foreach (HlslResourceTextureField field in resourceTextureFields)
@@ -565,22 +521,9 @@ partial class D2DPixelShaderDescriptorGenerator
writer.WriteLine($"SamplerState __sampler__{field.Name} : register(s{field.Index});");
}
- // Method declarations for discovered types
- foreach (string method in typeMethodDeclarations)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(method);
- }
-
- // Captured methods
- foreach (HlslMethod method in processedMethods)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(method.Declaration);
- }
+ HlslSourceHelper.WriteMethodDeclarations(writer, processedMethods, typeMethodDeclarations);
// Entry point
- writer.WriteLine(skipIfPresent: true);
writer.WriteLine(executeMethod);
return writer.ToString();
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs
index 4ae2df24..fbd70b2c 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs
@@ -489,14 +489,14 @@ partial class ComputeShaderDescriptorGenerator
/// The thread ids value for the X axis.
/// The thread ids value for the Y axis.
/// The thread ids value for the Z axis.
- /// The sequence of defined constants for the shader.
+ ///
/// The sequence of resource instance fields for the current shader.
- /// The sequence of value instance fields for the current shader.
- /// The sequence of static fields referenced by the shader.
+ ///
+ ///
/// The sequence of shared buffers declared by the shader.
- /// The sequence of processed methods used by the shader.
- /// The collection of declarations of all custom types.
- /// The collection of implementations of all methods in all custom types.
+ ///
+ ///
+ ///
/// Whether or not the current shader type is a compute shader.
/// The type of the implicit target texture, if present.
/// Whether the static sampler is used by the shader.
@@ -521,69 +521,21 @@ partial class ComputeShaderDescriptorGenerator
{
using IndentedTextWriter writer = new();
- // Header
- writer.WriteLine("""
- // ================================================
- // AUTO GENERATED
- // ================================================
- // This shader was created by ComputeSharp.
- // See: https://github.com/Sergio0694/ComputeSharp.
- """, isMultiline: true);
+ HlslSourceHelper.WriteHeader(writer);
// Group size constants
- 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 (HlslConstant constant in definedConstants)
- {
- writer.WriteLine($"#define {constant.Name} {constant.Value}");
- }
+ HlslSourceHelper.WriteTopDeclarations(
+ writer,
+ definedConstants,
+ staticFields,
+ processedMethods,
+ typeDeclarations,
+ includeTypeForwardDeclarations: true);
- writer.WriteLine(skipIfPresent: true);
-
- // Forward declarations of discovered types
- foreach (HlslUserType userType in typeDeclarations)
- {
- writer.WriteLine($"struct {userType.Name};");
- }
-
- writer.WriteLine(skipIfPresent: true);
-
- // Forward declarations of shader/static methods
- foreach (HlslMethod method in processedMethods)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(method.Signature);
- }
-
- writer.WriteLine(skipIfPresent: true);
-
- // Static fields
- foreach (HlslStaticField field in staticFields)
- {
- if (field.Assignment is string assignment)
- {
- writer.WriteLine($"{field.TypeDeclaration} {field.Name} = {assignment};");
- }
- else
- {
- writer.WriteLine($"{field.TypeDeclaration} {field.Name};");
- }
- }
-
- writer.WriteLine(skipIfPresent: true);
-
- // Declared types
- foreach (HlslUserType userType in typeDeclarations)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(userType.Definition);
- }
-
- writer.WriteLine(skipIfPresent: true);
writer.WriteLine("cbuffer _ : register(b0)");
// Captured variables
@@ -593,11 +545,7 @@ partial class ComputeShaderDescriptorGenerator
writer.WriteLine("uint __y;");
writer.WriteLineIf(isComputeShader, "uint __z;");
- // User-defined values
- foreach (HlslValueField valueField in valueFields)
- {
- writer.WriteLine($"{valueField.Type} {valueField.Name};");
- }
+ HlslSourceHelper.WriteCapturedFields(writer, valueFields);
}
int constantBuffersCount = 1;
@@ -645,22 +593,9 @@ partial class ComputeShaderDescriptorGenerator
writer.WriteLine($"groupshared {buffer.Type} {buffer.Name} [{count}];");
}
- // Method declarations for discovered types
- foreach (string method in typeMethodDeclarations)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(method);
- }
-
- // Captured methods
- foreach (HlslMethod method in processedMethods)
- {
- writer.WriteLine(skipIfPresent: true);
- writer.WriteLine(method.Declaration);
- }
+ HlslSourceHelper.WriteMethodDeclarations(writer, processedMethods, typeMethodDeclarations);
// Entry point
- writer.WriteLine(skipIfPresent: true);
writer.WriteLine("[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]");
writer.WriteLine(executeMethod);