OpenGLEmbeddedWindow.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Configuration;
  3. using SPB.Graphics;
  4. using SPB.Graphics.OpenGL;
  5. using SPB.Platform;
  6. using SPB.Platform.WGL;
  7. using SPB.Windowing;
  8. using System;
  9. namespace Ryujinx.Ava.UI.Helpers
  10. {
  11. public class OpenGLEmbeddedWindow : EmbeddedWindow
  12. {
  13. private readonly int _major;
  14. private readonly int _minor;
  15. private readonly GraphicsDebugLevel _graphicsDebugLevel;
  16. private SwappableNativeWindowBase _window;
  17. public OpenGLContextBase Context { get; set; }
  18. public OpenGLEmbeddedWindow(int major, int minor, GraphicsDebugLevel graphicsDebugLevel)
  19. {
  20. _major = major;
  21. _minor = minor;
  22. _graphicsDebugLevel = graphicsDebugLevel;
  23. }
  24. protected override void OnWindowDestroying()
  25. {
  26. Context.Dispose();
  27. base.OnWindowDestroying();
  28. }
  29. public override void OnWindowCreated()
  30. {
  31. base.OnWindowCreated();
  32. if (OperatingSystem.IsWindows())
  33. {
  34. _window = new WGLWindow(new NativeHandle(WindowHandle));
  35. }
  36. else if (OperatingSystem.IsLinux())
  37. {
  38. _window = X11Window;
  39. }
  40. else
  41. {
  42. throw new PlatformNotSupportedException();
  43. }
  44. var flags = OpenGLContextFlags.Compat;
  45. if (_graphicsDebugLevel != GraphicsDebugLevel.None)
  46. {
  47. flags |= OpenGLContextFlags.Debug;
  48. }
  49. Context = PlatformHelper.CreateOpenGLContext(FramebufferFormat.Default, _major, _minor, flags);
  50. Context.Initialize(_window);
  51. Context.MakeCurrent(_window);
  52. var bindingsContext = new OpenToolkitBindingsContext(Context.GetProcAddress);
  53. GL.LoadBindings(bindingsContext);
  54. Context.MakeCurrent(null);
  55. }
  56. public void MakeCurrent()
  57. {
  58. Context?.MakeCurrent(_window);
  59. }
  60. public void MakeCurrent(NativeWindowBase window)
  61. {
  62. Context?.MakeCurrent(window);
  63. }
  64. public void SwapBuffers()
  65. {
  66. _window.SwapBuffers();
  67. }
  68. }
  69. }