Skip to content

perf(render): pin the frame pass's fragment shader per kind on Windows - #1140

Merged
Hona merged 8 commits into
devfrom
fp-kind-pipelines
Nov 18, 2026
Merged

Hona merged 8 commits into
devfrom
fp-kind-pipelines

Conversation

@Hona

@Hona Hona commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

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.

flowchart LR
  B[batch: instances in paint order] --> R[runs of one kind]
  R -->|KIND_RECT| P0[pinned rect pipeline<br/>11,488 B]
  R -->|KIND_SHADOW| P1[pinned shadow pipeline<br/>9,088 B]
  R -->|KIND_GLYPH| P2[pinned glyph pipeline<br/>28,852 B]
  R -->|any other kind| G[shared uber pipeline<br/>76,936 B]
Loading
  • Shader: fs reads its kind through fragment_kind, which returns the compile-time PINNED_KIND when it's set (-1: every kind). variant::pinned_wgsl(kind) sets it; nothing else in the source changes.

    const PINNED_KIND: f32 = -1.0;
    fn fragment_kind(i: Out) -> f32 {
        if (PINNED_KIND >= 0.0) { return PINNED_KIND; }
        return i.params.y;
    }
  • Order: nothing is reordered. batch() records each instance's kind; draw_instances splits 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_dxbc writes uber.fs-k0/k1/k2.dxbc beside the shared uber.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.dxbc and the subpixel pair are byte-identical to dev's, and so are the Metal uber and Windfoil libraries (only their recorded WGSL hashes move). The builtin_native* Metal libraries are regenerated from dev'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).

