pub fn create_egui_sampler(device: &wgpu::Device) -> wgpu::Sampler {
device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("egui_bridge_sampler"),
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
..Default::default()
})
}
#[derive(Debug, Clone, Copy)]
pub struct ViewportSize {
pub width: u32,
pub height: u32,
}
impl ViewportSize {
#[must_use]
pub fn new(width: u32, height: u32) -> Self {
Self { width, height }
}
#[must_use]
pub fn aspect_ratio(&self) -> f32 {
if self.height == 0 {
1.0
} else {
self.width as f32 / self.height as f32
}
}
#[must_use]
pub fn needs_resize(&self, new_width: u32, new_height: u32) -> bool {
self.width != new_width || self.height != new_height
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn viewport_size() {
let v = ViewportSize::new(1280, 720);
assert!((v.aspect_ratio() - 16.0 / 9.0).abs() < 0.01);
}
#[test]
fn viewport_needs_resize() {
let v = ViewportSize::new(800, 600);
assert!(!v.needs_resize(800, 600));
assert!(v.needs_resize(1024, 768));
}
#[test]
fn viewport_zero_height() {
let v = ViewportSize::new(100, 0);
assert_eq!(v.aspect_ratio(), 1.0);
}
}