Faster Perlin volumes and climate search, same results

PerlinNoise::add_volume worked out each Y coordinate's cell, offset and
fade once per X/Z column. Every column walks the same Y coordinates, so
they are now computed once per volume. The arithmetic is unchanged, so
the output is bit for bit the same.

ClimateIndex kept its tree as nodes with a Vec of child ids each. The
built tree is now renumbered breadth first into flat arrays, so a
branch's children are one contiguous run. The search visits the same
nodes in the same order, so ties resolve exactly as before.

terrain_bench (seed 12345, 8x8 chunks, 1 thread, medians of 3 runs):
terrain 14.5 -> 12.4 ms/chunk, biomes 1.20 -> 1.06 ms/chunk.
Checked against vanilla 26.3 in-process: 2304 chunks on seeds 12345, -1
and 987654321987 identical, and the biome-source and biome-generation
parity suites (2.1M lines) match.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M3pNrSHA2ifNrH7SAMHgSW
This commit is contained in:
Claude
2026-10-22 02:42:12 +00:00
parent c1b5fe95d2
commit dfe795bc09
2 changed files with 84 additions and 39 deletions
+43 -15
View File
@@ -164,6 +164,31 @@ impl PerlinNoise {
return Err(DensityError::BufferTooShort);
}
let [size_x, size_y, size_z] = volume.sizes();
// Every X/Z column walks the same Y coordinates, so their cells, offsets
// and fades are computed once per volume instead of once per column.
let mut stack = [YSample::default(); STACK_Y];
let mut heap = Vec::new();
let ys = if size_y <= STACK_Y {
&mut stack[..size_y]
} else {
heap.resize(size_y, YSample::default());
&mut heap[..]
};
for (y, sample) in ys.iter_mut().enumerate() {
let input_y = f64::from(volume.coordinate(1, y)) * y_scale;
let shifted_y = wrap(input_y) + self.table.offsets[1];
let cell = floor_i32(shifted_y);
let dy = shifted_y - f64::from(cell);
*sample = YSample {
cell,
fade: smoothstep(dy as f32),
offset: match fudge_y_scale {
Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32,
None => dy as f32,
},
};
}
let ys = &*ys;
let mut index = 0;
for z in 0..size_z {
let shifted_z =
@@ -182,37 +207,40 @@ impl PerlinNoise {
let mut previous_y = i32::MIN;
let mut xz_dots = [0.0_f32; 8];
let mut y_gradients = [0.0_f32; 8];
for y in 0..size_y {
let input_y = f64::from(volume.coordinate(1, y)) * y_scale;
let shifted_y = wrap(input_y) + self.table.offsets[1];
let cell_y = floor_i32(shifted_y);
let dy = shifted_y - f64::from(cell_y);
let fade_y = smoothstep(dy as f32);
if previous_y != cell_y {
let gradients = self.gradients_at(p, q, cell_y, cell_z);
for (sample, out) in ys.iter().zip(&mut buffer[index..index + size_y]) {
if previous_y != sample.cell {
let gradients = self.gradients_at(p, q, sample.cell, cell_z);
for i in 0..8 {
xz_dots[i] = gradients[i][0] as f32 * (dx - (i & 1) as f32)
+ gradients[i][2] as f32 * (dz - ((i >> 2) & 1) as f32);
y_gradients[i] = gradients[i][1] as f32;
}
previous_y = cell_y;
previous_y = sample.cell;
}
let dy = match fudge_y_scale {
Some(scale) => (dy - compute_fudge_y(input_y, dy, scale)) as f32,
None => dy as f32,
};
let dy = sample.offset;
let values = std::array::from_fn(|i| {
xz_dots[i] + y_gradients[i] * (dy - ((i >> 1) & 1) as f32)
});
buffer[index] += amplitude * lerp3([fade_x, fade_y, fade_z], values);
index += 1;
*out += amplitude * lerp3([fade_x, sample.fade, fade_z], values);
}
index += size_y;
}
}
Ok(())
}
}
/// Y columns up to this height keep their per-Y samples on the stack.
const STACK_Y: usize = 64;
#[derive(Clone, Copy, Default)]
struct YSample {
cell: i32,
fade: f32,
/// The Y offset inside the cell used for the corner values, after any fudge.
offset: f32,
}
#[derive(Clone, Debug)]
pub struct SmearedPerlinNoise {
base: PerlinNoise,