Skip to content

d3d12, d3d11: occlusion queries keep every draw's count on Apple7 when a pass has many large draws - #463

Merged
dttdrv merged 2 commits into
dttdrv:mainfrom
partprogramming09:a1-visibility-accumulate
Dec 5, 2026
Merged

dttdrv merged 2 commits into
dttdrv:mainfrom
partprogramming09:a1-visibility-accumulate

Conversation

@partprogramming09

@partprogramming09 partprogramming09 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Part of a series that makes Mullion pass its suite on an M1 Pro (Apple7); #7 describes the setup.


What was wrong

On my M1 Pro d3d12_occlusion_wide fails every run, with both shader front ends: 2 of its 5 queries read fewer samples than they drew, and the counts change from run to run. The cause is outside Mullion: a plain Metal program with no Mullion in it shows it. When one render pass has many counting draws over a large target, the counts of its first draws come back as 0: over 2048x2048, a pass of 90 draws is fine and one of 92 or more loses the counts of its first 92 draws; over 1024x1024, 64 draws are fine and 100 or more lose the first 73. Every pass over that size loses them, not only the first. Only the counts are lost, not the drawing: in the same passes, a fragment-shader atomic counts every draw's fragments exactly once (none missing, none twice) and the pass timestamps read fine. Why the stored counts come back as 0 I cannot see from outside. D3D11 counts occlusion the same way and fails the same way: in d3d11_occlusion_wide the 100 triangles over 8192x8192 count about 539 M of their 6711 M samples.

What this PR does

Render passes now add their counts to the slot (MTLVisibilityResultTypeAccumulate) instead of storing them, and with that the same passes lose no count (measured below).

Adding needs each slot to start at 0, and a list can run again with the same slots:

  • VisibilitySlot zeroes a window of slots when it creates one, as it already did when it reused one.
  • At Close, the list zeroes the slots it counted in with blit fills, after the sums and resolves have read them (ZeroVisibility), so the next run of the list starts from 0.
  • Each slot is still counted in by one pass only, so adding gives the same number as storing did.

visibilityResultType exists from macOS 26, so on older macOS the pass stores as before and the bug stays there. The MTL4 path, which is macOS 26 only, sets it too.

