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 sealed unsafe partial class ShaderCompiler { /// /// The thread local instance. /// This is necessary because the DXC library is strictly single-threaded. /// [ThreadStatic] private static ShaderCompiler? instance; /// /// The instance to use to create the bytecode for HLSL sources. /// private readonly ComPtr DxcCompiler; /// /// The instance to use to work with . /// private readonly ComPtr DxcLibrary; /// /// The instance used to compile shaders with . /// private readonly ComPtr DxcIncludeHandler; /// /// Creates a new instance. /// private ShaderCompiler() { lock(this) { 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(); } /// /// Destroys the current instance. /// ~ShaderCompiler() { DxcCompiler.Dispose(); DxcLibrary.Dispose(); DxcIncludeHandler.Dispose(); } /// /// Gets a instance to use. /// public static ShaderCompiler Instance => instance ??= new(); /// /// Compiles a new HLSL shader from the input source code. /// /// The HLSL source code to compile. /// The bytecode for the compiled shader. [Pure] public 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") fixed (char* rowMajor = "-Zpr") { char** arguments = stackalloc char*[2] { optimization, rowMajor }; DxcCompiler.Get()->Compile( (IDxcBlob*)dxcBlobEncoding.Get(), (ushort*)shaderName, (ushort*)entryPoint, (ushort*)shaderProfile, (ushort**)arguments, 2, 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); } } }