Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
use cap_project::ClipTransitionType;
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct TransitionUniforms {
progress: f32,
kind: u32,
opaque: u32,
padding: u32,
}
struct TransitionTextures {
outgoing: wgpu::Texture,
incoming: wgpu::Texture,
bind_group: wgpu::BindGroup,
width: u32,
height: u32,
}
pub struct TransitionCompositor {
bind_group_layout: wgpu::BindGroupLayout,
pipeline: wgpu::RenderPipeline,
sampler: wgpu::Sampler,
uniforms_buffer: wgpu::Buffer,
textures: Option<TransitionTextures>,
}
pub struct TransitionParameters {
pub kind: ClipTransitionType,
pub progress: f32,
pub opaque: bool,
}
impl TransitionCompositor {
pub fn new(device: &wgpu::Device) -> Self {
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Transition Compositor Bind Group Layout"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::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::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Transition Compositor Pipeline Layout"),
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
let shader = device.create_shader_module(wgpu::include_wgsl!("shaders/transition.wgsl"));
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Transition Compositor Pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Rgba8Unorm,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: None,
});
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Transition Compositor Sampler"),
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,
..Default::default()
});
let uniforms_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Transition Compositor Uniforms"),
contents: bytemuck::bytes_of(&TransitionUniforms {
progress: 0.0,
kind: 0,
opaque: 1,
padding: 0,
}),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
Self {
bind_group_layout,
pipeline,
sampler,
uniforms_buffer,
textures: None,
}
}
pub fn ensure_size(&mut self, device: &wgpu::Device, width: u32, height: u32) {
if self
.textures
.as_ref()
.is_some_and(|textures| textures.width == width && textures.height == height)
{
return;
}
let create_texture = |label| {
device.create_texture(&wgpu::TextureDescriptor {
label: Some(label),
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: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
})
};
let outgoing = create_texture("Transition Outgoing Texture");
let incoming = create_texture("Transition Incoming Texture");
let outgoing_view = outgoing.create_view(&wgpu::TextureViewDescriptor::default());
let incoming_view = incoming.create_view(&wgpu::TextureViewDescriptor::default());
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Transition Compositor Bind Group"),
layout: &self.bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.uniforms_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&outgoing_view),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(&incoming_view),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
],
});
self.textures = Some(TransitionTextures {
outgoing,
incoming,
bind_group,
width,
height,
});
}
pub fn capture_outgoing(&self, encoder: &mut wgpu::CommandEncoder, source: &wgpu::Texture) {
if let Some(textures) = &self.textures {
copy_texture(
encoder,
source,
&textures.outgoing,
textures.width,
textures.height,
);
}
}
pub fn capture_incoming_and_render(
&self,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
source: &wgpu::Texture,
target: &wgpu::TextureView,
parameters: TransitionParameters,
) {
let Some(textures) = &self.textures else {
return;
};
copy_texture(
encoder,
source,
&textures.incoming,
textures.width,
textures.height,
);
queue.write_buffer(
&self.uniforms_buffer,
0,
bytemuck::bytes_of(&TransitionUniforms {
progress: parameters.progress.clamp(0.0, 1.0),
kind: match parameters.kind {
ClipTransitionType::CrossFade => 0,
ClipTransitionType::FadeThroughBlack => 1,
},
opaque: u32::from(parameters.opaque),
padding: 0,
}),
);
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("Transition Compositor Pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: target,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, &textures.bind_group, &[]);
pass.draw(0..3, 0..1);
}
}
fn copy_texture(
encoder: &mut wgpu::CommandEncoder,
source: &wgpu::Texture,
destination: &wgpu::Texture,
width: u32,
height: u32,
) {
encoder.copy_texture_to_texture(
wgpu::TexelCopyTextureInfo {
texture: source,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyTextureInfo {
texture: destination,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
},
);
}