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//! Shader inputs that are not part of the frozen [`lactea_abi::MilkUniforms`] block: for MilkDrop 3 the mouse
5//! (`_c14`), `q33..q64` (`_qi.._qp`), the shader FFT and the per-state shader time; for BeatDrop (MilkDrop 2 mode) the
6//! mouse (`_c14`), time / date (`_c15` / `_c16`) and the spectrum / waveform texture. Everything here is neutral
7//! (`mouse = (-1, -1, 0, 0)`, zero `q`, no FFT update, absolute time) outside those modes.
8
9use lactea_abi::{FFT_BINS, MilkExtUniforms};
10use std::sync::Arc;
11
12/// Texels per row of BeatDrop's `sampler_fft` (`MY_FFT_SHADER_SAMPLES`); equals `lactea_audio::SHADER_FFT_BINS`.
13pub const BEATDROP_FFT_BINS: usize = 1024;
14/// Texels per row of BeatDrop's `sampler_wave` (`NUM_WAVEFORM_SAMPLES`); equals `lactea_audio::SHADER_WAVE_SAMPLES`.
15pub const BEATDROP_WAVE_SAMPLES: usize = 480;
16
17/// BeatDrop's two shader textures, both `R32F` with two rows (`plugin.cpp`, `m_lpFFTTexture` / `m_lpWaveTexture`,
18/// refilled every frame by `DoCustomSoundAnalysis`): `sampler_fft` is [`BEATDROP_FFT_BINS`] x 2 (row 0 the smoothed
19/// spectrum, row 1 the peak hold) and `sampler_wave` [`BEATDROP_WAVE_SAMPLES`] x 2 (row 0 left, row 1 right, the
20/// waveform divided by 128). The renderer keeps them as two textures of their own.
21#[derive(Clone, Debug, PartialEq)]
22pub struct BeatDropTexture {
23    /// Row 0 of `sampler_fft`: `m_fFFTSmoothed`.
24    pub spectrum: [f32; BEATDROP_FFT_BINS],
25    /// Row 1 of `sampler_fft`: `m_fFFTPeak`.
26    pub peak: [f32; BEATDROP_FFT_BINS],
27    /// Rows 0 (left) and 1 (right) of `sampler_wave`.
28    pub wave: [[f32; BEATDROP_WAVE_SAMPLES]; 2],
29}
30
31impl BeatDropTexture {
32    /// The texels in upload order: the two rows of `sampler_fft` ([`BEATDROP_FFT_BINS`] each), then the two rows of
33    /// `sampler_wave` ([`BEATDROP_WAVE_SAMPLES`] each): `2 * (BEATDROP_FFT_BINS + BEATDROP_WAVE_SAMPLES)` floats.
34    pub fn texels(&self) -> Vec<f32> {
35        let mut t = Vec::with_capacity(2 * (BEATDROP_FFT_BINS + BEATDROP_WAVE_SAMPLES));
36        t.extend_from_slice(&self.spectrum);
37        t.extend_from_slice(&self.peak);
38        for w in &self.wave {
39            t.extend_from_slice(w);
40        }
41        t
42    }
43}
44
45/// See the module docs. One per shader pass owner: [`crate::FrameInput::ext`] for the state the frame is built for,
46/// [`crate::Md2OldPass::ext`] for the outgoing state of a MilkDrop 2 / 3 blend.
47#[derive(Clone, Debug, PartialEq)]
48pub struct ShaderExt {
49    /// `_c14` = `(x, y, held, released)`: `x = px/W`, `y = py/H` (top origin), `-1, -1` outside the window.
50    pub mouse: [f32; 4],
51    /// `q33..q64` (`q_hi[0] == q33`) of the state after its per-frame code (`_qi.._qp`).
52    pub q_hi: [f32; 32],
53    /// What `_c2.x` shows instead of [`crate::FrameInput::time`]: seconds since this state's preset started (the
54    /// renderer wraps it modulo 10000 like it does the absolute time). `None` = `FrameInput::time`.
55    pub time: Option<f64>,
56    /// The smoothed shader FFT (`sampler_fft`, [`FFT_BINS`] values), uploaded to the renderer's FFT texture before
57    /// the frame is drawn. `None` keeps the texture as it is (zeros until the first upload). The texture is
58    /// renderer-global: for [`crate::Md2OldPass::ext`] this field is ignored, the new state's one is used.
59    pub fft: Option<Arc<[f32; FFT_BINS]>>,
60    /// BeatDrop's spectrum / waveform texture (`CompatRules::md2_shader_inputs`), uploaded like [`fft`](Self::fft)
61    /// (renderer-global, ignored for [`crate::Md2OldPass::ext`]). `None` keeps the texture as it is.
62    pub beatdrop: Option<Arc<BeatDropTexture>>,
63    /// BeatDrop's `_c15` (`hour, minute, second, totalSeconds`) and `_c16` (`year, month, day, weekday`); they travel
64    /// in the first two `q_hi` registers of the extension block, which nothing else uses outside MilkDrop 3.
65    /// `None` leaves `q_hi` as it is.
66    pub time_date: Option<[[f32; 4]; 2]>,
67    /// The `_mxd3qxa .. _mxd3qsc` audio-envelope / beat-latch float4s (the MilkDrop 3 block).
68    pub mxd3q: [[f32; 4]; 7],
69}
70
71impl Default for ShaderExt {
72    fn default() -> Self {
73        Self {
74            mouse: [-1.0, -1.0, 0.0, 0.0],
75            q_hi: [0.0; 32],
76            time: None,
77            fft: None,
78            beatdrop: None,
79            time_date: None,
80            mxd3q: [[0.0; 4]; 7],
81        }
82    }
83}
84
85impl ShaderExt {
86    /// The uniform block uploaded at `@group(0) @binding(1)`.
87    pub fn uniforms(&self) -> MilkExtUniforms {
88        let mut u = MilkExtUniforms {
89            mouse: self.mouse,
90            mxd3q: self.mxd3q,
91            ..Default::default()
92        };
93        for (i, bank) in u.q_hi.iter_mut().enumerate() {
94            bank.copy_from_slice(&self.q_hi[i * 4..i * 4 + 4]);
95        }
96        if let Some(td) = self.time_date {
97            u.q_hi[0] = td[0];
98            u.q_hi[1] = td[1];
99        }
100        u
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn q_banks_pack_four_by_four() {
110        let mut e = ShaderExt::default();
111        for (i, q) in e.q_hi.iter_mut().enumerate() {
112            *q = 33.0 + i as f32;
113        }
114        let u = e.uniforms();
115        assert_eq!(u.q_hi[0], [33.0, 34.0, 35.0, 36.0]);
116        assert_eq!(u.q_hi[7], [61.0, 62.0, 63.0, 64.0]);
117        assert_eq!(u.mouse, [-1.0, -1.0, 0.0, 0.0]);
118    }
119}