pub struct TextureAtlas {
pub width: u32,
pub height: u32,
/* private fields */
}Expand description
Dynamic shelf-based texture atlas with LRU eviction.
§Allocation strategy
- Check the free-list for a first-fit region large enough.
- Walk existing shelves for one that can accommodate the request.
- Open a new shelf at the current bottom of the atlas.
- If all of the above fail, evict the LRU handle and retry once.
§Eviction
Evicted regions are pushed onto the free-list; they are re-used on the next allocation attempt. The atlas never compacts its shelf layout.
Fields§
§width: u32Atlas width in pixels.
height: u32Atlas height in pixels.
Implementations§
Source§impl TextureAtlas
impl TextureAtlas
Sourcepub fn new(width: u32, height: u32) -> Self
pub fn new(width: u32, height: u32) -> Self
Construct a new empty atlas of the given dimensions.
Sourcepub fn insert(&mut self, w: u32, h: u32) -> Option<AtlasHandle>
pub fn insert(&mut self, w: u32, h: u32) -> Option<AtlasHandle>
Insert a rectangle of size w × h into the atlas.
Returns an AtlasHandle on success, or None if the rectangle is
larger than the atlas or all eviction attempts were exhausted.
Sourcepub fn get(&self, h: AtlasHandle) -> Option<AtlasRect>
pub fn get(&self, h: AtlasHandle) -> Option<AtlasRect>
Retrieve the pixel rectangle for the given handle, if still live.
Sourcepub fn utilization(&self) -> f32
pub fn utilization(&self) -> f32
Fraction of the atlas area occupied by live allocations.
Returns a value in [0.0, 1.0].
Sourcepub fn allocation_count(&self) -> usize
pub fn allocation_count(&self) -> usize
Return the number of live (non-evicted) allocations.
Sourcepub fn is_fragmented(&self, threshold: f32) -> bool
pub fn is_fragmented(&self, threshold: f32) -> bool
Return true if atlas utilization has fallen below threshold
(0.0–1.0). This indicates significant fragmentation and the caller
should consider triggering a defrag/rebuild.
This check is cheap (a single float comparison).
Sourcepub fn defrag(&mut self) -> Vec<(AtlasHandle, AtlasRect)>
pub fn defrag(&mut self) -> Vec<(AtlasHandle, AtlasRect)>
Rebuild the atlas layout from scratch using only the currently live allocations.
After a defrag, all previously live handles continue to resolve to valid (but potentially repositioned) rectangles. Handles that had already been evicted remain invalid. The GPU texture content is not updated by this call — the caller must re-upload all live texture regions.
Returns a Vec<(AtlasHandle, AtlasRect)> mapping each live handle to
its new position, in insertion order. The caller uses this to schedule
GPU texture copies.
§Complexity
O(n log n) in the number of live allocations.
Sourcepub fn defrag_if_fragmented(
&mut self,
threshold: f32,
) -> Option<Vec<(AtlasHandle, AtlasRect)>>
pub fn defrag_if_fragmented( &mut self, threshold: f32, ) -> Option<Vec<(AtlasHandle, AtlasRect)>>
Check whether utilization is below the given threshold and, if so,
perform an in-place defrag.
Returns Some(relocations) if a defrag was performed, None if
utilization was already above the threshold.
§Example
let mut atlas = TextureAtlas::new(64, 64);
// … insert/evict until fragmented …
if let Some(moves) = atlas.defrag_if_fragmented(0.5) {
// re-upload the relocated regions
for (_handle, _new_rect) in moves {}
}Auto Trait Implementations§
impl Freeze for TextureAtlas
impl RefUnwindSafe for TextureAtlas
impl Send for TextureAtlas
impl Sync for TextureAtlas
impl Unpin for TextureAtlas
impl UnsafeUnpin for TextureAtlas
impl UnwindSafe for TextureAtlas
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more