Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(rendering): draw one warmup frame after launch so the first preview skips the driver's first-draw cost
Richie McIlroye979a8db3350
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rsindex 107d9f438..5d8a14f4d 100644--- a/apps/desktop-gpui/src/app_windows.rs+++ b/apps/desktop-gpui/src/app_windows.rs@@ -4883,10 +4883,11 @@ pub(crate) fn prewarm_shared_wgpu_device(cx: &App) {const LAUNCH_RENDER_WARMUP_DELAY: std::time::Duration = std::time::Duration::from_secs(3);/// The first editor open in a process paid for the system font scan, the-/// shared device and the renderer's first pipeline compiles before its first-/// frame: 0.7-1.2 s, against ~0.2 s for every later open. This warms all three-/// a few seconds after launch, off the main thread. The layers are dropped-/// again; only the driver's compiled pipelines and the font database stay.+/// shared device and the renderer's first pipeline compiles and draw before+/// its first frame: 0.7-1.2 s, against ~0.2 s for every later open. This warms+/// them a few seconds after launch, off the main thread. The layers are+/// dropped again; only the driver's compiled pipelines, the device and the+/// font database stay.pub(crate) fn schedule_launch_render_warmup(cx: &App) {gpui_tokio::Tokio::spawn(cx, async {tokio::time::sleep(LAUNCH_RENDER_WARMUP_DELAY).await;@@ -4911,11 +4912,20 @@ async fn warm_renderer() -> Option<f64> {}let shared = shared_wgpu_device().await?;let started = std::time::Instant::now();- tokio::task::spawn_blocking(move || {- cap_rendering::prewarm_renderer(&shared.device, &shared.queue, shared.is_software_adapter)+ let (device, queue, software) = (+ shared.device.clone(),+ shared.queue.clone(),+ shared.is_software_adapter,+ );+ let mut layers = tokio::task::spawn_blocking(move || {+ cap_rendering::RendererLayers::new_with_options(&device, &queue, software)}).await.ok()?;+ if let Err(error) = cap_rendering::prewarm_first_frame(shared, &mut layers).await {+ tracing::warn!(%error, "launch render warmup frame failed");+ }+ drop(layers);Some(started.elapsed().as_secs_f64() * 1000.0)}diff --git a/crates/editor/examples/editor-startup-benchmark.rs b/crates/editor/examples/editor-startup-benchmark.rsindex 7583b126a..09f1fdf8a 100644--- a/crates/editor/examples/editor-startup-benchmark.rs+++ b/crates/editor/examples/editor-startup-benchmark.rs@@ -215,11 +215,15 @@ async fn main() {shared.queue.clone(),shared.is_software_adapter,);- tokio::task::spawn_blocking(move || {- cap_rendering::prewarm_renderer(&device, &queue, software)+ let mut layers = tokio::task::spawn_blocking(move || {+ cap_rendering::RendererLayers::new_with_options(&device, &queue, software)}).await.expect("Renderer warmup failed");+ cap_rendering::prewarm_first_frame(clone_shared_device(&shared), &mut layers)+ .await+ .expect("Renderer warmup frame failed");+ drop(layers);shared_device = Some(shared);println!("Renderer warmup before startup timing: {:.3}ms",diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rsindex f6f9a1fa7..ef7301a45 100644--- a/crates/rendering/src/lib.rs+++ b/crates/rendering/src/lib.rs@@ -96,17 +96,90 @@ pub fn prewarm_fonts() {drop(layers::new_font_system());}-/// Builds and drops a full set of renderer layers on `device`. The first build-/// in a process waits on the driver compiling every layer's pipelines (about-/// half a second on macOS); once that has happened, later builds take a few-/// milliseconds, so warming it off the hot path keeps the first editor open-/// from waiting on it.-pub fn prewarm_renderer(device: &wgpu::Device, queue: &wgpu::Queue, prefer_cpu_conversion: bool) {- drop(RendererLayers::new_with_options(- device,- queue,- prefer_cpu_conversion,- ));+/// Renders one small RGBA frame and one NV12 frame through the full frame+/// pipeline with `layers`, then discards them. A fresh process waits on the+/// driver finishing each pipeline the first time it draws (the first preview+/// of the first editor took ~50 ms longer than any later one); building+/// `layers` and drawing once off the hot path moves both costs there.+pub async fn prewarm_first_frame(+ shared: SharedWgpuDevice,+ layers: &mut RendererLayers,+) -> Result<(), RenderingError> {+ const SIZE: u32 = 64;+ let studio_meta = StudioRecordingMeta::SingleSegment {+ segment: cap_project::SingleSegment {+ display: cap_project::VideoMeta {+ path: "prewarm.png".into(),+ fps: 30,+ start_time: Some(0.0),+ device_id: None,+ },+ camera: None,+ audio: None,+ cursor: None,+ },+ };+ let recording_meta = RecordingMeta {+ platform: None,+ project_path: std::env::temp_dir(),+ pretty_name: "Prewarm".to_string(),+ sharing: None,+ inner: cap_project::RecordingMetaInner::Studio(Box::new(studio_meta.clone())),+ upload: None,+ };+ let constants = RenderVideoConstants::from_shared_device(+ shared,+ RenderOptions {+ screen_size: XY::new(SIZE, SIZE),+ camera_size: None,+ preserve_screen_alpha: true,+ },+ studio_meta,+ recording_meta,+ Arc::new(BackgroundTextureCache::default()),+ );+ let config = ProjectConfiguration::default();+ let cursor_events = CursorEvents::default();+ let mut zoom_timeline = ZoomTransformTimeline::new(+ &[],+ None,+ &cursor_events,+ config.screen_movement_spring,+ 0.0,+ None,+ );+ zoom_timeline.ensure_precomputed_until(1.0 / 30.0);+ let (base_w, base_h) = ProjectUniforms::get_base_size(&constants.options, &config);+ let mut frame_renderer = FrameRenderer::new(&constants);+ let pixels = (SIZE * SIZE) as usize;+ for screen_frame in [+ DecodedFrame::new(vec![255; pixels * 4], SIZE, SIZE),+ DecodedFrame::new_nv12(vec![128; pixels * 3 / 2], SIZE, SIZE, SIZE, SIZE),+ ] {+ let segment_frames = DecodedSegmentFrames {+ screen_size: XY::new(SIZE, SIZE),+ screen_frame: Some(screen_frame),+ camera_frame: None,+ segment_time: 0.0,+ recording_time: 0.0,+ segment_has_camera: false,+ };+ let uniforms = ProjectUniforms::new(+ &constants,+ &config,+ 0,+ 30,+ XY::new(base_w, base_h),+ &cursor_events,+ &segment_frames,+ 0.0,+ &zoom_timeline,+ );+ frame_renderer+ .render_immediate(segment_frames, uniforms, &cursor_events, true, layers)+ .await?;+ }+ Ok(())}/// Unique installed font family names, sorted, for the editor's font picker.