Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(rendering): wait on the frame's submission in the preview and readback waits
Harsha Vardhan987634936ab9
diff --git a/apps/desktop-gpui/src/camera_blur.rs b/apps/desktop-gpui/src/camera_blur.rsindex e3e745013..4f72cdeb7 100644--- a/apps/desktop-gpui/src/camera_blur.rs+++ b/apps/desktop-gpui/src/camera_blur.rs@@ -128,8 +128,8 @@ pub struct BlurOutput {/// resource is dropped on exit, the same release-on-off behaviour as/// `release_blur_resources` (`camera.rs:1477-1484`).pub fn run(jobs: flume::Receiver<BlurJob>, results: flume::Sender<BlurOutput>) {- // Current-thread runtime for `request_adapter` and the tokio yields- // inside `PendingSurface::wait`.+ // Current-thread runtime for `request_adapter` and the timed blocking+ // wait inside `PendingSurface::wait`.let runtime = match tokio::runtime::Builder::new_current_thread().enable_time().build()@@ -293,10 +293,10 @@ impl Worker {30,)).map_err(|error| anyhow!("{error}"))?;- self.queue.submit(std::iter::once(encoder.finish()));+ let submission = self.queue.submit(std::iter::once(encoder.finish()));let frame = runtime- .block_on(pending.wait(&self.device, &self.queue))+ .block_on(pending.wait(&self.device, submission)).map_err(|error| anyhow!("{error}"))?;let cutout = if mode == cap_camera_effects::BlurMode::Remove {use core_foundation::base::TCFType as _;diff --git a/crates/rendering/examples/preview-surface-benchmark.rs b/crates/rendering/examples/preview-surface-benchmark.rsindex 33fec64de..d7e71fbb9 100644--- a/crates/rendering/examples/preview-surface-benchmark.rs+++ b/crates/rendering/examples/preview-surface-benchmark.rs@@ -330,8 +330,12 @@ fn fs_bgra(in: VertexOutput) -> @location(0) vec4<f32> {pool.pixel_buf().expect("pixel buffer")}- fn submit_and_wait(device: &wgpu::Device, queue: &wgpu::Queue, encoder: wgpu::CommandEncoder) {- queue.submit(std::iter::once(encoder.finish()));+ fn submit_and_wait(+ device: &wgpu::Device,+ queue: &wgpu::Queue,+ encoder: wgpu::CommandEncoder,+ ) -> wgpu::SubmissionIndex {+ let submission = queue.submit(std::iter::once(encoder.finish()));let done = Arc::new(AtomicBool::new(false));let signal = Arc::clone(&done);queue.on_submitted_work_done(move || signal.store(true, Ordering::Release));@@ -339,6 +343,7 @@ fn fs_bgra(in: VertexOutput) -> @location(0) vec4<f32> {let _ = device.poll(wgpu::PollType::Poll);std::hint::spin_loop();}+ submission}struct Stats {@@ -592,9 +597,9 @@ fn fs_bgra(in: VertexOutput) -> @location(0) vec4<f32> {let pending = runtime.block_on(bgra.encode(&device, &mut bgra_encoder, &source, width, height, 0, 60)).expect("bgra encode");- submit_and_wait(&device, &queue, bgra_encoder);+ let submission = submit_and_wait(&device, &queue, bgra_encoder);let bgra_frame = runtime- .block_on(pending.wait(&device, &queue))+ .block_on(pending.wait(&device, submission)).expect("bgra frame");// Paint-phase inputs: sampled imports of the converted surfaces,diff --git a/crates/rendering/src/frame_pipeline.rs b/crates/rendering/src/frame_pipeline.rsindex e2681b06d..cf5028200 100644--- a/crates/rendering/src/frame_pipeline.rs+++ b/crates/rendering/src/frame_pipeline.rs@@ -79,6 +79,34 @@ pub(crate) mod readback_delay {}}+async fn wait_for_submission(+ device: &wgpu::Device,+ submission: &wgpu::SubmissionIndex,+ started: Instant,+ frame_number: u32,+ timeout: std::time::Duration,+) -> bool {+ loop {+ let Some(remaining) = timeout.checked_sub(readback_waited(started, frame_number)) else {+ return false;+ };+ let device = device.clone();+ let submission = submission.clone();+ let waited = tokio::time::timeout(+ remaining,+ tokio::task::spawn_blocking(move || {+ device.poll(wgpu::PollType::WaitForSubmissionIndex(submission))+ }),+ )+ .await;+ match waited {+ Ok(Ok(Ok(_))) => return readback_waited(started, frame_number) <= timeout,+ Ok(Ok(Err(wgpu::PollError::Timeout))) => {}+ _ => return false,+ }+ }+}+#[derive(Clone, Copy, Debug, Default)]pub struct FinishEncoderTimings {pub wait_previous_duration: std::time::Duration,@@ -1593,28 +1621,19 @@ impl PendingSurface {pub async fn wait(self,device: &wgpu::Device,- queue: &wgpu::Queue,+ submission: wgpu::SubmissionIndex,) -> Result<SurfaceFrame, RenderingError> {- let completed = Arc::new(AtomicBool::new(false));- let callback_completed = Arc::clone(&completed);- queue.on_submitted_work_done(move || {- callback_completed.store(true, Ordering::Release);- });let started = Instant::now();- let mut poll_count = 0u32;- while !completed.load(Ordering::Acquire) {- if started.elapsed() > gpu_buffer_wait_timeout(false) {- return Err(RenderingError::BufferMapWaitingFailed);- }- device.poll(wgpu::PollType::Poll)?;- poll_count += 1;- if poll_count < 10 {- tokio::task::yield_now().await;- } else if poll_count < 100 {- tokio::time::sleep(std::time::Duration::from_micros(100)).await;- } else {- tokio::time::sleep(std::time::Duration::from_millis(1)).await;- }+ if !wait_for_submission(+ device,+ &submission,+ started,+ self.frame_number,+ gpu_buffer_wait_timeout(false),+ )+ .await+ {+ return Err(RenderingError::BufferMapWaitingFailed);}Ok(SurfaceFrame {@@ -1631,6 +1650,7 @@ impl PendingSurface {pub struct PendingReadback {readiness_phase: Option<crate::readiness::Phase>,rx: oneshot::Receiver<Result<(), wgpu::BufferAsyncError>>,+ submission: wgpu::SubmissionIndex,mapped: bool,buffer: Arc<wgpu::Buffer>,padded_bytes_per_row: u32,@@ -1665,53 +1685,29 @@ impl PendingReadback {}pub async fn wait(mut self, device: &wgpu::Device) -> Result<RenderedFrame, RenderingError> {- let mut poll_count = 0u32;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 {+ let completed = wait_for_submission(+ device,+ &self.submission,+ start_time,+ self.frame_number,+ gpu_buffer_wait_timeout(self.export_deadlines),+ )+ .await;+ match (completed, self.rx.try_recv()) {+ (true, Ok(Ok(()))) => {}+ (_, Ok(Err(error))) => {+ self.unmap();+ return Err(RenderingError::BufferMapFailed(error));+ }+ _ => {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());}-- match self.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)?;- poll_count += 1;- if poll_count < 10 {- tokio::task::yield_now().await;- } else if poll_count < 100 {- tokio::time::sleep(std::time::Duration::from_micros(100)).await;- } else {- tokio::time::sleep(std::time::Duration::from_millis(1)).await;- }- if poll_count.is_multiple_of(10000) {- tracing::warn!(- frame_number = self.frame_number,- poll_count = poll_count,- elapsed_ms = start_time.elapsed().as_millis() as u64,- "GPU buffer mapping taking longer than expected"- );- }- }- Err(oneshot::error::TryRecvError::Closed) => {- return Err(self.cancel());- }- }}if let Some(phase) = &self.readiness_phase {@@ -1899,7 +1895,7 @@ impl PipelinedGpuReadback {if let Some(phase) = &phase {phase.mark("submit_start");}- queue.submit(std::iter::once(render_encoder.finish()));+ let submission = queue.submit(std::iter::once(render_encoder.finish()));if let Some(phase) = &phase {phase.mark("submit_returned");}@@ -1917,6 +1913,7 @@ impl PipelinedGpuReadback {readiness_phase: phase,mapped: true,rx,+ submission,buffer,padded_bytes_per_row,width: uniforms.output_size.0,@@ -2301,11 +2298,11 @@ pub async fn finish_encoder_bgra_surface(if let Some(phase) = &phase {phase.mark("submit_start");}- queue.submit(std::iter::once(encoder.finish()));+ let submission = queue.submit(std::iter::once(encoder.finish()));if let Some(phase) = &phase {phase.mark("submit_returned");}- let result = pending.wait(device, queue).await;+ let result = pending.wait(device, submission).await;if let Some(phase) = phase {phase.finish(if result.is_ok() {"frame_ready"@@ -2896,9 +2893,9 @@ mod surface_output_tests {).await.expect("BGRA output");- queue.submit(std::iter::once(encoder.finish()));+ let submission = queue.submit(std::iter::once(encoder.finish()));let frame = pending- .wait(&device, &queue)+ .wait(&device, submission).await.expect("BGRA completion");expected.push(bgra_first_pixel(&frame));@@ -2925,8 +2922,8 @@ mod surface_output_tests {.encode(device, &mut encoder, source, width, height, 0, 30).await.expect("BGRA submission");- queue.submit(std::iter::once(encoder.finish()));- pending.wait(device, queue).await.expect("BGRA output")+ let submission = queue.submit(std::iter::once(encoder.finish()));+ pending.wait(device, submission).await.expect("BGRA output")}#[tokio::test]diff --git a/vendor/wgpu-hal/src/dx12/device.rs b/vendor/wgpu-hal/src/dx12/device.rsindex 7de69e368..9580b82c5 100644--- a/vendor/wgpu-hal/src/dx12/device.rs+++ b/vendor/wgpu-hal/src/dx12/device.rs@@ -1992,7 +1992,11 @@ impl crate::Device for super::Device {return Ok(true);}- unsafe { fence.raw.SetEventOnCompletion(value, self.idler.event.0) }+ // Backport of gfx-rs/wgpu#8273: the shared auto-reset idler event loses wakeups when+ // several threads wait on the same device, stranding a waiter until its timeout.+ let event = Event::create(false, false)?;++ unsafe { fence.raw.SetEventOnCompletion(value, event.0) }.into_device_result("Set event")?;let start_time = Instant::now();@@ -2032,7 +2036,7 @@ impl crate::Device for super::Device {match unsafe {Threading::WaitForSingleObject(- self.idler.event.0,+ event.0,remaining_wait_duration.as_millis().try_into().unwrap(),)} {diff --git a/vendor/wgpu-hal/src/metal/command.rs b/vendor/wgpu-hal/src/metal/command.rsindex ad946e55a..5e4ac682c 100644--- a/vendor/wgpu-hal/src/metal/command.rs+++ b/vendor/wgpu-hal/src/metal/command.rs@@ -30,6 +30,7 @@ impl Default for super::CommandState {impl super::CommandEncoder {// Cap-local patch to the vendored wgpu-hal; reapply it whenever vendor/wgpu-hal is replaced.+ // The other local patch is the per-wait event in dx12/device.rs `Device::wait`.// wgpu-core opens a HAL command buffer for every pass, plus one for the barriers it// records ahead of it. Barriers are no-ops on Metal, so the MTLCommandBuffer is only// created once something is actually encoded and empty ones are never committed