using System; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; 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 partial class ShaderCompiler { /// /// The instance to use to create the bytecode for HLSL sources. /// private static readonly ComPtr DxcCompiler; /// /// The instance to use to work with . /// private static readonly ComPtr DxcLibrary; /// /// The instance used to compile shaders with . /// private static readonly ComPtr DxcIncludeHandler; /// /// Initializes , and . /// static ShaderCompiler() { InitializeDxcLibrariesLoading(); using ComPtr dxcCompiler = default; using ComPtr dxcLibrary = default; using ComPtr dxcIncludeHandler = default; Guid dxcCompilerCLGuid = FX.CLSID_DxcCompiler; Guid dxcLibraryCLGuid = FX.CLSID_DxcLibrary; FX.DxcCreateInstance(&dxcCompilerCLGuid, FX.__uuidof(), dxcCompiler.GetVoidAddressOf()).Assert(); FX.DxcCreateInstance(&dxcLibraryCLGuid, FX.__uuidof(), dxcLibrary.GetVoidAddressOf()).Assert(); dxcLibrary.Get()->CreateIncludeHandler(dxcIncludeHandler.GetAddressOf()).Assert(); DxcCompiler = dxcCompiler.Move(); DxcLibrary = dxcLibrary.Move(); DxcIncludeHandler = dxcIncludeHandler.Move(); } /// /// 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 ComPtr CompileShader(ReadOnlySpan source) { using ComPtr dxcBlobEncoding = default; using ComPtr dxcOperationResult = default; using ComPtr dxcBlobBytecode = default; // Get the encoded blob from the source code fixed (char* p = source) { DxcLibrary.Get()->CreateBlobWithEncodingOnHeapCopy( p, (uint)source.Length * 2, 1200, dxcBlobEncoding.GetAddressOf()).Assert(); } // 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") { DxcCompiler.Get()->Compile( dxcBlobEncoding.Upcast().Get(), (ushort*)shaderName, (ushort*)entryPoint, (ushort*)shaderProfile, (ushort**)&optimization, 1, null, 0, DxcIncludeHandler.Get(), dxcOperationResult.GetAddressOf()).Assert(); } int status; dxcOperationResult.Get()->GetStatus(&status).Assert(); // The compilation was successful, so we can extract the shader bytecode if (status == 0) { dxcOperationResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); return dxcBlobBytecode.Move(); } return ThrowHslsCompilationException(dxcOperationResult); } /// /// Throws an exception when a shader compilation fails. /// /// The input (faulting) operation. /// This method always throws and never actually returs. [MethodImpl(MethodImplOptions.NoInlining)] private static ComPtr ThrowHslsCompilationException(IDxcOperationResult* dxcOperationResult) { using ComPtr dxcBlobEncodingError = default; dxcOperationResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert(); string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer()); throw new HlslCompilationException(message); } } }