diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs index 412c10cb..a022351d 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs @@ -187,7 +187,8 @@ partial class D2DPixelShaderDescriptorGenerator case FieldInfo.Primitive primitive: // Read a nested primitive value - writer.WriteLine($"{GetFieldAccessExpression(fieldInfo)} = buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))};"); + writer.WriteFieldAccessExpression(fieldInfo); + writer.WriteLine($" = buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))};"); break; case FieldInfo.NonLinearMatrix matrix: @@ -198,7 +199,9 @@ partial class D2DPixelShaderDescriptorGenerator // Get a reference to the whole matrix field, just once for all rows writer.WriteLine(skipIfPresent: true); - writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {GetFieldAccessExpression(fieldInfo)};"); + writer.Write($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref "); + writer.WriteFieldAccessExpression(fieldInfo); + writer.WriteLine(";"); writer.WriteLine(); // Read all rows of a given matrix type @@ -237,7 +240,9 @@ partial class D2DPixelShaderDescriptorGenerator case FieldInfo.Primitive primitive: // Assign a primitive value - writer.WriteLine($"buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))} = {GetFieldAccessExpression(fieldInfo)};"); + writer.Write($"buffer.{string.Join("_", primitive.FieldPath.Select(static path => path.Name))} = "); + writer.WriteFieldAccessExpression(fieldInfo); + writer.WriteLine(";"); break; case FieldInfo.NonLinearMatrix matrix: @@ -248,7 +253,9 @@ partial class D2DPixelShaderDescriptorGenerator // Get a reference to the whole matrix field, just once for all rows writer.WriteLine(skipIfPresent: true); - writer.WriteLine($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref {GetFieldAccessExpression(fieldInfo)};"); + writer.Write($"ref {matrixHlslTypeName} __{fieldNamePrefix} = ref "); + writer.WriteFieldAccessExpression(fieldInfo); + writer.WriteLine(";"); writer.WriteLine(); // Assign all rows of a given matrix type @@ -306,7 +313,7 @@ partial class D2DPixelShaderDescriptorGenerator /// The input shader instance. /// A mutable reference to . [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{pathPart.Name}")] - private static extern ref {typeName} {pathPart.Name}(ref readonly {containingTypeName} shader); + private static extern ref {typeName} {pathPart.Name}(this ref readonly {containingTypeName} shader); """, isMultiline: true); } } @@ -316,23 +323,27 @@ partial class D2DPixelShaderDescriptorGenerator callbacks.Add(ConstantBufferCallback); callbacks.Add(ConstantBufferMarshallerCallback); } + } +} - /// - /// Gets an expression to access a given field. - /// - /// The input field to generate the expression for. - /// An expression to access a given field. - private static string GetFieldAccessExpression(FieldInfo fieldInfo) +/// +file static class IndentedTextWriterExtensions +{ + /// + /// Writes an expression to access a given field. + /// + /// The instance to write into. + /// The input field to generate the expression for. + public static void WriteFieldAccessExpression(this IndentedTextWriter writer, FieldInfo fieldInfo) + { + writer.Write("shader"); + + // Invoke all generated field accessors to get to the target field. + // Because they're extension methods, we can chain them sequentially. + // This simplifies the formatting and makes the code easier to read. + foreach (FieldPathPart part in fieldInfo.FieldPath) { - string fieldExpression = "shader"; - - // Invoke all generated field accessors to get to the target field - foreach (FieldPathPart part in fieldInfo.FieldPath) - { - fieldExpression = $"{part.Name}(in {fieldExpression})"; - } - - return fieldExpression; + writer.Write($".{part.Name}()"); } } } \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs index 384a9944..4f99267f 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1ReflectionServicesTests.cs @@ -64,6 +64,7 @@ public partial class D2D1ReflectionServicesTests [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader41)] [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] internal readonly partial struct ReflectedShader : ID2D1PixelShader { private readonly float2 offset; @@ -127,6 +128,7 @@ public partial class D2D1ReflectionServicesTests [D2DInputCount(1)] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] + [AutoConstructor] internal readonly partial struct ReflectedShaderWithDoubleOperations : ID2D1PixelShader { private readonly double amount;