Code refactoring to the TerraFX shader compiler
このコミットが含まれているのは:
1 親
3f1b27e092
コミット
3e6f9d27c9
5ファイル変更
+160
-83
検索条件に一致するファイルはありません
@@ -2,6 +2,11 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.19041-beta1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,83 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TerraFX.Interop;
|
||||
|
||||
namespace ComputeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper methods for working with the <see cref="ComPtr{T}"/> type.
|
||||
/// </summary>
|
||||
public static class ComPtrExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets whether the current <see cref="ComPtr{T}"/> instance as is not <see langword="null"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to check.</param>
|
||||
/// <returns>Whether or not <paramref name="ptr"/> is not <see langword="null"/>.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe bool Exists<T>(this ComPtr<T> ptr)
|
||||
where T : unmanaged
|
||||
{
|
||||
return ptr.Get() != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="ComPtr{T}"/> instance as a raw <see langword="void"/> double pointer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to get the address for.</param>
|
||||
/// <returns>The raw pointer to the input <see cref="ComPtr{T}"/> instance.</returns>
|
||||
/// <remarks>This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.</remarks>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void** GetVoidAddressOf<T>(this in ComPtr<T> ptr)
|
||||
where T : unmanaged
|
||||
{
|
||||
return (void**)Unsafe.AsPointer(ref Unsafe.AsRef(in ptr));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the address of the current <see cref="ComPtr{T}"/> instance as a raw <see langword="void"/> double pointer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to wrap in the current <see cref="ComPtr{T}"/> instance.</typeparam>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to get the address for.</param>
|
||||
/// <returns>The raw pointer to the input <see cref="ComPtr{T}"/> instance.</returns>
|
||||
/// <remarks>This method is only valid when the current <see cref="ComPtr{T}"/> instance is on the stack or pinned.</remarks>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ComPtr<T> Move<T>(this in ComPtr<T> ptr)
|
||||
where T : unmanaged
|
||||
{
|
||||
ComPtr<T> copy = default;
|
||||
|
||||
Unsafe.AsRef(in ptr).Swap(ref copy);
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upcasts the current <see cref="ComPtr{T}"/> instance as a base type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the current <see cref="ComPtr{T}"/> instance.</typeparam>
|
||||
/// <typeparam name="U">The type to cast the input instance to.</typeparam>
|
||||
/// <param name="ptr">The input <see cref="ComPtr{T}"/> instance to upcast.</param>
|
||||
/// <returns>A <see cref="ComPtr{T}"/> instance of type <typeparamref name="U"/>.</returns>
|
||||
/// <remarks>
|
||||
/// This method is meant to be used in a chained expression and it does not increment the reference
|
||||
/// count for the input pointer. Do not expose the return value of this extension to consumers.
|
||||
/// </remarks>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe ComPtr<U> Upcast<T, U>(this in ComPtr<T> ptr)
|
||||
where T : unmanaged
|
||||
where U : unmanaged
|
||||
{
|
||||
ComPtr<U> copy = default;
|
||||
|
||||
*(T**)Unsafe.AsPointer(ref copy) = ptr.Get();
|
||||
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.19041-beta1" />
|
||||
<PackageReference Include="Vortice.Direct3D12" Version="1.8.6" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -27,28 +27,6 @@ namespace ComputeSharp.Graphics.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a <see cref="Win32Exception"/> if <paramref name="result"/> represents an error.
|
||||
/// </summary>
|
||||
/// <param name="result">The input <see cref="HRESULT"/> to check.</param>
|
||||
/// <param name="resource">An owned resource to dispose if it is not a null reference.</param>
|
||||
/// <exception cref="Win32Exception">Thrown if <paramref name="result"/> represents an error.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ThrowIfFailed(int result, void* resource)
|
||||
{
|
||||
if (result < 0)
|
||||
{
|
||||
static void Throw(int result, void* resource)
|
||||
{
|
||||
if (resource != null) ((IUnknown*)resource)->Release();
|
||||
|
||||
throw new Win32Exception(result);
|
||||
}
|
||||
|
||||
Throw(result, resource);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Throws a <see cref="Win32Exception"/> if <paramref name="result"/> represents an error.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.Exceptions;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using TerraFX.Interop;
|
||||
@@ -15,81 +17,79 @@ namespace ComputeSharp.Shaders.Translation
|
||||
/// <summary>
|
||||
/// The <see cref="IDxcCompiler"/> instance to use to create the bytecode for HLSL sources.
|
||||
/// </summary>
|
||||
private static readonly IDxcCompiler* DxcCompiler;
|
||||
private static readonly ComPtr<IDxcCompiler> DxcCompiler;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IDxcLibrary"/> instance to use to work with <see cref="DxcCompiler"/>.
|
||||
/// </summary>
|
||||
private static readonly IDxcLibrary* DxcLibrary;
|
||||
private static readonly ComPtr<IDxcLibrary> DxcLibrary;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IDxcIncludeHandler"/> instance used to compile shaders with <see cref="DxcCompiler"/>.
|
||||
/// </summary>
|
||||
private static readonly IDxcIncludeHandler* DxcIncludeHandler;
|
||||
private static readonly ComPtr<IDxcIncludeHandler> DxcIncludeHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes <see cref="DxcCompiler"/>, <see cref="DxcLibrary"/> and <see cref="DxcIncludeHandler"/>
|
||||
/// </summary>
|
||||
static ShaderCompiler2()
|
||||
{
|
||||
using ComPtr<IDxcCompiler> dxcCompiler = default;
|
||||
using ComPtr<IDxcLibrary> dxcLibrary = default;
|
||||
using ComPtr<IDxcIncludeHandler> dxcIncludeHandler = default;
|
||||
|
||||
Guid dxcCompilerCLGuid = FX.CLSID_DxcCompiler;
|
||||
Guid dxcCompilerGuid = FX.IID_IDxcCompiler;
|
||||
IDxcCompiler* dxcCompiler;
|
||||
Guid dxcLibraryCLGuid = FX.CLSID_DxcLibrary;
|
||||
Guid dxcLibraryGuid = FX.IID_IDxcLibrary;
|
||||
|
||||
int result = FX.DxcCreateInstance(&dxcCompilerCLGuid, &dxcCompilerGuid, (void**)&dxcCompiler);
|
||||
int result = FX.DxcCreateInstance(&dxcCompilerCLGuid, &dxcCompilerGuid, dxcCompiler.GetVoidAddressOf());
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
result = FX.DxcCreateInstance(&dxcLibraryCLGuid, &dxcLibraryGuid, dxcLibrary.GetVoidAddressOf());
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
result = dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf());
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
DxcCompiler = dxcCompiler;
|
||||
|
||||
Guid dxcLibraryCLGuid = FX.CLSID_DxcLibrary;
|
||||
Guid dxcLibraryGuid = FX.IID_IDxcLibrary;
|
||||
IDxcLibrary* dxcLibrary;
|
||||
|
||||
result = FX.DxcCreateInstance(&dxcLibraryCLGuid, &dxcLibraryGuid, (void**)&dxcLibrary);
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
DxcLibrary = dxcLibrary;
|
||||
|
||||
IDxcIncludeHandler* dxcIncludeHandler;
|
||||
|
||||
result = dxcLibrary->CreateIncludeHandler(&dxcIncludeHandler);
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
DxcIncludeHandler = dxcIncludeHandler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compiles a new HLSL shader from the input source code
|
||||
/// Compiles a new HLSL shader from the input source code.
|
||||
/// </summary>
|
||||
/// <param name="source">The HLSL source code to compile</param>
|
||||
/// <returns>The bytecode for the compiled shader</returns>
|
||||
/// <param name="source">The HLSL source code to compile.</param>
|
||||
/// <returns>The bytecode for the compiled shader.</returns>
|
||||
[Pure]
|
||||
public static IDxcBlob* CompileShader(string source)
|
||||
public static ComPtr<IDxcBlob> CompileShader(ReadOnlySpan<char> source)
|
||||
{
|
||||
using ComPtr<IDxcBlobEncoding> dxcBlobEncoding = default;
|
||||
using ComPtr<IDxcOperationResult> dxcOperationResult = default;
|
||||
|
||||
int result;
|
||||
IDxcBlobEncoding* dxcBlobEncoding;
|
||||
IDxcOperationResult* dxcOperationResult;
|
||||
|
||||
// Get the encoded blob from the source code
|
||||
fixed (char* p = source)
|
||||
{
|
||||
result = DxcLibrary->CreateBlobWithEncodingOnHeapCopy(p, (uint)source.Length * 2, 1200, &dxcBlobEncoding);
|
||||
result = DxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy(p, (uint)source.Length * 2, 1200, dxcBlobEncoding.GetAddressOf());
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
}
|
||||
|
||||
// Try to compile the new compute shader
|
||||
fixed (char* entryPoint = "CSMain")
|
||||
fixed (char* shaderProfile = "cs_6_1")
|
||||
fixed (char* optimization = "-O3")
|
||||
{
|
||||
char nullTerminator = '\0';
|
||||
|
||||
// Try to compile the new compute shader
|
||||
result = DxcCompiler->Compile(
|
||||
(IDxcBlob*)dxcBlobEncoding,
|
||||
result = DxcCompiler.Get()->Compile(
|
||||
dxcBlobEncoding.Upcast<IDxcBlobEncoding, IDxcBlob>().Get(),
|
||||
(ushort*)&nullTerminator,
|
||||
(ushort*)entryPoint,
|
||||
(ushort*)shaderProfile,
|
||||
@@ -97,50 +97,62 @@ namespace ComputeSharp.Shaders.Translation
|
||||
1,
|
||||
null,
|
||||
0,
|
||||
DxcIncludeHandler,
|
||||
&dxcOperationResult);
|
||||
DxcIncludeHandler.Get(),
|
||||
dxcOperationResult.GetAddressOf());
|
||||
|
||||
dxcBlobEncoding->Release();
|
||||
|
||||
// Handle failures during the operation that are not external to the compiler itself.
|
||||
// That is, if this call is successful, the compiler might still have found an error
|
||||
// in the source code, but the operation itself would have been run correctly. Here
|
||||
// we handle the scenario where that is not the case, so we dispose if needed and throw.
|
||||
ThrowHelper.ThrowIfFailed(result, dxcOperationResult);
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
}
|
||||
|
||||
int status;
|
||||
|
||||
// Check whether the actual compilation was executed successfully (exit code 0)
|
||||
result = dxcOperationResult->GetStatus(&status);
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result, dxcOperationResult);
|
||||
|
||||
if (status != 0)
|
||||
{
|
||||
ThrowHslsCompilationException(dxcOperationResult);
|
||||
}
|
||||
|
||||
IDxcBlob* dxcBlob;
|
||||
|
||||
result = dxcOperationResult->GetResult(&dxcBlob);
|
||||
result = dxcOperationResult.Get()->GetStatus(&status);
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
return dxcBlob;
|
||||
// The compilation was successful, so we can extract the shader bytecode
|
||||
if (status == 0)
|
||||
{
|
||||
using ComPtr<IDxcBlob> dxcBlob = default;
|
||||
|
||||
result = dxcOperationResult.Get()->GetResult(dxcBlob.GetAddressOf());
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
return dxcBlob.Move();
|
||||
}
|
||||
|
||||
return ThrowHslsCompilationException(dxcOperationResult);
|
||||
}
|
||||
|
||||
private static void ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult)
|
||||
/// <summary>
|
||||
/// Throws an exception when a shader compilation fails.
|
||||
/// </summary>
|
||||
/// <param name="dxcOperationResult">The input (faulting) operation.</param>
|
||||
/// <returns>This method always throws and never actually returs.</returns>
|
||||
[DoesNotReturn]
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static ComPtr<IDxcBlob> ThrowHslsCompilationException(ComPtr<IDxcOperationResult> dxcOperationResult)
|
||||
{
|
||||
IDxcBlobEncoding* dxcBlobEncodingError;
|
||||
using ComPtr<IDxcBlobEncoding> dxcBlobEncodingError = default;
|
||||
|
||||
int result = dxcOperationResult->GetErrorBuffer(&dxcBlobEncodingError);
|
||||
int result = dxcOperationResult.Get()->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf());
|
||||
|
||||
dxcOperationResult->Release();
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result, dxcBlobEncodingError);
|
||||
using ComPtr<IDxcBlobUtf16> dxcBlobUtf16Error = default;
|
||||
|
||||
throw new HlslCompilationException("Shader compilation error");
|
||||
result = DxcLibrary.Get()->GetBlobAsUtf16(
|
||||
dxcBlobEncodingError.Upcast<IDxcBlobEncoding, IDxcBlob>().Get(),
|
||||
dxcBlobUtf16Error.Upcast<IDxcBlobUtf16, IDxcBlobEncoding>().GetAddressOf());
|
||||
|
||||
ThrowHelper.ThrowIfFailed(result);
|
||||
|
||||
string message = new string(
|
||||
(char*)dxcBlobUtf16Error.Get()->GetStringPointer(),
|
||||
0,
|
||||
checked((int)dxcBlobUtf16Error.Get()->GetStringLength()));
|
||||
|
||||
throw new HlslCompilationException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする