Skip to content

perf: compile pipelines on first use and prewarm them in parallel - #177

Merged
proggeramlug merged 2 commits into
codex/rendering-wip-integrated-20260727from
perf/lazy-pipelines-startup
Nov 6, 2026
Merged

proggeramlug merged 2 commits into
codex/rendering-wip-integrated-20260727from
perf/lazy-pipelines-startup

Conversation

@proggeramlug

Copy link
Copy Markdown
Contributor

Stacked on #176.

Problem

Renderer::new created every pipeline of every feature up front, one after another. That's 57 pipelines, and 10.8 s on an M1 Max with a cold Metal shader cache: a user's first launch after installing, or after an OS or driver update. A game at the Medium preset used 16 of them. Path tracing, hardware-RT probes, the GPU-driven renderer and the rest compiled whether they ever ran or not.

Changes

  • renderer/lazy_pipeline.rs: LazyRenderPipeline and LazyComputePipeline keep an owned copy of their descriptor (modules and layouts are refcounted handles) and compile on first Deref. Each creation site changes one call, and use sites deref-coerce. Correctness doesn't depend on any list: whatever a frame draws is built on first use.
  • Renderer::prewarm_pipelines: when the settings that select pipelines change (the first frame, a quality preset, a feature toggled at runtime), the pipelines those settings use compile on background threads. A draw waits only for the pipeline it needs. The conditions come from a pipeline-use trace taken per preset (Off, Low, Medium, High, Ultra). Prewarming does nothing on wasm, where wgpu objects aren't Send.
  • BLOOM_PIPELINE_LOG=1: prints each compile with its time, and whether it ran on a prewarm thread or made a frame wait.
  • BRDF LUT at build time: build.rs includes brdf_lut.rs and writes the 256x256 table to OUT_DIR (1.7 s, only when the generator changes). Computing it at startup cost about 0.4 s on an M1 Max at every launch, and more on phones and the single-threaded web build. A test checks the embedded table against the generator.
  • Profiler: on tile-based GPUs consecutive passes overlap, so summing a label's intervals counted the same GPU time several times. The bloom mip chain read 5.4 ms for 0.8 ms of work, and 49 ms inside a 13 ms frame in a real scene. Per-label GPU time and the frame totals are now the union of the intervals, and the CPU totals too (phases nest). New tests cover both.

Measured (M1 Max; cold = MTL_SHADER_CACHE_SIZE=0)

before after
cold initWindow 11.4 s 0.5 s, then about 0.9 s in the first 3D frame
warm initWindow 0.6–0.9 s 0.33 s
tuichess 3D, Medium and High every pipeline it draws is prewarmed, none compiles on the render thread

Tests

  • cargo test --release --lib in native/shared: 489 passed. New tests cover the lazy pipelines (build once, prewarm with a racing first use), the union timing and the embedded BRDF table.
  • cargo check --target wasm32-unknown-unknown in native/web.
  • macOS build, and tuichess 3D played at Medium and High with screenshots checked.

Not in this PR

  • The atmosphere LUTs are still built at runtime (22 ms). Their sizes depend on the target through #[cfg], which a build script evaluates for the host.
  • Precompiled Metal shaders would remove the remaining cold compile, but wgpu 29 only supports pipeline caches on Vulkan.

Renderer::new created every pipeline of every feature up front, one after
another: 57 pipelines and 10.8 s on an M1 Max with a cold Metal shader cache
(first launch after install or an OS/driver update), of which a game at the
Medium preset used 16. Path tracing, hardware-RT probes, the GPU-driven
renderer and the rest compiled whether or not they ever ran.

- renderer/lazy_pipeline.rs: LazyRenderPipeline / LazyComputePipeline keep an
  owned copy of the descriptor (modules and layouts are refcounted handles)
  and compile on first Deref. Creation sites change one call; use sites
  deref-coerce. Correctness doesn't depend on any list: whatever a frame
  draws is built on first use.
- Renderer::prewarm_pipelines: when the settings that select pipelines
  change (first frame, a quality preset, a feature toggled at runtime), the
  pipelines those settings use compile on background threads; a draw waits
  only for the one it needs. Conditions measured per preset with a
  pipeline-use trace. No-op on wasm (wgpu objects aren't Send there).
- BLOOM_PIPELINE_LOG=1 prints each compile, its time and whether it ran on a
  prewarm thread or made a frame wait.

Measured (M1 Max, MTL_SHADER_CACHE_SIZE=0 for cold):
  cold initWindow 11.4 s -> 0.5 s, then ~0.9 s in the first 3D frame
  warm initWindow 0.6-0.9 s -> 0.33 s
  tuichess 3D at Medium and High: every pipeline it draws is prewarmed,
  none compiles on the render thread

BRDF LUT at build time: build.rs includes brdf_lut.rs and writes the
256x256 table to OUT_DIR once per build (1.7 s, only when the generator
changes); the renderer embeds it. Computing it at startup took ~0.4 s on an
M1 Max at every launch, more on phones and the single-threaded web build.
A test checks the embedded table against the generator.

Profiler: a label can cover several passes (the bloom mip chain), and on
tile-based GPUs consecutive passes overlap (the next pass's vertex work
starts while the previous pass's fragments run), so summing their intervals
counted the same GPU time several times: bloom read 5.4 ms for 0.8 ms of
work, 49 ms inside a 13 ms frame in a real scene. Per-label GPU time and
the frame totals are now the union of the intervals; CPU frame totals too
(phases nest: queue_submit runs inside render_total).
@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: da85447c-b26c-40df-a905-2232b46a7488

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@proggeramlug
proggeramlug changed the base branch from fix/apple-platforms-and-ffi to codex/rendering-wip-integrated-20260727 November 6, 2026 15:47
…0260727' into perf/lazy-pipelines-startup

# Conflicts:
#	native/shared/src/renderer/mod.rs
@proggeramlug
proggeramlug merged commit 30b3426 into codex/rendering-wip-integrated-20260727 Nov 6, 2026
1 check passed
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.

1 participant