Files
benilla-octo/third_party/kira/src/info.rs
T
SamandClaude Fable 5.1 b1294ce9a1 the quest windows go stock, and the 20 ms frames were mis-split pairs
- the quest giver's dialog and the quest log are the reference's own `QuestFrame.xml` and `QuestLogFrame.xml`, run off the player's chain at the reference's own toc seats; our 1366-line and 1999-line copies are deleted, and the verbs the stock files call are built — the four `GameTooltip` quest-item and reward-spell setters, the reward spell itself (parsed on all three packets and dropped by the model until now), and two verbs; the detail pane could never scroll because `UpdateScrollChildRect` measured a frame late where the reference's measure is synchronous, so it measures through the installed font engine now; and ten things the reference does that ours did not, each found by a test that had enshrined ours — a shift-click never selects, an empty log hides the whole detail pane and says "No Active Quests", the abandon confirm lives in the popup registry (1944)
- the quest bindings' law, read off the binary and folded back: no 1.12 packet carries a quest background material — `GetQuestBackgroundMaterial` (`0x502230`) forks on the dialog's SOURCE object, an item answering its template's PageMaterial, a GameObject its template data, a creature nil (FrameXML's "Parchment"), the id naming a `PageTextMaterial.dbc` row whose six words are nowhere in the binary; its twin `ItemTextGetMaterial` has the same body, so the reader window already held every piece and the giver's feed runs the same resolve; `QuestChooseRewardError` is game-error row `0x98`, and the row plays `igQuestFailed` first, which the verb now queues; the reward-spell getters' third return is `Spell.dbc` Attributes bit 5 (1161 of 22357 rows set — craft-cast spells set, `Pattern:` teaching spells clear), read off the catalog now (1946)
- the "20 ms frames" of a raid, chased to their number: the probe's worst frames now carry their churn (entities, assets, first-play sound decodes, live particles, the GPU reading) and in a Blackrock raid with sound on every one of those read flat; then the same window on the PACED clock — under vsync with pipelined rendering the raw delta is mis-split into long/short pairs that sum to exact refresh multiples, so an alone Stormwind leg read 115 of 300 frames over budget with cpu and gpu both at their means, while the paced clock read 0 skipped frames in every leg; what remains real in the raid is `cpu_p99` at 22–23 ms with sound on; and every submesh the streamer spawns now carries why it is an entity, counted per reason — 5.8k resident ADT-doodad submeshes at the Stormwind auction house, 5.3k of them hidden, three prop families the retained pass declines by rule, each riding every per-`Mesh3d` sweep for ~0.4 ms of the alone frame (1947)
- a raid's dozen voices cost the mixer ~1.5 ms of every 60 Hz frame: upstream kira spatializes every frame — normalizes, the attenuation curve and a dB→amplitude `powf` per frame per spatial track, two more `powf`s for the volume tween, ~1.7 M `powf`s a second at 48 kHz × 12 tracks for gains that change imperceptibly across a chunk — so kira 0.12.1 is vendored with one patch: the spatial gains and the volume × fade amplitude are evaluated at the chunk's two ends and interpolated across its ~10.7 ms, the granularity FMOD 3, the reference's own mixer, updates them at; nothing a listener can name changes (1945); and rig poses are evaluated in parallel over rigs — each reads its shared clips and writes only its own pose, and a raid's ~400 live rigs were 0.4 ms of one thread walked serially on the main thread's critical path (1947)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-04 20:55:12 +07:00

295 lines
8.4 KiB
Rust

