brutal/Brutal.Vulkan/ImportResolver.cs
Capsup 411f18c290 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 <capsup@live.dk>
2026-10-20 09:55:39 +02:00

59 lines
2.2 KiB
C#

namespace Brutal.VulkanApi;
public partial class Vulkan
{
// The dll name we expect LibraryImport functions calling Vulkan to use
private const string PInvokeDllName = "vulkan";
// Cached reference to the loaded Vulkan dll
private static readonly nint Dll;
/// <summary>
/// The function pointer to vkGetInstanceProcAddr, loaded from the Vulkan library.
/// </summary>
public static VkGetInstanceProcAddr GetInstanceProcAddr { get; }
static Vulkan()
{
// Load the correct Vulkan library for the platform
Dll = LoadVulkanLibrary();
// Assign the native library to the assembly resolver
NativeLibrary.SetDllImportResolver(
typeof(Vulkan).Assembly,
(libraryName, _, _) => libraryName == PInvokeDllName ? Dll : nint.Zero
);
// Load the vkGetInstanceProcAddr function
unsafe
{
GetInstanceProcAddr = new VkGetInstanceProcAddr(
(delegate* unmanaged[Cdecl]<VkInstance, byte*, PfnVkVoidFunction>)
NativeLibrary.GetExport(Dll, "vkGetInstanceProcAddr")
);
if (GetInstanceProcAddr.Invoke == null)
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)}");
}
}