From 940cdfdcdcae94608bb40afc1b4b7919a0998724 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 21 Oct 2023 12:20:10 +0200 Subject: [PATCH] Fix handling empty shaders with just artificial fields --- ...tBufferSyntaxProcessor.ArtificialFields.cs | 11 ++++++++++ ...onstantBufferSyntaxProcessor.Generation.cs | 19 +++++++++++++--- ...haderDescriptorGenerator.ConstantBuffer.cs | 22 ++++++++++--------- ...tBufferSyntaxProcessor.ArtificialFields.cs | 7 ++++++ 4 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/ComputeSharp.D2D1.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs new file mode 100644 index 00000000..51678124 --- /dev/null +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs @@ -0,0 +1,11 @@ +namespace ComputeSharp.SourceGeneration.SyntaxProcessors; + +/// +partial class ConstantBufferSyntaxProcessor +{ + /// + private static partial bool HasArtificialFields() + { + return false; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs index 9a26260e..b73a4da4 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs @@ -42,8 +42,8 @@ partial class ConstantBufferSyntaxProcessor ConstantBufferSyntaxProcessor.generatorName = generatorName; ConstantBufferSyntaxProcessor.direction = direction; - // If there are no fields, there is no need for a constant buffer type - if (info.Fields.IsEmpty) + // If there are no fields (including artificial ones), there is no need for a constant buffer type + if (info.Fields.IsEmpty && !HasArtificialFields()) { return; } @@ -307,9 +307,22 @@ partial class ConstantBufferSyntaxProcessor } callbacks.Add(ConstantBufferCallback); - callbacks.Add(ConstantBufferMarshallerCallback); + + // Only emit the marshaller type if there are any non artificial fields. + // Otherwise, it's not needed, as there are either just no fields at all, + // or only artificial ones, which would be assigned directly by callers. + if (!info.Fields.IsEmpty) + { + callbacks.Add(ConstantBufferMarshallerCallback); + } } + /// + /// Checks whether any artificial fields are present. + /// + /// Whether any artificial fields are present. + private static partial bool HasArtificialFields(); + /// /// Appends any artificial fields to the generated constant buffer type. /// diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs index 670a07c8..8ec40690 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.ConstantBuffer.cs @@ -63,23 +63,25 @@ partial class ComputeShaderDescriptorGenerator using (writer.WriteBlock()) { - // If there are no fields, just load an empty buffer + // If there are no fields (ie. there are only artificial ones), just get an uninitialized instance. + // All fields (ie. the thread size values) will be assigned directly inline, with no marshalling. if (info.Fields.IsEmpty) { - writer.WriteLine("loader.LoadConstantBuffer(default);"); + writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.SkipInit(out global::ComputeSharp.Generated.ConstantBuffer buffer);"); } else { - // Otherwise, pass a span into the marshalled native layout buffer. - // Also load the artificially created fields with dispatch data. + // Otherwise, let the generated marshaller create an initialized instance of the constant buffer type writer.WriteLine("global::ComputeSharp.Generated.ConstantBufferMarshaller.FromManaged(in shader, out global::ComputeSharp.Generated.ConstantBuffer buffer);"); - writer.WriteLine(); - writer.WriteLine("buffer.__x = x;"); - writer.WriteLine("buffer.__y = y;"); - writer.WriteLineIf(!info.IsPixelShaderLike, "buffer.__z = z;"); - writer.WriteLineIf(!info.IsPixelShaderLike); - writer.WriteLine("loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.Generated.ConstantBuffer)));"); } + + // Initialize the artificial fields and finally pass a span over the constant buffer + writer.WriteLine(); + writer.WriteLine("buffer.__x = x;"); + writer.WriteLine("buffer.__y = y;"); + writer.WriteLineIf(!info.IsPixelShaderLike, "buffer.__z = z;"); + writer.WriteLineIf(!info.IsPixelShaderLike); + writer.WriteLine("loader.LoadConstantBuffer(new global::System.ReadOnlySpan(&buffer, sizeof(global::ComputeSharp.Generated.ConstantBuffer)));"); } } } diff --git a/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs b/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs index fbe5c168..0b87fada 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxProcessors/ConstantBufferSyntaxProcessor.ArtificialFields.cs @@ -7,6 +7,12 @@ namespace ComputeSharp.SourceGeneration.SyntaxProcessors; /// partial class ConstantBufferSyntaxProcessor { + /// + private static partial bool HasArtificialFields() + { + return true; + } + /// static partial void AppendArtificialFields(IConstantBufferInfo info, IndentedTextWriter writer) { @@ -22,6 +28,7 @@ partial class ConstantBufferSyntaxProcessor """, isMultiline: true); // The Z axis is only present for non pixel-shader-like compute shaders + writer.WriteLineIf(!((ShaderInfo)info).IsPixelShaderLike); writer.WriteLineIf( condition: !((ShaderInfo)info).IsPixelShaderLike, """