Support primary field constructors in buffer marshaller

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-20 20:51:40 +01:00
1 親 ba9f421c97
コミット 041a5c0288
4ファイル変更
+90 -19

検索条件に一致するファイルはありません

@@ -308,13 +308,29 @@ partial class D2DPixelShaderDescriptorGenerator
}
writer.WriteLine();
writer.WriteLine($"""
/// <inheritdoc cref="{containingTypeName}.{pathPart.Name}"/>
/// <param name="shader">The input <see cref="{containingTypeName}"/> shader instance.</param>
/// <returns>A mutable reference to <see cref="{containingTypeName}.{pathPart.Name}"/>.</returns>
[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($"""
/// <inheritdoc cref="{containingTypeName}.{pathPart.Name}"/>
/// <param name="shader">The input <see cref="{containingTypeName}"/> value.</param>
/// <returns>A mutable reference to <see cref="{containingTypeName}.{pathPart.Name}"/>.</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field)]
private static extern ref {typeName} {pathPart.Name}(this ref readonly {containingTypeName} value);
""", isMultiline: true);
}
else
{
writer.WriteLine($"""
/// <summary>Gets a reference to the unspeakable field "{pathPart.Name}" of type <see cref="{containingTypeName}"/>.</summary>
/// <param name="shader">The input <see cref="{containingTypeName}"/> value.</param>
/// <returns>A mutable reference to the unspeakable field "{pathPart.Name}".</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{pathPart.UnspeakableName}")]
private static extern ref {typeName} {pathPart.Name}(this ref readonly {containingTypeName} value);
""", isMultiline: true);
}
}
}
}
@@ -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
/// </summary>
private static partial class ConstantBuffer
{
/// <summary>
/// A <see cref="Regex"/> to extract the parameter name of a primary constructor capture field.
/// </summary>
/// <remarks>
/// Temporary workaround until <see href="https://github.com/dotnet/roslyn/issues/70208"/> is available.
/// </remarks>
private static readonly Regex PrimaryConstructorNameRegex = new("^<([^>]+)>P$", RegexOptions.Compiled);
/// <summary>
/// Explores a given type hierarchy and generates statements to load fields.
/// </summary>
@@ -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<FieldPathPart> nestedFieldPath = fieldPath.Add(new FieldPathPart.Leaf(fieldName));
ImmutableArray<FieldPathPart> 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<FieldPathPart> 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
}
}
/// <summary>
/// Tries to get the field name and the accessor name for a given field.
/// </summary>
/// <param name="fieldSymbol">The field symbol to analyze.</param>
/// <param name="referencedName">The name of the field that's referenced in code by users.</param>
/// <param name="unspeakableName">The real name of the field that should be accessed, when different.</param>
/// <returns>Whether the field is supported and the necessary info could be retrieved.</returns>
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;
}
/// <summary>
/// Gets info on a given captured HLSL primitive type (either a scalar, a vector or a matrix).
/// </summary>
+8 -5
ファイルの表示
@@ -39,18 +39,21 @@ internal abstract record FieldInfo(EquatableArray<FieldPathPart> FieldPath, stri
/// A model for a part of a field path, ie. the name of the immediate parent field, and whether it's accessible.
/// </summary>
/// <param name="Name">The name of the immediate parent field to access (or self).</param>
internal abstract record FieldPathPart(string Name)
/// <param name="UnspeakableName">The unspeakable name of the field, if present.</param>
internal abstract record FieldPathPart(string Name, string? UnspeakableName)
{
/// <summary>
/// A model for a leaf part of a field path.
/// </summary>
/// <param name="Name"><inheritdoc cref="FieldPathPart(string, bool)" path="/param[@name='Name']/node()"/></param>
public sealed record Leaf(string Name) : FieldPathPart(Name);
/// <param name="Name"><inheritdoc cref="FieldPathPart(string, string?)" path="/param[@name='Name']/node()"/></param>
/// <param name="UnspeakableName"><inheritdoc cref="FieldPathPart(string, string?)" path="/param[@name='UnspeakableName']/node()"/></param>
public sealed record Leaf(string Name, string? UnspeakableName) : FieldPathPart(Name, UnspeakableName);
/// <summary>
/// A model for a nested field part path (ie. going through a nested struct type).
/// </summary>
/// <param name="Name"><inheritdoc cref="FieldPathPart(string, bool)" path="/param[@name='Name']/node()"/></param>
/// <param name="Name"><inheritdoc cref="FieldPathPart(string, string?)" path="/param[@name='Name']/node()"/></param>
/// <param name="UnspeakableName"><inheritdoc cref="FieldPathPart(string, string?)" path="/param[@name='UnspeakableName']/node()"/></param>
/// <param name="TypeName">The fully qualified type name of the containing type.</param>
public sealed record Nested(string Name, string TypeName) : FieldPathPart(Name);
public sealed record Nested(string Name, string? UnspeakableName, string TypeName) : FieldPathPart(Name, UnspeakableName);
}
@@ -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);
}
}
}