#229

vulkan: do not write colour from fragment shaders that declare no colour output

Openbigjokeryowants to merge intofrom

Closes #179 — the roof-hide/show flash and the persistent roof/wall-edge checkerboards both disappear on the test device with this change (details and measurements below; root-cause analysis also posted in the issue).

What changed

One place in draw() in dm/gfx/vulkan/engine/xbg_game.cpp: when the bound fragment shader declares no colour output at all, the pipeline is built with colorWriteMask = 0 on every colour attachment, so the draw only produces its depth/stencil result. The facts come from the reflection the engine already computes for every shader (XbgRasterFacts.outputs, XbgRasterFacts.sample_mask), so nothing new is parsed:

if(noout_color && state.shader[4] && state.shader[4]->stage==4 &&
   !state.shader[4]->raster.outputs && !state.shader[4]->raster.sample_mask){
    for(auto &a:attachments) a.colorWriteMask=0;
}

Marker gfx-noout-color=0 (or DM_GFX_NOOUT_COLOR=0) turns it off.

Why

Walking in and out of houses in D2R, the roof disappeared/reappeared with hard-edged black blocks over the roof area for 4-11 frames — every single transition, on Mali only. The blocks are not a "dark shading" artefact: dyeing every clear value magenta turned them magenta, i.e. those pixels end up holding values nobody meant to write.

What the frame dumps showed (present-time dump of every attachment written that frame, plus a same-camera control 12 presents later):

attachment (screen-sized, written every frame)flash framecontrol frame
RGBA8, 3 mip levels, cleared to (1,1,1,1) every frame — the roof-fade factor image567 black 16x16 cells (14.1%), bbox over the roof0 cells, uniformly 255
RGBA16F companion (cleared to 0)unchangedunchanged
depth, R16 masks, bloom chain (B10G11R11 pyramid)byte-identicalbyte-identical

So the black already existed in that factor image before the composite/upscale stage, and only in the frames where the roof was animating.

Watching every draw that renders into that image explained the rest. During the flash, each roof mesh is drawn twice, in this order:

ed8df20b114afc67 / 949495670492f2e4   count=264     <- prepass, empty fragment shader
ed8df20b114afc67 / 949495670492f2e4   count=450
...
54f730a6c0efc118 / 44e41de8108725b1   count=264     <- same meshes, shaded
54f730a6c0efc118 / 44e41de8108725b1   count=450

949495670492f2e4 is a 712-byte SPIR-V module whose body is OpLabel; OpReturn; OpFunctionEnd — no output variables, no stores, nothing. In NVN that is the normal way to express "depth-only, leave colour alone". In Vulkan the fragment's colour output is undefined in that situation, and Mali writes zero, so the prepass erased the white factor image under every roof triangle before the shading pass blended into it. The blocks are exactly those triangles, which is also why they have hard edges and why they only show up while the roof animates.

Masking the colour writes for output-less fragment shaders removes them.

Tested device:

GPUMali-G925-Immortalis MC12, vendorID 0x13b5, deviceID 0xd8300010, driver 0xc40100, Vulkan 1.3.278
SoC / deviceMediaTek MT6991 (Dimensity 9400+), Redmi K80 Ultra 25060RK16C (dali), Android 16 (API 36)

Measured with the presented-frame black-cell probe (upper 78% of the frame only, 16x16 cell = black when >=60% of its samples are near black), same route, walking in and out of the same house:

beforeafter
Roof-flash segments (4-11 frame runs of black over the roof)14 segments / 89 hit frames in 8400 scanned frames1 segment / 10 frames — a scene transition that happens at the same present index in both runs, no door-transition segment at all
Worst block area in a roof segment736 cells, bbox (16,0)-(1920,608)gone
Prepasse draws reported by the fixn/ano-colour-output draw masked key=949495670492f2e4 targets=2 (the RGBA8 factor image and its RGBA16F companion)

Visually: the black blocks are gone, the roof fade itself looks unchanged; nothing else on screen changed that I could see (shadows, fog, UI, spells all unchanged in a 10-minute run).

Checks run

No game files needed:

  • python3 dm/i18n/scan_literals.py — I18N OK:中文字面量 474 条全在白名单(111 行),其余走中英表
  • python3 dm/ios/tools/dm_i18n.py --check — dm_i18n OK 467 条
  • python3 dm/host/tests/mode_scan.py dm — ✓ 模式登记闸门:每帧泵 24 个都已分类,散判模式与替按键的处数没有超出登记,登记项函数齐全

Game files / platform needed, not run here (this machine is Windows+WSL, no Xcode, no ./setup.sh output):

  • make -C dm/host/tests run — needs build/main.orig.bin from ./setup.sh
  • make -C dm/gfx/tests run — needs clang/clang++ and Metal for most targets
  • make -C dm/ios/tests test, dm/verify/regress.sh, cd dm/ios && ./run.sh build, dm/ios/tools/smoke_mac.sh 1, dm/ios/tools/check_hud_gen.sh — macOS only

Anything else: the Android debug APK was built here (gradle 9.8.1 + NDK 29 + CMake 3.31.6, :app:assembleDebug, BUILD SUCCESSFUL in 1m 23s), on a tree that contains only this change; the file touched produces no new compiler warning (its remaining warnings are pre-existing). The measurements above come from builds that additionally carried a presented-frame black-cell scanner and a per-image draw log; this PR contains none of that instrumentation — the shipped path is this change plus nothing else.

Visible changes

  • The roof fade no longer paints black blocks. Everything else renders as before.
  • Draws whose fragment shader cannot write colour stop producing driver-specific garbage in the colour attachment. This is a correctness fix in general, not only for D2R: the same situation (NVN depth-only draw expressed with an empty fragment shader) can show up in any title.

No game content

  • This change contains no game files, nothing generated from game files, no keys, and no built package
  • It does not carry the local rewrites ./setup.sh makes to dm/gfx/tools/dmsh_chain.lock, dm/gfx/nvn/dmsh_chain.h and dm/gfx/nvn/c1_full.tsv

Comments

The fix is deliberately narrow: it only fires when the fragment module declares no output location and no SampleMask, so occlusion-query-only draws are covered too, while any shader that writes colour is untouched. If a title ever relies on the undefined write (which would be a bug on its side), the marker switches the new behaviour off without a rebuild.

Activity

2
yyan16010d ago

Thanks, this matches the Vulkan rules: with no Output for a location, what lands in that colour attachment is undefined, so masking the writes is the right fix. The roof analysis is convincing. Three notes:

  1. XbgRasterFacts.outputs is a bitmask of the declared locations, so the same reasoning covers a shader that writes location 0 while two or more targets are bound: the attachments it doesn't declare are just as undefined. Masking per attachment would cover both cases, e.g. if(!(outputs>>i&1)) attachments[i].colorWriteMask=0;. Not needed for #179, but cheap.
  2. colorWriteMask is part of the pipeline key (xbg_pipeline_key.h), so the warm-up records for these depth-prepass pipelines (the shipped seed and existing device lists) no longer match. Each masked pipeline then compiles once at draw time, a one-time hitch per device until the device list records it. Worth a line in the PR, or a seed refresh with the next pack.
  3. Nit: the short comment inside draw() is Chinese only; the repo's convention is Chinese plus English.

On an Adreno 730 (Redmi K50 Ultra) we haven't seen the roof flash, so there this should change nothing visible. We can run it there if useful.