Combined node and block buffer into one struct: content.
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
|
||||
use crate::renderer::buffers::{NODE_TEX_SIZE, NODE_TEX_LEN};
|
||||
|
||||
pub struct Nodes {
|
||||
pub nodes: Box<[u32]>,
|
||||
texture: wgpu::Texture,
|
||||
size: wgpu::Extent3d,
|
||||
pub bind_layout: wgpu::BindGroupLayout,
|
||||
pub bind_group: wgpu::BindGroup
|
||||
}
|
||||
|
||||
impl Nodes {
|
||||
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
|
||||
// Create texture
|
||||
let size = wgpu::Extent3d { width: NODE_TEX_SIZE as u32, height: NODE_TEX_SIZE as u32, depth: NODE_TEX_SIZE as u32 };
|
||||
let texture = device.create_texture(
|
||||
&wgpu::TextureDescriptor {
|
||||
size: size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D3,
|
||||
format: wgpu::TextureFormat::R32Uint,
|
||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, // STORAGE?
|
||||
label: Some("Node texture"),
|
||||
}
|
||||
);
|
||||
|
||||
// Bind layout
|
||||
let bind_layout = device.create_bind_group_layout(
|
||||
&wgpu::BindGroupLayoutDescriptor {
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture { multisampled: false, view_dimension: wgpu::TextureViewDimension::D3, sample_type: wgpu::TextureSampleType::Uint },
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler { comparison: false, filtering: false },
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
label: Some("Node texture layout"),
|
||||
}
|
||||
);
|
||||
|
||||
// Bind group
|
||||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
address_mode_u: wgpu::AddressMode::ClampToEdge, address_mode_v: wgpu::AddressMode::ClampToEdge, address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||
mag_filter: wgpu::FilterMode::Nearest, min_filter: wgpu::FilterMode::Nearest, mipmap_filter: wgpu::FilterMode::Nearest,
|
||||
..Default::default()
|
||||
});
|
||||
let bind_group = device.create_bind_group(
|
||||
&wgpu::BindGroupDescriptor {
|
||||
layout: &bind_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) },
|
||||
wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&sampler) }
|
||||
],
|
||||
label: Some("Node texture group"),
|
||||
}
|
||||
);
|
||||
|
||||
// Data
|
||||
let nodes = vec![0_u32; NODE_TEX_LEN].into_boxed_slice();
|
||||
println!("Nodes size: {} MB", (nodes.len() as f32 * 4.0 / 1024.0 / 1024.0 * 100.0).round() / 100.0);
|
||||
|
||||
// Done
|
||||
Self { nodes, texture, size, bind_layout, bind_group }
|
||||
}
|
||||
|
||||
pub fn write(&mut self, queue: &wgpu::Queue) {
|
||||
// Convert 32 bit values to 8 bit
|
||||
let mut bytes = [0_u8; NODE_TEX_LEN*4];
|
||||
LittleEndian::write_u32_into(&self.nodes, &mut bytes);
|
||||
|
||||
// Write
|
||||
queue.write_texture(
|
||||
wgpu::TextureCopyView { texture: &self.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO },
|
||||
&bytes,
|
||||
wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4*self.size.width, rows_per_image: self.size.height },
|
||||
self.size
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user