Add ExcedeedPixelShaderDispatchDataSizeAnalyzer

このコミットが含まれているのは:
Sergio Pedri committed 2024-01-01 19:03:15 +01:00
1 親 942f3aa50b
コミット d687e3b499
4ファイル変更
+77 -19

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

@@ -1,4 +1,3 @@
using System;
using System.Collections.Immutable;
using ComputeSharp.D2D1.SourceGenerators.Models;
using ComputeSharp.SourceGeneration.Extensions;
@@ -6,7 +5,6 @@ using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGeneration.SyntaxProcessors;
using Microsoft.CodeAnalysis;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
@@ -19,13 +17,11 @@ partial class D2DPixelShaderDescriptorGenerator
private static class ConstantBuffer
{
/// <inheritdoc cref="ConstantBufferSyntaxProcessor.GetInfo"/>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
/// <param name="compilation"><inheritdoc cref="ConstantBufferSyntaxProcessor.GetInfo" path="/param[@name='compilation']/node()"/></param>
/// <param name="structDeclarationSymbol"><inheritdoc cref="ConstantBufferSyntaxProcessor.GetInfo" path="/param[@name='structDeclarationSymbol']/node()"/></param>
/// <param name="constantBufferSizeInBytes"><inheritdoc cref="ConstantBufferSyntaxProcessor.GetInfo" path="/param[@name='constantBufferSizeInBytes']/node()"/></param>
/// <param name="fields"><inheritdoc cref="ConstantBufferSyntaxProcessor.GetInfo" path="/param[@name='fields']/node()"/></param>
public static void GetInfo(
ImmutableArrayBuilder<DiagnosticInfo> diagnostics,
Compilation compilation,
ITypeSymbol structDeclarationSymbol,
out int constantBufferSizeInBytes,
@@ -38,16 +34,6 @@ partial class D2DPixelShaderDescriptorGenerator
structDeclarationSymbol,
ref constantBufferSizeInBytes,
out fields);
// The maximum size for a constant buffer is 64KB
const int D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT = 4096;
const int MaximumConstantBufferSize = D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * 4 * sizeof(float);
// Emit a diagnostic if the shader constant buffer is too large
if (constantBufferSizeInBytes > MaximumConstantBufferSize)
{
diagnostics.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
}
}
/// <summary>
-1
ファイルの表示
@@ -88,7 +88,6 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene
// Constant buffer info
ConstantBuffer.GetInfo(
diagnostics,
context.SemanticModel.Compilation,
typeSymbol,
out int constantBufferSizeInBytes,
@@ -0,0 +1,73 @@
using System.Collections.Immutable;
using System.Linq;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.SyntaxProcessors;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates errors when a D2D pixel shader exceeds the maximum dispatch data size.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class ExceededPixelShaderDispatchDataSizeAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(ExceededDispatchDataSize);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the ID2D1PixelShader symbol
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.ID2D1PixelShader") is not { } d2D1PixelShaderSymbol)
{
return;
}
context.RegisterSymbolAction(context =>
{
// We're validating all struct types (since they're the possible shader types)
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
{
return;
}
// Only validate fields for shader types (even without a generated descriptor)
if (!typeSymbol.HasInterfaceWithType(d2D1PixelShaderSymbol))
{
return;
}
int constantBufferSizeInBytes = 0;
// Run the fast-path constant buffer processor logic.
// This only extracts the constant buffer size.
ConstantBufferSyntaxProcessor.GetInfo(
context.Compilation,
typeSymbol,
ref constantBufferSizeInBytes);
// The maximum size for a constant buffer is 64KB
const int D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT = 4096;
const int MaximumConstantBufferSize = D3D11_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * 4 * sizeof(float);
// Emit a diagnostic if the shader constant buffer is too large
if (constantBufferSizeInBytes > MaximumConstantBufferSize)
{
context.ReportDiagnostic(Diagnostic.Create(
ExceededDispatchDataSize,
typeSymbol.Locations.First(),
typeSymbol,
constantBufferSizeInBytes));
}
}, SymbolKind.NamedType);
});
}
}
+4 -4
ファイルの表示
@@ -431,17 +431,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 D2D1 shader of type {0} has exceeded the maximum allowed size for captured values"</c>.
/// Format: <c>"The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values (the maximum size is 64KB, and the actual constant buffer size was {1} bytes)"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor ShaderDispatchDataSizeExceeded = new(
public static readonly DiagnosticDescriptor ExceededDispatchDataSize = new(
id: "CMPSD2D0032",
title: "Shader dispatch data size exceeded",
messageFormat: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values",
messageFormat: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values (the maximum size is 64KB, and the actual constant buffer size was {1} bytes)",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values.",
description: "The D2D1 shader of type {0} has exceeded the maximum allowed size for captured values (the maximum size for constant buffers is 64KB).",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>