Skip to content

A NaN colour at alpha 0 is returned as 0, as NVIDIA's blender takes a zero factor: Ocarina of Time's white clouds over the ferns are gone - #2030

Closed
humanperzeus wants to merge 1 commit into
seedds:mainfrom
humanperzeus:q190-blendzero-pr

Conversation

@humanperzeus

@humanperzeus humanperzeus commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Fixes #1839 (the same white clouds, reported on the way between Kokiri Forest and Hyrule Field, same shader 5ad65c0eefd09c7f).
Symptom. The Legend of Zelda: Ocarina of Time, Kokiri Forest: with the camera low among the ferns, white clouds bloom over the near ferns and come and go as they sway. Dropping the fog composite (888c1068ac8f7043) removes them.

Cause. NVIDIA's blender takes a zero factor times anything as 0 (0 × NaN = 0 × Inf = 0, Direct3D's rule), so a pixel shader may write a NaN colour with alpha 0 under SRC_ALPHA / ONE_MINUS_SRC_ALPHA blending and the target keeps its value. Apple's blender multiplies as IEEE does, stores the NaN, and every later pass reading the target spreads it.

Here the half-resolution light shader 5ad65c0eefd09c7f (a mesh-drawn fog volume) averages hit positions over a fog layer whose density is 0 at that spot: a positive hit count with a weight sum of 0, 1/0 × 0. Its FMULs carry no .FMZ, so the translation is exact; the NaN is the game's own arithmetic. It writes the result with alpha 0 into an RGBA16F light target. The fog composite carried the NaN into the R11G11B10F scene and bloom turned each NaN pixel into a white blot. The ferns sway across the fog volume's surface, so those pixels pass and fail its depth test in turn, hence the coming and going.

Change. dmsh_rewrite.py blend_zero, called from dmsh_build.py after the entry points that share main0's body: main0, the entry point a pipeline with float colour targets takes, returns a non-finite rgb as 0 where alpha is exactly 0 (dm_az before each return out;). A finite output is never changed; the integer entry points (main0_u / main0_m, which call dm_body_main0) are left as they are, their outputs being bits. Outside SRC_ALPHA blending the rule only touches outputs that were NaN or Inf already.

The guard has to reach the text the library is compiled from:

  • dmsh_side.unit keeps a main0 that calls the shared body as it is.
  • main0's alpha test and logic operation versions, and main0_f (dmsh_fetch.fold), call the body themselves, so they take the same guard on their result (dmsh_side.az_cols / az_after).
  • A library built without its extra entry points gets the guard in its plain source too (without_extras).

The branch is rebuilt on 80fc2ec, which moved the MSL rewrites into dmsh_rewrite.py, as one commit.

Reference entry: A NaN colour at alpha 0 under SRC_ALPHA blending; a line under the game note's "Found on this game".

How it was found, in short: per-pass raw floats of one frame (DM_NVN_TEXSNAP + DM_NVN_SNAPRAW) put the first NaN in the light pass (0 before it, about 18,000 pixels after); that one shader, instrumented and loaded from a folder beside the library (DM_MSL_DIR), put the first NaN at 1/w × v with w = 0; the pass's blend state and the alpha it stores (1 = 1 − srcAlpha, so srcAlpha 0) gave the cause. Ruled out on the way: the fog volumes (finite, increasing slice by slice), FMZ handling (none in the code), stale uniforms (DM_NVN_UB_DEFER=0; DM_NVN_UBP_CHECK: 14,000 shadows equal to GPU memory), the depth test (every shaded pixel legitimately in front of the surface).

Checked

  • Tests: python3 dm/gfx/tests/test_dmsh_blend_zero.py (12, new: every return of main0 guarded, the shared body and the integer entry points untouched, integer outputs never touched, a second pass a no-op; through dmsh_side.unit and dmsh_fetch.fold, every float entry point of a shared and of a plain shader reaches the guard and the integer ones do not; without_extras; each text compiled by Metal's compiler. The 3 about the shared case fail against the first version of this PR, 3 others with blend_zero disabled), test_dmsh_side.py (6), test_dmsh_fetch.py (23), test_dmsh_meshstage.py (34), test_discard.py (4), test_dmsh_abi.py (11), test_dmsh_code_fp.py (8), python3 -m unittest dm/tools/tests/test_docs.py (6), python3 -m unittest dm/tools/tests/test_nsp_chain.py (17).
  • The guard in the compiled text, over all of Ocarina of Time's 22,352 pixel shaders (blend_zero, unit, fold on a local build's sources): every float entry point reaches it in all 22,296 that have a float colour output; no integer entry point is touched. The first version of this PR reached 13,031: the 9,265 shared-body shaders lost it in unit (found in review). The unit and main0_f text of 300 shared-body shaders compiles with Metal's compiler. A full Mac build (42,848 shaders) with this change on 641636e compiles; at the spot, the same recorded route shows clouds in 4 of 12 frames with the guard taken out of the two shaders and in 0 of 12 with it, the fog equally bright.
  • Games run, with title ID and version: The Legend of Zelda: Ocarina of Time 0400486024DEC000 v131072, a full Mac build (42,848 shaders) with this change, on a base before the latest 19 commits of main; the PR branch is this change alone on current main. A recorded camera route to the spot (DM_KEYS replay, LoadEnd-synced, 12 shots 0.7 s apart) before and after: frames with a cloud 8 of 12 → 0 of 12; mean picture brightness at the spot 21–25 after, 21–24 in the clean frames before (about 12 with the fog dropped), so the fog is kept; 0 command buffer errors. With the rule's condition marked in the new build (hit count > 0, weight sum 0), it still occurs at 3,056 and 4,913 pixels in two frames, and the light target and the scene hold 0 non-finite values.
  • Devices and OS: Apple M1 Max, macOS 27.2.
  • scripts/regress.py: impact reaches every NVN game through the chain (broad: Breath of the Wild, Pokémon Scarlet, Tears of the Kingdom; Hades not affected). check --games was not run: only Ocarina of Time is set up on this Mac, and each other game needs a full shader build of the new chain. By construction the rule changes no finite output.
  • Not checked: the console's blender behaviour itself; it is inferred from Direct3D's rule and from every other input being equal (the shader math, its constants, the depth test). The Python lint ratchet was not run locally (ruff and pyright not installed here).

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: dmsh_chain.py --header was run and dm/gfx/nvn/dmsh_chain.h is part of the change
  • No game files, keys or data extracted from a game in the change or the attachments

