use crate::math::Vector2;
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextureFormat {
Rgba8UnormSrgb,
Rgba8Unorm,
R8Unorm,
Rgba16Float,
Bc1RgbaUnormSrgb,
Bc7RgbaUnormSrgb,
}
impl TextureFormat {
pub fn is_block_compressed(self) -> bool {
matches!(self, Self::Bc1RgbaUnormSrgb | Self::Bc7RgbaUnormSrgb)
}
pub fn block_dim(self) -> (u32, u32) {
match self {
Self::Bc1RgbaUnormSrgb | Self::Bc7RgbaUnormSrgb => (4, 4),
_ => (1, 1),
}
}
pub fn bytes_per_block(self) -> usize {
match self {
Self::Rgba8UnormSrgb | Self::Rgba8Unorm => 4,
Self::R8Unorm => 1,
Self::Rgba16Float => 8,
Self::Bc1RgbaUnormSrgb => 8,
Self::Bc7RgbaUnormSrgb => 16,
}
}
pub fn data_len(self, width: u32, height: u32) -> usize {
let (bw, bh) = self.block_dim();
let blocks = (width.max(1).div_ceil(bw) as usize) * (height.max(1).div_ceil(bh) as usize);
blocks * self.bytes_per_block()
}
pub fn bytes_per_row(self, width: u32) -> u32 {
let (bw, _) = self.block_dim();
width.max(1).div_ceil(bw) * self.bytes_per_block() as u32
}
pub fn rows_per_image(self, height: u32) -> u32 {
let (_, bh) = self.block_dim();
height.max(1).div_ceil(bh)
}
}
pub fn pack_rgba16f(rgba: &[f32]) -> Vec<u8> {
let mut out = Vec::with_capacity(rgba.len() * 2);
for v in rgba {
out.extend_from_slice(&crate::renderer::gpu_texture::f32_to_f16_bits(*v).to_le_bytes());
}
out
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextureFilter {
Nearest,
Linear,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextureWrap {
ClampToEdge,
Repeat,
MirroredRepeat,
}
#[derive(Debug, Clone)]
pub struct Texture {
pub width: u32,
pub height: u32,
pub format: TextureFormat,
pub mag_filter: TextureFilter,
pub min_filter: TextureFilter,
pub wrap_s: TextureWrap,
pub wrap_t: TextureWrap,
pub data: Arc<Vec<u8>>,
pub flip_y: bool,
pub offset: Vector2,
pub repeat: Vector2,
pub rotation: f32,
pub external_rt_id: Option<u32>,
pub mips: Vec<Arc<Vec<u8>>>,
pub id: u64,
pub upload_seq: u64,
}
static NEXT_TEXTURE_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
fn next_texture_id() -> u64 {
NEXT_TEXTURE_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
impl Texture {
pub fn new(width: u32, height: u32, format: TextureFormat, data: Vec<u8>) -> Self {
Self {
width,
height,
format,
mag_filter: TextureFilter::Linear,
min_filter: TextureFilter::Linear,
wrap_s: TextureWrap::ClampToEdge,
wrap_t: TextureWrap::ClampToEdge,
data: Arc::new(data),
flip_y: true,
offset: Vector2::ZERO,
repeat: Vector2::ONE,
rotation: 0.0,
external_rt_id: None,
mips: Vec::new(),
id: next_texture_id(),
upload_seq: 0,
}
}
pub fn from_render_target(rt_id: u32, width: u32, height: u32, format: TextureFormat) -> Self {
Self {
width,
height,
format,
mag_filter: TextureFilter::Linear,
min_filter: TextureFilter::Linear,
wrap_s: TextureWrap::ClampToEdge,
wrap_t: TextureWrap::ClampToEdge,
data: Arc::new(Vec::new()),
flip_y: false,
offset: Vector2::ZERO,
repeat: Vector2::ONE,
rotation: 0.0,
external_rt_id: Some(rt_id),
mips: Vec::new(),
id: next_texture_id(),
upload_seq: 0,
}
}
pub fn replace_rgba_bytes(&mut self, data: Vec<u8>) -> bool {
let expected = self.format.data_len(self.width, self.height);
if data.len() != expected {
return false;
}
self.data = Arc::new(data);
self.upload_seq += 1;
true
}
pub fn bytes_per_pixel(&self) -> usize {
assert!(
!self.format.is_block_compressed(),
"bytes_per_pixel on {:?}: block-compressed textures have no per-pixel \
stride — use TextureFormat::data_len / is_block_compressed",
self.format
);
self.format.bytes_per_block()
}
pub fn handle(&self) -> Self {
Self {
data: Arc::new(Vec::new()),
mips: Vec::new(),
..self.clone()
}
}
pub fn is_handle(&self) -> bool {
self.data.is_empty() && self.width > 0 && self.height > 0
}
pub fn data_len(&self) -> usize {
self.format.data_len(self.width, self.height)
}
pub fn solid(rgba: [u8; 4], format: TextureFormat) -> Self {
Self::new(1, 1, format, rgba.to_vec())
}
}