From 411f18c290e151862209e261334f60462e7e6a9e Mon Sep 17 00:00:00 2001 From: Capsup Date: Tue, 20 Oct 2026 09:39:45 +0200 Subject: [PATCH] Find the system Vulkan loader on macOS and Linux macOS loaded VulkanEx, which links libvulkan.1.dylib through an rpath of /usr/local/lib and so only works with the LunarG SDK installed there. Try the loader directly first (the default search path, Homebrew and /usr/local), then VulkanEx as before. Linux loaded "vulkan", i.e. libvulkan.so, which only ships in -dev packages. Try libvulkan.so.1 first. Signed-off-by: Capsup --- Brutal.Vulkan/ImportResolver.cs | 35 +++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Brutal.Vulkan/ImportResolver.cs b/Brutal.Vulkan/ImportResolver.cs index 098163b..bfd5442 100644 --- a/Brutal.Vulkan/ImportResolver.cs +++ b/Brutal.Vulkan/ImportResolver.cs @@ -16,19 +16,7 @@ public partial class Vulkan static Vulkan() { // Load the correct Vulkan library for the platform - - if (OperatingSystem.IsWindows()) - Dll = NativeLibrary.Load("vulkan-1", typeof(Vulkan).Assembly, DllImportSearchPath.System32); - - // TODO: This is a hack which only works if SDK is installed, we need to package the Vulkan binary on macOS - if (OperatingSystem.IsMacOS()) - Dll = NativeLibrary.Load("VulkanEx", typeof(Vulkan).Assembly, DllImportSearchPath.LegacyBehavior); - - if (OperatingSystem.IsLinux()) - Dll = NativeLibrary.Load("vulkan", typeof(Vulkan).Assembly, DllImportSearchPath.LegacyBehavior); - - if (Dll == nint.Zero) - throw new NullReferenceException("Failed to load Vulkan library"); + Dll = LoadVulkanLibrary(); // Assign the native library to the assembly resolver NativeLibrary.SetDllImportResolver( @@ -47,4 +35,25 @@ public partial class Vulkan throw new NullReferenceException("Failed to load vkGetInstanceProcAddr from Vulkan library"); } } + + private static nint LoadVulkanLibrary() + { + if (OperatingSystem.IsWindows()) + return NativeLibrary.Load("vulkan-1", typeof(Vulkan).Assembly, DllImportSearchPath.System32); + + // Prefer the system Vulkan loader, falling back to the old names + string[] candidates = OperatingSystem.IsMacOS() + ? ["libvulkan.1.dylib", "/opt/homebrew/lib/libvulkan.1.dylib", "/usr/local/lib/libvulkan.1.dylib", "VulkanEx"] + : OperatingSystem.IsLinux() + ? ["libvulkan.so.1", "vulkan"] + : []; + + foreach (var candidate in candidates) + { + if (NativeLibrary.TryLoad(candidate, typeof(Vulkan).Assembly, DllImportSearchPath.LegacyBehavior, out var dll)) + return dll; + } + + throw new DllNotFoundException($"Failed to load Vulkan library, tried: {string.Join(", ", candidates)}"); + } }