Introduce draw_mesh_cache in mesh::Renderer

This commit is contained in:
Héctor Ramón Jiménez 2025-11-29 17:33:01 +01:00
parent 42bc4af972
commit afb3b8fce6
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
8 changed files with 119 additions and 69 deletions

View file

@ -8,8 +8,7 @@ use crate::graphics::mesh::{self, Mesh};
use rustc_hash::FxHashMap;
use std::collections::hash_map;
use std::sync::atomic::{self, AtomicU64};
use std::sync::{self, Arc};
use std::sync::Weak;
const INITIAL_INDEX_COUNT: usize = 1_000;
const INITIAL_VERTEX_COUNT: usize = 1_000;
@ -24,52 +23,21 @@ pub enum Item {
},
Cached {
transformation: Transformation,
cache: Cache,
cache: mesh::Cache,
},
}
#[derive(Debug, Clone)]
pub struct Cache {
id: Id,
batch: Arc<[Mesh]>,
version: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(u64);
impl Cache {
pub fn new(meshes: Vec<Mesh>) -> Option<Self> {
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
if meshes.is_empty() {
return None;
}
Some(Self {
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
batch: Arc::from(meshes),
version: 0,
})
}
pub fn update(&mut self, meshes: Vec<Mesh>) {
self.batch = Arc::from(meshes);
self.version += 1;
}
}
#[derive(Debug)]
struct Upload {
layer: Layer,
transformation: Transformation,
version: usize,
batch: sync::Weak<[Mesh]>,
batch: Weak<[Mesh]>,
}
#[derive(Debug, Default)]
pub struct Storage {
uploads: FxHashMap<Id, Upload>,
uploads: FxHashMap<mesh::Id, Upload>,
}
impl Storage {
@ -77,12 +45,12 @@ impl Storage {
Self::default()
}
fn get(&self, cache: &Cache) -> Option<&Upload> {
if cache.batch.is_empty() {
fn get(&self, cache: &mesh::Cache) -> Option<&Upload> {
if cache.is_empty() {
return None;
}
self.uploads.get(&cache.id)
self.uploads.get(&cache.id())
}
fn prepare(
@ -92,15 +60,15 @@ impl Storage {
belt: &mut wgpu::util::StagingBelt,
solid: &solid::Pipeline,
gradient: &gradient::Pipeline,
cache: &Cache,
cache: &mesh::Cache,
new_transformation: Transformation,
) {
match self.uploads.entry(cache.id) {
match self.uploads.entry(cache.id()) {
hash_map::Entry::Occupied(entry) => {
let upload = entry.into_mut();
if !cache.batch.is_empty()
&& (upload.version != cache.version
if !cache.is_empty()
&& (upload.version != cache.version()
|| upload.transformation != new_transformation)
{
upload.layer.prepare(
@ -109,12 +77,12 @@ impl Storage {
belt,
solid,
gradient,
&cache.batch,
cache.batch(),
new_transformation,
);
upload.batch = Arc::downgrade(&cache.batch);
upload.version = cache.version;
upload.batch = cache.downgrade();
upload.version = cache.version();
upload.transformation = new_transformation;
}
}
@ -127,7 +95,7 @@ impl Storage {
belt,
solid,
gradient,
&cache.batch,
cache.batch(),
new_transformation,
);
@ -135,12 +103,12 @@ impl Storage {
layer,
transformation: new_transformation,
version: 0,
batch: Arc::downgrade(&cache.batch),
batch: cache.downgrade(),
});
log::debug!(
"New mesh upload: {} (total: {})",
cache.id.0,
"New mesh upload: {:?} (total: {})",
cache.id(),
self.uploads.len()
);
}
@ -278,7 +246,7 @@ impl State {
Some((
&upload.layer,
&cache.batch,
cache.batch(),
screen_transformation * *transformation,
))
}