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:
@@ -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,
|
||||
|
||||
@@ -202,8 +202,11 @@ fn build(
|
||||
#[derive(Debug)]
|
||||
pub struct ClimateIndex<T> {
|
||||
entries: Vec<(ParameterPoint, T)>,
|
||||
nodes: Vec<Node>,
|
||||
root: usize,
|
||||
/// The built tree renumbered breadth first, root at 0, so each branch's
|
||||
/// children are one contiguous run and a search walks memory in order.
|
||||
spaces: Vec<[Parameter; 7]>,
|
||||
/// Per node: its first child and child count, or its entry and 0 for a leaf.
|
||||
links: Vec<(u32, u32)>,
|
||||
}
|
||||
impl<T> ClimateIndex<T> {
|
||||
pub fn new(entries: Vec<(ParameterPoint, T)>) -> Result<Self, IndexError> {
|
||||
@@ -232,10 +235,25 @@ impl<T> ClimateIndex<T> {
|
||||
.collect();
|
||||
let children = (0..nodes.len()).collect();
|
||||
let root = build(&mut nodes, children, branching)?;
|
||||
let mut order = vec![root];
|
||||
let mut links = Vec::with_capacity(nodes.len());
|
||||
let mut next = 0;
|
||||
while next < order.len() {
|
||||
let node = &nodes[order[next]];
|
||||
next += 1;
|
||||
links.push(match &node.children {
|
||||
Children::Leaf(entry) => (*entry as u32, 0),
|
||||
Children::Branch(children) => {
|
||||
let first = order.len() as u32;
|
||||
order.extend_from_slice(children);
|
||||
(first, children.len() as u32)
|
||||
}
|
||||
});
|
||||
}
|
||||
Ok(Self {
|
||||
entries,
|
||||
nodes,
|
||||
root,
|
||||
spaces: order.iter().map(|id| nodes[*id].space).collect(),
|
||||
links,
|
||||
})
|
||||
}
|
||||
pub fn entries(&self) -> &[(ParameterPoint, T)] {
|
||||
@@ -263,18 +281,18 @@ impl<T> ClimateIndex<T> {
|
||||
}
|
||||
/// Preorder leaf entry indices expose stable tree layout for comparison.
|
||||
pub fn leaf_order(&self) -> Vec<usize> {
|
||||
fn walk(nodes: &[Node], id: usize, out: &mut Vec<usize>) {
|
||||
match &nodes[id].children {
|
||||
Children::Leaf(entry) => out.push(*entry),
|
||||
Children::Branch(children) => {
|
||||
for child in children {
|
||||
walk(nodes, *child, out);
|
||||
fn walk(links: &[(u32, u32)], id: usize, out: &mut Vec<usize>) {
|
||||
match links[id] {
|
||||
(entry, 0) => out.push(entry as usize),
|
||||
(first, count) => {
|
||||
for child in first..first + count {
|
||||
walk(links, child as usize, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut result = Vec::with_capacity(self.entries.len());
|
||||
walk(&self.nodes, self.root, &mut result);
|
||||
walk(&self.links, 0, &mut result);
|
||||
result
|
||||
}
|
||||
fn search(
|
||||
@@ -283,19 +301,20 @@ impl<T> ClimateIndex<T> {
|
||||
values: &[i64; 7],
|
||||
previous: Option<usize>,
|
||||
) -> Result<usize, IndexError> {
|
||||
let Children::Branch(children) = &self.nodes[id].children else {
|
||||
let (first, count) = self.links[id];
|
||||
if count == 0 {
|
||||
return Ok(id);
|
||||
};
|
||||
}
|
||||
let mut best = previous;
|
||||
let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.nodes[id].space, values));
|
||||
for child in children {
|
||||
let lower = distance(&self.nodes[*child].space, values);
|
||||
let mut fitness = previous.map_or(i64::MAX, |id| distance(&self.spaces[id], values));
|
||||
for child in first as usize..(first + count) as usize {
|
||||
let lower = distance(&self.spaces[child], values);
|
||||
if fitness > lower {
|
||||
let result = self.search(*child, values, best)?;
|
||||
let next = if result == *child {
|
||||
let result = self.search(child, values, best)?;
|
||||
let next = if result == child {
|
||||
lower
|
||||
} else {
|
||||
distance(&self.nodes[result].space, values)
|
||||
distance(&self.spaces[result], values)
|
||||
};
|
||||
if fitness > next {
|
||||
fitness = next;
|
||||
@@ -313,13 +332,11 @@ pub struct ClimateSearch<'a, T> {
|
||||
}
|
||||
impl<'a, T> ClimateSearch<'a, T> {
|
||||
pub fn find(&mut self, target: TargetPoint) -> Result<&'a T, IndexError> {
|
||||
let result = self
|
||||
.index
|
||||
.search(self.index.root, &target.parameters(), self.last)?;
|
||||
let result = self.index.search(0, &target.parameters(), self.last)?;
|
||||
self.last = Some(result);
|
||||
let Children::Leaf(entry) = self.index.nodes[result].children else {
|
||||
let (entry, 0) = self.index.links[result] else {
|
||||
unreachable!("search returns a leaf");
|
||||
};
|
||||
Ok(&self.index.entries[entry].1)
|
||||
Ok(&self.index.entries[entry as usize].1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user