From 2fe7cb3a4bb156daab731ff5b031621f6e39c763 Mon Sep 17 00:00:00 2001 From: rdbtCVS <87152661+rdbtCVS@users.noreply.github.com> Date: Mon, 19 Oct 2026 01:23:23 +0100 Subject: [PATCH] Reuse terrain sampler scratch buffers between chunks --- crates/horizon-core/src/density/mod.rs | 7 ++ crates/horizon-core/src/density/tests.rs | 70 +++++++++++++++++++ .../horizon-world/src/worldgen/generator.rs | 26 +++++-- 3 files changed, 97 insertions(+), 6 deletions(-) diff --git a/crates/horizon-core/src/density/mod.rs b/crates/horizon-core/src/density/mod.rs index 6bc84a8d1..f4ee1af49 100644 --- a/crates/horizon-core/src/density/mod.rs +++ b/crates/horizon-core/src/density/mod.rs @@ -103,6 +103,13 @@ impl SamplerContext { self.scratch.push(buffer); result } + /// Start an independent evaluation, retaining only temporary scratch buffers. + /// Cached scalar/volume results and world bindings must not cross chunks or + /// graphs, whose cache IDs can refer to different functions. + pub fn reset(&mut self) { + self.caches = self.caches.as_ref().map(|_| Vec::new()); + self.fields = WorldFields::default(); + } /// Release retained storage between worlds or after unusually large work. pub fn clear_scratch(&mut self) { self.scratch.clear(); diff --git a/crates/horizon-core/src/density/tests.rs b/crates/horizon-core/src/density/tests.rs index 77a643097..9fad8bc4f 100644 --- a/crates/horizon-core/src/density/tests.rs +++ b/crates/horizon-core/src/density/tests.rs @@ -755,3 +755,73 @@ fn spline_lazy_branch_failure_preserves_completed_output_and_returns_scratch() { assert_eq!(context.scratch.len(), 2); assert_eq!(sampler.sample_value(&mut context, [0; 3]), Ok(7.0)); } + +#[test] +fn reset_retains_scratch_but_discards_graph_caches_and_world_bindings() { + let binding = Arc::new(ConstantSampler(17.0)); + let lifetime = Arc::downgrade(&binding); + let mut context = SamplerContext::cached() + .with_world_fields(WorldFields::default().with_field(WorldField::Beardifier, binding)); + let cached = CachingDensitySampler::new( + 0, + Arc::new(ContextBoundSampler::new(WorldField::Beardifier)), + ) + .unwrap(); + let volume = DensityVolume::new([2; 3], [0; 3], [1; 3]).unwrap(); + let mut output = [0.0; 8]; + cached + .sample_volume(&mut context, &mut output, volume) + .unwrap(); + assert_eq!(output, [17.0; 8]); + assert_eq!(cached.sample_value(&mut context, [9; 3]), Ok(17.0)); + context + .with_buffer(128, |_, buffer| { + buffer.fill(99.0); + Ok(()) + }) + .unwrap(); + let allocation = context.scratch[0].as_ptr(); + let capacity = context.scratch[0].capacity(); + context.reset(); + assert!(lifetime.upgrade().is_none()); + assert!(context.caches.as_ref().unwrap().is_empty()); + assert_eq!(context.scratch[0].as_ptr(), allocation); + assert_eq!(context.scratch[0].capacity(), capacity); + // Same coordinates and cache ID must resolve the new context's fallback. + assert_eq!(cached.sample_value(&mut context, [9; 3]), Ok(0.0)); + cached + .sample_volume(&mut context, &mut output, volume) + .unwrap(); + assert_eq!(output, [0.0; 8]); + // A different graph may reuse IDs without inheriting scalar/volume results. + context.reset(); + let replacement = CachingDensitySampler::new(0, Arc::new(ConstantSampler(3.0))).unwrap(); + assert_eq!(replacement.sample_value(&mut context, [9; 3]), Ok(3.0)); + replacement + .sample_volume(&mut context, &mut output, volume) + .unwrap(); + assert_eq!(output, [3.0; 8]); +} + +#[test] +fn reset_after_sampling_error_drops_blender_and_preserves_uncached_mode() { + let blender = Arc::new(EmptyBlender); + let lifetime = Arc::downgrade(&blender); + let mut context = + SamplerContext::default().with_world_fields(WorldFields::default().with_blender(blender)); + let result: Result<(), SampleError> = context.with_buffer(64, |_, buffer| { + buffer.fill(42.0); + Err(SampleError::RecursiveCache) + }); + assert_eq!(result, Err(SampleError::RecursiveCache)); + let allocation = context.scratch[0].as_ptr(); + context.reset(); + assert!(lifetime.upgrade().is_none()); + assert!(context.caches.is_none()); + context + .with_buffer(32, |_, buffer| { + assert_eq!(buffer.as_ptr(), allocation); + Ok(()) + }) + .unwrap(); +} diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 7b73d35a8..068fdf5bc 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -933,6 +933,19 @@ where &self, worker: &mut WorkerState, pos: ChunkPos, + ) -> Result { + let mut context = std::mem::take(&mut worker.terrain_context); + let result = self.generate_with_context(worker, pos, &mut context); + // Release chunk-specific bindings and caches even after a sampling error. + context.reset(); + worker.terrain_context = context; + result + } + fn generate_with_context( + &self, + worker: &mut WorkerState, + pos: ChunkPos, + context: &mut SamplerContext, ) -> Result { let data = &self.data; let (min, height, sea) = ( @@ -985,13 +998,12 @@ where ), None => Vec::new(), }; - let mut context = SamplerContext::cached(); if let Some(beard) = self .structures .as_ref() .and_then(|s| s.beardifier(&references, pos)) { - context = context.with_world_fields( + *context = std::mem::take(context).with_world_fields( horizon_core::density::WorldFields::default().with_field( horizon_core::density::WorldField::Beardifier, Arc::new(beard), @@ -1016,7 +1028,7 @@ where .fork_positional(), RegistryAquiferBlockStates::new(data.registry).map_err(stage("aquifer"))?, volume, - &mut context, + context, ) .map_err(stage("aquifer"))?, ), @@ -1030,7 +1042,7 @@ where &mut chunk, volume, self.router.get(RouterField::FinalDensity).as_ref(), - &mut context, + context, aquifer.as_mut(), self.settings.default_block, ) @@ -1051,7 +1063,7 @@ where possible: Some(possible), }, preliminary_surface: self.preliminary.clone(), - density_context: &mut context, + density_context: context, write_min_y: min, write_max_y: min + height - 1, }, @@ -1086,7 +1098,7 @@ where possible: None, }, preliminary_surface: self.preliminary.clone(), - density_context: &mut context, + density_context: context, }, aquifer.as_mut(), &data.carving_blocks, @@ -1391,6 +1403,7 @@ pub struct StageTimings { /// thread-local caches and never change results. pub(super) struct WorkerState { pub(super) temperatures: Vec, + terrain_context: SamplerContext, biome_context: SamplerContext, carver_context: SamplerContext, timings: StageTimings, @@ -1866,6 +1879,7 @@ impl OverworldGenerator { .iter() .map(|s| BiomeTemperature::new(*s)) .collect(), + terrain_context: SamplerContext::cached(), biome_context: SamplerContext::cached(), carver_context: SamplerContext::default(), timings: StageTimings::default(),