render.c (the draw table, the targets, the fragment key, the
recent textures, the texture memo, the constant buffer memos and windows, the storage and set caches, the index and
quad buffers, the per-draw block, and the command buffer's bind state g_dyn) hold an entry for each slot
(RC_SLOTS), which a thread takes by t_rc. The functions that forget them clear every slot. Cost, three runs of 100
s a side with a shader cache for each build: the renderer thread 11.54 ms a present at 79.2 fps without, 11.79 ms at
77.2 fps with (about 2%).record.c, VkRecList): a thread's packets go to its list, and vk_rec_list_splice puts a list
into the ring. Each thread has its own batch of barriers and its own draw tags. Descriptor set ids come from an
atomic count. A command that works on the ring itself (a command buffer's begin, a submit, a drain) first puts the
thread's list into the ring.draw_batches=1): after a draw, the draw records that follow it go to the worker thread
(prep.c), which sets them up into its list with slot 1 and its own copies of the draw state
(gpu_ds_batch_draw), while the renderer thread waits. The renderer thread then splices the list and passes over
those draws (gpu_ds_batch_done). The picture is right. A first run showed wrong draws: both threads record into one
command buffer, so the bind state of the slot that did not record last was stale. Each side now binds everything
again after the other recorded. Speed: 72 fps, the renderer thread 12.6 to 13.7 ms a present, slower than without (as
expected: the renderer thread waits for each batch).Next is step 4: the shared state that a batch and the renderer thread would touch at the same time. Besides the caches
above, that is the render pass (g_rp), the surfaces' layouts and write counts, the staging ring, the buffer cache, the
queries, the statistics, and the descriptor pools' counts.
A probe counted the calls of shared functions from the worker's draws in serial batches (600 presents, 155,000
batches of about 20 draws, 3.14 million draws, 76% of all draws). For each draw in a batch: the buffer cache about 14
calls (vk_buf_prefetch 11.8, vk_buf_get_input 2.3, vk_buf_draw_prepare 0.24), the surfaces about 7
(vk_surf_touch 2.2, vk_rt_note_read 2.2, vk_surf_wrote 1.5, vk_surf_use 1.0), the page order tracking 2.0
(vk_pages_read), the queries 1.0 (vk_query_draw), the staging ring 0.38 (vk_stage_upload), the texture checks,
views and samplers about 0.09, the pipelines and descriptor sets under 0.01.
So step 4 starts with the four structures that every draw uses: the buffer cache, the surfaces, the page tracking and the queries. Each must take two threads at once, or keep a part for each thread that the renderer thread merges in batch order (the page tracking and the queries record an order, and the surfaces' write counts and layouts decide barriers and the texture memos' freshness).
A map of vk_buf_prefetch, vk_buf_get_input, vk_buf_get_uniform_at and vk_buf_draw_prepare (buffer.c, with
uma.c and pagespace.c) found that a hit is not only a lookup. Where guest memory is read in place (the unified
memory hybrid, the default on a discrete GPU with resizable BAR on Windows), a hit also:
g_buffer.got, g_buffer.lk, g_buf_kind,
g_buf_sources, g_uma.memo, g_uma.rmemo, g_uma.hits) and LRU stamps (Vb.used, the own buffers' list,
g_uma.imp[].used);order in uma.c, order_use in pagespace.c). That choice depends on the order of all writes and barriers
before the draw;vk_lock in vk_release_pending on the page cache's path, and where image or compute results wait, in
vk_gfx_image_read_need and vk_compute_mem_need, which can land them.So the buffer cache needs three things: a part for each thread for (1), a lock for each draw's buffer phase for the entries and their LRU, and for (2) a change of where barriers are chosen. A worker notes the pages its batch reads and writes, and the renderer thread chooses the barriers when it splices the batch, in batch order. The surfaces' layouts and write counts choose barriers the same way. That change is the core of form B, and larger than the build steps above said. Results land only on miss paths and in rare checks, so an asynchronous download of results does not change the renderer thread's time in this scene (its waits are about 0.2 ms a present).
Made from docs/dev/parallel-draw-batches.md at 69237645142c. GPL-3.0-or-later.