Refactor texture atlas
- Split into multiple modules - Rename some concepts - Change API details
This commit is contained in:
parent
82f0a49062
commit
59d45a5440
11 changed files with 647 additions and 667 deletions
35
wgpu/src/texture/atlas/allocation.rs
Normal file
35
wgpu/src/texture/atlas/allocation.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use crate::texture::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,
|
||||
}
|
||||
}
|
||||
}
|
||||
57
wgpu/src/texture/atlas/allocator.rs
Normal file
57
wgpu/src/texture/atlas/allocator.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use guillotiere::{AtlasAllocator, Size};
|
||||
|
||||
pub struct Allocator {
|
||||
raw: AtlasAllocator,
|
||||
}
|
||||
|
||||
impl Allocator {
|
||||
pub fn new(size: u32) -> Allocator {
|
||||
let raw = AtlasAllocator::new(Size::new(size as i32, size as i32));
|
||||
|
||||
Allocator { raw }
|
||||
}
|
||||
|
||||
pub fn allocate(&mut self, width: u32, height: u32) -> Option<Region> {
|
||||
let allocation = self
|
||||
.raw
|
||||
.allocate(Size::new(width as i32 + 2, height as i32 + 2))?;
|
||||
|
||||
Some(Region(allocation))
|
||||
}
|
||||
|
||||
pub fn deallocate(&mut self, region: Region) {
|
||||
self.raw.deallocate(region.0.id);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Region(guillotiere::Allocation);
|
||||
|
||||
impl Region {
|
||||
pub fn position(&self) -> (u32, u32) {
|
||||
let rectangle = &self.0.rectangle;
|
||||
|
||||
(rectangle.min.x as u32 + 1, rectangle.min.y as u32 + 1)
|
||||
}
|
||||
|
||||
pub fn size(&self) -> (u32, u32) {
|
||||
let size = self.0.rectangle.size();
|
||||
|
||||
(size.width as u32 - 2, size.height as u32 - 2)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
write!(
|
||||
f,
|
||||
"Region {{ id: {:?}, rectangle: {:?} }}",
|
||||
self.0.id, self.0.rectangle
|
||||
)
|
||||
}
|
||||
}
|
||||
25
wgpu/src/texture/atlas/entry.rs
Normal file
25
wgpu/src/texture/atlas/entry.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
use crate::texture::atlas;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Entry {
|
||||
Contiguous(atlas::Allocation),
|
||||
Fragmented {
|
||||
size: (u32, u32),
|
||||
fragments: Vec<Fragment>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
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,
|
||||
}
|
||||
17
wgpu/src/texture/atlas/layer.rs
Normal file
17
wgpu/src/texture/atlas/layer.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use crate::texture::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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue