VulkanWindow.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(InputManager inputManager, GraphicsDebugLevel glLogLevel, AspectRatio aspectRatio, bool enableMouse) : base(inputManager, glLogLevel, aspectRatio, enableMouse)
  14. {
  15. _glLogLevel = glLogLevel;
  16. }
  17. public override SDL_WindowFlags GetWindowFlags() => SDL_WindowFlags.SDL_WINDOW_VULKAN;
  18. protected override void InitializeWindowRenderer() { }
  19. protected override void InitializeRenderer()
  20. {
  21. Renderer?.Window.SetSize(DefaultWidth, DefaultHeight);
  22. MouseDriver.SetClientSize(DefaultWidth, DefaultHeight);
  23. }
  24. private void BasicInvoke(Action action)
  25. {
  26. action();
  27. }
  28. public unsafe IntPtr CreateWindowSurface(IntPtr instance)
  29. {
  30. ulong surfaceHandle = 0;
  31. Action createSurface = () =>
  32. {
  33. if (SDL_Vulkan_CreateSurface(WindowHandle, instance, out surfaceHandle) == SDL_bool.SDL_FALSE)
  34. {
  35. string errorMessage = $"SDL_Vulkan_CreateSurface failed with error \"{SDL_GetError()}\"";
  36. Logger.Error?.Print(LogClass.Application, errorMessage);
  37. throw new Exception(errorMessage);
  38. }
  39. };
  40. if (SDL2Driver.MainThreadDispatcher != null)
  41. {
  42. SDL2Driver.MainThreadDispatcher(createSurface);
  43. }
  44. else
  45. {
  46. createSurface();
  47. }
  48. return (IntPtr)surfaceHandle;
  49. }
  50. public unsafe string[] GetRequiredInstanceExtensions()
  51. {
  52. if (SDL_Vulkan_GetInstanceExtensions(WindowHandle, out uint extensionsCount, IntPtr.Zero) == SDL_bool.SDL_TRUE)
  53. {
  54. IntPtr[] rawExtensions = new IntPtr[(int)extensionsCount];
  55. string[] extensions = new string[(int)extensionsCount];
  56. fixed (IntPtr* rawExtensionsPtr = rawExtensions)
  57. {
  58. if (SDL_Vulkan_GetInstanceExtensions(WindowHandle, out extensionsCount, (IntPtr)rawExtensionsPtr) == SDL_bool.SDL_TRUE)
  59. {
  60. for (int i = 0; i < extensions.Length; i++)
  61. {
  62. extensions[i] = Marshal.PtrToStringUTF8(rawExtensions[i]);
  63. }
  64. return extensions;
  65. }
  66. }
  67. }
  68. string errorMessage = $"SDL_Vulkan_GetInstanceExtensions failed with error \"{SDL_GetError()}\"";
  69. Logger.Error?.Print(LogClass.Application, errorMessage);
  70. throw new Exception(errorMessage);
  71. }
  72. protected override void FinalizeWindowRenderer()
  73. {
  74. Device.DisposeGpu();
  75. }
  76. protected override void SwapBuffers() { }
  77. }
  78. }