Made blocks texture have equal size, this created problems with memory usage

This commit is contained in:
Piotrek
2021-05-11 13:22:09 +02:00
parent 71e30abb62
commit 0c7f07bc7b
4 changed files with 25 additions and 14 deletions
+16 -9
View File
@@ -6,10 +6,12 @@ use crate::game::world::block::WorldBlock;
//note: remember to update in main.frag
pub const BLOCK_SIZE : usize = 32; // 32x32x32 block size
pub const BLOCK_TEX_SIZE : usize = 256; // 128x128 blocks in texture
pub const BLOCK_TEX_SIZE : usize = 32; // 32x32x32 blocks in texture (1GB)
pub const NODE_TEX_SIZE: usize = 128; // 32x32x32 nodes in texture
const BLOCK_SIZE_QB: usize = BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE;
const BLOCK_TEX_SIZE_SQ: usize = BLOCK_TEX_SIZE*BLOCK_TEX_SIZE;
const BLOCK_TEX_SIZE_QB: usize = BLOCK_TEX_SIZE*BLOCK_TEX_SIZE*BLOCK_TEX_SIZE;
const NODE_TEX_SIZE_SQ: usize = NODE_TEX_SIZE*NODE_TEX_SIZE;
const NODE_TEX_SIZE_QB: usize = NODE_TEX_SIZE*NODE_TEX_SIZE*NODE_TEX_SIZE;
@@ -41,7 +43,7 @@ impl Content {
let node_texture = device.create_texture(
&wgpu::TextureDescriptor {
mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D3,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, label: None,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, label: Some("NodeTexture"),
size: wgpu::Extent3d{ width: NODE_TEX_SIZE as u32, height: NODE_TEX_SIZE as u32, depth: NODE_TEX_SIZE as u32 },
format: wgpu::TextureFormat::R32Uint,
}
@@ -49,12 +51,13 @@ impl Content {
let block_texture = device.create_texture(
&wgpu::TextureDescriptor {
mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D3,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, label: None,
size: wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: (BLOCK_TEX_SIZE * BLOCK_SIZE) as u32, depth: (BLOCK_TEX_SIZE * BLOCK_SIZE) as u32 },
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, label: Some("BlockTexture"),
size: wgpu::Extent3d{ width: (BLOCK_TEX_SIZE * BLOCK_SIZE) as u32, height: (BLOCK_TEX_SIZE * BLOCK_SIZE) as u32, depth: (BLOCK_TEX_SIZE * BLOCK_SIZE) as u32 },
format: wgpu::TextureFormat::R8Uint,
}
);
// Bind layout
let texture_entry = wgpu::BindGroupLayoutEntry {
binding: 0, visibility: wgpu::ShaderStage::FRAGMENT, count: None,
@@ -89,11 +92,11 @@ impl Content {
const TO_MB: f64 = 1.0 / (1024.0 * 1024.0);
let block_used = self.block_freeidx as u64;
let block_count = BLOCK_TEX_SIZE_SQ as u64;
let block_count = BLOCK_TEX_SIZE_QB as u64;
let node_tex_size = (NODE_TEX_SIZE_QB*4) as f64 * TO_MB;
let block_tex_used = (block_used * BLOCK_SIZE_QB as u64) as f64 * TO_MB;
let block_tex_size = (BLOCK_TEX_SIZE_SQ*BLOCK_SIZE_QB) as f64 * TO_MB;
let block_tex_size = (BLOCK_TEX_SIZE_QB*BLOCK_SIZE_QB) as f64 * TO_MB;
ContentStats { node_tex_size, block_tex_size, block_tex_used, block_count, block_used }
}
@@ -117,9 +120,13 @@ impl Content {
}
// Calculate position in block texture
let y = ((value-1) % BLOCK_TEX_SIZE * BLOCK_SIZE) as u32;
let z = ((value-1) / BLOCK_TEX_SIZE * BLOCK_SIZE) as u32;
let block_origin = wgpu::Origin3d{ x:0, y, z };
let mut idx = value-1;
let z = idx / BLOCK_TEX_SIZE_SQ;
idx -= z * BLOCK_TEX_SIZE_SQ;
let y = idx / BLOCK_TEX_SIZE;
let x = idx % BLOCK_TEX_SIZE;
let block_origin = wgpu::Origin3d{ x: (x*BLOCK_SIZE) as u32, y: (y*BLOCK_SIZE) as u32, z: (z*BLOCK_SIZE) as u32 };
let block_size = wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: BLOCK_SIZE as u32 };
let block_bytes : [u8;BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE] = block.materials.try_into().unwrap();