From 17928792bdfabd8e9349161aef1de250e4193fab Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:45:21 -0500 Subject: [PATCH] fix(gamescope): composite every frame when the host releases buffers before scanout Holding a forwarded client buffer until scanout ends (0147) costs a three-buffer client a third of the refresh rate: Steam's UI ran at 80 fps on a 120 Hz screen. Once the backend sees a forwarded buffer released while it is still being scanned out, it composites every frame into its own output images, which 0146 reuses at latch. The Steam UI runs at 120 fps again on the Pocket FIT. --- tools/gamescope/PATCHES.md | 9 ++ ...te-when-host-releases-before-scanout.patch | 116 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index bd813adcb0..9c322dbff4 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -497,3 +497,12 @@ that the display has finished with it, on a waiter thread so nothing else blocks buffer was shown again is dropped, so the later release decides. A pending wait holds its own reference on the buffer, since that later release can drop the host's reference before the wait ends. Composite output images keep the 0146 path, and a host that waits for the fence itself releases buffers that are already idle, so nothing changes there. + +## 0148 Wayland backend: composite every frame when the host releases buffers before scanout + +With 0147 a forwarded client buffer stays held until the display has finished scanning it out, about one refresh after +the host releases it. Steam's web helper cycles three buffers of its own, so forwarding them held the Steam UI at 80 fps +on a 120 Hz screen (frames alternately one and two refreshes apart). The first time a forwarded buffer comes back while +it is still being scanned out, the backend now composites every frame into its own output images. Those are reused at +latch under 0146, and the client's buffers return as soon as the composite has read them: the Steam UI then runs at +the full 120 fps. Hosts that release a buffer only after scanout never take this path and keep forwarding. diff --git a/tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch b/tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch new file mode 100644 index 0000000000..e7f872ac0e --- /dev/null +++ b/tools/gamescope/patches/0148-composite-when-host-releases-before-scanout.patch @@ -0,0 +1,116 @@ +diff --git a/src/Backends/WaylandBackend.cpp b/src/Backends/WaylandBackend.cpp +index d9db2e6..5c4cd86 100644 +--- a/src/Backends/WaylandBackend.cpp ++++ b/src/Backends/WaylandBackend.cpp +@@ -804,24 +804,31 @@ namespace gamescope + { + auto iter = m_pOutputs.find( pOutput ); + if ( iter == m_pOutputs.end() ) + return nullptr; + + return &iter->second; + } + + wl_region *GetEmptyRegion() const { return m_pEmptyRegion; } + wl_region *GetFullRegion() const { return m_pFullRegion; } + CWaylandFb *GetBlackFb() const { return m_BlackFb.get(); } + ++ bool HostReleasesBeforeScanout() const { return m_bHostReleasesBeforeScanout; } ++ void OnHostReleasedBeforeScanout() ++ { ++ if ( !m_bHostReleasesBeforeScanout.exchange( true ) ) ++ xdg_log.infof( "Host releases buffers before scanout ends: compositing every frame into our own images" ); ++ } ++ + void OnConnectorDestroyed( CWaylandConnector *pConnector ) + { + m_pFocusConnector.compare_exchange_strong( pConnector, nullptr ); + } + + private: + + void Wayland_Registry_Global( wl_registry *pRegistry, uint32_t uName, const char *pInterface, uint32_t uVersion ); + static const wl_registry_listener s_RegistryListener; + + void Wayland_Modifier( zwp_linux_dmabuf_v1 *pDmabuf, uint32_t uFormat, uint32_t uModifierHi, uint32_t uModifierLo ); + +@@ -871,24 +878,25 @@ namespace gamescope + + wl_display *m_pDisplay = nullptr; + wl_shm *m_pShm = nullptr; + wl_compositor *m_pCompositor = nullptr; + wp_single_pixel_buffer_manager_v1 *m_pSinglePixelBufferManager = nullptr; + wl_subcompositor *m_pSubcompositor = nullptr; + zwp_linux_dmabuf_v1 *m_pLinuxDmabuf = nullptr; + xdg_wm_base *m_pXdgWmBase = nullptr; + wp_viewporter *m_pViewporter = nullptr; + wl_region *m_pEmptyRegion = nullptr; + wl_region *m_pFullRegion = nullptr; + Rc m_BlackFb; ++ std::atomic m_bHostReleasesBeforeScanout = { false }; + OwningRc m_pOwnedBlackFb; + OwningRc m_pBlackTexture; + wp_presentation *m_pPresentation = nullptr; + droiddeck_refresh_v1 *m_pHostRefresh = nullptr; + std::array m_HostRefreshRates{}; + std::atomic m_uHostRefreshRateCount = { 0 }; + std::vector m_PendingHostRefreshRates; + int m_nSentHostRefresh = 0; + int m_nSentHostFrameRate = 0; + frog_color_management_factory_v1 *m_pFrogColorMgmtFactory = nullptr; + wp_color_manager_v1 *m_pWPColorManager = nullptr; + wp_image_description_v1 *m_pWPImageDescriptions[ GamescopeAppTextureColorspace_Count ]{}; +@@ -1083,24 +1091,25 @@ namespace gamescope + xdg_log.errorf( "Compositor released us but we were not acquired. Oh no." ); + } + + void CWaylandFb::Wayland_Buffer_Release( wl_buffer *pBuffer ) + { + assert( m_pHostBuffer ); + assert( m_pHostBuffer == pBuffer ); + + xdg_log.debugf( "buffer_release: %p", pBuffer ); + + if ( ShowsClientBuffer() && gamescope::DmabufScanoutPending( m_nDmabufFd ) ) + { ++ m_pBackend->OnHostReleasedBeforeScanout(); + int nFd = fcntl( m_nDmabufFd, F_DUPFD_CLOEXEC, 0 ); + if ( nFd >= 0 ) + { + uint64_t ulSerial; + { + std::scoped_lock lock( m_AcquireMutex ); + ulSerial = m_ulAcquireSerial; + } + auto *pWait = new CWaylandScanoutRelease( this, nFd, ulSerial ); + if ( CWaylandScanoutRelease::Waiter().AddWaitable( pWait, EPOLLOUT | EPOLLHUP | EPOLLONESHOT ) ) + return; + delete pWait; +@@ -1194,24 +1203,27 @@ namespace gamescope + plane.Present( nullptr ); + } + else + { + // TODO: Dedupe some of this composite check code between us and drm.cpp + bool bLayer0ScreenSize = close_enough(pFrameInfo->layers.get( 0 ).scale.x, 1.0f) && close_enough(pFrameInfo->layers.get( 0 ).scale.y, 1.0f); + + GamescopeUpscaleFilter eLayer0Filter = pFrameInfo->layers.get( 0 ).filter; + bool bNeedsCompositeFromFilter = ( eLayer0Filter == GamescopeUpscaleFilter::NEAREST || + eLayer0Filter == GamescopeUpscaleFilter::PIXEL ) && !bLayer0ScreenSize; + + bNeedsFullComposite |= cv_composite_force; ++ // A forwarded buffer would stay out of its client's hands until scanout ends, which caps a ++ // three-image client at two thirds of the refresh rate. Our own output images are reused at latch. ++ bNeedsFullComposite |= m_pBackend->HostReleasesBeforeScanout(); + bNeedsFullComposite |= cv_wayland_overlay_planes >= 0 && pFrameInfo->layers.count() > 1 + cv_wayland_overlay_planes; + bNeedsFullComposite |= pFrameInfo->useFSRLayer0; + bNeedsFullComposite |= pFrameInfo->useNISLayer0; + bNeedsFullComposite |= pFrameInfo->useSGSRLayer0; + bNeedsFullComposite |= pFrameInfo->blurLayer0; + bNeedsFullComposite |= bNeedsCompositeFromFilter; + bNeedsFullComposite |= g_bColorSliderInUse; + bNeedsFullComposite |= pFrameInfo->bFadingOut; + bNeedsFullComposite |= !g_reshade_effect.empty(); + + if ( g_bOutputHDREnabled ) + bNeedsFullComposite |= g_bHDRItmEnable;