use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, channel};
use std::time::SystemTime;
use ultrafast_gpu::{Device, PixelFormat, Texture, TextureDesc};
pub type FileStamp = (SystemTime, u64);
pub fn file_stamp(path: &Path) -> Option<FileStamp> {
if path.to_string_lossy().contains("://") {
return None;
}
let m = std::fs::metadata(path).ok()?;
Some((m.modified().ok()?, m.len()))
}
pub use ultrafast_geom::f32_to_f16;
pub enum ImageState {
Loading(Receiver<Result<Decoded, String>>),
Uploaded(Texture),
Ready(Texture),
Failed(String),
}
pub struct Decoded {
pub width: u32,
pub height: u32,
pub rgba: Vec<u8>,
pub staged: Option<ultrafast_gpu::Buffer>,
pub final_format: Option<PixelFormat>,
}
pub fn parse_cube(text: &str) -> Result<Decoded, String> {
let mut n = 0usize;
let mut values: Vec<[f32; 3]> = Vec::new();
for line in text.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') || line.starts_with("TITLE") || line.starts_with("DOMAIN_") {
continue;
}
let mut it = line.split_whitespace();
match it.next().unwrap_or("") {
"LUT_3D_SIZE" => n = it.next().and_then(|v| v.parse().ok()).ok_or("bad LUT_3D_SIZE")?,
"LUT_1D_SIZE" => return Err("1D LUTs are not supported (use a 3D .cube)".into()),
_ => values.push(parse3(line.split_whitespace()).ok_or_else(|| format!("bad LUT line `{line}`"))?),
}
}
if n < 2 || values.len() != n * n * n {
return Err(format!("LUT has {} entries, expected {n}^3", values.len()));
}
let (w, h) = (n * n, n);
let mut rgba = Vec::with_capacity(w * h * 8);
for g in 0..n {
for b in 0..n {
for r in 0..n {
let v = values[r + g * n + b * n * n];
for c in [v[0], v[1], v[2], 1.0] {
rgba.extend(f32_to_f16(c).to_le_bytes());
}
}
}
}
Ok(Decoded {
width: w as u32,
height: h as u32,
rgba,
staged: None,
final_format: Some(PixelFormat::Rgba16Float),
})
}
fn parse3<'a>(mut it: impl Iterator<Item = &'a str>) -> Option<[f32; 3]> {
Some([
it.next()?.parse().ok()?,
it.next()?.parse().ok()?,
it.next()?.parse().ok()?,
])
}
type Job = Box<dyn FnOnce() + Send>;
fn decode_pool(job: Job) {
static POOL: std::sync::OnceLock<std::sync::Mutex<std::sync::mpsc::Sender<Job>>> = std::sync::OnceLock::new();
let tx = POOL.get_or_init(|| {
let (tx, rx) = channel::<Job>();
let rx = std::sync::Arc::new(std::sync::Mutex::new(rx));
let n = std::thread::available_parallelism()
.map_or(4, |n| n.get())
.saturating_sub(2)
.max(2);
for i in 0..n {
let rx = rx.clone();
let _ = std::thread::Builder::new()
.name(format!("ultrafast-image-{i}"))
.spawn(move || {
loop {
let job = match rx.lock().unwrap().recv() {
Ok(job) => job,
Err(_) => return,
};
job();
}
});
}
std::sync::Mutex::new(tx)
});
let _ = tx.lock().unwrap().send(job);
}
fn decode_webp(path: &Path, device: Option<&Device>) -> Option<Decoded> {
if !path.extension().is_some_and(|e| e.eq_ignore_ascii_case("webp")) {
return None;
}
let data = std::fs::read(path).ok()?;
let (mut w, mut h) = (0, 0);
unsafe {
if libwebp_sys::WebPGetInfo(data.as_ptr(), data.len(), &mut w, &mut h) == 0 || w <= 0 || h <= 0 {
return None;
}
let len = w as usize * h as usize * 4;
let decode = |dst: &mut [u8]| {
!libwebp_sys::WebPDecodeRGBAInto(data.as_ptr(), data.len(), dst.as_mut_ptr(), len, w * 4).is_null()
};
let (rgba, staged) = match device {
Some(device) => {
let buf = device.create_buffer(len, None);
if !decode(&mut buf.mapped()[..len]) {
return None;
}
(Vec::new(), Some(buf))
}
None => {
let mut rgba = vec![0u8; len];
if !decode(&mut rgba) {
return None;
}
(rgba, None)
}
};
Some(Decoded {
width: w as u32,
height: h as u32,
rgba,
staged,
final_format: None,
})
}
}
pub struct ImageCache {
entries: HashMap<String, ImageState>,
used: HashMap<String, u64>,
tick: u64,
base_dir: PathBuf,
ahead: std::collections::HashSet<String>,
device: Option<Device>,
stamps: HashMap<String, Option<FileStamp>>,
}
const MAX_IMAGES: usize = 96;
impl ImageCache {
pub fn new() -> Self {
Self {
entries: HashMap::new(),
used: HashMap::new(),
tick: 0,
base_dir: PathBuf::from("."),
ahead: Default::default(),
device: None,
stamps: HashMap::new(),
}
}
pub fn drop_changed(&mut self) -> usize {
let changed: Vec<String> = self
.stamps
.iter()
.filter(|(src, was)| !src.contains("://") && file_stamp(&crate::media_path(&self.base_dir, src)) != **was)
.map(|(src, _)| src.clone())
.collect();
for src in &changed {
self.entries.remove(src);
self.used.remove(src);
self.ahead.remove(src);
self.stamps.remove(src);
self.request_now(src);
}
changed.len()
}
pub fn set_base_dir(&mut self, dir: &Path) {
self.base_dir = dir.to_path_buf();
}
pub fn set_device(&mut self, device: &Device) {
self.device = Some(device.clone());
}
pub fn prefetch(&mut self, src: &str) {
if !self.entries.contains_key(src) {
self.ahead.insert(src.to_string());
self.request_now(src);
}
}
pub fn request(&mut self, src: &str) {
self.ahead.remove(src);
self.request_now(src);
}
fn request_now(&mut self, src: &str) {
if self.entries.contains_key(src) {
return;
}
let path = crate::media_path(&self.base_dir, src);
self.stamps.insert(src.to_string(), file_stamp(&path));
let (tx, rx) = channel();
let device = self.device.clone();
decode_pool(Box::new(move || {
let is_cube = path.extension().is_some_and(|e| e.eq_ignore_ascii_case("cube"));
let r = if is_cube {
std::fs::read_to_string(&path)
.map_err(|e| format!("{}: {e}", path.display()))
.and_then(|t| parse_cube(&t).map_err(|e| format!("{}: {e}", path.display())))
} else {
decode_webp(&path, device.as_ref()).map(Ok).unwrap_or_else(|| {
image::open(&path)
.map(|img| {
let rgba = img.to_rgba8();
Decoded {
width: rgba.width(),
height: rgba.height(),
rgba: rgba.into_raw(),
staged: None,
final_format: None,
}
})
.map_err(|e| format!("{}: {e}", path.display()))
})
};
let _ = tx.send(r);
}));
self.entries.insert(src.to_string(), ImageState::Loading(rx));
}
pub fn poll(&mut self, device: &Device, block: bool) -> Vec<(String, Texture)> {
let mut uploaded = Vec::new();
for (src, st) in self.entries.iter_mut() {
if let ImageState::Loading(rx) = st {
let r = if block && !self.ahead.contains(src) {
rx.recv().ok()
} else {
rx.try_recv().ok()
};
match r {
None => {}
Some(Ok(d)) => {
let desc = TextureDesc {
width: d.width,
height: d.height,
format: d.final_format.unwrap_or(PixelFormat::Rgba8Unorm),
cpu_readable: false,
};
let tex = match &d.staged {
Some(buf) => device.upload_texture_from(desc, buf),
None => device.upload_texture(desc, &d.rgba),
};
if d.final_format.is_some() {
*st = ImageState::Ready(tex);
} else {
uploaded.push((src.clone(), tex.clone()));
*st = ImageState::Uploaded(tex);
}
}
Some(Err(e)) => *st = ImageState::Failed(e),
}
}
}
uploaded
}
pub fn mark_ready(&mut self, src: &str, tex: Texture) {
self.entries.insert(src.to_string(), ImageState::Ready(tex));
}
pub fn get(&mut self, src: &str) -> Option<&ImageState> {
self.used.insert(src.to_string(), self.tick);
self.entries.get(src)
}
pub fn end_frame(&mut self) {
self.tick += 1;
if self.ahead.len() > 64 {
self.ahead.clear();
}
if self.entries.len() <= MAX_IMAGES {
return;
}
let mut ready: Vec<(u64, String)> = self
.entries
.iter()
.filter(|(_, s)| matches!(s, ImageState::Ready(_) | ImageState::Failed(_)))
.filter(|(k, _)| !self.ahead.contains(*k))
.map(|(k, _)| (self.used.get(k).copied().unwrap_or(0), k.clone()))
.filter(|(u, _)| *u + 4 < self.tick)
.collect();
ready.sort();
for (_, k) in ready.into_iter().take(self.entries.len() - MAX_IMAGES) {
self.entries.remove(&k);
self.used.remove(&k);
self.stamps.remove(&k);
}
}
pub fn is_known(&self, src: &str) -> bool {
self.entries.contains_key(src)
}
pub fn ready(&self, src: &str) -> Option<&Texture> {
match self.entries.get(src) {
Some(ImageState::Ready(t)) => Some(t),
_ => None,
}
}
pub fn bytes(&self) -> u64 {
self.entries
.values()
.filter_map(|s| match s {
ImageState::Ready(t) | ImageState::Uploaded(t) => Some(t.desc()),
_ => None,
})
.map(|d| d.width as u64 * d.height as u64 * d.format.bytes_per_pixel() as u64)
.sum()
}
pub fn loading(&self) -> usize {
self.entries
.values()
.filter(|s| matches!(s, ImageState::Loading(_) | ImageState::Uploaded(_)))
.count()
}
}
impl Default for ImageCache {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn half_conversion() {
assert_eq!(f32_to_f16(1.0), 0x3c00);
assert_eq!(f32_to_f16(0.5), 0x3800);
assert_eq!(f32_to_f16(0.0), 0);
assert_eq!(f32_to_f16(-2.0), 0xc000);
}
#[test]
fn identity_cube_parses() {
let mut s = String::from("LUT_3D_SIZE 2\n");
for b in 0..2 {
for g in 0..2 {
for r in 0..2 {
s += &format!("{r} {g} {b}\n");
}
}
}
let d = parse_cube(&s).unwrap();
assert_eq!((d.width, d.height), (4, 2));
}
}