Initial support for D2D1 shader compilation from generator

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:03:46 +02:00
1 親 f9ffb665c6
コミット e64f32b528
11ファイル変更
+287 -42

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

+3 -1
ファイルの表示
@@ -90,7 +90,7 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "ComputeSharp.Core.NetStanda
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.Core.SourceGenerators", "src\ComputeSharp.Core.SourceGenerators\ComputeSharp.Core.SourceGenerators.csproj", "{BFB003D1-788B-48DE-8065-39479C162CB0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.D2D1Interop.SourceGenerators", "src\ComputeSharp.D2D1Interop.SourceGenerators\ComputeSharp.D2D1Interop.SourceGenerators.csproj", "{E44053BD-A761-47FB-AA78-087A599672EA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.D2D1Interop.SourceGenerators", "src\ComputeSharp.D2D1Interop.SourceGenerators\ComputeSharp.D2D1Interop.SourceGenerators.csproj", "{E44053BD-A761-47FB-AA78-087A599672EA}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
@@ -116,6 +116,8 @@ Global
src\ComputeSharp.UI\ComputeSharp.UI.projitems*{afaafdc9-be42-45d4-8c79-0ee8c8edbf52}*SharedItemsImports = 5
src\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems*{bfb003d1-788b-48de-8065-39479c162cb0}*SharedItemsImports = 5
src\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5
src\ComputeSharp.D2D1Interop.NetStandard\ComputeSharp.D2D1Interop.NetStandard.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5
src\TerraFX.Interop.Windows.D2D1Interop\TerraFX.Interop.Windows.D2D1Interop.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5
samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{e7c9247d-016b-4819-af9f-0e9b3591e04b}*SharedItemsImports = 5
samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{e91f4d24-922a-4921-a8d7-9c45a143c45d}*SharedItemsImports = 5
src\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems*{eb52cb67-4162-4722-874b-44ea8da5450e}*SharedItemsImports = 5
@@ -63,6 +63,7 @@
<!-- ComputeSharp APIs -->
<Compile Include="..\ComputeSharp\Core\Helpers\AlignmentHelper.cs" Link="ComputeSharp\Core\Helpers\AlignmentHelper.cs" />
<Compile Include="..\ComputeSharp\Shaders\Dispatching\__Internals\ArrayPoolStringBuilder.cs" Link="ComputeSharp\Shaders\Dispatching\__Internals\ArrayPoolStringBuilder.cs" />
<!-- ComputeSharp.D2D1Interop APIs -->
<Compile Include="..\ComputeSharp.D2D1Interop\Attributes\Profiles\D2D1ShaderProfile.cs" Link="ComputeSharp.D2D1Interop\Attributes\Profiles\D2D1ShaderProfile.cs" />
@@ -71,11 +72,17 @@
<Compile Include="..\ComputeSharp.D2D1Interop\Attributes\D2DInputCountAttribute.cs" Link="ComputeSharp.D2D1Interop\Attributes\D2DInputCountAttribute.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Attributes\D2DInputSimpleAttribute.cs" Link="ComputeSharp.D2D1Interop\Attributes\D2DInputSimpleAttribute.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Attributes\D2DRequiresScenePositionAttribute.cs" Link="ComputeSharp.D2D1Interop\Attributes\D2DRequiresScenePositionAttribute.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Extensions\ComPtrExtensions.cs" Link="ComputeSharp.D2D1Interop\Extensions\ComPtrExtensions.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Extensions\HResultExtensions.cs" Link="ComputeSharp.D2D1Interop\Extensions\HResultExtensions.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Interfaces\ID2D1PixelShader.cs" Link="ComputeSharp.D2D1Interop\Interfaces\ID2D1PixelShader.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Interfaces\__Internals\ID2D1BytecodeLoader.cs" Link="ComputeSharp.D2D1Interop\Interfaces\__Internals\ID2D1BytecodeLoader.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Interfaces\__Internals\ID2D1DispatchDataLoader.cs" Link="ComputeSharp.D2D1Interop\Interfaces\__Internals\ID2D1DispatchDataLoader.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Interfaces\__Internals\ID2D1Shader.cs" Link="ComputeSharp.D2D1Interop\Interfaces\__Internals\ID2D1Shader.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Intrinsics\D2D1.cs" Link="ComputeSharp.D2D1Interop\Intrinsics\D2D1.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Shaders\Exceptions\FxcCompilationException.cs" Link="ComputeSharp.D2D1Interop\Shaders\Exceptions\FxcCompilationException.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Shaders\Translation\D2D1ShaderCompiler.cs" Link="ComputeSharp.D2D1Interop\Shaders\Translation\D2D1ShaderCompiler.cs" />
<Compile Include="..\ComputeSharp.D2D1Interop\Shaders\Translation\D2D1ShaderCompiler.ID3DInclude.cs" Link="ComputeSharp.D2D1Interop\Shaders\Translation\D2D1ShaderCompiler.ID3DInclude.cs" />
<EmbeddedResource Include="..\ComputeSharp.D2D1Interop\Shaders\Translation\Headers\d2d1effecthelpers.hlsli" Link="ComputeSharp.D2D1Interop\Shaders\Translation\Headers\d2d1effecthelpers.hlsli" />
</ItemGroup>
<ItemGroup>
@@ -90,4 +97,7 @@
<!-- Shared project with .NET Standard 2.0 polyfills -->
<Import Project="..\ComputeSharp.Core.NetStandard\ComputeSharp.Core.NetStandard.projitems" Label="Shared" />
<!-- Shared project with TerraFX.Interop.Windows fork -->
<Import Project="..\TerraFX.Interop.Windows.D2D1Interop\TerraFX.Interop.Windows.D2D1Interop.projitems" Label="Shared" />
<Import Project="..\ComputeSharp.D2D1Interop.NetStandard\ComputeSharp.D2D1Interop.NetStandard.projitems" Label="Shared" />
</Project>
@@ -0,0 +1,65 @@
using System;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using ComputeSharp.D2D1Interop.Exceptions;
using ComputeSharp.D2D1Interop.Shaders.Translation;
using ComputeSharp.D2D1Interop.SourceGenerators.Models;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static ComputeSharp.D2D1Interop.SourceGenerators.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1Interop.SourceGenerators;
/// <inheritdoc/>
partial class ID2D1ShaderGenerator
{
/// <inheritodoc/>
internal static partial class LoadBytecode
{
/// <summary>
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
/// </summary>
/// <param name="hlslSource">The generated HLSL source code (ignoring captured delegates, if present).</param>
/// <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(
string hlslSource,
CancellationToken token,
out DiagnosticInfo? diagnostic)
{
ImmutableArray<byte> bytecode = ImmutableArray<byte>.Empty;
try
{
token.ThrowIfCancellationRequested();
// Compile the shader bytecode
using ComPtr<ID3DBlob> dxcBlobBytecode = D2D1ShaderCompiler.Compile(hlslSource.AsSpan(), false);
token.ThrowIfCancellationRequested();
byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
byte[] array = new ReadOnlySpan<byte>(buffer, length).ToArray();
bytecode = Unsafe.As<byte[], ImmutableArray<byte>>(ref array);
diagnostic = null;
}
catch (Win32Exception e)
{
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, e.Message);
}
catch (FxcCompilationException e)
{
diagnostic = new DiagnosticInfo(EmbeddedBytecodeFailedWithDxcCompilationException, e.Message);
}
return bytecode;
}
}
}
@@ -249,7 +249,7 @@ partial class ID2D1ShaderGenerator
if (isShaderEntryPoint)
{
string entryPointDeclaration = processedMethod.NormalizeWhitespace(eol: "\n").ToFullString();
string adjustedEntryPointDeclaration = entryPointDeclaration.Replace("float4 Execute()", "D2D_ENTRY(Execute)");
string adjustedEntryPointDeclaration = entryPointDeclaration.Replace("float4 Execute()", "D2D_PS_ENTRY(Execute)");
entryPoint = adjustedEntryPointDeclaration;
}
@@ -389,6 +389,14 @@ partial class ID2D1ShaderGenerator
AppendLineAndLF("// See: https://github.com/Sergio0694/ComputeSharp.");
AppendLF();
// TODO: switch to discovered ones
AppendLineAndLF("#define D2D_INPUT_COUNT 1");
AppendLineAndLF("#define D2D_INPUT0_COMPLEX");
AppendLineAndLF("#define D2D_REQUIRES_SCENE_POSITION");
// Add the "d2d1effecthelpers.hlsli" header
AppendLineAndLF("#include \"d2d1effecthelpers.hlsli\"");
// Define declarations
foreach (var (name, value) in definedConstants)
{
@@ -1,4 +1,9 @@
using ComputeSharp.D2D1Interop.__Internals;
using System;
using System.Collections.Immutable;
using ComputeSharp.__Internals;
using ComputeSharp.D2D1Interop.__Internals;
using ComputeSharp.D2D1Interop.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@@ -16,11 +21,21 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Creates a <see cref="MethodDeclarationSyntax"/> instance for the <c>TryGetBytecode</c> method.
/// </summary>
/// <param name="hlslSource">The input HLSL source.</param>
/// <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>BuildHlslString</c> method.</returns>
public static MethodDeclarationSyntax GetSyntax(string hlslSource)
public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeInfo bytecodeInfo, out Func<SyntaxNode, string> fixup)
{
BlockSyntax block = GetShaderBytecodeBody(hlslSource);
BlockSyntax block = GetShaderBytecodeBody(bytecodeInfo, out string? bytecodeLiterals);
if (bytecodeLiterals is not null)
{
fixup = tree => tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals);
}
else
{
fixup = static tree => tree.ToFullString();
}
// This code produces a method declaration as follows:
//
@@ -45,21 +60,128 @@ partial class ID2D1ShaderGenerator
/// <summary>
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
/// </summary>
/// <param name="hlslSource">The input HLSL source.</param>
/// <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(string hlslSource)
private static unsafe BlockSyntax GetShaderBytecodeBody(EmbeddedBytecodeInfo bytecodeInfo, out string? bytecodeLiterals)
{
ImmutableArray<StatementSyntax>.Builder statements = ImmutableArray.CreateBuilder<StatementSyntax>();
// This code produces a branch as follows:
//
// hlslSource = <HLSL_SOURCE>;
return
Block(ExpressionStatement(
statements.Add(
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName("hlslSource"),
LiteralExpression(
SyntaxKind.StringLiteralExpression,
Literal(hlslSource)))));
Literal(bytecodeInfo.HlslSource)))));
// Load the bytecode literals, if available, otherwise emit the dynamic loading code
if (bytecodeInfo.Bytecode.IsEmpty)
{
bytecodeLiterals = null;
// This code produces a method declaration as follows:
//
// ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler.LoadDynamicBytecode(ref loader, hlslSource);
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("ComputeSharp.D2D1Interop.__Internals.D2D1ShaderCompiler"),
IdentifierName("LoadDynamicBytecode")))
.AddArgumentListArguments(
Argument(IdentifierName("loader")).WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)),
Argument(IdentifierName("hlslSource")))));
}
else
{
bytecodeLiterals = BuildShaderBytecodeExpressionString(bytecodeInfo.Bytecode.AsSpan());
// This code produces the following statements:
//
// global::System.ReadOnlySpan<byte> bytecode = new byte[] { __EMBEDDED_SHADER_BYTECODE };
//
// 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.
statements.Add(
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")))))))));
// loader.LoadEmbeddedBytecode(bytecode);
statements.Add(
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("loader"),
IdentifierName("LoadEmbeddedBytecode")))
.AddArgumentListArguments(Argument(IdentifierName("bytecode")))));
}
return Block(statements);
}
/// <summary>
/// A mapping of precomputed literals for all <see cref="byte"/> values.
/// </summary>
private static readonly string[] formattedBytes =
{
"0x00", "0x01", "0x02", "0x03", "0x04", "0x05", "0x06", "0x07", "0x08", "0x09", "0x0A", "0x0B", "0x0C", "0x0D", "0x0E", "0x0F",
"0x10", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "0x17", "0x18", "0x19", "0x1A", "0x1B", "0x1C", "0x1D", "0x1E", "0x1F",
"0x20", "0x21", "0x22", "0x23", "0x24", "0x25", "0x26", "0x27", "0x28", "0x29", "0x2A", "0x2B", "0x2C", "0x2D", "0x2E", "0x2F",
"0x30", "0x31", "0x32", "0x33", "0x34", "0x35", "0x36", "0x37", "0x38", "0x39", "0x3A", "0x3B", "0x3C", "0x3D", "0x3E", "0x3F",
"0x40", "0x41", "0x42", "0x43", "0x44", "0x45", "0x46", "0x47", "0x48", "0x49", "0x4A", "0x4B", "0x4C", "0x4D", "0x4E", "0x4F",
"0x50", "0x51", "0x52", "0x53", "0x54", "0x55", "0x56", "0x57", "0x58", "0x59", "0x5A", "0x5B", "0x5C", "0x5D", "0x5E", "0x5F",
"0x60", "0x61", "0x62", "0x63", "0x64", "0x65", "0x66", "0x67", "0x68", "0x69", "0x6A", "0x6B", "0x6C", "0x6D", "0x6E", "0x6F",
"0x70", "0x71", "0x72", "0x73", "0x74", "0x75", "0x76", "0x77", "0x78", "0x79", "0x7A", "0x7B", "0x7C", "0x7D", "0x7E", "0x7F",
"0x80", "0x81", "0x82", "0x83", "0x84", "0x85", "0x86", "0x87", "0x88", "0x89", "0x8A", "0x8B", "0x8C", "0x8D", "0x8E", "0x8F",
"0x90", "0x91", "0x92", "0x93", "0x94", "0x95", "0x96", "0x97", "0x98", "0x99", "0x9A", "0x9B", "0x9C", "0x9D", "0x9E", "0x9F",
"0xA0", "0xA1", "0xA2", "0xA3", "0xA4", "0xA5", "0xA6", "0xA7", "0xA8", "0xA9", "0xAA", "0xAB", "0xAC", "0xAD", "0xAE", "0xAF",
"0xB0", "0xB1", "0xB2", "0xB3", "0xB4", "0xB5", "0xB6", "0xB7", "0xB8", "0xB9", "0xBA", "0xBB", "0xBC", "0xBD", "0xBE", "0xBF",
"0xC0", "0xC1", "0xC2", "0xC3", "0xC4", "0xC5", "0xC6", "0xC7", "0xC8", "0xC9", "0xCA", "0xCB", "0xCC", "0xCD", "0xCE", "0xCF",
"0xD0", "0xD1", "0xD2", "0xD3", "0xD4", "0xD5", "0xD6", "0xD7", "0xD8", "0xD9", "0xDA", "0xDB", "0xDC", "0xDD", "0xDE", "0xDF",
"0xE0", "0xE1", "0xE2", "0xE3", "0xE4", "0xE5", "0xE6", "0xE7", "0xE8", "0xE9", "0xEA", "0xEB", "0xEC", "0xED", "0xEE", "0xEF",
"0xF0", "0xF1", "0xF2", "0xF3", "0xF4", "0xF5", "0xF6", "0xF7", "0xF8", "0xF9", "0xFA", "0xFB", "0xFC", "0xFD", "0xFE", "0xFF"
};
/// <summary>
/// Creates a formatted expression to initialize a <see cref="ReadOnlySpan{T}"/> with a given shader data.
/// </summary>
/// <param name="bytecode">The input shader bytecode to serialize.</param>
/// <returns>A formatted <see cref="string"/> with the serialized data.</returns>
private static string BuildShaderBytecodeExpressionString(ReadOnlySpan<byte> bytecode)
{
//The estimation is 4 characters per byte (up to "255" in hex), plus ", " to separate sequential values
using ArrayPoolStringBuilder builder = ArrayPoolStringBuilder.Create(bytecode.Length * 6);
builder.Append(formattedBytes[bytecode[0]]);
foreach (byte b in bytecode.Slice(1))
{
builder.Append(", ");
builder.Append(formattedBytes[b]);
}
return builder.WrittenSpan.ToString();
}
}
}
+50 -7
ファイルの表示
@@ -1,4 +1,6 @@
using System.Collections.Immutable;
using System;
using System.Collections.Immutable;
using System.Linq;
using ComputeSharp.D2D1Interop.SourceGenerators.Diagnostics;
using ComputeSharp.D2D1Interop.SourceGenerators.Extensions;
using ComputeSharp.D2D1Interop.SourceGenerators.Models;
@@ -121,13 +123,54 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
.Select(static (item, token) => item.SourceInfo)
.WithComparer(HlslShaderSourceInfo.Comparer.Default);
// Generate the LoadBytecode() methods
context.RegisterSourceOutput(hlslSourceInfo, static (context, item) =>
{
MethodDeclarationSyntax loadBytecodeMethod = LoadBytecode.GetSyntax(item.HlslSource);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, loadBytecodeMethod, false);
// Compile the requested shader bytecodes
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo, DiagnosticInfo? Diagnostic)> embeddedBytecodeWithErrors =
hlslSourceInfo
.Select(static (item, token) =>
{
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(item.HlslSource, token, out DiagnosticInfo? diagnostic);
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(LoadBytecode)}", compilationUnit.ToFullString());
token.ThrowIfCancellationRequested();
EmbeddedBytecodeInfo bytecodeInfo = new(item.HlslSource, bytecode);
return (item.Hierarchy, bytecodeInfo, diagnostic);
});
// Gather the diagnostics
IncrementalValuesProvider<ImmutableArray<Diagnostic>> 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(embeddedBytecodeDiagnostics);
// Get the filtered sequence to enable caching
IncrementalValuesProvider<(HierarchyInfo Hierarchy, EmbeddedBytecodeInfo BytecodeInfo)> embeddedBytecode =
embeddedBytecodeWithErrors
.Select(static (item, token) => (item.Hierarchy, item.BytecodeInfo))
.WithComparers(HierarchyInfo.Comparer.Default, EmbeddedBytecodeInfo.Comparer.Default);
// Generate the LoadBytecode() methods
context.RegisterSourceOutput(embeddedBytecode, static (context, item) =>
{
MethodDeclarationSyntax loadBytecodeMethod = LoadBytecode.GetSyntax(item.BytecodeInfo, out Func<SyntaxNode, string> fixup);
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMethod(item.Hierarchy, loadBytecodeMethod, false);
string text = fixup(compilationUnit);
context.AddSource($"{item.Hierarchy.FilenameHint}.{nameof(LoadBytecode)}", text);
});
}
}
+5 -11
ファイルの表示
@@ -6,13 +6,11 @@ using ComputeSharp.D2D1Interop.SourceGenerators.Extensions;
namespace ComputeSharp.D2D1Interop.SourceGenerators.Models;
/// <summary>
/// A model representing a compiled shader bytecode, or none.
/// A model representing a shared and its compiled bytecode, if available.
/// </summary>
/// <param name="X">The thread ids value for the X axis.</param>
/// <param name="Y">The thread ids value for the Y axis.</param>
/// <param name="Z">The thread ids value for the Z axis.</param>
/// <param name="HlslSource">The HLSL source for the shader.</param>
/// <param name="Bytecode">The compiled shader bytecode, if available.</param>
internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray<byte> Bytecode)
internal sealed record EmbeddedBytecodeInfo(string HlslSource, ImmutableArray<byte> Bytecode)
{
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="EmbeddedBytecodeInfo"/>.
@@ -43,9 +41,7 @@ internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray<
}
return
x.X == y.X &&
x.Y == y.Y &&
x.Z == y.Z &&
x.HlslSource == y.HlslSource &&
x.Bytecode.SequenceEqual(y.Bytecode);
}
@@ -54,9 +50,7 @@ internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, ImmutableArray<
{
HashCode hashCode = default;
hashCode.Add(obj.X);
hashCode.Add(obj.Y);
hashCode.Add(obj.Z);
hashCode.Add(obj.HlslSource);
hashCode.AddBytes(obj.Bytecode.AsSpan());
return hashCode.ToHashCode();
@@ -1,12 +0,0 @@
using System.ComponentModel;
namespace System.Runtime.CompilerServices;
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
+8 -1
ファイルの表示
@@ -1,4 +1,5 @@
using ComputeSharp.Exceptions;
using ComputeSharp.Core.Intrinsics.Attributes;
using ComputeSharp.Exceptions;
namespace ComputeSharp.D2D1Interop;
@@ -14,6 +15,7 @@ public static class D2D1
/// <param name="index">The index of the input texture to get the input from.</param>
/// <returns>The color from the target input at the current coordinate, in <c>INPUTN</c> format.</returns>
/// <remarks>This method is only available for simple inputs.</remarks>
[HlslIntrinsicName("D2DGetInput")]
public static Float4 GetInput(int index) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(GetInput)}({typeof(int)})");
/// <summary>
@@ -22,6 +24,7 @@ public static class D2D1
/// <param name="index">The index of the input texture to get the coordinate for.</param>
/// <returns>The input coordinate, in <c>TEXCOORDN</c> format.</returns>
/// <remarks>This method is only available for complex inputs.</remarks>
[HlslIntrinsicName("D2DGetInputCoordinate")]
public static Float4 GetInputCoordinate(int index) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(GetInputCoordinate)}({typeof(int)})");
/// <summary>
@@ -29,6 +32,7 @@ public static class D2D1
/// </summary>
/// <returns>The current scene position, in <c>SCENE_POSITION</c> format.</returns>
/// <remarks>This method is only available when <see cref="D2DRequiresScenePositionAttribute"/> is used on the shader type.</remarks>
[HlslIntrinsicName("D2DGetScenePosition")]
public static Float4 GetScenePosition() => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(GetScenePosition)}()");
/// <summary>
@@ -38,6 +42,7 @@ public static class D2D1
/// <param name="uv">The normalized coordinate to use to sample the texture.</param>
/// <returns>The sampled value from the texture, in <c>TEXCOORDN</c> format.</returns>
/// <remarks>This method is only available for complex inputs.</remarks>
[HlslIntrinsicName("D2DSampleInput")]
public static Float4 SampleInput(int index, Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(SampleInput)}({typeof(int)}, {typeof(Float2)})");
/// <summary>
@@ -47,6 +52,7 @@ public static class D2D1
/// <param name="offset">The normalized offset to use to sample the texture.</param>
/// <returns>The sampled value from the texture, in <c>TEXCOORDN</c> format.</returns>
/// <remarks>This method is only available for simple inputs.</remarks>
[HlslIntrinsicName("D2DSampleInputAtOffset")]
public static Float4 SampleInputAtOffset(int index, Float2 offset) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(SampleInputAtOffset)}({typeof(int)}, {typeof(Float2)})");
/// <summary>
@@ -56,5 +62,6 @@ public static class D2D1
/// <param name="uv">The normalized coordinate to use to sample the texture.</param>
/// <returns>The sampled value from the texture, in <c>TEXCOORDN</c> format.</returns>
/// <remarks>This method is only available for complex inputs.</remarks>
[HlslIntrinsicName("D2DSampleInputAtPosition")]
public static Float4 SampleInputAtPosition(int index, Float2 uv) => throw new InvalidExecutionContextException($"{typeof(D2D1)}.{nameof(SampleInputAtPosition)}({typeof(int)}, {typeof(Float2)})");
}
+4
ファイルの表示
@@ -128,7 +128,11 @@ partial class D2D1ShaderCompiler
/// <returns>A pointer to a buffer with the contents of <c>d2d1effecthelpers.hlsli</c>.</returns>
private static void* LoadD2D1EffectHelpersHeader(out int size)
{
#if SOURCE_GENERATOR
const string headerFilename = "ComputeSharp.D2D1Interop.SourceGenerators.ComputeSharp.D2D1Interop.Shaders.Translation.Headers.d2d1effecthelpers.hlsli";
#else
const string headerFilename = "ComputeSharp.D2D1Interop.Shaders.Translation.Headers.d2d1effecthelpers.hlsli";
#endif
using Stream source = Assembly.GetExecutingAssembly().GetManifestResourceStream(headerFilename)!;
using MemoryStream destination = new();
+2
ファイルの表示
@@ -6,6 +6,8 @@
using System.Runtime.CompilerServices;
using TerraFX.Interop.Windows;
#pragma warning disable CS0649
namespace TerraFX.Interop.DirectX
{
internal unsafe partial struct ID3DInclude