Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

16.3 KB
Raw Blame
use bytemuck::{Pod, Zeroable};
use cap_project::XY;
use wgpu::{include_wgsl, util::DeviceExt};

pub struct CompositeVideoFramePipeline {
    pub bind_group_layout: wgpu::BindGroupLayout,
    pub render_pipeline: wgpu::RenderPipeline,
    sampler: wgpu::Sampler,
}

static PIPELINE_CACHE_DATA: std::sync::OnceLock<Vec<u8>> = std::sync::OnceLock::new();

fn get_cache_path() -> Option<std::path::PathBuf> {
    dirs::data_local_dir().map(|p| p.join("Cap").join("shader_cache.bin"))
}

fn load_pipeline_cache(device: &wgpu::Device) -> Option<wgpu::PipelineCache> {
    if !device.features().contains(wgpu::Features::PIPELINE_CACHE) {
        return None;
    }

    if let Some(cached_data) = PIPELINE_CACHE_DATA.get() {
        return Some(unsafe {
            device.create_pipeline_cache(&wgpu::PipelineCacheDescriptor {
                label: Some("Cap Pipeline Cache"),
                data: Some(cached_data),
                fallback: true,
            })
        });
    }

    let cache_path = get_cache_path()?;
    if cache_path.exists()
        && let Ok(data) = std::fs::read(&cache_path)
    {
        let _ = PIPELINE_CACHE_DATA.set(data.clone());
        return Some(unsafe {
            device.create_pipeline_cache(&wgpu::PipelineCacheDescriptor {
                label: Some("Cap Pipeline Cache"),
                data: Some(&data),
                fallback: true,
            })
        });
    }

    Some(unsafe {
        device.create_pipeline_cache(&wgpu::PipelineCacheDescriptor {
            label: Some("Cap Pipeline Cache"),
            data: None,
            fallback: true,
        })
    })
}

fn save_pipeline_cache(cache: &wgpu::PipelineCache) {
    if let Some(data) = cache.get_data() {
        let _ = PIPELINE_CACHE_DATA.set(data.clone());
        if let Some(cache_path) = get_cache_path() {
            if let Some(parent) = cache_path.parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            let _ = std::fs::write(cache_path, &data);
        }
    }
}

#[derive(Debug, Clone, Copy, Pod, Zeroable)]
#[repr(C)]
pub struct CompositeVideoFrameUniforms {
    pub crop_bounds: [f32; 4],
    pub target_bounds: [f32; 4],
    pub output_size: [f32; 2],
    pub frame_size: [f32; 2],
    pub motion_blur_vector: [f32; 2],
    pub motion_blur_zoom_center: [f32; 2],
    pub motion_blur_params: [f32; 4],
    pub target_size: [f32; 2],
    pub rounding_px: f32,
    pub rounding_type: f32,
    pub mirror_x: f32,
    pub shadow: f32,
    pub shadow_size: f32,
    pub shadow_opacity: f32,
    pub shadow_blur: f32,
    pub opacity: f32,
    pub border_enabled: f32,
    pub border_width: f32,
    pub preserve_source_alpha: f32,
    pub _padding1: [f32; 3],
    pub border_color: [f32; 4],
    /// Per-corner multipliers on `rounding_px` (top-left, top-right,
    /// bottom-left, bottom-right). All 1s = uniform rounding; the display
    /// squares its top corners against decorative frame chrome with
    /// `[0, 0, 1, 1]`.
    pub corner_radii: [f32; 4],
    /// (exposure stops, contrast, saturation, temperature).
    pub color_adjust_a: [f32; 4],
    /// (tint, fade, split_tone, vignette).
    pub color_adjust_b: [f32; 4],
    /// (grain amount, grain seed, grade-active flag, full-frame vignette
    /// flag). The active flag gates the shader's whole color pass in one
    /// uniform branch, keeping ungraded layers bit-identical to before.
    pub grain_params: [f32; 4],
}

impl Default for CompositeVideoFrameUniforms {
    fn default() -> Self {
        Self {
            crop_bounds: Default::default(),
            target_bounds: Default::default(),
            output_size: Default::default(),
            frame_size: Default::default(),
            motion_blur_vector: Default::default(),
            motion_blur_zoom_center: [0.5, 0.5],
            motion_blur_params: Default::default(),
            target_size: Default::default(),
            rounding_px: Default::default(),
            rounding_type: 0.0,
            mirror_x: Default::default(),
            shadow: Default::default(),
            shadow_size: Default::default(),
            shadow_opacity: Default::default(),
            shadow_blur: Default::default(),
            opacity: 1.0,
            border_enabled: 0.0,
            border_width: 5.0,
            preserve_source_alpha: 0.0,
            _padding1: [0.0; 3],
            border_color: [0.0, 0.0, 0.0, 0.0],
            corner_radii: [1.0; 4],
            color_adjust_a: [0.0; 4],
            color_adjust_b: [0.0; 4],
            grain_params: [0.0; 4],
        }
    }
}

