Allow loading new DEM after one has already been loaded

This commit is contained in:
Matthew Gordon 2025-06-10 11:44:43 -03:00
parent b33af565f2
commit 420bfed5e9
3 changed files with 23 additions and 3 deletions

View File

@ -11,7 +11,7 @@ use {
};
pub struct DemRenderer {
source: Rc<Dem>,
pub source: Rc<Dem>,
pipeline: wgpu::RenderPipeline,
bind_group: wgpu::BindGroup,
vertex_buffer: wgpu::Buffer,

View File

@ -158,8 +158,12 @@ impl MvuApp<Model> for App {
async fn view(&mut self, model: Rc<Model>) -> Result<(), Box<dyn std::error::Error>> {
if let Some(context) = &mut self.context {
context.frame_timer.mark_frame_start();
if context.scene_data.is_none() {
if let Some(dem) = &model.dem {
if let Some(dem) = &model.dem {
if context
.scene_data
.as_ref()
.is_none_or(|scene_data| scene_data.source.id != dem.id)
{
context.scene_data = Some(DemRenderer::new(
dem.clone(),
&context.device,

View File

@ -1,4 +1,5 @@
use crate::mvu::File;
use std::sync::atomic::{AtomicU32, Ordering};
#[derive(Clone)]
pub struct Dem {
@ -11,6 +12,7 @@ pub struct Dem {
pub z_min: f32,
pub z_max: f32,
pub grid: Vec<u16>,
pub id: Id,
}
impl std::fmt::Debug for Dem {
@ -46,6 +48,18 @@ pub struct DemBvhLayer {
pub data: Vec<u16>,
}
#[derive(Clone, Eq, PartialEq)]
pub struct Id(u32);
impl Id {
fn get_next() -> Self {
NEXT_ID.fetch_add(1, Ordering::SeqCst);
Self(NEXT_ID.load(Ordering::SeqCst))
}
}
static NEXT_ID: AtomicU32 = AtomicU32::new(0);
/// Find the smallest number that's larger then the input and that is also one
/// less than a power of two.
fn round_bound(v: u32) -> u32 {
@ -198,6 +212,7 @@ impl Dem {
z_min,
z_max,
grid,
id: Id::get_next(),
}
}
_ => {
@ -283,6 +298,7 @@ mod tests {
z_min: z1.min(z2),
z_max: z1.max(z2),
grid,
id: Id::get_next()
},
)
.boxed()