allocating array on heap
This commit is contained in:
@@ -1,10 +1,39 @@
|
|||||||
|
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
const BRICK_SIZE : usize = 32; // 32x32x32 brick size
|
const BRICK_SIZE : usize = 32; // 32x32x32 brick size
|
||||||
const BRICK_NUM : usize = 64; // 64 bricks in texture
|
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 {
|
pub struct BrickBuffer {
|
||||||
//bricks: [[u8; BRICK_SIZE*BRICK_SIZE*BRICK_SIZE];BRICK_NUM],
|
bricks: BrickData,
|
||||||
texture: wgpu::Texture,
|
texture: wgpu::Texture,
|
||||||
size: wgpu::Extent3d,
|
size: wgpu::Extent3d,
|
||||||
pub bind_layout: wgpu::BindGroupLayout,
|
pub bind_layout: wgpu::BindGroupLayout,
|
||||||
@@ -68,17 +97,16 @@ impl BrickBuffer {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Data
|
// Data
|
||||||
// let mut bricks = [[0; BRICK_SIZE*BRICK_SIZE*BRICK_SIZE]; BRICK_NUM];
|
let mut bricks = BrickData::new();
|
||||||
// for x in 0..32 {
|
for x in 0..32 {
|
||||||
// for y in 0..16 {
|
for y in 0..16 {
|
||||||
// for z in 0..32 {
|
for z in 0..32 {
|
||||||
// let idx = x + BRICK_SIZE * (y + BRICK_SIZE * z);
|
bricks.set(0, x, y, z, 1);
|
||||||
// bricks[0][idx] = 1;
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
Self { texture, size, bind_layout, bind_group }
|
Self { bricks, texture, size, bind_layout, bind_group }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user