GLRenderer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Gdk;
  2. using Gtk;
  3. using OpenTK.Graphics.OpenGL;
  4. using Ryujinx.Common.Configuration;
  5. using Ryujinx.Graphics.OpenGL;
  6. using Ryujinx.Input.HLE;
  7. using SPB.Graphics;
  8. using SPB.Graphics.OpenGL;
  9. using SPB.Platform;
  10. using SPB.Platform.GLX;
  11. using SPB.Platform.WGL;
  12. using SPB.Windowing;
  13. using System;
  14. using System.Runtime.InteropServices;
  15. namespace Ryujinx.Ui
  16. {
  17. public class GlRenderer : RendererWidgetBase
  18. {
  19. private GraphicsDebugLevel _glLogLevel;
  20. private bool _initializedOpenGL;
  21. private OpenGLContextBase _openGLContext;
  22. private SwappableNativeWindowBase _nativeWindow;
  23. public GlRenderer(InputManager inputManager, GraphicsDebugLevel glLogLevel) : base(inputManager, glLogLevel)
  24. {
  25. _glLogLevel = glLogLevel;
  26. }
  27. protected override bool OnDrawn(Cairo.Context cr)
  28. {
  29. if (!_initializedOpenGL)
  30. {
  31. IntializeOpenGL();
  32. }
  33. return true;
  34. }
  35. private void IntializeOpenGL()
  36. {
  37. _nativeWindow = RetrieveNativeWindow();
  38. Window.EnsureNative();
  39. _openGLContext = PlatformHelper.CreateOpenGLContext(GetGraphicsMode(), 3, 3, _glLogLevel == GraphicsDebugLevel.None ? OpenGLContextFlags.Compat : OpenGLContextFlags.Compat | OpenGLContextFlags.Debug);
  40. _openGLContext.Initialize(_nativeWindow);
  41. _openGLContext.MakeCurrent(_nativeWindow);
  42. // Release the GL exclusivity that SPB gave us as we aren't going to use it in GTK Thread.
  43. _openGLContext.MakeCurrent(null);
  44. WaitEvent.Set();
  45. _initializedOpenGL = true;
  46. }
  47. private SwappableNativeWindowBase RetrieveNativeWindow()
  48. {
  49. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  50. {
  51. IntPtr windowHandle = gdk_win32_window_get_handle(Window.Handle);
  52. return new WGLWindow(new NativeHandle(windowHandle));
  53. }
  54. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  55. {
  56. IntPtr displayHandle = gdk_x11_display_get_xdisplay(Display.Handle);
  57. IntPtr windowHandle = gdk_x11_window_get_xid(Window.Handle);
  58. return new GLXWindow(new NativeHandle(displayHandle), new NativeHandle(windowHandle));
  59. }
  60. throw new NotImplementedException();
  61. }
  62. [DllImport("libgdk-3-0.dll")]
  63. private static extern IntPtr gdk_win32_window_get_handle(IntPtr d);
  64. [DllImport("libgdk-3.so.0")]
  65. private static extern IntPtr gdk_x11_display_get_xdisplay(IntPtr gdkDisplay);
  66. [DllImport("libgdk-3.so.0")]
  67. private static extern IntPtr gdk_x11_window_get_xid(IntPtr gdkWindow);
  68. private static FramebufferFormat GetGraphicsMode()
  69. {
  70. return Environment.OSVersion.Platform == PlatformID.Unix ? new FramebufferFormat(new ColorFormat(8, 8, 8, 0), 16, 0, ColorFormat.Zero, 0, 2, false) : FramebufferFormat.Default;
  71. }
  72. public override void InitializeRenderer()
  73. {
  74. // First take exclusivity on the OpenGL context.
  75. ((Renderer)Renderer).InitializeBackgroundContext(SPBOpenGLContext.CreateBackgroundContext(_openGLContext));
  76. _openGLContext.MakeCurrent(_nativeWindow);
  77. GL.ClearColor(0, 0, 0, 1.0f);
  78. GL.Clear(ClearBufferMask.ColorBufferBit);
  79. SwapBuffers();
  80. }
  81. public override void SwapBuffers()
  82. {
  83. _nativeWindow.SwapBuffers();
  84. }
  85. public override string GetGpuVendorName()
  86. {
  87. return ((Renderer)Renderer).GpuVendor;
  88. }
  89. protected override void Dispose(bool disposing)
  90. {
  91. // Try to bind the OpenGL context before calling the shutdown event
  92. try
  93. {
  94. _openGLContext?.MakeCurrent(_nativeWindow);
  95. }
  96. catch (Exception) { }
  97. Device.DisposeGpu();
  98. NpadManager.Dispose();
  99. // Unbind context and destroy everything
  100. try
  101. {
  102. _openGLContext?.MakeCurrent(null);
  103. }
  104. catch (Exception) { }
  105. _openGLContext.Dispose();
  106. }
  107. }
  108. }