Massively sped up VolMap offset calculations

Former-commit-id: 8f3cdf57a77691ca60c0921bc86a79c8cfe36539
This commit is contained in:
Joshua Barretto 2019-05-13 10:32:02 +01:00
parent 216819ca92
commit edfd2290eb
2 changed files with 23 additions and 8 deletions

View File

@ -110,7 +110,7 @@ impl State {
ecs.add_resource(TimeOfDay(0.0));
ecs.add_resource(Time(0.0));
ecs.add_resource(DeltaTime(0.0));
ecs.add_resource(TerrainMap::new());
ecs.add_resource(TerrainMap::new().unwrap());
}
/// Register a component with the state's ECS

View File

@ -19,6 +19,7 @@ pub enum VolMapErr {
NoSuchChunk,
ChunkErr(ChunkErr),
DynaErr(DynaErr),
InvalidChunkSize,
}
// V = Voxel
@ -31,13 +32,20 @@ pub struct VolMap<V: Vox + Clone, S: VolSize + Clone, M: Clone> {
impl<V: Vox + Clone, S: VolSize + Clone, M: Clone> VolMap<V, S, M> {
#[inline(always)]
fn chunk_key(pos: Vec3<i32>) -> Vec3<i32> {
pos.map2(S::SIZE, |e, sz| e.div_euclid(sz as i32))
pub fn chunk_key(pos: Vec3<i32>) -> Vec3<i32> {
pos.map2(S::SIZE, |e, sz| {
// Horrid, but it's faster than a cheetah with a red bull blood transfusion
let log2 = (sz - 1).count_ones();
((((e as i64 + (1 << 32)) as u64) >> log2) - (1 << (32 - log2))) as i32
})
}
#[inline(always)]
pub fn chunk_offs(pos: Vec3<i32>) -> Vec3<i32> {
pos.map2(S::SIZE, |e, sz| e.rem_euclid(sz as i32))
pos.map2(S::SIZE, |e, sz| {
// Horrid, but it's even faster than the aforementioned cheetah
(((e as i64 + (1 << 32)) as u64) & (sz - 1) as u64) as i32
})
}
}
@ -150,10 +158,17 @@ impl<V: Vox + Clone, S: VolSize + Clone, M: Clone> WriteVol for VolMap<V, S, M>
}
}
impl<V: Vox + Clone, S: VolSize + Clone, M: Clone> VolMap<V, S, M> {
pub fn new() -> Self {
Self {
chunks: HashMap::new(),
impl<V: Vox + Clone, S: VolSize + Clone, M + Clone> VolMap<V, S, M> {
pub fn new() -> Result<Self, VolMapErr> {
if Self::chunk_size()
.map(|e| e.is_power_of_two() && e > 0)
.reduce_and()
{
Ok(Self {
chunks: HashMap::new(),
})
} else {
Err(VolMapErr::InvalidChunkSize)
}
}