Improve IShader source generator cancellation support

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-30 13:58:37 +01:00
1 親 a4184221b0
コミット 704e535747
2ファイル変更
+27 -5

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

+7
ファイルの表示
@@ -6,6 +6,7 @@ using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using ComputeSharp.Exceptions;
using ComputeSharp.Shaders.Translation;
using ComputeSharp.SourceGenerators.Diagnostics;
@@ -138,11 +139,13 @@ public sealed partial class IShaderGenerator
/// </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(
ThreadIdsInfo threadIds,
string hlslSource,
CancellationToken token,
out DiagnosticInfo? diagnostic)
{
ImmutableArray<byte> bytecode = ImmutableArray<byte>.Empty;
@@ -160,6 +163,8 @@ public sealed partial class IShaderGenerator
// Try to load dxcompiler.dll and dxil.dll
LoadNativeDxcLibraries();
token.ThrowIfCancellationRequested();
// Replace the actual thread num values
hlslSource = hlslSource
.Replace("<THREADSX>", threadIds.X.ToString())
@@ -169,6 +174,8 @@ public sealed partial class IShaderGenerator
// Compile the shader bytecode
using ComPtr<IDxcBlob> dxcBlobBytecode = ShaderCompiler.Instance.CompileShader(hlslSource.AsSpan());
token.ThrowIfCancellationRequested();
byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
+20 -5
ファイルの表示
@@ -91,6 +91,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
out int root32BitConstantCount,
out ImmutableArray<Diagnostic> dispatchDataDiagnostics);
token.ThrowIfCancellationRequested();
DispatchDataInfo dispatchDataInfo = new(
item.Left.Left.Hierarchy,
item.Left.Left.Type,
@@ -98,6 +100,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
resourceCount,
root32BitConstantCount);
token.ThrowIfCancellationRequested();
// BuildHlslString() info
HlslShaderSourceInfo hlslSourceInfo = BuildHlslString.GetInfo(
item.Right,
@@ -105,6 +109,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
item.Left.Left.Symbol,
out ImmutableArray<Diagnostic> hlslSourceDiagnostics);
token.ThrowIfCancellationRequested();
// TryGetBytecode() info
ThreadIdsInfo threadIds = LoadBytecode.GetInfo(
item.Left.Left.Symbol,
@@ -112,6 +118,8 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
item.Left.Right,
out ImmutableArray<Diagnostic> threadIdsDiagnostics);
token.ThrowIfCancellationRequested();
return (
new Result<DispatchDataInfo>(dispatchDataInfo, dispatchDataDiagnostics),
new Result<HlslShaderSourceInfo>(hlslSourceInfo, hlslSourceDiagnostics),
@@ -167,13 +175,18 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
// Get the dispatch metadata info
IncrementalValuesProvider<(HierarchyInfo Hierarchy, DispatchMetadataInfo MetadataInfo)> dispatchMetadataInfo =
shaderInfo
.Select(static (item, token) => (
item.Dispatch.Hierarchy,
LoadDispatchMetadata.GetInfo(
.Select(static (item, token) =>
{
DispatchMetadataInfo dispatchMetadataInfo = LoadDispatchMetadata.GetInfo(
item.Dispatch.Root32BitConstantCount,
item.Hlsl.ImplicitTextureType is not null,
item.Hlsl.IsSamplerUsed,
item.Dispatch.FieldInfos)))
item.Dispatch.FieldInfos);
token.ThrowIfCancellationRequested();
return (item.Dispatch.Hierarchy, dispatchMetadataInfo);
})
.WithComparers(HierarchyInfo.Comparer.Default, DispatchMetadataInfo.Comparer.Default);
// Generate the LoadDispatchMetadata() methods
@@ -196,7 +209,9 @@ public sealed partial class IShaderGenerator : IIncrementalGenerator
shaderBytecodeInfo
.Select(static (item, token) =>
{
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(item.ThreadIds, item.Hlsl, out DiagnosticInfo? diagnostic);
ImmutableArray<byte> bytecode = LoadBytecode.GetBytecode(item.ThreadIds, item.Hlsl, token, out DiagnosticInfo? diagnostic);
token.ThrowIfCancellationRequested();
EmbeddedBytecodeInfo bytecodeInfo = new(
item.ThreadIds.X,