Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Merge remote-tracking branch 'origin/main' into perf/windows-media-pipeline
Marcos Daniel Rivera Gonzalezf7aa44c1140f
diff --cc crates/rendering/src/frame_pipeline.rsindex f9ab6928c,0381ce67a..39dc27cc3--- a/crates/rendering/src/frame_pipeline.rs+++ b/crates/rendering/src/frame_pipeline.rs@@@ -35,43 -39,44 +38,88 @@@ fn gpu_buffer_wait_timeout(export_deadlstd::time::Duration::from_secs(secs)}+enum MapWait {+ Mapped,+ Failed(wgpu::BufferAsyncError),+ Abandoned,+}++// Blocking on the submission's fence wakes the moment the GPU signals it. Sleep-polling+// instead rounds every wait up to the OS timer tick (~15.6ms on Windows outside+// timeBeginPeriod), which capped preview renders at ~25ms regardless of resolution.+async fn wait_for_map(+ device: &wgpu::Device,+ submission: &wgpu::SubmissionIndex,+ rx: &mut oneshot::Receiver<Result<(), wgpu::BufferAsyncError>>,++ export_deadlines: bool,++ frame_number: u32,+) -> Result<MapWait, RenderingError> {++ let Some(budget) = gpu_buffer_wait_timeout(export_deadlines)++ .checked_sub(readback_waited(Instant::now(), frame_number))++ else {++ return Ok(MapWait::Abandoned);++ };+ let device = device.clone();+ let submission = submission.clone();+ let waited = tokio::time::timeout(- gpu_buffer_wait_timeout(),++ budget,+ tokio::task::spawn_blocking(move || {+ device.poll(wgpu::PollType::WaitForSubmissionIndex(submission))+ }),+ )+ .await;+ match waited {+ Ok(Ok(result)) => {+ result?;+ }+ _ => return Ok(MapWait::Abandoned),+ }+ // The wait fires the map callbacks of every submission up to this one.+ Ok(match rx.try_recv() {+ Ok(Ok(())) => MapWait::Mapped,+ Ok(Err(error)) => MapWait::Failed(error),+ Err(_) => MapWait::Abandoned,+ })+}++ /// How long the readback of `frame_number` has been waited for.+ fn readback_waited(started: Instant, frame_number: u32) -> std::time::Duration {+ #[cfg(test)]+ {+ started.elapsed() + readback_delay::injected(frame_number)+ }+ #[cfg(not(test))]+ {+ let _ = frame_number;+ started.elapsed()+ }+ }++ /// Test seam: makes one frame's readback look older than it is, to exercise+ /// the readback deadlines without waiting for them.+ #[cfg(test)]+ pub(crate) mod readback_delay {+ use std::{cell::RefCell, collections::HashMap, time::Duration};++ thread_local! {+ static DELAYS: RefCell<HashMap<u32, Duration>> = RefCell::new(HashMap::new());+ }++ pub(crate) fn inject(frame_number: u32, delay: Duration) {+ DELAYS.with(|delays| delays.borrow_mut().insert(frame_number, delay));+ }++ pub(super) fn injected(frame_number: u32) -> Duration {+ DELAYS.with(|delays| {+ delays+ .borrow()+ .get(&frame_number)+ .copied()+ .unwrap_or_default()+ })+ }+ }+#[derive(Clone, Copy, Debug, Default)]pub struct FinishEncoderTimings {pub wait_previous_duration: std::time::Duration,@@@ -1085,13 -1108,37 +1154,21 @@@ impl PendingNv12Readbackreturn Err(self.cancel());};- match wait_for_map(device, &submission, &mut rx).await? {- let start_time = Instant::now();- let timeout_duration = gpu_buffer_wait_timeout(self.export_deadlines);-- loop {- if readback_waited(start_time, self.frame_number) > timeout_duration {- return Err(self.cancel());- }-- match rx.try_recv() {- Ok(result) => match result {- Ok(()) => break,- Err(error) => {- self.unmap();- return Err(RenderingError::BufferMapFailed(error));- }- },- Err(oneshot::error::TryRecvError::Empty) => {- device.poll(wgpu::PollType::Poll)?;- // tokio's timer ticks in whole milliseconds, so even a- // 100µs sleep outlasts a frame's GPU work and caps the- // export at the timer rate; sleep only once a frame is slow.- if start_time.elapsed() < NV12_READBACK_SPIN {- tokio::task::yield_now().await;- } else {- tokio::time::sleep(std::time::Duration::from_millis(1)).await;- }- }- Err(oneshot::error::TryRecvError::Closed) => {- return Err(self.cancel());- }++ match wait_for_map(++ device,++ &submission,++ &mut rx,++ self.export_deadlines,++ self.frame_number,++ )++ .await?++ {+ MapWait::Mapped => {}+ MapWait::Failed(error) => {+ self.unmap();+ return Err(RenderingError::BufferMapFailed(error));}+ MapWait::Abandoned => return Err(self.cancel()),}let buffer_slice = self.buffer.slice(..);@@@ -1608,18 -1655,18 +1686,26 @@@ impl PendingReadback}pub async fn wait(mut self, device: &wgpu::Device) -> Result<RenderedFrame, RenderingError> {- let mut poll_count = 0u32;let start_time = Instant::now();- match wait_for_map(device, &self.submission, &mut self.rx).await? {- let timeout_duration = gpu_buffer_wait_timeout(self.export_deadlines);-- loop {- if readback_waited(start_time, self.frame_number) > timeout_duration {++ match wait_for_map(++ device,++ &self.submission,++ &mut self.rx,++ self.export_deadlines,++ self.frame_number,++ )++ .await?++ {+ MapWait::Mapped => {}+ MapWait::Failed(error) => {+ self.unmap();+ return Err(RenderingError::BufferMapFailed(error));+ }+ MapWait::Abandoned => {tracing::error!(frame_number = self.frame_number,- elapsed_secs = start_time.elapsed().as_secs(),- poll_count = poll_count,- "GPU buffer mapping timed out after {}s",- timeout_duration.as_secs()+ elapsed_ms = start_time.elapsed().as_millis() as u64,+ "GPU buffer mapping timed out or was abandoned");return Err(self.cancel());}