From a79ded6d8e7a877167afb6232cd47274b1e103e1 Mon Sep 17 00:00:00 2001 From: vii Date: Wed, 2 Dec 2026 06:17:53 -0600 Subject: [PATCH 1/2] fix(vulkan): keep one host surface per window across game joins Roblox makes a new VkSurfaceKHR for the window on every join. Since the swapchain reuse in 0.5.6 kept the old one alive, NVIDIA's Wayland driver crashed on present once a swapchain moved to the second surface. --- CHANGELOG.md | 6 +++ .../mocktail/graphics/swapchain_retirement.h | 19 +++++++++ src/graphics/bionic_vulkan_loader_adapter.cc | 26 ++++++++++++ src/graphics/swapchain_retirement.cc | 30 ++++++++++++++ tests/swapchain_retirement_test.cc | 41 +++++++++++++++++++ 5 files changed, 122 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbcba156..64c266d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Release notes on GitHub are taken from the matching section here. ## [Unreleased] +### Fixed + +- With Vulkan on NVIDIA under Wayland, joining a game no longer crashes when the window changes size + during the join. Since 0.5.6, the join left two Vulkan surfaces on the window; Nightcap now keeps one + surface for the window and gives it back to Roblox each time. + ## [0.5.6] - 2026-12-01 ### Changed diff --git a/include/mocktail/graphics/swapchain_retirement.h b/include/mocktail/graphics/swapchain_retirement.h index ee18408a..313dd12c 100644 --- a/include/mocktail/graphics/swapchain_retirement.h +++ b/include/mocktail/graphics/swapchain_retirement.h @@ -56,6 +56,25 @@ struct KeptSurface { explicit operator bool() const { return surface != VK_NULL_HANDLE; } }; +// The one host surface for the SDL window. NVIDIA's Wayland driver crashes on +// present once a second VkSurfaceKHR exists for the window, which Roblox makes +// on every game join, so later creates get this one back and the game's +// destroys keep it until its instance goes. +class SharedWindowSurface final { + public: + VkSurfaceKHR Acquire(VkInstance instance, + const VkAllocationCallbacks* allocator); + void Store(VkInstance instance, VkSurfaceKHR surface, + const VkAllocationCallbacks* allocator); + bool Holds(VkSurfaceKHR surface); + VkSurfaceKHR Release(VkInstance instance); + + private: + std::mutex mutex_; + VkInstance instance_ = VK_NULL_HANDLE; + VkSurfaceKHR surface_ = VK_NULL_HANDLE; +}; + // Keeps the driver from destroying and creating swapchains Roblox rebuilds // unchanged. NVIDIA on X11 with PRIME blocks ~22 s in each swapchain destroy or // retiring create. Roblox rebuilds an identical swapchain on every view change diff --git a/src/graphics/bionic_vulkan_loader_adapter.cc b/src/graphics/bionic_vulkan_loader_adapter.cc index 78388ee3..2a2d0b96 100644 --- a/src/graphics/bionic_vulkan_loader_adapter.cc +++ b/src/graphics/bionic_vulkan_loader_adapter.cc @@ -251,6 +251,11 @@ mocktail::graphics::SwapchainRetirement& Retirement() { return retirement; } +mocktail::graphics::SharedWindowSurface& WindowSurface() { + static mocktail::graphics::SharedWindowSurface surface; + return surface; +} + void NoteSwapchainResult(VkResult result, const char* call) { if (result != VK_ERROR_OUT_OF_DATE_KHR && result != VK_ERROR_SURFACE_LOST_KHR) { @@ -1890,6 +1895,9 @@ vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* allocator) { parent_of_kept_device = state.kept_device.Held() != VK_NULL_HANDLE && state.kept_device_instance == instance; } + // A leaked instance leaks its surface too; the next instance makes its own. + const VkSurfaceKHR window_surface = + kept ? VK_NULL_HANDLE : WindowSurface().Release(instance); if (kept) { std::fprintf(stderr, " [vulkan] keeping the host VkInstance for the next " @@ -1903,6 +1911,9 @@ vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* allocator) { " [vulkan] keeping the old Vulkan instance; destroying it " "after the new one crashes the NVIDIA driver\n"); } else if (host_destroy != nullptr) { + if (window_surface != VK_NULL_HANDLE) { + State().android_wsi.DestroySurface(instance, window_surface, nullptr); + } host_destroy(instance, allocator); } AdapterState& state = State(); @@ -1942,9 +1953,18 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateAndroidSurfaceKHR( if (!EnsureInitialized()) { return VK_ERROR_INITIALIZATION_FAILED; } + if (const VkSurfaceKHR shared = WindowSurface().Acquire(instance, allocator); + shared != VK_NULL_HANDLE && surface != nullptr) { + *surface = shared; + std::fprintf(stderr, + " [vulkan] reusing the window's host surface for the new " + "Android surface\n"); + return VK_SUCCESS; + } const auto result = State().android_wsi.CreateAndroidSurface( instance, create_info, allocator, surface); if (result == VK_SUCCESS) { + WindowSurface().Store(instance, *surface, allocator); std::fprintf(stderr, " [vulkan] Android surface mapped to SDL WSI\n"); } return static_cast(result); @@ -1956,6 +1976,12 @@ vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface, if (!EnsureInitialized()) { return; } + if (WindowSurface().Holds(surface)) { + std::fprintf(stderr, + " [vulkan] keeping the window's host surface for the next " + "Android surface\n"); + return; + } if (Retirement().SurfaceDestroyed(surface, instance, allocator)) { std::fprintf(stderr, " [vulkan] keeping the old surface until its swapchain is " diff --git a/src/graphics/swapchain_retirement.cc b/src/graphics/swapchain_retirement.cc index c5a53241..cf174860 100644 --- a/src/graphics/swapchain_retirement.cc +++ b/src/graphics/swapchain_retirement.cc @@ -35,6 +35,36 @@ bool SwapchainShape::operator==(const SwapchainShape& other) const { present_mode == other.present_mode && clipped == other.clipped; } +VkSurfaceKHR SharedWindowSurface::Acquire( + VkInstance instance, const VkAllocationCallbacks* allocator) { + std::lock_guard lock(mutex_); + return allocator == nullptr && instance == instance_ ? surface_ + : VK_NULL_HANDLE; +} + +void SharedWindowSurface::Store(VkInstance instance, VkSurfaceKHR surface, + const VkAllocationCallbacks* allocator) { + std::lock_guard lock(mutex_); + if (allocator == nullptr && surface_ == VK_NULL_HANDLE) { + instance_ = instance; + surface_ = surface; + } +} + +bool SharedWindowSurface::Holds(VkSurfaceKHR surface) { + std::lock_guard lock(mutex_); + return surface != VK_NULL_HANDLE && surface == surface_; +} + +VkSurfaceKHR SharedWindowSurface::Release(VkInstance instance) { + std::lock_guard lock(mutex_); + if (instance != instance_) { + return VK_NULL_HANDLE; + } + instance_ = VK_NULL_HANDLE; + return std::exchange(surface_, VK_NULL_HANDLE); +} + void SwapchainRetirement::Created(VkSwapchainKHR swapchain, VkSurfaceKHR surface, VkSwapchainKHR old_swapchain, diff --git a/tests/swapchain_retirement_test.cc b/tests/swapchain_retirement_test.cc index d45a92c6..58a7894f 100644 --- a/tests/swapchain_retirement_test.cc +++ b/tests/swapchain_retirement_test.cc @@ -9,6 +9,7 @@ namespace { using mocktail::graphics::HeldSwapchain; using mocktail::graphics::KeptSurface; +using mocktail::graphics::SharedWindowSurface; using mocktail::graphics::SwapchainRetirement; using mocktail::graphics::SwapchainShape; @@ -179,6 +180,46 @@ TEST(SurfaceLifetimeTest, ASurfaceWaitsForEverySwapchainOnIt) { EXPECT_EQ(retirement.SwapchainGone(kSurface).surface, kSurface); } +// NVIDIA's Wayland driver crashes on present once a second VkSurfaceKHR exists +// for the window, so every game surface is the one host surface. +TEST(SharedWindowSurfaceTest, EveryCreateGetsTheFirstSurfaceBack) { + SharedWindowSurface shared; + EXPECT_EQ(shared.Acquire(kInstance, nullptr), VK_NULL_HANDLE); + shared.Store(kInstance, kSurface, nullptr); + EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface); + EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface); +} + +TEST(SharedWindowSurfaceTest, TheGamesDestroyKeepsIt) { + SharedWindowSurface shared; + shared.Store(kInstance, kSurface, nullptr); + EXPECT_TRUE(shared.Holds(kSurface)); + EXPECT_FALSE(shared.Holds(kOtherSurface)); + EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface); +} + +TEST(SharedWindowSurfaceTest, ItGoesWithItsInstance) { + SharedWindowSurface shared; + shared.Store(kInstance, kSurface, nullptr); + EXPECT_EQ(shared.Release(Fake(0x41)), VK_NULL_HANDLE); + EXPECT_EQ(shared.Release(kInstance), kSurface); + EXPECT_FALSE(shared.Holds(kSurface)); + EXPECT_EQ(shared.Acquire(kInstance, nullptr), VK_NULL_HANDLE); +} + +TEST(SharedWindowSurfaceTest, OnlyOneInstanceAndNoCustomAllocator) { + SharedWindowSurface shared; + VkAllocationCallbacks callbacks{}; + shared.Store(kInstance, kSurface, &callbacks); + EXPECT_EQ(shared.Acquire(kInstance, nullptr), VK_NULL_HANDLE); + + shared.Store(kInstance, kSurface, nullptr); + shared.Store(Fake(0x41), kOtherSurface, nullptr); + EXPECT_EQ(shared.Acquire(Fake(0x41), nullptr), VK_NULL_HANDLE); + EXPECT_EQ(shared.Acquire(kInstance, &callbacks), VK_NULL_HANDLE); + EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface); +} + TEST(SwapchainShapeTest, ChainedOrSharedCreatesAreNeverReused) { VkSwapchainCreateInfoKHR info{}; info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; From c74a23ef76bb89718bedc49448121ce274b336b3 Mon Sep 17 00:00:00 2001 From: vii Date: Wed, 2 Dec 2026 06:17:54 -0600 Subject: [PATCH 2/2] fix(runtime): give a resize during a join to the game, not the home screen While StartGame runs, Roblox is already in the game stage, so a LuaApp surface update restarted the home screen and abandoned the join. The surface is now held until the launch finishes and then given to the game. --- CHANGELOG.md | 2 + .../runtime/roblox_experience_composition.h | 7 ++ src/runtime/roblox_experience_composition.cc | 39 ++++++++++ tests/roblox_experience_composition_test.cc | 73 +++++++++++++++++++ 4 files changed, 121 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64c266d7..fd9966cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ Release notes on GitHub are taken from the matching section here. - With Vulkan on NVIDIA under Wayland, joining a game no longer crashes when the window changes size during the join. Since 0.5.6, the join left two Vulkan surfaces on the window; Nightcap now keeps one surface for the window and gives it back to Roblox each time. +- Resizing the window while a game loads no longer sends you back to the home screen. The new size now + goes to the game once it has started. ## [0.5.6] - 2026-12-01 diff --git a/include/runtime/roblox_experience_composition.h b/include/runtime/roblox_experience_composition.h index ba411061..42899ea8 100644 --- a/include/runtime/roblox_experience_composition.h +++ b/include/runtime/roblox_experience_composition.h @@ -229,6 +229,10 @@ class RobloxExperienceComposition final { jobject surface_object, jobject platform_params, jobject activity, jobject* start_app_params); Status RebindLuaAppSurface(const GameSurface& surface); + void ApplySurfaceChangedDuringLaunch( + const GameSurface& surface, uint64_t launch_generation, + bool game_started, + const std::shared_ptr& controller); Status RestartLuaAppSurface(const GameSurface& surface); Status ReleaseLuaAppGlobalObjects(); Status ReleaseGlobalObjects(); @@ -297,6 +301,9 @@ class RobloxExperienceComposition final { bool lua_app_return_pending_ = false; bool controlled_switch_waiting_for_return_ = false; bool lua_app_surface_recreation_pending_ = false; + // Roblox is already in the game stage while a launch runs, so a LuaApp + // surface update then abandons the join; the game gets it once started. + std::optional surface_changed_during_launch_; bool late_surface_tracking_ = false; GameSurface late_surface_snapshot_; jnivm::VM* late_lifecycle_vm_ = nullptr; diff --git a/src/runtime/roblox_experience_composition.cc b/src/runtime/roblox_experience_composition.cc index d44eacaf..cd12e8fd 100644 --- a/src/runtime/roblox_experience_composition.cc +++ b/src/runtime/roblox_experience_composition.cc @@ -1404,6 +1404,9 @@ void RobloxExperienceComposition::NotifyLuaAppDidReturn() { Status RobloxExperienceComposition::DrainLaunchRequests() { Status completed_result = Status::Ok(); if (launch_worker_.joinable()) { + std::optional deferred_surface; + uint64_t launch_generation = 0; + std::shared_ptr launch_controller; const OwnedPthreadWaitResult wait = launch_worker_.WaitFor(0, 1); if (wait.status == OwnedPthreadWaitStatus::kTimedOut) return Status::Ok(); @@ -1417,6 +1420,10 @@ Status RobloxExperienceComposition::DrainLaunchRequests() { launch_in_progress_ = false; if (active_launch_ != nullptr) { completed_result = active_launch_->result; + launch_generation = active_launch_->context.game_surface.generation; + deferred_surface = std::exchange(surface_changed_during_launch_, + std::nullopt); + launch_controller = controller_; game_active_ = completed_result.ok(); window::SetGameSessionActive(game_active_); active_game_canonical_json_ = @@ -1430,6 +1437,11 @@ Status RobloxExperienceComposition::DrainLaunchRequests() { playing_presence_published_ = false; } } + if (deferred_surface.has_value()) { + ApplySurfaceChangedDuringLaunch(*deferred_surface, launch_generation, + completed_result.ok(), + launch_controller); + } if (!completed_result.ok()) { NotifyPresence(RobloxExperiencePresencePhase::kBrowsing, nullptr); return completed_result; @@ -1678,6 +1690,11 @@ GameSessionUpdateResult RobloxExperienceComposition::SurfaceChanged( std::lock_guard lock(mutex_); controller = controller_; forward = game_active_; + if (!forward && (launch_in_progress_ || active_launch_ != nullptr)) { + readiness_.surface = surface; + surface_changed_during_launch_ = surface; + return AcceptedLuaAppSurface("surface kept for the game being launched"); + } recreate_lua_app = !forward && lua_app_surface_recreation_pending_; } GameSessionUpdateResult result = @@ -1701,6 +1718,28 @@ GameSessionUpdateResult RobloxExperienceComposition::SurfaceChanged( return result; } +void RobloxExperienceComposition::ApplySurfaceChangedDuringLaunch( + const GameSurface& surface, uint64_t launch_generation, bool game_started, + const std::shared_ptr& controller) { + std::lock_guard operation_lock(surface_operation_mutex_); + if (!game_started) { + (void)RebindLuaAppSurface(surface); + return; + } + if (controller == nullptr) { + return; + } + if (surface.generation != launch_generation) { + (void)controller->SurfaceCreated(surface.generation); + } + const GameSessionUpdateResult result = controller->RebindSurface(surface); + std::fprintf(stderr, + " [surface] game got the surface from its launch generation=%llu " + "pixels=%ux%u ok=%d\n", + static_cast(surface.generation), + surface.width, surface.height, result.ok() ? 1 : 0); +} + GameSessionUpdateResult RobloxExperienceComposition::SurfaceDestroyed( uint64_t generation) { std::lock_guard operation_lock(surface_operation_mutex_); diff --git a/tests/roblox_experience_composition_test.cc b/tests/roblox_experience_composition_test.cc index ce24004e..3658dfc4 100644 --- a/tests/roblox_experience_composition_test.cc +++ b/tests/roblox_experience_composition_test.cc @@ -1112,6 +1112,79 @@ TEST(RobloxExperienceCompositionTest, g_probe = nullptr; } +// Roblox is in the UGCGame stage during StartGame; a LuaApp surface update +// then restarts the LuaApp and abandons the join. +TEST(RobloxExperienceCompositionTest, + ResizeDuringLaunchGoesToTheGameNotTheLuaApp) { + jnivm::VM vm; + Probe probe{&vm}; + probe.publish_during_subscribe = true; + probe.block_start.store(true); + g_probe = &probe; + for (const char* class_name : { + "com/roblox/engine/jni/NativeGLInterface", + "android/view/Surface", + "com/roblox/engine/jni/model/PlatformParams", + "android/app/Activity", + "com/roblox/engine/jni/autovalue/AutoValue_StartGameParams", + "com/roblox/engine/jni/autovalue/StartAppParams", + "com/roblox/universalapp/messagebus/MessageBus", + "com/roblox/universalapp/messagebus/Connection", + "com/roblox/engine/jni/memstorage/MemStorage", + "com/roblox/engine/jni/memstorage/Connection", + "com/roblox/engine/jni/memstorage/Callback", + }) { + vm.RegisterClass(class_name); + } + RobloxExperienceComposition composition( + {vm.GetJavaVM(), &vm, Prepare}, {GetLaunchId, Subscribe, Disconnect}, + {GetWebViewOpenId, GetWebViewHandleWindowCloseId, GetWebViewProtocolName, + GetWebViewAvailabilityId, GetWebViewMessageId, InitializeWebViewProtocol, + Subscribe, Disconnect, SetRequestHandler, ClearNativeRequestHandler, + PublishRaw, BroadcastDataModelFocus, GetWebViewMutateId, + GetWebViewCloseId, SignalWebViewJavascriptCallback, + UpdateCookieSetHandler}, + BrowserServiceSymbols(), PermissionsSymbols(), + {Foreground, Start, Update, PauseGame, ResumeGame, Leave, PauseApp, + DestroyApp, UpdateApp, StartApp, CallMessagesFromMainThread}, + JniFactory(&probe), {&probe, RegisterObserver, ClearObserver}, {}, + nullptr, {}, {&probe, ObservePresence}); + RobloxLuaAppExperienceReadiness readiness; + readiness.principal = {GameSessionPrincipalKind::kAuthenticated, 4, "42", + "https://www.roblox.com"}; + readiness.surface = {7, 0x1234, 1280, 720}; + readiness.username = "typed-user"; + + ASSERT_TRUE(composition.InitializePlatformProtocols().ok()); + ASSERT_TRUE(composition.OnLuaAppReady(readiness).ok()); + ASSERT_TRUE(composition.DrainLaunchRequests().ok()); + for (int attempt = 0; attempt < 1000 && !probe.start_entered.load(); + ++attempt) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + ASSERT_TRUE(probe.start_entered.load()); + const int app_updates = probe.app_updates; + const int app_starts = probe.app_starts; + const int game_updates = probe.updates; + + EXPECT_TRUE(composition.SurfaceChanged({7, 0x1234, 1400, 872}).ok()); + EXPECT_EQ(probe.app_updates, app_updates); + EXPECT_EQ(probe.app_starts, app_starts); + + probe.release_start.store(true); + DrainUntilPresented(&composition); + for (int attempt = 0; attempt < 1000 && probe.updates == game_updates; + ++attempt) { + ASSERT_TRUE(composition.DrainLaunchRequests().ok()); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + EXPECT_EQ(probe.app_updates, app_updates); + EXPECT_EQ(probe.app_starts, app_starts); + EXPECT_GT(probe.updates, game_updates); + EXPECT_TRUE(composition.Shutdown().ok()); + g_probe = nullptr; +} + TEST(RobloxExperienceCompositionTest, RejectsIncompleteReadinessBeforeSubscribe) { jnivm::VM vm;