use crate::resources::GpuTexture;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TextureId(pub(crate) u64);
impl TextureId {
pub const INVALID: TextureId = TextureId(u64::MAX);
pub fn index(&self) -> usize {
(self.0 as u32) as usize
}
pub(crate) fn raw(&self) -> u64 {
self.0
}
}
impl crate::resources::handle::ContentHandle for TextureId {
const INVALID: Self = TextureId(u64::MAX);
fn index(&self) -> usize {
(self.0 as u32) as usize
}
fn generation(&self) -> u32 {
(self.0 >> 32) as u32
}
fn from_parts(index: u32, generation: u32) -> Self {
pack_texture_id(index, generation)
}
}
fn pack_texture_id(index: u32, generation: u32) -> TextureId {
TextureId(((generation as u64) << 32) | index as u64)
}
pub(crate) struct TextureStore {
store: crate::resources::handle::SlotStore<GpuTexture, TextureId>,
}
impl TextureStore {
pub fn new() -> Self {
Self {
store: crate::resources::handle::SlotStore::default(),
}
}
pub fn insert(&mut self, texture: GpuTexture, bytes: u64) -> TextureId {
self.store.insert(texture, bytes)
}
pub fn get(&self, id: TextureId) -> Option<&GpuTexture> {
self.store.get(id)
}
pub fn remove(&mut self, id: TextureId) -> bool {
self.store.remove(id).is_some()
}
pub fn replace(
&mut self,
id: TextureId,
texture: GpuTexture,
bytes: u64,
) -> Option<GpuTexture> {
self.store.replace(id, texture, bytes)
}
pub fn len(&self) -> usize {
self.store.len()
}
pub fn allocated_bytes(&self) -> u64 {
self.store.allocated_bytes()
}
}