D3D11 gets the same change in a second commit: its passes add too, and VisibilityResultReadback zeroes each window of slots it creates (on i386 the window's memory comes from aligned_malloc, which does not zero it). A window is made for one command buffer and never reused, and each slot is counted in by one pass only, as before.

how a slot is counted, before and after

What it changes, measured on the M1 Pro

In the plain Metal program, the same passes with accumulate lose nothing:

plain Metal program, store against accumulate

The occlusion scene of tests/perf/perf_probe.py costs the same within the noise: 0.81 ms of GPU time before, 0.81 to 0.84 ms after, about 0.30 ms of CPU in both.

GPU time of the occlusion scene

How to check

tests/run.py --build build --wine <wine> --family native,9,8,7,native-metal3,8-metal3 --repeat 3 d3d12_occlusion_far d3d12_occlusion_many d3d12_occlusion_passes d3d12_occlusion_replay d3d12_occlusion_wide d3d12_query_lists d3d12_query_memory d3d12_query_reruns d3d12_query_validation d3d12_query_cpu_resolve d3d12_visibility d3d12_predication d3d11_queries d3d11_occlusion_many d3d11_occlusion_wide d3d11_predication

d3d12_occlusion_wide and d3d11_occlusion_wide are the tests that fail without the change. d3d12_occlusion_replay, d3d12_query_reruns and d3d12_query_lists run a list more than once, so they fail if the slots are not zeroed again at Close (checked by leaving ZeroVisibility out).

tests

Evidence: test runs on the M1 Pro, before and after

MacBook Pro M1 Pro (Apple7), macOS 27.2.1, Wine 11.0, Microsoft's d3dcompiler_47 and DXC in the tests' prefix, built with -Dbuildtype=debugoptimized -Db_ndebug=false -Denable_tests=true; each run a fresh process. Commit d5a9d624: 5 files changed, 47 insertions(+), 2 deletions(-). Before and after were measured on main a44d3c5c on 2026-12-04; the commit was then rebased onto main ff7498a7 (no conflicts) and the family runs repeated there on 2026-12-05.

Test Before (main) After
d3d12_occlusion_wide, dxil and dxbc, native failed: 2 wrong results, a different count each run (×3) passed (×3)
the occlusion, query and visibility tests above, native d3d12_occlusion_wide fails (the rest not run) 19 passed (×3)
occlusion_replay, query_reruns, query_lists with ZeroVisibility left out — failed (dxil and dxbc)
all the tests above, families native, 9, 8, 7, native-metal3, 8-metal3 not run 138 passed, 0 failed (×3) on a44d3c5c; 150 passed, 0 failed (×3) on ff7498a7, which adds d3d12_query_cpu_resolve
d3d11_occlusion_wide, native failed: about 539 M of 6711 M samples (×3) passed (×3)
d3d11_queries, d3d11_occlusion_many, d3d11_occlusion_wide, d3d11_predication, families native, 9, 8, 7, native-metal3, 8-metal3 not run 24 passed, 0 failed (×3)
the same D3D11 tests, native and 7, --validate not run 8 passed, 0 failed (×3)
the D3D12 tests above, native, --validate d3d12_predication dxil and dxbc time out 21 passed; d3d12_predication dxil and dxbc time out

Speed: tests/perf/perf_probe.py run --scenes occlusion --repeat 5 --keep median, builds alternated, GPU ms of each run: before 0.811 and 0.814, after 0.805, 0.838 and 0.833. D3D11, the occlusion scene of d3d11_perf_probe (500 queries a frame), same settings, frame time: before 0.203 and 0.212 ms, after 0.201, 0.199 and 0.196 ms; its GPU timer read 0.000 ms in some runs with and without the change, so it is left out.

The plain Metal program, 3 runs of each case, each case in its own process (counts that read wrong):

Case Store (as main) Accumulate
141 draws in one pass, 2048x2048 92 0
300 draws in passes of 100, 2048x2048 276 0
141 draws in one pass, 2048x2048, with a depth target 92 0
141 draws in one pass, 1024x1024 73 0
1000 draws in one pass, 1024x1024 939, 939, 722 0

What was not run

  • macOS before 26: the change does nothing there, and the counts are still lost on Apple7.
  • Apple8 and Apple9 hardware: the families are emulated on the M1 Pro with --family. I left the change on for every family because adding gives the same result wherever a pass keeps its stored counts.
  • Under --validate, d3d12_predication times out with and without this change, so it could not be checked under validation.

…s has many large draws

On an M1 Pro (Apple7), a render pass with many counting draws over a large
target loses the counts of its first draws: they come back as 0. A plain
Metal program shows it too, so it is outside Mullion.

Render passes now add their counts to the visibility buffer
(MTLVisibilityResultTypeAccumulate, macOS 26 and later) instead of storing
them, so a count stored again as 0 is kept. Adding needs each slot to start
at 0: VisibilitySlot zeroes a new window, and Close zeroes the slots the
list counted in with blit fills after the sums and resolves read them.
Each slot is still counted in by one pass only, so adding gives the same
number as storing did. Older macOS stores as before; D3D11 is untouched.
@partprogramming09
partprogramming09 marked this pull request as ready for review December 5, 2026 07:42
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T07:46:16.165395Z d5a9d62 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

…s has many large draws

D3D11 counts occlusion like D3D12 and loses counts the same way on an M1 Pro
(Apple7): in d3d11_occlusion_wide the 100 triangles over 8192x8192 count
about 8% of their samples.

Render passes now add their counts to the visibility buffer (macOS 26 and
later), as D3D12 passes do. A VisibilityResultReadback window is made for
one command buffer and never reused; it is now zeroed when created, since on
i386 its memory comes from aligned_malloc. Each segment is still counted in
by one pass only, so adding gives the same number as storing did.
@partprogramming09 partprogramming09 changed the title d3d12: occlusion queries keep every draw's count on Apple7 when a pass has many large draws d3d12, d3d11: occlusion queries keep every draw's count on Apple7 when a pass has many large draws Dec 5, 2026
@dttdrv
dttdrv merged commit d53aeb3 into dttdrv:main Dec 5, 2026
19 checks passed
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