Skip to content

Commit

Permalink
Merge pull request #63 from nus-vv-streams/fix/width-and-height
Browse files Browse the repository at this point in the history
Fix resize issue and upgrade wgpu/winit/egui
  • Loading branch information
weitsang authored May 29, 2024
2 parents 6a765cc + df1794e commit 59bd207
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 463 deletions.
745 changes: 333 additions & 412 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ serde_with = { version = "3.0.0" }
crossbeam-channel = "0.5.6"

# For GUI Rendering
winit = { version = "0.27.3" }
wgpu = { version = "0.13.1" }
wgpu_glyph = { version = "0.17.0" }
egui = { version = "0.19.0" }
egui_winit_platform = { version = "0.16.0" }
egui_wgpu_backend = { version = "0.19.0" }
epi = { version = "0.17.0" }
winit = { version = "0.28" } # Need this for fix Sonoma bug with window size
wgpu = { version = "0.15" } # Need by egui_wgpu_backend 0.22
wgpu_glyph = { version = "0.19" } # Need this for wgpu 0.15
egui = { version = "0.21" } # Need this for egui_winit_platform 0.18
egui_winit_platform = { version = "^0.18" } # Need this for winit 0.28 onwards
egui_wgpu_backend = { version = "0.22" } # Need this for egui 0.21
epi = { version = "0.17" }
web-sys = { version = "0.3.64" }

# ffmpeg-next = "6"
ffmpeg-next = { version = "6", optional = true }
Expand Down Expand Up @@ -99,15 +100,15 @@ name = "vvplay"
# [[bin]]
# name = "vvdash"

[[bin]]
name = "vv"
#[[bin]]
#name = "vv"

[[bin]]
name = "vvplay_async"
#[[bin]]
#name = "vvplay_async"
# required-features = ["dash"]

[[bin]]
name = "exporter"
#[[bin]]
#name = "exporter"

[features]
default = ["with-tmc2-rs-decoder", "async" ]
Expand Down
18 changes: 17 additions & 1 deletion src/bin/vvplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,67 @@ struct Args {
/// 1. Directory with all the pcd files in lexicographical order
/// 2. location of the mpd file
src: String,
#[clap(short = 'q', long, default_value_t = 0)]

#[clap(short, long, default_value_t = 0)]
quality: u8,

#[clap(short, long, default_value_t = 30.0)]
fps: f32,

#[clap(
short = 'x',
long,
default_value_t = 0.0,
allow_negative_numbers = true
)]
camera_x: f32,

#[clap(
short = 'y',
long,
default_value_t = 0.0,
allow_negative_numbers = true
)]
camera_y: f32,

#[clap(
short = 'z',
long,
default_value_t = 1.3,
allow_negative_numbers = true
)]
camera_z: f32,

#[clap(long = "yaw", default_value_t = -90.0, allow_negative_numbers = true)]
camera_yaw: f32,

#[clap(long = "pitch", default_value_t = 0.0, allow_negative_numbers = true)]
camera_pitch: f32,

#[clap(short = 'W', long, default_value_t = 1600)]
width: u32,

#[clap(short = 'H', long, default_value_t = 900)]
height: u32,

#[clap(long = "controls", default_value_t = true)]
show_controls: bool,

#[clap(short, long)]
buffer_size: Option<u8>,

#[clap(short, long)]
metrics: Option<OsString>,

#[clap(long = "decoder", value_enum, default_value_t = DecoderType::Noop)]
decoder_type: DecoderType,

#[clap(long)]
decoder_path: Option<OsString>,

#[clap(long, default_value = "rgb(255,255,255)")]
bg_color: OsString,

