GLRenderer.cs 4.1 KB

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