Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290use std::{collections::HashMap, hash::Hash, sync::Arc};
use vk_graph::prelude::*;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]pub struct ShelfIdx(u16);impl ShelfIdx { const NONE: ShelfIdx = ShelfIdx(u16::MAX);}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]pub struct ItemIdx(u16);impl ItemIdx { const NONE: ItemIdx = ItemIdx(u16::MAX);}
pub struct TextureAtlas<K> { image: Arc<Image>, data: Box<[u8]>, contents: HashMap<K, AtlasLocation>, shelves: Vec<AtlasContent>, first_shelf: ShelfIdx, padding: u32, tick: u32,}
impl<K: Hash + Eq> TextureAtlas<K> { pub fn new(device: &Arc<Device>, width: u32, height: u32, padding: u32) -> Self { let image = Arc::new( Image::create( device, ImageInfo::image_2d( width, height, vk::Format::R8G8B8A8_UNORM, vk::ImageUsageFlags::SAMPLED | vk::ImageUsageFlags::TRANSFER_DST, ), ) .unwrap(), );
let data = vec![0; (width * height * 32) as usize].into_boxed_slice();
TextureAtlas { image, data, contents: HashMap::new(), shelves: vec![AtlasContent::empty(height, width)], first_shelf: ShelfIdx(0), padding, tick: 0, } }
pub fn info(&self) -> ImageInfo { self.image.info }
pub fn get(&self, key: &K) -> Option<AtlasLocation> { self.contents.get(key).copied() }
pub fn insert( &mut self, key: K, new_data: &[u8], width: u32, height: u32, ) -> Option<AtlasLocation> { assert_eq!(new_data.len(), (width * height * 4) as usize);
let padded_width = width + 2 * self.padding; let padded_height = height + 2 * self.padding; let info = self.info();
let mut least_wasted_height = u32::MAX; let mut best_fit_shelves = vec![];
let mut shelf_idx = self.first_shelf;
while shelf_idx != ShelfIdx::NONE { let shelf = &self.shelves[shelf_idx.0 as usize]; if shelf.height < padded_height { shelf_idx = shelf.next; continue; }
if shelf.height > padded_height + padded_height / 2 && !shelf.empty { shelf_idx = shelf.next; continue; }
let wasted_height = shelf.height - padded_height;
if wasted_height < least_wasted_height { least_wasted_height = wasted_height; best_fit_shelves.clear(); }
if wasted_height == least_wasted_height { best_fit_shelves.push(shelf_idx); }
shelf_idx = shelf.next; }
let mut wasted_space = u32::MAX; let mut best_shelf_idx = ShelfIdx::NONE; let mut best_item_idx = ItemIdx::NONE;
for shelf_idx in best_fit_shelves { let shelf = &self.shelves[shelf_idx.0 as usize]; let mut item_idx = shelf.first_item; while item_idx != ItemIdx::NONE { let item = &shelf.contents[item_idx.0 as usize]; if item.empty && item.width > padded_width && item.width - padded_width < wasted_space { wasted_space = item.width - padded_width; best_shelf_idx = shelf_idx; best_item_idx = item_idx; } item_idx = item.next; } }
if best_shelf_idx == ShelfIdx::NONE { return None; }
if self.shelves[best_shelf_idx.0 as usize].empty { let next_shelf_idx = ShelfIdx(self.shelves.len() as u16); let best_shelf = &mut self.shelves[best_shelf_idx.0 as usize]; let best_shelf_next = best_shelf.next;
let mut new_shelf = AtlasContent::empty(best_shelf.height - padded_height, info.width); new_shelf.prev = best_shelf_idx; new_shelf.next = best_shelf.next;
best_shelf.empty = false; best_shelf.height = padded_height; best_shelf.next = next_shelf_idx;
if best_shelf_next != ShelfIdx::NONE { self.shelves[best_shelf_next.0 as usize].prev = next_shelf_idx; } self.shelves.push(new_shelf); }
let best_shelf = &mut self.shelves[best_shelf_idx.0 as usize]; let next_item_idx = ItemIdx(best_shelf.contents.len() as u16); let best_item = &mut best_shelf.contents[best_item_idx.0 as usize]; let best_item_next = best_item.next;
let mut new_item = ShelfContent::empty(best_item.width - padded_width); new_item.prev = best_item_idx; new_item.next = best_item.next;
best_item.empty = false; best_item.width = padded_width; best_item.next = next_item_idx;
if best_item_next != ItemIdx::NONE { best_shelf.contents[best_item_next.0 as usize].prev = next_item_idx; } best_shelf.contents.push(new_item);
let mut x_pos = self.padding; let mut y_pos = self.padding;
let mut shelf_idx = self.first_shelf;
while shelf_idx != best_shelf_idx { let shelf = &self.shelves[shelf_idx.0 as usize]; y_pos += shelf.height; shelf_idx = shelf.next; }
let shelf = &self.shelves[shelf_idx.0 as usize];
let mut item_idx = shelf.first_item; while item_idx != best_item_idx { let item = &shelf.contents[item_idx.0 as usize]; x_pos += item.width; item_idx = item.next; }
for j in 0..height { for i in 0..width { let atlas_idx = 4 * ((j + y_pos) * info.width + (i + x_pos)) as usize; let img_idx = 4 * (j * width + i) as usize; self.data[atlas_idx] = new_data[img_idx]; self.data[atlas_idx + 1] = new_data[img_idx + 1]; self.data[atlas_idx + 2] = new_data[img_idx + 2]; self.data[atlas_idx + 3] = new_data[img_idx + 3]; } }
let loc = AtlasLocation { x: x_pos as f32 / info.width as f32, y: y_pos as f32 / info.height as f32, w: width as f32 / info.width as f32, h: height as f32 / info.height as f32, tick: self.tick, shelf_idx, item_idx, };
self.contents.insert(key, loc);
Some(loc) }
pub fn free(&mut self, _key: &K) { todo!() }
pub fn upload(&self, pool: &mut HashPool, render_graph: &mut RenderGraph) -> ImageNode { let mut staging_buffer = pool .lease(BufferInfo::host_mem( self.data.len() as u64, vk::BufferUsageFlags::empty(), )) .unwrap();
Buffer::copy_from_slice(&mut staging_buffer, 0, &*self.data);
let staging_node = render_graph.bind_node(staging_buffer); let image_node = render_graph.bind_node(&self.image);
render_graph.copy_buffer_to_image(staging_node, image_node); render_graph.unbind_node(staging_node); image_node }}
#[derive(Copy, Clone, Debug)]pub struct AtlasLocation { pub x: f32, pub y: f32, pub w: f32, pub h: f32, tick: u32, shelf_idx: ShelfIdx, item_idx: ItemIdx,}
struct AtlasContent { height: u32, next: ShelfIdx, prev: ShelfIdx, first_item: ItemIdx, empty: bool, contents: Vec<ShelfContent>,}
impl AtlasContent { fn empty(height: u32, width: u32) -> AtlasContent { AtlasContent { height, next: ShelfIdx::NONE, prev: ShelfIdx::NONE, first_item: ItemIdx(0), empty: true, contents: vec![ShelfContent::empty(width)], } }}
struct ShelfContent { width: u32, tick: u32, empty: bool, next: ItemIdx, prev: ItemIdx,}
impl ShelfContent { fn empty(width: u32) -> ShelfContent { ShelfContent { width, tick: 0, empty: true, next: ItemIdx::NONE, prev: ItemIdx::NONE, } }}