dev/asset-management (#6)
Co-authored-by: Nilton Constantino <nilton.constantino@visma.com> Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use prometeu_core::hardware::{AudioCommand, Channel, LoopMode, MAX_CHANNELS, OUTPUT_SAMPLE_RATE};
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use prometeu_core::hardware::{AudioCommand, Channel, LoopMode, MAX_CHANNELS, OUTPUT_SAMPLE_RATE};
|
||||
use ringbuf::traits::{Consumer, Producer, Split};
|
||||
use ringbuf::HeapRb;
|
||||
use std::sync::Arc;
|
||||
@@ -69,7 +69,9 @@ impl HostAudio {
|
||||
pub fn send_commands(&mut self, commands: &mut Vec<AudioCommand>) {
|
||||
if let Some(producer) = &mut self.producer {
|
||||
for cmd in commands.drain(..) {
|
||||
let _ = producer.try_push(cmd);
|
||||
if let Err(_) = producer.try_push(cmd) {
|
||||
eprintln!("[HostAudio] Command ringbuffer full, dropping command.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,6 +88,7 @@ impl HostAudio {
|
||||
pub struct AudioMixer {
|
||||
voices: [Channel; MAX_CHANNELS],
|
||||
pub last_processing_time: Duration,
|
||||
paused: bool,
|
||||
}
|
||||
|
||||
impl AudioMixer {
|
||||
@@ -93,6 +96,7 @@ impl AudioMixer {
|
||||
Self {
|
||||
voices: Default::default(),
|
||||
last_processing_time: Duration::ZERO,
|
||||
paused: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,8 +112,10 @@ impl AudioMixer {
|
||||
loop_mode,
|
||||
} => {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
println!("[AudioMixer] Playing voice {}: vol={}, pitch={}, loop={:?}", voice_id, volume, pitch, loop_mode);
|
||||
self.voices[voice_id] = Channel {
|
||||
sample: Some(sample),
|
||||
active: true,
|
||||
pos: 0.0,
|
||||
pitch,
|
||||
volume,
|
||||
@@ -121,6 +127,7 @@ impl AudioMixer {
|
||||
}
|
||||
AudioCommand::Stop { voice_id } => {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
self.voices[voice_id].active = false;
|
||||
self.voices[voice_id].sample = None;
|
||||
}
|
||||
}
|
||||
@@ -139,6 +146,14 @@ impl AudioMixer {
|
||||
self.voices[voice_id].pitch = pitch;
|
||||
}
|
||||
}
|
||||
AudioCommand::MasterPause => {
|
||||
println!("[AudioMixer] Master Pause");
|
||||
self.paused = true;
|
||||
}
|
||||
AudioCommand::MasterResume => {
|
||||
println!("[AudioMixer] Master Resume");
|
||||
self.paused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +164,11 @@ impl AudioMixer {
|
||||
*sample = 0.0;
|
||||
}
|
||||
|
||||
if self.paused {
|
||||
self.last_processing_time = start.elapsed();
|
||||
return;
|
||||
}
|
||||
|
||||
for voice in self.voices.iter_mut() {
|
||||
let sample_data = match &voice.sample {
|
||||
Some(s) => s,
|
||||
@@ -168,6 +188,7 @@ impl AudioMixer {
|
||||
let pos_fract = voice.pos - pos_int as f64;
|
||||
|
||||
if pos_int >= sample_data.data.len() {
|
||||
voice.active = false;
|
||||
voice.sample = None;
|
||||
break;
|
||||
}
|
||||
@@ -197,6 +218,7 @@ impl AudioMixer {
|
||||
let loop_start = sample_data.loop_start.unwrap_or(0) as f64;
|
||||
voice.pos = loop_start + (voice.pos - end_pos);
|
||||
} else {
|
||||
voice.active = false;
|
||||
voice.sample = None;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -230,17 +230,32 @@ impl HostDebugger {
|
||||
|
||||
// Map Certification tags (0xCA01-0xCA03) to 'Cert' protocol events.
|
||||
if event.tag >= 0xCA01 && event.tag <= 0xCA03 {
|
||||
let rule = match event.tag {
|
||||
0xCA01 => "cycles_budget",
|
||||
0xCA02 => "max_syscalls",
|
||||
0xCA03 => "max_host_cpu_us",
|
||||
_ => "unknown"
|
||||
}.to_string();
|
||||
let tel = &firmware.os.telemetry_last;
|
||||
let cert_config = &firmware.os.certifier.config;
|
||||
|
||||
let (rule, used, limit) = match event.tag {
|
||||
0xCA01 => (
|
||||
"cycles_budget".to_string(),
|
||||
tel.cycles_used,
|
||||
cert_config.cycles_budget_per_frame.unwrap_or(0),
|
||||
),
|
||||
0xCA02 => (
|
||||
"max_syscalls".to_string(),
|
||||
tel.syscalls as u64,
|
||||
cert_config.max_syscalls_per_frame.unwrap_or(0) as u64,
|
||||
),
|
||||
0xCA03 => (
|
||||
"max_host_cpu_us".to_string(),
|
||||
tel.host_cpu_time_us,
|
||||
cert_config.max_host_cpu_us_per_frame.unwrap_or(0),
|
||||
),
|
||||
_ => ("unknown".to_string(), 0, 0),
|
||||
};
|
||||
|
||||
self.send_event(DebugEvent::Cert {
|
||||
rule,
|
||||
used: 0,
|
||||
limit: 0,
|
||||
used,
|
||||
limit,
|
||||
frame_index: firmware.os.logical_frame_index,
|
||||
});
|
||||
}
|
||||
@@ -261,6 +276,14 @@ impl HostDebugger {
|
||||
vm_steps: tel.vm_steps,
|
||||
syscalls: tel.syscalls,
|
||||
cycles: tel.cycles_used,
|
||||
host_cpu_time_us: tel.host_cpu_time_us,
|
||||
violations: tel.violations,
|
||||
gfx_used_bytes: tel.gfx_used_bytes,
|
||||
gfx_inflight_bytes: tel.gfx_inflight_bytes,
|
||||
gfx_slots_occupied: tel.gfx_slots_occupied,
|
||||
audio_used_bytes: tel.audio_used_bytes,
|
||||
audio_inflight_bytes: tel.audio_inflight_bytes,
|
||||
audio_slots_occupied: tel.audio_slots_occupied,
|
||||
});
|
||||
self.last_telemetry_frame = current_frame;
|
||||
}
|
||||
|
||||
@@ -64,6 +64,8 @@ pub struct HostRunner {
|
||||
|
||||
/// The physical audio driver.
|
||||
audio: HostAudio,
|
||||
/// Last known pause state to sync with audio.
|
||||
last_paused_state: bool,
|
||||
}
|
||||
|
||||
impl HostRunner {
|
||||
@@ -99,6 +101,7 @@ impl HostRunner {
|
||||
debugger: HostDebugger::new(),
|
||||
overlay_enabled: false,
|
||||
audio: HostAudio::new(),
|
||||
last_paused_state: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,21 +127,31 @@ impl HostRunner {
|
||||
let color_bg = prometeu_core::model::Color::INDIGO; // Dark blue to stand out
|
||||
let color_warn = prometeu_core::model::Color::RED;
|
||||
|
||||
self.hardware.gfx.fill_rect(5, 5, 140, 65, color_bg);
|
||||
self.hardware.gfx.fill_rect(5, 5, 140, 100, color_bg);
|
||||
self.hardware.gfx.draw_text(10, 10, &format!("FPS: {:.1}", self.stats.current_fps), color_text);
|
||||
self.hardware.gfx.draw_text(10, 18, &format!("HOST: {:.2}MS", tel.host_cpu_time_us as f64 / 1000.0), color_text);
|
||||
self.hardware.gfx.draw_text(10, 26, &format!("STEPS: {}", tel.vm_steps), color_text);
|
||||
self.hardware.gfx.draw_text(10, 34, &format!("SYSC: {}", tel.syscalls), color_text);
|
||||
self.hardware.gfx.draw_text(10, 42, &format!("CYC: {}", tel.cycles_used), color_text);
|
||||
|
||||
self.hardware.gfx.draw_text(10, 50, &format!("GFX: {}K/16M ({}S)", tel.gfx_used_bytes / 1024, tel.gfx_slots_occupied), color_text);
|
||||
if tel.gfx_inflight_bytes > 0 {
|
||||
self.hardware.gfx.draw_text(10, 58, &format!("LOAD GFX: {}KB", tel.gfx_inflight_bytes / 1024), color_warn);
|
||||
}
|
||||
|
||||
self.hardware.gfx.draw_text(10, 66, &format!("AUD: {}K/32M ({}S)", tel.audio_used_bytes / 1024, tel.audio_slots_occupied), color_text);
|
||||
if tel.audio_inflight_bytes > 0 {
|
||||
self.hardware.gfx.draw_text(10, 74, &format!("LOAD AUD: {}KB", tel.audio_inflight_bytes / 1024), color_warn);
|
||||
}
|
||||
|
||||
let cert_color = if tel.violations > 0 { color_warn } else { color_text };
|
||||
self.hardware.gfx.draw_text(10, 50, &format!("CERT LAST: {}", tel.violations), cert_color);
|
||||
self.hardware.gfx.draw_text(10, 82, &format!("CERT LAST: {}", tel.violations), cert_color);
|
||||
|
||||
if tel.violations > 0 {
|
||||
if let Some(event) = self.firmware.os.log_service.get_recent(10).into_iter().rev().find(|e| e.tag >= 0xCA01 && e.tag <= 0xCA03) {
|
||||
let mut msg = event.msg.clone();
|
||||
if msg.len() > 30 { msg.truncate(30); }
|
||||
self.hardware.gfx.draw_text(10, 58, &msg, color_warn);
|
||||
self.hardware.gfx.draw_text(10, 90, &msg, color_warn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,7 +279,21 @@ impl ApplicationHandler for HostRunner {
|
||||
while self.accumulator >= self.frame_target_dt {
|
||||
// Unless the debugger is waiting for a 'start' command, advance the system.
|
||||
if !self.debugger.waiting_for_start {
|
||||
self.firmware.step_frame(&self.input.signals, &mut self.hardware);
|
||||
self.firmware.tick(&self.input.signals, &mut self.hardware);
|
||||
}
|
||||
|
||||
// Sync pause state with audio.
|
||||
// We do this AFTER firmware.tick to avoid MasterPause/Resume commands
|
||||
// being cleared by the OS if a new logical frame starts in this tick.
|
||||
let is_paused = self.firmware.os.paused || self.debugger.waiting_for_start;
|
||||
if is_paused != self.last_paused_state {
|
||||
self.last_paused_state = is_paused;
|
||||
let cmd = if is_paused {
|
||||
prometeu_core::hardware::AudioCommand::MasterPause
|
||||
} else {
|
||||
prometeu_core::hardware::AudioCommand::MasterResume
|
||||
};
|
||||
self.hardware.audio.commands.push(cmd);
|
||||
}
|
||||
|
||||
// Sync virtual audio commands to the physical mixer.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use prometeu_core::firmware::Firmware;
|
||||
use prometeu_core::Hardware;
|
||||
use std::time::{Duration, Instant};
|
||||
use winit::window::Window;
|
||||
use prometeu_core::Hardware;
|
||||
use prometeu_core::firmware::Firmware;
|
||||
|
||||
pub struct HostStats {
|
||||
pub last_stats_update: Instant,
|
||||
@@ -31,14 +31,12 @@ impl HostStats {
|
||||
self.audio_load_samples += 1;
|
||||
}
|
||||
|
||||
pub fn update(&mut self, now: Instant, window: Option<&Window>, hardware: &Hardware, firmware: &Firmware) {
|
||||
pub fn update(&mut self, now: Instant, window: Option<&Window>, _hardware: &Hardware, firmware: &Firmware) {
|
||||
let stats_elapsed = now.duration_since(self.last_stats_update);
|
||||
if stats_elapsed >= Duration::from_secs(1) {
|
||||
self.current_fps = self.frames_since_last_update as f64 / stats_elapsed.as_secs_f64();
|
||||
|
||||
if let Some(window) = window {
|
||||
let kb = hardware.gfx.memory_usage_bytes() as f64 / 1024.0;
|
||||
|
||||
// Fixed comparison always against 60Hz, keep even when doing CPU stress tests
|
||||
let frame_budget_us = 16666.0;
|
||||
let cpu_load_core = (firmware.os.last_frame_cpu_time_us as f64 / frame_budget_us) * 100.0;
|
||||
@@ -51,7 +49,7 @@ impl HostStats {
|
||||
|
||||
let title = format!(
|
||||
"PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: tick {} logical {}",
|
||||
kb, self.current_fps, cpu_load_core, cpu_load_audio, firmware.os.tick_index, firmware.os.logical_frame_index
|
||||
0, self.current_fps, cpu_load_core, cpu_load_audio, firmware.os.tick_index, firmware.os.logical_frame_index
|
||||
);
|
||||
window.set_title(&title);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user