Merge pull request #693 from Sergio0694/user/sergiopedri/partial-shader-declarations

Handle partial members in shader types
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2023-11-29 23:14:53 +01:00
コミット a0c4f53326
3ファイル変更
+211 -123

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

+59 -59
ファイルの表示
@@ -62,8 +62,8 @@ partial class D2DPixelShaderDescriptorGenerator
// Explore the syntax tree and extract the processed info
SemanticModelProvider semanticModelProvider = new(compilation);
(string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, out bool methodsNeedD2D1RequiresScenePosition);
ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions, out bool fieldsNeedD2D1RequiresScenePosition);
(string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods) = GetProcessedMethods(diagnostics, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, out bool methodsNeedD2D1RequiresScenePosition);
ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclarationSymbol, discoveredTypes, constantDefinitions, out bool fieldsNeedD2D1RequiresScenePosition);
// Process the discovered types and constants
ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods);
@@ -190,7 +190,6 @@ partial class D2DPixelShaderDescriptorGenerator
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the type to process.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the current type.</param>
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
@@ -199,7 +198,6 @@ partial class D2DPixelShaderDescriptorGenerator
private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
SemanticModelProvider semanticModel,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions,
@@ -209,46 +207,47 @@ partial class D2DPixelShaderDescriptorGenerator
needsD2D1RequiresScenePosition = false;
foreach (FieldDeclarationSyntax fieldDeclaration in structDeclaration.Members.OfType<FieldDeclarationSyntax>())
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
foreach (VariableDeclaratorSyntax variableDeclarator in fieldDeclaration.Declaration.Variables)
// Find all declared static fields in the type
if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, DeclaringSyntaxReferences: [SyntaxReference fieldReference, ..] } fieldSymbol)
{
IFieldSymbol fieldSymbol = (IFieldSymbol)semanticModel.For(variableDeclarator).GetDeclaredSymbol(variableDeclarator)!;
if (!fieldSymbol.IsStatic || fieldSymbol.IsConst)
{
continue;
}
// Constant properties must be of a primitive, vector or matrix type
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol ||
!HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName()))
{
diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
string typeDeclaration = fieldSymbol.IsReadOnly switch
{
true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}",
false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}"
};
StaticFieldRewriter staticFieldRewriter = new(
semanticModel,
discoveredTypes,
constantDefinitions,
diagnostics);
string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString();
needsD2D1RequiresScenePosition |= staticFieldRewriter.NeedsD2DRequiresScenePositionAttribute;
builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment));
continue;
}
if (fieldReference.GetSyntax() is not VariableDeclaratorSyntax variableDeclarator)
{
continue;
}
// Constant properties must be of a primitive, vector or matrix type
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol ||
!HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName()))
{
diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
string typeDeclaration = fieldSymbol.IsReadOnly switch
{
true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}",
false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}"
};
StaticFieldRewriter staticFieldRewriter = new(
semanticModel,
discoveredTypes,
constantDefinitions,
diagnostics);
string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString();
needsD2D1RequiresScenePosition |= staticFieldRewriter.NeedsD2DRequiresScenePositionAttribute;
builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment));
}
return builder.ToImmutable();
@@ -259,17 +258,15 @@ partial class D2DPixelShaderDescriptorGenerator
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the current type.</param>
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the type to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="needsD2D1RequiresScenePosition">Whether or not the shader needs the <c>[D2DRequiresScenePosition]</c> annotation.</param>
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
/// <returns>A sequence of processed methods in <paramref name="structDeclarationSymbol"/>, and the entry point.</returns>
private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods) GetProcessedMethods(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
@@ -278,29 +275,32 @@ partial class D2DPixelShaderDescriptorGenerator
IDictionary<IFieldSymbol, string> constantDefinitions,
out bool needsD2D1RequiresScenePosition)
{
// Find all declared methods in the type
ImmutableArray<MethodDeclarationSyntax> methodDeclarations = (
from syntaxNode in structDeclaration.DescendantNodes()
where syntaxNode.IsKind(SyntaxKind.MethodDeclaration)
select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray();
string? entryPoint = null;
using ImmutableArrayBuilder<(string, string)> methods = new();
string? entryPoint = null;
needsD2D1RequiresScenePosition = false;
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!;
// Find all declared methods in the type
if (memberSymbol is not IMethodSymbol { IsImplicitlyDeclared: false, DeclaringSyntaxReferences: [SyntaxReference methodReference, ..] } methodSymbol)
{
continue;
}
if (methodReference.GetSyntax() is not MethodDeclarationSyntax methodDeclaration)
{
continue;
}
bool isShaderEntryPoint =
methodDeclarationSymbol.Name == "Execute" &&
methodDeclarationSymbol.ReturnType.HasFullyQualifiedMetadataName("ComputeSharp.Float4") &&
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0;
methodSymbol.Name == "Execute" &&
methodSymbol.ReturnType.HasFullyQualifiedMetadataName("ComputeSharp.Float4") &&
methodSymbol.TypeParameters.Length == 0 &&
methodSymbol.Parameters.Length == 0;
// Except for the entry point, ignore explicit interface implementations
if (!isShaderEntryPoint && !methodDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
if (!isShaderEntryPoint && !methodSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
{
continue;
}
+65 -64
ファイルの表示
@@ -71,9 +71,9 @@ partial class ComputeShaderDescriptorGenerator
string? implicitTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!);
(ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, ImmutableArray<(string Name, string HlslType)> valueFields) = GetInstanceFields(diagnostics, structDeclarationSymbol, discoveredTypes, isComputeShader);
ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers = GetSharedBuffers(diagnostics, structDeclarationSymbol, discoveredTypes);
(string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader);
(string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods(diagnostics, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, instanceMethods, constantDefinitions, isComputeShader);
(string, string)? implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?);
ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields(diagnostics, semanticModelProvider, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Process the discovered types and constants
ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes(diagnostics, structDeclarationSymbol, discoveredTypes, instanceMethods);
@@ -204,7 +204,6 @@ partial class ComputeShaderDescriptorGenerator
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the type to process.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the current type.</param>
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
@@ -212,56 +211,56 @@ partial class ComputeShaderDescriptorGenerator
private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
SemanticModelProvider semanticModel,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions)
{
using ImmutableArrayBuilder<(string, string, string?)> builder = new();
foreach (FieldDeclarationSyntax fieldDeclaration in structDeclaration.Members.OfType<FieldDeclarationSyntax>())
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
foreach (VariableDeclaratorSyntax variableDeclarator in fieldDeclaration.Declaration.Variables)
// Find all declared static fields in the type
if (memberSymbol is not IFieldSymbol { IsImplicitlyDeclared: false, IsStatic: true, IsConst: false, DeclaringSyntaxReferences: [SyntaxReference fieldReference, ..] } fieldSymbol)
{
IFieldSymbol fieldSymbol = (IFieldSymbol)semanticModel.For(variableDeclarator).GetDeclaredSymbol(variableDeclarator)!;
if (!fieldSymbol.IsStatic || fieldSymbol.IsConst)
{
continue;
}
if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.GroupSharedAttribute", out _))
{
continue;
}
// Constant properties must be of a primitive, vector or matrix type
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol ||
!HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName()))
{
diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
string typeDeclaration = fieldSymbol.IsReadOnly switch
{
true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}",
false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}"
};
StaticFieldRewriter staticFieldRewriter = new(
semanticModel,
discoveredTypes,
constantDefinitions,
diagnostics);
string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString();
builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment));
continue;
}
if (fieldReference.GetSyntax() is not VariableDeclaratorSyntax variableDeclarator)
{
continue;
}
if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.GroupSharedAttribute", out _))
{
continue;
}
// Constant properties must be of a primitive, vector or matrix type
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol ||
!HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullyQualifiedMetadataName()))
{
diagnostics.Add(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
continue;
}
_ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
string typeDeclaration = fieldSymbol.IsReadOnly switch
{
true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}",
false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}"
};
StaticFieldRewriter staticFieldRewriter = new(
semanticModel,
discoveredTypes,
constantDefinitions,
diagnostics);
string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace(eol: "\n").ToFullString();
builder.Add((mapping ?? fieldSymbol.Name, typeDeclaration, assignment));
}
return builder.ToImmutable();
@@ -313,17 +312,15 @@ partial class ComputeShaderDescriptorGenerator
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="structDeclarationSymbol">The type symbol for the shader type.</param>
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> instance for the current type.</param>
/// <param name="semanticModel">The <see cref="SemanticModelProvider"/> instance for the type to process.</param>
/// <param name="discoveredTypes">The collection of currently discovered types.</param>
/// <param name="staticMethods">The set of discovered and processed static methods.</param>
/// <param name="instanceMethods">The collection of discovered instance methods for custom struct types.</param>
/// <param name="constantDefinitions">The collection of discovered constant definitions.</param>
/// <param name="isComputeShader">Indicates whether or not <paramref name="structDeclarationSymbol"/> represents a compute shader.</param>
/// <returns>A sequence of processed methods in <paramref name="structDeclaration"/>, and the entry point.</returns>
/// <returns>A sequence of processed methods in <paramref name="structDeclarationSymbol"/>, and the entry point.</returns>
private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods, bool IsSamplerUser) GetProcessedMethods(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
SemanticModelProvider semanticModel,
ICollection<INamedTypeSymbol> discoveredTypes,
@@ -332,34 +329,38 @@ partial class ComputeShaderDescriptorGenerator
IDictionary<IFieldSymbol, string> constantDefinitions,
bool isComputeShader)
{
// Find all declared methods in the type
ImmutableArray<MethodDeclarationSyntax> methodDeclarations = (
from syntaxNode in structDeclaration.DescendantNodes()
where syntaxNode.IsKind(SyntaxKind.MethodDeclaration)
select (MethodDeclarationSyntax)syntaxNode).ToImmutableArray();
using ImmutableArrayBuilder<(string, string)> methods = new();
string? entryPoint = null;
bool isSamplerUsed = false;
foreach (MethodDeclarationSyntax methodDeclaration in methodDeclarations)
foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers())
{
IMethodSymbol methodDeclarationSymbol = semanticModel.For(methodDeclaration).GetDeclaredSymbol(methodDeclaration)!;
// Find all declared methods in the type
if (memberSymbol is not IMethodSymbol { IsImplicitlyDeclared: false, DeclaringSyntaxReferences: [SyntaxReference methodReference, ..] } methodSymbol)
{
continue;
}
if (methodReference.GetSyntax() is not MethodDeclarationSyntax methodDeclaration)
{
continue;
}
bool isShaderEntryPoint =
(isComputeShader &&
methodDeclarationSymbol.Name == nameof(IComputeShader.Execute) &&
methodDeclarationSymbol.ReturnsVoid &&
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0) ||
methodSymbol.Name == nameof(IComputeShader.Execute) &&
methodSymbol.ReturnsVoid &&
methodSymbol.TypeParameters.Length == 0 &&
methodSymbol.Parameters.Length == 0) ||
(!isComputeShader &&
methodDeclarationSymbol.Name == nameof(IComputeShader<byte>.Execute) &&
methodDeclarationSymbol.ReturnType is not null && // TODO: match for pixel type
methodDeclarationSymbol.TypeParameters.Length == 0 &&
methodDeclarationSymbol.Parameters.Length == 0);
methodSymbol.Name == nameof(IComputeShader<byte>.Execute) &&
methodSymbol.ReturnType is not null && // TODO: match for pixel type
methodSymbol.TypeParameters.Length == 0 &&
methodSymbol.Parameters.Length == 0);
// Except for the entry point, ignore explicit interface implementations
if (!isShaderEntryPoint && !methodDeclarationSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
if (!isShaderEntryPoint && !methodSymbol.ExplicitInterfaceImplementations.IsDefaultOrEmpty)
{
continue;
}
+87
ファイルの表示
@@ -787,6 +787,93 @@ namespace ComputeSharp.Tests
{
}
}
[TestMethod]
public void ShaderWithPartialDeclarations_IsCombinedCorrectly()
{
ShaderInfo info = ReflectionServices.GetShaderInfo<ShaderWithPartialDeclarations>();
Assert.AreEqual(
"""
// ================================================
// AUTO GENERATED
// ================================================
// This shader was created by ComputeSharp.
// See: https://github.com/Sergio0694/ComputeSharp.
#define __GroupSize__get_X 64
#define __GroupSize__get_Y 1
#define __GroupSize__get_Z 1
#define __ComputeSharp_Tests_ShaderCompilerTests_ShaderWithPartialDeclarations__a 2
static const int b = 4;
cbuffer _ : register(b0)
{
uint __x;
uint __y;
uint __z;
}
RWStructuredBuffer<float> __reserved__buffer : register(u0);
static int Sum(int x, int y);
static int Sum(int x, int y)
{
return x + y;
}
[NumThreads(__GroupSize__get_X, __GroupSize__get_Y, __GroupSize__get_Z)]
void Execute(uint3 ThreadIds : SV_DispatchThreadID)
{
if (ThreadIds.x < __x && ThreadIds.y < __y && ThreadIds.z < __z)
{
__reserved__buffer[ThreadIds.x] = Sum(__ComputeSharp_Tests_ShaderCompilerTests_ShaderWithPartialDeclarations__a, b);
}
}
""",
info.HlslSource);
}
[AutoConstructor]
[ThreadGroupSize(DefaultThreadGroupSizes.X)]
[GeneratedComputeShaderDescriptor]
internal readonly partial struct ShaderWithPartialDeclarations : IComputeShader
{
}
partial struct ShaderWithPartialDeclarations
{
public readonly ReadWriteBuffer<float> buffer;
}
partial struct ShaderWithPartialDeclarations
{
/// <inheritdoc/>
public void Execute()
{
buffer[ThreadIds.X] = Sum(a, b);
}
}
partial struct ShaderWithPartialDeclarations
{
private const int a = 2;
}
partial struct ShaderWithPartialDeclarations
{
private static readonly int b = 4;
}
partial struct ShaderWithPartialDeclarations
{
private static int Sum(int x, int y)
{
return x + y;
}
}
}
}