Merge pull request #2057 from ludovic111/perf/remote-chunks-off-render

🤖 Agent: pr_ready passed (78 harnesses), lan_e2e in-frame and server-thread passed; frame p99 4.96→3.95 ms during a RD 32 flight. Queued for merge.
This commit is contained in:
Ludovic Marie
2026-10-26 09:56:18 +01:00
committed by GitHub
4 changed files with 449 additions and 200 deletions
+1
View File
@@ -131,6 +131,7 @@ mod pumpkin;
mod raids;
mod recipe_book;
mod remote;
mod remote_chunks;
mod remote_mob_data;
mod remote_mob_use;
mod remote_objects;
+87 -198
View File
@@ -6,11 +6,12 @@
//! clicks). The player still moves with the local physics, as the vanilla
//! client predicts its own movement; the server stays authoritative for
//! everything else.
use super::remote_chunks::{ChunkDecoder, ChunkFormat, Next};
use super::{Dim, Game, Mining, Screen};
use horizon_client::first_person::{Hand, SwingAnimation};
use horizon_client::{
Result,
chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section, block_strategy},
chunk::{Chunk, SECTIONS},
containers::{BlockEntity, Furnace, FurnaceKind},
entities::{Experience, ExperienceOrb, ItemEntity},
hud::{HudBatch, rgba},
@@ -25,11 +26,7 @@ use horizon_client::{
tab_list::SPECTATOR,
world::World,
};
use horizon_core::{
geometry::Vec3,
paletted::{PalettedContainer, Strategy},
random::RandomSource,
};
use horizon_core::{geometry::Vec3, random::RandomSource};
use horizon_protocol::{
entity_data::{ITEM_ENTITY_ITEM, Value},
item::{Hashed, Stack},
@@ -64,9 +61,6 @@ fn menu_title(tag: &Tag) -> horizon_client::text::Text {
remote_text(tag).resolve_legacy()
}
/// A section's sky and block light layers from the wire.
type LightLayers = (Option<Vec<u8>>, Option<Vec<u8>>);
/// Streaming needs a generator; a server world never generates.
struct NoTerrain;
impl ChunkGenerator for NoTerrain {
@@ -104,6 +98,14 @@ fn place_interpolated(m: &mut Mob, at: PosRot) {
m.head_pitch = (at.pitch % 360.0).clamp(-90.0, 90.0);
}
/// What the network thread delivered, in order: an event, or a chunk packet
/// handed to the decoder (with the format it decodes with), whose column
/// comes back from `ChunkDecoder::next`.
enum Received {
Event(Event),
Chunk(Arc<ChunkFormat>),
}
pub(super) struct Remote {
pub session: Session,
pub brand: String,
@@ -114,8 +116,11 @@ pub(super) struct Remote {
status: String,
pub(super) entity_id: i32,
dimension: DimensionType,
biome_map: Vec<u16>,
biome_bits: u8,
/// What chunk packets decode with (the dimension's height and sky, the
/// biome registry); replaced whenever one of them changes.
pub(super) chunk_format: Arc<ChunkFormat>,
/// Decodes chunk packets as they arrive, off the frame.
decoder: ChunkDecoder,
view_distance: i32,
sequence: i32,
position_sender: horizon_client::player_sync::PositionSender,
@@ -200,7 +205,7 @@ pub(super) struct Remote {
chunk_center: (i32, i32),
/// Received events a frame left for the next one, in order, and when
/// the oldest of them first waited (`poll_remote`).
backlog: VecDeque<Event>,
backlog: VecDeque<Received>,
backlog_since: Option<Instant>,
/// `ClientboundTransferPacket`'s target: when the connection ends the
/// game connects here, the stored cookies carried over (`TransferState`).
@@ -210,11 +215,12 @@ pub(super) struct Remote {
/// The time a frame spends on chunk packets before it leaves the rest of
/// the received events for the next frame. A batch of 25 to 60 columns
/// (0.1 ms each on an M4 Pro) all in one frame was a 3 to 8 ms frame every
/// time the player crossed a chunk border at 32 chunks.
/// time the player crossed a chunk border at 32 chunks; they decode on the
/// chunk decoder's thread now, and the frame only inserts them.
const CHUNK_PACKETS_PER_FRAME: Duration = Duration::from_millis(1);
/// Events that have waited this long are all handled in the next frame,
/// whatever their cost: the packets after a slow machine's chunks never
/// wait more than a tick.
/// whatever inserting their columns costs, up to a column still decoding:
/// the packets after a slow machine's chunks don't wait on its frames.
const BACKLOG_LIMIT: Duration = Duration::from_millis(50);
/// The open menu name of a server's horse inventory, then its chest's
/// columns (the packet names no `MenuType`).
@@ -420,8 +426,12 @@ impl Game<'_> {
status: "Connecting to the server...".into(),
entity_id: 0,
dimension: DimensionType::vanilla("minecraft:overworld"),
biome_map: Vec::new(),
biome_bits: 7,
chunk_format: Arc::new(ChunkFormat::new(
&DimensionType::vanilla("minecraft:overworld"),
Vec::new(),
7,
)),
decoder: ChunkDecoder::new(),
view_distance: self.settings.render_distance,
sequence: 0,
position_sender: Default::default(),
@@ -741,25 +751,63 @@ impl Game<'_> {
/// client's packet processor), in order. Chunk packets get
/// `CHUNK_PACKETS_PER_FRAME`: once a frame has spent it, the events from
/// the next chunk packet on wait for the next frame, unless they have
/// already waited `BACKLOG_LIMIT`.
/// already waited `BACKLOG_LIMIT`. A chunk packet still decoding holds
/// back what follows it.
pub(super) fn poll_remote(&mut self) -> Result<()> {
let Some(r) = self.remote.as_mut() else {
return Ok(());
};
let mut events = std::mem::take(&mut r.backlog);
events.extend(r.session.poll());
// Chunk packets start decoding now; the frame that reaches one in
// order inserts its column.
for event in r.session.poll() {
events.push_back(match event {
Event::Packet(p) if matches!(*p, C::LevelChunkWithLight(_)) => {
match r.decoder.submit(r.chunk_format.clone(), p) {
None => Received::Chunk(r.chunk_format.clone()),
Some(p) => Received::Event(Event::Packet(p)),
}
}
event => Received::Event(event),
});
}
let overdue = r
.backlog_since
.is_some_and(|since| since.elapsed() >= BACKLOG_LIMIT);
let mut chunks = Duration::ZERO;
while let Some(event) = events.pop_front() {
let chunk =
matches!(&event, Event::Packet(p) if matches!(**p, C::LevelChunkWithLight(_)));
while let Some(item) = events.pop_front() {
let chunk = matches!(item, Received::Chunk(_));
if chunk && !overdue && chunks >= CHUNK_PACKETS_PER_FRAME {
events.push_front(event);
events.push_front(item);
break;
}
let started = chunk.then(Instant::now);
let event = match item {
Received::Event(event) => event,
Received::Chunk(format) => {
// A column still decoding waits for the next frame, with
// what came after it, overdue or not.
let Some(next) = self.remote.as_ref().map(|r| r.decoder.next()) else {
break;
};
let (packet, column) = match next {
Next::Ready(decoded) => decoded,
Next::Decoding => {
events.push_front(Received::Chunk(format));
break;
}
Next::Lost => {
log::warn!("Horizon: the chunk decoder stopped; a column is missing");
continue;
}
};
if let C::LevelChunkWithLight(p) = *packet {
self.remote_chunk(&p, Some((&format, column)));
}
chunks += started.map_or(Duration::ZERO, |t| t.elapsed());
continue;
}
};
match event {
Event::Status(s) => {
if let Some(r) = self.remote.as_mut() {
@@ -915,8 +963,8 @@ impl Game<'_> {
})
.unwrap_or_default();
if let Some(r) = self.remote.as_mut() {
r.biome_bits = ceil_log2(biomes.len());
r.biome_map = biomes;
let bits = ceil_log2(biomes.len());
r.chunk_format = Arc::new(ChunkFormat::new(&r.dimension, biomes, bits));
r.registries = registries;
}
}
@@ -927,6 +975,7 @@ impl Game<'_> {
};
let dimension = r.registries.dimension(spawn.dimension_type);
let same = r.dimension.name == dimension.name && keep_world;
r.chunk_format = Arc::new(r.chunk_format.with_dimension(&dimension));
r.dimension = dimension;
r.game_mode = spawn.game_type;
r.loaded_sent = false;
@@ -989,145 +1038,10 @@ impl Game<'_> {
self.apply_settings();
}
/// LevelChunkSection data into the client's fixed -64..320 column.
fn remote_chunk(&self, p: &play::LevelChunkWithLight) -> std::result::Result<Chunk, String> {
let r = self.remote.as_ref().ok_or("no connection")?;
let sections = p
.chunk
.sections(r.dimension.sections(), 16, r.biome_bits)
.map_err(|e| e.to_string())?;
let mut chunk = Chunk::empty(p.x, p.z);
let offset = (r.dimension.min_y - MIN_Y).div_euclid(16);
// Biome table index per quart cell of the dimension's own sections.
let mut biome_cells: Vec<[u32; 64]> = Vec::with_capacity(sections.len());
for (i, s) in sections.iter().enumerate() {
let target = offset + i as i32;
let mut cells = [0u32; 64];
for (j, cell) in cells.iter_mut().enumerate() {
*cell = u32::from(
r.biome_map
.get(s.biomes.get(j) as usize)
.copied()
.unwrap_or(0),
);
}
biome_cells.push(cells);
if !(0..SECTIONS as i32).contains(&target) {
continue;
}
// A global section is as wide as the server's registry: 16 bits
// from a vanilla server, wider from a Horizon host with the same
// content mods. Read it at that width, then widen it to ours.
let ours = block_strategy();
let strategy = if ours.wire_has_palette(s.states.bits) {
ours
} else {
Strategy::blocks(s.states.bits)
};
let palette = s.states.palette.iter().map(|&p| p as u32).collect();
let words = s.states.data.iter().map(|&w| w as u64).collect();
let mut blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words)
.map_err(|e| e.to_string())?;
if strategy.global_bits != ours.global_bits {
let mut values = [0u32; 4096];
blocks.unpack(&mut values);
// Ids past our registry (a host with mods we lack) read as air.
let limit = 1u64 << ours.global_bits;
for v in &mut values {
if u64::from(*v) >= limit {
*v = 0;
}
}
blocks = PalettedContainer::from_values(ours, &values);
}
let mut section = Section {
blocks,
biomes: PalettedContainer::from_values(BIOME_STRATEGY, &cells),
light: Light::Uniform(0xF0),
};
section.compact();
chunk.sections[target as usize] = Arc::new(section);
}
// Sections outside the dimension's range take its nearest biomes
// (the column's top or bottom cell layer repeated).
if !biome_cells.is_empty() {
let last = biome_cells.len() - 1;
for si in 0..SECTIONS as i32 {
let i = si - offset;
if (0..sections.len() as i32).contains(&i) {
continue;
}
let (edge, layer) = if i < 0 { (0, 0) } else { (last, 3) };
let mut cells = [0u32; 64];
for (j, cell) in cells.iter_mut().enumerate() {
*cell = biome_cells[edge][layer * 16 + j % 16];
}
Arc::make_mut(&mut chunk.sections[si as usize]).biomes =
PalettedContainer::from_values(BIOME_STRATEGY, &cells);
}
}
// ClientPacketListener.applyLightData: the column keeps the light
// the server sent; nothing is relit.
let (layers, above) = self.wire_light(&p.light);
let sky = r.dimension.has_skylight;
horizon_client::light::light_from_layers(&mut chunk, &layers, above, sky);
Ok(chunk)
}
/// `ClientPacketListener.readSectionList`: the sky and block layers of a
/// light packet, by section of the client's column, and the sky light
/// above the column's top section (from the light section just above
/// it, else open sky).
fn wire_light(
&self,
light: &horizon_protocol::chunk::LightData,
) -> (Vec<LightLayers>, [u8; 256]) {
let mut layers: Vec<LightLayers> = vec![(None, None); SECTIONS];
let Some(r) = self.remote.as_ref() else {
return (layers, [0; 256]);
};
let sky = r.dimension.has_skylight;
let mut above = [if sky { 15 } else { 0 }; 256];
// Light section b is the dimension's section b - 1.
let first = (r.dimension.min_y - MIN_Y).div_euclid(16) - 1;
for (b, layer) in light
.layers(r.dimension.sections() + 2)
.into_iter()
.enumerate()
{
let si = first + b as i32;
if (0..SECTIONS as i32).contains(&si) {
layers[si as usize] = layer;
} else if si == SECTIONS as i32
&& sky
&& let Some(l) = &layer.0
{
for (i, a) in above.iter_mut().enumerate() {
*a = (l[i >> 1] >> ((i & 1) * 4)) & 15;
}
}
}
(layers, above)
}
/// `handleLightUpdatePacket`: the sections it names take its light.
fn remote_light_update(&mut self, p: &play::LightUpdate) {
let Some(sky) = self.remote.as_ref().map(|r| r.dimension.has_skylight) else {
return;
};
if !self.world.chunks.contains_key(&(p.x, p.z)) {
return;
}
let (layers, _) = self.wire_light(&p.light);
let Some(chunk) = self.world.chunks.get_mut(&(p.x, p.z)) else {
return;
};
let changed =
horizon_client::light::update_light_layers(Arc::make_mut(chunk), &layers, sky);
Streaming::light_updated(&mut self.world, p.x, p.z, &changed);
}
/// `ClientChunkCache.Storage.inRange`: within the storage around the
/// view centre, `calculateStorageRange` (the server's radius, at least
/// two, plus three) chunks each way.
fn remote_chunk_in_range(&self, x: i32, z: i32) -> bool {
pub(super) fn remote_chunk_in_range(&self, x: i32, z: i32) -> bool {
self.remote.as_ref().is_none_or(|r| {
let range = r.view_distance.max(2) + 3;
(x - r.chunk_center.0).abs() <= range && (z - r.chunk_center.1).abs() <= range
@@ -1135,29 +1049,33 @@ impl Game<'_> {
}
/// The columns a moved or resized storage no longer holds.
fn drop_remote_chunks_out_of_range(&mut self) {
let gone: Vec<(i32, i32)> = self
let gone: horizon_client::fast_hash::FxHashSet<(i32, i32)> = self
.world
.chunks
.keys()
.copied()
.filter(|&(x, z)| !self.remote_chunk_in_range(x, z))
.collect();
for (x, z) in gone {
if gone.is_empty() {
return;
}
for &(x, z) in &gone {
self.streaming.forget(&mut self.world, x, z);
self.world
.block_entities
.retain(|k, _| (k.0 >> 4, k.2 >> 4) != (x, z));
if let Some(renderer) = self.renderer.as_mut() {
for sy in 0..SECTIONS as i32 {
renderer.remove((x, sy, z));
}
}
}
// One pass for all of them.
self.world
.block_entities
.retain(|k, _| !gone.contains(&(k.0 >> 4, k.2 >> 4)));
}
/// `handleBlockEntityData`: the block entity at `pos`, when it is of the
/// packet's type, loads the tag (`loadWithComponents`); an empty tag
/// changes nothing.
fn remote_block_entity_data(&mut self, pos: [i32; 3], kind: i32, tag: &Tag) {
pub(super) fn remote_block_entity_data(&mut self, pos: [i32; 3], kind: i32, tag: &Tag) {
let key = (pos[0], pos[1], pos[2]);
let Some(name) = self
.remote
@@ -1248,7 +1166,7 @@ impl Game<'_> {
}
}
/// Keep the renderable block entities (chests, furnaces) in step with blocks.
fn sync_block_entity(&mut self, pos: (i32, i32, i32), state: BlockStateId) {
pub(super) fn sync_block_entity(&mut self, pos: (i32, i32, i32), state: BlockStateId) {
let name = self.res.facts().get(state).name.clone();
match BlockEntity::for_block(&name) {
Some(entity) => {
@@ -1704,36 +1622,7 @@ impl Game<'_> {
})
.clamp(-90.0, 90.0);
}
// ClientChunkCache.replaceWithPacketData: a column outside the
// storage around the view centre is ignored.
C::LevelChunkWithLight(p) if !self.remote_chunk_in_range(p.x, p.z) => {
log::warn!(
"Ignoring chunk since it's not in the view range: {}, {}",
p.x,
p.z
);
}
C::LevelChunkWithLight(p) => match self.remote_chunk(&p) {
Ok(chunk) => {
let (x, z) = (p.x, p.z);
self.streaming.insert_remote(&mut self.world, chunk);
self.world
.block_entities
.retain(|k, _| (k.0 >> 4, k.2 >> 4) != (x, z));
for info in &p.chunk.block_entities {
let pos = (
x * 16 + i32::from(info.packed_xz as u8 >> 4),
i32::from(info.y),
z * 16 + i32::from(info.packed_xz as u8 & 15),
);
let state = self.world.block(pos.0, pos.1, pos.2);
self.sync_block_entity(pos, state);
// LevelChunk.replaceWithPacketData: the block entity loads its update tag.
self.remote_block_entity_data([pos.0, pos.1, pos.2], info.kind, &info.tag);
}
}
Err(e) => log::warn!("Horizon: chunk {} {}: {e}", p.x, p.z),
},
C::LevelChunkWithLight(p) => self.remote_chunk(&p, None),
C::LightUpdate(p) => self.remote_light_update(&p),
C::ForgetLevelChunk(p) => {
let [x, z] = p.pos;
@@ -0,0 +1,355 @@
//! A server's chunk columns (`ClientboundLevelChunkWithLightPacket`): the
//! sections and light of each packet become a client column on a decoder
//! thread as soon as the packet arrives, so the frame that handles it in
//! order only inserts the result (`ClientPacketListener.handleLevelChunkWithLight`).
use super::Game;
use horizon_client::{
chunk::{BIOME_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section, block_strategy},
streaming::Streaming,
};
use horizon_core::paletted::{PalettedContainer, Strategy};
use horizon_protocol::{
chunk::{LightData, SectionLight},
packets::play::{self, Clientbound as C},
registry::DimensionType,
};
use std::sync::{
Arc,
mpsc::{self, TryRecvError},
};
/// What turning a chunk packet into a client column depends on: the
/// dimension's height and sky, and the server's biome registry mapped to
/// ours. The connection replaces it when either changes, so a column decoded
/// with an older one is decoded again.
#[derive(Default)]
pub(super) struct ChunkFormat {
min_y: i32,
sections: usize,
has_skylight: bool,
biome_map: Vec<u16>,
biome_bits: u8,
}
impl ChunkFormat {
pub(super) fn new(dimension: &DimensionType, biome_map: Vec<u16>, biome_bits: u8) -> Self {
Self {
min_y: dimension.min_y,
sections: dimension.sections(),
has_skylight: dimension.has_skylight,
biome_map,
biome_bits,
}
}
pub(super) fn with_dimension(&self, dimension: &DimensionType) -> Self {
Self::new(dimension, self.biome_map.clone(), self.biome_bits)
}
/// LevelChunkSection data into the client's fixed -64..320 column.
fn column(&self, p: &play::LevelChunkWithLight) -> Result<Chunk, String> {
let sections = p
.chunk
.sections(self.sections, 16, self.biome_bits)
.map_err(|e| e.to_string())?;
let mut chunk = Chunk::empty(p.x, p.z);
let offset = (self.min_y - MIN_Y).div_euclid(16);
// Biome table index per quart cell of the dimension's own sections.
let mut biome_cells: Vec<[u32; 64]> = Vec::with_capacity(sections.len());
for (i, s) in sections.iter().enumerate() {
let target = offset + i as i32;
let mut cells = [0u32; 64];
for (j, cell) in cells.iter_mut().enumerate() {
*cell = u32::from(
self.biome_map
.get(s.biomes.get(j) as usize)
.copied()
.unwrap_or(0),
);
}
biome_cells.push(cells);
if !(0..SECTIONS as i32).contains(&target) {
continue;
}
// A global section is as wide as the server's registry: 16 bits
// from a vanilla server, wider from a Horizon host with the same
// content mods. Read it at that width, then widen it to ours.
let ours = block_strategy();
let strategy = if ours.wire_has_palette(s.states.bits) {
ours
} else {
Strategy::blocks(s.states.bits)
};
let palette = s.states.palette.iter().map(|&p| p as u32).collect();
let words = s.states.data.iter().map(|&w| w as u64).collect();
let mut blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words)
.map_err(|e| e.to_string())?;
if strategy.global_bits != ours.global_bits {
let mut values = [0u32; 4096];
blocks.unpack(&mut values);
// Ids past our registry (a host with mods we lack) read as air.
let limit = 1u64 << ours.global_bits;
for v in &mut values {
if u64::from(*v) >= limit {
*v = 0;
}
}
blocks = PalettedContainer::from_values(ours, &values);
}
let mut section = Section {
blocks,
biomes: PalettedContainer::from_values(BIOME_STRATEGY, &cells),
light: Light::Uniform(0xF0),
};
section.compact();
chunk.sections[target as usize] = Arc::new(section);
}
// Sections outside the dimension's range take its nearest biomes
// (the column's top or bottom cell layer repeated).
if !biome_cells.is_empty() {
let last = biome_cells.len() - 1;
for si in 0..SECTIONS as i32 {
let i = si - offset;
if (0..sections.len() as i32).contains(&i) {
continue;
}
let (edge, layer) = if i < 0 { (0, 0) } else { (last, 3) };
let mut cells = [0u32; 64];
for (j, cell) in cells.iter_mut().enumerate() {
*cell = biome_cells[edge][layer * 16 + j % 16];
}
Arc::make_mut(&mut chunk.sections[si as usize]).biomes =
PalettedContainer::from_values(BIOME_STRATEGY, &cells);
}
}
// ClientPacketListener.applyLightData: the column keeps the light
// the server sent; nothing is relit.
let (layers, above) = self.light(&p.light);
horizon_client::light::light_from_layers(&mut chunk, &layers, above, self.has_skylight);
Ok(chunk)
}
/// `ClientPacketListener.readSectionList`: the sky and block layers of a
/// light packet, by section of the client's column, and the sky light
/// above the column's top section (from the light section just above
/// it, else open sky).
pub(super) fn light(&self, light: &LightData) -> (Vec<SectionLight>, [u8; 256]) {
let mut layers: Vec<SectionLight> = vec![(None, None); SECTIONS];
let sky = self.has_skylight;
let mut above = [if sky { 15 } else { 0 }; 256];
// Light section b is the dimension's section b - 1.
let first = (self.min_y - MIN_Y).div_euclid(16) - 1;
for (b, layer) in light.layers(self.sections + 2).into_iter().enumerate() {
let si = first + b as i32;
if (0..SECTIONS as i32).contains(&si) {
layers[si as usize] = layer;
} else if si == SECTIONS as i32
&& sky
&& let Some(l) = &layer.0
{
for (i, a) in above.iter_mut().enumerate() {
*a = (l[i >> 1] >> ((i & 1) * 4)) & 15;
}
}
}
(layers, above)
}
}
type Decoded = (Box<C>, Result<Chunk, String>);
/// One thread that decodes chunk packets in the order they arrived; its
/// results come back in that order, one per `submit`.
pub(super) struct ChunkDecoder {
jobs: mpsc::Sender<(Arc<ChunkFormat>, Box<C>)>,
done: mpsc::Receiver<Decoded>,
}
impl ChunkDecoder {
pub(super) fn new() -> Self {
let (jobs, todo) = mpsc::channel::<(Arc<ChunkFormat>, Box<C>)>();
let (finished, done) = mpsc::channel();
// The thread ends with the connection, when `jobs` is dropped.
let spawned = std::thread::Builder::new()
.name("Chunk decoder".into())
.spawn(move || {
// A frame waits for its columns, as it would for the render
// thread's own work.
horizon_threads::favour_current();
for (format, packet) in todo {
let column = match &*packet {
C::LevelChunkWithLight(p) => format.column(p),
_ => Err("not a chunk packet".into()),
};
if finished.send((packet, column)).is_err() {
break;
}
}
});
if let Err(e) = spawned {
log::warn!("Horizon: no chunk decoder thread ({e}); chunks decode in the frame");
}
Self { jobs, done }
}
/// Start decoding a chunk packet with the format it arrived under.
/// Without a decoder thread the packet comes back undecoded.
pub(super) fn submit(&self, format: Arc<ChunkFormat>, packet: Box<C>) -> Option<Box<C>> {
self.jobs.send((format, packet)).err().map(|e| (e.0).1)
}
/// The oldest submitted packet and its column, unless it is still
/// decoding: a frame never waits for the decoder.
pub(super) fn next(&self) -> Next {
match self.done.try_recv() {
Ok(decoded) => Next::Ready(decoded),
Err(TryRecvError::Empty) => Next::Decoding,
// The decoder thread panicked: its columns are gone.
Err(TryRecvError::Disconnected) => Next::Lost,
}
}
}
pub(super) enum Next {
Ready(Decoded),
Decoding,
Lost,
}
impl Game<'_> {
/// `handleLevelChunkWithLight`: the column replaces any loaded one, and
/// its block entities load their update tags. `decoded` is the column the
/// decoder made with `format`; another format since means decode again.
pub(super) fn remote_chunk(
&mut self,
p: &play::LevelChunkWithLight,
decoded: Option<(&Arc<ChunkFormat>, Result<Chunk, String>)>,
) {
// ClientChunkCache.replaceWithPacketData: a column outside the
// storage around the view centre is ignored.
if !self.remote_chunk_in_range(p.x, p.z) {
log::warn!(
"Ignoring chunk since it's not in the view range: {}, {}",
p.x,
p.z
);
return;
}
let Some(format) = self.remote.as_ref().map(|r| r.chunk_format.clone()) else {
return;
};
let column = match decoded {
Some((used, column)) if Arc::ptr_eq(used, &format) => column,
_ => format.column(p),
};
let chunk = match column {
Ok(chunk) => chunk,
Err(e) => {
log::warn!("Horizon: chunk {} {}: {e}", p.x, p.z);
return;
}
};
let (x, z) = (p.x, p.z);
self.streaming.insert_remote(&mut self.world, chunk);
self.world
.block_entities
.retain(|k, _| (k.0 >> 4, k.2 >> 4) != (x, z));
for info in &p.chunk.block_entities {
let pos = (
x * 16 + i32::from(info.packed_xz as u8 >> 4),
i32::from(info.y),
z * 16 + i32::from(info.packed_xz as u8 & 15),
);
let state = self.world.block(pos.0, pos.1, pos.2);
self.sync_block_entity(pos, state);
// LevelChunk.replaceWithPacketData: the block entity loads its update tag.
self.remote_block_entity_data([pos.0, pos.1, pos.2], info.kind, &info.tag);
}
}
/// `handleLightUpdatePacket`: the sections it names take its light.
pub(super) fn remote_light_update(&mut self, p: &play::LightUpdate) {
let Some(format) = self.remote.as_ref().map(|r| r.chunk_format.clone()) else {
return;
};
if !self.world.chunks.contains_key(&(p.x, p.z)) {
return;
}
let (layers, _) = format.light(&p.light);
let Some(chunk) = self.world.chunks.get_mut(&(p.x, p.z)) else {
return;
};
let changed = horizon_client::light::update_light_layers(
Arc::make_mut(chunk),
&layers,
format.has_skylight,
);
Streaming::light_updated(&mut self.world, p.x, p.z, &changed);
}
}
#[cfg(test)]
mod tests {
use super::*;
use horizon_protocol::chunk::{ChunkData, Paletted, Section as Wire, Strategy as Wiring};
/// A column of `state` with biome 1 everywhere, unlit.
fn packet(x: i32, state: i32) -> Box<C> {
let section = Wire {
non_empty: 4096,
fluids: 0,
states: Paletted::from_values(&[state; 4096], Wiring::Blocks, 16),
biomes: Paletted::from_values(&[1; 64], Wiring::Biomes, 2),
};
let none = LightData {
sky_mask: Vec::new(),
block_mask: Vec::new(),
empty_sky_mask: Vec::new(),
empty_block_mask: Vec::new(),
sky: Vec::new(),
block: Vec::new(),
};
Box::new(C::LevelChunkWithLight(play::LevelChunkWithLight {
x,
z: -3,
chunk: ChunkData {
heightmaps: Vec::new(),
buffer: ChunkData::from_sections(&vec![section; 24]),
block_entities: Vec::new(),
},
light: none,
}))
}
/// Columns come back one per packet, in the order the packets went in,
/// as the frame would have decoded them itself.
#[test]
fn the_decoder_returns_columns_in_order() {
let overworld = DimensionType::vanilla("minecraft:overworld");
let format = Arc::new(ChunkFormat::new(&overworld, vec![0, 7, 9], 2));
let decoder = ChunkDecoder::new();
for x in 0..20 {
assert!(decoder.submit(format.clone(), packet(x, 1 + x)).is_none());
}
for x in 0..20 {
let (packet, column) = loop {
match decoder.next() {
Next::Ready(decoded) => break decoded,
Next::Decoding => std::thread::yield_now(),
Next::Lost => panic!("the decoder stopped"),
}
};
let C::LevelChunkWithLight(p) = *packet else {
panic!("not a chunk packet");
};
let (column, direct) = (column.unwrap(), format.column(&p).unwrap());
assert_eq!((column.x, column.z), (x, -3));
for (cx, y, cz) in [(0, -64, 0), (5, 70, 9), (15, 319, 15)] {
assert_eq!(column.get(cx, y, cz), 1 + x as u32);
assert_eq!(column.get(cx, y, cz), direct.get(cx, y, cz));
// Wire biome 1 is our biome 7.
assert_eq!(column.biome(cx, y, cz), 7);
}
}
assert!(matches!(decoder.next(), Next::Decoding));
}
}
+6 -2
View File
@@ -639,8 +639,12 @@ impl Streaming {
self.lit.remove(&(x, z));
self.meshed.remove(&(x, z));
self.lighting.remove(&(x, z));
self.meshing.retain(|k, _| (k.0, k.2) != (x, z));
self.waiting.retain(|k| (k.0, k.2) != (x, z));
// By key, not by a pass over every pending section: a moved or
// shrunk storage forgets thousands of columns in one frame.
for sy in 0..SECTIONS as i32 {
self.meshing.remove(&(x, sy, z));
self.waiting.remove(&(x, sy, z));
}
world.dirty_sections.take_column(x, z);
world.chunks.remove(&(x, z))
}