diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs
index a022351d..be7d8692 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.Syntax.cs
@@ -308,13 +308,29 @@ partial class D2DPixelShaderDescriptorGenerator
}
writer.WriteLine();
- writer.WriteLine($"""
- ///
- /// The input shader instance.
- /// A mutable reference to .
- [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{pathPart.Name}")]
- private static extern ref {typeName} {pathPart.Name}(this ref readonly {containingTypeName} shader);
- """, isMultiline: true);
+
+ // If there is no unspeakable name, we can reference the field directly. This means we can both
+ // use that in XML docs to refer to it (and inherit the docs), and we don't need an explicit name.
+ if (pathPart.UnspeakableName is null)
+ {
+ writer.WriteLine($"""
+ ///
+ /// The input value.
+ /// A mutable reference to .
+ [UnsafeAccessor(UnsafeAccessorKind.Field)]
+ private static extern ref {typeName} {pathPart.Name}(this ref readonly {containingTypeName} value);
+ """, isMultiline: true);
+ }
+ else
+ {
+ writer.WriteLine($"""
+ /// Gets a reference to the unspeakable field "{pathPart.Name}" of type .
+ /// The input value.
+ /// A mutable reference to the unspeakable field "{pathPart.Name}".
+ [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{pathPart.UnspeakableName}")]
+ private static extern ref {typeName} {pathPart.Name}(this ref readonly {containingTypeName} value);
+ """, isMultiline: true);
+ }
}
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs
index 18bb162f..624449c6 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.ConstantBuffer.cs
@@ -1,5 +1,7 @@
using System;
using System.Collections.Immutable;
+using System.Diagnostics.CodeAnalysis;
+using System.Text.RegularExpressions;
using ComputeSharp.Core.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
@@ -18,6 +20,14 @@ partial class D2DPixelShaderDescriptorGenerator
///
private static partial class ConstantBuffer
{
+ ///
+ /// A to extract the parameter name of a primary constructor capture field.
+ ///
+ ///
+ /// Temporary workaround until is available.
+ ///
+ private static readonly Regex PrimaryConstructorNameRegex = new("^<([^>]+)>P$", RegexOptions.Compiled);
+
///
/// Explores a given type hierarchy and generates statements to load fields.
///
@@ -46,7 +56,7 @@ partial class D2DPixelShaderDescriptorGenerator
foreach (ISymbol memberSymbol in currentTypeSymbol.GetMembers())
{
// Only process fields of a valid shape/type
- if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } fieldSymbol)
+ if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } fieldSymbol)
{
continue;
}
@@ -57,7 +67,12 @@ partial class D2DPixelShaderDescriptorGenerator
continue;
}
- string fieldName = fieldSymbol.Name;
+ // Try to get the name to use for the field and the accessor
+ if (!TryGetFieldAccessorName(fieldSymbol, out string? fieldName, out string? unspeakableName))
+ {
+ continue;
+ }
+
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
// The first item in each nested struct needs to be aligned to 16 bytes
@@ -70,17 +85,17 @@ partial class D2DPixelShaderDescriptorGenerator
if (HlslKnownTypes.IsKnownHlslType(typeName))
{
- ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName));
+ ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName, unspeakableName));
fields.Add(GetHlslKnownTypeFieldInfo(nestedFieldPath, typeName, ref rawDataOffset));
}
else if (fieldSymbol.Type.IsUnmanagedType)
{
string nestedTypeName = fieldSymbol.Type.GetFullyQualifiedName(includeGlobal: true);
- ImmutableArray nestedFieldPath = fieldPath.Add(new FieldPathPart.Nested(fieldName, nestedTypeName));
+ FieldPathPart fieldPathPart = new FieldPathPart.Nested(fieldName, unspeakableName, nestedTypeName);
// Custom struct type defined by the user
- GetInfo(compilation, fieldSymbol.Type, nestedFieldPath, ref rawDataOffset, fields);
+ GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(fieldPathPart), ref rawDataOffset, fields);
}
}
}
@@ -113,6 +128,43 @@ partial class D2DPixelShaderDescriptorGenerator
}
}
+ ///
+ /// Tries to get the field name and the accessor name for a given field.
+ ///
+ /// The field symbol to analyze.
+ /// The name of the field that's referenced in code by users.
+ /// The real name of the field that should be accessed, when different.
+ /// Whether the field is supported and the necessary info could be retrieved.
+ private static bool TryGetFieldAccessorName(
+ IFieldSymbol fieldSymbol,
+ [NotNullWhen(true)] out string? referencedName,
+ out string? unspeakableName)
+ {
+ // If the field can be referenced by name, we can just access it directly
+ if (fieldSymbol.CanBeReferencedByName)
+ {
+ referencedName = fieldSymbol.Name;
+ unspeakableName = null;
+
+ return true;
+ }
+
+ // If the field is a primary constructor capture field, get the original name
+ if (PrimaryConstructorNameRegex.Match(fieldSymbol.Name) is { Success: true, Groups: [_, { Value: string parameterName }] })
+ {
+ referencedName = parameterName;
+ unspeakableName = fieldSymbol.Name;
+
+ return true;
+ }
+
+ // The field is not recognized, so just invalid
+ referencedName = null;
+ unspeakableName = null;
+
+ return false;
+ }
+
///
/// Gets info on a given captured HLSL primitive type (either a scalar, a vector or a matrix).
///
diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs
index dfecf3af..0463030c 100644
--- a/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs
+++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/FieldInfo.cs
@@ -39,18 +39,21 @@ internal abstract record FieldInfo(EquatableArray FieldPath, stri
/// A model for a part of a field path, ie. the name of the immediate parent field, and whether it's accessible.
///
/// The name of the immediate parent field to access (or self).
-internal abstract record FieldPathPart(string Name)
+/// The unspeakable name of the field, if present.
+internal abstract record FieldPathPart(string Name, string? UnspeakableName)
{
///
/// A model for a leaf part of a field path.
///
- ///
- public sealed record Leaf(string Name) : FieldPathPart(Name);
+ ///
+ ///
+ public sealed record Leaf(string Name, string? UnspeakableName) : FieldPathPart(Name, UnspeakableName);
///
/// A model for a nested field part path (ie. going through a nested struct type).
///
- ///
+ ///
+ ///
/// The fully qualified type name of the containing type.
- public sealed record Nested(string Name, string TypeName) : FieldPathPart(Name);
+ public sealed record Nested(string Name, string? UnspeakableName, string TypeName) : FieldPathPart(Name, UnspeakableName);
}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs
index 718d790a..01105349 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.cs
@@ -83,12 +83,12 @@ partial class ComputeShaderDescriptorGenerator
else if (HlslKnownTypes.IsKnownHlslType(typeName))
{
// TODO: share the logic and fix the field path parts
- fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(new FieldPathPart.Leaf(fieldName)), typeName, ref rawDataOffset));
+ fields.Add(GetHlslKnownTypeFieldInfo(fieldPath.Add(new FieldPathPart.Leaf(fieldName, null)), typeName, ref rawDataOffset));
}
else if (fieldSymbol.Type.IsUnmanagedType)
{
// Custom struct type defined by the user
- GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(new FieldPathPart.Leaf(fieldName)), ref resourceOffset, ref rawDataOffset, fields, resources);
+ GetInfo(compilation, fieldSymbol.Type, fieldPath.Add(new FieldPathPart.Leaf(fieldName, null)), ref resourceOffset, ref rawDataOffset, fields, resources);
}
}
}