From 2cb61a3eeca017e1d387f7e997f16cfed26c6690 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 18 Dec 2023 20:25:59 +0100 Subject: [PATCH] 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;