diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
index 0613edd1..bdbc1087 100644
--- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
+++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md
@@ -52,3 +52,4 @@ CMPS0042 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator,
CMPS0043 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0044 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPS0045 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+CMPS0046 | ComputeSharp.IComputeShader | Error | IComputeShaderDataLoaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
index b49d7ce1..03ca810a 100644
--- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
+++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs
@@ -630,5 +630,21 @@ namespace ComputeSharp.SourceGenerators.Diagnostics
isEnabledByDefault: true,
description: "Property declarations cannot be used in a compute shader.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
+
+ ///
+ /// Gets a for a shader with a root signature that is too large.
+ ///
+ /// Format: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources".
+ ///
+ ///
+ public static readonly DiagnosticDescriptor ShaderDispatchDataSizeExceeded = new(
+ id: "CMPS0046",
+ title: "Shader dispatch data size exceeded",
+ messageFormat: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources",
+ category: typeof(IComputeShader).FullName,
+ defaultSeverity: DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: "The compute shader of type {0} has exceeded the maximum allowed size for captured values and resources.",
+ helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
}
diff --git a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
index 9955ab55..76edda77 100644
--- a/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IComputeShaderDataLoaderGenerator.cs
@@ -79,7 +79,7 @@ namespace ComputeSharp.SourceGenerators
if (!structDeclarationSymbol.Interfaces.Any(static interfaceSymbol => interfaceSymbol.Name == nameof(IComputeShader))) return;
TypeSyntax shaderType = ParseTypeName(structDeclarationSymbol.ToDisplayString());
- BlockSyntax block = Block(GetDispatchDataLoadingStatements(structDeclarationSymbol, out int root32BitConstantsCount));
+ BlockSyntax block = Block(GetDispatchDataLoadingStatements(context, structDeclarationSymbol, out int root32BitConstantsCount));
// Create a static method to create the combined hashcode for a given shader type.
// This code takes a block syntax and produces a compilation unit as follows:
@@ -148,11 +148,12 @@ namespace ComputeSharp.SourceGenerators
///
/// Gets a sequence of statements to load the dispatch data for a given shader
///
+ /// The current generator context in use.
/// The input instance to process.
/// The total number of 32 bit root constants to load.
/// The sequence of instances to load shader dispatch data.
[Pure]
- private static IEnumerable GetDispatchDataLoadingStatements(INamedTypeSymbol structDeclarationSymbol, out int count)
+ private static IEnumerable GetDispatchDataLoadingStatements(GeneratorExecutionContext context, INamedTypeSymbol structDeclarationSymbol, out int count)
{
List statements = new();
@@ -172,6 +173,21 @@ namespace ComputeSharp.SourceGenerators
count = rawDataOffset / sizeof(int);
+ // 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 rootSignatureDwordSize = count + resourceOffset;
+
+ if (rootSignatureDwordSize > 64)
+ {
+ context.ReportDiagnostic(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol);
+ }
+
return statements;
}
diff --git a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
index 8b3799fc..0b1702d7 100644
--- a/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
+++ b/tests/ComputeSharp.Tests.SourceGenerators/DiagnosticsTests.cs
@@ -1338,6 +1338,38 @@ namespace ComputeSharp.Tests.SourceGenerators
VerifyGeneratedDiagnostics(source, "CMPS0045");
}
+ [TestMethod]
+ public void ShaderDispatchDataSizeExceeded()
+ {
+ string source = @"
+ using ComputeSharp;
+
+ namespace ComputeSharp
+ {
+ public class ReadWriteBuffer { }
+ public struct Double4x4 { }
+ public struct Float4 { }
+ }
+
+ namespace MyFancyApp.Sample
+ {
+ public struct MyShader : IComputeShader
+ {
+ public readonly ReadWriteBuffer buffer;
+ public readonly Double4x4 a;
+ public readonly Double4x4 b;
+ public readonly int c;
+ public readonly Float4 d;
+
+ public void Execute()
+ {
+ }
+ }
+ }";
+
+ VerifyGeneratedDiagnostics(source, "CMPS0046");
+ }
+
///
/// Verifies the output of a source generator.
///