From bad605164a9ebd975e17e0b3592f0069abfba591 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 13 Oct 2023 20:35:03 +0200 Subject: [PATCH] Remove ShaderType type, simplify code --- ...tor.CreateLoadDispatchDataMethod.Syntax.cs | 12 +++---- ...rGenerator.CreateLoadDispatchDataMethod.cs | 7 ++-- .../IShaderGenerator.Helpers.cs | 34 +++++++++---------- .../IShaderGenerator.cs | 10 +++--- .../Models/DispatchDataInfo.cs | 4 +-- .../Models/ShaderType.cs | 17 ---------- 6 files changed, 33 insertions(+), 51 deletions(-) delete mode 100644 src/ComputeSharp.SourceGenerators/Models/ShaderType.cs diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs index d73836c6..71cb2dcb 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs @@ -19,13 +19,13 @@ partial class IShaderGenerator /// /// Creates a instance for the LoadDispatchDataMethod method. /// - /// The type of shader interface currently being processed. + /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// The array of values for all captured fields. /// The total number of captured resources in the shader. /// The total number of needed 32 bit constants in the shader root signature. /// The resulting instance for the LoadDispatchDataMethod method. public static MethodDeclarationSyntax GetSyntax( - ShaderType shaderType, + bool isPixelShaderLike, ImmutableArray fieldInfos, int resourceCount, int root32BitConstantsCount) @@ -51,19 +51,19 @@ partial class IShaderGenerator Parameter(Identifier("x")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("y")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))), Parameter(Identifier("z")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword)))) - .WithBody(Block(GetDispatchDataLoadingStatements(shaderType, fieldInfos, resourceCount, root32BitConstantsCount))); + .WithBody(Block(GetDispatchDataLoadingStatements(isPixelShaderLike, fieldInfos, resourceCount, root32BitConstantsCount))); } /// /// Gets a sequence of statements to load the dispatch data for a given shader. /// - /// The type of shader interface currently being processed. + /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// The array of values for all captured fields. /// The total number of captured resources in the shader. /// The total number of needed 32 bit constants in the shader root signature. /// The sequence of instances to load shader dispatch data. private static ImmutableArray GetDispatchDataLoadingStatements( - ShaderType shaderType, + bool isPixelShaderLike, ImmutableArray fieldInfos, int resourceCount, int root32BitConstantsCount) @@ -157,7 +157,7 @@ partial class IShaderGenerator // If the shader is a compute shader, also track the bounds on the Z axis: // // span0[2] = (uint)z; - if (shaderType == ShaderType.ComputeShader) + if (!isPixelShaderLike) { statements.Add( ExpressionStatement( diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs index 6a3cba0f..0636ebc0 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadDispatchDataMethod.cs @@ -27,14 +27,14 @@ partial class IShaderGenerator /// /// The collection of produced instances. /// The current shader type being explored. - /// The type of shader currently being processed. + /// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// The total number of captured resources in the shader. /// The total number of needed 32 bit constants in the shader root signature. /// The sequence of instances for all captured resources and values. public static ImmutableArray GetInfo( ImmutableArrayBuilder diagnostics, ITypeSymbol structDeclarationSymbol, - ShaderType shaderType, + bool isPixelShaderLike, out int resourceCount, out int root32BitConstantCount) { @@ -91,8 +91,7 @@ partial class IShaderGenerator // Setup the resource and byte offsets for tracking. Pixel shaders have only two // implicitly captured int values, as they're always dispatched over a 2D texture. - bool isComputeShader = shaderType == ShaderType.ComputeShader; - int initialRawDataOffset = sizeof(int) * (isComputeShader ? 3 : 2); + int initialRawDataOffset = sizeof(int) * (isPixelShaderLike ? 2 : 3); StrongBox resourceOffsetAsBox = new(); StrongBox rawDataOffsetAsBox = new(initialRawDataOffset); diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 465269b7..6a29ab66 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using ComputeSharp.SourceGeneration.Models; -using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -12,36 +11,37 @@ namespace ComputeSharp.SourceGenerators; partial class IShaderGenerator { /// - /// Gets the shader type for a given shader, if any. + /// Gets whether a given type is a compute shader type (ie. implements any of the interfaces). /// /// The input instance to check. /// The instance currently in use. - /// The shader type for , or . - private static ShaderType? GetShaderType(INamedTypeSymbol typeSymbol, Compilation compilation) + /// Whether is a "pixel shader like" type. + /// Whether is a compute shader type at all. + private static bool TryGetIsPixelShaderLike(INamedTypeSymbol typeSymbol, Compilation compilation, out bool result) { + INamedTypeSymbol computeShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader")!; + INamedTypeSymbol pixelShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IPixelShader`1")!; + foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { - if (interfaceSymbol.Name == nameof(IComputeShader)) + if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol)) { - INamedTypeSymbol computeShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader")!; + result = false; - if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol)) - { - return ShaderType.ComputeShader; - } + return true; } - else if (interfaceSymbol is { IsGenericType: true, Name: nameof(IPixelShader) }) + else if (interfaceSymbol is { IsGenericType: true } && + SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol)) { - INamedTypeSymbol pixelShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IPixelShader`1")!; + result = true; - if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol)) - { - return ShaderType.PixelShader; - } + return true; } } - return null; + result = false; + + return false; } /// diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs index 391b730e..4523ab32 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs @@ -48,8 +48,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator return default; } - // Get the shader type, or return if none is present - if (GetShaderType(typeSymbol, context.SemanticModel.Compilation) is not ShaderType shaderType) + // Check whether type is a compute shader, and if so, if it's pixel shader like + if (!TryGetIsPixelShaderLike(typeSymbol, context.SemanticModel.Compilation, out bool isPixelShaderLike)) { return default; } @@ -60,7 +60,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator ImmutableArray fieldInfos = LoadDispatchData.GetInfo( diagnostics, typeSymbol, - shaderType, + isPixelShaderLike, out int resourceCount, out int root32BitConstantCount); @@ -97,7 +97,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator return new ShaderInfo( Hierarchy: HierarchyInfo.From(typeSymbol), DispatchData: new DispatchDataInfo( - shaderType, + isPixelShaderLike, fieldInfos, resourceCount, root32BitConstantCount), @@ -122,7 +122,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator context.RegisterSourceOutput(dispatchDataInfo, static (context, item) => { MethodDeclarationSyntax loadDispatchDataMethod = LoadDispatchData.GetSyntax( - item.DispatchData.Type, + item.DispatchData.IsPixelShaderLike, item.DispatchData.FieldInfos, item.DispatchData.ResourceCount, item.DispatchData.Root32BitConstantCount); diff --git a/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs b/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs index bf68a1ae..e07d16e8 100644 --- a/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs +++ b/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs @@ -5,12 +5,12 @@ namespace ComputeSharp.SourceGenerators.Models; /// /// A model representing gathered info on a shader dispatch data. /// -/// The shader interface type. +/// Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture. /// The description on shader instance fields. /// The total number of captured resources. /// The size of the shader root signature, in 32 bit constants. internal sealed record DispatchDataInfo( - ShaderType Type, + bool IsPixelShaderLike, EquatableArray FieldInfos, int ResourceCount, int Root32BitConstantCount); \ No newline at end of file diff --git a/src/ComputeSharp.SourceGenerators/Models/ShaderType.cs b/src/ComputeSharp.SourceGenerators/Models/ShaderType.cs deleted file mode 100644 index 9abb0833..00000000 --- a/src/ComputeSharp.SourceGenerators/Models/ShaderType.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace ComputeSharp.SourceGenerators.Models; - -/// -/// Indicates a type of shader being processed. -/// -public enum ShaderType -{ - /// - /// A compute shader. - /// - ComputeShader, - - /// - /// A pixel shader. - /// - PixelShader -} \ No newline at end of file