Enable diagnostics generation for LoadDispatchData()

このコミットが含まれているのは:
Sergio Pedri committed 2021-12-08 11:17:04 +01:00
1 親 b912b4f076
コミット 8c0637e363
5ファイル変更
+101 -29

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

+16
ファイルの表示
@@ -72,4 +72,20 @@ internal static class DiagnosticsExtensions
{
diagnostics.Add(Diagnostic.Create(descriptor, node.GetLocation(), args));
}
/// <summary>
/// Registers an output node into an <see cref="IncrementalGeneratorInitializationContext"/> to output diagnostics.
/// </summary>
/// <param name="context">The input <see cref="IncrementalGeneratorInitializationContext"/> instance.</param>
/// <param name="diagnostics">The input <see cref="IncrementalValuesProvider{TValues}"/> sequence of diagnostics.</param>
public static void ReportDiagnostics(this IncrementalGeneratorInitializationContext context, IncrementalValuesProvider<ImmutableArray<Diagnostic>> diagnostics)
{
context.RegisterSourceOutput(diagnostics, static (context, diagnostics) =>
{
foreach (Diagnostic diagnostic in diagnostics)
{
context.ReportDiagnostic(diagnostic);
}
});
}
}
@@ -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
/// <summary>
/// Explores a given type hierarchy and generates statements to load fields.
/// </summary>
/// <param name="currentTypeSymbol">The current type being explored.</param>
/// <param name="structDeclarationSymbol">The current shader type being explored.</param>
/// <param name="shaderInterfaceType">The type of shader interface urrently being processed.</param>
/// <param name="resourceCount">The total number of captured resources in the shader.</param>
/// <param name="root32BitConstantCount">The total number of needed 32 bit constants in the shader root signature.</param>
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
/// <returns>The sequence of <see cref="FieldInfo"/> instances for all captured resources and values.</returns>
private static ImmutableArray<FieldInfo> GetCapturedFieldInfos(
ITypeSymbol currentTypeSymbol,
ITypeSymbol structDeclarationSymbol,
Type shaderInterfaceType,
out int resourceCount,
out int root32BitConstantCount)
out int root32BitConstantCount,
out ImmutableArray<Diagnostic> diagnostics)
{
// Helper method that uses boxes instead of ref-s (illegal in enumerators)
static IEnumerable<FieldInfo> GetCapturedFieldInfos(
@@ -88,6 +91,8 @@ public sealed partial class IShaderGenerator2
}
}
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
// 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<FieldInfo> fieldInfos = GetCapturedFieldInfos(
currentTypeSymbol,
structDeclarationSymbol,
ImmutableArray<string>.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<uint> span0 = stackalloc uint[<VARIABLES>];
statements.Insert(0,
LocalDeclarationStatement(
+28 -8
ファイルの表示
@@ -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<FieldInfo> FieldInfos, int ResourceCount, int Root32BitConstantCount)> hierarchyInfoAndCaptureFieldInfos =
IncrementalValuesProvider<Result<DispatchDataInfo>> 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<FieldInfo> fieldInfos = GetCapturedFieldInfos(
item.Symbol,
item.Type,
out int resourceCount,
out int root32BitConstantCount,
out ImmutableArray<Diagnostic> diagnostics);
return new Result<DispatchDataInfo>(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> 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,
+19
ファイルの表示
@@ -0,0 +1,19 @@
using System;
using System.Collections.Immutable;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing gathered info on a shader dispatch data.
/// </summary>
/// <param name="Hierarchy">The hierarchy data for the shader.</param>
/// <param name="Type">The shader interface type.</param>
/// <param name="FieldInfos">The description on shader instance fields.</param>
/// <param name="ResourceCount">The total number of captured resources.</param>
/// <param name="Root32BitConstantCount">The size of the shader root signature, in 32 bit constants.</param>
internal sealed record DispatchDataInfo(
HierarchyInfo Hierarchy,
Type Type,
ImmutableArray<FieldInfo> FieldInfos,
int ResourceCount,
int Root32BitConstantCount);
+12
ファイルの表示
@@ -0,0 +1,12 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing a value and an associated set of diagnostic errors.
/// </summary>
/// <typeparam name="TValue">The type of the wrapped value.</typeparam>
/// <param name="Value">The wrapped value for the current result.</param>
/// <param name="Errors">The associated diagnostic errors, if any.</param>
internal sealed record Result<TValue>(TValue Value, ImmutableArray<Diagnostic> Errors);