From cf4b210ef85ffea68c4eb28c067e359080d700d4 Mon Sep 17 00:00:00 2001 From: wallentx Date: Sun, 11 Oct 2026 04:01:07 -0500 Subject: [PATCH] Support PowerVR swapchains and linear image imports in Gamescope --- tools/gamescope/PATCHES.md | 15 + tools/gamescope/build-in-arch.sh | 5 +- ...nested-swapchain-capability-fallback.patch | 358 ++++++++++++++++++ .../0121-powervr-single-pixel-rcas.patch | 15 + .../0122-powervr-linear-dmabuf-import.patch | 156 ++++++++ .../0123-powervr-rgba-copy-surface.patch | 61 +++ .../0124-sdl-preserve-explicit-refresh.patch | 78 ++++ .../0125-sdl-wait-before-present.patch | 23 ++ tools/gamescope/release.env | 6 +- tools/gamescope/tests/nested-swapchain.cpp | 156 ++++++++ 10 files changed, 869 insertions(+), 4 deletions(-) create mode 100644 tools/gamescope/patches/0120-nested-swapchain-capability-fallback.patch create mode 100644 tools/gamescope/patches/0121-powervr-single-pixel-rcas.patch create mode 100644 tools/gamescope/patches/0122-powervr-linear-dmabuf-import.patch create mode 100644 tools/gamescope/patches/0123-powervr-rgba-copy-surface.patch create mode 100644 tools/gamescope/patches/0124-sdl-preserve-explicit-refresh.patch create mode 100644 tools/gamescope/patches/0125-sdl-wait-before-present.patch create mode 100644 tools/gamescope/tests/nested-swapchain.cpp diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index acb3455152..6c02250743 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -45,5 +45,20 @@ library list, before anything is published. `GAMESCOPE_FOCUSABLE_WINDOWS`, so the client kept its loading screen over the running game (Skyrim SE, A Plague Tale: Innocence, on the games' own Xwayland). MapNotify now refreshes the flag. +The system PowerVR driver uses the existing SDL/Wayland swapchain backend. Its additional patches are: + +- `0120-nested-swapchain-capability-fallback.patch`: negotiate the surface's usages, mutable-format + support, image count, alpha and extent; use offscreen composition and GPU copies when required. +- `0121-powervr-single-pixel-rcas.patch`: use the existing single-pixel RCAS shader on PowerVR, + where the quad variant fails compilation. +- `0122-powervr-linear-dmabuf-import.patch`: retain the explicit linear layout when PowerVR + rejects the mutable-format query, and intersect imported-image and FD memory-type masks. +- `0123-powervr-rgba-copy-surface.patch`: require an advertised RGBA SDR copy surface matching + the shader's `rgba8` storage view. The tested workaround leaves other vendors, HDR and direct + composition unchanged; it does not restrict game-facing BGRA formats. +- `0124-sdl-preserve-explicit-refresh.patch` and `0125-sdl-wait-before-present.patch`: preserve + configured refresh across SDL events and finish composition before presenting without a + render-finished semaphore. The build runs the focused capability, format and refresh tests. + Sixteen more of Armada's patches are DRM/lease/HDR-on-KMS work for a native display, which this app's Wayland-hosted gamescope never reaches, or need a newer gamescope than the runtime has. diff --git a/tools/gamescope/build-in-arch.sh b/tools/gamescope/build-in-arch.sh index 2078592aa0..55d6db7b03 100755 --- a/tools/gamescope/build-in-arch.sh +++ b/tools/gamescope/build-in-arch.sh @@ -55,9 +55,12 @@ makepkg -A -s --noconfirm --skipchecksums --skippgpcheck # -A: the PKGBUILD lis ls -l *.pkg.tar.* " cd "$WORK" +g++ -std=c++17 -Wall -Wextra -Werror -UNDEBUG -I pkg/arch/src/gamescope/src \ + tools/gamescope/tests/nested-swapchain.cpp -o pkg/test-nested-swapchain +pkg/test-nested-swapchain rm -rf out && mkdir -p out/usr/local/bin PKG=$(ls pkg/arch/gamescope-*.pkg.tar.* | grep -v -- '-debug-' | head -1) -tar --use-compress-program=unzstd -xf "$PKG" -C out --strip-components=2 usr/bin/gamescope +tar -xf "$PKG" -C out --strip-components=2 usr/bin/gamescope mv out/gamescope out/usr/local/bin/gamescope chmod 755 out/usr/local/bin/gamescope strip --strip-unneeded out/usr/local/bin/gamescope || true diff --git a/tools/gamescope/patches/0120-nested-swapchain-capability-fallback.patch b/tools/gamescope/patches/0120-nested-swapchain-capability-fallback.patch new file mode 100644 index 0000000000..c26c8f82bb --- /dev/null +++ b/tools/gamescope/patches/0120-nested-swapchain-capability-fallback.patch @@ -0,0 +1,358 @@ +diff --git a/src/nested_swapchain.hpp b/src/nested_swapchain.hpp +new file mode 100644 +index 0000000..06fc987 +--- /dev/null ++++ b/src/nested_swapchain.hpp +@@ -0,0 +1,53 @@ ++#pragma once ++ ++#include ++#include ++ ++namespace gamescope ++{ ++struct NestedSwapchainConfig ++{ ++ bool copy = false; ++ uint32_t imageCount = 0; ++ VkImageUsageFlags usage = 0; ++ VkCompositeAlphaFlagBitsKHR alpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; ++ VkExtent2D extent = {}; ++}; ++ ++inline bool ChooseNestedSwapchainConfig( const VkSurfaceCapabilitiesKHR &caps, ++ bool mutableFormat, uint32_t width, uint32_t height, NestedSwapchainConfig &config ) ++{ ++ constexpr VkImageUsageFlags direct = VK_IMAGE_USAGE_SAMPLED_BIT | ++ VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; ++ config.copy = !mutableFormat || ( caps.supportedUsageFlags & direct ) != direct; ++ config.usage = config.copy ? VkImageUsageFlags{VK_IMAGE_USAGE_TRANSFER_DST_BIT} : direct; ++ if ( ( caps.supportedUsageFlags & config.usage ) != config.usage ) ++ return false; ++ ++ bool foundAlpha = false; ++ for ( auto alpha : { VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR, ++ VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR, VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR } ) ++ { ++ if ( caps.supportedCompositeAlpha & alpha ) ++ { ++ config.alpha = alpha; ++ foundAlpha = true; ++ break; ++ } ++ } ++ if ( !foundAlpha || caps.maxImageExtent.width < caps.minImageExtent.width || ++ caps.maxImageExtent.height < caps.minImageExtent.height ) ++ return false; ++ ++ config.imageCount = caps.minImageCount + ( caps.minImageCount != UINT32_MAX ); ++ if ( caps.maxImageCount ) ++ config.imageCount = std::min( config.imageCount, caps.maxImageCount ); ++ if ( config.imageCount < caps.minImageCount ) ++ return false; ++ config.extent = caps.currentExtent.width == UINT32_MAX ++ ? VkExtent2D{ std::clamp( width, caps.minImageExtent.width, caps.maxImageExtent.width ), ++ std::clamp( height, caps.minImageExtent.height, caps.maxImageExtent.height ) } ++ : caps.currentExtent; ++ return config.extent.width && config.extent.height; ++} ++} +diff --git a/src/rendervulkan.cpp b/src/rendervulkan.cpp +index ffad695..ffc8e28 100644 +--- a/src/rendervulkan.cpp ++++ b/src/rendervulkan.cpp +@@ -12,6 +12,7 @@ + #include + #include + #include "vulkan_include.h" ++#include "nested_swapchain.hpp" + #include "Utils/Algorithm.h" + + #if defined(__linux__) +@@ -450,6 +451,8 @@ bool CVulkanDevice::createDevice() + bool hasDrmProps = vulkan_has_drm_props(); + bool supportsForeignQueue = false; + bool supportsHDRMetadata = false; ++ bool presentIdExtension = false; ++ bool presentWaitExtension = false; + for (const auto& ext : m_supportedExts) { + if ( strcmp(ext.extensionName, VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME) == 0 ) + m_bSupportsModifiers = true; +@@ -457,6 +460,13 @@ bool CVulkanDevice::createDevice() + if ( strcmp(ext.extensionName, VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME) == 0 ) + supportsForeignQueue = true; + ++ if ( strcmp(ext.extensionName, VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME) == 0 ) ++ m_bSupportsMutableSwapchain = true; ++ if ( strcmp(ext.extensionName, VK_KHR_PRESENT_ID_EXTENSION_NAME) == 0 ) ++ presentIdExtension = true; ++ if ( strcmp(ext.extensionName, VK_KHR_PRESENT_WAIT_EXTENSION_NAME) == 0 ) ++ presentWaitExtension = true; ++ + if ( strcmp(ext.extensionName, VK_EXT_HDR_METADATA_EXTENSION_NAME) == 0 ) + supportsHDRMetadata = true; + } +@@ -576,10 +586,24 @@ bool CVulkanDevice::createDevice() + if ( GetBackend()->UsesVulkanSwapchain() ) + { + enabledExtensions.push_back( VK_KHR_SWAPCHAIN_EXTENSION_NAME ); +- enabledExtensions.push_back( VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME ); ++ if ( m_bSupportsMutableSwapchain ) ++ enabledExtensions.push_back( VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME ); + +- enabledExtensions.push_back( VK_KHR_PRESENT_ID_EXTENSION_NAME ); +- enabledExtensions.push_back( VK_KHR_PRESENT_WAIT_EXTENSION_NAME ); ++ if ( presentIdExtension && presentWaitExtension ) ++ { ++ VkPhysicalDevicePresentWaitFeaturesKHR wait = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_WAIT_FEATURES_KHR }; ++ VkPhysicalDevicePresentIdFeaturesKHR id = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR, .pNext = &wait }; ++ VkPhysicalDeviceFeatures2 features = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, .pNext = &id }; ++ vk.GetPhysicalDeviceFeatures2( physDev(), &features ); ++ m_bSupportsPresentWait = id.presentId && wait.presentWait; ++ } ++ if ( m_bSupportsPresentWait ) ++ { ++ enabledExtensions.push_back( VK_KHR_PRESENT_ID_EXTENSION_NAME ); ++ enabledExtensions.push_back( VK_KHR_PRESENT_WAIT_EXTENSION_NAME ); ++ } ++ else ++ vk_log.infof( "Using FIFO acquire pacing; presentation timestamps unavailable" ); + + if ( supportsHDRMetadata ) + enabledExtensions.push_back( VK_EXT_HDR_METADATA_EXTENSION_NAME ); +@@ -654,7 +678,7 @@ bool CVulkanDevice::createDevice() + + VkPhysicalDeviceFeatures2 features2 = { + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, +- .pNext = &presentIdFeatures, ++ .pNext = m_bSupportsPresentWait ? static_cast( &presentIdFeatures ) : static_cast( &features13 ), + .features = { + .shaderInt16 = m_bSupportsFp16, + }, +@@ -2660,7 +2684,7 @@ bool CVulkanTexture::BInit( uint32_t width, uint32_t height, uint32_t depth, uin + return true; + } + +-bool CVulkanTexture::BInitFromSwapchain( VkImage image, uint32_t width, uint32_t height, VkFormat format ) ++bool CVulkanTexture::BInitFromSwapchain( VkImage image, uint32_t width, uint32_t height, VkFormat format, bool transferOnly ) + { + m_drmFormat = VulkanFormatToDRM( format ); + m_vkImage = image; +@@ -2672,6 +2696,12 @@ bool CVulkanTexture::BInitFromSwapchain( VkImage image, uint32_t width, uint32_t + m_contentWidth = width; + m_contentHeight = height; + m_bOutputImage = true; ++ if ( transferOnly ) ++ { ++ // A copy destination needs no views (and the swapchain need not be mutable). ++ m_bInitialized = true; ++ return true; ++ } + + VkImageViewCreateInfo createInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, +@@ -3094,7 +3124,7 @@ void vulkan_present_to_window( void ) + + VkPresentInfoKHR presentInfo = { + .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, +- .pNext = &presentIdInfo, ++ .pNext = g_device.supportsPresentWait() ? &presentIdInfo : nullptr, + .swapchainCount = 1, + .pSwapchains = &g_output.swapChain, + .pImageIndices = &g_output.nOutImage, +@@ -3102,14 +3132,22 @@ void vulkan_present_to_window( void ) + + if ( g_device.vk.QueuePresentKHR( g_device.queue(), &presentInfo ) == VK_SUCCESS ) + { +- g_currentPresentWaitId = presentId; +- g_currentPresentWaitId.notify_all(); ++ if ( g_device.supportsPresentWait() ) ++ { ++ g_currentPresentWaitId = presentId; ++ g_currentPresentWaitId.notify_all(); ++ } + } + else + vulkan_remake_swapchain(); + + while ( !acquire_next_image() ) + vulkan_remake_swapchain(); ++ if ( !g_device.supportsPresentWait() ) ++ { ++ // Estimated scheduling anchor after FIFO buffer acquisition, not presentation feedback. ++ GetVBlankTimer().MarkVBlank( get_time_in_nanos(), true ); ++ } + } + + gamescope::Rc vulkan_create_1d_lut(uint32_t size) +@@ -3219,7 +3257,19 @@ extern bool g_bOutputHDREnabled; + + bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) + { +- uint32_t imageCount = pOutput->surfaceCaps.minImageCount + 1; ++ // Re-query on resize as well as initial creation. ++ if ( g_device.vk.GetPhysicalDeviceSurfaceCapabilitiesKHR( g_device.physDev(), pOutput->surface, &pOutput->surfaceCaps ) != VK_SUCCESS ) ++ return false; ++ gamescope::NestedSwapchainConfig config; ++ if ( !gamescope::ChooseNestedSwapchainConfig( pOutput->surfaceCaps, g_device.supportsMutableSwapchain(), g_nOutputWidth, g_nOutputHeight, config ) ) ++ { ++ vk_log.errorf( "Surface supports neither direct composition nor transfer presentation" ); ++ return false; ++ } ++ pOutput->copyToSwapchain = config.copy; ++ g_nOutputWidth = config.extent.width; ++ g_nOutputHeight = config.extent.height; ++ uint32_t imageCount = config.imageCount; + uint32_t formatCount = pOutput->surfaceFormats.size(); + uint32_t surfaceFormat = formatCount; + VkColorSpaceKHR preferredColorSpace = g_bOutputHDREnabled ? VK_COLOR_SPACE_HDR10_ST2084_EXT : VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; +@@ -3254,6 +3304,13 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) + } + } + ++ if ( surfaceFormat == formatCount ) ++ { ++ for ( surfaceFormat = 0; surfaceFormat < formatCount; ++surfaceFormat ) ++ if ( pOutput->surfaceFormats[surfaceFormat].format == VK_FORMAT_R8G8B8A8_UNORM && ++ pOutput->surfaceFormats[surfaceFormat].colorSpace == preferredColorSpace ) ++ break; ++ } + if ( surfaceFormat == formatCount ) + return false; + +@@ -3276,8 +3333,8 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) + + VkSwapchainCreateInfoKHR createInfo = { + .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, +- .pNext = formats[0] != formats[1] ? &usageListInfo : nullptr, +- .flags = formats[0] != formats[1] ? VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR : (VkSwapchainCreateFlagBitsKHR )0, ++ .pNext = !config.copy && formats[0] != formats[1] ? &usageListInfo : nullptr, ++ .flags = !config.copy && formats[0] != formats[1] ? VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR : (VkSwapchainCreateFlagBitsKHR )0, + .surface = pOutput->surface, + .minImageCount = imageCount, + .imageFormat = eVkFormat, +@@ -3287,10 +3344,10 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) + .height = g_nOutputHeight, + }, + .imageArrayLayers = 1, +- .imageUsage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, ++ .imageUsage = config.usage, + .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE, + .preTransform = pOutput->surfaceCaps.currentTransform, +- .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, ++ .compositeAlpha = config.alpha, + .presentMode = VK_PRESENT_MODE_FIFO_KHR, + .clipped = VK_TRUE, + }; +@@ -3304,12 +3361,30 @@ bool vulkan_make_swapchain( VulkanOutput_t *pOutput ) + g_device.vk.GetSwapchainImagesKHR( g_device.device(), pOutput->swapChain, &imageCount, swapchainImages.data() ); + + pOutput->outputImages.resize(imageCount); ++ pOutput->presentationImages.clear(); ++ if ( config.copy ) ++ { ++ vk_log.infof( "Nested swapchain: composing offscreen and copying to FIFO presentation images" ); ++ pOutput->presentationImages.resize(imageCount); ++ } + + for ( uint32_t i = 0; i < pOutput->outputImages.size(); i++ ) + { + pOutput->outputImages[i] = new CVulkanTexture(); + +- if ( !pOutput->outputImages[i]->BInitFromSwapchain(swapchainImages[i], g_nOutputWidth, g_nOutputHeight, eVkFormat)) ++ if ( config.copy ) ++ { ++ CVulkanTexture::createFlags flags; ++ flags.bStorage = true; ++ flags.bSampled = true; ++ flags.bTransferSrc = true; ++ if ( !pOutput->outputImages[i]->BInit( g_nOutputWidth, g_nOutputHeight, 1, pOutput->uOutputFormat, flags ) ) ++ return false; ++ pOutput->presentationImages[i] = new CVulkanTexture(); ++ if ( !pOutput->presentationImages[i]->BInitFromSwapchain( swapchainImages[i], g_nOutputWidth, g_nOutputHeight, eVkFormat, true ) ) ++ return false; ++ } ++ else if ( !pOutput->outputImages[i]->BInitFromSwapchain(swapchainImages[i], g_nOutputWidth, g_nOutputHeight, eVkFormat)) + return false; + } + +@@ -3335,6 +3410,10 @@ bool vulkan_remake_swapchain( void ) + g_device.vk.QueueWaitIdle( g_device.queue() ); + + pOutput->outputImages.clear(); ++ pOutput->presentationImages.clear(); ++ if ( pOutput->acquireFence ) ++ g_device.vk.DestroyFence( g_device.device(), pOutput->acquireFence, nullptr ); ++ pOutput->acquireFence = VK_NULL_HANDLE; + + g_device.vk.DestroySwapchainKHR( g_device.device(), pOutput->swapChain, nullptr ); + +@@ -3670,7 +3749,7 @@ bool vulkan_init( VkInstance instance, VkSurfaceKHR surface ) + if (!init_nis_data()) + return false; + +- if ( GetBackend()->UsesVulkanSwapchain() ) ++ if ( GetBackend()->UsesVulkanSwapchain() && g_device.supportsPresentWait() ) + { + std::thread present_wait_thread( present_wait_thread_func ); + present_wait_thread.detach(); +@@ -4579,6 +4658,9 @@ std::optional vulkan_composite( struct FrameInfo_t *frameInfo, gamesco + CompositeTimingEnd( cmdBuffer.get(), *oTimingSlot, pszKey, pszKeyPre, pszKeyPost ); + } + ++ if ( !pOutputOverride && g_output.copyToSwapchain ) ++ cmdBuffer->copyImage( compositeImage, g_output.presentationImages[g_output.nOutImage] ); ++ + uint64_t sequence = g_device.submit(std::move(cmdBuffer)); + if ( oTimingSlot ) + g_CompositeTiming.pending.back().ulSeqNo = sequence; +diff --git a/src/rendervulkan.hpp b/src/rendervulkan.hpp +index fa519db..674a3b4 100644 +--- a/src/rendervulkan.hpp ++++ b/src/rendervulkan.hpp +@@ -162,7 +162,7 @@ public: + }; + + bool BInit( uint32_t width, uint32_t height, uint32_t depth, uint32_t drmFormat, createFlags flags, wlr_dmabuf_attributes *pDMA = nullptr, uint32_t contentWidth = 0, uint32_t contentHeight = 0, CVulkanTexture *pExistingImageToReuseMemory = nullptr, gamescope::OwningRc pBackendFb = nullptr ); +- bool BInitFromSwapchain( VkImage image, uint32_t width, uint32_t height, VkFormat format ); ++ bool BInitFromSwapchain( VkImage image, uint32_t width, uint32_t height, VkFormat format, bool transferOnly = false ); + + uint32_t IncRef(); + uint32_t DecRef(); +@@ -586,7 +586,9 @@ struct VulkanOutput_t + + std::shared_ptr swapchainHDRMetadata; + VkSwapchainKHR swapChain; +- VkFence acquireFence; ++ VkFence acquireFence = VK_NULL_HANDLE; ++ bool copyToSwapchain = false; ++ std::vector> presentationImages; + + uint32_t nOutImage; // swapchain index in nested mode, or ping/pong between two RTs + std::vector> outputImages; +@@ -756,6 +758,7 @@ static inline uint32_t div_roundup(uint32_t x, uint32_t y) + VK_FUNC(CreateDescriptorPool) \ + VK_FUNC(CreateDescriptorSetLayout) \ + VK_FUNC(CreateFence) \ ++ VK_FUNC(DestroyFence) \ + VK_FUNC(CreateGraphicsPipelines) \ + VK_FUNC(CreateImage) \ + VK_FUNC(CreateImageView) \ +@@ -862,6 +865,8 @@ public: + inline VkPipelineLayout pipelineLayout() {return m_pipelineLayout;} + inline int drmRenderFd() {return m_drmRendererFd;} + inline bool supportsModifiers() {return m_bSupportsModifiers;} ++ inline bool supportsMutableSwapchain() {return m_bSupportsMutableSwapchain;} ++ inline bool supportsPresentWait() {return m_bSupportsPresentWait;} + inline bool hasDrmPrimaryDevId() {return m_bHasDrmPrimaryDevId;} + inline dev_t primaryDevId() {return m_drmPrimaryDevId;} + inline bool supportsFp16() {return m_bSupportsFp16;} +@@ -930,6 +935,8 @@ protected: + uint32_t m_uVendorID = 0; + bool m_bHasDrmPrimaryDevId = false; + bool m_bSupportsModifiers = false; ++ bool m_bSupportsMutableSwapchain = false; ++ bool m_bSupportsPresentWait = false; + bool m_bInitialized = false; + + diff --git a/tools/gamescope/patches/0121-powervr-single-pixel-rcas.patch b/tools/gamescope/patches/0121-powervr-single-pixel-rcas.patch new file mode 100644 index 0000000000..d2b3aafeab --- /dev/null +++ b/tools/gamescope/patches/0121-powervr-single-pixel-rcas.patch @@ -0,0 +1,15 @@ +diff --git a/src/rendervulkan.cpp b/src/rendervulkan.cpp +--- a/src/rendervulkan.cpp ++++ b/src/rendervulkan.cpp +@@ -996,8 +996,9 @@ + SHADER(BLUR, cs_composite_blur); + SHADER(BLUR_COND, cs_composite_blur_cond); + SHADER(BLUR_FIRST_PASS, cs_gaussian_blur_horizontal); +- // The one pixel layout is only measured on Adreno, every other vendor keeps the quad swizzle. +- if (m_uVendorID == 0x5143) /* Qualcomm */ ++ // PowerVR rejects the quad-swizzled RCAS for 3+ layers with YCbCr mask 2. ++ // The existing one-pixel shader compiles those variants and covers the same 16x16 tile. ++ if (m_uVendorID == 0x5143 || m_uVendorID == 0x1010) /* Qualcomm or Imagination */ + { + SHADER(RCAS, cs_composite_rcas_1px); + } diff --git a/tools/gamescope/patches/0122-powervr-linear-dmabuf-import.patch b/tools/gamescope/patches/0122-powervr-linear-dmabuf-import.patch new file mode 100644 index 0000000000..40a913560c --- /dev/null +++ b/tools/gamescope/patches/0122-powervr-linear-dmabuf-import.patch @@ -0,0 +1,156 @@ +diff --git a/src/rendervulkan.cpp b/src/rendervulkan.cpp +--- a/src/rendervulkan.cpp ++++ b/src/rendervulkan.cpp +@@ -13,6 +13,7 @@ + #include + #include "vulkan_include.h" + #include "nested_swapchain.hpp" ++#include "dmabuf_import.hpp" + #include "Utils/Algorithm.h" + + #if defined(__linux__) +@@ -2229,8 +2230,27 @@ + return false; + } + +- if ( res == VK_SUCCESS && +- ( externalImageProperties.externalMemoryProperties.externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT ) ) ++ bool importable = res == VK_SUCCESS && ++ ( externalImageProperties.externalMemoryProperties.externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT ); ++ if ( gamescope::RetryPowerVRLinearQuery( g_device.vendorID(), imageInfo.format, ++ pDMA->modifier, pDMA->n_planes, imageInfo.flags, res ) ) ++ { ++ // PowerVR's mutable-format query rejects this linear import even though ++ // explicit-layout imports and UNORM/sRGB GPU sampling work. Query the base ++ // format without changing the image we actually create or its supplied stride. ++ VkImageCreateInfo baseFormat = imageInfo; ++ baseFormat.flags &= ~VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; ++ baseFormat.pNext = nullptr; ++ res = getModifierProps( &baseFormat, pDMA->modifier, &externalImageProperties ); ++ importable = res == VK_SUCCESS && ++ ( externalImageProperties.externalMemoryProperties.externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT ); ++ } ++ if ( !importable && g_device.vendorID() == 0x1010 ) ++ { ++ vk_log.errorf( "PowerVR DMA-BUF modifier is not importable; refusing a different tiling" ); ++ return false; ++ } ++ if ( importable ) + { + modifierInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT, +@@ -2348,6 +2368,22 @@ + + m_format = imageInfo.format; + ++ VkMemoryFdPropertiesKHR fdProperties = { ++ .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR, ++ }; ++ if ( pDMA ) ++ { ++ if ( !g_device.vk.GetMemoryFdPropertiesKHR ) ++ return false; ++ res = g_device.vk.GetMemoryFdPropertiesKHR( g_device.device(), ++ VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, pDMA->fd[0], &fdProperties ); ++ if ( res != VK_SUCCESS ) ++ { ++ vk_errorf( res, "vkGetMemoryFdPropertiesKHR failed" ); ++ return false; ++ } ++ } ++ + res = g_device.vk.CreateImage(g_device.device(), &imageInfo, nullptr, &m_vkImage); + if (res != VK_SUCCESS) { + vk_errorf( res, "vkCreateImage failed" ); +@@ -2357,10 +2393,22 @@ + VkMemoryRequirements memRequirements; + g_device.vk.GetImageMemoryRequirements(g_device.device(), m_vkImage, &memRequirements); + ++ uint32_t memoryType = pDMA ++ ? gamescope::DmabufMemoryType( g_device.memoryProperties(), memRequirements.memoryTypeBits, ++ fdProperties.memoryTypeBits, properties ) ++ : uint32_t( g_device.findMemoryType( properties, memRequirements.memoryTypeBits ) ); ++ if ( memoryType == UINT32_MAX ) ++ { ++ vk_log.errorf( "No compatible image memory type (image=0x%x, fd=0x%x)", ++ memRequirements.memoryTypeBits, fdProperties.memoryTypeBits ); ++ g_device.vk.DestroyImage( g_device.device(), m_vkImage, nullptr ); ++ m_vkImage = VK_NULL_HANDLE; ++ return false; ++ } + VkMemoryAllocateInfo allocInfo = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = memRequirements.size, +- .memoryTypeIndex = uint32_t(g_device.findMemoryType(properties, memRequirements.memoryTypeBits)), ++ .memoryTypeIndex = memoryType, + }; + + m_size = allocInfo.allocationSize; +@@ -2453,6 +2501,13 @@ + if ( res != VK_SUCCESS ) + { + vk_errorf( res, "vkBindImageMemory failed" ); ++ if ( pDMA ) ++ vk_log.errorf( "DMA-BUF: %ux%u format=%u modifier=0x%" PRIx64 ++ " planes=%d stride=%u offset=%u tiling=%u allocation=%" PRIu64 ++ " memoryType=%u imageBits=0x%x fdBits=0x%x", width, height, imageInfo.format, ++ pDMA->modifier, pDMA->n_planes, pDMA->stride[0], pDMA->offset[0], imageInfo.tiling, ++ uint64_t( allocInfo.allocationSize ), allocInfo.memoryTypeIndex, ++ memRequirements.memoryTypeBits, fdProperties.memoryTypeBits ); + return false; + } + +diff --git a/src/rendervulkan.hpp b/src/rendervulkan.hpp +--- a/src/rendervulkan.hpp ++++ b/src/rendervulkan.hpp +@@ -791,6 +791,7 @@ + VK_FUNC(GetImageMemoryRequirements) \ + VK_FUNC(GetImageSubresourceLayout) \ + VK_FUNC(GetMemoryFdKHR) \ ++ VK_FUNC(GetMemoryFdPropertiesKHR) \ + VK_FUNC(GetQueryPoolResults) \ + VK_FUNC(GetSemaphoreCounterValue) \ + VK_FUNC(GetSwapchainImagesKHR) \ +@@ -865,6 +866,8 @@ + inline VkPipelineLayout pipelineLayout() {return m_pipelineLayout;} + inline int drmRenderFd() {return m_drmRendererFd;} + inline bool supportsModifiers() {return m_bSupportsModifiers;} ++ inline uint32_t vendorID() const {return m_uVendorID;} ++ inline const VkPhysicalDeviceMemoryProperties &memoryProperties() const {return m_memoryProperties;} + inline bool supportsMutableSwapchain() {return m_bSupportsMutableSwapchain;} + inline bool supportsPresentWait() {return m_bSupportsPresentWait;} + inline bool hasDrmPrimaryDevId() {return m_bHasDrmPrimaryDevId;} +diff --git a/src/dmabuf_import.hpp b/src/dmabuf_import.hpp +new file mode 100644 +--- /dev/null ++++ b/src/dmabuf_import.hpp +@@ -0,0 +1,29 @@ ++#pragma once ++#include ++ ++namespace gamescope ++{ ++inline bool RetryPowerVRLinearQuery( uint32_t vendor, VkFormat format, uint64_t modifier, ++ uint32_t planes, VkImageCreateFlags flags, VkResult result ) ++{ ++ return vendor == 0x1010 && modifier == 0 && planes == 1 && ++ flags == VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT && result == VK_ERROR_FORMAT_NOT_SUPPORTED && ++ ( format == VK_FORMAT_R8G8B8A8_UNORM || format == VK_FORMAT_B8G8R8A8_UNORM ); ++} ++ ++inline uint32_t DmabufMemoryType( const VkPhysicalDeviceMemoryProperties &memory, ++ uint32_t imageBits, uint32_t fdBits, VkMemoryPropertyFlags preferred ) ++{ ++ uint32_t fallback = UINT32_MAX; ++ const uint32_t compatible = imageBits & fdBits; ++ for ( uint32_t i = 0; i < memory.memoryTypeCount; ++i ) ++ { ++ if ( !( compatible & ( 1u << i ) ) ) continue; ++ if ( fallback == UINT32_MAX ) fallback = i; ++ if ( ( memory.memoryTypes[i].propertyFlags & preferred ) == preferred ) return i; ++ } ++ // Device-local is a preference for an existing imported buffer. CPU mapping ++ // properties are requirements and must never be silently relaxed. ++ return preferred == VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT ? fallback : UINT32_MAX; ++} ++} diff --git a/tools/gamescope/patches/0123-powervr-rgba-copy-surface.patch b/tools/gamescope/patches/0123-powervr-rgba-copy-surface.patch new file mode 100644 index 0000000000..72344e6bc6 --- /dev/null +++ b/tools/gamescope/patches/0123-powervr-rgba-copy-surface.patch @@ -0,0 +1,61 @@ +diff --git a/src/nested_surface_format.hpp b/src/nested_surface_format.hpp +new file mode 100644 +--- /dev/null ++++ b/src/nested_surface_format.hpp +@@ -0,0 +1,31 @@ ++#pragma once ++ ++#include ++ ++namespace gamescope ++{ ++// The SDR composition shader declares rgba8, which requires an R8G8B8A8_UNORM ++// storage view. Binding a BGRA view is a format mismatch; a same-format copy ++// cannot correct it. Keep the workaround scoped to the PowerVR SDR copy path ++// where the channel reversal was reproduced, not game-facing texture formats. ++// On success, formatCount means the existing selector should choose the format. ++// On failure, no matching RGBA SDR surface was advertised for this scoped path. ++inline bool ChooseNestedSDRCopySurfaceFormat( uint32_t vendorId, bool copy, bool hdr, ++ const VkSurfaceFormatKHR *formats, uint32_t formatCount, uint32_t &surfaceFormat ) ++{ ++ surfaceFormat = formatCount; ++ if ( vendorId != 0x1010 || !copy || hdr ) ++ return true; ++ ++ for ( uint32_t i = 0; i < formatCount; ++i ) ++ { ++ if ( formats[i].format == VK_FORMAT_R8G8B8A8_UNORM && ++ formats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR ) ++ { ++ surfaceFormat = i; ++ return true; ++ } ++ } ++ return false; ++} ++} +diff --git a/src/rendervulkan.cpp b/src/rendervulkan.cpp +--- a/src/rendervulkan.cpp ++++ b/src/rendervulkan.cpp +@@ -14,6 +14,7 @@ + #include "vulkan_include.h" + #include "nested_swapchain.hpp" + #include "dmabuf_import.hpp" ++#include "nested_surface_format.hpp" + #include "Utils/Algorithm.h" + + #if defined(__linux__) +@@ -3330,6 +3331,13 @@ + uint32_t surfaceFormat = formatCount; + VkColorSpaceKHR preferredColorSpace = g_bOutputHDREnabled ? VK_COLOR_SPACE_HDR10_ST2084_EXT : VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; + ++ if ( !gamescope::ChooseNestedSDRCopySurfaceFormat( g_device.vendorID(), config.copy, ++ g_bOutputHDREnabled, pOutput->surfaceFormats.data(), formatCount, surfaceFormat ) ) ++ { ++ vk_log.errorf( "PowerVR copy composition requires an RGBA SDR surface" ); ++ return false; ++ } ++ + if ( surfaceFormat == formatCount ) + { + for ( surfaceFormat = 0; surfaceFormat < formatCount; surfaceFormat++ ) diff --git a/tools/gamescope/patches/0124-sdl-preserve-explicit-refresh.patch b/tools/gamescope/patches/0124-sdl-preserve-explicit-refresh.patch new file mode 100644 index 0000000000..2ec1a18d29 --- /dev/null +++ b/tools/gamescope/patches/0124-sdl-preserve-explicit-refresh.patch @@ -0,0 +1,78 @@ +diff --git a/src/sdl_refresh.hpp b/src/sdl_refresh.hpp +new file mode 100644 +--- /dev/null ++++ b/src/sdl_refresh.hpp +@@ -0,0 +1,22 @@ ++#pragma once ++ ++#include ++ ++namespace gamescope ++{ ++struct SDLRefreshChoice ++{ ++ int32_t refresh = 0; ++ bool configured = false; ++}; ++ ++// SDL reports the desktop mode again when its window is shown, moved or resized. Keep an ++// explicit -r as the rate exposed by nested Gamescope; discover the desktop rate only when the ++// caller did not configure one. ++inline constexpr SDLRefreshChoice ChooseSDLRefresh( int32_t configuredRefresh, int32_t desktopRefresh ) ++{ ++ return configuredRefresh > 0 ++ ? SDLRefreshChoice{ configuredRefresh, true } ++ : SDLRefreshChoice{ desktopRefresh, false }; ++} ++} +diff --git a/src/Backends/SDLBackend.cpp b/src/Backends/SDLBackend.cpp +--- a/src/Backends/SDLBackend.cpp ++++ b/src/Backends/SDLBackend.cpp +@@ -18,14 +18,20 @@ + #include "steamcompmgr.hpp" + #include "Utils/Defer.h" + #include "refresh_rate.h" ++#include "log.hpp" ++#include "sdl_refresh.hpp" + + #include "sdlscancodetable.hpp" + + static int g_nOldNestedRefresh = 0; + static bool g_bWindowFocused = true; ++static int g_nLastLoggedConfiguredRefresh = -1; ++static int g_nLastLoggedDesktopRefresh = -1; ++static int g_nLastLoggedEffectiveRefresh = -1; + + static int g_nOutputWidthPts = 0; + static int g_nOutputHeightPts = 0; ++static LogScope sdl_log( "sdl_backend" ); + + extern bool g_bForceHDR10OutputDebug; + extern bool steamMode; +@@ -846,9 +851,25 @@ namespace gamescope + SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 }; + + display_index = SDL_GetWindowDisplayIndex( m_Connector.GetSDLWindow() ); + if ( SDL_GetDesktopDisplayMode( display_index, &mode ) == 0 ) + { +- g_nOutputRefresh = ConvertHztomHz( mode.refresh_rate ); ++ const int nDesktopRefresh = ConvertHztomHz( mode.refresh_rate ); ++ const SDLRefreshChoice refresh = ChooseSDLRefresh( g_nOldNestedRefresh, nDesktopRefresh ); ++ g_nOutputRefresh = refresh.refresh; ++ ++ if ( g_nLastLoggedConfiguredRefresh != g_nOldNestedRefresh || ++ g_nLastLoggedDesktopRefresh != nDesktopRefresh || ++ g_nLastLoggedEffectiveRefresh != refresh.refresh ) ++ { ++ sdl_log.infof( "Refresh mode: configured=%.3fHz desktop=%.3fHz effective=%.3fHz source=%s", ++ ConvertmHzToHz( float( g_nOldNestedRefresh ) ), ++ ConvertmHzToHz( float( nDesktopRefresh ) ), ++ ConvertmHzToHz( float( refresh.refresh ) ), ++ refresh.configured ? "configured -r" : "SDL desktop" ); ++ g_nLastLoggedConfiguredRefresh = g_nOldNestedRefresh; ++ g_nLastLoggedDesktopRefresh = nDesktopRefresh; ++ g_nLastLoggedEffectiveRefresh = refresh.refresh; ++ } + } + } + break; diff --git a/tools/gamescope/patches/0125-sdl-wait-before-present.patch b/tools/gamescope/patches/0125-sdl-wait-before-present.patch new file mode 100644 index 0000000000..d8037df9c4 --- /dev/null +++ b/tools/gamescope/patches/0125-sdl-wait-before-present.patch @@ -0,0 +1,23 @@ +diff --git a/src/Backends/SDLBackend.cpp b/src/Backends/SDLBackend.cpp +--- a/src/Backends/SDLBackend.cpp ++++ b/src/Backends/SDLBackend.cpp +@@ -351,14 +351,15 @@ namespace gamescope + std::optional oCompositeResult = vulkan_composite( (FrameInfo_t *)pFrameInfo, nullptr, false ); + if ( !oCompositeResult ) + return -EINVAL; + ++ // Present has no render-finished semaphore, so wait before the native consumer can read ++ // the old contents. This serializes composition and present; a future pipelined path must ++ // signal a binary render-finished semaphore and make vkQueuePresentKHR wait on it. ++ vulkan_wait( *oCompositeResult, true ); ++ + vulkan_present_to_window(); + + // TODO: Hook up PresentationFeedback. +- +- // Wait for the composite result on our side *after* we +- // commit the buffer to the compositor to avoid a bubble. +- vulkan_wait( *oCompositeResult, true ); + + GetVBlankTimer().UpdateWasCompositing( true ); + GetVBlankTimer().UpdateLastDrawTime( get_time_in_nanos() - g_SteamCompMgrVBlankTime.ulWakeupTime ); diff --git a/tools/gamescope/release.env b/tools/gamescope/release.env index 9f895d08d6..04e103bf57 100644 --- a/tools/gamescope/release.env +++ b/tools/gamescope/release.env @@ -1,3 +1,3 @@ -GAMESCOPE_REPO=Droid-Deck/DroidDeck-Components -GAMESCOPE_TAG=gamescope-3.16.29-p9 -GAMESCOPE_SHA256=7123a37bdbcd0d76d4bd1d7490e5d2017221e036395fdd48c1e92458594ca769 +GAMESCOPE_REPO=wallentx/DroidDeck +GAMESCOPE_TAG=gamescope-3.16.29-p13-powervr +GAMESCOPE_SHA256=39e70fe9d8674da86442805d137f6bb078a83c5fac503d45b50ca66d4d3b7b04 diff --git a/tools/gamescope/tests/nested-swapchain.cpp b/tools/gamescope/tests/nested-swapchain.cpp new file mode 100644 index 0000000000..a2054aa746 --- /dev/null +++ b/tools/gamescope/tests/nested-swapchain.cpp @@ -0,0 +1,156 @@ +#include "nested_swapchain.hpp" +#include "nested_surface_format.hpp" +#include "dmabuf_import.hpp" +#include "sdl_refresh.hpp" +#include +#include + +int main() +{ + using gamescope::ChooseSDLRefresh; + constexpr int configured60 = 60'000; + constexpr int desktop120 = 120'000; + auto capped = ChooseSDLRefresh(configured60, desktop120); + assert(capped.configured && capped.refresh == configured60); + + auto discovered = ChooseSDLRefresh(0, desktop120); + assert(!discovered.configured && discovered.refresh == desktop120); + + auto configured120 = ChooseSDLRefresh(120'000, 60'000); + assert(configured120.configured && configured120.refresh == 120'000); + + VkSurfaceCapabilitiesKHR caps = {}; + caps.minImageCount = 2; + caps.maxImageCount = 3; + caps.currentExtent = {UINT32_MAX, UINT32_MAX}; + caps.minImageExtent = {16, 16}; + caps.maxImageExtent = {4096, 4096}; + caps.supportedCompositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + caps.supportedUsageFlags = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; + gamescope::NestedSwapchainConfig config; + + // Existing desktop drivers retain direct composition into mutable swapchains. + assert(gamescope::ChooseNestedSwapchainConfig(caps, true, 1280, 720, config)); + assert(!config.copy && config.imageCount == 3 && config.extent.width == 1280); + + // No mutable formats: compose offscreen and request only a copy destination. + assert(gamescope::ChooseNestedSwapchainConfig(caps, false, 1280, 720, config)); + assert(config.copy && config.usage == VK_IMAGE_USAGE_TRANSFER_DST_BIT); + + // The Android bridge exposes transfer usage and inherited alpha, but no storage. + caps.supportedUsageFlags &= ~VK_IMAGE_USAGE_STORAGE_BIT; + caps.supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; + caps.maxImageCount = 2; + caps.currentExtent = {1602, 720}; + assert(gamescope::ChooseNestedSwapchainConfig(caps, true, 1920, 1080, config)); + assert(config.copy && config.alpha == VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR); + assert(config.imageCount == 2 && config.extent.width == 1602); + + // Fail rather than submitting unsupported image usage or alpha. + caps.supportedUsageFlags &= ~VK_IMAGE_USAGE_TRANSFER_DST_BIT; + assert(!gamescope::ChooseNestedSwapchainConfig(caps, true, 1280, 720, config)); + caps.supportedUsageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; + caps.supportedCompositeAlpha = 0; + assert(!gamescope::ChooseNestedSwapchainConfig(caps, false, 1280, 720, config)); + caps.supportedCompositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; + + caps.currentExtent = {0, 0}; + assert(!gamescope::ChooseNestedSwapchainConfig(caps, false, 1280, 720, config)); + caps.currentExtent = {UINT32_MAX, UINT32_MAX}; + assert(gamescope::ChooseNestedSwapchainConfig(caps, false, 1, 9000, config)); + assert(config.extent.width == 16 && config.extent.height == 4096); + caps.maxImageCount = 1; + assert(!gamescope::ChooseNestedSwapchainConfig(caps, false, 1280, 720, config)); + + using gamescope::ChooseNestedSDRCopySurfaceFormat; + const VkSurfaceFormatKHR rgba = { + VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}; + const VkSurfaceFormatKHR bgra = { + VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}; + uint32_t surfaceFormat = UINT32_MAX; + + // PowerVR SDR copy output needs the RGBA view matching the rgba8 shader. + const VkSurfaceFormatKHR bgraFirst[] = {bgra, rgba, rgba}; + assert(ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + bgraFirst, 3, surfaceFormat)); + assert(surfaceFormat == 1); + const VkSurfaceFormatKHR rgbaFirst[] = {rgba, bgra}; + assert(ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + rgbaFirst, 2, surfaceFormat)); + assert(surfaceFormat == 0); + assert(ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + &rgba, 1, surfaceFormat)); + assert(surfaceFormat == 0); + + // Reusing a successful index must not hide a missing compatible format. + assert(!ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + &bgra, 1, surfaceFormat)); + assert(surfaceFormat == 1); + const VkSurfaceFormatKHR wrongColorSpace = { + VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT}; + assert(!ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + &wrongColorSpace, 1, surfaceFormat)); + assert(surfaceFormat == 1); + const VkSurfaceFormatKHR srgbFormat = { + VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}; + assert(!ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + &srgbFormat, 1, surfaceFormat)); + assert(surfaceFormat == 1); + const VkSurfaceFormatKHR compatibleLast[] = {wrongColorSpace, srgbFormat, rgba}; + assert(ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + compatibleLast, 3, surfaceFormat)); + assert(surfaceFormat == 2); + assert(!ChooseNestedSDRCopySurfaceFormat(0x1010, true, false, + nullptr, 0, surfaceFormat)); + assert(surfaceFormat == 0); + + // The existing selector still owns HDR, direct output, and other vendors. + // Each call clears a stale index even when BGRA is the only advertised format. + surfaceFormat = 0; + assert(ChooseNestedSDRCopySurfaceFormat(0x1010, true, true, + &bgra, 1, surfaceFormat)); + assert(surfaceFormat == 1); + surfaceFormat = 0; + assert(ChooseNestedSDRCopySurfaceFormat(0x1010, false, false, + &bgra, 1, surfaceFormat)); + assert(surfaceFormat == 1); + surfaceFormat = 0; + assert(ChooseNestedSDRCopySurfaceFormat(0x5143, true, false, + &bgra, 1, surfaceFormat)); + assert(surfaceFormat == 1); + + // Retry only the device/format/layout/flag combination reproduced on PowerVR. + using gamescope::RetryPowerVRLinearQuery; + assert(RetryPowerVRLinearQuery(0x1010, VK_FORMAT_B8G8R8A8_UNORM, 0, 1, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_ERROR_FORMAT_NOT_SUPPORTED)); + assert(RetryPowerVRLinearQuery(0x1010, VK_FORMAT_R8G8B8A8_UNORM, 0, 1, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_ERROR_FORMAT_NOT_SUPPORTED)); + assert(!RetryPowerVRLinearQuery(0x5143, VK_FORMAT_B8G8R8A8_UNORM, 0, 1, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_ERROR_FORMAT_NOT_SUPPORTED)); + assert(!RetryPowerVRLinearQuery(0x1010, VK_FORMAT_B8G8R8A8_UNORM, 1, 1, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_ERROR_FORMAT_NOT_SUPPORTED)); + assert(!RetryPowerVRLinearQuery(0x1010, VK_FORMAT_B8G8R8A8_UNORM, 0, 2, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_ERROR_FORMAT_NOT_SUPPORTED)); + assert(!RetryPowerVRLinearQuery(0x1010, VK_FORMAT_R16G16B16A16_SFLOAT, 0, 1, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_ERROR_FORMAT_NOT_SUPPORTED)); + assert(!RetryPowerVRLinearQuery(0x1010, VK_FORMAT_B8G8R8A8_UNORM, 0, 1, + 0, VK_ERROR_FORMAT_NOT_SUPPORTED)); + assert(!RetryPowerVRLinearQuery(0x1010, VK_FORMAT_B8G8R8A8_UNORM, 0, 1, + VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, VK_ERROR_DEVICE_LOST)); + + VkPhysicalDeviceMemoryProperties memory = {}; + memory.memoryTypeCount = 3; + memory.memoryTypes[0].propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; + memory.memoryTypes[1].propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + memory.memoryTypes[2].propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; + using gamescope::DmabufMemoryType; + assert(DmabufMemoryType(memory, 7, 6, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 1); + assert(DmabufMemoryType(memory, 7, 4, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 2); + assert(DmabufMemoryType(memory, 1, 6, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == UINT32_MAX); + assert(DmabufMemoryType(memory, 7, 0, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == UINT32_MAX); + assert(DmabufMemoryType(memory, 7, 4, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == UINT32_MAX); + std::puts("Gamescope presentation and DMA-BUF import tests passed"); +}