VulkanWindow.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Input.HLE;
  4. using Ryujinx.SDL2.Common;
  5. using System;
  6. using System.Runtime.InteropServices;
  7. using static SDL2.SDL;
  8. namespace Ryujinx.Headless.SDL2.Vulkan
  9. {
  10. class VulkanWindow : WindowBase
  11. {
  12. private GraphicsDebugLevel _glLogLevel;
  13. public VulkanWindow(
  14. InputManager inputManager,
  15. GraphicsDebugLevel glLogLevel,
  16. AspectRatio aspectRatio,
  17. bool enableMouse,
  18. HideCursor hideCursor)
  19. : base(inputManager, glLogLevel, aspectRatio, enableMouse, hideCursor)
  20. {
  21. _glLogLevel = glLogLevel;
  22. }
  23. public override SDL_WindowFlags GetWindowFlags() => SDL_WindowFlags.SDL_WINDOW_VULKAN;
  24. protected override void InitializeWindowRenderer() { }
  25. protected override void InitializeRenderer()
  26. {
  27. Renderer?.Window.SetSize(DefaultWidth, DefaultHeight);
  28. MouseDriver.SetClientSize(DefaultWidth, DefaultHeight);
  29. }
  30. private void BasicInvoke(Action action)
  31. {
  32. action();
  33. }
  34. public unsafe IntPtr CreateWindowSurface(IntPtr instance)
  35. {
  36. ulong surfaceHandle = 0;
  37. Action createSurface = () =>
  38. {
  39. if (SDL_Vulkan_CreateSurface(WindowHandle, instance, out surfaceHandle) == SDL_bool.SDL_FALSE)
  40. {
  41. string errorMessage = $"SDL_Vulkan_CreateSurface failed with error \"{SDL_GetError()}\"";
  42. Logger.Error?.Print(LogClass.Application, errorMessage);
  43. throw new Exception(errorMessage);
  44. }
  45. };
  46. if (SDL2Driver.MainThreadDispatcher != null)
  47. {
  48. SDL2Driver.MainThreadDispatcher(createSurface);
  49. }
  50. else
  51. {
  52. createSurface();
  53. }
  54. return (IntPtr)surfaceHandle;
  55. }
  56. public unsafe string[] GetRequiredInstanceExtensions()
  57. {
  58. if (SDL_Vulkan_GetInstanceExtensions(WindowHandle, out uint extensionsCount, IntPtr.Zero) == SDL_bool.SDL_TRUE)
  59. {
  60. IntPtr[] rawExtensions = new IntPtr[(int)extensionsCount];
  61. string[] extensions = new string[(int)extensionsCount];
  62. fixed (IntPtr* rawExtensionsPtr = rawExtensions)
  63. {
  64. if (SDL_Vulkan_GetInstanceExtensions(WindowHandle, out extensionsCount, (IntPtr)rawExtensionsPtr) == SDL_bool.SDL_TRUE)
  65. {
  66. for (int i = 0; i < extensions.Length; i++)
  67. {
  68. extensions[i] = Marshal.PtrToStringUTF8(rawExtensions[i]);
  69. }
  70. return extensions;
  71. }
  72. }
  73. }
  74. string errorMessage = $"SDL_Vulkan_GetInstanceExtensions failed with error \"{SDL_GetError()}\"";
  75. Logger.Error?.Print(LogClass.Application, errorMessage);
  76. throw new Exception(errorMessage);
  77. }
  78. protected override void FinalizeWindowRenderer()
  79. {
  80. Device.DisposeGpu();
  81. }
  82. protected override void SwapBuffers() { }
  83. }
  84. }