Split runs: who owns the state of a draw (design)

Status: a design. One Switch game's measurements (about 2,100 draws a present) showed what remains after the share lock's cheap cuts (the write tracking with no lock, the range generations of the constant buffer and vertex entries, the kept targets' read checks once a present):

So the remaining takes are the cost, and they are takes of state that both threads change at each draw.

What a draw shares today

A map of the per-draw calls under the lock (vk_surf_use, target_written, render_draw_main, vk_query_draw, stage_set_, and the lookups vk_tex_recheck, vk_buf_get_storage, vk_buf_get_window, vk_shader_get, vk_table_words, vk_tex_view, vk_rec_new_set) found 13 objects that both threads write in one split run:

  1. g_wseq and Surf.wseq; g_surf_gen and Surf.wgen; g_take_gen and Surf.take_seen (surface.c's vk_surf_wrote, take_changed).
  2. The drawn list (g_drawn, g_ndrawn, in_drawn, drawn_from, g_drawn_pages) and a target's draw_* fields (vk_rt_drawn).
  3. The linear and block-linear lists (linear_note), fb_snap, fb_draw and fb_gen (texture.c).
  4. Surf.stage through vk_surf_read_covered, which takes no lock, and g_written's counts.
  5. The buffer cache's entries, LRU stamps and windows; the texture checks' state; the shader cache ways and the argument globals of g_pipeline; g_table; g_frame_sets.
  6. Counters with no lock: g_vk_drawn, g_stats's slot counts, g_predraw_stats, g_draws.

Of these, only a surface's layout, stage and access are deferred to the splice and checked there (surf_use_splice).

Two faults of order

On the GPU, the renderer thread's half of a split run comes before the worker's half. The shared counters do not follow that order:

Neither shows in a default run, as draw_batches is 0. Both must be fixed before the split goes on by default.

The design: journals applied at the join

Each thread owns what its half changes, and the renderer thread applies the worker's changes at the join, in the order of the GPU:

Split points with no dependency

The journal is right only when the worker's half reads nothing that the renderer thread's half writes, as the worker's half can't see those writes before the join. The split chooses its point so:

The batch probe already counts such dependencies ("sample a target an earlier batch of the present wrote"): in the other game's scene they were a few percent of the draws.

Build steps

Each step keeps the picture of the base run (screenshots at the same frame) and is measured with draw_timing=1: the share lock's takes a draw, the ns a draw on each thread, and the fps in A/B/B/A runs.

  1. Counters on each thread (the list above). Gate: no change of the picture, fewer lines that both cores write. Done: the draw count of each thread (vk_drawn) and the feedback statistics of each slot.
  2. The write journal and the worker's view of it. Gate: wseq follows the GPU order (a check that compares, at each join, the numbers of the targets of both halves), and no take of the lock in target_written on either thread.
  3. The split point with no dependency. The worker learns what a draw samples only in its setup (stage_set_), from the texture headers its shaders index, so a draw that depends on the renderer thread's half is found after its setup began. It is undone: the worker's list and journal go back to their length before the draw, and the cut (the records the worker drew) goes to the join, which skips that many (gpu_ds_batch_done). The renderer thread's copy of the state has every skipped record applied already, so it draws the rest from the cut. The renderer thread half's targets come from the worker's apply of the records before its half (gpu_ds_batch_apply), which it makes now. This needs step 2: with the target writes in the journal, nothing outside the list and the journal changed for the undone draw but caches, which a miss fills again. Count the draws given back.
  4. The lookups' hits under seqlocks.
  5. A judgement in three games, the split on against off. The split goes on by default only where it gains in each.

Where the build stands

Built: step 1 (vk_drawn, the feedback statistics), step 2 (the journal), step 3 (built as a wait, not a cut: a worker draw that reads a target of the renderer thread's half waits for that half's end, which needs no rollback), and step 4's lock-free calls (the texture table, new sets, a target's read note once a present). In one Switch game (about 2,100 draws a present) the worker's journal held 687 writes a present with none applied early, and the worker waited for the other half 1.1 times a present.

The share lock's takes a draw went from about 12 to 6.5 on the renderer thread and 5.7 on the worker, and its waits from 0.93 to 0.45 ms a present. The split did not gain from it: in A/B runs it stayed about 6 fps under the base (63.7 against 69.3 fps). A draw on the renderer thread still costs about 44% more than in base (4,617 against 3,205 ns), and in parts that take no lock:

Then the join kept the worker's record of the bound state (g_dyn, the index buffer), and a renderer-thread draw cost what it does in base (resolve, setup and binds 1,058, 1,874 and 352 ns, against 1,058, 1,809 and 338). Most of the 44% was the state bound again after each join, and its effect on the caches after it. The split still lost, in all three games measured, A/B/B/A with the same build:

GameBase fpsSplit fps
One Switch game's town70.6, 68.6, 69.264.0, 63.7, 63.8
Another's open world101.6, 102.784.4, 83.0
A third's race (its windows)290 to 317250 to 300

In none of them is the renderer thread what limits the frame: in the first it is busy about 10 of 14.5 ms, and the GPU waits about 4 ms a frame for submissions. The worker's draws reach the ring only at each join, in bursts, where in base they go as they are recorded; that is the likely cost, not measured yet. So draw_batches stays 0. The split can gain only in a scene that the renderer thread limits (the 6,860-draw scene above).

These are costs of cutting one stream into two halves, not of the lock. They fall when a split's runs are longer: the scene where the split gained had about 6,860 draws a present. The next steps, if the split goes on, are runs cut where the state changes anyway (a pass or a program change), not at the half; and binds the join keeps when both halves end in the same state.


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