Add new models for shader methods, update generators
このコミットが含まれているのは:
1 親
4e6f4f55d4
コミット
87768dc2b4
6ファイル変更
+132
-22
検索条件に一致するファイルはありません
+4
-2
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
@@ -21,7 +22,7 @@ partial class D2DPixelShaderSourceGenerator
|
||||
/// <param name="bytecodeInfo">The input bytecode info.</param>
|
||||
/// <param name="fixup">An opaque <see cref="Func{TResult}"/> instance to transform the final tree into text.</param>
|
||||
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the target method.</returns>
|
||||
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeInfo bytecodeInfo, out Func<SyntaxNode, SourceText> fixup)
|
||||
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeMethodInfo bytecodeInfo, out Func<SyntaxNode, SourceText> fixup)
|
||||
{
|
||||
// If there is no precompiled bytecode, just emit a span with a 0 value
|
||||
string bytecodeLiterals = bytecodeInfo.Bytecode.IsDefaultOrEmpty
|
||||
@@ -47,7 +48,8 @@ partial class D2DPixelShaderSourceGenerator
|
||||
MethodDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlySpan"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))),
|
||||
Identifier("TheMethodName")).AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
Identifier(bytecodeInfo.MethodName))
|
||||
.AddModifiers(bytecodeInfo.Modifiers.Select(Token).ToArray())
|
||||
.AddAttributeLists(
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
|
||||
|
||||
@@ -99,7 +99,7 @@ partial class D2DPixelShaderSourceGenerator
|
||||
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
|
||||
/// <param name="diagnostic">The resulting diagnostic from the processing operation, if any.</param>
|
||||
/// <returns>The <see cref="ImmutableArray{T}"/> instance with the compiled shader bytecode.</returns>
|
||||
public static unsafe ImmutableArray<byte> GetBytecode(HlslShaderSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic)
|
||||
public static unsafe ImmutableArray<byte> GetBytecode(HlslShaderMethodSourceInfo sourceInfo, CancellationToken token, out DiagnosticInfo? diagnostic)
|
||||
{
|
||||
ImmutableArray<byte> bytecode = ImmutableArray<byte>.Empty;
|
||||
|
||||
|
||||
@@ -22,33 +22,38 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
// Get all method declarations with the [D2DPixelShaderSource] attribute
|
||||
IncrementalValuesProvider<IMethodSymbol> methodSymbols =
|
||||
IncrementalValuesProvider<(MethodDeclarationSyntax Syntax, IMethodSymbol Symbol)> methodSymbols =
|
||||
context.SyntaxProvider
|
||||
.CreateSyntaxProvider(
|
||||
static (node, _) => node is MethodDeclarationSyntax { Parent: ClassDeclarationSyntax, AttributeLists.Count: > 0 },
|
||||
static (context, _) => (IMethodSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!)
|
||||
.Where(static symbol => symbol.HasAttributeWithFullyQualifiedName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute"));
|
||||
static (context, _) => (
|
||||
(MethodDeclarationSyntax)context.Node,
|
||||
Symbol: (IMethodSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!))
|
||||
.Where(static pair => pair.Symbol.HasAttributeWithFullyQualifiedName("ComputeSharp.D2D1.D2DPixelShaderSourceAttribute"));
|
||||
|
||||
// Gather info for all annotated methods
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo Source, ImmutableArray<Diagnostic> Diagnostics)> shaderInfoWithErrors =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo Source, ImmutableArray<Diagnostic> Diagnostics)> shaderInfoWithErrors =
|
||||
methodSymbols
|
||||
.Select(static (item, _) =>
|
||||
{
|
||||
ImmutableArray<Diagnostic>.Builder diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
|
||||
|
||||
// Get all necessary info for the current shader
|
||||
string? hlslSource = Execute.GetHlslSource(diagnostics, item) ?? "";
|
||||
D2D1ShaderProfile? shaderProfile = Execute.GetShaderProfile(diagnostics, item);
|
||||
D2D1CompileOptions? compileOptions = Execute.GetCompileOptions(diagnostics, item);
|
||||
ImmutableArray<SyntaxKind> modifiers = item.Syntax.Modifiers.Select(token => token.Kind()).ToImmutableArray();
|
||||
string methodName = item.Symbol.Name;
|
||||
string? hlslSource = Execute.GetHlslSource(diagnostics, item.Symbol) ?? "";
|
||||
D2D1ShaderProfile? shaderProfile = Execute.GetShaderProfile(diagnostics, item.Symbol);
|
||||
D2D1CompileOptions? compileOptions = Execute.GetCompileOptions(diagnostics, item.Symbol);
|
||||
|
||||
HlslShaderSourceInfo sourceInfo = new(
|
||||
HlslShaderMethodSourceInfo sourceInfo = new(
|
||||
modifiers,
|
||||
methodName,
|
||||
hlslSource,
|
||||
shaderProfile,
|
||||
compileOptions,
|
||||
IsLinkingSupported: false,
|
||||
HasErrors: diagnostics.Count > 0);
|
||||
|
||||
HierarchyInfo hierarchy = HierarchyInfo.From(item.ContainingType!);
|
||||
HierarchyInfo hierarchy = HierarchyInfo.From(item.Symbol.ContainingType!);
|
||||
|
||||
return (hierarchy, sourceInfo, diagnostics.ToImmutable());
|
||||
});
|
||||
@@ -57,13 +62,13 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
|
||||
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, _) => item.Diagnostics));
|
||||
|
||||
// Filter all items to enable caching at a coarse level, and remove diagnostics
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderSourceInfo Source)> shaderInfo =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, HlslShaderMethodSourceInfo Source)> shaderInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.Source))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<HlslShaderSourceInfo>.Default);
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, HlslShaderMethodSourceInfo.Comparer.Default);
|
||||
|
||||
// Compile the requested shader bytecodes
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
|
||||
shaderInfo
|
||||
.Select(static (item, token) =>
|
||||
{
|
||||
@@ -74,10 +79,10 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
EmbeddedBytecodeInfo bytecodeInfo = new(
|
||||
EmbeddedBytecodeMethodInfo bytecodeInfo = new(
|
||||
item.Source.Modifiers,
|
||||
item.Source.MethodName,
|
||||
item.Source.HlslSource,
|
||||
item.Source.ShaderProfile,
|
||||
item.Source.CompileOptions,
|
||||
bytecode);
|
||||
|
||||
return (item.Hierarchy, bytecodeInfo, diagnostic);
|
||||
@@ -103,10 +108,10 @@ public sealed partial class D2DPixelShaderSourceGenerator : IIncrementalGenerato
|
||||
context.ReportDiagnostics(embeddedBytecodeDiagnostics);
|
||||
|
||||
// Get the filtered sequence to enable caching
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode =
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeMethodInfo BytecodeInfo)> embeddedBytecode =
|
||||
embeddedBytecodeWithErrors
|
||||
.Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default);
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeMethodInfo.Comparer.Default);
|
||||
|
||||
// Generate the shader bytecode methods
|
||||
context.RegisterSourceOutput(embeddedBytecode, static (context, item) =>
|
||||
|
||||
@@ -7,7 +7,7 @@ using ComputeSharp.SourceGeneration.Helpers;
|
||||
namespace ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model representing a shared and its compiled bytecode, if available.
|
||||
/// A model representing info on a shader that has requested to be precompiled at build time.
|
||||
/// </summary>
|
||||
/// <param name="HlslSource">The HLSL source for the shader.</param>
|
||||
/// <param name="ShaderProfile">The shader profile to use to compile the shader, if requested.</param>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model representing info on a shader method that has requested to be precompiled at build time.
|
||||
/// </summary>
|
||||
///<param name="Modifiers">The modifiers for the annotated method.</param>
|
||||
/// <param name="MethodName">The name of the annotated method.</param>
|
||||
/// <param name="HlslSource">The HLSL source.</param>
|
||||
/// <param name="Bytecode">The compiled shader bytecode, if available.</param>
|
||||
internal sealed record EmbeddedBytecodeMethodInfo(
|
||||
ImmutableArray<SyntaxKind> Modifiers,
|
||||
string MethodName,
|
||||
string HlslSource,
|
||||
ImmutableArray<byte> Bytecode)
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="EmbeddedBytecodeMethodInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : Comparer<EmbeddedBytecodeMethodInfo, Comparer>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected override void AddToHashCode(ref HashCode hashCode, EmbeddedBytecodeMethodInfo obj)
|
||||
{
|
||||
hashCode.AddRange(obj.Modifiers);
|
||||
hashCode.Add(obj.MethodName);
|
||||
hashCode.Add(obj.HlslSource);
|
||||
hashCode.AddBytes(obj.Bytecode.AsSpan());
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool AreEqual(EmbeddedBytecodeMethodInfo x, EmbeddedBytecodeMethodInfo y)
|
||||
{
|
||||
return
|
||||
MemoryMarshal.Cast<SyntaxKind, ushort>(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast<SyntaxKind, ushort>(y.Modifiers.AsSpan())) &&
|
||||
x.MethodName == y.MethodName &&
|
||||
x.HlslSource == y.HlslSource &&
|
||||
x.Bytecode.SequenceEqual(y.Bytecode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.SourceGeneration.Extensions;
|
||||
using ComputeSharp.SourceGeneration.Helpers;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model for extracted info on a processed HLSL shader method.
|
||||
/// </summary>
|
||||
/// <param name="Modifiers">The modifiers for the annotated method.</param>
|
||||
/// <param name="MethodName">The name of the annotated method.</param>
|
||||
/// <param name="HlslSource">The HLSL source.</param>
|
||||
/// <param name="ShaderProfile">The shader profile to use to compile the shader, if requested.</param>
|
||||
/// <param name="CompileOptions">The compile options to use to compile the shader.</param>
|
||||
/// <param name="HasErrors">Whether any errors have been detected, and therefore the shader compilation should be skipped.</param>
|
||||
internal sealed record HlslShaderMethodSourceInfo(
|
||||
ImmutableArray<SyntaxKind> Modifiers,
|
||||
string MethodName,
|
||||
string HlslSource,
|
||||
D2D1ShaderProfile? ShaderProfile,
|
||||
D2D1CompileOptions? CompileOptions,
|
||||
bool HasErrors)
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="HlslShaderMethodSourceInfo"/>.
|
||||
/// </summary>
|
||||
public sealed class Comparer : Comparer<HlslShaderMethodSourceInfo, Comparer>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
protected override void AddToHashCode(ref HashCode hashCode, HlslShaderMethodSourceInfo obj)
|
||||
{
|
||||
hashCode.AddRange(obj.Modifiers);
|
||||
hashCode.Add(obj.MethodName);
|
||||
hashCode.Add(obj.HlslSource);
|
||||
hashCode.Add(obj.ShaderProfile);
|
||||
hashCode.Add(obj.CompileOptions);
|
||||
hashCode.Add(obj.HasErrors);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override bool AreEqual(HlslShaderMethodSourceInfo x, HlslShaderMethodSourceInfo y)
|
||||
{
|
||||
return
|
||||
MemoryMarshal.Cast<SyntaxKind, ushort>(x.Modifiers.AsSpan()).SequenceEqual(MemoryMarshal.Cast<SyntaxKind, ushort>(y.Modifiers.AsSpan())) &&
|
||||
x.MethodName == y.MethodName &&
|
||||
x.HlslSource == y.HlslSource &&
|
||||
x.ShaderProfile == y.ShaderProfile &&
|
||||
x.CompileOptions == y.CompileOptions &&
|
||||
x.HasErrors == y.HasErrors;
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする