[enhancement][memory] The title's own CPU readback buffers: a hook point for copy-back by intent (follow-up to #19) #287
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Follow-up to my proposal in #19 (#19 (comment)): first results on where the title asks for GPU results on the CPU. Addresses are RVAs in my dump's
game_scarlett_master.exe(120,181,760 bytes). No game files are changed, and no decompiled code is posted here.1. Textures: the title reads back one
DARNER_READ_WATCH=1, 15 minutes of mixed play: 826 textures the card writes were watched (493 by unordered access, 200 depth, 133 render targets). Title code touched exactly one: the 256x256 R8_UNORM render target that is already inDARNER_TEXTURE_BACK, from one place,+0x50313d2. No other watched texture was read by the title.2. That read is a latency-tolerant readback
The function at
0x5031380samples a 256x256 grid of bytes (signed around 0x80, scaled, x and y clamped), four times per query from0x5031160. It reads through a small table of pointers, indexed by a value that0x12bce40reads and writes, which looks like a per-frame rotation. If so, the title reads a copy from an earlier frame, not the one the card is writing, and a copy-back of this target can land a frame or more late without the title seeing a difference.3. The title has a readback buffer class:
CpuReadBackShaderResourceBufferThe function at
0x1adc2c0(in a vtable, and called from0x1d40f90) makes up to three buffers namedCpuReadBackShaderResourceBuffer [0]to[2]through an allocation callback it is given, and keeps their addresses in the object. The count comes from a call on its second argument. That is the usual rotating readback: the card writes one copy while the CPU reads another.Hooking it, found through its reference to that name so a title update doesn't lose it, would give Darner every readback buffer the title makes, as it makes it: copy those ranges back, leave the rest on the card.
DARNER_LAND_LEAVEalready does the second half from a file; this would make it a keep list filled by the title at run time.4. Open
DARNER_LAND_READS=1run is next, with a small patch (local for now, on mainf3840d7) that logs the code place of each first touch by title code, through readwatch'smodule_of. I'll post the result here, and can open the patch as a PR.Questions
DARNER_LAND_KEEP_TITLE=1), off by default, besideDARNER_LAND_LEAVE?Results of the
DARNER_LAND_READS=1runs promised above. Same PC and settings as in the issue (RTX 4090, i9-14900K, Series X, 1440p, DLSS Quality, frame generation 2x, hardware RT), mainf3840d7with #185, plus a small patch toland/reads.rs, described at the end.Who touches a landed page first
Two runs, 15 min and 8 min (the second ended in a crash, see below). Pages the census watched, whole of the second run (the first agrees within a point):
safecopy::mergereading title memory before it stores)So about 99 % of the watched pages are written by the card again before the title reads them. The "ours" touches are all
darner_safe_copy_at(named from the PDB), that is, the next landing's own merge: not a read by anything else, and not a round trip.The census of never-touched ranges is very stable between runs: the 16 biggest ranges hold 98 % of the never-touched pages, and 21 of their 22 MiB are the same in both runs.
Where the title reads
366 places in the first run. A touch inside
vcruntime140'smemcpy/memsetis now put on its caller (the return address at the stack pointer, these being leaves), so the title's copies and ours are told apart. The busiest place,+0x55799cb, looks through entries tagged with the frame number minus two: a readback consumed two frames late. The readback class from section 3 is not among the places itself; its buffers seem to be read through shared helpers.What a landing costs, and what's next
In normal play (no census), merging is about 1.2 ms a frame on the relay thread (3 ms on all landing threads); most of the copy-back time is the fetch of the pieces from the card. So leaving merges for later would save little: the gain is in not fetching. Next I'm A/B-testing
DARNER_LAND_LEAVEwith a list made from the ranges both censuses agree on (15 ranges, 21 MiB), against my earlier A runs, to measure what not fetching saves before building the keep list from the title's side.A crash in the census itself
The second run ended with the title reading a page the census had made no-access, at
+0x557b108, while the census no longer held that page, so the handler passed the fault on. Just before,shadowlogged "a buffer's host copy is in the card's memory" for several 8-byte buffers on that same 4 KiB page. My guess is that moving a host copy into the card's memory changes or takes the page without telling the census. It's only reachable withDARNER_LAND_READS=1.The patch
land/reads.rs: notes the code place of every first touch (module and offset, throughreadwatch::module_of), ours and other code's apart, puts touches inside the C runtime on their caller, and logs the most frequent places. I can open it as a PR if it's useful, with the crash above noted as known.Early preview, one run each, not a result yet. Leaving the never-read ranges on the card when shader writes are declared, instead of at the landing, halved what is copied back in my run. Heavy city scenes are not measured yet; that run is next.
Same PC and settings as above (RTX 4090, i9-14900K, Series X, 1440p, DLSS Quality, frame generation 2x, hardware RT, PGO build of main
f3840d7with #185). The list: 15 ranges, 21 MiB, the ranges bothDARNER_LAND_READScensuses above found never touched by the title's code.Two ways of leaving the same list
DARNER_LAND_LEAVEas it is on main: listed pieces are not fetched or merged. 185 pieces (5.6 MB) a frame were left, landed pieces fell by 42 %, but the relay's time did not fall. The card still hashes and compares those ranges before the landing decides.Shadow::shader_writes. The part of a declared shader write that lies in a listed range is left on the card the wayDARNER_WC_NO_BACKleaves write-combined memory (self.leaves, no deferred write), so the card neither hashes nor compares it. The rest of the write goes on as before. About 70 MB a frame of declared writes were left this way.No visual faults seen in either run with the list.
Caveats
Next
A run into the city early, with v1 plus #274 (its retirement queue walk blocks the relay too). If the heavy scenes hold up, I'll open v1 as its own PR: the switch, with how to make a list with
DARNER_LAND_READS, not a ready-made list. The keep list filled from the title's readback buffers (section 3 of the issue) is still the aim; this shows what leaving memory on the card at the declaration is worth.City run, following the preview above. #310 is now open as a draft with this in it.
Same list and settings, 10 min, into the city from about the second minute, with #310 and #274 in the build (and #283, inactive with Reflex off).
On the title's side (section 3 above): the readback ring helper is hooked in a local build (off by default,
DARNER_READBACK_HOOK=1, the entry checked by its first bytes as #185 and the mouse hook do), and every buffer it makes is kept: the leave list never leaves a part of one. The next run logs how many there are, how big, from which callers, and whether any of them fall in the census-made list.Where this has gone since the city run above: a census of what the title asked for each piece of its memory, two tries at leaving more of it on the card that failed, and why they failed. Nothing here is a PR yet; it is all on a local branch while the next test runs.
Same PC and settings as above (RTX 4090, i9-14900K, Series X, 1440p, DLSS Quality, frame generation 2x, hardware RT),
mainf3840d7with #310, #274 and #341.The census: what each pool is used for
A local switch notes every
XMemVirtualAllocwith the protection, kind and XMem flags the title asked for, and puts the bytes of each of the runtime's paths on the allocation they fall in: shader writes declared and left on the card, pieces landed, the title's first touches of landed pages (DARNER_LAND_READS=1), bytes copied up, and the readback buffers the hooks find. The pools are a handful of big allocations made at start (their addresses move per run). Results of a 900 s run, about 6 minutes of it in the world (the rest on the pause menu):0x22004, flags0x183)0x2404, flags0x283)0x2404, flags0x23)0x801)DARNER_WC_NO_BACKat its 1 MB floor already leaves 98.6 % and 99.9 % of the declared writes in the two write-combined pools on the card; what still lands there is the small copies under the floor.Leaving more on the card: two tries, both flickered
DARNER_WC_NO_BACKwith the floor at 0 and the readback buffers (exposure included) excepted. The same: the world flickered, the UI did not.The census only counted reads by the title's code. Darner reads that memory too: a texture placed in write-combined memory that shaders write is refreshed from title memory, so leaving the shaders' bytes on the card refreshes it from old ones. The 1 MB floor was protecting those textures as much as exposure.
How many textures
A counter in the same census: each texture refresh checks whether the card wrote the texture's memory in a write-combined copy since its last refresh, and whether those writes were left on the card or came back. 10 minutes in the world, normal settings (floor 1 MB, nothing else left):
0x2404/0x283pool, near the exposure read.For #310: a leave list made from
DARNER_LAND_READScannot see these either, since Darner's own reads are not the title's. A range with such a texture in it would show old texels.Next
A local switch that registers each texture Darner refreshes from write-combined memory with the readback buffers, so its bytes always come back however the memory round it is treated; then the floor at 0 again. That run is next. If the world stays clean, an A/B against the range-lock run follows and this becomes a PR; if not, the same counter finds the next reader.
Follow-up to the post above: the texture run, and how low
DARNER_WC_NO_BACK's floor goes.Keeping the textures is not enough. With the 81 textures the counter found registered and always copied back (the run's log confirms all 81), plus the readback buffers, the floor at 0 still made the world flicker as before. So those textures were not the only reader.
The floor, with the textures and readback buffers kept (
DARNER_READBACK_HOOK=1and the local texture switch; same PC and settings as above):DARNER_WC_NO_BACK_MIN_KBWhat goes wrong at 0 lies in write-combined copies under 64 KB. My main suspect now is overlapping host copies: when one copy's shader writes stay on the card, other copies over the same title memory never get them (with the write-back they did, through title memory). A census of exactly that is building; it runs at the normal floor, so it needs no flickering run.
The 64 KB tries are short checks for flicker, not an A/B. A proper 10-minute A/B of 64 KB against the range-lock run is next, once the census has said what is left under it.