Skip to main content

lactea_render/
shader_ext.rs

1// SPDX-FileCopyrightText: 2026 Gianluca Boiano
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! MilkDrop 3 shader inputs that are not part of the frozen [`lactea_abi::MilkUniforms`] block: the mouse
5//! (`_c14`), `q33..q64` (`_qi.._qp`), the shader FFT and the per-state shader time. Everything here is neutral
6//! (`mouse = (-1, -1, 0, 0)`, zero `q`, no FFT update, absolute time) outside MilkDrop 3.
7
8use lactea_abi::{FFT_BINS, MilkExtUniforms};
9use std::sync::Arc;
10
11/// See the module docs. One per shader pass owner: [`crate::FrameInput::ext`] for the state the frame is built for,
12/// [`crate::Md2OldPass::ext`] for the outgoing state of a MilkDrop 2 / 3 blend.
13#[derive(Clone, Debug, PartialEq)]
14pub struct ShaderExt {
15    /// `_c14` = `(x, y, held, released)`: `x = px/W`, `y = py/H` (top origin), `-1, -1` outside the window.
16    pub mouse: [f32; 4],
17    /// `q33..q64` (`q_hi[0] == q33`) of the state after its per-frame code (`_qi.._qp`).
18    pub q_hi: [f32; 32],
19    /// What `_c2.x` shows instead of [`crate::FrameInput::time`]: seconds since this state's preset started (the
20    /// renderer wraps it modulo 10000 like it does the absolute time). `None` = `FrameInput::time`.
21    pub time: Option<f64>,
22    /// The smoothed shader FFT (`sampler_fft`, [`FFT_BINS`] values), uploaded to the renderer's FFT texture before
23    /// the frame is drawn. `None` keeps the texture as it is (zeros until the first upload). The texture is
24    /// renderer-global: for [`crate::Md2OldPass::ext`] this field is ignored, the new state's one is used.
25    pub fft: Option<Arc<[f32; FFT_BINS]>>,
26    /// The `_mxd3qxa .. _mxd3qsc` audio-envelope / beat-latch float4s (the MilkDrop 3 block).
27    pub mxd3q: [[f32; 4]; 7],
28}
29
30impl Default for ShaderExt {
31    fn default() -> Self {
32        Self {
33            mouse: [-1.0, -1.0, 0.0, 0.0],
34            q_hi: [0.0; 32],
35            time: None,
36            fft: None,
37            mxd3q: [[0.0; 4]; 7],
38        }
39    }
40}
41
42impl ShaderExt {
43    /// The uniform block uploaded at `@group(0) @binding(1)`.
44    pub fn uniforms(&self) -> MilkExtUniforms {
45        let mut u = MilkExtUniforms {
46            mouse: self.mouse,
47            mxd3q: self.mxd3q,
48            ..Default::default()
49        };
50        for (i, bank) in u.q_hi.iter_mut().enumerate() {
51            bank.copy_from_slice(&self.q_hi[i * 4..i * 4 + 4]);
52        }
53        u
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn q_banks_pack_four_by_four() {
63        let mut e = ShaderExt::default();
64        for (i, q) in e.q_hi.iter_mut().enumerate() {
65            *q = 33.0 + i as f32;
66        }
67        let u = e.uniforms();
68        assert_eq!(u.q_hi[0], [33.0, 34.0, 35.0, 36.0]);
69        assert_eq!(u.q_hi[7], [61.0, 62.0, 63.0, 64.0]);
70        assert_eq!(u.mouse, [-1.0, -1.0, 0.0, 0.0]);
71    }
72}