Skip to content

fix(gfx/nvn): 随包贴图被 GPU 写过后登记进共享表并归一化块线性布局键,修地形块变黑(#674) - #1911

Closed
judeyang wants to merge 1 commit into
jhaizhou-ops:mainfrom
judeyang:fix/674-pkg-gpu-alias
Closed

judeyang wants to merge 1 commit into
jhaizhou-ops:mainfrom
judeyang:fix/674-pkg-gpu-alias

Conversation

@judeyang

Copy link
Copy Markdown
Contributor

现象

0400486024DEC000 在 macOS 上游玩数分钟(约 2000–3000 帧)后,地面会出现边界锐利的方形纯黑/暗沉地形块(草叶等 3D 模型仍正常受光,仅地表发黑):

修前黑方块地形截图

现场读取绑定到地形着色器的 64 张贴图(GPU 显存 vs 客体内存),确认差异只在 reg3 的 260×260 RG8Unorm(9 级 mip、214,528 字节、CPU_NO_ACCESS | GPU_CACHED、MINIMAL_LAYOUT)地形法线贴图:

  • 正常受光地块:mtl_nz = 135200/135200,像素头 80808080(RG = (0.5, 0.5) 朝上法线)
  • 发黑方块地块:mtl_nz = 0/135200,像素头 00000000(全 0 法线背对太阳 → 地表变黑)

根因(三处叠加)

  1. 随包贴图(NvPkg)被 GPU 写过后未登记进共享表 g_alias(nvn_mtl.m gpw_mark):
    • 地形法线贴图首次建出时部分对象走 NvPkg 路径(未进 g_alias),随后被 GPU 烘焙写入(gpw_mark 将 214,528 字节标为 GPU 写过)。
    • 游戏切块时 Finalize 旧句柄、在同一 pool + off 上重建非随包贴图对象去采样;因原 NvPkg 不在 g_alias,查找落空后新建空贴图,再因 gpw_stale 命中「存储已被 GPU 写过」被整张清零。
  2. alias_key_of 未归一化块线性贴图的实际生效块布局(nvn_mtl.m alias_key_of):
    • 260×260 RG8 9 级 下,pkg_layout = 1 与 pkg_layout = 0 + MINIMAL_LAYOUT (0x200) 经 nvn_tex_layout 均解析为完全一致的 1-GOB 布局(tileH = 0, tileD = 0, size = 214528);直接按原始 pkg_layout 与 MINIMAL_LAYOUT 建键会导致等价描述互相错失。
  3. macOS 上 alias_orphan_pass 将私有显存贴图挂进低区账本 s2a_lowva(nvn_mtl.m alias_orphan_pass):
    • MTLStorageModePrivate 贴图在 macOS 上不占用 s2a_lowva 巡检的低区 VA;低区余量偏紧时回调 alias_orph_evict 释放 0 字节低区地址,却把无人引用但仍有效的 GPU 烘焙地形法线贴图直接回收,重建后再次读成全 0。

修复

  • gpw_mark:当 GPU 写入一个非稀疏、非虚拟池的 NvPkg 时,将其底层 MTLTexture 登记进 g_alias / g_alias_set / g_alias_seen / g_alias_wrote;在 NvPkg 上记录 tex_shared / spare_shared,dealloc 与重传退役 spare 时避免重复 res_remove;alias_held 与 serial_holds 同步识别 NvPkg->tex。
  • alias_key_of:对非缓冲、非线性的块线性贴图,按 nvn_tex_layout 解析后的实际 (tileH, tileD, w_align) 归一化 pkg_layout 并清掉 MINIMAL_LAYOUT,保持实际布局不同的描述仍分开。
  • alias_orphan_pass:将 s2a_lowva 腾退登记收口在 #if TARGET_OS_IPHONE(iOS/iPad 保留 #357 腾退机制,macOS 不再误删不占低区 VA 的私有贴图)。

验证

1. 单元测试(先红后绿,Metal 4 & Metal 3 双路)

新增单测 graphics/nvn/tests/t_nvn_pkg_gpu_alias.m(runtime/tests/mk/nvn_pkg_gpu_alias.mk):

  • 修前(红):t_nvn_pkg_gpu_alias 4 项 2 项失败(① NvPkg GPU 写过后同存储重建落空 + ② pkg=1 与 pkg=0+MINIMAL_LAYOUT 拆成两张贴图)
  • 修后(绿):
    • native-test-nvn_pkg_gpu_alias:Metal 4 5/5 PASS,Metal 3 5/5 PASS
    • native-test-nvn_alias_orphan:Metal 4 11/11 PASS,Metal 3 11/11 PASS
    • native-test-nvn_alias_overwrite:Metal 4 7/7 PASS,Metal 3 7/7 PASS
    • native-test-nvn_pool_tex:Metal 4 26/26 PASS,Metal 3 26/26 PASS

2. 实机验证(0400486024DEC000,Apple M3 Max)

  • 修前:约 2000–3000 帧必现 2–4 块黑方块地形,260×260 format 0xd 触发 zero instead 8–16 次。
  • 修后:连续跑至 5400 帧,黑方块地形 0 次,260×260 format 0xd zero instead 0 次,无引用共享贴图稳定在 13 MB,无驻留集或释放报错。

@jhaizhou-ops

Copy link
Copy Markdown
Owner

@judeyang 谢谢,收到。现场对比了 64 张贴图、定位到那张地形法线贴图全为 0,根因三处写得很清楚,还附了单测。改动在 NVN 直连图形层,已转给负责这一层的开发者审,审完在这里回复。

jhaizhou-ops added a commit that referenced this pull request Nov 21, 2026
在 judeyang 的提交上补:
- alias_take_over 退掉一个表项时,若它的贴图仍是某个随包贴图(NvPkg,活着 / 定稿 / 本次提交在用)的 tex,
  只放共享表那份引用(defer_obj_unref)并清掉该 NvPkg 的 tex_shared,由 NvPkg 销毁时自己出驻留集。
  原样会 defer_obj_release:另一张贴图写过重叠存储、接管后,那张随包贴图出了驻留集,序号再画就读未驻留内存。
- gpw_mark 只在共享表这个键还空着时登记;同键下已有别的贴图时不覆盖(hm_put_id 覆盖会当场释放旧值,跳过延后退役)。
- 随包贴图整张重传(CPU 改写了存储)时,摘掉共享表里指向旧贴图的过期表项;spare_shared 不再需要。
- 单测 t_nvn_pkg_gpu_alias 加 ⑤:活着的随包贴图被接管后贴图仍在驻留集、再解析仍是它
  (负对照:去掉第一条,⑤ 两项红);mk 依赖改 NVN_MTL_DEPS。

Co-Authored-By: judeyang <judeyang@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jhaizhou-ops

Copy link
Copy Markdown
Owner

@judeyang 已合入 main(合入提交 095cc20),你的提交原样保留署名,维护者另补了一个提交。

定因和修法都对,原样收下:随包贴图被 GPU 写过后登记进共享表;块布局键按实际生效的布局归一化;macOS 上不再按低区地址回收这类贴图。

维护者补了三处:

  • 另一张贴图接管同一段存储时,原写法会把随包贴图自己那张移出常驻集,之后再画会读到没有常驻的内存。已改成只放掉共享表里那份引用。
  • 登记时不覆盖同一位置上别人的贴图。
  • 随包贴图整张重传时,摘掉过期的表项。

单测加了一例,负对照是红的。这次修复不在 0.6.4 里,随下一个版本发布,发布后在这里回帖给版本号。真机局我们没有复跑,你在 Mac 上跑到 5400 帧、黑块 0 次,这份数据很有帮助,谢谢!


@judeyang Merged into main (merge commit 095cc20). Your commits are kept as-is with your authorship, plus one maintainer follow-up commit.

Your diagnosis and fix are right and were taken as-is: register packaged textures in the shared table once the GPU has written them, normalize the block-linear layout key by the layout actually in effect, and stop reclaiming these textures by low-region address on macOS.

The maintainer added three changes:

  • When another texture takes over the same storage, the original code removed the packaged texture's own texture from the resident set, so later draws read non-resident memory. It now drops only the shared table's reference.
  • Registration no longer overwrites someone else's texture at the same location.
  • When a packaged texture is re-uploaded whole, stale table entries are removed.

One unit test was added, and its negative control was red. This isn't in the 0.6.4 candidate; it ships in the next release, and we'll post the version here when it's out. We haven't rerun a real-device session ourselves; your Mac run reaching 5400 frames with zero black blocks was very helpful — thank you!

@jhaizhou-ops

Copy link
Copy Markdown
Owner

@judeyang 已随 0.6.5 发布:https://github.com/jhaizhou-ops/S2Arm/releases/tag/v0.6.5 (095cc20f7)。再次感谢!

Shipped in 0.6.5: https://github.com/jhaizhou-ops/S2Arm/releases/tag/v0.6.5 (095cc20f7). Thanks again!

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