/// Intensity scaling and range clamping happen here, Rust-side, so the
/// shader never sees unscaled values and a hand-edited config can't push it
/// outside its designed ranges.
#[derive(Debug, Clone, Copy)]
pub struct ColorGradeUniformParams {
    pub color_adjust_a: [f32; 4],
    pub color_adjust_b: [f32; 4],
    pub grain_params: [f32; 4],
}

impl ColorGradeUniformParams {
    pub const IDENTITY: Self = Self {
        color_adjust_a: [0.0; 4],
        color_adjust_b: [0.0; 4],
        grain_params: [0.0; 4],
    };

    /// Must mirror the shader's own gate on `grain_params.z`.
    pub fn is_active(&self) -> bool {
        self.grain_params[2] > 0.5
    }

    /// `full_frame_vignette` selects the vignette's coordinate space: full
    /// output frame for the screen (one continuous field across card and
    /// backdrop), card-local for the camera.
    pub fn from_config(
        config: &cap_project::ColorCorrection,
        frame_number: u32,
        full_frame_vignette: bool,
    ) -> Self {
        let intensity = config.intensity.clamp(0.0, 1.0);
        let exposure_stops = config.exposure.clamp(-1.0, 1.0) * 1.5 * intensity;
        let contrast = config.contrast.clamp(-1.0, 1.0) * intensity;
        let saturation = config.saturation.clamp(-1.0, 1.0) * intensity;
        let temperature = config.temperature.clamp(-1.0, 1.0) * intensity;
        let tint = config.tint.clamp(-1.0, 1.0) * intensity;
        let fade = config.fade.clamp(0.0, 1.0) * intensity;
        let split_tone = config.split_tone.clamp(-1.0, 1.0) * intensity;
        let vignette = config.vignette.clamp(0.0, 1.0) * intensity;
        let grain = config.grain.clamp(0.0, 1.0);

        let active = [
            exposure_stops,
            contrast,
            saturation,
            temperature,
            tint,
            fade,
            split_tone,
            vignette,
            grain,
        ]
        .iter()
        .any(|v| v.abs() > 1e-4);

        // Seed is deterministic per frame number so preview and export match.
        let grain_seed = (frame_number % 600) as f32;

        Self {
            color_adjust_a: [exposure_stops, contrast, saturation, temperature],
            color_adjust_b: [tint, fade, split_tone, vignette],
            grain_params: [
                grain,
                grain_seed,
                if active { 1.0 } else { 0.0 },
                if full_frame_vignette { 1.0 } else { 0.0 },
            ],
        }
    }
}

impl CompositeVideoFrameUniforms {
    pub(crate) fn for_source_frame(mut self, source_size: XY<u32>) -> Self {
        let scale_x = source_size.x as f32 / self.frame_size[0].max(1.0);
        let scale_y = source_size.y as f32 / self.frame_size[1].max(1.0);

        self.crop_bounds = [
            self.crop_bounds[0] * scale_x,
            self.crop_bounds[1] * scale_y,
            self.crop_bounds[2] * scale_x,
            self.crop_bounds[3] * scale_y,
        ];
        self.frame_size = [source_size.x as f32, source_size.y as f32];

        let crop_w = self.crop_bounds[2] - self.crop_bounds[0];
        let crop_h = self.crop_bounds[3] - self.crop_bounds[1];
        let target_w = self.target_bounds[2] - self.target_bounds[0];
        let target_h = self.target_bounds[3] - self.target_bounds[1];

        if crop_w > 0.0 && crop_h > 0.0 && target_w > 0.0 && target_h > 0.0 {
            let source_aspect = crop_w / crop_h;
            let target_aspect = target_w / target_h;

            if (source_aspect - target_aspect).abs() > 0.001 {
                let scale = (target_w / crop_w).min(target_h / crop_h);
                let fitted_w = crop_w * scale;
                let fitted_h = crop_h * scale;
                let new_x0 = self.target_bounds[0] + (target_w - fitted_w) * 0.5;
                let new_y0 = self.target_bounds[1] + (target_h - fitted_h) * 0.5;

                self.target_bounds = [new_x0, new_y0, new_x0 + fitted_w, new_y0 + fitted_h];
                self.target_size = [fitted_w, fitted_h];
            }
        }

        self
    }

    pub(crate) fn source_uv_to_target(&self, position: XY<f64>) -> XY<f64> {
        let crop_size = XY::new(
            (self.crop_bounds[2] - self.crop_bounds[0]).max(f32::EPSILON) as f64,
            (self.crop_bounds[3] - self.crop_bounds[1]).max(f32::EPSILON) as f64,
        );
        let source_position =
            position * XY::new(self.frame_size[0] as f64, self.frame_size[1] as f64);
        let mut position_in_crop = (source_position
            - XY::new(self.crop_bounds[0] as f64, self.crop_bounds[1] as f64))
            / crop_size;
        if self.mirror_x != 0.0 {
            position_in_crop.x = 1.0 - position_in_crop.x;
        }

        XY::new(self.target_bounds[0] as f64, self.target_bounds[1] as f64)
            + position_in_crop * XY::new(self.target_size[0] as f64, self.target_size[1] as f64)
    }

