Skip to content

Commit

Permalink
Revert "CChunkData optimizations (#193)"
Browse files Browse the repository at this point in the history
This reverts commit a1b9773.

Due to Errors
```
thread 'tokio-runtime-worker' panicked at pumpkin-protocol/src/client/play/c_chunk_data.rs:84:25:
index out of bounds: the len is 4096 but the index is 25374
```
  • Loading branch information
Snowiiii committed Oct 27, 2024
1 parent b17ade7 commit b23b742
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 56 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ edition = "2021"
lto = true
codegen-units = 1

[profile.profiling]
inherits = "release"
debug = true

[workspace.dependencies]
log = "0.4"
tokio = { version = "1.41", features = [
Expand Down
1 change: 0 additions & 1 deletion pumpkin-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ num-traits.workspace = true
num-derive.workspace = true

bytes = "1.8"
arrayvec = "0.7.6"

flate2 = "1.0"

Expand Down
64 changes: 29 additions & 35 deletions pumpkin-protocol/src/client/play/c_chunk_data.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::collections::HashMap;

use crate::{bytebuf::ByteBuffer, BitSet, ClientPacket, VarInt};
use arrayvec::ArrayVec;
use itertools::Itertools;

use pumpkin_macros::client_packet;
use pumpkin_world::{
chunk::{ChunkData, TOTAL_SUBCHUNK},
DIRECT_PALETTE_BITS,
};
use pumpkin_world::{chunk::ChunkData, DIRECT_PALETTE_BITS};

use super::ClientboundPlayPackets;

Expand All @@ -27,23 +25,8 @@ impl<'a> ClientPacket for CChunkData<'a> {
buf.put_slice(&heightmap_nbt);

let mut data_buf = ByteBuffer::empty();
let mut palette_map = [0u16; 4096];
let mut lighting_subchunks = ArrayVec::<[u8; 2048], TOTAL_SUBCHUNK>::new();
self.0.blocks.iter_subchunks().for_each(|chunk| {
let mut block_count = 0;
let mut chunk_light = [0u8; 2048];
for (i, block) in chunk.iter().enumerate() {
if !block.is_air() {
block_count += 1;
continue;
}
let index = i / 2;
let mask = if i % 2 == 1 { 0xF0 } else { 0x0F };
chunk_light[index] |= mask;
}

lighting_subchunks.push(chunk_light);

let block_count = chunk.iter().filter(|block| !block.is_air()).count() as i16;
// Block count
data_buf.put_i16(block_count);
//// Block states
Expand All @@ -68,36 +51,34 @@ impl<'a> ClientPacket for CChunkData<'a> {
// PaletteType::Direct
};

let chunk_size = match palette_type {
PaletteType::Indirect(block_size) => 64 / block_size as usize,
PaletteType::Direct => 64 / DIRECT_PALETTE_BITS as usize,
};

let mut block_data_array = ArrayVec::<i64, 1024>::new();
let mut block_data_array = Vec::new();
match palette_type {
PaletteType::Indirect(block_size) => {
// Bits per entry
data_buf.put_u8(block_size as u8);
// Palette length
data_buf.put_var_int(&VarInt(palette.len() as i32));
let mut palette_map = HashMap::new();
palette.iter().enumerate().for_each(|(i, id)| {
palette_map[id.data as usize] = i as u16;
palette_map.insert(*id, i);
// Palette
data_buf.put_var_int(&VarInt(id.get_id_mojang_repr()));
});
for block_clump in chunk.chunks(chunk_size) {
for block_clump in chunk.chunks(64 / block_size as usize) {
let mut out_long: i64 = 0;
for block in block_clump.iter().rev() {
let index = palette_map[block.data as usize];
out_long = out_long << block_size | (index as i64);
let index = palette_map
.get(block)
.expect("Its just got added, ofc it should be there");
out_long = out_long << block_size | (*index as i64);
}
block_data_array.push(out_long);
}
}
PaletteType::Direct => {
// Bits per entry
data_buf.put_u8(DIRECT_PALETTE_BITS as u8);
for block_clump in chunk.chunks(chunk_size) {
for block_clump in chunk.chunks(64 / DIRECT_PALETTE_BITS as usize) {
let mut out_long: i64 = 0;
let mut shift = 0;
for block in block_clump {
Expand All @@ -113,9 +94,6 @@ impl<'a> ClientPacket for CChunkData<'a> {
// TODO: precompute this and omit making the `block_data_array`
data_buf.put_var_int(&VarInt(block_data_array.len() as i32));
// Data array
let bytes = data_buf.buf();
bytes.reserve(block_data_array.len() * 8);

for data_int in block_data_array {
data_buf.put_i64(data_int);
}
Expand Down Expand Up @@ -146,6 +124,22 @@ impl<'a> ClientPacket for CChunkData<'a> {
// Empty Block Light Mask
buf.put_bit_set(&BitSet(VarInt(1), &[0]));

let mut lighting_subchunks = Vec::new();

self.0.blocks.iter_subchunks().for_each(|chunk| {
let mut chunk_light = [0u8; 2048];
for (i, block) in chunk.iter().enumerate() {
if !block.is_air() {
continue;
}
let index = i / 2;
let mask = if i % 2 == 1 { 0xF0 } else { 0x0F };
chunk_light[index] |= mask;
}

lighting_subchunks.push(chunk_light);
});

buf.put_var_int(&lighting_subchunks.len().into());
for subchunk in lighting_subchunks {
buf.put_var_int(&VarInt(subchunk.len() as i32));
Expand Down
7 changes: 3 additions & 4 deletions pumpkin-world/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use crate::{

pub mod anvil;

pub const CHUNK_AREA: usize = 16 * 16;
pub const SUBCHUNK_VOLUME: usize = CHUNK_AREA * 16;
pub const CHUNK_VOLUME: usize = CHUNK_AREA * WORLD_HEIGHT;
pub const TOTAL_SUBCHUNK: usize = CHUNK_VOLUME / SUBCHUNK_VOLUME;
const CHUNK_AREA: usize = 16 * 16;
const SUBCHUNK_VOLUME: usize = CHUNK_AREA * 16;
const CHUNK_VOLUME: usize = CHUNK_AREA * WORLD_HEIGHT;

pub trait ChunkReader: Sync + Send {
fn read_chunk(
Expand Down
20 changes: 8 additions & 12 deletions pumpkin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@ const fn convert_logger_filter(level: pumpkin_config::logging::LevelFilter) -> L
}
}

fn main() {
// ensure rayon is built outside of tokio scope
rayon::ThreadPoolBuilder::new().build_global().unwrap();
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async_main())
.unwrap();
}

async fn async_main() -> io::Result<()> {
#[tokio::main]
async fn main() -> io::Result<()> {
init_logger();
// let rt = tokio::runtime::Builder::new_multi_thread()
// .enable_all()
// .build()
// .unwrap();
ctrlc::set_handler(|| {
log::warn!(
"{}",
Expand All @@ -110,6 +104,8 @@ async fn async_main() -> io::Result<()> {
std::process::exit(0);
})
.unwrap();
// ensure rayon is built outside of tokio scope
rayon::ThreadPoolBuilder::new().build_global().unwrap();
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
Expand Down

0 comments on commit b23b742

Please sign in to comment.