1440x960 p50 (p90) 2560x1440 p50 (p90)
dev 5,820 µs (6,934) 10,685 µs (11,254)
this PR 2,554 µs (5,125) 6,468 µs (7,229)
this PR, three swap-chain buffers (#1132) 2,498 µs (2,738); 235 frames drawn a second

The 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 kinds list draws.

The pinned bytecode can't be used stale

  • Each pinned fragment is tied to the exact bytes it was compiled from and beside. regen_dxbc records, with the three files, the BLAKE3 of the WGSL each was compiled from (pinned_wgsl(kind)), of each uber.fs-k*.dxbc and of the uber.vs.dxbc it was compiled beside (uber/dxbc_pinned_hash.rs).
  • build.rs takes 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_FRESH compares the two at compile time. Nothing is hashed at run time.
  • What that catches: a change to shader.wgsl, pinned_wgsl or PINNED; 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 until cargo run -p rune --example regen_dxbc is run.
  • uber_dxbc_is_fresh doesn't cover the three files, and nothing in this PR changes a test. That is safe because a stale file is never used. regen_dxbc is 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 to PINNED by a compile-time check.

Review fixes (notes\review-1140.md)

  • A span with many short runs draws in one call with the general shader: more than 64 runs (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.
  • If every pinned pipeline is refused, nothing is split, and the message says the general shader draws that kind (it never compiles WGSL on this path).
  • After a format or shader switch the pinned pipelines are made beside the frame pipeline (set_format, adopt), and kept per format like extra, so a switch back makes none. Only the renderer's first prepare (the warm frame) makes them lazily.

Start-up

  • The three pinned pipelines are made on a thread of their own (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.
  • Error scopes on that thread are its own: dxbc::checked pushes 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_scope and pop_error_scope at :1845 and :1860 take ThreadId::current(); an error is routed to the scopes of the thread that raised it, :663), so they never interleave with the UI thread's configure() or its own checked. 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).
  • The run-time freshness check before this head cost 0.20 to 0.34 ms each time the set was made; it's gone (PINNED_FRESH is settled at compile time).
  • Launch to first frame, warm, n=10 (launch-bench.ts, timing tickets): dev 31.3 ms p50 (41.9 p95), this PR at ceaae7d7 33.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.
  • Window creation builds nothing more: the prewarmed renderer is made for Bgra8Unorm, the window's own format on the Iris, so rebuild_renderer's set_format returns 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

  • A frame with subpixel text draws the whole pass with the subpixel pipeline, so pinning is off for it. That happens only when the user picks ClearType or RGB text in the app; the Windows default is neither. There are no usage numbers for those choices.
  • The subpixel pipeline doesn't get the split in this PR. It would need three more dual-source fragments from FXC (about a minute each to build, nothing at run time).

Gates (head 07d73ce8: this PR merged with Rune dev 1505f921 (#1136, #1130), no conflict; no shader, uber, build.rs, variant.rs or dxbc file changed on dev, so every pinned file stays valid. Native --all-features --all-targets and wasm32-unknown-unknown checks pass on adais)

  • Mac suite at 4382f594: 3,088 passed, 1 failed (the known scroll_children test, as on dev); pasteboard 19 → 19. Owed at the merged head with the next suite pair.
  • Windows suite at 4382f594 (lane, 13:26). Owed at the merged head with the next suite pair.
  • Rune's render:: and runtime::paint:: lib tests at 07d73ce8 on adais (the same 107 names; proof 17be0d61): 101 passed, 2 ignored, the same 4 failed. At 243c8424 (the 107 the two modules hold): 101 passed, 2 ignored, 4 failed, all four on dev too (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); proof b52883df.
  • Offscreen snapshot check, blade's Iris: 0 differing pixels against dev with the split forced on (RUNE_PINNED=all: 38,280 instances every 60 frames through the pinned pipelines, none unsplit) and forced off (none split).
  • Screens with the content pinned, blade's Iris, dev 3ef98871 against 243c8424: 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-worktree light 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.
  • The other 112 screens compared identical in the first run (all 488 pairs, at 4382f594 on dev 18caec76); the merged head's full 488 is owed (a rerun at --jobs 1 when blade's memory allows).
  • The RTX (adais, NVIDIA), dev 3ef98871 against 243c8424, 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). Runs proof-farm\c271e852… (1x) and 4397794e… (1.5x); verdict proof\frame-perf\gpu\ab\20261118-rtx\verdict.md.
  • The split limit's worst scenes, cold-cache frame times and launch: one timing ticket on blade's Iris (GPUI's list); owed, not a merge gate.
  • Independent reads (notes\review-1140.md), fixes in; 4382f594 confirmed "merge as is once its gates are in".

Other platforms (not in this PR)

how the uber shader gets to the GPU today what pinning needs there
Windows (DX12) precompiled DXBC (regen_dxbc); WGSL through FXC takes tens of seconds this PR
macOS (Metal) precompiled uber_windfoil.metallib (scripts/precompile-metal-shaders.sh); WGSL at run time through MSL the same split: three more fragment libraries from pinned_wgsl(k) in the same script, loaded beside the Windfoil one in variant::pinned_pipelines
Linux (Vulkan), web (WebGPU) WGSL at run time (Naga to SPIR-V or the browser's compiler: milliseconds) no precompiled step: make the three pinned pipelines from pinned_wgsl(k) on the warm-up thread, and draw with them once they're in
  • Whether the Mac pays the same per fragment is not measured yet. Apple's GPUs are tile-based and pay for register pressure differently from Intel's. Next: the timeline's main pass on air with Rune's GPU timer (RUNE_GPU_LOG, Metal timestamps), as shipped against pinned, under the Mac lock.

Hona added 4 commits November 18, 2026 02:59
…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.
Hona added 2 commits November 18, 2026 11:27
… 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.
@Hona

Hona commented Nov 18, 2026

Copy link
Copy Markdown
Member Author

Windows whole suite (-p rune -p rune-kit) on adais, judged against dev's own failures at 32eb7811 (this lane, whole suite) and eb5ae6c9 (FUNCTION's run, rune only) and 3468f3a3 and cca5df6f: #1140 4382f59 4382f594 (18 Nov 13:26): 2841 passed, 18 failed in 170 binaries; also on dev: alloc::disabled_trace_hooks_neither_format_nor_allocate, canvas_input::the_middle_button_drags_only_elements_that_take_it, rune::edit::tests::hidden_multiline_pristine_replace_and_delete, rune::edit::tests::pill_edge_cases_words_lines_bidi_and_direct_text_mutation, rune::edit::tests::the_caret_steps_over_a_pill_in_one_move, rune::input::keycaps::tests::repeated_chords_count_and_named_keys_use_glyphs, rune::native_surface::gpu_tests::mirrored_video_is_exact_cropped_centred_and_toggles_without_a_new_frame, rune::native_surface::gpu_tests::video_gpu_colour_sharpness_geometry_and_lifetime, rune::render::breaker::tests::a_refused_blur_pipeline_draws_unblurred, rune::render::layers::cache_tests::live_scale_zoom_sweep_buckets_during_motion_and_settles_crisp, rune::render::layers::rebind_tests::a_texture_switch_binds_no_parked_target_until_one_returns, rune::render::layers::tests::clipped_single_draw_fades_do_not_allocate_targets, rune::runtime::tests::layout_rows::send_flight_follows_the_measured_bubble_while_earlier_rows_land, rune::runtime::tests::picture_scale::a_changing_static_scale_keeps_the_glyph_atlas_bounded, rune::runtime::tests::picture_scale::text_inside_a_scale_draws_at_that_size, rune::runtime::tests::scroll_children::scroll_xy_with_direct_children_scrolls_both_axes_and_warns_on_trailing_growth_sibling, rune::text::macos::smoothing::tests::pipelines_never_share_masks, selectable_text::copy_select_all_and_the_context_menu; none new. Base 18caec76. Proof 1ef8f020.

@Hona
Hona marked this pull request as ready for review November 18, 2026 11:20
Copilot AI balanced review requested due to automatic review settings November 18, 2026 11:20
@Hona
Hona merged commit 78efd4a into dev Nov 18, 2026
3 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread crates/rune/src/render.rs
Comment on lines +1443 to +1445
/// 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;
Comment thread crates/rune/src/render.rs
Comment on lines +1449 to +1452
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)
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