quick_noise/noise/generators/simplex/
batch_2d.rs1use std::f32::consts::SQRT_2;
2
3use simply_simd::{Arch, Simd};
4
5use crate::api::batch::interface::BatchGenerator;
6use crate::noise::generators::Simplex;
7use crate::noise::util::constants::{BYTE_SHUFFLE, HASH_PRIME};
8
9const SQRT_3: f32 = 1.732_050_8;
10const SKEW_2D: f32 = (SQRT_3 - 1.0) / 2.0;
11const UNSKEW_2D: f32 = (3.0 - SQRT_3) / 6.0;
12
13const SCALE: f32 = 80.0;
14const SCALED_SQRT: f32 = (SQRT_2 / 2.0) * SCALE;
15
16const A: f32 = SCALE;
17const B: f32 = SCALED_SQRT;
18const C: f32 = 0.0;
19pub const X_GRADIENTS_2D: [f32; 8] = [A, B, C, -B, -A, -B, C, B];
20pub const Y_GRADIENTS_2D: [f32; 8] = [C, B, A, B, C, -B, -A, -B];
21
22impl BatchGenerator<2> for Simplex {
23 #[inline(always)]
24 fn sample_batch<A: Arch>(
25 seed: u32,
26 input: [Simd<f32, A>; 2],
27 freq: [Simd<f32, A>; 2],
28 ) -> Simd<f32, A> {
29 let skew: Simd<f32, A> = Simd::splat(SKEW_2D);
31 let unskew: Simd<f32, A> = Simd::splat(UNSKEW_2D);
32 let subbed_unskew: Simd<f32, A> = Simd::splat(UNSKEW_2D - 1.0);
33 let hi_skew_offset: Simd<f32, A> = Simd::splat(2.0 * UNSKEW_2D - 1.0);
34 let half: Simd<f32, A> = Simd::splat(0.5);
35 let zero: Simd<f32, A> = Simd::splat(0.0);
36 let t_hi_coef = Simd::splat(2.0 * SQRT_3 / 3.0);
37 let neg_two_thirds = Simd::splat(-2.0 / 3.0);
38
39 let shuffle_indices = Simd::<u8, A>::from_slice(&BYTE_SHUFFLE[..]);
41 let channel_seed = Simd::splat(seed);
42 let prime = Simd::splat(HASH_PRIME);
43
44 let x_scaled = input[0] * freq[0];
45 let y_scaled = input[1] * freq[1];
46
47 let s = (x_scaled + y_scaled) * skew;
49 let x_grid = (x_scaled + s).floor();
50 let y_grid = (y_scaled + s).floor();
51
52 let unskew_sub = (x_grid + y_grid) * unskew;
53 let x_dist_lo = x_scaled - x_grid + unskew_sub;
54 let y_dist_lo = y_scaled - y_grid + unskew_sub;
55 let triangle_mask = x_dist_lo.simd_gt(y_dist_lo);
56
57 let x_dist_mi_offset = triangle_mask.select(subbed_unskew, unskew);
58 let y_dist_mi_offset = triangle_mask.select(unskew, subbed_unskew);
59 let x_dist_mi = x_dist_lo + x_dist_mi_offset;
60 let y_dist_mi = y_dist_lo + y_dist_mi_offset;
61
62 let x_dist_hi = x_dist_lo + hi_skew_offset;
63 let y_dist_hi = y_dist_lo + hi_skew_offset;
64
65 let x1: Simd<u32, A> = x_grid.cast_int_trunc().raw_cast() * channel_seed;
67 let y1: Simd<u32, A> = y_grid.cast_int_trunc().raw_cast() * channel_seed;
68 let x2 = x1 + channel_seed;
69 let y2 = y1 + channel_seed;
70
71 let x1_shuf = x1.permute_8(shuffle_indices) ^ prime;
72 let y1_shuf = y1.permute_8(shuffle_indices) ^ prime;
73 let x2_shuf = x2.permute_8(shuffle_indices) ^ prime;
74 let y2_shuf = y2.permute_8(shuffle_indices) ^ prime;
75
76 let mix_lo = (x1_shuf * y1_shuf) ^ x1_shuf;
77 let mix_hi = (x2_shuf * y2_shuf) ^ x2_shuf;
78
79 let x_shuf_mi = triangle_mask.raw_cast().select(x2_shuf, x1_shuf);
80 let y_shuf_mi = triangle_mask.raw_cast().select(y1_shuf, y2_shuf);
81 let mix_mi = (x_shuf_mi * y_shuf_mi) ^ x_shuf_mi;
82
83 let indices_lo = mix_lo >> 29;
85 let indices_mi = mix_mi >> 29;
86 let indices_hi = mix_hi >> 29;
87
88 let x_grads_lo = indices_lo.gather(&X_GRADIENTS_2D);
89 let y_grads_lo = indices_lo.gather(&Y_GRADIENTS_2D);
90 let x_grads_mi = indices_mi.gather(&X_GRADIENTS_2D);
91 let y_grads_mi = indices_mi.gather(&Y_GRADIENTS_2D);
92 let x_grads_hi = indices_hi.gather(&X_GRADIENTS_2D);
93 let y_grads_hi = indices_hi.gather(&Y_GRADIENTS_2D);
94
95 let t_lo_pre = half - x_dist_lo.mul_add(x_dist_lo, y_dist_lo * y_dist_lo);
97 let t_mi_pre = half - x_dist_mi.mul_add(x_dist_mi, y_dist_mi * y_dist_mi);
98 let t_hi_pre = t_lo_pre + t_hi_coef.mul_add(x_dist_lo + y_dist_lo, neg_two_thirds);
99
100 let t_lo = t_lo_pre.max(zero);
101 let t_mi = t_mi_pre.max(zero);
102 let t_hi = t_hi_pre.max(zero);
103
104 let t2_lo = t_lo * t_lo;
105 let t2_mi = t_mi * t_mi;
106 let t2_hi = t_hi * t_hi;
107
108 let t4_lo = t2_lo * t2_lo;
109 let t4_mi = t2_mi * t2_mi;
110 let t4_hi = t2_hi * t2_hi;
111
112 let dot_lo = x_grads_lo.mul_add(x_dist_lo, y_grads_lo * y_dist_lo);
113 let dot_mi = x_grads_mi.mul_add(x_dist_mi, y_grads_mi * y_dist_mi);
114 let dot_hi = x_grads_hi.mul_add(x_dist_hi, y_grads_hi * y_dist_hi);
115
116 t4_lo.mul_add(dot_lo, t4_mi.mul_add(dot_mi, t4_hi * dot_hi))
117 }
118}