Merge pull request #752 from Sergio0694/dev/constant-buffer-size-analyzer
Move D2D constant buffer size diagnostics to analyzer
このコミットが含まれているのは:
9ファイル変更
+257
-20
検索条件に一致するファイルはありません
-14
@@ -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>
|
||||
|
||||
@@ -88,7 +88,6 @@ public sealed partial class D2DPixelShaderDescriptorGenerator : IIncrementalGene
|
||||
|
||||
// Constant buffer info
|
||||
ConstantBuffer.GetInfo(
|
||||
diagnostics,
|
||||
context.SemanticModel.Compilation,
|
||||
typeSymbol,
|
||||
out int constantBufferSizeInBytes,
|
||||
|
||||
+73
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
+84
@@ -106,6 +106,63 @@ internal static partial class ConstantBufferSyntaxProcessor
|
||||
fields = fieldBuilder.ToImmutable();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GetInfo(Compilation, ITypeSymbol, ref int, out ImmutableArray{FieldInfo})"/>
|
||||
public static void GetInfo(
|
||||
Compilation compilation,
|
||||
ITypeSymbol structDeclarationSymbol,
|
||||
ref int constantBufferSizeInBytes)
|
||||
{
|
||||
// Like with the GetHlslKnownTypeFieldInfo overload below, this method is a minimal, optimized
|
||||
// version of the GetInfo overload above, only meant to be used by the analyzer to compute the
|
||||
// constant buffer size in bytes, and nothing else. See comments above for the code below.
|
||||
static void GetInfo(
|
||||
Compilation compilation,
|
||||
ITypeSymbol currentTypeSymbol,
|
||||
int fieldDepth,
|
||||
ref int constantBufferSizeInBytes)
|
||||
{
|
||||
bool isFirstField = true;
|
||||
|
||||
foreach (ISymbol memberSymbol in currentTypeSymbol.GetMembers())
|
||||
{
|
||||
// Only process fields of a valid shape/type (same as the previous method).
|
||||
// We don't need the additional checks here for accessibility or the field
|
||||
// being valid, since those would already get separate warnings.
|
||||
if (memberSymbol is not IFieldSymbol { Type: INamedTypeSymbol { IsStatic: false }, IsConst: false, IsStatic: false, IsFixedSizeBuffer: false } fieldSymbol)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
string typeName = fieldSymbol.Type.GetFullyQualifiedMetadataName();
|
||||
|
||||
// Pad items (same as method above)
|
||||
if (isFirstField && fieldDepth > 0)
|
||||
{
|
||||
constantBufferSizeInBytes = AlignmentHelper.Pad(constantBufferSizeInBytes, 16);
|
||||
|
||||
isFirstField = false;
|
||||
}
|
||||
|
||||
// Quickly inspect the shader fields and update the rolling buffer size
|
||||
if (HlslKnownTypes.IsKnownHlslType(typeName))
|
||||
{
|
||||
GetHlslKnownTypeFieldInfo(typeName, ref constantBufferSizeInBytes);
|
||||
}
|
||||
else if (fieldSymbol.Type.IsUnmanagedType)
|
||||
{
|
||||
GetInfo(compilation, fieldSymbol.Type, fieldDepth + 1, ref constantBufferSizeInBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Traverse all shader fields and simply track the buffer size
|
||||
GetInfo(
|
||||
compilation,
|
||||
structDeclarationSymbol,
|
||||
fieldDepth: 0,
|
||||
ref constantBufferSizeInBytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the field name and the accessor name for a given field.
|
||||
/// </summary>
|
||||
@@ -149,6 +206,7 @@ internal static partial class ConstantBufferSyntaxProcessor
|
||||
/// <param name="fieldPath">The current path of the field with respect to the shader instance.</param>
|
||||
/// <param name="typeName">The type name currently being read.</param>
|
||||
/// <param name="rawDataOffset">The current offset within the loaded data buffer.</param>
|
||||
/// <returns>The <see cref="FieldInfo"/> for the inspected shader field.</returns>
|
||||
private static FieldInfo GetHlslKnownTypeFieldInfo(
|
||||
ImmutableArray<FieldPathPart> fieldPath,
|
||||
string typeName,
|
||||
@@ -198,4 +256,30 @@ internal static partial class ConstantBufferSyntaxProcessor
|
||||
return new FieldInfo.Primitive(fieldPath, typeName, startingRawDataOffset);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="GetHlslKnownTypeFieldInfo(ImmutableArray{FieldPathPart}, string, ref int)"/>
|
||||
private static void GetHlslKnownTypeFieldInfo(string typeName, ref int rawDataOffset)
|
||||
{
|
||||
// Note: this method is a minimal, optimized version of the overload above, simply computing
|
||||
// the field offset and nothing else. See comments there for the code below. There are no
|
||||
// changes done here other than removing all redundant code to compute additional field info.
|
||||
(int fieldSize, int fieldPack) = HlslKnownSizes.GetTypeInfo(typeName);
|
||||
|
||||
if (HlslKnownTypes.IsNonLinearMatrixType(typeName, out _, out int rows, out int columns))
|
||||
{
|
||||
for (int j = 0; j < rows; j++)
|
||||
{
|
||||
rawDataOffset = AlignmentHelper.Pad(rawDataOffset, 16) + (fieldPack * columns);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int startingRawDataOffset = AlignmentHelper.AlignToBoundary(
|
||||
AlignmentHelper.Pad(rawDataOffset, fieldPack),
|
||||
fieldSize,
|
||||
16);
|
||||
|
||||
rawDataOffset = startingRawDataOffset + fieldSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ using ComputeSharp.SourceGeneration.SyntaxProcessors;
|
||||
using ComputeSharp.SourceGenerators.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
#pragma warning disable CS0419
|
||||
|
||||
namespace ComputeSharp.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project>
|
||||
<Import Project="..\build\Directory.Build.props" />
|
||||
|
||||
<!-- Centralized packaging metadata for published projects -->
|
||||
<!-- Centralized packaging metadata for published projects -->
|
||||
<PropertyGroup Condition="$(IsPackagedProject)">
|
||||
|
||||
<!-- Package descriptions-->
|
||||
|
||||
@@ -643,4 +643,96 @@ public class Test_Analyzers
|
||||
|
||||
await CSharpAnalyzerWithLanguageVersionTest<InvalidShaderTypeCompileOptionsAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ExceededDispatchDataSize_ConstantBufferTooLarge_Warns()
|
||||
{
|
||||
const string source = """
|
||||
using ComputeSharp.D2D1;
|
||||
using float1x3 = ComputeSharp.Float1x3;
|
||||
using float2x2 = ComputeSharp.Float2x2;
|
||||
using float3x2 = ComputeSharp.Float3x2;
|
||||
using float4x3 = ComputeSharp.Float4x3;
|
||||
using float4x4 = ComputeSharp.Float4x4;
|
||||
using float2 = ComputeSharp.Float2;
|
||||
using float3 = ComputeSharp.Float3;
|
||||
using float4 = ComputeSharp.Float4;
|
||||
|
||||
public struct Data0
|
||||
{
|
||||
public float4x4 m0;
|
||||
public float4 v0;
|
||||
public float4 v1;
|
||||
public float s0;
|
||||
public float s1;
|
||||
public float s2;
|
||||
public float s3;
|
||||
}
|
||||
|
||||
public struct Data1
|
||||
{
|
||||
public Data0 d0;
|
||||
public Data0 d1;
|
||||
public Data0 d2;
|
||||
public Data0 d3;
|
||||
public Data0 d4;
|
||||
public float3x2 m0;
|
||||
public float2x2 m1;
|
||||
public float3 v0;
|
||||
public float s0;
|
||||
public float s1;
|
||||
}
|
||||
|
||||
public struct Data2
|
||||
{
|
||||
public Data1 d0;
|
||||
public Data1 d1;
|
||||
public Data1 d2;
|
||||
public Data1 d3;
|
||||
public float2x2 m0;
|
||||
public float1x3 m1;
|
||||
public float3 v0;
|
||||
public float4 v1;
|
||||
public float2 v2;
|
||||
public float s0;
|
||||
}
|
||||
|
||||
public struct Data3
|
||||
{
|
||||
public Data1 d0;
|
||||
public Data1 d1;
|
||||
public Data1 d2;
|
||||
public Data1 d3;
|
||||
public Data2 d4;
|
||||
public Data2 d5;
|
||||
public Data2 d6;
|
||||
public Data2 d7;
|
||||
public Data2 d8;
|
||||
public Data2 d9;
|
||||
public float3 v0;
|
||||
}
|
||||
|
||||
public struct Data4
|
||||
{
|
||||
public Data3 d0;
|
||||
public Data3 d1;
|
||||
public Data3 d2;
|
||||
public Data3 d3;
|
||||
public float4x3 m0;
|
||||
public float s0;
|
||||
}
|
||||
|
||||
|
||||
[D2DInputCount(0)]
|
||||
internal readonly partial struct {|CMPSD2D0032:MyType|}(Data4 data4) : ID2D1PixelShader
|
||||
{
|
||||
public float4 Execute()
|
||||
{
|
||||
return data4.s0;
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
await CSharpAnalyzerWithLanguageVersionTest<ExceededPixelShaderDispatchDataSizeAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
}
|
||||
}
|
||||
@@ -1766,6 +1766,7 @@ public class Test_D2DPixelShaderDescriptorGenerator
|
||||
{
|
||||
await CSharpAnalyzerWithLanguageVersionTest<D2DEnableRuntimeCompilationOnAssemblyAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
await CSharpAnalyzerWithLanguageVersionTest<D2DEnableRuntimeCompilationOnTypeAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
await CSharpAnalyzerWithLanguageVersionTest<ExceededPixelShaderDispatchDataSizeAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
await CSharpAnalyzerWithLanguageVersionTest<InvalidAssemblyLevelCompileOptionsAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
await CSharpAnalyzerWithLanguageVersionTest<InvalidD2D1CompileOptionsEnableLinkingOnShaderTypeAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
await CSharpAnalyzerWithLanguageVersionTest<InvalidD2DGeneratedPixelShaderDescriptorAttributeTargetAnalyzer>.VerifyAnalyzerAsync(source);
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする