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/raster.rs | 283 +++++++++++++++++++++++++++++++---------------- wgpu/src/image/vector.rs | 198 +++++++++++++++++++++++++-------- 2 files changed, 336 insertions(+), 145 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index fa107879..0418bc0b 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -1,17 +1,13 @@ use iced_native::image; use std::{ collections::{HashMap, HashSet}, - rc::Rc, + fmt, }; +use guillotiere::{Allocation, AtlasAllocator, Size}; -#[derive(Debug)] pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), - Device { - bind_group: Rc, - width: u32, - height: u32, - }, + Device(Allocation), NotFound, Invalid, } @@ -20,108 +16,59 @@ impl Memory { pub fn dimensions(&self) -> (u32, u32) { match self { Memory::Host(image) => image.dimensions(), - Memory::Device { width, height, .. } => (*width, *height), + Memory::Device(allocation) => { + let size = &allocation.rectangle.size(); + (size.width as u32, size.height as u32) + }, Memory::NotFound => (1, 1), Memory::Invalid => (1, 1), } } - - pub fn upload( - &mut self, - device: &wgpu::Device, - encoder: &mut wgpu::CommandEncoder, - texture_layout: &wgpu::BindGroupLayout, - ) -> Option> { - match self { - Memory::Host(image) => { - let (width, height) = image.dimensions(); - - let extent = wgpu::Extent3d { - width, - height, - depth: 1, - }; - - 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, - }); - - let temp_buf = { - let flat_samples = image.as_flat_samples(); - let slice = flat_samples.as_slice(); - - 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: &texture, - array_layer: 0, - mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, - }, - }, - extent, - ); - - 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); - - *self = Memory::Device { - bind_group: bind_group.clone(), - width, - height, - }; - - Some(bind_group) - } - Memory::Device { bind_group, .. } => Some(bind_group.clone()), - Memory::NotFound => None, - Memory::Invalid => None, - } - } } -#[derive(Debug)] pub struct Cache { + allocator: AtlasAllocator, + atlas: wgpu::Texture, map: HashMap, hits: HashSet, } +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("map", &String::from("HashMap")) + .field("hits", &self.hits) + .finish() + } +} + impl Cache { - pub fn new() -> Self { + pub fn new(device: &wgpu::Device) -> Self { + let (width, height) = (1000, 1000); + + 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 { + allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)), + atlas, map: HashMap::new(), hits: HashSet::new(), } @@ -153,9 +100,153 @@ impl Cache { self.get(handle).unwrap() } + pub fn atlas_size(&self) -> guillotiere::Size { + self.allocator.size() + } + + pub fn upload( + &mut self, + handle: &image::Handle, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + ) -> &Memory { + let _ = self.load(handle); + + let memory = self.map.get_mut(&handle.id()).unwrap(); + + if let Memory::Host(image) = memory { + let (width, height) = image.dimensions(); + 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; + } + + 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; + } + + let extent = wgpu::Extent3d { + width, + height, + depth: 1, + }; + + let temp_buf = { + let flat_samples = image.as_flat_samples(); + let slice = flat_samples.as_slice(); + + 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, + image_height: height, + }, + wgpu::TextureCopyView { + texture: &self.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, + }, + }, + extent, + ); + + *memory = Memory::Device(allocation); + } + + memory + } + + 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 hits = &self.hits; + for (id, mem) in &mut self.map { + if let Memory::Device(allocation) = mem { + if !hits.contains(&id) { + self.allocator.deallocate(allocation.id); + } + } + } + self.map.retain(|k, _| hits.contains(k)); self.hits.clear(); } 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/raster.rs | 79 ++++++++++-------------------------------------- wgpu/src/image/vector.rs | 78 +++++++++-------------------------------------- 2 files changed, 31 insertions(+), 126 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 0418bc0b..651ec078 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -1,13 +1,14 @@ use iced_native::image; use std::{ collections::{HashMap, HashSet}, - fmt, }; use guillotiere::{Allocation, AtlasAllocator, Size}; +use debug_stub_derive::*; +#[derive(DebugStub)] pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), - Device(Allocation), + Device(#[debug_stub="ReplacementValue"]Allocation), NotFound, Invalid, } @@ -26,49 +27,15 @@ impl Memory { } } +#[derive(Debug)] pub struct Cache { - allocator: AtlasAllocator, - atlas: wgpu::Texture, map: HashMap, hits: HashSet, } -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("map", &String::from("HashMap")) - .field("hits", &self.hits) - .finish() - } -} - impl Cache { - pub fn new(device: &wgpu::Device) -> Self { - let (width, height) = (1000, 1000); - - 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 { - allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)), - atlas, map: HashMap::new(), hits: HashSet::new(), } @@ -100,15 +67,13 @@ impl Cache { self.get(handle).unwrap() } - pub fn atlas_size(&self) -> guillotiere::Size { - self.allocator.size() - } - pub fn upload( &mut self, handle: &image::Handle, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, + allocator: &mut AtlasAllocator, + atlas: &mut wgpu::Texture, ) -> &Memory { let _ = self.load(handle); @@ -118,19 +83,19 @@ impl Cache { let (width, height) = image.dimensions(); 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 { @@ -151,7 +116,7 @@ impl Cache { encoder.copy_texture_to_texture( wgpu::TextureCopyView { - texture: &self.atlas, + texture: atlas, array_layer: 0, mip_level: 0, origin: wgpu::Origin3d { @@ -177,7 +142,7 @@ impl Cache { } ); - self.atlas = new_atlas; + *atlas = new_atlas; } let extent = wgpu::Extent3d { @@ -206,7 +171,7 @@ impl Cache { image_height: height, }, wgpu::TextureCopyView { - texture: &self.atlas, + texture: atlas, array_layer: 0, mip_level: 0, origin: wgpu::Origin3d { @@ -224,25 +189,13 @@ impl Cache { memory } - 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 hits = &self.hits; for (id, mem) in &mut self.map { if let Memory::Device(allocation) = mem { if !hits.contains(&id) { - self.allocator.deallocate(allocation.id); + allocator.deallocate(allocation.id); } } } 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/raster.rs | 5 ++++- wgpu/src/image/vector.rs | 19 ++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 651ec078..33750cac 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -8,7 +8,10 @@ use debug_stub_derive::*; #[derive(DebugStub)] pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), - Device(#[debug_stub="ReplacementValue"]Allocation), + Device( + #[debug_stub="ReplacementValue"] + Allocation + ), NotFound, Invalid, } 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/raster.rs | 129 +++++++--------------------------------- wgpu/src/image/vector.rs | 149 ++++++++++------------------------------------- 2 files changed, 51 insertions(+), 227 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 33750cac..32afb749 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -1,17 +1,19 @@ +use crate::image::AtlasArray; use iced_native::image; use std::{ collections::{HashMap, HashSet}, }; -use guillotiere::{Allocation, AtlasAllocator, Size}; +use guillotiere::{Allocation, Size}; use debug_stub_derive::*; #[derive(DebugStub)] pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), - Device( + Device { + layer: u32, #[debug_stub="ReplacementValue"] - Allocation - ), + allocation: Allocation, + }, NotFound, Invalid, } @@ -20,7 +22,7 @@ impl Memory { pub fn dimensions(&self) -> (u32, u32) { match self { Memory::Host(image) => image.dimensions(), - Memory::Device(allocation) => { + Memory::Device { allocation, .. } => { let size = &allocation.rectangle.size(); (size.width as u32, size.height as u32) }, @@ -75,8 +77,7 @@ impl Cache { handle: &image::Handle, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - allocator: &mut AtlasAllocator, - atlas: &mut wgpu::Texture, + atlas_array: &mut AtlasArray, ) -> &Memory { let _ = self.load(handle); @@ -86,119 +87,29 @@ impl Cache { let (width, height) = image.dimensions(); let size = Size::new(width as i32, height as i32); - let old_atlas_size = allocator.size(); - let allocation; + let (layer, allocation) = atlas_array.allocate(size).unwrap_or_else(|| { + atlas_array.grow(1, device, encoder); + atlas_array.allocate(size).unwrap() + }); - loop { - if let Some(a) = allocator.allocate(size) { - allocation = a; - break; - } - - allocator.grow(allocator.size() * 2); - } + let flat_samples = image.as_flat_samples(); + let slice = flat_samples.as_slice(); - 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; - } + atlas_array.upload(slice, layer, &allocation, device, encoder); - let extent = wgpu::Extent3d { - width, - height, - depth: 1, - }; - - let temp_buf = { - let flat_samples = image.as_flat_samples(); - let slice = flat_samples.as_slice(); - - 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, - image_height: height, - }, - 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, - }, - }, - extent, - ); - - *memory = Memory::Device(allocation); + *memory = Memory::Device { layer, allocation }; } memory } - pub fn trim(&mut self, allocator: &mut AtlasAllocator) { + pub fn trim(&mut self, atlas_array: &mut AtlasArray) { let hits = &self.hits; - for (id, mem) in &mut self.map { - if let Memory::Device(allocation) = mem { + for (id, mem) in &self.map { + if let Memory::Device { layer, allocation } = mem { if !hits.contains(&id) { - allocator.deallocate(allocation.id); + atlas_array.deallocate(*layer, allocation); } } } 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/raster.rs | 35 +++++++++++------------------------ wgpu/src/image/vector.rs | 27 +++++++++++---------------- 2 files changed, 22 insertions(+), 40 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 32afb749..387bd23a 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -1,19 +1,15 @@ -use crate::image::AtlasArray; +use crate::image::{TextureArray, ImageAllocation}; use iced_native::image; use std::{ collections::{HashMap, HashSet}, }; -use guillotiere::{Allocation, Size}; +use guillotiere::Size; use debug_stub_derive::*; #[derive(DebugStub)] pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), - Device { - layer: u32, - #[debug_stub="ReplacementValue"] - allocation: Allocation, - }, + Device(ImageAllocation), NotFound, Invalid, } @@ -22,10 +18,7 @@ impl Memory { pub fn dimensions(&self) -> (u32, u32) { match self { Memory::Host(image) => image.dimensions(), - Memory::Device { allocation, .. } => { - let size = &allocation.rectangle.size(); - (size.width as u32, size.height as u32) - }, + Memory::Device(allocation) => allocation.size(), Memory::NotFound => (1, 1), Memory::Invalid => (1, 1), } @@ -77,7 +70,7 @@ impl Cache { handle: &image::Handle, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - atlas_array: &mut AtlasArray, + atlas_array: &mut TextureArray, ) -> &Memory { let _ = self.load(handle); @@ -87,29 +80,23 @@ impl Cache { let (width, height) = image.dimensions(); 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 allocation = atlas_array.allocate(size); - let flat_samples = image.as_flat_samples(); - let slice = flat_samples.as_slice(); + atlas_array.upload(image, &allocation, device, encoder); - atlas_array.upload(slice, layer, &allocation, device, encoder); - - *memory = Memory::Device { layer, allocation }; + *memory = Memory::Device(allocation); } memory } - pub fn trim(&mut self, atlas_array: &mut AtlasArray) { + pub fn trim(&mut self, texture_array: &mut TextureArray) { let hits = &self.hits; for (id, mem) in &self.map { - if let Memory::Device { layer, allocation } = mem { + if let Memory::Device(allocation) = mem { if !hits.contains(&id) { - atlas_array.deallocate(*layer, allocation); + texture_array.deallocate(allocation); } } } 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/raster.rs | 12 ++---------- wgpu/src/image/vector.rs | 15 ++++----------- 2 files changed, 6 insertions(+), 21 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 387bd23a..bca2ebda 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -3,7 +3,6 @@ use iced_native::image; use std::{ collections::{HashMap, HashSet}, }; -use guillotiere::Size; use debug_stub_derive::*; #[derive(DebugStub)] @@ -72,17 +71,10 @@ impl Cache { encoder: &mut wgpu::CommandEncoder, atlas_array: &mut TextureArray, ) -> &Memory { - let _ = self.load(handle); - - let memory = self.map.get_mut(&handle.id()).unwrap(); + let memory = self.load(handle); if let Memory::Host(image) = memory { - let (width, height) = image.dimensions(); - let size = Size::new(width as i32, height as i32); - - let allocation = atlas_array.allocate(size); - - atlas_array.upload(image, &allocation, device, encoder); + let allocation = atlas_array.upload(image, device, encoder); *memory = Memory::Device(allocation); } 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/raster.rs | 13 +++++++++++-- wgpu/src/image/vector.rs | 24 ++++++++++++++++-------- 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index bca2ebda..648df0ff 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -3,9 +3,7 @@ use iced_native::image; use std::{ collections::{HashMap, HashSet}, }; -use debug_stub_derive::*; -#[derive(DebugStub)] pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), Device(ImageAllocation), @@ -24,6 +22,17 @@ impl Memory { } } +impl std::fmt::Debug for Memory { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Memory::Host(_) => write!(f, "Memory::Host"), + Memory::Device(_) => write!(f, "Memory::Device"), + Memory::NotFound => write!(f, "Memory::NotFound"), + Memory::Invalid => write!(f, "Memory::Invalid"), + } + } +} + #[derive(Debug)] pub struct Cache { map: HashMap, 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/raster.rs | 10 +--------- wgpu/src/image/vector.rs | 8 +------- 2 files changed, 2 insertions(+), 16 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 648df0ff..884dd65a 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -91,17 +91,9 @@ impl Cache { memory } - pub fn trim(&mut self, texture_array: &mut TextureArray) { + pub fn trim(&mut self) { let hits = &self.hits; - for (id, mem) in &self.map { - if let Memory::Device(allocation) = mem { - if !hits.contains(&id) { - texture_array.deallocate(allocation); - } - } - } - self.map.retain(|k, _| hits.contains(k)); self.hits.clear(); } 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/raster.rs | 27 +++++++++++++++++---------- wgpu/src/image/vector.rs | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 27 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 884dd65a..071d53c8 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -1,12 +1,10 @@ -use crate::image::{TextureArray, ImageAllocation}; +use crate::texture::atlas::{self, Atlas}; use iced_native::image; -use std::{ - collections::{HashMap, HashSet}, -}; +use std::collections::{HashMap, HashSet}; pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), - Device(ImageAllocation), + Device(atlas::Entry), NotFound, Invalid, } @@ -15,7 +13,7 @@ impl Memory { pub fn dimensions(&self) -> (u32, u32) { match self { Memory::Host(image) => image.dimensions(), - Memory::Device(allocation) => allocation.size(), + Memory::Device(entry) => entry.size(), Memory::NotFound => (1, 1), Memory::Invalid => (1, 1), } @@ -78,17 +76,26 @@ impl Cache { handle: &image::Handle, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - atlas_array: &mut TextureArray, - ) -> &Memory { + atlas: &mut Atlas, + ) -> Option<&atlas::Entry> { let memory = self.load(handle); if let Memory::Host(image) = memory { - let allocation = atlas_array.upload(image, device, encoder); + let (width, height) = image.dimensions(); + + let allocation = + atlas.upload(width, height, &image, device, encoder)?; + + dbg!("Uploaded"); *memory = Memory::Device(allocation); } - memory + if let Memory::Device(allocation) = memory { + Some(allocation) + } else { + None + } } pub fn trim(&mut self) { 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 c58d94f3fda40f215254008ec105aeab56085b0e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 12:52:30 +0100 Subject: Avoid creating a vertex buffer every frame --- wgpu/src/image/raster.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 071d53c8..8d2f342e 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -86,8 +86,6 @@ impl Cache { let allocation = atlas.upload(width, height, &image, device, encoder)?; - dbg!("Uploaded"); - *memory = Memory::Device(allocation); } -- cgit From 48d70280eb4f5908f1c9339bebdfbab856d55ae1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 18:49:46 +0100 Subject: Fix multiple issues from the refactoring - Update texture view on grow - Fix atlas texture coordinates - Fix fragmented uploads --- wgpu/src/image/raster.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 8d2f342e..b19da582 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -83,10 +83,9 @@ impl Cache { if let Memory::Host(image) = memory { let (width, height) = image.dimensions(); - let allocation = - atlas.upload(width, height, &image, device, encoder)?; + let entry = atlas.upload(width, height, &image, device, encoder)?; - *memory = Memory::Device(allocation); + *memory = Memory::Device(entry); } if let Memory::Device(allocation) = memory { -- 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/raster.rs | 15 +++++++++++++-- wgpu/src/image/vector.rs | 12 ++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index b19da582..cae8e065 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -95,10 +95,21 @@ impl Cache { } } - pub fn trim(&mut self) { + pub fn trim(&mut self, atlas: &mut Atlas) { let hits = &self.hits; - self.map.retain(|k, _| hits.contains(k)); + self.map.retain(|k, memory| { + let retain = hits.contains(k); + + if !retain { + if let Memory::Device(entry) = memory { + atlas.remove(entry); + } + } + + retain + }); + self.hits.clear(); } 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 271725faa585ba14207a9bb27ab95fe27473279d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 20:47:27 +0100 Subject: Derive `Debug` for `raster::Memory` --- wgpu/src/image/raster.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index cae8e065..883c32f7 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -2,6 +2,7 @@ use crate::texture::atlas::{self, Atlas}; use iced_native::image; use std::collections::{HashMap, HashSet}; +#[derive(Debug)] pub enum Memory { Host(::image::ImageBuffer<::image::Bgra, Vec>), Device(atlas::Entry), @@ -20,17 +21,6 @@ impl Memory { } } -impl std::fmt::Debug for Memory { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Memory::Host(_) => write!(f, "Memory::Host"), - Memory::Device(_) => write!(f, "Memory::Device"), - Memory::NotFound => write!(f, "Memory::NotFound"), - Memory::Invalid => write!(f, "Memory::Invalid"), - } - } -} - #[derive(Debug)] pub struct Cache { map: HashMap, -- 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') 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/atlas.rs | 361 +++++++++++++++++++++++++++++++++++++ wgpu/src/image/atlas/allocation.rs | 35 ++++ wgpu/src/image/atlas/allocator.rs | 69 +++++++ wgpu/src/image/atlas/entry.rs | 26 +++ wgpu/src/image/atlas/layer.rs | 17 ++ wgpu/src/image/raster.rs | 2 +- wgpu/src/image/vector.rs | 2 +- 7 files changed, 510 insertions(+), 2 deletions(-) create mode 100644 wgpu/src/image/atlas.rs create mode 100644 wgpu/src/image/atlas/allocation.rs create mode 100644 wgpu/src/image/atlas/allocator.rs create mode 100644 wgpu/src/image/atlas/entry.rs create mode 100644 wgpu/src/image/atlas/layer.rs (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/atlas.rs b/wgpu/src/image/atlas.rs new file mode 100644 index 00000000..86a5ff49 --- /dev/null +++ b/wgpu/src/image/atlas.rs @@ -0,0 +1,361 @@ +pub mod entry; + +mod allocation; +mod allocator; +mod layer; + +pub use allocation::Allocation; +pub use entry::Entry; +pub use layer::Layer; + +use allocator::Allocator; + +pub const SIZE: u32 = 2048; + +#[derive(Debug)] +pub struct Atlas { + texture: wgpu::Texture, + texture_view: wgpu::TextureView, + layers: Vec, +} + +impl Atlas { + pub fn new(device: &wgpu::Device) -> Self { + let extent = wgpu::Extent3d { + width: SIZE, + height: SIZE, + depth: 1, + }; + + let texture = device.create_texture(&wgpu::TextureDescriptor { + size: extent, + array_layer_count: 2, + 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, + }); + + let texture_view = texture.create_default_view(); + + Atlas { + texture, + texture_view, + layers: vec![Layer::Empty, Layer::Empty], + } + } + + pub fn view(&self) -> &wgpu::TextureView { + &self.texture_view + } + + pub fn layer_count(&self) -> usize { + self.layers.len() + } + + pub fn upload( + &mut self, + width: u32, + height: u32, + data: &[C], + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + ) -> Option + where + C: Copy + 'static, + { + let entry = { + let current_size = self.layers.len(); + let entry = self.allocate(width, height)?; + + // We grow the internal texture after allocating if necessary + let new_layers = self.layers.len() - current_size; + self.grow(new_layers, device, encoder); + + entry + }; + + log::info!("Allocated atlas entry: {:?}", entry); + + let buffer = device + .create_buffer_mapped(data.len(), wgpu::BufferUsage::COPY_SRC) + .fill_from_slice(data); + + match &entry { + Entry::Contiguous(allocation) => { + self.upload_allocation( + &buffer, + width, + height, + 0, + &allocation, + encoder, + ); + } + Entry::Fragmented { fragments, .. } => { + for fragment in fragments { + let (x, y) = fragment.position; + let offset = (y * width + x) as usize * 4; + + self.upload_allocation( + &buffer, + width, + height, + offset, + &fragment.allocation, + encoder, + ); + } + } + } + + log::info!("Current atlas: {:?}", self); + + Some(entry) + } + + pub fn remove(&mut self, entry: &Entry) { + log::info!("Removing atlas entry: {:?}", entry); + + match entry { + Entry::Contiguous(allocation) => { + self.deallocate(allocation); + } + Entry::Fragmented { fragments, .. } => { + for fragment in fragments { + self.deallocate(&fragment.allocation); + } + } + } + } + + fn allocate(&mut self, width: u32, height: u32) -> Option { + // Allocate one layer if texture fits perfectly + if width == SIZE && height == SIZE { + let mut empty_layers = self + .layers + .iter_mut() + .enumerate() + .filter(|(_, layer)| layer.is_empty()); + + if let Some((i, layer)) = empty_layers.next() { + *layer = Layer::Full; + + return Some(Entry::Contiguous(Allocation::Full { layer: i })); + } + + self.layers.push(Layer::Full); + + return Some(Entry::Contiguous(Allocation::Full { + layer: self.layers.len() - 1, + })); + } + + // Split big textures across multiple layers + if width > SIZE || height > SIZE { + let mut fragments = Vec::new(); + let mut y = 0; + + while y < height { + let height = std::cmp::min(height - y, SIZE); + let mut x = 0; + + while x < width { + let width = std::cmp::min(width - x, SIZE); + + let allocation = self.allocate(width, height)?; + + if let Entry::Contiguous(allocation) = allocation { + fragments.push(entry::Fragment { + position: (x, y), + allocation, + }); + } + + x += width; + } + + y += height; + } + + return Some(Entry::Fragmented { + size: (width, height), + fragments, + }); + } + + // Try allocating on an existing layer + for (i, layer) in self.layers.iter_mut().enumerate() { + match layer { + Layer::Empty => { + let mut allocator = Allocator::new(SIZE); + + if let Some(region) = allocator.allocate(width, height) { + *layer = Layer::Busy(allocator); + + return Some(Entry::Contiguous(Allocation::Partial { + region, + layer: i, + })); + } + } + Layer::Busy(allocator) => { + if let Some(region) = allocator.allocate(width, height) { + return Some(Entry::Contiguous(Allocation::Partial { + region, + layer: i, + })); + } + } + _ => {} + } + } + + // Create new layer with atlas allocator + let mut allocator = Allocator::new(SIZE); + + if let Some(region) = allocator.allocate(width, height) { + self.layers.push(Layer::Busy(allocator)); + + return Some(Entry::Contiguous(Allocation::Partial { + region, + layer: self.layers.len() - 1, + })); + } + + // We ran out of memory (?) + None + } + + fn deallocate(&mut self, allocation: &Allocation) { + log::info!("Deallocating atlas: {:?}", allocation); + + match allocation { + Allocation::Full { layer } => { + self.layers[*layer] = Layer::Empty; + } + Allocation::Partial { layer, region } => { + let layer = &mut self.layers[*layer]; + + if let Layer::Busy(allocator) = layer { + allocator.deallocate(region); + + if allocator.is_empty() { + *layer = Layer::Empty; + } + } + } + } + } + + fn upload_allocation( + &mut self, + buffer: &wgpu::Buffer, + image_width: u32, + image_height: u32, + offset: usize, + allocation: &Allocation, + encoder: &mut wgpu::CommandEncoder, + ) { + let (x, y) = allocation.position(); + let (width, height) = allocation.size(); + let layer = allocation.layer(); + + let extent = wgpu::Extent3d { + width, + height, + depth: 1, + }; + + encoder.copy_buffer_to_texture( + wgpu::BufferCopyView { + buffer, + offset: offset as u64, + row_pitch: 4 * image_width, + image_height, + }, + wgpu::TextureCopyView { + texture: &self.texture, + array_layer: layer as u32, + mip_level: 0, + origin: wgpu::Origin3d { + x: x as f32, + y: y as f32, + z: 0.0, + }, + }, + extent, + ); + } + + fn grow( + &mut self, + amount: usize, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + ) { + if amount == 0 { + return; + } + + let new_texture = device.create_texture(&wgpu::TextureDescriptor { + size: wgpu::Extent3d { + width: SIZE, + height: SIZE, + depth: 1, + }, + array_layer_count: self.layers.len() as u32, + 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, + }); + + let amount_to_copy = self.layers.len() - amount; + + for (i, layer) in + self.layers.iter_mut().take(amount_to_copy).enumerate() + { + if layer.is_empty() { + continue; + } + + encoder.copy_texture_to_texture( + wgpu::TextureCopyView { + texture: &self.texture, + array_layer: i as u32, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, + }, + }, + wgpu::TextureCopyView { + texture: &new_texture, + array_layer: i as u32, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, + }, + }, + wgpu::Extent3d { + width: SIZE, + height: SIZE, + depth: 1, + }, + ); + } + + self.texture = new_texture; + self.texture_view = self.texture.create_default_view(); + } +} diff --git a/wgpu/src/image/atlas/allocation.rs b/wgpu/src/image/atlas/allocation.rs new file mode 100644 index 00000000..59b7239f --- /dev/null +++ b/wgpu/src/image/atlas/allocation.rs @@ -0,0 +1,35 @@ +use crate::image::atlas::{self, allocator}; + +#[derive(Debug)] +pub enum Allocation { + Partial { + layer: usize, + region: allocator::Region, + }, + Full { + layer: usize, + }, +} + +impl Allocation { + pub fn position(&self) -> (u32, u32) { + match self { + Allocation::Partial { region, .. } => region.position(), + Allocation::Full { .. } => (0, 0), + } + } + + pub fn size(&self) -> (u32, u32) { + match self { + Allocation::Partial { region, .. } => region.size(), + Allocation::Full { .. } => (atlas::SIZE, atlas::SIZE), + } + } + + pub fn layer(&self) -> usize { + match self { + Allocation::Partial { layer, .. } => *layer, + Allocation::Full { layer } => *layer, + } + } +} diff --git a/wgpu/src/image/atlas/allocator.rs b/wgpu/src/image/atlas/allocator.rs new file mode 100644 index 00000000..7a4ff5b1 --- /dev/null +++ b/wgpu/src/image/atlas/allocator.rs @@ -0,0 +1,69 @@ +use guillotiere::{AtlasAllocator, Size}; + +pub struct Allocator { + raw: AtlasAllocator, + allocations: usize, +} + +impl Allocator { + pub fn new(size: u32) -> Allocator { + let raw = AtlasAllocator::new(Size::new(size as i32, size as i32)); + + Allocator { + raw, + allocations: 0, + } + } + + pub fn allocate(&mut self, width: u32, height: u32) -> Option { + let allocation = + self.raw.allocate(Size::new(width as i32, height as i32))?; + + self.allocations += 1; + + Some(Region { allocation }) + } + + pub fn deallocate(&mut self, region: &Region) { + self.raw.deallocate(region.allocation.id); + + self.allocations = self.allocations.saturating_sub(1); + } + + pub fn is_empty(&self) -> bool { + self.allocations == 0 + } +} + +pub struct Region { + allocation: guillotiere::Allocation, +} + +impl Region { + pub fn position(&self) -> (u32, u32) { + let rectangle = &self.allocation.rectangle; + + (rectangle.min.x as u32, rectangle.min.y as u32) + } + + pub fn size(&self) -> (u32, u32) { + let size = self.allocation.rectangle.size(); + + (size.width as u32, size.height as u32) + } +} + +impl std::fmt::Debug for Allocator { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Allocator") + } +} + +impl std::fmt::Debug for Region { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Region") + .field("id", &self.allocation.id) + .field("rectangle", &self.allocation.rectangle) + .finish() + } +} diff --git a/wgpu/src/image/atlas/entry.rs b/wgpu/src/image/atlas/entry.rs new file mode 100644 index 00000000..0310fc54 --- /dev/null +++ b/wgpu/src/image/atlas/entry.rs @@ -0,0 +1,26 @@ +use crate::image::atlas; + +#[derive(Debug)] +pub enum Entry { + Contiguous(atlas::Allocation), + Fragmented { + size: (u32, u32), + fragments: Vec, + }, +} + +impl Entry { + #[cfg(feature = "image")] + pub fn size(&self) -> (u32, u32) { + match self { + Entry::Contiguous(allocation) => allocation.size(), + Entry::Fragmented { size, .. } => *size, + } + } +} + +#[derive(Debug)] +pub struct Fragment { + pub position: (u32, u32), + pub allocation: atlas::Allocation, +} diff --git a/wgpu/src/image/atlas/layer.rs b/wgpu/src/image/atlas/layer.rs new file mode 100644 index 00000000..b1084ed9 --- /dev/null +++ b/wgpu/src/image/atlas/layer.rs @@ -0,0 +1,17 @@ +use crate::image::atlas::Allocator; + +#[derive(Debug)] +pub enum Layer { + Empty, + Busy(Allocator), + Full, +} + +impl Layer { + pub fn is_empty(&self) -> bool { + match self { + Layer::Empty => true, + _ => false, + } + } +} diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 883c32f7..3edec57e 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -1,4 +1,4 @@ -use crate::texture::atlas::{self, Atlas}; +use crate::image::atlas::{self, Atlas}; use iced_native::image; use std::collections::{HashMap, HashSet}; 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 From 18410154289fa3262403bb2c9de3dd741fd7dda2 Mon Sep 17 00:00:00 2001 From: Soham Chowdhury Date: Sat, 29 Feb 2020 07:32:42 +0530 Subject: Add support for loading already-decoded image pixels --- wgpu/src/image/raster.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'wgpu/src/image') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 3edec57e..4f69df8c 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -55,6 +55,21 @@ impl Cache { Memory::Invalid } } + image::Data::Pixels { + width, + height, + pixels, + } => { + if let Some(image) = ::image::ImageBuffer::from_vec( + *width, + *height, + pixels.to_vec(), + ) { + Memory::Host(image) + } else { + Memory::Invalid + } + } }; self.insert(handle, memory); -- cgit