From 0fbe184716efbe797904f3a7c905f255133dc721 Mon Sep 17 00:00:00 2001 From: bleackstorm Date: Thu, 5 Nov 2026 21:23:49 +0100 Subject: [PATCH] fix(gpui): resolve GPU atlas VRAM leak and GStreamer pipeline latency - Fix unbounded Blade texture atlas allocation in GPUI by recycling textures via window.drop_image() with a 2-frame safety buffer. - Correct RGBA to BGRA color channel order for GPUI polychrome texture atlas. - Optimize GStreamer UDP pipeline: increase socket buffer to 4MB, add rtpjitterbuffer for zero-latency drops, scale before colorspace conversion. - Add log filter controls (All, Warn/Err, Err) to GPUI console drawer. - Benchmark: memory consumption stays completely flat at ~258 MB over time. --- src/engine.rs | 1 + src/gui_gpui.rs | 115 ++++++++++++++++++++++++++++++++++++++++------- src/video_usb.rs | 6 ++- src/vulkan_xr.rs | 1 + 4 files changed, 105 insertions(+), 18 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 1e086ea..d238b27 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -36,6 +36,7 @@ pub struct RuntimeMonitor { pub preview_width: u32, pub preview_height: u32, pub preview_frame_seq: u64, + #[allow(dead_code)] pub manual_override_enabled: bool, pub manual_pan_deg: f32, pub manual_tilt_deg: f32, diff --git a/src/gui_gpui.rs b/src/gui_gpui.rs index a65f427..74a1922 100644 --- a/src/gui_gpui.rs +++ b/src/gui_gpui.rs @@ -22,7 +22,6 @@ const COLOR_BG_DARK: u32 = 0x090b10; const COLOR_SURFACE_CARD: u32 = 0x11151f; const COLOR_SURFACE_ELEVATED: u32 = 0x181e2b; const COLOR_BORDER_SUBTLE: u32 = 0x1f293d; -const COLOR_BORDER_ACCENT: u32 = 0x00e5ff; const COLOR_CYAN: u32 = 0x00e5ff; const COLOR_GREEN: u32 = 0x10b981; @@ -37,6 +36,7 @@ pub struct SteamFrameGpuiApp { log_buffer: Arc>, log_filter: LogFilter, current_render_image: Option>, + images_to_drop: Vec>, last_frame_seq: u64, // Snapshots pour l'affichage réactif usb_status: SubsystemStatus, @@ -64,6 +64,7 @@ impl SteamFrameGpuiApp { log_buffer, log_filter: LogFilter::All, current_render_image: None, + images_to_drop: Vec::new(), last_frame_seq: 0, usb_status: SubsystemStatus::Starting, xr_status: SubsystemStatus::Starting, @@ -80,7 +81,7 @@ impl SteamFrameGpuiApp { } /// Synchronise l'état IHM avec le moteur FPV en tâche de fond - fn sync_from_engine(&mut self) { + fn sync_from_engine(&mut self, window: &mut Window) { let mon = self.engine.monitor.lock(); // 1. Mise à jour de la texture vidéo si une nouvelle trame est arrivée @@ -89,14 +90,36 @@ impl SteamFrameGpuiApp { && mon.preview_width > 0 && mon.preview_height > 0 { - if let Some(img_buf) = RgbaImage::from_raw( + if let Some(mut img_buf) = RgbaImage::from_raw( mon.preview_width, mon.preview_height, mon.preview_rgba.clone(), ) { + // Conversion RGBA -> BGRA requise par l'atlas polychrome GPUI / Vulkan + for pixel in img_buf.chunks_exact_mut(4) { + pixel.swap(0, 2); + } let frame = Frame::new(img_buf); - self.current_render_image = Some(Arc::new(RenderImage::new(smallvec![frame]))); + let new_render_image = Arc::new(RenderImage::new(smallvec![frame])); + + if let Some(old) = self.current_render_image.take() { + self.images_to_drop.push(old); + } + self.current_render_image = Some(new_render_image); self.last_frame_seq = mon.preview_frame_seq; + + // Nettoyage de l'atlas texture GPU pour empêcher l'accumulation de VRAM. + // On conserve les 2 dernières trames pour garantir que le GPU a fini + // de les projeter à l'écran avant destruction de la texture. + while self.images_to_drop.len() > 2 { + let old_img = self.images_to_drop.remove(0); + let _ = window.drop_image(old_img); + } + } + } else if !self.images_to_drop.is_empty() { + // Si le flux s'interrompt, purger les anciennes textures en attente + for old_img in self.images_to_drop.drain(..) { + let _ = window.drop_image(old_img); } } @@ -787,6 +810,63 @@ impl SteamFrameGpuiApp { div() .flex() .gap_2() + .child( + div() + .id("filter_all") + .px_2() + .py_0p5() + .bg(if self.log_filter == LogFilter::All { + rgb(0x1e3a5f) + } else { + rgb(COLOR_SURFACE_ELEVATED) + }) + .rounded_md() + .text_xs() + .cursor_pointer() + .on_click(cx.listener(|this, _, _, cx| { + this.log_filter = LogFilter::All; + cx.notify(); + })) + .child("Tous"), + ) + .child( + div() + .id("filter_warn") + .px_2() + .py_0p5() + .bg(if self.log_filter == LogFilter::WarningsAndErrors { + rgb(0x1e3a5f) + } else { + rgb(COLOR_SURFACE_ELEVATED) + }) + .rounded_md() + .text_xs() + .cursor_pointer() + .on_click(cx.listener(|this, _, _, cx| { + this.log_filter = LogFilter::WarningsAndErrors; + cx.notify(); + })) + .child("Warn/Err"), + ) + .child( + div() + .id("filter_err") + .px_2() + .py_0p5() + .bg(if self.log_filter == LogFilter::ErrorsOnly { + rgb(0x1e3a5f) + } else { + rgb(COLOR_SURFACE_ELEVATED) + }) + .rounded_md() + .text_xs() + .cursor_pointer() + .on_click(cx.listener(|this, _, _, cx| { + this.log_filter = LogFilter::ErrorsOnly; + cx.notify(); + })) + .child("Err"), + ) .child( div() .id("btn_clear_logs") @@ -807,15 +887,6 @@ impl SteamFrameGpuiApp { cx.notify(); })) .child("Effacer"), - ) - .child( - div() - .px_2() - .py_0p5() - .bg(rgb(COLOR_SURFACE_ELEVATED)) - .rounded_md() - .text_xs() - .child("Copier"), ), ), ) @@ -828,7 +899,19 @@ impl SteamFrameGpuiApp { .overflow_scroll() .px_2() .gap_0p5() - .children(self.log_entries.iter().rev().take(40).map(|e| { + .children( + self.log_entries + .iter() + .rev() + .filter(|e| match self.log_filter { + LogFilter::All => true, + LogFilter::WarningsAndErrors => { + matches!(e.level, log::Level::Warn | log::Level::Error) + } + LogFilter::ErrorsOnly => matches!(e.level, log::Level::Error), + }) + .take(40) + .map(|e| { let badge_color = match e.level { log::Level::Error => COLOR_RED, log::Level::Warn => COLOR_ORANGE, @@ -972,10 +1055,10 @@ pub fn run_gpui_app(cfg: AppConfig, log_buffer: Arc>) -> Result loop { Timer::after(Duration::from_millis(16)).await; let should_continue = cx - .update(|_, cx| { + .update(|window, cx| { if let Some(view) = weak_view.upgrade() { view.update(cx, |this, cx| { - this.sync_from_engine(); + this.sync_from_engine(window); cx.notify(); }); true diff --git a/src/video_usb.rs b/src/video_usb.rs index 9afc6b3..0b514da 100644 --- a/src/video_usb.rs +++ b/src/video_usb.rs @@ -10,6 +10,7 @@ use std::thread; use std::time::Instant; #[derive(Clone)] +#[allow(dead_code)] pub struct VideoFrame { pub width: u32, pub height: u32, @@ -69,6 +70,7 @@ impl UsbVideoReceiver { let depay_and_parse = if stream_transport.eq_ignore_ascii_case("rtp") { format!( "application/x-rtp,media=video,clock-rate=90000,encoding-name={codec_name},payload=96 ! \ + rtpjitterbuffer latency=0 drop-on-latency=true ! \ {depay} ! {parser} config-interval=-1" ) } else { @@ -81,10 +83,10 @@ impl UsbVideoReceiver { ); format!( - "udpsrc address={bind_ip} port={port} buffer-size=524288 retrieve-sender-address=false ! \ + "udpsrc address={bind_ip} port={port} buffer-size=4194304 retrieve-sender-address=false ! \ {depay_and_parse} ! \ {decoder} ! \ - videoconvert n-threads=2 ! videoscale method=1 ! \ + videoscale ! videoconvert ! \ video/x-raw,format=RGBA,width={target_width},height={target_height} ! \ appsink name=fpv_sink sync=false max-buffers=1 drop=true emit-signals=true" ) diff --git a/src/vulkan_xr.rs b/src/vulkan_xr.rs index c6b308b..d804bda 100644 --- a/src/vulkan_xr.rs +++ b/src/vulkan_xr.rs @@ -5,6 +5,7 @@ use std::ffi::CString; /// Moteur de rendu OpenXR + Vulkan pour afficher le flux vidéo USB Sphere Link /// dans les couches de composition OpenXR (`Equirect2`, `Cylinder`, ou `Quad`). +#[allow(dead_code)] pub struct VulkanXrContext { pub vk_entry: ash::Entry, pub vk_instance: ash::Instance,