diff --git a/.claude/skills/run-game/SKILL.md b/.claude/skills/run-game/SKILL.md index 701a230e..2efc4122 100644 --- a/.claude/skills/run-game/SKILL.md +++ b/.claude/skills/run-game/SKILL.md @@ -22,6 +22,7 @@ prints `: rc=`. The game window opens behind the user's windows witho | `user-save-2026-10-21-monsters` | Moblins and Zonai constructs attack at once; use with `--script tools/input/fight.txt` | | `user-save-2026-10-21` | Hyrule field facing the castle, rainy; distant sky over the castle | | `user-save-2026-10-23-pillars` | Hyrule field by standing stone pillars, about 17:40, a Zonai construct beside Link; use with `--script tools/input/backrun.txt` (runs backward from 1550: phantom rocks, a one-frame glitch at about 1605) | +| `user-save-2026-10-24-brightbloom` | Cave, about 20:40: a Brightbloom plant at the lower left (its bulb floats beside the stem, `build/v121-bloom/NOTES.md`), teal Moblin and Bokoblin ahead or Zonai constructs (runs differ) | | `user-save-2026-10-22-island` | Sky island edge; use with `--script tools/input/island.txt` (runs off, skydives, opens the paraglider with B at frame 1975; X does nothing in a dive) | `--frames` counts presents. Pick `1500 + the frames you need in-world`. diff --git a/docs/agent-playbook.md b/docs/agent-playbook.md index 669a32e3..5caf902e 100644 --- a/docs/agent-playbook.md +++ b/docs/agent-playbook.md @@ -80,6 +80,11 @@ Check these classes before you re-investigate a symptom that looks like one of t writes them back first. Other CPU reads of GPU output are not covered yet. A write-back skips a texture whose storage the CPU rewrote after the GPU render (`vk_texture.c` `write_back`); writing it anyway put stale grass maps into the far terrain for a second (texture flicker on a hit). +- The CPU reads GPU-rendered textures in CPU-cached pools: `nvnMemoryPoolInvalidateMappedRange` writes them back + (`nvn_ext.c` `nvn_pool_invalidate`; TotK invalidates its 260x260 R16 grass maps and 1x1 targets), and once the CPU + waits for a fence, targets up to 64 KiB rendered before it go back too (`vk_texture.c` + `vk_texture_write_back_small`): the 128x128 cut map is read with no invalidate. Without it a cut left the grass + standing, with no blades and no insects. - A one-shot draw (a target not rendered in the previous frame) waits for its shader and pipeline instead of being skipped: `vk_exec_draw.c` `prepare_draw`. Skipping left the cloud and fog volumes empty for the session. - Storage buffers bound by Ryujinx first-use order instead of the NVN c0 slot: `vk_exec_draw.c` `ssbo_slot`. It showed diff --git a/runtime/gfx.h b/runtime/gfx.h index 98266c11..9d54c916 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -117,6 +117,9 @@ typedef struct { void (*write_texels)(const tex_t *t, const view_t *v, const NVNcopyRegion *r, const uint8_t *src, uint64_t row, uint64_t img); void (*guest_written)(uint64_t addr, uint64_t size); // host wrote pool storage (3.2) void (*guest_read)(uint64_t addr, uint64_t size); // the CPU reads pool storage: GPU-written texels go there first + // The CPU waited for fence: small render targets written by work up to it go to their storage (vk_texture.c + // vk_texture_write_back_small). + void (*cpu_synced)(uint64_t fence); } GfxBackend; extern const GfxBackend gfx_null_backend, gfx_vulkan_backend; const GfxBackend *gfx_backend(void); // TOTK_GPU=null|vulkan (default vulkan) diff --git a/runtime/gfx_null.c b/runtime/gfx_null.c index 847a82f7..073c3182 100644 --- a/runtime/gfx_null.c +++ b/runtime/gfx_null.c @@ -132,6 +132,7 @@ static void null_write_texels(const tex_t *t, const view_t *v, const NVNcopyRegi static void null_guest_written(uint64_t addr, uint64_t size) { (void)addr, (void)size; } static void null_guest_read(uint64_t addr, uint64_t size) { (void)addr, (void)size; } +static void null_cpu_synced(uint64_t fence) { (void)fence; } const GfxBackend gfx_null_backend = { .name = "null", @@ -151,4 +152,5 @@ const GfxBackend gfx_null_backend = { .write_texels = null_write_texels, .guest_written = null_guest_written, .guest_read = null_guest_read, + .cpu_synced = null_cpu_synced, }; diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index 15584262..665fcc24 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -429,12 +429,13 @@ NVN_API(nvnSyncWait) { uint64_t timeout = arg_x(c, 1); if (!s->fenced) { ret_x(c, NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED); return; } const GfxBackend *b = gfx_backend(); - if (b->fence_done() >= s->fence) { ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED); return; } + if (b->fence_done() >= s->fence) { b->cpu_synced(s->fence); ret_x(c, NVN_SYNC_WAIT_RESULT_ALREADY_SIGNALED); return; } int ok; { NVN_BLOCKING(); ok = b->fence_wait(s->fence, timeout); } + if (ok) b->cpu_synced(s->fence); ret_x(c, ok ? NVN_SYNC_WAIT_RESULT_CONDITION_SATISFIED : NVN_SYNC_WAIT_RESULT_TIMEOUT_EXPIRED); } // void nvnEventBuilderSetDefaults(NVNeventBuilder *b) @@ -516,8 +517,11 @@ NVN_API(nvnQueueGetError) { NVN_API(nvnQueueFlush) { (void)c; } // void nvnQueueFinish(NVNqueue *q) NVN_API(nvnQueueFinish) { - NVN_BLOCKING(); - gfx_backend()->finish(); + { + NVN_BLOCKING(); + gfx_backend()->finish(); + } + gfx_backend()->cpu_synced(gfx_backend()->fence_done()); } // ---------- Submit latency guard for TotK 1.2.1's per-object uniform double buffer // NVN starts the GPU when QueueSubmitCommands returns, within microseconds; here the replay into Vulkan takes 15-30 ms diff --git a/runtime/nvn_ext.c b/runtime/nvn_ext.c index e3ad5837..3e31ad0d 100644 --- a/runtime/nvn_ext.c +++ b/runtime/nvn_ext.c @@ -11,9 +11,14 @@ #include "nvn_api.h" #include "nvn_tex.h" -// The CPU is about to read what the GPU wrote into pool storage: a back end that renders into host-side copies (the -// Vulkan back end's images) defines the strong version and writes them back. -__attribute__((weak)) void nvn_pool_invalidate(const pool_t *p, uint64_t off, uint64_t size) { (void)p, (void)off, (void)size; } +// The CPU is about to read what the GPU wrote into pool storage: the back end writes its host-side copies (the Vulkan +// back end's images) back first. TotK invalidates its 260x260 R16 grass maps and 1x1 pick targets this way. A +// CPU-invisible pool has no storage to read. +void nvn_pool_invalidate(const pool_t *p, uint64_t off, uint64_t size) { + if (!p->mem || nvn_gpu_only((uintptr_t)p->mem) || off >= p->size || !size) return; + if (size > p->size - off) size = p->size - off; + gfx_backend()->guest_read((uintptr_t)p->mem + off, size); +} // ---------- debug labels, markers, groups, checkpoints: meaningful only to NVN debugging tools // void nvnSetDebugLabel(NVN *o, const char *label) diff --git a/runtime/vk.h b/runtime/vk.h index 09836c63..b1101d12 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -186,6 +186,8 @@ const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view); VkSampler vk_sampler_by_id_locked(int id); uint32_t vk_texture_slots_gen(void); void vk_texture_gpu_written(const tex_t *t); // rendered or copied into by the GPU: no re-upload from storage +int vk_texture_small_dirty(void); // a small mirrored target holds GPU texels its storage lacks +void vk_texture_write_back_small(uint64_t fence); // writes those rendered by work up to fence back to storage void vk_texture_debug(const VkTexImage *img, uint32_t *uploads, int *last_change, int *pages_written); // vk_dump int vk_texture_slot_swz(int id, uint8_t swz[4]); // vk_dump: swizzle of the descriptor in slot id int vk_texture_read_image(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint8_t *dst, uint64_t row, diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 7b18a945..c11181ef 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -159,6 +159,12 @@ static void vk_be_guest_read(uint64_t addr, uint64_t size) { vk_present_drain(); vk_texture_write_back(addr, size); } +static void vk_be_cpu_synced(uint64_t fence) { + if (!vk_texture_small_dirty()) return; + vk_sync_start(); + vk_present_drain(); + vk_texture_write_back_small(fence); +} const GfxBackend gfx_vulkan_backend = { .name = "vulkan", @@ -178,6 +184,7 @@ const GfxBackend gfx_vulkan_backend = { .write_texels = vk_be_write_texels, .guest_written = vk_be_guest_written, .guest_read = vk_be_guest_read, + .cpu_synced = vk_be_cpu_synced, }; void vk_backend_shutdown(void) { diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 4b1d5758..7cdbe4ad 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -47,6 +47,8 @@ VkExecFrame vk_exec_frame; static struct { const uint8_t *mem; uint32_t w, h, fmt, n; } fw[64]; // textures written this frame static int nfw; +uint64_t vk_exec_recording_fence(void) { return x_here ? X.fence : 0; } + void vk_exec_frame_written(const tex_t *t) { if (!(rt_trace & RT_TRACE_GFX) || !t) return; for (int i = 0; i < nfw; i++) diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 759d2f86..ca022f0d 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -136,6 +136,7 @@ typedef struct { } VkExecFrame; extern VkExecFrame vk_exec_frame; void vk_exec_frame_written(const tex_t *t); // vk_texture_gpu_written: t rendered or copied into +uint64_t vk_exec_recording_fence(void); // fence of the submission this thread records, else 0 void vk_exec_frame_log(const tex_t *presented, uint64_t frame); // TOTK_DUMP= (vk_dump.c): between presents frame-1 and frame, every pass's targets and every dispatch's storage diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 18b5edb5..f8484921 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -51,6 +51,7 @@ typedef struct Tex { uint8_t gpu_dirty; // mirrored, and the GPU wrote the image after the last write-back to the storage uint32_t gpu_gen; // page generation when the GPU write that set gpu_dirty was recorded uint64_t gpu_seq; // order of that GPU write among all of them (gpu_writes) + uint64_t gpu_fence; // fence of the submission that made that write (0: not recorded in one) uint8_t listed; // in owned[] } Tex; @@ -719,6 +720,11 @@ const VkTexImage *vk_texture_get(const tex_t *t) { return e ? &e->pub : NULL; } +// Small render targets the CPU reads without NVN's invalidate (vk_texture_write_back_small); small_pending: one may be +// dirty. +enum { WB_SMALL = 64 << 10 }; +static int small_pending; + void vk_texture_gpu_written(const tex_t *t) { if (!t || !t->mem) return; vk_exec_frame_written(t); @@ -731,6 +737,8 @@ void vk_texture_gpu_written(const tex_t *t) { e->gpu_dirty = 1; storage_rewritten(e, 0, &e->gpu_gen); // CPU writes up to here are older than this render e->gpu_seq = ++gpu_writes; + e->gpu_fence = vk_exec_recording_fence(); + if (e->t.L.size <= WB_SMALL) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE); own_add(e); } } @@ -1168,6 +1176,48 @@ void vk_texture_write_back(uint64_t addr, uint64_t size) { unlock(); } +// TotK's grass system renders a 128x128 RGBA8 map of cut and trampled grass into a CPU-cached pool every frame and its +// CPU code reads the pool bytes without nvnMemoryPoolInvalidateMappedRange (cut reactions: blades, insects, the cut +// itself; build/v121-bloom NOTES.md). On the Switch the bytes are there once the frame's fence has passed. So when the +// CPU waits for a fence, mirrored targets up to WB_SMALL bytes rendered by work up to that fence go back to their +// storage, unless the CPU rewrote it since. Larger targets still go back on an invalidate, memcpy or ReadTexels. +int vk_texture_small_dirty(void) { return __atomic_load_n(&small_pending, __ATOMIC_ACQUIRE); } + +void vk_texture_write_back_small(uint64_t fence) { + lock(); + __atomic_store_n(&small_pending, 0, __ATOMIC_RELAXED); + AcquireSRWLockShared(&owned_lock); + int n = 0, later = 0; + for (int i = 0; i < nowned; i++) n += owned[i]->gpu_dirty && owned[i]->t.L.size <= WB_SMALL; + Tex **hit = n ? malloc(sizeof *hit * (size_t)n) : NULL; + if (n && !hit) rt_fatal("vk: out of memory for a texture write-back"); + n = 0; + for (int i = 0; i < nowned; i++) { + Tex *e = owned[i]; + if (!e->gpu_dirty || e->t.L.size > WB_SMALL) continue; + if (e->gpu_fence > fence) later = 1; + else hit[n++] = e; + } + ReleaseSRWLockShared(&owned_lock); + if (later) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE); + int k = 0; + for (int i = 0; i < n; i++) { + uint32_t now; + if (storage_rewritten(hit[i], hit[i]->gpu_gen, &now) == 1) hit[i]->gpu_dirty = 0; + else hit[k++] = hit[i]; + } + for (int i = 1; i < k; i++) + for (int j = i; j > 0 && hit[j - 1]->gpu_seq > hit[j]->gpu_seq; j--) { + Tex *x = hit[j]; + hit[j] = hit[j - 1], hit[j - 1] = x; + } + for (int i = 0; i < k; i++) write_back(hit[i]); + for (int i = 0; i < k; i++) perf_count(PERF_CNT_WB); + for (int i = k; i < n; i++) perf_count(PERF_CNT_WB_SKIP); + free(hit); + unlock(); +} + // ---------------------------------------------------------------- samplers (LAS textures.c:778-844) static VkCompareOp compare_op(uint32_t f) { switch (f) {