diff --git a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs index 02a1e56e..6eb47106 100644 --- a/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Shaders/Translation/ShaderCompiler.cs @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.Core.Extensions; using ComputeSharp.Exceptions; +using Microsoft.Toolkit.Diagnostics; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; @@ -37,12 +38,7 @@ namespace ComputeSharp.Shaders.Translation /// static ShaderCompiler() { - FX.ResolveLibrary += ResolveLibrary; - - fixed (char* p = @"runtimes\win-x64\native") - { - FX.SetDllDirectoryW((ushort*)p).Assert(); - } + InitializeDxcLibrariesLoading(); using ComPtr dxcCompiler = default; using ComPtr dxcLibrary = default; @@ -61,6 +57,21 @@ namespace ComputeSharp.Shaders.Translation DxcIncludeHandler = dxcIncludeHandler.Move(); } + /// + /// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries. + /// + private static void InitializeDxcLibrariesLoading() + { + FX.ResolveLibrary += ResolveLibrary; + + fixed (char* p = @"runtimes\win-x64\native") + { + int result = FX.SetDllDirectoryW((ushort*)p); + + if (result == 0) ThrowHelper.ThrowWin32Exception(); + } + } + /// /// A custom resolver to override the default library resolution behavior for the DXC and DXIL libraries. ///