@namewta/skills-hub 0.1.49 → 0.1.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/THIRD_PARTY_NOTICES.md +19 -7
- package/cli.mjs +2 -2
- package/install.mjs +11 -4
- package/package.json +1 -1
- package/scripts/persistence/home.md +1 -1
- package/skills/dev/dev-build/SKILL.md +2 -0
- package/skills/dev/dev-build/references/implement-request.md +7 -2
- package/skills/dev/dev-formal-verify/SKILL.md +2 -1
- package/skills/dev/dev-formal-verify/references/create-check.md +3 -1
- package/skills/dev/dev-formal-verify/references/example.md +1 -1
- package/skills/dev/dev-formal-verify/references/tool-choice.md +1 -1
- package/skills/dev/dev-help/references/capabilities.md +6 -6
- package/skills/dev/dev-help/references/guide-verification.md +7 -1
- package/skills/dev/dev-help/references/help-method.md +7 -1
- package/skills/dev/dev-issue/SKILL.md +1 -0
- package/skills/dev/dev-issue/references/github-operations.md +8 -0
- package/skills/dev/dev-plan/references/plan-template.md +1 -0
- package/skills/dev/dev-plan/references/vertical-slicing.md +2 -0
- package/skills/dev/dev-pr/references/pull-request-evidence.md +2 -0
- package/skills/dev/dev-review/references/host-parity.md +12 -0
- package/skills/dev/dev-review/references/review-baseline.md +2 -0
- package/skills/dev/dev-setup/references/formal-options.md +1 -1
- package/skills/dev/dev-statechart/SKILL.md +17 -19
- package/skills/dev/dev-statechart/agents/openai.yaml +3 -3
- package/skills/dev/dev-statechart/assets/theme.css +135 -0
- package/skills/dev/dev-statechart/references/lean-proof.md +47 -0
- package/skills/dev/dev-statechart/references/render.md +15 -83
- package/skills/dev/dev-statechart/references/runtime.md +21 -1
- package/skills/dev/dev-statechart/scripts/lean/backends/lemma-chart.mjs +285 -0
- package/skills/dev/dev-statechart/scripts/lean/backends/mermaid.mjs +174 -0
- package/skills/dev/dev-statechart/scripts/lean/backends/proof-chart.mjs +540 -0
- package/skills/dev/dev-statechart/scripts/lean/backends/report.mjs +242 -0
- package/skills/dev/dev-statechart/scripts/lean/frontends/facts.mjs +234 -0
- package/skills/dev/dev-statechart/scripts/lean/frontends/view.mjs +217 -0
- package/skills/dev/dev-statechart/scripts/lean/ir.mjs +226 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/attribute.mjs +148 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/blocks.mjs +32 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/budget.mjs +167 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/check.mjs +70 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/contract.mjs +160 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/fold.mjs +206 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/inline.mjs +84 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/mark.mjs +432 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/reduce.mjs +73 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/trust.mjs +111 -0
- package/skills/dev/dev-statechart/scripts/lean/passes/view-lemmas.mjs +155 -0
- package/skills/dev/dev-statechart/scripts/lean/pipeline.mjs +164 -0
- package/skills/dev/dev-statechart/scripts/lean/run.mjs +168 -0
- package/skills/dev/dev-statechart/scripts/statechart.mjs +42 -23
- package/skills/dev/dev-statechart/scripts/worker.mjs +33 -7
- package/skills/dev/dev-ui-design/SKILL.md +2 -2
- package/skills/dev/dev-ui-design/references/interaction-review.md +9 -0
- package/skills/dev/dev-verify/SKILL.md +1 -1
- package/skills/dev/dev-verify/references/evidence-storage.md +12 -0
- package/skills/dev/dev-workflow/SKILL.md +6 -6
- package/skills/dev/dev-workflow/references/method-handoffs.md +7 -1
- package/skills/dev/dev-writing/SKILL.md +2 -0
- package/skills/dev/dev-writing/references/library-docs.md +37 -0
- package/skills/home/home-doctor/references/diagnosis.md +2 -2
- package/skills/home/home-doctor/scripts/doctor.mjs +68 -38
- package/skills/home/home-project-changelog/SKILL.md +7 -7
- package/skills/home/home-project-changelog/assets/project-changelog.example.json +188 -0
- package/skills/home/home-project-changelog/assets/project-changelog.schema.json +156 -0
- package/skills/home/home-project-changelog/assets/worker-brief.md +1 -1
- package/skills/home/home-project-changelog/references/git-ranges.md +2 -2
- package/skills/home/home-project-changelog/references/schema.md +61 -0
- package/skills/home/home-project-changelog/references/tools.md +28 -22
- package/skills/home/home-project-changelog/references/workflow.md +12 -9
- package/skills/home/home-project-changelog/references/writing.md +5 -3
- package/skills/home/home-project-changelog/scripts/changelog.mjs +42 -38
- package/skills/home/home-project-changelog/scripts/contract.mjs +40 -3
- package/skills/home/home-project-changelog/scripts/legacy-markdown.mjs +116 -0
- package/skills/home/home-project-changelog/scripts/migrate.mjs +122 -0
- package/skills/home/home-project-changelog/scripts/revision.mjs +2 -196
- package/skills/home/home-project-changelog/scripts/storage.mjs +120 -125
- package/skills/home/home-project-changelog/scripts/summary.mjs +0 -60
- package/skills/home/home-route/SKILL.md +2 -2
- package/skills/home/home-route/references/routing.md +3 -1
- package/skills/home/home-route/scripts/route.mjs +79 -11
- package/skills/home/home-setup/SKILL.md +2 -1
- package/skills/home/home-setup/assets/repos.schema.json +10 -0
- package/skills/home/home-setup/references/registry.md +18 -1
- package/skills/home/home-setup/scripts/registry.mjs +67 -7
- package/skills/home/home-website/SKILL.md +13 -9
- package/skills/home/home-website/assets/website/scripts/inputs.mjs +143 -128
- package/skills/home/home-website/assets/website/scripts/render.mjs +8 -6
- package/skills/home/home-website/assets/website/scripts/site.mjs +21 -10
- package/skills/home/home-website/assets/website/site/public/home-assets/app.js +81 -34
- package/skills/home/home-website/assets/website/site/public/home-assets/site.css +92 -70
- package/skills/home/home-website/assets/website/site/templates/favicon.png +0 -0
- package/skills/home/home-website/assets/website/site/templates/layout.mjs +13 -10
- package/skills/home/home-website/assets/website/site/templates/ledger.mjs +51 -46
- package/skills/home/home-website/assets/website/site/templates/model.mjs +58 -14
- package/skills/home/home-website/assets/website/site/templates/pages.mjs +28 -30
- package/skills/home/home-website/assets/website/site/templates/project-logs.mjs +3 -107
- package/skills/home/home-website/assets/website/site.config.json +1 -1
- package/skills/home/home-website/references/website.md +64 -46
- package/skills/home/home-website/scripts/activate.mjs +106 -0
- package/skills/home/home-website/scripts/init.mjs +98 -11
- package/skills/home/home-workflow/SKILL.md +6 -4
- package/skills/system/system-clash-proxy-config/SKILL.md +21 -0
- package/skills/system/system-clash-proxy-config/agents/openai.yaml +4 -0
- package/skills/system/system-clash-proxy-config/assets/clash-override.js +328 -0
- package/skills/system/{system-proxy → system-clash-proxy-config}/references/browser-privacy.md +2 -2
- package/skills/system/{system-config → system-clash-proxy-config}/references/changes.md +2 -2
- package/skills/system/{system-proxy → system-clash-proxy-config}/references/clash-verge.md +1 -1
- package/skills/system/{system-proxy → system-clash-proxy-config}/references/diagnosis.md +1 -1
- package/skills/system/system-clash-proxy-config/references/dns-workflow.md +42 -0
- package/skills/system/system-clash-proxy-config/references/egress-macos.md +31 -0
- package/skills/system/system-clash-proxy-config/references/egress-windows.md +37 -0
- package/skills/system/system-clash-proxy-config/references/egress.md +12 -0
- package/skills/system/{system-proxy → system-clash-proxy-config}/references/flclash.md +1 -1
- package/skills/system/{system-proxy → system-clash-proxy-config}/references/index.md +1 -1
- package/skills/system/{system-clash-tun-egress → system-clash-proxy-config}/references/linux-cutover.md +29 -0
- package/skills/system/system-clash-proxy-config/references/locale-workflow.md +9 -0
- package/skills/system/{system-proxy → system-clash-proxy-config}/references/mihomo.md +1 -25
- package/skills/system/system-clash-proxy-config/references/session.md +34 -0
- package/skills/system/system-clash-proxy-config/references/subscription.md +29 -0
- package/skills/system/{system-proxy → system-clash-proxy-config}/references/troubleshooting.md +7 -7
- package/skills/system/{system-clash-tun-egress → system-clash-proxy-config}/references/verification.md +2 -0
- package/skills/system/system-clash-proxy-config/scripts/prepare-session.mjs +155 -0
- package/skills/system/system-clash-proxy-config/scripts/run-session.mjs +138 -0
- package/skills/system/system-inspect/SKILL.md +1 -1
- package/skills/system/system-inspect/references/health-audit.md +1 -1
- package/upstream-skills/zh-archify/SKILL.md +11 -11
- package/upstream-skills/zh-archify/assets/template.html +6 -3
- package/upstream-skills/zh-archify/bin/archify.mjs +67 -4
- package/upstream-skills/zh-archify/modules/code-analysis/ARCHITECTURE.md +7 -1
- package/upstream-skills/zh-archify/modules/code-analysis/bin/analyze-run.mjs +2 -1
- package/upstream-skills/zh-archify/modules/code-analysis/bin/check.mjs +29 -15
- package/upstream-skills/zh-archify/modules/code-analysis/bin/drift.mjs +3 -3
- package/upstream-skills/zh-archify/modules/code-analysis/bin/prepare.mjs +5 -6
- package/upstream-skills/zh-archify/modules/code-analysis/bin/serve.mjs +66 -36
- package/upstream-skills/zh-archify/modules/code-analysis/bin/verify-change.mjs +43 -13
- package/upstream-skills/zh-archify/modules/code-analysis/docs/TECH-GUIDE.md +1 -1
- package/upstream-skills/zh-archify/modules/code-analysis/docs/USAGE.md +37 -4
- package/upstream-skills/zh-archify/modules/code-analysis/extract/ts/functions.mjs +7 -5
- package/upstream-skills/zh-archify/modules/code-analysis/extract/ts/index.mjs +4 -1
- package/upstream-skills/zh-archify/modules/code-analysis/extract/ts/snapshot-host.mjs +9 -0
- package/upstream-skills/zh-archify/modules/code-analysis/lib/architecture-drift.mjs +28 -7
- package/upstream-skills/zh-archify/modules/code-analysis/lib/drift-capture.mjs +4 -3
- package/upstream-skills/zh-archify/modules/code-analysis/lib/git-sources.mjs +5 -3
- package/upstream-skills/zh-archify/modules/code-analysis/lib/output.mjs +272 -0
- package/upstream-skills/zh-archify/modules/code-analysis/lib/pull-request.mjs +34 -4
- package/upstream-skills/zh-archify/modules/code-analysis/lib/task.mjs +4 -2
- package/upstream-skills/zh-archify/modules/code-analysis/lib/verification-context.mjs +13 -1
- package/upstream-skills/zh-archify/modules/code-analysis/lib/verification-history.mjs +112 -0
- package/upstream-skills/zh-archify/modules/code-analysis/lib/verification.mjs +6 -2
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/atlas.mjs +71 -5
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/client/atlas.css +16 -2
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/client/atlas.js +121 -5
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/client/atlas3d.js +38 -1
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/client/overlay.css +14 -1
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/client/overlay.js +187 -27
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/client/verification.js +1 -1
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/i18n.mjs +9 -0
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/inject.mjs +1 -1
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/page.mjs +11 -6
- package/upstream-skills/zh-archify/modules/code-analysis/overlay/pull-request.mjs +1 -1
- package/upstream-skills/zh-archify/modules/code-analysis/schemas/ai-task.schema.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/atlas.test.mjs +88 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/duplicates.test.mjs +1 -1
- package/upstream-skills/zh-archify/modules/code-analysis/test/external-workflow.test.mjs +1 -1
- package/upstream-skills/zh-archify/modules/code-analysis/test/fixtures/tasks/boundary.task.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/fixtures/tasks/boundary.task.missing.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/fixtures/tasks/complexity.task.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/fixtures/tasks/complexity.task.valid.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/fixtures/tasks/import-cycle.task.changed.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/fixtures/tasks/import-cycle.task.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/fixtures/tasks/module-cycle.task.json +1 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/function-source-range.test.mjs +10 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/output-recovery.test.mjs +626 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/pull-request.test.mjs +98 -2
- package/upstream-skills/zh-archify/modules/code-analysis/test/review.test.mjs +37 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/source-panel.test.mjs +2 -1
- package/upstream-skills/zh-archify/modules/code-analysis/test/task.test.mjs +3 -2
- package/upstream-skills/zh-archify/modules/code-analysis/test/verification-history.test.mjs +216 -0
- package/upstream-skills/zh-archify/modules/code-analysis/test/verification.test.mjs +122 -0
- package/upstream-skills/zh-archify/package-lock.json +2 -2
- package/upstream-skills/zh-archify/package.json +1 -1
- package/upstream-skills/zh-archify/references/authoring-contract.md +2 -0
- package/upstream-skills/zh-archify/references/explainer-authoring.md +1 -1
- package/upstream-skills/zh-archify/renderers/architecture/render-architecture.mjs +1 -0
- package/upstream-skills/zh-archify/renderers/class/render-class.mjs +1 -0
- package/upstream-skills/zh-archify/renderers/dataflow/render-dataflow.mjs +2 -1
- package/upstream-skills/zh-archify/renderers/erd/render-erd.mjs +1 -0
- package/upstream-skills/zh-archify/renderers/lifecycle/render-lifecycle.mjs +4 -2
- package/upstream-skills/zh-archify/renderers/sequence/render-sequence.mjs +4 -2
- package/upstream-skills/zh-archify/renderers/shared/cli.mjs +7 -1
- package/upstream-skills/zh-archify/renderers/timeline/render-timeline.mjs +1 -0
- package/upstream-skills/zh-archify/renderers/tree/render-tree.mjs +1 -0
- package/upstream-skills/zh-archify/renderers/waterfall/render-waterfall.mjs +1 -0
- package/upstream-skills/zh-archify/renderers/workflow/render-workflow.mjs +3 -2
- package/upstream-skills/zh-archify/renderers/workflow/workflow-compiler.mjs +68 -7
- package/upstream-skills/zh-archify/skill-release.json +1 -1
- package/upstream-skills/zh-modern-web-guidance/SKILL.md +4 -11
- package/upstream-skills/zh-modern-web-guidance/guides/built-in-ai/language-detection.md +20 -0
- package/upstream-skills/zh-modern-web-guidance/guides/built-in-ai/language-model.md +22 -0
- package/upstream-skills/zh-modern-web-guidance/guides/built-in-ai/prompt-api.md +22 -0
- package/upstream-skills/zh-modern-web-guidance/guides/built-in-ai/summarizer.md +20 -0
- package/upstream-skills/zh-modern-web-guidance/guides/built-in-ai/translator.md +22 -0
- package/upstream-skills/zh-modern-web-guidance/guides/performance/custom-ml-model-inference.md +342 -0
- package/upstream-skills/zh-modern-web-guidance/guides/performance/data-parallel-computation.md +337 -0
- package/upstream-skills/zh-modern-web-guidance/guides/performance/improve-next-page-load-performance.md +4 -1
- package/upstream-skills/zh-modern-web-guidance/guides/performance/large-scene-rendering.md +350 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-atoms/position-aware-tooltips.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-atoms/viewport-aware-dropdowns.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-behaviors/animate-to-from-top-layer.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-behaviors/declarative-dialog-popover-control.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-behaviors/interest-triggered-tooltips.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-behaviors/scroll-target-on-load.md +29 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-behaviors/sortable-table.md +1 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/app-menus.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/context-menu.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/copy-button.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/dialog.md +10 -1
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/hovercard.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/menu.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/navbar.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/persistent-app-tours.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/persistent-toast-notifications.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/ui-components/toolbar.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/user-experience/content-first-noise-reduction.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/user-experience/natural-interactions.md +9 -0
- package/upstream-skills/zh-modern-web-guidance/guides/webmcp/agentic-javascript-tools.md +27 -0
- package/skills/home/home-project-changelog/assets/project-changelog.md +0 -35
- package/skills/system/system-clash-tun-egress/SKILL.md +0 -22
- package/skills/system/system-clash-tun-egress/agents/openai.yaml +0 -4
- package/skills/system/system-config/SKILL.md +0 -37
- package/skills/system/system-config/agents/openai.yaml +0 -4
- package/skills/system/system-config/references/ai-cli-config.md +0 -75
- package/skills/system/system-config/references/ai-cli-optimization.md +0 -37
- package/skills/system/system-config/references/index.md +0 -12
- package/skills/system/system-config/references/locale-workflow.md +0 -33
- package/skills/system/system-config/references/locale.md +0 -50
- package/skills/system/system-proxy/SKILL.md +0 -40
- package/skills/system/system-proxy/agents/openai.yaml +0 -4
- package/skills/system/system-proxy/assets/protected-rules.yaml +0 -7
- package/skills/system/system-proxy/references/dns-workflow.md +0 -152
- /package/skills/system/{system-config → system-clash-proxy-config}/assets/configuration.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/assets/network.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/references/acceptance.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/references/dns-fields.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/references/dns-misconceptions.md +0 -0
- /package/skills/system/{system-clash-tun-egress/references/troubleshooting.md → system-clash-proxy-config/references/egress-troubleshooting.md} +0 -0
- /package/skills/system/{system-config → system-clash-proxy-config}/references/locale-linux.md +0 -0
- /package/skills/system/{system-config → system-clash-proxy-config}/references/locale-macos.md +0 -0
- /package/skills/system/{system-config → system-clash-proxy-config}/references/locale-windows.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/references/network-performance.md +0 -0
- /package/skills/system/{system-clash-tun-egress → system-clash-proxy-config}/references/nodes-and-dns.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/references/platform-dns.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/references/platforms.md +0 -0
- /package/skills/system/{system-proxy → system-clash-proxy-config}/references/tun-dns.md +0 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
# 渲染包含数千种不同物体的场景
|
|
2
|
+
|
|
3
|
+
CAD 查看器、设计工具和地图渲染器经常要绘制数千个网格、材质或样式各异的物体。城市或地形等大型 3D 世界还需要每帧判断摄像机可以看到哪些物体。如果每个物体对应独立的绘制调用,每次 `requestAnimationFrame` 从 JavaScript 发出数千次 `setPipeline()`、`setBindGroup()`、`setVertexBuffer()` 和 `draw()` 调用,即使 GPU 能轻松绘制这些几何体,也会使主线程繁忙。
|
|
4
|
+
|
|
5
|
+
WebGPU 通过 WebGL 2 所不支持的两种方式减少这部分工作:
|
|
6
|
+
|
|
7
|
+
- **渲染包**(`GPURenderBundle`,由 `GPURenderBundleEncoder` 录制)一次录制并验证一系列绘制命令。每帧只调用一次 `renderPass.executeBundles([bundle])` 就能重放整组命令,因而每帧的 JavaScript 开销、浏览器与 GPU 进程间通信和 WebGPU 验证开销不再随绘制调用数增长。
|
|
8
|
+
- **计算着色器和间接绘制**在 GPU 上决定绘制哪些物体及其数量,JavaScript 无需在每帧遍历物体。
|
|
9
|
+
|
|
10
|
+
GPU 仍会执行每次绘制,浏览器也仍会把每条绘制命令转换为原生 GPU API 命令。实际节约取决于场景、浏览器和设备,应测量更改前后的帧时间。
|
|
11
|
+
|
|
12
|
+
> [!NOTE]
|
|
13
|
+
> 无需绘制到屏幕的计算参见 `data-parallel-computation`(通过 `npx -y modern-web-guidance@latest retrieve "data-parallel-computation"`)。
|
|
14
|
+
|
|
15
|
+
## 判断 WebGPU 在哪些场景更快
|
|
16
|
+
|
|
17
|
+
场景需要 WebGL 2 无法提供的能力时,WebGPU 可能更快。请用真实场景实测:
|
|
18
|
+
|
|
19
|
+
- **数千个不同物体跨帧不变**(WebGPU 渲染包):含众多部件和材质的 CAD 装配体、多图层设计工具或图块样式各异的地图。在 WebGL 2 中,每个物体对应一次由 JavaScript 发起并由浏览器逐帧重复验证的绘制调用。WebGPU 渲染包只录制并验证一次,每帧通过一次调用重放。变换、颜色和摄像机数据仍可逐帧更新。
|
|
20
|
+
- **大量物体的可见性逐帧变化**(WebGPU 计算着色器和间接绘制,见“在 GPU 上剔除物体”):例如摄像机移动的大型城市、地形或点云。计算着色器在 GPU 上检查各物体,将可见物体数写入缓冲区,`drawIndexedIndirect()` 从其中读取数量,JavaScript 无需遍历物体。间接绘制可以录入渲染包。WebGL 2 不提供计算着色器和间接绘制,必须由 CPU 决定绘制内容。
|
|
21
|
+
- **实例逐帧由 GPU 更新**(WebGPU 计算着色器):例如粒子模拟或动画人群。计算着色器在存储缓冲区更新每个实例的数据,实例化绘制直接读取该数据。WebGL 2 需要变换反馈或纹理间复制等替代手段,或由 JavaScript 每帧更新和上传数据。
|
|
22
|
+
|
|
23
|
+
如果是**同一网格的众多副本,其数据静态不变或由 JavaScript 更新**,例如地图标记或网格图块,WebGPU 并不更快。两个 API 都只需一次实例化绘制调用,GPU 也做相同的工作。在 WebGPU 渲染器中,应使用一次 `draw(vertexCount, instanceCount)` 绘制相同网格,通过 `@builtin(instance_index)` 读取各实例的数据,并将调用录制进渲染包。
|
|
24
|
+
|
|
25
|
+
选择高亮等逐帧变化的绘制调用可作为普通绘制,与渲染包在同一个渲染通道中执行。
|
|
26
|
+
|
|
27
|
+
## 创建 GPU 设备
|
|
28
|
+
|
|
29
|
+
每次创建设备(包括设备丢失后的重建)时都要请求一个新适配器。一个适配器只能创建一个设备。
|
|
30
|
+
|
|
31
|
+
- 没有适合的 GPU 时,`requestAdapter()` 兑现为 `null`。`requestDevice()` 也可能被拒绝,例如所请求的特性或限制不受支持。两种情况都应采用回退。
|
|
32
|
+
仅请求应用实际使用的可选特性和限制。设备严格采用创建时设置的限制,因此只请求所需能力的应用在不同设备上会得到相同的验证错误,不受开发机器的限制影响。更高的限制也可能降低某些设备的性能。
|
|
33
|
+
仅在应用使用 `f16` 且适配器支持时请求 `shader-f16`。请求不受支持的特性会使 `requestDevice()` 被拒绝。
|
|
34
|
+
默认的 `maxStorageBufferBindingSize`(128 MiB)和 `maxBufferSize`(256 MiB)足够满足大多数应用。数据确有需要时,再请求更大的限制,并且不能超出适配器支持范围。
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
const LARGE_BUFFER_BYTES = 1024 * 1024 * 1024; // 1 GiB: set this to the largest buffer your app creates
|
|
38
|
+
|
|
39
|
+
// Returns null when the GPU path isn't available; use the fallback then.
|
|
40
|
+
async function createGpuDevice({ f16 = false, largeBuffers = false } = {}) {
|
|
41
|
+
if (!('gpu' in navigator)) return null;
|
|
42
|
+
try {
|
|
43
|
+
const adapter = await navigator.gpu.requestAdapter(); // New adapter per device
|
|
44
|
+
if (!adapter) return null;
|
|
45
|
+
// Only request what's needed. The device enforces exactly the features and limits it
|
|
46
|
+
// was created with, so exceeding a default limit fails on every device, not only on
|
|
47
|
+
// users' devices with lower limits than the development machine.
|
|
48
|
+
const supportsF16 = f16 && adapter.features.has('shader-f16');
|
|
49
|
+
const requiredLimits = {};
|
|
50
|
+
if (largeBuffers) {
|
|
51
|
+
requiredLimits.maxBufferSize = Math.min(LARGE_BUFFER_BYTES, adapter.limits.maxBufferSize);
|
|
52
|
+
requiredLimits.maxStorageBufferBindingSize =
|
|
53
|
+
Math.min(LARGE_BUFFER_BYTES, adapter.limits.maxStorageBufferBindingSize);
|
|
54
|
+
}
|
|
55
|
+
const device = await adapter.requestDevice({
|
|
56
|
+
requiredFeatures: supportsF16 ? ['shader-f16'] : [],
|
|
57
|
+
requiredLimits,
|
|
58
|
+
});
|
|
59
|
+
return { device, supportsF16 };
|
|
60
|
+
} catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
需要更多资源时(例如处理 GPU 能容纳的最大数据集),可改为请求适配器的上限。分配前检查得到的 `device.limits`,因为各设备的上限各不相同:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const device = await adapter.requestDevice({
|
|
70
|
+
requiredLimits: {
|
|
71
|
+
maxBufferSize: adapter.limits.maxBufferSize,
|
|
72
|
+
maxStorageBufferBindingSize: adapter.limits.maxStorageBufferBindingSize,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
本指南使用默认限制即可,而且不使用 `f16`,两者都不必请求:
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
const gpu = await createGpuDevice({ f16: false, largeBuffers: false });
|
|
81
|
+
if (!gpu) return useFallbackRenderer();
|
|
82
|
+
const { device } = gpu;
|
|
83
|
+
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
先创建管线、缓冲区和绑定组,全部成功后再获取 WebGPU 画布上下文。已有 WebGPU 上下文的画布,调用 `getContext('webgl2')` 和 `getContext('2d')` 会返回 `null`,因此回退渲染器无法使用此画布:
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
const context = canvas.getContext('webgpu');
|
|
90
|
+
context.configure({ device, format: presentationFormat });
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## 录制渲染包
|
|
94
|
+
|
|
95
|
+
在动画循环之外只录制一次渲染包:
|
|
96
|
+
|
|
97
|
+
- **匹配渲染通道**:编码器的 `colorFormats`、`depthStencilFormat` 和 `sampleCount` 必须与执行渲染包的渲染通道匹配。若该通道将深度或模板附件设为只读,录制时也应将 `depthReadOnly` 或 `stencilReadOnly` 设为 `true`,否则 `executeBundles()` 会产生验证错误。
|
|
98
|
+
- **在包内设置所有状态**:渲染包不继承渲染通道的管线、绑定组、顶点缓冲区和索引缓冲区,需在包内设置。视口、剪裁矩形、混合常量和模板参考值无法录入渲染包,要使用渲染通道设置的值。
|
|
99
|
+
- **跨管线共享绑定组**:使用 `layout: 'auto'` 时,每个管线获得独立的绑定组布局,为一个管线创建的绑定组不能给另一个管线使用。要在整个包的所有管线中只绑定一次共享摄像机数据,应让这些管线使用同一个显式的 `GPUPipelineLayout`。
|
|
100
|
+
- **检查错误**:`finish()` 不会抛出异常。无效渲染包会报告验证错误,因此应在错误作用域内录制。
|
|
101
|
+
|
|
102
|
+
下例在 `@group(0)` 处绑定一次共享摄像机 uniform,并在 `@group(1)` 绑定每个物体的 uniform。按管线和网格给物体排序,确保渲染包只在必要时切换状态:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
async function recordSceneBundle(device, format, cameraBindGroup, objects) {
|
|
106
|
+
device.pushErrorScope('validation');
|
|
107
|
+
const encoder = device.createRenderBundleEncoder({
|
|
108
|
+
colorFormats: [format],
|
|
109
|
+
// Add depthStencilFormat and sampleCount if the render pass uses them.
|
|
110
|
+
});
|
|
111
|
+
encoder.setBindGroup(0, cameraBindGroup); // Shared camera uniforms
|
|
112
|
+
|
|
113
|
+
let pipeline = null;
|
|
114
|
+
let mesh = null;
|
|
115
|
+
for (const object of objects) { // Sorted by pipeline, then mesh
|
|
116
|
+
if (object.pipeline !== pipeline) {
|
|
117
|
+
pipeline = object.pipeline;
|
|
118
|
+
encoder.setPipeline(pipeline);
|
|
119
|
+
}
|
|
120
|
+
if (object.mesh !== mesh) {
|
|
121
|
+
mesh = object.mesh;
|
|
122
|
+
encoder.setVertexBuffer(0, mesh.vertexBuffer);
|
|
123
|
+
}
|
|
124
|
+
encoder.setBindGroup(1, object.bindGroup); // Per-object uniforms
|
|
125
|
+
encoder.draw(mesh.vertexCount);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const bundle = encoder.finish();
|
|
129
|
+
if (await device.popErrorScope()) return null; // Use the fallback renderer
|
|
130
|
+
return bundle;
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## 每帧绘制渲染包
|
|
135
|
+
|
|
136
|
+
每帧将变化的数据写入渲染包已有的缓冲区,然后重放渲染包。`executeBundles()` 会清除渲染通道的管线、绑定组、顶点缓冲区和索引缓冲区,因此在同一通道内执行后续普通绘制前,需要重新设置它们:
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
function renderFrame() {
|
|
140
|
+
// Changing buffer contents doesn't require re-recording the bundle.
|
|
141
|
+
device.queue.writeBuffer(cameraUniformBuffer, 0, cameraMatrixData);
|
|
142
|
+
|
|
143
|
+
const commandEncoder = device.createCommandEncoder();
|
|
144
|
+
const renderPass = commandEncoder.beginRenderPass({
|
|
145
|
+
colorAttachments: [{
|
|
146
|
+
view: context.getCurrentTexture().createView(),
|
|
147
|
+
clearValue: { r: 0.05, g: 0.08, b: 0.14, a: 1 },
|
|
148
|
+
loadOp: 'clear',
|
|
149
|
+
storeOp: 'store',
|
|
150
|
+
}],
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
renderPass.executeBundles([sceneBundle]); // Replays every recorded draw call
|
|
154
|
+
|
|
155
|
+
// A regular draw call after the bundle must set its state again.
|
|
156
|
+
renderPass.setPipeline(highlightPipeline);
|
|
157
|
+
renderPass.setBindGroup(0, cameraBindGroup);
|
|
158
|
+
renderPass.setBindGroup(1, selectionBindGroup);
|
|
159
|
+
renderPass.setVertexBuffer(0, selectionMesh.vertexBuffer);
|
|
160
|
+
renderPass.draw(selectionMesh.vertexCount);
|
|
161
|
+
|
|
162
|
+
renderPass.end();
|
|
163
|
+
device.queue.submit([commandEncoder.finish()]);
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## 更新场景而无需逐帧重新录制
|
|
168
|
+
|
|
169
|
+
每帧创建新的渲染包编码器会抵消渲染包的优势。仅在录制的命令或其引用资源变化时重新录制:
|
|
170
|
+
|
|
171
|
+
- 物体新增、移除、重排,或某一物体更换网格、管线或绑定组时。
|
|
172
|
+
- 渲染包使用的缓冲区、纹理或绑定组被替换或销毁时。
|
|
173
|
+
- 渲染通道的颜色格式、深度模板格式或采样数量改变时。
|
|
174
|
+
- 设备丢失并创建设备时。
|
|
175
|
+
|
|
176
|
+
以下变化无需新建渲染包:
|
|
177
|
+
|
|
178
|
+
- 用 `device.queue.writeBuffer()` 向渲染包已使用的缓冲区写入新变换、颜色或摄像机矩阵。
|
|
179
|
+
- 画布调整大小,因为视口和剪裁矩形取自渲染通道。按新尺寸重新创建深度纹理或多重采样纹理,保持原格式和采样数量。
|
|
180
|
+
|
|
181
|
+
## 在 GPU 上剔除物体
|
|
182
|
+
|
|
183
|
+
摄像机穿过大型场景时,计算着色器可判断每个物体是否处在摄像机视锥内,并在 GPU 上生成可见物体列表。间接绘制随后从 GPU 缓冲区读取实例数量,所以每帧的绘制命令保持不变,可以录入渲染包:
|
|
184
|
+
|
|
185
|
+
- 用 `GPUBufferUsage.INDIRECT | GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST` 创建绘制参数缓冲区,并一次写入网格的索引数量。
|
|
186
|
+
- 每帧在计算通道前把实例数量重置为 0。
|
|
187
|
+
- 每个网格用一次独立的间接绘制和各自的参数。核心 WebGPU 没有多重间接绘制调用。
|
|
188
|
+
- 保持 `firstInstance` 为 0,因为非零值需要可选特性 `indirect-first-instance`。改为通过可见列表查找各物体。
|
|
189
|
+
|
|
190
|
+
```wgsl
|
|
191
|
+
struct Bounds { center: vec3f, radius: f32 };
|
|
192
|
+
struct DrawArgs { // Layout read by drawIndexedIndirect()
|
|
193
|
+
indexCount: u32,
|
|
194
|
+
instanceCount: atomic<u32>,
|
|
195
|
+
firstIndex: u32,
|
|
196
|
+
baseVertex: i32,
|
|
197
|
+
firstInstance: u32,
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
@group(0) @binding(0) var<storage, read> bounds: array<Bounds>;
|
|
201
|
+
@group(0) @binding(1) var<uniform> planes: array<vec4f, 6>; // Camera frustum planes
|
|
202
|
+
@group(0) @binding(2) var<storage, read_write> visible: array<u32>;
|
|
203
|
+
@group(0) @binding(3) var<storage, read_write> args: DrawArgs;
|
|
204
|
+
|
|
205
|
+
@compute @workgroup_size(64)
|
|
206
|
+
fn cull(@builtin(global_invocation_id) id: vec3u) {
|
|
207
|
+
let i = id.x;
|
|
208
|
+
if (i >= arrayLength(&bounds)) { return; }
|
|
209
|
+
for (var p = 0u; p < 6u; p++) {
|
|
210
|
+
if (dot(planes[p].xyz, bounds[i].center) + planes[p].w < -bounds[i].radius) { return; }
|
|
211
|
+
}
|
|
212
|
+
visible[atomicAdd(&args.instanceCount, 1u)] = i;
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
顶点着色器用 `visible[instanceIndex]` 从可见列表读取物体索引,其中 `instanceIndex` 是 `@builtin(instance_index)`。顶点着色器不能使用可写存储缓冲区,应把列表声明为只读,避免复制计算着色器的 `read_write` 声明:
|
|
217
|
+
|
|
218
|
+
```wgsl
|
|
219
|
+
@group(0) @binding(0) var<storage, read> visible: array<u32>; // In the vertex shader
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
渲染包为该网格录制 `setIndexBuffer()`,随后录制 `drawIndexedIndirect()`。JavaScript 每帧写入新的视锥平面、运行计算通道并重放包含 `drawIndexedIndirect(argsBuffer, 0)` 的渲染包:
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
device.queue.writeBuffer(planesBuffer, 0, frustumPlanes);
|
|
226
|
+
device.queue.writeBuffer(argsBuffer, 4, new Uint32Array([0])); // Reset instanceCount
|
|
227
|
+
|
|
228
|
+
const commandEncoder = device.createCommandEncoder();
|
|
229
|
+
const cullPass = commandEncoder.beginComputePass();
|
|
230
|
+
cullPass.setPipeline(cullPipeline);
|
|
231
|
+
cullPass.setBindGroup(0, cullBindGroup);
|
|
232
|
+
cullPass.dispatchWorkgroups(Math.ceil(objectCount / 64));
|
|
233
|
+
cullPass.end();
|
|
234
|
+
|
|
235
|
+
const renderPass = commandEncoder.beginRenderPass(renderPassDescriptor);
|
|
236
|
+
renderPass.executeBundles([sceneBundle]); // Draws only the visible objects
|
|
237
|
+
renderPass.end();
|
|
238
|
+
device.queue.submit([commandEncoder.finish()]);
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## 处理 GPU 错误和设备丢失
|
|
242
|
+
|
|
243
|
+
即使特性检测成功,GPU 初始化和执行也仍可能失败。大多数 WebGPU 调用不会抛出异常。`createBuffer()`、`createBindGroup()`、`dispatchWorkgroups()` 或 `draw()` 等编码器命令以及 `submit()` 会把验证错误和内存不足错误报告给错误作用域或设备的 `uncapturederror` 事件,并返回无效对象而不抛错。无效的提交会被丢弃,而 `mapAsync()` 仍可能以陈旧数据或零值兑现。一个例外是 `device.queue.writeBuffer()`:写入字节数不是 4 的倍数时会抛出 `OperationError`,因此需要填充 `f16` 数据。遇到任一情况,应丢弃结果并切换到回退方案,计算改用 CPU 实现,图形改用另一渲染器:
|
|
244
|
+
|
|
245
|
+
- **初始化失败**:`createGpuDevice()` 返回 `null`,`createComputePipelineAsync()` 或 `createRenderPipelineAsync()` 被拒绝,或者包裹缓冲区和绑定组创建的错误作用域报告错误。
|
|
246
|
+
- **数据超出设备限制**:缓冲区超过 `maxBufferSize`,或绑定超出 `maxStorageBufferBindingSize`,会产生验证错误。分配前检查尺寸。
|
|
247
|
+
- **GPU 内存不足**:分配失败时,通过 `'out-of-memory'` 错误作用域报告 `GPUOutOfMemoryError`。
|
|
248
|
+
- **单次运行失败**:将每轮的 `encoder.finish()` 和 `submit()` 包在 `'validation'` 错误作用域中,因为无效的编码器命令会在编码器完成时报告。若作用域报告错误,则丢弃结果。设备丢失时 `mapAsync()` 被拒绝,而 `popErrorScope()` 会兑现为 `null`。
|
|
249
|
+
- **错误未被任何错误作用域捕获**:错误会通过 `uncapturederror` 事件报告。
|
|
250
|
+
- **设备丢失**(驱动程序重置或 GPU 进程崩溃):`device.lost` 兑现。切换到回退方案,或使用新适配器创建新设备。
|
|
251
|
+
|
|
252
|
+
```js
|
|
253
|
+
// Setup: check limits, then capture allocation and validation errors.
|
|
254
|
+
const maxBytes = Math.min(device.limits.maxBufferSize, device.limits.maxStorageBufferBindingSize);
|
|
255
|
+
if (data.byteLength > maxBytes) return useFallback();
|
|
256
|
+
|
|
257
|
+
device.pushErrorScope('out-of-memory');
|
|
258
|
+
device.pushErrorScope('validation');
|
|
259
|
+
const dataBuffer = device.createBuffer({
|
|
260
|
+
size: data.byteLength,
|
|
261
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
262
|
+
});
|
|
263
|
+
const validationError = await device.popErrorScope(); // Scopes pop in reverse order
|
|
264
|
+
const oomError = await device.popErrorScope();
|
|
265
|
+
if (validationError || oomError) return useFallback();
|
|
266
|
+
device.queue.writeBuffer(dataBuffer, 0, data); // Write only to a buffer known to be valid
|
|
267
|
+
|
|
268
|
+
// Each run: an invalid command buffer is dropped without an exception.
|
|
269
|
+
device.pushErrorScope('validation');
|
|
270
|
+
device.queue.submit([encoder.finish()]);
|
|
271
|
+
if (await device.popErrorScope()) return useFallback();
|
|
272
|
+
|
|
273
|
+
// Errors outside any error scope, and device loss. One failure can trigger several of
|
|
274
|
+
// these (a single invalid pass reports errors from both finish() and submit()), so react
|
|
275
|
+
// only while this device is still the active one.
|
|
276
|
+
device.addEventListener('uncapturederror', () => {
|
|
277
|
+
if (gpu?.device === device) useFallback();
|
|
278
|
+
});
|
|
279
|
+
device.lost.then((info) => {
|
|
280
|
+
if (info.reason !== 'destroyed' && gpu?.device === device) useFallback();
|
|
281
|
+
});
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
在上面的片段中,初始化成功后 `gpu` 保存当前的 GPU 资源。`useFallback()` 会切换到回退、销毁设备并把 `gpu` 置为 `null`。使用标志记录回退是否已激活等方式,使得重复调用也是安全的。
|
|
285
|
+
|
|
286
|
+
只能在录制该包的设备上使用渲染包。设备丢失后创建新设备时,要重新创建管线、缓冲区和绑定组,并重新录制渲染包。已具有 WebGPU 上下文的画布要切换到回退渲染器时,应将其替换为新的 `<canvas>` 元素。
|
|
287
|
+
|
|
288
|
+
## 结束前验证
|
|
289
|
+
|
|
290
|
+
测试结果。逐项检查下列情况,发现失败后修复并重新测试:
|
|
291
|
+
|
|
292
|
+
- **无 WebGPU**:WebGPU 不可用或 `requestAdapter()` 兑现为 `null` 时,回退渲染器仍能绘制场景。
|
|
293
|
+
- **初始化失败**:`requestDevice()` 或创建管线失败时,页面显示回退渲染器的结果,不停留在加载状态。
|
|
294
|
+
- **数据更新**:通过 `writeBuffer()` 更改物体的位置或颜色后,屏幕会显示更新,无需重新录制渲染包。
|
|
295
|
+
- **场景改变**:新增或移除物体时只录制一次渲染包,避免逐帧重录。
|
|
296
|
+
- **GPU 剔除**:场景在 GPU 进行剔除时,绘制视野内物体并忽略视野外物体。结果应随摄像机移动,无需用 JavaScript 遍历物体。
|
|
297
|
+
- **帧时间**:以现实的物体数量实测时,每帧 JavaScript 耗时低于使用普通绘制调用。
|
|
298
|
+
- **无障碍**:画布有文本替代,例如带 `aria-label` 的 `role="img"`。匹配 `prefers-reduced-motion: reduce` 时停止连续动画。
|
|
299
|
+
|
|
300
|
+
## 其他解决方案
|
|
301
|
+
|
|
302
|
+
一些开源渲染引擎可以直接使用这些 WebGPU 功能,并在 WebGPU 不可用时采用 WebGL 2 渲染:
|
|
303
|
+
|
|
304
|
+
- **Babylon.js**(3D):快照渲染将场景录制为渲染包,计算着色器可更新或剔除实例。
|
|
305
|
+
- **PixiJS**(2D):其 WebGPU 渲染器能将场景的静态部分录制为渲染包。
|
|
306
|
+
- **PlayCanvas**(3D):计算着色器可写入间接绘制参数,用于 GPU 剔除及 GPU 更新实例。
|
|
307
|
+
- **Three.js**(3D):其 WebGPU 渲染器中的 `BundleGroup` 会把包含的物体录制为渲染包,计算着色器可写入间接绘制参数。
|
|
308
|
+
|
|
309
|
+
应用已用其中一个引擎时,使用其功能即可,无需自行录制渲染包或编写剔除着色器。遵循原生路径同样的做法:保持录制对象在帧之间稳定,更新数据而不重新录制,并保留回退。
|
|
310
|
+
|
|
311
|
+
## 回退策略
|
|
312
|
+
|
|
313
|
+
### WebGPU 回退
|
|
314
|
+
|
|
315
|
+
WebGPU 的浏览器支持:可用性有限。
|
|
316
|
+
支持:Chrome 144(2026 年 1 月)、Edge 144(2026 年 1 月)和 Safari 26(2025 年 9 月)。
|
|
317
|
+
不支持:Firefox。
|
|
318
|
+
|
|
319
|
+
当 `'gpu' in navigator` 为假、`requestAdapter()` 兑现为 `null`,或 GPU 初始化或渲染失败时,使用 WebGL 2 绘制场景。具体损失取决于场景:
|
|
320
|
+
|
|
321
|
+
- **同一网格的大量副本**:没有损失。使用 `gl.drawArraysInstanced()` 或 `gl.drawElementsInstanced()` 绘制。
|
|
322
|
+
- **数千个不同物体**:WebGL 2 没有渲染包,因此 JavaScript 每帧发出所有绘制调用。减少调用次数的方法包括按材质排序、把共享材质的静态网格合并到一个缓冲区、对重复网格使用实例化。支持 `WEBGL_multi_draw` 扩展时,可以一次调用绘制多个范围。
|
|
323
|
+
- **在 GPU 更新的实例**:在 JavaScript 中更新实例数据,再用 `gl.bufferSubData()` 每帧上传,或者使用变换反馈在 GPU 计算。
|
|
324
|
+
- **GPU 剔除**:WebGL 2 没有计算着色器或间接绘制,因此回到 CPU 判断,例如基于层次包围体剔除,并仅绘制可见物体。为避免阻塞主线程,可用 `OffscreenCanvas` 在 Worker 中渲染。
|
|
325
|
+
|
|
326
|
+
对于静态 2D 图层或连 WebGL 2 也不可用时,可回退到带缓存图层的 2D 画布。把图层绘制到离屏画布一次(可用 `OffscreenCanvas` 或未加入页面的 `<canvas>`),再用一次 `drawImage()` 复制到可见画布。仅在图层变化时重绘缓存。逐帧变化的图层不适用。
|
|
327
|
+
|
|
328
|
+
回退方案会继续显示场景,但大型场景的帧率较低。
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
function createLayerCache(width, height, drawLayer) {
|
|
332
|
+
const cache = new OffscreenCanvas(width, height);
|
|
333
|
+
drawLayer(cache.getContext('2d')); // Draw thousands of shapes once
|
|
334
|
+
return cache;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Redraw the visible canvas only when the scene or the layer changes.
|
|
338
|
+
function drawFromCache(ctx, cache) {
|
|
339
|
+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
340
|
+
ctx.drawImage(cache, 0, 0);
|
|
341
|
+
}
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### `WEBGL_multi_draw` 回退
|
|
345
|
+
|
|
346
|
+
`WEBGL_multi_draw` WebGL 扩展的浏览器支持:可用性有限。
|
|
347
|
+
支持:Chrome 86(2020 年 10 月)、Edge 86(2020 年 10 月)和 Safari 15(2021 年 9 月)。
|
|
348
|
+
不支持:Firefox。
|
|
349
|
+
|
|
350
|
+
`gl.getExtension('WEBGL_multi_draw')` 返回 `null` 时,使用 `gl.drawElements()` 逐次绘制,或对重复网格使用 `gl.drawElementsInstanced()`。
|
|
@@ -204,6 +204,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
204
204
|
}
|
|
205
205
|
```
|
|
206
206
|
|
|
207
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
208
|
+
|
|
209
|
+
```js
|
|
210
|
+
// Good:
|
|
211
|
+
const isOpen = element.matches(':popover-open');
|
|
212
|
+
// Bad:
|
|
213
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
214
|
+
```
|
|
215
|
+
|
|
207
216
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
208
217
|
|
|
209
218
|
```css
|
|
@@ -109,6 +109,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
109
109
|
}
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
// Good:
|
|
116
|
+
const isOpen = element.matches(':popover-open');
|
|
117
|
+
// Bad:
|
|
118
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
119
|
+
```
|
|
120
|
+
|
|
112
121
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
113
122
|
|
|
114
123
|
```css
|
package/upstream-skills/zh-modern-web-guidance/guides/ui-behaviors/animate-to-from-top-layer.md
CHANGED
|
@@ -264,6 +264,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
264
264
|
}
|
|
265
265
|
```
|
|
266
266
|
|
|
267
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
268
|
+
|
|
269
|
+
```js
|
|
270
|
+
// Good:
|
|
271
|
+
const isOpen = element.matches(':popover-open');
|
|
272
|
+
// Bad:
|
|
273
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
274
|
+
```
|
|
275
|
+
|
|
267
276
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
268
277
|
|
|
269
278
|
```css
|
|
@@ -164,6 +164,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
164
164
|
}
|
|
165
165
|
```
|
|
166
166
|
|
|
167
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
// Good:
|
|
171
|
+
const isOpen = element.matches(':popover-open');
|
|
172
|
+
// Bad:
|
|
173
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
174
|
+
```
|
|
175
|
+
|
|
167
176
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
168
177
|
|
|
169
178
|
```css
|
package/upstream-skills/zh-modern-web-guidance/guides/ui-behaviors/interest-triggered-tooltips.md
CHANGED
|
@@ -210,6 +210,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
210
210
|
}
|
|
211
211
|
```
|
|
212
212
|
|
|
213
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
// Good:
|
|
217
|
+
const isOpen = element.matches(':popover-open');
|
|
218
|
+
// Bad:
|
|
219
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
220
|
+
```
|
|
221
|
+
|
|
213
222
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
214
223
|
|
|
215
224
|
```css
|
|
@@ -41,6 +41,17 @@ CSS `scroll-initial-target` 提供声明式纯 CSS 方法,在滚动容器一
|
|
|
41
41
|
|
|
42
42
|
<a id="fallback-strategy"></a>
|
|
43
43
|
|
|
44
|
+
## 无障碍注意事项
|
|
45
|
+
|
|
46
|
+
`scroll-initial-target` 只更新视觉上的滚动偏移,因此应让键盘和读屏用户可以直接把焦点移到视觉上优先呈现的内容。页面前面已有页眉、导航等内容时,初始加载不应自动调用 `focus()`,否则会跳过前面的地标。
|
|
47
|
+
|
|
48
|
+
选择符合组件语义的焦点模式:
|
|
49
|
+
|
|
50
|
+
* **复合控件**(如从今天开始的日期选择条、缩略图 `toolbar`、可选择的 `listbox` 或 `tablist`):使用与行为匹配的 `focusgroup` 标记,如 `focusgroup="listbox"` 或 `focusgroup="toolbar"`。如果激活项可由外部改变,则添加 `nomemory` 并移动 `focusgroupstart`。在 `scroll-initial-target` 控件上设置 `focusgroupstart`,使 Tab 进入控件时直接落在优先项上,用户无需跳转链接也可用方向键在项目间移动。
|
|
51
|
+
* **内容流和消息流**(如实时聊天或媒体流,无论嵌入更大页面还是作为主视图):对于没有复合控件角色的静态正文项,不要使用 `focusgroup` 或游走式 `tabindex`。让优先项通过 `tabindex="-1"` 支持程序聚焦,也可改为聚焦其标题或首个控件。可在内容流前设置跳转链接,或在周围的导航界面加跳转控件,保留正常顺序的 `Tab` 导航。
|
|
52
|
+
|
|
53
|
+
优先内容不是预期的起点时,例如聊天中置顶的旧消息而非最新消息,应使用可见或无障碍文本说明为何先显示它。可以使用“已置顶消息”标签,避免只有无标签的图钉图标。
|
|
54
|
+
|
|
44
55
|
## 回退策略
|
|
45
56
|
|
|
46
57
|
scroll-initial-target 的浏览器支持:有限可用。
|
|
@@ -64,3 +75,21 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
|
64
75
|
}
|
|
65
76
|
});
|
|
66
77
|
```
|
|
78
|
+
|
|
79
|
+
### focusgroup 的回退和浏览器支持
|
|
80
|
+
|
|
81
|
+
focusgroup 的浏览器支持:可用性有限。
|
|
82
|
+
支持:Chrome 150(2026 年 6 月)和 Edge 150(2026 年 7 月)。
|
|
83
|
+
不支持:Firefox 和 Safari。
|
|
84
|
+
|
|
85
|
+
仅在缺少原生支持时加载 `@microsoft/focusgroup-polyfill`:
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
if (!(Object.hasOwn(HTMLElement.prototype, 'focusGroup'))) {
|
|
89
|
+
const { polyfill } = await import('@microsoft/focusgroup-polyfill');
|
|
90
|
+
polyfill();
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- 使用 `focusgroup` 和 `focusgroupstart` 属性;polyfill 不会添加 `focusGroup` 属性。
|
|
95
|
+
- polyfill 无法感知之后才隐藏或显示的项目;在某项隐藏期间为其添加 `focusgroup="none"`。
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
* **必须:** 每个可排序 `<th scope="col">` 内放 `<button type="button">`。不要让 `<th>` 自身可点击,不用 `<a>` 或 `role="button"`。
|
|
16
16
|
* 仅在当前排序列的 `<th>` 上设置 `aria-sort="ascending"` 或 `aria-sort="descending"`,并从其他表头移除该属性。不要设置 `aria-sort="none"`,也不要把 `aria-sort` 放到按钮上。
|
|
17
|
+
* 为排序按钮设置 `aria-description="Sortable"`。这有助于理解按钮用途,同时避免影响读屏用户浏览表格时听到的列名。
|
|
17
18
|
* 不可排序列保持普通 `<th>` 文本,无按钮。其内边距、对齐、字重与可排序标题一致;只对 `th:has(> button)` 去掉单元格内边距,因为按钮已提供。
|
|
18
19
|
* 保留 `<caption>` 作为无障碍名称,数据行放在 `<tbody>`,确保只排正文而不动 `<thead>`。
|
|
19
20
|
* 排序方向使用装饰图标(`aria-hidden="true"`、`currentColor`,参见 `icons`,通过 `npx -y modern-web-guidance@latest retrieve "icons"` 获取)。由 CSS `th[aria-sort]` 驱动外观,不用额外类或 JS。图标只视觉可见,因此始终通过 `<th>` 上的 `aria-sort` 向辅助技术传达排序状态。
|
|
@@ -177,6 +177,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
177
177
|
}
|
|
178
178
|
```
|
|
179
179
|
|
|
180
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
// Good:
|
|
184
|
+
const isOpen = element.matches(':popover-open');
|
|
185
|
+
// Bad:
|
|
186
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
187
|
+
```
|
|
188
|
+
|
|
180
189
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
181
190
|
|
|
182
191
|
```css
|
|
@@ -178,6 +178,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
178
178
|
}
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
182
|
+
|
|
183
|
+
```js
|
|
184
|
+
// Good:
|
|
185
|
+
const isOpen = element.matches(':popover-open');
|
|
186
|
+
// Bad:
|
|
187
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
188
|
+
```
|
|
189
|
+
|
|
181
190
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
182
191
|
|
|
183
192
|
```css
|
|
@@ -233,6 +233,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
233
233
|
}
|
|
234
234
|
```
|
|
235
235
|
|
|
236
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
237
|
+
|
|
238
|
+
```js
|
|
239
|
+
// Good:
|
|
240
|
+
const isOpen = element.matches(':popover-open');
|
|
241
|
+
// Bad:
|
|
242
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
243
|
+
```
|
|
244
|
+
|
|
236
245
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
237
246
|
|
|
238
247
|
```css
|
|
@@ -166,7 +166,7 @@ dialog::backdrop {
|
|
|
166
166
|
- **要**用具有无障碍名称的原生 `<button>` 作为关闭控件。
|
|
167
167
|
- **要**让对话框标题使用一致的标题级别。
|
|
168
168
|
- **要**在模态对话框中为主标题使用一级标题,因为它是该作用域内的第一个标题。
|
|
169
|
-
- **要**确保非模态对话框显示在没有内容或内容极少的位置。为符合 WCAG 2.4.11
|
|
169
|
+
- **要**确保非模态对话框显示在没有内容或内容极少的位置。为符合 WCAG 2.4.11,确保获得焦点的元素不会被非模态对话框完全遮挡。
|
|
170
170
|
|
|
171
171
|
<a id="fallback-strategies"></a>
|
|
172
172
|
|
|
@@ -259,6 +259,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
259
259
|
}
|
|
260
260
|
```
|
|
261
261
|
|
|
262
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
263
|
+
|
|
264
|
+
```js
|
|
265
|
+
// Good:
|
|
266
|
+
const isOpen = element.matches(':popover-open');
|
|
267
|
+
// Bad:
|
|
268
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
269
|
+
```
|
|
270
|
+
|
|
262
271
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
263
272
|
|
|
264
273
|
```css
|
|
@@ -255,6 +255,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
255
255
|
}
|
|
256
256
|
```
|
|
257
257
|
|
|
258
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
259
|
+
|
|
260
|
+
```js
|
|
261
|
+
// Good:
|
|
262
|
+
const isOpen = element.matches(':popover-open');
|
|
263
|
+
// Bad:
|
|
264
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
265
|
+
```
|
|
266
|
+
|
|
258
267
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
259
268
|
|
|
260
269
|
```css
|
|
@@ -392,6 +392,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
392
392
|
}
|
|
393
393
|
```
|
|
394
394
|
|
|
395
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
396
|
+
|
|
397
|
+
```js
|
|
398
|
+
// Good:
|
|
399
|
+
const isOpen = element.matches(':popover-open');
|
|
400
|
+
// Bad:
|
|
401
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
402
|
+
```
|
|
403
|
+
|
|
395
404
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
396
405
|
|
|
397
406
|
```css
|
|
@@ -256,6 +256,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
256
256
|
}
|
|
257
257
|
```
|
|
258
258
|
|
|
259
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
260
|
+
|
|
261
|
+
```js
|
|
262
|
+
// Good:
|
|
263
|
+
const isOpen = element.matches(':popover-open');
|
|
264
|
+
// Bad:
|
|
265
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
266
|
+
```
|
|
267
|
+
|
|
259
268
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
260
269
|
|
|
261
270
|
```css
|
|
@@ -119,6 +119,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
119
119
|
}
|
|
120
120
|
```
|
|
121
121
|
|
|
122
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
// Good:
|
|
126
|
+
const isOpen = element.matches(':popover-open');
|
|
127
|
+
// Bad:
|
|
128
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
129
|
+
```
|
|
130
|
+
|
|
122
131
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
123
132
|
|
|
124
133
|
```css
|
|
@@ -81,6 +81,15 @@ if (!Object.hasOwn(HTMLElement.prototype, "popover")) {
|
|
|
81
81
|
}
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
+
该 polyfill 修改文档和元素的 `querySelector`、`querySelectorAll`、`matches` 和 `closest`,使查询 `:popover-open` 时也匹配 `.\:popover-open` 类,因此只需写前者。
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
// Good:
|
|
88
|
+
const isOpen = element.matches(':popover-open');
|
|
89
|
+
// Bad:
|
|
90
|
+
const isOpen = element.matches(':popover-open, .\:popover-open');
|
|
91
|
+
```
|
|
92
|
+
|
|
84
93
|
为防止 polyfill 加载前显示浮层,请用 CSS 将它们隐藏:
|
|
85
94
|
|
|
86
95
|
```css
|