using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using System.Threading; using ComputeSharp.SourceGeneration.Extensions; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.Graphics.Direct3D.Dxc; using Windows.Win32.Graphics.Direct3D12; using DirectX = Windows.Win32.PInvoke; namespace ComputeSharp.SourceGenerators.Dxc; /// /// A that uses the DXC APIs to compile compute shaders. /// internal sealed unsafe class DxcShaderCompiler { /// /// The thread local instance. /// This is necessary because the DXC library is strictly single-threaded. /// [ThreadStatic] private static DxcShaderCompiler? instance; /// /// The instance to use to create the bytecode for HLSL sources. /// private readonly ComPtr dxcCompiler3; /// /// The instance to use to work with . /// private readonly ComPtr dxcUtils; /// /// Creates a new instance. /// private DxcShaderCompiler() { using ComPtr dxcCompiler = default; using ComPtr dxcUtils = default; fixed (Guid* pDxcCompiler = &DirectX.CLSID_DxcCompiler) fixed (Guid* pDxcCompiler3 = &IDxcCompiler3.IID_Guid) { DirectX.DxcCreateInstance( pDxcCompiler, pDxcCompiler3, (void**)dxcCompiler.GetAddressOf()).Assert(); } fixed (Guid* pDxcLibrary = &DirectX.CLSID_DxcLibrary) fixed (Guid* pDxcUtils = &IDxcUtils.IID_Guid) { DirectX.DxcCreateInstance( pDxcLibrary, pDxcUtils, (void**)dxcUtils.GetAddressOf()).Assert(); } this.dxcCompiler3 = new ComPtr(dxcCompiler.Get()); this.dxcUtils = new ComPtr(dxcUtils.Get()); } /// /// Destroys the current instance. /// ~DxcShaderCompiler() { this.dxcCompiler3.Dispose(); this.dxcUtils.Dispose(); } /// /// Gets a instance to use. /// public static DxcShaderCompiler Instance => instance ??= new(); /// /// Compiles a new HLSL shader from the input source code. /// /// The HLSL source code to compile. /// The compile options to use. /// The used to cancel the operation, if needed. /// The bytecode for the compiled shader. public ComPtr Compile(ReadOnlySpan source, CompileOptions compileOptions, CancellationToken token) { using ComPtr dxcResult = default; using (ComPtr dxcCompilerArgs = default) { // Create the arguments to append others to fixed (char* pSourceName = "") fixed (char* pEntryPoint = "Execute") fixed (char* pTargetProfile = "cs_6_0") fixed (char* pRowMajor = "-Zpr") { // The row major argument is the only one always set automatically. // All the others are dynamically added based on the selected options. PCWSTR rowMajorArgument = pRowMajor; this.dxcUtils.Get()->BuildArguments( pSourceName: pSourceName, pEntryPoint: pEntryPoint, pTargetProfile: pTargetProfile, pArguments: &rowMajorArgument, argCount: 1, pDefines: null, defineCount: 0, ppArgs: dxcCompilerArgs.GetAddressOf()).Assert(); } // Add any other optional arguments fixed (char* pAllResourcesBound = "-all-resources-bound") fixed (char* pDisableValidation = "-Vd") fixed (char* pDisableOptimization = "-Od") fixed (char* pAvoidFlowControl = "-Gfa") fixed (char* pPreferFlowControl = "-Gfp") fixed (char* pEnableStrictness = "-Ges") fixed (char* pIeeeStrictness = "-Gis") fixed (char* pEnableBackwardsCompatibility = "-Gec") fixed (char* pOptimizationLevel0 = "-O0") fixed (char* pOptimizationLevel1 = "-O1") fixed (char* pOptimizationLevel2 = "-O2") fixed (char* pOptimizationLevel3 = "-O3") fixed (char* pWarningsAreErrors = "-WX") fixed (char* pResourcesMayAlias = "-res-may-alias") fixed (char* pStripReflectionData = "-Qstrip_reflect") { // Helper to add a new argument static void AddArgument(IDxcCompilerArgs* args, char* pArgument) { PCWSTR argument = pArgument; args->AddArguments(&argument, 1).Assert(); } if (compileOptions.HasFlag(CompileOptions.AllResourcesBound)) { AddArgument(dxcCompilerArgs.Get(), pAllResourcesBound); } if (compileOptions.HasFlag(CompileOptions.DisableValidation)) { AddArgument(dxcCompilerArgs.Get(), pDisableValidation); } if (compileOptions.HasFlag(CompileOptions.DisableOptimization)) { AddArgument(dxcCompilerArgs.Get(), pDisableOptimization); } if (compileOptions.HasFlag(CompileOptions.AvoidFlowControl)) { AddArgument(dxcCompilerArgs.Get(), pAvoidFlowControl); } if (compileOptions.HasFlag(CompileOptions.PreferFlowControl)) { AddArgument(dxcCompilerArgs.Get(), pPreferFlowControl); } if (compileOptions.HasFlag(CompileOptions.EnableStrictness)) { AddArgument(dxcCompilerArgs.Get(), pEnableStrictness); } if (compileOptions.HasFlag(CompileOptions.IeeeStrictness)) { AddArgument(dxcCompilerArgs.Get(), pIeeeStrictness); } if (compileOptions.HasFlag(CompileOptions.EnableBackwardsCompatibility)) { AddArgument(dxcCompilerArgs.Get(), pEnableBackwardsCompatibility); } if (compileOptions.HasFlag(CompileOptions.OptimizationLevel0)) { AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel0); } if (compileOptions.HasFlag(CompileOptions.OptimizationLevel1)) { AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel1); } if (compileOptions.HasFlag(CompileOptions.OptimizationLevel2)) { AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel2); } if (compileOptions.HasFlag(CompileOptions.OptimizationLevel3)) { AddArgument(dxcCompilerArgs.Get(), pOptimizationLevel3); } if (compileOptions.HasFlag(CompileOptions.WarningsAreErrors)) { AddArgument(dxcCompilerArgs.Get(), pWarningsAreErrors); } if (compileOptions.HasFlag(CompileOptions.ResourcesMayAlias)) { AddArgument(dxcCompilerArgs.Get(), pResourcesMayAlias); } if (compileOptions.HasFlag(CompileOptions.StripReflectionData)) { AddArgument(dxcCompilerArgs.Get(), pStripReflectionData); } } fixed (char* pSource = source) { // Prepare the buffer with the HLSL source DxcBuffer dxcBuffer; dxcBuffer.Ptr = pSource; dxcBuffer.Size = (uint)(source.Length * sizeof(char)); dxcBuffer.Encoding = (uint)DXC_CP.DXC_CP_UTF16; Guid dxcResultIid = IDxcResult.IID_Guid; // Try to actually compile the HLSL source HRESULT hresult = this.dxcCompiler3.Get()->Compile( pSource: &dxcBuffer, pArguments: dxcCompilerArgs.Get()->GetArguments(), argCount: dxcCompilerArgs.Get()->GetCount(), pIncludeHandler: null, riid: &dxcResultIid, ppResult: (void**)dxcResult.GetAddressOf()); // Only assert the HRESULT after returning the array from the pool. Since the method itself // can return a failure but doesn't actually throw, there's no need to throw the buffer away. hresult.Assert(); } } token.ThrowIfCancellationRequested(); HRESULT status; dxcResult.Get()->GetStatus(&status).Assert(); // Throw an exception with the input messages, if the compilation fails if (status != 0) { ThrowHslsCompilationException(dxcResult.Get()); } using ComPtr dxcBlobBytecode = default; // The compilation was successful, so we can extract the shader bytecode dxcResult.Get()->GetResult(dxcBlobBytecode.GetAddressOf()).Assert(); return dxcBlobBytecode.Move(); } /// public bool IsDoublePrecisionSupportRequired(IDxcBlob* dxcBlob) { using ComPtr d3D12ShaderReflection = default; Guid iidOfID3D12ShaderReflection = ID3D12ShaderReflection.IID_Guid; DxcBuffer dxcBuffer = default; dxcBuffer.Ptr = dxcBlob->GetBufferPointer(); dxcBuffer.Size = dxcBlob->GetBufferSize(); this.dxcUtils.Get()->CreateReflection( &dxcBuffer, &iidOfID3D12ShaderReflection, (void**)d3D12ShaderReflection.GetAddressOf()).Assert(); const ulong doublePrecisionFlags = DirectX.D3D_SHADER_REQUIRES_DOUBLES | DirectX.D3D_SHADER_REQUIRES_11_1_DOUBLE_EXTENSIONS; return (d3D12ShaderReflection.Get()->GetRequiresFlags() & doublePrecisionFlags) != 0; } /// public static string FixupExceptionMessage(string message) { // Add square brackets around error headers message = Regex.Replace(message, @"^(error|warning):", static m => $"[{m.Groups[1].Value}]:", RegexOptions.Multiline); // Remove lines with notes message = Regex.Replace(message, @"^note:.+", string.Empty, RegexOptions.Multiline); // Remove syntax error indicators message = Regex.Replace(message, @"^ +\^", string.Empty, RegexOptions.Multiline); return message.NormalizeToSingleLine(); } /// /// Throws an exception when a shader compilation fails. /// /// The input (faulting) operation. /// This method always throws and never actually returs. [DoesNotReturn] [MethodImpl(MethodImplOptions.NoInlining)] private static void ThrowHslsCompilationException(IDxcResult* dxcResult) { using ComPtr dxcBlobEncodingError = default; dxcResult->GetErrorBuffer(dxcBlobEncodingError.GetAddressOf()).Assert(); string message = new((sbyte*)dxcBlobEncodingError.Get()->GetBufferPointer()); // The error message will be in a format like this: // "hlsl.hlsl:11:20: error: redefinition of 'float1' as different kind of symbol // static const float float1 = asfloat(0xFFC00000); // ^ // note: previous definition is here" // These regex-s try to match the unnecessary headers and remove them, if present. // This doesn't need to be bulletproof, and these regex-s should match all cases anyway. message = Regex.Replace(message.Trim(), @"^hlsl\.hlsl:\d+:\d+: (\w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline).Trim(); // Add a trailing '.' if not present if (message is [.., not '.']) { message += '.'; } throw new DxcCompilationException(message); } }