Refactor texture atlas

- Split into multiple modules
- Rename some concepts
- Change API details
This commit is contained in:
Héctor Ramón Jiménez 2020-02-26 12:34:34 +01:00
parent 82f0a49062
commit 59d45a5440
11 changed files with 647 additions and 667 deletions

View file

@ -1,12 +1,10 @@
use crate::image::{TextureArray, ImageAllocation};
use crate::texture::atlas::{self, Atlas};
use iced_native::image;
use std::{
collections::{HashMap, HashSet},
};
use std::collections::{HashMap, HashSet};
pub enum Memory {
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
Device(ImageAllocation),
Device(atlas::Entry),
NotFound,
Invalid,
}
@ -15,7 +13,7 @@ impl Memory {
pub fn dimensions(&self) -> (u32, u32) {
match self {
Memory::Host(image) => image.dimensions(),
Memory::Device(allocation) => allocation.size(),
Memory::Device(entry) => entry.size(),
Memory::NotFound => (1, 1),
Memory::Invalid => (1, 1),
}
@ -78,17 +76,26 @@ impl Cache {
handle: &image::Handle,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
atlas_array: &mut TextureArray,
) -> &Memory {
atlas: &mut Atlas,
) -> Option<&atlas::Entry> {
let memory = self.load(handle);
if let Memory::Host(image) = memory {
let allocation = atlas_array.upload(image, device, encoder);
let (width, height) = image.dimensions();
let allocation =
atlas.upload(width, height, &image, device, encoder)?;
dbg!("Uploaded");
*memory = Memory::Device(allocation);
}
memory
if let Memory::Device(allocation) = memory {
Some(allocation)
} else {
None
}
}
pub fn trim(&mut self) {

View file

@ -1,8 +1,6 @@
use crate::image::{TextureArray, ImageAllocation};
use crate::texture::atlas::{self, Atlas};
use iced_native::svg;
use std::{
collections::{HashMap, HashSet},
};
use std::collections::{HashMap, HashSet};
pub enum Svg {
Loaded(resvg::usvg::Tree),
@ -33,7 +31,7 @@ impl std::fmt::Debug for Svg {
pub struct Cache {
svgs: HashMap<u64, Svg>,
rasterized: HashMap<(u64, u32, u32), ImageAllocation>,
rasterized: HashMap<(u64, u32, u32), atlas::Entry>,
svg_hits: HashSet<u64>,
rasterized_hits: HashSet<(u64, u32, u32)>,
}
@ -71,8 +69,8 @@ impl Cache {
scale: f32,
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
texture_array: &mut TextureArray,
) -> Option<&ImageAllocation> {
texture_atlas: &mut Atlas,
) -> Option<&atlas::Entry> {
let id = handle.id();
let (width, height) = (
@ -104,10 +102,8 @@ impl Cache {
let screen_size =
resvg::ScreenSize::new(width, height).unwrap();
let mut canvas = resvg::raqote::DrawTarget::new(
width as i32,
height as i32,
);
let mut canvas =
resvg::raqote::DrawTarget::new(width as i32, height as i32);
resvg::backend_raqote::render_to_canvas(
tree,
@ -116,17 +112,21 @@ impl Cache {
&mut canvas,
);
let allocation = texture_array.upload(&canvas, device, encoder);
let allocation = texture_atlas.upload(
width,
height,
canvas.get_data(),
device,
encoder,
)?;
let _ = self.svg_hits.insert(id);
let _ = self.rasterized_hits.insert((id, width, height));
let _ = self
.rasterized
.insert((id, width, height), allocation);
let _ = self.rasterized.insert((id, width, height), allocation);
self.rasterized.get(&(id, width, height))
}
Svg::NotFound => None
Svg::NotFound => None,
}
}
@ -145,4 +145,4 @@ impl std::fmt::Debug for Cache {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "vector::Cache")
}
}
}