diff --git a/internal/graphicscommand/commandqueue.go b/internal/graphicscommand/commandqueue.go index f522caa1c..e419fe281 100644 --- a/internal/graphicscommand/commandqueue.go +++ b/internal/graphicscommand/commandqueue.go @@ -27,6 +27,16 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/shaderir" ) +var vsyncEnabled int32 = 1 + +func SetVsyncEnabled(enabled bool) { + if enabled { + atomic.StoreInt32(&vsyncEnabled, 1) + } else { + atomic.StoreInt32(&vsyncEnabled, 0) + } +} + // FlushCommands flushes the command queue and present the screen if needed. // If endFrame is true, the current screen might be used to present. func FlushCommands(graphicsDriver graphicsdriver.Graphics, endFrame bool, swapBuffersForGL func()) error { @@ -154,10 +164,16 @@ func (q *commandQueue) Flush(graphicsDriver graphicsdriver.Graphics, endFrame bo } var sync bool - for _, c := range q.commands { - if c.NeedsSync() { - sync = true - break + // Disable asynchrnous rendering when vsync is on, as this causes a rendering delay (#2822). + if endFrame && atomic.LoadInt32(&vsyncEnabled) != 0 { + sync = true + } + if !sync { + for _, c := range q.commands { + if c.NeedsSync() { + sync = true + break + } } } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index c93a0f0c7..d4e54d92f 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -30,6 +30,7 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/file" "github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/glfw" + "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/hook" "github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk" @@ -1317,8 +1318,10 @@ func (u *UserInterface) setFPSMode(fpsMode FPSModeType) error { return err } + vsyncEnabled := u.fpsMode == FPSModeVsyncOn + graphicscommand.SetVsyncEnabled(vsyncEnabled) u.renderThread.CallAsync(func() { - u.graphicsDriver.SetVsyncEnabled(u.fpsMode == FPSModeVsyncOn) + u.graphicsDriver.SetVsyncEnabled(vsyncEnabled) }) return nil