    pub fn to_buffer(self, device: &wgpu::Device) -> wgpu::Buffer {
        device.create_buffer_init(
            &(wgpu::util::BufferInitDescriptor {
                label: Some("CompositeVideoFrameUniforms Buffer"),
                contents: bytemuck::cast_slice(&[self]),
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            }),
        )
    }

    pub fn write_to_buffer(&self, queue: &wgpu::Queue, buffer: &wgpu::Buffer) {
        queue.write_buffer(buffer, 0, bytemuck::bytes_of(self));
    }
}

// pub struct CompositeFrameResources {
//     pub bind_group: wgpu::BindGroup,
//     pub bind_group_layout: wgpu::BindGroupLayout,
//     pub uniforms_buffer: wgpu::Buffer,
//     pub sampler: wgpu::Sampler,
// }

impl CompositeVideoFramePipeline {
    pub fn new(device: &wgpu::Device) -> Self {
        let pipeline_cache = load_pipeline_cache(device);
        let bind_group_layout = Self::bind_group_layout(device);
        let shader_desc = include_wgsl!("shaders/composite-video-frame.wgsl");
        let shader = device.create_shader_module(shader_desc);

        let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("Composite Render Pipeline Layout"),
            bind_group_layouts: &[&bind_group_layout],
            push_constant_ranges: &[],
        });

        let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("Composite Render Pipeline"),
            layout: Some(&pipeline_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs_main"),
                buffers: &[],
                compilation_options: wgpu::PipelineCompilationOptions {
                    constants: &[],
                    zero_initialize_workgroup_memory: false,
                },
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs_main"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: wgpu::TextureFormat::Rgba8Unorm,
                    blend: Some(wgpu::BlendState::ALPHA_BLENDING),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: wgpu::PipelineCompilationOptions {
                    constants: &[],
                    zero_initialize_workgroup_memory: false,
                },
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                strip_index_format: None,
                front_face: wgpu::FrontFace::Ccw,
                cull_mode: Some(wgpu::Face::Back),
                polygon_mode: wgpu::PolygonMode::Fill,
                unclipped_depth: false,
                conservative: false,
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState {
                count: 1,
                mask: !0,
                alpha_to_coverage_enabled: false,
            },
            multiview: None,
            cache: pipeline_cache.as_ref(),
        });

        if let Some(cache) = &pipeline_cache {
            save_pipeline_cache(cache);
        }

        let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            address_mode_w: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::FilterMode::Linear,
            ..Default::default()
        });

        Self {
            bind_group_layout,
            render_pipeline,
            sampler,
        }
    }

    fn bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
        device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("composite-video-frame.wgsl Bind Group Layout"),
            entries: &[
                wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: None,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Texture {
                        sample_type: wgpu::TextureSampleType::Float { filterable: true },
                        view_dimension: wgpu::TextureViewDimension::D2,
                        multisampled: false,
                    },
                    count: None,
                },
                wgpu::BindGroupLayoutEntry {
                    binding: 2,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
            ],
        })
    }

    pub fn bind_group(
        &self,
        device: &wgpu::Device,
        uniforms: &wgpu::Buffer,
        frame: &wgpu::TextureView,
    ) -> wgpu::BindGroup {
        device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout: &self.bind_group_layout,
            entries: &[
                wgpu::BindGroupEntry {
                    binding: 0,
                    resource: uniforms.as_entire_binding(),
                },
                wgpu::BindGroupEntry {
                    binding: 1,
                    resource: wgpu::BindingResource::TextureView(frame),
                },
                wgpu::BindGroupEntry {
                    binding: 2,
                    resource: wgpu::BindingResource::Sampler(&self.sampler),
                },
            ],
            label: Some("bind_group"),
        })
    }

    pub fn create_frame_texture(device: &wgpu::Device, width: u32, height: u32) -> wgpu::Texture {
        #[cfg(target_os = "macos")]
        let usage = wgpu::TextureUsages::TEXTURE_BINDING
            | wgpu::TextureUsages::STORAGE_BINDING
            | wgpu::TextureUsages::COPY_DST;
        #[cfg(not(target_os = "macos"))]
        let usage = wgpu::TextureUsages::TEXTURE_BINDING
            | wgpu::TextureUsages::RENDER_ATTACHMENT
            | wgpu::TextureUsages::COPY_DST;
        device.create_texture(
            &(wgpu::TextureDescriptor {
                size: wgpu::Extent3d {
                    width,
                    height,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Rgba8Unorm,
                usage,
                label: Some("Frame Composite texture"),
                view_formats: &[],
            }),
        )
    }
}