From dae0c5b7c5b8ceb3f600ac77986358b5d1fac6d0 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 08:25:22 -0500 Subject: [PATCH] RR runtime: free released input copies while no instance exists A game that tags its Ray Reconstruction inputs every frame while RR is available but switched off makes the plugin copy each lifetime-0 input (retain_transient_state) and release the previous copy. The release is deferred until the frame that recorded the copy has run, but the deferred list was emptied only by pump(), which runs in instance create, evaluate and destroy. With no instance, nothing ran it and every released copy stayed allocated. release_transient_locked now calls pump() after deferring the release. pump() frees only resources whose frame marker the GPU has written, takes no lock of its own and calls nothing back, and the adapter entry already holds the runtime lock, as at create, evaluate and destroy. rr_test_host gets a job "tagloop []": one RGBA16F lifetime-0 input copied and released every frame, no instance, no evaluate, no test drain or counters, video memory measured with QueryVideoMemoryInfo. e2e.py runs it (200 frames of 1706x960) before its scenarios, with a limit of 256 MiB of growth. Reported by @j0nsh1n in #8. Fixes #8. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/rr_f/runtime/src/rr_runtime.cpp | 6 +++ clean/rr_f/runtime/tests/e2e.py | 21 ++++++++- clean/rr_f/runtime/tests/rr_test_host.cpp | 53 +++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/clean/rr_f/runtime/src/rr_runtime.cpp b/clean/rr_f/runtime/src/rr_runtime.cpp index e1204a68..c642a468 100644 --- a/clean/rr_f/runtime/src/rr_runtime.cpp +++ b/clean/rr_f/runtime/src/rr_runtime.cpp @@ -3542,6 +3542,12 @@ void release_transient_locked(void* copy) { ID3D12Resource* r = *it; rt.transient.erase(it); defer_release(r, rt.serial); + // issue #8: create, evaluate and destroy run pump() too, but a game that tags its inputs every frame while no + // instance exists (RR available, switched off) calls none of them, and the released copies were never freed. + // pump() frees only resources whose frame marker the GPU has written (the same test as at evaluate), takes no lock + // of its own and calls nothing back. The caller, a_release_transient, holds rt.mu, as every other user of the deferred + // list does. + pump(); } // ---------------------------------------------------------------------------------------------------- the adapter diff --git a/clean/rr_f/runtime/tests/e2e.py b/clean/rr_f/runtime/tests/e2e.py index f4ec40fe..6a3db0c5 100644 --- a/clean/rr_f/runtime/tests/e2e.py +++ b/clean/rr_f/runtime/tests/e2e.py @@ -230,7 +230,11 @@ def write_frame(path, c): # ------------------------------------------------------------------------------------------------- GPU run -def run_gpu(out, stage, runs, perf_frames, name="e2e", leak=6, head=None, body="Z", inp="exact", front=True, mips=1): +TAGLOOP_LIMIT_MIB = 256 # issue #8: 200 frames grew video memory by about 2.6 GiB before the fix, about 60 MiB after + + +def run_gpu(out, stage, runs, perf_frames, name="e2e", leak=6, head=None, body="Z", inp="exact", front=True, mips=1, + tagloop=200): """runs: [(scenario name, calls)]; every scenario twice (WG_RR_ALIAS=0 and aliased).""" work = os.path.join(out, name) if os.path.exists(work): @@ -250,6 +254,9 @@ def run_gpu(out, stage, runs, perf_frames, name="e2e", leak=6, head=None, body=" job.append("env WG_RR_TAILPREP 0") if mips > 1: # 0.6.0 follow-up: the game's inputs with mips (mip 0 is read): the checks job.append(f"env WG_RR_TEST_MIPS {mips}") # below then prove the same bits as with one mip + if tagloop: # issue #8: inputs copied and released every frame before any instance + job += [f"outdir {os.path.join(work, 'tagloop')}", # exists; video memory may grow by at most TAGLOOP_LIMIT_MIB + f"tagloop {tagloop} 1706 960 {TAGLOOP_LIMIT_MIB}"] for sname, calls in runs: sc = SCEN[sname] for alias in (0, 1): @@ -815,6 +822,8 @@ def main(argv=None): ap.add_argument("--jobs", type=int, default=8) ap.add_argument("--perf", type=int, default=600) ap.add_argument("--leak", type=int, default=6) + ap.add_argument("--tagloop", type=int, default=200, help="issue #8: frames of inputs copied and released with no " + "instance, before the scenarios (0: skip)") ap.add_argument("--body", default="Z", help="WG_RR_BODY for the GPU run (default Z: exact body + exact front-end MLP, " "checked bit-exact; 'default': the runtime's default step E, which the exact body check flags)") ap.add_argument("--head", default="exact", help="WG_RR_HEAD for the GPU run (exact / fast / strict; default exact: since " @@ -842,7 +851,8 @@ def main(argv=None): work = os.path.join(a.out, name) if not a.skip_gpu: work = run_gpu(a.out, stage, runs, a.perf, name, a.leak, None if a.head == "default" else a.head, - None if a.body == "default" else a.body, None if a.input == "default" else a.input, mips=a.mips) + None if a.body == "default" else a.body, None if a.input == "default" else a.input, mips=a.mips, + tagloop=a.tagloop) rows, life, inv, al = [], [], [], [] jobs = [] metas = {} @@ -875,6 +885,9 @@ def main(argv=None): p = os.path.join(work, f"run{runs[-1][0]}1", "leak.json") if os.path.exists(p): report["leak"] = json.load(open(p)) + p = os.path.join(work, "tagloop", "tagloop.json") + if os.path.exists(p): + report["tagloop"] = json.load(open(p)) p = os.path.join(work, f"run{runs[-1][0]}1", "perf.json") if os.path.exists(p): report["perf"] = [json.loads(l) for l in open(p) if l.strip()] @@ -896,6 +909,10 @@ def main(argv=None): lines.append(f"| lifecycle (par / fc / reset / rebuild / failed call) | {sum(x['ok'] for x in life)} / {len(life)} |") lines.append(f"| failed call leaves state unchanged | {sum(x['state_unchanged'] for x in inv)} / {len(inv)} |") lines.append(f"| aliased run = unaliased run (outputs, state) | {sum(x['ok'] for x in al)} / {len(al)} |") + if "tagloop" in report: + t = report["tagloop"] + lines.append(f"| inputs released with no instance: video memory growth (limit {t['limit_mib']:.0f} MiB) | " + f"{t['growth_mib']:.1f} MiB over {t['frames']} frames |") with open(os.path.join(work, "report.md"), "w") as fh: fh.write("\n".join(lines) + "\n") print("\n".join(lines)) diff --git a/clean/rr_f/runtime/tests/rr_test_host.cpp b/clean/rr_f/runtime/tests/rr_test_host.cpp index 1ce175f2..574b7813 100644 --- a/clean/rr_f/runtime/tests/rr_test_host.cpp +++ b/clean/rr_f/runtime/tests/rr_test_host.cpp @@ -30,6 +30,9 @@ // retag bug hunt B: copied as a lifetime-0 input and released again before any // evaluate (the game re-tags it): the copy must stay until the list that recorded it // has run (a deferred release), and be gone after it (exit 9 otherwise) +// tagloop [] issue #8: a lifetime-0 RGBA16F input copied and released every frame with +// no instance and no evaluate (RR available, switched off); video memory may grow +// by at most (exit 12 otherwise) // destroy // leak create / 2 frames / destroy per cycle; live counters and video memory // perfcap ... game-setup timing: the frames resident, evaluated times without waits @@ -1008,6 +1011,56 @@ int main(int argc, char** argv) { for (ID3D12Resource* u : h.uploads) u->Release(); h.uploads.clear(); if (!ok) return 9; + } else if (cmd == "tagloop") { + // issue #8: a game that tags its RR inputs every frame while no RR instance exists (RR available, switched off). + // Each frame one lifetime-0 input is copied (retain_transient_state, as the plugin's tag handler does), the + // previous copy is released (the input tagged again), and the frame's list runs. No instance, no evaluate, no + // test drain or counters (both run the runtime's pump): only the adapter calls a game makes. Video memory is + // measured with QueryVideoMemoryInfo (CurrentUsage); the run stops early at +3 GiB. + int frames = 0; + uint32_t tw = 0, th = 0; + double limit_mib = 0; + s >> frames >> tw >> th >> limit_mib; + if (frames <= 0 || !tw || !th) { + fprintf(stderr, "usage: tagloop []\n"); + return 7; + } + Tex t = make_tex(tw, th, DXGI_FORMAT_R16G16B16A16_FLOAT, false, D3D12_RESOURCE_STATE_COMMON); + const uint64_t v0 = vram(), cap = 3ull << 30; + uint64_t peak = v0; + void* prev = nullptr; + int ran = 0; + for (int i = 0; i < frames; ++i) { + void* copy = nullptr; + if (g_rt->retain_transient_state(g_rt->ctx, g_l, t.r, (uint32_t)t.f, (uint32_t)t.state, ©) != WG_OK || !copy) { + fprintf(stderr, "retain_transient_state failed at frame %d\n", i); + return 8; + } + if (prev) g_rt->release_transient(g_rt->ctx, prev); // the input tagged again: the previous copy goes + prev = copy; + submit_wait(); // the game's frame runs + ++ran; + const uint64_t v = vram(); + peak = std::max(peak, v); + if (v > v0 + cap) break; + } + const uint64_t v1 = vram(); + if (prev) g_rt->release_transient(g_rt->ctx, prev); + t.r->Release(); + const double grow = ((double)v1 - (double)v0) / 1048576.0, peak_grow = ((double)peak - (double)v0) / 1048576.0; + const bool ok = limit_mib <= 0 || grow <= limit_mib; + FILE* fo = fopen((h.outdir + "\\tagloop.json").c_str(), "wb"); + if (fo) { + fprintf(fo, "{\"frames\":%d,\"width\":%u,\"height\":%u,\"vram_before\":%llu,\"vram_after\":%llu,\"vram_peak\":%llu," + "\"growth_mib\":%.1f,\"limit_mib\":%.1f}\n", ran, tw, th, (unsigned long long)v0, + (unsigned long long)v1, (unsigned long long)peak, grow, limit_mib); + fclose(fo); + } + printf("tagloop: %d frames of a %ux%u RGBA16F lifetime-0 input, no instance: video memory %llu -> %llu bytes " + "(%+.1f MiB, peak %+.1f MiB; limit %.0f MiB): %s\n", ran, tw, th, (unsigned long long)v0, + (unsigned long long)v1, grow, peak_grow, limit_mib, + ok ? "ok" : "FAIL (released copies are not freed without an evaluate)"); + if (!ok) return 12; } else if (cmd == "destroy") { destroy(h); } else if (cmd == "leak") {