psychopath/src/hash.rs
Nathan Vegdahl d71fd3b5c8 Implemented blue noise dithered sampling. Temporary.
After implementation, it does appear to make rendering slower
by a noticable bit compared to what I was doing before.  At very
low sampling rates it does provide a bit of visual improvement,
but by the time you get to even just 16 samples per pixel its
benefits seem to disappear.

Due to the slow down and the minimal gains, I'll be removing
this in the next commit.  But I want to commit it so I don't
lose the code, since it was an interesting experiment with
some promising results.
2017-05-14 12:25:01 -07:00

42 lines
986 B
Rust

use std;
pub fn hash_u32(n: u32, seed: u32) -> u32 {
let mut hash = n;
for _ in 0..3 {
hash = hash.wrapping_mul(1936502639);
hash ^= hash.wrapping_shr(16);
hash = hash.wrapping_add(seed);
}
return hash;
}
pub fn hash_u64(n: u64, seed: u64) -> u64 {
let mut hash = n;
for _ in 0..4 {
hash = hash.wrapping_mul(32416190071 * 314604959);
hash ^= hash.wrapping_shr(32);
hash = hash.wrapping_add(seed);
}
return hash;
}
/// Returns a random float in [0, 1] based on 'n' and a seed.
/// Generally use n for getting a bunch of different random
/// numbers, and use seed to vary between runs.
pub fn hash_u32_to_f32(n: u32, seed: u32) -> f32 {
let mut hash = n;
for _ in 0..3 {
hash = hash.wrapping_mul(1936502639);
hash ^= hash.wrapping_shr(16);
hash = hash.wrapping_add(seed);
}
const INV_MAX: f32 = 1.0 / std::u32::MAX as f32;
return hash as f32 * INV_MAX;
}