From 3e7a3c5b7c9bf8f1d55c7722991ef0f5de4dfe1b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 Jan 2022 13:32:21 +0100 Subject: [PATCH] Enable source generator support for DispatchAxis --- .../ColorfulInfinity.cs | 2 +- .../ContouredLayers.cs | 2 +- .../ExtrudedTruchetPattern.cs | 2 +- .../FourColorGradient.cs | 2 +- .../FractalTiling.cs | 2 +- .../HelloWorld.cs | 2 +- .../MengerJourney.cs | 2 +- .../Octagrams.cs | 2 +- .../ProteanClouds.cs | 2 +- .../PyramidPattern.cs | 2 +- .../TriangleGridContouring.cs | 2 +- .../TwoTiledTruchet.cs | 2 +- .../AnalyzerReleases.Shipped.md | 1 + .../Diagnostics/DiagnosticDescriptors.cs | 16 ++++++ ...haderGenerator.CreateLoadBytecodeMethod.cs | 54 ++++++++++++++++--- 15 files changed, 75 insertions(+), 20 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/ColorfulInfinity.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/ColorfulInfinity.cs index fbb83865..13437cbd 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/ColorfulInfinity.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/ColorfulInfinity.cs @@ -8,7 +8,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct ColorfulInfinity : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs index a089be50..90b878f8 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs @@ -7,7 +7,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct ContouredLayers : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/ExtrudedTruchetPattern.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/ExtrudedTruchetPattern.cs index c1805a42..dbaf8d63 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/ExtrudedTruchetPattern.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/ExtrudedTruchetPattern.cs @@ -7,7 +7,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct ExtrudedTruchetPattern : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/FourColorGradient.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/FourColorGradient.cs index 27c56503..7fa9a2ef 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/FourColorGradient.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/FourColorGradient.cs @@ -8,7 +8,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct FourColorGradient : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/FractalTiling.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/FractalTiling.cs index d9fc7195..06cf1d1e 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/FractalTiling.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/FractalTiling.cs @@ -8,7 +8,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct FractalTiling : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/HelloWorld.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/HelloWorld.cs index 0d2c3a80..793c234a 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/HelloWorld.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/HelloWorld.cs @@ -6,7 +6,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct HelloWorld : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/MengerJourney.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/MengerJourney.cs index 915bfbba..37f655d6 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/MengerJourney.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/MengerJourney.cs @@ -7,7 +7,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct MengerJourney : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs index 03b5d22b..cf09264b 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs @@ -7,7 +7,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct Octagrams : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/ProteanClouds.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/ProteanClouds.cs index 6ef57780..550ef977 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/ProteanClouds.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/ProteanClouds.cs @@ -8,7 +8,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct ProteanClouds : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/PyramidPattern.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/PyramidPattern.cs index b682aa29..b441299e 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/PyramidPattern.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/PyramidPattern.cs @@ -8,7 +8,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct PyramidPattern : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/TriangleGridContouring.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/TriangleGridContouring.cs index 3e4abfc0..a54daecf 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/TriangleGridContouring.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/TriangleGridContouring.cs @@ -7,7 +7,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct TriangleGridContouring : IPixelShader { diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/TwoTiledTruchet.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/TwoTiledTruchet.cs index 79b45dad..9c07eaac 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/TwoTiledTruchet.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/TwoTiledTruchet.cs @@ -7,7 +7,7 @@ /// [AutoConstructor] #if SAMPLE_APP -[EmbeddedBytecode(8, 8, 1)] +[EmbeddedBytecode(DispatchAxis.XY)] #endif internal readonly partial struct TwoTiledTruchet : IPixelShader { diff --git a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md index 571bb103..1e3bcdee 100644 --- a/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md +++ b/src/ComputeSharp.SourceGenerators/AnalyzerReleases.Shipped.md @@ -54,3 +54,4 @@ CMPS0044 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentatio CMPS0045 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0046 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) CMPS0047 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) +CMPS0048 | ComputeSharp.IComputeShader | Error | IShaderGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp) diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 4eedd7f1..00da57ab 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -662,4 +662,20 @@ internal static class DiagnosticDescriptors isEnabledByDefault: true, description: "When dynamic shader compilation is not supported (ie. when ComputeSharp.Dynamic is not referenced), all shaders need to be annotated as having embedded bytecode (using the [EmbeddedBytecode] attribute).", helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); + + /// + /// Gets a for an embedded bytecode shader with an invalid dispatch axis value. + /// + /// Format: "The shader of type {0} is annotated with an invalid dispatch axis value". + /// + /// + public static readonly DiagnosticDescriptor InvalidEmbeddedBytecodeDispatchAxis = new( + id: "CMPS0048", + title: "Invalid dispatch axis for shader with embedded bytecode", + messageFormat: "The shader of type {0} is annotated with with an invalid dispatch axis value", + category: typeof(IShaderGenerator).FullName, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true, + description: "The dispatch axis value for a shader marked as embedded bytecode have to be valid (flags are not supported).", + helpLinkUri: "https://github.com/Sergio0694/ComputeSharp"); } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs index 652c5a4e..da5d4aaa 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateLoadBytecodeMethod.cs @@ -59,21 +59,59 @@ public sealed partial class IShaderGenerator return new(true, 0, 0, 0); } - // Validate the thread number arguments - if (attribute.ConstructorArguments.Length != 3 || - attribute.ConstructorArguments[0].Value is not int threadsX || - attribute.ConstructorArguments[1].Value is not int threadsY || - attribute.ConstructorArguments[2].Value is not int threadsZ || - threadsX is < 1 or > 1024 || - threadsY is < 1 or > 1024 || - threadsZ is < 1 or > 64) + int threadsX; + int threadsY; + int threadsZ; + + // Check for a dispatch axis argument first + if (attribute.ConstructorArguments.Length == 1) { + int dispatchAxis = (int)attribute.ConstructorArguments[0].Value!; + + (threadsX, threadsY, threadsZ) = (DispatchAxis)dispatchAxis switch + { + DispatchAxis.X => (64, 1, 1), + DispatchAxis.Y => (1, 64, 1), + DispatchAxis.Z => (1, 1, 64), + DispatchAxis.XY => (8, 8, 1), + DispatchAxis.XZ => (8, 1, 8), + DispatchAxis.YZ => (1, 8, 8), + DispatchAxis.XYZ => (4, 4, 4), + _ => (0, 0, 0) + }; + + // Validate the dispatch axis argument + if ((threadsX, threadsY, threadsZ) is (0, 0, 0)) + { + builder.Add(InvalidEmbeddedBytecodeDispatchAxis, structDeclarationSymbol, structDeclarationSymbol); + + diagnostics = builder.ToImmutable(); + + return new(true, 0, 0, 0); + } + } + else if ( + attribute.ConstructorArguments.Length != 3 || + attribute.ConstructorArguments[0].Value is not int explicitThreadsX || + attribute.ConstructorArguments[1].Value is not int explicitThreadsY || + attribute.ConstructorArguments[2].Value is not int explicitThreadsZ || + explicitThreadsX is < 1 or > 1024 || + explicitThreadsY is < 1 or > 1024 || + explicitThreadsZ is < 1 or > 64) + { + // Failed to validate the thread number arguments builder.Add(InvalidEmbeddedBytecodeThreadIds, structDeclarationSymbol, structDeclarationSymbol); diagnostics = builder.ToImmutable(); return new(true, 0, 0, 0); } + else + { + threadsX = explicitThreadsX; + threadsY = explicitThreadsY; + threadsZ = explicitThreadsZ; + } // If the current shader is dynamic, emit a diagnostic error if (isDynamicShader)