mirror of
https://git.743378673.xyz/MeloNX/MeloNX.git
synced 2025-07-01 21:06:25 +02:00
test
This commit is contained in:
parent
ce9c8526b7
commit
353c4958dd
14 changed files with 190 additions and 12 deletions
96
src/LibRyujinx/VulkanLoader.cs
Normal file
96
src/LibRyujinx/VulkanLoader.cs
Normal file
|
@ -0,0 +1,96 @@
|
|||
using Ryujinx.Common.Logging;
|
||||
using Silk.NET.Core.Contexts;
|
||||
using Silk.NET.Vulkan;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibRyujinx
|
||||
{
|
||||
public class VulkanLoader : IDisposable
|
||||
{
|
||||
private delegate IntPtr GetInstanceProcAddress(IntPtr instance, IntPtr name);
|
||||
private delegate IntPtr GetDeviceProcAddress(IntPtr device, IntPtr name);
|
||||
|
||||
private IntPtr _loadedLibrary = IntPtr.Zero;
|
||||
private GetInstanceProcAddress _getInstanceProcAddr;
|
||||
private GetDeviceProcAddress _getDeviceProcAddr;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_loadedLibrary != IntPtr.Zero)
|
||||
{
|
||||
NativeLibrary.Free(_loadedLibrary);
|
||||
_loadedLibrary = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
public VulkanLoader(IntPtr driver)
|
||||
{
|
||||
_loadedLibrary = driver;
|
||||
|
||||
if(_loadedLibrary != IntPtr.Zero)
|
||||
{
|
||||
var instanceGetProc = NativeLibrary.GetExport(_loadedLibrary, "vkGetInstanceProcAddr");
|
||||
var deviceProc = NativeLibrary.GetExport(_loadedLibrary, "vkGetDeviceProcAddr");
|
||||
|
||||
_getInstanceProcAddr = Marshal.GetDelegateForFunctionPointer<GetInstanceProcAddress>(instanceGetProc);
|
||||
_getDeviceProcAddr = Marshal.GetDelegateForFunctionPointer<GetDeviceProcAddress>(deviceProc);
|
||||
}
|
||||
}
|
||||
|
||||
public Vk GetApi()
|
||||
{
|
||||
|
||||
if (_loadedLibrary == IntPtr.Zero)
|
||||
{
|
||||
return Vk.GetApi();
|
||||
}
|
||||
var ctx = new MultiNativeContext(new INativeContext[1]);
|
||||
var ret = new Vk(ctx);
|
||||
ctx.Contexts[0] = new LamdaNativeContext
|
||||
(
|
||||
x =>
|
||||
{
|
||||
var xPtr = Marshal.StringToHGlobalAnsi(x);
|
||||
try
|
||||
{
|
||||
nint ptr = default;
|
||||
ptr = _getInstanceProcAddr(ret.CurrentInstance.GetValueOrDefault().Handle, xPtr);
|
||||
|
||||
if (ptr == default)
|
||||
{
|
||||
ptr = _getInstanceProcAddr(IntPtr.Zero, xPtr);
|
||||
|
||||
if (ptr == default)
|
||||
{
|
||||
var currentDevice = ret.CurrentDevice.GetValueOrDefault().Handle;
|
||||
if (currentDevice != IntPtr.Zero)
|
||||
{
|
||||
ptr = _getDeviceProcAddr(currentDevice, xPtr);
|
||||
}
|
||||
|
||||
if (ptr == default)
|
||||
{
|
||||
Logger.Warning?.Print(LogClass.Gpu, $"Failed to get function pointer: {x}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(xPtr);
|
||||
}
|
||||
}
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue