diff --git a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.LibraryLoading.cs b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.LibraryLoading.cs new file mode 100644 index 00000000..679ebe0d --- /dev/null +++ b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.LibraryLoading.cs @@ -0,0 +1,77 @@ +using System; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; + +namespace ComputeSharp.Interop; + +/// +partial class ReflectionServices +{ + /// + /// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries. + /// + static ReflectionServices() + { + // Register a custom library resolver for the two DXC libraries. We need to either manually load the two + // libraries from the NuGet directory, if an RID is not in use, or we need to ensure that dxil.dll is + // loaded correctly in case the program was executed with the host being in another directory. + // This happens when doing eg. "dotnet bin\Debug\net8.0\MyApp.dll", which would crash at runtime. + NativeLibrary.SetDllImportResolver(typeof(ReflectionServices).Assembly, OnDllImport); + } + + /// + /// The custom for this assembly. + /// + /// + private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) + { + if (libraryName is not "dxcompiler") + { + return IntPtr.Zero; + } + + string rid = RuntimeInformation.ProcessArchitecture switch + { + Architecture.X64 => "win-x64", + Architecture.Arm64 => "win-arm64", + _ => default(NotSupportedException).Throw() + }; + + // Test whether the native libraries are present in the same folder of the executable + // (which is the case when the program was built with a runtime identifier), or whether + // they are in the "runtimes\win-x64\native" folder in the executable directory. + string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native"); + bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath); + + if (isNuGetRuntimeLibrariesDirectoryPresent) + { + string dxcompilerPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxcompiler.dll"); + string dxilPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxil.dll"); + + // Load DXIL first so that DXC doesn't fail to load it, and then DXIL, both from the NuGet path + if (NativeLibrary.TryLoad(dxilPath, out _) && + NativeLibrary.TryLoad(dxcompilerPath, out IntPtr handle)) + { + return handle; + } + } + else + { + // Even when the two libraries are correctly copied next to the executable in use, we load them + // manually to ensure the operation is successful. This is to avoid failures in cases such as when + // doing "dotnet bin\MyApp.dll", ie. when the host is in another path than the executable in use. + // This is probably because DXIL is a native dependency for DXC, but the way Windows loads these + // libraries doesn't take into account the .NET concepts of "app directory": neither the current "bin" + // directory nor the "process directory", which is "C:\Program Files\dotnet", actually contain the + // native library we need, hence the runtime crash. Manually loading the library this way solves this. + if (NativeLibrary.TryLoad("dxil", assembly, searchPath, out _) && + NativeLibrary.TryLoad("dxcompiler", assembly, searchPath, out IntPtr handle)) + { + return handle; + } + } + + return IntPtr.Zero; + } +} \ No newline at end of file diff --git a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs index a6f0be37..44d51921 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Interop/ReflectionServices.cs @@ -13,7 +13,7 @@ namespace ComputeSharp.Interop; /// /// Provides methods to extract reflection info on compute shaders generated using this library. /// -public static class ReflectionServices +public static partial class ReflectionServices { /// /// Gets the shader info associated with a given compute shader. diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.LibraryLoading.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.LibraryLoading.cs deleted file mode 100644 index f92c8808..00000000 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.LibraryLoading.cs +++ /dev/null @@ -1,75 +0,0 @@ -#if NET6_0_OR_GREATER - -using System; -using System.IO; -using System.Runtime.InteropServices; -using TerraFX.Interop.Windows; - -namespace ComputeSharp.Shaders.Translation; - -/// -unsafe partial class ShaderCompiler -{ - /// - /// Initializes the DLL resolvers for the dxcompiler.dll and dxil.dll libraries. - /// - static ShaderCompiler() - { - // Register a custom library resolver for the two DXC libraries. We need to either manually load the two - // libraries from the NuGet directory, if an RID is not in use, or we need to ensure that dxil.dll is - // loaded correctly in case the program was executed with the host being in another directory. - // This happens when doing eg. "dotnet bin\Debug\net5.0\MyApp.dll", which would crash at runtime. - Windows.ResolveLibrary += static (libraryName, assembly, searchPath) => - { - if (libraryName is not "dxcompiler") - { - return IntPtr.Zero; - } - - string rid = RuntimeInformation.ProcessArchitecture switch - { - Architecture.X64 => "win-x64", - Architecture.Arm64 => "win-arm64", - _ => default(NotSupportedException).Throw() - }; - - // Test whether the native libraries are present in the same folder of the executable - // (which is the case when the program was built with a runtime identifier), or whether - // they are in the "runtimes\win-x64\native" folder in the executable directory. - string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native"); - bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath); - - if (isNuGetRuntimeLibrariesDirectoryPresent) - { - string dxcompilerPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxcompiler.dll"); - string dxilPath = Path.Combine(AppContext.BaseDirectory, $@"runtimes\{rid}\native\dxil.dll"); - - // Load DXIL first so that DXC doesn't fail to load it, and then DXIL, both from the NuGet path - if (NativeLibrary.TryLoad(dxilPath, out _) && - NativeLibrary.TryLoad(dxcompilerPath, out IntPtr handle)) - { - return handle; - } - } - else - { - // Even when the two libraries are correctly copied next to the executable in use, we load them - // manually to ensure the operation is successful. This is to avoid failures in cases such as when - // doing "dotnet bin\MyApp.dll", ie. when the host is in another path than the executable in use. - // This is probably because DXIL is a native dependency for DXC, but the way Windows loads these - // libraries doesn't take into account the .NET concepts of "app directory": neither the current "bin" - // directory nor the "process directory", which is "C:\Program Files\dotnet", actually contain the - // native library we need, hence the runtime crash. Manually loading the library this way solves this. - if (NativeLibrary.TryLoad("dxil", assembly, searchPath, out _) && - NativeLibrary.TryLoad("dxcompiler", assembly, searchPath, out IntPtr handle)) - { - return handle; - } - } - - return IntPtr.Zero; - }; - } -} - -#endif \ No newline at end of file diff --git a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs index ff33ffde..7376f484 100644 --- a/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs +++ b/src/ComputeSharp.Dynamic/Shaders/Translation/ShaderCompiler.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.Shaders.Translation; /// /// A that uses the DXC APIs to compile compute shaders. /// -internal sealed unsafe partial class ShaderCompiler +internal sealed unsafe class ShaderCompiler { /// /// The thread local instance. @@ -158,8 +158,7 @@ internal sealed unsafe partial class ShaderCompiler message = Regex.Replace(message, @"^hlsl\.hlsl:\d+:\d+: (\w+:)", static m => m.Groups[1].Value, RegexOptions.Multiline); // Add a trailing '.' if not present - if (message is { Length: > 0 } && - message[message.Length - 1] != '.') + if (message is [.., not '.']) { message += '.'; }