2024-02-24 21:02:10 -06:00
|
|
|
use glam::{uvec2, vec2, UVec2, Vec2};
|
2024-02-21 13:13:58 -06:00
|
|
|
use hashbrown::HashMap;
|
|
|
|
use nohash_hasher::BuildNoHashHasher;
|
|
|
|
use rect_packer::DensePacker;
|
2024-02-24 21:02:10 -06:00
|
|
|
use crate::rectangle::Corners;
|
2024-02-24 16:32:09 -06:00
|
|
|
|
2024-02-21 13:13:58 -06:00
|
|
|
const CHANNEL_COUNT: u32 = 4;
|
2024-02-24 16:32:09 -06:00
|
|
|
//TODO: make this work
|
|
|
|
const ALLOW_ROTATION: bool = false;
|
2024-02-21 13:13:58 -06:00
|
|
|
|
2024-02-24 21:02:10 -06:00
|
|
|
pub struct TextureAtlasMeta<'a> {
|
|
|
|
pub data: &'a [u8],
|
|
|
|
pub size: UVec2,
|
|
|
|
pub modified: bool,
|
|
|
|
}
|
|
|
|
|
2024-02-21 13:13:58 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct TextureHandle {
|
|
|
|
//TODO automatic cleanup when handle is dropped
|
|
|
|
//man: Weak<RefCell<TextureAtlasManager>>,
|
|
|
|
pub(crate) index: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub(crate) struct TextureAllocation {
|
|
|
|
/// Index of the texture allocation
|
|
|
|
pub index: u32,
|
|
|
|
|
|
|
|
/// Position in the texture atlas
|
2024-02-24 21:02:10 -06:00
|
|
|
pub(crate) position: UVec2,
|
2024-02-21 13:13:58 -06:00
|
|
|
|
|
|
|
/// Requested texture size
|
|
|
|
pub size: UVec2,
|
|
|
|
|
|
|
|
/// True if the texture was rotated by 90 degrees
|
2024-02-24 21:02:10 -06:00
|
|
|
pub(crate) rotated: bool,
|
2024-02-21 13:13:58 -06:00
|
|
|
}
|
|
|
|
|
2024-02-24 16:32:09 -06:00
|
|
|
/// Manages a texture atlas and the allocation of space within it\
|
|
|
|
/// The atlas is alllowed to grow and resize dynamically, as needed
|
2024-02-21 13:13:58 -06:00
|
|
|
pub(crate) struct TextureAtlasManager {
|
|
|
|
packer: DensePacker,
|
|
|
|
count: u32,
|
|
|
|
size: UVec2,
|
|
|
|
data: Vec<u8>,
|
|
|
|
allocations: HashMap<u32, TextureAllocation, BuildNoHashHasher<u32>>,
|
2024-02-24 21:02:10 -06:00
|
|
|
/// True if the atlas has been modified in a way which requires a texture reupload
|
|
|
|
/// since the beginning of the current frame
|
2024-02-24 16:32:09 -06:00
|
|
|
modified: bool,
|
2024-02-21 13:13:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TextureAtlasManager {
|
2024-02-24 16:32:09 -06:00
|
|
|
/// Create a new texture atlas with the specified size\
|
|
|
|
/// 512x512 is a good default size for most applications, and the texture atlas can grow dynamically as needed
|
2024-02-24 21:02:10 -06:00
|
|
|
/// By default, the texture atlas gets initialized with a single white pixel texture
|
2024-02-21 13:13:58 -06:00
|
|
|
pub fn new(size: UVec2) -> Self {
|
2024-02-24 21:02:10 -06:00
|
|
|
let mut tmp = Self {
|
2024-02-21 13:13:58 -06:00
|
|
|
packer: DensePacker::new(size.x as i32, size.y as i32),
|
|
|
|
count: 0,
|
2024-02-24 21:02:10 -06:00
|
|
|
size,
|
2024-02-21 16:56:55 -06:00
|
|
|
data: vec![0; (size.x * size.y * CHANNEL_COUNT) as usize],
|
2024-02-21 13:13:58 -06:00
|
|
|
allocations: HashMap::default(),
|
2024-02-24 16:32:09 -06:00
|
|
|
modified: true,
|
2024-02-24 21:02:10 -06:00
|
|
|
};
|
|
|
|
tmp.add(1, &[255,255,255,255]);
|
|
|
|
tmp
|
2024-02-21 13:13:58 -06:00
|
|
|
}
|
|
|
|
|
2024-02-24 16:32:09 -06:00
|
|
|
/// Resize the texture atlas to the new size in-place, preserving the existing data
|
2024-02-21 13:13:58 -06:00
|
|
|
pub fn resize(&mut self, new_size: UVec2) {
|
2024-02-24 21:02:10 -06:00
|
|
|
log::trace!("resizing texture atlas to {:?}", new_size);
|
|
|
|
if self.size == new_size {
|
|
|
|
log::warn!("Texture atlas is already the requested size");
|
|
|
|
return
|
|
|
|
}
|
2024-02-21 13:13:58 -06:00
|
|
|
if new_size.x > self.size.x && new_size.y > self.size.y{
|
|
|
|
self.packer.resize(new_size.x as i32, new_size.y as i32);
|
|
|
|
//Resize the data array in-place
|
|
|
|
self.data.resize((new_size.x * new_size.y * CHANNEL_COUNT) as usize, 0);
|
|
|
|
for y in (1..self.size.y).rev() {
|
|
|
|
for x in (0..self.size.x).rev() {
|
2024-02-21 16:56:55 -06:00
|
|
|
let idx = ((y * self.size.x + x) * CHANNEL_COUNT) as usize;
|
|
|
|
let new_idx = ((y * new_size.x + x) * CHANNEL_COUNT) as usize;
|
|
|
|
for c in 0..(CHANNEL_COUNT as usize) {
|
|
|
|
self.data[new_idx + c] = self.data[idx + c];
|
|
|
|
}
|
2024-02-21 13:13:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//If scaling down, just recreate the atlas from scratch (since we need to re-pack everything anyway)
|
|
|
|
todo!("Atlas downscaling is not implemented yet");
|
|
|
|
}
|
|
|
|
self.size = new_size;
|
2024-02-24 16:32:09 -06:00
|
|
|
self.modified = true;
|
2024-02-21 13:13:58 -06:00
|
|
|
}
|
|
|
|
|
2024-02-21 16:56:55 -06:00
|
|
|
/// Allocate a new texture region in the atlas and return a handle to it\
|
|
|
|
/// Returns None if the texture could not be allocated due to lack of space\
|
2024-02-24 16:32:09 -06:00
|
|
|
/// Use `allocate` to allocate a texture and resize the atlas if necessary\
|
|
|
|
/// Does not modify the texture data
|
|
|
|
fn try_allocate(&mut self, size: UVec2) -> Option<TextureHandle> {
|
2024-02-24 21:02:10 -06:00
|
|
|
log::trace!("Allocating texture of size {:?}", size);
|
2024-02-24 16:32:09 -06:00
|
|
|
let result = self.packer.pack(size.x as i32, size.y as i32, ALLOW_ROTATION)?;
|
2024-02-21 13:13:58 -06:00
|
|
|
let index = self.count;
|
|
|
|
self.count += 1;
|
|
|
|
let allocation = TextureAllocation {
|
|
|
|
index,
|
|
|
|
position: UVec2::new(result.x as u32, result.y as u32),
|
|
|
|
size,
|
|
|
|
//If the size does not match the requested size, the texture was rotated
|
2024-02-24 16:32:09 -06:00
|
|
|
rotated: ALLOW_ROTATION && (result.width != size.x as i32),
|
2024-02-21 13:13:58 -06:00
|
|
|
};
|
|
|
|
self.allocations.insert_unique_unchecked(index, allocation);
|
|
|
|
Some(TextureHandle { index })
|
|
|
|
}
|
|
|
|
|
2024-02-21 16:56:55 -06:00
|
|
|
/// Allocate a new texture region in the atlas and resize the atlas if necessary\
|
2024-02-24 16:32:09 -06:00
|
|
|
/// This function should never fail under normal circumstances.\
|
|
|
|
/// May modify the texture data if the atlas is resized
|
|
|
|
pub fn allocate(&mut self, size: UVec2) -> TextureHandle {
|
2024-02-21 16:56:55 -06:00
|
|
|
let mut new_size = self.size;
|
|
|
|
while !self.packer.can_pack(size.x as i32, size.y as i32, true) {
|
|
|
|
new_size *= 2;
|
|
|
|
self.packer.resize(new_size.x as i32, new_size.y as i32);
|
|
|
|
}
|
2024-02-24 21:02:10 -06:00
|
|
|
if new_size != self.size {
|
|
|
|
self.resize(new_size);
|
|
|
|
}
|
2024-02-24 16:32:09 -06:00
|
|
|
self.try_allocate(size).unwrap()
|
2024-02-21 16:56:55 -06:00
|
|
|
}
|
|
|
|
|
2024-02-24 16:32:09 -06:00
|
|
|
/// Allocate a new texture region in the atlas and copy the data into it\
|
|
|
|
/// This function may resize the atlas as needed, and should never fail under normal circumstances.
|
2024-02-21 16:56:55 -06:00
|
|
|
pub fn add(&mut self, width: usize, data: &[u8]) -> TextureHandle {
|
|
|
|
let size = uvec2(width as u32, (data.len() / (width * CHANNEL_COUNT as usize)) as u32);
|
2024-02-24 21:02:10 -06:00
|
|
|
let handle: TextureHandle = self.allocate(size);
|
|
|
|
let allocation = self.allocations.get(&handle.index).unwrap();
|
2024-02-21 16:56:55 -06:00
|
|
|
assert!(!allocation.rotated, "Rotated textures are not implemented yet");
|
|
|
|
for y in 0..size.y {
|
|
|
|
for x in 0..size.x {
|
|
|
|
let src_idx = (y * size.x + x) * CHANNEL_COUNT;
|
|
|
|
let dst_idx = ((allocation.position.y + y) * self.size.x + allocation.position.x + x) * CHANNEL_COUNT;
|
|
|
|
for c in 0..CHANNEL_COUNT as usize {
|
|
|
|
self.data[dst_idx as usize + c] = data[src_idx as usize + c];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 21:02:10 -06:00
|
|
|
self.modified = true;
|
|
|
|
handle
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Works the same way as [`TextureAtlasManager::add`], but the input data is assumed to be grayscale (1 channel per pixel)\
|
|
|
|
/// The data is copied into the alpha channel of the texture, while all the other channels are set to 255\
|
|
|
|
/// May resize the atlas as needed, and should never fail under normal circumstances.
|
|
|
|
pub fn add_grayscale(&mut self, width: usize, data: &[u8]) -> TextureHandle {
|
|
|
|
let size = uvec2(width as u32, (data.len() / width) as u32);
|
|
|
|
let handle = self.allocate(size);
|
|
|
|
let allocation = self.allocations.get(&handle.index).unwrap();
|
|
|
|
assert!(!allocation.rotated, "Rotated textures are not implemented yet");
|
|
|
|
for y in 0..size.y {
|
|
|
|
for x in 0..size.x {
|
|
|
|
let src_idx = (y * size.x + x) as usize;
|
|
|
|
let dst_idx = (((allocation.position.y + y) * self.size.x + allocation.position.x + x) * CHANNEL_COUNT) as usize;
|
|
|
|
self.data[dst_idx..(dst_idx + CHANNEL_COUNT as usize)].copy_from_slice(&[255, 255, 255, data[src_idx]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.modified = true;
|
2024-02-21 16:56:55 -06:00
|
|
|
handle
|
2024-02-21 13:13:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn modify(&mut self, handle: TextureHandle) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove(&mut self, handle: TextureHandle) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, handle: TextureHandle) -> Option<&TextureAllocation> {
|
|
|
|
self.allocations.get(&handle.index)
|
|
|
|
}
|
2024-02-24 16:32:09 -06:00
|
|
|
|
2024-02-24 21:02:10 -06:00
|
|
|
pub(crate) fn get_uv(&self, handle: TextureHandle) -> Corners<Vec2> {
|
|
|
|
let info = self.get(handle).unwrap();
|
|
|
|
let atlas_size = self.meta().size.as_vec2();
|
|
|
|
let p0x = info.position.x as f32 / atlas_size.x;
|
|
|
|
let p1x = (info.position.x as f32 + info.size.x as f32) / atlas_size.x;
|
|
|
|
let p0y = info.position.y as f32 / atlas_size.y;
|
|
|
|
let p1y = (info.position.y as f32 + info.size.y as f32) / atlas_size.y;
|
|
|
|
Corners {
|
|
|
|
top_left: vec2(p0x, p0y),
|
|
|
|
top_right: vec2(p1x, p0y),
|
|
|
|
bottom_left: vec2(p0x, p1y),
|
|
|
|
bottom_right: vec2(p1x, p1y),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 16:32:09 -06:00
|
|
|
/// Reset the `is_modified` flag
|
2024-02-24 21:02:10 -06:00
|
|
|
pub(crate) fn reset_modified(&mut self) {
|
2024-02-24 16:32:09 -06:00
|
|
|
self.modified = false;
|
|
|
|
}
|
|
|
|
|
2024-02-24 21:02:10 -06:00
|
|
|
/// Returns true if the atlas has been modified since the beginning of the current frame\
|
|
|
|
/// If this function returns true, the texture atlas should be re-uploaded to the GPU before rendering\
|
2024-02-24 16:32:09 -06:00
|
|
|
pub fn is_modified(&self) -> bool {
|
|
|
|
self.modified
|
|
|
|
}
|
2024-02-24 21:02:10 -06:00
|
|
|
|
|
|
|
pub fn meta(&self) -> TextureAtlasMeta {
|
|
|
|
TextureAtlasMeta {
|
|
|
|
data: &self.data,
|
|
|
|
size: self.size,
|
|
|
|
modified: self.modified,
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 13:13:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TextureAtlasManager {
|
2024-02-24 16:32:09 -06:00
|
|
|
/// Create a new texture atlas with a default size of 512x512
|
2024-02-21 13:13:58 -06:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(UVec2::new(512, 512))
|
|
|
|
}
|
|
|
|
}
|