Minor cleanup of the Owen scramble code.

This commit is contained in:
Nathan Vegdahl 2022-07-23 14:17:38 -07:00
parent f95e869848
commit ef489c1ca2

View File

@ -1,3 +1,5 @@
#![allow(dead_code)]
/// Performs a base-2 Owen scramble on an integer.
pub fn owen2(n: u32, seed: u32) -> u32 {
// Multiply by a large random prime and xor by a random number.
@ -7,7 +9,6 @@ pub fn owen2(n: u32, seed: u32) -> u32 {
let seed = seed.wrapping_mul(0xe8559dcb) ^ 0x372fcdb9;
let mut result = n;
for i in 0..32 {
let mask = (!0 << 1) << i; // Two shifts to avoid undefined overflow.
result ^= hash((n & mask) ^ seed) & (1 << i);
@ -17,17 +18,6 @@ pub fn owen2(n: u32, seed: u32) -> u32 {
}
/// Performs a base-4 Owen scramble on an integer.
///
/// This is motivated by z-scrambling from the paper "Screen-Space
/// Blue-Noise Diffusion of Monte Carlo Sampling Error via Hierarchical
/// Ordering of Pixels" by Ahmed et al. Their scrambling function is
/// actually just Owen scrambling in base 4, but their attempt at
/// implementing that function isn't great, both in terms of memory
/// usage and scramble quality. (The paper itself is otherwise
/// fantastic, though.)
///
/// The implementation below is a full proper base-4 Owen scramble,
/// requiring only a 24-byte lookup table.
pub fn owen4(n: u32, seed: u32) -> u32 {
// Bit-packed permutation table.
const PERMUTATION_TABLE: [u8; 24] = [
@ -64,7 +54,6 @@ pub fn owen4(n: u32, seed: u32) -> u32 {
let seed = seed.wrapping_mul(0xe8559dcb) ^ 0x372fcdb9;
let mut result = 0;
for i in 0..16 {
let mask = (!0 << 2) << (i * 2); // Two shifts to avoid undefined overflow.
let perm_entry = PERMUTATION_TABLE[
@ -75,7 +64,6 @@ pub fn owen4(n: u32, seed: u32) -> u32 {
(hash((n & mask) ^ seed ^ (i << 16)) % 24) as usize
];
let perm_cell_idx = ((n >> (i * 2)) & 0b11) as usize;
result |= (((perm_entry >> (perm_cell_idx * 2)) & 0b11) as u32) << (i * 2);
}