VKRenderer.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Gdk;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Input.HLE;
  4. using Ryujinx.Ui.Helper;
  5. using SPB.Graphics.Vulkan;
  6. using SPB.Platform.Win32;
  7. using SPB.Platform.X11;
  8. using SPB.Platform.Metal;
  9. using SPB.Windowing;
  10. using System;
  11. using System.Runtime.InteropServices;
  12. namespace Ryujinx.Ui
  13. {
  14. public partial class VKRenderer : RendererWidgetBase
  15. {
  16. public NativeWindowBase NativeWindow { get; private set; }
  17. private UpdateBoundsCallbackDelegate _updateBoundsCallback;
  18. public VKRenderer(InputManager inputManager, GraphicsDebugLevel glLogLevel) : base(inputManager, glLogLevel) { }
  19. private NativeWindowBase RetrieveNativeWindow()
  20. {
  21. if (OperatingSystem.IsWindows())
  22. {
  23. IntPtr windowHandle = gdk_win32_window_get_handle(Window.Handle);
  24. return new SimpleWin32Window(new NativeHandle(windowHandle));
  25. }
  26. else if (OperatingSystem.IsLinux())
  27. {
  28. IntPtr displayHandle = gdk_x11_display_get_xdisplay(Display.Handle);
  29. IntPtr windowHandle = gdk_x11_window_get_xid(Window.Handle);
  30. return new SimpleX11Window(new NativeHandle(displayHandle), new NativeHandle(windowHandle));
  31. }
  32. else if (OperatingSystem.IsMacOS())
  33. {
  34. IntPtr metalLayer = MetalHelper.GetMetalLayer(Display, Window, out IntPtr nsView, out _updateBoundsCallback);
  35. return new SimpleMetalWindow(new NativeHandle(nsView), new NativeHandle(metalLayer));
  36. }
  37. throw new NotImplementedException();
  38. }
  39. [LibraryImport("libgdk-3-0.dll")]
  40. private static partial IntPtr gdk_win32_window_get_handle(IntPtr d);
  41. [LibraryImport("libgdk-3.so.0")]
  42. private static partial IntPtr gdk_x11_display_get_xdisplay(IntPtr gdkDisplay);
  43. [LibraryImport("libgdk-3.so.0")]
  44. private static partial IntPtr gdk_x11_window_get_xid(IntPtr gdkWindow);
  45. protected override bool OnConfigureEvent(EventConfigure evnt)
  46. {
  47. if (NativeWindow == null)
  48. {
  49. NativeWindow = RetrieveNativeWindow();
  50. WaitEvent.Set();
  51. }
  52. bool result = base.OnConfigureEvent(evnt);
  53. _updateBoundsCallback?.Invoke(Window);
  54. return result;
  55. }
  56. public unsafe IntPtr CreateWindowSurface(IntPtr instance)
  57. {
  58. return VulkanHelper.CreateWindowSurface(instance, NativeWindow);
  59. }
  60. public override void InitializeRenderer() { }
  61. public override void SwapBuffers() { }
  62. protected override string GetGpuBackendName()
  63. {
  64. return "Vulkan";
  65. }
  66. protected override void Dispose(bool disposing)
  67. {
  68. Device?.DisposeGpu();
  69. NpadManager.Dispose();
  70. }
  71. }
  72. }