Merged image and svg texture atlases into one owned by the image pipeline.
This commit is contained in:
parent
1bcfc9a5cc
commit
743637ebda
@ -22,6 +22,7 @@ glam = "0.8"
|
|||||||
font-kit = "0.4"
|
font-kit = "0.4"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
guillotiere = "0.4"
|
guillotiere = "0.4"
|
||||||
|
debug_stub_derive = "0.3"
|
||||||
|
|
||||||
[dependencies.image]
|
[dependencies.image]
|
||||||
version = "0.22"
|
version = "0.22"
|
||||||
|
@ -14,7 +14,10 @@ use std::mem;
|
|||||||
#[cfg(any(feature = "image", feature = "svg"))]
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
use std::{cell::RefCell, collections::HashMap};
|
use std::{cell::RefCell, collections::HashMap};
|
||||||
|
|
||||||
#[derive(Debug)]
|
use guillotiere::{AtlasAllocator, Size};
|
||||||
|
use debug_stub_derive::*;
|
||||||
|
|
||||||
|
#[derive(DebugStub)]
|
||||||
pub struct Pipeline {
|
pub struct Pipeline {
|
||||||
#[cfg(feature = "image")]
|
#[cfg(feature = "image")]
|
||||||
raster_cache: RefCell<raster::Cache>,
|
raster_cache: RefCell<raster::Cache>,
|
||||||
@ -28,6 +31,9 @@ pub struct Pipeline {
|
|||||||
instances: wgpu::Buffer,
|
instances: wgpu::Buffer,
|
||||||
constants: wgpu::BindGroup,
|
constants: wgpu::BindGroup,
|
||||||
texture_layout: wgpu::BindGroupLayout,
|
texture_layout: wgpu::BindGroupLayout,
|
||||||
|
#[debug_stub="ReplacementValue"]
|
||||||
|
allocator: AtlasAllocator,
|
||||||
|
atlas: wgpu::Texture,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pipeline {
|
impl Pipeline {
|
||||||
@ -208,12 +214,32 @@ impl Pipeline {
|
|||||||
usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
|
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 {
|
Pipeline {
|
||||||
#[cfg(feature = "image")]
|
#[cfg(feature = "image")]
|
||||||
raster_cache: RefCell::new(raster::Cache::new(&device)),
|
raster_cache: RefCell::new(raster::Cache::new()),
|
||||||
|
|
||||||
#[cfg(feature = "svg")]
|
#[cfg(feature = "svg")]
|
||||||
vector_cache: RefCell::new(vector::Cache::new(&device)),
|
vector_cache: RefCell::new(vector::Cache::new()),
|
||||||
|
|
||||||
pipeline,
|
pipeline,
|
||||||
uniforms: uniforms_buffer,
|
uniforms: uniforms_buffer,
|
||||||
@ -222,6 +248,8 @@ impl Pipeline {
|
|||||||
instances,
|
instances,
|
||||||
constants: constant_bind_group,
|
constants: constant_bind_group,
|
||||||
texture_layout,
|
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();
|
let mut raster_cache = self.raster_cache.borrow_mut();
|
||||||
|
|
||||||
if let Memory::Device(allocation) = raster_cache.upload(
|
if let Memory::Device(allocation) = raster_cache.upload(
|
||||||
_handle,
|
handle,
|
||||||
device,
|
device,
|
||||||
encoder)
|
encoder,
|
||||||
{
|
&mut self.allocator,
|
||||||
|
&mut self.atlas
|
||||||
|
) {
|
||||||
let rec = allocation.rectangle;
|
let rec = allocation.rectangle;
|
||||||
|
|
||||||
let _ = recs.insert(index, rec);
|
let _ = recs.insert(index, rec);
|
||||||
@ -291,12 +321,15 @@ impl Pipeline {
|
|||||||
{
|
{
|
||||||
let mut vector_cache = self.vector_cache.borrow_mut();
|
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(allocation) = vector_cache.upload(
|
||||||
_handle,
|
_handle,
|
||||||
image.scale,
|
image.scale,
|
||||||
_scale,
|
_scale,
|
||||||
device,
|
device,
|
||||||
encoder,
|
encoder,
|
||||||
|
&mut self.allocator,
|
||||||
|
&mut self.atlas,
|
||||||
) {
|
) {
|
||||||
let rec = allocation.rectangle;
|
let rec = allocation.rectangle;
|
||||||
|
|
||||||
@ -307,30 +340,34 @@ impl Pipeline {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "image")]
|
let atlas_width = self.allocator.size().width as f32;
|
||||||
let raster_atlas = self.raster_cache.borrow().atlas(device, &self.texture_layout);
|
let atlas_height = self.allocator.size().height as f32;
|
||||||
|
|
||||||
#[cfg(feature = "svg")]
|
let texture = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
let vector_atlas = self.vector_cache.borrow().atlas(device, &self.texture_layout);
|
layout: &self.texture_layout,
|
||||||
|
bindings: &[wgpu::Binding {
|
||||||
|
binding: 0,
|
||||||
|
resource: wgpu::BindingResource::TextureView(
|
||||||
|
&self.atlas.create_default_view(),
|
||||||
|
),
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
#[cfg(any(feature = "image", feature = "svg"))]
|
#[cfg(any(feature = "image", feature = "svg"))]
|
||||||
for (index, image) in instances.iter().enumerate() {
|
for (index, image) in instances.iter().enumerate() {
|
||||||
if let Some(rec) = recs.get(&index) {
|
if let Some(rec) = recs.get(&index) {
|
||||||
let atlas_size = match image.handle {
|
let x = rec.min.x as f32 / atlas_width;
|
||||||
#[cfg(feature = "image")]
|
let y = rec.min.y as f32 / atlas_height;
|
||||||
Handle::Raster(_) => self.raster_cache.borrow().atlas_size(),
|
let w = (rec.size().width - 1) as f32 / atlas_width;
|
||||||
#[cfg(feature = "svg")]
|
let h = (rec.size().height - 1) as f32 / atlas_height;
|
||||||
Handle::Vector(_) => self.vector_cache.borrow().atlas_size(),
|
|
||||||
_ => guillotiere::Size::new(0, 0)
|
|
||||||
};
|
|
||||||
|
|
||||||
let instance_buffer = device
|
let instance_buffer = device
|
||||||
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
||||||
.fill_from_slice(&[Instance {
|
.fill_from_slice(&[Instance {
|
||||||
_position: image.position,
|
_position: image.position,
|
||||||
_scale: image.scale,
|
_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],
|
_position_in_atlas: [x, y],
|
||||||
_scale_in_atlas: [rec.size().width as f32 / atlas_size.width as f32, rec.size().height as f32 / atlas_size.height as f32]
|
_scale_in_atlas: [w, h]
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
encoder.copy_buffer_to_buffer(
|
encoder.copy_buffer_to_buffer(
|
||||||
@ -341,17 +378,6 @@ impl Pipeline {
|
|||||||
mem::size_of::<Instance>() as u64,
|
mem::size_of::<Instance>() 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(
|
let mut render_pass = encoder.begin_render_pass(
|
||||||
&wgpu::RenderPassDescriptor {
|
&wgpu::RenderPassDescriptor {
|
||||||
color_attachments: &[
|
color_attachments: &[
|
||||||
@ -398,10 +424,10 @@ impl Pipeline {
|
|||||||
|
|
||||||
pub fn trim_cache(&mut self) {
|
pub fn trim_cache(&mut self) {
|
||||||
#[cfg(feature = "image")]
|
#[cfg(feature = "image")]
|
||||||
self.raster_cache.borrow_mut().trim();
|
self.raster_cache.borrow_mut().trim(&mut self.allocator);
|
||||||
|
|
||||||
#[cfg(feature = "svg")]
|
#[cfg(feature = "svg")]
|
||||||
self.vector_cache.borrow_mut().trim();
|
self.vector_cache.borrow_mut().trim(&mut self.allocator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
use iced_native::image;
|
use iced_native::image;
|
||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
fmt,
|
|
||||||
};
|
};
|
||||||
use guillotiere::{Allocation, AtlasAllocator, Size};
|
use guillotiere::{Allocation, AtlasAllocator, Size};
|
||||||
|
use debug_stub_derive::*;
|
||||||
|
|
||||||
|
#[derive(DebugStub)]
|
||||||
pub enum Memory {
|
pub enum Memory {
|
||||||
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
|
Host(::image::ImageBuffer<::image::Bgra<u8>, Vec<u8>>),
|
||||||
Device(Allocation),
|
Device(#[debug_stub="ReplacementValue"]Allocation),
|
||||||
NotFound,
|
NotFound,
|
||||||
Invalid,
|
Invalid,
|
||||||
}
|
}
|
||||||
@ -26,49 +27,15 @@ impl Memory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Cache {
|
pub struct Cache {
|
||||||
allocator: AtlasAllocator,
|
|
||||||
atlas: wgpu::Texture,
|
|
||||||
map: HashMap<u64, Memory>,
|
map: HashMap<u64, Memory>,
|
||||||
hits: HashSet<u64>,
|
hits: HashSet<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Cache {
|
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
fmt.debug_struct("Vector Cache")
|
|
||||||
.field("allocator", &String::from("AtlasAllocator"))
|
|
||||||
.field("atlas", &self.atlas)
|
|
||||||
.field("map", &String::from("HashMap<u64, Memory>"))
|
|
||||||
.field("hits", &self.hits)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Cache {
|
impl Cache {
|
||||||
pub fn new(device: &wgpu::Device) -> Self {
|
pub fn new() -> Self {
|
||||||
let (width, height) = (1000, 1000);
|
|
||||||
|
|
||||||
let extent = wgpu::Extent3d {
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
depth: 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
let atlas = device.create_texture(&wgpu::TextureDescriptor {
|
|
||||||
size: extent,
|
|
||||||
array_layer_count: 1,
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
||||||
usage: wgpu::TextureUsage::COPY_DST
|
|
||||||
| wgpu::TextureUsage::COPY_SRC
|
|
||||||
| wgpu::TextureUsage::SAMPLED,
|
|
||||||
});
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
allocator: AtlasAllocator::new(Size::new(width as i32, height as i32)),
|
|
||||||
atlas,
|
|
||||||
map: HashMap::new(),
|
map: HashMap::new(),
|
||||||
hits: HashSet::new(),
|
hits: HashSet::new(),
|
||||||
}
|
}
|
||||||
@ -100,15 +67,13 @@ impl Cache {
|
|||||||
self.get(handle).unwrap()
|
self.get(handle).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn atlas_size(&self) -> guillotiere::Size {
|
|
||||||
self.allocator.size()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn upload(
|
pub fn upload(
|
||||||
&mut self,
|
&mut self,
|
||||||
handle: &image::Handle,
|
handle: &image::Handle,
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
|
allocator: &mut AtlasAllocator,
|
||||||
|
atlas: &mut wgpu::Texture,
|
||||||
) -> &Memory {
|
) -> &Memory {
|
||||||
let _ = self.load(handle);
|
let _ = self.load(handle);
|
||||||
|
|
||||||
@ -118,19 +83,19 @@ impl Cache {
|
|||||||
let (width, height) = image.dimensions();
|
let (width, height) = image.dimensions();
|
||||||
let size = Size::new(width as i32, height as i32);
|
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;
|
let allocation;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(a) = self.allocator.allocate(size) {
|
if let Some(a) = allocator.allocate(size) {
|
||||||
allocation = a;
|
allocation = a;
|
||||||
break;
|
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 {
|
if new_atlas_size != old_atlas_size {
|
||||||
let new_atlas = device.create_texture(&wgpu::TextureDescriptor {
|
let new_atlas = device.create_texture(&wgpu::TextureDescriptor {
|
||||||
@ -151,7 +116,7 @@ impl Cache {
|
|||||||
|
|
||||||
encoder.copy_texture_to_texture(
|
encoder.copy_texture_to_texture(
|
||||||
wgpu::TextureCopyView {
|
wgpu::TextureCopyView {
|
||||||
texture: &self.atlas,
|
texture: atlas,
|
||||||
array_layer: 0,
|
array_layer: 0,
|
||||||
mip_level: 0,
|
mip_level: 0,
|
||||||
origin: wgpu::Origin3d {
|
origin: wgpu::Origin3d {
|
||||||
@ -177,7 +142,7 @@ impl Cache {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
self.atlas = new_atlas;
|
*atlas = new_atlas;
|
||||||
}
|
}
|
||||||
|
|
||||||
let extent = wgpu::Extent3d {
|
let extent = wgpu::Extent3d {
|
||||||
@ -206,7 +171,7 @@ impl Cache {
|
|||||||
image_height: height,
|
image_height: height,
|
||||||
},
|
},
|
||||||
wgpu::TextureCopyView {
|
wgpu::TextureCopyView {
|
||||||
texture: &self.atlas,
|
texture: atlas,
|
||||||
array_layer: 0,
|
array_layer: 0,
|
||||||
mip_level: 0,
|
mip_level: 0,
|
||||||
origin: wgpu::Origin3d {
|
origin: wgpu::Origin3d {
|
||||||
@ -224,25 +189,13 @@ impl Cache {
|
|||||||
memory
|
memory
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn atlas(&self, device: &wgpu::Device, texture_layout: &wgpu::BindGroupLayout) -> wgpu::BindGroup {
|
pub fn trim(&mut self, allocator: &mut AtlasAllocator) {
|
||||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
layout: texture_layout,
|
|
||||||
bindings: &[wgpu::Binding {
|
|
||||||
binding: 0,
|
|
||||||
resource: wgpu::BindingResource::TextureView(
|
|
||||||
&self.atlas.create_default_view(),
|
|
||||||
),
|
|
||||||
}],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn trim(&mut self) {
|
|
||||||
let hits = &self.hits;
|
let hits = &self.hits;
|
||||||
|
|
||||||
for (id, mem) in &mut self.map {
|
for (id, mem) in &mut self.map {
|
||||||
if let Memory::Device(allocation) = mem {
|
if let Memory::Device(allocation) = mem {
|
||||||
if !hits.contains(&id) {
|
if !hits.contains(&id) {
|
||||||
self.allocator.deallocate(allocation.id);
|
allocator.deallocate(allocation.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ use std::{
|
|||||||
fmt,
|
fmt,
|
||||||
};
|
};
|
||||||
use guillotiere::{Allocation, AtlasAllocator, Size};
|
use guillotiere::{Allocation, AtlasAllocator, Size};
|
||||||
|
use debug_stub_derive::*;
|
||||||
|
|
||||||
pub enum Svg {
|
pub enum Svg {
|
||||||
Loaded { tree: resvg::usvg::Tree },
|
Loaded { tree: resvg::usvg::Tree },
|
||||||
@ -29,57 +30,22 @@ impl fmt::Debug for Svg {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(DebugStub)]
|
||||||
pub struct Cache {
|
pub struct Cache {
|
||||||
allocator: AtlasAllocator,
|
|
||||||
atlas: wgpu::Texture,
|
|
||||||
svgs: HashMap<u64, Svg>,
|
svgs: HashMap<u64, Svg>,
|
||||||
|
#[debug_stub="ReplacementValue"]
|
||||||
rasterized: HashMap<(u64, u32, u32), Allocation>,
|
rasterized: HashMap<(u64, u32, u32), Allocation>,
|
||||||
svg_hits: HashSet<u64>,
|
svg_hits: HashSet<u64>,
|
||||||
rasterized_hits: HashSet<(u64, u32, u32)>,
|
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 {
|
impl Cache {
|
||||||
pub fn new(device: &wgpu::Device) -> Self {
|
pub fn new() -> Self {
|
||||||
let (width, height) = (512, 512);
|
|
||||||
|
|
||||||
let extent = wgpu::Extent3d {
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
depth: 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
let atlas = device.create_texture(&wgpu::TextureDescriptor {
|
|
||||||
size: extent,
|
|
||||||
array_layer_count: 1,
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
||||||
usage: wgpu::TextureUsage::COPY_DST
|
|
||||||
| wgpu::TextureUsage::COPY_SRC
|
|
||||||
| wgpu::TextureUsage::SAMPLED,
|
|
||||||
});
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
svgs: HashMap::new(),
|
svgs: HashMap::new(),
|
||||||
rasterized: HashMap::new(),
|
rasterized: HashMap::new(),
|
||||||
svg_hits: HashSet::new(),
|
svg_hits: HashSet::new(),
|
||||||
rasterized_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()
|
self.svgs.get(&handle.id()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn atlas_size(&self) -> guillotiere::Size {
|
|
||||||
self.allocator.size()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn upload(
|
pub fn upload(
|
||||||
&mut self,
|
&mut self,
|
||||||
handle: &svg::Handle,
|
handle: &svg::Handle,
|
||||||
@ -110,6 +72,8 @@ impl Cache {
|
|||||||
scale: f32,
|
scale: f32,
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
|
allocator: &mut AtlasAllocator,
|
||||||
|
atlas: &mut wgpu::Texture,
|
||||||
) -> Option<&Allocation> {
|
) -> Option<&Allocation> {
|
||||||
let id = handle.id();
|
let id = handle.id();
|
||||||
|
|
||||||
@ -138,19 +102,19 @@ impl Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let size = Size::new(width as i32, height as i32);
|
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;
|
let allocation;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(a) = self.allocator.allocate(size) {
|
if let Some(a) = allocator.allocate(size) {
|
||||||
allocation = a;
|
allocation = a;
|
||||||
break;
|
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 {
|
if new_atlas_size != old_atlas_size {
|
||||||
let new_atlas = device.create_texture(&wgpu::TextureDescriptor {
|
let new_atlas = device.create_texture(&wgpu::TextureDescriptor {
|
||||||
@ -171,7 +135,7 @@ impl Cache {
|
|||||||
|
|
||||||
encoder.copy_texture_to_texture(
|
encoder.copy_texture_to_texture(
|
||||||
wgpu::TextureCopyView {
|
wgpu::TextureCopyView {
|
||||||
texture: &self.atlas,
|
texture: atlas,
|
||||||
array_layer: 0,
|
array_layer: 0,
|
||||||
mip_level: 0,
|
mip_level: 0,
|
||||||
origin: wgpu::Origin3d {
|
origin: wgpu::Origin3d {
|
||||||
@ -197,7 +161,7 @@ impl Cache {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
self.atlas = new_atlas;
|
*atlas = new_atlas;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Optimize!
|
// TODO: Optimize!
|
||||||
@ -238,7 +202,7 @@ impl Cache {
|
|||||||
image_height: height as u32,
|
image_height: height as u32,
|
||||||
},
|
},
|
||||||
wgpu::TextureCopyView {
|
wgpu::TextureCopyView {
|
||||||
texture: &self.atlas,
|
texture: atlas,
|
||||||
array_layer: 0,
|
array_layer: 0,
|
||||||
mip_level: 0,
|
mip_level: 0,
|
||||||
origin: wgpu::Origin3d {
|
origin: wgpu::Origin3d {
|
||||||
@ -266,25 +230,13 @@ impl Cache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn atlas(&self, device: &wgpu::Device, texture_layout: &wgpu::BindGroupLayout) -> wgpu::BindGroup {
|
pub fn trim(&mut self, allocator: &mut AtlasAllocator) {
|
||||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
|
||||||
layout: texture_layout,
|
|
||||||
bindings: &[wgpu::Binding {
|
|
||||||
binding: 0,
|
|
||||||
resource: wgpu::BindingResource::TextureView(
|
|
||||||
&self.atlas.create_default_view(),
|
|
||||||
),
|
|
||||||
}],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn trim(&mut self) {
|
|
||||||
let svg_hits = &self.svg_hits;
|
let svg_hits = &self.svg_hits;
|
||||||
let rasterized_hits = &self.rasterized_hits;
|
let rasterized_hits = &self.rasterized_hits;
|
||||||
|
|
||||||
for (k, alloc) in &mut self.rasterized {
|
for (k, alloc) in &mut self.rasterized {
|
||||||
if !rasterized_hits.contains(&k) {
|
if !rasterized_hits.contains(&k) {
|
||||||
self.allocator.deallocate(alloc.id);
|
allocator.deallocate(alloc.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user