Enabled rewriting for shader static constants

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-07 20:14:32 +01:00
1 親 c14c64dbc7
コミット f8e951dd15
5ファイル変更
+119 -20

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

+69 -4
ファイルの表示
@@ -71,12 +71,13 @@ namespace ComputeSharp.SourceGenerators
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
var processedMembers = GetProcessedFields(context, structDeclarationSymbol, discoveredTypes).ToArray();
var sharedBuffers = GetGroupSharedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(context, structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
var processedMethods = localFunctions.Concat(staticMethods.Values).Select(static method => method.NormalizeWhitespace().ToFullString()).ToArray();
var processedConstants = GetProcessedConstants(constantDefinitions);
var staticConstants = GetConstantProperties(context, semanticModel, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Create the compilation unit with the source attribute
var source =
@@ -90,6 +91,7 @@ namespace ComputeSharp.SourceGenerators
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))),
AttributeArgument(ArrayExpression(processedMethods)),
AttributeArgument(NestedArrayExpression(processedConstants)),
AttributeArgument(NestedArrayExpression(staticConstants)),
AttributeArgument(NestedArrayExpression(sharedBuffers)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
@@ -101,14 +103,14 @@ namespace ComputeSharp.SourceGenerators
}
/// <summary>
/// Gets a sequence of captured members and their mapped names.
/// Gets a sequence of captured fields and their mapped names.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="types">The collection of currently discovered types.</param>
/// <returns>A sequence of captured members in <paramref name="structDeclarationSymbol"/>.</returns>
/// <returns>A sequence of captured fields in <paramref name="structDeclarationSymbol"/>.</returns>
[Pure]
private static IEnumerable<IEnumerable<string>> GetProcessedMembers(
private static IEnumerable<IEnumerable<string>> GetProcessedFields(
GeneratorExecutionContext context,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> types)
@@ -171,6 +173,69 @@ namespace ComputeSharp.SourceGenerators
}
}
/// <summary>
/// Gets a sequence of shader static constant properties and their mapped names.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="semanticModel">The <see cref="SemanticModel"/> 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>
/// <returns>A sequence of static constant properties in <paramref name="structDeclarationSymbol"/>.</returns>
[Pure]
private static IEnumerable<IEnumerable<string>> GetConstantProperties(
GeneratorExecutionContext context,
SemanticModel semanticModel,
StructDeclarationSyntax structDeclaration,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> discoveredTypes,
IDictionary<IFieldSymbol, string> constantDefinitions)
{
foreach (var propertyDeclaration in structDeclaration.Members.OfType<PropertyDeclarationSyntax>())
{
IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration)!;
if (!propertySymbol.IsStatic || !propertySymbol.IsReadOnly)
{
continue;
}
// Constant properties must be named type symbols
if (propertySymbol.Type is not INamedTypeSymbol typeSymbol)
{
context.ReportDiagnostic(InvalidShaderConstantPropertyType, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name, propertySymbol.Type);
continue;
}
string metadataName = typeSymbol.GetFullMetadataName();
// Constant properties must be of a primitive, vector or matrix type
if (!HlslKnownTypes.IsKnownHlslType(metadataName))
{
context.ReportDiagnostic(InvalidShaderConstantPropertyType, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name, propertySymbol.Type);
continue;
}
ConstantPropertyRewriter constantPropertyRewriter = new(
semanticModel,
discoveredTypes,
constantDefinitions,
context);
if (constantPropertyRewriter.Visit(propertyDeclaration) is ExpressionSyntax expression)
{
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
_ = HlslKnownKeywords.TryGetMappedName(propertySymbol.Name, out string? mapping);
yield return new[] { mapping ?? propertySymbol.Name, typeName, expression.NormalizeWhitespace().ToFullString() };
}
}
}
/// <summary>
/// Gets a sequence of captured members and their mapped names.
/// </summary>
+11 -3
ファイルの表示
@@ -24,6 +24,7 @@ namespace ComputeSharp.__Internals
/// <param name="args">The mapped collection of shader fields.</param>
/// <param name="executeMethod">The source code for the <see cref="IComputeShader.Execute"/> method.</param>
/// <param name="methods">The collection of processed methods.</param>
/// <param name="defines">The collection of discovered defines.</param>
/// <param name="constants">The collection of discovered constants.</param>
/// <param name="sharedBuffers">The collection of group shared buffers.</param>
public IComputeShaderSourceAttribute(
@@ -32,6 +33,7 @@ namespace ComputeSharp.__Internals
object[] args,
string executeMethod,
string[] methods,
object[] defines,
object[] constants,
object[] sharedBuffers)
{
@@ -40,7 +42,8 @@ namespace ComputeSharp.__Internals
Fields = args.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
ExecuteMethod = executeMethod;
Methods = methods;
Constants = constants.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
Defines = defines.Cast<string[]>().ToDictionary(static c => c[0], static c => c[1]);
Constants = constants.Cast<string[]>().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
SharedBuffers = sharedBuffers.Cast<object[]>().ToDictionary(static t => (string)t[0], static t => ((string)t[1], (int?)t[2]));
}
@@ -70,13 +73,18 @@ namespace ComputeSharp.__Internals
internal IReadOnlyCollection<string> Methods { get; }
/// <summary>
/// Gets the collection of discovered constants.
/// Gets the collection of discovered define declarations.
/// </summary>
internal IReadOnlyDictionary<string, string> Constants { get; }
internal IReadOnlyDictionary<string, string> Defines { get; }
/// <summary>
/// Gets the collection of discovered constants.
/// </summary>
internal IReadOnlyDictionary<string, (string Type, string Assignment)> Constants { get; }
/// <summary>
/// Gets the collection of discovered shared buffers.
/// </summary>
internal IReadOnlyDictionary<string, (string Type, int? Count)> SharedBuffers { get; }
/// <summary>
+21 -4
ファイルの表示
@@ -42,13 +42,30 @@ namespace ComputeSharp.Shaders.Renderer
builder.Append("#define __GroupSize__get_Z ");
builder.AppendLine(threadsZ.ToString());
// User defined onstants
foreach (var constant in info.ConstantsInfo)
// Define declarations
foreach (var define in info.DefinesInfo)
{
builder.Append("#define ");
builder.Append(constant.Key);
builder.Append(define.Key);
builder.Append(' ');
builder.AppendLine(constant.Value);
builder.AppendLine(define.Value);
}
// Static constants
if (info.ConstantsInfo.Count > 0)
{
builder.AppendLine();
foreach (var constant in info.ConstantsInfo)
{
builder.Append("static const ");
builder.Append(constant.Value.Type);
builder.Append(' ');
builder.Append(constant.Key);
builder.Append(" = ");
builder.Append(constant.Value.Assignment);
builder.AppendLine(";");
}
}
// Declared types
+8 -3
ファイルの表示
@@ -29,9 +29,14 @@ namespace ComputeSharp.Shaders.Translation
IReadOnlyCollection<string> MethodsInfo { get; }
/// <summary>
/// Gets the collection of constants to be declared in the shader.
/// Gets the collection of define declarations to be added to the shader.
/// </summary>
IReadOnlyDictionary<string, string> ConstantsInfo { get; }
IReadOnlyDictionary<string, string> DefinesInfo { get; }
/// <summary>
/// Gets the collection of discovered constants.
/// </summary>
IReadOnlyDictionary<string, (string Type, string Assignment)> ConstantsInfo { get; }
/// <summary>
/// Gets the collection of declared types for the shader.
@@ -39,7 +44,7 @@ namespace ComputeSharp.Shaders.Translation
IReadOnlyCollection<string> DeclaredTypes { get; }
/// <summary>
/// Gets the collection of discovered constants.
/// Gets the collection of discovered group shared buffers.
/// </summary>
IReadOnlyDictionary<string, (string Type, int? Count)> SharedBuffers { get; }
}
+10 -6
ファイルの表示
@@ -66,9 +66,9 @@ namespace ComputeSharp.Shaders.Translation
private readonly List<string> methodsInfo = new();
/// <summary>
/// The <see cref="Dictionary{TKey,TValue}"/> with the discovered constants for the shader.
/// The <see cref="Dictionary{TKey,TValue}"/> with the discovered define declarations for the shader.
/// </summary>
private readonly Dictionary<string, string> constantsInfo = new();
private readonly Dictionary<string, string> definesInfo = new();
/// <summary>
/// The <see cref="List{T}"/> with the collected declared types for the shader.
@@ -108,11 +108,14 @@ namespace ComputeSharp.Shaders.Translation
public IReadOnlyCollection<string> MethodsInfo => this.methodsInfo;
/// <inheritdoc/>
public IReadOnlyDictionary<string, string> ConstantsInfo => this.constantsInfo;
public IReadOnlyDictionary<string, string> DefinesInfo => this.definesInfo;
/// <inheritdoc/>
public IReadOnlyCollection<string> DeclaredTypes => this.declaredTypes;
/// <inheritdoc/>
public IReadOnlyDictionary<string, (string Type, string Assignment)> ConstantsInfo { get; private set; }
/// <inheritdoc/>
public IReadOnlyDictionary<string, (string Type, int? Count)> SharedBuffers { get; private set; }
@@ -143,14 +146,15 @@ namespace ComputeSharp.Shaders.Translation
private void LoadMethodMetadata()
{
EntryPoint = Attribute.ExecuteMethod;
ConstantsInfo = Attribute.Constants;
SharedBuffers = Attribute.SharedBuffers;
this.declaredTypes.AddRange(Attribute.Types);
this.methodsInfo.AddRange(Attribute.Methods);
foreach (var pair in Attribute.Constants)
foreach (var pair in Attribute.Defines)
{
this.constantsInfo.Add(pair.Key, pair.Value);
this.definesInfo.Add(pair.Key, pair.Value);
}
}
@@ -233,7 +237,7 @@ namespace ComputeSharp.Shaders.Translation
foreach (var pair in methodSource.Constants)
{
this.constantsInfo[pair.Key] = pair.Value;
this.definesInfo[pair.Key] = pair.Value;
}
}
else ThrowHelper.ThrowArgumentException("Invalid captured variable");