Centralize marshalling logic in DxcShaderCompiler

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-17 03:10:38 +02:00
1 親 862ea5a51b
コミット 3fe4491f63
2ファイル変更
+18 -15

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

+4 -11
ファイルの表示
@@ -10,8 +10,6 @@ using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGenerators.Dxc;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
@@ -47,24 +45,19 @@ partial class ComputeShaderDescriptorGenerator
try
{
token.ThrowIfCancellationRequested();
// Try to load dxcompiler.dll and dxil.dll
DxcLibraryLoader.LoadNativeDxcLibraries();
token.ThrowIfCancellationRequested();
// Compile the shader bytecode
using ComPtr<IDxcBlob> dxcBlobBytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan());
byte[] bytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan(), token);
token.ThrowIfCancellationRequested();
byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
byte[] array = new ReadOnlySpan<byte>(buffer, length).ToArray();
ImmutableArray<byte> bytecode = Unsafe.As<byte[], ImmutableArray<byte>>(ref array);
return new HlslBytecodeInfo.Success(bytecode);
return new HlslBytecodeInfo.Success(Unsafe.As<byte[], ImmutableArray<byte>>(ref bytecode));
}
catch (Win32Exception e)
{
+14 -4
ファイルの表示
@@ -1,6 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using ComputeSharp.Core.Extensions;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
@@ -79,12 +80,12 @@ internal sealed unsafe class DxcShaderCompiler
/// Compiles a new HLSL shader from the input source code.
/// </summary>
/// <param name="source">The HLSL source code to compile.</param>
/// <param name="token">The <see cref="CancellationToken"/> used to cancel the operation, if needed.</param>
/// <returns>The bytecode for the compiled shader.</returns>
public ComPtr<IDxcBlob> Compile(ReadOnlySpan<char> source)
public byte[] Compile(ReadOnlySpan<char> source, CancellationToken token)
{
using ComPtr<IDxcBlobEncoding> dxcBlobEncoding = default;
using ComPtr<IDxcOperationResult> dxcOperationResult = default;
using ComPtr<IDxcBlob> dxcBlobBytecode = default;
// Get the encoded blob from the source code
fixed (char* p = source)
@@ -96,6 +97,8 @@ internal sealed unsafe class DxcShaderCompiler
dxcBlobEncoding.GetAddressOf()).Assert();
}
token.ThrowIfCancellationRequested();
// Try to compile the new compute shader
fixed (char* shaderName = "")
fixed (char* entryPoint = nameof(IComputeShader.Execute))
@@ -119,6 +122,8 @@ internal sealed unsafe class DxcShaderCompiler
dxcOperationResult.GetAddressOf()).Assert();
}
token.ThrowIfCancellationRequested();
HRESULT status;
dxcOperationResult.Get()->GetStatus(&status).Assert();
@@ -126,9 +131,14 @@ internal sealed unsafe class DxcShaderCompiler
// The compilation was successful, so we can extract the shader bytecode
if (status == 0)
{
using ComPtr<IDxcBlob> dxcBlobBytecode = default;
dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert();
return dxcBlobBytecode.Move();
byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
return new ReadOnlySpan<byte>(buffer, length).ToArray();
}
return ThrowHslsCompilationException(dxcOperationResult.Get());
@@ -140,7 +150,7 @@ internal sealed unsafe class DxcShaderCompiler
/// <param name="dxcOperationResult">The input (faulting) operation.</param>
/// <returns>This method always throws and never actually returs.</returns>
[MethodImpl(MethodImplOptions.NoInlining)]
private static ComPtr<IDxcBlob> ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult)
private static byte[] ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult)
{
using ComPtr<IDxcBlobEncoding> dxcBlobEncodingError = default;