Adopt HlslBytecodeInfo pattern in D3D12 generator

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-14 00:35:04 +02:00
1 親 49d0ec2a5b
コミット fa8d9446cd
7ファイル変更
+104 -56

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

@@ -148,7 +148,7 @@ partial class D2DPixelShaderDescriptorGenerator
/// </summary>
/// <param name="key">The <see cref="HlslBytecodeInfoKey"/> instance for the shader to compile.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <returns></returns>
/// <returns>The <see cref="HlslBytecodeInfo"/> instance for the current shader.</returns>
public static HlslBytecodeInfo GetInfo(ref HlslBytecodeInfoKey key, CancellationToken token)
{
static unsafe HlslBytecodeInfo GetInfo(HlslBytecodeInfoKey key, CancellationToken token)
+2 -2
ファイルの表示
@@ -24,7 +24,7 @@ partial class IShaderGenerator
using (writer.WriteBlock())
{
// If there is no HLSL bytecode, there's nothing left to do
if (info.EmbeddedBytecode.Bytecode.IsEmpty)
if (info.HlslInfo is not HlslBytecodeInfo.Success success)
{
return;
}
@@ -36,7 +36,7 @@ partial class IShaderGenerator
{
writer.Write("global::System.ReadOnlySpan<byte> bytecode = new byte[] { ");
SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(info.EmbeddedBytecode.Bytecode.AsSpan(), writer);
SyntaxFormattingHelper.WriteByteArrayInitializationExpressions(success.Bytecode.AsSpan(), writer);
writer.WriteLine(" };");
writer.WriteLine();
+49 -31
ファイルの表示
@@ -1,7 +1,6 @@
using System;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
@@ -14,7 +13,6 @@ using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
@@ -31,6 +29,11 @@ partial class IShaderGenerator
/// </summary>
private static partial class LoadBytecode
{
/// <summary>
/// Indicates whether the required <c>dxcompiler.dll</c> and <c>dxil.dll</c> libraries have been loaded.
/// </summary>
private static volatile bool areDxcLibrariesLoaded;
/// <summary>
/// Gets the thread ids values for a given shader type, if available.
/// </summary>
@@ -113,32 +116,21 @@ partial class IShaderGenerator
}
/// <summary>
/// Indicates whether the required <c>dxcompiler.dll</c> and <c>dxil.dll</c> libraries have been loaded.
/// </summary>
private static volatile bool areDxcLibrariesLoaded;
/// <summary>
/// Gets a <see cref="BlockSyntax"/> instance with the logic to try to get a compiled shader bytecode.
/// Gets the <see cref="HlslBytecodeInfo"/> instance for the input shader info.
/// </summary>
/// <param name="threadIds">The input <see cref="ThreadIdsInfo"/> instance to process.</param>
/// <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(
/// <returns>The <see cref="HlslBytecodeInfo"/> instance for the current shader.</returns>
public static unsafe HlslBytecodeInfo GetBytecode(
ThreadIdsInfo threadIds,
string hlslSource,
CancellationToken token,
out DeferredDiagnosticInfo? diagnostic)
CancellationToken token)
{
ImmutableArray<byte> bytecode = ImmutableArray<byte>.Empty;
// No embedded shader was requested
// Skip even attempting to compile if compilation is disabled (see comments in D2D1 generator)
if (threadIds.IsDefault)
{
diagnostic = null;
goto End;
return HlslBytecodeInfo.Missing.Instance;
}
try
@@ -148,12 +140,6 @@ partial class IShaderGenerator
token.ThrowIfCancellationRequested();
// Replace the actual thread num values
hlslSource = hlslSource
.Replace("<THREADSX>", threadIds.X.ToString(CultureInfo.InvariantCulture))
.Replace("<THREADSY>", threadIds.Y.ToString(CultureInfo.InvariantCulture))
.Replace("<THREADSZ>", threadIds.Z.ToString(CultureInfo.InvariantCulture));
// Compile the shader bytecode
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.Compile(hlslSource.AsSpan());
@@ -164,20 +150,52 @@ partial class IShaderGenerator
byte[] array = new ReadOnlySpan<byte>(buffer, length).ToArray();
bytecode = Unsafe.As<byte[], ImmutableArray<byte>>(ref array);
diagnostic = null;
ImmutableArray<byte> bytecode = Unsafe.As<byte[], ImmutableArray<byte>>(ref array);
return new HlslBytecodeInfo.Success(bytecode);
}
catch (Win32Exception e)
{
diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithWin32Exception, e.HResult, FixupExceptionMessage(e.Message));
return new HlslBytecodeInfo.Win32Error(e.NativeErrorCode, FixupExceptionMessage(e.Message));
}
catch (DxcCompilationException e)
{
diagnostic = DeferredDiagnosticInfo.Create(EmbeddedBytecodeFailedWithDxcCompilationException, FixupExceptionMessage(e.Message));
return new HlslBytecodeInfo.DxcError(FixupExceptionMessage(e.Message));
}
}
/// <summary>
/// Gets any diagnostics from a processed <see cref="HlslBytecodeInfo"/> instance.
/// </summary>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="info">The source <see cref="HlslBytecodeInfo"/> instance.</param>
/// <param name="diagnostics">The collection of produced <see cref="DiagnosticInfo"/> instances.</param>
public static void GetInfoDiagnostics(
INamedTypeSymbol structDeclarationSymbol,
HlslBytecodeInfo info,
ImmutableArrayBuilder<DiagnosticInfo> diagnostics)
{
DiagnosticInfo? diagnostic = null;
if (info is HlslBytecodeInfo.Win32Error win32Error)
{
diagnostic = DiagnosticInfo.Create(
EmbeddedBytecodeFailedWithWin32Exception,
structDeclarationSymbol,
new object[] { structDeclarationSymbol, win32Error.HResult, win32Error.Message });
}
else if (info is HlslBytecodeInfo.DxcError dxcError)
{
diagnostic = DiagnosticInfo.Create(
EmbeddedBytecodeFailedWithDxcCompilationException,
structDeclarationSymbol,
new object[] { structDeclarationSymbol, dxcError.Message });
}
End:
return bytecode;
if (diagnostic is not null)
{
diagnostics.Add(diagnostic);
}
}
/// <summary>
+11 -8
ファイルの表示
@@ -92,18 +92,21 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
token.ThrowIfCancellationRequested();
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(threadIds, hlslSourceInfo.HlslSource, token, out DeferredDiagnosticInfo? diagnostic);
HlslBytecodeInfo hlslInfo = LoadBytecode.GetBytecode(threadIds, hlslSourceInfo.HlslSource, token);
token.ThrowIfCancellationRequested();
EmbeddedBytecodeInfo bytecodeInfo = new(
threadIds.X,
threadIds.Y,
threadIds.Z,
bytecode);
LoadBytecode.GetInfoDiagnostics(typeSymbol, hlslInfo, diagnostics);
token.ThrowIfCancellationRequested();
// Also get the hierarchy too
HierarchyInfo hierarchyInfo = HierarchyInfo.From(typeSymbol);
token.ThrowIfCancellationRequested();
return new ShaderInfo(
Hierarchy: HierarchyInfo.From(typeSymbol),
Hierarchy: hierarchyInfo,
DispatchData: new DispatchDataInfo(
isPixelShaderLike,
fieldInfos,
@@ -111,7 +114,7 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
root32BitConstantCount),
DispatchMetadata: dispatchMetadataInfo,
HlslShaderSource: hlslSourceInfo,
EmbeddedBytecode: bytecodeInfo,
HlslInfo: hlslInfo,
ThreadIds: threadIds,
Diagnostcs: diagnostics.ToImmutable());
})
-12
ファイルの表示
@@ -1,12 +0,0 @@
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing a compiled shader bytecode, or none.
/// </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="Bytecode">The compiled shader bytecode, if available.</param>
internal sealed record EmbeddedBytecodeInfo(int X, int Y, int Z, EquatableArray<byte> Bytecode);
+39
ファイルの表示
@@ -0,0 +1,39 @@
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGenerators.Models;
/// <summary>
/// A model representing info on compiled HLSL bytecode, if available.
/// </summary>
internal abstract record HlslBytecodeInfo
{
/// <summary>
/// A successfully compiled HLSL shader.
/// </summary>
/// <param name="Bytecode">The resulting HLSL bytecode.</param>
public sealed record Success(EquatableArray<byte> Bytecode) : HlslBytecodeInfo;
/// <summary>
/// An HLSL shader that failed to compile due to a Win32 error.
/// </summary>
/// <param name="HResult">The HRESULT for the error.</param>
/// <param name="Message">The error message from compiling the shader.</param>
public sealed record Win32Error(int HResult, string Message) : HlslBytecodeInfo;
/// <summary>
/// An HLSL shader that failed to compile due to an DXC compiler error.
/// </summary>
/// <param name="Message">The error message from compiling the shader.</param>
public sealed record DxcError(string Message) : HlslBytecodeInfo;
/// <summary>
/// No HLSL bytecode is available (ie. compilation was not requested or some other error was detected).
/// </summary>
public sealed record Missing : HlslBytecodeInfo
{
/// <summary>
/// Gets the shared <see cref="Missing"/> instance.
/// </summary>
public static Missing Instance { get; } = new();
}
}
+2 -2
ファイルの表示
@@ -10,7 +10,7 @@ namespace ComputeSharp.SourceGenerators.Models;
/// <param name="DispatchData">The gathered shader dispatch data.</param>
/// <param name="DispatchMetadata">The gathered shader dispatch metadata.</param>
/// <param name="HlslShaderSource">The processed HLSL source for the shader.</param>
/// <param name="EmbeddedBytecode">The info on the compiled bytecode, if available.</param>
/// <param name="HlslInfo">The value with processed info on the shader.</param>
/// <param name="ThreadIds">The thread ids for the shader, if compilation is requested.</param>
/// <param name="Diagnostcs">The discovered diagnostics, if any.</param>
internal sealed record ShaderInfo(
@@ -18,6 +18,6 @@ internal sealed record ShaderInfo(
DispatchDataInfo DispatchData,
DispatchMetadataInfo DispatchMetadata,
HlslShaderSourceInfo HlslShaderSource,
EmbeddedBytecodeInfo EmbeddedBytecode,
HlslBytecodeInfo HlslInfo,
ThreadIdsInfo ThreadIds,
EquatableArray<DiagnosticInfo> Diagnostcs);