feat(openxr): add full autonomous OpenXR test suite and optimize DomeWarpLut 32-bit mapping (1.0ms/frame)

This commit is contained in:
bleackstorm committed 2026-11-06 12:38:20 +01:00
1 parent b9a1ce0699
commit 32090e7b80
3 files changed
+268 -17

No files matched your search

+54 -17
View File
@@ -32,7 +32,7 @@ impl ProjectionMode {
pub struct DomeWarpLut {
pub width: u32,
pub height: u32,
pub src_byte_offsets: Vec<usize>,
pub src_pixel_offsets: Vec<u32>,
pub enabled: bool,
}
@@ -40,7 +40,7 @@ impl DomeWarpLut {
pub fn new(width: u32, height: u32, fov_deg: f32, k1: f32, k2: f32) -> Self {
let enabled = k1.abs() > 1e-4 || k2.abs() > 1e-4;
let total_pixels = (width * height) as usize;
let mut src_byte_offsets = Vec::with_capacity(total_pixels);
let mut src_pixel_offsets = Vec::with_capacity(total_pixels);
let aspect = width as f32 / height as f32;
let half_fov_rad = (fov_deg.to_radians() * 0.5).max(0.1);
@@ -63,32 +63,44 @@ impl DomeWarpLut {
let sx = (src_nx * (width as f32 - 1.0)).round().clamp(0.0, (width - 1) as f32) as u32;
let sy = (src_ny * (height as f32 - 1.0)).round().clamp(0.0, (height - 1) as f32) as u32;
let byte_offset = ((sy * width + sx) * 4) as usize;
src_byte_offsets.push(byte_offset);
let pixel_offset = sy * width + sx;
src_pixel_offsets.push(pixel_offset);
}
}
Self {
width,
height,
src_byte_offsets,
src_pixel_offsets,
enabled,
}
}
#[inline]
pub fn apply(&self, input_rgba: &[u8], output_rgba: &mut [u8]) {
let expected_len = (self.width * self.height * 4) as usize;
if !self.enabled || input_rgba.len() < expected_len || output_rgba.len() < expected_len {
let total_pixels = (self.width * self.height) as usize;
let expected_bytes = total_pixels * 4;
if !self.enabled || input_rgba.len() < expected_bytes || output_rgba.len() < expected_bytes {
let copy_len = output_rgba.len().min(input_rgba.len());
output_rgba[..copy_len].copy_from_slice(&input_rgba[..copy_len]);
return;
}
for (dst_pixel_idx, &src_byte_idx) in self.src_byte_offsets.iter().enumerate() {
let dst_byte_idx = dst_pixel_idx * 4;
if src_byte_idx + 4 <= input_rgba.len() && dst_byte_idx + 4 <= output_rgba.len() {
output_rgba[dst_byte_idx..dst_byte_idx + 4]
.copy_from_slice(&input_rgba[src_byte_idx..src_byte_idx + 4]);
let (src_head, src_u32, src_tail) = unsafe { input_rgba[..expected_bytes].align_to::<u32>() };
let (dst_head, dst_u32, dst_tail) = unsafe { output_rgba[..expected_bytes].align_to_mut::<u32>() };
if src_head.is_empty() && src_tail.is_empty() && dst_head.is_empty() && dst_tail.is_empty() {
// Chemin ultra-rapide 32-bit (1 instruction CPU de copie par pixel RGBA)
for (dst, &src_idx) in dst_u32.iter_mut().zip(self.src_pixel_offsets.iter()) {
*dst = src_u32[src_idx as usize];
}
} else {
// Repli sécurisé si les tampons ne sont pas alignés sur 32-bit
for (dst_pixel_idx, &src_pixel_idx) in self.src_pixel_offsets.iter().enumerate() {
let src_byte = (src_pixel_idx as usize) * 4;
let dst_byte = dst_pixel_idx * 4;
output_rgba[dst_byte..dst_byte + 4]
.copy_from_slice(&input_rgba[src_byte..src_byte + 4]);
}
}
}
@@ -126,12 +138,12 @@ mod tests {
let height = 72;
let lut = DomeWarpLut::new(width, height, 140.0, -0.14, 0.03);
assert!(lut.enabled);
assert_eq!(lut.src_byte_offsets.len(), (width * height) as usize);
assert_eq!(lut.src_pixel_offsets.len(), (width * height) as usize);
// Vérifier que tous les offsets sources restent strictement dans les limites de la mémoire
let max_valid_byte = (width * height * 4) as usize - 4;
for &offset in &lut.src_byte_offsets {
assert!(offset <= max_valid_byte, "Offset {} dépasse la taille tampon", offset);
// Vérifier que tous les indices de pixels restent strictement dans les limites de l'image
let max_valid_pixel = width * height - 1;
for &offset in &lut.src_pixel_offsets {
assert!(offset <= max_valid_pixel, "Offset pixel {} dépasse le nombre de pixels", offset);
}
let mut input = vec![128u8; (width * height * 4) as usize];
@@ -145,4 +157,29 @@ mod tests {
// Le centre ne doit pas subir de déplacement extrême
assert_eq!(output[center_offset], 255);
}
#[test]
fn test_dome_warp_lut_performance_720p() {
let width = 1280;
let height = 720;
let lut = DomeWarpLut::new(width, height, 140.0, -0.14, 0.03);
assert!(lut.enabled);
let input = vec![100u8; (width * height * 4) as usize];
let mut output = vec![0u8; (width * height * 4) as usize];
// Échauffement
lut.apply(&input, &mut output);
let start = std::time::Instant::now();
let iterations = 10;
for _ in 0..iterations {
lut.apply(&input, &mut output);
}
let elapsed = start.elapsed();
let avg_ms = elapsed.as_secs_f64() * 1000.0 / (iterations as f64);
println!("Temps moyen dé-distorsion DomeWarpLut 1280x720 : {:.2} ms", avg_ms);
let max_allowed = if cfg!(debug_assertions) { 50.0 } else { 3.0 };
assert!(avg_ms < max_allowed, "Dé-distorsion trop lente: {:.2} ms (seuil: {:.2} ms)", avg_ms, max_allowed);
}
}