Barriers at the splice: predict and check

The calls that choose barriers and passes by the order of all earlier work (vk_surf_use, render_begin, the unified memory's order, the page cache's order_use) do not run on a worker. Each becomes a deferred call in the worker's list (vk_rec_defer), which the renderer thread runs when it splices the list, at the call's place, with the state of all earlier batches applied. Its barriers then go into the ring there, in batch order.

A worker's later draws in the batch read the state that those calls change (a surface's layout, stage and access). So the worker keeps a view of its batch's changes (an overlay of the surfaces it used), and the readers ask the overlay first (vk_surf_layout and the like).

A deferred call can also end the render pass (a barrier needs the pass ended), and the commands after it in the list assume the pass the worker saw. Dropping the rest of a list is not safe: it can hold uploads that the CPU's caches already count as done. So a deferred call corrects in place. It records the barriers that the real state needs at the splice, and where that ended the pass, the splice opens the same pass again (with its loads) before the draws that follow. For that, the worker records with each draw a deferred pass call with the draw's targets. A batch still ends where the worker sees a new pass: it hands back the draws from there, and their uploads so far stay in the list. In batches in turn (serial), the splice never corrects. In batches in parallel, a correction is a batch that depends on an earlier one, and the checks of its sampled surfaces (above) decide whether it is set up again.

The steps: deferred calls in the lists, then vk_surf_use with the overlay and the check, then render_begin (a batch ends at a new pass, and the worker hands back the draws from there), then the unified memory's and the page cache's order.

The deferral, one funnel

Every change of a surface's layout, stage and access in use goes through vk_surf_use (about 78 calls in 13 files: the pass's begin, uploads, copies, the copies of targets, blits). The other writes set a surface to UNDEFINED where it is made, loaded again, swapped or moved. So the deferral does not need the paths one by one:

  1. On a worker, vk_surf_use changes the worker's overlay and defers the real call to the splice. The commands that follow it in the list (an upload's copy, the draw) stay after it, so the order holds.
  2. The readers of a surface's layout, stage and access on the draw's path ask the overlay.
  3. A surface made, loaded again, swapped or moved on a worker, and a new pass, stop the batch at that draw. The renderer thread sets up that draw and the rest. The commands that the worker recorded for it so far stay in the list.

Built so far: the deferred calls in the lists (vk_rec_defer, run by vk_rec_list_splice), not yet used.

Rewrite progress (serial batches, the picture right after each)

  1. Deferred calls in the lists (vk_rec_defer), run at the splice.
  2. vk_surf_use on a worker: the worker's view of the surface (layout, stage, access), the pass ended where its prediction needs a barrier, the real use at the splice (surf_use_splice, which counts mispredictions: none in the runs). 24 readers of a surface's layout on the draw's path ask vk_surf_layout. A surface in the view is never made over, loaded again, moved, swapped or freed before the list's calls run (vk_surf_before_reset).
  3. The pass state g_rp for each slot: a worker starts from the renderer thread's, and the renderer thread takes the worker's after the splice.
  4. The buffer cache's lookup state for each thread (t_lookup: the last lookup, its kind and sources, the patch scratch, the staged map of a draw with nothing aliased). The unified memory's memos (g_uma.memo, g_uma.rmemo) and the descriptor set cache for each slot (vk_render_slot).

Left: the surfaces' other state that draws change (write counts wgen, gw, recency, discards, pred_undefined), the buffer cache's entries and recency under a lock, the page tracking and the queries for each batch, the memory order's barriers deferred with the same prediction, the staging ring and the statistics for each thread, then batches in parallel with the check of the dependencies.

Split runs (draw_batches=2): what the validation runs showed

Split runs: the measured cost (one build, one scene of about 6,860 draws a present)

The renderer thread a presentfpsIts own draws a presentns a draw (resolve, setup, binds)
draw_batches=012.70 ms72.66,851394, 874, 300
draw_batches=212.73 ms72.64,448542, 1,303, 471

The worker draws about 2,400 draws a present, at about 2.1 µs each. But each draw that stays on the renderer thread costs about 750 ns more, and that cancels the gain.

To gain, a draw must not write a cache line that the other thread also writes. The hit paths of the buffer cache, the surfaces and the unified memory tables must then read with no lock and write nothing that is shared. That is a larger change than the steps so far.

Split runs: the renderer thread is the limit of this scene

An earlier version of this section said that the guest's threads limit the depths scene of game-11ccd5f3. That was wrong. It read the line "the GPU thread over the last 600 presents: busy", which is the front end's GPU thread (gpu.c, gpu_thread_busy_ns). The renderer thread is the GPU renderer thread of gpu_queue.c, and its line is "the renderer thread ran N records a present in X ms". In base, that is 11.5 to 12.4 ms of a 12.6 to 13.4 ms present (about 91%). The guest's threads together ran about half of one core.

Split runs: the changes that made the split gain

Each change was measured in base and split runs in turn, on the same build and the same route, with no other load. The numbers are for the presents with about 4,900 KB of records (the same scene in each run).

BuildChangeBase fpsSplit fpsThe renderer thread, base and split (ms a present)
before796911.5, 13.4
c3the share lock's counts on each side's own cache lines; the wrappers call through while no run is split; the worker's copies of the draw state taken by group74.3 to 74.976.0 to 79.912.2 to 12.5, 11.4 to 11.8
c5the batch probe and the draw marks on the renderer thread alone; the worker's statistics counters apart, folded in at the join73.8 to 75.276.9 to 81.012.2 to 12.5, 11.4

(The first row came from an older build and from runs with the game read from another disk, so compare its two columns only with each other.)

Split runs: fewer takes of the share lock

The scene and the presses are those of the table above. Each row is one or two runs of 180 s. The renderer thread's time is from its busy line, "ran N records a present (K KB) in X ms", in runs at about 4,900 KB a present.

BuildModefpsRenderer thread, ms a presentShare lock takes a present, renderer thread
c6: the commit of the splitbase74.0 to 74.812.1 to 12.4
c7: draw_timing=0base76.4 to 76.711.95
c7split81.6 to 81.711.1628,086
c8: the takes groupedsplit83.5 to 83.710.922,211
c9: the vertex and index memosplit84.0 to 87.110.5 to 10.819,666 to 19,750

The search for a scene that the renderer thread limits

Of the logs on this machine, only those of game-11ccd5f3 and of the Ounce game have the busy line. An older Ounce log (from a build of another branch) showed the renderer thread busy 18.8 ms of a 19.3 ms present, at about 1,830 draws and 10 µs a draw. This build gives 1.6 to 2.2 ms of busy time a present, at 1,260 to 1,730 draws, 1.1 µs a draw, and 59 to 77 fps. So with this branch, neither game has a scene that the renderer thread limits. The split cannot gain in them.

The Ounce runs found a bug of this work. The ring's batch of barriers was made per thread for the worker's lists. But with the split off, other threads also record into the ring, under vk_lock. A thread's batch then stayed pending after another thread submitted its command buffer. It went into the ring later, into a command buffer that was not recording (VUID-vkCmdPipelineBarrier2-commandBuffer-recording), and the driver crashed on the recording thread. The ring has one batch again. Only a thread that records into a list batches apart, and list_pause puts that batch into the list before the list goes into the ring.


Made from docs/dev/parallel-draw-batches.md at 69237645142c. GPL-3.0-or-later.