Repository navigation
Add optional asynchronous shader compilation with render skipping - #4
NicolasV15 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Shader retention, duplicate pipeline compilation after toggling, and takeover-unsafe verifier cleanup remain unresolved.
3 open issues
What changed in this PR
Adds optional asynchronous shader compilation to TOTK’s Metal renderer, trading temporary missing visuals for reduced compilation waits.
Changes:
- Adds bounded compilation queues and skips draws until shaders or pipelines are ready.
- Adds synchronized startup and menu controls with per-profile persistence.
- Documents limitations and adds queue, UI, and packaged-app verification tools.
| File | Description |
|---|---|
| tests/test_async_compile.py | Runs the compilation queue probe on macOS. |
| tests/async_shader_ui_probe.m | Checks controls, persistence, and overlay behavior. |
| tests/async_compile_probe.m | Tests queue states, deduplication, and settings. |
| README.md | Documents the optional mode in English and Chinese. |
| games/totk/third_party/d2r_gfx/gfx/nvn/nvn_mtl.m | Integrates asynchronous compilation and draw skipping. |
| games/totk/third_party/d2r_gfx/gfx/nvn/nvn_async_compile.m | Implements compilation queues and settings persistence. |
| games/totk/third_party/d2r_gfx/gfx/nvn/nvn_async_compile.h | Defines compilation state and settings interfaces. |
| games/totk/scripts/verify_first_launch_shaders.py | Explicitly disables asynchronous mode for baseline verification. |
| games/totk/scripts/verify_async_shaders.py | Observes packaged-app behavior with isolated test data. |
| games/totk/runtime/frontend.m | Adds startup and menu controls. |
| docs/first-launch-shaders.md | Records behavior, limitations, and verification scope. |
Review effort: Balanced
| (out/'game.log').write_text(text) | ||
| if not taken and game.poll() is None: | ||
| with commands.open('a') as f: f.write('close\n') | ||
| try: game.wait(timeout=30) | ||
| except subprocess.TimeoutExpired: report.update(status='failed', close_error='normal close timed out; left open') |
There was a problem hiding this comment.
Fixed in 7092afc.
finish_trial() now refreshes the session log and preemption state after writing the evidence and opening the command file, immediately before enqueueing close. A detected takeover changes the report to interrupted and leaves the game open. Missing/unreadable logs or a log still belonging to the previous session prevent automatic closing.
Added deterministic tests in tests/test_async_verifier.py that inject keyboard input or preemption during the evidence write, plus missing/old logs, normal closing and timeout cases. Restoring the stale cleanup check in an isolated negative control makes both takeover tests fail. The complete local suite passes 49 tests; these tests do not launch or close a real game.
| dispatch_once(&once, ^{ cache = [[NVNCompileCache alloc] initWithLimit:64]; }); | ||
| NSString *key = [NSString stringWithFormat:@"%d:%@", stage, pc]; | ||
| return [cache resultForKey:key wait:wait build:^{ return shader_load_raw(pc, stage); } state:state]; |
There was a problem hiding this comment.
Fixed in 7092afc.
The iOS warm-only shader_load() path now uses transientResultForKey:build:state:. It still shares in-flight work, but the cache removes a completed warm-only job once its active callers have returned. A persistent rendering request that joins the job promotes its ownership; warm-only callers cannot evict an already resident result. The existing macOS persistent warm-up behavior is preserved.
tests/async_compile_lifetime_probe.m verifies release of 300 transient objects, in-flight promotion without recompilation, and retention of resident results/failures. An isolated negative control that restores unconditional retention fails the lifetime test. All 49 local tests pass. This validates ARC ownership with Foundation objects; it is not an iOS device memory measurement.
| // Prediction for parallel precompilation (see the note before pso_get): scan from after the current operation to the end of this submit, replaying only state; at a draw, compute the pipeline key from "the state at that time + that render pass's attachment formats" and dispatch those not yet built to the background. | ||
| // Whether a render pass actually attaches depth / stencil is only decided at replay (depth-only / stencil-only / neither attached are omitted), so one key is computed for each possible attach mode -- guessing too many only means wasted background compiles and does not affect the result. | ||
| static void pso_predict_rest(void) { | ||
| if (nvn_async_shaders_enabled()) return; /* demand jobs have priority over speculative compilation */ |
There was a problem hiding this comment.
Fixed in 7092afc.
The asynchronous PSO registry is now visible to prediction scheduling, and pso_pend_kick() excludes keys already registered there. The reverse mode transition also reuses existing predicted work: async draws poll an unfinished prediction without consuming its slot, then consume the result and free the slot when ready. Blocking draws can still wait for that same prediction.
The production coordination code is shared through nvn_pso_pending.h with tests/async_pipeline_probe.m. A fake compiler verifies both switching directions, unrelated predictions, compilation counts and slot reclamation without creating a GPU device. Removing the async-registry exclusion in an isolated negative control makes the test fail. All 49 local tests and the modified Objective-C compilation checks pass.
|
Thank you for this, one of the first ideas here. main now has asynchronous shader preparation, on by default on the Mac: new shaders are prepared in the background and their draws wait or skip until they are ready. So we are closing this PR. If something in your approach does better than what is on main, a new PR based on main is welcome. |

解决的问题
首次启动当前会在显示第一帧前等待整批着色器预热;即使后台已经开始编译,渲染遇到尚未就绪的库或管线时仍会等待。本 PR 增加一个默认关闭、由玩家选择的异步着色器模式:允许先进入游戏,并在后台继续准备着色器;暂时无法执行的绘制跳过,后续帧再次检查,准备好后恢复。
这是以短暂画面缺失换取较少编译等待的选项。界面直接提示可能缺少物体或特效,不承诺开启后画面始终完整或彻底消除卡顿。
使用方式与行为
TOTK_PROFILE_DIR/graphics-settings.json,原文件其他字段会保留。TOTK_ASYNC_SHADERS=0|1可以覆盖本次启动,便于独立复测。关闭选项不会补做此前已经跳过的绘制,也不会清除玩家已有缓存。
实现说明
编译任务与状态
新增
nvn_async_compile.h/.m:渲染流程
nvn_mtl.m将尚未完成的库视为可重试状态,不把它永久记成缺失。初始程序未完整准备时不向后续绘制流程暴露半成品;绑定状态变化后的 shader 变体仍按原有匹配规则选择。渲染管线任务捕获本次管线 key、着色器和特化函数,后台完成描述符及管线创建,绘制线程在后续请求时发布结果。异步模式下停止额外的提交内预测编译,让当前实际请求优先。编译配置初始化、统计及派生函数缓存访问补充了并发保护。
某一 shader stage 已切换、另一 stage 仍在编译时,立即使旧 PSO / 资源摘要失效,避免恢复绘制后错误沿用此前的管线。
界面、文档与复测工具
frontend.m:启动复选框、图形菜单、设置保存和异步模式下的遮罩行为。docs/first-launch-shaders.md:用法、代价、实现范围及验证记录。verify_first_launch_shaders.py:显式关闭异步模式,继续检查原有首启全量预热要求。verify_async_shaders.py:使用独立空档案/缓存和共享 game-slot 检查真实 App;真实用户输入或用户启动抢占后停止控制。截图需要另行视觉检查。本 PR 基于上游
41e1e0c,保留其新增的启动阶段提示。提交只包含异步着色器选项的 15 个源码、测试和文档文件;其他本地游戏适配、地址布局修复、游戏数据、缓存、App 和存档不在提交中。验证结果
python -m unittest discover -s tests -v本地原始日志保留在项目外,未把资源或个人数据加入 PR。文档记录了复测入口和本地证据位置。
Copilot 审查意见处理(提交
7092afc)SessionMonitor;在证据写入及命令文件打开之后、发送close之前重新读取本次日志并检查抢占。发现接管即改记为中断并保留窗口;日志缺失或仍属于前次运行时不关闭。nvn_pso_pending.h。修复后 49 项本地测试通过,修改的 Objective-C 文件编译检查通过。另在项目外临时副本中分别恢复三种旧错误,相应测试均按预期失败。本次仅更新 PR 源码及测试,没有重新安装本机 App,也未追加完整游戏或 iOS 真机测试。审查线程保留供复核。
复测建议
关闭模式使用另一个全新的输出目录和
--async-mode 0;首次 GPU 编译较慢的机器需要增加观察时限。已知限制