Skip to main content

quick_noise/noise/generators/simplex/
batch_3d.rs

1use simply_simd::{Arch, Simd};
2
3use crate::api::batch::interface::BatchGenerator;
4use crate::noise::generators::Simplex;
5use crate::noise::util::constants::{
6    BYTE_SHUFFLE, X_GRAD_ENCODING, Y_GRAD_ENCODING, Z_GRAD_ENCODING,
7};
8
9const SKEW_3D: f32 = 1.0 / 3.0;
10const UNSKEW_3D: f32 = 1.0 / 6.0;
11
12impl BatchGenerator<3> for Simplex {
13    #[inline(always)]
14    fn sample_batch<A: Arch>(
15        seed: u32,
16        input: [Simd<f32, A>; 3],
17        freq: [Simd<f32, A>; 3],
18    ) -> Simd<f32, A> {
19        // Constants.
20        let skew: Simd<f32, A> = Simd::splat(SKEW_3D);
21        let unskew: Simd<f32, A> = Simd::splat(UNSKEW_3D);
22        let subbed_unskew: Simd<f32, A> = Simd::splat(UNSKEW_3D - 1.0);
23        let hi_skew_offset: Simd<f32, A> = Simd::splat(3.0 * UNSKEW_3D - 1.0);
24        let two_unskew: Simd<f32, A> = Simd::splat(2.0 * UNSKEW_3D);
25        let mi2_skew_offset: Simd<f32, A> = Simd::splat(2.0 * UNSKEW_3D - 1.0);
26        let half: Simd<f32, A> = Simd::splat(0.5);
27        let zero: Simd<f32, A> = Simd::splat(0.0);
28        let three_int: Simd<u32, A> = Simd::splat(3);
29
30        let x_encoding: Simd<u32, A> = Simd::splat(X_GRAD_ENCODING);
31        let y_encoding: Simd<u32, A> = Simd::splat(Y_GRAD_ENCODING);
32        let z_encoding: Simd<u32, A> = Simd::splat(Z_GRAD_ENCODING);
33
34        // Hash constants.
35        // TODO: Figure out what this needs to be.
36        const S: f32 = 100.0;
37        const GRAD_TABLE: [f32; 4] = [0.0, S, -S, 0.0];
38
39        let shuffle_indices = Simd::<u8, A>::from_slice(&BYTE_SHUFFLE[..]);
40        let channel_seed = Simd::splat(seed);
41        let prime = Simd::splat(0x85ebca6b_u32);
42
43        // Scale: 3
44        let x_scaled = input[0] * freq[0];
45        let y_scaled = input[1] * freq[1];
46        let z_scaled = input[2] * freq[2];
47
48        // Gridpoints and distances: 39
49        let s = (x_scaled + y_scaled + z_scaled) * skew;
50        let x_grid = (x_scaled + s).floor();
51        let y_grid = (y_scaled + s).floor();
52        let z_grid = (z_scaled + s).floor();
53
54        let unskew_sub = (x_grid + y_grid + z_grid) * unskew;
55        let x_dist_lo = x_scaled - x_grid + unskew_sub;
56        let y_dist_lo = y_scaled - y_grid + unskew_sub;
57        let z_dist_lo = z_scaled - z_grid + unskew_sub;
58
59        let x_gt_y = x_dist_lo.simd_gt(y_dist_lo);
60        let x_gt_z = x_dist_lo.simd_gt(z_dist_lo);
61        let ny_gt_z = y_dist_lo.simd_le(z_dist_lo);
62
63        let nx_gt_y = x_dist_lo.simd_le(y_dist_lo);
64        let nx_gt_z = x_dist_lo.simd_le(z_dist_lo);
65        let y_gt_z = y_dist_lo.simd_gt(z_dist_lo);
66
67        let i1 = x_gt_y & x_gt_z;
68        let j1 = nx_gt_y & y_gt_z;
69        let k1 = nx_gt_z & ny_gt_z;
70
71        let i2 = x_gt_y | x_gt_z;
72        let j2 = nx_gt_y | y_gt_z;
73        let k2 = nx_gt_z | ny_gt_z;
74
75        let x_dist_mi1 = x_dist_lo + i1.select(subbed_unskew, unskew);
76        let y_dist_mi1 = y_dist_lo + j1.select(subbed_unskew, unskew);
77        let z_dist_mi1 = z_dist_lo + k1.select(subbed_unskew, unskew);
78
79        let x_dist_mi2 = x_dist_lo + i2.select(mi2_skew_offset, two_unskew);
80        let y_dist_mi2 = y_dist_lo + j2.select(mi2_skew_offset, two_unskew);
81        let z_dist_mi2 = z_dist_lo + k2.select(mi2_skew_offset, two_unskew);
82
83        let x_dist_hi = x_dist_lo + hi_skew_offset;
84        let y_dist_hi = y_dist_lo + hi_skew_offset;
85        let z_dist_hi = z_dist_lo + hi_skew_offset;
86
87        // Hash: 35
88        let x1: Simd<u32, A> = x_grid.cast_int_trunc().raw_cast() * channel_seed;
89        let y1: Simd<u32, A> = y_grid.cast_int_trunc().raw_cast() * channel_seed;
90        let z1: Simd<u32, A> = z_grid.cast_int_trunc().raw_cast() * channel_seed;
91        let x2 = x1 + channel_seed;
92        let y2 = y1 + channel_seed;
93        let z2 = z1 + channel_seed;
94
95        let x1_shuf = x1.permute_8(shuffle_indices) ^ prime;
96        let y1_shuf = y1.permute_8(shuffle_indices) ^ prime;
97        let z1_shuf = z1.permute_8(shuffle_indices) ^ prime;
98
99        let x2_shuf = x2.permute_8(shuffle_indices) ^ prime;
100        let y2_shuf = y2.permute_8(shuffle_indices) ^ prime;
101        let z2_shuf = z2.permute_8(shuffle_indices) ^ prime;
102
103        let x_mi1_shuf = i1.raw_cast().select(x2_shuf, x1_shuf);
104        let y_mi1_shuf = j1.raw_cast().select(y2_shuf, y1_shuf);
105        let z_mi1_shuf = k1.raw_cast().select(z2_shuf, z1_shuf);
106
107        let x_mi2_shuf = i2.raw_cast().select(x2_shuf, x1_shuf);
108        let y_mi2_shuf = j2.raw_cast().select(y2_shuf, y1_shuf);
109        let z_mi2_shuf = k2.raw_cast().select(z2_shuf, z1_shuf);
110
111        let mix_lo = x1_shuf * y1_shuf * z1_shuf;
112        let mix_hi = x2_shuf * y2_shuf * z2_shuf;
113        let mix_mi1 = x_mi1_shuf * y_mi1_shuf * z_mi1_shuf;
114        let mix_mi2 = x_mi2_shuf * y_mi2_shuf * z_mi2_shuf;
115
116        // Gradient lookup: 44
117        let indices_lo = (mix_lo >> 28) << 1;
118        let indices_mi1 = (mix_mi1 >> 28) << 1;
119        let indices_mi2 = (mix_mi2 >> 28) << 1;
120        let indices_hi = (mix_hi >> 28) << 1;
121
122        let x_grads_lo = ((x_encoding >> indices_lo) & three_int).gather(&GRAD_TABLE);
123        let y_grads_lo = ((y_encoding >> indices_lo) & three_int).gather(&GRAD_TABLE);
124        let z_grads_lo = ((z_encoding >> indices_lo) & three_int).gather(&GRAD_TABLE);
125        let x_grads_mi1 = ((x_encoding >> indices_mi1) & three_int).gather(&GRAD_TABLE);
126        let y_grads_mi1 = ((y_encoding >> indices_mi1) & three_int).gather(&GRAD_TABLE);
127        let z_grads_mi1 = ((z_encoding >> indices_mi1) & three_int).gather(&GRAD_TABLE);
128        let x_grads_mi2 = ((x_encoding >> indices_mi2) & three_int).gather(&GRAD_TABLE);
129        let y_grads_mi2 = ((y_encoding >> indices_mi2) & three_int).gather(&GRAD_TABLE);
130        let z_grads_mi2 = ((z_encoding >> indices_mi2) & three_int).gather(&GRAD_TABLE);
131        let x_grads_hi = ((x_encoding >> indices_hi) & three_int).gather(&GRAD_TABLE);
132        let y_grads_hi = ((y_encoding >> indices_hi) & three_int).gather(&GRAD_TABLE);
133        let z_grads_hi = ((z_encoding >> indices_hi) & three_int).gather(&GRAD_TABLE);
134
135        // Sum of products: 44
136        let t_lo = (half
137            - x_dist_lo.mul_add(
138                x_dist_lo,
139                y_dist_lo.mul_add(y_dist_lo, z_dist_lo * z_dist_lo),
140            ))
141        .max(zero);
142        let t_mi1 = (half
143            - x_dist_mi1.mul_add(
144                x_dist_mi1,
145                y_dist_mi1.mul_add(y_dist_mi1, z_dist_mi1 * z_dist_mi1),
146            ))
147        .max(zero);
148        let t_mi2 = (half
149            - x_dist_mi2.mul_add(
150                x_dist_mi2,
151                y_dist_mi2.mul_add(y_dist_mi2, z_dist_mi2 * z_dist_mi2),
152            ))
153        .max(zero);
154        let t_hi = (half
155            - x_dist_hi.mul_add(
156                x_dist_hi,
157                y_dist_hi.mul_add(y_dist_hi, z_dist_hi * z_dist_hi),
158            ))
159        .max(zero);
160
161        let t2_lo = t_lo * t_lo;
162        let t2_mi1 = t_mi1 * t_mi1;
163        let t2_mi2 = t_mi2 * t_mi2;
164        let t2_hi = t_hi * t_hi;
165
166        let t4_lo = t2_lo * t2_lo;
167        let t4_mi1 = t2_mi1 * t2_mi1;
168        let t4_mi2 = t2_mi2 * t2_mi2;
169        let t4_hi = t2_hi * t2_hi;
170
171        let dot_lo = x_grads_lo.mul_add(
172            x_dist_lo,
173            y_grads_lo.mul_add(y_dist_lo, z_dist_lo * z_grads_lo),
174        );
175        let dot_mi1 = x_grads_mi1.mul_add(
176            x_dist_mi1,
177            y_grads_mi1.mul_add(y_dist_mi1, z_dist_mi1 * z_grads_mi1),
178        );
179        let dot_mi2 = x_grads_mi2.mul_add(
180            x_dist_mi2,
181            y_grads_mi2.mul_add(y_dist_mi2, z_dist_mi2 * z_grads_mi2),
182        );
183        let dot_hi = x_grads_hi.mul_add(
184            x_dist_hi,
185            y_grads_hi.mul_add(y_dist_hi, z_dist_hi * z_grads_hi),
186        );
187
188        t4_lo.mul_add(
189            dot_lo,
190            t4_mi1.mul_add(dot_mi1, t4_mi2.mul_add(dot_mi2, t4_hi * dot_hi)),
191        )
192    }
193}