use super::TextureId;
use crate::error::Error;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TextureType {
UnsignedByte,
HalfFloat,
UnsignedInt,
Float,
}
impl TextureType {
pub(crate) fn is_color(self) -> bool {
matches!(self, TextureType::UnsignedByte | TextureType::HalfFloat)
}
pub(crate) fn is_depth(self) -> bool {
matches!(self, TextureType::UnsignedInt | TextureType::Float)
}
pub(crate) fn color_gpu_format(self) -> wgpu::TextureFormat {
match self {
TextureType::UnsignedByte => wgpu::TextureFormat::Rgba8Unorm,
TextureType::HalfFloat => wgpu::TextureFormat::Rgba16Float,
other => panic!(
"three-rs: the render target's texture type is a colour type \
(RenderTarget::new_with_options checks it), got {other:?}"
),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TextureFilter {
Nearest,
Linear,
}
#[derive(Debug)]
pub struct DepthTextureInner {
pub texture_type: TextureType,
pub mag_filter: TextureFilter,
pub min_filter: TextureFilter,
pub width: u32,
pub height: u32,
pub(crate) gpu: Option<wgpu::Texture>,
pub multisample: bool,
pub copyable: bool,
}
#[derive(Clone)]
pub struct DepthTexture(Rc<RefCell<DepthTextureInner>>, TextureId);
impl std::fmt::Debug for DepthTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("DepthTexture").field(&self.0).finish()
}
}
impl Default for DepthTexture {
fn default() -> Self {
Self::new()
}
}
impl DepthTexture {
pub fn dispose(self) {}
pub fn new() -> Self {
Self(
Rc::new(RefCell::new(DepthTextureInner {
texture_type: TextureType::UnsignedInt,
mag_filter: TextureFilter::Nearest,
min_filter: TextureFilter::Nearest,
width: 0,
height: 0,
gpu: None,
multisample: false,
copyable: false,
})),
TextureId::next(),
)
}
pub fn set_filters(&self, min_filter: TextureFilter, mag_filter: TextureFilter) {
let mut inner = self.0.borrow_mut();
inner.min_filter = min_filter;
inner.mag_filter = mag_filter;
}
pub fn set_multisample(&self, multisample: bool) {
let mut inner = self.0.borrow_mut();
if inner.multisample != multisample {
inner.multisample = multisample;
inner.gpu = None;
}
}
pub(crate) fn set_copyable(&self) {
let mut inner = self.0.borrow_mut();
if !inner.copyable {
inner.copyable = true;
inner.gpu = None;
}
}
pub fn is_multisample(&self) -> bool {
self.0.borrow().multisample
}
pub fn set_type(&self, texture_type: TextureType) -> Result<(), Error> {
if !texture_type.is_depth() {
return Err(Error::UnsupportedTextureType {
what: "depth",
texture_type,
});
}
self.0.borrow_mut().texture_type = texture_type;
Ok(())
}
pub fn texture_type(&self) -> TextureType {
self.0.borrow().texture_type
}
pub(crate) fn gpu_format(&self) -> wgpu::TextureFormat {
match self.texture_type() {
TextureType::UnsignedInt => wgpu::TextureFormat::Depth24Plus,
TextureType::Float => wgpu::TextureFormat::Depth32Float,
other => panic!(
"three-rs: the depth texture's type is a depth type \
(DepthTexture::set_type checks it), got {other:?}"
),
}
}
pub fn id(&self) -> usize {
self.1.get()
}
pub(crate) fn owner(&self) -> super::TextureOwner {
Rc::downgrade(&self.0) as super::TextureOwner
}
pub(crate) fn inner(&self) -> &RefCell<DepthTextureInner> {
&self.0
}
}