diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs
index 74771087..645108ca 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateTryGetBytecodeMethod.cs
@@ -94,20 +94,21 @@ public sealed partial class IShaderGenerator
///
/// The input instance to process.
/// The generated HLSL source code (ignoring captured delegates, if present).
- /// The resulting diagnostics from the processing operation.
+ /// The resulting diagnostic from the processing operation, if any.
/// The instance with the compiled shader bytecode.
[Pure]
public static unsafe ImmutableArray GetBytecode(
ThreadIdsInfo threadIds,
string hlslSource,
- out ImmutableArray diagnostics)
+ out DiagnosticInfo? diagnostic)
{
ImmutableArray bytecode = ImmutableArray.Empty;
- ImmutableArray.Builder builder = ImmutableArray.CreateBuilder();
// No embedded shader was requested
if (threadIds.IsDefault)
{
+ diagnostic = null;
+
goto End;
}
@@ -131,30 +132,18 @@ public sealed partial class IShaderGenerator
byte[] array = new ReadOnlySpan(buffer, length).ToArray();
bytecode = Unsafe.As>(ref array);
+ diagnostic = null;
}
- catch (Win32Exception)
+ catch (Win32Exception e)
{
- // TODO
- //builder.Add(
- // EmbeddedBytecodeFailedWithWin32Exception,
- // structDeclarationSymbol,
- // structDeclarationSymbol,
- // e.HResult,
- // e.Message);
+ diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message);
}
- catch (HlslCompilationException)
+ catch (HlslCompilationException e)
{
- // TODO
- //builder.Add(
- // EmbeddedBytecodeFailedWithHlslCompilationException,
- // structDeclarationSymbol,
- // structDeclarationSymbol,
- // e.Message);
+ diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithHlslCompilationException, e.Message);
}
End:
- diagnostics = builder.ToImmutable();
-
return bytecode;
}
diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
index ebcf35b2..04d0fdd7 100644
--- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
+++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.cs
@@ -193,11 +193,11 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default, ThreadIdsInfo.Comparer.Default);
// Compile the requested shader bytecodes
- IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result BytecodeInfo)> embeddedBytecodeWithErrors =
+ IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
shaderBytecodeInfo
.Select(static (item, token) =>
{
- ImmutableArray bytecode = TryGetBytecode.GetBytecode(item.ThreadIds, item.Hlsl, out ImmutableArray diagnostics);
+ ImmutableArray bytecode = TryGetBytecode.GetBytecode(item.ThreadIds, item.Hlsl, out DiagnosticInfo? diagnostic);
EmbeddedBytecodeInfo bytecodeInfo = new(
item.ThreadIds.X,
@@ -205,16 +205,33 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
item.ThreadIds.Z,
bytecode);
- return (item.Hierarchy, new Result(bytecodeInfo, diagnostics));
+ return (item.Hierarchy, bytecodeInfo, diagnostic);
+ });
+
+ // Gather the diagnostics
+ IncrementalValuesProvider> embeddedBytecodeDiagnostics =
+ embeddedBytecodeWithErrors
+ .Select(static (item, token) => (item.Hierarchy.MetadataName, item.Diagnostic))
+ .Where(static item => item.Diagnostic is not null)
+ .Combine(context.CompilationProvider)
+ .Select(static (item, token) =>
+ {
+ INamedTypeSymbol? typeSymbol = item.Right.GetTypeByMetadataName(item.Left.MetadataName)!;
+ Diagnostic diagnostic = Diagnostic.Create(
+ item.Left.Diagnostic!.Descriptor,
+ typeSymbol.Locations.FirstOrDefault(),
+ new object[] { typeSymbol }.Concat(item.Left.Diagnostic.Args).ToArray());
+
+ return ImmutableArray.Create(diagnostic);
});
// Output the diagnostics
- context.ReportDiagnostics(embeddedBytecodeWithErrors.Select(static (item, token) => item.BytecodeInfo.Errors));
+ context.ReportDiagnostics(embeddedBytecodeDiagnostics);
// Get the filtered sequence to enable caching
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode =
embeddedBytecodeWithErrors
- .Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo.Value))
+ .Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo))
.WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default);
// Generate the TryGetBytecode() methods
diff --git a/src/ComputeSharp.SourceGenerators/Models/DiagnosticInfo.cs b/src/ComputeSharp.SourceGenerators/Models/DiagnosticInfo.cs
new file mode 100644
index 00000000..e3711b93
--- /dev/null
+++ b/src/ComputeSharp.SourceGenerators/Models/DiagnosticInfo.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using ComputeSharp.SourceGenerators.Extensions;
+using Microsoft.CodeAnalysis;
+
+namespace ComputeSharp.SourceGenerators.Models;
+
+///
+/// A model for a serializeable diagnostic provider.
+///
+/// The wrapped instance.
+/// The diagnostic args.
+internal sealed record DiagnosticInfo(DiagnosticDescriptor Descriptor, params object[] Args)
+{
+ ///
+ /// An implementation for .
+ ///
+ public sealed class Comparer : IEqualityComparer
+ {
+ ///
+ /// The singleton instance.
+ ///
+ public static Comparer Default { get; } = new();
+
+ ///
+ public bool Equals(DiagnosticInfo? x, DiagnosticInfo? y)
+ {
+ if (x is null && y is null)
+ {
+ return true;
+ }
+
+ if (x is null || y is null)
+ {
+ return false;
+ }
+
+ if (ReferenceEquals(x, y))
+ {
+ return true;
+ }
+
+ return
+ x.Descriptor.Equals(y.Descriptor) &&
+ x.Args.SequenceEqual(y.Args);
+ }
+
+ ///
+ public int GetHashCode(DiagnosticInfo obj)
+ {
+ HashCode hashCode = default;
+
+ hashCode.Add(obj.Descriptor);
+
+ foreach (object arg in obj.Args)
+ {
+ hashCode.Add(arg);
+ }
+
+ return hashCode.ToHashCode();
+ }
+ }
+}
diff --git a/src/ComputeSharp.SourceGenerators/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGenerators/Models/HierarchyInfo.cs
index 0842eb94..10039dac 100644
--- a/src/ComputeSharp.SourceGenerators/Models/HierarchyInfo.cs
+++ b/src/ComputeSharp.SourceGenerators/Models/HierarchyInfo.cs
@@ -12,9 +12,10 @@ namespace ComputeSharp.SourceGenerators.Models;
/// A model describing the hierarchy info for a specific type.
///
/// The filename hint for the current type.
+/// The metadata name for the current type.
/// Gets the namespace for the current type.
/// Gets the sequence of type definitions containing the current type.
-internal sealed record HierarchyInfo(string FilenameHint, string Namespace, ImmutableArray Names)
+internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, string Namespace, ImmutableArray Names)
{
///
/// Creates a new instance from a given .
@@ -35,6 +36,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string Namespace, Immu
return new(
typeSymbol.GetGeneratedFileName(),
+ typeSymbol.MetadataName,
typeSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)),
names.ToImmutable());
}
@@ -69,6 +71,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string Namespace, Immu
return
x.FilenameHint == y.FilenameHint &&
+ x.MetadataName == y.MetadataName &&
x.Namespace == y.Namespace &&
x.Names.SequenceEqual(y.Names);
}
@@ -76,7 +79,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string Namespace, Immu
///
public int GetHashCode(HierarchyInfo obj)
{
- return HashCode.Combine(obj.Namespace, obj.Names, obj.Names[0]);
+ return HashCode.Combine(obj.Namespace, obj.MetadataName, obj.Names, obj.Names[0]);
}
}
}