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.rs | 177 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 112 insertions(+), 65 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index f7ed67c3..65780886 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -3,13 +3,16 @@ mod raster; #[cfg(feature = "svg")] mod vector; +#[cfg(feature = "image")] +use crate::image::raster::Memory; + use crate::Transformation; use iced_native::{image, svg, Rectangle}; use std::mem; #[cfg(any(feature = "image", feature = "svg"))] -use std::cell::RefCell; +use std::{cell::RefCell, collections::HashMap}; #[derive(Debug)] pub struct Pipeline { @@ -174,6 +177,16 @@ impl Pipeline { format: wgpu::VertexFormat::Float2, offset: 4 * 2, }, + wgpu::VertexAttributeDescriptor { + shader_location: 3, + format: wgpu::VertexFormat::Float2, + offset: 4 * 4, + }, + wgpu::VertexAttributeDescriptor { + shader_location: 4, + format: wgpu::VertexFormat::Float2, + offset: 4 * 6, + }, ], }, ], @@ -197,9 +210,10 @@ impl Pipeline { Pipeline { #[cfg(feature = "image")] - raster_cache: RefCell::new(raster::Cache::new()), + raster_cache: RefCell::new(raster::Cache::new(&device)), + #[cfg(feature = "svg")] - vector_cache: RefCell::new(vector::Cache::new()), + vector_cache: RefCell::new(vector::Cache::new(&device)), pipeline, uniforms: uniforms_buffer, @@ -251,50 +265,72 @@ impl Pipeline { std::mem::size_of::() as u64, ); - // TODO: Batch draw calls using a texture atlas - // Guillotière[1] by @nical can help us a lot here. - // - // [1]: https://github.com/nical/guillotiere - for image in instances { - let uploaded_texture = match &image.handle { + #[cfg(any(feature = "image", feature = "svg"))] + let mut recs = HashMap::new(); + + for (index, image) in instances.iter().enumerate() { + match &image.handle { Handle::Raster(_handle) => { #[cfg(feature = "image")] { - let mut cache = self.raster_cache.borrow_mut(); - let memory = cache.load(&_handle); + let mut raster_cache = self.raster_cache.borrow_mut(); - memory.upload(device, encoder, &self.texture_layout) - } + if let Memory::Device(allocation) = raster_cache.upload( + _handle, + device, + encoder) + { + let rec = allocation.rectangle; - #[cfg(not(feature = "image"))] - None + let _ = recs.insert(index, rec); + } + } } Handle::Vector(_handle) => { #[cfg(feature = "svg")] { - let mut cache = self.vector_cache.borrow_mut(); + let mut vector_cache = self.vector_cache.borrow_mut(); - cache.upload( + if let Some(allocation) = vector_cache.upload( _handle, image.scale, _scale, device, encoder, - &self.texture_layout, - ) - } + ) { + let rec = allocation.rectangle; - #[cfg(not(feature = "svg"))] - None + let _ = recs.insert(index, rec); + } + } } - }; + } + } + + #[cfg(feature = "image")] + let raster_atlas = self.raster_cache.borrow().atlas(device, &self.texture_layout); + + #[cfg(feature = "svg")] + let vector_atlas = self.vector_cache.borrow().atlas(device, &self.texture_layout); + + #[cfg(any(feature = "image", feature = "svg"))] + for (index, image) in instances.iter().enumerate() { + if let Some(rec) = recs.get(&index) { + let atlas_size = match image.handle { + #[cfg(feature = "image")] + Handle::Raster(_) => self.raster_cache.borrow().atlas_size(), + #[cfg(feature = "svg")] + Handle::Vector(_) => self.vector_cache.borrow().atlas_size(), + _ => guillotiere::Size::new(0, 0) + }; - if let Some(texture) = uploaded_texture { let instance_buffer = device .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) .fill_from_slice(&[Instance { _position: image.position, _scale: image.scale, + _position_in_atlas: [rec.min.x as f32 / atlas_size.width as f32, rec.min.y as f32 / atlas_size.height as f32], + _scale_in_atlas: [rec.size().width as f32 / atlas_size.width as f32, rec.size().height as f32 / atlas_size.height as f32] }]); encoder.copy_buffer_to_buffer( @@ -305,48 +341,57 @@ impl Pipeline { mem::size_of::() as u64, ); - { - let mut render_pass = encoder.begin_render_pass( - &wgpu::RenderPassDescriptor { - color_attachments: &[ - wgpu::RenderPassColorAttachmentDescriptor { - attachment: target, - resolve_target: None, - load_op: wgpu::LoadOp::Load, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 0.0, - }, + let texture = match &image.handle { + #[cfg(feature = "image")] + Handle::Raster(_) => &raster_atlas, + #[cfg(feature = "svg")] + Handle::Vector(_) => &vector_atlas, + #[cfg(feature = "image")] + _ => &raster_atlas, + #[cfg(feature = "svg")] + _ => &vector_atlas, + }; + + let mut render_pass = encoder.begin_render_pass( + &wgpu::RenderPassDescriptor { + color_attachments: &[ + wgpu::RenderPassColorAttachmentDescriptor { + attachment: target, + resolve_target: None, + load_op: wgpu::LoadOp::Load, + store_op: wgpu::StoreOp::Store, + clear_color: wgpu::Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.0, }, - ], - depth_stencil_attachment: None, - }, - ); - - render_pass.set_pipeline(&self.pipeline); - render_pass.set_bind_group(0, &self.constants, &[]); - render_pass.set_bind_group(1, &texture, &[]); - render_pass.set_index_buffer(&self.indices, 0); - render_pass.set_vertex_buffers( - 0, - &[(&self.vertices, 0), (&self.instances, 0)], - ); - render_pass.set_scissor_rect( - bounds.x, - bounds.y, - bounds.width, - bounds.height, - ); - - render_pass.draw_indexed( - 0..QUAD_INDICES.len() as u32, - 0, - 0..1 as u32, - ); - } + }, + ], + depth_stencil_attachment: None, + }, + ); + + render_pass.set_pipeline(&self.pipeline); + render_pass.set_bind_group(0, &self.constants, &[]); + render_pass.set_bind_group(1, &texture, &[]); + render_pass.set_index_buffer(&self.indices, 0); + render_pass.set_vertex_buffers( + 0, + &[(&self.vertices, 0), (&self.instances, 0)], + ); + render_pass.set_scissor_rect( + bounds.x, + bounds.y, + bounds.width, + bounds.height, + ); + + render_pass.draw_indexed( + 0..QUAD_INDICES.len() as u32, + 0, + 0..1 as u32, + ); } } } @@ -399,6 +444,8 @@ const QUAD_VERTS: [Vertex; 4] = [ struct Instance { _position: [f32; 2], _scale: [f32; 2], + _position_in_atlas: [f32; 2], + _scale_in_atlas: [f32; 2], } #[repr(C)] -- 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.rs | 92 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 33 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 65780886..aaecd492 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -14,7 +14,10 @@ use std::mem; #[cfg(any(feature = "image", feature = "svg"))] use std::{cell::RefCell, collections::HashMap}; -#[derive(Debug)] +use guillotiere::{AtlasAllocator, Size}; +use debug_stub_derive::*; + +#[derive(DebugStub)] pub struct Pipeline { #[cfg(feature = "image")] raster_cache: RefCell, @@ -28,6 +31,9 @@ pub struct Pipeline { instances: wgpu::Buffer, constants: wgpu::BindGroup, texture_layout: wgpu::BindGroupLayout, + #[debug_stub="ReplacementValue"] + allocator: AtlasAllocator, + atlas: wgpu::Texture, } impl Pipeline { @@ -208,12 +214,32 @@ impl Pipeline { usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, }); + 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, + }); + Pipeline { #[cfg(feature = "image")] - raster_cache: RefCell::new(raster::Cache::new(&device)), + raster_cache: RefCell::new(raster::Cache::new()), #[cfg(feature = "svg")] - vector_cache: RefCell::new(vector::Cache::new(&device)), + vector_cache: RefCell::new(vector::Cache::new()), pipeline, uniforms: uniforms_buffer, @@ -222,6 +248,8 @@ impl Pipeline { instances, constants: constant_bind_group, texture_layout, + allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)), + atlas, } } @@ -276,10 +304,12 @@ impl Pipeline { let mut raster_cache = self.raster_cache.borrow_mut(); if let Memory::Device(allocation) = raster_cache.upload( - _handle, + handle, device, - encoder) - { + encoder, + &mut self.allocator, + &mut self.atlas + ) { let rec = allocation.rectangle; let _ = recs.insert(index, rec); @@ -291,12 +321,15 @@ impl Pipeline { { let mut vector_cache = self.vector_cache.borrow_mut(); + // Upload rasterized svg to texture atlas if let Some(allocation) = vector_cache.upload( _handle, image.scale, _scale, device, encoder, + &mut self.allocator, + &mut self.atlas, ) { let rec = allocation.rectangle; @@ -307,30 +340,34 @@ impl Pipeline { } } - #[cfg(feature = "image")] - let raster_atlas = self.raster_cache.borrow().atlas(device, &self.texture_layout); - - #[cfg(feature = "svg")] - let vector_atlas = self.vector_cache.borrow().atlas(device, &self.texture_layout); + let atlas_width = self.allocator.size().width as f32; + let atlas_height = self.allocator.size().height as f32; + + let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { + layout: &self.texture_layout, + bindings: &[wgpu::Binding { + binding: 0, + resource: wgpu::BindingResource::TextureView( + &self.atlas.create_default_view(), + ), + }], + }); #[cfg(any(feature = "image", feature = "svg"))] for (index, image) in instances.iter().enumerate() { if let Some(rec) = recs.get(&index) { - let atlas_size = match image.handle { - #[cfg(feature = "image")] - Handle::Raster(_) => self.raster_cache.borrow().atlas_size(), - #[cfg(feature = "svg")] - Handle::Vector(_) => self.vector_cache.borrow().atlas_size(), - _ => guillotiere::Size::new(0, 0) - }; + let x = rec.min.x as f32 / atlas_width; + let y = rec.min.y as f32 / atlas_height; + let w = (rec.size().width - 1) as f32 / atlas_width; + let h = (rec.size().height - 1) as f32 / atlas_height; let instance_buffer = device .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) .fill_from_slice(&[Instance { _position: image.position, _scale: image.scale, - _position_in_atlas: [rec.min.x as f32 / atlas_size.width as f32, rec.min.y as f32 / atlas_size.height as f32], - _scale_in_atlas: [rec.size().width as f32 / atlas_size.width as f32, rec.size().height as f32 / atlas_size.height as f32] + _position_in_atlas: [x, y], + _scale_in_atlas: [w, h] }]); encoder.copy_buffer_to_buffer( @@ -341,17 +378,6 @@ impl Pipeline { mem::size_of::() as u64, ); - let texture = match &image.handle { - #[cfg(feature = "image")] - Handle::Raster(_) => &raster_atlas, - #[cfg(feature = "svg")] - Handle::Vector(_) => &vector_atlas, - #[cfg(feature = "image")] - _ => &raster_atlas, - #[cfg(feature = "svg")] - _ => &vector_atlas, - }; - let mut render_pass = encoder.begin_render_pass( &wgpu::RenderPassDescriptor { color_attachments: &[ @@ -398,10 +424,10 @@ impl Pipeline { pub fn trim_cache(&mut self) { #[cfg(feature = "image")] - self.raster_cache.borrow_mut().trim(); + self.raster_cache.borrow_mut().trim(&mut self.allocator); #[cfg(feature = "svg")] - self.vector_cache.borrow_mut().trim(); + self.vector_cache.borrow_mut().trim(&mut self.allocator); } } -- cgit From 8562a4c986ff48d478be794c8c4268047a9a57d7 Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Sun, 12 Jan 2020 02:37:46 +0100 Subject: Fixed texture bleeding --- wgpu/src/image.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index aaecd492..59257399 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -356,10 +356,10 @@ impl Pipeline { #[cfg(any(feature = "image", feature = "svg"))] for (index, image) in instances.iter().enumerate() { if let Some(rec) = recs.get(&index) { - let x = rec.min.x as f32 / atlas_width; - let y = rec.min.y as f32 / atlas_height; - let w = (rec.size().width - 1) as f32 / atlas_width; - let h = (rec.size().height - 1) as f32 / atlas_height; + let x = (rec.min.x as f32 + 0.5) / atlas_width; + let y = (rec.min.y as f32 + 0.5) / atlas_height; + let w = (rec.size().width as f32 - 0.5) / atlas_width; + let h = (rec.size().height as f32 - 0.5) / atlas_height; let instance_buffer = device .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) -- 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.rs | 252 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 203 insertions(+), 49 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 59257399..bda24280 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -9,15 +9,15 @@ use crate::image::raster::Memory; use crate::Transformation; use iced_native::{image, svg, Rectangle}; -use std::mem; +use std::{collections::{HashMap, HashSet}, mem}; #[cfg(any(feature = "image", feature = "svg"))] -use std::{cell::RefCell, collections::HashMap}; +use std::cell::RefCell; -use guillotiere::{AtlasAllocator, Size}; +use guillotiere::{Allocation, AtlasAllocator, Size}; use debug_stub_derive::*; -#[derive(DebugStub)] +#[derive(Debug)] pub struct Pipeline { #[cfg(feature = "image")] raster_cache: RefCell, @@ -31,9 +31,7 @@ pub struct Pipeline { instances: wgpu::Buffer, constants: wgpu::BindGroup, texture_layout: wgpu::BindGroupLayout, - #[debug_stub="ReplacementValue"] - allocator: AtlasAllocator, - atlas: wgpu::Texture, + atlas_array: AtlasArray, } impl Pipeline { @@ -193,6 +191,11 @@ impl Pipeline { format: wgpu::VertexFormat::Float2, offset: 4 * 6, }, + wgpu::VertexAttributeDescriptor { + shader_location: 5, + format: wgpu::VertexFormat::Float, + offset: 4 * 8, + }, ], }, ], @@ -214,25 +217,7 @@ impl Pipeline { usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, }); - 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, - }); + let atlas_array = AtlasArray::new(1, device); Pipeline { #[cfg(feature = "image")] @@ -248,8 +233,7 @@ impl Pipeline { instances, constants: constant_bind_group, texture_layout, - allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)), - atlas, + atlas_array, } } @@ -303,14 +287,13 @@ impl Pipeline { { let mut raster_cache = self.raster_cache.borrow_mut(); - if let Memory::Device(allocation) = raster_cache.upload( - handle, + if let Memory::Device { layer, allocation } = raster_cache.upload( + _handle, device, encoder, - &mut self.allocator, - &mut self.atlas + &mut self.atlas_array, ) { - let rec = allocation.rectangle; + let rec = (*layer, allocation.rectangle); let _ = recs.insert(index, rec); } @@ -322,16 +305,15 @@ impl Pipeline { let mut vector_cache = self.vector_cache.borrow_mut(); // Upload rasterized svg to texture atlas - if let Some(allocation) = vector_cache.upload( + if let Some((layer, allocation)) = vector_cache.upload( _handle, image.scale, _scale, device, encoder, - &mut self.allocator, - &mut self.atlas, + &mut self.atlas_array, ) { - let rec = allocation.rectangle; + let rec = (*layer, allocation.rectangle); let _ = recs.insert(index, rec); } @@ -340,26 +322,23 @@ impl Pipeline { } } - let atlas_width = self.allocator.size().width as f32; - let atlas_height = self.allocator.size().height as f32; - let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &self.texture_layout, bindings: &[wgpu::Binding { binding: 0, resource: wgpu::BindingResource::TextureView( - &self.atlas.create_default_view(), + &self.atlas_array.texture().create_default_view(), ), }], }); #[cfg(any(feature = "image", feature = "svg"))] for (index, image) in instances.iter().enumerate() { - if let Some(rec) = recs.get(&index) { - let x = (rec.min.x as f32 + 0.5) / atlas_width; - let y = (rec.min.y as f32 + 0.5) / atlas_height; - let w = (rec.size().width as f32 - 0.5) / atlas_width; - let h = (rec.size().height as f32 - 0.5) / atlas_height; + if let Some((layer, rec)) = recs.get(&index) { + let x = (rec.min.x as f32 + 0.5) / (ATLAS_SIZE as f32); + let y = (rec.min.y as f32 + 0.5) / (ATLAS_SIZE as f32); + let w = (rec.size().width as f32 - 0.5) / (ATLAS_SIZE as f32); + let h = (rec.size().height as f32 - 0.5) / (ATLAS_SIZE as f32); let instance_buffer = device .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) @@ -367,7 +346,8 @@ impl Pipeline { _position: image.position, _scale: image.scale, _position_in_atlas: [x, y], - _scale_in_atlas: [w, h] + _scale_in_atlas: [w, h], + _layer: *layer as f32, }]); encoder.copy_buffer_to_buffer( @@ -424,10 +404,10 @@ impl Pipeline { pub fn trim_cache(&mut self) { #[cfg(feature = "image")] - self.raster_cache.borrow_mut().trim(&mut self.allocator); + self.raster_cache.borrow_mut().trim(&mut self.atlas_array); #[cfg(feature = "svg")] - self.vector_cache.borrow_mut().trim(&mut self.allocator); + self.vector_cache.borrow_mut().trim(&mut self.atlas_array); } } @@ -442,6 +422,177 @@ pub enum Handle { Vector(svg::Handle), } +#[derive(DebugStub)] +pub struct AtlasArray { + texture: wgpu::Texture, + #[debug_stub="ReplacementValue"] + allocators: HashMap, + layers_without_allocators: HashSet, + size: u32, +} + +impl AtlasArray { + pub fn new(array_size: u32, device: &wgpu::Device) -> Self { + let (width, height) = (ATLAS_SIZE, ATLAS_SIZE); + + let extent = wgpu::Extent3d { + width, + height, + depth: 1, + }; + + let texture = device.create_texture(&wgpu::TextureDescriptor { + size: extent, + array_layer_count: array_size, + 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, + }); + + AtlasArray { + texture, + allocators: HashMap::new(), + layers_without_allocators: HashSet::new(), + size: array_size, + } + } + + pub fn texture(&self) -> &wgpu::Texture { + &self.texture + } + + pub fn allocate(&mut self, size: Size) -> Option<(u32, Allocation)> { + for layer in 0..self.size { + if self.layers_without_allocators.contains(&layer) { + continue; + } + + let allocator = self.allocators.entry(layer) + .or_insert_with(|| AtlasAllocator::new( + Size::new(ATLAS_SIZE as i32, ATLAS_SIZE as i32) + )); + + if let Some(a) = allocator.allocate(size.clone()) { + return Some((layer, a)); + } + } + + None + } + + pub fn deallocate(&mut self, layer: u32, allocation: &Allocation) { + if let Some(allocator) = self.allocators.get_mut(&layer) { + allocator.deallocate(allocation.id); + } + } + + pub fn upload( + &mut self, + data: &[T], + layer: u32, + allocation: &guillotiere::Allocation, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + ) { + let size = allocation.rectangle.size(); + let (width, height) = (size.width as u32, size.height as u32); + + let extent = wgpu::Extent3d { + width, + height, + depth: 1, + }; + + let temp_buf = device + .create_buffer_mapped( + data.len(), + wgpu::BufferUsage::COPY_SRC, + ) + .fill_from_slice(data); + + encoder.copy_buffer_to_texture( + wgpu::BufferCopyView { + buffer: &temp_buf, + offset: 0, + row_pitch: 4 * width, + image_height: height, + }, + wgpu::TextureCopyView { + texture: &self.texture, + array_layer: layer as u32, + mip_level: 0, + origin: wgpu::Origin3d { + x: allocation.rectangle.min.x as f32, + y: allocation.rectangle.min.y as f32, + z: 0.0, + }, + }, + extent, + ); + } + + pub fn grow( + &mut self, + grow_by: u32, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + ) { + let old_atlas_array_size = self.size; + + let new_texture = device.create_texture(&wgpu::TextureDescriptor { + size: wgpu::Extent3d { + width: ATLAS_SIZE, + height: ATLAS_SIZE, + depth: 1, + }, + array_layer_count: old_atlas_array_size + grow_by, + 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, + }); + + for i in 0..old_atlas_array_size { + encoder.copy_texture_to_texture( + wgpu::TextureCopyView { + texture: &self.texture, + array_layer: i, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, + }, + }, + wgpu::TextureCopyView { + texture: &new_texture, + array_layer: i, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, + }, + }, + wgpu::Extent3d { + width: ATLAS_SIZE, + height: ATLAS_SIZE, + depth: 1, + } + ); + } + + self.texture = new_texture; + } +} + #[repr(C)] #[derive(Clone, Copy)] pub struct Vertex { @@ -465,6 +616,8 @@ const QUAD_VERTS: [Vertex; 4] = [ }, ]; +const ATLAS_SIZE: u32 = 8192; + #[repr(C)] #[derive(Clone, Copy)] struct Instance { @@ -472,6 +625,7 @@ struct Instance { _scale: [f32; 2], _position_in_atlas: [f32; 2], _scale_in_atlas: [f32; 2], + _layer: f32, } #[repr(C)] -- 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.rs | 685 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 517 insertions(+), 168 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index bda24280..f8faa543 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -9,7 +9,7 @@ use crate::image::raster::Memory; use crate::Transformation; use iced_native::{image, svg, Rectangle}; -use std::{collections::{HashMap, HashSet}, mem}; +use std::mem; #[cfg(any(feature = "image", feature = "svg"))] use std::cell::RefCell; @@ -31,7 +31,7 @@ pub struct Pipeline { instances: wgpu::Buffer, constants: wgpu::BindGroup, texture_layout: wgpu::BindGroupLayout, - atlas_array: AtlasArray, + texture_array: TextureArray, } impl Pipeline { @@ -217,7 +217,7 @@ impl Pipeline { usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, }); - let atlas_array = AtlasArray::new(1, device); + let texture_array = TextureArray::new(device); Pipeline { #[cfg(feature = "image")] @@ -233,7 +233,7 @@ impl Pipeline { instances, constants: constant_bind_group, texture_layout, - atlas_array, + texture_array, } } @@ -259,8 +259,8 @@ impl Pipeline { encoder: &mut wgpu::CommandEncoder, instances: &[Image], transformation: Transformation, - bounds: Rectangle, - target: &wgpu::TextureView, + _bounds: Rectangle, + _target: &wgpu::TextureView, _scale: f32, ) { let uniforms_buffer = device @@ -277,25 +277,27 @@ impl Pipeline { std::mem::size_of::() as u64, ); - #[cfg(any(feature = "image", feature = "svg"))] - let mut recs = HashMap::new(); - - for (index, image) in instances.iter().enumerate() { + for image in instances { match &image.handle { Handle::Raster(_handle) => { #[cfg(feature = "image")] { let mut raster_cache = self.raster_cache.borrow_mut(); - if let Memory::Device { layer, allocation } = raster_cache.upload( + if let Memory::Device(allocation) = raster_cache.upload( _handle, device, encoder, - &mut self.atlas_array, + &mut self.texture_array, ) { - let rec = (*layer, allocation.rectangle); - - let _ = recs.insert(index, rec); + self.draw_image( + device, + encoder, + image, + allocation, + _bounds, + _target, + ); } } } @@ -305,109 +307,173 @@ impl Pipeline { let mut vector_cache = self.vector_cache.borrow_mut(); // Upload rasterized svg to texture atlas - if let Some((layer, allocation)) = vector_cache.upload( + if let Some(allocation) = vector_cache.upload( _handle, image.scale, _scale, device, encoder, - &mut self.atlas_array, + &mut self.texture_array, ) { - let rec = (*layer, allocation.rectangle); - - let _ = recs.insert(index, rec); + self.draw_image( + device, + encoder, + image, + allocation, + _bounds, + _target, + ); } } } } } + } + + pub fn trim_cache(&mut self) { + #[cfg(feature = "image")] + self.raster_cache.borrow_mut().trim(&mut self.texture_array); + + #[cfg(feature = "svg")] + self.vector_cache.borrow_mut().trim(&mut self.texture_array); + } + fn draw_image( + &self, + device: &mut wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + image: &Image, + allocation: &ImageAllocation, + bounds: Rectangle, + target: &wgpu::TextureView, + ) { let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &self.texture_layout, bindings: &[wgpu::Binding { binding: 0, resource: wgpu::BindingResource::TextureView( - &self.atlas_array.texture().create_default_view(), + &self.texture_array.texture.create_default_view(), ), }], }); - #[cfg(any(feature = "image", feature = "svg"))] - for (index, image) in instances.iter().enumerate() { - if let Some((layer, rec)) = recs.get(&index) { - let x = (rec.min.x as f32 + 0.5) / (ATLAS_SIZE as f32); - let y = (rec.min.y as f32 + 0.5) / (ATLAS_SIZE as f32); - let w = (rec.size().width as f32 - 0.5) / (ATLAS_SIZE as f32); - let h = (rec.size().height as f32 - 0.5) / (ATLAS_SIZE as f32); - - let instance_buffer = device - .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) - .fill_from_slice(&[Instance { - _position: image.position, - _scale: image.scale, - _position_in_atlas: [x, y], - _scale_in_atlas: [w, h], - _layer: *layer as f32, - }]); - - encoder.copy_buffer_to_buffer( - &instance_buffer, - 0, - &self.instances, - 0, - mem::size_of::() as u64, - ); - - let mut render_pass = encoder.begin_render_pass( - &wgpu::RenderPassDescriptor { - color_attachments: &[ - wgpu::RenderPassColorAttachmentDescriptor { - attachment: target, - resolve_target: None, - load_op: wgpu::LoadOp::Load, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 0.0, - }, - }, - ], - depth_stencil_attachment: None, - }, - ); - - render_pass.set_pipeline(&self.pipeline); - render_pass.set_bind_group(0, &self.constants, &[]); - render_pass.set_bind_group(1, &texture, &[]); - render_pass.set_index_buffer(&self.indices, 0); - render_pass.set_vertex_buffers( - 0, - &[(&self.vertices, 0), (&self.instances, 0)], - ); - render_pass.set_scissor_rect( - bounds.x, - bounds.y, - bounds.width, - bounds.height, - ); - - render_pass.draw_indexed( - 0..QUAD_INDICES.len() as u32, - 0, - 0..1 as u32, - ); + match allocation { + ImageAllocation::SingleAllocation(allocation) => { + self.draw_allocation( + device, + encoder, + image.position, + image.scale, + allocation, + &texture, + bounds, + target, + ) + } + ImageAllocation::MultipleAllocations { mappings, size } => { + let scaling_x = image.scale[0] / size.0 as f32; + let scaling_y = image.scale[1] / size.1 as f32; + + for mapping in mappings { + let mut position = image.position; + let mut scale = image.scale; + + position[0] += mapping.src_pos.0 as f32 * scaling_x; + position[1] += mapping.src_pos.1 as f32 * scaling_y; + scale[0] = mapping.allocation.size().0 as f32 * scaling_x; + scale[1] = mapping.allocation.size().1 as f32 * scaling_y; + + self.draw_allocation( + device, + encoder, + position, + scale, + &mapping.allocation, + &texture, + bounds, + target, + ) + } } + _ => {} } } - pub fn trim_cache(&mut self) { - #[cfg(feature = "image")] - self.raster_cache.borrow_mut().trim(&mut self.atlas_array); + fn draw_allocation( + &self, + device: &mut wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + position: [f32; 2], + scale: [f32; 2], + allocation: &ArrayAllocation, + texture: &wgpu::BindGroup, + bounds: Rectangle, + target: &wgpu::TextureView, + ) { + let x = (allocation.position().0 as f32 + 0.5) / (ATLAS_SIZE as f32); + let y = (allocation.position().1 as f32 + 0.5) / (ATLAS_SIZE as f32); + let w = (allocation.size().0 as f32 - 0.5) / (ATLAS_SIZE as f32); + let h = (allocation.size().1 as f32 - 0.5) / (ATLAS_SIZE as f32); + let layer = allocation.layer() as f32; - #[cfg(feature = "svg")] - self.vector_cache.borrow_mut().trim(&mut self.atlas_array); + let instance_buffer = device + .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) + .fill_from_slice(&[Instance { + _position: position, + _scale: scale, + _position_in_atlas: [x, y], + _scale_in_atlas: [w, h], + _layer: layer, + }]); + + encoder.copy_buffer_to_buffer( + &instance_buffer, + 0, + &self.instances, + 0, + mem::size_of::() as u64, + ); + + let mut render_pass = encoder.begin_render_pass( + &wgpu::RenderPassDescriptor { + color_attachments: &[ + wgpu::RenderPassColorAttachmentDescriptor { + attachment: target, + resolve_target: None, + load_op: wgpu::LoadOp::Load, + store_op: wgpu::StoreOp::Store, + clear_color: wgpu::Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.0, + }, + }, + ], + depth_stencil_attachment: None, + }, + ); + + render_pass.set_pipeline(&self.pipeline); + render_pass.set_bind_group(0, &self.constants, &[]); + render_pass.set_bind_group(1, &texture, &[]); + render_pass.set_index_buffer(&self.indices, 0); + render_pass.set_vertex_buffers( + 0, + &[(&self.vertices, 0), (&self.instances, 0)], + ); + render_pass.set_scissor_rect( + bounds.x, + bounds.y, + bounds.width, + bounds.height, + ); + + render_pass.draw_indexed( + 0..QUAD_INDICES.len() as u32, + 0, + 0..1 as u32, + ); } } @@ -422,17 +488,96 @@ pub enum Handle { Vector(svg::Handle), } +#[derive(Debug)] +pub struct ArrayAllocationMapping { + src_pos: (u32, u32), + allocation: ArrayAllocation, +} + +#[derive(Debug)] +pub enum ImageAllocation { + SingleAllocation(ArrayAllocation), + MultipleAllocations { + mappings: Vec, + size: (u32, u32), + }, + Error, +} + +impl ImageAllocation { + pub fn size(&self) -> (u32, u32) { + match self { + ImageAllocation::SingleAllocation(allocation) => { + allocation.size() + } + ImageAllocation::MultipleAllocations { size, .. } => { + *size + } + _ => (0, 0) + } + } +} + +#[derive(DebugStub)] +pub enum ArrayAllocation { + AtlasAllocation { + layer: usize, + #[debug_stub = "ReplacementValue"] + allocation: Allocation, + }, + WholeLayer { + layer: usize, + } +} + +impl ArrayAllocation { + pub fn size(&self) -> (u32, u32) { + match self { + ArrayAllocation::AtlasAllocation { allocation, .. } => { + let size = allocation.rectangle.size(); + (size.width as u32, size.height as u32) + } + ArrayAllocation::WholeLayer { .. } => (ATLAS_SIZE, ATLAS_SIZE) + } + } + + pub fn position(&self) -> (u32, u32) { + match self { + ArrayAllocation::AtlasAllocation { allocation, .. } => { + let min = &allocation.rectangle.min; + (min.x as u32, min.y as u32) + } + ArrayAllocation::WholeLayer { .. } => (0, 0) + } + } + + pub fn layer(&self) -> usize { + match self { + ArrayAllocation::AtlasAllocation { layer, .. } => *layer, + ArrayAllocation::WholeLayer { layer } => *layer, + } + } +} + #[derive(DebugStub)] -pub struct AtlasArray { +pub enum TextureLayer { + Whole, + Atlas( + #[debug_stub="ReplacementValue"] + AtlasAllocator + ), + Empty, +} + +#[derive(Debug)] +pub struct TextureArray { texture: wgpu::Texture, - #[debug_stub="ReplacementValue"] - allocators: HashMap, - layers_without_allocators: HashSet, - size: u32, + texture_array_size: u32, + layers: Vec, } -impl AtlasArray { - pub fn new(array_size: u32, device: &wgpu::Device) -> Self { +impl TextureArray { + pub fn new(device: &wgpu::Device) -> Self { let (width, height) = (ATLAS_SIZE, ATLAS_SIZE); let extent = wgpu::Extent3d { @@ -443,7 +588,7 @@ impl AtlasArray { let texture = device.create_texture(&wgpu::TextureDescriptor { size: extent, - array_layer_count: array_size, + array_layer_count: 1, mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, @@ -453,53 +598,217 @@ impl AtlasArray { | wgpu::TextureUsage::SAMPLED, }); - AtlasArray { + let size = Size::new(ATLAS_SIZE as i32, ATLAS_SIZE as i32); + + TextureArray { texture, - allocators: HashMap::new(), - layers_without_allocators: HashSet::new(), - size: array_size, + texture_array_size: 1, + layers: vec!(TextureLayer::Atlas(AtlasAllocator::new(size))), } } - pub fn texture(&self) -> &wgpu::Texture { - &self.texture - } + pub fn allocate(&mut self, size: Size) -> ImageAllocation { + // Allocate one layer if allocation fits perfectly + if size.width == ATLAS_SIZE as i32 && size.height == ATLAS_SIZE as i32 { + for (i, layer) in &mut self.layers.iter_mut().enumerate() { + if let TextureLayer::Empty = layer + { + *layer = TextureLayer::Whole; + return ImageAllocation::SingleAllocation( + ArrayAllocation::WholeLayer { layer: i } + ); + } + } + + self.layers.push(TextureLayer::Whole); + return ImageAllocation::SingleAllocation( + ArrayAllocation::WholeLayer { layer: self.layers.len() - 1 } + ); + } - pub fn allocate(&mut self, size: Size) -> Option<(u32, Allocation)> { - for layer in 0..self.size { - if self.layers_without_allocators.contains(&layer) { - continue; + // Split big allocations across multiple layers + if size.width > ATLAS_SIZE as i32 || size.height > ATLAS_SIZE as i32 { + let mut mappings = Vec::new(); + + let mut y = 0; + while y < size.height { + let height = std::cmp::min(size.height - y, ATLAS_SIZE as i32); + let mut x = 0; + + while x < size.width { + let width = std::cmp::min(size.width - x, ATLAS_SIZE as i32); + if let ImageAllocation::SingleAllocation(allocation) = self.allocate(Size::new(width, height)) { + let src_pos = (x as u32, y as u32); + mappings.push(ArrayAllocationMapping { src_pos, allocation }); + } + + x += width; + } + y += height; } - let allocator = self.allocators.entry(layer) - .or_insert_with(|| AtlasAllocator::new( - Size::new(ATLAS_SIZE as i32, ATLAS_SIZE as i32) - )); + return ImageAllocation::MultipleAllocations { + mappings, + size: (size.width as u32, size.height as u32), + }; + } - if let Some(a) = allocator.allocate(size.clone()) { - return Some((layer, a)); + // Try allocating on an existing layer + for (i, layer) in self.layers.iter_mut().enumerate() { + if let TextureLayer::Atlas(allocator) = layer { + if let Some(allocation) = allocator.allocate(size.clone()) { + let array_allocation = ArrayAllocation::AtlasAllocation { layer: i, allocation }; + return ImageAllocation::SingleAllocation(array_allocation); + } } } - None + // Create new layer with atlas allocator + let mut allocator = AtlasAllocator::new(Size::new(ATLAS_SIZE as i32, ATLAS_SIZE as i32)); + if let Some(allocation) = allocator.allocate(size) { + self.layers.push(TextureLayer::Atlas(allocator)); + + return ImageAllocation::SingleAllocation( + ArrayAllocation::AtlasAllocation { + layer: self.layers.len() - 1, + allocation, + } + ); + } + + // One of the above should have worked + ImageAllocation::Error } - pub fn deallocate(&mut self, layer: u32, allocation: &Allocation) { - if let Some(allocator) = self.allocators.get_mut(&layer) { - allocator.deallocate(allocation.id); + pub fn deallocate(&mut self, allocation: &ImageAllocation) { + match allocation { + ImageAllocation::SingleAllocation(allocation) => { + if let Some(layer) = self.layers.get_mut(allocation.layer()) { + match allocation { + ArrayAllocation::WholeLayer { .. } => { + *layer = TextureLayer::Empty; + } + ArrayAllocation::AtlasAllocation { allocation, .. } => { + if let TextureLayer::Atlas(allocator) = layer { + allocator.deallocate(allocation.id); + } + } + } + } + } + ImageAllocation::MultipleAllocations { mappings, .. } => { + for mapping in mappings { + if let Some(layer) = self.layers.get_mut(mapping.allocation.layer()) { + match &mapping.allocation { + ArrayAllocation::WholeLayer { .. } => { + *layer = TextureLayer::Empty; + } + ArrayAllocation::AtlasAllocation { allocation, .. } => { + if let TextureLayer::Atlas(allocator) = layer { + allocator.deallocate(allocation.id); + } + } + } + } + } + } + _ => {} } + } - pub fn upload( + fn upload( &mut self, - data: &[T], - layer: u32, - allocation: &guillotiere::Allocation, + image: &I, + allocation: &ImageAllocation, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, + ) + where + I: RawImageData, + C: Copy + 'static, + { + match allocation { + ImageAllocation::SingleAllocation(allocation) => { + let data = image.data(); + let buffer = device + .create_buffer_mapped( + data.len(), + wgpu::BufferUsage::COPY_SRC, + ) + .fill_from_slice(data); + + if allocation.layer() >= self.texture_array_size as usize { + self.grow(1, device, encoder); + } + + self.upload_texture( + &buffer, + allocation, + encoder, + ); + } + ImageAllocation::MultipleAllocations { mappings, .. } => { + let chunks_per_pixel = 4 / std::mem::size_of::(); + let chunks_per_line = chunks_per_pixel * image.width() as usize; + + for mapping in mappings { + let sub_width = mapping.allocation.size().0 as usize; + let sub_height = mapping.allocation.size().1 as usize; + let sub_line_start = mapping.src_pos.0 as usize * chunks_per_pixel; + let sub_line_end = (mapping.src_pos.0 as usize + sub_width) * chunks_per_pixel; + + let mut sub_lines = image + .data() + .chunks(chunks_per_line) + .skip(mapping.src_pos.1 as usize) + .take(sub_height) + .map(|line| &line[sub_line_start..sub_line_end]); + + let buffer = device + .create_buffer_mapped( + chunks_per_pixel * sub_width * sub_height, + wgpu::BufferUsage::COPY_SRC, + ); + + let mut buffer_lines = buffer.data.chunks_mut(sub_width * chunks_per_pixel); + + while let (Some(buffer_line), Some(sub_line)) = (buffer_lines.next(), sub_lines.next()) { + buffer_line.copy_from_slice(sub_line); + } + + let highest_layer = mappings + .iter() + .map(|m| m.allocation.layer() as u32) + .max() + .unwrap_or(0); + + if highest_layer >= self.texture_array_size { + let grow_by = 1 + highest_layer - self.texture_array_size; + self.grow(grow_by, device, encoder); + } + + self.upload_texture( + &buffer.finish(), + &mapping.allocation, + encoder, + ); + } + } + _ => {} + } + + } + + fn upload_texture( + &mut self, + buffer: &wgpu::Buffer, + allocation: &ArrayAllocation, + encoder: &mut wgpu::CommandEncoder, ) { - let size = allocation.rectangle.size(); - let (width, height) = (size.width as u32, size.height as u32); + let array_layer = allocation.layer() as u32; + + let (width, height) = allocation.size(); let extent = wgpu::Extent3d { width, @@ -507,27 +816,22 @@ impl AtlasArray { depth: 1, }; - let temp_buf = device - .create_buffer_mapped( - data.len(), - wgpu::BufferUsage::COPY_SRC, - ) - .fill_from_slice(data); + let (x, y) = allocation.position(); encoder.copy_buffer_to_texture( wgpu::BufferCopyView { - buffer: &temp_buf, + buffer, offset: 0, row_pitch: 4 * width, image_height: height, }, wgpu::TextureCopyView { texture: &self.texture, - array_layer: layer as u32, + array_layer, mip_level: 0, origin: wgpu::Origin3d { - x: allocation.rectangle.min.x as f32, - y: allocation.rectangle.min.y as f32, + x: x as f32, + y: y as f32, z: 0.0, }, }, @@ -535,13 +839,17 @@ impl AtlasArray { ); } - pub fn grow( + fn grow( &mut self, grow_by: u32, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, ) { - let old_atlas_array_size = self.size; + if grow_by == 0 { + return; + } + + let old_texture_array_size = self.texture_array_size; let new_texture = device.create_texture(&wgpu::TextureDescriptor { size: wgpu::Extent3d { @@ -549,7 +857,7 @@ impl AtlasArray { height: ATLAS_SIZE, depth: 1, }, - array_layer_count: old_atlas_array_size + grow_by, + array_layer_count: old_texture_array_size + grow_by, mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, @@ -559,40 +867,81 @@ impl AtlasArray { | wgpu::TextureUsage::SAMPLED, }); - for i in 0..old_atlas_array_size { - encoder.copy_texture_to_texture( - wgpu::TextureCopyView { - texture: &self.texture, - array_layer: i, - mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, - }, + encoder.copy_texture_to_texture( + wgpu::TextureCopyView { + texture: &self.texture, + array_layer: 0, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, }, - wgpu::TextureCopyView { - texture: &new_texture, - array_layer: i, - mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, - }, + }, + wgpu::TextureCopyView { + texture: &new_texture, + array_layer: 0, + mip_level: 0, + origin: wgpu::Origin3d { + x: 0.0, + y: 0.0, + z: 0.0, }, - wgpu::Extent3d { - width: ATLAS_SIZE, - height: ATLAS_SIZE, - depth: 1, - } - ); - } + }, + wgpu::Extent3d { + width: ATLAS_SIZE, + height: ATLAS_SIZE, + depth: self.texture_array_size, + } + ); + self.texture_array_size += grow_by; self.texture = new_texture; } } +trait RawImageData { + type Chunk; + + fn data(&self) -> &[Self::Chunk]; + fn width(&self) -> u32; + fn height(&self) -> u32; +} + +#[cfg(feature = "image")] +impl RawImageData for ::image::ImageBuffer<::image::Bgra, Vec> { + type Chunk = u8; + + fn data(&self) -> &[Self::Chunk] { + &self + } + + fn width(&self) -> u32 { + self.dimensions().0 + } + + fn height(&self) -> u32 { + self.dimensions().1 + } +} + +#[cfg(feature = "svg")] +impl RawImageData for resvg::raqote::DrawTarget { + type Chunk = u32; + + fn data(&self) -> &[Self::Chunk] { + self.get_data() + } + + fn width(&self) -> u32 { + self.width() as u32 + } + + fn height(&self) -> u32 { + self.height() as u32 + } +} + #[repr(C)] #[derive(Clone, Copy)] pub struct Vertex { @@ -616,7 +965,7 @@ const QUAD_VERTS: [Vertex; 4] = [ }, ]; -const ATLAS_SIZE: u32 = 8192; +const ATLAS_SIZE: u32 = 4096; #[repr(C)] #[derive(Clone, Copy)] -- 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.rs | 336 +++++++++++++++++++++++++----------------------------- 1 file changed, 154 insertions(+), 182 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index f8faa543..9443b876 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -28,7 +28,6 @@ pub struct Pipeline { uniforms: wgpu::Buffer, vertices: wgpu::Buffer, indices: wgpu::Buffer, - instances: wgpu::Buffer, constants: wgpu::BindGroup, texture_layout: wgpu::BindGroupLayout, texture_array: TextureArray, @@ -212,11 +211,6 @@ impl Pipeline { .create_buffer_mapped(QUAD_INDICES.len(), wgpu::BufferUsage::INDEX) .fill_from_slice(&QUAD_INDICES); - let instances = device.create_buffer(&wgpu::BufferDescriptor { - size: mem::size_of::() as u64, - usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, - }); - let texture_array = TextureArray::new(device); Pipeline { @@ -230,7 +224,6 @@ impl Pipeline { uniforms: uniforms_buffer, vertices, indices, - instances, constants: constant_bind_group, texture_layout, texture_array, @@ -257,10 +250,10 @@ impl Pipeline { &mut self, device: &mut wgpu::Device, encoder: &mut wgpu::CommandEncoder, - instances: &[Image], + images: &[Image], transformation: Transformation, - _bounds: Rectangle, - _target: &wgpu::TextureView, + bounds: Rectangle, + target: &wgpu::TextureView, _scale: f32, ) { let uniforms_buffer = device @@ -277,7 +270,9 @@ impl Pipeline { std::mem::size_of::() as u64, ); - for image in instances { + let mut instances: Vec = Vec::new(); + + for image in images { match &image.handle { Handle::Raster(_handle) => { #[cfg(feature = "image")] @@ -290,13 +285,10 @@ impl Pipeline { encoder, &mut self.texture_array, ) { - self.draw_image( - device, - encoder, + add_instances( image, allocation, - _bounds, - _target, + &mut instances, ); } } @@ -315,38 +307,17 @@ impl Pipeline { encoder, &mut self.texture_array, ) { - self.draw_image( - device, - encoder, + add_instances( image, allocation, - _bounds, - _target, + &mut instances, ); } } } } } - } - - pub fn trim_cache(&mut self) { - #[cfg(feature = "image")] - self.raster_cache.borrow_mut().trim(&mut self.texture_array); - - #[cfg(feature = "svg")] - self.vector_cache.borrow_mut().trim(&mut self.texture_array); - } - fn draw_image( - &self, - device: &mut wgpu::Device, - encoder: &mut wgpu::CommandEncoder, - image: &Image, - allocation: &ImageAllocation, - bounds: Rectangle, - target: &wgpu::TextureView, - ) { let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &self.texture_layout, bindings: &[wgpu::Binding { @@ -357,82 +328,10 @@ impl Pipeline { }], }); - match allocation { - ImageAllocation::SingleAllocation(allocation) => { - self.draw_allocation( - device, - encoder, - image.position, - image.scale, - allocation, - &texture, - bounds, - target, - ) - } - ImageAllocation::MultipleAllocations { mappings, size } => { - let scaling_x = image.scale[0] / size.0 as f32; - let scaling_y = image.scale[1] / size.1 as f32; - - for mapping in mappings { - let mut position = image.position; - let mut scale = image.scale; - - position[0] += mapping.src_pos.0 as f32 * scaling_x; - position[1] += mapping.src_pos.1 as f32 * scaling_y; - scale[0] = mapping.allocation.size().0 as f32 * scaling_x; - scale[1] = mapping.allocation.size().1 as f32 * scaling_y; - - self.draw_allocation( - device, - encoder, - position, - scale, - &mapping.allocation, - &texture, - bounds, - target, - ) - } - } - _ => {} - } - } - - fn draw_allocation( - &self, - device: &mut wgpu::Device, - encoder: &mut wgpu::CommandEncoder, - position: [f32; 2], - scale: [f32; 2], - allocation: &ArrayAllocation, - texture: &wgpu::BindGroup, - bounds: Rectangle, - target: &wgpu::TextureView, - ) { - let x = (allocation.position().0 as f32 + 0.5) / (ATLAS_SIZE as f32); - let y = (allocation.position().1 as f32 + 0.5) / (ATLAS_SIZE as f32); - let w = (allocation.size().0 as f32 - 0.5) / (ATLAS_SIZE as f32); - let h = (allocation.size().1 as f32 - 0.5) / (ATLAS_SIZE as f32); - let layer = allocation.layer() as f32; - - let instance_buffer = device - .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) - .fill_from_slice(&[Instance { - _position: position, - _scale: scale, - _position_in_atlas: [x, y], - _scale_in_atlas: [w, h], - _layer: layer, - }]); - - encoder.copy_buffer_to_buffer( - &instance_buffer, - 0, - &self.instances, - 0, - mem::size_of::() as u64, - ); + let instances_buffer = device.create_buffer_mapped( + instances.len(), + wgpu::BufferUsage::VERTEX, + ).fill_from_slice(&instances); let mut render_pass = encoder.begin_render_pass( &wgpu::RenderPassDescriptor { @@ -460,8 +359,9 @@ impl Pipeline { render_pass.set_index_buffer(&self.indices, 0); render_pass.set_vertex_buffers( 0, - &[(&self.vertices, 0), (&self.instances, 0)], + &[(&self.vertices, 0), (&instances_buffer, 0)], ); + render_pass.set_scissor_rect( bounds.x, bounds.y, @@ -472,9 +372,69 @@ impl Pipeline { render_pass.draw_indexed( 0..QUAD_INDICES.len() as u32, 0, - 0..1 as u32, + 0..instances.len() as u32, ); } + + pub fn trim_cache(&mut self) { + #[cfg(feature = "image")] + self.raster_cache.borrow_mut().trim(&mut self.texture_array); + + #[cfg(feature = "svg")] + self.vector_cache.borrow_mut().trim(&mut self.texture_array); + } +} + +fn add_instances( + image: &Image, + allocation: &ImageAllocation, + instances: &mut Vec, +) { + match allocation { + ImageAllocation::SingleAllocation(allocation) => { + add_instance(image.position, image.scale, allocation, instances); + } + ImageAllocation::MultipleAllocations { mappings, size } => { + let scaling_x = image.scale[0] / size.0 as f32; + let scaling_y = image.scale[1] / size.1 as f32; + + for mapping in mappings { + let allocation = &mapping.allocation; + let mut position = image.position; + let mut scale = image.scale; + + position[0] += mapping.src_pos.0 as f32 * scaling_x; + position[1] += mapping.src_pos.1 as f32 * scaling_y; + scale[0] = allocation.size().0 as f32 * scaling_x; + scale[1] = allocation.size().1 as f32 * scaling_y; + + add_instance(position, scale, allocation, instances); + } + } + } +} + +fn add_instance( + position: [f32; 2], + scale: [f32; 2], + allocation: &ArrayAllocation, + instances: &mut Vec, +) { + let x = (allocation.position().0 as f32 + 0.5) / (ATLAS_SIZE as f32); + let y = (allocation.position().1 as f32 + 0.5) / (ATLAS_SIZE as f32); + let w = (allocation.size().0 as f32 - 0.5) / (ATLAS_SIZE as f32); + let h = (allocation.size().1 as f32 - 0.5) / (ATLAS_SIZE as f32); + let layer = allocation.layer() as f32; + + let instance = Instance { + _position: position, + _scale: scale, + _position_in_atlas: [x, y], + _scale_in_atlas: [w, h], + _layer: layer, + }; + + instances.push(instance); } pub struct Image { @@ -501,10 +461,10 @@ pub enum ImageAllocation { mappings: Vec, size: (u32, u32), }, - Error, } impl ImageAllocation { + #[cfg(feature = "image")] pub fn size(&self) -> (u32, u32) { match self { ImageAllocation::SingleAllocation(allocation) => { @@ -513,7 +473,6 @@ impl ImageAllocation { ImageAllocation::MultipleAllocations { size, .. } => { *size } - _ => (0, 0) } } } @@ -522,7 +481,7 @@ impl ImageAllocation { pub enum ArrayAllocation { AtlasAllocation { layer: usize, - #[debug_stub = "ReplacementValue"] + #[debug_stub = "Allocation"] allocation: Allocation, }, WholeLayer { @@ -563,7 +522,7 @@ impl ArrayAllocation { pub enum TextureLayer { Whole, Atlas( - #[debug_stub="ReplacementValue"] + #[debug_stub="AtlasAllocator"] AtlasAllocator ), Empty, @@ -577,7 +536,7 @@ pub struct TextureArray { } impl TextureArray { - pub fn new(device: &wgpu::Device) -> Self { + fn new(device: &wgpu::Device) -> Self { let (width, height) = (ATLAS_SIZE, ATLAS_SIZE); let extent = wgpu::Extent3d { @@ -598,32 +557,30 @@ impl TextureArray { | wgpu::TextureUsage::SAMPLED, }); - let size = Size::new(ATLAS_SIZE as i32, ATLAS_SIZE as i32); - TextureArray { texture, texture_array_size: 1, - layers: vec!(TextureLayer::Atlas(AtlasAllocator::new(size))), + layers: vec!(TextureLayer::Empty), } } - pub fn allocate(&mut self, size: Size) -> ImageAllocation { + fn allocate(&mut self, size: Size) -> Option { // Allocate one layer if allocation fits perfectly if size.width == ATLAS_SIZE as i32 && size.height == ATLAS_SIZE as i32 { - for (i, layer) in &mut self.layers.iter_mut().enumerate() { + for (i, layer) in self.layers.iter_mut().enumerate() { if let TextureLayer::Empty = layer { *layer = TextureLayer::Whole; - return ImageAllocation::SingleAllocation( + return Some(ImageAllocation::SingleAllocation( ArrayAllocation::WholeLayer { layer: i } - ); + )); } } self.layers.push(TextureLayer::Whole); - return ImageAllocation::SingleAllocation( + return Some(ImageAllocation::SingleAllocation( ArrayAllocation::WholeLayer { layer: self.layers.len() - 1 } - ); + )); } // Split big allocations across multiple layers @@ -637,7 +594,11 @@ impl TextureArray { while x < size.width { let width = std::cmp::min(size.width - x, ATLAS_SIZE as i32); - if let ImageAllocation::SingleAllocation(allocation) = self.allocate(Size::new(width, height)) { + let allocation = self + .allocate(Size::new(width, height)) + .expect("Allocating texture space"); + + if let ImageAllocation::SingleAllocation(allocation) = allocation { let src_pos = (x as u32, y as u32); mappings.push(ArrayAllocationMapping { src_pos, allocation }); } @@ -647,10 +608,10 @@ impl TextureArray { y += height; } - return ImageAllocation::MultipleAllocations { + return Some(ImageAllocation::MultipleAllocations { mappings, size: (size.width as u32, size.height as u32), - }; + }); } // Try allocating on an existing layer @@ -658,7 +619,7 @@ impl TextureArray { if let TextureLayer::Atlas(allocator) = layer { if let Some(allocation) = allocator.allocate(size.clone()) { let array_allocation = ArrayAllocation::AtlasAllocation { layer: i, allocation }; - return ImageAllocation::SingleAllocation(array_allocation); + return Some(ImageAllocation::SingleAllocation(array_allocation)); } } } @@ -668,19 +629,19 @@ impl TextureArray { if let Some(allocation) = allocator.allocate(size) { self.layers.push(TextureLayer::Atlas(allocator)); - return ImageAllocation::SingleAllocation( + return Some(ImageAllocation::SingleAllocation( ArrayAllocation::AtlasAllocation { layer: self.layers.len() - 1, allocation, } - ); + )); } // One of the above should have worked - ImageAllocation::Error + None } - pub fn deallocate(&mut self, allocation: &ImageAllocation) { + fn deallocate(&mut self, allocation: &ImageAllocation) { match allocation { ImageAllocation::SingleAllocation(allocation) => { if let Some(layer) = self.layers.get_mut(allocation.layer()) { @@ -712,7 +673,6 @@ impl TextureArray { } } } - _ => {} } } @@ -720,15 +680,17 @@ impl TextureArray { fn upload( &mut self, image: &I, - allocation: &ImageAllocation, device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - ) + ) -> ImageAllocation where I: RawImageData, C: Copy + 'static, { - match allocation { + let size = Size::new(image.width() as i32, image.height() as i32); + let allocation = self.allocate(size).expect("Allocating texture space"); + + match &allocation { ImageAllocation::SingleAllocation(allocation) => { let data = image.data(); let buffer = device @@ -744,7 +706,7 @@ impl TextureArray { self.upload_texture( &buffer, - allocation, + &allocation, encoder, ); } @@ -752,6 +714,17 @@ impl TextureArray { let chunks_per_pixel = 4 / std::mem::size_of::(); let chunks_per_line = chunks_per_pixel * image.width() as usize; + let highest_layer = mappings + .iter() + .map(|m| m.allocation.layer() as u32) + .max() + .unwrap_or(0); + + if highest_layer >= self.texture_array_size { + let grow_by = 1 + highest_layer - self.texture_array_size; + self.grow(grow_by, device, encoder); + } + for mapping in mappings { let sub_width = mapping.allocation.size().0 as usize; let sub_height = mapping.allocation.size().1 as usize; @@ -777,17 +750,6 @@ impl TextureArray { buffer_line.copy_from_slice(sub_line); } - let highest_layer = mappings - .iter() - .map(|m| m.allocation.layer() as u32) - .max() - .unwrap_or(0); - - if highest_layer >= self.texture_array_size { - let grow_by = 1 + highest_layer - self.texture_array_size; - self.grow(grow_by, device, encoder); - } - self.upload_texture( &buffer.finish(), &mapping.allocation, @@ -795,9 +757,9 @@ impl TextureArray { ); } } - _ => {} } + allocation } fn upload_texture( @@ -867,33 +829,43 @@ impl TextureArray { | wgpu::TextureUsage::SAMPLED, }); - encoder.copy_texture_to_texture( - wgpu::TextureCopyView { - texture: &self.texture, - array_layer: 0, - mip_level: 0, - origin: wgpu::Origin3d { - x: 0.0, - y: 0.0, - z: 0.0, + for (i, layer) in self.layers.iter().enumerate() { + if i >= old_texture_array_size as usize { + break; + } + + if let TextureLayer::Empty = layer { + 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: 0, - 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: ATLAS_SIZE, - height: ATLAS_SIZE, - depth: self.texture_array_size, - } - ); + wgpu::Extent3d { + width: ATLAS_SIZE, + height: ATLAS_SIZE, + depth: 1, + } + ); + } self.texture_array_size += grow_by; self.texture = new_texture; @@ -968,7 +940,7 @@ const QUAD_VERTS: [Vertex; 4] = [ const ATLAS_SIZE: u32 = 4096; #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] struct Instance { _position: [f32; 2], _scale: [f32; 2], -- 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.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 9443b876..2fd73b54 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -15,7 +15,6 @@ use std::mem; use std::cell::RefCell; use guillotiere::{Allocation, AtlasAllocator, Size}; -use debug_stub_derive::*; #[derive(Debug)] pub struct Pipeline { @@ -477,11 +476,9 @@ impl ImageAllocation { } } -#[derive(DebugStub)] pub enum ArrayAllocation { AtlasAllocation { layer: usize, - #[debug_stub = "Allocation"] allocation: Allocation, }, WholeLayer { @@ -518,16 +515,35 @@ impl ArrayAllocation { } } -#[derive(DebugStub)] +impl std::fmt::Debug for ArrayAllocation { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ArrayAllocation::AtlasAllocation { layer, .. } => { + write!(f, "ArrayAllocation::AtlasAllocation {{ layer: {} }}", layer) + }, + ArrayAllocation::WholeLayer { layer } => { + write!(f, "ArrayAllocation::WholeLayer {{ layer: {} }}", layer) + } + } + } +} + pub enum TextureLayer { Whole, - Atlas( - #[debug_stub="AtlasAllocator"] - AtlasAllocator - ), + Atlas(AtlasAllocator), Empty, } +impl std::fmt::Debug for TextureLayer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + TextureLayer::Whole => write!(f, "TextureLayer::Whole"), + TextureLayer::Atlas(_) => write!(f, "TextureLayer::Atlas"), + TextureLayer::Empty => write!(f, "TextureLayer::Empty"), + } + } +} + #[derive(Debug)] pub struct TextureArray { texture: wgpu::Texture, -- cgit From 8f9f44b9e8ff1f1629d2b19edd2ecdad79e80836 Mon Sep 17 00:00:00 2001 From: Malte Veerman Date: Sat, 18 Jan 2020 12:49:11 +0100 Subject: When deallocating the last allocation in an allocator mark its layer as empty --- wgpu/src/image.rs | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 2fd73b54..97e30403 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -660,37 +660,36 @@ impl TextureArray { fn deallocate(&mut self, allocation: &ImageAllocation) { match allocation { ImageAllocation::SingleAllocation(allocation) => { - if let Some(layer) = self.layers.get_mut(allocation.layer()) { - match allocation { - ArrayAllocation::WholeLayer { .. } => { - *layer = TextureLayer::Empty; - } - ArrayAllocation::AtlasAllocation { allocation, .. } => { - if let TextureLayer::Atlas(allocator) = layer { - allocator.deallocate(allocation.id); - } - } - } - } + self.deallocate_single_allocation(allocation); } ImageAllocation::MultipleAllocations { mappings, .. } => { for mapping in mappings { - if let Some(layer) = self.layers.get_mut(mapping.allocation.layer()) { - match &mapping.allocation { - ArrayAllocation::WholeLayer { .. } => { - *layer = TextureLayer::Empty; - } - ArrayAllocation::AtlasAllocation { allocation, .. } => { - if let TextureLayer::Atlas(allocator) = layer { - allocator.deallocate(allocation.id); - } - } + self.deallocate_single_allocation(&mapping.allocation); + } + } + } + } + + fn deallocate_single_allocation(&mut self, allocation: &ArrayAllocation) { + if let Some(layer) = self.layers.get_mut(allocation.layer()) { + match allocation { + ArrayAllocation::WholeLayer { .. } => { + *layer = TextureLayer::Empty; + } + ArrayAllocation::AtlasAllocation { allocation, .. } => { + if let TextureLayer::Atlas(allocator) = layer { + allocator.deallocate(allocation.id); + + let mut empty_allocator = true; + allocator.for_each_allocated_rectangle(|_, _| empty_allocator = false); + + if empty_allocator { + *layer = TextureLayer::Empty; } } } } } - } fn upload( @@ -953,7 +952,7 @@ const QUAD_VERTS: [Vertex; 4] = [ }, ]; -const ATLAS_SIZE: u32 = 4096; +const ATLAS_SIZE: u32 = 256; #[repr(C)] #[derive(Debug, Clone, Copy)] -- 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.rs | 155 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 86 insertions(+), 69 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 97e30403..7eaa1ae8 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -9,10 +9,7 @@ use crate::image::raster::Memory; use crate::Transformation; use iced_native::{image, svg, Rectangle}; -use std::mem; - -#[cfg(any(feature = "image", feature = "svg"))] -use std::cell::RefCell; +use std::{cell::RefCell, mem, rc::Rc}; use guillotiere::{Allocation, AtlasAllocator, Size}; @@ -377,10 +374,10 @@ impl Pipeline { pub fn trim_cache(&mut self) { #[cfg(feature = "image")] - self.raster_cache.borrow_mut().trim(&mut self.texture_array); + self.raster_cache.borrow_mut().trim(); #[cfg(feature = "svg")] - self.vector_cache.borrow_mut().trim(&mut self.texture_array); + self.vector_cache.borrow_mut().trim(); } } @@ -423,14 +420,14 @@ fn add_instance( let y = (allocation.position().1 as f32 + 0.5) / (ATLAS_SIZE as f32); let w = (allocation.size().0 as f32 - 0.5) / (ATLAS_SIZE as f32); let h = (allocation.size().1 as f32 - 0.5) / (ATLAS_SIZE as f32); - let layer = allocation.layer() as f32; + let layer_index = allocation.layer_index() as f32; let instance = Instance { _position: position, _scale: scale, _position_in_atlas: [x, y], _scale_in_atlas: [w, h], - _layer: layer, + _layer: layer_index, }; instances.push(instance); @@ -478,11 +475,13 @@ impl ImageAllocation { pub enum ArrayAllocation { AtlasAllocation { - layer: usize, + layer_index: usize, + layer: Rc>, allocation: Allocation, }, WholeLayer { - layer: usize, + layer_index: usize, + layer: Rc>, } } @@ -507,10 +506,10 @@ impl ArrayAllocation { } } - pub fn layer(&self) -> usize { + pub fn layer_index(&self) -> usize { match self { - ArrayAllocation::AtlasAllocation { layer, .. } => *layer, - ArrayAllocation::WholeLayer { layer } => *layer, + ArrayAllocation::AtlasAllocation { layer_index, .. } => *layer_index, + ArrayAllocation::WholeLayer { layer_index, .. } => *layer_index, } } } @@ -518,11 +517,34 @@ impl ArrayAllocation { impl std::fmt::Debug for ArrayAllocation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - ArrayAllocation::AtlasAllocation { layer, .. } => { - write!(f, "ArrayAllocation::AtlasAllocation {{ layer: {} }}", layer) + ArrayAllocation::AtlasAllocation { layer_index, .. } => { + write!(f, "ArrayAllocation::AtlasAllocation {{ layer_index: {:} }}", layer_index) }, - ArrayAllocation::WholeLayer { layer } => { - write!(f, "ArrayAllocation::WholeLayer {{ layer: {} }}", layer) + ArrayAllocation::WholeLayer { layer_index, .. } => { + write!(f, "ArrayAllocation::WholeLayer {{ layer_index: {} }}", layer_index) + } + } + } +} + +impl Drop for ArrayAllocation { + fn drop(&mut self) { + match self { + ArrayAllocation::WholeLayer { layer, .. } => { + let _ = layer.replace(TextureLayer::Whole); + } + ArrayAllocation::AtlasAllocation { allocation, layer, .. } => { + let mut layer = layer.borrow_mut(); + if let Some(allocator) = layer.allocator_mut() { + allocator.deallocate(allocation.id); + + let mut empty_allocator = true; + allocator.for_each_allocated_rectangle(|_, _| empty_allocator = false); + + if empty_allocator { + *layer = TextureLayer::Empty; + } + } } } } @@ -534,6 +556,23 @@ pub enum TextureLayer { Empty, } +impl TextureLayer { + pub fn is_empty(&self) -> bool { + if let TextureLayer::Empty = self { + true + } else { + false + } + } + + pub fn allocator_mut(&mut self) -> Option<&mut AtlasAllocator> { + match self { + TextureLayer::Atlas(allocator) => Some(allocator), + _ => None + } + } +} + impl std::fmt::Debug for TextureLayer { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -544,11 +583,17 @@ impl std::fmt::Debug for TextureLayer { } } +impl From for TextureLayer { + fn from(allocator: AtlasAllocator) -> Self { + TextureLayer::Atlas(allocator) + } +} + #[derive(Debug)] pub struct TextureArray { texture: wgpu::Texture, texture_array_size: u32, - layers: Vec, + layers: Vec>>, } impl TextureArray { @@ -576,7 +621,7 @@ impl TextureArray { TextureArray { texture, texture_array_size: 1, - layers: vec!(TextureLayer::Empty), + layers: vec!(Rc::new(RefCell::new(TextureLayer::Empty))), } } @@ -584,18 +629,19 @@ impl TextureArray { // Allocate one layer if allocation fits perfectly if size.width == ATLAS_SIZE as i32 && size.height == ATLAS_SIZE as i32 { for (i, layer) in self.layers.iter_mut().enumerate() { - if let TextureLayer::Empty = layer + if layer.borrow().is_empty() { - *layer = TextureLayer::Whole; + let _ = layer.replace(TextureLayer::Whole); return Some(ImageAllocation::SingleAllocation( - ArrayAllocation::WholeLayer { layer: i } + ArrayAllocation::WholeLayer { layer: layer.clone(), layer_index: i } )); } } - self.layers.push(TextureLayer::Whole); + let layer = Rc::new(RefCell::new(TextureLayer::Whole)); + self.layers.push(layer.clone()); return Some(ImageAllocation::SingleAllocation( - ArrayAllocation::WholeLayer { layer: self.layers.len() - 1 } + ArrayAllocation::WholeLayer { layer, layer_index: self.layers.len() - 1 } )); } @@ -632,9 +678,13 @@ impl TextureArray { // Try allocating on an existing layer for (i, layer) in self.layers.iter_mut().enumerate() { - if let TextureLayer::Atlas(allocator) = layer { + if let Some(allocator) = layer.borrow_mut().allocator_mut() { if let Some(allocation) = allocator.allocate(size.clone()) { - let array_allocation = ArrayAllocation::AtlasAllocation { layer: i, allocation }; + let array_allocation = ArrayAllocation::AtlasAllocation { + layer: layer.clone(), + layer_index: i, + allocation + }; return Some(ImageAllocation::SingleAllocation(array_allocation)); } } @@ -643,11 +693,13 @@ impl TextureArray { // Create new layer with atlas allocator let mut allocator = AtlasAllocator::new(Size::new(ATLAS_SIZE as i32, ATLAS_SIZE as i32)); if let Some(allocation) = allocator.allocate(size) { - self.layers.push(TextureLayer::Atlas(allocator)); + let layer = Rc::new(RefCell::new(allocator.into())); + self.layers.push(layer.clone()); return Some(ImageAllocation::SingleAllocation( ArrayAllocation::AtlasAllocation { - layer: self.layers.len() - 1, + layer, + layer_index: self.layers.len() - 1, allocation, } )); @@ -657,41 +709,6 @@ impl TextureArray { None } - fn deallocate(&mut self, allocation: &ImageAllocation) { - match allocation { - ImageAllocation::SingleAllocation(allocation) => { - self.deallocate_single_allocation(allocation); - } - ImageAllocation::MultipleAllocations { mappings, .. } => { - for mapping in mappings { - self.deallocate_single_allocation(&mapping.allocation); - } - } - } - } - - fn deallocate_single_allocation(&mut self, allocation: &ArrayAllocation) { - if let Some(layer) = self.layers.get_mut(allocation.layer()) { - match allocation { - ArrayAllocation::WholeLayer { .. } => { - *layer = TextureLayer::Empty; - } - ArrayAllocation::AtlasAllocation { allocation, .. } => { - if let TextureLayer::Atlas(allocator) = layer { - allocator.deallocate(allocation.id); - - let mut empty_allocator = true; - allocator.for_each_allocated_rectangle(|_, _| empty_allocator = false); - - if empty_allocator { - *layer = TextureLayer::Empty; - } - } - } - } - } - } - fn upload( &mut self, image: &I, @@ -715,7 +732,7 @@ impl TextureArray { ) .fill_from_slice(data); - if allocation.layer() >= self.texture_array_size as usize { + if allocation.layer_index() >= self.texture_array_size as usize { self.grow(1, device, encoder); } @@ -731,7 +748,7 @@ impl TextureArray { let highest_layer = mappings .iter() - .map(|m| m.allocation.layer() as u32) + .map(|m| m.allocation.layer_index() as u32) .max() .unwrap_or(0); @@ -783,7 +800,7 @@ impl TextureArray { allocation: &ArrayAllocation, encoder: &mut wgpu::CommandEncoder, ) { - let array_layer = allocation.layer() as u32; + let array_layer = allocation.layer_index() as u32; let (width, height) = allocation.size(); @@ -844,12 +861,12 @@ impl TextureArray { | wgpu::TextureUsage::SAMPLED, }); - for (i, layer) in self.layers.iter().enumerate() { + for (i, layer) in self.layers.iter_mut().enumerate() { if i >= old_texture_array_size as usize { break; } - if let TextureLayer::Empty = layer { + if layer.borrow().is_empty() { continue; } @@ -952,7 +969,7 @@ const QUAD_VERTS: [Vertex; 4] = [ }, ]; -const ATLAS_SIZE: u32 = 256; +const ATLAS_SIZE: u32 = 4096; #[repr(C)] #[derive(Debug, Clone, Copy)] -- 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.rs | 768 +++++++++++------------------------------------------- 1 file changed, 145 insertions(+), 623 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 7eaa1ae8..e14b3024 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -3,15 +3,13 @@ mod raster; #[cfg(feature = "svg")] mod vector; -#[cfg(feature = "image")] -use crate::image::raster::Memory; +use crate::{ + texture::{self, atlas}, + Transformation, +}; -use crate::Transformation; use iced_native::{image, svg, Rectangle}; - -use std::{cell::RefCell, mem, rc::Rc}; - -use guillotiere::{Allocation, AtlasAllocator, Size}; +use std::{cell::RefCell, mem}; #[derive(Debug)] pub struct Pipeline { @@ -25,8 +23,10 @@ pub struct Pipeline { vertices: wgpu::Buffer, indices: wgpu::Buffer, constants: wgpu::BindGroup, + texture: wgpu::BindGroup, + texture_version: usize, texture_layout: wgpu::BindGroupLayout, - texture_array: TextureArray, + texture_atlas: texture::Atlas, } impl Pipeline { @@ -207,7 +207,17 @@ impl Pipeline { .create_buffer_mapped(QUAD_INDICES.len(), wgpu::BufferUsage::INDEX) .fill_from_slice(&QUAD_INDICES); - let texture_array = TextureArray::new(device); + let texture_atlas = texture::Atlas::new(device); + + let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { + layout: &texture_layout, + bindings: &[wgpu::Binding { + binding: 0, + resource: wgpu::BindingResource::TextureView( + &texture_atlas.view(), + ), + }], + }); Pipeline { #[cfg(feature = "image")] @@ -221,8 +231,10 @@ impl Pipeline { vertices, indices, constants: constant_bind_group, + texture, + texture_version: texture_atlas.layer_count(), texture_layout, - texture_array, + texture_atlas, } } @@ -252,85 +264,88 @@ impl Pipeline { target: &wgpu::TextureView, _scale: f32, ) { - let uniforms_buffer = device - .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) - .fill_from_slice(&[Uniforms { - transform: transformation.into(), - }]); + let mut instances: Vec = Vec::new(); - encoder.copy_buffer_to_buffer( - &uniforms_buffer, - 0, - &self.uniforms, - 0, - std::mem::size_of::() as u64, - ); + #[cfg(feature = "image")] + let mut raster_cache = self.raster_cache.borrow_mut(); - let mut instances: Vec = Vec::new(); + #[cfg(feature = "svg")] + let mut vector_cache = self.vector_cache.borrow_mut(); for image in images { match &image.handle { Handle::Raster(_handle) => { #[cfg(feature = "image")] { - let mut raster_cache = self.raster_cache.borrow_mut(); - - if let Memory::Device(allocation) = raster_cache.upload( + if let Some(atlas_entry) = raster_cache.upload( _handle, device, encoder, - &mut self.texture_array, + &mut self.texture_atlas, ) { - add_instances( - image, - allocation, - &mut instances, - ); + add_instances(image, atlas_entry, &mut instances); } - } + }; } Handle::Vector(_handle) => { #[cfg(feature = "svg")] { - let mut vector_cache = self.vector_cache.borrow_mut(); - - // Upload rasterized svg to texture atlas - if let Some(allocation) = vector_cache.upload( + if let Some(atlas_entry) = vector_cache.upload( _handle, - image.scale, + image.size, _scale, device, encoder, - &mut self.texture_array, + &mut self.texture_atlas, ) { - add_instances( - image, - allocation, - &mut instances, - ); + add_instances(image, atlas_entry, &mut instances); } - } + }; } } } - let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { - layout: &self.texture_layout, - bindings: &[wgpu::Binding { - binding: 0, - resource: wgpu::BindingResource::TextureView( - &self.texture_array.texture.create_default_view(), - ), - }], - }); + if instances.is_empty() { + return; + } + + let texture_version = self.texture_atlas.layer_count(); + + if self.texture_version != texture_version { + self.texture = + device.create_bind_group(&wgpu::BindGroupDescriptor { + layout: &self.texture_layout, + bindings: &[wgpu::Binding { + binding: 0, + resource: wgpu::BindingResource::TextureView( + &self.texture_atlas.view(), + ), + }], + }); - let instances_buffer = device.create_buffer_mapped( - instances.len(), - wgpu::BufferUsage::VERTEX, - ).fill_from_slice(&instances); + self.texture_version = texture_version; + } - let mut render_pass = encoder.begin_render_pass( - &wgpu::RenderPassDescriptor { + let uniforms_buffer = device + .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC) + .fill_from_slice(&[Uniforms { + transform: transformation.into(), + }]); + + encoder.copy_buffer_to_buffer( + &uniforms_buffer, + 0, + &self.uniforms, + 0, + std::mem::size_of::() as u64, + ); + + let instances_buffer = device + .create_buffer_mapped(instances.len(), wgpu::BufferUsage::VERTEX) + .fill_from_slice(&instances); + + let mut render_pass = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { color_attachments: &[ wgpu::RenderPassColorAttachmentDescriptor { attachment: target, @@ -346,12 +361,11 @@ impl Pipeline { }, ], depth_stencil_attachment: None, - }, - ); + }); render_pass.set_pipeline(&self.pipeline); render_pass.set_bind_group(0, &self.constants, &[]); - render_pass.set_bind_group(1, &texture, &[]); + render_pass.set_bind_group(1, &self.texture, &[]); render_pass.set_index_buffer(&self.indices, 0); render_pass.set_vertex_buffers( 0, @@ -381,62 +395,10 @@ impl Pipeline { } } -fn add_instances( - image: &Image, - allocation: &ImageAllocation, - instances: &mut Vec, -) { - match allocation { - ImageAllocation::SingleAllocation(allocation) => { - add_instance(image.position, image.scale, allocation, instances); - } - ImageAllocation::MultipleAllocations { mappings, size } => { - let scaling_x = image.scale[0] / size.0 as f32; - let scaling_y = image.scale[1] / size.1 as f32; - - for mapping in mappings { - let allocation = &mapping.allocation; - let mut position = image.position; - let mut scale = image.scale; - - position[0] += mapping.src_pos.0 as f32 * scaling_x; - position[1] += mapping.src_pos.1 as f32 * scaling_y; - scale[0] = allocation.size().0 as f32 * scaling_x; - scale[1] = allocation.size().1 as f32 * scaling_y; - - add_instance(position, scale, allocation, instances); - } - } - } -} - -fn add_instance( - position: [f32; 2], - scale: [f32; 2], - allocation: &ArrayAllocation, - instances: &mut Vec, -) { - let x = (allocation.position().0 as f32 + 0.5) / (ATLAS_SIZE as f32); - let y = (allocation.position().1 as f32 + 0.5) / (ATLAS_SIZE as f32); - let w = (allocation.size().0 as f32 - 0.5) / (ATLAS_SIZE as f32); - let h = (allocation.size().1 as f32 - 0.5) / (ATLAS_SIZE as f32); - let layer_index = allocation.layer_index() as f32; - - let instance = Instance { - _position: position, - _scale: scale, - _position_in_atlas: [x, y], - _scale_in_atlas: [w, h], - _layer: layer_index, - }; - - instances.push(instance); -} - pub struct Image { pub handle: Handle, pub position: [f32; 2], - pub scale: [f32; 2], + pub size: [f32; 2], } pub enum Handle { @@ -444,508 +406,6 @@ pub enum Handle { Vector(svg::Handle), } -#[derive(Debug)] -pub struct ArrayAllocationMapping { - src_pos: (u32, u32), - allocation: ArrayAllocation, -} - -#[derive(Debug)] -pub enum ImageAllocation { - SingleAllocation(ArrayAllocation), - MultipleAllocations { - mappings: Vec, - size: (u32, u32), - }, -} - -impl ImageAllocation { - #[cfg(feature = "image")] - pub fn size(&self) -> (u32, u32) { - match self { - ImageAllocation::SingleAllocation(allocation) => { - allocation.size() - } - ImageAllocation::MultipleAllocations { size, .. } => { - *size - } - } - } -} - -pub enum ArrayAllocation { - AtlasAllocation { - layer_index: usize, - layer: Rc>, - allocation: Allocation, - }, - WholeLayer { - layer_index: usize, - layer: Rc>, - } -} - -impl ArrayAllocation { - pub fn size(&self) -> (u32, u32) { - match self { - ArrayAllocation::AtlasAllocation { allocation, .. } => { - let size = allocation.rectangle.size(); - (size.width as u32, size.height as u32) - } - ArrayAllocation::WholeLayer { .. } => (ATLAS_SIZE, ATLAS_SIZE) - } - } - - pub fn position(&self) -> (u32, u32) { - match self { - ArrayAllocation::AtlasAllocation { allocation, .. } => { - let min = &allocation.rectangle.min; - (min.x as u32, min.y as u32) - } - ArrayAllocation::WholeLayer { .. } => (0, 0) - } - } - - pub fn layer_index(&self) -> usize { - match self { - ArrayAllocation::AtlasAllocation { layer_index, .. } => *layer_index, - ArrayAllocation::WholeLayer { layer_index, .. } => *layer_index, - } - } -} - -impl std::fmt::Debug for ArrayAllocation { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - ArrayAllocation::AtlasAllocation { layer_index, .. } => { - write!(f, "ArrayAllocation::AtlasAllocation {{ layer_index: {:} }}", layer_index) - }, - ArrayAllocation::WholeLayer { layer_index, .. } => { - write!(f, "ArrayAllocation::WholeLayer {{ layer_index: {} }}", layer_index) - } - } - } -} - -impl Drop for ArrayAllocation { - fn drop(&mut self) { - match self { - ArrayAllocation::WholeLayer { layer, .. } => { - let _ = layer.replace(TextureLayer::Whole); - } - ArrayAllocation::AtlasAllocation { allocation, layer, .. } => { - let mut layer = layer.borrow_mut(); - if let Some(allocator) = layer.allocator_mut() { - allocator.deallocate(allocation.id); - - let mut empty_allocator = true; - allocator.for_each_allocated_rectangle(|_, _| empty_allocator = false); - - if empty_allocator { - *layer = TextureLayer::Empty; - } - } - } - } - } -} - -pub enum TextureLayer { - Whole, - Atlas(AtlasAllocator), - Empty, -} - -impl TextureLayer { - pub fn is_empty(&self) -> bool { - if let TextureLayer::Empty = self { - true - } else { - false - } - } - - pub fn allocator_mut(&mut self) -> Option<&mut AtlasAllocator> { - match self { - TextureLayer::Atlas(allocator) => Some(allocator), - _ => None - } - } -} - -impl std::fmt::Debug for TextureLayer { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - TextureLayer::Whole => write!(f, "TextureLayer::Whole"), - TextureLayer::Atlas(_) => write!(f, "TextureLayer::Atlas"), - TextureLayer::Empty => write!(f, "TextureLayer::Empty"), - } - } -} - -impl From for TextureLayer { - fn from(allocator: AtlasAllocator) -> Self { - TextureLayer::Atlas(allocator) - } -} - -#[derive(Debug)] -pub struct TextureArray { - texture: wgpu::Texture, - texture_array_size: u32, - layers: Vec>>, -} - -impl TextureArray { - fn new(device: &wgpu::Device) -> Self { - let (width, height) = (ATLAS_SIZE, ATLAS_SIZE); - - 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::COPY_SRC - | wgpu::TextureUsage::SAMPLED, - }); - - TextureArray { - texture, - texture_array_size: 1, - layers: vec!(Rc::new(RefCell::new(TextureLayer::Empty))), - } - } - - fn allocate(&mut self, size: Size) -> Option { - // Allocate one layer if allocation fits perfectly - if size.width == ATLAS_SIZE as i32 && size.height == ATLAS_SIZE as i32 { - for (i, layer) in self.layers.iter_mut().enumerate() { - if layer.borrow().is_empty() - { - let _ = layer.replace(TextureLayer::Whole); - return Some(ImageAllocation::SingleAllocation( - ArrayAllocation::WholeLayer { layer: layer.clone(), layer_index: i } - )); - } - } - - let layer = Rc::new(RefCell::new(TextureLayer::Whole)); - self.layers.push(layer.clone()); - return Some(ImageAllocation::SingleAllocation( - ArrayAllocation::WholeLayer { layer, layer_index: self.layers.len() - 1 } - )); - } - - // Split big allocations across multiple layers - if size.width > ATLAS_SIZE as i32 || size.height > ATLAS_SIZE as i32 { - let mut mappings = Vec::new(); - - let mut y = 0; - while y < size.height { - let height = std::cmp::min(size.height - y, ATLAS_SIZE as i32); - let mut x = 0; - - while x < size.width { - let width = std::cmp::min(size.width - x, ATLAS_SIZE as i32); - let allocation = self - .allocate(Size::new(width, height)) - .expect("Allocating texture space"); - - if let ImageAllocation::SingleAllocation(allocation) = allocation { - let src_pos = (x as u32, y as u32); - mappings.push(ArrayAllocationMapping { src_pos, allocation }); - } - - x += width; - } - y += height; - } - - return Some(ImageAllocation::MultipleAllocations { - mappings, - size: (size.width as u32, size.height as u32), - }); - } - - // Try allocating on an existing layer - for (i, layer) in self.layers.iter_mut().enumerate() { - if let Some(allocator) = layer.borrow_mut().allocator_mut() { - if let Some(allocation) = allocator.allocate(size.clone()) { - let array_allocation = ArrayAllocation::AtlasAllocation { - layer: layer.clone(), - layer_index: i, - allocation - }; - return Some(ImageAllocation::SingleAllocation(array_allocation)); - } - } - } - - // Create new layer with atlas allocator - let mut allocator = AtlasAllocator::new(Size::new(ATLAS_SIZE as i32, ATLAS_SIZE as i32)); - if let Some(allocation) = allocator.allocate(size) { - let layer = Rc::new(RefCell::new(allocator.into())); - self.layers.push(layer.clone()); - - return Some(ImageAllocation::SingleAllocation( - ArrayAllocation::AtlasAllocation { - layer, - layer_index: self.layers.len() - 1, - allocation, - } - )); - } - - // One of the above should have worked - None - } - - fn upload( - &mut self, - image: &I, - device: &wgpu::Device, - encoder: &mut wgpu::CommandEncoder, - ) -> ImageAllocation - where - I: RawImageData, - C: Copy + 'static, - { - let size = Size::new(image.width() as i32, image.height() as i32); - let allocation = self.allocate(size).expect("Allocating texture space"); - - match &allocation { - ImageAllocation::SingleAllocation(allocation) => { - let data = image.data(); - let buffer = device - .create_buffer_mapped( - data.len(), - wgpu::BufferUsage::COPY_SRC, - ) - .fill_from_slice(data); - - if allocation.layer_index() >= self.texture_array_size as usize { - self.grow(1, device, encoder); - } - - self.upload_texture( - &buffer, - &allocation, - encoder, - ); - } - ImageAllocation::MultipleAllocations { mappings, .. } => { - let chunks_per_pixel = 4 / std::mem::size_of::(); - let chunks_per_line = chunks_per_pixel * image.width() as usize; - - let highest_layer = mappings - .iter() - .map(|m| m.allocation.layer_index() as u32) - .max() - .unwrap_or(0); - - if highest_layer >= self.texture_array_size { - let grow_by = 1 + highest_layer - self.texture_array_size; - self.grow(grow_by, device, encoder); - } - - for mapping in mappings { - let sub_width = mapping.allocation.size().0 as usize; - let sub_height = mapping.allocation.size().1 as usize; - let sub_line_start = mapping.src_pos.0 as usize * chunks_per_pixel; - let sub_line_end = (mapping.src_pos.0 as usize + sub_width) * chunks_per_pixel; - - let mut sub_lines = image - .data() - .chunks(chunks_per_line) - .skip(mapping.src_pos.1 as usize) - .take(sub_height) - .map(|line| &line[sub_line_start..sub_line_end]); - - let buffer = device - .create_buffer_mapped( - chunks_per_pixel * sub_width * sub_height, - wgpu::BufferUsage::COPY_SRC, - ); - - let mut buffer_lines = buffer.data.chunks_mut(sub_width * chunks_per_pixel); - - while let (Some(buffer_line), Some(sub_line)) = (buffer_lines.next(), sub_lines.next()) { - buffer_line.copy_from_slice(sub_line); - } - - self.upload_texture( - &buffer.finish(), - &mapping.allocation, - encoder, - ); - } - } - } - - allocation - } - - fn upload_texture( - &mut self, - buffer: &wgpu::Buffer, - allocation: &ArrayAllocation, - encoder: &mut wgpu::CommandEncoder, - ) { - let array_layer = allocation.layer_index() as u32; - - let (width, height) = allocation.size(); - - let extent = wgpu::Extent3d { - width, - height, - depth: 1, - }; - - let (x, y) = allocation.position(); - - encoder.copy_buffer_to_texture( - wgpu::BufferCopyView { - buffer, - offset: 0, - row_pitch: 4 * width, - image_height: height, - }, - wgpu::TextureCopyView { - texture: &self.texture, - array_layer, - mip_level: 0, - origin: wgpu::Origin3d { - x: x as f32, - y: y as f32, - z: 0.0, - }, - }, - extent, - ); - } - - fn grow( - &mut self, - grow_by: u32, - device: &wgpu::Device, - encoder: &mut wgpu::CommandEncoder, - ) { - if grow_by == 0 { - return; - } - - let old_texture_array_size = self.texture_array_size; - - let new_texture = device.create_texture(&wgpu::TextureDescriptor { - size: wgpu::Extent3d { - width: ATLAS_SIZE, - height: ATLAS_SIZE, - depth: 1, - }, - array_layer_count: old_texture_array_size + grow_by, - 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, - }); - - for (i, layer) in self.layers.iter_mut().enumerate() { - if i >= old_texture_array_size as usize { - break; - } - - if layer.borrow().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: ATLAS_SIZE, - height: ATLAS_SIZE, - depth: 1, - } - ); - } - - self.texture_array_size += grow_by; - self.texture = new_texture; - } -} - -trait RawImageData { - type Chunk; - - fn data(&self) -> &[Self::Chunk]; - fn width(&self) -> u32; - fn height(&self) -> u32; -} - -#[cfg(feature = "image")] -impl RawImageData for ::image::ImageBuffer<::image::Bgra, Vec> { - type Chunk = u8; - - fn data(&self) -> &[Self::Chunk] { - &self - } - - fn width(&self) -> u32 { - self.dimensions().0 - } - - fn height(&self) -> u32 { - self.dimensions().1 - } -} - -#[cfg(feature = "svg")] -impl RawImageData for resvg::raqote::DrawTarget { - type Chunk = u32; - - fn data(&self) -> &[Self::Chunk] { - self.get_data() - } - - fn width(&self) -> u32 { - self.width() as u32 - } - - fn height(&self) -> u32 { - self.height() as u32 - } -} - #[repr(C)] #[derive(Clone, Copy)] pub struct Vertex { @@ -969,16 +429,14 @@ const QUAD_VERTS: [Vertex; 4] = [ }, ]; -const ATLAS_SIZE: u32 = 4096; - #[repr(C)] #[derive(Debug, Clone, Copy)] struct Instance { _position: [f32; 2], - _scale: [f32; 2], + _size: [f32; 2], _position_in_atlas: [f32; 2], - _scale_in_atlas: [f32; 2], - _layer: f32, + _size_in_atlas: [f32; 2], + _layer: u32, } #[repr(C)] @@ -986,3 +444,67 @@ struct Instance { struct Uniforms { transform: [f32; 16], } + +fn add_instances( + image: &Image, + entry: &atlas::Entry, + instances: &mut Vec, +) { + match entry { + atlas::Entry::Contiguous(allocation) => { + add_instance(image.position, image.size, allocation, instances); + } + atlas::Entry::Fragmented { fragments, size } => { + let scaling_x = image.size[0] / size.0 as f32; + let scaling_y = image.size[1] / size.1 as f32; + + for fragment in fragments { + let allocation = &fragment.allocation; + + let [x, y] = image.position; + let (fragment_x, fragment_y) = fragment.position; + let (fragment_width, fragment_height) = allocation.size(); + + let position = [ + x + fragment_x as f32 * scaling_x, + y + fragment_y as f32 * scaling_y, + ]; + + let size = [ + fragment_width as f32 * scaling_x, + fragment_height as f32 * scaling_y, + ]; + + add_instance(position, size, allocation, instances); + } + } + } +} + +#[inline] +fn add_instance( + position: [f32; 2], + size: [f32; 2], + allocation: &atlas::Allocation, + instances: &mut Vec, +) { + let (x, y) = allocation.position(); + let (width, height) = allocation.size(); + let layer = allocation.layer(); + + let instance = Instance { + _position: position, + _size: size, + _position_in_atlas: [ + x as f32 / atlas::SIZE as f32, + y as f32 / atlas::SIZE as f32, + ], + _size_in_atlas: [ + width as f32 / atlas::SIZE as f32, + height as f32 / atlas::SIZE as f32, + ], + _layer: layer as u32, + }; + + instances.push(instance); +} -- 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.rs | 109 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 40 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index e14b3024..afff52a6 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -22,6 +22,7 @@ pub struct Pipeline { uniforms: wgpu::Buffer, vertices: wgpu::Buffer, indices: wgpu::Buffer, + instances: wgpu::Buffer, constants: wgpu::BindGroup, texture: wgpu::BindGroup, texture_version: usize, @@ -188,7 +189,7 @@ impl Pipeline { }, wgpu::VertexAttributeDescriptor { shader_location: 5, - format: wgpu::VertexFormat::Float, + format: wgpu::VertexFormat::Uint, offset: 4 * 8, }, ], @@ -207,6 +208,11 @@ impl Pipeline { .create_buffer_mapped(QUAD_INDICES.len(), wgpu::BufferUsage::INDEX) .fill_from_slice(&QUAD_INDICES); + let instances = device.create_buffer(&wgpu::BufferDescriptor { + size: mem::size_of::() as u64 * Instance::MAX as u64, + usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, + }); + let texture_atlas = texture::Atlas::new(device); let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { @@ -230,6 +236,7 @@ impl Pipeline { uniforms: uniforms_buffer, vertices, indices, + instances, constants: constant_bind_group, texture, texture_version: texture_atlas.layer_count(), @@ -341,49 +348,67 @@ impl Pipeline { ); let instances_buffer = device - .create_buffer_mapped(instances.len(), wgpu::BufferUsage::VERTEX) + .create_buffer_mapped(instances.len(), wgpu::BufferUsage::COPY_SRC) .fill_from_slice(&instances); - let mut render_pass = - encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - color_attachments: &[ - wgpu::RenderPassColorAttachmentDescriptor { - attachment: target, - resolve_target: None, - load_op: wgpu::LoadOp::Load, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 0.0, + let mut i = 0; + let total = instances.len(); + + while i < total { + let end = (i + Instance::MAX).min(total); + let amount = end - i; + + encoder.copy_buffer_to_buffer( + &instances_buffer, + (i * std::mem::size_of::()) as u64, + &self.instances, + 0, + (amount * std::mem::size_of::()) as u64, + ); + + let mut render_pass = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + color_attachments: &[ + wgpu::RenderPassColorAttachmentDescriptor { + attachment: target, + resolve_target: None, + load_op: wgpu::LoadOp::Load, + store_op: wgpu::StoreOp::Store, + clear_color: wgpu::Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.0, + }, }, - }, - ], - depth_stencil_attachment: None, - }); - - render_pass.set_pipeline(&self.pipeline); - render_pass.set_bind_group(0, &self.constants, &[]); - render_pass.set_bind_group(1, &self.texture, &[]); - render_pass.set_index_buffer(&self.indices, 0); - render_pass.set_vertex_buffers( - 0, - &[(&self.vertices, 0), (&instances_buffer, 0)], - ); - - render_pass.set_scissor_rect( - bounds.x, - bounds.y, - bounds.width, - bounds.height, - ); + ], + depth_stencil_attachment: None, + }); - render_pass.draw_indexed( - 0..QUAD_INDICES.len() as u32, - 0, - 0..instances.len() as u32, - ); + render_pass.set_pipeline(&self.pipeline); + render_pass.set_bind_group(0, &self.constants, &[]); + render_pass.set_bind_group(1, &self.texture, &[]); + render_pass.set_index_buffer(&self.indices, 0); + render_pass.set_vertex_buffers( + 0, + &[(&self.vertices, 0), (&self.instances, 0)], + ); + + render_pass.set_scissor_rect( + bounds.x, + bounds.y, + bounds.width, + bounds.height, + ); + + render_pass.draw_indexed( + 0..QUAD_INDICES.len() as u32, + 0, + 0..amount as u32, + ); + + i += Instance::MAX; + } } pub fn trim_cache(&mut self) { @@ -439,6 +464,10 @@ struct Instance { _layer: u32, } +impl Instance { + pub const MAX: usize = 1_000; +} + #[repr(C)] #[derive(Debug, Clone, Copy)] struct Uniforms { -- 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.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index afff52a6..1ffa50d2 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -319,6 +319,8 @@ impl Pipeline { let texture_version = self.texture_atlas.layer_count(); if self.texture_version != texture_version { + log::info!("Atlas has grown. Recreating bind group..."); + self.texture = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &self.texture_layout, @@ -525,12 +527,12 @@ fn add_instance( _position: position, _size: size, _position_in_atlas: [ - x as f32 / atlas::SIZE as f32, - y as f32 / atlas::SIZE as f32, + (x as f32 + 0.5) / atlas::SIZE as f32, + (y as f32 + 0.5) / atlas::SIZE as f32, ], _size_in_atlas: [ - width as f32 / atlas::SIZE as f32, - height as f32 / atlas::SIZE as f32, + (width as f32 - 0.5) / atlas::SIZE as f32, + (height as f32 - 0.5) / atlas::SIZE as f32, ], _layer: layer as u32, }; -- 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.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 1ffa50d2..7155b540 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -415,10 +415,10 @@ impl Pipeline { pub fn trim_cache(&mut self) { #[cfg(feature = "image")] - self.raster_cache.borrow_mut().trim(); + self.raster_cache.borrow_mut().trim(&mut self.texture_atlas); #[cfg(feature = "svg")] - self.vector_cache.borrow_mut().trim(); + self.vector_cache.borrow_mut().trim(&mut self.texture_atlas); } } @@ -531,8 +531,8 @@ fn add_instance( (y as f32 + 0.5) / atlas::SIZE as f32, ], _size_in_atlas: [ - (width as f32 - 0.5) / atlas::SIZE as f32, - (height as f32 - 0.5) / atlas::SIZE as f32, + (width as f32 - 1.0) / atlas::SIZE as f32, + (height as f32 - 1.0) / atlas::SIZE as f32, ], _layer: layer as u32, }; -- cgit From 6cb7fb6d52a25dc69f44c0ed1bd8d0254b6b213f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 26 Feb 2020 20:35:39 +0100 Subject: Remove unused code warnings in `iced_wgpu::image` --- wgpu/src/image.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 7155b540..dc19cfbf 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -3,13 +3,16 @@ mod raster; #[cfg(feature = "svg")] mod vector; -use crate::{ - texture::{self, atlas}, - Transformation, -}; +use crate::{texture, Transformation}; use iced_native::{image, svg, Rectangle}; -use std::{cell::RefCell, mem}; +use std::mem; + +#[cfg(any(feature = "image", feature = "svg"))] +use std::cell::RefCell; + +#[cfg(any(feature = "image", feature = "svg"))] +use crate::texture::atlas; #[derive(Debug)] pub struct Pipeline { @@ -271,7 +274,7 @@ impl Pipeline { target: &wgpu::TextureView, _scale: f32, ) { - let mut instances: Vec = Vec::new(); + let instances: &mut Vec = &mut Vec::new(); #[cfg(feature = "image")] let mut raster_cache = self.raster_cache.borrow_mut(); @@ -290,7 +293,7 @@ impl Pipeline { encoder, &mut self.texture_atlas, ) { - add_instances(image, atlas_entry, &mut instances); + add_instances(image, atlas_entry, instances); } }; } @@ -305,7 +308,7 @@ impl Pipeline { encoder, &mut self.texture_atlas, ) { - add_instances(image, atlas_entry, &mut instances); + add_instances(image, atlas_entry, instances); } }; } @@ -476,6 +479,7 @@ struct Uniforms { transform: [f32; 16], } +#[cfg(any(feature = "image", feature = "svg"))] fn add_instances( image: &Image, entry: &atlas::Entry, @@ -512,6 +516,7 @@ fn add_instances( } } +#[cfg(any(feature = "image", feature = "svg"))] #[inline] fn add_instance( position: [f32; 2], -- 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.rs | 74 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 36 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index dc19cfbf..d3603676 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -1,18 +1,23 @@ +mod atlas; + #[cfg(feature = "image")] mod raster; + #[cfg(feature = "svg")] mod vector; -use crate::{texture, Transformation}; +use crate::Transformation; +use atlas::Atlas; -use iced_native::{image, svg, Rectangle}; +use iced_native::Rectangle; +use std::cell::RefCell; use std::mem; -#[cfg(any(feature = "image", feature = "svg"))] -use std::cell::RefCell; +#[cfg(feature = "image")] +use iced_native::image; -#[cfg(any(feature = "image", feature = "svg"))] -use crate::texture::atlas; +#[cfg(feature = "svg")] +use iced_native::svg; #[derive(Debug)] pub struct Pipeline { @@ -30,7 +35,7 @@ pub struct Pipeline { texture: wgpu::BindGroup, texture_version: usize, texture_layout: wgpu::BindGroupLayout, - texture_atlas: texture::Atlas, + texture_atlas: Atlas, } impl Pipeline { @@ -216,7 +221,7 @@ impl Pipeline { usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, }); - let texture_atlas = texture::Atlas::new(device); + let texture_atlas = Atlas::new(device); let texture = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &texture_layout, @@ -284,33 +289,29 @@ impl Pipeline { for image in images { match &image.handle { - Handle::Raster(_handle) => { - #[cfg(feature = "image")] - { - if let Some(atlas_entry) = raster_cache.upload( - _handle, - device, - encoder, - &mut self.texture_atlas, - ) { - add_instances(image, atlas_entry, instances); - } - }; + #[cfg(feature = "image")] + Handle::Raster(handle) => { + if let Some(atlas_entry) = raster_cache.upload( + handle, + device, + encoder, + &mut self.texture_atlas, + ) { + add_instances(image, atlas_entry, instances); + } } - Handle::Vector(_handle) => { - #[cfg(feature = "svg")] - { - if let Some(atlas_entry) = vector_cache.upload( - _handle, - image.size, - _scale, - device, - encoder, - &mut self.texture_atlas, - ) { - add_instances(image, atlas_entry, instances); - } - }; + #[cfg(feature = "svg")] + Handle::Vector(handle) => { + if let Some(atlas_entry) = vector_cache.upload( + handle, + image.size, + _scale, + device, + encoder, + &mut self.texture_atlas, + ) { + add_instances(image, atlas_entry, instances); + } } } } @@ -432,7 +433,10 @@ pub struct Image { } pub enum Handle { + #[cfg(feature = "image")] Raster(image::Handle), + + #[cfg(feature = "svg")] Vector(svg::Handle), } @@ -479,7 +483,6 @@ struct Uniforms { transform: [f32; 16], } -#[cfg(any(feature = "image", feature = "svg"))] fn add_instances( image: &Image, entry: &atlas::Entry, @@ -516,7 +519,6 @@ fn add_instances( } } -#[cfg(any(feature = "image", feature = "svg"))] #[inline] fn add_instance( position: [f32; 2], -- cgit