nvnCommandBufferFenceSync is implemented: the sync waits for the fence of the submit that carries the record (#1388)

Zonai Fieldworks' native-font renderer (Cryonis in its BotW Abilities member) records it, and the weak stub ended the
game with "NVN nvnCommandBufferFenceSync is not implemented". The record is a new op, OP_FENCE_SYNC, that every replay
skips; at QueueSubmitCommands the syncs named in the submitted handles get that submit's fence value, which completes
no earlier than the console's point. The game itself never records the op, so its submits pay one flag load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-25 21:23:19 +03:00
1 parent 4fccaa5bef
commit fc2863a15c
7 files changed
+71 -9

No files matched your search

+7
View File
@@ -72,6 +72,10 @@ void nvn_submit_prepare(const uint64_t *handles, int n); // weak no-op; the co
// Asynchronous replay: as nvn_submit_prepare, writing the handles to replay to out (a clone where a still queued
// submission uses the same recording with other deferred binds)
void nvn_submit_prepare_async(const uint64_t *handles, uint64_t *out, int n);
// After the back end took a submission as fence value `fence`: every sync an OP_FENCE_SYNC of the game's handles names
// is fenced at that value. Returns at once while no recording ever held the op (the game records none; mod plugins do).
void nvn_submit_fence_syncs(const uint64_t *handles, int n, uint64_t fence);
void nvn_sync_fence(sync_t *s, uint64_t fence); // nvn_api_core.c: an initialized sync now waits for fence
int gfx_replay_async(void); // 1: the back end replays submissions on its own thread
uint64_t gfx_replayed_submits(void); // newest submit number (nvn_submit_count) replayed
#define NVN_HANDLE_TEX(h) ((int)((h) & 0xfffff)) // combined texture handle = texture id | sampler id << 20
@@ -110,6 +114,9 @@ typedef struct { int32_t prim, itype; uint64_t ibuf, ind; } p_draw_idx_ind;
typedef struct { uint64_t ind, caller; } p_dispatch_ind; // OP_DISPATCH_IND: ind = GPU address of the 3 group counts
// OP_CLEAR_BUF: size bytes at addr filled with the 32-bit value in GPU order (ClearBuffer; ReportValue is a 4-byte one)
typedef struct { uint64_t addr, size; uint32_t value, pad; } p_clear_buf;
// OP_FENCE_SYNC: CommandBufferFenceSync. The front end, not the replay, signals the sync: at the submit that carries
// the record it gets that submit's fence value (nvn_submit_fence_syncs). Every replay skips the record.
typedef struct { sync_t *sync; } p_fence_sync;
_Static_assert(sizeof(p_signal_event) == 16 && sizeof(p_report_counter) == 16 && sizeof(p_copy_b2b) == 24 &&
sizeof(p_update_ub) == 16, "op payload layouts of hle_nvn.c");
static inline const op_hdr *nvn_op_first(uint64_t handle) { return (const op_hdr *)(uintptr_t)handle; } // OP_BEGIN
+31 -4
View File
@@ -414,8 +414,9 @@ void nvn_pins_stats(uint64_t *live, uint64_t *freed) {
// deferred: 0 unknown, 1 none, 2 some; pins: texture pins its ops name, counted in their refs; pin_v: those pins, in
// record order, or NULL (then pin_refs walks the records); cap: bytes of the block from the large-block cache
// (copy_block), 0 for a plain heap block; hw: the recording's command memory as the real driver counts it (set at
// EndRecording), what CopyCommands of it adds to nvnCommandBufferGetCommandMemoryUsed (inline_recording)
typedef struct { uint64_t magic, bytes, made_at, used_at, deferred, pins, cap, hw; const tex_t **pin_v; } copy_prefix;
// EndRecording), what CopyCommands of it adds to nvnCommandBufferGetCommandMemoryUsed (inline_recording); fsync: 1 when
// the records hold an OP_FENCE_SYNC (flatten_recording), which nvn_submit_fence_syncs signals at submit
typedef struct { uint64_t magic, bytes, made_at, used_at, deferred, pins, cap, hw, fsync; const tex_t **pin_v; } copy_prefix;
_Static_assert(sizeof(copy_prefix) % 8 == 0, "records stay 8-byte aligned");
typedef struct { uintptr_t lo, hi; uint8_t *copy; } cmd_copy;
typedef struct { cmd_copy *v; uint32_t n, cap; } copy_bucket;
@@ -423,6 +424,7 @@ static PlatMutex copy_lock = PLAT_MUTEX_INIT; // guards the two maps, the cou
static nvn_hmap copy_pages = {.klen = 8}; // page -> copy_bucket
static nvn_hmap copy_live = {.klen = 8}; // handle -> copy_prefix of a live copy
static uint64_t ncopies, copy_bytes, copies_made;
static int fence_sync_recorded; // 1 once nvnCommandBufferFenceSync recorded an op (never set by the game itself)
// Asynchronous replay: copies released by the game, and per-submission clones, that a queued submission still reads.
// Freed once gfx_replayed_submits() reaches their used_at. Empty with synchronous replay.
static copy_prefix **zombies;
@@ -598,6 +600,8 @@ static uint8_t *flatten_recording(const uint8_t *p, size_t n) {
static __thread struct { const tex_t **v; uint64_t cap; } named; // the pins of this recording, reused
uint64_t pins_named = 0;
int listed = 1; // 0 after a failed growth: the copy goes without a list
// Looked for only once some recording held the op (fence_sync_recorded): the game's own recordings never do.
int find_fsync = __atomic_load_n(&fence_sync_recorded, __ATOMIC_RELAXED), fsync = 0;
for (size_t at = 0;;) { // the records stay checked: a corrupt one stops here, not in the replay
op_hdr h;
memcpy(&h, p + at, sizeof h);
@@ -616,6 +620,7 @@ static uint8_t *flatten_recording(const uint8_t *p, size_t n) {
}
if (k && listed) memcpy((void *)(named.v + pins_named), pinned, sizeof *pinned * (size_t)k);
pins_named += (uint64_t)k;
fsync |= find_fsync && h.op == OP_FENCE_SYNC;
at += h.size;
if (h.op == OP_END) {
if (at != n) rt_fatal("nvn: recording at %p ends at %zu of %zu bytes", (const void *)p, at, n);
@@ -625,7 +630,8 @@ static uint8_t *flatten_recording(const uint8_t *p, size_t n) {
const tex_t **pin_v = NULL;
if (pins_named && listed && (pin_v = malloc(sizeof *pin_v * pins_named)))
memcpy((void *)pin_v, (const void *)named.v, sizeof *pin_v * pins_named);
*block = (copy_prefix){.magic = COPY_MAGIC, .bytes = n, .pins = pins_named, .cap = block->cap, .pin_v = pin_v};
*block = (copy_prefix){.magic = COPY_MAGIC, .bytes = n, .pins = pins_named, .cap = block->cap, .fsync = (uint64_t)fsync,
.pin_v = pin_v};
return out + sizeof(copy_prefix);
}
@@ -1260,6 +1266,15 @@ NVN_API(nvnCommandBufferWaitEvent) { (void)c; }
NVN_API(nvnCommandBufferSetShaderScratchMemory) { (void)c; }
NVN_API(nvnCommandBufferSetPatchSize) { (void)c; }
// void nvnCommandBufferFenceSync(NVNcommandBuffer *cmd, NVNsync *sync, NVNsyncCondition condition, int flags): the sync
// is signaled once the GPU ran every command before it. Here it is fenced at the value of the whole submit that carries
// the record (nvn_submit_fence_syncs), which completes no earlier than the console's point. The game never calls it;
// mod plugins do (Zonai Fieldworks' native-font renderer).
NVN_API(nvnCommandBufferFenceSync) {
__atomic_store_n(&fence_sync_recorded, 1, __ATOMIC_RELAXED);
REC(cmd_of(c), OP_FENCE_SYNC, p_fence_sync)->sync = arg_p(c, 1);
}
// ---------- submit
// What the console reads at the submit call is read here, on the game thread: every handle must be a live recording,
// and deferred binds take the handles the game's arrays hold now (the game fills its tables for the next frame as soon
@@ -1353,6 +1368,18 @@ void nvn_submit_prepare_async(const uint64_t *handles, uint64_t *out, int n) {
(unsigned long long)now, (unsigned long long)__atomic_load_n(&n_clones, __ATOMIC_RELAXED), nzombies);
}
// The game's handles, not the per-submission clones of nvn_submit_prepare_async: a clone may be freed as soon as the
// replay is done with it, and the handles stay live while the game is in QueueSubmitCommands. Copies hold no OP_CALL
// or OP_JUMP (flatten_recording), so each is one run of records to OP_END.
void nvn_submit_fence_syncs(const uint64_t *handles, int n, uint64_t fence) {
if (!__atomic_load_n(&fence_sync_recorded, __ATOMIC_RELAXED)) return;
for (int i = 0; i < n; i++) {
if (!((const copy_prefix *)(uintptr_t)handles[i] - 1)->fsync) continue;
for (const op_hdr *h = nvn_op_first(handles[i]); h->op != OP_END; h = nvn_op_next(h))
if (h->op == OP_FENCE_SYNC) nvn_sync_fence(((const p_fence_sync *)nvn_op_payload(h))->sync, fence);
}
}
uint64_t nvn_submit_count(void) { return __atomic_load_n(&nsubmits, __ATOMIC_RELAXED); }
const char *nvn_op_name(uint32_t op) {
@@ -1362,7 +1389,7 @@ const char *nvn_op_name(uint32_t op) {
"BIND_VSTREAM", "BIND_VBUF", "BIND_UBO", "BIND_SSBO", "BIND_TEX", "BIND_SAMP", "STENCIL", "POLY_OFFSET", "DRAW",
"DRAW_IDX", "DISPATCH", "BARRIER", "SET_TEX_POOL", "SET_SAMP_POOL", "COPY_B2T", "COPY_STRIDE", "BLEND_COLOR",
"COPY_T2B", "BIND_IMG", "COPY_T2T", "RESET_COUNTER", "CLEAR_TEX", "DEPTH_CLAMP", "RENDER_ENABLE",
"RASTER_DISCARD", "RENDER_COND", "PRIM_RESTART", "DRAW_IDX_IND", "DISPATCH_IND", "CLEAR_BUF",
"RASTER_DISCARD", "RENDER_COND", "PRIM_RESTART", "DRAW_IDX_IND", "DISPATCH_IND", "CLEAR_BUF", "FENCE_SYNC",
};
_Static_assert(sizeof names / sizeof *names == OP_COUNT_, "one name per op");
return op < OP_COUNT_ ? names[op] : "?";
+10 -1
View File
@@ -463,7 +463,8 @@ NVN_API(nvnBufferFlushMappedRange) {
}
// ---------- Sync and events
// A sync is a fence value: fenced = 1 after QueueFenceSync (the newest submit), 3 after WindowAcquireTexture (fence 0).
// A sync is a fence value: fenced = 1 after QueueFenceSync (the newest submit) or the submit of a CommandBufferFenceSync
// record (that submit), 3 after WindowAcquireTexture (fence 0).
// NVNboolean nvnSyncInitialize(NVNsync *s, NVNdevice *d)
NVN_API(nvnSyncInitialize) {
*(sync_t *)arg_p(c, 0) = (sync_t){SYNC_MAGIC, 0, 0};
@@ -478,6 +479,13 @@ NVN_API(nvnQueueFenceSync) {
s->fenced = 1;
s->fence = nvn_fence_last();
}
// CommandBufferFenceSync at its submit (nvn_api_cmd.c nvn_submit_fence_syncs): the value first, then fenced, so a
// thread that sees fenced waits for this value or a newer one. A sync finalized since the record is left alone.
void nvn_sync_fence(sync_t *s, uint64_t fence) {
if (!s || s->magic != SYNC_MAGIC) return;
__atomic_store_n(&s->fence, fence, __ATOMIC_RELAXED);
__atomic_store_n(&s->fenced, 1, __ATOMIC_RELEASE);
}
// TOTK_SYNC_AHEAD=1: one more frame in flight. Each frame the game waits (one nvnSyncWait, frame_wait_lr) for
// the frame it presented last; on the Switch that work is long done by then, here it is still in the replay and on the
// GPU (about 11 ms from the submit), and the wait sets the frame time. With the switch on, that wait, and only that
@@ -720,6 +728,7 @@ NVN_API(nvnQueueSubmitCommands) {
nvn_submit_prepare(h, n);
f = gfx_backend()->submit(h, n);
}
nvn_submit_fence_syncs(h, n, f); // the syncs this submit's CommandBufferFenceSync records name wait for f
uint64_t prev = __atomic_load_n(&last_fence, __ATOMIC_RELAXED);
while (f > prev && !__atomic_compare_exchange_n(&last_fence, &prev, f, 1, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {}
__atomic_store_n(&lod_fence[no % LOD_RING], f, __ATOMIC_RELAXED);
+1 -1
View File
@@ -128,7 +128,7 @@ enum {
OP_BIND_PROGRAM, OP_BIND_STATE, OP_BIND_VATTR, OP_BIND_VSTREAM, OP_BIND_VBUF,
OP_BIND_UBO, OP_BIND_SSBO, OP_BIND_TEX, OP_BIND_SAMP,
OP_STENCIL, OP_POLY_OFFSET, OP_DRAW, OP_DRAW_IDX, OP_DISPATCH, OP_BARRIER,
OP_SET_TEX_POOL, OP_SET_SAMP_POOL, OP_COPY_B2T, OP_COPY_STRIDE, OP_BLEND_COLOR, OP_COPY_T2B, OP_BIND_IMG, OP_COPY_T2T, OP_RESET_COUNTER, OP_CLEAR_TEX, OP_DEPTH_CLAMP, OP_RENDER_ENABLE, OP_RASTER_DISCARD, OP_RENDER_COND, OP_PRIM_RESTART, OP_DRAW_IDX_IND, OP_DISPATCH_IND, OP_CLEAR_BUF, OP_COUNT_
OP_SET_TEX_POOL, OP_SET_SAMP_POOL, OP_COPY_B2T, OP_COPY_STRIDE, OP_BLEND_COLOR, OP_COPY_T2B, OP_BIND_IMG, OP_COPY_T2T, OP_RESET_COUNTER, OP_CLEAR_TEX, OP_DEPTH_CLAMP, OP_RENDER_ENABLE, OP_RASTER_DISCARD, OP_RENDER_COND, OP_PRIM_RESTART, OP_DRAW_IDX_IND, OP_DISPATCH_IND, OP_CLEAR_BUF, OP_FENCE_SYNC, OP_COUNT_
};
// Payload (immediately follows op_hdr)
typedef struct { int32_t n; const tex_t *color[8]; view_t cview[8]; uint8_t has_cview[8]; const tex_t *depth; view_t dview; uint8_t has_dview; } p_set_rt;
+15 -1
View File
@@ -319,6 +319,7 @@ static void probe_samplers(void) {
// ---- sync and events
OBJ(NVNsync, syncs);
OBJ(NVNsync, sync_future);
OBJ(NVNsync, sync_cmd); // CommandBufferFenceSync's sync (record_all, probe_null_effects)
OBJ(NVNevent, ev);
OBJ(NVNevent, ev2);
OBJ(NVNeventBuilder, evb);
@@ -326,6 +327,7 @@ OBJ(NVNeventBuilder, evb);
static void probe_sync_events(void) {
CHECK((uint8_t)CALL(nvnSyncInitialize, U(&syncs), U(&dev)) == 1, "SyncInitialize");
CHECK(CALL(nvnSyncWait, U(&syncs), 1000) == NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED, "a sync never fenced times out at once");
CALL(nvnSyncInitialize, U(&sync_cmd), U(&dev));
CALL(nvnSyncInitialize, U(&sync_future), U(&dev));
sync_t *sf = (sync_t *)(void *)&sync_future;
sf->fenced = 1;
@@ -461,6 +463,7 @@ static uint64_t record_all(void) {
CALL(nvnCommandBufferSetColorReductionEnable, U(c), 1); // hints: nothing recorded
CALL(nvnCommandBufferSetColorReductionThresholds, U(c), 0, U(thresholds));
REC(OP_COPY_T2T, CALL(nvnCommandBufferDownsample, U(c), U(&tex1), U(&tex2)));
REC(OP_FENCE_SYNC, CALL(nvnCommandBufferFenceSync, U(c), U(&sync_cmd), NVN_SYNC_CONDITION_ALL_GPU_COMMANDS_COMPLETE, 0));
CHECK((uint8_t)CALL(nvnCommandBufferIsRecording, U(c)) == 1, "IsRecording while recording");
uint64_t h = CALL(nvnCommandBufferEndRecording, U(c));
CHECK((uint8_t)CALL(nvnCommandBufferIsRecording, U(c)) == 0, "IsRecording after EndRecording");
@@ -681,6 +684,7 @@ static void parse_all(uint64_t h) {
(p->addr == 0xc300 && p->size == 4 && p->value == 0x1234), "CLEAR_BUF payload (ClearBuffer, ReportValue)");
break;
}
case OP_FENCE_SYNC: CHECK(((const p_fence_sync *)a)->sync == (sync_t *)(void *)&sync_cmd, "FENCE_SYNC payload"); break;
default: CHECK(0, "unexpected op %s", nvn_op_name(o->op)); break;
}
}
@@ -815,6 +819,7 @@ static void probe_null_effects(void) {
CALL(nvnCommandBufferUpdateUniformBuffer, U(c), gbase, 64, 8, 8, U(d2));
CALL(nvnCommandBufferDrawArrays, U(c), NVN_DRAW_PRIMITIVE_TRIANGLES, 0, 3); // ignored
CALL(nvnCommandBufferBarrier, U(c), 1);
CALL(nvnCommandBufferFenceSync, U(c), U(&sync_cmd), NVN_SYNC_CONDITION_ALL_GPU_COMMANDS_COMPLETE, 0);
uint64_t ha = CALL(nvnCommandBufferEndRecording, U(c));
begin(&cmd2, ctlmem2, 0x38000);
@@ -824,6 +829,9 @@ static void probe_null_effects(void) {
CHECK(*(uint32_t *)ev_val == 0 && !pmem[OFF_UB + 4] && !pmem[OFF_DST] && pmem[OFF_COUNTER_B] == 0xaa,
"recording has no side effects");
// record_all and the call that inlined it recorded a FenceSync of sync_cmd too, and neither was submitted
CHECK(CALL(nvnSyncWait, U(&sync_cmd), 1000) == NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED,
"a CommandBufferFenceSync recorded but not submitted leaves the sync unsignaled");
uint64_t t0 = rt_ticks(), subs = nvn_submit_count(), fence0 = nvn_fence_last();
uint64_t hs[2] = {ha, hb};
@@ -835,6 +843,12 @@ static void probe_null_effects(void) {
(unsigned long long)wait);
CHECK(nvn_submit_count() == subs + 1 && nvn_fence_last() == fence0 + 1 && gfx_backend()->fence_done() == nvn_fence_last(),
"one submit advances the fence once (%llu -> %llu)", (unsigned long long)fence0, (unsigned long long)nvn_fence_last());
const sync_t *sc = (const sync_t *)(const void *)&sync_cmd;
uint64_t wait_cmd = CALL(nvnSyncWait, U(&sync_cmd), 1000000000ull);
CHECK(sc->fenced == 1 && sc->fence == nvn_fence_last() &&
(wait_cmd == NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED || wait_cmd == NVN_SYNC_WAIT_RESULT_CONDITION_SATISFIED),
"CommandBufferFenceSync: the submit fenced the sync at its value (fenced %u, fence %llu of %llu), SyncWait %llu",
sc->fenced, (unsigned long long)sc->fence, (unsigned long long)nvn_fence_last(), (unsigned long long)wait_cmd);
// event: WRITE 0x11, ADD 5, then handle 2's WRITE 0x99
CHECK((uint32_t)CALL(nvnEventGetValue, U(&ev)) == 0x99, "event value %#x after the stream, expected 0x99", *(uint32_t *)ev_val);
@@ -852,7 +866,7 @@ static void probe_null_effects(void) {
CHECK(cb[0] == 0 && cb[1] + 1 >= t0 && cb[1] <= t1 + 1, "samples-passed counter {%llu, %llu}", (unsigned long long)cb[0],
(unsigned long long)cb[1]);
CHECK(!memcmp(pmem + OFF_B2B_DST, keep, 16), "a copy out of the GPU window left the target alone");
n_effects = 6;
n_effects = 7;
CALL(nvnCommandBufferFinalize, U(&cmd2));
CALL(nvnCommandBufferFinalize, U(&cmd));
+2 -2
View File
@@ -1175,7 +1175,7 @@ static int walk(VkExec *ex, const op_hdr *h, int depth, const op_hdr *stop) {
if (ex->wpos_i < ex->wpos_end && ex->wpos[ex->wpos_i] == ex->pos) ex->drop_n++, ex->wpos_i++;
}
switch (h->op) {
case OP_BEGIN: break;
case OP_BEGIN: case OP_FENCE_SYNC: break; // FENCE_SYNC: the front end signals it at submit (p_fence_sync)
case OP_JUMP: h = (const op_hdr *)(uintptr_t)((const p_jump *)a)->target; continue;
case OP_CALL:
if (depth >= CALL_DEPTH) rt_fatal("vk: command buffer calls nested deeper than %d", CALL_DEPTH);
@@ -1749,7 +1749,7 @@ static void scan(VkExec *s, int hi, const op_hdr *h, int depth) {
if (vk_exec_xfer_revive(h->op, a, s->pos)) scan_hold(HOLD_TEX);
SC.open = 0;
break;
case OP_SIGNAL_EVENT: break;
case OP_SIGNAL_EVENT: case OP_FENCE_SYNC: break;
case OP_RENDER_COND: SC.cond[SC.ncond++] = (uint8_t)vk_exec_xfer_scan(s, h->op, a); break;
case OP_REPORT_COUNTER: case OP_RESET_COUNTER: case OP_RENDER_ENABLE: vk_exec_xfer_scan(s, h->op, a); break;
default: state_op(s, h->op, a); // walk stops on an op without a handler
+5
View File
@@ -758,6 +758,10 @@ static int test_xfer(const GfxBackend *b, uint8_t *guest, int *bytes) {
p = put(p, OP_CLEAR_TEX, &ct, sizeof ct);
const p_signal_event ev1 = {event, NVN_EVENT_SIGNAL_MODE_WRITE, 7}, ev2 = {event, NVN_EVENT_SIGNAL_MODE_ADD, 5};
p = put(p, OP_SIGNAL_EVENT, &ev1, sizeof ev1);
// CommandBufferFenceSync's record: the replay passes over it (the front end signals the sync at submit)
static sync_t fs_sync;
const p_fence_sync fs = {&fs_sync};
p = put(p, OP_FENCE_SYNC, &fs, sizeof fs);
p = put(p, OP_SIGNAL_EVENT, &ev2, sizeof ev2);
const p_report_counter rc = {NVN_COUNTER_TYPE_TIMESTAMP, (uint64_t)(uintptr_t)ctr};
p = put(p, OP_REPORT_COUNTER, &rc, sizeof rc);
@@ -786,6 +790,7 @@ static int test_xfer(const GfxBackend *b, uint8_t *guest, int *bytes) {
memcpy(&clv, cl, 4);
for (int i = 0; i < XF_W * XF_H; i++) CHECK(px[i] == clv, "CLEAR_TEX texel %d is %08x, expected %08x", i, px[i], clv);
CHECK(*event == 12, "event word is %u after WRITE 7 and ADD 5", *event);
CHECK(!fs_sync.fenced && !fs_sync.fence, "the replay left the FENCE_SYNC record's sync alone");
CHECK(ctr->counter == 0 && ctr->timestamp != 0, "counter report is {%llu, %llu}", (unsigned long long)ctr->counter,
(unsigned long long)ctr->timestamp);
CHECK(sp->counter == 0 && sp->timestamp != 0, "samples-passed report after a reset and no draw is {%llu, %llu}",