Write GPU-rendered textures back when the CPU invalidates a pool range or waits for a fence

nvn_pool_invalidate was a weak no-op, so nvnMemoryPoolInvalidateMappedRange never wrote anything back. TotK
invalidates its 260x260 R16 grass maps and 1x1 targets before reading them; they now go through guest_read.

The grass system also renders a 128x128 RGBA8 cut map into a CPU-cached pool every frame and reads it with no
invalidate. Without its texels the CPU saw no cut: swings left the grass standing, with no blades and no insects.
After nvnSyncWait or nvnQueueFinish, dirty mirrored targets up to 64 KiB rendered in a submission up to the waited
fence go back to storage (cpu_synced, vk_texture_write_back_small), unless the CPU rewrote it since.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-24 10:41:54 +03:00
1 parent 3505258a06
commit 0beafeb066
10 files changed
+87 -6

No files matched your search

+5
View File
@@ -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
+3
View File
@@ -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)
+2
View File
@@ -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,
};
+7 -3
View File
@@ -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
+8 -3
View File
@@ -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 nvn<Object>SetDebugLabel(NVN<object> *o, const char *label)
+2
View File
@@ -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,
+7
View File
@@ -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) {
+2
View File
@@ -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++)
+1
View File
@@ -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=<frame> (vk_dump.c): between presents frame-1 and frame, every pass's targets and every dispatch's storage
+50
View File
@@ -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) {