use std::collections::HashMap; use crate::engine::types::{ TextureFormat, TextureId, TextureUpload, FIRST_USER_TEXTURE_ID, }; /// Linear sRGB premultiply matching Chromium's CSS compositing on our UNORM Vulkan swapchain. pub fn premultiply_rgba_srgb(raw: &[u8]) -> Vec { let mut out = Vec::with_capacity(raw.len()); for px in raw.chunks_exact(4) { let a = px[3] as u16; if a == 255 { out.extend_from_slice(px); } else if a == 0 { out.extend_from_slice(&[0, 0, 0, 0]); } else { out.push(((px[0] as u16 * a + 127) / 255) as u8); out.push(((px[1] as u16 * a + 127) / 255) as u8); out.push(((px[2] as u16 * a + 127) / 255) as u8); out.push(px[3]); } } out } /// CPU-side GPU texture ID registry & deferred upload queue drained each frame by `UiVulkanRenderer`. pub struct TextureRegistry { next_id: u32, pending: Vec, sizes: HashMap, } impl Default for TextureRegistry { fn default() -> Self { Self::new() } } impl TextureRegistry { pub fn new() -> Self { Self { next_id: FIRST_USER_TEXTURE_ID, pending: Vec::with_capacity(64), sizes: HashMap::with_capacity(128), } } pub fn allocate_id(&mut self) -> TextureId { let id = TextureId(self.next_id); self.next_id = self.next_id.wrapping_add(1).max(FIRST_USER_TEXTURE_ID); id } /// Create a new GPU texture from straight (unpremultiplied) RGBA8 bytes. /// Premultiplies linearly in sRGB space so bilinear filtering has zero dark edge fringes. pub fn create_rgba_unpremul(&mut self, width: u32, height: u32, rgba: &[u8]) -> TextureId { let id = self.allocate_id(); let premul = premultiply_rgba_srgb(rgba); self.upload_rgba_premul_with_id(id, width, height, premul); id } /// Create a new GPU texture from already-premultiplied RGBA8 bytes (e.g. `resvg` pixmap). pub fn create_rgba_premul(&mut self, width: u32, height: u32, rgba: Vec) -> TextureId { let id = self.allocate_id(); self.upload_rgba_premul_with_id(id, width, height, rgba); id } /// Upload or replace a specific `TextureId` with premultiplied RGBA8 pixels. pub fn upload_rgba_premul_with_id( &mut self, id: TextureId, width: u32, height: u32, pixels: Vec, ) { self.sizes.insert(id, (width, height)); self.pending.push(TextureUpload::CreateOrReplace { id, width: width.max(1), height: height.max(1), format: TextureFormat::Rgba8Premul, pixels, }); } /// Upload or replace an R8 single-channel texture (used by `GLYPH_ATLAS_TEXTURE_ID`). pub fn upload_r8_with_id(&mut self, id: TextureId, width: u32, height: u32, pixels: Vec) { self.sizes.insert(id, (width, height)); self.pending.push(TextureUpload::CreateOrReplace { id, width: width.max(1), height: height.max(1), format: TextureFormat::R8, pixels, }); } /// Partial sub-rectangle upload for an existing R8 texture (fast glyph atlas updates). pub fn update_r8_region( &mut self, id: TextureId, x: u32, y: u32, width: u32, height: u32, pixels: Vec, ) { if width == 0 || height == 0 { return; } self.pending.push(TextureUpload::UpdateRegion { id, x, y, width, height, format: TextureFormat::R8, pixels, }); } /// Partial sub-rectangle upload for an existing premultiplied RGBA8 texture. pub fn update_rgba_region( &mut self, id: TextureId, x: u32, y: u32, width: u32, height: u32, pixels: Vec, ) { if width == 0 || height == 0 { return; } self.pending.push(TextureUpload::UpdateRegion { id, x, y, width, height, format: TextureFormat::Rgba8Premul, pixels, }); } /// Queue GPU destruction for an evicted texture. pub fn free(&mut self, id: TextureId) { self.sizes.remove(&id); self.pending.push(TextureUpload::Free { id }); } pub fn size_of(&self, id: TextureId) -> Option<(u32, u32)> { self.sizes.get(&id).copied() } pub fn has_pending(&self) -> bool { !self.pending.is_empty() } /// Drain all queued GPU texture uploads/frees for submission to `UiVulkanRenderer`. pub fn drain_uploads(&mut self) -> Vec { std::mem::take(&mut self.pending) } }