#[clap(long, default_value = "false")]
adaptive_upsampling: bool,
}
Expand Down
5 changes: 1 addition & 4 deletions src/reconstruct/poisson_reconstruction/hgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ impl BuildHasher for DeterministicState {

/// A grid based on spacial hashing.
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(
feature = "serde-serialize",
derive(serde::Serialize, serde::Deserialize)
)]
// #[cfg_attr(derive(serde::Serialize, serde::Deserialize),)]
pub struct HGrid<T> {
cells: HashMap<Point3<i64>, Vec<T>, DeterministicState>,
origin: Point3<Real>,
Expand Down
35 changes: 25 additions & 10 deletions src/render/wgpu/builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::render::wgpu::camera::Camera;
use std::collections::HashMap;
use winit::dpi::PhysicalSize;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event::{ElementState, Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop, EventLoopBuilder};
use winit::window::{Window, WindowId};

Expand Down Expand Up @@ -102,6 +102,7 @@ impl RenderBuilder {
}

pub fn run(mut self) {
// The main event loop of vvplay
self.event_loop.run(move |new_event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match &new_event {
Expand All @@ -128,6 +129,7 @@ impl RenderBuilder {
WindowEvent::CloseRequested
| WindowEvent::Destroyed
| WindowEvent::KeyboardInput {
// ESC quits vvplay
input:
KeyboardInput {
state: ElementState::Pressed,
Expand All @@ -139,21 +141,34 @@ impl RenderBuilder {
*control_flow = ControlFlow::Exit;
}
WindowEvent::Resized(physical_size) => {
#[cfg(target_os = "macos")]
if physical_size.width == u32::MAX
|| physical_size.height == u32::MAX
{
// HACK to fix a bug on Macos 14
// https://github.com/rust-windowing/winit/issues/2876
return;
}
windowed_object.object.resize(*physical_size);
if windowed_object.focused {
windowed_object
.object
.handle_event(&new_event, &windowed_object.window)
}
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
windowed_object.object.resize(**new_inner_size);
}
WindowEvent::Focused(focus) => windowed_object.focused = *focus,
WindowEvent::Focused(focus) => {
windowed_object.focused = *focus;
}
WindowEvent::MouseInput {
button: MouseButton::Left,
..
} => {
// self.mouse_pressed = *state == ElementState::Pressed;
// true
if windowed_object.focused {
windowed_object
.object
.handle_event(&new_event, &windowed_object.window)
}
}
_ => {
// For all other events, we pass them to the focused window
// Continue in renderer.rs
if windowed_object.focused {
windowed_object
.object
Expand Down
8 changes: 7 additions & 1 deletion src/render/wgpu/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,19 @@ impl CameraState {
}
}

pub fn process_input(&mut self, event: &DeviceEvent) -> bool {
pub fn handle_keyboard_input(&mut self, event: &DeviceEvent) -> bool {
match event {
DeviceEvent::Key(KeyboardInput {
virtual_keycode: Some(key),
state,
..
}) => self.camera_controller.process_keyboard(*key, *state),
_ => false,
}
}

pub fn handle_mouse_input(&mut self, event: &DeviceEvent) -> bool {
match event {
DeviceEvent::MouseWheel { delta, .. } => {
self.camera_controller.process_scroll(delta);
true
Expand Down
2 changes: 1 addition & 1 deletion src/render/wgpu/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Attachable for Controller {

let gpu = pollster::block_on(WindowGpu::new(&window));

let surface_format = gpu.surface.get_supported_formats(&gpu.adapter)[0];
let surface_format = gpu.surface.get_capabilities(&gpu.adapter).formats[0];

let event_proxy = Arc::new(EventProxy(
std::sync::Mutex::new(event_loop.create_proxy()),
Expand Down
11 changes: 8 additions & 3 deletions src/render/wgpu/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use wgpu::InstanceDescriptor;
use winit::window::Window;

pub struct WindowGpu {
Expand All @@ -14,12 +15,12 @@ impl WindowGpu {
let size = window.inner_size();
// The instance is a handle to our GPU
// Backends::all => Vulkan + Metal + DX12 + Browser WebGPU
let instance = wgpu::Instance::new(wgpu::Backends::all());
let instance = wgpu::Instance::new(InstanceDescriptor::default());

// The surface is the part of the window that we draw to.
//
// Safety!: The surface needs to live as long as the window that created it.
let surface = unsafe { instance.create_surface(window) };
let surface = unsafe { instance.create_surface(window).unwrap() };
// The adapter is a handle to our actual graphics card.
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand All @@ -46,13 +47,16 @@ impl WindowGpu {
.unwrap();

// config defines how the surface creates its underlying `SurfaceTexture`
let surface_caps = surface.get_capabilities(&adapter);
let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
// wgpu::TextureFormat::Bgra8UnormSrgb,
format: surface.get_supported_formats(&adapter)[0],
format: surface_caps.formats[0],
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: surface_caps.alpha_modes[0],
view_formats: vec![], // new 0.15
};
surface.configure(&device, &config);

Expand All @@ -69,6 +73,7 @@ impl WindowGpu {
/// Reconfigure the surface every time the window's size changes
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
// print!("WindowGpu::resize {:#?}\n", new_size);
self.size = new_size;
self.config.width = new_size.width;
self.config.height = new_size.height;
Expand Down
5 changes: 3 additions & 2 deletions src/render/wgpu/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ffi::OsString;
use std::num::NonZeroU32;
use std::path::Path;
use std::str::FromStr;
use wgpu::{Buffer, Device, Queue, Texture, TextureDescriptor, TextureView};
use wgpu::{Buffer, Device, InstanceDescriptor, Queue, Texture, TextureDescriptor, TextureView};
use winit::dpi::PhysicalSize;

use super::camera::CameraPosition;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> PngWriter<'a> {
std::fs::create_dir_all(output_path).expect("Failed to create output directory");

let size = PhysicalSize::new(width, height);
let instance = wgpu::Instance::new(wgpu::Backends::all());
let instance = wgpu::Instance::new(InstanceDescriptor::default());
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
compatible_surface: None,
Expand All @@ -97,6 +97,7 @@ impl<'a> PngWriter<'a> {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
view_formats: &[],
usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::RENDER_ATTACHMENT,
label: None,
};
Expand Down
2 changes: 2 additions & 0 deletions src/render/wgpu/renderable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub trait Renderable: Clone {
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float,
view_formats: &[],
usage: TextureUsages::RENDER_ATTACHMENT,
});

Expand Down Expand Up @@ -192,6 +193,7 @@ impl Renderable for PointCloud<PointXyzRgba> {
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float,
view_formats: &[],
usage: TextureUsages::RENDER_ATTACHMENT,
});

Expand Down
Loading

0 comments on commit 59bd207

Please sign in to comment.