From 49b2314dd8fc4403dafb4a2e4f76005cad82fdb6 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 6 Nov 2026 22:35:01 +0000 Subject: [PATCH 1/2] Overlay uniform updates between passes too, so a run of them before a pass costs one barrier An update between passes was copied at once behind a full barrier of its own, and the copy it recorded made the next update or pass begin need another: a run of k updates before a pass cost k + 1 full barriers, about 250 updates a frame on Great Sky Island. They now become overlays like the updates inside a pass; every GPU reader of the real buffer after them comes behind barrier_to, which records the copies first. TOTK_UB_OVERLAY_OUT: 1, 0, ab; the perf line counts the overlays made between passes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BspNcezsiV8zT8wcnXrrME --- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk_exec_draw.c | 39 ++++++++++++++++++++++++++------------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index eb7cee24..bf5e45eb 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -136,6 +136,7 @@ Many rows below are developer tools that only act together with another setting | `tex_vram_pressure` (default `90`) | Percent of the device-local memory budget above which released textures are dropped, 0 to 100; `0` is off. First those the game has overwritten with a later texture and left alone for 30 s, then those that can be uploaded again. | A low value drops textures sooner and re-uploads them; `0` keeps them and lets the card spill into system memory. Also a slider in the menu. | Caution | | `txd_transpose` (default `1`) | `0` turns off the swap of gradient components for gradient texture samples. | Terrain textures turned across the screen show crawling patches again. | Caution | | `ub_overlay` (default `1`) | `0` copies each uniform update inside a pass into its buffer at once, ending the pass with a full barrier; `ab` alternates with the `ab` blocks. | With `0` scenes with many updated blocks (the terrain, the sky islands) take longer on the GPU; `TOTK_TRACE=perf` prints how many updates stayed in their pass. | Caution | +| `ub_overlay_out` (default `1`) | `0` copies each uniform update between passes into its buffer at once, behind a full barrier of its own; `ab` alternates with the `ab` blocks. Needs `ub_overlay`. | With `0` a run of updates before a pass costs a full GPU barrier each; `TOTK_TRACE=perf` prints how many updates between passes were overlaid. | Caution | | `ubo_snapshot` (default `1`) | `0` binds live memory for uniform blocks; `ab` alternates with the `ab` blocks. | Without snapshots late draws read another object's matrix, so objects can be drawn in the wrong place. | Risky | | `vk_no_float64` (default `0`) | `1` clears the 64-bit float feature, so the translator lowers FP64 to FP32. | Shaders change, so the shader cache entries for it are made again; precision of FP64 shaders drops. | Caution | | `vk_ssbo_align` (default unset) | Power of two that storage buffer offsets are treated as needing, to test the unaligned path. | The translator and the draw code round storage offsets down to it; shaders are translated again. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index e2a06afd..6e0170ce 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -59,7 +59,7 @@ static const char *const known[] = { "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", - "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", + "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 4507d8d9..195c516b 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -175,7 +175,8 @@ static struct { struct { uint64_t dst; VkBuffer src; VkDeviceSize soff; VkMemSpan sp; uint64_t size; } p[OV_N]; // to copy struct { uint64_t lo, hi; } f[OV_FOREIGN]; // GPU writes of other kinds // since the last perf line (vk_exec_ub_perf) - uint64_t c_update, c_overlaid, c_flush, c_bind, c_merged, c_mixed, c_inpass, c_outside, c_early, c_order; + uint64_t c_update, c_overlaid, c_overlaid_out, c_flush, c_bind, c_merged, c_mixed, c_inpass, c_outside, c_early, + c_order; } OV = {.mode = -1}; // A binding without a size reaches to the end of its backing: as far as the overlays go, for ov_at. @@ -3254,11 +3255,11 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) { // it read the new bytes, those before it the old ones. Replayed as a buffer copy at that point it needs a barrier // against the earlier draws, and a barrier ends the open pass; the terrain and the sky islands' meshes update before // most of their draws, about 200 updates inside passes in a field submission, each a pass end, a full barrier and a -// pass begin. Here an update inside a pass goes into the scratch buffer as an overlay: the later bindings of a range it -// covers bind the scratch bytes (ub_bind, from ubo_info and bind_range), and the copies into the real buffers wait for -// the next full barrier, where the pass has ended anyway (vk_exec_ub_flush from vk_exec.c barrier_to: a pass end, a -// dispatch, a transfer, the end of the submission), behind one barrier of their own. Each draw reads the bytes it would -// have read with the copy in between. +// pass begin. Here an update inside a pass, and with TOTK_UB_OVERLAY_OUT one between passes, goes into the scratch +// buffer as an overlay: the later bindings of a range it covers bind the scratch bytes (ub_bind, from ubo_info and +// bind_range), and the copies into the real buffers wait for the next full barrier, where the pass has ended anyway +// (vk_exec_ub_flush from vk_exec.c barrier_to: a pass end, a dispatch, a transfer, the end of the submission), behind +// one barrier of their own. Each draw reads the bytes it would have read with the copy in between. // - A GPU write of another kind (a copy, a fill, a shader's storage write) is ordered with the copies only by the // command buffer. vk_exec_ub_foreign notes it and retires the overlays it touches; an update over such bytes takes // the old path. A shader that writes a range with copies pending gets them first (ub_written: the pass restarts). @@ -3274,6 +3275,16 @@ static int ov_on(void) { return RT_KNOB_ON(OV.mode); } +// TOTK_UB_OVERLAY_OUT: updates between passes become overlays too. Copied at once, each needs a full barrier of its +// own, and the copy it records makes the next update or pass begin need another: a run of k updates before a pass cost +// k + 1 barriers. Every GPU reader of the real buffer after them comes behind barrier_to, which records the copies +// first (a pass begin, a dispatch, a transfer, the end of the submission), so they cost one barrier for the run. +static int ov_out_on(void) { + static int mode = -1; + if (mode < 0) mode = rt_knob("UB_OVERLAY_OUT", "1"); + return RT_KNOB_ON(mode); +} + static void ov_sub(const VkExec *ex) { // the submission being replayed if (OV.fence == ex->fence) return; if (OV.np) // barrier_to records them at the latest at the submission's closing barrier @@ -3378,12 +3389,12 @@ void vk_exec_ub_perf(void) { static unsigned subs; if (OV.mode <= 0 || ++subs % 600) return; const double k = 1 / 600.0; - rt_log("perf: uniform updates per submission %.1f: %.1f overlaid in a pass (copied in %.2f batches), %.1f copied " - "in a pass, %.1f outside one; bindings from an overlay %.1f (%.1f merged, %.1f over other GPU writes), %.2f " - "batches brought forward for a storage write, %.2f barriers between overlapping copies", - OV.c_update * k, OV.c_overlaid * k, OV.c_flush * k, OV.c_inpass * k, OV.c_outside * k, OV.c_bind * k, - OV.c_merged * k, OV.c_mixed * k, OV.c_early * k, OV.c_order * k); - OV.c_update = OV.c_overlaid = OV.c_flush = OV.c_bind = OV.c_merged = OV.c_mixed = 0; + rt_log("perf: uniform updates per submission %.1f: %.1f overlaid (%.1f of them between passes; copied in %.2f " + "batches), %.1f copied in a pass, %.1f outside one; bindings from an overlay %.1f (%.1f merged, %.1f over " + "other GPU writes), %.2f batches brought forward for a storage write, %.2f barriers between overlapping copies", + OV.c_update * k, OV.c_overlaid * k, OV.c_overlaid_out * k, OV.c_flush * k, OV.c_inpass * k, OV.c_outside * k, + OV.c_bind * k, OV.c_merged * k, OV.c_mixed * k, OV.c_early * k, OV.c_order * k); + OV.c_update = OV.c_overlaid = OV.c_overlaid_out = OV.c_flush = OV.c_bind = OV.c_merged = OV.c_mixed = 0; OV.c_inpass = OV.c_outside = OV.c_early = OV.c_order = 0; } @@ -3448,7 +3459,8 @@ static void op_update_ub(VkExec *ex, const p_update_ub *u) { if (ov_on()) { ov_sub(ex); OV.c_update++; - if (ex->in_pass && OV.n < OV_N && OV.np < OV_N && !ov_foreign_hits(u->dst, u->dst + u->size)) { + int here = ex->in_pass || ov_out_on(); + if (here && OV.n < OV_N && OV.np < OV_N && !ov_foreign_hits(u->dst, u->dst + u->size)) { uint8_t *d = scratch(ex, u->size, &b, &o); memcpy(d, u->data, u->size); VK_MEM_PICK(sp); @@ -3459,6 +3471,7 @@ static void op_update_ub(VkExec *ex, const p_update_ub *u) { note_written(ex, u->dst, u->size); upd_note(ex, u); OV.c_overlaid++; + if (!ex->in_pass) OV.c_overlaid_out++; return; } if (ex->in_pass) OV.c_inpass++; From 324f85602f516dce1142ecb3face1419da75f340 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 00:18:52 +0000 Subject: [PATCH 2/2] Clear the uniform overlays once their copies are recorded After vk_exec_ub_flush the full barrier barrier_to records makes the copied bytes visible, so later bindings can read the real buffer. The overlays stayed until the submission's end instead: every later binding inside their span scanned them, and a block an overlay covered in part got a merged scratch copy that the descriptor build skip then left alone. With the updates between passes overlaid too (Great Sky Island: 274 of 594 per submission) that was 290 merged copies and 1.1 ms more replay a frame, more than the barriers saved on the GPU (0.3 ms). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BspNcezsiV8zT8wcnXrrME --- runtime/vk_exec_draw.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 195c516b..730664c5 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -3381,6 +3381,11 @@ void vk_exec_ub_flush(VkExec *ex) { ex->recorded++; } OV.np = 0; + // The full barrier barrier_to records next makes the copied bytes visible: later bindings read the real buffer, + // as after an update copied at once, and need no overlay lookups or merged copies (ov_at answers 0 for them, so + // the descriptor build skip rebuilds the draws that bound an overlay) + OV.n = 0; + ov_span(); OV.c_flush++; }