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 shown = if rules.shader_time_wrap {
301 ft - ((ft as f64 / 10000.0) as i32 * 10000) as f32
302 } else {
303 ft
304 };
305 u.c[2] = [shown, f.fps, f.frame as f32, f.progress];
306 let (vol, vol_att) = if rules.vol_is_comma_bug {
307 (0.3333 * f.treb, 0.3333 * f.treb_att)
308 } else {
309 (
310 (f.bass + f.mid + f.treb) * 0.333,
311 (f.bass_att + f.mid_att + f.treb_att) * 0.333,
312 )
313 };
314 u.c[3] = [f.bass, f.mid, f.treb, vol];
315 u.c[4] = [f.bass_att, f.mid_att, f.treb_att, vol_att];
316
317 let (bmin, bmax) = safe_blur_min_max(blur.min, blur.max);
318 u.c[5] = [bmax[0] - bmin[0], bmin[0], bmax[1] - bmin[1], bmin[1]];
319 u.c[6] = [bmax[2] - bmin[2], bmin[2], bmin[0], bmax[0]];
320 u.c[7] = [
321 tex_w as f32,
322 tex_h as f32,
323 1.0 / tex_w as f32,
324 1.0 / tex_h as f32,
325 ];
326
327 let t = f.time;
328 let cosv = |m: [f32; 4], o: [f32; 4]| -> [f32; 4] {
329 let mut r = [0.0; 4];
330 for k in 0..4 {
331 r[k] = 0.5 + 0.5 * ((t * m[k] as f64 + o[k] as f64) as f32).cos();
332 }
333 r
334 };
335 let sinv = |m: [f32; 4], o: [f32; 4]| -> [f32; 4] {
336 let mut r = [0.0; 4];
337 for k in 0..4 {
338 r[k] = 0.5 + 0.5 * ((t * m[k] as f64 + o[k] as f64) as f32).sin();
339 }
340 r
341 };
342 let fast_m = [0.329, 1.293, 5.070, 20.051];
343 let fast_o = [1.2, 3.9, 2.5, 5.4];
344 let slow_m = [0.0050, 0.0085, 0.0133, 0.0217];
345 let slow_o = [2.7, 5.3, 4.5, 3.8];
346 u.c[8] = cosv(fast_m, fast_o);
347 u.c[9] = sinv(fast_m, fast_o);
348 u.c[10] = cosv(slow_m, slow_o);
349 u.c[11] = sinv(slow_m, slow_o);
350
351 let mip_x = w.ln() / 2.0f32.ln();
352 let mip_y = if rules.mip_y_from_width {
353 w.ln() / 2.0f32.ln()
354 } else {
355 h.ln() / 2.0f32.ln()
356 };
357 u.c[12] = [mip_x, mip_y, 0.5 * (mip_x + mip_y), 0.0];
358 u.c[13] = [bmin[1], bmax[1], bmin[2], bmax[2]];
359
360 for i in 0..8 {
361 u.q[i] = [q[i * 4], q[i * 4 + 1], q[i * 4 + 2], q[i * 4 + 3]];
362 }
363 u.rot = match pass.and_then(|p| p.rot.as_deref()) {
364 Some(r) => *r,
365 None => {
366 let mut r = rot_matrices(rot, f.time, rot_rng);
367 if rules.rot_translation_dropped {
368 drop_rot_translation(&mut r);
369 }
370 r
371 }
372 };
373 u.texsize_noise = noise_texsizes();
374 u
375}
376
377#[cfg(test)]
378mod tests {
379 use super::*;
380 use crate::config::Compat;
381
382 fn close(a: f32, b: f32) -> bool {
383 (a - b).abs() < 1e-5
384 }
385
386 fn frame() -> FrameInput {
387 let mut f = FrameInput::default();
388 f.time = 12.5;
389 f.fps = 60.0;
390 f.frame = 750;
391 f.progress = 0.25;
392 f.bass = 1.1;
393 f.mid = 0.9;
394 f.treb = 0.6;
395 f.bass_att = 1.2;
396 f.mid_att = 1.0;
397 f.treb_att = 0.3;
398 f.blur.min = [0.1, 0.2, 0.3];
399 f.blur.max = [0.9, 0.8, 0.7];
400 for i in 0..32 {
401 f.q[i] = i as f32 + 1.0;
402 }
403 f.rand_frame = [0.1, 0.2, 0.3, 0.4];
404 f.rand_preset = [0.5, 0.6, 0.7, 0.8];
405 f
406 }
407
408 #[test]
409 fn registers_match_hand_computation() {
410 let f = frame();
411 let rot = RotParams::default();
412 let u = fill_milk_uniforms(
413 &MilkUniformInputs::for_frame(640, 360, &f, &rot, &Compat::MilkDrop2.rules(), None),
414 &mut Rng::new(1),
415 );
416 assert_eq!(u.c[0], [1.0, 0.5625, 1.0, 1.0 / 0.5625]);
418 assert_eq!(u.c[1], u.c[0]); assert_eq!(u.c[2], [12.5, 60.0, 750.0, 0.25]);
420 assert_eq!(u.c[3][..3], [1.1, 0.9, 0.6]);
422 assert!(close(u.c[3][3], 0.3333 * 0.6));
423 assert!(close(u.c[4][3], 0.3333 * 0.3));
424 assert!(
426 close(u.c[5][0], 0.8)
427 && close(u.c[5][1], 0.1)
428 && close(u.c[5][2], 0.6)
429 && close(u.c[5][3], 0.2)
430 );
431 assert!(
432 close(u.c[6][0], 0.4)
433 && close(u.c[6][1], 0.3)
434 && close(u.c[6][2], 0.1)
435 && close(u.c[6][3], 0.9)
436 );
437 assert!(
438 close(u.c[13][0], 0.2)
439 && close(u.c[13][1], 0.8)
440 && close(u.c[13][2], 0.3)
441 && close(u.c[13][3], 0.7)
442 );
443 assert_eq!(u.c[7], [640.0, 360.0, 1.0 / 640.0, 1.0 / 360.0]);
444 let c = 0.5 + 0.5 * ((12.5f64 * 0.329f32 as f64 + 1.2f32 as f64) as f32).cos();
446 assert!(close(u.c[8][0], c));
447 let s = 0.5 + 0.5 * ((12.5f64 * 20.051f32 as f64 + 5.4f32 as f64) as f32).sin();
448 assert!(close(u.c[9][3], s));
449 let sc = 0.5 + 0.5 * ((12.5f64 * 0.0085f32 as f64 + 5.3f32 as f64) as f32).cos();
450 assert!(close(u.c[10][1], sc));
451 let m = 640f32.ln() / 2f32.ln();
453 assert!(close(u.c[12][0], m) && close(u.c[12][1], m) && close(u.c[12][2], m));
454 let up = fill_milk_uniforms(
456 &MilkUniformInputs::for_frame(640, 360, &f, &rot, &Compat::ProjectM.rules(), None),
457 &mut Rng::new(1),
458 );
459 assert!(close(up.c[3][3], (1.1 + 0.9 + 0.6) * 0.333));
460 assert!(close(up.c[4][3], (1.2 + 1.0 + 0.3) * 0.333));
461 assert!(close(up.c[12][1], 360f32.ln() / 2f32.ln()));
462 assert_eq!(up.c[1], [0.0; 4]);
463 assert_eq!(u.q[0], [1.0, 2.0, 3.0, 4.0]);
465 assert_eq!(u.q[7], [29.0, 30.0, 31.0, 32.0]);
466 assert_eq!(u.rand_frame, [0.1, 0.2, 0.3, 0.4]);
467 assert_eq!(u.rand_preset, [0.5, 0.6, 0.7, 0.8]);
468 assert_eq!(u.texsize_noise[0], [256.0, 256.0, 1.0 / 256.0, 1.0 / 256.0]);
470 assert_eq!(u.texsize_noise[1][0], 32.0);
471 assert_eq!(u.texsize_noise[5][0], 32.0);
472 }
473
474 #[test]
475 fn portrait_aspect() {
476 let f = frame();
477 let u = fill_milk_uniforms(
478 &MilkUniformInputs::for_frame(
479 360,
480 640,
481 &f,
482 &RotParams::default(),
483 &Compat::MilkDrop2.rules(),
484 None,
485 ),
486 &mut Rng::new(1),
487 );
488 assert_eq!(u.c[0], [0.5625, 1.0, 1.0 / 0.5625, 1.0]);
489 }
490
491 #[test]
492 fn rot_matrices_static_dynamic_random() {
493 let rot = RotParams::randomize(&mut Rng::new(42));
494 let a = rot_matrices(&rot, 0.0, &mut Rng::new(1));
496 let b = rot_matrices(&rot, 100.0, &mut Rng::new(1));
497 assert_eq!(a[0], b[0], "rot_s1 is static");
498 assert_ne!(a[2], b[2], "rot_s3 drifts slowly (0.9*(2/8)^3.2 rad/s max)");
499 assert_ne!(a[8], b[8]);
501 let c = rot_matrices(&rot, 0.0, &mut Rng::new(2));
503 assert_ne!(a[20], c[20]);
504 assert_eq!(a[19], c[19]);
505 }
506
507 #[test]
508 fn rot_matrix_layout_matches_d3dx() {
509 let m = rot_matrix(0.0, 0.0, std::f32::consts::FRAC_PI_2, [0.0, 0.0, 0.0]);
512 let p = pack_float4x3(&m);
513 let v = [1.0f32, 0.0, 0.0, 1.0];
515 let res: Vec<f32> = (0..3)
516 .map(|c| (0..4).map(|k| v[k] * p[c][k]).sum())
517 .collect();
518 assert!(close(res[0], 0.0) && close(res[1], 1.0) && close(res[2], 0.0));
519 let m = rot_matrix(0.0, 0.0, 0.0, [0.25, 0.5, 0.75]);
521 let p = pack_float4x3(&m);
522 assert_eq!([p[0][3], p[1][3], p[2][3]], [0.25, 0.5, 0.75]);
523 }
524
525 #[test]
526 fn randomize_matches_md2_distribution() {
527 let r = RotParams::randomize(&mut Rng::new(9));
528 assert_eq!(r.speed[0], [0.0; 3]);
530 for k in 1..4 {
531 let lim = 0.9 * (k as f32 / 8.0).powf(3.2) + 1e-6;
532 assert!(
533 r.speed[k].iter().all(|s| s.abs() <= lim),
534 "static rotation {k} speed bound"
535 );
536 }
537 for k in 0..NUM_ROT {
538 for c in 0..3 {
539 assert!(r.base[k][c] >= 0.0 && r.base[k][c] < 6.28);
540 assert!(r.xlate[k][c].abs() <= 1.0);
541 assert!(r.speed[k][c].abs() <= 0.9 * (k as f32 / 8.0).powf(3.2) + 1e-6);
542 }
543 }
544 }
545
546 fn fill(f: &FrameInput, rules: &CompatRules) -> MilkUniforms {
547 fill_milk_uniforms(
548 &MilkUniformInputs::for_frame(640, 360, f, &RotParams::default(), rules, None),
549 &mut Rng::new(1),
550 )
551 }
552
553 #[test]
556 fn shader_time_is_the_ext_time_wrapped_at_10000() {
557 let rules = Compat::MilkDrop3.rules();
558 let mut f = frame();
560 f.ext.time = Some(12345.5);
561 let u = fill(&f, &rules);
562 assert_eq!(u.c[2][0], 2345.5);
563 let base = fill(&frame(), &rules);
564 assert_eq!(
565 u.c[2][1..],
566 base.c[2][1..],
567 "fps, frame, progress untouched"
568 );
569 assert_eq!(
570 u.c[8..12],
571 base.c[8..12],
572 "roam registers use FrameInput::time"
573 );
574 let mut g = frame();
576 g.time = 12345.5;
577 assert_eq!(fill(&g, &rules).c[2][0], 2345.5);
578 let mut h = frame();
580 h.time = 99999.0;
581 h.ext.time = Some(3.25);
582 assert_eq!(fill(&h, &rules).c[2][0], 3.25);
583 }
584
585 #[test]
587 fn md2_shader_time_is_not_wrapped() {
588 let mut f = frame();
589 f.time = 12345.5;
590 assert_eq!(fill(&f, &Compat::MilkDrop2.rules()).c[2][0], 12345.5);
591 assert_eq!(fill(&f, &Compat::ProjectM.rules()).c[2][0], 2345.5);
592 }
593
594 #[test]
597 fn default_ext_leaves_the_registers_alone() {
598 let rules = Compat::MilkDrop2.rules();
599 let bytes = |f: &FrameInput| bytemuck::bytes_of(&fill(f, &rules)).to_vec();
600 let f = frame();
601 assert_eq!(f.ext, ShaderExt::default());
602 assert_eq!(fill(&f, &rules).c[2][0], 12.5);
603 let base = bytes(&f);
604
605 let mut g = frame();
606 g.ext.mouse = [0.3, 0.6, 1.0, 1.0];
607 g.ext.q_hi = [7.0; 32];
608 g.ext.fft = Some(std::sync::Arc::new([0.5; lactea_abi::FFT_BINS]));
609 assert_eq!(bytes(&g), base);
610
611 let mut h = frame();
612 h.ext.time = Some(h.time);
613 assert_eq!(bytes(&h), base);
614 }
615}