From 1bcfc9a5cce0b30c3ad9983e407c06e237b491f3 Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Fri, 10 Jan 2020 14:39:29 +0100 Subject: Implemented a texture atlas for images and svgs. --- wgpu/src/image/vector.rs | 198 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 149 insertions(+), 49 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 713978f5..1a9352f2 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,8 +1,9 @@ use iced_native::svg; use std::{ collections::{HashMap, HashSet}, - rc::Rc, + fmt, }; +use guillotiere::{Allocation, AtlasAllocator, Size}; pub enum Svg { Loaded { tree: resvg::usvg::Tree }, @@ -22,27 +23,63 @@ impl Svg { } } -impl std::fmt::Debug for Svg { +impl fmt::Debug for Svg { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "Svg") } } -#[derive(Debug)] pub struct Cache { + allocator: AtlasAllocator, + atlas: wgpu::Texture, svgs: HashMap, - rasterized: HashMap<(u64, u32, u32), Rc>, + rasterized: HashMap<(u64, u32, u32), Allocation>, svg_hits: HashSet, rasterized_hits: HashSet<(u64, u32, u32)>, } +impl fmt::Debug for Cache { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("Vector Cache") + .field("allocator", &String::from("AtlasAllocator")) + .field("atlas", &self.atlas) + .field("svgs", &self.svgs) + .field("rasterized", &String::from("HashMap<(u64, u32, u32), Allocation>")) + .field("svg_hits", &self.svg_hits) + .field("rasterized_hits", &self.rasterized_hits) + .finish() + } +} + impl Cache { - pub fn new() -> Self { + pub fn new(device: &wgpu::Device) -> Self { + let (width, height) = (512, 512); + + let extent = wgpu::Extent3d { + width, + height, + depth: 1, + }; + + let atlas = device.create_texture(&wgpu::TextureDescriptor { + size: extent, + array_layer_count: 1, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Bgra8UnormSrgb, + usage: wgpu::TextureUsage::COPY_DST + | wgpu::TextureUsage::COPY_SRC + | wgpu::TextureUsage::SAMPLED, + }); + Self { svgs: HashMap::new(), rasterized: HashMap::new(), svg_hits: HashSet::new(), rasterized_hits: HashSet::new(), + allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)), + atlas, } } @@ -62,6 +99,10 @@ impl Cache { self.svgs.get(&handle.id()).unwrap() } + pub fn atlas_size(&self) -> guillotiere::Size { + self.allocator.size() + } + pub fn upload( &mut self, handle: &svg::Handle, @@ -69,8 +110,7 @@ impl Cache { scale: f32, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - texture_layout: &wgpu::BindGroupLayout, - ) -> Option> { + ) -> Option<&Allocation> { let id = handle.id(); let (width, height) = ( @@ -82,36 +122,88 @@ impl Cache { // We currently rerasterize the SVG when its size changes. This is slow // as heck. A GPU rasterizer like `pathfinder` may perform better. // It would be cool to be able to smooth resize the `svg` example. - if let Some(bind_group) = self.rasterized.get(&(id, width, height)) { + if self.rasterized.get(&(id, width, height)).is_some() { let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); - return Some(bind_group.clone()); + return self.rasterized.get(&(id, width, height)); } - match self.load(handle) { + let _ = self.load(handle); + + match self.svgs.get(&handle.id()).unwrap() { Svg::Loaded { tree } => { if width == 0 || height == 0 { return None; } - let extent = wgpu::Extent3d { - width, - height, - depth: 1, - }; + let size = Size::new(width as i32, height as i32); + let old_atlas_size = self.allocator.size(); + let allocation; + + loop { + if let Some(a) = self.allocator.allocate(size) { + allocation = a; + break; + } - let texture = device.create_texture(&wgpu::TextureDescriptor { - size: extent, - array_layer_count: 1, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Bgra8UnormSrgb, - usage: wgpu::TextureUsage::COPY_DST - | wgpu::TextureUsage::SAMPLED, - }); + self.allocator.grow(self.allocator.size() * 2); + } + let new_atlas_size = self.allocator.size(); + + if new_atlas_size != old_atlas_size { + let new_atlas = device.create_texture(&wgpu::TextureDescriptor { + size: wgpu::Extent3d { + width: new_atlas_size.width as u32, + height: new_atlas_size.height as u32, + depth: 1, + }, + array_layer_count: 1, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Bgra8UnormSrgb, + usage: wgpu::TextureUsage::COPY_DST + | wgpu::TextureUsage::COPY_SRC + | wgpu::TextureUsage::SAMPLED, + }); + + encoder.copy_texture_to_texture( + wgpu::TextureCopyView { + texture: &self.atlas, + array_layer: 0, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, + }, + }, + wgpu::TextureCopyView { + texture: &new_atlas, + array_layer: 0, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, + }, + }, + wgpu::Extent3d { + width: old_atlas_size.width as u32, + height: old_atlas_size.height as u32, + depth: 1, + } + ); + + self.atlas = new_atlas; + } + + // TODO: Optimize! + // We currently rerasterize the SVG when its size changes. This is slow + // as heck. A GPU rasterizer like `pathfinder` may perform better. + // It would be cool to be able to smooth resize the `svg` example. let temp_buf = { let screen_size = resvg::ScreenSize::new(width, height).unwrap(); @@ -122,7 +214,7 @@ impl Cache { ); resvg::backend_raqote::render_to_canvas( - &tree, + tree, &resvg::Options::default(), screen_size, &mut canvas, @@ -146,48 +238,56 @@ impl Cache { image_height: height as u32, }, wgpu::TextureCopyView { - texture: &texture, + texture: &self.atlas, array_layer: 0, mip_level: 0, origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, + x: allocation.rectangle.min.x as f32, + y: allocation.rectangle.min.y as f32, z: 0.0, }, }, - extent, + wgpu::Extent3d { + width, + height, + depth: 1, + }, ); - let bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: texture_layout, - bindings: &[wgpu::Binding { - binding: 0, - resource: wgpu::BindingResource::TextureView( - &texture.create_default_view(), - ), - }], - }); - - let bind_group = Rc::new(bind_group); - - let _ = self - .rasterized - .insert((id, width, height), bind_group.clone()); - let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); + let _ = self + .rasterized + .insert((id, width, height), allocation); - Some(bind_group) + self.rasterized.get(&(id, width, height)) } - Svg::NotFound => None, + Svg::NotFound => None } } + pub fn atlas(&self, device: &wgpu::Device, texture_layout: &wgpu::BindGroupLayout) -> wgpu::BindGroup { + device.create_bind_group(&wgpu::BindGroupDescriptor { + layout: texture_layout, + bindings: &[wgpu::Binding { + binding: 0, + resource: wgpu::BindingResource::TextureView( + &self.atlas.create_default_view(), + ), + }], + }) + } + pub fn trim(&mut self) { let svg_hits = &self.svg_hits; let rasterized_hits = &self.rasterized_hits; + for (k, alloc) in &mut self.rasterized { + if !rasterized_hits.contains(&k) { + self.allocator.deallocate(alloc.id); + } + } + self.svgs.retain(|k, _| svg_hits.contains(k)); self.rasterized.retain(|k, _| rasterized_hits.contains(k)); self.svg_hits.clear(); -- cgit From 743637ebda8c3e4ba30f41e755ee1079ee66a86c Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Fri, 10 Jan 2020 16:07:09 +0100 Subject: Merged image and svg texture atlases into one owned by the image pipeline. --- wgpu/src/image/vector.rs | 78 ++++++++++-------------------------------------- 1 file changed, 15 insertions(+), 63 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 1a9352f2..89477877 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -4,6 +4,7 @@ use std::{ fmt, }; use guillotiere::{Allocation, AtlasAllocator, Size}; +use debug_stub_derive::*; pub enum Svg { Loaded { tree: resvg::usvg::Tree }, @@ -29,57 +30,22 @@ impl fmt::Debug for Svg { } } +#[derive(DebugStub)] pub struct Cache { - allocator: AtlasAllocator, - atlas: wgpu::Texture, svgs: HashMap, + #[debug_stub="ReplacementValue"] rasterized: HashMap<(u64, u32, u32), Allocation>, svg_hits: HashSet, rasterized_hits: HashSet<(u64, u32, u32)>, } -impl fmt::Debug for Cache { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.debug_struct("Vector Cache") - .field("allocator", &String::from("AtlasAllocator")) - .field("atlas", &self.atlas) - .field("svgs", &self.svgs) - .field("rasterized", &String::from("HashMap<(u64, u32, u32), Allocation>")) - .field("svg_hits", &self.svg_hits) - .field("rasterized_hits", &self.rasterized_hits) - .finish() - } -} - impl Cache { - pub fn new(device: &wgpu::Device) -> Self { - let (width, height) = (512, 512); - - let extent = wgpu::Extent3d { - width, - height, - depth: 1, - }; - - let atlas = device.create_texture(&wgpu::TextureDescriptor { - size: extent, - array_layer_count: 1, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Bgra8UnormSrgb, - usage: wgpu::TextureUsage::COPY_DST - | wgpu::TextureUsage::COPY_SRC - | wgpu::TextureUsage::SAMPLED, - }); - + pub fn new() -> Self { Self { svgs: HashMap::new(), rasterized: HashMap::new(), svg_hits: HashSet::new(), rasterized_hits: HashSet::new(), - allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)), - atlas, } } @@ -99,10 +65,6 @@ impl Cache { self.svgs.get(&handle.id()).unwrap() } - pub fn atlas_size(&self) -> guillotiere::Size { - self.allocator.size() - } - pub fn upload( &mut self, handle: &svg::Handle, @@ -110,6 +72,8 @@ impl Cache { scale: f32, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, + allocator: &mut AtlasAllocator, + atlas: &mut wgpu::Texture, ) -> Option<&Allocation> { let id = handle.id(); @@ -138,19 +102,19 @@ impl Cache { } let size = Size::new(width as i32, height as i32); - let old_atlas_size = self.allocator.size(); + let old_atlas_size = allocator.size(); let allocation; loop { - if let Some(a) = self.allocator.allocate(size) { + if let Some(a) = allocator.allocate(size) { allocation = a; break; } - self.allocator.grow(self.allocator.size() * 2); + allocator.grow(allocator.size() * 2); } - let new_atlas_size = self.allocator.size(); + let new_atlas_size = allocator.size(); if new_atlas_size != old_atlas_size { let new_atlas = device.create_texture(&wgpu::TextureDescriptor { @@ -171,7 +135,7 @@ impl Cache { encoder.copy_texture_to_texture( wgpu::TextureCopyView { - texture: &self.atlas, + texture: atlas, array_layer: 0, mip_level: 0, origin: wgpu::Origin3d { @@ -197,7 +161,7 @@ impl Cache { } ); - self.atlas = new_atlas; + *atlas = new_atlas; } // TODO: Optimize! @@ -238,7 +202,7 @@ impl Cache { image_height: height as u32, }, wgpu::TextureCopyView { - texture: &self.atlas, + texture: atlas, array_layer: 0, mip_level: 0, origin: wgpu::Origin3d { @@ -266,25 +230,13 @@ impl Cache { } } - pub fn atlas(&self, device: &wgpu::Device, texture_layout: &wgpu::BindGroupLayout) -> wgpu::BindGroup { - device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: texture_layout, - bindings: &[wgpu::Binding { - binding: 0, - resource: wgpu::BindingResource::TextureView( - &self.atlas.create_default_view(), - ), - }], - }) - } - - pub fn trim(&mut self) { + pub fn trim(&mut self, allocator: &mut AtlasAllocator) { let svg_hits = &self.svg_hits; let rasterized_hits = &self.rasterized_hits; for (k, alloc) in &mut self.rasterized { if !rasterized_hits.contains(&k) { - self.allocator.deallocate(alloc.id); + allocator.deallocate(alloc.id); } } -- cgit From 82e0675c071eee6ee71a989f4c8fb9a9fc18fcfe Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Sat, 11 Jan 2020 21:50:42 +0100 Subject: Some small debug changes --- wgpu/src/image/vector.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 89477877..2afe7d92 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,20 +1,23 @@ use iced_native::svg; use std::{ collections::{HashMap, HashSet}, - fmt, }; use guillotiere::{Allocation, AtlasAllocator, Size}; use debug_stub_derive::*; +#[derive(DebugStub)] pub enum Svg { - Loaded { tree: resvg::usvg::Tree }, + Loaded( + #[debug_stub="ReplacementValue"] + resvg::usvg::Tree + ), NotFound, } impl Svg { pub fn viewport_dimensions(&self) -> (u32, u32) { match self { - Svg::Loaded { tree } => { + Svg::Loaded(tree) => { let size = tree.svg_node().size; (size.width() as u32, size.height() as u32) @@ -24,12 +27,6 @@ impl Svg { } } -impl fmt::Debug for Svg { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "Svg") - } -} - #[derive(DebugStub)] pub struct Cache { svgs: HashMap, @@ -57,7 +54,7 @@ impl Cache { let opt = resvg::Options::default(); let svg = match resvg::usvg::Tree::from_file(handle.path(), &opt.usvg) { - Ok(tree) => Svg::Loaded { tree }, + Ok(tree) => Svg::Loaded(tree), Err(_) => Svg::NotFound, }; @@ -96,7 +93,7 @@ impl Cache { let _ = self.load(handle); match self.svgs.get(&handle.id()).unwrap() { - Svg::Loaded { tree } => { + Svg::Loaded(tree) => { if width == 0 || height == 0 { return None; } -- cgit From 2f77a6bf5ac1b657c1f54ea0b589b1e115b95e6b Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Mon, 13 Jan 2020 15:33:12 +0100 Subject: Use array of atlases instead of one growing indefinitely. --- wgpu/src/image/vector.rs | 149 ++++++++++------------------------------------- 1 file changed, 31 insertions(+), 118 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 2afe7d92..2972bc4a 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,8 +1,9 @@ +use crate::image::AtlasArray; use iced_native::svg; use std::{ collections::{HashMap, HashSet}, }; -use guillotiere::{Allocation, AtlasAllocator, Size}; +use guillotiere::{Allocation, Size}; use debug_stub_derive::*; #[derive(DebugStub)] @@ -31,7 +32,7 @@ impl Svg { pub struct Cache { svgs: HashMap, #[debug_stub="ReplacementValue"] - rasterized: HashMap<(u64, u32, u32), Allocation>, + rasterized: HashMap<(u64, u32, u32), (u32, Allocation)>, svg_hits: HashSet, rasterized_hits: HashSet<(u64, u32, u32)>, } @@ -69,9 +70,8 @@ impl Cache { scale: f32, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - allocator: &mut AtlasAllocator, - atlas: &mut wgpu::Texture, - ) -> Option<&Allocation> { + atlas_array: &mut AtlasArray, + ) -> Option<&(u32, Allocation)> { let id = handle.id(); let (width, height) = ( @@ -99,127 +99,40 @@ impl Cache { } let size = Size::new(width as i32, height as i32); - let old_atlas_size = allocator.size(); - let allocation; - loop { - if let Some(a) = allocator.allocate(size) { - allocation = a; - break; - } - - allocator.grow(allocator.size() * 2); - } - - let new_atlas_size = allocator.size(); - - if new_atlas_size != old_atlas_size { - let new_atlas = device.create_texture(&wgpu::TextureDescriptor { - size: wgpu::Extent3d { - width: new_atlas_size.width as u32, - height: new_atlas_size.height as u32, - depth: 1, - }, - array_layer_count: 1, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Bgra8UnormSrgb, - usage: wgpu::TextureUsage::COPY_DST - | wgpu::TextureUsage::COPY_SRC - | wgpu::TextureUsage::SAMPLED, - }); - - encoder.copy_texture_to_texture( - wgpu::TextureCopyView { - texture: atlas, - array_layer: 0, - mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, - }, - }, - wgpu::TextureCopyView { - texture: &new_atlas, - array_layer: 0, - mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, - }, - }, - wgpu::Extent3d { - width: old_atlas_size.width as u32, - height: old_atlas_size.height as u32, - depth: 1, - } - ); - - *atlas = new_atlas; - } + let (layer, allocation) = atlas_array.allocate(size).unwrap_or_else(|| { + atlas_array.grow(1, device, encoder); + atlas_array.allocate(size).unwrap() + }); // TODO: Optimize! // We currently rerasterize the SVG when its size changes. This is slow // as heck. A GPU rasterizer like `pathfinder` may perform better. // It would be cool to be able to smooth resize the `svg` example. - let temp_buf = { - let screen_size = - resvg::ScreenSize::new(width, height).unwrap(); - - let mut canvas = resvg::raqote::DrawTarget::new( - width as i32, - height as i32, - ); - - resvg::backend_raqote::render_to_canvas( - tree, - &resvg::Options::default(), - screen_size, - &mut canvas, - ); - - let slice = canvas.get_data(); - - device - .create_buffer_mapped( - slice.len(), - wgpu::BufferUsage::COPY_SRC, - ) - .fill_from_slice(slice) - }; - - encoder.copy_buffer_to_texture( - wgpu::BufferCopyView { - buffer: &temp_buf, - offset: 0, - row_pitch: 4 * width as u32, - image_height: height as u32, - }, - wgpu::TextureCopyView { - texture: atlas, - array_layer: 0, - mip_level: 0, - origin: wgpu::Origin3d { - x: allocation.rectangle.min.x as f32, - y: allocation.rectangle.min.y as f32, - z: 0.0, - }, - }, - wgpu::Extent3d { - width, - height, - depth: 1, - }, + let screen_size = + resvg::ScreenSize::new(width, height).unwrap(); + + let mut canvas = resvg::raqote::DrawTarget::new( + width as i32, + height as i32, ); + resvg::backend_raqote::render_to_canvas( + tree, + &resvg::Options::default(), + screen_size, + &mut canvas, + ); + + let slice = canvas.get_data(); + + atlas_array.upload(slice, layer, &allocation, device, encoder); + let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); let _ = self .rasterized - .insert((id, width, height), allocation); + .insert((id, width, height), (layer, allocation)); self.rasterized.get(&(id, width, height)) } @@ -227,13 +140,13 @@ impl Cache { } } - pub fn trim(&mut self, allocator: &mut AtlasAllocator) { + pub fn trim(&mut self, atlas_array: &mut AtlasArray) { let svg_hits = &self.svg_hits; let rasterized_hits = &self.rasterized_hits; - for (k, alloc) in &mut self.rasterized { - if !rasterized_hits.contains(&k) { - allocator.deallocate(alloc.id); + for (k, (layer, allocation)) in &self.rasterized { + if !rasterized_hits.contains(k) { + atlas_array.deallocate(*layer, allocation); } } -- cgit From 3f388351054c0961923b5f78e7dcf42289565a48 Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Thu, 16 Jan 2020 14:03:46 +0100 Subject: Implement allocating large images across multiple texture array layers. --- wgpu/src/image/vector.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 2972bc4a..a83d1a0a 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,9 +1,9 @@ -use crate::image::AtlasArray; +use crate::image::{TextureArray, ImageAllocation}; use iced_native::svg; use std::{ collections::{HashMap, HashSet}, }; -use guillotiere::{Allocation, Size}; +use guillotiere::Size; use debug_stub_derive::*; #[derive(DebugStub)] @@ -32,7 +32,7 @@ impl Svg { pub struct Cache { svgs: HashMap, #[debug_stub="ReplacementValue"] - rasterized: HashMap<(u64, u32, u32), (u32, Allocation)>, + rasterized: HashMap<(u64, u32, u32), ImageAllocation>, svg_hits: HashSet, rasterized_hits: HashSet<(u64, u32, u32)>, } @@ -70,8 +70,8 @@ impl Cache { scale: f32, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - atlas_array: &mut AtlasArray, - ) -> Option<&(u32, Allocation)> { + texture_array: &mut TextureArray, + ) -> Option<&ImageAllocation> { let id = handle.id(); let (width, height) = ( @@ -100,10 +100,7 @@ impl Cache { let size = Size::new(width as i32, height as i32); - let (layer, allocation) = atlas_array.allocate(size).unwrap_or_else(|| { - atlas_array.grow(1, device, encoder); - atlas_array.allocate(size).unwrap() - }); + let array_allocation = texture_array.allocate(size); // TODO: Optimize! // We currently rerasterize the SVG when its size changes. This is slow @@ -124,15 +121,13 @@ impl Cache { &mut canvas, ); - let slice = canvas.get_data(); - - atlas_array.upload(slice, layer, &allocation, device, encoder); + texture_array.upload(&canvas, &array_allocation, device, encoder); let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); let _ = self .rasterized - .insert((id, width, height), (layer, allocation)); + .insert((id, width, height), array_allocation); self.rasterized.get(&(id, width, height)) } @@ -140,13 +135,13 @@ impl Cache { } } - pub fn trim(&mut self, atlas_array: &mut AtlasArray) { + pub fn trim(&mut self, texture_array: &mut TextureArray) { let svg_hits = &self.svg_hits; let rasterized_hits = &self.rasterized_hits; - for (k, (layer, allocation)) in &self.rasterized { + for (k, allocation) in &self.rasterized { if !rasterized_hits.contains(k) { - atlas_array.deallocate(*layer, allocation); + texture_array.deallocate(allocation); } } -- cgit From c0996923c6aab39bb61ca6d3310149c66a73fac8 Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Fri, 17 Jan 2020 20:15:20 +0100 Subject: Batch image draw calls into one with multiple instances --- wgpu/src/image/vector.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index a83d1a0a..9bddcc2b 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -3,7 +3,6 @@ use iced_native::svg; use std::{ collections::{HashMap, HashSet}, }; -use guillotiere::Size; use debug_stub_derive::*; #[derive(DebugStub)] @@ -83,25 +82,19 @@ impl Cache { // We currently rerasterize the SVG when its size changes. This is slow // as heck. A GPU rasterizer like `pathfinder` may perform better. // It would be cool to be able to smooth resize the `svg` example. - if self.rasterized.get(&(id, width, height)).is_some() { + if self.rasterized.contains_key(&(id, width, height)) { let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); return self.rasterized.get(&(id, width, height)); } - let _ = self.load(handle); - - match self.svgs.get(&handle.id()).unwrap() { + match self.load(handle) { Svg::Loaded(tree) => { if width == 0 || height == 0 { return None; } - let size = Size::new(width as i32, height as i32); - - let array_allocation = texture_array.allocate(size); - // TODO: Optimize! // We currently rerasterize the SVG when its size changes. This is slow // as heck. A GPU rasterizer like `pathfinder` may perform better. @@ -121,13 +114,13 @@ impl Cache { &mut canvas, ); - texture_array.upload(&canvas, &array_allocation, device, encoder); + let allocation = texture_array.upload(&canvas, device, encoder); let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); let _ = self .rasterized - .insert((id, width, height), array_allocation); + .insert((id, width, height), allocation); self.rasterized.get(&(id, width, height)) } -- cgit From 2f695ef9803c9c08f64961f1b9902a661a385160 Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Fri, 17 Jan 2020 20:48:49 +0100 Subject: Updated shaders and removed debug_stub_derive dependency --- wgpu/src/image/vector.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 9bddcc2b..a8746566 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -3,14 +3,9 @@ use iced_native::svg; use std::{ collections::{HashMap, HashSet}, }; -use debug_stub_derive::*; -#[derive(DebugStub)] pub enum Svg { - Loaded( - #[debug_stub="ReplacementValue"] - resvg::usvg::Tree - ), + Loaded(resvg::usvg::Tree), NotFound, } @@ -27,10 +22,17 @@ impl Svg { } } -#[derive(DebugStub)] +impl std::fmt::Debug for Svg { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Svg::Loaded(_) => write!(f, "Svg::Loaded"), + Svg::NotFound => write!(f, "Svg::NotFound"), + } + } +} + pub struct Cache { svgs: HashMap, - #[debug_stub="ReplacementValue"] rasterized: HashMap<(u64, u32, u32), ImageAllocation>, svg_hits: HashSet, rasterized_hits: HashSet<(u64, u32, u32)>, @@ -144,3 +146,9 @@ impl Cache { self.rasterized_hits.clear(); } } + +impl std::fmt::Debug for Cache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "vector::Cache") + } +} \ No newline at end of file -- cgit From 4617da2818eb3ecc17b1da9571b7baa15056c026 Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Sun, 19 Jan 2020 18:06:46 +0100 Subject: Implemented automatic deallocation of texture space for dropped allocations --- wgpu/src/image/vector.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index a8746566..98588e5c 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -130,16 +130,10 @@ impl Cache { } } - pub fn trim(&mut self, texture_array: &mut TextureArray) { + pub fn trim(&mut self) { let svg_hits = &self.svg_hits; let rasterized_hits = &self.rasterized_hits; - for (k, allocation) in &self.rasterized { - if !rasterized_hits.contains(k) { - texture_array.deallocate(allocation); - } - } - self.svgs.retain(|k, _| svg_hits.contains(k)); self.rasterized.retain(|k, _| rasterized_hits.contains(k)); self.svg_hits.clear(); -- cgit From 59d45a5440aaa46c7dc8f3dc70c8518167c10418 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 12:34:34 +0100 Subject: Refactor texture atlas - Split into multiple modules - Rename some concepts - Change API details --- wgpu/src/image/vector.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 98588e5c..0dabc9ca 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,8 +1,6 @@ -use crate::image::{TextureArray, ImageAllocation}; +use crate::texture::atlas::{self, Atlas}; use iced_native::svg; -use std::{ - collections::{HashMap, HashSet}, -}; +use std::collections::{HashMap, HashSet}; pub enum Svg { Loaded(resvg::usvg::Tree), @@ -33,7 +31,7 @@ impl std::fmt::Debug for Svg { pub struct Cache { svgs: HashMap, - rasterized: HashMap<(u64, u32, u32), ImageAllocation>, + rasterized: HashMap<(u64, u32, u32), atlas::Entry>, svg_hits: HashSet, rasterized_hits: HashSet<(u64, u32, u32)>, } @@ -71,8 +69,8 @@ impl Cache { scale: f32, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - texture_array: &mut TextureArray, - ) -> Option<&ImageAllocation> { + texture_atlas: &mut Atlas, + ) -> Option<&atlas::Entry> { let id = handle.id(); let (width, height) = ( @@ -104,10 +102,8 @@ impl Cache { let screen_size = resvg::ScreenSize::new(width, height).unwrap(); - let mut canvas = resvg::raqote::DrawTarget::new( - width as i32, - height as i32, - ); + let mut canvas = + resvg::raqote::DrawTarget::new(width as i32, height as i32); resvg::backend_raqote::render_to_canvas( tree, @@ -116,17 +112,21 @@ impl Cache { &mut canvas, ); - let allocation = texture_array.upload(&canvas, device, encoder); + let allocation = texture_atlas.upload( + width, + height, + canvas.get_data(), + device, + encoder, + )?; let _ = self.svg_hits.insert(id); let _ = self.rasterized_hits.insert((id, width, height)); - let _ = self - .rasterized - .insert((id, width, height), allocation); + let _ = self.rasterized.insert((id, width, height), allocation); self.rasterized.get(&(id, width, height)) } - Svg::NotFound => None + Svg::NotFound => None, } } @@ -145,4 +145,4 @@ impl std::fmt::Debug for Cache { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "vector::Cache") } -} \ No newline at end of file +} -- cgit From d06d06e05096e0145be74fd02d67eada0a1665a1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 20:10:19 +0100 Subject: Deallocate atlas entries and remove padding --- wgpu/src/image/vector.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 0dabc9ca..e7eb4906 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -130,12 +130,20 @@ impl Cache { } } - pub fn trim(&mut self) { + pub fn trim(&mut self, atlas: &mut Atlas) { let svg_hits = &self.svg_hits; let rasterized_hits = &self.rasterized_hits; self.svgs.retain(|k, _| svg_hits.contains(k)); - self.rasterized.retain(|k, _| rasterized_hits.contains(k)); + self.rasterized.retain(|k, entry| { + let retain = rasterized_hits.contains(k); + + if !retain { + atlas.remove(entry); + } + + retain + }); self.svg_hits.clear(); self.rasterized_hits.clear(); } -- cgit From bb397cc66808dad14eace4fa40edf2e9bebf42bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 20:47:37 +0100 Subject: Move `Debug` implementation for `vector::Svg` --- wgpu/src/image/vector.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index e7eb4906..6b12df54 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -20,15 +20,7 @@ impl Svg { } } -impl std::fmt::Debug for Svg { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Svg::Loaded(_) => write!(f, "Svg::Loaded"), - Svg::NotFound => write!(f, "Svg::NotFound"), - } - } -} - +#[derive(Debug)] pub struct Cache { svgs: HashMap, rasterized: HashMap<(u64, u32, u32), atlas::Entry>, @@ -149,8 +141,11 @@ impl Cache { } } -impl std::fmt::Debug for Cache { +impl std::fmt::Debug for Svg { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "vector::Cache") + match self { + Svg::Loaded(_) => write!(f, "Svg::Loaded"), + Svg::NotFound => write!(f, "Svg::NotFound"), + } } } -- cgit From 4e7159c22c6be90f61aa715d5eb6811f805cb597 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 28 Feb 2020 14:38:42 +0100 Subject: Stop creating image pipeline when unnecessary --- wgpu/src/image/vector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/image/vector.rs') diff --git a/wgpu/src/image/vector.rs b/wgpu/src/image/vector.rs index 6b12df54..bae0f82f 100644 --- a/wgpu/src/image/vector.rs +++ b/wgpu/src/image/vector.rs @@ -1,4 +1,4 @@ -use crate::texture::atlas::{self, Atlas}; +use crate::image::atlas::{self, Atlas}; use iced_native::svg; use std::collections::{HashMap, HashSet}; -- cgit