Let the submitting thread go on past the replay workers' groups, with up to four groups in flight.

With NX2NAVI_REPLAY_PIPE=1 (off by default) the submitting thread hands a group's chunks to the workers, takes chunks
of it itself until none is left to take, and goes on with the ops after the group in a new command buffer after the
chunks' places in the part (vk_sync_append_later), also into the next group's front pass.
Up to four groups are handed out and not in their part yet; their chunks wait in one queue in the order of the stream.
The submitting thread waits for them only where the order needs it: for room, before a part goes to the GPU, before
copies and texture or buffer clears, where DLAA's run or the contact shadows' hook acts, and where a lookup would make
a texture entry or take back a retired one (the op goes again afterwards, as a chunk's does in turn).

What the chunks read stays as the stream had it while the submitting thread runs ahead: uniform snapshot drops count
from their place in the stream, a chunk reads only the inline uniform updates before its group, the HDR present's
source goes by stream position, the texture pool set syncs under the chunks' lock, each group keeps its own render
conditions, occlusion segments and closed query slots, and the queries after a group's last report go into the next
segment made after it. A group's first chunk runs its pass hooks after the group before began its pass; turns wait
for every earlier chunk, of earlier groups too. Locks the chunks take, the submitting thread takes while it is behind.

Draws keep the sampled-texture descriptors they resolved per draw context and reuse them without the texture lock
while their slot is unchanged; the workers waited for that lock at nearly every draw. Texture lookups that change
nothing no longer wait for their turn. The perf line says where the submitting thread waited, what the chunks took,
and which texture lookups waited; NX2NAVI_GPU_TIMELINE also shows the chunks and the waits.

Off by default because it does not pay yet: in Hyrule Field with 3 workers it shortens the replay (11.5 against 12.1
ms a frame) but not the frame (49.2 against 49.6 fps in the same batch). The GPU gets a group's commands only once its
last chunk is recorded, so the main thread's wait for the GPU after the frame's first submission grew by 0.8 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
epicmeme546
2026-11-12 03:06:33 -05:00
committed by chrg6
parent 2ac56ecc6f
commit ffa599ef33
14 changed files with 623 additions and 165 deletions

View File

@@ -3720,6 +3720,10 @@ static void start_at(Tex *e, const TexKey *k, int taken_back) {
// returns nothing and sets this; the chunk replays the op again once every earlier chunk is done, so entries are made,
// initialised and taken back in the order of the stream, as the replay would.
static _Thread_local int tex_deferred;
static volatile uint64_t n_deferred[3]; // by what the lookup found: no entry, a retired one, a released one
void vk_texture_deferred_stats(uint64_t out[3]) {
for (int i = 0; i < 3; i++) out[i] = __atomic_exchange_n(&n_deferred[i], 0, __ATOMIC_RELAXED);
}
int vk_texture_deferred(void) { return tex_deferred; }
int vk_texture_deferred_take(void) {
int d = tex_deferred;
@@ -3727,11 +3731,26 @@ int vk_texture_deferred_take(void) {
return d;
}
// A lookup of k that finds e (NULL: no entry) and needs no turn (vk_exec_turn_due): it returns nothing without changing
// anything (the pool refresh asking for a held key or a retired entry, a creation that failed lately: the texture pool
// refresh looks its unresolved slots up at every sync), or it is the submitting thread's past a group (vk_exec_behind)
// and takes back a released entry whose image stayed its own. That keeps its content and is ordered after the
// group's chunks anyway: one of them that looks the entry up later finds it in use, as it would have after taking it
// back itself. A transient render target the frame releases and takes back at every pass that draws it did that after
// nearly every group (Hyrule Field, 2026-11-12).
static int lookup_quiet(const Tex *e, const TexKey *k) {
if (e && e->retiring) return pool_ctx;
if (e) return !e->superseded && vk_exec_behind();
if (holds.n && nvn_hm_get(&holds, k)) return pool_ctx;
return create_held(k);
}
static Tex *entry_of(const tex_t *t) {
TexKey k;
make_key(t, &k);
Tex *e = nvn_hm_get(&by_key, &k);
if ((!e || e->retiring || e->released) && vk_exec_turn_due()) {
if ((!e || e->retiring || e->released) && vk_exec_turn_due() && !lookup_quiet(e, &k)) {
__atomic_add_fetch(&n_deferred[!e ? 0 : e->retiring ? 1 : 2], 1, __ATOMIC_RELAXED);
tex_deferred = 1;
return NULL;
}