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 +++++++++++++++++++++++++++++++---------------- 1 file changed, 187 insertions(+), 96 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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(); } -- 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 ++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 63 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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); } } } -- 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 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'wgpu/src/image/raster.rs') 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, } -- 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 ++++++++--------------------------------------- 1 file changed, 20 insertions(+), 109 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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); } } } -- 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 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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); } } } -- 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 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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); } -- 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 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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, -- 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 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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(); } -- 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 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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) { -- 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/raster.rs') 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/raster.rs') 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 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'wgpu/src/image/raster.rs') 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(); } -- 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/raster.rs') 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 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/raster.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/image/raster.rs') 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}; -- cgit