diff options
Diffstat (limited to 'wgpu/src/image/atlas')
| -rw-r--r-- | wgpu/src/image/atlas/allocation.rs | 35 | ||||
| -rw-r--r-- | wgpu/src/image/atlas/allocator.rs | 69 | ||||
| -rw-r--r-- | wgpu/src/image/atlas/entry.rs | 26 | ||||
| -rw-r--r-- | wgpu/src/image/atlas/layer.rs | 17 | 
4 files changed, 147 insertions, 0 deletions
diff --git a/wgpu/src/image/atlas/allocation.rs b/wgpu/src/image/atlas/allocation.rs new file mode 100644 index 00000000..59b7239f --- /dev/null +++ b/wgpu/src/image/atlas/allocation.rs @@ -0,0 +1,35 @@ +use crate::image::atlas::{self, allocator}; + +#[derive(Debug)] +pub enum Allocation { +    Partial { +        layer: usize, +        region: allocator::Region, +    }, +    Full { +        layer: usize, +    }, +} + +impl Allocation { +    pub fn position(&self) -> (u32, u32) { +        match self { +            Allocation::Partial { region, .. } => region.position(), +            Allocation::Full { .. } => (0, 0), +        } +    } + +    pub fn size(&self) -> (u32, u32) { +        match self { +            Allocation::Partial { region, .. } => region.size(), +            Allocation::Full { .. } => (atlas::SIZE, atlas::SIZE), +        } +    } + +    pub fn layer(&self) -> usize { +        match self { +            Allocation::Partial { layer, .. } => *layer, +            Allocation::Full { layer } => *layer, +        } +    } +} diff --git a/wgpu/src/image/atlas/allocator.rs b/wgpu/src/image/atlas/allocator.rs new file mode 100644 index 00000000..7a4ff5b1 --- /dev/null +++ b/wgpu/src/image/atlas/allocator.rs @@ -0,0 +1,69 @@ +use guillotiere::{AtlasAllocator, Size}; + +pub struct Allocator { +    raw: AtlasAllocator, +    allocations: usize, +} + +impl Allocator { +    pub fn new(size: u32) -> Allocator { +        let raw = AtlasAllocator::new(Size::new(size as i32, size as i32)); + +        Allocator { +            raw, +            allocations: 0, +        } +    } + +    pub fn allocate(&mut self, width: u32, height: u32) -> Option<Region> { +        let allocation = +            self.raw.allocate(Size::new(width as i32, height as i32))?; + +        self.allocations += 1; + +        Some(Region { allocation }) +    } + +    pub fn deallocate(&mut self, region: &Region) { +        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 { +    allocation: guillotiere::Allocation, +} + +impl Region { +    pub fn position(&self) -> (u32, u32) { +        let rectangle = &self.allocation.rectangle; + +        (rectangle.min.x as u32, rectangle.min.y as u32) +    } + +    pub fn size(&self) -> (u32, u32) { +        let size = self.allocation.rectangle.size(); + +        (size.width as u32, size.height as u32) +    } +} + +impl std::fmt::Debug for Allocator { +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        write!(f, "Allocator") +    } +} + +impl std::fmt::Debug for Region { +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        f.debug_struct("Region") +            .field("id", &self.allocation.id) +            .field("rectangle", &self.allocation.rectangle) +            .finish() +    } +} diff --git a/wgpu/src/image/atlas/entry.rs b/wgpu/src/image/atlas/entry.rs new file mode 100644 index 00000000..0310fc54 --- /dev/null +++ b/wgpu/src/image/atlas/entry.rs @@ -0,0 +1,26 @@ +use crate::image::atlas; + +#[derive(Debug)] +pub enum Entry { +    Contiguous(atlas::Allocation), +    Fragmented { +        size: (u32, u32), +        fragments: Vec<Fragment>, +    }, +} + +impl Entry { +    #[cfg(feature = "image")] +    pub fn size(&self) -> (u32, u32) { +        match self { +            Entry::Contiguous(allocation) => allocation.size(), +            Entry::Fragmented { size, .. } => *size, +        } +    } +} + +#[derive(Debug)] +pub struct Fragment { +    pub position: (u32, u32), +    pub allocation: atlas::Allocation, +} diff --git a/wgpu/src/image/atlas/layer.rs b/wgpu/src/image/atlas/layer.rs new file mode 100644 index 00000000..b1084ed9 --- /dev/null +++ b/wgpu/src/image/atlas/layer.rs @@ -0,0 +1,17 @@ +use crate::image::atlas::Allocator; + +#[derive(Debug)] +pub enum Layer { +    Empty, +    Busy(Allocator), +    Full, +} + +impl Layer { +    pub fn is_empty(&self) -> bool { +        match self { +            Layer::Empty => true, +            _ => false, +        } +    } +}  | 
