From 054390ef1d3617ea941ed6501902c272e0047ef7 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Thu, 19 Nov 2026 09:02:22 +0300 Subject: [PATCH 1/2] nvnCommandBufferGetCommandMemoryUsed no longer grows on CallCommands and grows by the copied recording's hardware size on CopyCommands The real driver puts a call in control memory, so a call adds no command memory; a copy adds what the copied commands take on hardware. We added our record bytes of the inlined recording for both. On 1.4.3 the game queries recordings that hold calls every frame (field save, 20 s: 15,700 such queries from six call sites, up to 14,604 bytes reported where the driver rule gives 3,204); copies were not seen. Each recording now keeps its hardware size from EndRecording (copy_prefix.hw), and memory added while recording keeps that size whole. Same accounting as MacNativeGames 6909dbb7, which measured 40 bytes on the original driver where their port reported 88 and traced a renderer heap crash near Zora's Domain to the larger sizes. Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_cmd.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index 8141c61b..fbfcd441 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -412,8 +412,9 @@ void nvn_pins_stats(uint64_t *live, uint64_t *freed) { // copies per page), and the recording threads waited on it. // 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 -typedef struct { uint64_t magic, bytes, made_at, used_at, deferred, pins, cap; const tex_t **pin_v; } copy_prefix; +// (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; _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; @@ -573,11 +574,12 @@ uint64_t nvn_handle_bytes(uint64_t handle) { } // Marks a live copy used by submission now; its size from OP_BEGIN to OP_END inclusive. -static uint64_t copy_touch(uint64_t handle, uint64_t now, const char *what) { +static uint64_t copy_touch(uint64_t handle, uint64_t now, const char *what, uint64_t *hw) { plat_mutex_lock_shared(©_lock); copy_prefix *pf = copy_find(handle); uint64_t bytes = 0; if (pf) __atomic_store_n(&pf->used_at, now, __ATOMIC_RELAXED), bytes = pf->bytes; + if (hw) *hw = pf ? pf->hw : 0; plat_mutex_unlock_shared(©_lock); if (!pf) rt_fatal("nvn: %s: command handle %#llx is not a live recording (released or never recorded)", what, (unsigned long long)handle); @@ -731,6 +733,9 @@ NVN_API(nvnCommandBufferAddCommandMemory) { const pool_t *p = arg_p(c, 1); o->cmd_base = (uintptr_t)(p ? p->mem : NULL) + (uintptr_t)arg_x(c, 2); o->game_size = arg_x(c, 3); + // Memory added while recording (the out-of-memory callback): hw_start moves with hw_used, so hw_used - hw_start + // stays the whole recording's size at EndRecording (unsigned wrap-around cancels out). + if (o->recording) o->hw_start -= o->hw_used; o->cmd_used = 0; o->hw_used = 0; } @@ -781,6 +786,7 @@ NVN_API(nvnCommandBufferEndRecording) { uintptr_t lo = (uintptr_t)o->ctl + (o->ctl_used < o->ctl_size ? o->ctl_used : 0); uintptr_t hi = o->ctl ? lo + 1 : 0; o->ctl_used += CTL_PER_RECORDING; + ((copy_prefix *)copy - 1)->hw = o->hw_used - o->hw_start; // BEGIN and END excluded; what a copy of it adds copy_add(lo, hi, copy); ret_x(c, (uintptr_t)copy); } @@ -789,18 +795,26 @@ NVN_API(nvnCommandBufferIsRecording) { ret_x(c, cmd_of(c)->recording != 0); } // The called recording is copied into the caller at the call position: the game recycles command memory in small steps, // so a handle may be gone by the time the caller is submitted. Deferred binds stay deferred. A copy is OP_BEGIN, its -// records and OP_END (flatten_recording checked them), so the records go over as one block; usage grows by the same +// records and OP_END (flatten_recording checked them), so the records go over as one block; cmd_used grows by the same // bytes as record by record. -static void inline_recording(cmd_t *o, uint64_t handle, const char *what) { +// nvnCommandBufferGetCommandMemoryUsed grows as on the real driver: a call by nothing (the driver puts a call in +// control memory) and a copy by the copied recording's hardware size (copy_prefix.hw), not by our record bytes. The +// game measures small command sets it records and sizes renderer memory pools from them: MacNativeGames commit 6909dbb7 +// measured one recording with two uniform buffer binds and one CallCommands at 40 bytes on the original driver +// (Ryujinx, 1.0.0) and 88 in their port, whose larger sizes doubled a renderer's pool and ran its private heap dry +// (NULL write crash near Zora's Domain). 1.4.3 calls in recordings it queries every frame (copies were not seen). +static void inline_recording(cmd_t *o, uint64_t handle, int call) { + const char *what = call ? "CallCommands" : "CopyCommands"; if (!handle) rt_fatal("nvn: %s with a null handle", what); - uint64_t bytes = copy_touch(handle, __atomic_load_n(&nsubmits, __ATOMIC_RELAXED), what); + uint64_t hw = 0; + uint64_t bytes = copy_touch(handle, __atomic_load_n(&nsubmits, __ATOMIC_RELAXED), what, &hw); if (bytes <= 16) return; size_t body = (size_t)bytes - 16; cmd_reserve(o, body); memcpy(o->cur, (const uint8_t *)(uintptr_t)handle + 8, body); o->cur += body; o->cmd_used += body; - o->hw_used += body; + if (!call) o->hw_used += hw; o->last_rec = 0; } // void nvnCommandBuffer{Call,Copy}Commands(NVNcommandBuffer *cmd, int n, const NVNcommandHandle *handles) @@ -808,13 +822,13 @@ NVN_API(nvnCommandBufferCallCommands) { cmd_t *o = cmd_of(c); int n = (int)arg_x(c, 1); const uint64_t *h = arg_p(c, 2); - for (int i = 0; i < n; i++) inline_recording(o, h[i], "CallCommands"); + for (int i = 0; i < n; i++) inline_recording(o, h[i], 1); } NVN_API(nvnCommandBufferCopyCommands) { cmd_t *o = cmd_of(c); int n = (int)arg_x(c, 1); const uint64_t *h = arg_p(c, 2); - for (int i = 0; i < n; i++) inline_recording(o, h[i], "CopyCommands"); + for (int i = 0; i < n; i++) inline_recording(o, h[i], 0); } // ---------- CommandBuffer: commands with CPU-visible results (performed by the back end in GPU order) @@ -1285,7 +1299,7 @@ static int resolve_deferred(const uint8_t *p, int depth) { // returns the numb void nvn_submit_prepare(const uint64_t *handles, int n) { uint64_t now = __atomic_add_fetch(&nsubmits, 1, __ATOMIC_RELAXED); - for (int i = 0; i < n; i++) copy_touch(handles[i], now, "QueueSubmitCommands"); + for (int i = 0; i < n; i++) copy_touch(handles[i], now, "QueueSubmitCommands", NULL); for (int i = 0; i < n; i++) resolve_deferred((const uint8_t *)(uintptr_t)handles[i], 0); } From 7795c0a5a980d402f7f1553e41867fbb6faf81ce Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Thu, 19 Nov 2026 09:14:37 +0300 Subject: [PATCH 2/2] nvnCommandBufferCallCommands adds 24 bytes of control memory per called handle, so a buffer that only called recordings no longer reads as empty A call adds no command memory since the previous commit. Control memory grew only at EndRecording, so a buffer queried after calls and before its first EndRecording reported 0 command and 0 control bytes, which the game treats as an empty buffer. The game's inline copy of the driver's call (1.4.3 main+0x2d33a4) writes one 24-byte control entry per segment of the called handle; each called handle now adds that much. The NVN probe checks that a call-only buffer reports 0 command and some control memory. Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_cmd.c | 8 +++++++- runtime/nvn_probe.c | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/runtime/nvn_api_cmd.c b/runtime/nvn_api_cmd.c index fbfcd441..5662b965 100644 --- a/runtime/nvn_api_cmd.c +++ b/runtime/nvn_api_cmd.c @@ -30,6 +30,7 @@ #define MIN_COMMAND_SIZE 512 // NVN_DEVICE_INFO_COMMAND_BUFFER_MIN_COMMAND_SIZE (nvn_api_core.c devinfo) #define MIN_CONTROL_SIZE 256 // NVN_DEVICE_INFO_COMMAND_BUFFER_MIN_CONTROL_SIZE #define CTL_PER_RECORDING 64 // control memory one recording is reported to use +#define CTL_PER_CALL 24 // control memory one called handle is reported to use (CallCommands) #define COPY_PAGE_SHIFT 16 // recording copy index granularity (release_copies) static cmd_t *cmd_of(const Cpu *c) { return arg_p(c, 0); } @@ -798,7 +799,7 @@ NVN_API(nvnCommandBufferIsRecording) { ret_x(c, cmd_of(c)->recording != 0); } // records and OP_END (flatten_recording checked them), so the records go over as one block; cmd_used grows by the same // bytes as record by record. // nvnCommandBufferGetCommandMemoryUsed grows as on the real driver: a call by nothing (the driver puts a call in -// control memory) and a copy by the copied recording's hardware size (copy_prefix.hw), not by our record bytes. The +// control memory, nvnCommandBufferCallCommands) and a copy by the copied recording's hardware size (copy_prefix.hw), not by our record bytes. The // game measures small command sets it records and sizes renderer memory pools from them: MacNativeGames commit 6909dbb7 // measured one recording with two uniform buffer binds and one CallCommands at 40 bytes on the original driver // (Ryujinx, 1.0.0) and 88 in their port, whose larger sizes doubled a renderer's pool and ran its private heap dry @@ -818,11 +819,16 @@ static void inline_recording(cmd_t *o, uint64_t handle, int call) { o->last_rec = 0; } // void nvnCommandBuffer{Call,Copy}Commands(NVNcommandBuffer *cmd, int n, const NVNcommandHandle *handles) +// A call takes control memory instead: the game's inline copy of the driver's call (1.4.3 main+0x2d33a4, in the +// function that measures a renderer's command set) writes one 24-byte control entry per segment of the called handle, +// after an 8-byte block header. The game reads a buffer whose command and control usage are both 0 as empty +// (main+0x83f8c clears bit 0 of its flags), so a buffer that only called recordings must show control usage. NVN_API(nvnCommandBufferCallCommands) { cmd_t *o = cmd_of(c); int n = (int)arg_x(c, 1); const uint64_t *h = arg_p(c, 2); for (int i = 0; i < n; i++) inline_recording(o, h[i], 1); + if (n > 0) o->ctl_used += CTL_PER_CALL * (size_t)n; } NVN_API(nvnCommandBufferCopyCommands) { cmd_t *o = cmd_of(c); diff --git a/runtime/nvn_probe.c b/runtime/nvn_probe.c index a3996aed..43107407 100644 --- a/runtime/nvn_probe.c +++ b/runtime/nvn_probe.c @@ -770,6 +770,16 @@ static void probe_commands(void) { // adding the control memory again retires the recordings made in it CALL(nvnCommandBufferAddControlMemory, U(&cmd2), U(ctlmem2), 0x1000); CHECK(!nvn_handle_live(h2) && nvn_handle_live(h), "reusing control memory retires only the handles recorded in it"); + + // a buffer that only called a recording is not empty: the call takes control memory and no command memory + CALL(nvnCommandBufferAddCommandMemory, U(&cmd2), U(&pool), 0x34000, 0x4000); + CALL(nvnCommandBufferBeginRecording, U(&cmd2)); + CALL(nvnCommandBufferCallCommands, U(&cmd2), 1, U(&h)); + uint64_t used = CALL(nvnCommandBufferGetCommandMemoryUsed, U(&cmd2)); + uint64_t ctl_used = CALL(nvnCommandBufferGetControlMemoryUsed, U(&cmd2)); + CHECK(used == 0 && ctl_used > 0, "a call takes control memory only: command %llu, control %llu bytes", + (unsigned long long)used, (unsigned long long)ctl_used); + CALL(nvnCommandBufferEndRecording, U(&cmd2)); CALL(nvnCommandBufferFinalize, U(&cmd2)); probe_pins(); n_objects += 5;