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.
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:
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.Built so far: the deferred calls in the lists (vk_rec_defer, run by vk_rec_list_splice), not yet used.
vk_rec_defer), run at the splice.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).g_rp for each slot: a worker starts from the renderer thread's, and the renderer thread takes the
worker's after the splice.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.
draw_batches=2): what the validation runs showedgpu_ds_batch_apply) while the renderer thread runs the same
records. The draw queue frees a record when the renderer thread is done with it, so the front end could put new
records over bytes that the worker still reads. The renderer thread now keeps those records in the queue from the
split to the join (gq_keep, gq_keep_end).VUID-vkCmdBindVertexBuffers2-pStrides-06209 (stride 6, attribute extent 8) only in
runs whose shader objects come from SPIR-V. This occurs in the first run after each build, because the binaries of
the shader objects are not used then. The base build draws the same layouts: a 6-byte stream with an 8-byte
attribute is valid, and vkCmdBindVertexBuffers2 gets no strides. With no pipeline bound, the layer (SDK 1.4.341)
takes the binding stride as dynamic and compares the stride of vkCmdSetVertexInputEXT. With binaries, it has no
SPIR-V to reflect and skips the check. A split run with a warm cache shows no error of this kind. Compare
validation runs only at the same cache state.| The renderer thread a present | fps | Its own draws a present | ns a draw (resolve, setup, binds) | |
|---|---|---|---|---|
draw_batches=0 | 12.70 ms | 72.6 | 6,851 | 394, 874, 300 |
draw_batches=2 | 12.73 ms | 72.6 | 4,448 | 542, 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.
vk_cmd with no lock during a split brought it to 41,000 takes and 1.5 ms
of wait on each side. That is still about 360 ns of each draw of the renderer thread.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.
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.
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).
| Build | Change | Base fps | Split fps | The renderer thread, base and split (ms a present) |
|---|---|---|---|---|
| before | 79 | 69 | 11.5, 13.4 | |
| c3 | the 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 group | 74.3 to 74.9 | 76.0 to 79.9 | 12.2 to 12.5, 11.4 to 11.8 |
| c5 | the batch probe and the draw marks on the renderer thread alone; the worker's statistics counters apart, folded in at the join | 73.8 to 75.2 | 76.9 to 81.0 | 12.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.)
g_draw_marks) were one array that both threads wrote. The worker's marks went into the renderer
thread's steps: "to render_draw" showed 400 ns a draw that did not exist. With the marks on the renderer thread
alone, it is 43 ns.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.
| Build | Mode | fps | Renderer thread, ms a present | Share lock takes a present, renderer thread |
|---|---|---|---|---|
| c6: the commit of the split | base | 74.0 to 74.8 | 12.1 to 12.4 | |
c7: draw_timing=0 | base | 76.4 to 76.7 | 11.95 | |
| c7 | split | 81.6 to 81.7 | 11.16 | 28,086 |
| c8: the takes grouped | split | 83.5 to 83.7 | 10.9 | 22,211 |
| c9: the vertex and index memo | split | 84.0 to 87.1 | 10.5 to 10.8 | 19,666 to 19,750 |
draw_timing setting (default 0). They cost about 3% in base.cb_get (the pending checks and the staging upload) take
the lock one time, not one time for each call. A call inside a held group does not set the label of the next take
(vk_share_lock). Before this, the counts by function gave takes to the wrong function.vin_get keeps the vertex and index lookups of each thread by address, with the rules of cb_kept. A hit
takes no lock. 89% of the 16,300 lookups a present hit.vk_surf_use (3,700 takes a present, both sides) is the next largest. After the lock, the renderer
thread spends about 4.7 ms a present in records that are not draws. The GPU time of this scene is not measured.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.