use super::TextureId;
use std::cell::{Ref, RefCell};
use std::rc::Rc;
pub struct DataArrayTextureInner {
pub data: Vec<f32>,
pub width: u32,
pub height: u32,
pub depth: u32,
pub(crate) gpu: Option<wgpu::Texture>,
}
#[derive(Clone)]
pub struct DataArrayTexture(Rc<RefCell<DataArrayTextureInner>>, TextureId);
impl std::fmt::Debug for DataArrayTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("DataArrayTexture").field(&self.0).finish()
}
}
impl DataArrayTexture {
pub fn dispose(self) {}
pub fn new(data: Vec<f32>, width: u32, height: u32, depth: u32) -> Self {
Self(
Rc::new(RefCell::new(DataArrayTextureInner {
data,
width,
height,
depth,
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<'_, DataArrayTextureInner> {
self.0.borrow()
}
pub fn size(&self) -> (u32, u32, u32) {
let inner = self.0.borrow();
(inner.width, inner.height, inner.depth)
}
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 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 array texture not uploaded"))
}
}
impl std::fmt::Debug for DataArrayTextureInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DataArrayTextureInner")
.field("data", &super::texture::DataLen(self.data.len()))
.field("width", &self.width)
.field("height", &self.height)
.field("depth", &self.depth)
.field("gpu", &self.gpu)
.finish()
}
}