use super::TextureId;
use std::cell::{Ref, RefCell};
use std::rc::Rc;
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum DataTextureData {
F32(Vec<f32>),
U32(Vec<u32>),
}
pub struct DataTextureInner {
pub data: DataTextureData,
pub width: u32,
pub height: u32,
pub version: u32,
uploaded: u32,
pub(crate) gpu: Option<wgpu::Texture>,
}
#[derive(Clone)]
pub struct DataTexture(Rc<RefCell<DataTextureInner>>, TextureId);
impl std::fmt::Debug for DataTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("DataTexture").field(&self.0).finish()
}
}
impl DataTexture {
pub fn dispose(self) {}
pub fn new_f32(data: Vec<f32>, width: u32, height: u32) -> Self {
Self::new(DataTextureData::F32(data), width, height)
}
pub fn new_u32(data: Vec<u32>, width: u32, height: u32) -> Self {
Self::new(DataTextureData::U32(data), width, height)
}
fn new(data: DataTextureData, width: u32, height: u32) -> Self {
Self(
Rc::new(RefCell::new(DataTextureInner {
data,
width,
height,
version: 1,
uploaded: 0,
gpu: None,
})),
TextureId::next(),
)
}
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<'_, DataTextureInner> {
self.0.borrow()
}
pub fn size(&self) -> (u32, u32) {
let inner = self.0.borrow();
(inner.width, inner.height)
}
pub(crate) fn is_uint(&self) -> bool {
matches!(self.0.borrow().data, DataTextureData::U32(_))
}
pub fn set_needs_update(&self) {
self.0.borrow_mut().version += 1;
}
pub fn with_f32_mut<R>(&self, f: impl FnOnce(&mut [f32]) -> R) -> R {
let mut inner = self.0.borrow_mut();
inner.version += 1;
match &mut inner.data {
DataTextureData::F32(v) => f(v),
DataTextureData::U32(_) => panic!("three-rs: data texture is not a float texture"),
}
}
pub fn with_u32_mut<R>(&self, f: impl FnOnce(&mut [u32]) -> R) -> R {
let mut inner = self.0.borrow_mut();
inner.version += 1;
match &mut inner.data {
DataTextureData::U32(v) => f(v),
DataTextureData::F32(_) => panic!("three-rs: data texture is not a uint texture"),
}
}
pub fn with_f32<R>(&self, f: impl FnOnce(&[f32]) -> R) -> R {
let inner = self.0.borrow();
match &inner.data {
DataTextureData::F32(v) => f(v),
DataTextureData::U32(_) => panic!("three-rs: data texture is not a float texture"),
}
}
pub fn with_u32<R>(&self, f: impl FnOnce(&[u32]) -> R) -> R {
let inner = self.0.borrow();
match &inner.data {
DataTextureData::U32(v) => f(v),
DataTextureData::F32(_) => panic!("three-rs: data texture is not a uint texture"),
}
}
pub(crate) fn set_gpu(&self, gpu: wgpu::Texture) {
self.0.borrow_mut().gpu = Some(gpu);
}
pub(crate) fn has_gpu(&self) -> bool {
self.0.borrow().gpu.is_some()
}
pub(crate) fn needs_upload(&self) -> bool {
let inner = self.0.borrow();
inner.gpu.is_none() || inner.uploaded != inner.version
}
pub(crate) fn mark_uploaded(&self) {
let mut inner = self.0.borrow_mut();
inner.uploaded = inner.version;
}
pub fn with_gpu<R>(&self, f: impl FnOnce(&wgpu::Texture) -> R) -> R {
let inner = self.0.borrow();
f(inner
.gpu
.as_ref()
.expect("three-rs: data texture not uploaded"))
}
}
impl std::fmt::Debug for DataTextureInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let len = match &self.data {
DataTextureData::F32(v) => v.len(),
DataTextureData::U32(v) => v.len(),
};
f.debug_struct("DataTextureInner")
.field("data", &super::texture::DataLen(len))
.field("width", &self.width)
.field("height", &self.height)
.field("gpu", &self.gpu)
.finish()
}
}