Adopt HlslSourceHelper type to generate HLSL sources

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-13 14:22:25 +01:00
1 親 4bbe96af12
コミット 8d5d24ea44
2ファイル変更
+33 -155

検索条件に一致するファイルはありません

+17 -74
ファイルの表示
@@ -451,13 +451,13 @@ partial class D2DPixelShaderDescriptorGenerator
/// <summary>
/// Produces the series of statements to build the current HLSL source.
/// </summary>
/// <param name="definedConstants">The sequence of defined constants for the shader.</param>
/// <param name="valueFields">The sequence of value instance fields for the current shader.</param>
/// <param name="definedConstants"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='definedConstants']/node()"/></param>
/// <param name="valueFields"><inheritdoc cref="HlslSourceHelper.WriteCapturedFields" path="/param[@name='valueFields']/node()"/></param>
/// <param name="resourceTextureFields">The sequence of captured resource textures for the current shader.</param>
/// <param name="staticFields">The sequence of static fields referenced by the shader.</param>
/// <param name="processedMethods">The sequence of processed methods used by the shader.</param>
/// <param name="typeDeclarations">The collection of declarations of all custom types.</param>
/// <param name="typeMethodDeclarations">The collection of implementations of all methods in all custom types.</param>
/// <param name="staticFields"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='staticFields']/node()"/></param>
/// <param name="processedMethods"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='processedMethods']/node()"/></param>
/// <param name="typeDeclarations"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='typeDeclarations']/node()"/></param>
/// <param name="typeMethodDeclarations"><inheritdoc cref="HlslSourceHelper.WriteMethodDeclarations" path="/param[@name='typeMethodDeclarations']/node()"/></param>
/// <param name="executeMethod">The body of the entry point of the shader.</param>
/// <param name="inputCount">The number of shader inputs to declare.</param>
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
@@ -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();
+16 -81
ファイルの表示
@@ -489,14 +489,14 @@ partial class ComputeShaderDescriptorGenerator
/// <param name="threadsX">The thread ids value for the X axis.</param>
/// <param name="threadsY">The thread ids value for the Y axis.</param>
/// <param name="threadsZ">The thread ids value for the Z axis.</param>
/// <param name="definedConstants">The sequence of defined constants for the shader.</param>
/// <param name="definedConstants"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='definedConstants']/node()"/></param>
/// <param name="resourceFields">The sequence of resource instance fields for the current shader.</param>
/// <param name="valueFields">The sequence of value instance fields for the current shader.</param>
/// <param name="staticFields">The sequence of static fields referenced by the shader.</param>
/// <param name="valueFields"><inheritdoc cref="HlslSourceHelper.WriteCapturedFields" path="/param[@name='valueFields']/node()"/></param>
/// <param name="staticFields"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='staticFields']/node()"/></param>
/// <param name="sharedBuffers">The sequence of shared buffers declared by the shader.</param>
/// <param name="processedMethods">The sequence of processed methods used by the shader.</param>
/// <param name="typeDeclarations">The collection of declarations of all custom types.</param>
/// <param name="typeMethodDeclarations">The collection of implementations of all methods in all custom types.</param>
/// <param name="processedMethods"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='processedMethods']/node()"/></param>
/// <param name="typeDeclarations"><inheritdoc cref="HlslSourceHelper.WriteTopDeclarations" path="/param[@name='typeDeclarations']/node()"/></param>
/// <param name="typeMethodDeclarations"><inheritdoc cref="HlslSourceHelper.WriteMethodDeclarations" path="/param[@name='typeMethodDeclarations']/node()"/></param>
/// <param name="isComputeShader">Whether or not the current shader type is a compute shader.</param>
/// <param name="implicitTextureType">The type of the implicit target texture, if present.</param>
/// <param name="isSamplerUsed">Whether the static sampler is used by the shader.</param>
@@ -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);