use std::collections::HashMap;
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TextureId(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum TextureFormat {
#[default]
Rgba8,
}
impl TextureFormat {
#[inline(always)]
pub const fn bytes_per_pixel(self) -> usize {
4
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum FilterMode {
Nearest,
#[default]
Bilinear,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum AddressMode {
#[default]
ClampToEdge,
Repeat,
MirrorRepeat,
}
impl AddressMode {
#[inline(always)]
pub fn wrap(self, t: f32) -> f32 {
match self {
Self::ClampToEdge => t.clamp(0.0, 1.0),
Self::Repeat => t - t.floor(),
Self::MirrorRepeat => {
let period = 2.0;
let mut m = t - (t / period).floor() * period;
if m > 1.0 {
m = period - m;
}
m
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct UvTransform {
pub scale: [f32; 2],
pub offset: [f32; 2],
}
impl Default for UvTransform {
fn default() -> Self {
Self::IDENTITY
}
}
impl UvTransform {
pub const IDENTITY: Self = Self {
scale: [1.0, 1.0],
offset: [0.0, 0.0],
};
pub fn from_sub_rect(u_min: f32, v_min: f32, u_max: f32, v_max: f32) -> Self {
Self {
scale: [u_max - u_min, v_max - v_min],
offset: [u_min, v_min],
}
}
#[inline(always)]
pub fn apply(self, uv: (f32, f32)) -> (f32, f32) {
(
uv.0 * self.scale[0] + self.offset[0],
uv.1 * self.scale[1] + self.offset[1],
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct SamplerDesc {
pub filter: FilterMode,
pub address_u: AddressMode,
pub address_v: AddressMode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TextureDesc {
pub width: u32,
pub height: u32,
pub format: TextureFormat,
}
impl TextureDesc {
#[inline(always)]
pub const fn byte_len(&self) -> usize {
self.width as usize * self.height as usize * self.format.bytes_per_pixel()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextureData {
pub desc: TextureDesc,
pub pixels: Arc<[u8]>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Texture {
pub data: TextureData,
pub sampler: SamplerDesc,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TextureStore {
textures: HashMap<TextureId, Texture>,
next_id: u32,
generation: u64,
}
impl TextureStore {
pub fn new() -> Self {
Self {
textures: HashMap::new(),
next_id: 1,
generation: 0,
}
}
pub fn insert(&mut self, data: TextureData, sampler: SamplerDesc) -> Option<TextureId> {
if data.pixels.len() != data.desc.byte_len()
|| data.desc.width == 0
|| data.desc.height == 0
{
return None;
}
let id = TextureId(self.next_id);
self.next_id += 1;
self.textures.insert(id, Texture { data, sampler });
self.generation += 1;
Some(id)
}
pub fn insert_rgba8(
&mut self,
width: u32,
height: u32,
pixels: impl Into<Arc<[u8]>>,
) -> Option<TextureId> {
self.insert(
TextureData {
desc: TextureDesc {
width,
height,
format: TextureFormat::Rgba8,
},
pixels: pixels.into(),
},
SamplerDesc::default(),
)
}
pub fn get(&self, id: TextureId) -> Option<&Texture> {
self.textures.get(&id)
}
pub fn remove(&mut self, id: TextureId) -> Option<Texture> {
let removed = self.textures.remove(&id);
if removed.is_some() {
self.generation += 1;
}
removed
}
pub fn len(&self) -> usize {
self.textures.len()
}
pub fn is_empty(&self) -> bool {
self.textures.is_empty()
}
pub fn generation(&self) -> u64 {
self.generation
}
pub fn sample(&self, id: TextureId, uv: (f32, f32)) -> Option<[u8; 4]> {
let texture = self.get(id)?;
Some(sample_texture(texture, uv))
}
}
#[inline]
pub fn sample_texture(texture: &Texture, uv: (f32, f32)) -> [u8; 4] {
let width = texture.data.desc.width as usize;
let height = texture.data.desc.height as usize;
let pixels = &texture.data.pixels;
let u = texture.sampler.address_u.wrap(uv.0);
let v = texture.sampler.address_v.wrap(uv.1);
match texture.sampler.filter {
FilterMode::Nearest => {
let x = ((u * width as f32) as isize).clamp(0, width as isize - 1) as usize;
let y = ((v * height as f32) as isize).clamp(0, height as isize - 1) as usize;
texel(pixels, width, x, y)
}
FilterMode::Bilinear => {
let fx = u * width as f32 - 0.5;
let fy = v * height as f32 - 0.5;
let x0 = fx.floor();
let y0 = fy.floor();
let tx = fx - x0;
let ty = fy - y0;
let xi = |raw: f32| -> usize { raw.clamp(0.0, width as f32 - 1.0) as usize };
let yi = |raw: f32| -> usize { raw.clamp(0.0, height as f32 - 1.0) as usize };
let x0u = xi(x0);
let x1u = xi(x0 + 1.0);
let y0u = yi(y0);
let y1u = yi(y0 + 1.0);
let c00 = texel(pixels, width, x0u, y0u);
let c10 = texel(pixels, width, x1u, y0u);
let c01 = texel(pixels, width, x0u, y1u);
let c11 = texel(pixels, width, x1u, y1u);
let mut out = [0u8; 4];
for ch in 0..4 {
let top = c00[ch] as f32 * (1.0 - tx) + c10[ch] as f32 * tx;
let bottom = c01[ch] as f32 * (1.0 - tx) + c11[ch] as f32 * tx;
let value = top * (1.0 - ty) + bottom * ty;
out[ch] = value.round().clamp(0.0, 255.0) as u8;
}
out
}
}
}
#[inline(always)]
pub fn sample_bilinear_clamped(texture: &Texture, u: f32, v: f32) -> [u8; 4] {
let width = texture.data.desc.width as usize;
let height = texture.data.desc.height as usize;
let pixels = &texture.data.pixels;
let u = u.clamp(0.0, 1.0);
let v = v.clamp(0.0, 1.0);
let fx = u * width as f32 - 0.5;
let fy = v * height as f32 - 0.5;
let x0 = fx.floor();
let y0 = fy.floor();
let tx = fx - x0;
let ty = fy - y0;
let x0u = x0.clamp(0.0, width as f32 - 1.0) as usize;
let x1u = (x0 + 1.0).clamp(0.0, width as f32 - 1.0) as usize;
let y0u = y0.clamp(0.0, height as f32 - 1.0) as usize;
let y1u = (y0 + 1.0).clamp(0.0, height as f32 - 1.0) as usize;
let c00 = texel(pixels, width, x0u, y0u);
let c10 = texel(pixels, width, x1u, y0u);
let c01 = texel(pixels, width, x0u, y1u);
let c11 = texel(pixels, width, x1u, y1u);
let w00 = (1.0 - tx) * (1.0 - ty);
let w10 = tx * (1.0 - ty);
let w01 = (1.0 - tx) * ty;
let w11 = tx * ty;
let mix = |c00: u8, c10: u8, c01: u8, c11: u8| -> u8 {
let value = c00 as f32 * w00 + c10 as f32 * w10 + c01 as f32 * w01 + c11 as f32 * w11;
value.round().clamp(0.0, 255.0) as u8
};
[
mix(c00[0], c10[0], c01[0], c11[0]),
mix(c00[1], c10[1], c01[1], c11[1]),
mix(c00[2], c10[2], c01[2], c11[2]),
mix(c00[3], c10[3], c01[3], c11[3]),
]
}
#[inline(always)]
fn texel(pixels: &[u8], width: usize, x: usize, y: usize) -> [u8; 4] {
let idx = (y * width + x) * 4;
[
pixels[idx],
pixels[idx + 1],
pixels[idx + 2],
pixels[idx + 3],
]
}