The build takes a narrower form of the design, chosen so that no structure needs a lock or an atomic:
Lock step, not a free pipeline. The renderer thread gives the next draw to the preparation thread (prep.c,
vk_prep_kick) after the current draw's commits, and it waits for the result (vk_prep_join) before it lets go of
vk_lock. Between the two, the renderer thread runs only the binds, the dynamic state, the push constants and the
draw command (render.c, from rp_record to the record). That code touches nothing the resolve reads or writes. So
the two threads never touch one structure at the same time, and the memos keep one owner at each moment.
The commits of a plain draw (draw_commit: its targets written, its feedback slots, its buffer writes) move before
its binds, so the next draw's resolve sees them. A draw with commits that must follow its command (transform
feedback, late depth, writes that land, the pre-draw capture, storage images) gives no draw ahead.
vk_lock is lent: the preparation thread runs under the renderer thread's hold (vk_lock_lend, depth 2). A take
of the lock there fails the resolve.
Stage 1 is render_draw_main itself, from its start to the pipeline key, on the kept path only. Each step that a
memo hit does not cover fails it (AHEAD_FAIL, and gpu_spec_fail in the callees): a miss of the draw table, the
targets' memo or a texture memo, a target to reload, a texture to check in full or to take from a target, a guest
read that would land results (gpu.c), and the rare paths (mesh, raster samples, coverage to color, advanced blend,
per-vertex passes, save attachments). The renderer thread then resolves the draw itself, as before.
The preparation thread has its own copy of each draw state (g_ds_ahead, renderer.c). It takes from the renderer's
copy each group whose generation is not its own, then applies the next queue record (gq_peek_call). The memos
that keyed on the state's pointer key on the renderer's copy (gpu_ds_canonical).
The result goes back by value in one record (DrawPrep). The renderer thread takes it where the record is the one
it runs and none of the stamps moved since the resolve began (prep_holds): the GPU writes, the results added, the
storage images, the targets taken, the surface table, the write epoch, the submission, the present, the frame, the
shader and fold generations and the heaps mode. A draw that takes it skips to the vertex buffers (resolved:).
With draw_state_check=1, the renderer thread resolves each draw in full and compares the result made ahead
(prep_differs). The log line "draw_prep over the last 600 presents" counts the draws given ahead, the results
taken, those stale, and the failures by reason.
The setting is off (0), on (1) or auto (2). Auto chooses by measurement, as gpu_split=2 does for the front end
and renderer split (gq_auto): every 600 presents or more, it measures the renderer thread's time in records a
present (gq_run_ns) over 120 presents in the chosen mode, 120 in the other and 120 in the chosen mode again, and
it changes the mode after two probes in a row cost under 97% of both. On a CPU with few cores the thread competes
with the game's threads, and the pipeline can then lose time: auto keeps one thread there. The test for that limits
the process to 4 cores (an affinity mask) and runs the game with the thread on and off.
The gain of this form is at most the binds' and the record's share of a draw (about 18%), not the resolve's (about 25%): the resolve runs only while the binds run.
Made from docs/dev/parallel-draw-prep.md at 69237645142c. GPL-3.0-or-later.