diff --git a/src/ComputeSharp.Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs b/src/ComputeSharp.Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs
index 195ea4d2..97104dfd 100644
--- a/src/ComputeSharp.Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs
+++ b/src/ComputeSharp.Core/Attributes/__Internals/ShaderMethodSourceAttribute.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
+using System.Linq;
using System.Reflection;
using ComputeSharp.Core.Extensions;
using ComputeSharp.Exceptions;
@@ -35,13 +36,15 @@ namespace ComputeSharp.__Internals
/// The collection of custom types.
/// The source code for the target entry point method.
/// The collection of processed methods.
- public ShaderMethodSourceAttribute(string methodName, string[] types, string invokeMethod, string[] methods)
+ /// The collection of discovered constants.
+ public ShaderMethodSourceAttribute(string methodName, string[] types, string invokeMethod, string[] methods, object[] constants)
{
this.invokeMethod = invokeMethod;
MethodName = methodName;
Types = types;
Methods = methods;
+ Constants = constants.Cast().ToDictionary(static c => c[0], static c => c[1]);
}
///
@@ -59,6 +62,11 @@ namespace ComputeSharp.__Internals
///
internal IReadOnlyCollection Methods { get; }
+ ///
+ /// Gets the collection of discovered constants.
+ ///
+ internal IReadOnlyDictionary Constants { get; }
+
///
/// Gets the mapped source code for the current method.
///
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
index 6f3c62b0..12606154 100644
--- a/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
+++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader{T}.cs
@@ -132,14 +132,13 @@ namespace ComputeSharp.Shaders.Translation
{
EntryPoint = Attribute.ExecuteMethod;
+ this.declaredTypes.AddRange(Attribute.Types);
this.methodsInfo.AddRange(Attribute.Methods);
foreach (var pair in Attribute.Constants)
{
this.constantsInfo.Add(pair.Key, pair.Value);
}
-
- this.declaredTypes.AddRange(Attribute.Types);
}
///
@@ -223,6 +222,11 @@ namespace ComputeSharp.Shaders.Translation
this.declaredTypes.AddRange(methodSource.Types);
this.methodsInfo.Add(methodSource.GetMappedInvokeMethod(hlslName));
this.methodsInfo.AddRange(methodSource.Methods);
+
+ foreach (var pair in methodSource.Constants)
+ {
+ this.constantsInfo[pair.Key] = pair.Value;
+ }
}
else ThrowHelper.ThrowArgumentException("Invalid captured variable");
}
diff --git a/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs
new file mode 100644
index 00000000..5fb2c00a
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs
@@ -0,0 +1,61 @@
+using System.Collections.Generic;
+using System.Diagnostics.Contracts;
+using System.Linq;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
+
+namespace ComputeSharp.SourceGenerators.Helpers
+{
+ ///
+ /// A class containing additional factory methods for constructing syntax nodes, tokens and trivia.
+ ///
+ internal static class SyntaxFactoryHelper
+ {
+ ///
+ /// Creates a [] array expression from the given sequence of instances.
+ ///
+ /// That it, it applies the following transformation:
+ ///
+ /// { "S1", "S2" } => new string[] { "S1", "S2" }
+ ///
+ ///
+ ///
+ /// The input sequence of instances.
+ /// An instance with the described contents.
+ [Pure]
+ public static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable values)
+ {
+ return
+ ArrayCreationExpression(
+ ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
+ .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression()))))
+ .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
+ .AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
+ }
+
+ ///
+ /// Creates an [] array expression with the nested groups in the input sequence.
+ ///
+ /// That it, it applies the following transformation:
+ ///
+ /// { ("K1", "V1"), ("K2", "V2") } => new object[] { new[] { "K1", "V1" }, new[] { "K2", "V2" } }
+ ///
+ ///
+ ///
+ /// The input sequence of groups.
+ /// An instance with the described contents.
+ [Pure]
+ public static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable> groups)
+ {
+ return
+ ArrayCreationExpression(
+ ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
+ .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression()))))
+ .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
+ .AddExpressions(groups.Select(static group =>
+ ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
+ group.Select(static item => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))).ToArray()))).ToArray()));
+ }
+ }
+}
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 5e96e74c..c1b061a3 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -12,6 +12,7 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
+using static ComputeSharp.SourceGenerators.Helpers.SyntaxFactoryHelper;
#pragma warning disable CS0618
@@ -47,34 +48,6 @@ namespace ComputeSharp.SourceGenerators
Dictionary staticMethods = new(SymbolEqualityComparer.Default);
Dictionary constantDefinitions = new(SymbolEqualityComparer.Default);
- // Helper that converts a sequence of string sequences into a nested array expression.
- // That is, this applies the following transformation:
- // - { ("K1", "V1"), ("K2", "V2") } => new object[] { new[] { "K1", "V1" }, new[] { "K2", "V2" } }
- static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable> groups)
- {
- return
- ArrayCreationExpression(
- ArrayType(PredefinedType(Token(SyntaxKind.ObjectKeyword)))
- .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression()))))
- .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
- .AddExpressions(groups.Select(static group =>
- ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression).AddExpressions(
- group.Select(static item => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item))).ToArray()))).ToArray()));
- }
-
- // Helper that converts a sequence of strings into an array expression.
- // That is, this applies the following transformation:
- // - { "S1", "S2" } => new string[] { "S1", "S2" }
- static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable values)
- {
- return
- ArrayCreationExpression(
- ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
- .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression()))))
- .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
- .AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
- }
-
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
@@ -200,7 +173,7 @@ namespace ComputeSharp.SourceGenerators
/// The collection of discovered constant definitions.
/// A sequence of discovered constants to declare in the shader.
[Pure]
- private static IEnumerable> GetProcessedConstants(IReadOnlyDictionary constantDefinitions)
+ internal static IEnumerable> GetProcessedConstants(IReadOnlyDictionary constantDefinitions)
{
foreach (var constant in constantDefinitions)
{
diff --git a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
index 3d39992e..6d817f4b 100644
--- a/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/ShaderMethodSourceGenerator.cs
@@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
+using static ComputeSharp.SourceGenerators.Helpers.SyntaxFactoryHelper;
#pragma warning disable CS0618
@@ -44,23 +45,12 @@ namespace ComputeSharp.SourceGenerators
// We need to sets to track all discovered custom types and static methods
HashSet discoveredTypes = new(SymbolEqualityComparer.Default);
Dictionary staticMethods = new(SymbolEqualityComparer.Default);
+ Dictionary constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
- var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods);
+ var (invokeMethod, processedMethods) = GetProcessedMethods(methodDeclaration, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = IComputeShaderSourceGenerator.GetProcessedTypes(discoveredTypes).ToArray();
-
- // Helper that converts a sequence of strings into an array expression.
- // That is, this applies the following transformation:
- // - { "S1", "S2" } => new string[] { "S1", "S2" }
- static ArrayCreationExpressionSyntax ArrayExpression(IEnumerable values)
- {
- return
- ArrayCreationExpression(
- ArrayType(PredefinedType(Token(SyntaxKind.StringKeyword)))
- .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression()))))
- .WithInitializer(InitializerExpression(SyntaxKind.ArrayInitializerExpression)
- .AddExpressions(values.Select(static value => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(value))).ToArray()));
- }
+ var processedConstants = IComputeShaderSourceGenerator.GetProcessedConstants(constantDefinitions);
// Create the compilation unit with the source attribute
var source =
@@ -70,7 +60,8 @@ namespace ComputeSharp.SourceGenerators
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(methodDeclarationSymbol.GetFullMetadataName(true)))),
AttributeArgument(ArrayExpression(processedTypes)),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(invokeMethod))),
- AttributeArgument(ArrayExpression(processedMethods)))))
+ AttributeArgument(ArrayExpression(processedMethods)),
+ AttributeArgument(NestedArrayExpression(processedConstants)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
.NormalizeWhitespace()
@@ -88,15 +79,17 @@ namespace ComputeSharp.SourceGenerators
/// The instance for the method to process.
/// The collection of currently discovered types.
/// The set of discovered and processed static methods.
+ /// The collection of discovered constant definitions.
/// A sequence of processed methods in .
[Pure]
private static (string InvokeMethod, IEnumerable Methods) GetProcessedMethods(
MethodDeclarationSyntax methodDeclaration,
SemanticModel semanticModel,
ICollection discoveredTypes,
- IDictionary staticMethods)
+ IDictionary staticMethods,
+ IDictionary constantDefinitions)
{
- ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, null); // TODO: implement constant support
+ ShaderSourceRewriter shaderSourceRewriter = new(semanticModel, discoveredTypes, staticMethods, constantDefinitions);
// Rewrite the method syntax tree
var processedMethod = shaderSourceRewriter