Skip to content

Scoped pass barriers: the dependency without the ALL_COMMANDS drain - #468

Open
maxi000q wants to merge 3 commits into
mamcjr:mainfrom
maxi000q:scoped-pass-barriers
Open

maxi000q wants to merge 3 commits into
mamcjr:mainfrom
maxi000q:scoped-pass-barriers

Conversation

@maxi000q

@maxi000q maxi000q commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What

A full barrier at every render-pass boundary (ALL_COMMANDS -> ALL_COMMANDS) is the most expensive thing to ask of a tiler between two render passes: it drains every stage at every boundary, hundreds of times a frame. On a Helio G95's Mali-G76, LARECOMP_VK_NO_PASS_BARRIER=1 measured what removing it entirely was worth - a large fps gain - at the price of flickering: a new pass read targets whose stores were not visible yet, or wrote one the previous pass was still sampling.

This keeps the dependency and drops the drain: the barrier is built from the writers actually recorded into the main command buffer since the last barrier, with the stages of that exact reader - a pass's draws, a dispatch, or whatever follows where the reader isn't known. Opt-in: LARECOMP_VK_PASS_BARRIER=1; the default is the full barrier exactly as main has it (every boundary, unconditionally). The gain measured below was with the first version's incomplete masks, so treat it as an upper bound: the number to trust is the one with complete masks, and the Vulkan sync validation on top of it.

The seven points, and where each lives

  1. The destination carries the reader now - pass_barrier(cb, reader): PB_PASS for a pass's draws (shader reads in every graphics stage, vertex/index fetches, indirect commands, attachment loads, tests and stores), PB_DISPATCH for a dispatch (COMPUTE_SHADER, its storage/uniform/sampled reads and its own writes - the WAW between two dispatches included), PB_ANY where what follows isn't known (the gfast window path, the dchain end): a pass, a dispatch or a copy, with TRANSFER reads and writes among them.
  2. WAW between passes - COLOR_ATTACHMENT_OUTPUT with COLOR_ATTACHMENT_READ and WRITE in the destination (the read has its stage now, no validation error), EARLY+LATE_FRAGMENT_TESTS with DEPTH_STENCIL_ATTACHMENT_WRITE beside the read.
  3. A draw's vertex/fragment storage writes - noted in note_gmem_store itself (PW_SHADER): the source of the next barrier carries VERTEX_SHADER | FRAGMENT_SHADER with SHADER_STORAGE_WRITE, and SHADER_READ beside it - an atomic counts as a read.
  4. Indirect and uniform reads - DRAW_INDIRECT + INDIRECT_COMMAND_READ in the destination for any writer, not only after a compute one; UNIFORM_READ in every destination.
  5. The two full barriers are back - after gfast_finish's copy-back and after gres_get's merge copies: the heap and the arena are read by draws' attributes, dispatches and copies alike, not a tracked subset.
  6. copy_buffer_to_texture is noted - right after vkCmdCopyBufferToImage, past the full barrier that cleared everything before it.
  7. vk_wait_seq doesn't touch the tracking - dropped: it runs on the game's thread while the render thread records, and a host wait orders nothing on the GPU anyway. The clearing is pass_barrier's and vk_full_barrier's alone.

Where it touches

runtime/nvn/vulkan/exec.c only: the tracker (a draw's shader stores added to the pass/dispatch/transfer bits), pass_barrier with its reader parameter, the two restored full barriers, and the note after each transfer call - every vkCmdCopy* / BlitImage / ResolveImage a real call, as before.

Measurements

On the Helio G95 (Mali-G76): the full barrier at every pass boundary was ~10% of each frame's GPU-side cost in the world; with the scoped barriers the same scenes held the no-barrier frame rate with no flickering across a 30-minute session (title screen, Great Plateau, menus with the map open) - 23-26 fps with the scoped barriers vs 19-21 with the full ones. As said above: an upper bound until re-measured with the complete masks. The Vulkan synchronization validation (VK_LAYER_KHRONOS_validation, sync validation) on the title screen, the Plateau and the map is the next thing I'll post.

A full barrier at every render-pass boundary (ALL_COMMANDS ->
ALL_COMMANDS) is the most expensive thing to ask of a tiler between two
render passes: it drains every stage at every boundary, hundreds of times
a frame - on a Helio G95's Mali-G76 (Redmi Note 9 Pro) removing it
entirely (LARECOMP_VK_NO_PASS_BARRIER=1) was worth a large fps gain, at
the price of flickering: a new pass read targets whose stores were not
visible yet, or wrote one the previous pass was still sampling.

This keeps the dependencies and drops the drain. Writers into the main
command buffer since the last barrier are tracked by kind - a pass's
colour and depth/stencil stores (vk_end_pass, vkCmdClearAttachments), a
compute dispatch's storage writes (dispatch, gfast_finish's placed
window), and every real transfer write recorded there (the vkCmdCopy* /
vkCmdBlitImage / vkCmdResolveImage calls, copy_buffer: all kept as real
commands) - and the barrier before a pass (or a dispatch that isn't
chained) is built from exactly those, with the stages that consume them:
attachment loads and depth tests, sampled and storage reads in the shader
stages, vertex/index fetches, indirect commands for a compute writer.
Nothing at all is emitted when nothing was written since the last
barrier.

