Deallocate atlas entries and remove padding
This commit is contained in:
parent
48d70280eb
commit
d06d06e050
@ -415,10 +415,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.texture_atlas);
|
||||||
|
|
||||||
#[cfg(feature = "svg")]
|
#[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,
|
(y as f32 + 0.5) / atlas::SIZE as f32,
|
||||||
],
|
],
|
||||||
_size_in_atlas: [
|
_size_in_atlas: [
|
||||||
(width as f32 - 0.5) / atlas::SIZE as f32,
|
(width as f32 - 1.0) / atlas::SIZE as f32,
|
||||||
(height as f32 - 0.5) / atlas::SIZE as f32,
|
(height as f32 - 1.0) / atlas::SIZE as f32,
|
||||||
],
|
],
|
||||||
_layer: layer as u32,
|
_layer: layer as u32,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -95,10 +95,21 @@ impl Cache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trim(&mut self) {
|
pub fn trim(&mut self, atlas: &mut Atlas) {
|
||||||
let hits = &self.hits;
|
let hits = &self.hits;
|
||||||
|
|
||||||
self.map.retain(|k, _| hits.contains(k));
|
self.map.retain(|k, memory| {
|
||||||
|
let retain = hits.contains(k);
|
||||||
|
|
||||||
|
if !retain {
|
||||||
|
if let Memory::Device(entry) = memory {
|
||||||
|
atlas.remove(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
retain
|
||||||
|
});
|
||||||
|
|
||||||
self.hits.clear();
|
self.hits.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -130,12 +130,20 @@ impl Cache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn trim(&mut self) {
|
pub fn trim(&mut self, atlas: &mut Atlas) {
|
||||||
let svg_hits = &self.svg_hits;
|
let svg_hits = &self.svg_hits;
|
||||||
let rasterized_hits = &self.rasterized_hits;
|
let rasterized_hits = &self.rasterized_hits;
|
||||||
|
|
||||||
self.svgs.retain(|k, _| svg_hits.contains(k));
|
self.svgs.retain(|k, _| svg_hits.contains(k));
|
||||||
self.rasterized.retain(|k, _| rasterized_hits.contains(k));
|
self.rasterized.retain(|k, entry| {
|
||||||
|
let retain = rasterized_hits.contains(k);
|
||||||
|
|
||||||
|
if !retain {
|
||||||
|
atlas.remove(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
retain
|
||||||
|
});
|
||||||
self.svg_hits.clear();
|
self.svg_hits.clear();
|
||||||
self.rasterized_hits.clear();
|
self.rasterized_hits.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -112,11 +112,47 @@ impl Atlas {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info!("Current atlas: {:?}", &self);
|
log::info!("Current atlas: {:?}", self);
|
||||||
|
|
||||||
Some(entry)
|
Some(entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove(&mut self, entry: &Entry) {
|
||||||
|
log::info!("Removing atlas entry: {:?}", entry);
|
||||||
|
|
||||||
|
match entry {
|
||||||
|
Entry::Contiguous(allocation) => {
|
||||||
|
self.deallocate(allocation);
|
||||||
|
}
|
||||||
|
Entry::Fragmented { fragments, .. } => {
|
||||||
|
for fragment in fragments {
|
||||||
|
self.deallocate(&fragment.allocation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deallocate(&mut self, allocation: &Allocation) {
|
||||||
|
log::info!("Deallocating atlas: {:?}", allocation);
|
||||||
|
|
||||||
|
match allocation {
|
||||||
|
Allocation::Full { layer } => {
|
||||||
|
self.layers[*layer] = Layer::Empty;
|
||||||
|
}
|
||||||
|
Allocation::Partial { layer, region } => {
|
||||||
|
let layer = &mut self.layers[*layer];
|
||||||
|
|
||||||
|
if let Layer::Busy(allocator) = layer {
|
||||||
|
allocator.deallocate(region);
|
||||||
|
|
||||||
|
if allocator.is_empty() {
|
||||||
|
*layer = Layer::Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn allocate(&mut self, width: u32, height: u32) -> Option<Entry> {
|
fn allocate(&mut self, width: u32, height: u32) -> Option<Entry> {
|
||||||
// Allocate one layer if texture fits perfectly
|
// Allocate one layer if texture fits perfectly
|
||||||
if width == SIZE && height == SIZE {
|
if width == SIZE && height == SIZE {
|
||||||
|
|||||||
@ -2,70 +2,54 @@ use guillotiere::{AtlasAllocator, Size};
|
|||||||
|
|
||||||
pub struct Allocator {
|
pub struct Allocator {
|
||||||
raw: AtlasAllocator,
|
raw: AtlasAllocator,
|
||||||
size: u32,
|
allocations: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Allocator {
|
impl Allocator {
|
||||||
const PADDING: u32 = 1;
|
|
||||||
|
|
||||||
pub fn new(size: u32) -> Allocator {
|
pub fn new(size: u32) -> Allocator {
|
||||||
let raw = AtlasAllocator::new(Size::new(size as i32, size as i32));
|
let raw = AtlasAllocator::new(Size::new(size as i32, size as i32));
|
||||||
|
|
||||||
Allocator { raw, size }
|
Allocator {
|
||||||
|
raw,
|
||||||
|
allocations: 0,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn allocate(&mut self, width: u32, height: u32) -> Option<Region> {
|
pub fn allocate(&mut self, width: u32, height: u32) -> Option<Region> {
|
||||||
let padding = (
|
let allocation =
|
||||||
if width + Self::PADDING * 2 < self.size {
|
self.raw.allocate(Size::new(width as i32, height as i32))?;
|
||||||
Self::PADDING
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
},
|
|
||||||
if height + Self::PADDING * 2 < self.size {
|
|
||||||
Self::PADDING
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
let allocation = self.raw.allocate(Size::new(
|
self.allocations += 1;
|
||||||
(width + padding.0 * 2) as i32,
|
|
||||||
(height + padding.1 * 2) as i32,
|
|
||||||
))?;
|
|
||||||
|
|
||||||
Some(Region {
|
Some(Region { allocation })
|
||||||
allocation,
|
|
||||||
padding,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deallocate(&mut self, region: Region) {
|
pub fn deallocate(&mut self, region: &Region) {
|
||||||
self.raw.deallocate(region.allocation.id);
|
self.raw.deallocate(region.allocation.id);
|
||||||
|
|
||||||
|
self.allocations = self.allocations.saturating_sub(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.allocations == 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Region {
|
pub struct Region {
|
||||||
allocation: guillotiere::Allocation,
|
allocation: guillotiere::Allocation,
|
||||||
padding: (u32, u32),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Region {
|
impl Region {
|
||||||
pub fn position(&self) -> (u32, u32) {
|
pub fn position(&self) -> (u32, u32) {
|
||||||
let rectangle = &self.allocation.rectangle;
|
let rectangle = &self.allocation.rectangle;
|
||||||
|
|
||||||
(
|
(rectangle.min.x as u32, rectangle.min.y as u32)
|
||||||
rectangle.min.x as u32 + self.padding.0,
|
|
||||||
rectangle.min.y as u32 + self.padding.1,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn size(&self) -> (u32, u32) {
|
pub fn size(&self) -> (u32, u32) {
|
||||||
let size = self.allocation.rectangle.size();
|
let size = self.allocation.rectangle.size();
|
||||||
|
|
||||||
(
|
(size.width as u32, size.height as u32)
|
||||||
size.width as u32 - self.padding.0 * 2,
|
|
||||||
size.height as u32 - self.padding.1 * 2,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +64,6 @@ impl std::fmt::Debug for Region {
|
|||||||
f.debug_struct("Region")
|
f.debug_struct("Region")
|
||||||
.field("id", &self.allocation.id)
|
.field("id", &self.allocation.id)
|
||||||
.field("rectangle", &self.allocation.rectangle)
|
.field("rectangle", &self.allocation.rectangle)
|
||||||
.field("padding", &self.padding)
|
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user