//! Every screenshot MAME took during the reference runs is reproduced by the //! Rust video pipeline, pixel for pixel. Needs rom/galaga; skips without it. //! Our frames are written to target/video// for viewing either way. use std::path::Path; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::trace::parse_inputs; use emu_galaga::galaga::video::{Video, SCREEN_HEIGHT, SCREEN_WIDTH}; use emu_galaga::galaga::Machine; fn read_png(path: &Path) -> (u32, u32, Vec) { let decoder = png::Decoder::new(std::fs::File::open(path).unwrap()); let mut reader = decoder.read_info().unwrap(); let mut buf = vec![0; reader.output_buffer_size()]; let info = reader.next_frame(&mut buf).unwrap(); let bytes = &buf[..info.buffer_size()]; let channels = match info.color_type { png::ColorType::Rgb => 3, png::ColorType::Rgba => 4, other => panic!("unexpected PNG colour type {other:?}"), }; let rgb: Vec = bytes .chunks(channels) .flat_map(|px| px[..3].to_vec()) .collect(); (info.width, info.height, rgb) } fn write_png(path: &Path, rgba: &[u8]) { std::fs::create_dir_all(path.parent().unwrap()).unwrap(); let file = std::fs::File::create(path).unwrap(); let mut encoder = png::Encoder::new(file, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32); encoder.set_color(png::ColorType::Rgba); encoder.set_depth(png::BitDepth::Eight); let mut writer = encoder.write_header().unwrap(); writer.write_image_data(rgba).unwrap(); } #[test] fn reference_screenshots_are_identical() { let root = Path::new(env!("CARGO_MANIFEST_DIR")); let Ok(roms) = load_directory(&root.join("rom/galaga")) else { eprintln!("skipped: no extracted set at rom/galaga"); return; }; let runs: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(root.join("oracle/runs.json")).unwrap()) .unwrap(); let mut failures = Vec::new(); let mut compared = 0; for run_def in runs["runs"].as_array().unwrap() { let name = run_def["name"].as_str().unwrap(); let frames = run_def["frames"].as_u64().unwrap(); let inputs = parse_inputs(run_def["inputs"].as_str().unwrap_or("")).unwrap(); let snaps: Vec = run_def["snaps"] .as_str() .unwrap_or("") .split(',') .filter_map(|s| s.trim().parse().ok()) .collect(); let mut machine = Machine::new(&roms); let mut video = Video::new(&roms); let mut rgba = vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4]; let mut next_input = 0; for frame in 0..frames { machine.run_frame(); video.render(&machine.shared.borrow(), machine.videolatch_at_vblank_end); if snaps.contains(&frame) { video.screen_rgba(&mut rgba); let ours_path = root.join(format!("target/video/{name}/frame{frame:05}.png")); write_png(&ours_path, &rgba); let reference = root.join(format!("oracle/reference/{name}/frame{frame:05}.png")); let (w, h, theirs) = read_png(&reference); assert_eq!( (w, h), (SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32), "{name} frame {frame}: size" ); let differing = (0..SCREEN_WIDTH * SCREEN_HEIGHT) .filter(|&i| rgba[i * 4..i * 4 + 3] != theirs[i * 3..i * 3 + 3]) .count(); compared += 1; if differing != 0 { let first = (0..SCREEN_WIDTH * SCREEN_HEIGHT) .find(|&i| rgba[i * 4..i * 4 + 3] != theirs[i * 3..i * 3 + 3]) .unwrap(); failures.push(format!( "{name} frame {frame}: {differing} of {} pixels differ; first at ({}, {}): ours {:?} vs MAME {:?} (ours written to {})", SCREEN_WIDTH * SCREEN_HEIGHT, first % SCREEN_WIDTH, first / SCREEN_WIDTH, &rgba[first * 4..first * 4 + 3], &theirs[first * 3..first * 3 + 3], ours_path.display() )); } } while next_input < inputs.len() && inputs[next_input].frame <= frame { machine .set_control(&inputs[next_input].control, inputs[next_input].pressed) .unwrap(); next_input += 1; } } } assert!(compared > 0, "no screenshots compared"); assert!(failures.is_empty(), "{}", failures.join("\n")); }