Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glyphon text rendering #369

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,637 changes: 1,535 additions & 1,102 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ nokhwa = { git = "https://github.com/CapSoftware/nokhwa", rev = "e309b938ccd6",
"serialize",
] }
nokhwa-bindings-macos = { git = "https://github.com/CapSoftware/nokhwa", rev = "e309b938ccd6" }
wgpu = "22.1.0"
wgpu = "24.0.1"
flume = "0.11.0"
thiserror = "1.0"
sentry = { version = "0.34.0", features = [
Expand Down
9 changes: 7 additions & 2 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use cap_media::{feeds::RawCameraFrame, frame_ws::WSFrame};
use cap_project::{BackgroundSource, CursorEvents, RecordingMeta, StudioRecordingMeta, XY};
use cap_rendering::{
decoder::DecodedFrame, DecodedSegmentFrames, FrameRenderer, ProjectRecordings, ProjectUniforms,
RenderVideoConstants,
RenderVideoConstants, RenderVideoState,
};
use tokio::{
sync::Mutex,
sync::{mpsc, oneshot},
task::JoinHandle,
};
Expand All @@ -27,6 +28,7 @@ pub struct Renderer {
rx: mpsc::Receiver<RendererMessage>,
frame_tx: flume::Sender<WSFrame>,
render_constants: Arc<RenderVideoConstants>,
render_state: Mutex<RenderVideoState>,
total_frames: u32,
}

Expand All @@ -37,6 +39,7 @@ pub struct RendererHandle {
impl Renderer {
pub fn spawn(
render_constants: Arc<RenderVideoConstants>,
render_state: Mutex<RenderVideoState>,
frame_tx: flume::Sender<WSFrame>,
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
Expand All @@ -61,6 +64,7 @@ impl Renderer {
rx,
frame_tx,
render_constants,
render_state,
total_frames,
};

Expand All @@ -72,7 +76,8 @@ impl Renderer {
async fn run(mut self) {
let mut frame_task: Option<JoinHandle<()>> = None;

let mut frame_renderer = FrameRenderer::new(&self.render_constants);
let mut mutables = self.render_state.lock().await;
let mut frame_renderer = FrameRenderer::new(&self.render_constants, &mut mutables);

loop {
while let Some(msg) = self.rx.recv().await {
Expand Down
16 changes: 15 additions & 1 deletion crates/editor/src/editor_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cap_project::{CursorEvents, ProjectConfiguration, RecordingMeta, RecordingMe
use cap_project::{RecordingConfig, StudioRecordingMeta};
use cap_rendering::{
get_duration, ProjectRecordings, ProjectUniforms, RecordingSegmentDecoders, RenderOptions,
RenderVideoConstants, SegmentVideoPaths,
RenderVideoConstants, RenderVideoState, SegmentVideoPaths,
};
use std::ops::Deref;
use std::sync::Mutex as StdMutex;
Expand Down Expand Up @@ -102,9 +102,11 @@ impl EditorInstance {
.await
.unwrap(),
);
let render_state = Mutex::new(RenderVideoState::new(&render_constants));

let renderer = Arc::new(editor::Renderer::spawn(
render_constants.clone(),
render_state,
frame_tx,
&recording_meta,
meta,
Expand Down Expand Up @@ -278,6 +280,18 @@ impl EditorInstance {
frame_number,
fps,
resolution_base,
None,
// Some(cap_rendering::Text {
// x: 500.0,
// y: 500.0,
// background_color: Some([0, 255, 0]),
// background_opacity: Some(10.0),
// padding: Some(10.0),
// text_color: Some([255, 0, 0]),
// font_size: Some(60.0),
// line_height: Some(82.0),
// content: "Preview".into(),
// }),
),
segment.cursor.clone(),
)
Expand Down
1 change: 1 addition & 0 deletions crates/editor/src/playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Playback {
frame_number,
fps,
resolution_base,
None, // Some("Playback".into()),
);

self.renderer
Expand Down
10 changes: 5 additions & 5 deletions crates/gpu-converters/src/nv12_rgba/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct NV12ToRGBA {
impl NV12ToRGBA {
pub async fn new() -> Self {
println!("NV12ToRGBA");
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default());
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default());

// Get adapter for GPU
let adapter = instance
Expand Down Expand Up @@ -85,7 +85,7 @@ impl NV12ToRGBA {
label: Some("NV12 Converter Pipeline"),
layout: Some(&pipeline_layout),
module: &shader,
entry_point: "main",
entry_point: Some("main"),
compilation_options: Default::default(),
cache: None,
});
Expand Down Expand Up @@ -209,15 +209,15 @@ impl NV12ToRGBA {

// Copy texture to buffer
encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
wgpu::TexelCopyTextureInfo {
texture: &output_texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::ImageCopyBuffer {
wgpu::TexelCopyBufferInfo {
buffer: &output_buffer,
layout: wgpu::ImageDataLayout {
layout: wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(width * 4),
rows_per_image: Some(height),
Expand Down
6 changes: 3 additions & 3 deletions crates/gpu-converters/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ pub fn copy_texture_to_buffer_command(
let bytes_per_row = texture.width() * bytes_per_px;

encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
wgpu::TexelCopyTextureInfo {
texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::ImageCopyBuffer {
wgpu::TexelCopyBufferInfo {
buffer: &output_buffer,
layout: wgpu::ImageDataLayout {
layout: wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(texture.height()),
Expand Down
4 changes: 2 additions & 2 deletions crates/gpu-converters/src/uyvy_nv12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl UYVYToNV12 {
pub async fn new() -> Self {
todo!("implement UV downsampling for UYVYToNV12");

let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default());
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default());

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand Down Expand Up @@ -99,7 +99,7 @@ impl UYVYToNV12 {
label: Some("YUYV Converter Pipeline"),
layout: Some(&pipeline_layout),
module: &shader,
entry_point: "main",
entry_point: Some("main"),
compilation_options: Default::default(),
cache: None,
});
Expand Down
4 changes: 2 additions & 2 deletions crates/gpu-converters/src/uyvy_rgba/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct UYVYToRGBA {

impl UYVYToRGBA {
pub async fn new() -> Self {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::default());
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default());

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand Down Expand Up @@ -74,7 +74,7 @@ impl UYVYToRGBA {
label: Some("YUYV Converter Pipeline"),
layout: Some(&pipeline_layout),
module: &shader,
entry_point: "main",
entry_point: Some("main"),
compilation_options: Default::default(),
cache: None,
});
Expand Down
1 change: 1 addition & 0 deletions crates/rendering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ thiserror.workspace = true
wgpu.workspace = true
bezier_easing = "0.1.1"
reactive_graph = "0.1.5"
glyphon = "0.8.0"

[target.'cfg(target_os = "macos")'.dependencies]
cidre.workspace = true
Expand Down
11 changes: 7 additions & 4 deletions crates/rendering/src/frame_pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use futures_intrusive::channel::shared::oneshot_channel;
use wgpu::COPY_BYTES_PER_ROW_ALIGNMENT;

use crate::{get_either, ProjectUniforms, RenderVideoConstants, RenderingError};
use crate::{get_either, ProjectUniforms, RenderVideoConstants, RenderVideoState, RenderingError};

pub struct FramePipeline<'a, 'b> {
pub state: &'a mut FramePipelineState<'b>,
Expand All @@ -10,6 +10,7 @@ pub struct FramePipeline<'a, 'b> {

pub struct FramePipelineState<'a> {
pub constants: &'a RenderVideoConstants,
pub state: &'a mut RenderVideoState,
pub uniforms: &'a ProjectUniforms,
pub textures: &'a (wgpu::Texture, wgpu::Texture),
pub texture_views: (wgpu::TextureView, wgpu::TextureView),
Expand All @@ -19,6 +20,7 @@ pub struct FramePipelineState<'a> {
impl<'a> FramePipelineState<'a> {
pub fn new(
constants: &'a RenderVideoConstants,
state: &'a mut RenderVideoState,
uniforms: &'a ProjectUniforms,
textures: &'a (wgpu::Texture, wgpu::Texture),
) -> Self {
Expand All @@ -33,6 +35,7 @@ impl<'a> FramePipelineState<'a> {

Self {
constants,
state,
uniforms,
textures,
texture_views,
Expand Down Expand Up @@ -156,15 +159,15 @@ impl FramePipelineEncoder {
);

encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
wgpu::TexelCopyTextureInfo {
texture: state.get_current_texture(),
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::ImageCopyBuffer {
wgpu::TexelCopyBufferInfo {
buffer: &output_buffer,
layout: wgpu::ImageDataLayout {
layout: wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(padded_bytes_per_row),
rows_per_image: Some(state.uniforms.output_size.1),
Expand Down
16 changes: 6 additions & 10 deletions crates/rendering/src/layers/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ impl BackgroundLayer {
});

constants.queue.write_texture(
wgpu::ImageCopyTexture {
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&rgba,
wgpu::ImageDataLayout {
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(4 * dimensions.0),
rows_per_image: Some(dimensions.1),
Expand Down Expand Up @@ -304,17 +304,16 @@ impl ImageBackgroundPipeline {
),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: wgpu::PipelineCompilationOptions {
constants: &empty_constants,
zero_initialize_workgroup_memory: false,
vertex_pulling_transform: false,
},
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Rgba8UnormSrgb,
blend: Some(wgpu::BlendState::REPLACE),
Expand All @@ -323,7 +322,6 @@ impl ImageBackgroundPipeline {
compilation_options: wgpu::PipelineCompilationOptions {
constants: &empty_constants,
zero_initialize_workgroup_memory: false,
vertex_pulling_transform: false,
},
}),
primitive: wgpu::PrimitiveState {
Expand Down Expand Up @@ -540,17 +538,16 @@ impl BackgroundBlurPipeline {
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: wgpu::PipelineCompilationOptions {
constants: &empty_constants,
zero_initialize_workgroup_memory: false,
vertex_pulling_transform: false,
},
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Rgba8UnormSrgb,
blend: Some(wgpu::BlendState::REPLACE),
Expand All @@ -559,7 +556,6 @@ impl BackgroundBlurPipeline {
compilation_options: wgpu::PipelineCompilationOptions {
constants: &empty_constants,
zero_initialize_workgroup_memory: false,
vertex_pulling_transform: false,
},
}),
primitive: wgpu::PrimitiveState {
Expand Down
4 changes: 2 additions & 2 deletions crates/rendering/src/layers/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ impl CameraLayer {
let constants = pipeline.state.constants;

constants.queue.write_texture(
wgpu::ImageCopyTexture {
wgpu::TexelCopyTextureInfo {
texture: &texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
camera_frame,
wgpu::ImageDataLayout {
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(camera_size.x * 4),
rows_per_image: None,
Expand Down
6 changes: 2 additions & 4 deletions crates/rendering/src/layers/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,16 @@ impl CursorLayer {
),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
entry_point: Some("vs_main"),
buffers: &[],
compilation_options: wgpu::PipelineCompilationOptions {
constants: &empty_constants,
zero_initialize_workgroup_memory: false,
vertex_pulling_transform: false,
},
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: "fs_main",
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: wgpu::TextureFormat::Rgba8UnormSrgb,
blend: Some(wgpu::BlendState {
Expand All @@ -97,7 +96,6 @@ impl CursorLayer {
compilation_options: wgpu::PipelineCompilationOptions {
constants: &empty_constants,
zero_initialize_workgroup_memory: false,
vertex_pulling_transform: false,
},
}),
primitive: wgpu::PrimitiveState {
Expand Down
4 changes: 2 additions & 2 deletions crates/rendering/src/layers/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ impl DisplayLayer {
let frame_size = pipeline.state.constants.options.screen_size;

constants.queue.write_texture(
wgpu::ImageCopyTexture {
wgpu::TexelCopyTextureInfo {
texture: &constants.screen_frame.0,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
&segment_frames.screen_frame,
wgpu::ImageDataLayout {
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(constants.options.screen_size.x * 4),
rows_per_image: None,
Expand Down
Loading
Loading