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

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

+1 -6
ファイルの表示
@@ -70,6 +70,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" PrivateAssets="all" Pack="false" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.49-beta" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
@@ -88,12 +89,6 @@
<Import Project="..\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems" Label="Shared" />
<Import Project="..\ComputeSharp.SourceGeneration.Hlsl\ComputeSharp.SourceGeneration.Hlsl.projitems" Label="Shared" />
<!-- Shared ComputeSharp.Win32.D3D12 with D3D12 bindings -->
<Import Project="..\ComputeSharp.Win32.D3D12\ComputeSharp.Win32.D3D12.projitems" Label="Shared" />
<!-- Shared projects with .NET Standard 2.0 polyfills -->
<Import Project="..\ComputeSharp.NetStandard\ComputeSharp.NetStandard.projitems" Label="Shared" />
<!-- Give access to the code fixers project for the exported diagnostic ids and properties -->
<ItemGroup>
<InternalsVisibleTo Include="ComputeSharp.CodeFixers, PublicKey=$(AssemblySignPublicKey)" />
+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());
+62
ファイルの表示
@@ -0,0 +1,62 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
// Ported from winrt/wrl/client.h in the Windows SDK for Windows 10.0.20348.0
// Original source is Copyright © Microsoft. All rights reserved.
using System;
using System.Runtime.CompilerServices;
using Windows.Win32.System.Com;
namespace ComputeSharp.SourceGenerators.Dxc.Interop;
/// <summary>A type that allows working with pointers to COM objects more securely.</summary>
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
internal unsafe struct ComPtr<T> : IDisposable
where T : unmanaged
{
/// <summary>The raw pointer to a COM object, if existing.</summary>
private T* ptr;
/// <summary>Creates a new <see cref="ComPtr{T}"/> instance from a raw pointer and increments the ref count.</summary>
/// <param name="other">The raw pointer to wrap.</param>
public ComPtr(T* other)
{
this.ptr = other;
if (other is not null)
{
_ = ((IUnknown*)other)->AddRef();
}
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
T* pointer = this.ptr;
if (pointer is not null)
{
this.ptr = null;
_ = ((IUnknown*)pointer)->Release();
}
}
/// <summary>Gets the currently wrapped raw pointer to a COM object.</summary>
/// <returns>The raw pointer wrapped by the current <see cref="ComPtr{T}"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly T* Get()
{
return this.ptr;
}
/// <summary>Gets the address of the current <see cref="ComPtr{T}"/> instance as a raw <typeparamref name="T"/> double pointer. This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.
/// </summary>
/// <returns>The raw pointer to the current <see cref="ComPtr{T}"/> instance.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly T** GetAddressOf()
{
return (T**)Unsafe.AsPointer(ref Unsafe.AsRef(in this));
}
}
+10
ファイルの表示
@@ -0,0 +1,10 @@
{
"$schema": "https://aka.ms/CsWin32.schema.json",
"allowMarshaling": false,
"useSafeHandles": false,
"comInterop": {
"preserveSigMethods": [
"*"
]
}
}
+7
ファイルの表示
@@ -0,0 +1,7 @@
CLSID_DxcCompiler
CLSID_DxcLibrary
DxcCreateInstance
IDxcCompiler
IDxcLibrary
IDxcIncludeHandler
IUnknown