1use crate::blur::safe_blur_min_max;
9use crate::config::CompatRules;
10use crate::frame::{BlurParams, FrameInput, PassRandom};
11use crate::rng::Rng;
12use crate::shader_ext::ShaderExt;
13use lactea_abi::{MilkUniforms, NUM_ROT};
14
15#[derive(Clone, Debug, PartialEq)]
18pub struct RotParams {
19 pub base: [[f32; 3]; NUM_ROT],
21 pub speed: [[f32; 3]; NUM_ROT],
23 pub xlate: [[f32; 3]; NUM_ROT],
25 pub rand_preset: [f32; 4],
28 pub hue_offsets: [f32; 4],
30}
31
32impl RotParams {
33 pub fn randomize(rng: &mut Rng) -> Self {
36 let mut p = RotParams {
37 base: [[0.0; 3]; NUM_ROT],
38 speed: [[0.0; 3]; NUM_ROT],
39 xlate: [[0.0; 3]; NUM_ROT],
40 rand_preset: [rng.frand(), rng.frand(), rng.frand(), rng.frand()],
41 hue_offsets: [
42 rng.below(64841) as f32 * 0.01,
43 rng.below(53751) as f32 * 0.01,
44 rng.below(42661) as f32 * 0.01,
45 rng.below(31571) as f32 * 0.01,
46 ],
47 };
48 for k in 0..NUM_ROT {
49 let rot_mult = 0.9 * (k as f32 / 8.0).powf(3.2);
50 let xlate_mult = 1.0;
51 p.xlate[k] = [
52 (rng.frand() * 2.0 - 1.0) * xlate_mult,
53 (rng.frand() * 2.0 - 1.0) * xlate_mult,
54 (rng.frand() * 2.0 - 1.0) * xlate_mult,
55 ];
56 p.base[k] = [rng.frand() * 6.28, rng.frand() * 6.28, rng.frand() * 6.28];
57 p.speed[k] = [
58 (rng.frand() * 2.0 - 1.0) * rot_mult,
59 (rng.frand() * 2.0 - 1.0) * rot_mult,
60 (rng.frand() * 2.0 - 1.0) * rot_mult,
61 ];
62 }
63 p
64 }
65}
66
67impl Default for RotParams {
68 fn default() -> Self {
69 Self::randomize(&mut Rng::new(0))
70 }
71}
72
73type M4 = [[f32; 4]; 4];
74
75fn mat_mul(a: &M4, b: &M4) -> M4 {
76 let mut r = [[0.0f32; 4]; 4];
77 for i in 0..4 {
78 for j in 0..4 {
79 let mut s = 0.0;
80 for k in 0..4 {
81 s += a[i][k] * b[k][j];
82 }
83 r[i][j] = s;
84 }
85 }
86 r
87}
88
89fn ident() -> M4 {
90 let mut m = [[0.0; 4]; 4];
91 for i in 0..4 {
92 m[i][i] = 1.0;
93 }
94 m
95}
96
97fn rotation_x(a: f32) -> [[f32; 4]; 4] {
99 let (s, c) = a.sin_cos();
100 let mut m = ident();
101 m[1][1] = c;
102 m[1][2] = s;
103 m[2][1] = -s;
104 m[2][2] = c;
105 m
106}
107
108fn rotation_y(a: f32) -> [[f32; 4]; 4] {
110 let (s, c) = a.sin_cos();
111 let mut m = ident();
112 m[0][0] = c;
113 m[0][2] = -s;
114 m[2][0] = s;
115 m[2][2] = c;
116 m
117}
118
119fn rotation_z(a: f32) -> [[f32; 4]; 4] {
121 let (s, c) = a.sin_cos();
122 let mut m = ident();
123 m[0][0] = c;
124 m[0][1] = s;
125 m[1][0] = -s;
126 m[1][1] = c;
127 m
128}
129
130fn translation(x: f32, y: f32, z: f32) -> [[f32; 4]; 4] {
132 let mut m = ident();
133 m[3][0] = x;
134 m[3][1] = y;
135 m[3][2] = z;
136 m
137}
138
139pub fn rot_matrix(ax: f32, ay: f32, az: f32, t: [f32; 3]) -> [[f32; 4]; 4] {
141 let mut m = mat_mul(&rotation_x(ax), &translation(t[0], t[1], t[2]));
142 m = mat_mul(&m, &rotation_z(az));
143 mat_mul(&m, &rotation_y(ay))
144}
145
146pub fn pack_float4x3(m: &M4) -> [[f32; 4]; 3] {
149 let mut out = [[0.0f32; 4]; 3];
150 for col in 0..3 {
151 for row in 0..4 {
152 out[col][row] = m[row][col];
153 }
154 }
155 out
156}
157
158pub(crate) fn rot_matrices(rot: &RotParams, time: f64, rng: &mut Rng) -> [[[f32; 4]; 3]; NUM_ROT] {
161 let mut out = [[[0.0f32; 4]; 3]; NUM_ROT];
162 for i in 0..20 {
163 let ang = |k: usize| (rot.base[i][k] as f64 + rot.speed[i][k] as f64 * time) as f32;
165 let m = rot_matrix(ang(0), ang(1), ang(2), rot.xlate[i]);
166 out[i] = pack_float4x3(&m);
167 }
168 for i in 20..24 {
169 let ax = rng.frand() * 6.28;
170 let ay = rng.frand() * 6.28;
171 let az = rng.frand() * 6.28;
172 let t = [rng.frand(), rng.frand(), rng.frand()];
173 let m = rot_matrix(ax, ay, az, t);
174 out[i] = pack_float4x3(&m);
175 }
176 out
177}
178
179pub fn drop_rot_translation(rot: &mut [[[f32; 4]; 3]; NUM_ROT]) {
182 for m in rot.iter_mut() {
183 for col in m.iter_mut() {
184 col[3] = 0.0;
185 }
186 }
187}
188
189pub(crate) fn noise_texsizes() -> [[f32; 4]; 6] {
191 let mut out = [[0.0; 4]; 6];
192 for (i, t) in crate::noise::ALL_NOISE.iter().enumerate() {
193 let (s, _, _) = crate::noise::noise_spec(*t);
194 let s = s as f32;
195 out[i] = [s, s, 1.0 / s, 1.0 / s];
196 }
197 out
198}
199
200pub(crate) struct MilkUniformInputs<'a> {
207 pub width: u32,
208 pub height: u32,
209 pub tex_w: u32,
210 pub tex_h: u32,
211 pub frame: &'a FrameInput,
212 pub q: &'a [f32; 32],
213 pub blur: &'a BlurParams,
214 pub ext: &'a ShaderExt,
215 pub rot: &'a RotParams,
216 pub rules: &'a CompatRules,
217 pub pass: Option<&'a PassRandom>,
218}
219
220impl<'a> MilkUniformInputs<'a> {
221 pub fn for_frame(
223 width: u32,
224 height: u32,
225 f: &'a FrameInput,
226 rot: &'a RotParams,
227 rules: &'a CompatRules,
228 pass: Option<&'a PassRandom>,
229 ) -> Self {
230 Self {
231 width,
232 height,
233 tex_w: width,
234 tex_h: height,
235 frame: f,
236 q: &f.q,
237 blur: &f.blur,
238 ext: &f.ext,
239 rot,
240 rules,
241 pass,
242 }
243 }
244}
245
246pub(crate) fn fill_milk_uniforms(inp: &MilkUniformInputs<'_>, rot_rng: &mut Rng) -> MilkUniforms {
259 let MilkUniformInputs {
260 width,
261 height,
262 tex_w,
263 tex_h,
264 frame: f,
265 q,
266 blur,
267 ext,
268 rot,
269 rules,
270 pass,
271 } = *inp;
272 let mut u = MilkUniforms::default();
273 let w = width as f32;
274 let h = height as f32;
275
276 match pass {
277 Some(p) => {
278 u.rand_frame = p.rand_frame;
279 u.rand_preset = p.rand_preset;
280 }
281 None => {
282 u.rand_frame = f.rand_frame;
283 u.rand_preset = f.rand_preset;
284 }
285 }
286
287 let (mut aspect_x, mut aspect_y) = (1.0f32, 1.0f32);
289 if width > height {
290 aspect_y = h / w;
291 } else {
292 aspect_x = w / h;
293 }
294 u.c[0] = [aspect_x, aspect_y, 1.0 / aspect_x, 1.0 / aspect_y];
295 if !rules.c1_is_zero {
296 u.c[1] = [aspect_x, aspect_y, 1.0 / aspect_x, 1.0 / aspect_y];
297 }
298 let ft = ext.time.unwrap_or(f.time) as f32;
300 let wrapped = ft - ((ft as f64 / 10000.0) as i32 * 10000) as f32;
301 u.c[2] = [wrapped, f.fps, f.frame as f32, f.progress];
302 let (vol, vol_att) = if rules.vol_is_comma_bug {
303 (0.3333 * f.treb, 0.3333 * f.treb_att)
304 } else {
305 (
306 (f.bass + f.mid + f.treb) * 0.333,
307 (f.bass_att + f.mid_att + f.treb_att) * 0.333,
308 )
309 };
310 u.c[3] = [f.bass, f.mid, f.treb, vol];
311 u.c[4] = [f.bass_att, f.mid_att, f.treb_att, vol_att];
312
313 let (bmin, bmax) = safe_blur_min_max(blur.min, blur.max);
314 u.c[5] = [bmax[0] - bmin[0], bmin[0], bmax[1] - bmin[1], bmin[1]];
315 u.c[6] = [bmax[2] - bmin[2], bmin[2], bmin[0], bmax[0]];
316 u.c[7] = [
317 tex_w as f32,
318 tex_h as f32,
319 1.0 / tex_w as f32,
320 1.0 / tex_h as f32,
321 ];
322
323 let t = f.time;
324 let cosv = |m: [f32; 4], o: [f32; 4]| -> [f32; 4] {
325 let mut r = [0.0; 4];
326 for k in 0..4 {
327 r[k] = 0.5 + 0.5 * ((t * m[k] as f64 + o[k] as f64) as f32).cos();
328 }
329 r
330 };
331 let sinv = |m: [f32; 4], o: [f32; 4]| -> [f32; 4] {
332 let mut r = [0.0; 4];
333 for k in 0..4 {
334 r[k] = 0.5 + 0.5 * ((t * m[k] as f64 + o[k] as f64) as f32).sin();
335 }
336 r
337 };
338 let fast_m = [0.329, 1.293, 5.070, 20.051];
339 let fast_o = [1.2, 3.9, 2.5, 5.4];
340 let slow_m = [0.0050, 0.0085, 0.0133, 0.0217];
341 let slow_o = [2.7, 5.3, 4.5, 3.8];
342 u.c[8] = cosv(fast_m, fast_o);
343 u.c[9] = sinv(fast_m, fast_o);
344 u.c[10] = cosv(slow_m, slow_o);
345 u.c[11] = sinv(slow_m, slow_o);
346
347 let mip_x = w.ln() / 2.0f32.ln();
348 let mip_y = if rules.mip_y_from_width {
349 w.ln() / 2.0f32.ln()
350 } else {
351 h.ln() / 2.0f32.ln()
352 };
353 u.c[12] = [mip_x, mip_y, 0.5 * (mip_x + mip_y), 0.0];
354 u.c[13] = [bmin[1], bmax[1], bmin[2], bmax[2]];
355
356 for i in 0..8 {
357 u.q[i] = [q[i * 4], q[i * 4 + 1], q[i * 4 + 2], q[i * 4 + 3]];
358 }
359 u.rot = match pass.and_then(|p| p.rot.as_deref()) {
360 Some(r) => *r,
361 None => {
362 let mut r = rot_matrices(rot, f.time, rot_rng);
363 if rules.rot_translation_dropped {
364 drop_rot_translation(&mut r);
365 }
366 r
367 }
368 };
369 u.texsize_noise = noise_texsizes();
370 u
371}
372
373#[cfg(test)]
374mod tests {
375 use super::*;
376 use crate::config::Compat;
377
378 fn close(a: f32, b: f32) -> bool {
379 (a - b).abs() < 1e-5
380 }
381
382 fn frame() -> FrameInput {
383 let mut f = FrameInput::default();
384 f.time = 12.5;
385 f.fps = 60.0;
386 f.frame = 750;
387 f.progress = 0.25;
388 f.bass = 1.1;
389 f.mid = 0.9;
390 f.treb = 0.6;
391 f.bass_att = 1.2;
392 f.mid_att = 1.0;
393 f.treb_att = 0.3;
394 f.blur.min = [0.1, 0.2, 0.3];
395 f.blur.max = [0.9, 0.8, 0.7];
396 for i in 0..32 {
397 f.q[i] = i as f32 + 1.0;
398 }
399 f.rand_frame = [0.1, 0.2, 0.3, 0.4];
400 f.rand_preset = [0.5, 0.6, 0.7, 0.8];
401 f
402 }
403
404 #[test]
405 fn registers_match_hand_computation() {
406 let f = frame();
407 let rot = RotParams::default();
408 let u = fill_milk_uniforms(
409 &MilkUniformInputs::for_frame(640, 360, &f, &rot, &Compat::MilkDrop2.rules(), None),
410 &mut Rng::new(1),
411 );
412 assert_eq!(u.c[0], [1.0, 0.5625, 1.0, 1.0 / 0.5625]);
414 assert_eq!(u.c[1], u.c[0]); assert_eq!(u.c[2], [12.5, 60.0, 750.0, 0.25]);
416 assert_eq!(u.c[3][..3], [1.1, 0.9, 0.6]);
418 assert!(close(u.c[3][3], 0.3333 * 0.6));
419 assert!(close(u.c[4][3], 0.3333 * 0.3));
420 assert!(
422 close(u.c[5][0], 0.8)
423 && close(u.c[5][1], 0.1)
424 && close(u.c[5][2], 0.6)
425 && close(u.c[5][3], 0.2)
426 );
427 assert!(
428 close(u.c[6][0], 0.4)
429 && close(u.c[6][1], 0.3)
430 && close(u.c[6][2], 0.1)
431 && close(u.c[6][3], 0.9)
432 );
433 assert!(
434 close(u.c[13][0], 0.2)
435 && close(u.c[13][1], 0.8)
436 && close(u.c[13][2], 0.3)
437 && close(u.c[13][3], 0.7)
438 );
439 assert_eq!(u.c[7], [640.0, 360.0, 1.0 / 640.0, 1.0 / 360.0]);
440 let c = 0.5 + 0.5 * ((12.5f64 * 0.329f32 as f64 + 1.2f32 as f64) as f32).cos();
442 assert!(close(u.c[8][0], c));
443 let s = 0.5 + 0.5 * ((12.5f64 * 20.051f32 as f64 + 5.4f32 as f64) as f32).sin();
444 assert!(close(u.c[9][3], s));
445 let sc = 0.5 + 0.5 * ((12.5f64 * 0.0085f32 as f64 + 5.3f32 as f64) as f32).cos();
446 assert!(close(u.c[10][1], sc));
447 let m = 640f32.ln() / 2f32.ln();
449 assert!(close(u.c[12][0], m) && close(u.c[12][1], m) && close(u.c[12][2], m));
450 let up = fill_milk_uniforms(
452 &MilkUniformInputs::for_frame(640, 360, &f, &rot, &Compat::ProjectM.rules(), None),
453 &mut Rng::new(1),
454 );
455 assert!(close(up.c[3][3], (1.1 + 0.9 + 0.6) * 0.333));
456 assert!(close(up.c[4][3], (1.2 + 1.0 + 0.3) * 0.333));
457 assert!(close(up.c[12][1], 360f32.ln() / 2f32.ln()));
458 assert_eq!(up.c[1], [0.0; 4]);
459 assert_eq!(u.q[0], [1.0, 2.0, 3.0, 4.0]);
461 assert_eq!(u.q[7], [29.0, 30.0, 31.0, 32.0]);
462 assert_eq!(u.rand_frame, [0.1, 0.2, 0.3, 0.4]);
463 assert_eq!(u.rand_preset, [0.5, 0.6, 0.7, 0.8]);
464 assert_eq!(u.texsize_noise[0], [256.0, 256.0, 1.0 / 256.0, 1.0 / 256.0]);
466 assert_eq!(u.texsize_noise[1][0], 32.0);
467 assert_eq!(u.texsize_noise[5][0], 32.0);
468 }
469
470 #[test]
471 fn portrait_aspect() {
472 let f = frame();
473 let u = fill_milk_uniforms(
474 &MilkUniformInputs::for_frame(
475 360,
476 640,
477 &f,
478 &RotParams::default(),
479 &Compat::MilkDrop2.rules(),
480 None,
481 ),
482 &mut Rng::new(1),
483 );
484 assert_eq!(u.c[0], [0.5625, 1.0, 1.0 / 0.5625, 1.0]);
485 }
486
487 #[test]
488 fn rot_matrices_static_dynamic_random() {
489 let rot = RotParams::randomize(&mut Rng::new(42));
490 let a = rot_matrices(&rot, 0.0, &mut Rng::new(1));
492 let b = rot_matrices(&rot, 100.0, &mut Rng::new(1));
493 assert_eq!(a[0], b[0], "rot_s1 is static");
494 assert_ne!(a[2], b[2], "rot_s3 drifts slowly (0.9*(2/8)^3.2 rad/s max)");
495 assert_ne!(a[8], b[8]);
497 let c = rot_matrices(&rot, 0.0, &mut Rng::new(2));
499 assert_ne!(a[20], c[20]);
500 assert_eq!(a[19], c[19]);
501 }
502
503 #[test]
504 fn rot_matrix_layout_matches_d3dx() {
505 let m = rot_matrix(0.0, 0.0, std::f32::consts::FRAC_PI_2, [0.0, 0.0, 0.0]);
508 let p = pack_float4x3(&m);
509 let v = [1.0f32, 0.0, 0.0, 1.0];
511 let res: Vec<f32> = (0..3)
512 .map(|c| (0..4).map(|k| v[k] * p[c][k]).sum())
513 .collect();
514 assert!(close(res[0], 0.0) && close(res[1], 1.0) && close(res[2], 0.0));
515 let m = rot_matrix(0.0, 0.0, 0.0, [0.25, 0.5, 0.75]);
517 let p = pack_float4x3(&m);
518 assert_eq!([p[0][3], p[1][3], p[2][3]], [0.25, 0.5, 0.75]);
519 }
520
521 #[test]
522 fn randomize_matches_md2_distribution() {
523 let r = RotParams::randomize(&mut Rng::new(9));
524 assert_eq!(r.speed[0], [0.0; 3]);
526 for k in 1..4 {
527 let lim = 0.9 * (k as f32 / 8.0).powf(3.2) + 1e-6;
528 assert!(
529 r.speed[k].iter().all(|s| s.abs() <= lim),
530 "static rotation {k} speed bound"
531 );
532 }
533 for k in 0..NUM_ROT {
534 for c in 0..3 {
535 assert!(r.base[k][c] >= 0.0 && r.base[k][c] < 6.28);
536 assert!(r.xlate[k][c].abs() <= 1.0);
537 assert!(r.speed[k][c].abs() <= 0.9 * (k as f32 / 8.0).powf(3.2) + 1e-6);
538 }
539 }
540 }
541
542 fn fill(f: &FrameInput, rules: &CompatRules) -> MilkUniforms {
543 fill_milk_uniforms(
544 &MilkUniformInputs::for_frame(640, 360, f, &RotParams::default(), rules, None),
545 &mut Rng::new(1),
546 )
547 }
548
549 #[test]
552 fn shader_time_is_the_ext_time_wrapped_at_10000() {
553 let rules = Compat::MilkDrop3.rules();
554 let mut f = frame();
556 f.ext.time = Some(12345.5);
557 let u = fill(&f, &rules);
558 assert_eq!(u.c[2][0], 2345.5);
559 let base = fill(&frame(), &rules);
560 assert_eq!(
561 u.c[2][1..],
562 base.c[2][1..],
563 "fps, frame, progress untouched"
564 );
565 assert_eq!(
566 u.c[8..12],
567 base.c[8..12],
568 "roam registers use FrameInput::time"
569 );
570 let mut g = frame();
572 g.time = 12345.5;
573 assert_eq!(fill(&g, &rules).c[2][0], 2345.5);
574 let mut h = frame();
576 h.time = 99999.0;
577 h.ext.time = Some(3.25);
578 assert_eq!(fill(&h, &rules).c[2][0], 3.25);
579 }
580
581 #[test]
584 fn default_ext_leaves_the_registers_alone() {
585 let rules = Compat::MilkDrop2.rules();
586 let bytes = |f: &FrameInput| bytemuck::bytes_of(&fill(f, &rules)).to_vec();
587 let f = frame();
588 assert_eq!(f.ext, ShaderExt::default());
589 assert_eq!(fill(&f, &rules).c[2][0], 12.5);
590 let base = bytes(&f);
591
592 let mut g = frame();
593 g.ext.mouse = [0.3, 0.6, 1.0, 1.0];
594 g.ext.q_hi = [7.0; 32];
595 g.ext.fft = Some(std::sync::Arc::new([0.5; lactea_abi::FFT_BINS]));
596 assert_eq!(bytes(&g), base);
597
598 let mut h = frame();
599 h.ext.time = Some(h.time);
600 assert_eq!(bytes(&h), base);
601 }
602}