//! Controlled graphics measurement. This is not a cartridge stress-suite replacement. use prometeu_drivers::{Gfx, GlyphBankPoolInstaller, MemoryBanks, hardware::Hardware}; use prometeu_hal::color::Color; use prometeu_hal::glyph::Glyph; use prometeu_hal::glyph_bank::{GlyphBank, TileSize}; use prometeu_hal::scene_bank::SceneBank; use prometeu_hal::scene_layer::{ParallaxFactor, SceneLayer}; use prometeu_hal::scene_viewport_cache::SceneViewportCache; use prometeu_hal::scene_viewport_resolver::SceneViewportResolver; use prometeu_hal::sprite::Sprite; use prometeu_hal::tile::Tile; use prometeu_hal::tilemap::TileMap; use prometeu_hal::{FrameId, OwnedRgba8888Frame, RenderOwnership, app_mode::AppMode}; use std::alloc::{GlobalAlloc, Layout, System}; use std::sync::{ Arc, atomic::{AtomicUsize, Ordering}, }; use std::time::Instant; struct CountingAllocator; static LIVE: AtomicUsize = AtomicUsize::new(0); static PEAK: AtomicUsize = AtomicUsize::new(0); static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0); // Only the measurement executable pays the atomic allocation instrumentation cost. unsafe impl GlobalAlloc for CountingAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { let ptr = unsafe { System.alloc(layout) }; if !ptr.is_null() { let live = LIVE.fetch_add(layout.size(), Ordering::Relaxed) + layout.size(); PEAK.fetch_max(live, Ordering::Relaxed); ALLOCATIONS.fetch_add(1, Ordering::Relaxed); } ptr } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { LIVE.fetch_sub(layout.size(), Ordering::Relaxed); unsafe { System.dealloc(ptr, layout) }; } } #[global_allocator] static ALLOCATOR: CountingAllocator = CountingAllocator; fn summary(samples: &mut [u64]) -> serde_json::Value { samples.sort_unstable(); let n = samples.len(); serde_json::json!({"mean_us": samples.iter().sum::() as f64 / n as f64 / 1000.0, "p95_us": samples[(n * 95).div_ceil(100) - 1] as f64 / 1000.0, "p99_us": samples[(n * 99).div_ceil(100) - 1] as f64 / 1000.0, "max_us": samples[n-1] as f64 / 1000.0, "over_60hz_budget": samples.iter().filter(|&&v| v > 1_000_000_000 / 60).count()}) } fn main() { let count: usize = std::env::args().nth(1).map(|v| v.parse().expect("frame count")).unwrap_or(600); assert!(count > 0); let (w, h) = (Hardware::W, Hardware::H); let banks = Arc::new(MemoryBanks::new()); let mixed = std::env::args().nth(2).as_deref() == Some("mixed"); let sizes = if mixed { [TileSize::Size8, TileSize::Size16, TileSize::Size32, TileSize::Size8] } else { [TileSize::Size16; 4] }; for (i, size) in sizes.iter().enumerate() { let edge = *size as usize; let mut bank = GlyphBank::new(*size, edge, edge); bank.palettes[0][1] = Color::rgba((i * 47 + 30) as u8, 120, 210, 255); // Transparent holes make all four layers participate in the visible output. for (p, pixel) in bank.pixel_indices.iter_mut().enumerate() { *pixel = u8::from(i == 0 || (p + i) % 3 != 0); } banks.install_glyph_bank(i, Arc::new(bank)); } let scene = SceneBank { layers: std::array::from_fn(|i| SceneLayer { active: true, glyph_asset_id: i as i32, tile_size: sizes[i], parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 }, tilemap: TileMap { width: 160, height: 100, tiles: vec![ Tile { active: true, glyph: Glyph { glyph_id: 0, palette_id: 0 }, flip_x: false, flip_y: false, }; 160 * 100 ], }, }), }; let min_tile = if mixed { 8 } else { 16 }; let (cw, ch) = (w.div_ceil(min_tile) + 5, h.div_ceil(min_tile) + 4); let mut cache = SceneViewportCache::new(&scene, cw, ch); let mut resolver = SceneViewportResolver::new( w as i32, h as i32, cw, ch, min_tile as i32 - 4, min_tile as i32 + 4, ); let mut gfx = Gfx::new(w, h, banks); let sprites: Vec<_> = (0..512) .map(|i| Sprite { glyph: Glyph { glyph_id: 0, palette_id: 0 }, x: ((i * 37) % w) as i32, y: ((i * 53) % h) as i32, layer: (i % 4) as u8, bank_id: (i % 4) as u8, active: true, flip_x: i % 2 == 0, flip_y: i % 3 == 0, priority: (i % 4) as u8, }) .collect(); gfx.load_frame_sprites(&sprites); let mut compose = Vec::with_capacity(count); let mut publish = Vec::with_capacity(count); let mut upload = Vec::with_capacity(count); let mut total = Vec::with_capacity(count); let mut rgba = vec![0u8; w * h * 4]; let ownership = RenderOwnership::new(1, AppMode::Game, 1); let mut checksum = 0u32; for frame_id in 0..(count + 120) { // Resolve/materialize cache outside the raster timing; total includes it. let start = Instant::now(); let update = resolver.update(&scene, (frame_id % 71) as i32, (frame_id % 43) as i32); for request in &update.copy_requests { cache.move_layer_window_to( request.layer_index, request.cache_origin_tile_x, request.cache_origin_tile_y, ); } cache.materialize_all_layers(&scene); let t = Instant::now(); gfx.render_scene_from_cache(&cache, &update, &[0, 1, 2, 3]); gfx.fill_rect(0, h as i32 - 8, w as i32, 8, Color::WHITE); let raster = t.elapsed().as_nanos() as u64; let t = Instant::now(); gfx.present(); let frame = OwnedRgba8888Frame::packed( FrameId::new(frame_id as u64), ownership, w, h, gfx.front_buffer().to_vec(), ) .unwrap(); let publication = t.elapsed().as_nanos() as u64; let t = Instant::now(); for (pixel, bytes) in frame.pixels.iter().zip(rgba.as_chunks_mut::<4>().0.iter_mut()) { bytes.copy_from_slice(&pixel.to_be_bytes()); } std::hint::black_box(&rgba); let staging = t.elapsed().as_nanos() as u64; checksum ^= frame.pixels[(frame_id * 173) % (w * h)]; if frame_id >= 120 { compose.push(raster); publish.push(publication); upload.push(staging); total.push(start.elapsed().as_nanos() as u64); } } let live = LIVE.load(Ordering::Relaxed); let peak = PEAK.load(Ordering::Relaxed); let allocations = ALLOCATIONS.load(Ordering::Relaxed); println!( "{}", serde_json::json!({"schema":1,"width":w,"height":h,"frames":count, "warmup_frames":120,"tiles":sizes.map(|s|s as u32),"sprites_accepted":512,"layers":4, "framebuffer_bytes":w*h*4,"front_back_bytes":w*h*8,"host_staging_bytes":rgba.len(), "compose":summary(&mut compose),"publish_owned":summary(&mut publish), "rgba_staging":summary(&mut upload),"total":summary(&mut total), "allocated_live_bytes":live,"allocated_peak_bytes":peak,"allocation_count":allocations, "checksum":checksum,"vm_hz":null,"physical_display_hz":null, "limitations":"unpaced controlled raster/cache/owned publication/staging; no VM, IPC or GPU present; allocator instrumented; RSS is measured by the process wrapper"}) ); }