Skip to main content

quick_noise/noise/generators/value/
batch_3d.rs

1use simply_simd::{Arch, Simd};
2
3use crate::api::batch::interface::BatchGenerator;
4use crate::noise::generators::Value;
5use crate::noise::util::constants::{BYTE_SHUFFLE, VALUE_EXP_MASK, HASH_MASK, HASH_PRIME};
6
7impl BatchGenerator<3> for Value {
8    #[inline(always)]
9    fn sample_batch<A: Arch>(
10        seed: u32,
11        input: [Simd<f32, A>; 3],
12        freq: [Simd<f32, A>; 3],
13    ) -> Simd<f32, A> {
14        // Constants.
15        let neg_two: Simd<f32, A> = Simd::splat(-2.0);
16        let three: Simd<f32, A> = Simd::splat(3.0);
17
18        let hash_mask: Simd<u32, A> = Simd::splat(HASH_MASK);
19        let exp_bits: Simd<u32, A> = Simd::splat(VALUE_EXP_MASK);
20
21        let shuffle_indices = Simd::<u8, A>::from_slice(&BYTE_SHUFFLE[..]);
22        let channel_seed = Simd::splat(seed);
23        let prime = Simd::splat(HASH_PRIME);
24
25        // Scale: 3
26        let x_scaled = input[0] * freq[0];
27        let y_scaled = input[1] * freq[1];
28        let z_scaled = input[2] * freq[2];
29
30        // Gridpoints and distances: 9
31        let x_scaled_floored = x_scaled.floor();
32        let y_scaled_floored = y_scaled.floor();
33        let z_scaled_floored = z_scaled.floor();
34
35        let x_grid_lo = x_scaled_floored.cast_int_trunc();
36        let y_grid_lo = y_scaled_floored.cast_int_trunc();
37        let z_grid_lo = z_scaled_floored.cast_int_trunc();
38
39        let x_dist_lo = x_scaled - x_scaled_floored;
40        let y_dist_lo = y_scaled - y_scaled_floored;
41        let z_dist_lo = z_scaled - z_scaled_floored;
42
43        // Lerp fade calculation: 9
44        let t = x_dist_lo;
45        let s = y_dist_lo;
46        let u = z_dist_lo;
47        let x_lerp = t * t * t.mul_add(neg_two, three);
48        let y_lerp = s * s * s.mul_add(neg_two, three);
49        let z_lerp = u * u * u.mul_add(neg_two, three);
50
51        // Hash: 42
52        let x1: Simd<u32, A> = x_grid_lo.raw_cast() * channel_seed;
53        let y1: Simd<u32, A> = y_grid_lo.raw_cast() * channel_seed;
54        let z1: Simd<u32, A> = z_grid_lo.raw_cast() * channel_seed;
55        let x2 = x1 + channel_seed;
56        let y2 = y1 + channel_seed;
57        let z2 = z1 + channel_seed;
58
59        let x1_shuf = x1.permute_8(shuffle_indices) ^ prime;
60        let y1_shuf = y1.permute_8(shuffle_indices) ^ prime;
61        let z1_shuf = z1.permute_8(shuffle_indices) ^ prime;
62        let x2_shuf = x2.permute_8(shuffle_indices) ^ prime;
63        let y2_shuf = y2.permute_8(shuffle_indices) ^ prime;
64        let z2_shuf = z2.permute_8(shuffle_indices) ^ prime;
65
66        let hash_tlf = x1_shuf * y1_shuf + z1_shuf * y1_shuf;
67        let hash_trf = x1_shuf * y1_shuf + z2_shuf * y1_shuf;
68        let hash_blf = x1_shuf * y2_shuf + z1_shuf * y2_shuf;
69        let hash_brf = x1_shuf * y2_shuf + z2_shuf * y2_shuf;
70        let hash_tlb = x2_shuf * y1_shuf + z1_shuf * y1_shuf;
71        let hash_trb = x2_shuf * y1_shuf + z2_shuf * y1_shuf;
72        let hash_blb = x2_shuf * y2_shuf + z1_shuf * y2_shuf;
73        let hash_brb = x2_shuf * y2_shuf + z2_shuf * y2_shuf;
74
75        // Values: 24
76        let val_tlf = ((hash_tlf & hash_mask) | exp_bits).raw_cast::<f32>() - three;
77        let val_trf = ((hash_trf & hash_mask) | exp_bits).raw_cast::<f32>() - three;
78        let val_blf = ((hash_blf & hash_mask) | exp_bits).raw_cast::<f32>() - three;
79        let val_brf = ((hash_brf & hash_mask) | exp_bits).raw_cast::<f32>() - three;
80        let val_tlb = ((hash_tlb & hash_mask) | exp_bits).raw_cast::<f32>() - three;
81        let val_trb = ((hash_trb & hash_mask) | exp_bits).raw_cast::<f32>() - three;
82        let val_blb = ((hash_blb & hash_mask) | exp_bits).raw_cast::<f32>() - three;
83        let val_brb = ((hash_brb & hash_mask) | exp_bits).raw_cast::<f32>() - three;
84
85        // Interpolation: 14
86        let lerp_tf = z_lerp.mul_add(val_trf - val_tlf, val_tlf);
87        let lerp_bf = z_lerp.mul_add(val_brf - val_blf, val_blf);
88        let lerp_tb = z_lerp.mul_add(val_trb - val_tlb, val_tlb);
89        let lerp_bb = z_lerp.mul_add(val_brb - val_blb, val_blb);
90
91        let lerp_front = y_lerp.mul_add(lerp_bf - lerp_tf, lerp_tf);
92        let lerp_back = y_lerp.mul_add(lerp_bb - lerp_tb, lerp_tb);
93
94        x_lerp.mul_add(lerp_back - lerp_front, lerp_front)
95    }
96}