mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 17:29:57 -04:00
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:
@@ -44,8 +44,9 @@ typedef struct Arena {
|
||||
} Arena;
|
||||
|
||||
typedef struct { uint64_t lo, hi; } Rng;
|
||||
typedef struct { uint64_t lo, hi, off; int dropped; } Reg;
|
||||
typedef struct { uint64_t lo, hi, fence; } Wrote;
|
||||
// dropped: 0, or the epoch of its first drop in the stream (vk_exec_epoch); epoch: of the write
|
||||
typedef struct { uint64_t lo, hi, off, dropped; } Reg;
|
||||
typedef struct { uint64_t lo, hi, fence, epoch; } Wrote;
|
||||
|
||||
static struct {
|
||||
int mode; // rt_knob: 0 off, 1 on, 2 ab
|
||||
@@ -422,7 +423,7 @@ int vk_usnap_find(uint64_t addr, uint64_t n, uint64_t align, VkDescriptorBufferI
|
||||
if (i >= U.nreg || addr < U.reg[i].lo || addr >= U.reg[i].hi) i = first_above(addr);
|
||||
if (i >= U.nreg) return 0;
|
||||
const Reg *r = &U.reg[i];
|
||||
if (r->dropped || addr < r->lo || addr + n > r->hi) return 0;
|
||||
if ((r->dropped && r->dropped <= vk_exec_epoch(0)) || addr < r->lo || addr + n > r->hi) return 0;
|
||||
uint64_t off = r->off + (addr - r->lo);
|
||||
if (off % align) return 0;
|
||||
*out = (VkDescriptorBufferInfo){U.arena->buf, off, n};
|
||||
@@ -433,15 +434,22 @@ int vk_usnap_find(uint64_t addr, uint64_t n, uint64_t align, VkDescriptorBufferI
|
||||
|
||||
static PlatMutex drop_lock = PLAT_MUTEX_INIT; // the replay workers' shaders write storage at once
|
||||
|
||||
// The replay workers (vk_exec.c NX2NAVI_REPLAY_PIPE) record a group's draws while the submitting thread goes on past
|
||||
// it: a drop holds from its place in the stream on (vk_exec_epoch), so a chunk does not see the drops of ops after its
|
||||
// group, and the submitting thread sees every one.
|
||||
void vk_usnap_drop(uint64_t addr, uint64_t n) {
|
||||
if (!U.mode || !n) return;
|
||||
int lk = vk_exec_in_chunk();
|
||||
uint64_t ep = vk_exec_epoch(1);
|
||||
int lk = vk_exec_shared();
|
||||
if (lk) plat_mutex_lock(&drop_lock);
|
||||
if (U.nwr == U.cwr) U.wr = grow(U.wr, &U.cwr, sizeof *U.wr);
|
||||
U.wr[U.nwr++] = (Wrote){addr, addr + n, U.fence};
|
||||
U.wr[U.nwr++] = (Wrote){addr, addr + n, U.fence, ep};
|
||||
if (U.on)
|
||||
for (int i = first_above(addr); i < U.nreg && U.reg[i].lo < addr + n; i++)
|
||||
if (!U.reg[i].dropped) U.reg[i].dropped = 1, U.drops++, __atomic_add_fetch(&U.gen, 1, __ATOMIC_RELAXED);
|
||||
if (!U.reg[i].dropped || ep < U.reg[i].dropped) {
|
||||
if (!U.reg[i].dropped) U.drops++;
|
||||
U.reg[i].dropped = ep, __atomic_add_fetch(&U.gen, 1, __ATOMIC_RELAXED);
|
||||
}
|
||||
if (lk) plat_mutex_unlock(&drop_lock);
|
||||
}
|
||||
|
||||
@@ -450,8 +458,9 @@ uint64_t vk_usnap_gen(void) { return U.gen; }
|
||||
// A GPU write of a submission not known to be complete touches [lo, hi). A linear scan: vk_usnap_drop appends after
|
||||
// wr_prune sorted the list, so wr_hits's binary search does not hold here; only called on rare paths.
|
||||
static int wr_overlaps(uint64_t lo, uint64_t hi) {
|
||||
uint64_t ep = vk_exec_epoch(0);
|
||||
for (int i = 0; i < U.nwr; i++)
|
||||
if (U.wr[i].lo < hi && lo < U.wr[i].hi) return 1;
|
||||
if (U.wr[i].lo < hi && lo < U.wr[i].hi && U.wr[i].epoch <= ep) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -464,7 +473,12 @@ static int cpu_written(uint64_t addr, uint64_t n) { // the same test vk_usnap_
|
||||
int vk_usnap_read(uint64_t addr, uint64_t n, void *dst) {
|
||||
const uint8_t *s = vk_usnap_bytes(addr, n);
|
||||
if (s) return memcpy(dst, s, n), 1;
|
||||
if (!n || !cpu_written(addr, n) || wr_overlaps(addr, addr + n)) return 0;
|
||||
if (!n || !cpu_written(addr, n)) return 0;
|
||||
int lk = vk_exec_shared(); // a chunk's drop may grow the list
|
||||
if (lk) plat_mutex_lock(&drop_lock);
|
||||
int w = wr_overlaps(addr, addr + n);
|
||||
if (lk) plat_mutex_unlock(&drop_lock);
|
||||
if (w) return 0;
|
||||
memcpy(dst, (const void *)(uintptr_t)addr, n);
|
||||
return 2;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user