From f2c8d5e091d990a5d0d91575b77150f4833b6cf3 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:42:52 +0000 Subject: [PATCH] fix(gamescope): destroy the image of a failed texture allocation and build patches on pull requests --- .github/workflows/build-gamescope.yml | 13 +++++- tools/gamescope/PATCHES.md | 5 ++- ...0936-nested-swapchain-remake-retries.patch | 29 +++++++++---- .../test_gamescope_swapchain_image_release.py | 43 +++++++++++++++++++ 4 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 tools/tests/test_gamescope_swapchain_image_release.py diff --git a/.github/workflows/build-gamescope.yml b/.github/workflows/build-gamescope.yml index f18c9ad84e..b6ae5078d3 100644 --- a/.github/workflows/build-gamescope.yml +++ b/.github/workflows/build-gamescope.yml @@ -7,6 +7,15 @@ on: - 'tools/gamescope/build-in-arch.sh' - 'tools/gamescope/patches/**' - 'tools/gamescope/runtime-sonames.txt' + - 'tools/gamescope/tests/**' + - '.github/workflows/build-gamescope.yml' + pull_request: + types: [opened, synchronize, reopened] + paths: + - 'tools/gamescope/build-in-arch.sh' + - 'tools/gamescope/patches/**' + - 'tools/gamescope/runtime-sonames.txt' + - 'tools/gamescope/tests/**' - '.github/workflows/build-gamescope.yml' workflow_dispatch: inputs: @@ -16,11 +25,11 @@ on: default: '' permissions: - contents: write + contents: read concurrency: group: ${{ inputs.tag && format('gamescope-tag-{0}', inputs.tag) || format('gamescope-{0}-{1}', github.event_name, github.ref) }} - cancel-in-progress: ${{ github.event_name == 'push' }} + cancel-in-progress: ${{ github.event_name != 'workflow_dispatch' }} jobs: build: diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index 7e1792b6d4..fe901f08d9 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -138,7 +138,10 @@ app's Wayland-hosted gamescope never reaches, or need a newer gamescope than the `vkQueuePresentKHR` and `vkAcquireNextImageKHR` calls now log their VkResult. A failed attempt releases whatever it created, so a retry never finds the surface still owned by a half-built swapchain, and is retried up to 10 times, 100 ms apart. A swapchain that still cannot be made - ends gamescope with a logged reason instead of the assertion. + ends gamescope with a logged reason instead of the assertion. A texture whose + `vkAllocateMemory` (or bind onto shared memory) failed kept its `VkImage`, which the destructor only + destroys together with owned memory; it is destroyed on that failure, so a retry that succeeds + leaves nothing behind. The shared DroidDeck patch also includes `0116-release-cursor-shm-buffers.patch`: pointer cursor surfaces have no X11/xdg window association, so routing their commits through `g_PendingCommits` diff --git a/tools/gamescope/patches/0936-nested-swapchain-remake-retries.patch b/tools/gamescope/patches/0936-nested-swapchain-remake-retries.patch index 95248e86ba..1b0e6d5cce 100644 --- a/tools/gamescope/patches/0936-nested-swapchain-remake-retries.patch +++ b/tools/gamescope/patches/0936-nested-swapchain-remake-retries.patch @@ -19,7 +19,20 @@ index 0835bfe..21a287d 100644 #include "Utils/Algorithm.h" #if defined(__linux__) -@@ -3224,7 +3228,10 @@ bool acquire_next_image( void ) +@@ -2629,2 +2633,4 @@ + } ++ g_device.vk.DestroyImage( g_device.device(), m_vkImage, nullptr ); ++ m_vkImage = VK_NULL_HANDLE; + return false; +@@ -2654,2 +2660,7 @@ + memRequirements.memoryTypeBits, fdProperties.memoryTypeBits ); ++ if ( m_vkImageMemory == VK_NULL_HANDLE ) ++ { ++ g_device.vk.DestroyImage( g_device.device(), m_vkImage, nullptr ); ++ m_vkImage = VK_NULL_HANDLE; ++ } + return false; +@@ -3224,7 +3235,10 @@ bool acquire_next_image( void ) { VkResult res = g_device.vk.AcquireNextImageKHR( g_device.device(), g_output.swapChain, UINT64_MAX, VK_NULL_HANDLE, g_output.acquireFence, &g_output.nOutImage ); if ( res != VK_SUCCESS && res != VK_SUBOPTIMAL_KHR ) @@ -30,7 +43,7 @@ index 0835bfe..21a287d 100644 if ( g_device.vk.WaitForFences( g_device.device(), 1, &g_output.acquireFence, false, UINT64_MAX ) != VK_SUCCESS ) return false; return g_device.vk.ResetFences( g_device.device(), 1, &g_output.acquireFence ) == VK_SUCCESS; -@@ -3330,7 +3337,8 @@ void vulkan_present_to_window( void ) +@@ -3330,7 +3344,8 @@ void vulkan_present_to_window( void ) .pImageIndices = &g_output.nOutImage, }; @@ -40,7 +53,7 @@ index 0835bfe..21a287d 100644 { if ( g_device.supportsPresentWait() ) { -@@ -3339,7 +3347,11 @@ void vulkan_present_to_window( void ) +@@ -3339,7 +3354,11 @@ void vulkan_present_to_window( void ) } } else @@ -52,7 +65,7 @@ index 0835bfe..21a287d 100644 while ( !acquire_next_image() ) vulkan_remake_swapchain(); -@@ -3461,2 +3473,6 @@ +@@ -3461,2 +3480,6 @@ - if ( g_device.vk.GetPhysicalDeviceSurfaceCapabilitiesKHR( g_device.physDev(), pOutput->surface, &pOutput->surfaceCaps ) != VK_SUCCESS ) - return false; + VkResult capsResult = g_device.vk.GetPhysicalDeviceSurfaceCapabilitiesKHR( g_device.physDev(), pOutput->surface, &pOutput->surfaceCaps ); @@ -61,7 +74,7 @@ index 0835bfe..21a287d 100644 + vk_errorf( capsResult, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR failed" ); + return false; + } -@@ -3559,7 +3575,12 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) +@@ -3559,7 +3582,12 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) .clipped = VK_TRUE, }; @@ -75,7 +88,7 @@ index 0835bfe..21a287d 100644 return false; } -@@ -3606,6 +3627,19 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) +@@ -3606,6 +3634,19 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) return true; } @@ -95,7 +108,7 @@ index 0835bfe..21a287d 100644 bool vulkan_remake_swapchain( void ) { std::unique_lock lock(present_wait_lock); -@@ -3618,9 +3652,3 @@ bool vulkan_remake_swapchain( void ) +@@ -3618,9 +3659,3 @@ bool vulkan_remake_swapchain( void ) - pOutput->outputImages.clear(); - pOutput->presentationImages.clear(); @@ -106,7 +119,7 @@ index 0835bfe..21a287d 100644 - g_device.vk.DestroySwapchainKHR( g_device.device(), pOutput->swapChain, nullptr ); + vulkan_release_swapchain( pOutput ); -@@ -3630,9 +3658,18 @@ bool vulkan_remake_swapchain( void ) +@@ -3630,9 +3665,18 @@ bool vulkan_remake_swapchain( void ) for (auto& pCaptureTexture : pOutput->pCaptureTextures) pCaptureTexture = nullptr; diff --git a/tools/tests/test_gamescope_swapchain_image_release.py b/tools/tests/test_gamescope_swapchain_image_release.py new file mode 100644 index 0000000000..d30503b226 --- /dev/null +++ b/tools/tests/test_gamescope_swapchain_image_release.py @@ -0,0 +1,43 @@ +from pathlib import Path +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +PATCH = ROOT / "tools/gamescope/patches/0936-nested-swapchain-remake-retries.patch" +DESTROY = "g_device.vk.DestroyImage( g_device.device(), m_vkImage, nullptr );" +RESET = "m_vkImage = VK_NULL_HANDLE;" + + +def hunks(): + out = [] + for line in PATCH.read_text().splitlines(): + if line.startswith("@@"): + out.append([]) + elif out: + out[-1].append(line) + return out + + +def hunk_after(anchor): + for hunk in hunks(): + if any(anchor in line for line in hunk): + return hunk + raise AssertionError(anchor) + + +class SwapchainImageReleaseTest(unittest.TestCase): + def test_failed_allocation_destroys_the_image(self): + hunk = hunk_after(DESTROY) + added = [line[1:].strip() for line in hunk if line.startswith("+")] + self.assertEqual(added, [DESTROY, RESET]) + self.assertEqual(hunk[-1].strip(), "return false;") + + def test_failed_bind_destroys_only_an_image_the_destructor_would_skip(self): + hunk = hunk_after("if ( m_vkImageMemory == VK_NULL_HANDLE )") + added = [line[1:].strip() for line in hunk if line.startswith("+")] + self.assertEqual(added, ["if ( m_vkImageMemory == VK_NULL_HANDLE )", "{", DESTROY, RESET, "}"]) + self.assertEqual(hunk[-1].strip(), "return false;") + + +if __name__ == "__main__": + unittest.main()