GLRenderer.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(0);
  78. }
  79. public override void SwapBuffers(object image)
  80. {
  81. if((int)image != 0)
  82. {
  83. // The game's framebruffer is already bound, so blit it to the window's backbuffer
  84. GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0);
  85. GL.Clear(ClearBufferMask.ColorBufferBit);
  86. GL.ClearColor(0, 0, 0, 1);
  87. GL.BlitFramebuffer(0,
  88. 0,
  89. WindowWidth,
  90. WindowHeight,
  91. 0,
  92. 0,
  93. WindowWidth,
  94. WindowHeight,
  95. ClearBufferMask.ColorBufferBit,
  96. BlitFramebufferFilter.Linear);
  97. }
  98. _nativeWindow.SwapBuffers();
  99. }
  100. protected override string GetGpuBackendName()
  101. {
  102. return "OpenGL";
  103. }
  104. protected override void Dispose(bool disposing)
  105. {
  106. // Try to bind the OpenGL context before calling the shutdown event
  107. try
  108. {
  109. _openGLContext?.MakeCurrent(_nativeWindow);
  110. }
  111. catch (Exception) { }
  112. Device.DisposeGpu();
  113. NpadManager.Dispose();
  114. // Unbind context and destroy everything
  115. try
  116. {
  117. _openGLContext?.MakeCurrent(null);
  118. }
  119. catch (Exception) { }
  120. _openGLContext.Dispose();
  121. }
  122. }
  123. }