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