summaryrefslogtreecommitdiffstats
path: root/wgpu
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-06 03:06:40 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-06 03:06:40 +0200
commit441aac25995290a83162a4728f22492ff69a5f4d (patch)
treee53143c42d73c729a021ff510819dbf2261c55f1 /wgpu
parent7eb16452f340fe228e6928b496f8df6e9e86e554 (diff)
downloadiced-441aac25995290a83162a4728f22492ff69a5f4d.tar.gz
iced-441aac25995290a83162a4728f22492ff69a5f4d.tar.bz2
iced-441aac25995290a83162a4728f22492ff69a5f4d.zip
Avoid generating empty caches in `iced_wgpu`
Diffstat (limited to 'wgpu')
-rw-r--r--wgpu/src/geometry.rs17
-rw-r--r--wgpu/src/lib.rs9
-rw-r--r--wgpu/src/text.rs31
-rw-r--r--wgpu/src/triangle.rs31
4 files changed, 66 insertions, 22 deletions
diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs
index b689d2a7..c36ff38e 100644
--- a/wgpu/src/geometry.rs
+++ b/wgpu/src/geometry.rs
@@ -38,8 +38,8 @@ pub enum Geometry {
#[derive(Clone)]
pub struct Cache {
- pub meshes: triangle::Cache,
- pub text: text::Cache,
+ pub meshes: Option<triangle::Cache>,
+ pub text: Option<text::Cache>,
}
impl Cached for Geometry {
@@ -53,8 +53,17 @@ impl Cached for Geometry {
match self {
Self::Live { meshes, text } => {
if let Some(mut previous) = previous {
- previous.meshes.update(meshes);
- previous.text.update(text);
+ if let Some(cache) = &mut previous.meshes {
+ cache.update(meshes);
+ } else {
+ previous.meshes = triangle::Cache::new(meshes);
+ }
+
+ if let Some(cache) = &mut previous.text {
+ cache.update(text);
+ } else {
+ previous.text = text::Cache::new(text);
+ }
previous
} else {
diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs
index d632919f..dfc1aad4 100644
--- a/wgpu/src/lib.rs
+++ b/wgpu/src/lib.rs
@@ -517,8 +517,13 @@ impl graphics::geometry::Renderer for Renderer {
self.layers.draw_text_group(text);
}
Geometry::Cached(cache) => {
- self.layers.draw_mesh_cache(cache.meshes);
- self.layers.draw_text_cache(cache.text);
+ if let Some(meshes) = cache.meshes {
+ self.layers.draw_mesh_cache(meshes);
+ }
+
+ if let Some(text) = cache.text {
+ self.layers.draw_text_cache(text);
+ }
}
}
}
diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs
index e84e675d..1b21bb1c 100644
--- a/wgpu/src/text.rs
+++ b/wgpu/src/text.rs
@@ -43,14 +43,18 @@ pub struct Cache {
pub struct Id(u64);
impl Cache {
- pub fn new(text: Vec<Text>) -> Self {
+ pub fn new(text: Vec<Text>) -> Option<Self> {
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
- Self {
+ if text.is_empty() {
+ return None;
+ }
+
+ Some(Self {
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
text: Rc::from(text),
version: 0,
- }
+ })
}
pub fn update(&mut self, text: Vec<Text>) {
@@ -78,8 +82,12 @@ impl Storage {
Self::default()
}
- fn get(&self, id: Id) -> Option<&Upload> {
- self.uploads.get(&id)
+ fn get(&self, cache: &Cache) -> Option<&Upload> {
+ if cache.text.is_empty() {
+ return None;
+ }
+
+ self.uploads.get(&cache.id)
}
fn prepare(
@@ -97,8 +105,9 @@ impl Storage {
hash_map::Entry::Occupied(entry) => {
let upload = entry.into_mut();
- if upload.version != cache.version
- || upload.transformation != new_transformation
+ if !cache.text.is_empty()
+ && (upload.version != cache.version
+ || upload.transformation != new_transformation)
{
let _ = prepare(
device,
@@ -154,6 +163,12 @@ impl Storage {
transformation: new_transformation,
version: 0,
});
+
+ log::info!(
+ "New text upload: {} (total: {})",
+ cache.id.0,
+ self.uploads.len()
+ );
}
}
@@ -291,7 +306,7 @@ impl Pipeline {
layer_count += 1;
}
Item::Cached { cache, .. } => {
- if let Some(upload) = storage.get(cache.id) {
+ if let Some(upload) = storage.get(cache) {
upload
.renderer
.render(&upload.atlas, render_pass)
diff --git a/wgpu/src/triangle.rs b/wgpu/src/triangle.rs
index a08b6987..de6c026a 100644
--- a/wgpu/src/triangle.rs
+++ b/wgpu/src/triangle.rs
@@ -38,14 +38,18 @@ pub struct Cache {
pub struct Id(u64);
impl Cache {
- pub fn new(meshes: Vec<Mesh>) -> Self {
+ pub fn new(meshes: Vec<Mesh>) -> Option<Self> {
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
- Self {
+ if meshes.is_empty() {
+ return None;
+ }
+
+ Some(Self {
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
batch: Rc::from(meshes),
version: 0,
- }
+ })
}
pub fn update(&mut self, meshes: Vec<Mesh>) {
@@ -72,8 +76,12 @@ impl Storage {
Self::default()
}
- fn get(&self, id: Id) -> Option<&Upload> {
- self.uploads.get(&id)
+ fn get(&self, cache: &Cache) -> Option<&Upload> {
+ if cache.batch.is_empty() {
+ return None;
+ }
+
+ self.uploads.get(&cache.id)
}
fn prepare(
@@ -90,8 +98,9 @@ impl Storage {
hash_map::Entry::Occupied(entry) => {
let upload = entry.into_mut();
- if upload.version != cache.version
- || upload.transformation != new_transformation
+ if !cache.batch.is_empty()
+ && (upload.version != cache.version
+ || upload.transformation != new_transformation)
{
upload.layer.prepare(
device,
@@ -125,6 +134,12 @@ impl Storage {
transformation: new_transformation,
version: 0,
});
+
+ log::info!(
+ "New mesh upload: {} (total: {})",
+ cache.id.0,
+ self.uploads.len()
+ );
}
}
@@ -247,7 +262,7 @@ impl Pipeline {
transformation,
cache,
} => {
- let upload = storage.get(cache.id)?;
+ let upload = storage.get(cache)?;
Some((
&upload.layer,