glyphon/src/viewport.rs
2024-05-08 11:47:58 -02:30

57 lines
1.5 KiB
Rust

use crate::{Cache, Params, Resolution};
use wgpu::{BindGroup, Buffer, BufferDescriptor, BufferUsages, Device, Queue};
use std::mem;
use std::slice;
#[derive(Debug)]
pub struct Viewport {
params: Params,
params_buffer: Buffer,
pub(crate) bind_group: BindGroup,
}
impl Viewport {
pub fn new(device: &Device, cache: &Cache) -> Self {
let params = Params {
screen_resolution: Resolution {
width: 0,
height: 0,
},
_pad: [0, 0],
};
let params_buffer = device.create_buffer(&BufferDescriptor {
label: Some("glyphon params"),
size: mem::size_of::<Params>() as u64,
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let bind_group = cache.create_uniforms_bind_group(device, &params_buffer);
Self {
params,
params_buffer,
bind_group,
}
}
pub fn update(&mut self, queue: &Queue, resolution: Resolution) {
if self.params.screen_resolution != resolution {
self.params.screen_resolution = resolution;
queue.write_buffer(&self.params_buffer, 0, unsafe {
slice::from_raw_parts(
&self.params as *const Params as *const u8,
mem::size_of::<Params>(),
)
});
}
}
pub fn resolution(&self) -> Resolution {
self.params.screen_resolution
}
}