OpenGLEmbeddedWindow.cs 2.3 KB

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