The tracking is exact, not a heuristic: bits are cleared by the barrier
itself, by vk_full_barrier on the main buffer only (the upload buffer's
end-of-submission publication barrier does not order the main buffer's
recorded work, so those writers stay), and in vk_wait_seq only for the
submissions that actually completed (pass_writers_seq) - commands
recorded after a waited-on submission's buffers were closed are not
lost.

LARECOMP_VK_PASS_BARRIER: 0 none (as NO_PASS_BARRIER, unsafe), 1 scoped
(default), 2 the full barrier, as before. The dchain deferred barrier
(the one owed after a storing dispatch) is scoped the same way.
@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks, the copies are all real calls now. But the barriers still miss hazards, and it's on by default on every GPU:

  1. pass_barrier is also the barrier before a dispatch now, but its destination has no COMPUTE_SHADER or TRANSFER stage.
  2. No write-after-write between passes: the destination lacks COLOR_ATTACHMENT_OUTPUT / EARLY+LATE_FRAGMENT_TESTS with the attachment write accesses. COLOR_ATTACHMENT_READ is in the destination without its stage (a validation error).
  3. Fragment/vertex storage writes (note_gmem_store) are still not in the source for passes.
  4. Indirect reads are covered only after compute writes, not transfer; uniform reads aren't in the destination.
  5. Two full barriers that are needed were removed: after gfast_finish's copy-back (~5140) and after gres_get's merge copies (~5451).
  6. copy_buffer_to_texture (~7945) isn't noted, after a full barrier clears the tracking.
  7. Clearing the tracking in vk_wait_seq runs on the game's thread while the render thread updates it, and a host wait doesn't make GPU writes visible to later GPU work anyway. Please drop it.

Please make it opt-in (an env switch) and run the Vulkan synchronization validation (VK_LAYER_KHRONOS_validation with sync validation) on the title screen, the Plateau and the map with zero hazards. Then I'll A/B it here. With complete masks the gain may be smaller than measured, since part of it may come from the missing dependencies.

…rs back

The destination carries the reader's stages now (a pass, a dispatch, or whatever
follows): COMPUTE_SHADER before a dispatch, TRANSFER where the reader isn't known,
colour/depth attachment writes between passes (WAW), and the stages the reads
belong to. The source gains a draw's vertex/fragment storage writes
(note_gmem_store). Indirect-command and uniform reads are in every destination.
The full barriers after gfast_finish's copy-back and gres_get's merge copies are
back (everything reads the heap and the arena, not a tracked subset), and
vk_wait_seq no longer touches the tracking: the game's thread is not the one
recording it, and a host wait orders nothing on the GPU. copy_buffer_to_texture's
write is tracked. Opt-in: LARECOMP_VK_PASS_BARRIER=1; the default is the full
barrier exactly as main has it.
@maxi000q

maxi000q commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

All seven taken, and you were right to be suspicious of the masks: with them completed (5 and 6 included) I can see how part of the measured gain was missing dependencies, not saved work. Pushed as 977411b, one commit on top:

  1. The destination is per-reader now: pass_barrier(cb, reader). A pass's draws get the graphics stages, vertex/index fetches, indirect commands and the attachments; a dispatch gets COMPUTE_SHADER with its storage/uniform/sampled reads and its own writes (the dispatch-after-dispatch WAW included); the two places where what follows isn't known (gfast's window path, the dchain end) get everything, TRANSFER reads and writes among it.
  2. WAW between passes: COLOR_ATTACHMENT_OUTPUT is in the destination with COLOR_ATTACHMENT_READ and WRITE (the read has its stage now, no validation error), and EARLY+LATE_FRAGMENT_TESTS with DEPTH_STENCIL_ATTACHMENT_WRITE beside the read.
  3. A draw's vertex/fragment storage writes are noted in note_gmem_store itself: PW_SHADER, VERTEX_SHADER | FRAGMENT_SHADER with SHADER_STORAGE_WRITE and SHADER_READ (an atomic counts as a read), in the source of the next barrier.
  4. DRAW_INDIRECT + INDIRECT_COMMAND_READ for any writer now, not only after a compute one; UNIFORM_READ in every destination.
  5. Both full barriers are back: after gfast_finish's copy-back and after gres_get's merge copies. You're right that they can't be scoped: the heap and the arena are read by draws' attributes, dispatches and copies alike, and "everything" is what ALL_COMMANDS says.
  6. copy_buffer_to_texture is noted right after vkCmdCopyBufferToImage, past the full barrier that cleared everything before it.
  7. vk_wait_seq doesn't touch the tracking anymore: dropped. The thread point alone would have been enough; the host-wait one makes it twice right.

It's opt-in now: LARECOMP_VK_PASS_BARRIER=1 for the scoped barriers, and the default is the full barrier exactly as main has it, every boundary, unconditionally, nothing tracked. 0 still means none, as a measurement.

@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks, every item is done, and making it opt-in (LARECOMP_VK_PASS_BARRIER=1, the full barrier by default) is right. Two small things:

  1. With the default (2), pass_note_writers (exec.c:75-82) still adds a full barrier when writers are left over from an earlier submission, so the default isn't the same as main. That barrier isn't needed (a barrier in submission N+1 already orders N's writes). Please skip the tracking when the mode isn't 1.
  2. The header comment says copy_buffer is tracked; it isn't (every caller is followed by a full barrier). Just the comment.

Then, as said: the sync-validation log with zero hazards (title, Plateau, map), and I'll A/B =1 here.

pass_note_writers() still noted writers with the full barrier on (the default),
and a note straddling a submission boundary added a vk_full_barrier main doesn't
have - the default wasn't the same as main. The tracking (and that boundary
barrier) is the scoped mode's alone now: pass_barrier_mode() is shared, and every
other mode notes nothing, so the default is exactly main's and 0 exactly the old
NO_PASS_BARRIER switch. The header comment also listed copy_buffer among the
tracked transfers; it isn't (every caller is followed by a full barrier, which
orders and clears all of the tracking anyway) - the comment says so now.
@maxi000q

maxi000q commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Both taken, pushed as f645172:

  1. The tracking is the scoped mode's alone now. pass_barrier_mode() is shared between pass_barrier and pass_note_writers, and a note does nothing unless the mode is 1: nothing is tracked with the full barrier, so the boundary barrier you found is gone too, and the default is exactly main, barrier for barrier and nothing besides. Mode 0 is exactly the old NO_PASS_BARRIER switch.
  2. The header comment: copy_buffer is out of the tracked list, with the reason where it stood: every caller of it is followed by a full barrier, which orders the writes and clears the whole tracking anyway.

@mamcjr mamcjr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patience on this one. Main has moved under it, so it needs a rebase before I can test it:

  1. Conflicts in the dispatch chaining added since (7845d58, and d36c06a's arena copy-back): gfast_finish, the gres_get merge, and dispatch_compute, where main now carries dchain_after = owe_after and the PR resets it to false. That code runs in the copy mode, which Turnip uses, so the rebase has to keep main's owe_after exactly, or the default barriers change on Adreno.
  2. Mode 0 and AB nobarrier aren't the old switch any more. On main they only removed the begin_pass barrier. Here pass_barrier returns early for them, and it is also used before dispatches, after the gfast window and at the dchain end. So those measurement switches now drop the dispatch and compute barriers too, and old A/B numbers aren't comparable. Applying that early return only for PB_PASS would keep them as they were.
  3. Mode 1 records a full barrier from inside pass_note_writers when the submission changes, wherever the note is made, possibly inside a dynamic-rendering instance (clears through vkCmdClearAttachments, note_gmem_store after an in-pass draw), where an ALL_COMMANDS barrier is invalid. It's probably unreachable because begin_pass clears pass_writers, but that is what the sync-validation run would prove. Could you post it (title, Plateau, map, zero hazards)?
  4. After the rebase, the scoped masks need checking against the paths main gained since: the in-place feedback reads (#507) and the per-submission cbwin snapshot.

Once it's rebased I'll compare debug frames and barrier counts against main on the POCO with the default, then try =1.

@mamcjr

mamcjr commented Nov 27, 2026

Copy link
Copy Markdown
Owner

The four points from 11-24 are still open:

  1. Conflict in exec.c. The PR still has dchain_after = false in dispatch_compute, where main has bool owe_after = chained && dchain_after; ... dchain_after = owe_after;. Turnip runs that code in copy mode, so the rebase has to keep it exactly.
  2. Mode 0 / AB("nobarrier") now return at the top of pass_barrier (PR exec.c:2224), which also drops the dispatch, gfast-window and dchain-end barriers. On main they only removed the begin_pass one. Please return early only for PB_PASS there.
  3. A full barrier inside a pass (mode 1). pass_note_writers calls vk_full_barrier when the seq changes (PR exec.c:94), and it runs right after vkCmdClearAttachments inside a pass (2799-2800) and from note_gmem_store after an in-pass draw (6057). An ALL_COMMANDS barrier inside dynamic rendering without a self-dependency is invalid. The sync-validation log (title, Plateau, map, zero hazards) is still the proof we need.
  4. The masks haven't been checked against vulkan: allow in-place render-target feedback reads setting on Mali #507's in-place feedback reads and the per-submission cbwin snapshot.

About the numbers: this PR says the Helio G95 went 19-21 to 23-26 fps, and #466 says the same phone went 14-15 to 23-26 with the waiter alone. Which build was each measured on? (#466 is merged now, with the waiter on by default on Mali, so new measurements here should start from main.)

If there's no update in a couple of weeks, I'll close it as stale; it can always be reopened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants