Repository navigation
simple_renderer: Destroy resources when released, after the GPU is done - #139
Merged
Merged
Conversation
Resources were kept alive by the renderer until Destroy* was called, and
then deleted at the start of the next frame:
- Vulkan deleted them before waiting for any frame fence, while frames
still in flight could be using them. Releasing a vertex buffer and
texture right after drawing with them (a temporary test hook) gave
VUID-vkDestroyBuffer-buffer-00922 and VUID-vkDestroySampler-sampler-01082
every frame.
- A resource whose owner just dropped its pointer without Destroy* (e.g.
PlayScene's fallback wall texture) stayed alive until shutdown.
Resources are now owned by their shared pointers: the pointers returned
by Create* have a deleter that queues the resource for deferred
deletion (RendererResources), and the renderer keeps no reference:
- Vulkan: a queued resource is deleted once every frame slot's fence has
been waited after its release (tracked when BeginFrame acquires an
image), so no frame that could use it is still executing. The frame
slots are the swapchain's frames in flight. Everything queued is
deleted when the swapchain is recreated (after waiting for the device
to be idle) and at shutdown.
- GLES: at the next frame with the context current, as before.
- A resource that releases others when deleted (e.g. a render state's
shader program) queues them for a later deletion.
- Live render passes are tracked weakly for the swapchain recreation
framebuffer purge.
- At shutdown, everything queued is deleted and resources still
referenced are logged. From then on (also for a release on another
thread racing the shutdown), a released resource can't be deleted
(its device or context is gone) and is leaked with an error, instead
of crashing.
GLES shutdown: the window (and surface) is usually gone by then
(APP_CMD_TERM_WINDOW comes before APP_CMD_DESTROY), and the deletes ran
without a current context ("call to OpenGL ES API with no current
context"), leaking the objects. The renderer now makes its context
current for the shutdown, without a surface if needed
(EGL_KHR_surfaceless_context; GraphicsAPIResourcesGLES provides the
display for it). A GLES resource only deletes its GL objects with the
context it was created with current, so it never deletes another
context's objects of the same name; a destroyed context took them with
it.
Vulkan frame tracking: GetSwapchainFrameResourcesVk acquiring the image
of a frame that was already acquired (and not presented) returned
without waiting for the frame fence, so a second BeginFrame would reuse
(and count as done) a frame still executing; it now fails. Frame
resources report the swapchain's frame count, which RendererVk uses
instead of assuming the display buffer mode didn't change.
Destroy* are now optional virtual no-ops in the Renderer base class,
kept for compatibility. The README and interface documentation describe
the new lifetime rules (and that UniformBuffer copies its element
array).
agdktunnel:
- TextureManager created another copy of every texture each time the
loader scene ran (on every return to the main menu): 8 more live
textures per play session. A texture that is already loaded is kept,
and the loader scene doesn't read its file again.
- TextureManager copies texture names and checks for a null name.
TextureManager and GfxManager release their resources without
Destroy* (which needed the renderer instance).
Tested on a Pixel 10 Pro with Vulkan Validation Layers 1.4.363.1 and a
temporary hook counting live Vulkan textures and buffers, over three
play/quit cycles, 6 s of creating and releasing a vertex buffer and
texture every frame during play, and quitting the app (renderer
shutdown):
- Before: the in-use VUIDs above while churning; textures 8 -> 40.
- After: no new validation messages (only the known Swappy semaphore
VUID-vkQueueSubmit-pSignalSemaphores-00067), textures stay at 8 and
buffers return to their count after each scene, nothing still
referenced or leaked at shutdown.
- GLES (forced): the same sequence runs without errors, and the
shutdown no longer calls GL without a current context.
- A host test (ASan/UBSan) covers the deletion timing per frame slot
(including a frame count change), copies keeping a resource alive,
resources released while deleting, the render pass registry, and
release after shutdown and after the renderer is gone.
- Debug and release build.
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.
Resources were kept alive by the renderer until Destroy* was called, and then deleted at the start of the next frame:
Resources are now owned by their shared pointers: the pointers returned by Create* have a deleter that queues the resource for deferred deletion (RendererResources), and the renderer keeps no reference:
GLES shutdown: the window (and surface) is usually gone by then (APP_CMD_TERM_WINDOW comes before APP_CMD_DESTROY), and the deletes ran without a current context ("call to OpenGL ES API with no current context"), leaking the objects. The renderer now makes its context current for the shutdown, without a surface if needed (EGL_KHR_surfaceless_context; GraphicsAPIResourcesGLES provides the display for it). A GLES resource only deletes its GL objects with the context it was created with current, so it never deletes another context's objects of the same name; a destroyed context took them with it.
Vulkan frame tracking: GetSwapchainFrameResourcesVk acquiring the image of a frame that was already acquired (and not presented) returned without waiting for the frame fence, so a second BeginFrame would reuse (and count as done) a frame still executing; it now fails. Frame resources report the swapchain's frame count, which RendererVk uses instead of assuming the display buffer mode didn't change.
Destroy* are now optional virtual no-ops in the Renderer base class, kept for compatibility. The README and interface documentation describe the new lifetime rules (and that UniformBuffer copies its element array).
agdktunnel: