using bytemuck for node buffer
This commit is contained in:
@@ -1,15 +1,31 @@
|
||||
|
||||
pub const DATA: &[u32] = &[
|
||||
// z0
|
||||
1,0,
|
||||
0,1,
|
||||
// z1
|
||||
0,0,
|
||||
0,0,
|
||||
];
|
||||
// pub const DATA: &[u32] = &[
|
||||
// // z0
|
||||
// 1,0,
|
||||
// 0,1,
|
||||
// // z1
|
||||
// 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 {
|
||||
nodes: [Node;MAX_NODE_NUM],
|
||||
texture: wgpu::Texture,
|
||||
size: wgpu::Extent3d,
|
||||
pub bind_layout: wgpu::BindGroupLayout,
|
||||
@@ -18,10 +34,12 @@ pub struct NodeBuffer {
|
||||
|
||||
impl NodeBuffer {
|
||||
|
||||
pub fn new(device: &wgpu::Device, _size: u32) -> Self {
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
|
||||
|
||||
|
||||
// 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(
|
||||
&wgpu::TextureDescriptor {
|
||||
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
|
||||
Self { texture, size, bind_layout, bind_group }
|
||||
Self { nodes, texture, size, bind_layout, bind_group }
|
||||
}
|
||||
|
||||
pub fn write(&mut self, queue: &wgpu::Queue) {
|
||||
// Convert 32 bit values to 8 bit
|
||||
const LEN: usize = DATA.len();
|
||||
let mut bytes : [u8; LEN*4] = [0; LEN*4];
|
||||
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];
|
||||
}
|
||||
let bytes = bytemuck::bytes_of(&self.nodes);
|
||||
println!("Node buffer size: {} kB", bytes.len() as f32 / 1024.0);
|
||||
|
||||
// Write
|
||||
queue.write_texture(
|
||||
|
||||
+1
-1
@@ -105,7 +105,7 @@ impl Renderer {
|
||||
// Buffers
|
||||
let uniform_buffer = UniformBuffer::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);
|
||||
|
||||
// Pipeline
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user