Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(perf): gate the refactor to only affect linux

The-Best-Codes6d13723967ab
diff --git a/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl b/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
index ddf8a61cc..65f746ab7 100644
--- a/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
+++ b/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
@@ -3,23 +3,38 @@
@group(0) @binding(2) var v_plane: texture_2d<f32>;
@group(0) @binding(3) var output: texture_storage_2d<rgba8unorm, write>;
-// One invocation per 2x2 block: the block shares its chroma sample, so this loads U and V once instead of four times.
+// One invocation per pixel.
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
+ let coords = global_id.xy;
+ let dims = textureDimensions(output);
+ let uv = load_uv(coords / 2u);
+ convert_pixel(coords, dims, uv.x, uv.y);
+}
+
+// One invocation per 2x2 block: the block shares its chroma sample, so this loads U and V once instead of four times.
+@compute @workgroup_size(8, 8)
+fn main_block(@builtin(global_invocation_id) global_id: vec3<u32>) {
let uv_coords = global_id.xy;
let dims = textureDimensions(output);
+ let uv = load_uv(uv_coords);
+
+ let origin = uv_coords * 2u;
+ convert_pixel(origin, dims, uv.x, uv.y);
+ convert_pixel(origin + vec2<u32>(1u, 0u), dims, uv.x, uv.y);
+ convert_pixel(origin + vec2<u32>(0u, 1u), dims, uv.x, uv.y);
+ convert_pixel(origin + vec2<u32>(1u, 1u), dims, uv.x, uv.y);
+}
+
+fn load_uv(uv_coords: vec2<u32>) -> vec2<f32> {
let u_dims = textureDimensions(u_plane);
let v_dims = textureDimensions(v_plane);
let u_clamped = min(uv_coords, u_dims - vec2<u32>(1, 1));
let v_clamped = min(uv_coords, v_dims - vec2<u32>(1, 1));
- let u_raw = textureLoad(u_plane, u_clamped, 0).r;
- let v_raw = textureLoad(v_plane, v_clamped, 0).r;
-
- let origin = uv_coords * 2u;
- convert_pixel(origin, dims, u_raw, v_raw);
- convert_pixel(origin + vec2<u32>(1u, 0u), dims, u_raw, v_raw);
- convert_pixel(origin + vec2<u32>(0u, 1u), dims, u_raw, v_raw);
- convert_pixel(origin + vec2<u32>(1u, 1u), dims, u_raw, v_raw);
+ return vec2<f32>(
+ textureLoad(u_plane, u_clamped, 0).r,
+ textureLoad(v_plane, v_clamped, 0).r
+ );
}
fn convert_pixel(coords: vec2<u32>, dims: vec2<u32>, u_raw: f32, v_raw: f32) {
diff --git a/crates/rendering/src/yuv_converter.rs b/crates/rendering/src/yuv_converter.rs
index 73519a0b9..167991374 100644
--- a/crates/rendering/src/yuv_converter.rs
+++ b/crates/rendering/src/yuv_converter.rs
@@ -107,6 +107,16 @@ const INITIAL_TEXTURE_HEIGHT: u32 = 1080;
const TEXTURE_SIZE_PADDING: u32 = 64;
+// The 2x2 block kernel only pays off on Linux; on Apple GPUs it measured slower than one invocation per pixel.
+#[cfg(target_os = "linux")]
+const YUV420P_ENTRY_POINT: &str = "main_block";
+#[cfg(target_os = "linux")]
+const YUV420P_PIXELS_PER_WORKGROUP: u32 = 16;
+#[cfg(not(target_os = "linux"))]
+const YUV420P_ENTRY_POINT: &str = "main";
+#[cfg(not(target_os = "linux"))]
+const YUV420P_PIXELS_PER_WORKGROUP: u32 = 8;
+
fn align_dimension(dim: u32) -> u32 {
dim.div_ceil(TEXTURE_SIZE_PADDING) * TEXTURE_SIZE_PADDING
}
@@ -394,7 +404,7 @@ impl YuvConverterPipelines {
label: Some("YUV420P Converter Pipeline"),
layout: Some(&yuv420p_pipeline_layout),
module: &yuv420p_shader,
- entry_point: Some("main"),
+ entry_point: Some(YUV420P_ENTRY_POINT),
compilation_options: Default::default(),
cache: None,
});
@@ -1168,7 +1178,11 @@ impl YuvToRgbaConverter {
});
compute_pass.set_pipeline(&self.pipelines.yuv420p_pipeline);
compute_pass.set_bind_group(0, bind_group, &[]);
- compute_pass.dispatch_workgroups(width.div_ceil(16), height.div_ceil(16), 1);
+ compute_pass.dispatch_workgroups(
+ width.div_ceil(YUV420P_PIXELS_PER_WORKGROUP),
+ height.div_ceil(YUV420P_PIXELS_PER_WORKGROUP),
+ 1,
+ );
}
queue.submit(std::iter::once(encoder.finish()));
@@ -1239,7 +1253,11 @@ impl YuvToRgbaConverter {
});
compute_pass.set_pipeline(&self.pipelines.yuv420p_pipeline);
compute_pass.set_bind_group(0, bind_group, &[]);
- compute_pass.dispatch_workgroups(width.div_ceil(16), height.div_ceil(16), 1);
+ compute_pass.dispatch_workgroups(
+ width.div_ceil(YUV420P_PIXELS_PER_WORKGROUP),
+ height.div_ceil(YUV420P_PIXELS_PER_WORKGROUP),
+ 1,
+ );
}
Ok(self.current_output_view())