Minor code tweaks to DX12 generator

このコミットが含まれているのは:
Sergio Pedri committed 2023-12-17 12:17:21 +01:00
1 親 ac66eb7827
コミット 35bc8ce649
3ファイル変更
+31 -19

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

+14 -2
ファイルの表示
@@ -10,6 +10,8 @@ using ComputeSharp.SourceGeneration.Models;
using ComputeSharp.SourceGenerators.Dxc;
using ComputeSharp.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using Windows.Win32;
using Windows.Win32.Graphics.Direct3D.Dxc;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.SourceGenerators;
@@ -53,11 +55,21 @@ partial class ComputeShaderDescriptorGenerator
token.ThrowIfCancellationRequested();
// Compile the shader bytecode
byte[] bytecode = DxcShaderCompiler.Instance.Compile(key.HlslSource.AsSpan(), key.CompileOptions, token);
using ComPtr<IDxcBlob> dxcBlob = DxcShaderCompiler.Instance.Compile(
key.HlslSource.AsSpan(),
key.CompileOptions,
token);
token.ThrowIfCancellationRequested();
return new HlslBytecodeInfo.Success(Unsafe.As<byte[], ImmutableArray<byte>>(ref bytecode));
byte* buffer = (byte*)dxcBlob.Get()->GetBufferPointer();
int length = checked((int)dxcBlob.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);
}
catch (Win32Exception e)
{
+13 -13
ファイルの表示
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
@@ -73,7 +74,7 @@ internal sealed unsafe class DxcShaderCompiler
/// <param name="compileOptions">The compile options to use.</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 byte[] Compile(ReadOnlySpan<char> source, CompileOptions compileOptions, CancellationToken token)
public ComPtr<IDxcBlob> Compile(ReadOnlySpan<char> source, CompileOptions compileOptions, CancellationToken token)
{
using ComPtr<IDxcResult> dxcResult = default;
@@ -232,20 +233,18 @@ internal sealed unsafe class DxcShaderCompiler
dxcResult.Get()->GetStatus(&status).Assert();
// The compilation was successful, so we can extract the shader bytecode
if (status == 0)
// Throw an exception with the input messages, if the compilation fails
if (status != 0)
{
using ComPtr<IDxcBlob> dxcBlobBytecode = default;
dxcResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert();
byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
return new ReadOnlySpan<byte>(buffer, length).ToArray();
ThrowHslsCompilationException(dxcResult.Get());
}
return ThrowHslsCompilationException(dxcResult.Get());
using ComPtr<IDxcBlob> dxcBlobBytecode = default;
// The compilation was successful, so we can extract the shader bytecode
dxcResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert();
return dxcBlobBytecode.Move();
}
/// <summary>
@@ -253,8 +252,9 @@ internal sealed unsafe class DxcShaderCompiler
/// </summary>
/// <param name="dxcResult">The input (faulting) operation.</param>
/// <returns>This method always throws and never actually returs.</returns>
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static byte[] ThrowHslsCompilationException(IDxcResult* dxcResult)
private static void ThrowHslsCompilationException(IDxcResult* dxcResult)
{
using ComPtr<IDxcBlobEncoding> dxcBlobEncodingError = default;