Switch to CsWin32 for DXC APIs in D3D12 generator

このコミットが含まれているのは:
Sergio Pedri committed 2023-10-17 12:47:01 +02:00
1 親 38c02e48d0
コミット e0b2c6a9fd
7ファイル変更
+121 -53

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

+34 -41
ファイルの表示
@@ -1,10 +1,11 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using ComputeSharp.SourceGenerators.Dxc.Interop;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Direct3D.Dxc;
namespace ComputeSharp.SourceGenerators.Dxc;
@@ -42,19 +43,20 @@ internal sealed unsafe class DxcShaderCompiler
{
using ComPtr<IDxcCompiler> dxcCompiler = default;
using ComPtr<IDxcLibrary> dxcLibrary = default;
_ = PInvoke.DxcCreateInstance(
PInvoke.CLSID_DxcCompiler,
IDxcCompiler.IID_Guid,
out *(void**)dxcCompiler.GetAddressOf()).ThrowOnFailure();
_ = PInvoke.DxcCreateInstance(
PInvoke.CLSID_DxcLibrary,
IDxcLibrary.IID_Guid,
out *(void**)dxcLibrary.GetAddressOf()).ThrowOnFailure();
using ComPtr<IDxcIncludeHandler> dxcIncludeHandler = default;
Marshal.ThrowExceptionForHR(DirectX.DxcCreateInstance(
(Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcCompiler)),
Windows.__uuidof<IDxcCompiler>(),
(void**)dxcCompiler.GetAddressOf()));
Marshal.ThrowExceptionForHR(DirectX.DxcCreateInstance(
(Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_DxcLibrary)),
Windows.__uuidof<IDxcLibrary>(),
(void**)dxcLibrary.GetAddressOf()));
Marshal.ThrowExceptionForHR(dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()));
_ = dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()).ThrowOnFailure();
this.dxcCompiler = new ComPtr<IDxcCompiler>(dxcCompiler.Get());
this.dxcLibrary = new ComPtr<IDxcLibrary>(dxcLibrary.Get());
@@ -90,54 +92,45 @@ internal sealed unsafe class DxcShaderCompiler
// Get the encoded blob from the source code
fixed (char* p = source)
{
int hresult = this.dxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy(
p,
(uint)source.Length * 2,
1200,
dxcBlobEncoding.GetAddressOf());
Marshal.ThrowExceptionForHR(hresult);
_ = this.dxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy(
pText: p,
size: (uint)source.Length * 2,
codePage: DXC_CP.DXC_CP_UTF16,
pBlobEncoding: dxcBlobEncoding.GetAddressOf()).ThrowOnFailure();
}
token.ThrowIfCancellationRequested();
// Try to compile the new compute shader
fixed (char* shaderName = "")
fixed (char* entryPoint = nameof(IComputeShader.Execute))
fixed (char* shaderProfile = "cs_6_0")
fixed (char* optimization = "-O3")
fixed (char* rowMajor = "-Zpr")
fixed (char* warningsAsErrors = "-Werror")
{
char** arguments = stackalloc char*[3] { optimization, rowMajor, warningsAsErrors };
Span<PCWSTR> arguments = stackalloc PCWSTR[] { optimization, rowMajor, warningsAsErrors };
int hresult = this.dxcCompiler.Get()->Compile(
(IDxcBlob*)dxcBlobEncoding.Get(),
(ushort*)shaderName,
(ushort*)entryPoint,
(ushort*)shaderProfile,
(ushort**)arguments,
3,
null,
0,
this.dxcIncludeHandler.Get(),
dxcOperationResult.GetAddressOf());
Marshal.ThrowExceptionForHR(hresult);
_ = this.dxcCompiler.Get()->Compile(
pSource: (IDxcBlob*)dxcBlobEncoding.Get(),
pSourceName: "",
pEntryPoint: "Execute",
pTargetProfile: "cs_6_0",
pArguments: arguments,
pDefines: ReadOnlySpan<DxcDefine>.Empty,
pIncludeHandler: this.dxcIncludeHandler.Get(),
ppResult: dxcOperationResult.GetAddressOf()).ThrowOnFailure();
}
token.ThrowIfCancellationRequested();
HRESULT status;
Marshal.ThrowExceptionForHR(dxcOperationResult.Get()->GetStatus(&status));
_ = dxcOperationResult.Get()->GetStatus(&status).ThrowOnFailure();
// The compilation was successful, so we can extract the shader bytecode
if (status == 0)
{
using ComPtr<IDxcBlob> dxcBlobBytecode = default;
Marshal.ThrowExceptionForHR(dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()));
_ = dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).ThrowOnFailure();
byte* buffer = (byte*)dxcBlobBytecode.Get()->GetBufferPointer();
int length = checked((int)dxcBlobBytecode.Get()->GetBufferSize());
@@ -158,7 +151,7 @@ internal sealed unsafe class DxcShaderCompiler
{
using ComPtr<IDxcBlobEncoding> dxcBlobEncodingError = default;
Marshal.ThrowExceptionForHR(dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()));
_ = dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).ThrowOnFailure();
string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer());