Fixed rng stuff.

This commit is contained in:
Acrimon 2019-09-17 16:56:23 +02:00
parent 5487c8b7bc
commit 2786e28577
3 changed files with 14 additions and 27 deletions

View File

@ -17,7 +17,7 @@ impl<K: Hash + Eq + Clone, V> HashCache<K, V> {
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
Self { Self {
capacity, capacity,
map: HashMap::default(), map: HashMap::with_capacity(1024),
counter: 0, counter: 0,
} }
} }

View File

@ -1,5 +1,6 @@
use super::Sampler; use super::Sampler;
use vek::*; use vek::*;
use super::seed_expan;
pub struct RandomField { pub struct RandomField {
seed: u32, seed: u32,
@ -17,22 +18,7 @@ impl Sampler<'static> for RandomField {
fn get(&self, pos: Self::Index) -> Self::Sample { fn get(&self, pos: Self::Index) -> Self::Sample {
let pos = pos.map(|e| u32::from_le_bytes(e.to_le_bytes())); let pos = pos.map(|e| u32::from_le_bytes(e.to_le_bytes()));
seed_expan::diffuse_mult(&[self.seed, pos.x, pos.y, pos.z])
let mut a = self.seed;
a = (a ^ 61) ^ (a >> 16);
a = a.wrapping_add(a << 3);
a = a ^ pos.x;
a = a ^ (a >> 4);
a = a.wrapping_mul(0x27d4eb2d);
a = a ^ (a >> 15);
a = a ^ pos.y;
a = (a ^ 61) ^ (a >> 16);
a = a.wrapping_add(a << 3);
a = a ^ (a >> 4);
a = a ^ pos.z;
a = a.wrapping_mul(0x27d4eb2d);
a = a ^ (a >> 15);
a
} }
} }
@ -51,14 +37,6 @@ impl Sampler<'static> for RandomPerm {
type Sample = u32; type Sample = u32;
fn get(&self, perm: Self::Index) -> Self::Sample { fn get(&self, perm: Self::Index) -> Self::Sample {
let a = self seed_expan::diffuse_mult(&[self.seed, perm])
.seed
.wrapping_mul(3471)
.wrapping_add(perm)
.wrapping_add(0x3BE7172B)
.wrapping_mul(perm)
.wrapping_add(0x172A3BE1);
let b = a.wrapping_mul(a);
b ^ (a >> 17) ^ b >> 15
} }
} }

View File

@ -20,10 +20,19 @@ pub fn diffuse(mut x: u32) -> u32 {
x = x.wrapping_add(0xd3a2646c) ^ (x << 9); x = x.wrapping_add(0xd3a2646c) ^ (x << 9);
x = x.wrapping_add(0xfd7046c5).wrapping_add(x << 3); x = x.wrapping_add(0xfd7046c5).wrapping_add(x << 3);
x = (x ^ 0xb55a4f09) ^ (x >> 16); x = (x ^ 0xb55a4f09) ^ (x >> 16);
x = x.wrapping_add((1 << 31) - 1) ^ (x << 13); x = x.wrapping_add((1 << 31) - 13) ^ (x << 13);
x x
} }
/// Diffuse but takes multiple values as input.
pub fn diffuse_mult(v: &[u32]) -> u32 {
let mut state = (1 << 31) - 1;
for e in v {
state = diffuse(state ^ e);
}
state
}
/// Expand a 32 bit seed into a 32 byte RNG state. /// Expand a 32 bit seed into a 32 byte RNG state.
pub fn rng_state(mut x: u32) -> [u8; 32] { pub fn rng_state(mut x: u32) -> [u8; 32] {
let mut r: [u32; 8] = [0; 8]; let mut r: [u32; 8] = [0; 8];