From 83000839dd361b49d47dbce440f4ea770fbce3ff Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 18 Dec 2023 12:25:07 +0100 Subject: [PATCH 1/2] Handle shader fields of ComputeSharp.Bool type --- .../ConstantBufferSyntaxProcessor.Generation.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs index fdefa720..33ef9421 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/ConstantBufferSyntaxProcessor.Generation.cs @@ -81,13 +81,15 @@ partial class ConstantBufferSyntaxProcessor { switch (field) { - case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive: + case FieldInfo.Primitive { TypeName: "System.Boolean" or "ComputeSharp.Bool" } primitive: - // Append a field as a global::ComputeSharp.Bool value (will use the implicit conversion from bool values) + // Append a field as a Bool value for the native constant buffer layout. This is the case both for + // fields of type System.Boolean (they will use the implicit conversion from bool values), as well + // as for fields that were originally declared as Bool from the start (not recommended, but supported). writer.WriteFieldXmlSummary(field, fullyQualifiedTypeName); writer.WriteFieldXmlRemarks(field); writer.WriteLine($"""[FieldOffset({primitive.Offset})]"""); - writer.WriteLine($"""public global::ComputeSharp.Bool {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};"""); + writer.WriteLine($"""public Bool {string.Join("_", primitive.FieldPath.Select(static path => path.Name))};"""); break; case FieldInfo.Primitive primitive: @@ -266,6 +268,14 @@ partial class ConstantBufferSyntaxProcessor _ => HlslKnownTypes.GetMappedName(fieldInfo.TypeName) }; + // Special case: if the field was actually of type ComputeSharp.Bool, and not System.Boolean, + // we need to preserve the original type name, and we cannot use the mapped type name (bool), + // as it would cause the generated field accessor to fail (the field type would not match). + if (fieldInfo.TypeName == "ComputeSharp.Bool") + { + typeName = "Bool"; + } + // Get the friendly type name for the field. If this nested field is the // first path, then the parent is the shader type itself. Otherwise, the // container path is the type of the previous containing type of the field. From 1349c881456cdb7f62e2a97534503e6588bf288f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 18 Dec 2023 20:59:16 +0100 Subject: [PATCH 2/2] Add unit tests for shader ComputeSharp.Bool fields --- .../D2D1PixelShaderTests.cs | 29 +++++++++++++++++++ .../ComputeSharp.Tests/ShaderMembersTests.cs | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index b678afca..ab7e00ea 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -1232,5 +1232,34 @@ namespace ComputeSharp.D2D1.Tests // Just a sanity check that the generated code is present Assert.AreEqual(0, D2D1PixelShader.GetInputCount()); } + + // https://github.com/Sergio0694/ComputeSharp/issues/727 + [TestMethod] + public unsafe void ShaderWithComputeSharpBoolInstanceField_IsMarshalledCorrectly() + { + Assert.AreEqual(4, D2D1PixelShader.GetConstantBufferSize()); + + ShaderWithComputeSharpBoolInstanceField shader = new(true); + + ReadOnlyMemory memory = D2D1PixelShader.GetConstantBuffer(in shader); + + ShaderWithComputeSharpBoolInstanceField roundTrip = D2D1PixelShader.CreateFromConstantBuffer(memory.Span); + + Assert.IsTrue(shader.value); + } + + [D2DInputCount(0)] + [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] + [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] + internal readonly partial struct ShaderWithComputeSharpBoolInstanceField : ID2D1PixelShader + { + public readonly Bool value; + + public float4 Execute() + { + return Hlsl.BoolToFloat(this.value); + } + } } } \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/ShaderMembersTests.cs b/tests/ComputeSharp.Tests/ShaderMembersTests.cs index 85305bd5..21f9a00c 100644 --- a/tests/ComputeSharp.Tests/ShaderMembersTests.cs +++ b/tests/ComputeSharp.Tests/ShaderMembersTests.cs @@ -122,7 +122,7 @@ public partial class ShaderMembersTests public readonly bool g; public readonly bool2 h; public readonly int i; - public readonly bool j; + public readonly ComputeSharp.Bool j; // See https://github.com/Sergio0694/ComputeSharp/issues/727 public void Execute() {