diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
index 5c46397b..b858e0fa 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems
@@ -30,6 +30,7 @@
+
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/HlslSourceHelper.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/HlslSourceHelper.cs
new file mode 100644
index 00000000..8aff4c3e
--- /dev/null
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/HlslSourceHelper.cs
@@ -0,0 +1,146 @@
+using System.Collections.Immutable;
+
+namespace ComputeSharp.SourceGeneration.Helpers;
+
+///
+/// A helper type to write HLSL source.
+///
+internal static class HlslSourceHelper
+{
+ ///
+ /// Writes the header included at the top of each generated HLSL shader.
+ ///
+ /// The instance to write into.
+ public static void WriteHeader(IndentedTextWriter writer)
+ {
+ writer.WriteLine("""
+ // ================================================
+ // AUTO GENERATED
+ // ================================================
+ // This shader was created by ComputeSharp.
+ // See: https://github.com/Sergio0694/ComputeSharp.
+ """, isMultiline: true);
+
+ writer.WriteLine();
+ }
+
+ ///
+ /// Writes the top declarations in each generated HLSL shader:
+ ///
+ /// - Defined constants.
+ /// - Type forward declarations (optional).
+ /// - Method forward declarations.
+ /// - Static fields.
+ /// - Type declarations.
+ ///
+ ///
+ /// The instance to write into.
+ /// The sequence of defined constants for the 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.
+ /// Whether to include type forward declarations (if supported).
+ public static void WriteTopDeclarations(
+ IndentedTextWriter writer,
+ ImmutableArray definedConstants,
+ ImmutableArray staticFields,
+ ImmutableArray processedMethods,
+ ImmutableArray typeDeclarations,
+ bool includeTypeForwardDeclarations)
+ {
+ // Define declarations
+ foreach (HlslConstant constant in definedConstants)
+ {
+ writer.WriteLine($"#define {constant.Name} {constant.Value}");
+ }
+
+ writer.WriteLine(skipIfPresent: true);
+
+ // Forward declarations of discovered types, only if supported
+ if (includeTypeForwardDeclarations)
+ {
+ 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);
+ }
+
+ ///
+ /// Writes the declarations for captured shader fields.
+ ///
+ /// The instance to write into.
+ /// The sequence of value instance fields for the current shader.
+ public static void WriteCapturedFields(
+ IndentedTextWriter writer,
+ ImmutableArray valueFields)
+ {
+ foreach (HlslValueField valueField in valueFields)
+ {
+ writer.WriteLine($"{valueField.Type} {valueField.Name};");
+ }
+ }
+
+ ///
+ /// Writes all method declarations (ie. implementations of previous forward declarations).
+ ///
+ /// The instance to write into.
+ /// The sequence of processed methods used by the shader.
+ /// The collection of implementations of all methods in all custom types.
+ public static void WriteMethodDeclarations(
+ IndentedTextWriter writer,
+ ImmutableArray processedMethods,
+ ImmutableArray typeMethodDeclarations)
+ {
+ // 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);
+ }
+
+ writer.WriteLine(skipIfPresent: true);
+ }
+}