diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.GetNonDynamicHlslSourceInfo.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.GetNonDynamicHlslSourceInfo.cs index 09ad0ef4..5f153721 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.GetNonDynamicHlslSourceInfo.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.GetNonDynamicHlslSourceInfo.cs @@ -58,7 +58,7 @@ public sealed partial class IShaderGenerator2 var pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: nameof(IPixelShader) }); var isComputeShader = pixelShaderSymbol is null; var implicitTextureType = isComputeShader ? null : HlslKnownTypes.GetMappedNameForPixelShaderType(pixelShaderSymbol!); - var nonDelegateInstanceFields = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes, isComputeShader); + var (resourceFields, valueFields) = GetInstanceFields(builder, structDeclarationSymbol, discoveredTypes, isComputeShader); var delegateInstanceFields = GetDelegateFieldNames(structDeclarationSymbol); var sharedBuffers = GetSharedBuffers(builder, structDeclarationSymbol, discoveredTypes); var (entryPoint, processedMethods, isSamplerUsed) = GetProcessedMethods(builder, structDeclaration, structDeclarationSymbol, semanticModelProvider, discoveredTypes, staticMethods, constantDefinitions, isComputeShader); @@ -73,7 +73,8 @@ public sealed partial class IShaderGenerator2 return GetNonDynamicHlslSourceInfo( definedConstants, declaredTypes, - nonDelegateInstanceFields, + resourceFields, + valueFields, delegateInstanceFields, staticFields, sharedBuffers, @@ -93,13 +94,17 @@ public sealed partial class IShaderGenerator2 /// Indicates whether or not represents a compute shader. /// A sequence of captured fields in . [Pure] - private static ImmutableArray<(string MetadataName, string Name, string HlslType)> GetInstanceFields( - ImmutableArray.Builder diagnostics, - INamedTypeSymbol structDeclarationSymbol, - ICollection types, - bool isComputeShader) + private static ( + ImmutableArray<(string MetadataName, string Name, string HlslType)>, + ImmutableArray<(string Name, string HlslType)>) + GetInstanceFields( + ImmutableArray.Builder diagnostics, + INamedTypeSymbol structDeclarationSymbol, + ICollection types, + bool isComputeShader) { - ImmutableArray<(string, string, string)>.Builder builder = ImmutableArray.CreateBuilder<(string, string, string)>(); + ImmutableArray<(string, string, string)>.Builder resources = ImmutableArray.CreateBuilder<(string, string, string)>(); + ImmutableArray<(string, string)>.Builder values = ImmutableArray.CreateBuilder<(string, string)>(); bool hlslResourceFound = false; foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) @@ -126,6 +131,9 @@ public sealed partial class IShaderGenerator2 } string metadataName = typeSymbol.GetFullMetadataName(); + string typeName = HlslKnownTypes.GetMappedName(typeSymbol); + + _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); // Allowed fields must be either resources, unmanaged values or delegates if (HlslKnownTypes.IsTypedResourceType(metadataName)) @@ -137,6 +145,9 @@ public sealed partial class IShaderGenerator2 { types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]); } + + // Add the current mapping for the name (if the name used a reserved keyword) + resources.Add((metadataName, mapping ?? fieldSymbol.Name, typeName)); } else if (!typeSymbol.IsUnmanagedType && typeSymbol.TypeKind != TypeKind.Delegate) @@ -146,19 +157,16 @@ public sealed partial class IShaderGenerator2 continue; } - else if (typeSymbol.IsUnmanagedType && - !HlslKnownTypes.IsKnownHlslType(metadataName)) + else if (typeSymbol.IsUnmanagedType) { // Track the type if it's a custom struct - types.Add(typeSymbol); - } + if (!HlslKnownTypes.IsKnownHlslType(metadataName)) + { + types.Add(typeSymbol); + } - string typeName = HlslKnownTypes.GetMappedName(typeSymbol); - - _ = HlslKnownKeywords.TryGetMappedName(fieldSymbol.Name, out string? mapping); - - // Yield back the current mapping for the name (if the name used a reserved keyword) - builder.Add((metadataName, mapping ?? fieldSymbol.Name, typeName)); + values.Add((mapping ?? fieldSymbol.Name, typeName)); + } } // If the shader is a compute one (so no implicit output texture), it has to contain at least one resource @@ -167,7 +175,7 @@ public sealed partial class IShaderGenerator2 diagnostics.Add(MissingShaderResources, structDeclarationSymbol, structDeclarationSymbol); } - return builder.ToImmutable(); + return (resources.ToImmutable(), values.ToImmutable()); } /// @@ -484,7 +492,8 @@ public sealed partial class IShaderGenerator2 /// /// The sequence of defined constants for the shader. /// The sequence of declared types used by the shader. - /// The sequence of instance fields for the current shader. + /// The sequence of resource instance fields for the current shader. + /// The sequence of value instance fields for the current shader. /// The sequence of delegate fields for the current shader. /// The sequence of static fields referenced by the shader. /// The sequence of shared buffers declared by the shader. @@ -497,7 +506,8 @@ public sealed partial class IShaderGenerator2 private static HlslSourceInfo GetNonDynamicHlslSourceInfo( ImmutableArray<(string Name, string Value)> definedConstants, ImmutableArray<(string Name, string Definition)> declaredTypes, - ImmutableArray<(string MetadataName, string Name, string HlslType)> instanceFields, + ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, + ImmutableArray<(string Name, string HlslType)> valueFields, ImmutableArray delegateInstanceFields, ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields, ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers, @@ -614,7 +624,7 @@ public sealed partial class IShaderGenerator2 } // User-defined values - foreach (var (_, fieldName, fieldType) in instanceFields.Where(static t => !HlslKnownTypes.IsTypedResourceType(t.MetadataName))) + foreach (var (fieldName, fieldType) in valueFields) { AppendLineAndLF($" {fieldType} {fieldName};"); } @@ -640,7 +650,7 @@ public sealed partial class IShaderGenerator2 } // Resources - foreach (var (metadataName, fieldName, fieldType) in instanceFields) + foreach (var (metadataName, fieldName, fieldType) in resourceFields) { if (HlslKnownTypes.IsConstantBufferType(metadataName)) {