EmbeddedWindowOpenGL.cs
1 using OpenTK.Graphics.OpenGL; 2 using Ryujinx.Common.Configuration; 3 using Ryujinx.Common.Logging; 4 using Ryujinx.Graphics.GAL; 5 using Ryujinx.Graphics.OpenGL; 6 using Ryujinx.UI.Common.Configuration; 7 using SPB.Graphics; 8 using SPB.Graphics.Exceptions; 9 using SPB.Graphics.OpenGL; 10 using SPB.Platform; 11 using SPB.Platform.WGL; 12 using SPB.Windowing; 13 using System; 14 15 namespace Ryujinx.Ava.UI.Renderer 16 { 17 public class EmbeddedWindowOpenGL : EmbeddedWindow 18 { 19 private SwappableNativeWindowBase _window; 20 21 public OpenGLContextBase Context { get; set; } 22 23 protected override void OnWindowDestroying() 24 { 25 Context.Dispose(); 26 27 base.OnWindowDestroying(); 28 } 29 30 public override void OnWindowCreated() 31 { 32 base.OnWindowCreated(); 33 34 if (OperatingSystem.IsWindows()) 35 { 36 _window = new WGLWindow(new NativeHandle(WindowHandle)); 37 } 38 else if (OperatingSystem.IsLinux()) 39 { 40 _window = X11Window; 41 } 42 else 43 { 44 throw new PlatformNotSupportedException(); 45 } 46 47 var flags = OpenGLContextFlags.Compat; 48 if (ConfigurationState.Instance.Logger.GraphicsDebugLevel != GraphicsDebugLevel.None) 49 { 50 flags |= OpenGLContextFlags.Debug; 51 } 52 53 var graphicsMode = Environment.OSVersion.Platform == PlatformID.Unix ? new FramebufferFormat(new ColorFormat(8, 8, 8, 0), 16, 0, ColorFormat.Zero, 0, 2, false) : FramebufferFormat.Default; 54 55 Context = PlatformHelper.CreateOpenGLContext(graphicsMode, 3, 3, flags); 56 57 Context.Initialize(_window); 58 Context.MakeCurrent(_window); 59 60 GL.LoadBindings(new OpenTKBindingsContext(Context.GetProcAddress)); 61 62 Context.MakeCurrent(null); 63 } 64 65 public void MakeCurrent(bool unbind = false, bool shouldThrow = true) 66 { 67 try 68 { 69 Context?.MakeCurrent(!unbind ? _window : null); 70 } 71 catch (ContextException e) 72 { 73 if (shouldThrow) 74 { 75 throw; 76 } 77 78 Logger.Warning?.Print(LogClass.UI, $"Failed to {(!unbind ? "bind" : "unbind")} OpenGL context: {e}"); 79 } 80 } 81 82 public void SwapBuffers() 83 { 84 _window?.SwapBuffers(); 85 } 86 87 public void InitializeBackgroundContext(IRenderer renderer) 88 { 89 (renderer as OpenGLRenderer)?.InitializeBackgroundContext(SPBOpenGLContext.CreateBackgroundContext(Context)); 90 91 MakeCurrent(); 92 } 93 } 94 }