From 49dd6639f173729fb407a53217760c955b267475 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Mon, 9 Nov 2026 21:32:45 -0500 Subject: [PATCH] Count a texture pool take only when its sync left no slot behind, so a retired texture is not freed while a set still holds its view. Once the frame's creation time is spent, pool_refill skips a slot it left earlier in the frame, and pool_sync_entries may return before the walk. Such a slot keeps its old view in the current set with nothing in the change log, so the takes after it do not carry the change into the other sets. vk_pool_takes counted those takes all the same, and at about six submissions a frame, more than VK_POOL_SETS of them could pass within one frame while a retired texture's slot was skipped every time. retire_sweep would then free the image while every set still named it. A take now counts only when its sync left no slot behind (PL.retry clear): after it, every slot change made before it is in that set and in the log, and the takes after it bring it into the others. A sealed second take inside one submission still goes uncounted, which only delays the free. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 5cd431f792e00ca3b9a03657cd4aab64fc38dae7) --- runtime/vk.h | 5 +++-- runtime/vk_exec_draw.c | 9 +++++++-- runtime/vk_texture.c | 3 ++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 8d0bd5a7..4ca82456 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -498,8 +498,9 @@ int vk_pool_marks_enabled(void); // NX2NAVI_TEX_DISUSE: modu extern volatile int32_t vk_pool_marks_missed; // modules indexing the pool that could not be marked void vk_pool_marks_read(const uint32_t *marks, uint32_t n); // vk_texture.c: slots marked since the last read // The texture pool's ring of descriptor sets (vk_exec_draw.c pool_take_set): a submission's first pool use takes the next -// set and brings it up to date. vk_pool_takes counts those takes; once it has moved on by more than VK_POOL_SETS since a -// pool entry changed, every set holds the change (vk_texture.c retire_sweep). +// set and brings it up to date. vk_pool_takes counts the takes whose sync left no slot behind (vk_exec_draw.c pool_sync); +// once it has moved on by more than VK_POOL_SETS since a pool entry changed, every set holds the change (vk_texture.c +// retire_sweep). It may count fewer takes than there were, which only delays that. #define VK_POOL_SETS 4 uint64_t vk_pool_takes(void); // Texture lock held: 1 while the texture pool refresh looks its slots up (vk_exec_draw.c pool_sync_entries), 0 after. diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 736c1182..4fd70b13 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2165,8 +2165,13 @@ static void pool_sync(VkExec *ex) { PL.catch_ns += kern_now_ns() - t1; } PL.set_synced[PL.cur] = PL.logn; - // a set taken and up to date (with one set, the refresh above wrote into it directly) - if (took) __atomic_fetch_add(&pool_takes, 1, __ATOMIC_RELEASE); + // A set taken and up to date (with one set, the refresh above wrote into it directly), counted only when this sync + // left no slot behind: once the frame's creation time is spent, pool_refill skips a slot it left earlier in the frame + // and pool_sync_entries may return before the walk, and such a slot keeps its old view in this set with nothing in + // the log, so the takes after it would not carry the change into the other sets. After a counted take, every slot + // change made before it is in this set and in the log; vk_texture.c retire_sweep frees a retired image only after + // more than VK_POOL_SETS of them, so no set of the ring still holds its view. + if (took && !PL.retry) __atomic_fetch_add(&pool_takes, 1, __ATOMIC_RELEASE); } typedef struct { VkExec *ex; uint32_t used; int n, retry, spent; } PoolDirty; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 8abe57a3..5812141f 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1161,7 +1161,8 @@ static uint64_t evict_idle(uint64_t want, uint64_t used, uint64_t budget) { // // every slot that names it gets the kind's stand-in. Any use in the meantime takes it back as it is (retire_cancel): // a lookup other than the pool refresh, or a mark on one of its slots (the stand-in was sampled). // - destroyed (retire_sweep) once the texture pool has taken every set of its ring since, each brought up to date -// when taken (vk_exec_draw.c pool_take_set, pool_catch_up), and the GPU has finished the submissions recorded until +// when taken (vk_exec_draw.c pool_take_set, pool_catch_up), after a sync that left no slot behind wrote its stand-in +// (vk_pool_takes counts only such takes), and the GPU has finished the submissions recorded until // then: no set can be bound with its view any more, and no submission that bound one is still running. Its key is // held then: a held key makes no entry in the pool refresh (held_out), so the slot keeps its stand-in; a mark on the // slot drops the hold and the next refresh makes the texture again, as does any other lookup.