Fix generated field accessors for resource fields

このコミットが含まれているのは:
Sergio Pedri committed 2023-11-20 22:07:18 +01:00
1 親 5811ba60cf
コミット cf8969a77e
4ファイル変更
+41 -8

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

+2 -3
ファイルの表示
@@ -75,7 +75,6 @@ partial class ComputeShaderDescriptorGenerator
// Define the FromManaged method (managed shader type to native constant buffer)
writer.WriteLine($$"""/// <inheritdoc cref="IComputeShaderDescriptor{T}.LoadGraphicsResources"/>""");
writer.WriteLine($"""[MethodImpl(MethodImplOptions.AggressiveInlining)]""");
writer.WriteLine($"""[SkipLocalsInit]""");
writer.WriteLine($"public static void LoadGraphicsResources<TLoader>(in {fullyQualifiedTypeName} shader, ref TLoader loader)");
writer.WriteLine(" where TLoader : struct, IGraphicsResourceLoader");
@@ -102,7 +101,7 @@ partial class ComputeShaderDescriptorGenerator
/// <param name="shader">The input <see cref="{fullyQualifiedTypeName}"/> value.</param>
/// <returns>A reference to <see cref="{fullyQualifiedTypeName}.{resourceInfo.FieldName}"/>.</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field)]
private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value);
private static extern ref readonly {resourceInfo.FieldType} {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value);
""", isMultiline: true);
}
else
@@ -112,7 +111,7 @@ partial class ComputeShaderDescriptorGenerator
/// <param name="shader">The input <see cref="{fullyQualifiedTypeName}"/> value.</param>
/// <returns>A reference to the unspeakable field "{resourceInfo.FieldName}".</returns>
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "{resourceInfo.UnspeakableName}")]
private static extern ref readonly IGraphicsResource {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value);
private static extern ref readonly {resourceInfo.FieldType} {resourceInfo.FieldName}(this ref readonly {fullyQualifiedTypeName} value);
""", isMultiline: true);
}
}
+36 -4
ファイルの表示
@@ -54,13 +54,13 @@ partial class ComputeShaderDescriptorGenerator
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
// Only process instance fields
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 } typeSymbol, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } fieldSymbol)
{
continue;
}
// Skip fields of not accessible types (the analyzer will handle this just like other fields)
if (!fieldSymbol.Type.IsAccessibleFromCompilationAssembly(compilation))
if (!typeSymbol.IsAccessibleFromCompilationAssembly(compilation))
{
continue;
}
@@ -71,12 +71,12 @@ partial class ComputeShaderDescriptorGenerator
continue;
}
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
string typeName = typeSymbol.GetFullyQualifiedMetadataName();
// Check if the field is a resource (note: resources can only be top level fields)
if (HlslKnownTypes.IsTypedResourceType(typeName))
{
resourceBuilder.Add(new ResourceInfo(fieldName, unspeakableName));
resourceBuilder.Add(new ResourceInfo(fieldName, GetResourceTypeName(typeSymbol), unspeakableName));
// Populate the resource descriptors as well
if (HlslKnownTypes.IsConstantBufferType(typeName))
@@ -110,5 +110,37 @@ partial class ComputeShaderDescriptorGenerator
diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
}
}
/// <summary>
/// Gets the name of a given resource field type.
/// </summary>
/// <param name="typeSymbol">The input field type to inspect.</param>
/// <returns>The fully qualified resource type name, nicely formatted.</returns>
private static string GetResourceTypeName(INamedTypeSymbol typeSymbol)
{
using ImmutableArrayBuilder<char> builder = new();
// Add the type name (we'll add the ComputeSharp namespace, so no need for fully qualified type names)
builder.AddRange(typeSymbol.Name.AsSpan());
builder.Add('<');
_ = HlslKnownTypes.TryGetMappedName(typeSymbol.TypeArguments[0].GetFullyQualifiedMetadataName(), out string? mappedName);
// We always have at least one type argument, so first append it. We either append the mapped
// type name (ie. the friendly primitive or HLSL type name), or the fully qualified type name.
builder.AddRange((mappedName ?? typeSymbol.TypeArguments[0].GetFullyQualifiedName(includeGlobal: true)).AsSpan());
// If the resource also has a pixel type, append that too.
// We only need the name again, as the namespace is imported.
if (typeSymbol.TypeArguments is [_, INamedTypeSymbol pixelTypeSymbol])
{
builder.AddRange(", ".AsSpan());
builder.AddRange(pixelTypeSymbol.Name.AsSpan());
}
builder.Add('>');
return builder.ToString();
}
}
}
+2 -1
ファイルの表示
@@ -4,5 +4,6 @@ namespace ComputeSharp.SourceGenerators.Models;
/// A model describing a captured resource (either a buffer or a texture).
/// </summary>
/// <param name="FieldName">The name of the resource field.</param>
/// <param name="FieldType">The fully qualified type name of the field.</param>
/// <param name="UnspeakableName">The unspeakable name of the field, if present.</param>
public sealed record ResourceInfo(string FieldName, string? UnspeakableName);
public sealed record ResourceInfo(string FieldName, string FieldType, string? UnspeakableName);
+1
ファイルの表示
@@ -5,6 +5,7 @@ namespace ComputeSharp;
/// </summary>
/// <typeparam name="T">The pixel type, when stored in memory (either on the CPU or the GPU side).</typeparam>
/// <typeparam name="TPixel">The type of pixel when normalized and used within a shader.</typeparam>
/// <remarks>This interface is not meant to be implemented by user defined types.</remarks>
public interface IPixel<T, TPixel>
where T : unmanaged, IPixel<T, TPixel>
where TPixel : unmanaged