From 8c0637e363f33b843f6e2bd95bf6877238857ca6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 6 Dec 2021 11:15:45 +0100 Subject: [PATCH] Enable diagnostics generation for LoadDispatchData() --- .../Diagnostics/DiagnosticsExtensions.cs | 16 +++++++ ...ncremental.CreateLoadDispatchDataMethod.cs | 47 ++++++++++--------- .../IShaderGenerator.Incremental.cs | 36 ++++++++++---- .../Models/DispatchDataInfo.cs | 19 ++++++++ .../Models/Result{T}.cs | 12 +++++ 5 files changed, 101 insertions(+), 29 deletions(-) create mode 100644 src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs create mode 100644 src/ComputeSharp.SourceGenerators/Models/Result{T}.cs diff --git a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticsExtensions.cs b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticsExtensions.cs index 5ff64485..8584bee1 100644 --- a/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticsExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Diagnostics/DiagnosticsExtensions.cs @@ -72,4 +72,20 @@ internal static class DiagnosticsExtensions { diagnostics.Add(Diagnostic.Create(descriptor, node.GetLocation(), args)); } + + /// + /// Registers an output node into an to output diagnostics. + /// + /// The input instance. + /// The input sequence of diagnostics. + public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider> diagnostics) + { + context.RegisterSourceOutput(diagnostics, static (context, diagnostics) => + { + foreach (Diagnostic diagnostic in diagnostics) + { + context.ReportDiagnostic(diagnostic); + } + }); + } } diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.CreateLoadDispatchDataMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.CreateLoadDispatchDataMethod.cs index e1c56267..ca058e47 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.CreateLoadDispatchDataMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.CreateLoadDispatchDataMethod.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Runtime.CompilerServices; using ComputeSharp.__Internals; using ComputeSharp.Core.Helpers; +using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Extensions; using ComputeSharp.SourceGenerators.Mappings; using ComputeSharp.SourceGenerators.Models; @@ -24,16 +25,18 @@ public sealed partial class IShaderGenerator2 /// /// Explores a given type hierarchy and generates statements to load fields. /// - /// The current type being explored. + /// The current shader type being explored. /// The type of shader interface urrently being processed. /// The total number of captured resources in the shader. /// The total number of needed 32 bit constants in the shader root signature. + /// The resulting diagnostics from the processing operation. /// The sequence of instances for all captured resources and values. private static ImmutableArray GetCapturedFieldInfos( - ITypeSymbol currentTypeSymbol, + ITypeSymbol structDeclarationSymbol, Type shaderInterfaceType, out int resourceCount, - out int root32BitConstantCount) + out int root32BitConstantCount, + out ImmutableArray diagnostics) { // Helper method that uses boxes instead of ref-s (illegal in enumerators) static IEnumerable GetCapturedFieldInfos( @@ -88,6 +91,8 @@ public sealed partial class IShaderGenerator2 } } + ImmutableArray.Builder builder = ImmutableArray.CreateBuilder(); + // Setup the resource and byte offsets for tracking. Pixel shaders have only two // implicitly captured int values, as they're always dispatched over a 2D texture. bool isComputeShader = shaderInterfaceType == typeof(IComputeShader); @@ -97,7 +102,7 @@ public sealed partial class IShaderGenerator2 // Traverse all shader fields and gather info, and update the tracking offsets ImmutableArray fieldInfos = GetCapturedFieldInfos( - currentTypeSymbol, + structDeclarationSymbol, ImmutableArray.Empty, resourceOffsetAsBox, rawDataOffsetAsBox).ToImmutableArray(); @@ -110,6 +115,23 @@ public sealed partial class IShaderGenerator2 // of size 32 bits (via SetComputeRoot32BitConstants), without reading out of bounds. root32BitConstantCount = AlignmentHelper.Pad(rawDataOffsetAsBox.Value, sizeof(int)) / 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 = root32BitConstantCount + resourceCount; + + if (rootSignatureDwordSize > 64) + { + builder.Add(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); + } + + diagnostics = builder.ToImmutable(); + return fieldInfos; } @@ -319,23 +341,6 @@ public sealed partial class IShaderGenerator2 } } - // 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 = root32BitConstantsCount + resourceCount; - - if (rootSignatureDwordSize > 64) - { - // TODO - //context.ReportDiagnostic(ShaderDispatchDataSizeExceeded, structDeclarationSymbol, structDeclarationSymbol); - context.ReportDiagnostic(Diagnostic.Create(ShaderDispatchDataSizeExceeded, null)); - } - // global::System.Span span0 = stackalloc uint[]; statements.Insert(0, LocalDeclarationStatement( diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs index 74bcf298..303a9307 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Incremental.cs @@ -4,6 +4,7 @@ using System.Collections.Immutable; using System.Diagnostics.Contracts; using System.Linq; using System.Text; +using ComputeSharp.SourceGenerators.Diagnostics; using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -71,17 +72,36 @@ public sealed partial class IShaderGenerator2 : IIncrementalGenerator }); // Get the hierarchy info and captured field infos for each shader type - IncrementalValuesProvider<(HierarchyInfo Hierarchy, Type Type, ImmutableArray FieldInfos, int ResourceCount, int Root32BitConstantCount)> hierarchyInfoAndCaptureFieldInfos = + IncrementalValuesProvider> dispatchDataInfoWithErrors = shaderDeclarations - .Select(static (item, token) => ( - item.Hierarchy, - item.Type, - FieldInfos: GetCapturedFieldInfos(item.Symbol, item.Type, out int resourceCount, out int root32BitConstantCount), - ResourceCount: resourceCount, - Root32BitConstantCount: root32BitConstantCount)); + .Select(static (item, token) => + { + ImmutableArray fieldInfos = GetCapturedFieldInfos( + item.Symbol, + item.Type, + out int resourceCount, + out int root32BitConstantCount, + out ImmutableArray diagnostics); + + return new Result(new DispatchDataInfo( + item.Hierarchy, + item.Type, + fieldInfos, + resourceCount, + root32BitConstantCount), + diagnostics); + }); + + // Output the diagnostics from the previous step + context.ReportDiagnostics(dispatchDataInfoWithErrors.Select(static (item, token) => item.Errors)); + + // Get a filtered sequence to enable caching + IncrementalValuesProvider dispatchDataInfo = + dispatchDataInfoWithErrors + .Select(static (item, token) => item.Value); // Generate the LoadDispatchData() methods - context.RegisterSourceOutput(hierarchyInfoAndCaptureFieldInfos.Combine(canUseSkipLocalsInit), static (context, item) => + context.RegisterSourceOutput(dispatchDataInfo.Combine(canUseSkipLocalsInit), static (context, item) => { MethodDeclarationSyntax loadDispatchDataMethod = CreateLoadDispatchDataMethod( context, diff --git a/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs b/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs new file mode 100644 index 00000000..39fc905d --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Models/DispatchDataInfo.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Immutable; + +namespace ComputeSharp.SourceGenerators.Models; + +/// +/// A model representing gathered info on a shader dispatch data. +/// +/// The hierarchy data for the shader. +/// The shader interface type. +/// The description on shader instance fields. +/// The total number of captured resources. +/// The size of the shader root signature, in 32 bit constants. +internal sealed record DispatchDataInfo( + HierarchyInfo Hierarchy, + Type Type, + ImmutableArray FieldInfos, + int ResourceCount, + int Root32BitConstantCount); diff --git a/src/ComputeSharp.SourceGenerators/Models/Result{T}.cs b/src/ComputeSharp.SourceGenerators/Models/Result{T}.cs new file mode 100644 index 00000000..4bee6af9 --- /dev/null +++ b/src/ComputeSharp.SourceGenerators/Models/Result{T}.cs @@ -0,0 +1,12 @@ +using System.Collections.Immutable; +using Microsoft.CodeAnalysis; + +namespace ComputeSharp.SourceGenerators.Models; + +/// +/// A model representing a value and an associated set of diagnostic errors. +/// +/// The type of the wrapped value. +/// The wrapped value for the current result. +/// The associated diagnostic errors, if any. +internal sealed record Result(TValue Value, ImmutableArray Errors);