Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(rendering): render background blur as a separable two-pass gaussian

Richie McIlroycfea08f3a82c
diff --git a/crates/rendering/src/layers/blur.rs b/crates/rendering/src/layers/blur.rs
index 59e202ac5..4f9ea8afd 100644
--- a/crates/rendering/src/layers/blur.rs
+++ b/crates/rendering/src/layers/blur.rs
@@ -6,13 +6,24 @@ use crate::ProjectUniforms;
pub struct BlurLayer {
pub blur_amount: f64,
sampler: wgpu::Sampler,
- uniforms_buffer: wgpu::Buffer,
+ uniforms_buffer_h: wgpu::Buffer,
+ uniforms_buffer_v: wgpu::Buffer,
pipeline: BlurPipeline,
cached_uniforms: Option<BlurUniforms>,
}
impl BlurLayer {
pub fn new(device: &wgpu::Device) -> Self {
+ let make_buffer = |direction: f32| {
+ device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
+ label: Some("BackgroundBlur Uniform Buffer"),
+ contents: bytemuck::cast_slice(&[BlurUniforms {
+ direction,
+ ..Default::default()
+ }]),
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ })
+ };
Self {
blur_amount: 0.0,
sampler: device.create_sampler(&wgpu::SamplerDescriptor {
@@ -24,11 +35,8 @@ impl BlurLayer {
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
}),
- uniforms_buffer: device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
- label: Some("BackgroundBlur Uniform Buffer"),
- contents: bytemuck::cast_slice(&[BlurUniforms::default()]),
- usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
- }),
+ uniforms_buffer_h: make_buffer(0.0),
+ uniforms_buffer_v: make_buffer(1.0),
pipeline: BlurPipeline::new(device),
cached_uniforms: None,
}
@@ -44,31 +52,58 @@ impl BlurLayer {
let blur_uniform = BlurUniforms {
output_size: [uniforms.output_size.0 as f32, uniforms.output_size.1 as f32],
blur_strength,
- _padding: 0.0,
+ direction: 0.0,
};
if self.cached_uniforms.as_ref() != Some(&blur_uniform) {
queue.write_buffer(
- &self.uniforms_buffer,
+ &self.uniforms_buffer_h,
0,
bytemuck::cast_slice(&[blur_uniform]),
);
+ queue.write_buffer(
+ &self.uniforms_buffer_v,
+ 0,
+ bytemuck::cast_slice(&[BlurUniforms {
+ direction: 1.0,
+ ..blur_uniform
+ }]),
+ );
self.cached_uniforms = Some(blur_uniform);
}
}
- pub fn render(
+ pub fn render_h(
+ &self,
+ pass: &mut wgpu::RenderPass<'_>,
+ device: &wgpu::Device,
+ source_texture: &wgpu::TextureView,
+ ) {
+ self.render_pass(pass, device, source_texture, &self.uniforms_buffer_h);
+ }
+
+ pub fn render_v(
+ &self,
+ pass: &mut wgpu::RenderPass<'_>,
+ device: &wgpu::Device,
+ source_texture: &wgpu::TextureView,
+ ) {
+ self.render_pass(pass, device, source_texture, &self.uniforms_buffer_v);
+ }
+
+ fn render_pass(
&self,
pass: &mut wgpu::RenderPass<'_>,
device: &wgpu::Device,
source_texture: &wgpu::TextureView,
+ uniforms_buffer: &wgpu::Buffer,
) {
pass.set_pipeline(&self.pipeline.render_pipeline);
pass.set_bind_group(
0,
&self
.pipeline
- .bind_group(device, &self.uniforms_buffer, source_texture, &self.sampler),
+ .bind_group(device, uniforms_buffer, source_texture, &self.sampler),
&[],
);
pass.draw(0..4, 0..1);
@@ -80,7 +115,7 @@ impl BlurLayer {
pub struct BlurUniforms {
output_size: [f32; 2],
blur_strength: f32,
- _padding: f32,
+ direction: f32,
}
pub struct BlurPipeline {
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index fe56996e6..dfca86300 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -5792,12 +5792,18 @@ impl RendererLayers {
self.background.render(&mut pass);
}
+ // Separable gaussian: horizontal into the spare texture, vertical back
+ // into the current one, so the result ends up where it started and no
+ // swap is needed.
if self.background_blur.blur_amount > 0.0 {
- let mut pass = render_pass!(session.other_texture_view(), wgpu::LoadOp::Load);
+ {
+ let mut pass = render_pass!(session.other_texture_view(), wgpu::LoadOp::Load);
+ self.background_blur
+ .render_h(&mut pass, device, session.current_texture_view());
+ }
+ let mut pass = render_pass!(session.current_texture_view(), wgpu::LoadOp::Load);
self.background_blur
- .render(&mut pass, device, session.current_texture_view());
-
- session.swap_textures();
+ .render_v(&mut pass, device, session.other_texture_view());
}
// Runs before content layers so the screen grade covers the whole
diff --git a/crates/rendering/src/shaders/background-blur.wgsl b/crates/rendering/src/shaders/background-blur.wgsl
index 7baca8986..dc775a687 100644
--- a/crates/rendering/src/shaders/background-blur.wgsl
+++ b/crates/rendering/src/shaders/background-blur.wgsl
@@ -1,7 +1,15 @@
+// Background blur: separable gaussian, run twice (horizontal then vertical).
+//
+// The kernel always spans the full blur radius with evenly spaced taps; when
+// the radius exceeds the tap budget the spacing widens instead of leaving
+// gaps, and linear filtering interpolates between texels. Spacing stays well
+// under sigma, so the widened kernel cannot alias into the comb/grid pattern
+// the old single-pass sparse kernel produced.
+
struct Uniforms {
output_size: vec2<f32>,
blur_strength: f32,
- _padding: f32,
+ direction: f32, // 0 = horizontal, 1 = vertical
};
@group(0) @binding(0) var<uniform> u: Uniforms;
@@ -25,34 +33,35 @@ fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> VertexOutput {
@fragment
fn fs_main(@location(0) tex_coords: vec2<f32>) -> @location(0) vec4<f32> {
- // Early return if no blur
- if (u.blur_strength <= 0.01) {
- return textureSample(t_background, s_background, tex_coords);
+ if (u.blur_strength <= 0.001) {
+ return textureSampleLevel(t_background, s_background, tex_coords, 0.0);
}
- // Use smaller kernel for light blur
- let radius = u.blur_strength * 16.0;
- let sigma = radius * 0.5;
- let samples = min(16, max(4, i32(radius * 0.3)));
-
- var color = vec4<f32>(0.0);
- var total_weight = 0.0;
-
- for (var y = -samples; y <= samples; y++) {
- for (var x = -samples; x <= samples; x++) {
- let offset = vec2<f32>(
- f32(x) * radius / u.output_size.x,
- f32(y) * radius / u.output_size.y
- );
-
- let sample_pos = tex_coords + offset;
- let dist = f32(x * x + y * y);
- let weight = exp(-dist / (2.0 * sigma * sigma));
-
- color += textureSample(t_background, s_background, sample_pos) * weight;
- total_weight += weight;
- }
+ // Sigma scales with output height so preview and export look identical;
+ // full strength blurs with a sigma of 2.5% of the frame height.
+ let sigma = u.blur_strength * u.output_size.y * 0.025;
+ let radius = sigma * 3.0;
+ let taps = min(24.0, ceil(radius));
+ let spacing = radius / taps;
+
+ var step = vec2<f32>(spacing / u.output_size.x, 0.0);
+ if (u.direction > 0.5) {
+ step = vec2<f32>(0.0, spacing / u.output_size.y);
}
-
+
+ var color = textureSampleLevel(t_background, s_background, tex_coords, 0.0);
+ var total_weight = 1.0;
+ let inv_sigma2 = 1.0 / (2.0 * sigma * sigma);
+ let n = i32(taps);
+
+ for (var i = 1; i <= n; i++) {
+ let dist = f32(i) * spacing;
+ let weight = exp(-dist * dist * inv_sigma2);
+ let offset = step * f32(i);
+ color += textureSampleLevel(t_background, s_background, tex_coords + offset, 0.0) * weight;
+ color += textureSampleLevel(t_background, s_background, tex_coords - offset, 0.0) * weight;
+ total_weight += 2.0 * weight;
+ }
+
return color / total_weight;
-}
\ No newline at end of file
+}