use crate::marks::gpu;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VolumeFormat {
R8Unorm,
Rgba8Unorm,
R16Float,
R32Float,
R8Uint,
R16Uint,
R32Uint,
Rg32Uint,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VolumeSample {
Float,
Uint,
}
impl VolumeFormat {
pub fn bytes_per_texel(self) -> u32 {
match self {
VolumeFormat::R8Unorm | VolumeFormat::R8Uint => 1,
VolumeFormat::R16Float | VolumeFormat::R16Uint => 2,
VolumeFormat::Rgba8Unorm | VolumeFormat::R32Float | VolumeFormat::R32Uint => 4,
VolumeFormat::Rg32Uint => 8,
}
}
pub fn sample(self) -> VolumeSample {
match self {
VolumeFormat::R8Unorm
| VolumeFormat::Rgba8Unorm
| VolumeFormat::R16Float
| VolumeFormat::R32Float => VolumeSample::Float,
VolumeFormat::R8Uint
| VolumeFormat::R16Uint
| VolumeFormat::R32Uint
| VolumeFormat::Rg32Uint => VolumeSample::Uint,
}
}
pub fn wgpu(self) -> wgpu::TextureFormat {
match self {
VolumeFormat::R8Unorm => wgpu::TextureFormat::R8Unorm,
VolumeFormat::Rgba8Unorm => wgpu::TextureFormat::Rgba8Unorm,
VolumeFormat::R16Float => wgpu::TextureFormat::R16Float,
VolumeFormat::R32Float => wgpu::TextureFormat::R32Float,
VolumeFormat::R8Uint => wgpu::TextureFormat::R8Uint,
VolumeFormat::R16Uint => wgpu::TextureFormat::R16Uint,
VolumeFormat::R32Uint => wgpu::TextureFormat::R32Uint,
VolumeFormat::Rg32Uint => wgpu::TextureFormat::Rg32Uint,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Data3DTexture {
pub data: Vec<u8>,
pub width: u32,
pub height: u32,
pub depth: u32,
pub format: VolumeFormat,
}
impl Data3DTexture {
pub fn new(width: u32, height: u32, depth: u32, format: VolumeFormat, data: Vec<u8>) -> Self {
let expected =
width as usize * height as usize * depth as usize * format.bytes_per_texel() as usize;
assert_eq!(
data.len(),
expected,
"Data3DTexture {width}x{height}x{depth} {format:?} needs {expected} bytes"
);
Self {
data,
width,
height,
depth,
format,
}
}
pub fn from_f32(width: u32, height: u32, depth: u32, values: &[f32]) -> Self {
Self::new(
width,
height,
depth,
VolumeFormat::R32Float,
bytemuck::cast_slice(values).to_vec(),
)
}
pub fn from_u64(width: u32, height: u32, depth: u32, labels: &[u64]) -> Self {
let data = labels.iter().flat_map(|l| l.to_le_bytes()).collect();
Self::new(width, height, depth, VolumeFormat::Rg32Uint, data)
}
pub fn size(&self) -> [u32; 3] {
[self.width, self.height, self.depth]
}
}
pub struct GpuVolume {
pub texture: gpu::Texture,
pub view: gpu::TextureView,
pub size: [u32; 3],
pub format: VolumeFormat,
}
#[derive(Default)]
pub struct VolumeMaps {
uploaded: std::collections::HashMap<usize, (std::sync::Arc<Data3DTexture>, GpuVolume)>,
}
impl VolumeMaps {
fn key(t: &std::sync::Arc<Data3DTexture>) -> usize {
std::sync::Arc::as_ptr(t) as usize
}
pub fn ensure(&mut self, gpu: &gpu::Gpu, t: &std::sync::Arc<Data3DTexture>) -> bool {
let k = Self::key(t);
if self.uploaded.contains_key(&k) {
return false;
}
self.uploaded
.insert(k, (t.clone(), GpuVolume::from_texture_on(gpu, t)));
true
}
pub fn get(&self, t: &std::sync::Arc<Data3DTexture>) -> Option<&GpuVolume> {
self.uploaded.get(&Self::key(t)).map(|(_, v)| v)
}
pub fn evict(&mut self) {
self.uploaded
.retain(|_, (t, _)| std::sync::Arc::strong_count(t) > 1);
}
}
impl GpuVolume {
pub fn new(device: impl Into<gpu::Gpu>, size: [u32; 3], format: VolumeFormat) -> Self {
let [w, h, d] = size.map(|n| n.max(1));
let texture = device.into().create_texture(&wgpu::TextureDescriptor {
label: Some("threers volume"),
size: wgpu::Extent3d {
width: w,
height: h,
depth_or_array_layers: d,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D3,
format: format.wgpu(),
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Self {
texture,
view,
size: [w, h, d],
format,
}
}
pub fn from_texture(
device: &wgpu::Device,
queue: &wgpu::Queue,
texture: &Data3DTexture,
) -> Self {
Self::from_texture_on(&gpu::Gpu::from((device, queue)), texture)
}
pub fn from_texture_on(gpu: &gpu::Gpu, texture: &Data3DTexture) -> Self {
let volume = Self::new(gpu, texture.size(), texture.format);
volume.write_region_on(gpu, [0, 0, 0], texture.size(), &texture.data);
volume
}
pub fn write_region(
&self,
queue: &wgpu::Queue,
origin: [u32; 3],
size: [u32; 3],
bytes: &[u8],
) {
if let Some((copy, layout, extent)) = self.region(origin, size, bytes) {
queue.write_texture(
wgpu::TexelCopyTextureInfo {
texture: self.texture.wgpu(),
mip_level: copy.mip_level,
origin: copy.origin,
aspect: copy.aspect,
},
bytes,
layout,
extent,
);
}
}
pub fn write_region_on(&self, gpu: &gpu::Gpu, origin: [u32; 3], size: [u32; 3], bytes: &[u8]) {
if let Some((copy, layout, extent)) = self.region(origin, size, bytes) {
gpu.write_texture(copy, bytes, layout, extent);
}
}
fn region(
&self,
origin: [u32; 3],
size: [u32; 3],
bytes: &[u8],
) -> Option<(
gpu::TexelCopyTextureInfo<'_>,
wgpu::TexelCopyBufferLayout,
wgpu::Extent3d,
)> {
for axis in 0..3 {
assert!(
origin[axis] + size[axis] <= self.size[axis],
"region {origin:?}+{size:?} is outside the {:?} volume",
self.size
);
}
let bpt = self.format.bytes_per_texel();
let [w, h, d] = size;
assert_eq!(
bytes.len(),
(w * h * d * bpt) as usize,
"region {size:?} of {:?} needs {} bytes",
self.format,
w * h * d * bpt
);
if w == 0 || h == 0 || d == 0 {
return None;
}
Some((
gpu::TexelCopyTextureInfo {
texture: &self.texture,
mip_level: 0,
origin: wgpu::Origin3d {
x: origin[0],
y: origin[1],
z: origin[2],
},
aspect: wgpu::TextureAspect::All,
},
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: Some(w * bpt),
rows_per_image: Some(h),
},
wgpu::Extent3d {
width: w,
height: h,
depth_or_array_layers: d,
},
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn u64_labels_are_little_endian_word_pairs() {
let t = Data3DTexture::from_u64(1, 1, 1, &[0x0000_0002_0000_0001]);
assert_eq!(t.data, [1, 0, 0, 0, 2, 0, 0, 0]);
assert_eq!(t.format.sample(), VolumeSample::Uint);
}
#[test]
#[should_panic(expected = "needs 24 bytes")]
fn wrong_length_is_rejected() {
Data3DTexture::new(2, 3, 1, VolumeFormat::R32Float, vec![0; 20]);
}
}