From 3f1b27e0920c07cec10c05364850e284f9b108e3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 16 Nov 2020 14:03:00 +0100 Subject: [PATCH] Added initial version of TerraFX ShaderCompiler type --- .../Helpers/ThrowHelper.cs | 22 +++ .../Translation/ShaderCompiler2.cs | 146 ++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs diff --git a/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs b/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs index 43444b20..18ed2279 100644 --- a/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs +++ b/src/ComputeSharp.Graphics/Helpers/ThrowHelper.cs @@ -27,6 +27,28 @@ namespace ComputeSharp.Graphics.Helpers } } + /// + /// Throws a if represents an error. + /// + /// The input to check. + /// An owned resource to dispose if it is not a null reference. + /// Thrown if represents an error. + [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); + } + } + /// /// Throws a if represents an error. /// diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs new file mode 100644 index 00000000..25ab56f6 --- /dev/null +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler2.cs @@ -0,0 +1,146 @@ +using System; +using System.Diagnostics.Contracts; +using ComputeSharp.Exceptions; +using ComputeSharp.Graphics.Helpers; +using TerraFX.Interop; +using FX = TerraFX.Interop.Windows; + +namespace ComputeSharp.Shaders.Translation +{ + /// + /// A that uses the DXC APIs to compile compute shaders. + /// + internal static unsafe class ShaderCompiler2 + { + /// + /// The instance to use to create the bytecode for HLSL sources. + /// + private static readonly IDxcCompiler* DxcCompiler; + + /// + /// The instance to use to work with . + /// + private static readonly IDxcLibrary* DxcLibrary; + + /// + /// The instance used to compile shaders with . + /// + private static readonly IDxcIncludeHandler* DxcIncludeHandler; + + /// + /// Initializes , and + /// + static ShaderCompiler2() + { + Guid dxcCompilerCLGuid = FX.CLSID_DxcCompiler; + Guid dxcCompilerGuid = FX.IID_IDxcCompiler; + IDxcCompiler* dxcCompiler; + + int result = FX.DxcCreateInstance(&dxcCompilerCLGuid, &dxcCompilerGuid, (void**)&dxcCompiler); + + 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; + } + + /// + /// Compiles a new HLSL shader from the input source code + /// + /// The HLSL source code to compile + /// The bytecode for the compiled shader + [Pure] + public static IDxcBlob* CompileShader(string source) + { + 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); + + ThrowHelper.ThrowIfFailed(result); + } + + 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, + (ushort*)&nullTerminator, + (ushort*)entryPoint, + (ushort*)shaderProfile, + (ushort**)&optimization, + 1, + null, + 0, + DxcIncludeHandler, + &dxcOperationResult); + + 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); + } + + 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); + + ThrowHelper.ThrowIfFailed(result); + + return dxcBlob; + } + + private static void ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult) + { + IDxcBlobEncoding* dxcBlobEncodingError; + + int result = dxcOperationResult->GetErrorBuffer(&dxcBlobEncodingError); + + dxcOperationResult->Release(); + + ThrowHelper.ThrowIfFailed(result, dxcBlobEncodingError); + + throw new HlslCompilationException("Shader compilation error"); + } + } +}