VulkanWindow.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Ryujinx.Common.Configuration;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Input.HLE;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using static SDL2.SDL;
  7. namespace Ryujinx.Headless.SDL2.Vulkan
  8. {
  9. class VulkanWindow : WindowBase
  10. {
  11. private GraphicsDebugLevel _glLogLevel;
  12. public VulkanWindow(InputManager inputManager, GraphicsDebugLevel glLogLevel, AspectRatio aspectRatio, bool enableMouse) : base(inputManager, glLogLevel, aspectRatio, enableMouse)
  13. {
  14. _glLogLevel = glLogLevel;
  15. }
  16. public override SDL_WindowFlags GetWindowFlags() => SDL_WindowFlags.SDL_WINDOW_VULKAN;
  17. protected override void InitializeWindowRenderer() { }
  18. protected override void InitializeRenderer()
  19. {
  20. Renderer?.Window.SetSize(DefaultWidth, DefaultHeight);
  21. MouseDriver.SetClientSize(DefaultWidth, DefaultHeight);
  22. }
  23. public unsafe IntPtr CreateWindowSurface(IntPtr instance)
  24. {
  25. if (SDL_Vulkan_CreateSurface(WindowHandle, instance, out ulong surfaceHandle) == SDL_bool.SDL_FALSE)
  26. {
  27. string errorMessage = $"SDL_Vulkan_CreateSurface failed with error \"{SDL_GetError()}\"";
  28. Logger.Error?.Print(LogClass.Application, errorMessage);
  29. throw new Exception(errorMessage);
  30. }
  31. return (IntPtr)surfaceHandle;
  32. }
  33. // TODO: Fix this in SDL2-CS.
  34. [DllImport("SDL2", EntryPoint = "SDL_Vulkan_GetInstanceExtensions", CallingConvention = CallingConvention.Cdecl)]
  35. public static extern SDL_bool SDL_Vulkan_GetInstanceExtensions_Workaround(IntPtr window, out uint count, IntPtr names);
  36. public unsafe string[] GetRequiredInstanceExtensions()
  37. {
  38. if (SDL_Vulkan_GetInstanceExtensions_Workaround(WindowHandle, out uint extensionsCount, IntPtr.Zero) == SDL_bool.SDL_TRUE)
  39. {
  40. IntPtr[] rawExtensions = new IntPtr[(int)extensionsCount];
  41. string[] extensions = new string[(int)extensionsCount];
  42. fixed (IntPtr* rawExtensionsPtr = rawExtensions)
  43. {
  44. if (SDL_Vulkan_GetInstanceExtensions_Workaround(WindowHandle, out extensionsCount, (IntPtr)rawExtensionsPtr) == SDL_bool.SDL_TRUE)
  45. {
  46. for (int i = 0; i < extensions.Length; i++)
  47. {
  48. extensions[i] = Marshal.PtrToStringUTF8(rawExtensions[i]);
  49. }
  50. return extensions;
  51. }
  52. }
  53. }
  54. string errorMessage = $"SDL_Vulkan_GetInstanceExtensions failed with error \"{SDL_GetError()}\"";
  55. Logger.Error?.Print(LogClass.Application, errorMessage);
  56. throw new Exception(errorMessage);
  57. }
  58. protected override void FinalizeWindowRenderer()
  59. {
  60. Device.DisposeGpu();
  61. }
  62. protected override void SwapBuffers() { }
  63. }
  64. }