From a926a1211fcb489e5bd0695e8f082fada409e37d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 18 Dec 2023 20:01:07 +0100 Subject: [PATCH 1/3] Move and rename the HlslSourceHelper type --- ...xelShaderDescriptorGenerator.HlslSource.cs | 20 +++++++++---------- ...mputeSharp.SourceGeneration.Hlsl.projitems | 2 +- .../HlslSourceSyntaxProcessor.cs} | 7 ++++--- ...uteShaderDescriptorGenerator.HlslSource.cs | 20 +++++++++---------- 4 files changed, 25 insertions(+), 24 deletions(-) rename src/ComputeSharp.SourceGeneration.Hlsl/{Helpers/HlslSourceHelper.cs => SyntaxProcessors/HlslSourceSyntaxProcessor.cs} (95%) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index 3b4a1f00..831bcb05 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 captured resource textures for the current shader. - /// - /// - /// - /// + /// + /// + /// + /// /// The body of the entry point of the shader. /// The number of shader inputs to declare. /// The indicess of the simple shader inputs. @@ -480,7 +480,7 @@ partial class D2DPixelShaderDescriptorGenerator { using IndentedTextWriter writer = new(); - HlslSourceHelper.WriteHeader(writer); + HlslSourceSyntaxProcessor.WriteHeader(writer); // Shader metadata writer.WriteLine($"#define D2D_INPUT_COUNT {inputCount}"); @@ -503,7 +503,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine(); // The FXC compiler does not support type forward declarations - HlslSourceHelper.WriteTopDeclarations( + HlslSourceSyntaxProcessor.WriteTopDeclarations( writer, definedConstants, staticFields, @@ -511,7 +511,7 @@ partial class D2DPixelShaderDescriptorGenerator typeDeclarations, includeTypeForwardDeclarations: false); - HlslSourceHelper.WriteCapturedFields(writer, valueFields); + HlslSourceSyntaxProcessor.WriteCapturedFields(writer, valueFields); // Resource textures foreach (HlslResourceTextureField field in resourceTextureFields) @@ -521,7 +521,7 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine($"SamplerState __sampler__{field.Name} : register(s{field.Index});"); } - HlslSourceHelper.WriteMethodDeclarations(writer, processedMethods, typeMethodDeclarations); + HlslSourceSyntaxProcessor.WriteMethodDeclarations(writer, processedMethods, typeMethodDeclarations); // Entry point writer.WriteLine(executeMethod); diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index ac737e05..5b6c9f91 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -31,7 +31,7 @@ - + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/HlslSourceHelper.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslSourceSyntaxProcessor.cs similarity index 95% rename from src/ComputeSharp.SourceGeneration.Hlsl/Helpers/HlslSourceHelper.cs rename to src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslSourceSyntaxProcessor.cs index 8aff4c3e..9d2ee587 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Helpers/HlslSourceHelper.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslSourceSyntaxProcessor.cs @@ -1,11 +1,12 @@ using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Helpers; -namespace ComputeSharp.SourceGeneration.Helpers; +namespace ComputeSharp.SourceGeneration.SyntaxProcessors; /// -/// A helper type to write HLSL source. +/// A processor responsible for formatting shared HLSL source for all shader types. /// -internal static class HlslSourceHelper +internal static class HlslSourceSyntaxProcessor { /// /// Writes the header included at the top of each generated HLSL shader. diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index fbd70b2c..48d351d6 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 resource instance fields for the current shader. - /// - /// + /// + /// /// The sequence of shared buffers declared by the shader. - /// - /// - /// + /// + /// + /// /// 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,14 +521,14 @@ partial class ComputeShaderDescriptorGenerator { using IndentedTextWriter writer = new(); - HlslSourceHelper.WriteHeader(writer); + HlslSourceSyntaxProcessor.WriteHeader(writer); // Group size constants writer.WriteLine($"#define __GroupSize__get_X {threadsX}"); writer.WriteLine($"#define __GroupSize__get_Y {threadsY}"); writer.WriteLine($"#define __GroupSize__get_Z {threadsZ}"); - HlslSourceHelper.WriteTopDeclarations( + HlslSourceSyntaxProcessor.WriteTopDeclarations( writer, definedConstants, staticFields, @@ -545,7 +545,7 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLine("uint __y;"); writer.WriteLineIf(isComputeShader, "uint __z;"); - HlslSourceHelper.WriteCapturedFields(writer, valueFields); + HlslSourceSyntaxProcessor.WriteCapturedFields(writer, valueFields); } int constantBuffersCount = 1; @@ -593,7 +593,7 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLine($"groupshared {buffer.Type} {buffer.Name} [{count}];"); } - HlslSourceHelper.WriteMethodDeclarations(writer, processedMethods, typeMethodDeclarations); + HlslSourceSyntaxProcessor.WriteMethodDeclarations(writer, processedMethods, typeMethodDeclarations); // Entry point writer.WriteLine("[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]"); From 2cb61a3eeca017e1d387f7e997f16cfed26c6690 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 18 Dec 2023 20:25:59 +0100 Subject: [PATCH 2/3] Fix sorting of custom type declarations in HLSL --- .../HlslSourceSyntaxProcessor.cs | 21 ++++++++------- .../ComputeSharp.Tests/ShaderCompilerTests.cs | 26 +++++++++---------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslSourceSyntaxProcessor.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslSourceSyntaxProcessor.cs index 9d2ee587..27823c55 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslSourceSyntaxProcessor.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslSourceSyntaxProcessor.cs @@ -68,6 +68,18 @@ internal static class HlslSourceSyntaxProcessor writer.WriteLine(skipIfPresent: true); } + // Declared types (these have to be declared early on in the shader so that even if + // forward declarations for types are not supported, like is the case for D2D shaders + // using the FXC compiler, the resulting HLSL code is valid in case any forward + // declaration of methods in the shader has one of these types in its signature). + foreach (HlslUserType userType in typeDeclarations) + { + writer.WriteLine(skipIfPresent: true); + writer.WriteLine(userType.Definition); + } + + writer.WriteLine(skipIfPresent: true); + // Forward declarations of shader/static methods foreach (HlslMethod method in processedMethods) { @@ -91,15 +103,6 @@ internal static class HlslSourceSyntaxProcessor } writer.WriteLine(skipIfPresent: true); - - // Declared types - foreach (HlslUserType userType in typeDeclarations) - { - writer.WriteLine(skipIfPresent: true); - writer.WriteLine(userType.Definition); - } - - writer.WriteLine(skipIfPresent: true); } /// diff --git a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs index 2b83b52a..200498f4 100644 --- a/tests/ComputeSharp.Tests/ShaderCompilerTests.cs +++ b/tests/ComputeSharp.Tests/ShaderCompilerTests.cs @@ -992,19 +992,6 @@ namespace ComputeSharp.Tests struct ComputeSharp_Tests_ShaderCompilerTests_StructType1; struct ComputeSharp_Tests_ShaderCompilerTests_StructType2; - int InstanceMethodInShader(); - - static float StaticMethodInShader(float x); - - static float ComputeSharp_Tests_ShaderCompilerTests_StructType1_StaticMethod(int x); - - static float ComputeSharp_Tests_ShaderCompilerTests_StructType2_StaticMethod(int x); - - static const float Init = abs(__ComputeSharp_Tests_ShaderCompilerTests_ShaderWithAllSupportedMembers__PI); - static int Temp; - static int ComputeSharp_Tests_ShaderCompilerTests_ExternalContainerClass_Temp; - static const float ComputeSharp_Tests_ShaderCompilerTests_ExternalContainerClass_PI2 = 3.14 * 2; - struct ComputeSharp_Tests_ShaderCompilerTests_StructType1 { int X; @@ -1020,6 +1007,19 @@ namespace ComputeSharp.Tests float Combine(ComputeSharp_Tests_ShaderCompilerTests_StructType1 other); }; + int InstanceMethodInShader(); + + static float StaticMethodInShader(float x); + + static float ComputeSharp_Tests_ShaderCompilerTests_StructType1_StaticMethod(int x); + + static float ComputeSharp_Tests_ShaderCompilerTests_StructType2_StaticMethod(int x); + + static const float Init = abs(__ComputeSharp_Tests_ShaderCompilerTests_ShaderWithAllSupportedMembers__PI); + static int Temp; + static int ComputeSharp_Tests_ShaderCompilerTests_ExternalContainerClass_Temp; + static const float ComputeSharp_Tests_ShaderCompilerTests_ExternalContainerClass_PI2 = 3.14 * 2; + cbuffer _ : register(b0) { uint __x; From 5f429dfd38f3343c4d71b64b73648a99c6ec1375 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 18 Dec 2023 20:26:28 +0100 Subject: [PATCH 3/3] Add D2D tests for shader rewriting --- .../D2D1ReflectionServicesTests.cs | 4 +- .../ShaderRewriterTests.cs | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs index 99dc004d..6245c859 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs @@ -51,7 +51,7 @@ public partial class D2D1ReflectionServicesTests float4 value4 = __reserved__texture.Sample(__sampler____reserved__texture, float2(0, 0.5)); return input0 + input1 + input2 + value3 + value4; } - """.Replace("\r\n", "\n"), shaderInfo.HlslSource); + """, shaderInfo.HlslSource); CollectionAssert.AreEqual(D2D1PixelShader.LoadBytecode().ToArray(), shaderInfo.HlslBytecode.ToArray()); } @@ -119,7 +119,7 @@ public partial class D2D1ReflectionServicesTests { return (float4)(D2DGetInput(0) + (double4)amount); } - """.Replace("\r\n", "\n"), shaderInfo.HlslSource); + """, shaderInfo.HlslSource); CollectionAssert.AreEqual(D2D1PixelShader.LoadBytecode().ToArray(), shaderInfo.HlslBytecode.ToArray()); } diff --git a/tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs b/tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs new file mode 100644 index 00000000..5a19fc7e --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/ShaderRewriterTests.cs @@ -0,0 +1,76 @@ +using ComputeSharp.D2D1.Interop; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +#pragma warning disable CA1822 + +namespace ComputeSharp.D2D1.Tests; + +[TestClass] +public partial class ShaderRewriterTests +{ + // See https://github.com/Sergio0694/ComputeSharp/issues/725 + [TestMethod] + public void ShaderWithStructAndInstanceMethodUsingIt_IsRewrittenCorrectly() + { + D2D1ShaderInfo shaderInfo = D2D1ReflectionServices.GetShaderInfo(); + + Assert.AreEqual(""" + // ================================================ + // AUTO GENERATED + // ================================================ + // This shader was created by ComputeSharp. + // See: https://github.com/Sergio0694/ComputeSharp. + + #define D2D_INPUT_COUNT 0 + + #include "d2d1effecthelpers.hlsli" + + struct ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data + { + int value; + }; + + void UseData(inout ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data data); + + void UseData(inout ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data data) + { + ++data.value; + } + + D2D_PS_ENTRY(Execute) + { + ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data data = (ComputeSharp_D2D1_Tests_ShaderRewriterTests_ClassWithShader_ShaderWithStructAndInstanceMethodUsingIt_Data)0; + UseData(data); + return float4(data.value, data.value, data.value, data.value); + } + """, shaderInfo.HlslSource); + } + + internal sealed partial class ClassWithShader + { + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + internal readonly partial struct ShaderWithStructAndInstanceMethodUsingIt : ID2D1PixelShader + { + private struct Data + { + public int value; + } + + public float4 Execute() + { + Data data = default; + + UseData(ref data); + + return new float4(data.value, data.value, data.value, data.value); + } + + private void UseData(ref Data data) + { + ++data.value; + } + } + } +} \ No newline at end of file