EmbeddedWindowOpenGL.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Graphics.GAL;
  4. using Ryujinx.Graphics.OpenGL;
  5. using Ryujinx.Ui.Common.Configuration;
  6. using SPB.Graphics;
  7. using SPB.Graphics.OpenGL;
  8. using SPB.Platform;
  9. using SPB.Platform.WGL;
  10. using SPB.Windowing;
  11. using System;
  12. namespace Ryujinx.Ava.UI.Renderer
  13. {
  14. public class EmbeddedWindowOpenGL : EmbeddedWindow
  15. {
  16. private SwappableNativeWindowBase _window;
  17. public OpenGLContextBase Context { get; set; }
  18. public EmbeddedWindowOpenGL() { }
  19. protected override void OnWindowDestroying()
  20. {
  21. Context.Dispose();
  22. base.OnWindowDestroying();
  23. }
  24. public override void OnWindowCreated()
  25. {
  26. base.OnWindowCreated();
  27. if (OperatingSystem.IsWindows())
  28. {
  29. _window = new WGLWindow(new NativeHandle(WindowHandle));
  30. }
  31. else if (OperatingSystem.IsLinux())
  32. {
  33. _window = X11Window;
  34. }
  35. else
  36. {
  37. throw new PlatformNotSupportedException();
  38. }
  39. var flags = OpenGLContextFlags.Compat;
  40. if (ConfigurationState.Instance.Logger.GraphicsDebugLevel != GraphicsDebugLevel.None)
  41. {
  42. flags |= OpenGLContextFlags.Debug;
  43. }
  44. var graphicsMode = Environment.OSVersion.Platform == PlatformID.Unix ? new FramebufferFormat(new ColorFormat(8, 8, 8, 0), 16, 0, ColorFormat.Zero, 0, 2, false) : FramebufferFormat.Default;
  45. Context = PlatformHelper.CreateOpenGLContext(graphicsMode, 3, 3, flags);
  46. Context.Initialize(_window);
  47. Context.MakeCurrent(_window);
  48. GL.LoadBindings(new OpenTKBindingsContext(Context.GetProcAddress));
  49. Context.MakeCurrent(null);
  50. }
  51. public void MakeCurrent()
  52. {
  53. Context?.MakeCurrent(_window);
  54. }
  55. public void MakeCurrent(NativeWindowBase window)
  56. {
  57. Context?.MakeCurrent(window);
  58. }
  59. public void SwapBuffers()
  60. {
  61. _window?.SwapBuffers();
  62. }
  63. public void InitializeBackgroundContext(IRenderer renderer)
  64. {
  65. (renderer as OpenGLRenderer)?.InitializeBackgroundContext(SPBOpenGLContext.CreateBackgroundContext(Context));
  66. MakeCurrent();
  67. }
  68. }
  69. }