use core::ops::Range;
use std::sync::{Mutex, PoisonError};
use crcbl_hal::{
BindGroupDesc, BindGroupEntry, BindGroupHandle, BindGroupLayoutHandle, BindingResource, Device,
ImageViewHandle,
};
use crcbl_ui::draw_list::TextureRun;
use crcbl_ui::image::TextureId;
use crate::graph::ImageId;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UiTexture {
pub id: TextureId,
pub image: ImageId,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Draw {
pub(super) indices: Range<u32>,
pub(super) texture: Option<TextureId>,
}
pub(super) fn draws(segment: Range<u32>, runs: &[TextureRun]) -> Vec<Draw> {
let mut draws = Vec::new();
let mut cursor = segment.start;
for run in runs {
let start = run.indices.start.max(segment.start);
let end = run.indices.end.min(segment.end);
if start >= end {
continue;
}
if cursor < start {
draws.push(Draw {
indices: cursor..start,
texture: None,
});
}
draws.push(Draw {
indices: start..end,
texture: Some(run.texture),
});
cursor = end;
}
if cursor < segment.end {
draws.push(Draw {
indices: cursor..segment.end,
texture: None,
});
}
draws
}
pub(super) fn image_of(textures: &[UiTexture], id: TextureId) -> Option<ImageId> {
textures
.iter()
.find(|texture| texture.id == id)
.map(|texture| texture.image)
}
#[derive(Debug)]
struct Cached {
view: ImageViewHandle,
group: BindGroupHandle,
used: bool,
}
#[derive(Debug)]
pub(super) struct TextureGroups {
slots: Mutex<Vec<Vec<Cached>>>,
}
impl TextureGroups {
pub(super) fn new(frames: usize) -> Self {
Self {
slots: Mutex::new((0..frames).map(|_| Vec::new()).collect()),
}
}
pub(super) fn retire(&mut self, device: &dyn Device, slot: usize) {
let slots = self.slots.get_mut().unwrap_or_else(PoisonError::into_inner);
slots[slot].retain_mut(|cached| {
if !cached.used {
device.destroy_bind_group(cached.group);
}
let kept = cached.used;
cached.used = false;
kept
});
}
pub(super) fn group(
&self,
device: &dyn Device,
layout: BindGroupLayoutHandle,
slot: usize,
view: ImageViewHandle,
) -> Option<BindGroupHandle> {
let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner);
let groups = &mut slots[slot];
if let Some(cached) = groups.iter_mut().find(|cached| cached.view == view) {
cached.used = true;
return Some(cached.group);
}
match device.create_bind_group(&BindGroupDesc {
label: Some("ui texture"),
layout,
entries: &[BindGroupEntry {
binding: 0,
array_index: 0,
resource: BindingResource::ImageView(view),
}],
variable_count: None,
}) {
Ok(group) => {
groups.push(Cached {
view,
group,
used: true,
});
Some(group)
}
Err(error) => {
crcbl_core::log::error!("graph: ui texture bind group failed: {error}");
None
}
}
}
#[cfg(test)]
pub(super) fn views(&self) -> Vec<ImageViewHandle> {
self.slots
.lock()
.unwrap_or_else(PoisonError::into_inner)
.iter()
.flatten()
.map(|cached| cached.view)
.collect()
}
pub(super) fn destroy(&mut self, device: &dyn Device) {
let slots = self.slots.get_mut().unwrap_or_else(PoisonError::into_inner);
for cached in slots.iter_mut().flat_map(|slot| slot.drain(..)) {
device.destroy_bind_group(cached.group);
}
}
}