/*!
* Types for providing info about resources to trait implementors.
*
* You'll only need this if you're implementing one of Kira's traits,
* like [`Sound`](crate::sound::Sound) or [`Effect`](crate::effect::Effect).
*/
use atomic_arena::Arena;
use glam::{Quat, Vec3};
use crate::{
clock::{Clock, ClockId, ClockTime, State as ClockState},
listener::{Listener, ListenerId},
modulator::{Modulator, ModulatorId},
};
/// Provides info about resources on the audio thread.
///
/// You'll only need this if you're implementing one of Kira's traits,
/// like [`Sound`](crate::sound::Sound) or [`Effect`](crate::effect::Effect).
pub struct Info<'a> {
kind: InfoKind<'a>,
spatial_track_info: Option<SpatialTrackInfo>,
}
impl<'a> Info<'a> {
pub(crate) fn new(
clocks: &'a Arena<Clock>,
modulators: &'a Arena<Box<dyn Modulator>>,
listeners: &'a Arena<Listener>,
spatial_track_info: Option<SpatialTrackInfo>,
) -> Self {
Self {
kind: InfoKind::Real {
clocks,
modulators,
listeners,
},
spatial_track_info,
}
}
/// Gets information about the clock with the given ID if it
/// exists, returns `None` otherwise.
#[must_use]
pub fn clock_info(&self, id: ClockId) -> Option<ClockInfo> {
match &self.kind {
InfoKind::Real { clocks, .. } => clocks.get(id.0).map(|clock| ClockInfo {
ticking: clock.ticking(),
time: ClockTime {
clock: id,
ticks: match clock.state() {
ClockState::NotStarted => 0,
ClockState::Started { ticks, .. } => ticks,
},
fraction: match clock.state() {
ClockState::NotStarted => 0.0,
ClockState::Started {
fractional_position,
..
} => fractional_position,
},
},
}),
InfoKind::Mock { clock_info, .. } => clock_info.get(id.0).copied(),
}
}
/// Returns whether something with the given start time should
/// start now, later, or never given the current state of the clocks.
#[must_use]
pub fn when_to_start(&self, time: ClockTime) -> WhenToStart {
if let Some(clock_info) = self.clock_info(time.clock) {
if clock_info.ticking && clock_info.time >= time {
WhenToStart::Now
} else {
WhenToStart::Later
}
} else {
WhenToStart::Never
}
}
/// Gets the value of the modulator with the given ID if it
/// exists, returns `None` otherwise.
#[must_use]
pub fn modulator_value(&self, id: ModulatorId) -> Option<f64> {
match &self.kind {
InfoKind::Real { modulators, .. } => {
modulators.get(id.0).map(|modulator| modulator.value())
}
InfoKind::Mock {
modulator_values, ..
} => modulator_values.get(id.0).copied(),
}
}
/// Gets information about the listener linked to the current spatial track
/// if there is one.
#[must_use]
pub fn listener_info(&self) -> Option<ListenerInfo> {
self.spatial_track_info.and_then(|spatial_track_info| {
let listener_id = spatial_track_info.listener_id;
match &self.kind {
InfoKind::Real { listeners, .. } => {
listeners.get(listener_id.0).map(|listener| ListenerInfo {
position: listener.position.value().into(),
orientation: listener.orientation.value().into(),
previous_position: listener.position.previous_value().into(),
previous_orientation: listener.orientation.previous_value().into(),
})
}
InfoKind::Mock { listener_info, .. } => listener_info.get(listener_id.0).copied(),
}
})
}
/// If this is called from an effect on a spatial track, returns the distance
/// of the spatial track's listener from the spatial track. Otherwise, returns
/// `None`.
pub fn listener_distance(&self) -> Option<f32> {
self.spatial_track_info.zip(self.listener_info()).map(
|(spatial_track_info, listener_info)| {
Vec3::from(listener_info.position).distance(spatial_track_info.position)
},
)
}
}
/// Information about the current state of a [clock](super::clock).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ClockInfo {
/// Whether the clock is currently running.
pub ticking: bool,
/// The current time of the clock.
pub time: ClockTime,
}
/// When something should start given the current state
/// of the clocks.
///
/// The "something" in question can be anything that
/// would start at a given [`StartTime`](crate::StartTime).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WhenToStart {
/// The thing should start now.
Now,
/// The thing should start later because the appropriate
/// clock isn't ticking or hasn't reached the target tick
/// yet.
Later,
/// The thing will never start because the clock it depends
/// on no longer exists.
Never,
}
/// Information about a listener.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ListenerInfo {
/// The position of the listener.
pub position: mint::Vector3<f32>,
/// The rotation of the listener.
pub orientation: mint::Quaternion<f32>,
/// The position of the listener prior to the last update.
pub previous_position: mint::Vector3<f32>,
/// The rotation of the listener prior to the last update.
pub previous_orientation: mint::Quaternion<f32>,
}
impl ListenerInfo {
/// Returns the interpolated position between the previous and current
/// position of the listener.
pub fn interpolated_position(self, amount: f32) -> mint::Vector3<f32> {
let position: Vec3 = self.position.into();
let previous_position: Vec3 = self.previous_position.into();
previous_position.lerp(position, amount).into()
}
/// Returns the interpolated orientation between the previous and current
/// orientation of the listener.
pub fn interpolated_orientation(self, amount: f32) -> mint::Quaternion<f32> {
let orientation: Quat = self.orientation.into();
let previous_orientation: Quat = self.previous_orientation.into();
previous_orientation.lerp(orientation, amount).into()
}
}
/// Generates a fake `Info` with arbitrary data. Useful for writing unit tests.
pub struct MockInfoBuilder {
clock_info: Arena<ClockInfo>,
modulator_values: Arena<f64>,
listener_info: Arena<ListenerInfo>,
spatial_track_info: Option<SpatialTrackInfo>,
}
impl MockInfoBuilder {
/// Creates a new `MockInfoBuilder`.
pub fn new() -> Self {
Self {
clock_info: Arena::new(100),
modulator_values: Arena::new(100),
listener_info: Arena::new(100),
spatial_track_info: None,
}
}
/// Adds a fake clock with the given ticking state and time. Returns a fake
/// `ClockId`.
pub fn add_clock(&mut self, ticking: bool, ticks: u64, fraction: f64) -> ClockId {
let id = ClockId(
self.clock_info
.controller()
.try_reserve()
.expect("clock info arena is full"),
);
self.clock_info
.insert_with_key(
id.0,
ClockInfo {
ticking,
time: ClockTime {
clock: id,
ticks,
fraction,
},
},
)
.unwrap();
id
}
/// Adds a fake modulator outputting the given value. Returns a fake `ModulatorId`.
pub fn add_modulator(&mut self, value: f64) -> ModulatorId {
let id = ModulatorId(
self.modulator_values
.controller()
.try_reserve()
.expect("modulator info arena is full"),
);
self.modulator_values.insert_with_key(id.0, value).unwrap();
id
}
/// Adds a fake listener at the given position and orientation. Returns a fake `ListenerId`.
pub fn add_listener(&mut self, listener_info: ListenerInfo) -> ListenerId {
let id = ListenerId(
self.listener_info
.controller()
.try_reserve()
.expect("listener info arena is full"),
);
self.listener_info
.insert_with_key(id.0, listener_info)
.unwrap();
id
}
/// Consumes the `MockInfoProvider` and returns a fake `Info`.
pub fn build(self) -> Info<'static> {
Info {
kind: InfoKind::Mock {
clock_info: self.clock_info,
modulator_values: self.modulator_values,
listener_info: self.listener_info,
},
spatial_track_info: self.spatial_track_info,
}
}
}
impl Default for MockInfoBuilder {
fn default() -> Self {
Self::new()
}
}
enum InfoKind<'a> {
Real {
clocks: &'a Arena<Clock>,
modulators: &'a Arena<Box<dyn Modulator>>,
listeners: &'a Arena<Listener>,
},
Mock {
clock_info: Arena<ClockInfo>,
modulator_values: Arena<f64>,
listener_info: Arena<ListenerInfo>,
},
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct SpatialTrackInfo {
pub position: Vec3,
pub listener_id: ListenerId,
}