Fix Ocarina's white dialogue frames, the fountain floor speckle and the button prompt text

The texture pool descriptors are now a ring of sets, one per submission, brought up to date from a change log, and the first submission of each frame waits for its GPU work before the game goes on: the GPU ran late and part of a frame read data of the next one, and distant characters drew NaN that bloom spread over the frame. The window acquire also waits for the image's previous frame. Function locals start at zero unless DLAA is on, which removes the per-warp light tiles on the Great Fairy Fountain floor and Link's dark, speckled look. Texture slots follow only live entries of their own target, so the volumetric fog gets its 3D volume. The pool refresh walks only the slots that changed.

The translator implements PRMT (patch 0021), which it only logged before; the button prompts render again. HFMA2.RELU no longer depends on it, HMMA.F16 rounds once and HMMA.F32 is lowered, checked against GPU vectors with a new Maxwell warp emulator.

The frame dump gains a re-arming white-frame trigger, a trigger on a depth target's first pass, NaN pixels in magenta, image handles and view targets, and depth copies of sampled depth textures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-06 11:02:48 -05:00
parent c8738a948c
commit 618b9ba4e5
23 changed files with 2845 additions and 95 deletions

View File

@@ -40,7 +40,7 @@ static const char *const known[] = {
"BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA",
"COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT",
"DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR",
"DUMP_LOG", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES",
"DUMP_LOG", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "DUMP_WHITE", "DUMP_AFTER", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES",
"FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES",
"FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE",
"HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD",

View File

@@ -6,7 +6,8 @@
#define FLIGHT_THUMB_W 320
#define FLIGHT_THUMB_H 180
extern int flight_on; // NX2NAVI_FLIGHT=1, set once by flight_init before the first present
extern int flight_on;
extern int vk_dump_white; // vk_dump.c, NX2NAVI_DUMP_WHITE // NX2NAVI_FLIGHT=1, set once by flight_init before the first present
void flight_init(void); // prof_start calls it
// perf_present, once per present on the render thread: the interval since the previous present, the render thread's

View File

@@ -45,6 +45,7 @@ const pool_t *nvn_pool_find(uint64_t addr, uint64_t len); // GPU-visible pool h
uint64_t nvn_new_serial(void); // next tex_t / prog_t serial, from 1
uint64_t nvn_fence_last(void); // fence value of the newest submit (0 before the first)
int nvn_depth_minus_w(void); // 1 after DeviceSetDepthMode(NEAR_IS_MINUS_W), else 0
extern int nvn_submits_in_frame; // nvnQueueSubmitCommands calls since the last present (NX2NAVI_SUBMIT_FINISH)
void nvn_guest_written(uint64_t addr, uint64_t size); // the host or the game wrote pool storage: back end re-reads
// NX2NAVI_TRACE=perf: MemoryPoolFlushMappedRange [0] and BufferFlushMappedRange [1] calls [0] and bytes [1] by the pool's
// CPU class (0 CPU_CACHED, 1 CPU_UNCACHED, 2 other) since the previous call.

View File

@@ -622,6 +622,7 @@ NVN_API(nvnQueueFinish) {
// lod_entered / lod_issued count submits that entered QueueSubmitCommands / reached the back end; lod_fence holds
// the fence of the last LOD_RING. NX2NAVI_LOD_GUARD=0 turns the wait off.
#define LOD_RING 64
int nvn_submits_in_frame; // nvnQueueSubmitCommands calls since the last nvnQueuePresentTexture (NX2NAVI_SUBMIT_FINISH)
static uint64_t lod_entered, lod_issued, lod_fence[LOD_RING];
// void nvnQueueSubmitCommands(NVNqueue *q, int n, const NVNcommandHandle *handles): replayed on this thread with its
@@ -655,6 +656,23 @@ NVN_API(nvnQueueSubmitCommands) {
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);
__atomic_store_n(&lod_issued, no, __ATOMIC_SEQ_CST);
// The first submission of a frame (since the last present) returns once the GPU has run it. NVN starts the GPU when
// the submit returns and it is through a frame's first submission (Ocarina's frame-start compute: light grid, probes)
// within a few milliseconds; here the replay records first and the GPU runs later. Ocarina rewrites data those
// passes and the frame's lit materials read once it has submitted its later work, with no fence: with the GPU late,
// part of a frame drew with the next frame's data, distant characters wrote NaN and bloom spread it (white dialogue
// frames, 3% of them in 100 s of dialogue; none with this wait, at the same 58-59 fps: the GPU is idle at that
// point). Copies of the data at submit made it worse, a mix of two frames' versions.
// NX2NAVI_SUBMIT_FINISH=<mask>: bit i waits after the i-th submission of a frame (default 0x1); a plain 1 waits
// after every submission, 0 never.
static long long finish_mask = -2;
if (finish_mask == -2) {
const char *e = cfg_get("NX2NAVI_SUBMIT_FINISH");
finish_mask = e && *e ? strtoll(e, NULL, 0) : 1;
if (e && *e && finish_mask == 1 && !(e[0] == '0' && (e[1] == 'x' || e[1] == 'X'))) finish_mask = -1; // "1": all
}
int idx = nvn_submits_in_frame++;
if (finish_mask && f && (finish_mask == -1 || (idx < 63 && (finish_mask >> idx & 1)))) gfx_backend()->fence_wait(f, UINT64_MAX);
}
// The producer (1.2.1 main f_70008c6460) releases the read lock on obj+0x538 at lr 0x70008c70c8; when

View File

@@ -756,6 +756,19 @@ static void sleep_until(uint64_t deadline) {
}
}
// The submit fence of each window image's newest present (win_t has no room: a side table for the first windows).
static struct { const win_t *w; uint64_t fence[4]; } img_fence[4];
static uint64_t last_present_fence; // the submit fence at the newest present of any window
static uint64_t *img_fence_of(const win_t *o, int idx) {
if (idx < 0 || idx >= 4) return NULL;
for (int i = 0; i < 4; i++) {
if (img_fence[i].w == o) return &img_fence[i].fence[idx];
if (!img_fence[i].w) return img_fence[i].w = o, &img_fence[i].fence[idx];
}
return NULL;
}
// NVNwindowAcquireTextureResult nvnWindowAcquireTexture(NVNwindow *w, NVNsync *s, int *index): blocks here, not in the
// present, until the image's previous present plus one period has passed (the game often presents with a lock held).
// That is the time the next frame goes to the display, so the image is free as soon as that time is reached.
@@ -772,6 +785,24 @@ NVN_API(nvnWindowAcquireTexture) {
sleep_until(at);
perf_end(PERF_PACE, p0);
}
// On the Switch the image comes back only after the display has shown it, which needs the GPU to have finished the
// frame drawn into it, so the CPU never runs more than the swapchain's depth ahead of the GPU. Here the pacer frees
// images by the clock alone, so the acquire also waits for the GPU to finish the image's newest present. With the
// GPU slowed (frame dump logging), this alone cut Ocarina's NaN frames from 56 to 23 of 61; the wait after a frame's
// first submission (nvn_api_core.c nvnQueueSubmitCommands) removes the rest. NX2NAVI_ACQUIRE_WAIT=0 leaves it to the
// clock; =2 waits for the newest present of any image (one frame in flight at most).
static int acq_wait = -1;
if (acq_wait < 0) acq_wait = rt_knob("ACQUIRE_WAIT", "1");
uint64_t *fe = img_fence_of(o, idx), want = fe ? *fe : 0;
if (acq_wait == 2) want = last_present_fence;
if (acq_wait && want) {
const GfxBackend *b = gfx_backend();
if (b->fence_done() < want) {
uint64_t p0 = perf_begin();
b->fence_wait(want, UINT64_MAX);
perf_end(PERF_PACE, p0);
}
}
// The sync means "the display no longer reads this image". Presents are copies queued in submit order, so it is
// already satisfied: fenced = 3 with fence 0.
if (s) {
@@ -794,12 +825,16 @@ NVN_API(nvnQueuePresentTexture) {
if (o->magic != NVN_WIN_MAGIC || idx < 0 || idx >= o->ntex || idx >= 4 || !o->tex[idx])
rt_fatal("nvn: QueuePresentTexture of image %d on window %p", idx, (void *)o);
o->presents++;
nvn_submits_in_frame = 0;
const tex_t *t = (const tex_t *)o->tex[idx];
uint64_t period = present_period(o), now = kern_now_ns();
uint64_t slot = now + pace(&o->pace, now, period);
gfx_backend()->present(t, o->crop, slot);
o->ticket[idx] = o->presents;
o->avail[idx] = slot + period;
uint64_t *fe = img_fence_of(o, idx);
if (fe) *fe = nvn_fence_last(); // the frame drawn into it (nvnWindowAcquireTexture waits for it)
last_present_fence = nvn_fence_last();
frame_step_present(slot, o->interval); // frame_rate.c: the real-time step follows the display times
rt_frame_presented(t, o->presents);
}

View File

@@ -425,6 +425,11 @@ VkImage vk_tonemap_record(VkCommandBuffer cb, VkImage src, VkFormat sf, uint32_t
// its storage address is created, dropped, released or taken back into use. Unlocked; read it before the lookup it
// guards (a lookup may create an entry). 0 when the slot was never registered: nothing is kept under 0.
uint64_t vk_texture_slot_token(int id);
// Slots whose token may have changed since *pos (0 at first; then the value these calls leave): fn is called for each
// (some more than once) and *pos advanced. 0 when too many changes came since: the caller looks at every slot. *pos
// from vk_texture_dirty_pos_locked after such a walk. Texture lock held.
int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void *arg);
uint64_t vk_texture_dirty_pos_locked(void);
void vk_sampler_dlss_bias(float bias); // texture LOD bias added for DLSS upscaling (material samplers)
uint32_t vk_sampler_gen(void); // changes whenever a sampler slot is registered
void vk_texture_gpu_written(const tex_t *t); // rendered or copied into by the GPU: no re-upload from storage

View File

@@ -58,7 +58,7 @@ static void present_main(void *arg) {
busy = 1;
plat_cond_broadcast(&pcv);
plat_mutex_unlock(&pl);
if (flight_on) vk_thumbs_frame(j.frame);
if (flight_on || vk_dump_white) vk_thumbs_frame(j.frame); // NX2NAVI_DUMP_WHITE reads them too
vk_present_blit(j.image, VK_IMAGE_LAYOUT_GENERAL, j.format, j.w, j.h, j.has_crop ? j.crop : NULL);
uint64_t f = vk_sync_submit(NULL, 0); // a timeline value after the blit: deferred destruction waits for the blit too
if (j.value && f != j.value)

View File

@@ -116,9 +116,14 @@ void vk_caps_init(VkCtx *c) {
// BotW and NX2navi on AMD). The translator biases gather coordinates by the host's sub-texel precision; NVIDIA rounds
// as Maxwell does. NX2NAVI_GATHER_BIAS=<bits> forces it (0: off).
uint32_t gather = env_u32("NX2NAVI_GATHER_BIAS", c->props.vendorID == 0x10de ? 0 : l->subTexelPrecisionBits);
// A register read before any write is an undefined load in SPIR-V, and Mesa folds undefined values freely, so on
// Mesa function-local variables start at 0 (docs/maxwell.md 3.12). NX2NAVI_ZERO_LOCALS=1 forces it, 0 turns it off.
uint32_t zero_locals = env_u32("NX2NAVI_ZERO_LOCALS", (uint32_t)vk_caps.mesa);
// A register read before any write is an undefined load in SPIR-V, so function-local variables start at 0
// (docs/maxwell.md 3.12). Mesa folds undefined values freely; NVIDIA's compiler gives such a read a value of another
// lane of the warp: Ocarina's clustered-light loop reads registers that only some paths write, and on an RTX 5090
// the Great Fairy Fountain floor took an extra light term in 4x8-pixel tiles (one 32-lane warp each) and Link went
// dark with green speckles. With DLAA on (vk_dlaa.c, which finds the game's FXAA by its SPIR-V hash) they stay
// undefined off Mesa, as before: zeroing moved that hash and the picture shook under DLAA (maxwell.md 3.12).
// NX2NAVI_ZERO_LOCALS=1 / 0 forces either.
uint32_t zero_locals = env_u32("NX2NAVI_ZERO_LOCALS", vk_caps.mesa || !cfg_bool("NX2NAVI_DLAA"));
gfx_shader_set_host(vk_caps.float64, vk_caps.sbuf_align, told, gather, zero_locals != 0);
if (gather) rt_log("vk caps: gather coordinates biased for %u sub-texel bits (NX2NAVI_GATHER_BIAS)", gather);

View File

@@ -5,7 +5,8 @@
// written in the frame are copied once each, slices side by side. Files in NX2NAVI_DUMP_DIR (default "dump"):
// pass_<seq>_t<i>_<w>x<h>_<vkformat>_<name>.png colour target i (td: depth; img<slot>: storage image; lut<id>: 3D;
// a BC target drawn through a block view: its blocks as that view)
// ..._tm.png float formats: x/(1+x) of the same image (the plain one clamps)
// ..._tm.png float formats: x/(1+x) of the same image (the plain one clamps);
// in both, a pixel with a NaN channel is magenta
// passes.txt per pass or dispatch: targets, draws, each distinct pipeline + texture set with its shaders' SPIR-V
// hashes (sh_<hash>.spv written once) and the bound textures with their samplers
// images.txt per image: channel min/max, distinct texel values over the image and over its top quarter
@@ -35,6 +36,7 @@
#include "vk.h"
#include "vk_exec.h"
#include "flight.h"
uint64_t vk_exec_cs_storage_addr(const VkExec *ex, const GfxShaderRes *r); // vk_exec_draw.c
int vk_exec_raw_9e5(VkImage img, VkFormat f); // vk_exec_draw.c
#include "platform/platform.h"
@@ -49,6 +51,10 @@ int vk_exec_raw_9e5(VkImage img, VkFormat f); // v
int vk_dump_armed;
static uint64_t dump_frame, log_from; // log_from: NX2NAVI_DUMP_LOG, first frame logged to passes.txt without images
static char dir[400];
static char base_dir[400]; // NX2NAVI_DUMP_DIR; with NX2NAVI_DUMP_WHITE each attempt goes to base_dir/f<frame>
static uint64_t white_last; // NX2NAVI_DUMP_WHITE: the frame dumped last (0: none), checked 3 presents on
static int white_tries, white_caught;
static uint64_t dump_after; // NX2NAVI_DUMP_AFTER
static FILE *passes_f, *images_f;
static int seq, finished, copied; // copied: passes and dispatches of the dump frame whose images were copied
static int skip_runs, dump_ubo; // NX2NAVI_DUMP_SKIP_RUNS, NX2NAVI_DUMP_UBO (vec4 per block, 0 for none)
@@ -221,8 +227,8 @@ static void write_images(const Job *j) {
uint32_t pw = j->w * j->d, ph = j->h;
uint64_t n = (uint64_t)pw * ph;
float *px = malloc(n * 16);
uint8_t *out = malloc(n * 4);
if (!px || !out) rt_fatal("vk_dump: out of memory for %s", j->name);
uint8_t *out = malloc(n * 4), *isnan_ = calloc(n, 1); // isnan_: a pixel with a NaN channel, drawn magenta
if (!px || !out || !isnan_) rt_fatal("vk_dump: out of memory for %s", j->name);
float mn[4] = {INFINITY, INFINITY, INFINITY, INFINITY}, mx[4] = {-INFINITY, -INFINITY, -INFINITY, -INFINITY};
uint64_t nans = 0;
for (uint32_t z = 0; z < j->d; z++)
@@ -231,7 +237,7 @@ static void write_images(const Job *j) {
float *o = px + 4 * ((uint64_t)y * pw + (uint64_t)z * j->w + x);
decode(j->f, j->map + (((uint64_t)z * j->h + y) * j->w + x) * j->f->bytes, o);
for (int c = 0; c < 4; c++) {
if (isnan(o[c])) { nans++, o[c] = 0; continue; }
if (isnan(o[c])) { nans++, o[c] = 0, isnan_[(uint64_t)y * pw + (uint64_t)z * j->w + x] = 1; continue; }
if (o[c] < mn[c]) mn[c] = o[c];
if (o[c] > mx[c]) mx[c] = o[c];
}
@@ -255,6 +261,7 @@ static void write_images(const Job *j) {
out[4 * i + c] = u8(x);
}
out[4 * i + 3] = 255;
if (isnan_[i]) out[4 * i] = 255, out[4 * i + 1] = 0, out[4 * i + 2] = 255;
}
char path[600];
snprintf(path, sizeof path, "%s/%s%s.png", dir, j->name, pass ? "_tm" : "");
@@ -270,6 +277,7 @@ static void write_images(const Job *j) {
}
plat_mutex_unlock(&img_lock);
free(out);
free(isnan_);
free(px);
}
@@ -527,9 +535,12 @@ static void flush_luts(VkExec *ex, int n) {
VkFormat bf = block_format(l->format);
snprintf(name, sizeof name, "pass_%04d_%s%d_%ux%ux%u_%d_%s%s", n, l->is2d ? "tex" : "lut", l->id, l->w, l->h, l->d,
(int)l->format, fmt_name(l->format, fn), bf ? "_blocks" : "");
// a sampled depth texture copies its depth aspect (the colour aspect of a depth image read as zeros)
VkImageAspectFlags asp = l->format >= VK_FORMAT_D16_UNORM && l->format <= VK_FORMAT_D32_SFLOAT_S8_UINT
? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
const char *why = bf ? copy_region(ex, l->image, bf, VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 0,
(VkExtent3D){l->w, l->h, l->d}, (l->w + 3) / 4, (l->h + 3) / 4, l->d, name)
: copy_out(ex, l->image, l->format, VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, l->w, l->h, l->d, name);
: copy_out(ex, l->image, l->format, asp, 0, 0, l->w, l->h, l->d, name);
fprintf(passes_f, " %s tex %d -> %s.png%s%s\n", l->is2d ? "2D" : "3D", l->id, name, why ? " skipped: " : "",
why ? why : "");
}
@@ -572,8 +583,12 @@ static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info)
}
if (st == 5) sp(b, " -> %#llx", (unsigned long long)vk_exec_cs_storage_addr(ex, r));
for (int k = 0; k < 16; k++)
if (ex->ssbo[st][k].addr)
sp(b, " %d=%#llx+%#llx", k, (unsigned long long)ex->ssbo[st][k].addr, (unsigned long long)ex->ssbo[st][k].size);
if (ex->ssbo[st][k].addr) {
VkDescriptorBufferInfo cb; // "(copy)": a binding that only reads it uses the submit-time copy (vk_usnap.c)
int copy = vk_usnap_find(ex->ssbo[st][k].addr, ex->ssbo[st][k].size, 1, &cb);
sp(b, " %d=%#llx+%#llx%s", k, (unsigned long long)ex->ssbo[st][k].addr, (unsigned long long)ex->ssbo[st][k].size,
copy ? "(copy)" : "");
}
sp(b, "\n");
// NX2NAVI_DUMP_UBO: also the small storage buffers' guest bytes (GPU results as of the last write-back); per-view
// constants (exposure and such) live in such buffers.
@@ -624,10 +639,11 @@ static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info)
const VkTexImage *t = vk_texture_by_id(NVN_HANDLE_TEX(h), &v, &hv);
if (!t) { sp(b, "tex %d unresolved (dummy)\n", NVN_HANDLE_TEX(h)); continue; }
static const char *vt[] = {"1D", "2D", "3D", "cube", "1DArray", "2DArray", "cubeArray"};
sp(b, "tex %d %s(%d) %ux%ux%u levels %u layers %u image %s", NVN_HANDLE_TEX(h), fmt_name(t->format, fn),
sp(b, "tex %d %s(%d) %ux%ux%u levels %u layers %u image %s vkimage %p", NVN_HANDLE_TEX(h), fmt_name(t->format, fn),
(int)t->format, t->width, t->height, t->depth, t->levels, t->layers,
t->view_type <= 6 ? vt[t->view_type] : "?");
t->view_type <= 6 ? vt[t->view_type] : "?", (void *)t->image);
if (hv && v.set_format) sp(b, " view-nvn-fmt %#x", v.format);
if (hv && v.set_target) sp(b, " view-target %u", v.target);
if (hv && v.set_levels) sp(b, " view-levels %u+%u", v.base_level, v.nlevels);
if (hv && v.set_layers) sp(b, " view-layers %u+%u", v.min_layer, v.nlayers);
samp_par s;
@@ -730,9 +746,9 @@ static void target_line(VkExec *ex, const VkExecTarget *t, const char *tag, int
t->height, 1, name) :
t->is3d ? "3D slice (the volume is dumped as lut<id> where it is sampled)" :
copy_out(ex, t->image, t->format, t->aspect, t->level, t->base_layer, t->width, t->height, 1, name);
fprintf(passes_f, " %s: nvn %#x %s(%d) %ux%u level %u layers %u+%u samples %u addr %#llx -> %s.png%s%s\n", tag,
t->nvn_format, fmt_name(t->format, fn), (int)t->format, t->width, t->height, t->level, t->base_layer,
t->layers, t->samples, (unsigned long long)(uintptr_t)t->tex->mem, name, why ? " skipped: " : "",
fprintf(passes_f, " %s: nvn %#x %s(%d) %ux%u level %u layers %u+%u samples %u addr %#llx vkimage %p -> %s.png%s%s\n",
tag, t->nvn_format, fmt_name(t->format, fn), (int)t->format, t->width, t->height, t->level, t->base_layer,
t->layers, t->samples, (unsigned long long)(uintptr_t)t->tex->mem, (void *)t->image, name, why ? " skipped: " : "",
why ? why : "");
}
@@ -755,6 +771,11 @@ static void pass_decide(int same_targets) {
void vk_dump_pass_end(VkExec *ex) {
plat_mutex_lock(&rec_lock);
if (dump_after && dump_frame == UINT64_MAX && ex->depth.tex && (uint64_t)(uintptr_t)ex->depth.tex->mem == dump_after) {
dump_frame = frame_now(); // this pass and the rest of its frame
rt_log("vk_dump: a pass drew into %#llx in frame %llu: it and the rest of the frame go to %s",
(unsigned long long)dump_after, (unsigned long long)dump_frame, dir);
}
if (!active()) goto out;
int n = seq++;
uint64_t sig = targets_sig(ex);
@@ -893,9 +914,34 @@ out:
static void make_dirs(char *path) { plat_file_mkdir_p(path); }
// NX2NAVI_DUMP_WHITE=1 (diagnosis): no frame is named; from frame 1800 each present looks at the thumbnail of the frame
// three presents back (gfx_present.c vk_thumbs_*), and the first one whose picture left of the overlay and above the
// dialogue box is blank white makes the dump take the next frame (white flashes come in runs of several frames).
int vk_dump_white; // vk_backend.c asks for a thumbnail of every present while it is set
#define dump_white vk_dump_white
// passes.txt and images.txt in dir (a new folder per NX2NAVI_DUMP_WHITE attempt)
static void open_files(void) {
make_dirs(dir);
char path[600];
snprintf(path, sizeof path, "%s/passes.txt", dir);
passes_f = fopen(path, "w");
snprintf(path, sizeof path, "%s/images.txt", dir);
images_f = fopen(path, "w");
if (!passes_f || !images_f) rt_fatal("vk_dump: cannot write to %s", dir);
}
void vk_dump_init(void) {
const char *e = cfg_get("NX2NAVI_DUMP");
if (!e || !*e || !(dump_frame = strtoull(e, NULL, 10))) return;
dump_white = rt_knob("DUMP_WHITE", "0");
// NX2NAVI_DUMP_AFTER=<address>: the first render pass whose depth target lives at that address and the rest of its frame
// (a target drawn once, at an area load)
const char *da = cfg_get("NX2NAVI_DUMP_AFTER");
dump_after = da && *da ? strtoull(da, NULL, 0) : 0;
if (dump_white || dump_after) {
dump_frame = UINT64_MAX;
vk_thumbs_enable(16);
} else if (!e || !*e || !(dump_frame = strtoull(e, NULL, 10))) return;
const char *lf = cfg_get("NX2NAVI_DUMP_LOG");
if (lf && *lf) log_from = strtoull(lf, NULL, 10);
const char *sr = cfg_get("NX2NAVI_DUMP_SKIP_RUNS");
@@ -912,14 +958,10 @@ void vk_dump_init(void) {
w[1] = *q == 'x' ? (uint32_t)strtoul(q + 1, &q, 10) : 0;
}
const char *d = cfg_get("NX2NAVI_DUMP_DIR");
snprintf(dir, sizeof dir, "%s", d && *d ? d : "dump");
make_dirs(dir);
snprintf(base_dir, sizeof base_dir, "%s", d && *d ? d : "dump");
snprintf(dir, sizeof dir, "%s", base_dir);
char path[600];
snprintf(path, sizeof path, "%s/passes.txt", dir);
passes_f = fopen(path, "w");
snprintf(path, sizeof path, "%s/images.txt", dir);
images_f = fopen(path, "w");
if (!passes_f || !images_f) rt_fatal("vk_dump: cannot write to %s", dir);
if (!dump_white) open_files();
const char *bf = cfg_get("NX2NAVI_DUMP_BUF");
if (bf && *bf) {
char *q;
@@ -937,7 +979,43 @@ void vk_dump_init(void) {
rt_log("vk_dump: frame %llu goes to %s", (unsigned long long)dump_frame, dir);
}
// The thumbnail of frame f is blank white left of the overlay and above the dialogue box: -1 not available, 0 no, 1 yes.
static int thumb_white(uint64_t f) {
static uint8_t px[FLIGHT_THUMB_W * FLIGHT_THUMB_H * 4];
if (!vk_thumbs_read(f, px)) return -1;
int n = 0, bright = 0;
for (int y = 0; y < 105; y++)
for (int x = 0; x < 250; x++, n++) {
const uint8_t *q = px + 4 * (y * FLIGHT_THUMB_W + x);
bright += (q[0] * 30 + q[1] * 59 + q[2] * 11) / 100 > 245;
}
return bright * 100 > n * 97;
}
static void white_check(uint64_t frame) {
static int run; // consecutive white thumbnails: white runs are mostly 1-2 frames, too short for a dump 4 frames on
if (frame < 1800) return;
int w = thumb_white(frame - 3);
if (w < 0) return;
run = w ? run + 1 : 0;
if (run >= 3) { // frames frame-5..frame-3 white: the run is likely still on 4 frames later
run = 0;
dump_frame = frame + 1;
snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)dump_frame);
open_files();
rt_log("vk_dump: frames %llu-%llu were white; frame %llu goes to %s", (unsigned long long)(frame - 5),
(unsigned long long)(frame - 3), (unsigned long long)dump_frame, dir);
}
}
void vk_dump_present(uint64_t frame) {
if (dump_white && white_last && frame == white_last + 3) { // was the dumped frame itself white?
int w = thumb_white(white_last);
rt_log("vk_dump: dumped frame %llu was %s", (unsigned long long)white_last, w > 0 ? "white: done" : "not white: trying again");
if (w > 0 || ++white_tries >= 8) white_caught = 1;
white_last = 0;
}
if (dump_white && !white_caught && !white_last && dump_frame == UINT64_MAX) white_check(frame);
if (frame != dump_frame) return;
plat_mutex_lock(&rec_lock);
finished = 1;
@@ -952,4 +1030,13 @@ void vk_dump_present(uint64_t frame) {
passes_f = NULL;
rt_log("vk_dump: frame %llu: %d passes and dispatches, about %llu MB of PNGs in %s", (unsigned long long)frame, seq,
(unsigned long long)(png_bytes >> 20), dir);
if (dump_white) { // ready for another attempt (vk_dump_present checks this one's thumbnail 3 presents on)
white_last = frame;
plat_mutex_lock(&rec_lock);
seq = copied = 0, last_targets = 0, pass_skip = NULL, png_bytes = 0;
nluts = nluts_done = nents = ents_over = pass_draws = 0;
dump_frame = UINT64_MAX;
finished = 0;
plat_mutex_unlock(&rec_lock);
}
}

View File

@@ -63,6 +63,7 @@ void vk_exec_frame_written(const tex_t *t) {
}
void vk_exec_frame_log(const tex_t *p, uint64_t frame) {
vk_exec_cond_frame_end(frame); // a frame boundary in replay order (present_image runs on the replay thread)
if (!(rt_trace & RT_TRACE_GFX)) return;
VkExecFrame *f = &vk_exec_frame;
int hit = 0;
@@ -743,6 +744,13 @@ static void clear_attachment(VkExec *ex, const VkClearAttachment *a) {
static void clear_cond_log(const VkExec *ex, const char *what, int index) {
static int on = -1, n;
if (on < 0) on = rt_knob("LOG_RCOND", "0");
if (on == 2 && index < 0 && ex->has_render_cond && ex->depth.width >= 1600) { // full-size depth clears, all of them
char why[256];
vk_exec_cond_why(&ex->render_cond, why, sizeof why);
rt_log("SEQ depth clear %#llx %s; %s", ex->depth.img ? (unsigned long long)ex->depth.img->addr : 0ull,
ex->render_disabled ? "skipped" : "RUN", why);
return;
}
if (!on || !ex->has_render_cond || n >= 400) return;
n++;
const VkExecTarget *t = index < 0 ? &ex->depth : index < (int)ex->pass_ncolor ? &ex->color[index] : NULL;
@@ -754,6 +762,7 @@ static void clear_cond_log(const VkExec *ex, const char *what, int index) {
static void clear_color(VkExec *ex, const p_clear_color *c) {
vk_exec_frame.clears++;
clear_cond_log(ex, "colour", c->index);
if (ex->has_render_cond) ex->render_disabled ? vk_cond_clears_skip++ : vk_cond_clears_run++;
if (ex->render_disabled) return;
resolve_rt(ex);
int i = c->index;
@@ -785,6 +794,7 @@ static void clear_color(VkExec *ex, const p_clear_color *c) {
static void clear_ds(VkExec *ex, const p_clear_ds *c) {
clear_cond_log(ex, "depth", -1);
if (ex->has_render_cond) ex->render_disabled ? vk_cond_clears_skip++ : (vk_cond_clears_run++, vk_cond_depth_run++);
if (ex->render_disabled) return;
resolve_rt(ex);
const VkExecTarget *t = &ex->depth;

View File

@@ -38,6 +38,10 @@ typedef struct {
uint8_t scale; // VkTexImage.scale
} VkExecTarget;
void vk_exec_cond_frame_end(uint64_t frame); // vk_exec_xfer.c: a present, in replay order (NX2NAVI_LOG_RCOND)
extern unsigned vk_cond_clears_run, vk_cond_clears_skip, vk_cond_draws_skip, vk_cond_depth_run;
void vk_exec_cond_why(const p_render_cond *c, char *buf, size_t cap); // NX2NAVI_LOG_RCOND=ab
// The replay state. One instance lives for the run: NVN state is queue state and carries over from one submission to
// the next, as in MacNativeGames' and LAS Recomp's replays. The "per command buffer" fields are reset by vk_exec_record.
typedef struct VkExec {

View File

@@ -1526,13 +1526,33 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto
// rewrites the entries whose slot changed (vk_texture_slot_token) and fills the kinds a new pipeline asked for; with
// update-after-bind (descriptorIndexing) a write never invalidates a command buffer being recorded. Each submit's first
// sync also looks every registered slot up once, the freshness check a bound texture gets (vk_texture_by_id_locked).
// The entries are not double-buffered: a slot registered again while a frame using it is in flight is read new by that
// frame, as on the Switch, where the pool is memory the GPU reads as it goes.
// The set is one of a ring of POOL_SETS, one per submission: on the Switch the GPU reads the pool as it goes, but it
// runs a submission right after nvnQueueSubmitCommands, so what it reads is the pool as of about the submit. Here the
// GPU runs a submission several milliseconds later, and Ocarina registers slots for its next submission right after
// submitting: with one set, an in-flight submission sampled a texture registered for the next one, the lit materials
// of distant characters read the wrong texture as their light or probe data, wrote NaN and bloom spread it (white
// dialogue frames, a fifth of them; none once each submission waited for its GPU work, NX2NAVI_SUBMIT_FINISH). A
// submission's first pool_sync takes the next set (pool_take_set), waits for the GPU only if that set's last
// submission is still running, and brings it up to date from the change log (every entry written since the set was
// last current; the whole set when the log has wrapped). Writes within a submission go to its own set at once, as
// before.
volatile uint32_t vk_pool_kinds_used;
#define POOL_SETS 4
#define POOL_LOG (1u << 20) // entries of the change log: binding * VK_POOL_TEX + id, one per write
static struct {
VkDescriptorSetLayout layout, empty;
VkDescriptorPool dpool;
VkDescriptorSet set;
VkDescriptorSet set; // the current submission's (sets[cur])
VkDescriptorSet sets[POOL_SETS];
uint64_t set_fence[POOL_SETS]; // the timeline value of the last submission that bound it
uint64_t set_synced[POOL_SETS]; // the change-log position up to which it holds every write
int nsets, cur, all; // all: the first fill, written to every set
uint64_t cur_fence; // the submission sets[cur] serves
uint32_t *log; // POOL_LOG entries
uint64_t logn; // writes so far
uint64_t *last; // per entry: the log position of its newest write (catch-up writes it once, there)
uint64_t switches, waits, catchup, written, rewrites, catch_ns, sync_ns, n_slots, n_refill, n_fresh;
uint64_t dirty_pos; // vk_texture_dirty_slots_locked position (0: the next sync walks every slot)
int ready, uab;
uint32_t kinds, gen, epoch, sgen;
int retry; // the last sync left slots to the creation budget
@@ -1567,19 +1587,33 @@ static VkDescriptorSetLayout pool_layout_make(void) {
if (vkCreateDescriptorSetLayout(c->device, &li, NULL, &l) != VK_SUCCESS) rt_fatal("vk: the texture pool layout failed");
VkDescriptorPoolSize ps[2] = {{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, VK_POOL_KINDS * VK_POOL_TEX},
{VK_DESCRIPTOR_TYPE_SAMPLER, VK_POOL_SAMP}};
VkDescriptorPoolCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = PL.uab ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0,
.maxSets = 1, .poolSizeCount = 2, .pPoolSizes = ps};
if (vkCreateDescriptorPool(c->device, &pi, NULL, &PL.dpool) != VK_SUCCESS) rt_fatal("vk: the texture pool descriptor pool failed");
VkDescriptorSetAllocateInfo ai = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, .descriptorPool = PL.dpool,
.descriptorSetCount = 1, .pSetLayouts = &l};
if (vkAllocateDescriptorSets(c->device, &ai, &PL.set) != VK_SUCCESS) rt_fatal("vk: the texture pool set failed");
// POOL_SETS sets, fewer where the device has no room for them (one at the least, as before)
for (int n = POOL_SETS; n >= 1 && !PL.nsets; n--) {
VkDescriptorPoolSize psn[2] = {{ps[0].type, ps[0].descriptorCount * (uint32_t)n},
{ps[1].type, ps[1].descriptorCount * (uint32_t)n}};
VkDescriptorPoolCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = PL.uab ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0,
.maxSets = (uint32_t)n, .poolSizeCount = 2, .pPoolSizes = psn};
if (vkCreateDescriptorPool(c->device, &pi, NULL, &PL.dpool) != VK_SUCCESS) continue;
VkDescriptorSetLayout ls[POOL_SETS];
for (int i = 0; i < n; i++) ls[i] = l;
VkDescriptorSetAllocateInfo ai = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, .descriptorPool = PL.dpool,
.descriptorSetCount = (uint32_t)n, .pSetLayouts = ls};
if (vkAllocateDescriptorSets(c->device, &ai, PL.sets) == VK_SUCCESS) PL.nsets = n;
else vkDestroyDescriptorPool(c->device, PL.dpool, NULL), PL.dpool = VK_NULL_HANDLE;
}
if (!PL.nsets) rt_fatal("vk: the texture pool set failed");
PL.set = PL.sets[0];
PL.log = malloc(sizeof *PL.log * POOL_LOG);
PL.last = calloc((size_t)VK_POOL_KINDS * VK_POOL_TEX + VK_POOL_SAMP, sizeof *PL.last);
if (!PL.log || !PL.last) rt_fatal("vk: out of memory");
PL.tok = calloc(VK_POOL_TEX, sizeof *PL.tok);
PL.view = calloc((size_t)VK_POOL_KINDS * VK_POOL_TEX, sizeof *PL.view);
PL.samp = calloc(VK_POOL_SAMP, sizeof *PL.samp);
if (!PL.tok || !PL.view || !PL.samp) rt_fatal("vk: out of memory");
rt_log("vk: bindless texture pools: %d kinds x %d textures, %d samplers, %s", VK_POOL_KINDS, VK_POOL_TEX, VK_POOL_SAMP,
PL.uab ? "update after bind" : "no update-after-bind on this GPU (entries written while bound are undefined)");
rt_log("vk: bindless texture pools: %d kinds x %d textures, %d samplers, %s, %d sets (one per submission in turn)",
VK_POOL_KINDS, VK_POOL_TEX, VK_POOL_SAMP,
PL.uab ? "update after bind" : "no update-after-bind on this GPU (entries written while bound are undefined)", PL.nsets);
return l;
}
@@ -1620,16 +1654,83 @@ static void pool_flush(void) {
pool_nw = 0;
}
static void pool_write(uint32_t binding, uint32_t id, VkImageView view, VkSampler samp) {
static void pool_write_to(VkDescriptorSet set, uint32_t binding, uint32_t id, VkImageView view, VkSampler samp) {
if (pool_nw == POOL_WBATCH) pool_flush();
pool_ii[pool_nw] = (VkDescriptorImageInfo){samp, view, VK_IMAGE_LAYOUT_GENERAL};
pool_w[pool_nw] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = PL.set,
pool_w[pool_nw] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, .dstSet = set,
.dstBinding = binding, .dstArrayElement = id, .descriptorCount = 1,
.descriptorType = view ? VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE : VK_DESCRIPTOR_TYPE_SAMPLER,
.pImageInfo = &pool_ii[pool_nw]};
pool_nw++;
}
// An entry whose wanted value PL.view / PL.samp already hold: into the current set, and into the log for the others
// (the first fill goes into every set).
static void pool_write(uint32_t binding, uint32_t id, VkImageView view, VkSampler samp) {
if (PL.all) {
for (int i = 0; i < PL.nsets; i++) pool_write_to(PL.sets[i], binding, id, view, samp);
return;
}
pool_write_to(PL.set, binding, id, view, samp);
uint32_t e = binding * VK_POOL_TEX + id;
PL.last[e] = PL.logn;
PL.log[PL.logn++ % POOL_LOG] = e;
}
// Writes entry e (binding * VK_POOL_TEX + id) of set as PL.view / PL.samp hold it now.
static void pool_entry_to(VkDescriptorSet set, uint32_t e) {
uint32_t binding = e / VK_POOL_TEX, id = e % VK_POOL_TEX;
if (binding < VK_POOL_KINDS) {
VkImageView v = PL.view[(size_t)binding * VK_POOL_TEX + id];
if (v) pool_write_to(set, binding, id, v, VK_NULL_HANDLE);
} else if (id < VK_POOL_SAMP && PL.samp[id]) {
pool_write_to(set, binding, id, VK_NULL_HANDLE, PL.samp[id]);
}
}
// A submission's first pool use: the next set of the ring, once the GPU is done with it. 1 when it switched; *from is
// the log position the set holds writes up to (pool_catch_up brings it level once pool_sync has refreshed the entries:
// a view written into a set must be alive, and the refresh is what replaces the views of destroyed textures).
static int pool_take_set(VkExec *ex, uint64_t *from) {
if (PL.cur_fence == ex->fence) return 0;
int i = (PL.cur + 1) % PL.nsets;
if (PL.nsets > 1) {
const GfxBackend *b = gfx_backend();
if (b->fence_done() < PL.set_fence[i]) PL.waits++, b->fence_wait(PL.set_fence[i], UINT64_MAX);
}
*from = PL.set_synced[i];
PL.cur = i, PL.set = PL.sets[i], PL.cur_fence = ex->fence, PL.set_fence[i] = ex->fence;
return PL.nsets > 1;
}
// The current set gets the entries written into other sets in [from, to) of the log, as PL.view / PL.samp hold them now.
static void pool_catch_up(uint64_t from, uint64_t to) {
VkDescriptorSet set = PL.set;
if (to - from > POOL_LOG) { // the log has wrapped since: every entry
PL.rewrites++;
for (uint32_t e = 0; e < VK_POOL_KINDS * VK_POOL_TEX + VK_POOL_SAMP; e++) pool_entry_to(set, e);
} else {
// each entry once, at its newest write: an entry written again later (also by this submission's own sync,
// which wrote it into this set already) is skipped at the older positions
PL.catchup += to - from;
for (uint64_t q = from; q < to; q++) {
uint32_t e = PL.log[q % POOL_LOG];
if (PL.last[e] == q) pool_entry_to(set, e), PL.written++;
}
}
pool_flush();
PL.switches++;
if (PL.switches % 3600 == 0)
rt_log("vk: texture pool sets: %llu switches, %llu waited for the GPU, %.1f log entries and %.1f writes per "
"catch-up, %llu whole rewrites; %.3f ms catch-up and %.3f ms entry refresh per switch (%.0f slots, %.0f "
"refilled, %.0f checked)",
(unsigned long long)PL.switches, (unsigned long long)PL.waits, (double)PL.catchup / (double)PL.switches,
(double)PL.written / (double)PL.switches, (unsigned long long)PL.rewrites,
PL.catch_ns / 1e6 / (double)PL.switches, PL.sync_ns / 1e6 / (double)PL.switches,
(double)PL.n_slots / (double)PL.switches, (double)PL.n_refill / (double)PL.switches,
(double)PL.n_fresh / (double)PL.switches);
}
// Entry id of every kind in mask: the slot's view of that kind or the kind's stand-in. Texture lock held. 0: the
// slot's texture was left to a later sync by the creation budget (pool_sync).
// NX2NAVI_SKIP_SHADERS=<hash>,...: draws and dispatches whose shader's SPIR-V has one of these FNV-1a hashes (the "vs"/"fs"/
@@ -1684,8 +1785,38 @@ static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) {
return !vk_texture_create_deferred_locked();
}
static void pool_sync_entries(VkExec *ex);
static void pool_sync(VkExec *ex) {
if (!PL.layout) vk_pool_set_layout();
uint64_t from = 0, pre = PL.logn;
int switched = pool_take_set(ex, &from); // every submission its own set, before the shortcut
uint64_t t0 = kern_now_ns();
pool_sync_entries(ex);
uint64_t t1 = kern_now_ns();
PL.sync_ns += t1 - t0;
if (switched) {
vk_texture_lock();
pool_catch_up(from, pre); // what the other sets got since; this sync's own writes went into it already
vk_texture_unlock();
PL.catch_ns += kern_now_ns() - t1;
}
PL.set_synced[PL.cur] = PL.logn;
}
typedef struct { VkExec *ex; uint32_t used; int n, retry; } PoolDirty;
static void pool_dirty_slot(int id, void *arg) { // vk_texture_dirty_slots_locked: a slot whose token may have changed
PoolDirty *d = arg;
if (id >= d->n) return;
uint64_t t = vk_texture_slot_token(id);
if (t == PL.tok[id]) return;
PL.n_refill++;
if (pool_fill(d->ex, (uint32_t)id, d->used, t)) PL.tok[id] = t;
else d->retry = 1;
}
static void pool_sync_entries(VkExec *ex) {
uint32_t used = __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED);
uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen();
if (PL.ready && !PL.retry && used == PL.kinds && gen == PL.gen && ep == PL.epoch && sg == PL.sgen) return;
@@ -1694,24 +1825,37 @@ static void pool_sync(VkExec *ex) {
int n = vk_texture_slot_count_locked();
if (n > VK_POOL_TEX) n = VK_POOL_TEX;
if (!PL.ready) { // every entry a stand-in first: partially bound arrays are still read out of range by guests
PL.all = 1;
for (uint32_t id = 0; id < VK_POOL_TEX; id++) pool_fill(ex, id, 0xffffu, 0);
for (uint32_t id = 0; id < VK_POOL_SAMP; id++) PL.samp[id] = Z.samp, pool_write(VK_POOL_KINDS, id, VK_NULL_HANDLE, Z.samp);
pool_flush();
PL.all = 0;
PL.ready = 1, PL.sgen = 0;
}
int retry = 0; // slots whose texture the creation budget left: filled again by the next sync
if (fresh || gen != PL.gen || ep != PL.epoch || PL.retry) {
vk_texture_create_budget_locked(POOL_CREATE_BUDGET);
for (int id = 0; id < n; id++) { // slots past n were never registered: stand-ins since the first sync
// Only a slot change since the last sync (no new kinds, no new submission, nothing left over): the slots that
// changed (vk_texture_dirty_slots_locked), not the whole table
PoolDirty pd = {ex, used, n, 0};
int walked = !fresh && ep == PL.epoch && !PL.retry && PL.dirty_pos &&
vk_texture_dirty_slots_locked(&PL.dirty_pos, pool_dirty_slot, &pd);
retry = pd.retry;
if (!walked) PL.dirty_pos = vk_texture_dirty_pos_locked();
for (int id = 0; id < n && !walked; id++) { // slots past n were never registered: stand-ins since the first sync
uint64_t t = vk_texture_slot_token(id);
if (t != PL.tok[id]) {
PL.n_refill++;
if (pool_fill(ex, (uint32_t)id, used, t)) PL.tok[id] = t;
else retry = 1;
} else if (t && fresh) {
pool_fill(ex, (uint32_t)id, fresh, t);
} else if (t && ep != PL.epoch) {
PL.n_fresh++;
vk_texture_by_id_locked(id, NULL, NULL); // the submit's freshness check
}
}
if (!walked) PL.n_slots += (uint64_t)n;
vk_texture_create_budget_locked(-1);
}
PL.retry = retry;
@@ -2620,7 +2764,7 @@ static int draw_skipped(const VkExec *ex, int mesh, int why) {
// mesh: a mesh draw (op_draw_mesh): the program's mesh and task stages stand in for the vertex stage, and nothing
// vertex-related (vertex buffers, motion vector bookkeeping of vertex shaders) is bound or noted.
static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int mesh) {
if (ex->render_disabled) return draw_skipped(ex, mesh, SK_DISABLED);
if (ex->render_disabled) return vk_cond_draws_skip += ex->has_render_cond, draw_skipped(ex, mesh, SK_DISABLED);
cb_res(ex);
dummies_init();
if (ex->pass_noatt) { // no attachment: the pass covers this draw's viewport or scissor, reopened when it needs more

View File

@@ -124,9 +124,29 @@ static void counter_cb(void *arg) {
// report counts as pending until then, not until its fence passes: the callback runs after the fence, and in between
// the record still held what the CPU wrote (Ocarina re-arms its condition records with 1 for the next frame), so a
// condition read 1 where the GPU had written 0, a depth clear ran after the depth copy and frames showed only sky.
typedef struct { uint64_t addr, seq; volatile uint64_t written; int unknown, type; } Pending;
// frame: the present count (vk_exec_cond_frame_end, in replay order) when the report was replayed. The game arms its
// condition records from the CPU between frames, so within a frame a timestamp report decides the record's value even
// after it reached memory: Ocarina's CPU, running a frame ahead, had re-armed a record with 1 by the time a later depth
// clear of the frame read it, the clear ran after the depth copy and the frame showed only sky (white flashes).
typedef struct { uint64_t addr, seq, frame; volatile uint64_t written; int unknown, type; } Pending;
static Pending pending[PENDING];
static uint64_t report_seq;
static uint64_t cond_frame;
// Per-frame counts of conditional work (NX2NAVI_LOG_RCOND=1, vk_exec_cond_frame_end logs them): clears and
// draws issued under a render condition, run or skipped.
unsigned vk_cond_clears_run, vk_cond_clears_skip, vk_cond_draws_skip, vk_cond_depth_run;
void vk_exec_cond_frame_end(uint64_t frame) {
static int on = -1;
if (on < 0) on = rt_knob("LOG_RCOND", "0");
if (on && (vk_cond_clears_run || vk_cond_clears_skip || vk_cond_draws_skip))
rt_log("vk: frame %llu conditions: clears run %u skipped %u, draws skipped %u, depth clears run %u",
(unsigned long long)frame, vk_cond_clears_run, vk_cond_clears_skip, vk_cond_draws_skip, vk_cond_depth_run);
vk_cond_clears_run = vk_cond_clears_skip = vk_cond_draws_skip = vk_cond_depth_run = 0;
if (on == 2) rt_log("SEQ present %llu", (unsigned long long)frame);
cond_frame++;
}
static Pending *pending_slot(uint64_t addr, int insert);
@@ -149,11 +169,12 @@ static Pending *pending_slot(uint64_t addr, int insert) {
if (a == addr) return e;
if (!insert) continue;
if (!a) { if (!free_) free_ = e; continue; }
if (__atomic_load_n(&e->written, __ATOMIC_ACQUIRE) >= e->seq && (!victim || e->seq < victim->seq)) victim = e;
if (__atomic_load_n(&e->written, __ATOMIC_ACQUIRE) >= e->seq && e->frame != cond_frame && (!victim || e->seq < victim->seq))
victim = e; // done, and not from this frame (whose reports still decide their records)
}
Pending *e = free_ ? free_ : victim;
if (!e) return NULL; // every probed entry still has a report in flight
e->seq = 0, e->unknown = 0, e->type = -1;
e->seq = 0, e->frame = 0, e->unknown = 0, e->type = -1;
__atomic_store_n(&e->written, 0, __ATOMIC_RELEASE);
__atomic_store_n(&e->addr, addr, __ATOMIC_RELEASE);
return e;
@@ -342,7 +363,7 @@ static void report_counter(VkExec *ex, const p_report_counter *p) {
vk_completion_add(ex->fence, counter_cb, a);
}
Pending *e = pending_slot(p->addr, 1);
if (e) e->seq = report_seq, e->unknown = samples, e->type = (int)p->type;
if (e) e->seq = report_seq, e->frame = cond_frame, e->unknown = samples, e->type = (int)p->type;
else LOG_ONCE("vk: a counter report's table neighbourhood is all in flight: conditions on %#llx read memory",
(unsigned long long)p->addr);
}
@@ -352,6 +373,7 @@ static void report_counter(VkExec *ex, const p_report_counter *p) {
// 1: *v read, 0: not readable, 2: a samples-passed report not in memory yet (unknown)
static int counter_word(uint64_t addr, uint64_t *v) {
const Pending *e = pending_slot(addr, 0); // the last report to addr decides
if (e && !e->unknown && e->frame == cond_frame) return *v = 0, 1; // a timestamp/statistics report of this frame
if (e && __atomic_load_n(&e->written, __ATOMIC_ACQUIRE) < e->seq) return *v = 0, e->unknown ? 2 : 1;
VkMemSpan m;
if (nvn_gpu_only(addr) || !vk_mem_resolve(addr, sizeof *v, &m)) return 0;
@@ -366,6 +388,21 @@ static int last_type(uint64_t addr) {
return e ? e->type : -1;
}
// NX2NAVI_LOG_RCOND=ab (diagnosis): the words a condition compares and where each came from: "f" a report replayed
// this frame (0), "p" a pending report (0 or unknown), "m" memory, with the newest report's type and frame.
void vk_exec_cond_why(const p_render_cond *c, char *buf, size_t cap) {
int o = snprintf(buf, cap, "%#llx mode %d:", (unsigned long long)c->addr, c->mode);
for (int k = 0; k < 2 && o < (int)cap; k++) {
uint64_t a = c->addr + 16 * (uint64_t)k;
const Pending *e = pending_slot(a, 0);
const char *src = !e ? "m" : !e->unknown && e->frame == cond_frame ? "f"
: __atomic_load_n(&e->written, __ATOMIC_ACQUIRE) < e->seq ? "p" : "m";
o += snprintf(buf + o, cap - (size_t)o, " %llu %s(type %d, frame %lld of %llu)",
(unsigned long long)*(const uint64_t *)(uintptr_t)a, src, e ? e->type : -1,
e ? (long long)e->frame : -1LL, (unsigned long long)cond_frame);
}
}
static int cond_enabled(const p_render_cond *c) {
uint64_t x, y;
int rx = counter_word(c->addr, &x), ry = counter_word(c->addr + 16, &y);
@@ -546,6 +583,10 @@ static void copy_t2t(VkExec *ex, const p_copy_t2t *p) {
}
ex->recorded++;
vk_texture_gpu_written(p->dst);
static int lg = -1;
if (lg < 0) lg = rt_knob("LOG_RCOND", "0");
if (lg == 2 && (d.img->aspect & VK_IMAGE_ASPECT_DEPTH_BIT) && d.w >= 1600)
rt_log("SEQ depth copy %#llx -> %#llx", (unsigned long long)s.img->addr, (unsigned long long)d.img->addr);
}
// ---- buffer to buffer

View File

@@ -126,11 +126,59 @@ static uint32_t reg_serial;
static uint32_t agen_idx(uint64_t addr) { return (uint32_t)(addr * 0x9E3779B97F4A7C15ull >> 52); }
// Slots that may resolve differently since a position, for the texture pool descriptors (vk_exec_draw.c
// pool_sync_entries), which otherwise looked at every registered slot after each change: Ocarina registers slots all
// through a submission, and the refresh walked the whole table about twenty times per submission (3.5 ms). A ring of
// changes, each a slot id (registered) or an address bucket (an entry at that agen index changed: every slot registered
// over it, kept in per-bucket lists). Lock held for all of it.
#define DIRTY_N (1u << 16)
#define DIRTY_BUCKET 0x80000000u
static uint32_t dirty[DIRTY_N];
static uint64_t ndirty;
static int32_t bucket_head[AGEN_N];
static int32_t *slot_next, *slot_prev; // per slot id: its bucket list links (slot_prev -2: in no list)
static uint16_t *slot_bucket;
static void dirty_push(uint32_t x) { dirty[ndirty++ % DIRTY_N] = x; }
static void tex_gen_bump(uint64_t addr) {
if (!++tex_gen) tex_gen = 1;
slots_gen++;
uint32_t *g = &agen[agen_idx(addr)];
uint32_t b = agen_idx(addr);
uint32_t *g = &agen[b];
__atomic_store_n(g, *g + 1, __ATOMIC_RELEASE);
dirty_push(DIRTY_BUCKET | b);
}
// Moves slot id into bucket b's list (the lists exist once a slot was registered: vk_texture_register).
static void slot_rebucket(int id, uint32_t b) {
if (slot_prev[id] != -2) { // out of its old list
if (slot_prev[id] >= 0) slot_next[slot_prev[id]] = slot_next[id];
else bucket_head[slot_bucket[id]] = slot_next[id];
if (slot_next[id] >= 0) slot_prev[slot_next[id]] = slot_prev[id];
}
slot_bucket[id] = (uint16_t)b;
slot_prev[id] = -1;
slot_next[id] = bucket_head[b];
if (bucket_head[b] >= 0) slot_prev[bucket_head[b]] = id;
bucket_head[b] = id;
}
uint64_t vk_texture_dirty_pos_locked(void) { return ndirty; }
int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void *arg) {
if (!slot_next || ndirty - *pos > DIRTY_N) {
*pos = ndirty;
return 0;
}
for (uint64_t q = *pos; q < ndirty; q++) {
uint32_t x = dirty[q % DIRTY_N];
if (x & DIRTY_BUCKET)
for (int32_t id = bucket_head[x & ~DIRTY_BUCKET]; id >= 0; id = slot_next[id]) fn(id, arg);
else fn((int)x, arg);
}
*pos = ndirty;
return 1;
}
static volatile int32_t tex_ready;
@@ -1109,7 +1157,11 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) {
}
if (!sinfo) {
uint64_t *p = calloc(1 << 20, sizeof *p);
if (!p) rt_fatal("vk: out of memory for texture slot tokens");
slot_next = malloc(sizeof *slot_next << 20), slot_prev = malloc(sizeof *slot_prev << 20);
slot_bucket = calloc(1 << 20, sizeof *slot_bucket);
if (!p || !slot_next || !slot_prev || !slot_bucket) rt_fatal("vk: out of memory for texture slot tokens");
for (int i = 0; i < (1 << 20); i++) slot_prev[i] = -2, slot_next[i] = -1;
for (int i = 0; i < AGEN_N; i++) bucket_head[i] = -1;
__atomic_store_n(&sinfo, p, __ATOMIC_RELEASE);
}
TexSlot *s = &tslots[id];
@@ -1119,6 +1171,8 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) {
slots_gen++;
if (!++reg_serial) reg_serial = 1;
__atomic_store_n(&sinfo[id], (uint64_t)reg_serial << 16 | agen_idx((uintptr_t)t->mem), __ATOMIC_RELEASE);
slot_rebucket(id, agen_idx((uintptr_t)t->mem));
dirty_push((uint32_t)id);
s->has_view = v != NULL;
if (v) s->v = *v;
else memset(&s->v, 0, sizeof s->v);
@@ -1149,6 +1203,11 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) {
e = NULL;
for (Tex *p = nvn_hm_get(&by_addr, &k.addr); p; p = p->next_at) {
if (p->released) continue;
// Only an entry of the slot's target: Ocarina's volumetric fog registers a 3D two-channel volume (32x24x64)
// over the storage of the bloom pass's 480x270 2D target; followed, the fog computes and the fog pass got
// that 2D image, no 3D view can be made of it (vk_exec_draw.c get_view) and they read and wrote stand-ins.
// With the target kept they get the volume itself.
if (p->key.d.target != k.d.target) continue;
if (!any) any = p;
if (same_but_swizzle(&p->key, &k)) { e = p; break; }
}