🤖 Generated with Claude Code

@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders game: 0400486024DEC000 A game labels Nov 9, 2026
Know-It-All-Brother added a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
… zero factor (humanperzeus's PR seedds#2030): Ocarina of Time's white clouds over the ferns (PR seedds#2030), with this branch's translator rebuilt from its own source; chain 43821b3e4cba
AmateurWizrd added a commit to AmateurWizrd/nxnative that referenced this pull request Nov 9, 2026
…ers (chain 402b1a4404e1)

blend_zero puts the dm_az guard into main0. For a shader whose main0 already calls
dm_body_main0 (it has main0_u, main0_m or main0_d), dmsh_side.unit wrote main0 again as a
plain `return dm_body_main0(...)`, and ps_libraries compiled that text, so the guard was
gone: 9,265 of Ocarina of Time's 22,352 pixel shaders. unit now keeps such a main0 as it is.

Found by a review of play before the build (astra-code-reviewer). The defect is in seedds#2030
as written upstream, not in the merge.
betogzo pushed a commit to betogzo/nxnative that referenced this pull request Nov 10, 2026
… zero factor: Ocarina of Time's white clouds over the ferns at the camera are gone (chain 4d8f4364df2b)

NVIDIA's blender takes a zero factor times anything as 0 (0 x NaN = 0), so a pixel shader may write a
NaN colour with alpha 0 under SRC_ALPHA / ONE_MINUS_SRC_ALPHA blending and the target keeps its value.
Apple's blender multiplies as IEEE does and stores the NaN; every later pass that reads the target
spreads it.

Ocarina of Time's half-resolution light shader 5ad65c0eefd09c7f averages hit positions over a fog layer
whose density is 0 at the spot (a positive hit count, a weight sum of 0: 1/0 x 0 = NaN; its FMULs carry
no .FMZ, so the translation is exact) and writes them with alpha 0. The fog composite 888c1068ac8f7043
carried the NaN into the scene and bloom turned it into white clouds over the ferns at the camera,
coming and going as they swayed across the fog volume's surface.

dmsh_build.py blend_zero: main0, the entry point a pipeline with float colour targets takes, returns a
non-finite rgb as 0 where alpha is exactly 0. A finite output is never changed, nor are the integer
entry points (main0_u / main0_m), whose outputs are bits.

(cherry picked from seedds/nxnative PR seedds#2030, commit 7d86e5e; issue seedds#1839)
betogzo added a commit to betogzo/nxnative that referenced this pull request Nov 10, 2026
seedds#2030, seedds#2077, seedds#2022, seedds#2075, seedds#2085), last merged at 04811a6, and the Castle Town result on a MacBook Air M4

Castle Town on a 16 GB MacBook Air M4 at 30 fps and full resolution: GPU p50 26-27 ms a frame and 6.9 GB of GPU memory allocated with seedds#2085, against 16.2 GB and about 9 fps before it.

Tests: dm/tools/tests/test_docs.py passes.
FeReuss pushed a commit to FeReuss/nxnative-oot that referenced this pull request Nov 10, 2026
…0 is returned as 0 (Ocarina of Time's white clouds over the ferns)

Conflict with our border-fc branch resolved by hand in dmsh_build.py: border_emu keeps our BORDER_LIVE_FC return line, the PR's BLEND_ZERO / blend_zero block follows it; README keeps both entries; dmsh_chain.h regenerated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
FeReuss pushed a commit to FeReuss/nxnative-oot that referenced this pull request Nov 10, 2026
…s (PR seedds#1833) with the M4 fix, on the synced main

The branch is PR seedds#1833 (shader_raw_direct, on for Ocarina of Time) plus one commit that lets raw stores and atomics reach pool buffers above 2^40, which an M4's addresses are.

Conflicts resolved by hand:
- GlobalToStorage.cs: the PR's structure (TryGenerateRawDirectOp, EmitRawAccess) with main's two changes to the lookup path kept: the 'a miss reads 0 in every word of a vector load' block in TryGenerateRawStorageOp's tail (the direct op has no miss path for loads: the GPU reads 0 at an unmapped address) and the GlobalVectorGroup guard of the follower scan.
- nvn_mtl.m (mtl_pool_del): main collects the retired chunks in 'gone' and releases them behind the fence; the PR's whole-pool address buffer (va) joins that batch instead of a separate defer_obj_release.
- The translator is rebuilt from the merged sources (dotnet publish; dmsh_xlate.dll, Ryujinx.Graphics.Shader.dll, Ryujinx.Common.dll, Spv.Generator.dll and the pdb replaced, the universal launcher and the other files kept), dmsh_chain.lock updated with tool_lock.py --update-lock (only the four translator fingerprints change), dmsh_chain.h regenerated. tool_lock.py --check-tools passes.
- Docs: game-patches.md, symptoms.md and env_switches.tsv regenerated.

Checked: test_dmsh_raw_direct, test_xlate_anycpu, test_nsp_chain, the translator tests for border-fc, float-atomic-add, seedds#2081, seedds#2030 and layer_out, docs tests; the host builds for Ocarina of Time. Not run: a game.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@AmateurWizrd

Copy link
Copy Markdown
Contributor

On shaders whose main0 shares its body (those that get main0_u, main0_m or main0_d), the dm_az guard does not reach the compiled library. blend_zero adds it to main0, then dmsh_side.unit (dmsh_side.py:103) writes every entry point again as a plain return dm_body_<name>(...), and ps_libraries compiles that text. In Ocarina of Time that is 9,265 of 22,352 pixel shaders. The fern shader 5ad65c0eefd09c7f has no shared body and keeps the guard, so the test added here passes.

This change in unit keeps a shared main0 as it is, with what blend_zero did to its result:

plain = [msl[e['start']:e['end']] + '\n' if shared and e['name'] == 'main0'
         else f'{e["attr"]}fragment {e["ret"]} {e["name"]}({", ".join(e["params"])})\n{{\n    return dm_body_{e["name"]}({e["args"]});\n}}\n' for e in es]

A test that runs blend_zero on a shared-body shader, passes it through unit and looks for dm_az( in main0 fails without it. With it, a full Ocarina of Time shader build compiles all 42,848 shaders.

A smaller one: the fallback at dmsh_build.py:1757 writes plain, the source from before blend_zero, so a shader that only builds without its extra entry points loses the guard too.

humanperzeus added a commit to humanperzeus/nxnative that referenced this pull request Nov 10, 2026
…red body too: all 22,296 of Ocarina of Time's pixel shaders with a float colour output (chain 0f5a5ce70b65)

Review of seedds#2030: in a pixel shader whose main0 calls the shared body (main0_u / main0_m / main0_d beside it), blend_zero put the
guard in main0, and dmsh_side.unit then wrote main0 again as a plain `return dm_body_main0(...)`, which ps_libraries compiles.
Ocarina of Time: 9,265 of its 22,352 pixel shaders lost the guard that way.

- dmsh_side.unit keeps a shared main0 as it is, with what blend_zero put after its call of the body (the reviewer's change).
- The entry points that call the body themselves take the same guard on their result: main0's alpha test and logic operation
  versions (unit) and main0_f (dmsh_fetch.fold). dmsh_side.az_cols reads the guarded colours from main0, az_after adds them.
  In a plain shader the guard is in the body, so nothing is added there; the integer entry points stay untouched.
- A library that does not build with its extra entry points is built from the plain source with the guard too
  (dmsh_build.py without_extras; it wrote the source from before blend_zero).

Tests: test_dmsh_blend_zero.py 12 (4 new: unit on a shared and on a plain shader, fold's main0_f, without_extras); the 3 new
ones about the shared case fail against the old dmsh_side.py / dmsh_fetch.py / dmsh_build.py. test_dmsh_side 6, test_dmsh_fetch 23.
Over Ocarina of Time's real 22,352 pixel shaders (blend_zero, unit, fold, on a local build's sources): every float entry point
reaches the guard in all 22,296 that have a float colour output (before: 13,031), no integer entry point is touched; the unit
and main0_f text of 300 shared-body shaders compiles with Metal's compiler.
@humanperzeus

Copy link
Copy Markdown
Contributor Author

Thank you, both points were right, and I've pushed a fix (a548c50; the branch is rebased onto 641636e).

What changed

  • dmsh_side.unit keeps a shared main0 as it is, as you suggested.

  • Entry points that call the body themselves. Your change keeps the guard in main0. Three other float entry points of a shared-body shader call dm_body_main0 directly, so they skipped it:

    • main0_a (alpha test);
    • main0_l (logic operation);
    • main0_f (from dmsh_fetch.fold).

    They now apply the same guard to their own result. dmsh_side.az_cols reads the guarded colours from main0, and az_after adds them. In a plain shader the guard is already in the body, so nothing is added there.

  • The fallback writes the plain source through blend_zero (without_extras), so a shader that only builds without its extra entry points keeps the guard.

  • The integer entry points stay untouched.

Checked

  • test_dmsh_blend_zero.py: 12 tests, 4 of them new.
    • New: unit on a shared shader and on a plain one; fold's main0_f; without_extras.
    • The 3 new tests about the shared case fail against the previous dmsh_side.py, dmsh_fetch.py and dmsh_build.py.
    • test_dmsh_side (6) and test_dmsh_fetch (23) also pass.
  • Over all 22,352 of Ocarina of Time's pixel shaders. Each was run through blend_zero, unit and fold, using a local build's sources.
    • 22,296 have a float colour output. In all of them, every float entry point now reaches the guard. Before, 13,031 did, and the other 9,265 were exactly the shared-body ones you counted.
    • No integer entry point is touched.
  • Compile: the unit and main0_f text of 300 shared-body shaders compiles with Metal's compiler.

Not yet run: a full Ocarina build on this branch. I'll add the result here once it has run.

Fasen24-AI added a commit to Fasen24-AI/swordboiMacNative that referenced this pull request Nov 10, 2026
seedds#1901 seedds#1459 seedds#1928 seedds#1942 seedds#1940 seedds#1952 seedds#1611 seedds#2065 seedds#2083 seedds#2008 seedds#2079 seedds#2022 seedds#2075 seedds#2077 seedds#2020 seedds#2021 onto main 04811a6

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014eD437mxrMnW2UwLZhpwc7

Upstream pull requests applied in this commit (their authors' work, brought onto this tree with a three-way merge; the description is in each PR):
- seedds#1459 by PromethOps (open): DM_UPSCALER=metalfx takes agl's agl_ndsr_vr.pck networks too, and a variant with no curve and no power is drawn as one colour space: Ocarina of Time's upscaler is MetalFX's
- seedds#1611 by Esteban-V (open): An argument block whose only change is a uniform buffer is patched instead of encoded again
- seedds#1774 by uncraftablexd (closed): Compute threads that exit before a barrier keep arriving at it, and every shader loop has a cap (GPU hang defences), chain change
- seedds#1824 by cocoman93 (open): Clamp texel fetches past a sampled 2D texture's edge: no hard edge in Ocarina of Time's mist
- seedds#1901 by FifteenPen (open): The game's compute dispatches with no game barrier between them run side by side (profile key dispatch_overlap, on for Ocarina of Time): Kokiri Forest GPU p50 15.63 → 14.76 ms
- seedds#1928 by FeReuss (open): Ocarina of Time: enc_thread on, the replay encodes on a thread of its own
- seedds#1929 by FeReuss (open): Unused border corrections are compiled out of pipelines that bind no sampler with a twin (function constant DMSH_BORDER_FC)
- seedds#1930 by FeReuss (open): A float add to storage memory with no result read is one native atomic add instead of a compare-and-swap loop
- seedds#1940 by uncraftablexd (open): A prewarm list's lines after its #@after_first_frame line are built after the first frame, in the background, instead of in the prepare step (issue seedds#1747)
- seedds#1942 by uncraftablexd (open): Frame step: late frames with the GPU idle do not step up, and a longer hold the GPU does not explain is probed
- seedds#1952 by AdyaGMD (open): Compute moves ahead of scene passes past occlusion queries and conditional rendering; clears wait across dispatches
- seedds#2008 by bquilty25 (open): nvn: reactive memory watchdog, 16-way view cache, and texture upload batching
- seedds#2020 by padsiwono (open): Ocarina of Time v131072: Output Native draws the full output size instead of the game's 80 % frame (`DM_OUTPUT`)
- seedds#2021 by padsiwono (open): Ocarina of Time v131072 renders at the launcher's Resolution (`DM_RENDER_SIZE`), docked
- seedds#2022 by sleepyeyedjoe (closed): Ocarina of Time: large models no longer go black for one frame twice, 4–5 frames apart, when the game streams new textures into them (a texture ID a shader has read keeps its read mark when it gets another texture)
- seedds#2030 by humanperzeus (open): A NaN colour at alpha 0 is returned as 0, as NVIDIA's blender takes a zero factor: Ocarina of Time's white clouds over the ferns are gone
- seedds#2035 by Know-It-All-Brother (open): The shader build runs as worker processes, and no shader waits for a batch (Ocarina of Time's cold Mac map: 3,002 s to 1,298 s)
- seedds#2065 by Raymon82777 (closed): A vertex library with layer entry points of its own gets bases for its main0 pairs: Ocarina of Time's 230 ms camera-turn hitch in Hyrule Field goes to 39 ms
- seedds#2075 by sleepyeyedjoe (closed): Ocarina of Time: a patch of scenery no longer draws dark for one frame when the game gives it a sharper texture (a pool texture the walk does not make holds a stand-in of its own colours until its first read)
- seedds#2077 by roandegraaf (closed): Ocarina of Time: fix the "plaid" terrain / missing texture tiles (GPU-drawn textures now reach the storage the game copies)
- seedds#2079 by uncraftablexd (open): Render pipelines that share a vertex function are taken from the archive or compiled one at a time: the AGX driver crashes in createVertexProgramVariant's completion block
- seedds#2083 by FifteenPen (closed): Ocarina of Time's prewarm list gets 41 more task / mesh pipelines from later play
… zero factor: Ocarina of Time's white clouds over the ferns at the camera are gone (chain ee91be81885f)

NVIDIA's blender takes a zero factor times anything as 0 (0 x NaN = 0 x Inf = 0, Direct3D's rule), so a pixel shader may write a
NaN colour with alpha 0 under SRC_ALPHA / ONE_MINUS_SRC_ALPHA blending and the target keeps its value. Apple's blender multiplies
as IEEE does, stores the NaN, and every later pass that reads the target spreads it. Ocarina of Time's half-resolution light
shader 5ad65c0eefd09c7f averages hit positions over a fog layer whose density is 0 there (1/0 x 0); the fog pass carried the NaN
into the scene and bloom made white clouds over the ferns at the camera.

dmsh_rewrite.py blend_zero, called from dmsh_build.py after the entry points that share main0's body: main0 returns a non-finite
rgb as 0 where alpha is exactly 0 (dm_az before each `return out;`). A finite output is never changed, the integer entry points
(main0_u / main0_m, whose outputs are bits) are left as they are. The guard reaches the text the library is compiled from:
dmsh_side.unit keeps a main0 that calls the shared body as it is, and main0's alpha test and logic operation versions and main0_f
(dmsh_fetch.fold), which call the body themselves, take the same guard on their result (dmsh_side.az_cols / az_after). A library
built without its extra entry points gets the guard in its plain source too (without_extras).

Rebuilt on 80fc2ec, which moved the MSL rewrites into dmsh_rewrite.py; the first version of this PR (review: the guard missed
shared-body shaders in unit) is folded in.

Tests: test_dmsh_blend_zero.py 12 (the 3 about the shared case fail against the first version), test_dmsh_side 6,
test_dmsh_fetch 23, test_dmsh_meshstage 34, test_discard 4, test_dmsh_abi 11, test_dmsh_code_fp 8, test_docs 6,
test_nsp_chain 17. Over Ocarina of Time's 22,352 pixel shaders (blend_zero, unit, fold on a full local build's sources): every
float entry point reaches the guard in all 22,296 with a float colour output, no integer entry point touched. A full Mac build
(42,848 shaders) with this change on 641636e compiles; at the spot, a recorded camera route shows clouds in 4 of 12 frames
with the guard taken out of the two shaders and in 0 of 12 with it, the fog equally bright.
@humanperzeus

Copy link
Copy Markdown
Contributor Author

The branch had conflicts with main after 80fc2ec, which moved the shader build's MSL rewrites into dmsh_rewrite.py. I rebuilt it on current main (c4ffb9d) as one commit, f67642d.

  • blend_zero and without_extras now live in dmsh_rewrite.py beside frag_z_snap; dmsh_build.py calls them module-qualified.
  • The test imports dmsh_rewrite instead of copying the functions out by AST.
  • The review fix from a548c50 is unchanged in substance.

Checked again on the rebuilt branch:

  • test_dmsh_blend_zero 12, test_dmsh_side 6, test_dmsh_fetch 23, test_dmsh_meshstage 34, test_discard 4, test_dmsh_abi 11, test_dmsh_code_fp 8, test_docs 6, test_nsp_chain 17.
  • The count over Ocarina of Time's pixel shaders is unchanged: the guard reaches every float entry point in all 22,296 that have a float colour output.
  • A full Mac build of the change on 641636e compiles all 42,848 shaders.
  • At the spot, the recorded route shows clouds in 4 of 12 frames with the guard taken out of the two shaders, and in 0 of 12 with it. Before and after pictures follow in the next comment.

betogzo added a commit to betogzo/nxnative that referenced this pull request Nov 10, 2026
 and seedds#2085 in upstream's versions, the host at -O2, Ocarina of Time's camera-turn hitch in Hyrule Field (230 → 39 ms), 41 more task / mesh pipelines and 4,959 render / compute pipelines in its prewarm list, GPU writes into uploaded textures kept on the GPU, compact NVN recordings; chain 8f2d8a09c3f9

PR seedds#2030 (blend_zero) stays the fork's own: upstream moved the shader build's MSL rewrites into dmsh_rewrite.py (80fc2ec), so blend_zero and BLEND_ZERO move there, dmsh_build.py finish calls dmsh_rewrite.blend_zero, and test_dmsh_blend_zero.py imports it. dmsh_chain.h regenerated (8f2d8a09c3f9).

Tests: test_dmsh_*.py 26/26 OK (test_dmsh_pairs with DM_GAME=0400486024DEC000); t_pool_chunks 151/0, t_cpu_storage 135/0, t_readback 218/0, t_pool_seen_replace 44/0, t_pool_standin 31/0, t_pool_notes 81/0, t_mesh_first_use 53/0.
@reefbluesky

Copy link
Copy Markdown
Contributor

A second-machine check on an M2 Pro (macOS 27.2): main ab41d6a1 plus this commit (f67642dc, applied without conflict), plus local commits that do not touch drawing, with main's uniform bound 26062f9e on. At the Kokiri Forest exit into Hyrule Field, where the white clouds showed before, the mist is there and the clouds are gone in play.

DM_NVN_MAGSCAN=888c1068ac8f7043:10:1000000000 over 494 s: 0 non-finite pixels in all 1,022 measurements. Without the commit, the same spot had non-finite pixels in all 348 measurements of the pass after the fog composite (14,746 to 326,952 of 1,327,104).

Screenshot and numbers in #2187: #2187 (comment)

betogzo added a commit to betogzo/nxnative that referenced this pull request Nov 10, 2026
…h_rewrite.py in 10298c1): the pull request's rebuilt version f67642d follows, with its review fix

The first version's guard missed every pixel shader whose main0 shares its body with other entry points (dmsh_side.unit wrote main0 again as a plain call of the body): 9,265 of Ocarina of Time's 22,352 pixel shaders. The fern shader 5ad65c0eefd09c7f has no shared body, so the clouds it caused were gone either way.
betogzo pushed a commit to betogzo/nxnative that referenced this pull request Nov 10, 2026
… zero factor: Ocarina of Time's white clouds over the ferns at the camera are gone (chain 637e72cf3b20)

NVIDIA's blender takes a zero factor times anything as 0 (0 x NaN = 0 x Inf = 0, Direct3D's rule), so a pixel shader may write a
NaN colour with alpha 0 under SRC_ALPHA / ONE_MINUS_SRC_ALPHA blending and the target keeps its value. Apple's blender multiplies
as IEEE does, stores the NaN, and every later pass that reads the target spreads it. Ocarina of Time's half-resolution light
shader 5ad65c0eefd09c7f averages hit positions over a fog layer whose density is 0 there (1/0 x 0); the fog pass carried the NaN
into the scene and bloom made white clouds over the ferns at the camera.

dmsh_rewrite.py blend_zero, called from dmsh_build.py after the entry points that share main0's body: main0 returns a non-finite
rgb as 0 where alpha is exactly 0 (dm_az before each `return out;`). A finite output is never changed, the integer entry points
(main0_u / main0_m, whose outputs are bits) are left as they are. The guard reaches the text the library is compiled from:
dmsh_side.unit keeps a main0 that calls the shared body as it is, and main0's alpha test and logic operation versions and main0_f
(dmsh_fetch.fold), which call the body themselves, take the same guard on their result (dmsh_side.az_cols / az_after). A library
built without its extra entry points gets the guard in its plain source too (without_extras).

Rebuilt on 80fc2ec, which moved the MSL rewrites into dmsh_rewrite.py; the first version of this PR (review: the guard missed
shared-body shaders in unit) is folded in.

Tests: test_dmsh_blend_zero.py 12 (the 3 about the shared case fail against the first version), test_dmsh_side 6,
test_dmsh_fetch 23, test_dmsh_meshstage 34, test_discard 4, test_dmsh_abi 11, test_dmsh_code_fp 8, test_docs 6,
test_nsp_chain 17. Over Ocarina of Time's 22,352 pixel shaders (blend_zero, unit, fold on a full local build's sources): every
float entry point reaches the guard in all 22,296 with a float colour output, no integer entry point touched. A full Mac build
(42,848 shaders) with this change on 641636e compiles; at the spot, a recorded camera route shows clouds in 4 of 12 frames
with the guard taken out of the two shaders and in 0 of 12 with it, the fog equally bright.

(cherry picked from seedds/nxnative PR seedds#2030, commit f67642d, rebuilt with the review fix a548c50; issue seedds#1839)
betogzo added a commit to betogzo/nxnative that referenced this pull request Nov 10, 2026
seedds added a commit that referenced this pull request Nov 10, 2026
…g, as on the console: NVN's driver sets the float blender's "zero times anything is zero" option (the command word for SET_BLEND_FLOAT_OPTION is in the sdk module), while Apple's blender stores NaN for 0 x NaN and 0 x Inf. Every pixel shader with a float4 colour output ends in a guard that reads a word from the host (fragment buffer 28, two bits per colour output), and the host sets an output's bits only where its state can have that weight: blending on, a floating-point target, a sum or difference, a source colour factor of SRC_ALPHA (zero at alpha 0) or ONE_MINUS_SRC_ALPHA (zero at alpha 1) and a destination factor that does not read the source colour. Every other draw returns its colour as it is; a factor of ZERO needs nothing, since Apple's blender leaves that term out. Ocarina of Time's mist shader divides by zero at alpha 0 and blends into an RGBA16F target with SRC_ALPHA / ONE_MINUS_SRC_ALPHA, which the bloom spreads into white clouds; they did not show on the Mac route run here, with the rule on or off, so the game itself awaits a retest. t_blend_zero draws NaN, +Inf, -Inf and 2 at alpha 0 and 1 under seven blend states and reads the target back, with the rule and with DM_NVN_BLEND_ZERO=0 (Apple's blender as it is). Ocarina of Time's 42,848 shaders build; standing in Kokiri Forest GPU p50 10.86 -> 10.95 ms, within the spread between runs, 60 fps both (Apple M6). Open: NaN already in the target under a zero destination weight, a non-finite weight on a colour of 0, the alpha channel's own weights, the Vulkan and OpenGL paths, and the iOS libraries were not run (PR #2030, issue #1839)

Co-authored-by: Human Khoobsirat <3029935+humanperzeus@users.noreply.github.com>
@seedds

seedds commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thank you: the diagnosis here (the mist shader's NaN at alpha 0, blended with SRC_ALPHA / ONE_MINUS_SRC_ALPHA into the RGBA16F target, where Apple's blender stores 0 x NaN as NaN) is what 0662f9d on main is built on, and you are credited as co-author. The code is a different design, so I am closing this PR in its favour.

What changed against the PR:

  • Gated by blend state. The shader's guard reads a word from the host (two bits per colour output), and the host sets it only for a draw whose state can have a zero source weight: blending on, a floating-point target, a sum or difference, a source colour factor of SRC_ALPHA or ONE_MINUS_SRC_ALPHA, and a destination factor that does not read the source colour. Every other draw returns its colour untouched, so a draw with blending off or a factor of ONE still stores its NaN or Inf, as the console does. The PR's guard zeroed every non-finite colour at alpha 0 in every float pixel shader, whatever the state.
  • ONE_MINUS_SRC_ALPHA at alpha 1 is covered too (the same zero weight).
  • The rule's source. NVN's driver sets the float blender's "zero times anything is zero" option (the SET_BLEND_FLOAT_OPTION command word is in the sdk module). Direct3D is not the reference: its 11.3 spec asks for NaN to propagate through blending, zero weights included.
  • Measured. A GPU test (t_blend_zero) draws NaN, +Inf, -Inf and 2 at alpha 0 and 1 under seven blend states and reads the target back, with the rule and without. On an M6, Ocarina of Time's GPU time standing in the village is 10.86 ms on main and 10.95 ms with the change, within the spread between runs.

One thing is open: the white clouds did not show on my Mac route (the Kokiri Forest village from a save), with the rule on or off, so the game itself is not confirmed. If you have a save where they show, a retest on main would settle it; DM_NVN_BLEND_ZERO=0 turns the rule off for a comparison.

@reefbluesky

reefbluesky commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Retest on an M2 Pro (macOS 27.2), save in Kokiri Forest, same spots at the forest exit and in Hyrule Field near the tree.

Main 0d84a3f7 (with 0662f9d) plus local commits that do not touch drawing: the white clouds are gone. But the mist is gone too: at the forest exit there is none up close, day or night, and in the field only a faint trace is left near the tree. My build on ab41d6a1 with #2030 shows both places with mist.

What I ruled out:

  • DM_NVN_BLEND_ZERO=0: still no mist and no white clouds, so the mist pass seems to draw nothing now, not a blend rule effect.
  • DM_NVN_GPU_TS=segment_gaps with DM_NVN_REPORT_EXACT=0: no change.
  • A build with 0cad950, 01de04f, adaff73, fd8188c and 25c2546 reverted: no change.

Left between the two builds are the translator changes e45694b and 918e9f3 (and 0662f9d itself). A build with the first two reverted is compiling now; I will post the result. 1d090a6 (bound rounded up to 256 bytes) may be related, I have not tested it yet.

Screenshots, before and after:

1_kokiri_antes_v132 2_kokiri_depois_main 3_campo_antes_v132 4_campo_depois_main

Aznracer pushed a commit to Aznracer/Mighty-Green-Kokiri-Kid that referenced this pull request Nov 11, 2026
…g, as on the console: NVN's driver sets the float blender's "zero times anything is zero" option (the command word for SET_BLEND_FLOAT_OPTION is in the sdk module), while Apple's blender stores NaN for 0 x NaN and 0 x Inf. Every pixel shader with a float4 colour output ends in a guard that reads a word from the host (fragment buffer 28, two bits per colour output), and the host sets an output's bits only where its state can have that weight: blending on, a floating-point target, a sum or difference, a source colour factor of SRC_ALPHA (zero at alpha 0) or ONE_MINUS_SRC_ALPHA (zero at alpha 1) and a destination factor that does not read the source colour. Every other draw returns its colour as it is; a factor of ZERO needs nothing, since Apple's blender leaves that term out. Ocarina of Time's mist shader divides by zero at alpha 0 and blends into an RGBA16F target with SRC_ALPHA / ONE_MINUS_SRC_ALPHA, which the bloom spreads into white clouds; they did not show on the Mac route run here, with the rule on or off, so the game itself awaits a retest. t_blend_zero draws NaN, +Inf, -Inf and 2 at alpha 0 and 1 under seven blend states and reads the target back, with the rule and with DM_NVN_BLEND_ZERO=0 (Apple's blender as it is). Ocarina of Time's 42,848 shaders build; standing in Kokiri Forest GPU p50 10.86 -> 10.95 ms, within the spread between runs, 60 fps both (Apple M6). Open: NaN already in the target under a zero destination weight, a non-finite weight on a colour of 0, the alpha channel's own weights, the Vulkan and OpenGL paths, and the iOS libraries were not run (PR seedds#2030, issue seedds#1839)

Co-authored-by: Human Khoobsirat <3029935+humanperzeus@users.noreply.github.com>
(cherry picked from commit 0662f9d)
UserDerezzed pushed a commit to UserDerezzed/nxnative that referenced this pull request Nov 12, 2026
…its outputs are drawn to: bz_word (0662f9d, PR seedds#2030) maps physical attachment i to its logical output through g_pso_lmap, but draw_ sets g_pso_lmap to NULL before it encodes the fragment arguments (encode_args, which calls bz_bind), so in the later pass of a readable-tile merge (DM_NVN_TBDR=1, off by default), when fz_p2_draw moves an output to another attachment, its bits were taken with the format of the attachment that has the output's number, not the one it is drawn to; the deferred pass merge and same-pass tile reads map each output to itself, so no pixel of theirs changes. A later-pass draw whose output 0 blends SRC_ALPHA / ONE_MINUS_SRC_ALPHA into an RGBA16F attachment 1, behind the earlier pass's RGBA8 attachment 0, got the word 0, so a NaN colour at alpha 0 was stored there as Apple's blender stores it. Found reading the code, not seen in a game. draw_ now copies the eight mapping bytes beside raster_mask, before the reset (raster_mask is taken there for the same reason), and hands them to bz_bind through g_bz_lmap for the fragment encode_args call only; bz_word takes the mapping as an argument, and the word bz_bind bound last is kept in g_bz_last for the test. The host alone: no shader or chain change, and DM_NVN_BLEND_ZERO=0 still gives 0 for every draw. t_blend_zero has a new case drawn through draw() with g_pso_lmap set, as defer_replay passes a mapping in: physical attachment 0 RGBA8 with no output, physical 1 RGBA16F holding output 0; the bound word is 1 with the rule (0 with DM_NVN_BLEND_ZERO=0), the mapping is gone after the draw, and the same draw unmapped binds 0. Built with bz_bind reading g_pso_lmap as before, the mapped draw binds 0 and that check fails. make check_t_blend_zero: 165 checks, 0 failed, with the rule and with DM_NVN_BLEND_ZERO=0 (main: 160 and 160). Also checked: test_dmsh_blend_zero.py passes (its compile test for macOS and for iOS 18 too), t_alpha, t_logic, t_defer_chain, t_fuse_check, t_pso_warm and t_image_attachment_store pass as on main, and so does t_feedback but for its check "with nothing running: a commit", which fails in most runs on main too (13 of 15 runs here, 12 of 15 on main), test_docs passes, the Mac host builds with the same 58 warnings as main (M3 Max, macOS 26.7.2, Xcode 27.2, metal 32023.922). Not run: a game, regress.py (the readable-tile merge is off with default settings), lint_c.py (no clang-tidy here), iOS
seedds added a commit that referenced this pull request Nov 16, 2026
The blend-zero word (#2030) and the rounding word (#2807) at fragment
buffer DMSH_BLEND_BUFFER are computed per physical attachment through the
map of a merged pass (g_pso_lmap: physical target i holds output
lmap[i]). bz_bind ran inside the fragment encode_args, but draw_ clears
g_pso_lmap once it has the pipeline, before that call, so a merged draw
got the words of an unmerged pass while its pipeline blended under the
map. Output 0 drawn SRC_ALPHA / ONE_MINUS_SRC_ALPHA into an RGBA16F
attachment 1 got the zero word 0 and stored the NaN of alpha 0; output 0
drawn unblended into an RGBA8 attachment 1 got the rounding word 0 and
stored 66.5 / 255 as 67, not NVIDIA's 66.

draw_ now keeps a copy of the map beside raster_mask, before the reset,
and binds the fragment state the translated shader reads beside its
bindings itself (at_bind, bz_bind, lo_bind), after the fragment
arguments and the re-encode check; bz_bind takes the map as an argument
and bz_word / rop_word read it. encode_args_ binds only the shader's own
resources. g_bz_last keeps the words bound last, for the tests.

No game's picture changes on main: the only map draw_ sees now comes
from same-pass tile reads (fb_tile_plan), which map each output to its
own attachment, so only bits of outputs a shader does not write differ.

Checked (M4 Pro, macOS 27.2): t_blend_zero has a merged-pass row through
the real replay with readback (from PR #2463) and t_rop_round one for
the rounding word. With bz_bind given no map, as on main, they fail (10
and 33 checks); with the fix t_blend_zero 192/0 and t_rop_round 9284/0
in both modes. t_alpha, t_logic, t_feedback, t_pso_warm (MTLDevice and
Metal 4), t_image_attachment_store and t_nvn pass; the Mac host builds;
test_docs passes; clang-tidy at baseline. Not run: a game, regress.py
(no path on main maps an output to another attachment), iOS.

Co-authored-by: dean0396 <4335213+dean0396@users.noreply.github.com>
Co-authored-by: Lennart Kotzur <74553440+mmkzer0@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation game: 0400486024DEC000 A game nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders

Projects

None yet

4 participants