diff --git a/docs/architecture.md b/docs/architecture.md index bcdbd0f6..b94aa625 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -189,6 +189,10 @@ keeps per-core data without locks and relies on that, so the host models it (`ru they are ready (`mesh_fallback_workers`, auto: one for each 4 logical processors, 1 to 4, so a place's programs, which all arrive in its first frames, are not made one after the other); they are not recorded for the next start, where the pipeline cache makes them quick. + - The game draws a culled list of grass or leaves twice from one indirect record, depth first and colour after, and + the depth draw's smaller program is ready first. While the colour draw is skipped for its modules or pipeline, + the depth draw of the same record is skipped too (`mfb_hold_role`, `mesh_fallback_hold`): alone it left the + list's depth in the scene with nothing drawn over it, and the sky's colour showed through the ground. Not carried: the far shadow cascades' coarser shading rate, a draw's first workgroup other than 0, and more workgroups in one draw than one compute dispatch may have. - Pipelines: `VK_EXT_graphics_pipeline_library` where the driver has it: stage libraries compiled ahead, a fast link diff --git a/docs/releases/v0.10.2.md b/docs/releases/v0.10.2.md index 2f4b738f..25d761df 100644 --- a/docs/releases/v0.10.2.md +++ b/docs/releases/v0.10.2.md @@ -34,6 +34,9 @@ cards). - **AMD Radeon RX 5000 and 6000 cards losing the device** (the game ending with code 72): on AMD's driver these cards now take the mesh shader fallback by default (`mesh_fallback = auto`), and a lost device is noticed and reported sooner. Not yet confirmed by players with these cards: please report how it goes. +- **Sky-coloured holes in the ground where grass is still loading**, on cards that take the mesh shader fallback: a + patch of grass or leaves is now left out whole until it is ready, where its depth alone was drawn before + (`mesh_fallback_hold = 0` brings that back). Not yet confirmed by players with these cards either. - **Crashes and hangs found in a review of the whole runtime**; none was seen in our own play, and each fix has a check of its own: - a crash when the game read memory the lazy write-back was just handing to the GPU (NVIDIA and Intel cards); @@ -88,6 +91,6 @@ cards). - In `nx2navi.ini`, a comment after a value (`pace_smooth = 0 ; off`) and the words `off`, `false` and `no` now mean what they say for on/off settings; before, such a line could turn the setting on. Paths and names keep their whole text. -- New: `reflex_limit`, `ocarina_timing`, `audio_strict`, `hang_watch_s`, and switches that bring back the earlier - behaviour of this release's larger changes should one cause trouble: `upscaler_idle_wait`, `present_order`, - `wb_lazy_unlocked`. See `nx2navi.ini.example`. +- New: `reflex_limit`, `ocarina_timing`, `audio_strict`, `hang_watch_s`, `mesh_fallback_hold`, and switches that + bring back the earlier behaviour of this release's larger changes should one cause trouble: `upscaler_idle_wait`, + `present_order`, `wb_lazy_unlocked`. See `nx2navi.ini.example`. diff --git a/nx2navi.ini.example b/nx2navi.ini.example index d37c1497..63083298 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -1241,6 +1241,10 @@ # Threads that prepare those compute passes for the mesh programs a place uses, the first time each is drawn (until # then its draws are left out). auto (default): one for each 4 logical processors, 1 to 4. # mesh_fallback_workers = auto +# 1 (default): while the colour draw of a patch of grass or leaves is left out because it is still being prepared, its +# depth draw is left out too, so the ground shows as it does without the grass. 0: the depth draw is made alone, as +# before, and the sky's colour can show through the ground in the shape of the grass until the patch is ready. +# mesh_fallback_hold = 1 # Windows, 1 (experimental): the game's heap is committed a 64 KiB run at a time where it is first used, instead of # all of it when the game sizes it. Windows counts committed memory against RAM plus the page file whether it is used # or not, and in Castle Town after Hyrule Field about 3.4 of the heap's 6.8 GiB had never been used. 0: committed up diff --git a/runtime/cfg.c b/runtime/cfg.c index d2b060d3..515e9318 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -41,7 +41,7 @@ static const char *const known[] = { "OCARINA_TIMING", "AA", "AA_LOG", "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CATCHUP", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_RAMP", "AUDIO_STATS", "AUDIO_STRICT", - "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "MESH_FALLBACK_WORKERS", "PROBE_MESHFB_DIR", + "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "MESH_FALLBACK_WORKERS", "MESH_FALLBACK_HOLD", "PROBE_MESHFB_DIR", "CAPTURE_AT", "CAPTURE_DIR", "CAPTURE_REPLAY", "CAPTURE_REPLAY_CAMERA", // bug captures and their replays (bugcap.c) "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", @@ -181,7 +181,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_NAN_PRESERVE", "performance"}, {"VK_NAN_BITS", "performance"}, {"DRAW_CRUMBS", "diagnostics"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, {"STUTTER_MAP", "diagnostics"}, {"PAR_JOBS", "performance"}, {"PIPE_RELEASE", "performance"}, {"FLIGHT_FLASH", "diagnostics"}, {"FLIGHT_FLASH_DIFF", "diagnostics"}, {"FLIGHT_FLASH_CELLS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"HEAP_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"MESH_FALLBACK", "performance"}, {"MESH_FALLBACK_MB", "performance"}, {"MESH_FALLBACK_WORKERS", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_UNLOCKED", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"MIRROR_CAPTURE_KB", "diagnostics"}, {"WB_SKIP_MB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"HEAP_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"MESH_FALLBACK", "performance"}, {"MESH_FALLBACK_MB", "performance"}, {"MESH_FALLBACK_WORKERS", "performance"}, {"MESH_FALLBACK_HOLD", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_UNLOCKED", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"MIRROR_CAPTURE_KB", "diagnostics"}, {"WB_SKIP_MB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"WAIT_WATCH_S", "diagnostics"}, {"PRESENT_ORDER", "diagnostics"}, @@ -363,6 +363,8 @@ static const CfgMeta metas[] = { // buffers the passes write (vk_mesh_fb.c). {"MESH_FALLBACK", "auto", "auto|0|1", RESTART}, {"MESH_FALLBACK_MB", "128", "16..4096", RESTART}, {"MESH_FALLBACK_WORKERS", "auto", "auto|1|2|3|4", RESTART}, // vk_exec_draw.c mfb_worker_count + // vk_exec_draw.c mfb_hold_role: a list's depth-only draw waits for its colour draw's pipeline + {"MESH_FALLBACK_HOLD", "1", "0|1", RESTART}, {"PIPE_AHEAD", "1", "0|1", RESTART}, // vk_exec.c vk_exec_ahead, vk_pipeline.c vk_pipeline_hint {"LINE_WIDTH", "1", "0|1", RESTART}, // vk_pipeline.c key_lines {"POOL_SHOWN_FIRST", "1", "0|1", RESTART}, // vk_texture.c shown_take diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 9e5e18e2..ac156232 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -295,6 +295,7 @@ typedef struct DrawCtx { int sf_mvprev; // shader_for asks for the vertex shader's previous-frame variant (motion) int mfb_stage; // the mesh fallback binds this NVN stage's resources (nvn_stage_of) const GfxShader *mfb_cur_vs; // the mesh fallback's vertex stage (prepare_draw_ex) + uint64_t mfb_list; // the address of its draw's indirect record, 0: none or not held (mfb_hold_role) PbCount *pb_cap; // per-binding reuse: where the resolving descriptor's census count goes int pb_spoiled; // ... and it may not be kept PbEntry PB[PB_N]; @@ -4469,6 +4470,74 @@ static const VkPipeInfo *PD_info; // the pipeline of the last draw prepare_dra // vertex-related (vertex buffers, motion vector bookkeeping of vertex shaders) is bound or noted. 2: a mesh draw of // the mesh fallback (mfb_draw): the vertex stage is DrawCtx.mfb_cur_vs, in an ordinary pipeline without vertex input. +// ---- mesh fallback: a list's depth-only draw is held while its colour draw waits for a pipeline +// The game draws a culled list of grass or leaves twice from one indirect record: into the depth target alone, then +// into the colour targets. Each draw has modules and a pipeline of its own, and the depth-only one, the smaller, is +// built first. Drawn without its colour draw it leaves the list's depth in the scene with nothing drawn over it: what +// lies behind fails the depth test there, and the sky's colour shows through the ground in the shape of the grass for +// as long as the colour draw's pipeline builds, seconds each on the drivers the fallback is the default for. So while +// a list's colour draw is skipped for its modules or pipeline, its depth-only draw is skipped too, and the ground +// shows as it does without the grass. +// A list is its record's address in the game's memory. The colour draw stamps the list with the present it was +// skipped in and clears the stamp when it is drawn; the depth-only draw, which comes first in the frame, is held when +// the stamp is at most MFB_HOLD_PRESENTS presents old, so a list whose colour draw no longer comes is held no longer +// than that. A draw whose stages write memory is never held (mfb_draw): the writes are wanted whatever is drawn. +// One word per slot (the address above 24 bits of the present count), read and written by the replay's threads without +// a lock: a lost update costs a list one frame of the hold. NX2NAVI_MESH_FALLBACK_HOLD=0 turns it off. +#define MFB_HOLD_SLOTS 1024 +#define MFB_HOLD_PRESENTS 2 +static volatile uint64_t mfb_hold[MFB_HOLD_SLOTS]; +static volatile uint64_t mfb_held_draws; + +static int mfb_hold_on(void) { + static int on = -1; + if (on < 0) on = rt_knob("MESH_FALLBACK_HOLD", "1"); + return on; +} + +static volatile uint64_t *mfb_hold_slot(uint64_t list) { + return &mfb_hold[(uint32_t)((list >> 2) * 0x9e3779b1u) >> 22]; +} + +// The list's colour draw was skipped for its modules or pipeline (waits 1), or was drawn (0). +static void mfb_hold_set(uint64_t list, int waits) { + volatile uint64_t *slot = mfb_hold_slot(list); + if (waits) + __atomic_store_n(slot, list << 24 | (__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) & 0xffffff), + __ATOMIC_RELAXED); + else if (__atomic_load_n(slot, __ATOMIC_RELAXED) >> 24 == list) __atomic_store_n(slot, 0, __ATOMIC_RELAXED); +} + +static int mfb_held(uint64_t list) { + uint64_t w = __atomic_load_n(mfb_hold_slot(list), __ATOMIC_RELAXED); + if (!w || w >> 24 != list) return 0; + return ((__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) - w) & 0xffffff) <= MFB_HOLD_PRESENTS; +} + +// A held depth-only draw is left out, counted as a draw whose own pipeline is building: the frame lacks it for the +// same reason (draw_skipped, vk_backend.c frame_hold). +static int mfb_hold_skip(VkExec *ex) { + uint64_t n = __atomic_add_fetch(&mfb_held_draws, 1, __ATOMIC_RELAXED); + if (!(n & (n - 1))) // the first, then at powers of two + rt_log("vk: mesh fallback: a list's depth-only draw is left out while its colour draw waits for a pipeline (%llu " + "so far; NX2NAVI_MESH_FALLBACK_HOLD=0 draws it)", (unsigned long long)n); + return draw_skipped(ex, 1, SK_PIPE_PENDING); +} + +// A colour target is bound (OP_SET_RT), whatever the draw's write masks are. +static int mfb_hold_colour(const VkExec *ex) { + for (int i = 0; i < ex->rt.n && i < 8; i++) + if (ex->rt.color[i]) return 1; + return 0; +} + +// What a fallback draw with state s writes: 1 a colour target, 2 depth and no colour, 0 neither. +static int mfb_hold_role(const VkExec *ex, const VkPipeState *s) { + for (uint32_t i = 0; i < ex->pass_ncolor; i++) + if (ex->color[i].image && vk_pipeline_target_wmask(s, (int)i)) return 1; + return ex->depth.image && s->ds.dt && s->ds.dw ? 2 : 0; +} + static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int mesh) { DrawCtx *const cx = ex->dc; int fb = mesh == 2; // from here on mesh says what the draw is, and !fb that its pipeline is a mesh pipeline @@ -4672,6 +4741,11 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int s.feedback ? ", feedback loop" : "", pending ? ", skipped after all" : "", count, ex->pass_w, ex->pass_h, tg); } + if (fb && cx->mfb_list) { // the list's depth-only draw goes with its colour draw (mfb_hold_role) + int role = mfb_hold_role(ex, &s); + if (role == 1) mfb_hold_set(cx->mfb_list, pending); + else if (role == 2 && !pending && mfb_held(cx->mfb_list)) return mfb_hold_skip(ex); + } if (pending) return draw_skipped(ex, mesh, SK_PIPE_PENDING); // a background worker is building it: skipped until it is ready if (!p || !info) return draw_skipped(ex, mesh, SK_PIPE_FAILED); if (info->pool && !pool_on()) return draw_skipped(ex, mesh, SK_POOL_OFF); @@ -6004,9 +6078,11 @@ static int mfb_dispatch(VkExec *ex, const GfxShader *cs, int stage, VkBuffer rec return 1; } -// A mesh draw of `count` workgroups, or of the count in the game's record at ind (4 bytes, GPU-written). -static void mfb_draw(VkExec *ex, uint32_t count, const VkMemSpan *ind) { +// A mesh draw of `count` workgroups, or of the count in the game's record at ind (4 bytes, GPU-written), which lies +// at `list` in the game's memory (0: no record). +static void mfb_draw(VkExec *ex, uint32_t count, const VkMemSpan *ind, uint64_t list) { DrawCtx *const cx = ex->dc; + cx->mfb_list = 0; if (ex->render_disabled) { vk_cond_draws_skip += ex->has_render_cond; draw_skipped(ex, 1, SK_DISABLED); @@ -6027,6 +6103,11 @@ static void mfb_draw(VkExec *ex, uint32_t count, const VkMemSpan *ind) { return; } if (!ms || (has_ts && !ts) || (has_fs && !fs)) { draw_skipped(ex, 1, SK_SHADER_FAILED); return; } + // the list's hold (mfb_hold_role): not for a draw whose stages write memory + if (list && mfb_hold_on() && !stage_writes(ms) && !stage_writes(ts) && !stage_writes(fs)) cx->mfb_list = list; + // without a colour target the draw is its list's depth-only one, known before its dispatches are recorded; with + // colour targets bound and their write masks off it is known where its state is (prepare_draw_ex) + if (cx->mfb_list && !mfb_hold_colour(ex) && mfb_held(cx->mfb_list)) { mfb_hold_skip(ex); return; } MfbCs *c = mfb_cs_get(ex, ms, ts); int state = __atomic_load_n(&c->state, __ATOMIC_ACQUIRE); if (!state && !vk_exec_targets_recent(ex, pipe_skip_mode() >= 1)) { @@ -6036,7 +6117,14 @@ static void mfb_draw(VkExec *ex, uint32_t count, const VkMemSpan *ind) { while (!state && pool_ms(pool, t0)) plat_sleep_ms(1), state = __atomic_load_n(&c->state, __ATOMIC_ACQUIRE); pool_charge(pool, t0); } - if (!state) { MFB.pending_skips++; draw_skipped(ex, 1, SK_PIPE_PENDING); return; } + if (!state) { + // its modules are being made: with a colour target bound it counts as the list's colour draw. Its write masks + // are not known here; a depth-only draw taken for one holds itself for MFB_HOLD_PRESENTS presents at most. + if (cx->mfb_list && mfb_hold_colour(ex)) mfb_hold_set(cx->mfb_list, 1); + MFB.pending_skips++; + draw_skipped(ex, 1, SK_PIPE_PENDING); + return; + } const GfxShader *vs = state > 0 ? mfb_vs_get(c, fs) : NULL; if (!vs || !vk_meshfb_init()) { draw_skipped(ex, 1, SK_PIPE_FAILED); return; } uint32_t verts = c->mi.maxp * c->mi.ipp, slot_words = meshfb_slot_words(&c->mi); @@ -6116,7 +6204,7 @@ static void op_draw_mesh(VkExec *ex, const p_draw_mesh *d) { if (!d->count || !on) return; if (vk_ctx()->mesh_fallback) { if (d->first) warn_once(W_MESH, "a mesh draw from workgroup %u: drawn from workgroup 0", d->first); - mfb_draw(ex, d->count, NULL); + mfb_draw(ex, d->count, NULL, 0); return; } if (!fn) return warn_once(W_MESH, "mesh draws need VK_EXT_mesh_shader (mesh_emulation = 1 emulates them): skipped"); @@ -6324,7 +6412,7 @@ static void op_draw_mesh_ind(VkExec *ex, const p_draw_mesh_ind *d) { return; } if (fallback) { - mfb_draw(ex, 0, &ip); + mfb_draw(ex, 0, &ip, d->ind); vk_mem_count(ip.cls, VK_MUSE_IND, 4); VK_MEM_BOUND(ip); return;