Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
improve: batch camera layer YUV conversion into shared GPU encoder
Richie McIlroye2955540e30d
diff --git a/crates/rendering/src/layers/camera.rs b/crates/rendering/src/layers/camera.rsindex 700bf52c4..7a6b04d21 100644--- a/crates/rendering/src/layers/camera.rs+++ b/crates/rendering/src/layers/camera.rs@@ -289,6 +289,188 @@ impl CameraLayer {}}+ fn copy_from_yuv_output_to_encoder(+ &self,+ encoder: &mut wgpu::CommandEncoder,+ next_texture: usize,+ frame_size: XY<u32>,+ ) {+ if let Some(output_texture) = self.yuv_converter.output_texture() {+ encoder.copy_texture_to_texture(+ wgpu::TexelCopyTextureInfo {+ texture: output_texture,+ mip_level: 0,+ origin: wgpu::Origin3d::ZERO,+ aspect: wgpu::TextureAspect::All,+ },+ wgpu::TexelCopyTextureInfo {+ texture: &self.frame_textures[next_texture],+ mip_level: 0,+ origin: wgpu::Origin3d::ZERO,+ aspect: wgpu::TextureAspect::All,+ },+ wgpu::Extent3d {+ width: frame_size.x,+ height: frame_size.y,+ depth_or_array_layers: 1,+ },+ );+ }+ }++ pub fn prepare_with_encoder(+ &mut self,+ device: &wgpu::Device,+ queue: &wgpu::Queue,+ uniforms: Option<CompositeVideoFrameUniforms>,+ frame_data: Option<(XY<u32>, &DecodedFrame, f32)>,+ encoder: &mut wgpu::CommandEncoder,+ ) {+ let Some(uniforms) = uniforms else {+ self.hidden = true;+ return;+ };++ let has_previous_frame = self.last_recording_time.is_some();+ self.hidden = frame_data.is_none() && !has_previous_frame;++ queue.write_buffer(&self.uniforms_buffer, 0, bytemuck::cast_slice(&[uniforms]));++ let Some((frame_size, camera_frame, recording_time)) = frame_data else {+ return;+ };++ let format = camera_frame.format();++ let is_same_frame = self+ .last_recording_time+ .is_some_and(|last| (last - recording_time).abs() < 0.001);++ if !is_same_frame {+ let next_texture = 1 - self.current_texture;++ if self.frame_textures[next_texture].width() != frame_size.x+ || self.frame_textures[next_texture].height() != frame_size.y+ {+ self.frame_textures[next_texture] =+ CompositeVideoFramePipeline::create_frame_texture(+ device,+ frame_size.x,+ frame_size.y,+ );+ self.frame_texture_views[next_texture] =+ self.frame_textures[next_texture].create_view(&Default::default());++ self.bind_groups[next_texture] = Some(self.pipeline.bind_group(+ device,+ &self.uniforms_buffer,+ &self.frame_texture_views[next_texture],+ ));+ }++ match format {+ PixelFormat::Rgba => {+ let frame_data_bytes = camera_frame.data();+ let src_bytes_per_row = frame_size.x * 4;++ queue.write_texture(+ wgpu::TexelCopyTextureInfo {+ texture: &self.frame_textures[next_texture],+ mip_level: 0,+ origin: wgpu::Origin3d::ZERO,+ aspect: wgpu::TextureAspect::All,+ },+ frame_data_bytes,+ wgpu::TexelCopyBufferLayout {+ offset: 0,+ bytes_per_row: Some(src_bytes_per_row),+ rows_per_image: Some(frame_size.y),+ },+ wgpu::Extent3d {+ width: frame_size.x,+ height: frame_size.y,+ depth_or_array_layers: 1,+ },+ );+ }+ PixelFormat::Nv12 => {+ if let Err(e) = self.yuv_converter.prepare_for_dimensions(+ device,+ frame_size.x,+ frame_size.y,+ ) {+ tracing::warn!(error = %e, "YUV converter prepare failed");+ return;+ }++ if let (Some(y_data), Some(uv_data)) =+ (camera_frame.y_plane(), camera_frame.uv_plane())+ && self+ .yuv_converter+ .convert_nv12_to_encoder(+ device,+ queue,+ encoder,+ y_data,+ uv_data,+ frame_size.x,+ frame_size.y,+ camera_frame.y_stride(),+ camera_frame.uv_stride(),+ )+ .is_ok()+ {+ self.copy_from_yuv_output_to_encoder(+ encoder,+ next_texture,+ frame_size,+ );+ }+ }+ PixelFormat::Yuv420p => {+ if let Err(e) = self.yuv_converter.prepare_for_dimensions(+ device,+ frame_size.x,+ frame_size.y,+ ) {+ tracing::warn!(error = %e, "YUV converter prepare failed");+ return;+ }++ if let (Some(y_data), Some(u_data), Some(v_data)) = (+ camera_frame.y_plane(),+ camera_frame.u_plane(),+ camera_frame.v_plane(),+ ) && self+ .yuv_converter+ .convert_yuv420p_to_encoder(+ device,+ queue,+ encoder,+ y_data,+ u_data,+ v_data,+ frame_size.x,+ frame_size.y,+ camera_frame.y_stride(),+ camera_frame.uv_stride(),+ )+ .is_ok()+ {+ self.copy_from_yuv_output_to_encoder(+ encoder,+ next_texture,+ frame_size,+ );+ }+ }+ }++ self.last_recording_time = Some(recording_time);+ self.current_texture = next_texture;+ }+ }+pub fn copy_to_texture(&mut self, _encoder: &mut wgpu::CommandEncoder) {}pub fn render(&self, pass: &mut wgpu::RenderPass<'_>) {diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rsindex 6c63d2951..dbcf768c0 100644--- a/crates/rendering/src/lib.rs+++ b/crates/rendering/src/lib.rs@@ -2067,7 +2067,7 @@ impl RendererLayers {constants,);- self.camera.prepare(+ self.camera.prepare_with_encoder(&constants.device,&constants.queue,uniforms.camera,@@ -2077,9 +2077,10 @@ impl RendererLayers {.as_ref().map(|frame| (size, frame, segment_frames.recording_time))}),+ encoder,);- self.camera_only.prepare(+ self.camera_only.prepare_with_encoder(&constants.device,&constants.queue,uniforms.camera_only,@@ -2089,6 +2090,7 @@ impl RendererLayers {.as_ref().map(|frame| (size, frame, segment_frames.recording_time))}),+ encoder,);self.text.prepare(