use std::num::NonZeroU32;
use std::sync::Arc;
use etagere::{AllocId, AtlasAllocator, size2};
use mln_gfx::gpu::Gpu;
use mln_gfx::texture2d::{SamplerState, texture_format};
use mln_gfx::types::{TextureChannelDataType, TextureFilterType, TexturePixelType, TextureWrapType};
use mln_style::bitmap::AlphaImage;
use mln_style::hash::HashMap;
use mln_style::size::Size;
use crate::draw::context::Context;
use crate::draw::store::SharedTexture;
use crate::draw::texture_size;
use crate::text::atlas::RectU16;
use crate::text::glyph::Glyph;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) struct DynamicTextureBinId(NonZeroU32);
impl DynamicTextureBinId {
fn new(id: u32) -> Option<Self> {
NonZeroU32::new(id).map(Self)
}
pub(crate) const fn first() -> Self {
Self(NonZeroU32::MIN)
}
fn next(self) -> Option<Self> {
self.0.get().checked_add(1).and_then(Self::new)
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct TextureHandle {
id: DynamicTextureBinId,
rectangle: RectU16,
needs_upload: bool,
}
impl TextureHandle {
#[must_use]
pub(crate) fn id(&self) -> DynamicTextureBinId {
self.id
}
#[must_use]
pub(crate) fn rectangle(&self) -> &RectU16 {
&self.rectangle
}
#[must_use]
pub(crate) fn is_upload_needed(&self) -> bool {
self.needs_upload
}
}
impl PartialEq for TextureHandle {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for TextureHandle {}
struct Bin {
alloc_id: AllocId,
rectangle: RectU16,
refcount: u32,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum QueuedPixels {
Bytes(Vec<u8>),
Glyph { glyph: Arc<Glyph>, padding: u16 },
}
pub(crate) struct DynamicTexture {
size: Size,
pixel_format: TexturePixelType,
allocator: AtlasAllocator,
bins: HashMap<DynamicTextureBinId, Bin>,
max_id: Option<DynamicTextureBinId>,
images_to_upload: HashMap<DynamicTextureBinId, (RectU16, QueuedPixels)>,
texture: Option<SharedTexture>,
}
impl std::fmt::Debug for DynamicTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DynamicTexture").field("size", &self.size).finish_non_exhaustive()
}
}
fn pixel_stride(pixel_type: TexturePixelType) -> usize {
match pixel_type {
TexturePixelType::Alpha | TexturePixelType::Luminance => 1,
TexturePixelType::RGBA | TexturePixelType::Stencil | TexturePixelType::Depth => 4,
}
}
fn pad_glyph(bitmap: &AlphaImage, rect: RectU16, padding: u16, padded: &mut Vec<u8>) {
let width = usize::from(rect.w);
let padding = usize::from(padding);
let bitmap_width = bitmap.size.width as usize;
padded.clear();
padded.resize(width * usize::from(rect.h), 0);
if width == 0 || bitmap_width == 0 {
return;
}
for (row, source) in padded.chunks_exact_mut(width).skip(padding).zip(bitmap.data.chunks_exact(bitmap_width)) {
if let Some(target) = row.get_mut(padding..padding + bitmap_width) {
target.copy_from_slice(source);
}
}
}
impl DynamicTexture {
pub(crate) fn new(size: Size, pixel_type: TexturePixelType) -> Self {
DynamicTexture {
size,
pixel_format: pixel_type,
allocator: AtlasAllocator::new(size2(size.width as i32, size.height as i32)),
bins: HashMap::default(),
max_id: None,
images_to_upload: HashMap::default(),
texture: None,
}
}
pub(crate) fn texture(&self) -> Option<&SharedTexture> {
self.texture.as_ref()
}
pub(crate) fn pixel_format(&self) -> TexturePixelType {
self.pixel_format
}
pub(crate) fn is_empty(&self) -> bool {
self.bins.is_empty()
}
pub(crate) fn bin(&self, id: DynamicTextureBinId) -> Option<RectU16> {
self.bins.get(&id).map(|bin| bin.rectangle)
}
pub(crate) fn reserve_size(&mut self, size: Size, id: Option<DynamicTextureBinId>) -> Option<TextureHandle> {
if let Some(id) = id
&& let Some(bin) = self.bins.get_mut(&id)
{
bin.refcount += 1;
return Some(TextureHandle { id, rectangle: bin.rectangle, needs_upload: false });
}
let id = match id {
Some(id) => id,
None => match self.max_id {
Some(max_id) => max_id.next()?,
None => DynamicTextureBinId::first(),
},
};
let width = u16::try_from(size.width).ok()?;
let height = u16::try_from(size.height).ok()?;
let allocation = self.allocator.allocate(size2(i32::from(width), i32::from(height)))?;
self.max_id = Some(self.max_id.map_or(id, |max_id| max_id.max(id)));
let min = allocation.rectangle.min;
let rectangle = RectU16 { x: min.x as u16, y: min.y as u16, w: width, h: height };
self.bins.insert(id, Bin { alloc_id: allocation.id, rectangle, refcount: 1 });
Some(TextureHandle { id, rectangle, needs_upload: true })
}
pub(crate) fn add_image(
&mut self,
pixel_data: Vec<u8>,
image_size: Size,
id: Option<DynamicTextureBinId>,
) -> Option<TextureHandle> {
let mut tex_handle = self.reserve_size(image_size, id)?;
if tex_handle.is_upload_needed() {
self.upload_image(pixel_data, &mut tex_handle);
}
Some(tex_handle)
}
pub(crate) fn upload_image(&mut self, pixel_data: Vec<u8>, tex_handle: &mut TextureHandle) {
let rect = tex_handle.rectangle;
debug_assert_eq!(pixel_data.len(), usize::from(rect.w) * usize::from(rect.h) * pixel_stride(self.pixel_format));
self.queue(tex_handle, QueuedPixels::Bytes(pixel_data));
}
pub(crate) fn upload_glyph(&mut self, glyph: Arc<Glyph>, padding: u16, tex_handle: &mut TextureHandle) {
self.queue(tex_handle, QueuedPixels::Glyph { glyph, padding });
}
fn queue(&mut self, tex_handle: &mut TextureHandle, pixels: QueuedPixels) {
self.images_to_upload.entry(tex_handle.id).or_insert((tex_handle.rectangle, pixels));
tex_handle.needs_upload = false;
}
pub(crate) fn queue_upload(&mut self, id: DynamicTextureBinId, rect: RectU16, data: Vec<u8>) {
self.images_to_upload.insert(id, (rect, QueuedPixels::Bytes(data)));
}
#[cfg(test)]
pub(crate) fn queued_uploads(&self) -> &HashMap<DynamicTextureBinId, (RectU16, QueuedPixels)> {
&self.images_to_upload
}
pub(crate) fn upload(&mut self, gpu: &mut dyn Gpu, context: &mut Context) {
if self.texture.is_none() {
let texture = context.add_texture_with(|texture| {
texture.set_size(texture_size(self.size));
texture.set_format(texture_format(self.pixel_format, TextureChannelDataType::UnsignedByte));
texture.set_sampler(SamplerState {
filter: TextureFilterType::Linear,
wrap_u: TextureWrapType::Clamp,
wrap_v: TextureWrapType::Clamp,
..SamplerState::default()
});
});
if let Err(error) = context.create_texture(gpu, &texture) {
context.fail(format!("Making an atlas texture failed: {error}"));
}
self.texture = Some(texture);
}
let Some(texture) = self.texture.as_ref() else {
return;
};
if self.images_to_upload.is_empty() {
return;
}
let mut padded = Vec::new();
for (rect, pixels) in self.images_to_upload.values() {
let data = match pixels {
QueuedPixels::Bytes(data) => data,
QueuedPixels::Glyph { glyph, padding } => {
pad_glyph(&glyph.bitmap, *rect, *padding, &mut padded);
&padded
}
};
let size = Size::new(u32::from(rect.w), u32::from(rect.h));
if let Err(error) = context.upload_texture_region(gpu, texture, data, texture_size(size), rect.x, rect.y) {
context.fail(format!("Uploading to an atlas texture failed: {error}"));
}
}
self.images_to_upload.clear();
}
pub(crate) fn remove_texture(&mut self, tex_handle: &TextureHandle) -> bool {
let Some(bin) = self.bins.get_mut(&tex_handle.id) else {
return false;
};
bin.refcount -= 1;
if bin.refcount > 0 {
return false;
}
let alloc_id = bin.alloc_id;
self.bins.remove(&tex_handle.id);
self.allocator.deallocate(alloc_id);
self.images_to_upload.remove(&tex_handle.id);
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bin_ids_are_nonzero_and_do_not_wrap() {
assert!(DynamicTextureBinId::new(0).is_none());
assert_eq!(DynamicTextureBinId::new(u32::MAX).unwrap().next(), None);
}
#[test]
fn reserve_size_allocates_fresh_ids_and_reuses_explicit_ids() {
let mut texture = DynamicTexture::new(Size::new(64, 64), TexturePixelType::Alpha);
let size = Size::new(8, 8);
let first = texture.reserve_size(size, None).expect("first bin");
let second = texture.reserve_size(size, None).expect("second bin");
let reused = texture.reserve_size(size, Some(first.id())).expect("existing bin");
assert_eq!(first.id(), DynamicTextureBinId::new(1).unwrap());
assert_eq!(second.id(), DynamicTextureBinId::new(2).unwrap());
assert_eq!(reused.id(), first.id());
assert!(!reused.is_upload_needed());
assert_eq!(reused.rectangle(), first.rectangle());
}
}