|
| 1 | +//! wgpu visual backend client for the scheduler. |
| 2 | +//! |
| 3 | +//! Wraps the `VisualWgpuBackend` from the visual-bridge crate and falls |
| 4 | +//! back to the `VisualReferenceBackend` when GPU initialisation fails. |
| 5 | +//! Audio and lighting events are forwarded to the reference backends. |
| 6 | +
|
| 7 | +use std::cell::RefCell; |
| 8 | + |
| 9 | +use vidodo_ir::{ |
| 10 | + AudioEvent, BackendAck, BackendAdapter, BackendHealthSnapshot, BackendTopology, |
| 11 | + ExecutablePayload, LightingEvent, VisualEvent, |
| 12 | +}; |
| 13 | + |
| 14 | +use crate::BackendClient; |
| 15 | +use crate::audio_backend::AudioReferenceBackend; |
| 16 | +use crate::lighting_backend::LightingReferenceBackend; |
| 17 | +use crate::visual_backend::VisualReferenceBackend; |
| 18 | + |
| 19 | +/// Indicates whether the wgpu backend was successfully prepared. |
| 20 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 21 | +pub enum WgpuAvailability { |
| 22 | + Available, |
| 23 | + Fallback, |
| 24 | +} |
| 25 | + |
| 26 | +/// Backend client that attempts to use the real wgpu visual backend. |
| 27 | +/// |
| 28 | +/// If wgpu initialisation fails (prepare returns error), it falls back |
| 29 | +/// to the reference visual backend with a diagnostic message. |
| 30 | +pub struct WgpuBackendClient { |
| 31 | + visual: RefCell<VisualBackendState>, |
| 32 | + audio: RefCell<AudioReferenceBackend>, |
| 33 | + lighting: RefCell<LightingReferenceBackend>, |
| 34 | + availability: WgpuAvailability, |
| 35 | + diagnostics: Vec<String>, |
| 36 | +} |
| 37 | + |
| 38 | +enum VisualBackendState { |
| 39 | + Wgpu(Box<vidodo_visual_bridge::backend::VisualWgpuBackend>), |
| 40 | + Fallback(VisualReferenceBackend), |
| 41 | +} |
| 42 | + |
| 43 | +impl WgpuBackendClient { |
| 44 | + /// Attempt to create a wgpu visual backend client. |
| 45 | + /// |
| 46 | + /// Tries to prepare the `VisualWgpuBackend` with a flat display topology. |
| 47 | + /// On failure, falls back to the reference backend and records the diagnostic. |
| 48 | + pub fn new() -> Self { |
| 49 | + let mut wgpu = vidodo_visual_bridge::backend::VisualWgpuBackend::new("wgpu-v1"); |
| 50 | + let topology = BackendTopology::Visual { |
| 51 | + topology_ref: String::from("flat-main"), |
| 52 | + calibration_profile: None, |
| 53 | + display_endpoints: vec![String::from("main")], |
| 54 | + }; |
| 55 | + |
| 56 | + let (visual, availability, diagnostics) = match wgpu.prepare_backend(&topology) { |
| 57 | + Ok(()) => { |
| 58 | + (VisualBackendState::Wgpu(Box::new(wgpu)), WgpuAvailability::Available, vec![]) |
| 59 | + } |
| 60 | + Err(reason) => ( |
| 61 | + VisualBackendState::Fallback(VisualReferenceBackend::new("fallback-visual")), |
| 62 | + WgpuAvailability::Fallback, |
| 63 | + vec![format!("wgpu unavailable ({reason}), fell back to reference visual backend")], |
| 64 | + ), |
| 65 | + }; |
| 66 | + |
| 67 | + Self { |
| 68 | + visual: RefCell::new(visual), |
| 69 | + audio: RefCell::new(AudioReferenceBackend::new("ref-audio")), |
| 70 | + lighting: RefCell::new(LightingReferenceBackend::new("ref-lighting")), |
| 71 | + availability, |
| 72 | + diagnostics, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + pub fn availability(&self) -> WgpuAvailability { |
| 77 | + self.availability |
| 78 | + } |
| 79 | + |
| 80 | + pub fn diagnostics(&self) -> &[String] { |
| 81 | + &self.diagnostics |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl Default for WgpuBackendClient { |
| 86 | + fn default() -> Self { |
| 87 | + Self::new() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl BackendClient for WgpuBackendClient { |
| 92 | + fn dispatch_audio(&self, event: &AudioEvent) -> BackendAck { |
| 93 | + let payload = ExecutablePayload::Audio { |
| 94 | + layer_id: event.layer_id.clone(), |
| 95 | + op: event.op.clone(), |
| 96 | + target_asset_id: event.target_asset_id.clone(), |
| 97 | + gain_db: event.gain_db, |
| 98 | + duration_beats: event.duration_beats, |
| 99 | + route_set_ref: event.route_set_ref.clone(), |
| 100 | + speaker_group: event.speaker_group.clone(), |
| 101 | + }; |
| 102 | + self.audio.borrow_mut().execute_payload(&payload).unwrap_or_else(|detail| BackendAck { |
| 103 | + backend: String::from("ref-audio"), |
| 104 | + target: event.layer_id.clone(), |
| 105 | + status: String::from("error"), |
| 106 | + detail, |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + fn dispatch_visual(&self, event: &VisualEvent) -> BackendAck { |
| 111 | + let payload = ExecutablePayload::Visual { |
| 112 | + scene_id: event.scene_id.clone(), |
| 113 | + shader_program: event.shader_program.clone(), |
| 114 | + uniforms: event.uniforms.clone(), |
| 115 | + duration_beats: event.duration_beats, |
| 116 | + blend: event.blend.clone(), |
| 117 | + view_group: event.view_group.clone(), |
| 118 | + }; |
| 119 | + match &mut *self.visual.borrow_mut() { |
| 120 | + VisualBackendState::Wgpu(backend) => { |
| 121 | + backend.execute_payload(&payload).unwrap_or_else(|detail| BackendAck { |
| 122 | + backend: String::from("wgpu-v1"), |
| 123 | + target: event.scene_id.clone(), |
| 124 | + status: String::from("error"), |
| 125 | + detail, |
| 126 | + }) |
| 127 | + } |
| 128 | + VisualBackendState::Fallback(backend) => { |
| 129 | + backend.execute_payload(&payload).unwrap_or_else(|detail| BackendAck { |
| 130 | + backend: String::from("fallback-visual"), |
| 131 | + target: event.scene_id.clone(), |
| 132 | + status: String::from("error"), |
| 133 | + detail, |
| 134 | + }) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + fn dispatch_lighting(&self, event: &LightingEvent) -> BackendAck { |
| 140 | + let payload = ExecutablePayload::Lighting { |
| 141 | + cue_set_id: event.cue_set_id.clone(), |
| 142 | + source_ref: event.source_ref.clone(), |
| 143 | + fixture_group: event.fixture_group.clone(), |
| 144 | + intensity: event.intensity, |
| 145 | + color: event.color, |
| 146 | + fade_beats: event.fade_beats, |
| 147 | + }; |
| 148 | + self.lighting.borrow_mut().execute_payload(&payload).unwrap_or_else(|detail| BackendAck { |
| 149 | + backend: String::from("ref-lighting"), |
| 150 | + target: event.cue_set_id.clone(), |
| 151 | + status: String::from("error"), |
| 152 | + detail, |
| 153 | + }) |
| 154 | + } |
| 155 | + |
| 156 | + fn health_snapshots(&self) -> Vec<BackendHealthSnapshot> { |
| 157 | + let visual_status = match &*self.visual.borrow() { |
| 158 | + VisualBackendState::Wgpu(b) => b.collect_backend_status(), |
| 159 | + VisualBackendState::Fallback(b) => b.collect_backend_status(), |
| 160 | + }; |
| 161 | + vec![BackendHealthSnapshot { |
| 162 | + backend_ref: String::from("wgpu-visual"), |
| 163 | + plugin_ref: visual_status.plugin_id, |
| 164 | + status: visual_status.status, |
| 165 | + timestamp: String::from("0"), |
| 166 | + latency_ms: visual_status.latency_ms, |
| 167 | + error_count: visual_status.error_count, |
| 168 | + last_ack_lag_ms: visual_status.last_ack_lag_ms, |
| 169 | + degrade_reason: visual_status.detail, |
| 170 | + }] |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +#[cfg(test)] |
| 175 | +mod tests { |
| 176 | + use super::*; |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn wgpu_client_creates_with_fallback() { |
| 180 | + let client = WgpuBackendClient::new(); |
| 181 | + assert!( |
| 182 | + client.availability() == WgpuAvailability::Available |
| 183 | + || client.availability() == WgpuAvailability::Fallback |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn dispatch_visual_returns_ack() { |
| 189 | + let client = WgpuBackendClient::new(); |
| 190 | + let event = VisualEvent { |
| 191 | + scene_id: String::from("scene-1"), |
| 192 | + shader_program: String::from("particles-basic"), |
| 193 | + output_backend: String::from("wgpu"), |
| 194 | + view_group: None, |
| 195 | + display_topology: None, |
| 196 | + calibration_profile: None, |
| 197 | + uniforms: std::collections::BTreeMap::new(), |
| 198 | + views: vec![], |
| 199 | + duration_beats: Some(4), |
| 200 | + blend: None, |
| 201 | + }; |
| 202 | + let ack = client.dispatch_visual(&event); |
| 203 | + assert_eq!(ack.target, "scene-1"); |
| 204 | + assert!(ack.status == "ok" || ack.status == "error"); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn diagnostics_on_fallback() { |
| 209 | + let client = WgpuBackendClient::new(); |
| 210 | + if client.availability() == WgpuAvailability::Fallback { |
| 211 | + assert!(!client.diagnostics().is_empty()); |
| 212 | + assert!(client.diagnostics()[0].contains("wgpu unavailable")); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + #[test] |
| 217 | + fn dispatch_audio_via_reference() { |
| 218 | + let client = WgpuBackendClient::new(); |
| 219 | + let event = AudioEvent { |
| 220 | + layer_id: String::from("layer-1"), |
| 221 | + op: String::from("play"), |
| 222 | + output_backend: String::from("ref-audio"), |
| 223 | + route_mode: None, |
| 224 | + route_set_ref: None, |
| 225 | + speaker_group: vec![], |
| 226 | + gain_db: Some(-6.0), |
| 227 | + duration_beats: Some(8), |
| 228 | + filter: None, |
| 229 | + automation: std::collections::BTreeMap::new(), |
| 230 | + target_asset_id: Some(String::from("pad.wav")), |
| 231 | + }; |
| 232 | + let ack = client.dispatch_audio(&event); |
| 233 | + assert_eq!(ack.target, "layer-1"); |
| 234 | + assert!(ack.status == "ok" || ack.status == "error"); |
| 235 | + } |
| 236 | + |
| 237 | + #[test] |
| 238 | + fn health_snapshot_reports_visual() { |
| 239 | + let client = WgpuBackendClient::new(); |
| 240 | + let snaps = client.health_snapshots(); |
| 241 | + assert_eq!(snaps.len(), 1); |
| 242 | + assert_eq!(snaps[0].backend_ref, "wgpu-visual"); |
| 243 | + } |
| 244 | +} |
0 commit comments