Repository navigation
Fix swapchain recreation, window loss and resize - #136
Merged
Merged
Conversation
…r and simple_renderer Fix how the swapchain follows the window (lost, gained, resized, rotated) in base_game_framework and simple_renderer, for Vulkan and GLES: - GraphicsAPIVulkan::RecreateSwapchain sent kSwapchain_Needs_Recreation before the old swapchain was even destroyed, and recreated it with the old size and transform. It now re-queries the surface capabilities (transform, size; the display resolutions and swapchain resolution are updated), creates the new swapchain, and then notifies: the display changed callback (kDisplay_Change_Window_Resized) if the resolution changed, then kSwapchain_Needs_Recreation. The image count requested is the one InitSwapchain asked for (rather than the count the previous swapchain ended up with), within the surface's current image count limits, and the extent is kept within the surface's extent limits. A surface without a size (a zero extent) gets no swapchain until it has one. The present queue's support for a new surface is checked, rather than dereferencing an empty queue family index. - The swapchain configurations (formats, resolutions, image counts) came from the temporary surface used to query availability; they are now queried again on the real surface when the API is initialized. A second InitSwapchain without ShutdownSwapchain is rejected instead of leaking the first swapchain. - A Vulkan swapchain keeps its size when the window is resized (the image is scaled to the window, so the game rendered squashed after a resize and the display changed callback was never called on Vulkan). APP_CMD_WINDOW_RESIZED now recreates the swapchain if the surface size or transform changed (or if there is no swapchain because the surface had no size). - On window loss, the surface was destroyed while its swapchain was still alive (VUID-vkDestroySurfaceKHR-surface-01266). The swapchain is now torn down (after the device is idle, Swappy first) before the surface, and recreated when a window is gained. kSwapchain_Needs_Recreation is sent when a swapchain is destroyed this way too. - The render complete semaphore passed to vkQueuePresentKHR was per in-flight frame (2) rather than per swapchain image (5), so it could be signaled again while the presentation engine might still use it (VUID-vkQueueSubmit-pSignalSemaphores-00067). There is now one per swapchain image, selected by the acquired image index. - The per-frame fences and image available semaphores are now created and destroyed with each swapchain (sized by its in-flight frame count). A frame interrupted between acquiring an image and submitting (e.g. the swapchain is recreated mid-frame) left its fence unsignaled, deadlocking the next wait on it, and its semaphore signaled. - RendererVk::BeginFrame ignored a failed acquire (out of date swapchain), then recorded and submitted the frame with stale resources, waiting on a semaphore nothing signals. It now acquires again from the recreated swapchain, and if that fails too skips the frame: nothing is recorded or submitted until the next BeginFrame. If an image was acquired but recording can't start (vkBeginCommandBuffer fails), the frame is still submitted, without a command buffer, so its semaphores and fence are signaled. A swapchain recreated during a frame abandons the frame (its resources were destroyed with the swapchain). - RendererVk::PrepareShutdown and SwapchainRecreated now wait for the device to be idle before releasing resources and framebuffers. - RendererGLES kept the EGL surface from its creation, so after the window was lost and gained eglMakeCurrent failed every frame (EGL_BAD_SURFACE). It now gets the current context, display and surface each frame (for the swapchain passed to BeginFrame, and in SwapchainRecreated). Without a surface or current context the frame is skipped (nothing is drawn or bound), and queued deletions wait for a current context (GL deletes without one were ignored, leaking the objects). - The swapchain changed callback could run before the first frame (e.g. a resize), and agdktunnel's NativeEngine then created the renderer through Renderer::GetInstance before its API was set: a GLES renderer on a Vulkan session, crashing when Vulkan resources were created with it. NativeEngine now only notifies an existing renderer (Renderer::HasInstance), and SetRendererAPI replaces an instance created for the other API (reporting an error). - Window events reached a graphics API that wasn't active (api_ was left set after a failed or not-ready InitGraphicsAPI). GLES no longer restores a surface without an initialized swapchain or over an existing one, and doesn't keep a restored surface it couldn't make current. - DisplayManager::SetDisplayChangedCallback and SetSwapchainChangedCallback dropped callbacks set before InitGraphicsAPI. They are now kept and passed to the API when it is initialized (and cleared from it at shutdown). - DisplayManager::ShutdownSwapchain skipped a swapchain that had lost its window (which isn't "valid"), e.g. when the Activity is destroyed while backgrounded, leaving it (and GLES Swappy) to be recreated or leaked. - Renderer::ShutdownInstance dereferenced a null instance. - Swappy's device state is now released (SwappyVk_destroyDevice) before every vkDestroyDevice, for the preflight device created to query capabilities and for the real one, and the preflight swapchain's Swappy instance is released even when Swappy fails to initialize. Tested on a Pixel 10 Pro with agdktunnel, with Vulkan Validation Layers 1.4.363.1 (Vulkan) and with GLES: launch, play, Home, resume, resize the display (wm size 1080x1800) and restore it. - Vulkan: surface-01266 on window loss is gone. After a resize the swapchain is recreated at 1800x1080 and the game renders unsquashed (it was stretched before), and back at 2410x1080 after the restore. Resume renders as before. - The remaining pSignalSemaphores-00067 reports are on Swappy's own semaphores: SwappyVk_queuePresent injects a submit that waits on the app's semaphore and presents with one of its own, of which it only has 2. With Swappy's present bypassed (vkQueuePresentKHR directly, a temporary test build), the error was reported on the app's semaphores before this change and not at all after it. - GLES: eglMakeCurrent failed every frame after resume (~900 errors per phase), now none. Rendering after resume and resize as before. - Rotation: the phone flipped between the two landscape orientations repeatedly (22 VK_SUBOPTIMAL_KHR presents, each recreating the swapchain with the new transform): no validation messages, renders upright. - With temporary hooks: kSwapchain_Needs_Recreation before the first frame crashed (GLES renderer created on Vulkan, assertion in ShaderProgramGLES), now runs. The window lost and gained between acquiring and submitting a frame, every 150 frames: crashed (SIGSEGV, VUID-VkFramebufferCreateInfo-flags-02778), now runs through 29 of them with no validation errors (other than Swappy's above). - Debug and release both build. The Activity leak on recreation (finding android#22) is not fixed by the Swappy teardown: after 6 night mode toggles, 7 Activities are still alive and 12 threads were added, the same as before. A heap dump shows the old Activities retained through the Paddleboat GameControllerThread (it never quits its looper and holds an InputManager with the Activity context), agdktunnel's VibrationHelper (a JNI global reference to the SystemVibratorManager), and JNI local references to the Activity's WindowManager and Display. The threads added are one Paddleboat GameControllerThread and one AppJankTracker per recreation; Swappy's display manager threads all terminate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix how the swapchain follows the window (lost, gained, resized, rotated) in base_game_framework and simple_renderer, for Vulkan and GLES: