diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 4ba6f1152..69a29217c 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -61,10 +61,78 @@ pub struct Heard { pub struct MixerBackend { mixer: Arc>, root: PathBuf, - cache: HashMap>, u32)>>, + cache: Arc>, + /// The decoder thread for static sounds, started with the first one. + decoder: Option>, next: ChannelId, live: bool, } +/// Decoded samples and their rate (`None`: the file did not load). +type Samples = Option<(Arc>, u32)>; +/// Decoded static sounds (`SoundBufferLibrary`'s buffers), dropped least +/// recently used first past `CACHE_BYTES`: kept forever, every sound heard in +/// a session stayed (up to 1.4 GiB of samples, villages playing hundreds). +#[derive(Default)] +struct SoundCache { + entries: HashMap, + bytes: usize, + clock: u64, +} +const CACHE_BYTES: usize = 64 << 20; +impl SoundCache { + fn get(&mut self, key: &str) -> Option { + self.clock += 1; + let entry = self.entries.get_mut(key)?; + entry.1 = self.clock; + Some(entry.0.clone()) + } + fn insert(&mut self, key: String, samples: Samples) { + self.clock += 1; + self.bytes += Self::size(&samples); + if let Some((old, _)) = self.entries.insert(key, (samples, self.clock)) { + self.bytes -= Self::size(&old); + } + while self.bytes > CACHE_BYTES && self.entries.len() > 1 { + let Some(oldest) = self + .entries + .iter() + .min_by_key(|(_, (_, used))| *used) + .map(|(k, _)| k.clone()) + else { + break; + }; + if let Some((gone, _)) = self.entries.remove(&oldest) { + self.bytes -= Self::size(&gone); + } + } + } + fn size(samples: &Samples) -> usize { + samples + .as_ref() + .map_or(0, |(s, _)| s.len() * std::mem::size_of::()) + } +} +/// A static sound to decode for a channel waiting on it. +struct Decode { + key: String, + file: PathBuf, + channel: ChannelId, +} +/// Give a channel waiting on its sound the decoded samples and start it, as +/// the buffer future attaching and playing the source. A source stopped or +/// released meanwhile stays so. +fn attach(mixer: &Mutex, channel: ChannelId, samples: Samples, looping: bool) { + if let Some(samples) = samples + && let Ok(mut m) = mixer.lock() + && let Some(c) = m.channels.get_mut(&channel) + && c.state == State::Initial + { + c.samples = Some(samples); + c.position = 0.0; + c.state = State::Playing; + c.looping |= looping; + } +} pub struct Audio { _stream: Option, pub engine: SoundEngine, @@ -176,53 +244,59 @@ impl Backend for MixerBackend { fn set_relative(&mut self, channel: ChannelId, relative: bool) { self.with(channel, |c| c.relative = relative); } - /// `getCompleteBuffer` (decoded now, cached) or `getStream` (decoded on a - /// worker), then attach and `alSourcePlay`. A sound that cannot load - /// leaves its source initial, as a failed buffer future does in vanilla. + /// `getCompleteBuffer` (cached, else decoded on the decoder thread) or + /// `getStream` (decoded on a thread of its own), then attach and + /// `alSourcePlay`. A sound that cannot load leaves its source initial, + /// as a failed buffer future does in vanilla. fn start(&mut self, channel: ChannelId, sound: &Sound, looping: bool) { if !self.live { return; } let (namespace, path) = sound.path(); let file = self.root.join("assets").join(namespace).join(&path); - let attach = move |mixer: &Mutex, samples: Option<(Arc>, u32)>| { - if let Some(samples) = samples - && let Ok(mut m) = mixer.lock() - && let Some(c) = m.channels.get_mut(&channel) - { - c.samples = Some(samples); - c.position = 0.0; - c.state = State::Playing; - } - }; if sound.stream { // A streamed sound loops by restarting its stream. let mixer = self.mixer.clone(); std::thread::spawn(move || { let samples = decode(&file).map(|(s, r)| (Arc::new(s), r)); - if looping - && let Ok(mut m) = mixer.lock() - && let Some(c) = m.channels.get_mut(&channel) - { - c.looping = true; - } - attach(&mixer, samples); + attach(&mixer, channel, samples, looping); }); - } else { - let samples = self - .cache - .entry(format!("{namespace}:{path}")) - .or_insert_with(|| decode(&file).map(|(s, r)| (Arc::new(s), r))) - .clone(); - attach(&self.mixer, samples); + return; } - } - fn stop(&mut self, channel: ChannelId) { - self.with(channel, |c| { - if c.state != State::Initial { - c.state = State::Stopped; - } + let key = format!("{namespace}:{path}"); + let cached = self.cache.lock().ok().and_then(|mut c| c.get(&key)); + if let Some(samples) = cached { + attach(&self.mixer, channel, samples, false); + return; + } + let decoder = self.decoder.get_or_insert_with(|| { + let (send, receive) = std::sync::mpsc::channel::(); + let (mixer, cache) = (self.mixer.clone(), self.cache.clone()); + std::thread::Builder::new() + .name("sound decoder".into()) + .spawn(move || { + for job in receive { + // Queued twice before the first decode finished. + let cached = cache.lock().ok().and_then(|mut c| c.get(&job.key)); + let samples = cached.unwrap_or_else(|| { + let samples = decode(&job.file).map(|(s, r)| (Arc::new(s), r)); + if let Ok(mut c) = cache.lock() { + c.insert(job.key, samples.clone()); + } + samples + }); + attach(&mixer, job.channel, samples, false); + } + }) + .expect("sound decoder thread"); + send }); + let _ = decoder.send(Decode { key, file, channel }); + } + /// `alSourceStop`: any state becomes stopped, an initial source too (a + /// sound still decoding then never starts). + fn stop(&mut self, channel: ChannelId) { + self.with(channel, |c| c.state = State::Stopped); } fn pause(&mut self, channel: ChannelId) { self.with(channel, |c| { @@ -425,7 +499,8 @@ impl Audio { backend: MixerBackend { mixer, root: root.to_owned(), - cache: HashMap::new(), + cache: Arc::default(), + decoder: None, next: 0, live, }, @@ -598,6 +673,21 @@ impl Audio { self.release(id); } /// Every sound event id (for command completion). + /// The engine's table sizes and the mixer's channels (`status`). + pub fn census(&self) -> serde_json::Value { + let mut census = self.engine.census(); + census["channels"] = self + .backend + .mixer + .lock() + .map_or(0, |m| m.channels.len()) + .into(); + if let Ok(cache) = self.backend.cache.lock() { + census["decoded"] = cache.entries.len().into(); + census["decoded_mib"] = (cache.bytes >> 20).into(); + } + census + } pub fn event_names(&self) -> Vec { let mut v: Vec = self.engine.registry.names().cloned().collect(); v.sort(); @@ -663,6 +753,26 @@ mod tests { } } + #[test] + fn decoded_sounds_stay_within_the_budget_least_recently_used_out() { + let mut cache = SoundCache::default(); + let quarter = CACHE_BYTES / 4 / std::mem::size_of::(); + let sound = || Some((Arc::new(vec![0.0f32; quarter]), 44100)); + for key in ["a", "b", "c", "d"] { + cache.insert(key.into(), sound()); + } + assert!(cache.get("a").is_some()); + cache.insert("e".into(), sound()); + assert!(cache.bytes <= CACHE_BYTES); + assert!( + cache.get("b").is_none(), + "the least recently used goes first" + ); + assert!(cache.get("a").is_some() && cache.get("e").is_some()); + // A failed load is remembered without counting. + cache.insert("missing".into(), None); + assert_eq!(cache.get("missing"), Some(None)); + } #[test] fn output_stays_off_on_a_virtual_screen_unless_asked() { let env = |pairs: &'static [(&'static str, &'static str)]| { diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index e11776548..e7b7b546c 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -491,6 +491,7 @@ impl Game<'_> { "present_mode": format!("{:?}", r.config.present_mode), "drawn_sections": r.drawn_sections, "uploaded_bytes": r.uploaded_bytes, + "resident_sections": r.resident_sections(), }) }); json!({ @@ -510,6 +511,7 @@ impl Game<'_> { "ticks": self.ticks, "day_time": self.world.day_time, "music": self.music.current(&self.audio.engine), + "sounds": self.audio.census(), "fishing": self.hook_status(), "tridents": self.tridents.len(), "rockets": self.rockets.len(), @@ -519,6 +521,8 @@ impl Game<'_> { "using": self.using, "render_distance": self.streaming.radius, "chunks": self.world.chunks.len(), + "mobs": self.mobs.len(), + "block_entities": self.world.block_entities.len(), "pending_chunks": self.streaming.pending(), "generated_chunks": self.streaming.generated_total, "meshed_sections": self.streaming.meshed_total, diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 3d45098b0..e153b0e52 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -226,6 +226,12 @@ const NO_GPU_DRIVER: &str = "No Metal graphics device found"; #[cfg(not(any(target_os = "windows", target_os = "macos")))] const NO_GPU_DRIVER: &str = "No Vulkan graphics driver found: Horizon needs the Vulkan loader \ (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's)"; +impl Renderer { + /// Sections with GPU buffers, for soak tests watching residency. + pub fn resident_sections(&self) -> usize { + self.sections.len() + } +} impl Renderer { pub async fn new( window: Arc, diff --git a/crates/horizon-client/src/sound_engine.rs b/crates/horizon-client/src/sound_engine.rs index 288e85faf..327fceaaa 100644 --- a/crates/horizon-client/src/sound_engine.rs +++ b/crates/horizon-client/src/sound_engine.rs @@ -445,6 +445,17 @@ impl SoundEngine { self.instances.remove(&id); } } + /// Table sizes, for soak tests watching what the engine holds. + pub fn census(&self) -> serde_json::Value { + serde_json::json!({ + "instances": self.instances.len(), + "playing": self.instance_to_channel.len(), + "handles": self.handles.len(), + "ticking": self.ticking.len(), + "queued": self.queued.len(), + "used": self.used, + }) + } pub fn instance(&self, id: InstanceId) -> Option<&SoundInstance> { self.instances.get(&id) } diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index e4a3063b3..09deb0b75 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -358,20 +358,24 @@ impl Streaming { } Done::Generated(mut chunk) => { let key = (chunk.x, chunk.z); - world - .block_entities - .extend(std::mem::take(&mut chunk.block_entities)); - world - .block_names - .extend(std::mem::take(&mut chunk.block_names)); - let entities = std::mem::take(&mut chunk.entities); - if !entities.is_empty() { - world.loaded_entities.push(((chunk.x, chunk.z), entities)); - } self.generating.remove(&key); self.finishing.remove(&key); self.decorated.insert(key); + // A column that finished after the player moved away is + // dropped whole: its block entities and saved entities + // too, or they would stay in the world (and the entities + // spawn again, duplicated, when the column loads later). if self.in_range(key.0, key.1, 2) || self.near_finished(key.0, key.1, 1) { + world + .block_entities + .extend(std::mem::take(&mut chunk.block_entities)); + world + .block_names + .extend(std::mem::take(&mut chunk.block_names)); + let entities = std::mem::take(&mut chunk.entities); + if !entities.is_empty() { + world.loaded_entities.push(((chunk.x, chunk.z), entities)); + } self.generated_total += 1; // A chunk saved with its light needs no light job; its // unlit neighbours still light themselves from it. @@ -424,6 +428,12 @@ impl Streaming { if self.meshing.get(&(x, sy, z)) != Some(&seq) { continue; } + // Its column unloaded while it was meshed: nothing would + // ever remove the uploaded section. + if !world.chunks.contains_key(&(x, z)) { + self.meshing.remove(&(x, sy, z)); + continue; + } self.meshing.remove(&(x, sy, z)); self.meshed_total += 1; uploads.push(((x, sy, z), mesh)); @@ -449,6 +459,13 @@ impl Streaming { } self.lit.remove(key); self.meshed.remove(key); + self.lighting.remove(key); + self.light_snapshots.remove(key); + } + if !far.is_empty() { + let far: HashSet<(i32, i32)> = far.iter().copied().collect(); + self.meshing.retain(|k, _| !far.contains(&(k.0, k.2))); + world.dirty_sections.retain(|k| !far.contains(&(k.0, k.2))); } // Drop far proto chunks that are not checked out. let areas: Vec<((i32, i32), i64)> =