diff --git a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs
index e443be64..9a594438 100644
--- a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs
+++ b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs
@@ -20,7 +20,7 @@
private const int MaxSteps = 30;
private const float MinimumDistance = 0.0009f;
- private const float normalDistance = 0.0002f;
+ private const float NormalDistance = 0.0002f;
private const int Iterations = 7;
private const float Scale = 3.0f;
private const float FieldOfView = 1.0f;
@@ -30,11 +30,11 @@
private const float Ambient = 0.32184f;
private const float Diffuse = 0.5f;
- private static Float3 LightDir => 1.0f;
- private static Float3 LightColor => new(1.0f, 1.0f, 0.858824f);
- private static Float3 LightDir2 => new(1.0f, -1.0f, 1.0f);
- private static Float3 LightColor2 => new(0, 0.333333f, 1.0f);
- private static Float3 Offset => new(0.92858f, 0.92858f, 0.32858f);
+ private static readonly Float3 LightDir = 1.0f;
+ private static readonly Float3 LightColor = new(1.0f, 1.0f, 0.858824f);
+ private static readonly Float3 LightDir2 = new(1.0f, -1.0f, 1.0f);
+ private static readonly Float3 LightColor2 = new(0, 0.333333f, 1.0f);
+ private static readonly Float3 Offset = new(0.92858f, 0.92858f, 0.32858f);
///
/// Rotates a given value by a specified amount.
@@ -104,7 +104,7 @@
///
private Float3 getNormal(in Float3 pos)
{
- Float3 e = new(0.0f, normalDistance, 0.0f);
+ Float3 e = new(0.0f, NormalDistance, 0.0f);
return Hlsl.Normalize(new Float3(
DE(pos + e.YXX) - DE(pos - e.YXX),
@@ -143,7 +143,7 @@
float smoothStep = steps + distance / MinimumDistance;
float ao = 1.1f - smoothStep / MaxSteps;
- Float3 normal = getNormal(pos - dir * normalDistance * 3.0f);
+ Float3 normal = getNormal(pos - dir * NormalDistance * 3.0f);
Float3 color = 1.0f;
Float3 light = GetLight(color, normal);
diff --git a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs
index 04f08476..5a4f4bc9 100644
--- a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs
+++ b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs
@@ -19,7 +19,7 @@
///
public readonly float time;
- private static Float3x3 m3 => new Float3x3(0.33338f, 0.56034f, -0.71817f, -0.87887f, 0.32651f, -0.15323f, 0.15162f, 0.69596f, 0.61339f) * 1.93f;
+ private static readonly Float3x3 m3 = new Float3x3(0.33338f, 0.56034f, -0.71817f, -0.87887f, 0.32651f, -0.15323f, 0.15162f, 0.69596f, 0.61339f) * 1.93f;
private static Float2x2 Rotate(in float a)
{
diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md
index 97be482d..55dfd8b3 100644
--- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md
+++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Unshipped.md
@@ -48,7 +48,4 @@ CMPS0040 | ComputeSharp.SourceGenerators.IComputeShaderSourceGenerator | Warning
CMPS0041 | ComputeSharp.SourceGenerators.ShaderMethodSourceGenerator | Warning | ShaderMethodSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0042 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0043 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
-CMPS0044 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
-CMPS0045 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
-CMPS0046 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
-CMPS0047 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
\ No newline at end of file
+CMPS0044 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index 2b3f33ba..69af8ec8 100644
--- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -590,74 +590,26 @@ namespace ComputeSharp.SourceGenerators.Diagnostics
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
///
- /// Gets a for an invalid shader constant property type.
+ /// Gets a for an invalid shader static field type.
///
- /// Format: "The compute shader of type {0} contains a constant property \"{1}\" of an invalid type {2} (only primitive, vector and matrix types are supported)".
+ /// Format: "The compute shader of type {0} contains a static field \"{1}\" of an invalid type {2} (only primitive, vector and matrix types are supported)".
///
///
- public static readonly DiagnosticDescriptor InvalidShaderConstantPropertyType = new(
+ public static readonly DiagnosticDescriptor InvalidShaderStaticFieldType = new(
id: "CMPS0043",
- title: "Invalid shader constant property type",
- messageFormat: "The compute shader of type {0} contains a constant property \"{1}\" of an invalid type {2} (only primitive, vector and matrix types are supported)",
+ title: "Invalid shader static field type",
+ messageFormat: "The compute shader of type {0} contains a static field \"{1}\" of an invalid type {2} (only primitive, vector and matrix types are supported)",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
- description: "A type representing a compute shader contains a constant property of a type that is not supported.",
- helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
-
- ///
- /// Gets a for an invalid shader constant property declaration.
- ///
- /// Format: "The compute shader of type {0} contains a constant property \"{1}\" with an invalid declaration (must use a lambda expression)".
- ///
- ///
- public static readonly DiagnosticDescriptor InvalidShaderConstantPropertyDeclaration = new(
- id: "CMPS0044",
- title: "Invalid shader constant property",
- messageFormat: "The compute shader of type {0} contains a constant property \"{1}\" with an invalid declaration (must use a lambda expression)",
- category: typeof(IComputeShader).FullName,
- defaultSeverity: DiagnosticSeverity.Error,
- isEnabledByDefault: true,
- description: "A type representing a compute shader contains a constant property with an invalid declaration.",
- helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
-
- ///
- /// Gets a for an invalid shader constant property declaration.
- ///
- /// Format: "The compute shader of type {0} contains a mutable property \"{1}\" (only readonly properties are supported)".
- ///
- ///
- public static readonly DiagnosticDescriptor NonReadonlyShaderPropertyDeclaration = new(
- id: "CMPS0045",
- title: "Invalid shader constant property",
- messageFormat: "The compute shader of type {0} contains a mutable property \"{1}\" (only readonly properties are supported)",
- category: typeof(IComputeShader).FullName,
- defaultSeverity: DiagnosticSeverity.Error,
- isEnabledByDefault: true,
- description: "A type representing a compute shader contains a non readonly property declaration.",
- helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
-
- ///
- /// Gets a for an invalid shader constant property declaration.
- ///
- /// Format: "The compute shader of type {0} contains an instance property \"{1}\" (only static properties are supported)".
- ///
- ///
- public static readonly DiagnosticDescriptor InstanceShaderPropertyDeclaration = new(
- id: "CMPS0046",
- title: "Invalid shader constant property",
- messageFormat: "The compute shader of type {0} contains an instance property \"{1}\" (only static properties are supported)",
- category: typeof(IComputeShader).FullName,
- defaultSeverity: DiagnosticSeverity.Error,
- isEnabledByDefault: true,
- description: "A type representing a compute shader contains an instance property declaration.",
+ description: "A type representing a compute shader contains a static field of a type that is not supported.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
///
/// Gets a for an invalid usage.
///
public static readonly DiagnosticDescriptor InvalidDispatchSizeUsage = new(
- id: "CMPS0047",
+ id: "CMPS0044",
title: "Invalid DispatchSize usage",
messageFormat: "The DispatchSize type can only be used within the main body of a compute shader",
category: typeof(IComputeShader).FullName,
diff --git a/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs
index 894f6630..e582b220 100644
--- a/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs
+++ b/src/ComputeSharp.SourceGenerators/Helpers/SyntaxFactoryHelper.cs
@@ -46,7 +46,7 @@ namespace ComputeSharp.SourceGenerators.Helpers
/// The input sequence of groups.
/// An instance with the described contents.
[Pure]
- public static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable> groups)
+ public static ArrayCreationExpressionSyntax NestedArrayExpression(IEnumerable> groups)
{
return
ArrayCreationExpression(
@@ -55,7 +55,10 @@ namespace ComputeSharp.SourceGenerators.Helpers
.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()));
+ group.Select(static item => item is null
+ ? LiteralExpression(SyntaxKind.NullLiteralExpression)
+ : LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(item)))
+ .ToArray()))).ToArray()));
}
///
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
index beaff28e..9ef136f1 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
@@ -164,7 +164,7 @@ namespace ComputeSharp.SourceGenerators
IFieldSymbol fieldSymbol in
from fieldSymbol in structDeclarationSymbol.GetMembers().OfType()
where fieldSymbol.Type is INamedTypeSymbol { IsStatic: false } &&
- !fieldSymbol.IsConst
+ !fieldSymbol.IsConst && !fieldSymbol.IsStatic
select fieldSymbol)
{
string typeName = fieldSymbol.Type.GetFullMetadataName();
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
index 1f8dff73..2e29bb97 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderSourceGenerator.cs
@@ -77,7 +77,7 @@ namespace ComputeSharp.SourceGenerators
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);
+ var staticFields = GetStaticFields(context, semanticModel, structDeclaration, structDeclarationSymbol, discoveredTypes, constantDefinitions);
// Create the compilation unit with the source attribute
var source =
@@ -91,7 +91,7 @@ namespace ComputeSharp.SourceGenerators
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(entryPoint))),
AttributeArgument(ArrayExpression(processedMethods)),
AttributeArgument(NestedArrayExpression(processedConstants)),
- AttributeArgument(NestedArrayExpression(staticConstants)),
+ AttributeArgument(NestedArrayExpression(staticFields)),
AttributeArgument(NestedArrayExpression(sharedBuffers)))))
.WithOpenBracketToken(Token(TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), SyntaxKind.OpenBracketToken, TriviaList()))
.WithTarget(AttributeTargetSpecifier(Token(SyntaxKind.AssemblyKeyword))))
@@ -174,7 +174,7 @@ namespace ComputeSharp.SourceGenerators
}
///
- /// Gets a sequence of shader static constant properties and their mapped names.
+ /// Gets a sequence of shader static fields and their mapped names.
///
/// The current generator context in use.
/// The instance for the type to process.
@@ -182,9 +182,9 @@ namespace ComputeSharp.SourceGenerators
/// The type symbol for the shader type.
/// The collection of currently discovered types.
/// The collection of discovered constant definitions.
- /// A sequence of static constant properties in .
+ /// A sequence of static constant fields in .
[Pure]
- private static IEnumerable> GetConstantProperties(
+ private static IEnumerable> GetStaticFields(
GeneratorExecutionContext context,
SemanticModel semanticModel,
StructDeclarationSyntax structDeclaration,
@@ -192,55 +192,43 @@ namespace ComputeSharp.SourceGenerators
ICollection discoveredTypes,
IDictionary constantDefinitions)
{
- foreach (var propertyDeclaration in structDeclaration.Members.OfType())
+ foreach (var fieldDeclaration in structDeclaration.Members.OfType())
{
- IPropertySymbol propertySymbol = semanticModel.GetDeclaredSymbol(propertyDeclaration)!;
-
- if (!propertySymbol.IsStatic)
+ foreach (var variableDeclarator in fieldDeclaration.Declaration.Variables)
{
- context.ReportDiagnostic(InstanceShaderPropertyDeclaration, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name);
+ IFieldSymbol fieldSymbol = (IFieldSymbol)semanticModel.GetDeclaredSymbol(variableDeclarator)!;
- continue;
- }
+ if (!fieldSymbol.IsStatic || fieldSymbol.IsConst)
+ {
+ continue;
+ }
- if (!propertySymbol.IsReadOnly)
- {
- context.ReportDiagnostic(NonReadonlyShaderPropertyDeclaration, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name);
+ // Constant properties must be of a primitive, vector or matrix type
+ if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol ||
+ !HlslKnownTypes.IsKnownHlslType(typeSymbol.GetFullMetadataName()))
+ {
+ context.ReportDiagnostic(InvalidShaderStaticFieldType, variableDeclarator, structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type);
- continue;
- }
+ continue;
+ }
- // Constant properties must be named type symbols
- if (propertySymbol.Type is not INamedTypeSymbol typeSymbol)
- {
- context.ReportDiagnostic(InvalidShaderConstantPropertyType, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name, propertySymbol.Type);
+ _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping);
- continue;
- }
+ string typeDeclaration = fieldSymbol.IsReadOnly switch
+ {
+ true => $"static const {HlslKnownTypes.GetMappedName(typeSymbol)}",
+ false => $"static {HlslKnownTypes.GetMappedName(typeSymbol)}"
+ };
- string metadataName = typeSymbol.GetFullMetadataName();
+ StaticFieldRewriter staticFieldRewriter = new(
+ semanticModel,
+ discoveredTypes,
+ constantDefinitions,
+ context);
- // Constant properties must be of a primitive, vector or matrix type
- if (!HlslKnownTypes.IsKnownHlslType(metadataName))
- {
- context.ReportDiagnostic(InvalidShaderConstantPropertyType, propertyDeclaration, structDeclarationSymbol, propertySymbol.Name, propertySymbol.Type);
+ string? assignment = staticFieldRewriter.Visit(variableDeclarator)?.NormalizeWhitespace().ToFullString();
- 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() };
+ yield return new[] { mapping ?? fieldSymbol.Name, typeDeclaration, assignment };
}
}
}
diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs
similarity index 85%
rename from src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs
rename to src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs
index 800d6595..644bf065 100644
--- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ConstantPropertyRewriter.cs
+++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/StaticFieldRewriter.cs
@@ -14,18 +14,18 @@ using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace ComputeSharp.SourceGenerators.SyntaxRewriters
{
///
- /// A custom type that processes C# static properties to convert to HLSL static constants.
+ /// A custom type that processes C# static field to convert to HLSL static fields (possibly constant).
///
- internal sealed class ConstantPropertyRewriter : HlslSourceRewriter
+ internal sealed class StaticFieldRewriter : HlslSourceRewriter
{
///
- /// Creates a new instance with the specified parameters.
+ /// Creates a new instance with the specified parameters.
///
/// The instance for the target syntax tree.
/// The set of discovered custom types.
/// The collection of discovered constant definitions.
/// The current generator context in use.
- public ConstantPropertyRewriter(
+ public StaticFieldRewriter(
SemanticModel semanticModel,
ICollection discoveredTypes,
IDictionary constantDefinitions,
@@ -35,22 +35,14 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
}
///
- public ExpressionSyntax? Visit(PropertyDeclarationSyntax? node)
+ public ExpressionSyntax? Visit(VariableDeclaratorSyntax? node)
{
- if (node!.ExpressionBody is null)
+ if (node?.Initializer is EqualsValueClauseSyntax fieldInitializer)
{
- Context.ReportDiagnostic(
- InvalidShaderConstantPropertyDeclaration,
- node,
- SemanticModel.GetDeclaredSymbol(node.FirstAncestorOrSelf()!)!,
- SemanticModel.GetDeclaredSymbol(node)!.Name);
-
- return null;
+ return ((EqualsValueClauseSyntax)Visit(fieldInitializer))!.Value;
}
- ArrowExpressionClauseSyntax? updatedNode = (ArrowExpressionClauseSyntax?)Visit(node.ExpressionBody)!;
-
- return updatedNode.Expression;
+ return null;
}
///
diff --git a/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs b/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs
index 64f6bba7..221215a2 100644
--- a/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs
+++ b/src/ComputeSharp/Core/Attributes/__Internals/IComputeShaderSourceAttribute.cs
@@ -25,7 +25,7 @@ namespace ComputeSharp.__Internals
/// The source code for the method.
/// The collection of processed methods.
/// The collection of discovered defines.
- /// The collection of discovered constants.
+ /// The collection of discovered static fields.
/// The collection of group shared buffers.
public IComputeShaderSourceAttribute(
Type shaderType,
@@ -34,7 +34,7 @@ namespace ComputeSharp.__Internals
string executeMethod,
string[] methods,
object[] defines,
- object[] constants,
+ object[] staticFields,
object[] sharedBuffers)
{
ShaderType = shaderType;
@@ -43,7 +43,7 @@ namespace ComputeSharp.__Internals
ExecuteMethod = executeMethod;
Methods = methods;
Defines = defines.Cast().ToDictionary(static c => c[0], static c => c[1]);
- Constants = constants.Cast().ToDictionary(static arg => arg[0], static arg => (arg[1], arg[2]));
+ StaticFields = staticFields.Cast().ToDictionary(static arg => arg[0]!, static arg => (arg[1]!, arg[2]));
SharedBuffers = sharedBuffers.Cast