Skip to content

Compute dispatches can be ordered by the memory they touch (DM_NVN_DISPATCH_DEPS=1, opt-in): Fire Emblem Fortune's Weave's 345 dispatches take 0.9 ms of GPU instead of 4.2 - #1076

Closed
parsifal295 wants to merge 3 commits into
seedds:mainfrom
parsifal295:dispatch-deps

Conversation

@parsifal295

Copy link
Copy Markdown
Contributor

What and why

DM_NVN_GPUPROF on Fire Emblem Fortune's Weave's battle map shows one compute encoder with 345 dispatches taking 4.2 ms of GPU a frame. The dispatches are cube map faces and mip levels, then about 285 small particle dispatches. With DM_NVN_BLIT_ORDER=0 (the diagnostic that drops barriers) the same encoder takes 0.9 ms. The time goes to dispatch_enc, which records every dispatch as having written all memory, so each dispatch waits for everything before it. That works out to about 9.7 µs per barrier on an M1 Pro.

With DM_NVN_DISPATCH_DEPS=1, a translated shader's dispatch is ordered by the memory its bindings touch, the same way blit_buf already orders copies:

  • Buffers: storage and uniform buffers by their bound ranges.
  • Textures and images: by their storage in guest memory. For a single-level, single-layer view of a block-linear texture the host keeps in a texture of its own, that is one level of one layer; otherwise the whole texture.
  • Indirect arguments.

A dispatch takes a barrier only when these ranges meet what the encoder's operations wrote since the last barrier, or, for its writes, what they read.

Some bindings cannot be bounded: global memory, bindless or pool handles, buffer textures, a storage buffer in a sparse pool, or more than 48 ranges. Those dispatches keep the old rule, as do host kernels. With the variable unset, the code path is the same as before. The only change there is that the copy arrays grow to 256 entries, and copies still take a barrier at 64.

This is opt-in on purpose. It relies on the reflection naming every byte a shader touches, and I have checked only one game. If it looks right to you, it could become the default after regress.py on more games.

The reference entry is Compute dispatches ordered by the memory they touch (opt-in). The switch is in docs/env_switches.tsv.

Checked

  • Tests: make -C dm/gfx/tests GAME=04007EF01F8AE000 run passes with and without DM_NVN_DISPATCH_DEPS=1. test_docs and test_env_switches pass, and docs_symptoms.py was run.

  • Game: Fire Emblem Fortune's Weave (04007EF01F8AE000, v131072), battle map save, headless, docked, DM_NVN_GPUPROF=1500. Same binary, with and without the variable:

    Without With
    Compute encoder (345 dispatches) 4,185 µs 931 µs
    Render, per frame 17.9 ms 12.0 ms
    GPU span, first start to last end 17.0 ms 14.6 ms
    Frame time p50 16.6-16.7 ms 16.6-16.7 ms
    • Frame time is the same because the machine was quiet, so both runs sat at the 60 Hz cap. Under load the old rule gave 17.3 ms.
    • The log's counter: 1,144,535 of 1,179,648 dispatches ran without a barrier, and 3,315 kept the old rule.
    • Screenshots of the same three frames differ only where fog and effects animate (0.4-3 % of pixels by more than 32 levels, the same as between two runs of one build).
  • Not checked:

    • Other games, in particular the dispatch-heavy ones (Hyrule Warriors: Age of Calamity's battles, the SMBW fountain) and games with GMEM compute. Those keep the old rule for any binding the host cannot bound, but they are the ones to try.
    • Scenes in this game with heavy spell effects.
  • Measured on: main 4129936's build. The PR branch is main aa9fa76 plus this change, built and tested; the chain has changed, so there is no game build at that revision here.

  • Devices and OS: MacBook Pro M1 Pro, macOS 27.2, Xcode 27.2.

  • regress.py: not run (it needs every local game rebuilt at main's chain). impact lists every NVN game with compute.

Docs

  • The finding is in its game note or its docs/reference/<subsystem>.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • A chain tool changed: not applicable
  • No game files, keys or data extracted from a game in the change or the attachments

@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 3, 2026
@dpadr

dpadr commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

the -[NvPkg heap] crash on DK. The cause is that dr_tex treats what tex_get returns as a texture. For a texture from the game’s files it returns the NvPkg holder, which the rest of the host unwraps first; I named the one-line fix.

@parsifal295

Copy link
Copy Markdown
Contributor Author

Thanks @dpadr, that's exactly it: dr_tex asked tex_get's result for its heap without unwrapping the NvPkg that holds a texture from the game's files. Fixed in ebad9bf: it unwraps NvPkg to its tex first, as tex_mtl does. The gfx suite passes with DM_NVN_DISPATCH_DEPS=1. I couldn't rerun Donkey Kong Bananza here, so a check of that scene on your side would be welcome.

…SPATCH_DEPS=1): Fire Emblem Fortune's Weave's 345 dispatches a frame take 0.9 ms of GPU instead of 4.2
…les) is unwrapped before its Metal texture is asked for its heap (Donkey Kong Bananza crashed in -[NvPkg heap])
…ernel main added since, takes dispatch_enc's host-kernel path (NULL, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants