Skip to main content

TextureAtlas

Struct TextureAtlas 

Source
pub struct TextureAtlas {
    pub width: u32,
    pub height: u32,
    /* private fields */
}
Expand description

Dynamic shelf-based texture atlas with LRU eviction.

§Allocation strategy

  1. Check the free-list for a first-fit region large enough.
  2. Walk existing shelves for one that can accommodate the request.
  3. Open a new shelf at the current bottom of the atlas.
  4. 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: u32

Atlas width in pixels.

§height: u32

Atlas height in pixels.

Implementations§

Source§

impl TextureAtlas

Source

pub fn new(width: u32, height: u32) -> Self

Construct a new empty atlas of the given dimensions.

Source

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.

Source

pub fn get(&self, h: AtlasHandle) -> Option<AtlasRect>

Retrieve the pixel rectangle for the given handle, if still live.

Source

pub fn utilization(&self) -> f32

Fraction of the atlas area occupied by live allocations.

Returns a value in [0.0, 1.0].

Source

pub fn allocation_count(&self) -> usize

Return the number of live (non-evicted) allocations.

Source

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).

Source

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.

Source

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 {}
}
Source

pub fn resize(&mut self, new_width: u32, new_height: u32)

Discard all allocations and reinitialise with new dimensions.

Any existing handles become invalid. Uploading updated GPU texture data is the caller’s responsibility.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,