Remove ShaderType type, simplify code

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-13 23:07:48 +02:00
1 親 61f155bfb6
コミット bad605164a
6ファイル変更
+33 -51

検索条件に一致するファイルはありません

@@ -19,13 +19,13 @@ partial class IShaderGenerator
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>LoadDispatchDataMethod</c> method.
/// </summary>
/// <param name="shaderType">The type of shader interface currently being processed.</param>
/// <param name="isPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="fieldInfos">The array of <see cref="FieldInfo"/> values for all captured fields.</param>
/// <param name="resourceCount">The total number of captured resources in the shader.</param>
/// <param name="root32BitConstantsCount">The total number of needed 32 bit constants in the shader root signature.</param>
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>LoadDispatchDataMethod</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(
ShaderType shaderType,
bool isPixelShaderLike,
ImmutableArray<FieldInfo> 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)));
}
/// <summary>
/// Gets a sequence of statements to load the dispatch data for a given shader.
/// </summary>
/// <param name="shaderType">The type of shader interface currently being processed.</param>
/// <param name="isPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="fieldInfos">The array of <see cref="FieldInfo"/> values for all captured fields.</param>
/// <param name="resourceCount">The total number of captured resources in the shader.</param>
/// <param name="root32BitConstantsCount">The total number of needed 32 bit constants in the shader root signature.</param>
/// <returns>The sequence of <see cref="StatementSyntax"/> instances to load shader dispatch data.</returns>
private static ImmutableArray<StatementSyntax> GetDispatchDataLoadingStatements(
ShaderType shaderType,
bool isPixelShaderLike,
ImmutableArray<FieldInfo> 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(
+3 -4
ファイルの表示
@@ -27,14 +27,14 @@ partial class IShaderGenerator
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="structDeclarationSymbol">The current shader type being explored.</param>
/// <param name="shaderType">The type of shader currently being processed.</param>
/// <param name="isPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="resourceCount">The total number of captured resources in the shader.</param>
/// <param name="root32BitConstantCount">The total number of needed 32 bit constants in the shader root signature.</param>
/// <returns>The sequence of <see cref="FieldInfo"/> instances for all captured resources and values.</returns>
public static ImmutableArray<FieldInfo> GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> 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<int> resourceOffsetAsBox = new();
StrongBox<int> rawDataOffsetAsBox = new(initialRawDataOffset);
+17 -17
ファイルの表示
@@ -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
{
/// <summary>
/// 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).
/// </summary>
/// <param name="typeSymbol">The input <see cref="INamedTypeSymbol"/> instance to check.</param>
/// <param name="compilation">The <see cref="Compilation"/> instance currently in use.</param>
/// <returns>The shader type for <paramref name="typeSymbol"/>, or <see langword="null"/>.</returns>
private static ShaderType? GetShaderType(INamedTypeSymbol typeSymbol, Compilation compilation)
/// <param name="result">Whether <paramref name="typeSymbol"/> is a "pixel shader like" type.</param>
/// <returns>Whether <paramref name="typeSymbol"/> is a compute shader type at all.</returns>
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<byte>) })
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;
}
/// <summary>
+5 -5
ファイルの表示
@@ -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<FieldInfo> 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);
+2 -2
ファイルの表示
@@ -5,12 +5,12 @@ namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing gathered info on a shader dispatch data.
/// </summary>
/// <param name="Type">The shader interface type.</param>
/// <param name="IsPixelShaderLike">Whether the compute shader is "pixel shader like", ie. outputting a pixel into a target texture.</param>
/// <param name="FieldInfos">The description on shader instance fields.</param>
/// <param name="ResourceCount">The total number of captured resources.</param>
/// <param name="Root32BitConstantCount">The size of the shader root signature, in 32 bit constants.</param>
internal sealed record DispatchDataInfo(
ShaderType Type,
bool IsPixelShaderLike,
EquatableArray<FieldInfo> FieldInfos,
int ResourceCount,
int Root32BitConstantCount);
-17
ファイルの表示
@@ -1,17 +0,0 @@
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// Indicates a type of shader being processed.
/// </summary>
public enum ShaderType
{
/// <summary>
/// A compute shader.
/// </summary>
ComputeShader,
/// <summary>
/// A pixel shader.
/// </summary>
PixelShader
}