Add and implement ID2D1Shader.BuildHlslSource method
このコミットが含まれているのは:
1 親
6f39eee106
コミット
f95079ea51
6ファイル変更
+151
-34
検索条件に一致するファイルはありません
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ComputeSharp.D2D1Interop.SourceGenerators.Mappings;
|
||||
@@ -29,6 +30,17 @@ internal static class ISymbolExtensions
|
||||
.WithMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType)
|
||||
.WithParameterOptions(SymbolDisplayParameterOptions.None);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether or not a given type symbol has a specified full name.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
|
||||
/// <param name="name">The full name to check.</param>
|
||||
/// <returns>Whether <paramref name="symbol"/> has a full name equals to <paramref name="name"/>.</returns>
|
||||
public static bool HasFullyQualifiedName(this ISymbol symbol, string name)
|
||||
{
|
||||
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full metadata name for a given <see cref="ITypeSymbol"/> instance.
|
||||
/// </summary>
|
||||
@@ -131,4 +143,30 @@ internal static class ISymbolExtensions
|
||||
|
||||
return ParseTypeName(typeName.ToHlslIdentifierName());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get an attribute with the specified full name.
|
||||
/// </summary>
|
||||
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
|
||||
/// <param name="name">The attribute name to look for.</param>
|
||||
/// <param name="attributeData">The resulting attribute data, if found.</param>
|
||||
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified name.</returns>
|
||||
public static bool TryGetAttributeWithFullyQualifiedName(this ISymbol symbol, string name, out AttributeData? attributeData)
|
||||
{
|
||||
ImmutableArray<AttributeData> attributes = symbol.GetAttributes();
|
||||
|
||||
foreach (AttributeData attribute in attributes)
|
||||
{
|
||||
if (attribute.AttributeClass?.HasFullyQualifiedName(name) == true)
|
||||
{
|
||||
attributeData = attribute;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
attributeData = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
using ComputeSharp.D2D1Interop.__Internals;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1Interop.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
partial class BuildHlslSource
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.
|
||||
/// </summary>
|
||||
/// <param name="hlslSource">The input HLSL source.</param>
|
||||
/// <returns>The resulting <see cref="MethodDeclarationSyntax"/> instance for the <c>BuildHlslSource</c> method.</returns>
|
||||
public static MethodDeclarationSyntax GetSyntax(string hlslSource)
|
||||
{
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly void global::ComputeSharp.D2D1Interop.__Internals.ID2D1Shader.BuildHlslSource(out string hlslSource)
|
||||
// {
|
||||
// hlslSource = <HLSL_SOURCE>;
|
||||
// }
|
||||
return
|
||||
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("BuildHlslSource"))
|
||||
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1Interop.__Internals.{nameof(ID2D1Shader)}")))
|
||||
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.AddParameterListParameters(
|
||||
Parameter(Identifier("hlslSource")).AddModifiers(Token(SyntaxKind.OutKeyword)).WithType(PredefinedType(Token(SyntaxKind.StringKeyword))))
|
||||
.WithBody(Block(ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
IdentifierName("hlslSource"),
|
||||
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(hlslSource))))));
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-16
@@ -22,9 +22,9 @@ namespace ComputeSharp.D2D1Interop.SourceGenerators;
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the <c>LoadBytecode</c> method.
|
||||
/// A helper with all logic to generate the <c>BuildHlslSource</c> method.
|
||||
/// </summary>
|
||||
internal static partial class LoadBytecode
|
||||
internal static partial class BuildHlslSource
|
||||
{
|
||||
/// <summary>
|
||||
/// Gathers all necessary information on a transpiled HLSL source for a given shader type.
|
||||
@@ -33,8 +33,8 @@ partial class ID2D1ShaderGenerator
|
||||
/// <param name="structDeclaration">The <see cref="StructDeclarationSyntax"/> node to process.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="structDeclaration"/>.</param>
|
||||
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
|
||||
/// <returns>The resulting info on the processed shader.</returns>
|
||||
public static (string HlslSource, D2D1ShaderProfile? ShaderProfile) GetHlslSource(
|
||||
/// <returns>The HLSL source for the shader.</returns>
|
||||
public static string GetHlslSource(
|
||||
Compilation compilation,
|
||||
StructDeclarationSyntax structDeclaration,
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
@@ -64,13 +64,12 @@ partial class ID2D1ShaderGenerator
|
||||
out int inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out bool requiresScenePosition,
|
||||
out D2D1ShaderProfile? shaderProfile);
|
||||
out bool requiresScenePosition);
|
||||
|
||||
diagnostics = builder.ToImmutable();
|
||||
|
||||
// Get the HLSL source
|
||||
string hlslSource = GetHlslSource(
|
||||
return GetHlslSource(
|
||||
definedConstants,
|
||||
declaredTypes,
|
||||
valueFields,
|
||||
@@ -81,8 +80,6 @@ partial class ID2D1ShaderGenerator
|
||||
inputSimpleIndices,
|
||||
inputComplexIndices,
|
||||
requiresScenePosition);
|
||||
|
||||
return (hlslSource, shaderProfile);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -373,18 +370,15 @@ partial class ID2D1ShaderGenerator
|
||||
/// <param name="inputSimpleIndices">The indicess of the simple shader inputs.</param>
|
||||
/// <param name="inputComplexIndices">The indicess of the complex shader inputs.</param>
|
||||
/// <param name="requiresScenePosition">Whether the shader requires the scene position.</param>
|
||||
/// <param name="shaderProfile">The shader profile to use to precompile the shader, if requested.</param>
|
||||
private static void GatherD2D1AttributeInfo(
|
||||
INamedTypeSymbol structDeclarationSymbol,
|
||||
out int inputCount,
|
||||
out ImmutableArray<int> inputSimpleIndices,
|
||||
out ImmutableArray<int> inputComplexIndices,
|
||||
out bool requiresScenePosition,
|
||||
out D2D1ShaderProfile? shaderProfile)
|
||||
out bool requiresScenePosition)
|
||||
{
|
||||
inputCount = 0;
|
||||
requiresScenePosition = false;
|
||||
shaderProfile = null;
|
||||
|
||||
ImmutableArray<int>.Builder inputSimpleIndicesBuilder = ImmutableArray.CreateBuilder<int>();
|
||||
ImmutableArray<int>.Builder inputComplexIndicesBuilder = ImmutableArray.CreateBuilder<int>();
|
||||
@@ -405,9 +399,6 @@ partial class ID2D1ShaderGenerator
|
||||
case "ComputeSharp.D2D1Interop.D2DRequiresScenePositionAttribute":
|
||||
requiresScenePosition = true;
|
||||
break;
|
||||
case "ComputeSharp.D2D1Interop.D2DEmbeddedBytecodeAttribute":
|
||||
shaderProfile = (D2D1ShaderProfile)attributeData.ConstructorArguments[0].Value!;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
+17
@@ -5,7 +5,9 @@ using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using ComputeSharp.D2D1Interop.Exceptions;
|
||||
using ComputeSharp.D2D1Interop.Shaders.Translation;
|
||||
using ComputeSharp.D2D1Interop.SourceGenerators.Extensions;
|
||||
using ComputeSharp.D2D1Interop.SourceGenerators.Models;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
@@ -19,6 +21,21 @@ partial class ID2D1ShaderGenerator
|
||||
/// <inheritodoc/>
|
||||
internal static partial class LoadBytecode
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the shader profile for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
|
||||
/// <returns>The shader profile to use to compile the shader, if present.</returns>
|
||||
public static D2D1ShaderProfile? GetShaderProfile(INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
if (structDeclarationSymbol.TryGetAttributeWithFullyQualifiedName("ComputeSharp.D2D1Interop.D2DEmbeddedBytecodeAttribute", out AttributeData? attributeData))
|
||||
{
|
||||
return (D2D1ShaderProfile)attributeData!.ConstructorArguments[0].Value!;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
|
||||
/// </summary>
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using ComputeSharp.D2D1Interop.SourceGenerators.Diagnostics;
|
||||
@@ -50,7 +51,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
|
||||
// Get the dispatch data, HLSL source and embedded bytecode info. This info is computed on the
|
||||
// same step as parts are shared in following sub-branches in the incremental generator pipeline.
|
||||
IncrementalValuesProvider<(Result<DispatchDataInfo> Dispatch, Result<HlslShaderSourceInfo> SourceInfo)> shaderInfoWithErrors =
|
||||
IncrementalValuesProvider<(Result<DispatchDataInfo> Dispatch, Result<string> SourceInfo, Result<D2D1ShaderProfile?> ProfileInfo)> shaderInfoWithErrors =
|
||||
shaderDeclarations
|
||||
.Combine(context.CompilationProvider)
|
||||
.Select(static (item, token) =>
|
||||
@@ -70,8 +71,8 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// Get HLSL source for LoadBytecode()
|
||||
(string hlslSource, D2D1ShaderProfile? shaderProfile) = LoadBytecode.GetHlslSource(
|
||||
// Get HLSL source for BuildHlslSource()
|
||||
string hlslSource = BuildHlslSource.GetHlslSource(
|
||||
item.Right,
|
||||
item.Left.Syntax,
|
||||
item.Left.Symbol,
|
||||
@@ -79,14 +80,13 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
HlslShaderSourceInfo sourceInfo = new(
|
||||
item.Left.Hierarchy,
|
||||
hlslSource,
|
||||
shaderProfile);
|
||||
// Get the shader profile
|
||||
D2D1ShaderProfile? shaderProfile = LoadBytecode.GetShaderProfile(item.Left.Symbol);
|
||||
|
||||
return (
|
||||
new Result<DispatchDataInfo>(dispatchDataInfo, dispatchDataDiagnostics),
|
||||
new Result<HlslShaderSourceInfo>(sourceInfo, hlslSourceDiagnostics));
|
||||
new Result<string>(hlslSource, hlslSourceDiagnostics),
|
||||
new Result<D2D1ShaderProfile?>(shaderProfile, ImmutableArray<Diagnostic>.Empty));
|
||||
});
|
||||
|
||||
// Output the diagnostics
|
||||
@@ -94,10 +94,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
context.ReportDiagnostics(shaderInfoWithErrors.Select(static (item, token) => item.SourceInfo.Errors));
|
||||
|
||||
// Filter all items to enable caching at a coarse level, and remove diagnostics
|
||||
IncrementalValuesProvider<(DispatchDataInfo Dispatch, HlslShaderSourceInfo SourceInfo)> shaderInfo =
|
||||
IncrementalValuesProvider<(DispatchDataInfo Dispatch, string HlslSource, D2D1ShaderProfile? ShaderProfile)> shaderInfo =
|
||||
shaderInfoWithErrors
|
||||
.Select(static (item, token) => (item.Dispatch.Value, item.SourceInfo.Value))
|
||||
.WithComparers(DispatchDataInfo.Comparer.Default, HlslShaderSourceInfo.Comparer.Default);
|
||||
.Select(static (item, token) => (item.Dispatch.Value, item.SourceInfo.Value, item.ProfileInfo.Value))
|
||||
.WithComparers(DispatchDataInfo.Comparer.Default, EqualityComparer<string>.Default, EqualityComparer<D2D1ShaderProfile?>.Default);
|
||||
|
||||
// Get a filtered sequence to enable caching
|
||||
IncrementalValuesProvider<DispatchDataInfo> dispatchDataInfo =
|
||||
@@ -114,15 +114,30 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
context.AddSource($"{item.Left.Hierarchy.FilenameHint}.{nameof(LoadDispatchData)}", compilationUnit.ToFullString());
|
||||
});
|
||||
|
||||
// Get a filtered sequence to enable caching
|
||||
IncrementalValuesProvider<HlslShaderSourceInfo> hlslSourceInfo =
|
||||
// Get the filtered sequence to enable caching
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource)> hlslSourceInfo =
|
||||
shaderInfo
|
||||
.Select(static (item, token) => item.SourceInfo)
|
||||
.WithComparer(HlslShaderSourceInfo.Comparer.Default);
|
||||
.Select(static (item, token) => (item.Dispatch.Hierarchy, item.HlslSource))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<string>.Default);
|
||||
|
||||
// Generate the BuildHlslSource() methods
|
||||
context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax buildHlslStringMethod = BuildHlslSource.GetSyntax(item.HlslSource);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, buildHlslStringMethod, canUseSkipLocalsInit: false);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(BuildHlslSource)}", compilationUnit.ToFullString());
|
||||
});
|
||||
|
||||
// Get a filtered sequence to enable caching
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, string HlslSource, D2D1ShaderProfile? ShaderProfile)> shaderBytecodeInfo =
|
||||
shaderInfo
|
||||
.Select(static (item, token) => (item.Dispatch.Hierarchy, item.HlslSource, item.ShaderProfile))
|
||||
.WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer<string>.Default, EqualityComparer<D2D1ShaderProfile?>.Default);
|
||||
|
||||
// Compile the requested shader bytecodes
|
||||
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
|
||||
hlslSourceInfo
|
||||
shaderBytecodeInfo
|
||||
.Select(static (item, token) =>
|
||||
{
|
||||
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(item.HlslSource, item.ShaderProfile, token, out DiagnosticInfo? diagnostic);
|
||||
|
||||
@@ -28,17 +28,31 @@ public interface ID2D1Shader
|
||||
;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Builds the HLSL source code for the current shader instance.
|
||||
/// </summary>
|
||||
/// <param name="hlslSource">The resulting HLSL source.</param>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This method is not intended to be called directly by user code")]
|
||||
void BuildHlslSource(out string hlslSource)
|
||||
#if NET6_0_OR_GREATER
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#else
|
||||
;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Loads the bytecode for the current shader.
|
||||
/// </summary>
|
||||
/// <typeparam name="TLoader">The type of bytecode loader being used.</typeparam>
|
||||
/// <param name="loader">The <typeparamref name="TLoader"/> instance to use to load the bytecode.</param>
|
||||
/// <param name="shaderProfile">The shader profile to use to get the shader bytecode (if <see langword="null"/>, the precompiled shader will be used).</param>
|
||||
/// <param name="hlslSource">The HLSL source that was compiled.</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if a precompiled bytecode was requested (<paramref name="shaderProfile"/> is <see langword="null"/>), but it wasn't availablle.</exception>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This method is not intended to be used directly by user code")]
|
||||
void LoadBytecode<TLoader>(ref TLoader loader, D2D1ShaderProfile? shaderProfile, out string hlslSource)
|
||||
void LoadBytecode<TLoader>(ref TLoader loader, D2D1ShaderProfile? shaderProfile)
|
||||
where TLoader : struct, ID2D1BytecodeLoader
|
||||
#if NET6_0_OR_GREATER
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする