Merging one-record indirect mesh draws into one ExecuteIndirect: what blocks it, how many would merge (68–74%), and a plan #828

Open
opened 2026-11-26 03:52:00 +00:00 by github-bot · 1 comment
Owner

@⁠VibeClauding on GitHub · 2026-11-25 09:22 UTC · original

The case for merging, and a plan

Goal: draw a run of consecutive one-record indirect mesh draws (same pipeline, same list) with one multi-record ExecuteIndirect, keeping the picture correct.

Why it's worth it:

  • #107: on an RTX 5080, 40 of 63 ms a frame of GPU time sat in about 3,200 indirect executions, most of them one-record mesh draws. dkdknight's upper-bound experiment (merging while ignoring what differs, so the picture broke) saved about 30 ms of GPU a frame.
  • ryotdev timed each execution at about 11.5 µs, plus 3.5 µs for its clamp. Neither the clamp barriers (#220) nor windows removed that per-execution cost.

What blocks it today, and why it can be lifted

dkdknight found that a descriptor table changes between consecutive draws, and command signatures can't set descriptor tables. Reading the code on 9376fd6, that table is ours, not the title's:

  • The title's tables are already offsets in root constants. State::bind (draw.rs ~1035) makes the title's tables fresh in dynamic slots and writes their offsets into the stage's user-data root constants. Shaders index the heap directly (CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED, createHandleFromHeap). A command signature can set root constants per record (D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT).
  • The only per-draw descriptor tables are one-view tables for root SRVs/UAVs. These are root_uav / root_srv_table (draw.rs ~1166–1350), made because bounded_root_srvs() (pipeline/host.rs:277) turns root views into one-view tables so that a read can't leave its buffer.
  • VICTORY_ROOT_SRV_DIRECT=1 switches them back to root descriptors, which a command signature can set per record, but the bounds are needed. On an RTX 3070 (run 73: 8dd30ab + #220 #648 #670 #705 #346, Series S), that switch removed the device during loading: 0x887A0006 with no breadcrumbs, the submitting thread preparing, nothing of ours on the card. One run doesn't prove the cause, but unbounded root views are the obvious suspect. So root descriptors are not the way.

The way that keeps the bounds: have the translator read a bounded root view through the heap. The shader takes ResourceDescriptorHeap[i], where i is the one-view table's slot, passed in a root-constant dword of the stage's user data. That is the same thing bind already does for the title's own tables (RootType::Table offsets). After that, everything bind sets per draw is root constants, and a command signature can set those per record. Our own root views (SLOT_MEMORY, SLOT_STRIDES, SLOT_FAULTS) are the same for every draw of a list, so they stay set once.

How many draws could merge (this fork, RTX 3070, Series S, first room)

I ran a small script over VICTORY_CAPTURE's ops log. A run is consecutive operations of one host list that are all one-record indirect draws of the same kind and pipeline, with nothing else of that list between them.

frame one-record mesh draws in runs of 2+ calls a merge would save draws in runs of 16+
run 72, frame 2400 1,371 1,132 929 (68%) 455
run 71, frame 2400 606 529 451 (74%) 254

That matches ThiagoAlexsander's 58% on the 5080 (#107). One-record indirect dispatches rarely run together (3–10%), so this is about mesh draws.

The script (Python, reads frame-N-ops.txt)
import re, sys
from collections import Counter, defaultdict
OP = re.compile(r"^(\d+) list (0x[0-9a-f]+) (\S+)(.*)$")
IND = re.compile(r"kind (\d+) max (\d+) at (0x[0-9a-f]+)( count)?.*?\[([^\]]*)\]")
for path in sys.argv[1:]:
    last, lengths = {}, defaultdict(list)
    for line in open(path, encoding="utf-8", errors="replace"):
        m = OP.match(line)
        if not m: continue
        _, lst, op, rest = m.groups()
        key = None
        if op == "indirect":
            i = IND.search(rest)
            if i and i.group(2) == "1" and not i.group(4): key = (i.group(1), i.group(5))
        prev = last.get(lst)
        if key is not None and prev is not None and prev[0] == key:
            last[lst] = (key, prev[1] + 1); continue
        if prev is not None: lengths[prev[0][0]].append(prev[1])
        last[lst] = (key, 1) if key is not None else None
    for prev in last.values():
        if prev is not None: lengths[prev[0][0]].append(prev[1])
    for kind, ls in lengths.items():
        print(path, "kind", kind, "draws", sum(ls), "runs", len(ls), "saved", sum(ls) - len(ls))

Plan (each step can be a PR of its own, behind VICTORY_INDIRECT_MERGE=1, off by default)

  1. Bounded root views by heap index (translator + bind). For each root SRV/UAV slot, the translation gets a user-data dword (a new RootType::Other kind) holding the heap index of the view root_uav / root_srv_table make. The shader builds the handle with createHandleFromHeap. pipeline/host.rs then makes no table parameter for those slots, and bind writes the index into the constants instead of calling Set*RootDescriptorTable. It changes translations, so the cache key moves. This keeps the bounds and is worth having on its own: one fewer root parameter per view.
  2. Root writes as data. Make the root-argument calls in bind / prepare_for go through one recorder: (param, Constants(Vec<u32>) | Srv(u64) | Uav(u64) | Cbv(u64)). Count every other command they record (barriers, render targets, tables, scissor). A draw whose preparation recorded nothing but root writes can join a run. This is a refactor with no behaviour change.
  3. Signatures with root arguments. For a host pipeline, build a command signature with one CONSTANT argument per stage's user data (after step 0, that is all that varies), then DISPATCH_MESH. It needs the root signature. Cached per root signature.
  4. Deferred run in State. A one-record mesh draw that passes step 1's test is appended to a pending run instead of executed: its root writes, and where its clamped arguments are ((scratch, at) from the window). The run is flushed with one ExecuteIndirect(signature, n, merged, 0, None, 0) when the next draw differs (pipeline, signature, a non-root command), on any other operation of the list, at close, and at a size limit.
  5. The merged argument buffer.
    • The CPU knows the root fields at record time, so they go into an upload ring per list.
    • The GPU knows the mesh arguments (clamped into scratch), so a tiny gather pass per run copies each record's 12 bytes from scratch + at_i into the merged buffer at the record's offset.
    • A first version may use one CopyBufferRegion per record, which is still far cheaper than an ExecuteIndirect, and measure that first.
  6. Proof.
    • A WARP test: N one-record mesh draws with differing root constants and root SRVs, drawn separately and merged, must give byte-identical render targets.
    • In game: VICTORY_GPU_TIMING per command, fps A/B, and VICTORY_CAPTURE frames compared.

Pitfalls I can see

  • Barriers between draws of a run. If the preparation of draw k+1 records a barrier, the run must end before it. Step 1's counter decides that.
  • Mesh scissor. mesh_scissor(list, true/false) is set around each mesh ExecuteIndirect. It's the same for a whole run, so it should bracket the merged call once.
  • Windows' scratch. Records of a run can come from more than one window, and a newer window overwrites the scratch at offset 0 (venglov's note on #107). The gather in step 4 must run before the next clamp, so a new window should flush the run.
  • Draws that store. The windows already exclude shaders that store, and a run should too (same rule).

I'm starting on step 0, then 1–3, on VibeClauding/victory-fork and will link the branch here. Anyone who wants a step, say which one so we don't duplicate. Steps 2 and 4 can be written against step 1's interface without waiting for it, and step 1 doesn't depend on step 0.

AI-assisted (Claude Code); measured on my machine with my own copy; counts, timings and code references only.

**@⁠VibeClauding** on GitHub · 2026-11-25 09:22 UTC · [original](https://github.com/fxceless/victory-fork/issues/828) ## The case for merging, and a plan **Goal:** draw a run of consecutive one-record indirect mesh draws (same pipeline, same list) with **one** multi-record `ExecuteIndirect`, keeping the picture correct. **Why it's worth it:** - #107: on an RTX 5080, 40 of 63 ms a frame of GPU time sat in about 3,200 indirect executions, most of them one-record mesh draws. dkdknight's upper-bound experiment (merging while ignoring what differs, so the picture broke) saved **about 30 ms of GPU a frame**. - ryotdev timed each execution at about 11.5 µs, plus 3.5 µs for its clamp. Neither the clamp barriers (#220) nor windows removed that per-execution cost. ### What blocks it today, and why it can be lifted dkdknight found that a descriptor table changes between consecutive draws, and command signatures can't set descriptor tables. Reading the code on `9376fd6`, that table is ours, not the title's: - **The title's tables are already offsets in root constants.** `State::bind` (draw.rs ~1035) makes the title's tables fresh in dynamic slots and writes their offsets into the stage's user-data root constants. Shaders index the heap directly (`CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED`, `createHandleFromHeap`). A command signature can set root constants per record (`D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT`). - **The only per-draw descriptor tables are one-view tables for root SRVs/UAVs.** These are `root_uav` / `root_srv_table` (draw.rs ~1166–1350), made because `bounded_root_srvs()` (pipeline/host.rs:277) turns root views into one-view tables so that a read can't leave its buffer. - **`VICTORY_ROOT_SRV_DIRECT=1` switches them back to root descriptors, which a command signature can set per record, but the bounds are needed.** On an RTX 3070 (run 73: `8dd30ab` + #220 #648 #670 #705 #346, Series S), that switch removed the device during loading: `0x887A0006` with no breadcrumbs, the submitting thread preparing, nothing of ours on the card. One run doesn't prove the cause, but unbounded root views are the obvious suspect. So root descriptors are not the way. **The way that keeps the bounds:** have the translator read a bounded root view through the heap. The shader takes `ResourceDescriptorHeap[i]`, where `i` is the one-view table's slot, passed in a root-constant dword of the stage's user data. That is the same thing `bind` already does for the title's own tables (`RootType::Table` offsets). After that, everything `bind` sets per draw is root constants, and a command signature can set those per record. Our own root views (`SLOT_MEMORY`, `SLOT_STRIDES`, `SLOT_FAULTS`) are the same for every draw of a list, so they stay set once. ### How many draws could merge (this fork, RTX 3070, Series S, first room) I ran a small script over `VICTORY_CAPTURE`'s ops log. A run is consecutive operations of one host list that are all one-record indirect draws of the same kind and pipeline, with nothing else of that list between them. | frame | one-record mesh draws | in runs of 2+ | calls a merge would save | draws in runs of 16+ | |---|---|---|---|---| | run 72, frame 2400 | 1,371 | 1,132 | **929 (68%)** | 455 | | run 71, frame 2400 | 606 | 529 | **451 (74%)** | 254 | That matches ThiagoAlexsander's 58% on the 5080 (#107). One-record indirect *dispatches* rarely run together (3–10%), so this is about mesh draws. <details><summary>The script (Python, reads frame-N-ops.txt)</summary> ```python import re, sys from collections import Counter, defaultdict OP = re.compile(r"^(\d+) list (0x[0-9a-f]+) (\S+)(.*)$") IND = re.compile(r"kind (\d+) max (\d+) at (0x[0-9a-f]+)( count)?.*?\[([^\]]*)\]") for path in sys.argv[1:]: last, lengths = {}, defaultdict(list) for line in open(path, encoding="utf-8", errors="replace"): m = OP.match(line) if not m: continue _, lst, op, rest = m.groups() key = None if op == "indirect": i = IND.search(rest) if i and i.group(2) == "1" and not i.group(4): key = (i.group(1), i.group(5)) prev = last.get(lst) if key is not None and prev is not None and prev[0] == key: last[lst] = (key, prev[1] + 1); continue if prev is not None: lengths[prev[0][0]].append(prev[1]) last[lst] = (key, 1) if key is not None else None for prev in last.values(): if prev is not None: lengths[prev[0][0]].append(prev[1]) for kind, ls in lengths.items(): print(path, "kind", kind, "draws", sum(ls), "runs", len(ls), "saved", sum(ls) - len(ls)) ``` </details> ### Plan (each step can be a PR of its own, behind `VICTORY_INDIRECT_MERGE=1`, off by default) 0. **Bounded root views by heap index (translator + bind).** For each root SRV/UAV slot, the translation gets a user-data dword (a new `RootType::Other` kind) holding the heap index of the view `root_uav` / `root_srv_table` make. The shader builds the handle with `createHandleFromHeap`. `pipeline/host.rs` then makes no table parameter for those slots, and `bind` writes the index into the constants instead of calling `Set*RootDescriptorTable`. It changes translations, so the cache key moves. This keeps the bounds and is worth having on its own: one fewer root parameter per view. 1. **Root writes as data.** Make the root-argument calls in `bind` / `prepare_for` go through one recorder: `(param, Constants(Vec<u32>) | Srv(u64) | Uav(u64) | Cbv(u64))`. Count every *other* command they record (barriers, render targets, tables, scissor). A draw whose preparation recorded nothing but root writes can join a run. This is a refactor with no behaviour change. 2. **Signatures with root arguments.** For a host pipeline, build a command signature with one `CONSTANT` argument per stage's user data (after step 0, that is all that varies), then `DISPATCH_MESH`. It needs the root signature. Cached per root signature. 3. **Deferred run in `State`.** A one-record mesh draw that passes step 1's test is appended to a pending run instead of executed: its root writes, and where its clamped arguments are (`(scratch, at)` from the window). The run is flushed with one `ExecuteIndirect(signature, n, merged, 0, None, 0)` when the next draw differs (pipeline, signature, a non-root command), on any other operation of the list, at close, and at a size limit. 4. **The merged argument buffer.** - The CPU knows the root fields at record time, so they go into an upload ring per list. - The GPU knows the mesh arguments (clamped into scratch), so a tiny gather pass per run copies each record's 12 bytes from `scratch + at_i` into the merged buffer at the record's offset. - A first version may use one `CopyBufferRegion` per record, which is still far cheaper than an `ExecuteIndirect`, and measure that first. 5. **Proof.** - A WARP test: N one-record mesh draws with differing root constants and root SRVs, drawn separately and merged, must give byte-identical render targets. - In game: `VICTORY_GPU_TIMING` per command, fps A/B, and `VICTORY_CAPTURE` frames compared. ### Pitfalls I can see - **Barriers between draws of a run.** If the preparation of draw k+1 records a barrier, the run must end before it. Step 1's counter decides that. - **Mesh scissor.** `mesh_scissor(list, true/false)` is set around each mesh `ExecuteIndirect`. It's the same for a whole run, so it should bracket the merged call once. - **Windows' scratch.** Records of a run can come from more than one window, and a newer window overwrites the scratch at offset 0 (venglov's note on #107). The gather in step 4 must run before the next clamp, so a new window should flush the run. - **Draws that store.** The windows already exclude shaders that store, and a run should too (same rule). I'm starting on step 0, then 1–3, on [VibeClauding/victory-fork](https://github.com/VibeClauding/victory-fork) and will link the branch here. Anyone who wants a step, say which one so we don't duplicate. Steps 2 and 4 can be written against step 1's interface without waiting for it, and step 1 doesn't depend on step 0. AI-assisted (Claude Code); measured on my machine with my own copy; counts, timings and code references only.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:52:00 +00:00
Author
Owner

@⁠VibeClauding on GitHub · 2026-11-25 09:44 UTC · original

Step 0 is done and tested in game: VibeClauding/victory-fork@pr/root-view-index (6176086 on 9376fd6, 4 files, +114/−34).

  • VICTORY_ROOT_VIEW_INDEX=1: the translator reads the title's root SRVs/UAVs with createHandleFromHeap at an index from a user-data dword (new RootType::Other code USER_VIEW_SLOT, dword 0 SRV / 1 UAV). The slot gets no table parameter.
  • bind makes the same bounded one-view table as before. root_uav / root_srv_table now return the slot and set a table only when given a parameter. bind writes the slot into the constants.
  • The switch is in the translation fingerprint (the build script picks it up).
  • Measured (RTX 3070, Series S, my stack): translate --warm all gives the same 254 programs that don't translate, with or without the switch. In game, run 74 (pipelines being made) and run 75: 8.2–9.0 fps, no errors, no device loss, nothing logged by the new path. That's the same speed as without the switch (8.1–9.0), as expected: it only makes the merge possible.
  • No unit test of the new path yet. The emit/host.rs harness would need to pass the slot in user data. That's a good small task if someone wants one.

With this, everything bind sets per draw is root constants, apart from our own SLOT_MEMORY / SLOT_STRIDES / SLOT_FAULTS, which are the same for every draw of a list. Steps 1–3 are open: take one. I'm working on the processor side for a while (see #10 / #612), so I won't start step 1 this week.

AI-assisted (Claude Code).

**@⁠VibeClauding** on GitHub · 2026-11-25 09:44 UTC · [original](https://github.com/fxceless/victory-fork/issues/828#issuecomment-7158742933) Step 0 is done and tested in game: [VibeClauding/victory-fork@pr/root-view-index](https://github.com/VibeClauding/victory-fork/commits/pr/root-view-index) (`6176086` on `9376fd6`, 4 files, +114/−34). - `VICTORY_ROOT_VIEW_INDEX=1`: the translator reads the title's root SRVs/UAVs with `createHandleFromHeap` at an index from a user-data dword (new `RootType::Other` code `USER_VIEW_SLOT`, dword 0 SRV / 1 UAV). The slot gets no table parameter. - `bind` makes the same bounded one-view table as before. `root_uav` / `root_srv_table` now return the slot and set a table only when given a parameter. `bind` writes the slot into the constants. - The switch is in the translation fingerprint (the build script picks it up). - **Measured** (RTX 3070, Series S, my stack): `translate --warm all` gives the same 254 programs that don't translate, with or without the switch. In game, run 74 (pipelines being made) and run 75: **8.2–9.0 fps**, no errors, no device loss, nothing logged by the new path. That's the same speed as without the switch (8.1–9.0), as expected: it only makes the merge possible. - No unit test of the new path yet. The `emit/host.rs` harness would need to pass the slot in user data. That's a good small task if someone wants one. With this, everything `bind` sets per draw is root constants, apart from our own `SLOT_MEMORY` / `SLOT_STRIDES` / `SLOT_FAULTS`, which are the same for every draw of a list. Steps 1–3 are open: take one. I'm working on the processor side for a while (see #10 / #612), so I won't start step 1 this week. AI-assisted (Claude Code).
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/fxceless--victory-fork#828
No description provided.