Add ExcedeedPixelShaderDispatchDataSizeAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2024-01-07 20:55:26 +01:00
1 親 d1578bb997
コミット 38c885f74e
5ファイル変更
+161 -29

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

+5 -5
ファイルの表示
@@ -10,9 +10,9 @@ partial class ComputeShaderDescriptorGenerator
/// </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>
/// <param name="result">Whether <paramref name="typeSymbol"/> is a "pixel shader like" type.</param>
/// <param name="isPixelShaderLike">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)
private static bool TryGetIsPixelShaderLike(INamedTypeSymbol typeSymbol, Compilation compilation, out bool isPixelShaderLike)
{
INamedTypeSymbol computeShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader")!;
INamedTypeSymbol pixelShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1")!;
@@ -21,19 +21,19 @@ partial class ComputeShaderDescriptorGenerator
{
if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol))
{
result = false;
isPixelShaderLike = false;
return true;
}
else if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol))
{
result = true;
isPixelShaderLike = true;
return true;
}
}
result = false;
isPixelShaderLike = false;
return false;
}
-19
ファイルの表示
@@ -3,11 +3,9 @@ using System.Collections.Immutable;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGeneration.SyntaxProcessors;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
@@ -22,19 +20,15 @@ partial class ComputeShaderDescriptorGenerator
/// <summary>
/// Gathers info on all resources captured by a given shader type.
/// </summary>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
/// <param name="structDeclarationSymbol">The current shader type being explored.</param>
/// <param name="isImplicitTextureUsed">Indicates whether the current shader uses an implicit texture.</param>
/// <param name="constantBufferSizeInBytes">The size of the shader constant buffer.</param>
/// <param name="resources">The sequence of <see cref="ResourceInfo"/> instances for all captured resources.</param>
/// <param name="resourceDescriptors">The sequence of <see cref="ResourceDescriptor"/> instances for all captured resources.</param>
public static void GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
Compilation compilation,
ITypeSymbol structDeclarationSymbol,
bool isImplicitTextureUsed,
int constantBufferSizeInBytes,
out ImmutableArray<ResourceInfo> resources,
out ImmutableArray<ResourceDescriptor> resourceDescriptors)
{
@@ -96,19 +90,6 @@ partial class ComputeShaderDescriptorGenerator
resources = resourceBuilder.ToImmutable();
resourceDescriptors = resourceDescriptorBuilder.ToImmutable();
// A shader root signature has a maximum size of 64 DWORDs, so 256 bytes.
// Loaded values in the root signature have the following costs:
// - Root constants cost 1 DWORD each, since they are 32-bit values.
// - Descriptor tables cost 1 DWORD each.
// - Root descriptors (64-bit GPU virtual addresses) cost 2 DWORDs each.
// So here we check whether the current signature respects that constraint,
// and emit a build error otherwise. For more info on this, see the docs here:
// https://docs.microsoft.com/windows/win32/direct3d12/root-signature-limits.
if ((constantBufferSizeInBytes / sizeof(int)) + resourceBuilder.Count > 64)
{
diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
}
}
/// <summary>
-2
ファイルの表示
@@ -98,11 +98,9 @@ public sealed partial class ComputeShaderDescriptorGenerator : IIncrementalGener
// Get the resources info
Resources.GetInfo(
diagnostics,
context.SemanticModel.Compilation,
typeSymbol,
isImplicitTextureUsed,
constantBufferSizeInBytes,
out ImmutableArray<ResourceInfo> resourceInfo,
out ImmutableArray<ResourceDescriptor> resourceDescriptors);
@@ -0,0 +1,153 @@
using System.Collections.Immutable;
using System.Linq;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Mappings;
using ComputeSharp.SourceGeneration.SyntaxProcessors;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates errors when a compute shader exceeds the maximum root descriptor size.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class ExcedeedComputeShaderDispatchDataSizeAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(ShaderDispatchDataSizeExceeded);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the IComputeShader and IComputeShader<TPixel> symbols
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader") is not { } computeShaderSymbol ||
context.Compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader`1") is not { } pixelShaderSymbol)
{
return;
}
context.RegisterSymbolAction(context =>
{
// Only struct types are possible targets
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
{
return;
}
// If the type is not a compute shader type, immediately bail out
if (!TryGetIsPixelShaderLike(typeSymbol, computeShaderSymbol, pixelShaderSymbol, out bool isPixelShaderLike))
{
return;
}
// Setup the resource and byte offsets for tracking. This is the same logic as
// in ComputeShaderDescriptorGenerator.ConstantBuffer, see there for more info.
int constantBufferSizeInBytes = sizeof(int) * (isPixelShaderLike ? 2 : 3);
// Run the fast-path constant buffer processor logic (same as in the equivalent D2D analyzer)
ConstantBufferSyntaxProcessor.GetInfo(
context.Compilation,
typeSymbol,
ref constantBufferSizeInBytes);
// Pad the size to get the number of root DWORD constants (see more notes in the same method)
constantBufferSizeInBytes = AlignmentHelper.Pad(constantBufferSizeInBytes, sizeof(int));
// Also get the number of captured resources
int resourceCount = GetNumberOfResources(typeSymbol);
// A shader root signature has a maximum size of 64 DWORDs, so 256 bytes.
// Loaded values in the root signature have the following costs:
// - Root constants cost 1 DWORD each, since they are 32-bit values.
// - Descriptor tables cost 1 DWORD each.
// - Root descriptors (64-bit GPU virtual addresses) cost 2 DWORDs each.
// So here we check whether the current signature respects that constraint,
// and emit a build error otherwise. For more info on this, see the docs here:
// https://docs.microsoft.com/windows/win32/direct3d12/root-signature-limits.
int numberOfDwordConstants = (constantBufferSizeInBytes / sizeof(int)) + resourceCount;
// Emit an error in case we have in fact exceeded that limit
if (numberOfDwordConstants > 64)
{
context.ReportDiagnostic(Diagnostic.Create(
ShaderDispatchDataSizeExceeded,
typeSymbol.Locations.First(),
typeSymbol,
numberOfDwordConstants));
}
}, SymbolKind.NamedType);
});
}
/// <summary>
/// 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="computeShaderSymbol">The <see cref="INamedTypeSymbol"/> for <c>"ComputeSharp.IComputeShader"</c>.</param>
/// <param name="pixelShaderSymbol">The <see cref="INamedTypeSymbol"/> for <c>"ComputeSharp.IComputeShader`1"</c>.</param>
/// <param name="isPixelShaderLike">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,
INamedTypeSymbol computeShaderSymbol,
INamedTypeSymbol pixelShaderSymbol,
out bool isPixelShaderLike)
{
foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces)
{
if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol))
{
isPixelShaderLike = false;
return true;
}
else if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol))
{
isPixelShaderLike = true;
return true;
}
}
isPixelShaderLike = false;
return false;
}
/// <summary>
/// Gets the number of captured resources in a given compute shader type.
/// </summary>
/// <param name="typeSymbol">The shader type to inspect.</param>
/// <returns>The number of captured resources in the given shader type.</returns>
private static int GetNumberOfResources(ITypeSymbol typeSymbol)
{
int resourceCount = 0;
foreach (ISymbol memberSymbol in typeSymbol.GetMembers())
{
// Only process instance fields
if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } fieldSymbol)
{
continue;
}
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
// Check if the field is a resource
if (HlslKnownTypes.IsTypedResourceType(typeName))
{
resourceCount++;
}
}
return resourceCount;
}
}
+3 -3
ファイルの表示
@@ -560,17 +560,17 @@ partial class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for a shader with a root signature that is too large.
/// <para>
/// Format: <c>"The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources"</c>.
/// Format: <c>"The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources (the maximum size for the root signature is 64 DWORD constants, but the actual size was {1})"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor ShaderDispatchDataSizeExceeded = new(
id: "CMPS0041",
title: "Shader dispatch data size exceeded",
messageFormat: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources",
messageFormat: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources (the maximum size for the root signature is 64 DWORD constants, but the actual size was {1})",
category: "ComputeSharp.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources.",
description: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources (the maximum size for the root signature is 64 DWORD constants).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>