Rune comes from app/vendor/rune now, a checkout of 63ad0209 on branch ported, carrying the four commits of upstream issues 1320 (a pause while a clip is layered holds the layer's picture and time) and 1325 (a video's sound is read ahead of the clock and leads it). Both were written against eb9f1104 and ported across the refactors since. The faults are the first two and the third in docs/rune-upstream.md; the app's workarounds in review.rs stay until the fixes arrive upstream.
59 KiB
01 — GPUI, Zed Text & Rendering Architecture, and Warp's UI Framework (warpui)
Sources inspected directly:
kitlangton/gpui(gpui 0.2.2+ Kit's commit2582af8, "Fix Metal display link fallback and rendering quality")zed-industries/zed(5447804, Oct 2026) —crates/gpui,crates/gpui_apple,crates/gpui_macos,crates/gpui_ios,crates/gpui_linux,crates/gpui_windows,crates/gpui_wgpu,crates/gpui_web,crates/sum_tree,crates/rope,crates/text,crates/multi_buffer,crates/editorwarpdotdev/warp(f8a9c71, Oct 2026) —crates/warpui_core(MIT),crates/warpui(MIT), and "How Warp Works"- a private Mac launcher app, cited as
launcher/(docs/motion.md,docs/glass.md,docs/perf-log.md,docs/raycast-research.md,src/native_ui.rs) scrawl(src/native/pill/{mod.rs,model.rs,atlas.rs,pill.metal},scrawl/perf/start-latency.md)
Executive Summary
- GPUI has grown into a 174k-LoC multi-crate platform engine (
gpui*across 13 crates at Zed5447804), up from ~97k LoC ingpui 0.2.2. Between0.2.2and Oct 2026, Zed split GPUI intogpui(101,878 LoC),gpui_apple(5,351 LoC, shared Metal + CoreText),gpui_macos(9,711 LoC),gpui_ios(3,684 LoC),gpui_linux(16,934 LoC),gpui_windows(14,798 LoC),gpui_wgpu(7,969 LoC, replacingblade-graphicsand powering Linux + Web), andgpui_web(7,866 LoC, WASM + WebGPU/WebGL2 + Canvas2D fallback). - GPUI's renderer is a flat 7-primitive batcher (
Shadow,Quad,Underline,MonochromeSprite,SubpixelSprite,PolychromeSprite,Path,Surface) sorted by a 4-ary spatial R-tree (BoundsTree). It achieves ~0.2–0.8 ms GPU encode/render times for typical editor frames by packing primitives into a single 64 KB-alignedInstanceBuffer(crates/gpui_apple/src/metal_renderer.rs:58-77) and merging non-overlapping draw batches acrossDrawOrderviaBoundsTree<Bounds<ScaledPixels>>(crates/gpui/src/bounds_tree.rs:14-18,MAX_CHILDREN = 4). - GPUI's biggest rendering weaknesses are full-window redraws, lack of offscreen layer/filter compositing, and synchronous window-resize barriers. GPUI clears and redraws the entire window drawable every dirty frame (
MTLLoadAction::Clear, no dirty-rect scissor damage). ItsAnyView::cachedmechanism (crates/gpui/src/view.rs:258-312,crates/gpui/src/window.rs:4014-4123) only skips CPU layout/prepaint/paint by replayingSceneslices (Scene::replay,crates/gpui/src/scene.rs:208-274). Worse,Pathbatches clear and resolve a full-viewport 4x MSAA intermediate texture on every path batch (crates/gpui_apple/src/metal_renderer.rs:1100-1215), and live window resizing setspresentsWithTransaction = truewithcommand_buffer.waitUntilScheduled()on the main thread (crates/gpui_apple/src/metal_renderer.rs:845-853). - Two of Kit's four
kitlangton/gpui(2582af8) fixes are still missing in upstream Zed (5447804). Upstream Zed now includes perceptual luminance-based glyph dilation (glyph_dilation_for_color,crates/gpui_apple/src/text_system.rs:265-274, 510-516) and triangular blue-noise gradient dithering (gradient_color,crates/gpui_apple/src/shaders.metal:326-331). However, upstream Zed still lacks Gaussian shadow dithering inshadow_fragment(crates/gpui_apple/src/shaders.metal:472-502), still leavesCAMetalLayercolorspace unconfigured whenuse_display_p3is false (crates/gpui_apple/src/metal_renderer.rs:321-334), and still lacks theCVDisplayLinkCreateWithCGDisplayfallback incrates/gpui_macos/src/display_link.rs:28-56(causingkCVReturnInvalidDisplay = -6661panics/failures on headless or asleep Mac Studio displays). - GPUI's 3-phase
Elementpipeline (request_layout→prepaint→paint) +EntityMaplease model is ergonomic for static/editor UIs but brittle for choreographed motion and nested state. Entities are leased out of aSecondaryMap<EntityId, Box<dyn Any>>onto the stack duringupdate(crates/gpui/src/app/entity_map.rs:138-233), causing runtimedouble_lease_paniccrashes whenever an observer, event callback, or AppKit delegate re-enters an entity currently being updated. - Zed's text engine (
sum_tree→rope→text→multi_buffer→editor::display_map) is best-in-class and cleanly decoupled from GPUI.SumTree<T>(crates/sum_tree/src/sum_tree.rs:18,TREE_BASE = 6, 12 items/node) +Chunk(crates/rope/src/chunk.rs:9-33, 128-byteArrayStringwith fouru128bitmaps forchars,chars_utf16,newlines,tabs) providesO(\log N)seek between UTF-8 bytes, UTF-16 code units, andPoint(row, col)using hardwarepopcnt/tzcnt, andLineLayoutCache(crates/gpui/src/text_system/line_layout.rs:458-560) adds zero-allocationshape_line_by_hash(crates/gpui/src/text_system.rs:689-720). - Upstream GPUI added
spring.rs,hot_reload.rs(subsecond),AccessKit(window/a11y.rs),gpui_ios, andgpui_web, but animation remains a closure wrapper. Even withSpringAnimation(crates/gpui/src/spring.rs:1-214), GPUI animations work by cloning and rebuilding the wrapped element subtree insiderequest_layouton every tick (crates/gpui/src/elements/animation.rs:208-246), with no persistent velocity across interrupted retargets, no off-main-thread compositor, and no GPU-property-only fast path. - The launcher and Scrawl both hit hard architectural walls in GPUI and replaced GPUI windows with native AppKit/Core Animation/Metal surfaces. The launcher's GPUI morph suffered 6 structural visual defects (
launcher/docs/motion.md:20-75), 11–18 ms cold glyph/emoji stalls (launcher/docs/perf-log.md:72-97), and 641 µsSyncListre-measurements, leading it to move its panel to aWindowServer-compositedCASpringAnimation+ SwiftUI panel (launcher/src/native_ui.rs). Scrawl replaced GPUI'sHudPillWindowwith a 440-line 3-pass nativeCAMetalLayer+NSGlassEffectViewrenderer (scrawl/src/native/pill/mod.rs:1-31) to get offscreen blur/glow passes, sub-millisecond cold start (0.08 mshidden init vs6.93 mseager), and zero idle CPU. - Warp's
warpui(crates/warpui_core+crates/warpui, MIT-licensed inwarpdotdev/warp) is GPUI v1's direct sibling—originally co-designed with Nathan Sobo in 2021. Warp kept the retainedPresenter+ Flutter-styleSizeConstraintElementtree while adding two notable innovations GPUI lacks: thread-local automatic dependency tracking viaTracked<T>(crates/warpui_core/src/core/autotracking/mod.rs:1-200, eliminating manualcx.notify()) and a dual GUI + Ratatui TUI presenter (crates/warpui_core/src/presenter/tui.rs:1-150). However, its rendering pipeline (3 shaders, shelf-next-fit atlas without eviction, no subpixel AA, no offscreen filter passes) is less capable than GPUI's. - Recommendation for Rune: Do NOT fork GPUI (or
warpui); build a fresh, modular architecture while stealingsum_tree/rope,BoundsTreebatching,etagere+LineLayoutCache, andkitlangton/gpui(2582af8) shader/text refinements. Forking GPUI locks Rune into a 174k-LoC monolithicApp/Windowgod-object where fixing borrow-reentrancy, retained compositor layers, interruptible spring retargeting, offscreen filter graphs, and damage-tracked rendering requires rewriting the core anyway.
1. GPUI Renderer (gpui, gpui_apple, gpui_wgpu, gpui_windows)
1.1 Scene Representation & Batching (crates/gpui/src/scene.rs, bounds_tree.rs)
Every frame, GPUI's paint phase populates a flat Scene struct (crates/gpui/src/scene.rs:34-46):
pub struct Scene {
pub(crate) shadows: Vec<Shadow>,
pub(crate) quads: Vec<Quad>,
pub(crate) paths: Vec<Path<ScaledPixels>>,
pub(crate) underlines: Vec<Underline>,
pub(crate) monochrome_sprites: Vec<MonochromeSprite>,
pub(crate) subpixel_sprites: Vec<SubpixelSprite>,
pub(crate) polychrome_sprites: Vec<PolychromeSprite>,
pub(crate) surfaces: Vec<PaintSurface>,
layer_stack: Vec<DrawOrder>,
bounds_tree: BoundsTree<Bounds<ScaledPixels>>,
}
- Spatial
DrawOrderassignment viaBoundsTree: Instead of assigning monotonically increasing z-indices (which would break batching whenever a parent draws a quad, then text, then another sibling quad),Scene::insert_primitivequeriesbounds_tree.insert(bounds)(crates/gpui/src/scene.rs:171-206).BoundsTree(crates/gpui/src/bounds_tree.rs:14-18) is a custom 4-ary R-tree (MAX_CHILDREN: usize = 4) where each node stores the maximumu32ordering among its descendants. Querying the tree finds the highestDrawOrderof any spatially intersecting primitive already in the scene and assignsmax_overlapping_order + 1. Non-overlapping primitives of the same kind therefore receive identicalDrawOrders (or sort adjacent to each other). - Finish &
PrimitiveBatchiterator: At the end ofWindow::draw,Scene::finish()(crates/gpui/src/scene.rs:283-292) sorts all 7 primitive vectors by(draw_order, id)using.sort_unstable(). Duringpresent(),Scene::batches()(crates/gpui/src/scene.rs:299-415) runs a 7-way merge iterator that coalesces contiguous primitives of the same kind (and sameAtlasTextureIdfor sprites) into a singlePrimitiveBatchinstanced draw call. - Coordinate spaces: Layout runs in logical
Pixels(f32), scene primitives are stored inScaledPixels(f32)(Pixels * scale_factor), and atlas/rasterization coordinates use integerDevicePixels(i32)(crates/gpui/src/scene.rs:10-25).
1.2 Metal Backend (crates/gpui_apple/src/metal_renderer.rs, shaders.metal)
On macOS and iOS, MetalRenderer (crates/gpui_apple/src/metal_renderer.rs:226-265) renders into a CAMetalLayer:
- Single-buffer instancing: All per-frame instance data (
Quad,Shadow,Underline,MonochromeSprite,SubpixelSprite,PolychromeSprite,PathRasterizationVertex,PathSprite) is bumped into one sharedInstanceBuffer(crates/gpui_apple/src/metal_renderer.rs:58-77, default capacity2 * 1024 * 1024bytes = 2 MB, grown dynamically) with 64 KB-aligned sub-allocations (align = 64 * 1024). If the buffer overflows mid-frame,draw()returnsfalse, doublesinstance_buffer_size, and retries the command buffer encode (crates/gpui_apple/src/metal_renderer.rs:483-509). - Triple-buffered pool:
InstanceBufferPool(crates/gpui_apple/src/metal_renderer.rs:79-121) holdsmax_buffers_in_flight = 3(CAMetalLayer.maximumDrawableCount = 3), returning buffers to the pool in theMTLCommandBuffercompletion handler block (crates/gpui_apple/src/metal_renderer.rs:835-843). - 8 Render Pipeline States:
quads(quad_vertex/quad_fragment,crates/gpui_apple/src/shaders.metal:333-444): SDF rounded rectangle with per-corner radii (Corners<ScaledPixels>), per-edge border widths (Edges<ScaledPixels>), dashed/solid border antialiasing, and solid/linear-gradient background fill (BackgroundTag::Solid | LinearGradient | PatternSlash).shadows(shadow_vertex/shadow_fragment,crates/gpui_apple/src/shaders.metal:446-502): Analytic 4-step Gaussian integral of an SDF rounded box (erfapproximation along X, 4-sample quadrature along Y).underlines(underline_vertex/underline_fragment,crates/gpui_apple/src/shaders.metal:551-621): Straight or sine-wave (wavy) underlines.monochrome_sprites(monochrome_sprite_vertex/monochrome_sprite_fragment,crates/gpui_apple/src/shaders.metal:623-702): SamplesR8Unormglyph/SVG masks fromMetalAtlas, supports 2x2TransformationMatrix(for rotated SVGs), and outputs premultipliedHSLA -> linear/sRGB RGBA.subpixel_sprites(subpixel_sprite_vertex/subpixel_sprite_fragment,crates/gpui_apple/src/shaders.metal:704-777): Dual-source blending (color(0), index(0)andcolor(0), index(1)) withSource1Colorblend factor for LCD subpixel text AA. (Note: disabled by default on macOS ingpui_applewhere grayscale + luminance dilation is preferred, used on Windows/Linux).polychrome_sprites(polychrome_sprite_vertex/polychrome_sprite_fragment,crates/gpui_apple/src/shaders.metal:779-858):BGRA8Unormatlas tiles (color emojis, raster images) with SDF corner clipping, grayscale filter, and opacity.paths_rasterization+paths(path_rasterization_vertex/fragmentandpath_vertex/fragment,crates/gpui_apple/src/shaders.metal:504-549): Arbitrary vector paths (PathBuilder,crates/gpui/src/path_builder.rs:1-250). Filled paths use Lyon (lyon::tessellation::FillTessellator) or Loop-Blinn quadratic curve UV coordinates (st_positioninPathVertex); stroked paths uselyon::tessellation::StrokeTessellator.surfaces(surface_vertex/surface_fragment,crates/gpui_apple/src/shaders.metal:860-910): Hardware-decodedCVPixelBuffer(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) YCbCr-to-RGB video surfaces viaCVMetalTextureCache.
1.3 Severe Bottlenecks in GPUI's Renderer
- Path rendering allocates & clears a full-viewport 4x MSAA texture per path batch: Look at
MetalRenderer::draw_paths_to_intermediate(crates/gpui_apple/src/metal_renderer.rs:1100-1215). Every timeScene::batches()yields aPrimitiveBatch::Paths(which cannot be merged across an interveningQuadorMonochromeSpriteif bounding boxes overlap), GPUI ends the mainMTLRenderCommandEncoder, starts a new render pass on a full-windowpath_sample_count = 4MSAA texture + resolve texture (MTLLoadAction::Clearacross the entire viewport, not just the path's bounding box!), renders the path triangles, ends the encoder, and starts a new mainMTLRenderCommandEncoderwithMTLLoadAction::Loadto blit the resolved texture viapath_sprite. Having N interleaved path layers in a frame triggers2N + 1full render-pass switches andNfull-window 4x MSAA clears. - No offscreen layer stack, backdrop filter, or group opacity:
window.with_element_opacity(opacity, ...)(crates/gpui/src/window.rs:1249) simply multiplieselement_opacityinto each individual quad/sprite color at CPU paint time. Overlapping children within a translucent container bleed through each other (Classic alpha-compositing bug) instead of compositing as a group. There is zero support for offscreen Gaussian blur, vibrancy, or shader post-processing passes—which is why Scrawl had to bypass GPUI for its glowing/blurred HUD pill (scrawl/src/native/pill/mod.rs:1-31). - Full-window redraw every frame &
Scene::replaylimits: GPUI has no dirty-rect scissor/damage tracking at the GPU level;draw()always runsMTLLoadAction::Clearon the fullCAMetalDrawable.Entity::cached(crates/gpui/src/view.rs:258) +Scene::replay(crates/gpui/src/scene.rs:208-274) copies the previous frame'sQuad/MonochromeSpritestructs fromrendered_frame.sceneintonext_frame.sceneand re-inserts every primitive intoBoundsTreeon the CPU. If any animation ticks in a window, the entire window'sSceneis re-sorted, re-uploaded toInstanceBuffer, and re-rendered by the GPU. - Synchronous
presentsWithTransactionstall during resize: During window resize (crates/gpui_macos/src/window.rs:3320-3341), GPUI setsrenderer.set_presents_with_transaction(true), which forcescommand_buffer.commit(); command_buffer.wait_until_scheduled(); drawable.present();on the main thread (crates/gpui_apple/src/metal_renderer.rs:845-853).
1.4 Cross-Platform Renderers (gpui_wgpu, gpui_windows)
gpui_wgpu(crates/gpui_wgpu/src/wgpu_renderer.rs:1-250): Ingpui 0.2.2, Linux usedblade-graphics. By Oct 2026 (5447804), Zed replacedblade-graphicswithgpui_wgpu(7,969 LoC), which now powers bothgpui_linux(Wayland + X11 via Vulkan/GL) andgpui_web(WebGPU + WebGL2 fallback). It implements the exact same 7-primitive pipeline in WGSL (crates/gpui_wgpu/src/shaders.wgsl) including dual-source blending (wgpu::Features::DUAL_SOURCE_BLENDING) when supported by the adapter (crates/gpui_wgpu/src/wgpu_renderer.rs:150-166), falling back to single-source alpha blending on WebGL2/uncooperative drivers.gpui_windows(crates/gpui_windows/src/directx_renderer.rs:1-200): Uses raw Direct3D 11 (ID3D11Device,IDXGISwapChain1) bound toIDCompositionDevice/IDCompositionVisual(DirectComposition) for transparent/acrylic window support and low-latency flip-model presentation, with 4x MSAA intermediate textures for paths (PATH_MULTISAMPLE_COUNT = 4, line 29) and HLSL shaders compiled at build time.
1.5 Texture Atlas (etagere in crates/gpui_apple/src/metal_atlas.rs)
MetalAtlas (crates/gpui_apple/src/metal_atlas.rs:1-250) wraps etagere::BucketedAtlasAllocator:
- Separate texture lists by
AtlasTextureKind:Monochrome→MTLPixelFormatR8Unorm(glyphs, rasterizedresvgSVGs)Subpixel→MTLPixelFormatBGRA8UnormPolychrome→MTLPixelFormatBGRA8Unorm(color emojis, images)
- Starting page size: 1024×1024 for Monochrome/Subpixel, 2048×2048 for Polychrome (
crates/gpui_apple/src/metal_atlas.rs:138-145), dynamically allocating additionalMTLTexturepages whenetagerefills up. - Uploads happen synchronously on the CPU into
MTLStorageModeManaged(Intel) orMTLStorageModeShared(Apple Silicon unified memory) viareplaceRegion_mipmapLevel_withBytes_bytesPerRow(crates/gpui_apple/src/metal_atlas.rs:265-285).
1.6 Exact Diff Analysis: kitlangton/gpui (2582af8) vs Upstream Zed (5447804)
In kitlangton/gpui, commit 2582af8 ("Fix Metal display link fallback and rendering quality", 4 files changed, +127/-16 lines) fixed four concrete production bugs in gpui 0.2.2:
Fix in kitlangton/gpui (2582af8) |
How it works in kitlangton/gpui |
Status in Upstream Zed (5447804, Oct 2026) |
|---|---|---|
1. CVDisplayLink fallback (src/platform/mac/display_link.rs:25-60) |
When CVDisplayLinkCreateWithCGDisplay(display_id, ...) fails with kCVReturnInvalidDisplay (-6661, common on headless/asleep displays or virtual displays), falls back to CVDisplayLinkCreateWithActiveCGDisplays and calls CVDisplayLinkSetCurrentCGDisplay best-effort. |
MISSING in upstream Zed. crates/gpui_macos/src/display_link.rs:28-56 still calls CVDisplayLinkCreateWithCGDisplay and immediately returns Err on non-zero CVReturn! Worse, crates/gpui_macos/src/window.rs:839-845 skips starting the display link if !occlusionState.contains(NSWindowOcclusionStateVisible), but WindowFrameSource still uses a 60 Hz dispatch_source_create(DISPATCH_source_type_timer) fallback only when occluded (display_link.rs:240-310). |
2. Explicit sRGB CAMetalLayer colorspace (src/platform/mac/metal_renderer.rs:257-263) |
Sets layer.set_colorspace(CGColorSpaceCreateWithName(kCGColorSpaceSRGB)) on the CAMetalLayer so macOS WindowServer composites BGRA8Unorm in a deterministic sRGB color space instead of untagged device RGB. |
PARTIALLY MISSING in upstream Zed. crates/gpui_apple/src/metal_renderer.rs:321-334 only sets kCGColorSpaceDisplayP3 when use_display_p3() is true (colorspace setting =P3) and sets null() when false, leaving default sRGB windows untagged! |
3. Gradient + Shadow dithering (src/platform/mac/shaders.metal:326-331, 495-503) |
Adds interleaved gradient noise dither_noise(position) (fract(52.9829189 * fract(dot(position, float2(0.06711056, 0.00583715))))) with triangular PDF (n1 + n2 - 1.0) / 255.0 to both linear gradients (gradient_color) and shadow_fragment (scaled by min(alpha * 2.0, 1.0)), eliminating 8-bit Mach banding on dark glass/shadows. |
HALF-MERGED in upstream Zed. crates/gpui_apple/src/shaders.metal:326-331 has the exact same gradient_color triangular dither, but shadow_fragment (crates/gpui_apple/src/shaders.metal:472-502) has NO dithering, so subtle dark drop shadows still band in 8-bit BGRA8Unorm. |
4. Perceptual luminance glyph dilation (src/platform/mac/text_system.rs:274-305, 514-525) |
Replaces constant black-on-white CoreGraphics glyph rasterization (set_gray_fill_color(0.0, 1.0)) with 5 discrete dilation levels (0..=4) keyed in RenderGlyphParams::dilation based on perceptual luminance (0.2126*R + 0.7152*G + 0.0722*B).sqrt(), calling cx.set_should_smooth_fonts(true) + cx.set_gray_fill_color(luminance, 1.0) so light text on dark backgrounds gets native macOS stem thickening. |
MERGED in upstream Zed. Present verbatim in crates/gpui_apple/src/text_system.rs:265-274, 510-516 (glyph_dilation_for_color, params.dilation as f64 * 0.25). |
2. Element / View / Entity Model (crates/gpui/src/{app.rs,view.rs,element.rs,window.rs})
2.1 Ownership & the EntityMap Lease Model (crates/gpui/src/app/entity_map.rs)
GPUI solves Rust's single-owner graph problem by making App the sole owner of all stateful models and views (crates/gpui/src/_ownership_and_data_flow.rs:1-40):
EntityMap(crates/gpui/src/app/entity_map.rs:56-60) storesentities: SecondaryMap<EntityId, Box<dyn Any>>alongsideref_counts: Arc<RwLock<EntityRefCounts>>(aSlotMap<EntityId, AtomicUsize>).Entity<T>is a lightweight handle (EntityId+Weak<RwLock<EntityRefCounts>>+PhantomData<T>) that increments/decrements the atomic refcount onClone/Dropand pushes zero-refcount IDs ontodropped_entity_ids(crates/gpui/src/app/entity_map.rs:190-209).- How
entity.update(cx, |this, cx| ...)works:EntityMap::lease(crates/gpui/src/app/entity_map.rs:138-167, 219-227) physically removesBox<dyn Any>fromself.entities(self.entities.remove(entity_id)), moves it onto the call stack inside aLease<T>, runs the closure with(&mut T, &mut Context<T>), and re-inserts it inend_lease(self.entities.insert(entity_id, entity)). - The Re-Entrancy Footgun (
double_lease_panic): Becauseleaseremoves theBox<dyn Any>fromSecondaryMap, any attempt to.read(cx)or.update(cx)that sameEntity<T>while itsupdateclosure is still on the call stack hitsNoneand panics at runtime (crates/gpui/src/app/entity_map.rs:230-233):Whilepanic!("cannot {operation} {entity_type} while it is already being updated")Context::notify()andContext::emit()queue effects ontoApp::pending_effects(VecDeque<Effect>) and flush them in a loop only when the outermostApp::updatefinishes, synchronous callbacks (such as window activation/appearance callbacks, focus listeners, or helper methods passed an&mut Appthat also hold anEntity<Self>) routinely trigger this panic. Notecrates/gpui/src/window.rs:1992-2000where Zed itself had to spawn an async task onforeground_executorinsideon_appearance_changedspecifically to avoid a synchronous App borrow panic!
2.2 Views (Render), Components (RenderOnce), and the 3-Phase Element Pipeline
In GPUI (crates/gpui/src/view.rs:197-247, crates/gpui/src/element.rs:1-520):
- Stateful Views (
impl Render for MyView): Hold persistent state inEntity<MyView>. Callingcx.notify()marks theEntityIddirty inwindow.dirty_views(crates/gpui/src/window.rs:2265-2275). - Stateless Components (
#[derive(IntoElement)] impl RenderOnce for MyButton): Consumed by value (fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement) each frame. - 3-Phase
ElementTrait (crates/gpui/src/element.rs:50-145):request_layout(&mut self, global_id, inspector_id, window, cx) -> (LayoutId, Self::RequestLayoutState): Builds the Taffy layout tree (TaffyLayoutEngine,crates/gpui/src/taffy.rs:31-107, which wrapstaffy::TaffyTreewithtaffy.disable_rounding()and custom pixel-snapping incompute_layout). Views callRender::renderduring this phase (or duringprepaintif cached).prepaint(&mut self, global_id, inspector_id, bounds, request_layout, window, cx) -> Self::PrepaintState: Runs after Taffy solvescompute_layoutat the root (crates/gpui/src/window.rs:3721-3728). Every element now knows its resolvedBounds<Pixels>. Elements registerHitboxes (window.insert_hitbox), pushDispatchNodes intoDispatchTreefor keyboard/action routing, buildAccessKitnodes if a screen reader is active (crates/gpui/src/element.rs:428-463), and shape text lines.paint(&mut self, global_id, inspector_id, bounds, request_layout, prepaint, window, cx): Runs afterself.mouse_hit_test = self.next_frame.hit_test(self.mouse_position)(crates/gpui/src/window.rs:3758). Because hit-testing happens betweenprepaintandpaint, elements can queryhitbox.is_hovered(window)duringpaintand emit hover colors/cursors on the same frame without a 1-frame lag! Elements push primitives intowindow.next_frame.sceneand register mouse listeners (window.on_mouse_event).
ElementArena(crates/gpui/src/arena.rs,window.rs:395-476):AnyElementboxes are bump-allocated into a per-AppArena(cleared at the end ofwindow.draw(cx)viaarena_clear_needed.clear(cx),crates/gpui/src/window.rs:1910-1912) so constructing thousands ofDiv/Svg/Canvaselements per frame does not hit the system allocator.- Cross-frame Element State (
GlobalElementId): Elements with an explicit.id(...)push ontowindow.element_id_stack(ElementIdStack,crates/gpui/src/window.rs:1023), forming a path-hashedGlobalElementId(crates/gpui/src/element.rs:250-285).window.with_element_state::<S, _>(global_id, ...)moves state fromrendered_frame.element_states(FxHashMap<(GlobalElementId, TypeId), ElementStateBox>) intonext_frame.element_states(crates/gpui/src/window.rs:1203-1210). Unvisited state is automatically dropped at frame end (immediate-mode GC). - View Caching (
Entity::cached,crates/gpui/src/view.rs:258, 484-643): Opt-in only (entity.cached(style)), requiring a caller-supplied definiteStyleRefinementbecause GPUI skips callingRender::renderduringrequest_layout! Duringprepaint_view(view.rs:546-561), ifbounds,content_mask, andtext_stylematch the previous frame and!window.dirty_views.contains(&entity_id), GPUI callswindow.reuse_prepaintandwindow.reuse_paint(crates/gpui/src/window.rs:4014-4123), which slice-clones hitboxes, mouse listeners, dispatch tree nodes, line layouts, andSceneprimitives fromrendered_frameintonext_frame. Pain point: Notice comments atwindow.rs:3463-3500and3893-3900—becausereuse_prepaint/reuse_paintrely on fragile index ranges (Range<PrepaintStateIndex>,Range<PaintIndex>) into flatFramevectors, any mutation that shifts vector indices (such as nesteddeferred_drawsor.pop()oninput_handlers) corrupts cached subtrees and panics!
2.3 Focus, Actions, and Keybinding Dispatch (crates/gpui/src/key_dispatch.rs)
FocusHandle&FocusId(crates/gpui/src/window.rs:543-726): Ref-counted slotmap key owned byApp::focus_handles. Duringprepaint,.track_focus(&focus_handle)attaches aFocusIdto the currentDispatchNodeinDispatchTree(crates/gpui/src/key_dispatch.rs:220-224).DispatchTree&KeyContext(crates/gpui/src/key_dispatch.rs:71-250): Every frame builds a tree ofDispatchNodes mirroring the element hierarchy. Each node can hold aKeyContext(e.g.,"Editor && mode == full"),focus_id,key_listeners, andaction_listeners(TypeId→ closure).- Capture & Bubble Phases: When a keystroke arrives,
DispatchTree::dispatch_keycomputes thefocus_pathfrom the rootDispatchNodedown to the focusedFocusId, matches single-key or multi-key chord sequences ("cmd-k left") against theKeymapfrom deepest context to root, and dispatches the resultingAction(Capturephase top-down, thenBubblephase bottom-up).
3. Text & Layout System (crates/gpui/src/text_system.rs, sum_tree, rope, text, multi_buffer)
3.1 Platform Shaping & Rasterization Backends
GPUI abstracts platform text shaping behind PlatformTextSystem (crates/gpui/src/platform.rs):
- macOS & iOS (
AppleTextSystem,crates/gpui_apple/src/text_system.rs:1-749):- Uses
font-kitfor font loading/metrics and Apple CoreText (CTLine::new_with_attributed_string,crates/gpui_apple/src/text_system.rs:540-636) for shaping. - Ligature-breaking trick across syntax runs (
crates/gpui_apple/src/text_system.rs:567-579): Because CoreText shapes an entireCFAttributedStringat once and will form ligatures across adjacent syntax tokens if their attributes are identical, GPUI alternatespx(f32::from(font_size).next_up())(1 ULP float increment!) on adjacentFontRuns (break_ligature = !break_ligature) so CoreText breaks ligatures at token boundaries (e.g. when a cursor or selection splits!=). - Subpixel positioning & Dilation (
crates/gpui_apple/src/text_system.rs:480-537): Rasterizes glyphs into an 8-bit grayscaleCGContext(or 32-bit RGBA premultiplied for color emojis, swapped to straight-alpha BGRA) withSUBPIXEL_VARIANTS_X = 4, SUBPIXEL_VARIANTS_Y = 1(crates/gpui/src/text_system.rs:49-52), shifting the glyph origin bysubpixel_variant / 4.0device pixels and applying 5-step luminance dilation (0.0..=1.0gray fill color withset_should_smooth_fonts(true)).
- Uses
- Linux & Web (
CosmicTextSystem,crates/gpui_wgpu/src/cosmic_text_system.rs:1-200):- Uses
cosmic-text(FontSystem,ShapeLine,ShapeBuffer— which usesrustybuzz/ HarfBuzz under the hood) for shaping andswash(ScaleContext) for subpixel-positioned glyph/emoji rasterization. - On
gpui_web(crates/gpui_web/src/text_system.rs:1-200),WebTextSystemwrapsCosmicTextSystem(loaded with embedded fonts) and adds a Canvas2D fallback (CanvasFontFallback) that measures and rasterizes missing CJK/emoji glyphs via the browser's native<canvas>2D context using system fonts!
- Uses
- Windows (
DirectWriteTextSystem,crates/gpui_windows/src/direct_write.rs:1-200):- Uses
IDWriteFactory5,IDWriteTextAnalyzer1, andIDWriteFontFallbackfor shaping, ClearType subpixel rendering with gamma/contrast tables (DirectXRenderer::get_font_info,directx_renderer.rs:32-37), and a dedicated Direct3D 11 shader pass (ShaderModule::EmojiRasterization,direct_write.rs:63-165) to rasterize layered COLRv0/COLRv1/DirectWrite color glyphs into the atlas.
- Uses
3.2 LineLayoutCache & Zero-Allocation Hash Lookups (crates/gpui/src/text_system/line_layout.rs:458-560)
LineLayoutCachemaintains twoFrameCachemaps (previous_frameandcurrent_frame). Every shaped line is looked up incurrent_frame; on miss, it checksprevious_frame, moves theArc<LineLayout>intocurrent_frameif found, or callsplatform_text_system.layout_line(...)on miss. Atfinish_frame(),previous_frameis replaced bycurrent_frame, automatically evicting any line layout that wasn't used in the frame just drawn.shape_line_by_hash(crates/gpui/src/text_system.rs:689-720): Added in recent Zed so the editor can hash rope chunks +FontRuns and hitlines_by_hashwithout allocating a contiguousSharedStringper visible line on every frame.
3.3 Zed's Rope, SumTree, CRDT Buffer, MultiBuffer, and DisplayMap
Zed's text architecture is a layered tower of immutable, persistent, Arc-shared B+ trees (crates/sum_tree/src/sum_tree.rs:1-200):
[ DisplayMap ] ← Highlighting + BlockMap (diagnostics/creases)
↑
[ WrapMap ] ← Soft-wrapping transforms (SumTree<WrapTransform>)
↑
[ TabMap ] ← Hard-tab expansion transforms
↑
[ FoldMap ] ← Code folding transforms (SumTree<FoldTransform>)
↑
[ InlayMap ] ← LSP inlay hints injected without mutating buffer
↑
[ MultiBuffer ] ← Excerpts from 1..N buffers (SumTree<Excerpt>) — powers project search & split diffs
↑
[ text::Buffer ] ← CRDT operation log, Lamport anchors, UndoMap, SumTree<Fragment>
↑
[ rope::Rope ] ← SumTree<Chunk> (128-byte ArrayString + 4x u128 bitmaps)
SumTree<T: Item>(crates/sum_tree/src/sum_tree.rs:18): A copy-on-write B+ tree (Arc<Node<T>>) withTREE_BASE = 6(up to2 \times 6 = 12child pointers or leaf items per node). Every internal node caches aSummary(monoid withzero()andadd_summary()) for each child. A singleSumTreecan be indexed and binary-searched (cursor.seek(&target, Bias::Left)) along anyDimension<'a, S>derived fromSinO(\log_{12} N)time, and cloned inO(1)(Arc::clone) for background threads!rope::Rope&Chunk(crates/rope/src/{rope.rs,chunk.rs}):Ropeis simplySumTree<Chunk>(crates/rope/src/rope.rs:26-28).- Each leaf
Chunk(crates/rope/src/chunk.rs:9-33) holds an inlineArrayString<128, u8>(MAX_BASE = 128,MIN_BASE = 64) plus fouru128bitmaps (1 bit per byte of the 128-byte chunk!):chars: u128— bitiis 1 ifftext[i]is a UTF-8 code point start byte.chars_utf16: u128— bitiis 1 for every UTF-8 start byte, plus biti+1is 1 for 4-byte UTF-8 sequences (\ge 240, which take 2 UTF-16 code units). Thus(chars_utf16 & mask).count_ones()gives the exact UTF-16 offset in 1 CPU instruction!newlines: u128— bitiis 1 ifftext[i] == b'\n'.tabs: u128— bitiis 1 ifftext[i] == b'\t'.
- Each node summarizes
TextSummary { len: usize, chars: usize, len_utf16: OffsetUtf16, lines: Point, first_line_chars: u32, last_line_chars: u32, last_line_len_utf16: u32, longest_row: u32, longest_row_chars: u32 }. Converting between byte offset,Point(row, column), and LSPPointUtf16(row, utf16_col)descends theSumTreeto the 128-byteChunkand uses bitwise mask +count_ones()(popcnt) /leading_zeros()(lzcnt)—no linear byte scanning.
text::Buffer&Anchor(crates/text/src/{text.rs,anchor.rs}):- Implements an operation-based CRDT (
BufferSnapshot,crates/text/src/text.rs:113-124) backed byvisible_text: Rope,deleted_text: Rope,fragments: SumTree<Fragment>, andinsertions: SumTree<InsertionFragment>. Anchor(Lamporttimestamp + offset within insertion fragment +Bias) survives arbitrary concurrent local and remote edits and resolves back to a byte/Pointoffset inO(\log N)viafragments.
- Implements an operation-based CRDT (
multi_buffer::MultiBuffer(crates/multi_buffer/src/multi_buffer.rs:74-93):- Presents either a single
Buffer(singleton: true) or aSumTree<Excerpt>stitching slices from multipleBuffers into one unified coordinate space (MultiBufferRow,MultiBufferPoint,Anchor). Every Zed editor view—single file, multibuffer project search results, inline git diff hunks, and split diffs—renders aMultiBuffer.
- Presents either a single
editor::DisplayMap(crates/editor/src/display_map.rs:1-145):- A 5-layer pipeline (
InlayMap→FoldMap→TabMap→WrapMap→BlockMap) where each layer is aSumTree<Transform>withTransformSummary { input: TextSummary, output: TextSummary }. Incrementally synced viaPatch<T>(Edit<InputDim>→Edit<OutputDim>), allowing soft-wrapping (WrapMapruns asynchronously on a background thread when lines exceed a threshold), code folding, inlay hints, and inline diagnostic/AI blocks without mutating the underlyingMultiBuffer.
- A 5-layer pipeline (
3.4 IME & Accessibility Integration
- macOS
NSTextInputClient(crates/gpui_macos/src/window.rs:236-270, 3368-3587): Implemented via Objective-C runtime class registration onGPUIView, forwardinginsertText:replacementRange:,setMarkedText:selectedRange:replacementRange:,unmarkText,firstRectForCharacterRange:actualRange:, andcharacterIndexForPoint:to the active element'sPlatformInputHandler(registered duringpaintviawindow.handle_input(&focus_handle, handler, cx)). - iOS
UITextInput(crates/gpui_ios/src/ios/text_input.rs:129-200): Implemented via a hidden responderGPUITextInputViewsubclassingUIViewwith customGPUITextPosition,GPUITextRange, andGPUITextSelectionRectclasses. - Web
ImeMirror(crates/gpui_web/src/window.rs:57, 70-86): Synchronizes a hidden DOM<textarea>(ImeMirror) with the canvas caret. - Accessibility (
crates/gpui/src/window/a11y.rs,crates/gpui/src/_accessibility.rs): Recently addedAccessKitintegration (accesskit::TreeUpdatebuilt lazily duringprepaintonly whenwindow.a11y.is_active()is true,crates/gpui/src/window.rs:3691-3694, 3790-3815, usingGlobalElementId::accesskit_node_id(),crates/gpui/src/element.rs:253-255, 428-463).
4. Animation, Platform Layer, Hot Reload, and Real-World Failure Modes
4.1 Animation Architecture (crates/gpui/src/elements/animation.rs, spring.rs)
- In
gpui 0.2.2(kitlangton/gpui): Only time-basedAnimation { duration, oneshot, easing: Rc<dyn Fn(f32) -> f32> }(ease_in_out,cubic_bezier,pulsating_between,bounce). - In Zed
5447804(crates/gpui/src/spring.rs:1-214): Upstream Zed added an analytic underdamped/critically-damped/overdamped harmonic oscillator solver (SpringConfig { stiffness, damping, mass }, presetsSpringConfig::snappy()= stiffness 400 / damping 30,bouncy()= 300 / 15,smooth()= 200 / 25) andSpringAnimation<T: Interpolatable>(f32,Pixels,Point<Pixels>,Size<Pixels>,Hsla). - Why GPUI's animation model is still fundamentally limited:
- Immediate-mode subtree cloning on every tick: Look at
AnimationElement::request_layout(crates/gpui/src/elements/animation.rs:208-246) andSpringElement::request_layout(crates/gpui/src/spring.rs:410-465). On every frame while an animation is active, GPUI clones the underlying element (self.element.clone()), calls the user's animator closureanimator(element, delta), scheduleswindow.request_animation_frame(), and runs full Taffyrequest_layout→prepaint→paint→ full-windowScenesort → GPU clear & redraw. - No velocity preservation on retargeting:
SpringAnimation(crates/gpui/src/spring.rs:165-195) computes position analytically from(from, to, start: Instant)with zero initial velocity (\dot{x}(0) = 0). Iftochanges mid-flight (interruptible UI interaction), resetting the spring discards current velocity\dot{x}(t)instead of integrating or solving the initial-value problem with non-zerov_0. - Main-thread bound: Because animations run inside
Window::drawvianext_frame_callbacks(crates/gpui/src/window.rs:1878-1887), any main-thread hitch (even 8.4 ms on a 120 Hz ProMotion display) drops frames across every animation in the window.
- Immediate-mode subtree cloning on every tick: Look at
4.2 Why the Launcher and Scrawl Had to Bypass GPUI
Kit's own production apps (the launcher and scrawl) provide the most concrete empirical evidence of where GPUI breaks down:
Case Study A: The launcher (launcher/docs/{motion.md,perf-log.md})
The launcher originally built its panel and compact pill morph in GPUI (kitlangton/gpui) and hit two walls:
- Six structural motion/compositing defects (
launcher/docs/motion.md:20-75):- Two competing coordinate spaces: Moving/resizing the
NSWindowvia AppKit (window.resize()) while rendering the inner pill inside GPUI'sScenecaused 1-frame desynchronization ("two systems fighting over the same pixels"). - Top-left anchor jump on collapse: Switching from the expanded 680×424 window to the compact 100×36 pill caused the pill to render at
(0,0)(top-left of the large window) for 1–2 frames beforeNSWindow::setFrame:took effect. - Hard content pop & background snap: Because GPUI cannot cross-fade two retained view snapshots as offscreen textures with independent scale/blur transforms, the search bar and results list popped out in 1 frame (
Frame 12 → Frame 13), and background alpha snapped (0.95 → 0.30). - Fix in the launcher: The launcher replaced the GPUI morph and launcher panel with native AppKit/SwiftUI (
launcher/src/native_ui.rs:1-99) using a singleNSGlassEffectView/CAMetalLayerdriven byCASpringAnimationinWindowServerat a locked 120 Hz with zero per-frame Rust/CPU participation.
- Two competing coordinate spaces: Moving/resizing the
- Measured GPUI CPU frame times & cold-glyph spikes (
launcher/docs/perf-log.md:1-145):- Steady-state GPUI
Window::drawCPU frame time (cached): 2.65 ms before optimization, 1.46 ms after removing per-rowSyncListre-measurement (641 µs/frame wasted re-measuring row 0) andTextInputre-layout (266 µs/frame). - Cold glyph/emoji rasterization spikes: Opening the Emoji grid in GPUI caused 11.1–18.4 ms frame spikes (2.2× the 8.33 ms 120 Hz budget) because
AppleTextSystem::rasterize_glyph(crates/gpui_apple/src/text_system.rs:435-537) synchronously allocates aCGContext, renders theCTFontglyph on the CPU main thread, and uploads toMetalAtlasduringpaint. - Memory footprint: Even an idle GPUI window with
MetalRenderer(3× 2 MBInstanceBuffers +MetalAtlaspages + font caches) consumed 78.8 MB RSS at launch (launcher/docs/perf-log.md:135).
- Steady-state GPUI
Case Study B: Scrawl (scrawl/src/native/pill/mod.rs, scrawl/perf/start-latency.md)
Scrawl's HUD pill needs a liquid-glass capsule (NSGlassEffectView), an offscreen Gaussian-blurred waveform glow (glow_a, glow_b intermediate RGBA8Unorm textures), and 120 Hz spring animations:
- GPUI's
Scenecannot express multi-pass offscreen blur (Pass 1: Clear glow_a→Pass 2: H-blur glow_a -> glow_b→Pass 3: Composite V-blurred glow_b + SDF shapes + R8 atlas text onto drawable,scrawl/src/native/pill/mod.rs:11-23). - Scrawl replaced GPUI's
HudPillWindowwith a 440-line nativeNSPanel+CAMetalLayer+CVDisplayLinkmodule (scrawl/src/native/pill/{mod.rs,model.rs,atlas.rs,pill.metal}). - Measured startup win (
scrawl/perf/start-latency.md:26-35): DeferringNSPanel/CAMetalLayer/pipeline creation until the pill is shown cutHudPillHandle::spawn()cold-start latency from 6.93 ms to 0.08 ms (an 86× reduction), and the display link fully stops (CVDisplayLinkStop) when idle for 0% CPU/GPU usage.
4.3 Mobile (gpui_ios), Web (gpui_web), Hot Reload (subsecond), and Compile Times
gpui_ios(crates/gpui_ios/src/ios/{window.rs,text_input.rs}, 3,684 LoC): Usesobjc2-ui-kit(UIWindow,UIViewController,GPUIMetalViewwithCAMetalLayer,CADisplayLink,UIPanGestureRecognizer,UIScreenEdgePanGestureRecognizerfor back-swipe, andGPUITextInputViewimplementingUITextInput). Still early:TouchEventincrates/gpui/src/interactive.rs:114-117explicitly notes "Dispatch contract (core implementation pending)".gpui_web(crates/gpui_web/src/{window.rs,text_system.rs}, 7,866 LoC): Runs GPUI in WASM on a full-bleed<canvas>usinggpui_wgpu(WebGPUorWebGL2),ResizeObserver,requestAnimationFrame,ImeMirror, andWebTextSystem(CosmicTextSystem+<canvas>2D fallback for system CJK/emoji fonts).- Hot Reload (
crates/gpui/src/hot_reload.rs:1-82,crates/gpui/src/view.rs:344-352): Compiled when--cfg gpui_hot_reloadis active. Integrates Dioxus'ssubsecondbinary hot-patching crate (dx serve --hot-patch). EveryViewElementrender passes throughsubsecond::HotFn::current(render_view_impl::<V>).call((view, window, cx))(crates/gpui/src/view.rs:351), and whensubsecondapplies a jump-table patch,hot_reload::registermarks all views in all windows dirty and callswindow.refresh(). - Compile-time & Dependency Weight:
gpui+ platform crates total 174,157 LoC and pull in heavy transitive dependencies (taffy,lyon,resvg/usvg,etagere,cosmic-text/swash/fontdb,font-kit,accesskit,wgpu/naga,objc2-*/windows,blade-macros). Clean builds ofgpuitake ~25–45s on Apple Silicon M-series Max/Ultra, and incremental links withoutsubsecondorlld/wildtake 2–6s.
5. Warp's UI Framework (warpui_core + warpui)
5.1 Shared Genealogy with GPUI & Open-Source Status
In Warp's July 2021 engineering post ("How Warp Works"), Warp explained that they partnered with Nathan Sobo (before he left to co-found Zed) to build a Flutter-inspired Rust UI framework on top of Metal and SumTree. As of 2026, warpdotdev/warp (f8a9c71) is open-source on GitHub: the terminal application is AGPL-3.0, while crates/warpui_core (91,602 LoC) and crates/warpui (38,862 LoC) are MIT-licensed (crates/warpui_core/Cargo.toml:7, crates/warpui/Cargo.toml:7).
Studying warpui_core side-by-side with gpui reveals that warpui is the evolutionary sibling of GPUI v1:
- Both share
App/AppContext,EntityId,ViewHandle<V>/ModelHandle<M>,Action, hierarchicalKeymapcontext matching,Scene, andsum_tree. - Where Zed rewrote GPUI v1 into GPUI v2 (replacing Flutter constraints with Taffy flexbox and replacing retained
Box<dyn Element>trees with 3-phase arena-allocated immediate/retained elements), Warp kept and evolved the originalPresenterarchitecture.
5.2 How warpui Differs from GPUI v2
- Retained
Box<dyn Element>per View (Presenter,crates/warpui_core/src/presenter.rs:28-37, 318-381):Presenterholdsrendered_views: EntityIdMap<Box<dyn Element>>.- When a view is invalidated,
Presenter::invalidatecallsapp.render_view(window_id, view_id)only on the invalidatedview_ids and replaces theirBox<dyn Element>inrendered_views(presenter.rs:318-331). Unchanged child views are embedded viaChildView<T>(presenter.rs:282-287) and never re-runView::render! - Layout is single-pass Flutter-style box constraints (
Element::layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext, app: &AppContext) -> Vector2F,crates/warpui_core/src/elements/gui/mod.rs:109-115), followed byafter_layoutandpaint.
- Thread-Local Automatic Dependency Tracking (
Tracked<T>,crates/warpui_core/src/core/autotracking/mod.rs:1-200):- Unlike GPUI (where forgetting
cx.observe(&model, ...)orcx.notify()is a constant source of stale-UI bugs),warpui_coreprovidesTracked<T>. - During
View::render,autotracking::render_viewsets a thread-localCACHE.rendering_view = Some(View { window_id, view_id })(autotracking/mod.rs:174-192). - Reading a
Tracked<T>viaDerefautomatically records(View, TrackedId)in a bidirectionalHashMap. Mutating aTracked<T>viaDerefMutautomatically pushes all dependentViews intoCACHE.invalidations, whichAppdrains alongside manualctx.notify()calls!
- Unlike GPUI (where forgetting
- 3-Primitive Renderer (
Rect,Image/Icon,Glyph) +rstar::RTreeHit Map (crates/warpui_core/src/scene.rs:29-123):warpuihas no vector path tessellator (lyon) and no separate shadow or underline pipeline. Instead,Rect(crates/warpui/src/platform/mac/rendering/metal/shaders/shaders.metal:9-43, 117-200) evaluates drop shadow (4-step Gaussianerf), per-corner radius, per-edge solid/dashed border, and linear gradient inside a single uber-fragment shader (rect_fragment_shader)!- Icons and SVGs are rasterized once via
resvgintoTextureCacheand drawn viaimage_fragment_shader. - Glyphs (
glyph_fragment_shader) support horizontal alpha fades (GlyphFade::Horizontal { start, end },scene.rs:78-83) so overflowing terminal/tab text fades out smoothly in the shader without extra mask layers. - Each
LayerinScenebuilds anrstar::RTree<Rectangle>(scene.rs:31) of opaque hit rects soEventContext::is_covered(point)andvisible_rect(origin, size)can test z-layer occlusion during event dispatch (presenter.rs:246-258). - Weakness:
warpui's glyph atlas (crates/warpui/src/rendering/atlas/allocator.rs:13-55) is a naive shelf-next-fit allocator (ATLAS_SIZE = 1024) with zero eviction—whenGlyphConfigchanges it blows away all textures (glyph_cache.rs:80-86), otherwise it allocates new 1024×1024MTLTextures forever.
- Dual GUI + Ratatui TUI Runtime (
crates/warpui_core/src/presenter/tui.rs:1-150):- Enabled via
--features tui(ratatui = "0.30"). Allows the exact sameApp,Model,EntityId,Action, andKeymapcore to render either to a GPU window (Presenter→Scene) or to a terminal screen (TuiPresenter→TuiFramecell buffer).
- Enabled via
- Platform Split:
- macOS uses custom AppKit + raw Metal (
crates/warpui/src/platform/mac/rendering/metal/renderer.rs:1-200), reporting 1.9 ms average redraw time (>144 FPS on 4K) in Warp's telemetry. - Linux, Windows, and WASM use
winit+wgpu+cosmic-text/swash(crates/warpui/src/windowing/winit/andcrates/warpui/src/rendering/wgpu/renderer.rs:1-145).
- macOS uses custom AppKit + raw Metal (
6. What to Steal vs. What to Avoid
Steal (With Exact File Citations)
- Zed's
sum_tree+rope(Chunkwith 4×u128bitmaps):- Source:
zed:crates/sum_tree/src/sum_tree.rs:18-200andzed:crates/rope/src/{rope.rs,chunk.rs:9-102}. - Why: Self-contained (7,765 LoC combined for
sum_tree+rope), zero GPUI dependency,O(1)clone (Arc),O(\log_{12} N)multi-dimensional seek across byte/char/UTF-16/Point(row, col), and hardware-acceleratedpopcnt/tzcntwithin 128-byte leaf chunks. Use it directly or vendor it verbatim for Rune's text buffers and virtualized list height prefix sums.
- Source:
BoundsTreeSpatial Z-Order Assignment for Instanced Batching:- Source:
zed:crates/gpui/src/bounds_tree.rs:14-180andzed:crates/gpui/src/scene.rs:171-206. - Why: A ~200-line 4-ary R-tree that assigns each primitive
max_overlapping_order + 1instead of a global counter. This turns painter's-algorithm trees (e.g. 100 list rows each drawing a background quad + icon + text) into 3 instanced GPU draw calls instead of 300.
- Source:
- Kit's GPUI fork (
kitlangton/gpui) (2582af8) Rendering & DisplayLink Fixes:- Source:
kitlangton/gpuicommit2582af8:- Triangular blue-noise dithering in both gradients and Gaussian drop shadows (
src/platform/mac/shaders.metal:326-331, 495-503). - Perceptual luminance-driven 5-step CoreText glyph dilation (
src/platform/mac/text_system.rs:274-305, 514-525). - Explicit
kCGColorSpaceSRGB(orkCGColorSpaceExtendedSRGB/DisplayP3) tagging onCAMetalLayer(src/platform/mac/metal_renderer.rs:257-263). CVDisplayLinkCreateWithCGDisplay→CVDisplayLinkCreateWithActiveCGDisplaysfallback (src/platform/mac/display_link.rs:25-60) + complete stop when idle (scrawl/src/native/pill/mod.rs:310-340).
- Triangular blue-noise dithering in both gradients and Gaussian drop shadows (
- Source:
- Two-Generation
LineLayoutCache+shape_line_by_hash+ Ligature-Breaking ULP Trick:- Source:
zed:crates/gpui/src/text_system/line_layout.rs:458-560,zed:crates/gpui/src/text_system.rs:689-720,zed:crates/gpui_apple/src/text_system.rs:567-579. - Why: Swapping
current_frameandprevious_frameFxHashMaps gives zero-overhead frame-to-frame layout retention and automatic eviction of offscreen lines without LRU bookkeeping, whilepx(font_size.next_up())cleanly breaks CoreText ligatures across syntax/selection boundaries.
- Source:
- Hit-Testing Between
prepaint(Layout Resolved) andpaint:- Source:
zed:crates/gpui/src/window.rs:3728-3765. - Why: Running
hit_test(mouse_position)immediately after layout/hitbox registration—beforepaint—means hover states and cursors are always 0-frame-lag accurate even when layout shifts under the cursor.
- Source:
- Warp's
Tracked<T>Automatic Dependency Tracking &GlyphFade:- Source:
warp:crates/warpui_core/src/core/autotracking/mod.rs:1-200andwarp:crates/warpui_core/src/scene.rs:78-89. - Why: Automatic read-tracking during
rendereliminates forgottencx.notify()/cx.observe()bugs; shader-level horizontalGlyphFadeclips overflowing text with a smooth gradient at zero extra draw-call or mask-texture cost.
- Source:
- Dioxus
subsecondJump-Table Hot Patching Hook at View Boundaries:- Source:
zed:crates/gpui/src/hot_reload.rs:1-82andzed:crates/gpui/src/view.rs:344-352. - Why: Wrapping view render functions in
subsecond::HotFn::current(...)gives sub-second Rust UI hot-reloading while preserving liveApp/entity state.
- Source:
Avoid (With Concrete Reasons)
- Avoid GPUI's Full-Viewport 4x MSAA Intermediate Pass per
PathBatch:- Where:
zed:crates/gpui_apple/src/metal_renderer.rs:1100-1215. - Why: Clearing and resolving a full-window 4x MSAA texture on every interleaved path batch destroys tile memory bandwidth. Either render vector shapes via analytical SDFs / Vello / slug-style compute shaders, or scissor-clip the MSAA resolve to the tight union of the batch's
Bounds.
- Where:
- Avoid GPUI's
EntityMapRemove-on-Lease Pattern Without Granular State Splits:- Where:
zed:crates/gpui/src/app/entity_map.rs:138-233. - Why: Leasing an entire view/model out of
SecondaryMapguarantees runtimedouble_lease_paniccrashes whenever nested callbacks, observers, or OS delegates fire synchronously. Fine-grained reactive signals/atoms or split state cells avoid whole-view lease panics.
- Where:
- Avoid Animation-by-Element-Subtree-Rebuild (
AnimationElement/SpringElement):- Where:
zed:crates/gpui/src/elements/animation.rs:208-246andzed:crates/gpui/src/spring.rs:410-465. - Why: Cloning the element subtree and re-running Taffy layout + full-window CPU/GPU draw on every 120 Hz tick wastes CPU and prevents compositor-level transform/opacity/filter animations. Springs must also preserve velocity
(\dot{x}_0 \ne 0)on retargeting.
- Where:
- Avoid Lack of Offscreen Compositor Layers / Filter Passes:
- Where:
zed:crates/gpui/src/scene.rs:34-46. - Why: Without offscreen layer targets (
RenderLayerwith group opacity, transform matrix, clip mask, Gaussian blur, and blend mode), you cannot build Scrawl-grade glass morphs, cross-dissolving view transitions, or glow passes without dropping down to rawCAMetalLayer/CALayer.
- Where:
- Avoid Fragile Vector-Slice Index Caching (
reuse_prepaint/reuse_paint):- Where:
zed:crates/gpui/src/window.rs:4003-4123andzed:crates/gpui/src/view.rs:540-642. - Why: Caching views by recording
(start_idx..end_idx)slices into global per-frame vectors breaks whenever deferred draws or input handlers shift indices, and still forces the CPU to copy primitives and rebuildBoundsTreeevery frame. Retain per-view display lists (RenderNode/Layer) directly!
- Where:
- Avoid Synchronous Main-Thread Glyph/Emoji Rasterization Without Prewarming/Async Staging:
- Where:
zed:crates/gpui_apple/src/text_system.rs:435-537(launcher/docs/perf-log.md:72-97). - Why: Cold CoreText/CGContext rasterization of 50+ emojis stalls the main thread for 11–18 ms, blowing past the 8.33 ms 120 Hz budget.
- Where:
7. Recommendation for Rune
Decisive Verdict: Build Fresh (Modular Crate Architecture) — Do NOT Fork GPUI or warpui
Rune should NOT fork GPUI (neither gpui 0.2.2 nor Zed HEAD 5447804) and should NOT fork warpui. Instead, Rune should build a fresh, layered UI & rendering engine while directly vendoring/adapting Zed's standalone text crates (sum_tree, rope) and porting ~1,500 lines of proven low-level algorithms (BoundsTree, etagere atlas + LineLayoutCache, and kitlangton/gpui 2582af8 shader/text refinements).
Why Starting Fresh Beats Forking GPUI
- The 4 architectural walls that forced the launcher and Scrawl off GPUI are load-bearing in GPUI's core:
- In GPUI,
Window(crates/gpui/src/window.rs, 9,979 LoC) +App(crates/gpui/src/app.rs) +Scene(crates/gpui/src/scene.rs) are tightly coupled around a single-pass, flat-vector, full-window-clear frame loop. - Adding retained compositor layers (group opacity, 2D/3D layer transforms, offscreen blur/glow render passes like Scrawl's 3-pass HUD pill, and cross-fade view transitions like the launcher's morph), damage-tracked partial redraws, velocity-preserving retargetable springs, and fine-grained reactive state that doesn't panic on re-entrant borrows requires replacing
Scene,MetalRenderer::draw,ViewElementState,AnimationElement, andEntityMap—i.e., rewriting the heart of GPUI while fighting 174,157 lines of Zed-specific coupling.
- In GPUI,
- Massive complexity and compile-time tax for features Rune doesn't want:
- Upstream GPUI is 174k LoC (and growing rapidly for Zed's specific IDE needs). Maintaining a fork of GPUI (as
kitlangton/gpuialready demonstrated) means either freezing on an old snapshot (0.2.2, losinggpui_wgpu,gpui_ios,gpui_web,AccessKit,subsecond) or rebasing against thousands of Zed-internal commits per month.
- Upstream GPUI is 174k LoC (and growing rapidly for Zed's specific IDE needs). Maintaining a fork of GPUI (as
- The highest-leverage parts of Zed are already small and separable:
sum_tree(3,327 LoC) +rope(4,438 LoC) have zero dependency ongpui.BoundsTreeis 240 LoC;MetalAtlas(etagere) is 405 LoC;shaders.metal(with2582af8fixes) is 910 LoC;LineLayoutCacheis ~300 LoC. You can bring all of GPUI's best engineering into Rune in <10,000 LoC without inheriting the 165,000 LoC of baggage.
Rejected Alternatives & Why
- Rejected Alternative 1: Forking
kitlangton/gpui(gpui 0.2.2+2582af8)- Why rejected:
gpui 0.2.2is a 97k-LoC monolithic crate frozen before Zed'sgpui_apple/gpui_wgpu/gpui_ios/gpui_web/AccessKit/subsecondwork. It still suffers from the 11–18 ms cold emoji stalls, 78.8 MB idle RSS,double_lease_panicre-entrancy, lack of offscreen passes, and full-window redraws that forced the launcher and Scrawl to write native SwiftUI/Metal escape hatches.
- Why rejected:
- Rejected Alternative 2: Tracking Upstream Zed's
gpui(5447804) as a Git Dependency or Rebased Fork- Why rejected: Upstream
gpuiis not published as a stable independent product—its APIs churn weekly to serve Zed, it still lacks two of Kit's2582af8fixes (shadow dithering andCVDisplayLinkfallback), and its flatScene+InstanceBufferarchitecture still cannot do offscreen blur/glow passes, compositor-driven morphs, or partial-damage rendering.
- Why rejected: Upstream
- Rejected Alternative 3: Forking Warp's
warpui_core+warpui(f8a9c71, MIT)- Why rejected: Although MIT-licensed and possessing nice ideas (
Tracked<T>, 3-shader simplicity, Ratatui TUI backend),warpuiis 130,464 LoC of 2021-era GPUI-v1 architecture with custom Flutter-style layout (no CSS Grid/Taffy), a naive shelf-next-fit glyph atlas with no eviction, no subpixel AA or luminance glyph dilation, and heavy Warp-specific telemetry/asset coupling.
- Why rejected: Although MIT-licensed and possessing nice ideas (
8. Open Questions for Rune's Design
- Compositor Layer Hybrid vs. Pure GPU Scene: Should Rune's window compositor use native
CALayer/CAMetalLayersublayers for window-level glass/morph surfaces (like the launcher and Scrawl proved on macOS viaNSGlassEffectView+WindowServerCASpringAnimation) while running a multi-pass GPU scene graph inside each surface, or should Rune implement its own multi-pass offscreen layer compositor across all platforms? - Vector Path Rendering Strategy: Since GPUI's Lyon + 4x MSAA full-window intermediate pass is a major bottleneck and
warpuiskips arbitrary paths entirely, should Rune restrict UI primitives to analytical SDFs +resvgsprite caching, or adopt a tile-based GPU path rasterizer? - Reactive State Model: Should Rune adopt
warpui-style automatic read tracking (Tracked<T>/ fine-grained signals) so views never need manualcx.notify()and never hitdouble_lease_panicon re-entrant reads, or keep explicit entity handles with split read/write arenas? - Text Buffer Scope: Does Rune only need
sum_tree+rope+ single-buffer undo/anchors (~10k LoC), or does it also need Zed's fullmulti_buffer+ 5-layerDisplayMap(InlayMap→FoldMap→TabMap→WrapMap→BlockMap) for code-editor-class views?