Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(rendering): integrate shared camera background blur processor

Richie McIlroy5f5d6f62eb3a
diff --git a/crates/rendering/Cargo.toml b/crates/rendering/Cargo.toml
index 8727008bc..34ac9e4a2 100644
--- a/crates/rendering/Cargo.toml
+++ b/crates/rendering/Cargo.toml
@@ -9,6 +9,7 @@ workspace = true
[dependencies]
anyhow.workspace = true
bytemuck = { version = "1.7", features = ["derive"] }
+cap-camera-effects = { path = "../camera-effects" }
cap-flags = { path = "../flags" }
cap-project = { path = "../project" }
cap-video-decode = { path = "../video-decode" }
diff --git a/crates/rendering/src/layers/camera.rs b/crates/rendering/src/layers/camera.rs
index d7c9944b1..e2be7f5dd 100644
--- a/crates/rendering/src/layers/camera.rs
+++ b/crates/rendering/src/layers/camera.rs
@@ -18,6 +18,17 @@ pub struct CameraLayer {
hidden: bool,
last_recording_time: Option<f32>,
yuv_converter: YuvToRgbaConverter,
+ blur_bind_group: Option<wgpu::BindGroup>,
+ blur_active: bool,
+ blur_cache: Option<BlurCacheEntry>,
+}
+
+#[derive(Clone, Copy)]
+struct BlurCacheEntry {
+ recording_time: f32,
+ mode: cap_camera_effects::BlurMode,
+ texture_idx: usize,
+ output_generation: u64,
}
impl CameraLayer {
@@ -65,6 +76,9 @@ impl CameraLayer {
hidden: false,
last_recording_time: None,
yuv_converter,
+ blur_bind_group: None,
+ blur_active: false,
+ blur_cache: None,
}
}
@@ -75,6 +89,8 @@ impl CameraLayer {
uniforms: Option<CompositeVideoFrameUniforms>,
frame_data: Option<(XY<u32>, &DecodedFrame, f32)>,
) {
+ self.blur_active = false;
+
let Some(uniforms) = uniforms else {
self.hidden = true;
return;
@@ -327,6 +343,8 @@ impl CameraLayer {
frame_data: Option<(XY<u32>, &DecodedFrame, f32)>,
encoder: &mut wgpu::CommandEncoder,
) {
+ self.blur_active = false;
+
let Some(uniforms) = uniforms else {
self.hidden = true;
return;
@@ -464,12 +482,75 @@ impl CameraLayer {
}
}
+ pub fn source_texture_for_blur(&self) -> Option<&wgpu::Texture> {
+ if self.hidden || self.last_recording_time.is_none() {
+ return None;
+ }
+ Some(&self.frame_textures[self.current_texture])
+ }
+
+ pub fn attach_shared_blur(
+ &mut self,
+ device: &wgpu::Device,
+ processor: &cap_camera_effects::BlurProcessor,
+ mode: cap_camera_effects::BlurMode,
+ ) {
+ if self.hidden || self.last_recording_time.is_none() {
+ return;
+ }
+
+ let recording_time = self.last_recording_time.expect("checked above");
+ let current = self.current_texture;
+ let processor_generation = processor.output_generation();
+
+ let cache_hit = matches!(
+ self.blur_cache,
+ Some(entry)
+ if entry.texture_idx == current
+ && entry.mode == mode
+ && (entry.recording_time - recording_time).abs() < 0.001
+ && entry.output_generation == processor_generation
+ ) && self.blur_bind_group.is_some();
+
+ if cache_hit {
+ self.blur_active = true;
+ return;
+ }
+
+ let Some(output_view) = processor.output_view() else {
+ return;
+ };
+
+ self.blur_bind_group = Some(self.pipeline.bind_group(
+ device,
+ &self.uniforms_buffer,
+ output_view,
+ ));
+ self.blur_cache = Some(BlurCacheEntry {
+ recording_time,
+ mode,
+ texture_idx: current,
+ output_generation: processor_generation,
+ });
+ self.blur_active = true;
+ }
+
pub fn copy_to_texture(&mut self, _encoder: &mut wgpu::CommandEncoder) {}
pub fn render(&self, pass: &mut wgpu::RenderPass<'_>) {
- if !self.hidden
- && let Some(bind_group) = &self.bind_groups[self.current_texture]
- {
+ if self.hidden {
+ return;
+ }
+
+ let bind_group = if self.blur_active {
+ self.blur_bind_group
+ .as_ref()
+ .or(self.bind_groups[self.current_texture].as_ref())
+ } else {
+ self.bind_groups[self.current_texture].as_ref()
+ };
+
+ if let Some(bind_group) = bind_group {
pass.set_pipeline(&self.pipeline.render_pipeline);
pass.set_bind_group(0, bind_group, &[]);
pass.draw(0..3, 0..1);
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index 1335c6a29..72b3f44d4 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -3407,6 +3407,8 @@ pub struct RendererLayers {
text: TextLayer,
captions: CaptionsLayer,
keyboard: KeyboardLayer,
+ camera_blur_processor: Option<cap_camera_effects::BlurProcessor>,
+ camera_blur_init_failed: bool,
}
impl RendererLayers {
@@ -3447,9 +3449,90 @@ impl RendererLayers {
text: TextLayer::new(device, queue),
captions: CaptionsLayer::new(device, queue),
keyboard: KeyboardLayer::new(device, queue),
+ camera_blur_processor: None,
+ camera_blur_init_failed: false,
}
}
+ fn ensure_camera_blur_processor(&mut self, device: &wgpu::Device) {
+ if self.camera_blur_processor.is_none() && !self.camera_blur_init_failed {
+ match cap_camera_effects::BlurProcessor::new(device, wgpu::TextureFormat::Rgba8Unorm) {
+ Ok(processor) => {
+ self.camera_blur_processor = Some(processor);
+ }
+ Err(e) => {
+ tracing::warn!("Failed to init camera background blur in renderer: {e}");
+ self.camera_blur_init_failed = true;
+ }
+ }
+ }
+ }
+
+ fn run_shared_camera_blur(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ mode: cap_camera_effects::BlurMode,
+ ) {
+ if self.camera.source_texture_for_blur().is_none()
+ && self.camera_only.source_texture_for_blur().is_none()
+ {
+ return;
+ }
+
+ self.ensure_camera_blur_processor(device);
+ let Some(processor) = self.camera_blur_processor.as_mut() else {
+ return;
+ };
+
+ let source_texture = self
+ .camera
+ .source_texture_for_blur()
+ .or_else(|| self.camera_only.source_texture_for_blur());
+ let Some(source_texture) = source_texture else {
+ return;
+ };
+
+ let _ = processor.process(device, queue, source_texture, mode);
+
+ let processor: &cap_camera_effects::BlurProcessor = processor;
+ self.camera.attach_shared_blur(device, processor, mode);
+ self.camera_only.attach_shared_blur(device, processor, mode);
+ }
+
+ fn run_shared_camera_blur_with_encoder(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ encoder: &mut wgpu::CommandEncoder,
+ mode: cap_camera_effects::BlurMode,
+ ) {
+ if self.camera.source_texture_for_blur().is_none()
+ && self.camera_only.source_texture_for_blur().is_none()
+ {
+ return;
+ }
+
+ self.ensure_camera_blur_processor(device);
+ let Some(processor) = self.camera_blur_processor.as_mut() else {
+ return;
+ };
+
+ let source_texture = self
+ .camera
+ .source_texture_for_blur()
+ .or_else(|| self.camera_only.source_texture_for_blur());
+ let Some(source_texture) = source_texture else {
+ return;
+ };
+
+ processor.process_into_encoder(device, queue, source_texture, encoder, mode);
+
+ let processor: &cap_camera_effects::BlurProcessor = processor;
+ self.camera.attach_shared_blur(device, processor, mode);
+ self.camera_only.attach_shared_blur(device, processor, mode);
+ }
+
pub fn prepare_for_video_dimensions(
&mut self,
device: &wgpu::Device,
@@ -3537,6 +3620,10 @@ impl RendererLayers {
}),
);
+ if let Some(mode) = blur_mode_from_config(&uniforms.project.camera.background_blur) {
+ self.run_shared_camera_blur(&constants.device, &constants.queue, mode);
+ }
+
self.text.prepare(
&constants.device,
&constants.queue,
@@ -3629,6 +3716,15 @@ impl RendererLayers {
encoder,
);
+ if let Some(mode) = blur_mode_from_config(&uniforms.project.camera.background_blur) {
+ self.run_shared_camera_blur_with_encoder(
+ &constants.device,
+ &constants.queue,
+ encoder,
+ mode,
+ );
+ }
+
self.text.prepare(
&constants.device,
&constants.queue,
@@ -3803,6 +3899,16 @@ async fn produce_frame(
.await
}
+fn blur_mode_from_config(
+ config: &cap_project::BackgroundBlurConfig,
+) -> Option<cap_camera_effects::BlurMode> {
+ match config.mode {
+ cap_project::BackgroundBlurMode::Off => None,
+ cap_project::BackgroundBlurMode::Light => Some(cap_camera_effects::BlurMode::Light),
+ cap_project::BackgroundBlurMode::Heavy => Some(cap_camera_effects::BlurMode::Heavy),
+ }
+}
+
fn parse_color_component(hex_color: &str, index: usize) -> f32 {
let color = hex_color.trim_start_matches('#');