Generate HLSL bytecode as ReadOnlyMemory<T>
このコミットが含まれているのは:
1 親
423c87cab8
コミット
fe49828288
3ファイル変更
+208
-181
検索条件に一致するファイルはありません
+198
-166
@@ -71,200 +71,232 @@ partial class ID2D1ShaderGenerator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>TryGetBytecode</c> method.
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>HlslBytecode</c> property.
|
||||
/// </summary>
|
||||
/// <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 <c>BuildHlslSource</c> method.</returns>
|
||||
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeInfo bytecodeInfo, out Func<SyntaxNode, SourceText> fixup)
|
||||
/// <param name="additionalTypes">Any additional <see cref="TypeDeclarationSyntax"/> instances needed by the generated code, if needed.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>HlslBytecode</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetHlslBytecodeSyntax(
|
||||
EmbeddedBytecodeInfo bytecodeInfo,
|
||||
out Func<SyntaxNode, SourceText> fixup,
|
||||
out TypeDeclarationSyntax[] additionalTypes)
|
||||
{
|
||||
BlockSyntax block = GetShaderBytecodeBody(bytecodeInfo, out string? bytecodeLiterals);
|
||||
ExpressionSyntax memoryExpression;
|
||||
|
||||
if (bytecodeLiterals is not null)
|
||||
// If there is no bytecode, simply return a default expression.
|
||||
// Otherwise, declare the memory manager and access it.
|
||||
if (bytecodeInfo.Bytecode.Length == 0)
|
||||
{
|
||||
fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals), Encoding.UTF8);
|
||||
memoryExpression = LiteralExpression(SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword));
|
||||
fixup = static tree => tree.GetText(Encoding.UTF8);
|
||||
additionalTypes = Array.Empty<TypeDeclarationSyntax>();
|
||||
}
|
||||
else
|
||||
{
|
||||
fixup = static tree => tree.GetText(Encoding.UTF8);
|
||||
memoryExpression = MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("HlslBytecodeMemoryManager"),
|
||||
IdentifierName("Memory"));
|
||||
|
||||
TypeDeclarationSyntax memoryManagerDeclaration = GetMemoryManagerDeclaration(bytecodeInfo);
|
||||
string bytecodeLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(bytecodeInfo.Bytecode.AsSpan());
|
||||
|
||||
additionalTypes = new TypeDeclarationSyntax[] { memoryManagerDeclaration };
|
||||
fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals), Encoding.UTF8);
|
||||
}
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly void global::ComputeSharp.D2D1.__Internals.ID2D1Shader.LoadBytecode<TLoader>(ref TLoader loader, ref global::ComputeSharp.D2D1.D2D1ShaderProfile? shaderProfile, ref global::ComputeSharp.D2D1.D2D1CompileOptions? compileOptions)
|
||||
// {
|
||||
// <BODY>
|
||||
// }
|
||||
// readonly global::System.ReadOnlyMemory<byte> global::ComputeSharp.D2D1.__Internals.ID2D1Shader.HlslBytecode => <MEMORY_EXPRESSION>;
|
||||
return
|
||||
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(nameof(LoadBytecode)))
|
||||
PropertyDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlyMemory"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))),
|
||||
Identifier(nameof(ID2D1Shader.HlslBytecode)))
|
||||
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
|
||||
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.AddTypeParameterListParameters(TypeParameter(Identifier("TLoader")))
|
||||
.AddParameterListParameters(
|
||||
Parameter(Identifier("loader")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(IdentifierName("TLoader")),
|
||||
Parameter(Identifier("shaderProfile")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"))),
|
||||
Parameter(Identifier("compileOptions")).AddModifiers(Token(SyntaxKind.RefKeyword)).WithType(NullableType(IdentifierName("global::ComputeSharp.D2D1.D2D1CompileOptions"))))
|
||||
.WithBody(block);
|
||||
.WithExpressionBody(ArrowExpressionClause(memoryExpression))
|
||||
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
|
||||
/// </summary>
|
||||
/// <param name="bytecodeInfo">The input bytecode info.</param>
|
||||
/// <param name="bytecodeLiterals">The resulting bytecode literals to insert into the final source code.</param>
|
||||
/// <returns>The <see cref="BlockSyntax"/> instance trying to retrieve the precompiled shader.</returns>
|
||||
private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, out string? bytecodeLiterals)
|
||||
private static unsafe TypeDeclarationSyntax GetMemoryManagerDeclaration(EmbeddedBytecodeInfo bytecodeInfo)
|
||||
{
|
||||
// This method needs to handle several different scenarions, which are as follows:
|
||||
// 1) If [EmbeddedBytecode] is used, then the shader is precompiled and stored as a ReadOnlySpan<byte>. In this case,
|
||||
// this bytecode would have been generated with some specific D2D1ShaderProfile and D2D1CompileOptions values. So,
|
||||
// a branch is generated to try to retrieve this precompiled bytecode, which can be returned if and only if the
|
||||
// requested shader profile and compile options are either null, or either matches the values that have been used
|
||||
// to generate that bytecode. If this is the case, then no further work is done and that binary data is returned.
|
||||
// 2) If either the requested shader profile or options do not match the values used to create the shader bytecode,
|
||||
// then a call to the internal D2D1ShaderCompiler.LoadDynamicBytecode API is used, to create the shader on the fly.
|
||||
// 3) If [EmbeddedBytecode] is not used, then regardless of the requested profile and options, a new shader is compiled.
|
||||
// 4) If this is the case and either the shader profile or the options are null, then this method will manually set the
|
||||
// value being null to the default one to use. That is, D2D1ShaderProfile.PixelShader50 will be used as the target,
|
||||
// and the options will be set to D2D1CompileOptions.Default (and optionally, D2D1CompileOptions.EnableLinking too).
|
||||
|
||||
// Get a formatted representation of the compile options being used
|
||||
ExpressionSyntax compileOptionsExpression =
|
||||
ParseExpression(
|
||||
bytecodeInfo.CompileOptions
|
||||
.ToString(CultureInfo.InvariantCulture)
|
||||
.Split(',')
|
||||
.Select(static name => $"global::ComputeSharp.D2D1.D2D1CompileOptions.{name.Trim()}")
|
||||
.Aggregate("", static (left, right) => left.Length > 0 ? $"{left} | {right}" : right));
|
||||
|
||||
// Add parantheses if needed
|
||||
if (compileOptionsExpression is BinaryExpressionSyntax)
|
||||
{
|
||||
compileOptionsExpression = ParenthesizedExpression(compileOptionsExpression);
|
||||
}
|
||||
|
||||
// This code produces a block declaration as follows:
|
||||
// Create the MemoryManager<T> declaration:
|
||||
//
|
||||
// global::ComputeSharp.D2D1.D2D1ShaderProfile effectiveShaderProfile = shaderProfile ??= <DEFAULT_PROFILE>;
|
||||
// global::ComputeSharp.D2D1.D2D1CompileOptions effectiveCompileOptions = compileOptions ??= <REQUESTED_OPTIONS>;
|
||||
//
|
||||
// global::ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, in this, effectiveShaderProfile, effectiveCompileOptions);
|
||||
BlockSyntax dynamicLoadingBlock =
|
||||
Block(
|
||||
LocalDeclarationStatement(
|
||||
VariableDeclaration(IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier("effectiveShaderProfile"))
|
||||
.WithInitializer(EqualsValueClause(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.CoalesceAssignmentExpression,
|
||||
IdentifierName("shaderProfile"),
|
||||
IdentifierName($"global::ComputeSharp.D2D1.D2D1ShaderProfile.{D2D1ShaderProfile.PixelShader50}")))))),
|
||||
LocalDeclarationStatement(
|
||||
VariableDeclaration(IdentifierName("global::ComputeSharp.D2D1.D2D1CompileOptions"))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier("effectiveCompileOptions"))
|
||||
.WithInitializer(EqualsValueClause(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.CoalesceAssignmentExpression,
|
||||
IdentifierName("compileOptions"),
|
||||
compileOptionsExpression))))),
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::ComputeSharp.D2D1.__Internals.D2D1ShaderCompiler"),
|
||||
IdentifierName("LoadDynamicBytecode")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(IdentifierName("loader")).WithRefKindKeyword(Token(SyntaxKind.RefKeyword)),
|
||||
Argument(ThisExpression()).WithRefKindKeyword(Token(SyntaxKind.InKeyword)),
|
||||
Argument(IdentifierName("effectiveShaderProfile")),
|
||||
Argument(IdentifierName("effectiveCompileOptions")))));
|
||||
|
||||
// If there is no precompiled bytecode, just return the dynamic path
|
||||
if (bytecodeInfo.Bytecode.IsDefaultOrEmpty)
|
||||
{
|
||||
bytecodeLiterals = null;
|
||||
|
||||
return dynamicLoadingBlock;
|
||||
}
|
||||
|
||||
bytecodeLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(bytecodeInfo.Bytecode.AsSpan());
|
||||
|
||||
// Prepare the expression for the default shader profile to use
|
||||
ExpressionSyntax shaderProfileExpression =
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::ComputeSharp.D2D1.D2D1ShaderProfile"),
|
||||
IdentifierName(bytecodeInfo.ShaderProfile.ToString(CultureInfo.InvariantCulture)));
|
||||
|
||||
// This code produces a branch as follows:
|
||||
//
|
||||
// if (shaderProfile is null or <SHADER_PROFILE> && compileOptions is null or <COMPILE_OPTIONS>)
|
||||
// /// <summary>
|
||||
// /// <see cref="global::System.Buffers.MemoryManager{T}"/> implementation to get the HLSL bytecode.
|
||||
// /// </summary>
|
||||
// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
|
||||
// [global::System.Diagnostics.DebuggerNonUserCode]
|
||||
// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||
// file sealed HlslBytecodeMemoryManager : global::System.Buffers.MemoryManager<byte>
|
||||
// {
|
||||
// global::System.ReadOnlySpan<byte> bytecode = new byte[] { __EMBEDDED_SHADER_BYTECODE };
|
||||
//
|
||||
// loader.LoadEmbeddedBytecode(bytecode);
|
||||
//
|
||||
// shaderProfile = <SHADER_PROFILE>;
|
||||
// compileOptions = <COMPILE_OPTIONS>;
|
||||
// }
|
||||
//
|
||||
// Note that the __EMBEDDED_SHADER_BYTECODE identifier will be replaced after the string
|
||||
// is created by the generator. This greatly speeds up the code generation, as it avoids
|
||||
// having to create/parse tens of thousands of literal expression nodes for every shader.
|
||||
IfStatementSyntax embeddedBranch =
|
||||
IfStatement(
|
||||
BinaryExpression(
|
||||
SyntaxKind.LogicalAndExpression,
|
||||
IsPatternExpression(
|
||||
IdentifierName("shaderProfile"),
|
||||
BinaryPattern(
|
||||
SyntaxKind.OrPattern,
|
||||
ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
|
||||
ConstantPattern(shaderProfileExpression))),
|
||||
IsPatternExpression(
|
||||
IdentifierName("compileOptions"),
|
||||
BinaryPattern(
|
||||
SyntaxKind.OrPattern,
|
||||
ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)),
|
||||
ConstantPattern(compileOptionsExpression)))),
|
||||
Block(
|
||||
LocalDeclarationStatement(
|
||||
VariableDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlySpan"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier("bytecode"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))))
|
||||
.WithInitializer(
|
||||
InitializerExpression(
|
||||
SyntaxKind.ArrayInitializerExpression,
|
||||
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE")))))))),
|
||||
ExpressionStatement(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("loader"),
|
||||
IdentifierName("LoadEmbeddedBytecode")))
|
||||
.AddArgumentListArguments(Argument(IdentifierName("bytecode")))),
|
||||
ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
IdentifierName("shaderProfile"),
|
||||
shaderProfileExpression)),
|
||||
ExpressionStatement(
|
||||
AssignmentExpression(
|
||||
SyntaxKind.SimpleAssignmentExpression,
|
||||
IdentifierName("compileOptions"),
|
||||
compileOptionsExpression))));
|
||||
TypeDeclarationSyntax typeDeclaration =
|
||||
ClassDeclaration("HlslBytecodeMemoryManager")
|
||||
.AddModifiers(Token(SyntaxKind.FileKeyword), Token(SyntaxKind.SealedKeyword))
|
||||
.AddBaseListTypes(SimpleBaseType(
|
||||
GenericName(Identifier("global::System.Buffers.MemoryManager"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword)))))
|
||||
.AddAttributeLists(
|
||||
AttributeList(SingletonSeparatedList(
|
||||
Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments(
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).FullName))),
|
||||
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).Assembly.GetName().Version.ToString())))))),
|
||||
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))),
|
||||
AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))))
|
||||
.WithLeadingTrivia(
|
||||
Comment("/// <summary>"),
|
||||
Comment("""/// A <see cref="global::System.Buffers.MemoryManager{T}"/> implementation to get the HLSL bytecode."""),
|
||||
Comment("/// </summary>"));
|
||||
|
||||
// Return the combined block
|
||||
return Block(embeddedBranch.WithElse(ElseClause(dynamicLoadingBlock)));
|
||||
using ImmutableArrayBuilder<MemberDeclarationSyntax> memberDeclarations = ImmutableArrayBuilder<MemberDeclarationSyntax>.Rent();
|
||||
|
||||
// Declare the singleton property to get the memory instance:
|
||||
//
|
||||
// /// <summary>The singleton <see cref="global::System.ReadOnlyMemory{T}"/> instance for the memory manager.</summary>
|
||||
// public static new readonly global::System.ReadOnlyMemory<byte> Memory = new HlslBytecodeMemoryManager().CreateMemory(<BYTECODE_SIZE>);
|
||||
memberDeclarations.Add(
|
||||
FieldDeclaration(
|
||||
VariableDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlyMemory"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))))
|
||||
.AddVariables(
|
||||
VariableDeclarator(Identifier("Memory"))
|
||||
.WithInitializer(
|
||||
EqualsValueClause(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
ObjectCreationExpression(IdentifierName("HlslBytecodeMemoryManager")).WithArgumentList(ArgumentList()),
|
||||
IdentifierName("CreateMemory")))
|
||||
.AddArgumentListArguments(Argument(
|
||||
LiteralExpression(
|
||||
SyntaxKind.NumericLiteralExpression,
|
||||
Literal(bytecodeInfo.Bytecode.Length))))))))
|
||||
.AddModifiers(
|
||||
Token(SyntaxKind.PublicKeyword),
|
||||
Token(SyntaxKind.StaticKeyword),
|
||||
Token(SyntaxKind.NewKeyword),
|
||||
Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.WithLeadingTrivia(Comment("""/// <summary>The singleton <see cref="global::System.ReadOnlyMemory{T}"/> instance for the memory manager.</summary>""")));
|
||||
|
||||
// Construct the RVA span property:
|
||||
//
|
||||
// /// <summary>The RVA data with the HLSL bytecode.</summary>
|
||||
// private static global::System.ReadOnlySpan<byte> Data => new byte[] { __EMBEDDED_SHADER_BYTECODE };
|
||||
memberDeclarations.Add(
|
||||
PropertyDeclaration(
|
||||
GenericName(Identifier("global::System.ReadOnlySpan"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))),
|
||||
Identifier("Data"))
|
||||
.AddModifiers(Token(SyntaxKind.PrivateKeyword), Token(SyntaxKind.StaticKeyword))
|
||||
.WithExpressionBody(
|
||||
ArrowExpressionClause(
|
||||
ArrayCreationExpression(
|
||||
ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword)))
|
||||
.AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList<ExpressionSyntax>(OmittedArraySizeExpression()))),
|
||||
InitializerExpression(
|
||||
SyntaxKind.ArrayInitializerExpression,
|
||||
SingletonSeparatedList<ExpressionSyntax>(IdentifierName("__EMBEDDED_SHADER_BYTECODE"))))))
|
||||
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
|
||||
.WithLeadingTrivia(Comment("/// <summary>The RVA data with the HLSL bytecode.</summary>")));
|
||||
|
||||
// Add the GetSpan() method:
|
||||
//
|
||||
// /// <inheritdoc/>
|
||||
// public override unsafe global::System.Span<byte> GetSpan
|
||||
// {
|
||||
// return new(global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref global::System.Runtime.InteropServices.MemoryMarshal(Data)), <BYTECODE_SIZE>);
|
||||
// }
|
||||
memberDeclarations.Add(
|
||||
MethodDeclaration(
|
||||
GenericName(Identifier("global::System.Span"))
|
||||
.AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))),
|
||||
Identifier("GetSpan"))
|
||||
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword), Token(SyntaxKind.UnsafeKeyword))
|
||||
.AddBodyStatements(
|
||||
ReturnStatement(
|
||||
ImplicitObjectCreationExpression()
|
||||
.AddArgumentListArguments(
|
||||
Argument(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::System.Runtime.CompilerServices.Unsafe"),
|
||||
IdentifierName("AsPointer")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::System.Runtime.InteropServices.MemoryMarshal"),
|
||||
IdentifierName("GetReference")))
|
||||
.AddArgumentListArguments(Argument(IdentifierName("Data"))))
|
||||
.WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))),
|
||||
Argument(LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(bytecodeInfo.Bytecode.Length))))))
|
||||
.WithLeadingTrivia(Comment("/// <inheritdoc/>")));
|
||||
|
||||
// Add the Pin(int elementIndex) method:
|
||||
//
|
||||
// /// <inheritdoc/>
|
||||
// public override unsafe global::System.Buffers.MemoryHandle Pin(int elementIndex)
|
||||
// {
|
||||
// return new(Unsafe.AsPointer(ref Unsafe.AsRef(in Data[elementIndex])), pinnable: this);
|
||||
// }
|
||||
memberDeclarations.Add(
|
||||
MethodDeclaration(IdentifierName("global::System.Buffers.MemoryHandle"), Identifier("Pin"))
|
||||
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword), Token(SyntaxKind.UnsafeKeyword))
|
||||
.AddParameterListParameters(Parameter(Identifier("elementIndex")).WithType(PredefinedType(Token(SyntaxKind.IntKeyword))))
|
||||
.AddBodyStatements(
|
||||
ReturnStatement(
|
||||
ImplicitObjectCreationExpression()
|
||||
.AddArgumentListArguments(
|
||||
Argument(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::System.Runtime.CompilerServices.Unsafe"),
|
||||
IdentifierName("AsPointer")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(
|
||||
InvocationExpression(
|
||||
MemberAccessExpression(
|
||||
SyntaxKind.SimpleMemberAccessExpression,
|
||||
IdentifierName("global::System.Runtime.CompilerServices.Unsafe"),
|
||||
IdentifierName("AsRef")))
|
||||
.AddArgumentListArguments(
|
||||
Argument(
|
||||
ElementAccessExpression(IdentifierName("Data"))
|
||||
.AddArgumentListArguments(Argument(IdentifierName("elementIndex"))))
|
||||
.WithRefOrOutKeyword(Token(SyntaxKind.InKeyword))))
|
||||
.WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)))),
|
||||
Argument(ThisExpression()).WithNameColon(NameColon(IdentifierName("pinnable"))))))
|
||||
.WithLeadingTrivia(Comment("/// <inheritdoc/>")));
|
||||
|
||||
// Add the empty Unpin() method
|
||||
memberDeclarations.Add(
|
||||
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("Unpin"))
|
||||
.AddModifiers(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.OverrideKeyword))
|
||||
.WithBody(Block())
|
||||
.WithLeadingTrivia(Comment("/// <inheritdoc/>")));
|
||||
|
||||
// Add the empty Dispose(bool disposing) method
|
||||
memberDeclarations.Add(
|
||||
MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier("Dispose"))
|
||||
.AddModifiers(Token(SyntaxKind.ProtectedKeyword), Token(SyntaxKind.OverrideKeyword))
|
||||
.AddParameterListParameters(
|
||||
Parameter(Identifier("disposing")).WithType(PredefinedType(Token(SyntaxKind.BoolKeyword))))
|
||||
.WithBody(Block())
|
||||
.WithLeadingTrivia(Comment("/// <inheritdoc/>")));
|
||||
|
||||
return typeDeclaration.AddMembers(memberDeclarations.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -362,11 +362,11 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
// Generate the LoadBytecode() methods
|
||||
context.RegisterSourceOutput(embeddedBytecode, static (context, item) =>
|
||||
{
|
||||
MethodDeclarationSyntax loadBytecodeMethod = LoadBytecode.GetSyntax(item.BytecodeInfo, out Func<SyntaxNode, SourceText> fixup);
|
||||
CompilationUnitSyntax loadBytecodeCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, loadBytecodeMethod, skipLocalsInit: false);
|
||||
SourceText text = fixup(loadBytecodeCompilationUnit);
|
||||
PropertyDeclarationSyntax hlslBytecodeProperty = LoadBytecode.GetHlslBytecodeSyntax(item.BytecodeInfo, out Func<SyntaxNode, SourceText> fixup, out TypeDeclarationSyntax[] additionalTypes);
|
||||
CompilationUnitSyntax hlslBytecodeCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, hlslBytecodeProperty, skipLocalsInit: false, additionalMemberDeclarations: additionalTypes);
|
||||
SourceText text = fixup(hlslBytecodeCompilationUnit);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(LoadBytecode)}.g.cs", text);
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.HlslBytecode.g.cs", text);
|
||||
|
||||
PropertyDeclarationSyntax shaderProfileProperty = LoadBytecode.GetShaderProfileSyntax(item.BytecodeInfo);
|
||||
CompilationUnitSyntax shaderProfileCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, shaderProfileProperty, skipLocalsInit: false);
|
||||
@@ -374,7 +374,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.ShaderProfile.g.cs", shaderProfileCompilationUnit.GetText(Encoding.UTF8));
|
||||
|
||||
PropertyDeclarationSyntax compileOptionsProperty = LoadBytecode.GetCompileOptionsSyntax(item.BytecodeInfo);
|
||||
CompilationUnitSyntax compileOptionsCompilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, compileOptionsProperty, canUseSkipLocalsInit: false);
|
||||
CompilationUnitSyntax compileOptionsCompilationUnit = GetCompilationUnitFromMember(item.Hierarchy, compileOptionsProperty, skipLocalsInit: false);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.CompileOptions.g.cs", compileOptionsCompilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
|
||||
namespace ComputeSharp.D2D1.__Internals;
|
||||
@@ -148,16 +147,12 @@ public interface ID2D1Shader
|
||||
D2D1CompileOptions CompileOptions { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Loads the bytecode for the current shader.
|
||||
/// Gets the HLSL bytecode for the current shader, if available.
|
||||
/// </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="compileOptions">The compile options to use to get the shader bytecode (if <see langword="null"/>, the precompiled shader will be used).</param>
|
||||
/// <exception cref="InvalidOperationException">Thrown if a precompiled bytecode was requested (<paramref name="shaderProfile"/> is <see langword="null"/>), but it wasn't availablle.</exception>
|
||||
/// <remarks>When this method returns, <paramref name="shaderProfile"/> and <paramref name="compileOptions"/> will be set to the effective laues used to create the shader bytecode that was loaded.</remarks>
|
||||
/// <remarks>
|
||||
/// If no precompiled HLSL bytecode is available, the returned <see cref="ReadOnlyMemory{T}"/> instance will be empty.
|
||||
/// </remarks>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This method is not intended to be used directly by user code")]
|
||||
void LoadBytecode<TLoader>(ref TLoader loader, [NotNull] ref D2D1ShaderProfile? shaderProfile, [NotNull] ref D2D1CompileOptions? compileOptions)
|
||||
where TLoader : struct, ID2D1BytecodeLoader;
|
||||
ReadOnlyMemory<byte> HlslBytecode { get; }
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする