Merged image and svg texture atlases into one owned by the image pipeline.
This commit is contained in:
parent
1bcfc9a5cc
commit
743637ebda
4 changed files with 90 additions and 158 deletions
|
|
@ -4,6 +4,7 @@ use std::{
|
|||
fmt,
|
||||
};
|
||||
use guillotiere::{Allocation, AtlasAllocator, Size};
|
||||
use debug_stub_derive::*;
|
||||
|
||||
pub enum Svg {
|
||||
Loaded { tree: resvg::usvg::Tree },
|
||||
|
|
@ -29,57 +30,22 @@ impl fmt::Debug for Svg {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(DebugStub)]
|
||||
pub struct Cache {
|
||||
allocator: AtlasAllocator,
|
||||
atlas: wgpu::Texture,
|
||||
svgs: HashMap<u64, Svg>,
|
||||
#[debug_stub="ReplacementValue"]
|
||||
rasterized: HashMap<(u64, u32, u32), Allocation>,
|
||||
svg_hits: HashSet<u64>,
|
||||
rasterized_hits: HashSet<(u64, u32, u32)>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Cache {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt.debug_struct("Vector Cache")
|
||||
.field("allocator", &String::from("AtlasAllocator"))
|
||||
.field("atlas", &self.atlas)
|
||||
.field("svgs", &self.svgs)
|
||||
.field("rasterized", &String::from("HashMap<(u64, u32, u32), Allocation>"))
|
||||
.field("svg_hits", &self.svg_hits)
|
||||
.field("rasterized_hits", &self.rasterized_hits)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
let (width, height) = (512, 512);
|
||||
|
||||
let extent = wgpu::Extent3d {
|
||||
width,
|
||||
height,
|
||||
depth: 1,
|
||||
};
|
||||
|
||||
let atlas = device.create_texture(&wgpu::TextureDescriptor {
|
||||
size: extent,
|
||||
array_layer_count: 1,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
usage: wgpu::TextureUsage::COPY_DST
|
||||
| wgpu::TextureUsage::COPY_SRC
|
||||
| wgpu::TextureUsage::SAMPLED,
|
||||
});
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
svgs: HashMap::new(),
|
||||
rasterized: HashMap::new(),
|
||||
svg_hits: HashSet::new(),
|
||||
rasterized_hits: HashSet::new(),
|
||||
allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)),
|
||||
atlas,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -99,10 +65,6 @@ impl Cache {
|
|||
self.svgs.get(&handle.id()).unwrap()
|
||||
}
|
||||
|
||||
pub fn atlas_size(&self) -> guillotiere::Size {
|
||||
self.allocator.size()
|
||||
}
|
||||
|
||||
pub fn upload(
|
||||
&mut self,
|
||||
handle: &svg::Handle,
|
||||
|
|
@ -110,6 +72,8 @@ impl Cache {
|
|||
scale: f32,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
allocator: &mut AtlasAllocator,
|
||||
atlas: &mut wgpu::Texture,
|
||||
) -> Option<&Allocation> {
|
||||
let id = handle.id();
|
||||
|
||||
|
|
@ -138,19 +102,19 @@ impl Cache {
|
|||
}
|
||||
|
||||
let size = Size::new(width as i32, height as i32);
|
||||
let old_atlas_size = self.allocator.size();
|
||||
let old_atlas_size = allocator.size();
|
||||
let allocation;
|
||||
|
||||
loop {
|
||||
if let Some(a) = self.allocator.allocate(size) {
|
||||
if let Some(a) = allocator.allocate(size) {
|
||||
allocation = a;
|
||||
break;
|
||||
}
|
||||
|
||||
self.allocator.grow(self.allocator.size() * 2);
|
||||
allocator.grow(allocator.size() * 2);
|
||||
}
|
||||
|
||||
let new_atlas_size = self.allocator.size();
|
||||
let new_atlas_size = allocator.size();
|
||||
|
||||
if new_atlas_size != old_atlas_size {
|
||||
let new_atlas = device.create_texture(&wgpu::TextureDescriptor {
|
||||
|
|
@ -171,7 +135,7 @@ impl Cache {
|
|||
|
||||
encoder.copy_texture_to_texture(
|
||||
wgpu::TextureCopyView {
|
||||
texture: &self.atlas,
|
||||
texture: atlas,
|
||||
array_layer: 0,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d {
|
||||
|
|
@ -197,7 +161,7 @@ impl Cache {
|
|||
}
|
||||
);
|
||||
|
||||
self.atlas = new_atlas;
|
||||
*atlas = new_atlas;
|
||||
}
|
||||
|
||||
// TODO: Optimize!
|
||||
|
|
@ -238,7 +202,7 @@ impl Cache {
|
|||
image_height: height as u32,
|
||||
},
|
||||
wgpu::TextureCopyView {
|
||||
texture: &self.atlas,
|
||||
texture: atlas,
|
||||
array_layer: 0,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d {
|
||||
|
|
@ -266,25 +230,13 @@ impl Cache {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn atlas(&self, device: &wgpu::Device, texture_layout: &wgpu::BindGroupLayout) -> wgpu::BindGroup {
|
||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: texture_layout,
|
||||
bindings: &[wgpu::Binding {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::TextureView(
|
||||
&self.atlas.create_default_view(),
|
||||
),
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
pub fn trim(&mut self) {
|
||||
pub fn trim(&mut self, allocator: &mut AtlasAllocator) {
|
||||
let svg_hits = &self.svg_hits;
|
||||
let rasterized_hits = &self.rasterized_hits;
|
||||
|
||||
for (k, alloc) in &mut self.rasterized {
|
||||
if !rasterized_hits.contains(&k) {
|
||||
self.allocator.deallocate(alloc.id);
|
||||
allocator.deallocate(alloc.id);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue