using bytemuck for node buffer
This commit is contained in:
@@ -1,15 +1,31 @@
|
|||||||
|
|
||||||
pub const DATA: &[u32] = &[
|
// pub const DATA: &[u32] = &[
|
||||||
// z0
|
// // z0
|
||||||
1,0,
|
// 1,0,
|
||||||
0,1,
|
// 0,1,
|
||||||
// z1
|
// // z1
|
||||||
0,0,
|
// 0,0,
|
||||||
0,0,
|
// 0,0,
|
||||||
];
|
// ];
|
||||||
|
|
||||||
|
const MAX_NODE_NUM : usize = 16;
|
||||||
|
const TEXTURE_SIZE : u32 = 4;
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
struct Node {
|
||||||
|
data: [u32; 8]
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
pub fn set(&mut self, x: usize, y: usize, z: usize, value: u32) {
|
||||||
|
let idx = x + 2 * (y + 2 * z);
|
||||||
|
self.data[idx] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct NodeBuffer {
|
pub struct NodeBuffer {
|
||||||
|
nodes: [Node;MAX_NODE_NUM],
|
||||||
texture: wgpu::Texture,
|
texture: wgpu::Texture,
|
||||||
size: wgpu::Extent3d,
|
size: wgpu::Extent3d,
|
||||||
pub bind_layout: wgpu::BindGroupLayout,
|
pub bind_layout: wgpu::BindGroupLayout,
|
||||||
@@ -18,10 +34,12 @@ pub struct NodeBuffer {
|
|||||||
|
|
||||||
impl NodeBuffer {
|
impl NodeBuffer {
|
||||||
|
|
||||||
pub fn new(device: &wgpu::Device, _size: u32) -> Self {
|
pub fn new(device: &wgpu::Device) -> Self {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Create texture
|
// Create texture
|
||||||
let size = wgpu::Extent3d { width: _size, height: _size, depth: _size };
|
let size = wgpu::Extent3d { width: TEXTURE_SIZE, height: TEXTURE_SIZE, depth: TEXTURE_SIZE };
|
||||||
let texture = device.create_texture(
|
let texture = device.create_texture(
|
||||||
&wgpu::TextureDescriptor {
|
&wgpu::TextureDescriptor {
|
||||||
size: size,
|
size: size,
|
||||||
@@ -73,22 +91,20 @@ impl NodeBuffer {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Data
|
||||||
|
let mut nodes : [Node; MAX_NODE_NUM] = [Default::default(); MAX_NODE_NUM];
|
||||||
|
nodes[0].set(0, 0, 0, 1);
|
||||||
|
nodes[1].set(0, 0, 0, 1);
|
||||||
|
nodes[1].set(1, 1, 1, 1);
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
Self { texture, size, bind_layout, bind_group }
|
Self { nodes, texture, size, bind_layout, bind_group }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&mut self, queue: &wgpu::Queue) {
|
pub fn write(&mut self, queue: &wgpu::Queue) {
|
||||||
// Convert 32 bit values to 8 bit
|
// Convert 32 bit values to 8 bit
|
||||||
const LEN: usize = DATA.len();
|
let bytes = bytemuck::bytes_of(&self.nodes);
|
||||||
let mut bytes : [u8; LEN*4] = [0; LEN*4];
|
println!("Node buffer size: {} kB", bytes.len() as f32 / 1024.0);
|
||||||
for i in 0..LEN {
|
|
||||||
let idx = i * 4;
|
|
||||||
let byte = DATA[i].to_be_bytes();
|
|
||||||
bytes[idx+0] = byte[0];
|
|
||||||
bytes[idx+1] = byte[1];
|
|
||||||
bytes[idx+2] = byte[2];
|
|
||||||
bytes[idx+3] = byte[3];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
queue.write_texture(
|
queue.write_texture(
|
||||||
|
|||||||
+1
-1
@@ -105,7 +105,7 @@ impl Renderer {
|
|||||||
// Buffers
|
// Buffers
|
||||||
let uniform_buffer = UniformBuffer::new(&device);
|
let uniform_buffer = UniformBuffer::new(&device);
|
||||||
let raster_buffer = RasterBuffer::new(&device);
|
let raster_buffer = RasterBuffer::new(&device);
|
||||||
let mut node_buffer = NodeBuffer::new(&device, 2);
|
let mut node_buffer = NodeBuffer::new(&device);
|
||||||
node_buffer.write(&queue);
|
node_buffer.write(&queue);
|
||||||
|
|
||||||
// Pipeline
|
// Pipeline
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user