From 0d667a5da081358242769ec8f77412cd8b702dad Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 12 Dec 2023 23:22:37 +0100 Subject: [PATCH] Use type aliases in DX12/D2D generators to write HLSL --- ...xelShaderDescriptorGenerator.HlslSource.cs | 76 +++++------ ...mputeSharp.SourceGeneration.Hlsl.projitems | 1 + .../Models/TypeAliases.cs | 13 ++ .../HlslDefinitionsSyntaxProcessor.cs | 8 +- .../SyntaxRewriters/HlslSourceRewriter.cs | 4 +- .../SyntaxRewriters/ShaderSourceRewriter.cs | 2 +- .../SyntaxRewriters/StaticFieldRewriter.cs | 2 +- ...uteShaderDescriptorGenerator.HlslSource.cs | 120 +++++++++--------- 8 files changed, 121 insertions(+), 105 deletions(-) create mode 100644 src/ComputeSharp.SourceGeneration.Hlsl/Models/TypeAliases.cs diff --git a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs index 05af11da..aada8953 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/D2DPixelShaderDescriptorGenerator.HlslSource.cs @@ -52,7 +52,7 @@ partial class D2DPixelShaderDescriptorGenerator Dictionary instanceMethods = new(SymbolEqualityComparer.Default); Dictionary constructors = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); - Dictionary staticFieldDefinitions = new(SymbolEqualityComparer.Default); + Dictionary staticFieldDefinitions = new(SymbolEqualityComparer.Default); token.ThrowIfCancellationRequested(); @@ -61,15 +61,15 @@ partial class D2DPixelShaderDescriptorGenerator diagnostics, structDeclarationSymbol, discoveredTypes, - out ImmutableArray<(string Name, string HlslType)> valueFields, - out ImmutableArray<(string Name, string HlslType, int Index)> resourceTextureFields); + out ImmutableArray valueFields, + out ImmutableArray resourceTextureFields); token.ThrowIfCancellationRequested(); SemanticModelProvider semanticModelProvider = new(compilation); // Explore the syntax tree and extract the processed info - (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods) = GetProcessedMethods( + (string entryPoint, ImmutableArray processedMethods) = GetProcessedMethods( diagnostics, structDeclarationSymbol, semanticModelProvider, @@ -84,7 +84,7 @@ partial class D2DPixelShaderDescriptorGenerator token.ThrowIfCancellationRequested(); - ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields( + ImmutableArray staticFields = GetStaticFields( diagnostics, semanticModelProvider, structDeclarationSymbol, @@ -97,7 +97,7 @@ partial class D2DPixelShaderDescriptorGenerator token.ThrowIfCancellationRequested(); // Process the discovered types and constants - ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes( + ImmutableArray declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes( diagnostics, structDeclarationSymbol, discoveredTypes, @@ -106,7 +106,7 @@ partial class D2DPixelShaderDescriptorGenerator token.ThrowIfCancellationRequested(); - ImmutableArray<(string Name, string Value)> definedConstants = HlslDefinitionsSyntaxProcessor.GetDefinedConstants(constantDefinitions); + ImmutableArray definedConstants = HlslDefinitionsSyntaxProcessor.GetDefinedConstants(constantDefinitions); token.ThrowIfCancellationRequested(); @@ -149,11 +149,11 @@ partial class D2DPixelShaderDescriptorGenerator ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, ICollection types, - out ImmutableArray<(string Name, string HlslType)> valueFields, - out ImmutableArray<(string Name, string HlslType, int Index)> resourceTextureFields) + out ImmutableArray valueFields, + out ImmutableArray resourceTextureFields) { - using ImmutableArrayBuilder<(string, string)> values = new(); - using ImmutableArrayBuilder<(string, string, int)> resourceTextures = new(); + using ImmutableArrayBuilder values = new(); + using ImmutableArrayBuilder resourceTextures = new(); foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers()) { @@ -244,17 +244,17 @@ partial class D2DPixelShaderDescriptorGenerator /// The used to cancel the operation, if needed. /// Whether or not the shader needs the [D2DRequiresScenePosition] annotation. /// A sequence of static constant fields in . - private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( + private static ImmutableArray GetStaticFields( ImmutableArrayBuilder diagnostics, SemanticModelProvider semanticModel, INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, IDictionary constantDefinitions, - IDictionary staticFieldDefinitions, + IDictionary staticFieldDefinitions, CancellationToken token, out bool needsD2D1RequiresScenePosition) { - using ImmutableArrayBuilder<(string, string, string?)> builder = new(); + using ImmutableArrayBuilder builder = new(); needsD2D1RequiresScenePosition = false; @@ -286,7 +286,7 @@ partial class D2DPixelShaderDescriptorGenerator } // Also gather the external static fields (same as in the DX12 generator) - foreach ((string, string, string?) externalField in staticFieldDefinitions.Values) + foreach (HlslStaticField externalField in staticFieldDefinitions.Values) { builder.Add(externalField); } @@ -309,7 +309,7 @@ partial class D2DPixelShaderDescriptorGenerator /// Whether or not the shader needs the [D2DRequiresScenePosition] annotation. /// The used to cancel the operation, if needed. /// A sequence of processed methods in , and the entry point. - private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods) GetProcessedMethods( + private static (string EntryPoint, ImmutableArray Methods) GetProcessedMethods( ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, SemanticModelProvider semanticModel, @@ -318,11 +318,11 @@ partial class D2DPixelShaderDescriptorGenerator IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, - IDictionary staticFieldDefinitions, + IDictionary staticFieldDefinitions, CancellationToken token, out bool needsD2D1RequiresScenePosition) { - using ImmutableArrayBuilder<(string, string)> methods = new(); + using ImmutableArrayBuilder methods = new(); string? entryPoint = null; needsD2D1RequiresScenePosition = false; @@ -458,12 +458,12 @@ partial class D2DPixelShaderDescriptorGenerator /// Whether the shader requires the scene position. /// The series of statements to build the HLSL source to compile to execute the current shader. private static string GetHlslSource( - ImmutableArray<(string Name, string Value)> definedConstants, - ImmutableArray<(string Name, string Definition)> declaredTypes, - ImmutableArray<(string Name, string HlslType)> valueFields, - ImmutableArray<(string Name, string HlslType, int Index)> resourceTextureFields, - ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields, - ImmutableArray<(string Signature, string Definition)> processedMethods, + ImmutableArray definedConstants, + ImmutableArray declaredTypes, + ImmutableArray valueFields, + ImmutableArray resourceTextureFields, + ImmutableArray staticFields, + ImmutableArray processedMethods, string executeMethod, int inputCount, ImmutableArray inputSimpleIndices, @@ -503,15 +503,15 @@ partial class D2DPixelShaderDescriptorGenerator writer.WriteLine(); // Define declarations - foreach ((string name, string value) in definedConstants) + foreach (HlslConstant constant in definedConstants) { - writer.WriteLine($"#define {name} {value}"); + writer.WriteLine($"#define {constant.Name} {constant.Value}"); } writer.WriteLine(skipIfPresent: true); // Static fields - foreach ((string Name, string TypeDeclaration, string? Assignment) field in staticFields) + foreach (HlslStaticField field in staticFields) { if (field.Assignment is string assignment) { @@ -524,40 +524,40 @@ partial class D2DPixelShaderDescriptorGenerator } // Declared types - foreach ((string _, string typeDefinition) in declaredTypes) + foreach (HlslUserType userType in declaredTypes) { writer.WriteLine(skipIfPresent: true); - writer.WriteLine(typeDefinition); + writer.WriteLine(userType.Definition); } writer.WriteLine(skipIfPresent: true); // Captured variables - foreach ((string fieldName, string fieldType) in valueFields) + foreach (HlslValueField valueField in valueFields) { - writer.WriteLine($"{fieldType} {fieldName};"); + writer.WriteLine($"{valueField.Type} {valueField.Name};"); } // Resource textures - foreach ((string fieldName, string fieldType, int index) in resourceTextureFields) + foreach (HlslResourceTextureField field in resourceTextureFields) { writer.WriteLine(skipIfPresent: true); - writer.WriteLine($"{fieldType} {fieldName} : register(t{index});"); - writer.WriteLine($"SamplerState __sampler__{fieldName} : register(s{index});"); + writer.WriteLine($"{field.Type} {field.Name} : register(t{field.Index});"); + writer.WriteLine($"SamplerState __sampler__{field.Name} : register(s{field.Index});"); } // Forward declarations - foreach ((string forwardDeclaration, string _) in processedMethods) + foreach (HlslMethod method in processedMethods) { writer.WriteLine(skipIfPresent: true); - writer.WriteLine(forwardDeclaration); + writer.WriteLine(method.Signature); } // Captured methods - foreach ((string _, string method) in processedMethods) + foreach (HlslMethod method in processedMethods) { writer.WriteLine(skipIfPresent: true); - writer.WriteLine(method); + writer.WriteLine(method.Declaration); } // Entry point diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems index b3e8892c..5c46397b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems +++ b/src/ComputeSharp.SourceGeneration.Hlsl/ComputeSharp.SourceGeneration.Hlsl.projitems @@ -28,6 +28,7 @@ + diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Models/TypeAliases.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Models/TypeAliases.cs new file mode 100644 index 00000000..6e489267 --- /dev/null +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Models/TypeAliases.cs @@ -0,0 +1,13 @@ +#pragma warning disable IDE0005 + +// Shared type aliases for transparent models for gathered HLSL info. These are used +// when writing the transpiled HLSL. No need for concrete types for any of these, so +// just using type aliases keeps things simpler while still making the code explicit. +global using HlslConstant = (string Name, string Value); +global using HlslUserType = (string Name, string Definition); +global using HlslResourceField = (string MetadataName, string Name, string Type); +global using HlslValueField = (string Name, string Type); +global using HlslResourceTextureField = (string Name, string Type, int Index); +global using HlslStaticField = (string Name, string TypeDeclaration, string? Assignment); +global using HlslSharedBuffer = (string Name, string Type, int? Count); +global using HlslMethod = (string Signature, string Declaration); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs index 2fb2f2d9..0e9cab64 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxProcessors/HlslDefinitionsSyntaxProcessor.cs @@ -26,9 +26,9 @@ internal static class HlslDefinitionsSyntaxProcessor /// /// The collection of discovered constant definitions. /// A sequence of discovered constants to declare in the shader. - public static ImmutableArray<(string Name, string Value)> GetDefinedConstants(IReadOnlyDictionary constantDefinitions) + public static ImmutableArray GetDefinedConstants(IReadOnlyDictionary constantDefinitions) { - using ImmutableArrayBuilder<(string, string)> builder = new(); + using ImmutableArrayBuilder builder = new(); foreach (KeyValuePair constant in constantDefinitions) { @@ -144,14 +144,14 @@ internal static class HlslDefinitionsSyntaxProcessor /// The collection of discovered instance methods for custom struct types. /// The collection of discovered constructors for custom struct types. /// A sequence of custom type definitions to add to the shader source. - public static ImmutableArray<(string Name, string Definition)> GetDeclaredTypes( + public static ImmutableArray GetDeclaredTypes( ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, IEnumerable types, IReadOnlyDictionary instanceMethods, IReadOnlyDictionary constructors) { - using ImmutableArrayBuilder<(string, string)> builder = new(); + using ImmutableArrayBuilder builder = new(); IReadOnlyCollection invalidTypes; diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs index f1248359..ecaf4fa1 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/HlslSourceRewriter.cs @@ -40,7 +40,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, - IDictionary staticFieldDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token) { @@ -70,7 +70,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter /// /// Gets the collection of discovered static field definitions. /// - protected IDictionary StaticFieldDefinitions { get; } + protected IDictionary StaticFieldDefinitions { get; } /// /// Gets the collection of produced instances. diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs index ac5fb1b2..00f1714f 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/ShaderSourceRewriter.cs @@ -40,7 +40,7 @@ internal sealed partial class ShaderSourceRewriter( IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, - IDictionary staticFieldDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token, bool isEntryPoint = false) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs index a9515f21..346ac572 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/SyntaxRewriters/StaticFieldRewriter.cs @@ -30,7 +30,7 @@ internal sealed partial class StaticFieldRewriter( SemanticModelProvider semanticModel, ICollection discoveredTypes, IDictionary constantDefinitions, - IDictionary staticFieldDefinitions, + IDictionary staticFieldDefinitions, ImmutableArrayBuilder diagnostics, CancellationToken token) : HlslSourceRewriter(semanticModel, discoveredTypes, constantDefinitions, staticFieldDefinitions, diagnostics, token) diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs index 8d19bbb9..7a442894 100644 --- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs +++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.HlslSource.cs @@ -59,7 +59,7 @@ partial class ComputeShaderDescriptorGenerator Dictionary instanceMethods = new(SymbolEqualityComparer.Default); Dictionary constructors = new(SymbolEqualityComparer.Default); Dictionary constantDefinitions = new(SymbolEqualityComparer.Default); - Dictionary staticFieldDefinitions = new(SymbolEqualityComparer.Default); + Dictionary staticFieldDefinitions = new(SymbolEqualityComparer.Default); // Setup the semantic model and basic properties INamedTypeSymbol? pixelShaderSymbol = structDeclarationSymbol.AllInterfaces.FirstOrDefault(static interfaceSymbol => interfaceSymbol is { IsGenericType: true, Name: "IComputeShader" }); @@ -68,15 +68,17 @@ partial class ComputeShaderDescriptorGenerator token.ThrowIfCancellationRequested(); - (ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, ImmutableArray<(string Name, string HlslType)> valueFields) = GetInstanceFields( + GetInstanceFields( diagnostics, structDeclarationSymbol, discoveredTypes, - isComputeShader); + isComputeShader, + out ImmutableArray resourceFields, + out ImmutableArray valueFields); token.ThrowIfCancellationRequested(); - ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers = GetSharedBuffers( + ImmutableArray sharedBuffers = GetSharedBuffers( diagnostics, structDeclarationSymbol, discoveredTypes); @@ -85,7 +87,7 @@ partial class ComputeShaderDescriptorGenerator SemanticModelProvider semanticModelProvider = new(compilation); - (string entryPoint, ImmutableArray<(string Signature, string Definition)> processedMethods, isSamplerUsed) = GetProcessedMethods( + (string entryPoint, ImmutableArray processedMethods, isSamplerUsed) = GetProcessedMethods( diagnostics, structDeclarationSymbol, semanticModelProvider, @@ -100,8 +102,7 @@ partial class ComputeShaderDescriptorGenerator token.ThrowIfCancellationRequested(); - (string, string)? implicitSamplerField = isSamplerUsed ? ("SamplerState", "__sampler") : default((string, string)?); - ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields = GetStaticFields( + ImmutableArray staticFields = GetStaticFields( diagnostics, semanticModelProvider, structDeclarationSymbol, @@ -113,7 +114,7 @@ partial class ComputeShaderDescriptorGenerator token.ThrowIfCancellationRequested(); // Process the discovered types and constants - ImmutableArray<(string Name, string Definition)> declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes( + ImmutableArray declaredTypes = HlslDefinitionsSyntaxProcessor.GetDeclaredTypes( diagnostics, structDeclarationSymbol, discoveredTypes, @@ -122,7 +123,7 @@ partial class ComputeShaderDescriptorGenerator token.ThrowIfCancellationRequested(); - ImmutableArray<(string Name, string Value)> definedConstants = HlslDefinitionsSyntaxProcessor.GetDefinedConstants(constantDefinitions); + ImmutableArray definedConstants = HlslDefinitionsSyntaxProcessor.GetDefinedConstants(constantDefinitions); token.ThrowIfCancellationRequested(); @@ -154,18 +155,18 @@ partial class ComputeShaderDescriptorGenerator /// The input instance to process. /// The collection of currently discovered types. /// Indicates whether or not represents a compute shader. - /// A sequence of captured fields in . - private static ( - ImmutableArray<(string MetadataName, string Name, string HlslType)>, - ImmutableArray<(string Name, string HlslType)>) - GetInstanceFields( - ImmutableArrayBuilder diagnostics, - INamedTypeSymbol structDeclarationSymbol, - ICollection types, - bool isComputeShader) + /// The resulting sequence of resource instance fields for the current shader. + /// The resulting sequence of value instance fields for the current shader. + private static void GetInstanceFields( + ImmutableArrayBuilder diagnostics, + INamedTypeSymbol structDeclarationSymbol, + ICollection types, + bool isComputeShader, + out ImmutableArray resourceFields, + out ImmutableArray valueFields) { - using ImmutableArrayBuilder<(string, string, string)> resources = new(); - using ImmutableArrayBuilder<(string, string)> values = new(); + using ImmutableArrayBuilder resources = new(); + using ImmutableArrayBuilder values = new(); bool hlslResourceFound = false; @@ -243,7 +244,8 @@ partial class ComputeShaderDescriptorGenerator diagnostics.Add(MissingShaderResources, structDeclarationSymbol, structDeclarationSymbol); } - return (resources.ToImmutable(), values.ToImmutable()); + resourceFields = resources.ToImmutable(); + valueFields = values.ToImmutable(); } /// @@ -257,16 +259,16 @@ partial class ComputeShaderDescriptorGenerator /// The collection of discovered static field definitions. /// The used to cancel the operation, if needed. /// A sequence of static constant fields in . - private static ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> GetStaticFields( + private static ImmutableArray GetStaticFields( ImmutableArrayBuilder diagnostics, SemanticModelProvider semanticModel, INamedTypeSymbol structDeclarationSymbol, ICollection discoveredTypes, IDictionary constantDefinitions, - IDictionary staticFieldDefinitions, + IDictionary staticFieldDefinitions, CancellationToken token) { - using ImmutableArrayBuilder<(string, string, string?)> builder = new(); + using ImmutableArrayBuilder builder = new(); foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers()) { @@ -301,7 +303,7 @@ partial class ComputeShaderDescriptorGenerator } // Also gather the external static fields - foreach ((string, string, string?) externalField in staticFieldDefinitions.Values) + foreach (HlslStaticField externalField in staticFieldDefinitions.Values) { builder.Add(externalField); } @@ -316,12 +318,12 @@ partial class ComputeShaderDescriptorGenerator /// The input instance to process. /// The collection of currently discovered types. /// A sequence of captured members in . - private static ImmutableArray<(string Name, string Type, int? Count)> GetSharedBuffers( + private static ImmutableArray GetSharedBuffers( ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, ICollection types) { - using ImmutableArrayBuilder<(string, string, int?)> builder = new(); + using ImmutableArrayBuilder builder = new(); foreach (ISymbol memberSymbol in structDeclarationSymbol.GetMembers()) { @@ -365,7 +367,7 @@ partial class ComputeShaderDescriptorGenerator /// Indicates whether or not represents a compute shader. /// The used to cancel the operation, if needed. /// A sequence of processed methods in , and the entry point. - private static (string EntryPoint, ImmutableArray<(string Signature, string Definition)> Methods, bool IsSamplerUser) GetProcessedMethods( + private static (string EntryPoint, ImmutableArray Methods, bool IsSamplerUser) GetProcessedMethods( ImmutableArrayBuilder diagnostics, INamedTypeSymbol structDeclarationSymbol, SemanticModelProvider semanticModel, @@ -374,11 +376,11 @@ partial class ComputeShaderDescriptorGenerator IDictionary instanceMethods, IDictionary constructors, IDictionary constantDefinitions, - IDictionary staticFieldDefinitions, + IDictionary staticFieldDefinitions, bool isComputeShader, CancellationToken token) { - using ImmutableArrayBuilder<(string, string)> methods = new(); + using ImmutableArrayBuilder methods = new(); string? entryPoint = null; bool isSamplerUsed = false; @@ -500,13 +502,13 @@ partial class ComputeShaderDescriptorGenerator int threadsX, int threadsY, int threadsZ, - ImmutableArray<(string Name, string Value)> definedConstants, - ImmutableArray<(string Name, string Definition)> declaredTypes, - ImmutableArray<(string MetadataName, string Name, string HlslType)> resourceFields, - ImmutableArray<(string Name, string HlslType)> valueFields, - ImmutableArray<(string Name, string TypeDeclaration, string? Assignment)> staticFields, - ImmutableArray<(string Name, string Type, int? Count)> sharedBuffers, - ImmutableArray<(string Signature, string Definition)> processedMethods, + ImmutableArray definedConstants, + ImmutableArray declaredTypes, + ImmutableArray resourceFields, + ImmutableArray valueFields, + ImmutableArray staticFields, + ImmutableArray sharedBuffers, + ImmutableArray processedMethods, bool isComputeShader, string? implicitTextureType, bool isSamplerUsed, @@ -530,15 +532,15 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLine($"#define __GroupSize__get_Z {threadsZ}"); // Define declarations - foreach ((string name, string value) in definedConstants) + foreach (HlslConstant constant in definedConstants) { - writer.WriteLine($"#define {name} {value}"); + writer.WriteLine($"#define {constant.Name} {constant.Value}"); } writer.WriteLine(skipIfPresent: true); // Static fields - foreach ((string Name, string TypeDeclaration, string? Assignment) field in staticFields) + foreach (HlslStaticField field in staticFields) { if (field.Assignment is string assignment) { @@ -551,10 +553,10 @@ partial class ComputeShaderDescriptorGenerator } // Declared types - foreach ((string _, string typeDefinition) in declaredTypes) + foreach (HlslUserType userType in declaredTypes) { writer.WriteLine(skipIfPresent: true); - writer.WriteLine(typeDefinition); + writer.WriteLine(userType.Definition); } writer.WriteLine(skipIfPresent: true); @@ -568,9 +570,9 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLineIf(isComputeShader, "uint __z;"); // User-defined values - foreach ((string fieldName, string fieldType) in valueFields) + foreach (HlslValueField valueField in valueFields) { - writer.WriteLine($"{fieldType} {fieldName};"); + writer.WriteLine($"{valueField.Type} {valueField.Name};"); } } @@ -587,50 +589,50 @@ partial class ComputeShaderDescriptorGenerator writer.WriteLineIf(isSamplerUsed, "SamplerState __sampler : register(s);"); // Resources - foreach ((string metadataName, string fieldName, string fieldType) in resourceFields) + foreach (HlslResourceField resourceField in resourceFields) { writer.WriteLine(skipIfPresent: true); - if (HlslKnownTypes.IsConstantBufferType(metadataName)) + if (HlslKnownTypes.IsConstantBufferType(resourceField.MetadataName)) { - writer.WriteLine($"cbuffer _{fieldName} : register(b{constantBuffersCount++})"); + writer.WriteLine($"cbuffer _{resourceField.Name} : register(b{constantBuffersCount++})"); using (writer.WriteBlock()) { - writer.WriteLine($"{fieldType} {fieldName}[2];"); + writer.WriteLine($"{resourceField.Type} {resourceField.Name}[2];"); } } - else if (HlslKnownTypes.IsReadOnlyTypedResourceType(metadataName)) + else if (HlslKnownTypes.IsReadOnlyTypedResourceType(resourceField.MetadataName)) { - writer.WriteLine($"{fieldType} {fieldName} : register(t{readOnlyBuffersCount++});"); + writer.WriteLine($"{resourceField.Type} {resourceField.Name} : register(t{readOnlyBuffersCount++});"); } - else if (HlslKnownTypes.IsReadWriteTypedResourceType(metadataName)) + else if (HlslKnownTypes.IsReadWriteTypedResourceType(resourceField.MetadataName)) { - writer.WriteLine($"{fieldType} {fieldName} : register(u{readWriteBuffersCount++});"); + writer.WriteLine($"{resourceField.Type} {resourceField.Name} : register(u{readWriteBuffersCount++});"); } } // Shared buffers - foreach ((string bufferName, string bufferType, int? bufferCount) in sharedBuffers) + foreach (HlslSharedBuffer buffer in sharedBuffers) { - object count = (object?)bufferCount ?? "__GroupSize__get_X * __GroupSize__get_Y * __GroupSize__get_Z"; + object count = (object?)buffer.Count ?? "__GroupSize__get_X * __GroupSize__get_Y * __GroupSize__get_Z"; writer.WriteLine(skipIfPresent: true); - writer.WriteLine($"groupshared {bufferType} {bufferName} [{count}];"); + writer.WriteLine($"groupshared {buffer.Type} {buffer.Name} [{count}];"); } // Forward declarations - foreach ((string forwardDeclaration, string _) in processedMethods) + foreach (HlslMethod method in processedMethods) { writer.WriteLine(skipIfPresent: true); - writer.WriteLine(forwardDeclaration); + writer.WriteLine(method.Signature); } // Captured methods - foreach ((string _, string method) in processedMethods) + foreach (HlslMethod method in processedMethods) { writer.WriteLine(skipIfPresent: true); - writer.WriteLine(method); + writer.WriteLine(method.Declaration); } // Entry point