allocating array on heap

This commit is contained in:
Piotrek
2021-04-22 23:25:35 +02:00
parent 5e968d0a30
commit 6239e145e9
+39 -11
View File
@@ -1,10 +1,39 @@
use std::convert::TryInto;
const BRICK_SIZE : usize = 32; // 32x32x32 brick size
const BRICK_NUM : usize = 64; // 64 bricks in texture
const BRICK_LEN : usize = BRICK_SIZE*BRICK_SIZE*BRICK_SIZE;
const DATA_LEN : usize = BRICK_LEN * BRICK_NUM;
struct BrickData {
data: Box<[u8]>
}
impl BrickData {
pub fn new() -> Self {
let data = vec![0_u8; DATA_LEN].into_boxed_slice();
Self { data }
}
pub fn set(&mut self, brick: usize, x: usize, y: usize, z: usize, value: u8)
{
let local_idx = x + BRICK_SIZE * (y + BRICK_SIZE * z);
let offset = brick * BRICK_LEN;
self.data[offset + local_idx] = value;
}
pub fn get_1024(&mut self, offset: usize) -> [u8; 1024]
{
self.data[offset..offset+1024].try_into().expect("wrong size")
}
}
pub struct BrickBuffer {
//bricks: [[u8; BRICK_SIZE*BRICK_SIZE*BRICK_SIZE];BRICK_NUM],
bricks: BrickData,
texture: wgpu::Texture,
size: wgpu::Extent3d,
pub bind_layout: wgpu::BindGroupLayout,
@@ -68,17 +97,16 @@ impl BrickBuffer {
);
// Data
// let mut bricks = [[0; BRICK_SIZE*BRICK_SIZE*BRICK_SIZE]; BRICK_NUM];
// for x in 0..32 {
// for y in 0..16 {
// for z in 0..32 {
// let idx = x + BRICK_SIZE * (y + BRICK_SIZE * z);
// bricks[0][idx] = 1;
// }
// }
// }
let mut bricks = BrickData::new();
for x in 0..32 {
for y in 0..16 {
for z in 0..32 {
bricks.set(0, x, y, z, 1);
}
}
}
// Done
Self { texture, size, bind_layout, bind_group }
Self { bricks, texture, size, bind_layout, bind_group }
}
}