use std::cell::Ref;
use super::texture::MinFilter;
use super::{TextureFilter, TextureId, TextureType};
use crate::math::ColorSpace;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Mapping {
CubeReflection,
CubeRefraction,
}
#[derive(Clone)]
pub struct Image {
pub width: u32,
pub height: u32,
pub data: Vec<u8>,
}
impl Image {
pub fn rgba8(width: u32, height: u32, data: Vec<u8>) -> Self {
assert_eq!(
data.len() as u32,
width * height * 4,
"three-rs: an RGBA8 cube face holds four bytes per texel"
);
Self {
width,
height,
data,
}
}
pub fn rgba16float(width: u32, height: u32, data: &[u16]) -> Self {
assert_eq!(
data.len() as u32,
width * height * 4,
"three-rs: an rgba16float cube face holds four halves per texel"
);
Self {
width,
height,
data: bytemuck::cast_slice(data).to_vec(),
}
}
}
#[derive(Debug)]
pub struct CubeTextureInner {
pub images: Vec<Image>,
pub mapping: Mapping,
pub color_space: ColorSpace,
pub texture_type: TextureType,
pub flip_y: bool,
pub mipmaps: Vec<Vec<Image>>,
pub generate_mipmaps: bool,
pub anisotropy: u16,
pub mag_filter: TextureFilter,
pub min_filter: MinFilter,
pub(crate) gpu: Option<wgpu::Texture>,
}
#[derive(Clone)]
pub struct CubeTexture(Rc<RefCell<CubeTextureInner>>, TextureId);
impl std::fmt::Debug for CubeTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("CubeTexture").field(&self.0).finish()
}
}
impl CubeTexture {
pub fn dispose(self) {}
pub fn new(images: Vec<Image>) -> Self {
Self(
Rc::new(RefCell::new(CubeTextureInner {
images,
mapping: Mapping::CubeReflection,
color_space: ColorSpace::NoColorSpace,
texture_type: TextureType::UnsignedByte,
flip_y: false,
mipmaps: Vec::new(),
generate_mipmaps: true,
anisotropy: 1,
mag_filter: TextureFilter::Linear,
min_filter: MinFilter::LinearMipmapLinear,
gpu: None,
})),
TextureId::next(),
)
}
pub fn render_target(size: u32, texture_type: TextureType) -> Self {
let face = Image {
width: size,
height: size,
data: Vec::new(),
};
let texture = Self::new(vec![face; 6]);
{
let mut inner = texture.0.borrow_mut();
inner.texture_type = texture_type;
inner.generate_mipmaps = false;
inner.min_filter = MinFilter::Linear;
inner.mag_filter = TextureFilter::Linear;
}
texture
}
pub(crate) fn pmrem_render_target(size: u32, levels: Option<u32>) -> Self {
let texture = Self::render_target(size, TextureType::HalfFloat);
{
let mut inner = texture.0.borrow_mut();
inner.min_filter = MinFilter::LinearMipmapLinear;
match levels {
Some(levels) => {
inner.mipmaps = (1..levels)
.map(|lod| {
let face = Image {
width: size >> lod,
height: size >> lod,
data: Vec::new(),
};
vec![face; 6]
})
.collect();
}
None => inner.generate_mipmaps = true,
}
}
texture
}
pub fn set_color_space(&self, color_space: ColorSpace) {
self.0.borrow_mut().color_space = color_space;
}
pub fn color_space(&self) -> ColorSpace {
self.0.borrow().color_space
}
pub fn mapping(&self) -> Mapping {
self.0.borrow().mapping
}
pub fn set_texture_type(&self, texture_type: TextureType) -> Result<(), crate::error::Error> {
if !texture_type.is_color() {
return Err(crate::error::Error::UnsupportedTextureType {
what: "colour",
texture_type,
});
}
self.0.borrow_mut().texture_type = texture_type;
Ok(())
}
pub fn texture_type(&self) -> TextureType {
self.0.borrow().texture_type
}
pub fn set_generate_mipmaps(&self, generate_mipmaps: bool) {
self.0.borrow_mut().generate_mipmaps = generate_mipmaps;
}
pub fn set_mipmaps(&self, mipmaps: Vec<Vec<Image>>) {
assert!(
mipmaps.iter().all(|level| level.len() == 6),
"three-rs: a cube mip level has six faces"
);
self.0.borrow_mut().mipmaps = mipmaps;
}
pub fn clone_texture(&self) -> Self {
let inner = self.0.borrow();
Self(
Rc::new(RefCell::new(CubeTextureInner {
images: inner.images.clone(),
mapping: inner.mapping,
color_space: inner.color_space,
texture_type: inner.texture_type,
flip_y: inner.flip_y,
mipmaps: inner.mipmaps.clone(),
generate_mipmaps: inner.generate_mipmaps,
anisotropy: inner.anisotropy,
mag_filter: inner.mag_filter,
min_filter: inner.min_filter,
gpu: None,
})),
TextureId::next(),
)
}
pub fn set_filters(&self, min_filter: MinFilter, mag_filter: TextureFilter) {
let mut inner = self.0.borrow_mut();
inner.min_filter = min_filter;
inner.mag_filter = mag_filter;
}
pub fn gpu_format(&self) -> wgpu::TextureFormat {
match self.texture_type() {
TextureType::HalfFloat => wgpu::TextureFormat::Rgba16Float,
_ => match self.color_space() {
ColorSpace::Srgb => wgpu::TextureFormat::Rgba8UnormSrgb,
ColorSpace::NoColorSpace | ColorSpace::LinearSrgb => {
wgpu::TextureFormat::Rgba8Unorm
}
},
}
}
pub fn mip_level_count(&self) -> u32 {
let inner = self.0.borrow();
if !inner.mipmaps.is_empty() {
return inner.mipmaps.len() as u32 + 1;
}
if !inner.generate_mipmaps {
return 1;
}
let size = inner.images[0].width.max(inner.images[0].height) as f64;
size.log2().floor() as u32 + 1
}
pub fn size(&self) -> (u32, u32) {
let inner = self.0.borrow();
(inner.images[0].width, inner.images[0].height)
}
pub fn id(&self) -> usize {
self.1.get()
}
pub(crate) fn owner(&self) -> super::TextureOwner {
Rc::downgrade(&self.0) as super::TextureOwner
}
pub fn borrow(&self) -> Ref<'_, CubeTextureInner> {
self.0.borrow()
}
pub(crate) fn inner(&self) -> &RefCell<CubeTextureInner> {
&self.0
}
}
impl std::fmt::Debug for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Image")
.field("width", &self.width)
.field("height", &self.height)
.field("data", &super::texture::DataLen(self.data.len()))
.finish()
}
}