Built so far (uncommitted)

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.

Shared calls of a batch's draws (measured)

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).

The buffer cache's hit path (mapped)

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:

  1. writes memos, counters and statistics of the last lookup (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);
  2. records a barrier, and ends the render pass for it, where a page was written in place since the last barrier (order in uma.c, order_use in pagespace.c). That choice depends on the order of all writes and barriers before the draw;
  3. takes 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.