diff --git a/Dashboard.Common/Pal/DeviceContext.cs b/Dashboard.Common/Pal/DeviceContext.cs index cba5131..a6be2e3 100644 --- a/Dashboard.Common/Pal/DeviceContext.cs +++ b/Dashboard.Common/Pal/DeviceContext.cs @@ -1,10 +1,10 @@ +using System.Numerics; using Dashboard.Collections; using Dashboard.Windowing; -using BindingFlags = System.Reflection.BindingFlags; namespace Dashboard.Pal { - public abstract class DeviceContext : IContextBase + public abstract class DeviceContext : IContextBase, IDeviceContext { private readonly TypeDictionary _extensions = new TypeDictionary(true); @@ -19,6 +19,9 @@ namespace Dashboard.Pal public abstract string DriverName { get; } public abstract string DriverVendor { get; } public abstract Version DriverVersion { get; } + public abstract ISwapGroup SwapGroup { get; } + public abstract Vector2 FramebufferSize { get; } + public virtual bool DoubleBuffered { get; } = false; public bool IsDisposed { get; private set; } diff --git a/Dashboard.OpenGL/GLDeviceContext.cs b/Dashboard.OpenGL/GLDeviceContext.cs index 35e0f09..241cf89 100644 --- a/Dashboard.OpenGL/GLDeviceContext.cs +++ b/Dashboard.OpenGL/GLDeviceContext.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Collections.Immutable; +using System.Numerics; using Dashboard.Drawing; using Dashboard.OpenGL.Drawing; using Dashboard.Pal; @@ -27,6 +28,9 @@ namespace Dashboard.OpenGL public override string DriverName => "Dashboard OpenGL Device Context"; public override string DriverVendor => "Dashboard"; public override Version DriverVersion => new Version(0, 1, 0); + public override ISwapGroup SwapGroup { get; } + public override Vector2 FramebufferSize => GLContext.FramebufferSize; + public override bool DoubleBuffered { get; } = true; public Version GLVersion { get; } public string GLRenderer { get; } @@ -39,9 +43,11 @@ namespace Dashboard.OpenGL private readonly ConcurrentQueue _beforeDrawActions = new ConcurrentQueue(); private readonly ConcurrentQueue _afterDrawActions = new ConcurrentQueue(); - public GLDeviceContext(Application app, IWindow? window, IGLContext context) : base(app, window) + public GLDeviceContext(Application app, IWindow? window, IGLContext context, ISwapGroup swap) : base(app, window) { GLContext = context; + SwapGroup = swap; + context.MakeCurrent(); GLLoader.LoadBindings(new GLContextBindingsContext(context)); diff --git a/Dashboard.OpenGL/IGLContext.cs b/Dashboard.OpenGL/IGLContext.cs index 27d9ae6..8e16fc1 100644 --- a/Dashboard.OpenGL/IGLContext.cs +++ b/Dashboard.OpenGL/IGLContext.cs @@ -14,11 +14,6 @@ namespace Dashboard.OpenGL /// -1 assigns no group. public int ContextGroup { get; } - /// - /// The size of the framebuffer in pixels. - /// - public Vector2 FramebufferSize { get; } - /// /// Called when the context is disposed. /// diff --git a/Dashboard.OpenTK/PAL2/PhysicalWindow.cs b/Dashboard.OpenTK/PAL2/PhysicalWindow.cs index 03b4e5e..2cca335 100644 --- a/Dashboard.OpenTK/PAL2/PhysicalWindow.cs +++ b/Dashboard.OpenTK/PAL2/PhysicalWindow.cs @@ -64,7 +64,7 @@ namespace Dashboard.OpenTK.PAL2 { Application = app; WindowHandle = window; - DeviceContext = new GLDeviceContext(app, this, new Pal2GLContext(window, context)); + DeviceContext = new GLDeviceContext(app, this, new Pal2GLContext(window, context), new DummySwapGroup(context)); } public PhysicalWindow(Application app, GraphicsApiHints hints) @@ -81,7 +81,8 @@ namespace Dashboard.OpenTK.PAL2 { case GraphicsApi.OpenGL: case GraphicsApi.OpenGLES: - return new GLDeviceContext(app, window, new Pal2GLContext(handle, TK.OpenGL.CreateFromWindow(handle))); + OpenGLContextHandle context = TK.OpenGL.CreateFromWindow(handle); + return new GLDeviceContext(app, window, new Pal2GLContext(handle, context), new DummySwapGroup(context)); default: throw new Exception($"Unknown graphics API {hints.Api}."); } @@ -146,5 +147,28 @@ namespace Dashboard.OpenTK.PAL2 return Math.Max(x, y); } } + + private class DummySwapGroup(OpenGLContextHandle context) : ISwapGroup + { + public int SwapInterval + { + get + { + TK.OpenGL.SetCurrentContext(context); + return TK.OpenGL.GetSwapInterval(); + } + + set + { + TK.OpenGL.SetCurrentContext(context); + TK.OpenGL.SetSwapInterval(value); + } + } + + public void Swap() + { + TK.OpenGL.SwapBuffers(context); + } + } } }