mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 01:49:58 -04:00
nvnQueueSubmitCommands resolves a copy's deferred binds at the submit call: the handles the game's arrays hold now are
written into the recording copy (nvn_api_cmd.c resolve_deferred). With the replay on the submitting thread, a second
guest thread that submitted the same copy while the first one replayed it, or while the first copied it for a held
continuation (vk_exec.c cont_new), rewrote those handles under that replay: the rest of it bound the next frame's
textures and samplers, and a held continuation could be copied torn (F-2-03's part left after the private copies).
Now each copy counts the synchronous submissions between their prepare and the end of their replay (copy_prefix.busy,
which shares the 8 bytes pins had, so the prefix keeps its 56 bytes). nvn_submit_prepare takes copy_lock shared once
for all handles: the first submission in flight resolves the copy in place, as before; one that finds another in
flight, unless the copy is known to hold no deferred binds, resolves a clone made under that lock, with its texture
pins counted, and nvn_submit_done frees it after the replay, as the asynchronous path does for a copy a queued
submission still reads. A copy the game releases while a submission of it is in flight becomes a zombie and is freed at
the next sweep after it ends (release_copies, zombie_sweep). The bind loop moved unchanged into
runtime/nvn_submit_guard.h (nvn_resolve_binds) so a check can build it; it reports a corrupt record or a call past 16
deep to resolve_deferred, which ends the run with the messages it had, and keeps the first six changes' trace line.
Being code the front end carries under its MIT header, the new header keeps that line and THIRD_PARTY_NOTICES.md's row
names it. A clone is logged the first four times and at each power of two ("submits the recording ... while another
thread's submission of it is replayed"). No timing change and no wait: the clone path runs only when two threads have
one copy in flight at once.
Not reproduced before the change; nobody knows yet whether the game ever submits one copy from two threads at once (the
new line will say). Checks that passed: the new tests/submit_clone_check (four threads submitting two copies 200,000
times each while their arrays change: no replay saw its handles change, both paths ran, the counts ended at 0; the
unguarded 0.10.1 shape through the same harness: about 450,000 of 800,000 replays saw them change; nvn_resolve_binds
step by step), copy_sweep_check (its stand-in prefix follows the new layout), pass_cc_check on every runtime file, no new
-Wall -Wextra warnings in nvn_api_cmd.c and nvn_api_core.c.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
4.1 KiB
C
70 lines
4.1 KiB
C
// Contains code Copyright (c) 2026 jhaizhou (MIT; see THIRD_PARTY_NOTICES.md).
|
|
// The deferred binds of a recording copy at a synchronous submission, and what keeps a second submission of the same
|
|
// copy, made by another guest thread while the first is replayed, from rewriting it (nvn_api_cmd.c
|
|
// nvn_submit_prepare, nvn_submit_done). Header only, so that tests/submit_clone_check.c checks it without a device.
|
|
//
|
|
// A deferred bind takes the handles the game's array holds at the submit call, and they are written into the recording
|
|
// copy (nvn_resolve_binds). With the replay on the submitting thread, a second thread that submitted the same copy
|
|
// while the first one replayed it, or copied it for a held continuation (vk_exec.c cont_new), rewrote the handles that
|
|
// replay was reading: textures and samplers of the next frame halfway through it. So each copy counts the submissions
|
|
// between their prepare and the end of their replay (busy): the first one resolves in place, as before; one that finds
|
|
// another in flight resolves a clone of its own, freed after its replay, as the asynchronous path does for a copy a
|
|
// queued submission still reads. A clone taken while the first thread still resolves in place may hold a mix of old and
|
|
// new handles in its binds' arrays only, and its own resolve then writes every deferred array again: it ends whole.
|
|
#pragma once
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "nvn_obj.h"
|
|
|
|
// 1: no other submission has the copy in flight; resolve it in place and call nvn_copy_leave after the replay. 0:
|
|
// another one has; take a clone and call nvn_copy_leave once the clone is made.
|
|
static inline int nvn_copy_enter(uint32_t *busy) { return __atomic_fetch_add(busy, 1, __ATOMIC_ACQ_REL) == 0; }
|
|
static inline void nvn_copy_leave(uint32_t *busy) { __atomic_fetch_sub(busy, 1, __ATOMIC_RELEASE); }
|
|
|
|
// Told of a bind whose handles changed, before they are written: b holds the handles at record, now the game's, nth
|
|
// how many changed binds came before this one (the changed count).
|
|
typedef void (*NvnBindNote)(const p_bind_handles *b, const uint64_t *now, uint64_t nth);
|
|
|
|
// Deferred binds of the recording at p take the handles the game's arrays hold now: OP_BIND_TEX, OP_BIND_IMG and
|
|
// OP_BIND_SAMP with src set and 0 < n <= 32, through OP_JUMP and OP_CALL (16 calls deep at most). reads and changed
|
|
// count the binds read and those whose handles changed (atomic adds; either may be NULL); note may be NULL. Returns
|
|
// how many deferred binds the recording has, or -1 with *bad at the record that stopped it: a corrupt one, or a call
|
|
// past the depth limit.
|
|
static inline int nvn_resolve_binds(const uint8_t *p, int depth, uint64_t *reads, uint64_t *changed, NvnBindNote note,
|
|
const uint8_t **bad) {
|
|
int nd = 0;
|
|
for (;;) {
|
|
op_hdr h;
|
|
memcpy(&h, p, sizeof h);
|
|
if (h.size < 8 || (h.size & 7) || h.op >= OP_COUNT_) return *bad = p, -1;
|
|
if (h.op == OP_END) return nd;
|
|
if (h.op == OP_JUMP) {
|
|
memcpy(&p, p + 8, 8);
|
|
continue;
|
|
}
|
|
if (h.op == OP_CALL) {
|
|
uint64_t t;
|
|
memcpy(&t, p + 8, 8);
|
|
if (depth >= 16) return *bad = p, -1;
|
|
int k = nvn_resolve_binds((const uint8_t *)(uintptr_t)t, depth + 1, reads, changed, note, bad);
|
|
if (k < 0) return k;
|
|
nd += k;
|
|
} else if (h.op == OP_BIND_TEX || h.op == OP_BIND_IMG || h.op == OP_BIND_SAMP) {
|
|
p_bind_handles *b = (p_bind_handles *)(uintptr_t)(p + 8);
|
|
if (b->src && b->n > 0 && b->n <= 32) {
|
|
nd++;
|
|
uint64_t cur[32];
|
|
memcpy(cur, (const void *)(uintptr_t)b->src, 8 * (size_t)b->n);
|
|
if (reads) __atomic_fetch_add(reads, 1, __ATOMIC_RELAXED);
|
|
if (memcmp(cur, b->h, 8 * (size_t)b->n)) {
|
|
uint64_t nth = changed ? __atomic_fetch_add(changed, 1, __ATOMIC_RELAXED) : 0;
|
|
if (note) note(b, cur, nth);
|
|
memcpy(b->h, cur, 8 * (size_t)b->n);
|
|
}
|
|
}
|
|
}
|
|
p += h.size;
|
|
}
|
|
}
|