From ece60af1b7cd2a7600f6119bf1661cb9c84a243d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 27 Dec 2022 18:53:45 +0900 Subject: [PATCH] internal/graphicsdriver/metal: stop using presentsWithTransaction presentsWithTransaction caused many troubles. The critical thing was that nextDrawable sometimes took more than one second when a user inputs with NSTextView. Fortunately, applications work well even without presentsWithTransaction. Updates #1029 Updates #1196 Updates #1745 Updates #1974 --- .../directx/graphics_windows.go | 3 --- internal/graphicsdriver/graphics.go | 1 - .../graphicsdriver/metal/graphics_darwin.go | 10 +------ internal/graphicsdriver/metal/view_darwin.go | 27 +++++-------------- internal/graphicsdriver/metal/view_ios.go | 9 ------- .../graphicsdriver/metal/view_macos_darwin.go | 17 ------------ internal/graphicsdriver/opengl/graphics.go | 4 --- internal/ui/ui_glfw.go | 1 - 8 files changed, 7 insertions(+), 65 deletions(-) diff --git a/internal/graphicsdriver/directx/graphics_windows.go b/internal/graphicsdriver/directx/graphics_windows.go index 51b9469e4..55855449b 100644 --- a/internal/graphicsdriver/directx/graphics_windows.go +++ b/internal/graphicsdriver/directx/graphics_windows.go @@ -1189,9 +1189,6 @@ func (g *Graphics) SetVsyncEnabled(enabled bool) { g.vsyncEnabled = enabled } -func (g *Graphics) SetFullscreen(fullscreen bool) { -} - func (g *Graphics) NeedsRestoring() bool { return false } diff --git a/internal/graphicsdriver/graphics.go b/internal/graphicsdriver/graphics.go index f2607547e..2b45ba5cf 100644 --- a/internal/graphicsdriver/graphics.go +++ b/internal/graphicsdriver/graphics.go @@ -45,7 +45,6 @@ type Graphics interface { NewImage(width, height int) (Image, error) NewScreenFramebufferImage(width, height int) (Image, error) SetVsyncEnabled(enabled bool) - SetFullscreen(fullscreen bool) NeedsRestoring() bool NeedsClearingScreen() bool IsGL() bool diff --git a/internal/graphicsdriver/metal/graphics_darwin.go b/internal/graphicsdriver/metal/graphics_darwin.go index e1e1e127e..cdd01e8bf 100644 --- a/internal/graphicsdriver/metal/graphics_darwin.go +++ b/internal/graphicsdriver/metal/graphics_darwin.go @@ -208,14 +208,10 @@ func (g *Graphics) flushIfNeeded(present bool) { g.screenDrawable = g.view.nextDrawable() } - if !g.view.presentsWithTransaction() && present && g.screenDrawable != (ca.MetalDrawable{}) { + if present && g.screenDrawable != (ca.MetalDrawable{}) { g.cb.PresentDrawable(g.screenDrawable) } g.cb.Commit() - if g.view.presentsWithTransaction() && present && g.screenDrawable != (ca.MetalDrawable{}) { - g.cb.WaitUntilScheduled() - g.screenDrawable.Present() - } for _, t := range g.tmpTextures { t.Release() @@ -631,10 +627,6 @@ func (g *Graphics) SetVsyncEnabled(enabled bool) { g.view.setDisplaySyncEnabled(enabled) } -func (g *Graphics) SetFullscreen(fullscreen bool) { - g.view.setFullscreen(fullscreen) -} - func (g *Graphics) NeedsRestoring() bool { return false } diff --git a/internal/graphicsdriver/metal/view_darwin.go b/internal/graphicsdriver/metal/view_darwin.go index 9a703a890..d62d9ce5e 100644 --- a/internal/graphicsdriver/metal/view_darwin.go +++ b/internal/graphicsdriver/metal/view_darwin.go @@ -28,7 +28,6 @@ type view struct { windowChanged bool vsyncDisabled bool - fullscreen bool device mtl.Device ml ca.MetalLayer @@ -54,22 +53,6 @@ func (v *view) setDisplaySyncEnabled(enabled bool) { func (v *view) forceSetDisplaySyncEnabled(enabled bool) { v.ml.SetDisplaySyncEnabled(enabled) v.vsyncDisabled = !enabled - - // setting presentsWithTransaction true makes the FPS stable (#1196). We're not sure why... - v.updatePresentsWithTransaction() -} - -func (v *view) setFullscreen(fullscreen bool) { - if v.fullscreen == fullscreen { - return - } - v.fullscreen = fullscreen - v.updatePresentsWithTransaction() -} - -func (v *view) updatePresentsWithTransaction() { - v.ml.SetPresentsWithTransaction(v.usePresentsWithTransaction()) - v.ml.SetMaximumDrawableCount(v.maximumDrawableCount()) } func (v *view) colorPixelFormat() mtl.PixelFormat { @@ -96,6 +79,12 @@ func (v *view) initialize() error { v.forceSetDisplaySyncEnabled(!v.vsyncDisabled) v.ml.SetFramebufferOnly(true) + // presentsWithTransaction doesn't work in the fullscreen mode (#1745, #1974). + // presentsWithTransaction doesn't work with vsync off (#1196). + // nextDrawable took more than one second if the window has other controls like NSTextView (#1029). + v.ml.SetPresentsWithTransaction(false) + v.ml.SetMaximumDrawableCount(3) + return nil } @@ -107,7 +96,3 @@ func (v *view) nextDrawable() ca.MetalDrawable { } return d } - -func (v *view) presentsWithTransaction() bool { - return v.ml.PresentsWithTransaction() -} diff --git a/internal/graphicsdriver/metal/view_ios.go b/internal/graphicsdriver/metal/view_ios.go index e2707e917..136335cd3 100644 --- a/internal/graphicsdriver/metal/view_ios.go +++ b/internal/graphicsdriver/metal/view_ios.go @@ -57,15 +57,6 @@ func (v *view) update() { C.setFrame(v.ml.Layer(), unsafe.Pointer(v.uiview)) } -func (v *view) usePresentsWithTransaction() bool { - // Do not use presentsWithTransaction on iOS (#1799). - return false -} - -func (v *view) maximumDrawableCount() int { - return 3 -} - const ( storageMode = mtl.StorageModeShared resourceStorageMode = mtl.ResourceStorageModeShared diff --git a/internal/graphicsdriver/metal/view_macos_darwin.go b/internal/graphicsdriver/metal/view_macos_darwin.go index 47aee137a..bad45f14f 100644 --- a/internal/graphicsdriver/metal/view_macos_darwin.go +++ b/internal/graphicsdriver/metal/view_macos_darwin.go @@ -42,23 +42,6 @@ func (v *view) update() { v.windowChanged = false } -func (v *view) usePresentsWithTransaction() bool { - // Disable presentsWithTransaction on the fullscreen mode (#1745, #1974). - if v.fullscreen { - return false - } - return !v.vsyncDisabled -} - -func (v *view) maximumDrawableCount() int { - // When presentsWithTransaction is YES and triple buffering is enabled, nextDrawing returns immediately once every two times. - // This makes FPS doubled. To avoid this, disable the triple buffering. - if v.usePresentsWithTransaction() { - return 2 - } - return 3 -} - const ( storageMode = mtl.StorageModeManaged resourceStorageMode = mtl.ResourceStorageModeManaged diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index b11d3c6e5..a00330713 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -272,10 +272,6 @@ func (g *Graphics) SetVsyncEnabled(enabled bool) { // Do nothing } -func (g *Graphics) SetFullscreen(fullscreen bool) { - // Do nothing -} - func (g *Graphics) NeedsRestoring() bool { // Though it is possible to have a logic to restore the graphics data for GPU, do not use it for performance (#1603). if runtime.GOOS == "js" { diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 4f0e6cf6e..0c079daa4 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -1266,7 +1266,6 @@ func (u *userInterfaceImpl) setFullscreen(fullscreen bool) { if u.isFullscreen() == fullscreen { return } - u.graphicsDriver.SetFullscreen(fullscreen) // Enter the fullscreen. if fullscreen {