Repository navigation
perf(render): pin the frame pass's fragment shader per kind on Windows - #1140
Conversation
…es pinned to their kind, so a fragment no longer pays for every kind's code (Windows, from precompiled bytecode)
…ts/precompile-metal-shaders.sh on air); the native built-in libraries regenerated from dev's other shaders with them
…he WGSL pinned_wgsl gives now; a span of many short runs draws in one call; the pinned pipelines go with the frame pipeline - Each pinned fragment's source hash is recorded by regen_dxbc (uber/dxbc_pinned_hash.rs) and checked at load. A stale one isn't used: the general shader draws that kind, and stderr says which file is stale and how to rebuild it. build.rs warns when the Windfoil WGSL moved since. regen_dxbc is the one documented way to regenerate. - A span with more than 64 runs, or fewer than 8 instances a run, draws in one call with the general shader (provisional numbers). - When every pinned pipeline is refused nothing is split, and the refusal says what draws instead. - set_format and adopt make the pinned pipelines beside the frame pipeline, kept per format, not on the next visible frame. - FRAGMENT_PINNED's order is tied to PINNED at compile time. Regenerated with regen_dxbc: every .dxbc file is byte-identical.
…de, checked by the build; RUNE_PINNED forces the split on or off in frame-profile builds - regen_dxbc records the BLAKE3 of uber.vs.dxbc and of each uber.fs-k*.dxbc with the WGSL each came from (uber/dxbc_pinned_hash.rs). build.rs hashes those files and the three pinned WGSL sources and emits them (render/uber_wgsl.rs); render/dxbc.rs `PINNED_FRESH` compares the two at compile time. A fragment from another shader, another wgpu or another vertex stage, a hand-edited hash or a swapped file isn't used, and nothing is hashed at run time. - build.rs's warning says that Windows scrolling draws on the slow path until regen_dxbc is run. - Frame-profile builds: RUNE_PINNED=all splits every span whatever its runs, RUNE_PINNED=off draws every span with the general shader (the screen comparison runs both), and every 60th frame says how many instances each way drew; "rune-warm pinned pipelines" says what making them cost and what the old run-time check cost. Regenerated with regen_dxbc: every .dxbc file is byte-identical.
… general shader draws until they arrive On a cold driver cache (the first launch of an exe path, as after an install or a driver update) the driver took 152.5 ms to make the three on blade's Iris (2.1 ms warm); made at the first prepare, that landed on the hand-over frame. Now `start_pinned` hands them to a thread (as the subpixel pipeline is built) and `take_pinned` takes them at a later prepare, for the format they were made for. The picture is the same either way (forced on and forced off both compare byte-identical with dev). set_format and adopt start them the same way; a format left meanwhile drops its set.
…=all waits for them; the build's check is positional - The thread that makes the pinned pipelines runs at THREAD_PRIORITY_BELOW_NORMAL: on a two-core machine a cold driver cache makes it a 150 ms compile, which mustn't compete with the first input. - Frame-profile builds with RUNE_PINNED=all block in take_pinned until the pipelines arrive, so every frame a screen comparison captures draws pinned; no other build ever waits. - build.rs compares each recorded hash with its own kind's (a swapped file now warns at build time too, as it was already refused at run time), and its warning says what it checks: this shader, beside this uber.vs.dxbc, the files regen_dxbc wrote.
|
Windows whole suite ( |
There was a problem hiding this comment.
🟡 Changes recommended
The new run-selection helpers lack regression tests for cutoff and range boundaries, and the pinning implementation needs extraction from the already oversized render module.
2 open issues
What changed in this PR
This PR reduces Windows frame-pass shader cost by adding specialized pipelines for rectangles, shadows, and glyphs while preserving paint order and general-shader fallback.
Changes:
- Build specialized pipelines asynchronously from precompiled DXBC.
- Split eligible batches into same-kind runs, with limits to avoid excessive draw calls.
- Validate shader bytecode freshness at build time and extend regeneration tooling.
| File | Description |
|---|---|
crates/rune/src/shader.wgsl |
Adds compile-time fragment-kind selection. |
crates/rune/src/render/variant.rs |
Builds and collects specialized pipelines asynchronously. |
crates/rune/src/render/upload.rs |
Tracks instance kinds and polls pipeline readiness. |
crates/rune/src/render/uber/windfoil_hash.rs |
Refreshes the Windfoil source hash. |
crates/rune/src/render/uber/metallib_hash.rs |
Updates Metal shader source hashes. |
crates/rune/src/render/uber/dxbc_pinned_hash.rs |
Records specialized shader and bytecode hashes. |
crates/rune/src/render/uber/dxbc_hash.rs |
Updates general and subpixel shader hashes. |
crates/rune/src/render/pipelines.rs |
Initializes specialization state and caches pipelines by format. |
crates/rune/src/render/dxbc.rs |
Loads fresh specialized bytecode with general-shader fallback. |
crates/rune/src/render/backdrop_blur.rs |
Supplies explicit fallback text to the shader loader. |
crates/rune/src/render.rs |
Adds ordered run splitting, thresholds, and profiling counters. |
crates/rune/src/gpu/metal/builtin_table.rs |
Refreshes generated Metal shader lookup entries. |
crates/rune/examples/regen_dxbc.rs |
Generates specialized fragments and freshness records. |
crates/rune/build.rs |
Computes and checks specialized shader hashes during builds. |
Review effort: Balanced
| /// Most runs a span is split into (`few_runs`), and fewest instances a run has on average. | ||
| const MAX_RUNS: usize = 64; | ||
| const MIN_RUN: usize = 8; |
| fn few_runs(kinds: &[u8], span: std::ops::Range<u32>) -> bool { | ||
| let len = span.len(); | ||
| let runs = kind_runs(kinds, span).take(MAX_RUNS + 1).count(); | ||
| runs <= 1 || (runs <= MAX_RUNS && runs * MIN_RUN <= len) |

Every fragment of the frame pass runs Rune's uber shader, which carries the code for every kind of shape. On Intel's integrated GPUs that costs occupancy: the compiled fragment shader is 76,936 B, and every pixel of a rect or a glyph pays for gradients, images, layers, dashes and the rest. This pass draws rects, shadows and glyphs with pipelines whose fragment stage is pinned to that kind, so the compiler drops every other kind's code.
Shader:
fsreads its kind throughfragment_kind, which returns the compile-timePINNED_KINDwhen it's set (-1: every kind).variant::pinned_wgsl(kind)sets it; nothing else in the source changes.Order: nothing is reordered.
batch()records each instance's kind;draw_instancessplits each batch into runs of one kind, in paint order, and switches pipeline only where the kind changes. Overlap and blending are exactly today's.Where: the window's own frame pass only (
std::ptr::eq(frame_pipeline, &self.pipeline), no layer target, no additive layer, no subpixel-text pipeline). Everything else draws as before.Shipping: Windows only, from precompiled bytecode (
regen_dxbcwritesuber.fs-k0/k1/k2.dxbcbeside the shareduber.vs.dxbc, and the hash of the WGSL each came from: see "The pinned bytecode can't be used stale"). Never from WGSL at run time (FXC takes tens of seconds per variant). macOS, Linux, web, a stale hash: no pinned pipelines, nothing changes.Same bytes for everything else: the regenerated
uber.vs.dxbc,uber.fs.dxbcand the subpixel pair are byte-identical todev's, and so are the Metal uber and Windfoil libraries (only their recorded WGSL hashes move). Thebuiltin_native*Metal libraries are regenerated fromdev's other shaders by the same script run.When the pipelines are made: on a thread of their own, asked for at the renderer's first prepare and when the shader or format changes; the general shader draws until they arrive (see "Start-up").
GPU per scroll frame
blade's Iris Xe, SCALE's 10,000-message timeline, PRESENT's glide (a wheel step every 4,167 µs), frames that moved, D3D12 timestamps around every pass, private hidden desktop. This PR's own build: the pinned shaders as shipped, from
uber.fs-k0/k1/k2.dxbc(run fP r2).devThe measuring build that compiled the same pinning at run time gave 2,695 µs and 7,679 µs: the bytecode from FXC keeps the gain. Encode on the CPU: 90 → 93 µs a frame. A timeline frame has 36 runs of one kind at the median, 53 at most.
GPUI measured the same on
list(d17-spec-list): 2,322.9 → 1,342.4 µs with the shader cut to the two kindslistdraws.The pinned bytecode can't be used stale
regen_dxbcrecords, with the three files, the BLAKE3 of the WGSL each was compiled from (pinned_wgsl(kind)), of eachuber.fs-k*.dxbcand of theuber.vs.dxbcit was compiled beside (uber/dxbc_pinned_hash.rs).build.rstakes the same three hashes from the tree (the pinned WGSL from the joined Windfoil source, both files' bytes) and emits them (render/uber_wgsl.rs);dxbc::PINNED_FRESHcompares the two at compile time. Nothing is hashed at run time.shader.wgsl,pinned_wgslorPINNED; a regenerated vertex stage beside the old pinned three (which is how a wgpu or Naga upgrade shows: the existing test's regeneration path rewrites the vertex stage and the general fragment only); a hand-edited hash; a swapped file. The build compares each kind's hashes with its own record, so a swap warns at build time as well. A stale fragment isn't used: the general shader draws that kind (the same picture, slower), stderr says which file once, and the build warns that Windows scrolling draws on the slow path untilcargo run -p rune --example regen_dxbcis run.uber_dxbc_is_freshdoesn't cover the three files, and nothing in this PR changes a test. That is safe because a stale file is never used.regen_dxbcis the one documented way to regenerate (the hash file's header no longer offers the test path as an equal alternative; that path still writes its own header, which isn't ours to change).FRAGMENT_PINNED's order is tied toPINNEDby a compile-time check.Review fixes (
notes\review-1140.md)MAX_RUNS), or fewer than 8 instances a run on average (MIN_RUN). Both numbers are provisional until a highlighted diff with a selection and the terminal's full redraw are measured, before this leaves draft.set_format,adopt), and kept per format likeextra, so a switch back makes none. Only the renderer's firstprepare(the warm frame) makes them lazily.Start-up
start_pinned, as the subpixel pipeline is built), at below-normal priority (a cold compile mustn't compete with the first input), asked for at the renderer's first prepare and taken at a later one (try_recv, so no frame waits); the general shader draws every kind until they arrive, the same picture.dxbc::checkedpushes and pops scopes on the pinned thread, and wgpu 30 keys error scopes by thread (wgpu-30.0.1/src/backend/wgpu_core.rs:634,scopes: HashMap<ThreadId, Vec<ErrorScope>>;push_error_scopeandpop_error_scopeat:1845and:1860takeThreadId::current(); an error is routed to the scopes of the thread that raised it,:663), so they never interleave with the UI thread'sconfigure()or its ownchecked. Measured on blade's Iris (frame-profile build,rune-warm pinned pipelines): 152.5 ms for the three on a cold driver cache (the first launch of an exe path: an install or a driver update), 2.1 ms warm. On the frame path, the cold case would have landed on the hand-over frame (IB-3's Windows: the first GPU frames after the hand-over record on warmed D3D12 command allocators #1082 window, told in its note).PINNED_FRESHis settled at compile time).launch-bench.ts, timing tickets):dev31.3 ms p50 (41.9 p95), this PR atceaae7d733.3 ms (36.9): the first frame is the CPU painter's, before any GPU pipeline, so within noise. Cold runs at this head are queued.Bgra8Unorm, the window's own format on the Iris, sorebuild_renderer'sset_formatreturns at once. A blend switch (Tx::set_srgb_blend) would start a new set for the new format on the thread; the app never makes one (nothing in rune-desktop calls it).Subpixel text
Gates (head
07d73ce8: this PR merged with Runedev1505f921(#1136, #1130), no conflict; no shader,uber,build.rs,variant.rsordxbcfile changed ondev, so every pinned file stays valid. Native--all-features --all-targetsandwasm32-unknown-unknownchecks pass on adais)4382f594: 3,088 passed, 1 failed (the knownscroll_childrentest, as ondev); pasteboard 19 → 19. Owed at the merged head with the next suite pair.4382f594(lane, 13:26). Owed at the merged head with the next suite pair.render::andruntime::paint::lib tests at07d73ce8on adais (the same 107 names; proof17be0d61): 101 passed, 2 ignored, the same 4 failed. At243c8424(the 107 the two modules hold): 101 passed, 2 ignored, 4 failed, all four ondevtoo (train 1118d's list:render::breaker::tests::a_refused_blur_pipeline_draws_unblurred,render::layers::cache_tests::live_scale_zoom_sweep_buckets_during_motion_and_settles_crisp,render::layers::rebind_tests::a_texture_switch_binds_no_parked_target_until_one_returns,render::layers::tests::clipped_single_draw_fades_do_not_allocate_targets); proofb52883df.devwith the split forced on (RUNE_PINNED=all: 38,280 instances every 60 frames through the pinned pipelines, none unsplit) and forced off (none split).dev3ef98871against243c8424: the 10 screens that differed in the first 122-screen run, each captured on both builds back to back per screen and scale, each from a freshly seeded fixture server, both builds into the same folder (one fixture port); each capture waits on content through the app's own tree: the git branch control a branch, not "No branch", or both taken again (at most three tries; then masked and named). Forced on: 39 of 40 pairs identical (one,session-md-blocks, outside the markdown line that holds an inline image, decoded in one capture only); forced off: 40 of 40. The one left,new-session-worktreelight at 1x, differs by 4,244 px (max 149 levels) in the worktree picker's project row, where one capture shows the branch tag "main" and the other none: git hadn't answered after three tries, content, not shading. Its differing pixels lie in 338,192 to 921,487 of 1280x800, the worktree picker's project row and its "from main" / "from No branch" option beneath (the tag shifts the row's path text); that screen's other three variants are identical. Largest per-channel difference outside a mask anywhere else: 0.4382f594ondev18caec76); the merged head's full 488 is owed (a rerun at--jobs 1when blade's memory allows).dev3ef98871against243c8424, same pinned-content method, 38 screens x 2 themes, scales 1 and 1.5: 294 of 304 pairs identical; the other 10 differ only inside a content region named by the app's tree, 0 px outside it: the timeline's scroll offset (hs-mid,hs-narrow-top; dev ahead in one pair, this PR in the other), the review panel still loading in one capture (panel-review-empty,session-review-side-tabs), the git branch icon (session-inbox), the WSL dialog loading in one capture (dialog-wsl: 24 levels at most in the taller dialog's shadow, 0 outside the modal). Runsproof-farm\c271e852…(1x) and4397794e…(1.5x); verdictproof\frame-perf\gpu\ab\20261118-rtx\verdict.md.notes\review-1140.md), fixes in;4382f594confirmed "merge as is once its gates are in".Other platforms (not in this PR)
regen_dxbc); WGSL through FXC takes tens of secondsuber_windfoil.metallib(scripts/precompile-metal-shaders.sh); WGSL at run time through MSLpinned_wgsl(k)in the same script, loaded beside the Windfoil one invariant::pinned_pipelinespinned_wgsl(k)on the warm-up thread, and draw with them once they're inRUNE_GPU_LOG, Metal timestamps), as shipped against pinned, under the Mac lock.