Files
prometeu-runtime/crates/host/prometeu-host-headless/tests/camera_viewport.rs
T
bquarkz 0d20194aa2
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
fix(runtime): bound camera resolver work and handle extreme coordinates
Preserve camera hysteresis with constant-time wide arithmetic, refresh nonoverlapping cache jumps, and clip wide offsets before drawing. Add guest pixel-oracle and resource/snapshot regressions and document the contract.

Validated by the complete tooling verify run 20261126T113331Z-1d071c1d (all 11 stages passed), plus focused debug/release tests. Work: WRK-0020.
2026-11-26 11:47:32 +00:00

156 lines
6.5 KiB
Rust

#[path = "support/camera.rs"]
mod camera;
use camera::*;
use prometeu_drivers::{GlyphBankPoolAccess, SceneBankPoolAccess};
use prometeu_hal::{Gfx2dCommand, color::Color, primitives::Rect};
use prometeu_host_headless::{AdvanceFrameOutcome, HeadlessHost};
use std::sync::Arc;
fn assert_guest_frames(fixture: &CameraFixture, positions: &[(i32, i32)]) {
let cartridge = fixture.cartridge(positions);
let mut host = HeadlessHost::new();
host.boot(cartridge.0.to_string_lossy()).unwrap();
for (index, &(x, y)) in positions.iter().enumerate() {
let result = host.advance_frame().unwrap();
assert!(
matches!(result, AdvanceFrameOutcome::FrameCompleted { logical_frame_index, .. }
if logical_frame_index == index as u64 + 1),
"{result:?}, fault={:?}",
host.get_fault()
);
let frame = host.capture_frame().unwrap();
assert_pixels(&frame.frame.pixels, &fixture.expected(Some(&fixture.scene), x, y, true));
}
assert!(host.get_fault().is_none());
}
#[test]
fn guest_camera_follow_teleport_parallax_and_return_match_canonical_pixels() {
for profile in ["size8", "size16", "size32", "mixed", "finite-extreme"] {
let fixture = CameraFixture::new(profile, 512, 256);
let positions: Vec<_> = (0..24)
.map(|n| (n * 4 - 16, 40 + n % 4))
.chain((0..12).rev().map(|n| (n * 8, 42)))
.chain([
(1600, 1200),
(0, 0),
(-100, -70),
(150, 80),
(i32::MAX, i32::MIN),
(i32::MIN, i32::MAX),
(0, 0),
])
.collect();
assert_guest_frames(&fixture, &positions);
}
}
#[test]
fn guest_first_frame_handles_extreme_cameras_and_admitted_empty_scenes() {
for profile in ["size8", "mixed"] {
for first in [(i32::MIN, 0), (0, i32::MIN), (i32::MAX, i32::MAX)] {
assert_guest_frames(&CameraFixture::new(profile, 128, 128), &[first, (0, 0)]);
}
for (width, height) in [(u32::MAX as usize, 0), (0, u32::MAX as usize), (0, 0)] {
let fixture = CameraFixture::new(profile, width, height);
assert_guest_frames(&fixture, &[(0, 0), (i32::MAX, i32::MIN), (0, 0)]);
}
}
}
#[test]
fn closed_camera_packet_ignores_live_changes_and_preserves_resident_resources() {
for profile in ["size8", "mixed"] {
let fixture = CameraFixture::new(profile, 512, 256);
let mut hw = fixture.hardware(false);
let scene = hw.memory_banks.scene_bank_slot(0).unwrap();
let scene_bytes = scene_payload(&scene);
let glyphs: Vec<_> = (0..4).map(|i| hw.memory_banks.glyph_bank_slot(i).unwrap()).collect();
hw.frame_composer.bind_scene(0);
hw.frame_composer.set_camera(40, 20);
for sprite in sprites() {
assert!(hw.frame_composer.emit_sprite(sprite));
}
let packet = hw.frame_composer.close_game2d_packet();
let saved = packet.clone();
hw.frame_composer.set_camera(i32::MAX, i32::MIN);
hw.frame_composer.begin_frame();
hw.frame_composer.unbind_scene();
hw.frame_composer.render_game2d_frame_packet(&packet, &mut hw.gfx);
hw.gfx.present();
assert_eq!(packet, saved);
assert_eq!(hw.frame_composer.camera(), (40, 20));
assert_pixels(
hw.gfx.front_buffer(),
&fixture.expected(Some(&fixture.scene), 40, 20, false),
);
assert_cache(hw.frame_composer.cache().unwrap(), &fixture.scene);
assert!(Arc::ptr_eq(&scene, hw.frame_composer.active_scene().unwrap()));
assert_resident_identity(&hw, &[scene], &glyphs);
assert_eq!(scene_bytes, scene_payload(&hw.memory_banks.scene_bank_slot(0).unwrap()));
for (i, actual) in glyphs.iter().enumerate() {
let expected = fixture.glyph_bank(i);
assert_eq!(actual.pixel_indices, expected.pixel_indices);
assert_eq!(actual.palettes, expected.palettes);
}
}
}
#[test]
fn camera_scene_transitions_preserve_ring_contents_resources_and_no_scene_buffers() {
for profile in ["size8", "mixed"] {
let fixture = CameraFixture::new(profile, 512, 256);
let mut hw = fixture.hardware(true);
let scenes: Vec<_> = (0..2).map(|i| hw.memory_banks.scene_bank_slot(i).unwrap()).collect();
let glyphs: Vec<_> = (0..4).map(|i| hw.memory_banks.glyph_bank_slot(i).unwrap()).collect();
let mut reference_front = vec![Color::BLACK.raw(); WIDTH * HEIGHT];
let mut reference_back = reference_front.clone();
for (slot, x, y) in [
(Some(0), 0, 0),
(Some(0), 1600, 1200),
(None, 0, 0),
(Some(1), 100, 40),
(Some(0), 20, 10),
] {
hw.frame_composer.begin_frame();
if slot != hw.frame_composer.active_scene_id() {
match slot {
Some(slot) => assert!(hw.frame_composer.bind_scene(slot)),
None => hw.frame_composer.unbind_scene(),
}
}
hw.frame_composer.set_camera(x, y);
for sprite in sprites() {
assert!(hw.frame_composer.emit_sprite(sprite));
}
let packet = hw.frame_composer.close_game2d_packet();
assert_eq!((packet.composer.camera.x, packet.composer.camera.y), (x, y));
hw.frame_composer.render_game2d_frame_packet(&packet, &mut hw.gfx);
hw.gfx.apply_gfx2d_commands(&[Gfx2dCommand::FillRect {
rect: Rect { x: 3, y: 4, w: 13, h: 7 },
color: Color::CYAN,
}]);
hw.gfx.present();
let direct = fixture.expected(slot.map(|i| scenes[i].as_ref()), x, y, true);
let expected = if slot.is_some() {
direct
} else {
// No-scene composition preserves the existing back buffer.
let mut pixels = reference_back.clone();
for (index, &pixel) in direct.iter().enumerate() {
if pixel != Color::BLACK.raw() {
pixels[index] = pixel;
}
}
pixels
};
assert_pixels(hw.gfx.front_buffer(), &expected);
reference_back = std::mem::replace(&mut reference_front, expected);
assert_resident_identity(&hw, &scenes, &glyphs);
if let Some(i) = slot {
assert_cache(hw.frame_composer.cache().unwrap(), &scenes[i]);
}
}
}
}