Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Merge remote-tracking branch 'upstream/main' into best/yuv420p-convert-blocks

BestCodes72b7d313dc71
diff --cc crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
index 5cb3fe842,c95d3ae38..93ad874ed
--- a/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
+++ b/crates/rendering/src/shaders/yuv420p_to_rgba.wgsl
@@@ -30,26 -27,16 +38,26 @@@ fn load_uv(uv_coords: vec2<u32>) -> vec
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;
+ 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) {
+ if (coords.x >= dims.x || coords.y >= dims.y) {
+ return;
+ }
+
+ let y_raw = textureLoad(y_plane, coords, 0).r;
- let y = (y_raw - 0.0625) * 1.164;
- let u = u_raw - 0.5;
- let v = v_raw - 0.5;
+ let y = (y_raw - 16.0 / 255.0) * (255.0 / 219.0);
+ let u = u_raw - 128.0 / 255.0;
+ let v = v_raw - 128.0 / 255.0;
- let r = y + 1.596 * v;
- let g = y - 0.391 * u - 0.813 * v;
- let b = y + 2.018 * u;
+ let r = y + RV * v;
+ let g = y - GU * u - GV * v;
+ let b = y + BU * u;
let color = vec4<f32>(
clamp(r, 0.0, 1.0),
diff --cc crates/rendering/src/yuv_converter.rs
index f0e7fe848,edac1b916..cd6d7cb9e
--- a/crates/rendering/src/yuv_converter.rs
+++ b/crates/rendering/src/yuv_converter.rs
@@@ -269,9 -259,60 +269,63 @@@ impl BindGroupCache
use std::sync::Arc;
+ /// One compute pipeline per source matrix, compiled on first use, so a
+ /// project only ever builds the variants its sources need.
+ struct MatrixPipelines {
+ device: wgpu::Device,
+ label: &'static str,
+ layout: wgpu::PipelineLayout,
+ module: wgpu::ShaderModule,
++ entry_point: &'static str,
+ bt601: std::sync::OnceLock<wgpu::ComputePipeline>,
+ bt709: std::sync::OnceLock<wgpu::ComputePipeline>,
+ }
+
+ impl MatrixPipelines {
+ fn new(
+ device: &wgpu::Device,
+ label: &'static str,
+ layout: wgpu::PipelineLayout,
+ module: wgpu::ShaderModule,
++ entry_point: &'static str,
+ ) -> Self {
+ Self {
+ device: device.clone(),
+ label,
+ layout,
+ module,
++ entry_point,
+ bt601: std::sync::OnceLock::new(),
+ bt709: std::sync::OnceLock::new(),
+ }
+ }
+
+ fn get(&self, matrix: YuvMatrix) -> &wgpu::ComputePipeline {
+ let cell = match matrix {
+ YuvMatrix::Bt601 => &self.bt601,
+ YuvMatrix::Bt709 => &self.bt709,
+ };
+ cell.get_or_init(|| {
+ let constants = matrix.shader_constants();
+ self.device
+ .create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
+ label: Some(self.label),
+ layout: Some(&self.layout),
+ module: &self.module,
- entry_point: Some("main"),
++ entry_point: Some(self.entry_point),
+ compilation_options: wgpu::PipelineCompilationOptions {
+ constants: &constants,
+ ..Default::default()
+ },
+ cache: None,
+ })
+ })
+ }
+ }
+
pub struct YuvConverterPipelines {
- pub nv12_pipeline: wgpu::ComputePipeline,
- pub yuv420p_pipeline: wgpu::ComputePipeline,
+ nv12: MatrixPipelines,
+ yuv420p: MatrixPipelines,
pub nv12_bind_group_layout: wgpu::BindGroupLayout,
pub yuv420p_bind_group_layout: wgpu::BindGroupLayout,
}
@@@ -391,24 -432,18 +445,20 @@@ impl YuvConverterPipelines
push_constant_ranges: &[],
});
- let nv12_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
- label: Some("NV12 Converter Pipeline"),
- layout: Some(&nv12_pipeline_layout),
- module: &nv12_shader,
- entry_point: Some("main"),
- compilation_options: Default::default(),
- cache: None,
- });
-
- let yuv420p_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
- label: Some("YUV420P Converter Pipeline"),
- layout: Some(&yuv420p_pipeline_layout),
- module: &yuv420p_shader,
- entry_point: Some(YUV420P_ENTRY_POINT),
- compilation_options: Default::default(),
- cache: None,
- });
-
+ let nv12 = MatrixPipelines::new(
+ device,
+ "NV12 Converter Pipeline",
+ nv12_pipeline_layout,
+ nv12_shader,
++ "main",
+ );
+ let yuv420p = MatrixPipelines::new(
+ device,
+ "YUV420P Converter Pipeline",
+ yuv420p_pipeline_layout,
+ yuv420p_shader,
++ YUV420P_ENTRY_POINT,
+ );
tracing::info!("Shared YUV converter pipelines created successfully");
Self {
@@@ -1176,13 -1254,9 +1269,13 @@@ impl YuvToRgbaConverter
label: Some("YUV420P Conversion Pass"),
..Default::default()
});
- compute_pass.set_pipeline(&self.pipelines.yuv420p_pipeline);
+ compute_pass.set_pipeline(self.pipelines.yuv420p.get(self.matrix));
compute_pass.set_bind_group(0, bind_group, &[]);
- compute_pass.dispatch_workgroups(width.div_ceil(8), height.div_ceil(8), 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()));
@@@ -1251,13 -1325,9 +1344,13 @@@
label: Some("YUV420P Conversion Pass (Batched)"),
..Default::default()
});
- compute_pass.set_pipeline(&self.pipelines.yuv420p_pipeline);
+ compute_pass.set_pipeline(self.pipelines.yuv420p.get(self.matrix));
compute_pass.set_bind_group(0, bind_group, &[]);
- compute_pass.dispatch_workgroups(width.div_ceil(8), height.div_ceil(8), 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())