Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace custom library load with net8 NativeLibrary #12

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions VulkanGen/Evergine.Bindings.Vulkan/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Runtime.InteropServices;
using static Evergine.Bindings.Vulkan.OperatingSystemHelper;

namespace Evergine.Bindings.Vulkan
{
Expand All @@ -13,7 +12,7 @@ public static unsafe partial class VulkanNative
static VulkanNative()
{
NativeLib = LoadNativeLibrary();
LoadFuncionPointers();
LoadFunctionPointers();
}

private static NativeLibrary LoadNativeLibrary()
Expand All @@ -23,19 +22,19 @@ private static NativeLibrary LoadNativeLibrary()

private static string GetVulkanName()
{
if (IsOSPlatform(PlatformType.Windows))
if (OperatingSystem.IsWindows())
{
return "vulkan-1.dll";
}
else if (IsOSPlatform(PlatformType.Android))
else if (OperatingSystem.IsAndroid())
{
return "libvulkan.so";
}
else if (IsOSPlatform(PlatformType.Linux))
else if (OperatingSystem.IsLinux())
{
return "libvulkan.so.1";
}
else if (IsOSPlatform(PlatformType.MacOS))
else if (OperatingSystem.IsMacOS())
{
return "libvulkan.dylib";
}
Expand Down
2 changes: 1 addition & 1 deletion VulkanGen/Evergine.Bindings.Vulkan/Generated/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3509,7 +3509,7 @@ public static void vkCmdBindDescriptorSets2KHR(VkCommandBuffer commandBuffer, Vk
public static void vkCmdPushConstants2KHR(VkCommandBuffer commandBuffer, VkPushConstantsInfoKHR* pPushConstantsInfo)
=> vkCmdPushConstants2KHR_ptr(commandBuffer, pPushConstantsInfo);

public static void LoadFuncionPointers(VkInstance instance = default)
public static void LoadFunctionPointers(VkInstance instance = default)
{
if (instance != default)
{
Expand Down
19 changes: 0 additions & 19 deletions VulkanGen/Evergine.Bindings.Vulkan/Kernel32.cs

This file was deleted.

24 changes: 0 additions & 24 deletions VulkanGen/Evergine.Bindings.Vulkan/Libdl.cs

This file was deleted.

104 changes: 24 additions & 80 deletions VulkanGen/Evergine.Bindings.Vulkan/NativeLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using static Evergine.Bindings.Vulkan.OperatingSystemHelper;
using SysNativeLibrary = System.Runtime.InteropServices.NativeLibrary;

namespace Evergine.Bindings.Vulkan
{
public abstract class NativeLibrary : IDisposable
public class NativeLibrary : IDisposable
{
private readonly string libraryName;
private readonly IntPtr libraryHandle;
internal VkInstance instance;

public IntPtr NativeHandle => libraryHandle;

public NativeLibrary(string libraryName)
protected NativeLibrary(string libraryName)
{
this.libraryName = libraryName;
libraryHandle = LoadLibrary(this.libraryName);
Expand All @@ -24,19 +24,31 @@ public NativeLibrary(string libraryName)
}
}

protected abstract IntPtr LoadLibrary(string libraryName);
protected abstract void FreeLibrary(IntPtr libraryHandle);
public abstract IntPtr LoadFunction(string functionName);
protected IntPtr LoadLibrary(string libraryName)
{
if (SysNativeLibrary.TryLoad(libraryName, typeof(NativeLibrary).Assembly, null, out var lib))
{
return lib;
}

Debug.WriteLine($" ===> Error loading native library {libraryName}");
return IntPtr.Zero;
}

protected void FreeLibrary(IntPtr libraryHandle)
{
if (libraryHandle != IntPtr.Zero)
{
SysNativeLibrary.Free(libraryHandle);
}
}

public unsafe void LoadFunction<T>(string name, out T field)
{
IntPtr funcPtr = LoadFunction(name);
SysNativeLibrary.TryGetExport(libraryHandle, name, out IntPtr funcPtr);
if (funcPtr == IntPtr.Zero)
{
if (instance != IntPtr.Zero)
{
funcPtr = VulkanNative.vkGetInstanceProcAddr(instance, (byte*)Marshal.StringToHGlobalAnsi(name));
}
funcPtr = VulkanNative.vkGetInstanceProcAddr(instance, (byte*)Marshal.StringToHGlobalAnsi(name));
}

if (funcPtr != IntPtr.Zero)
Expand All @@ -57,75 +69,7 @@ public void Dispose()

public static NativeLibrary Load(string libraryName)
{
if (IsOSPlatform(PlatformType.Windows))
{
return new WindowsNativeLibrary(libraryName);
}
else if (IsOSPlatform(PlatformType.Android) || IsOSPlatform(PlatformType.Linux) || IsOSPlatform(PlatformType.MacOS))
{
return new UnixNativeLibrary(libraryName);
}
else
{
throw new PlatformNotSupportedException("Cannot load native libraries on this platform: " + RuntimeInformation.OSDescription);
}
}

private class WindowsNativeLibrary : NativeLibrary
{
public WindowsNativeLibrary(string libraryName) : base(libraryName)
{
}

protected override IntPtr LoadLibrary(string libraryName)
{
return Kernel32.LoadLibrary(libraryName);
}

protected override void FreeLibrary(IntPtr libraryHandle)
{
Kernel32.FreeLibrary(libraryHandle);
}

public override IntPtr LoadFunction(string functionName)
{
Debug.WriteLine("Loading " + functionName);
return Kernel32.GetProcAddress(NativeHandle, functionName);
}
}

private class UnixNativeLibrary : NativeLibrary
{
public UnixNativeLibrary(string libraryName) : base(libraryName)
{
}

protected override IntPtr LoadLibrary(string libraryName)
{
Libdl.dlerror();
IntPtr handle = Libdl.dlopen(libraryName, Libdl.RTLD_NOW);
if (handle == IntPtr.Zero && !Path.IsPathRooted(libraryName))
{
string baseDir = AppContext.BaseDirectory;
if (!string.IsNullOrWhiteSpace(baseDir))
{
string localPath = Path.Combine(baseDir, libraryName);
handle = Libdl.dlopen(localPath, Libdl.RTLD_NOW);
}
}

return handle;
}

protected override void FreeLibrary(IntPtr libraryHandle)
{
Libdl.dlclose(libraryHandle);
}

public override IntPtr LoadFunction(string functionName)
{
return Libdl.dlsym(NativeHandle, functionName);
}
return new NativeLibrary(libraryName);
}
}
}
133 changes: 0 additions & 133 deletions VulkanGen/Evergine.Bindings.Vulkan/OperatingSystemHelper.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void CreateInstance()
fixed (VkInstance* instancePtr = &instance)
{
Helpers.CheckErrors(VulkanNative.vkCreateInstance(&createInfo, null, instancePtr));
VulkanNative.LoadFuncionPointers(instance);
VulkanNative.LoadFunctionPointers(instance);
}
}

Expand Down
2 changes: 1 addition & 1 deletion VulkanGen/VulkanGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ static void Main(string[] args)
file.WriteLine($"\t\t\t=> {command.Prototype.Name}_ptr({command.GetParametersSignature(vulkanSpec, useTypes: false)});\n");
}

file.WriteLine($"\t\tpublic static void LoadFuncionPointers(VkInstance instance = default)");
file.WriteLine($"\t\tpublic static void LoadFunctionPointers(VkInstance instance = default)");
file.WriteLine("\t\t{");
file.WriteLine("\t\t\tif (instance != default)");
file.WriteLine("\t\t\t{");
Expand Down