Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

aspect aware screenshots, update readme

Layle40f7ccb42b40
diff --git a/.gitignore b/.gitignore
index 683aba6..76747d0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,4 +19,5 @@ config.toml
system/
*.sram
*.dff
-docs/*.md
\ No newline at end of file
+docs/*.md
+internal/
\ No newline at end of file
diff --git a/crates/backend-wgpu/src/capture.rs b/crates/backend-wgpu/src/capture.rs
index 51ffeaa..5ef7f7a 100644
--- a/crates/backend-wgpu/src/capture.rs
+++ b/crates/backend-wgpu/src/capture.rs
@@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::align_up;
+use crate::sink::TargetAspect;
pub struct CapturedFrame {
pub width: u32,
@@ -11,6 +12,49 @@ pub struct CapturedFrame {
pub rgba: Vec<u8>,
}
+impl CapturedFrame {
+ pub fn with_aspect(self, aspect: TargetAspect) -> Self {
+ let TargetAspect::Ratio(ratio) = aspect else {
+ return self;
+ };
+ let width = (self.height as f32 * ratio).round() as u32;
+ if width == self.width {
+ return self;
+ }
+
+ let scale = self.width as f32 / width as f32;
+ let max_x = self.width as usize - 1;
+ let samples: Vec<_> = (0..width)
+ .map(|x| {
+ let src = ((x as f32 + 0.5) * scale - 0.5).clamp(0.0, max_x as f32);
+ let left = src.floor() as usize;
+ (left * 4, (left + 1).min(max_x) * 4, src.fract())
+ })
+ .collect();
+
+ let mut rgba = vec![0; width as usize * self.height as usize * 4];
+ for (src, dst) in self
+ .rgba
+ .chunks_exact(self.width as usize * 4)
+ .zip(rgba.chunks_exact_mut(width as usize * 4))
+ {
+ for (pixel, &(left, right, weight)) in dst.chunks_exact_mut(4).zip(&samples) {
+ for channel in 0..4 {
+ pixel[channel] = (src[left + channel] as f32 * (1.0 - weight)
+ + src[right + channel] as f32 * weight)
+ .round() as u8;
+ }
+ }
+ }
+
+ Self {
+ width,
+ height: self.height,
+ rgba,
+ }
+ }
+}
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CaptureRequest {
None,
diff --git a/crates/backend-wgpu/src/sink.rs b/crates/backend-wgpu/src/sink.rs
index e9cee33..ac8648d 100644
--- a/crates/backend-wgpu/src/sink.rs
+++ b/crates/backend-wgpu/src/sink.rs
@@ -39,15 +39,17 @@ pub enum TargetAspect {
}
impl TargetAspect {
+ pub fn auto(is_wii: bool) -> Self {
+ if is_wii {
+ TargetAspect::Ratio(16.0 / 9.0)
+ } else {
+ TargetAspect::Ratio(4.0 / 3.0)
+ }
+ }
+
pub fn from_arg(arg: &str, is_wii: bool) -> Self {
match arg {
- "auto" => {
- if is_wii {
- TargetAspect::Ratio(16.0 / 9.0)
- } else {
- TargetAspect::Ratio(4.0 / 3.0)
- }
- }
+ "auto" => Self::auto(is_wii),
"4:3" => TargetAspect::Ratio(4.0 / 3.0),
"16:9" => TargetAspect::Ratio(16.0 / 9.0),
"stretch" => TargetAspect::Stretch,
@@ -640,6 +642,7 @@ pub struct Renderer {
blit_bind_group_layout: wgpu::BindGroupLayout,
blit_sampler: wgpu::Sampler,
target_aspect: TargetAspect,
+ last_target_size: Arc<Mutex<Option<(u32, u32)>>>,
frame_ready_cb: Arc<OnceLock<FrameReadyCallback>>,
#[cfg(feature = "renderdoc-capture")]
renderdoc: Arc<Mutex<crate::renderdoc_capture::RenderDocCapture>>,
@@ -776,6 +779,7 @@ impl Renderer {
blit_bind_group_layout,
blit_sampler,
target_aspect,
+ last_target_size: Arc::new(Mutex::new(None)),
frame_ready_cb,
#[cfg(feature = "renderdoc-capture")]
renderdoc,
@@ -827,7 +831,11 @@ impl Renderer {
/// resolution times the EFB scale. Blocks until the GPU copy completes.
pub fn capture_xfb(&self) -> Option<crate::capture::CapturedFrame> {
let texture = self.shared.output.lock().unwrap().clone();
- crate::capture::capture_texture(&self.device, &self.queue, &texture)
+ let aspect = match (self.target_aspect, *self.last_target_size.lock().unwrap()) {
+ (TargetAspect::Stretch, Some((width, height))) => TargetAspect::Ratio(width as f32 / height as f32),
+ (aspect, _) => aspect,
+ };
+ crate::capture::capture_texture(&self.device, &self.queue, &texture).map(|frame| frame.with_aspect(aspect))
}
/// Blit the latest XFB output to the given render target. `target_size`
@@ -857,6 +865,7 @@ impl Renderer {
target_size: (u32, u32),
load: wgpu::LoadOp<wgpu::Color>,
) {
+ *self.last_target_size.lock().unwrap() = Some((target_size.0.max(1), target_size.1.max(1)));
let output = self.shared.output.lock().unwrap().clone();
let view = output.create_view(&Default::default());
let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
diff --git a/crates/fifoplayer/src/debug/ui.rs b/crates/fifoplayer/src/debug/ui.rs
index 21dbf03..33bb980 100644
--- a/crates/fifoplayer/src/debug/ui.rs
+++ b/crates/fifoplayer/src/debug/ui.rs
@@ -133,6 +133,7 @@ pub fn run_headless<const SYSTEM: SystemId>(
start: usize,
end: usize,
out: &std::path::Path,
+ aspect: backend_wgpu::sink::TargetAspect,
) {
let (_instance, _adapter, device, queue) = crate::init_wgpu();
@@ -154,7 +155,9 @@ pub fn run_headless<const SYSTEM: SystemId>(
});
let g = gx.lock().unwrap();
- let captured = backend_wgpu::capture::capture_texture(&device, &queue, &g.xfb_texture).expect("XFB capture failed");
+ let captured = backend_wgpu::capture::capture_texture(&device, &queue, &g.xfb_texture)
+ .expect("XFB capture failed")
+ .with_aspect(aspect);
backend_wgpu::capture::write_png(out, captured, true).expect("failed to write PNG");
eprintln!("wrote {}", out.display());
}
diff --git a/crates/fifoplayer/src/main.rs b/crates/fifoplayer/src/main.rs
index 9ee81ac..8ffa200 100644
--- a/crates/fifoplayer/src/main.rs
+++ b/crates/fifoplayer/src/main.rs
@@ -103,17 +103,18 @@ fn run<const SYSTEM: gecko::SystemId>(file: dff::DffFile, args: Args) {
let last = file.frames.len() - 1;
let start = args.start.min(last);
let end = args.end.unwrap_or(last).clamp(start, last);
+ let aspect = TargetAspect::from_arg(&args.aspect, file.is_wii);
if args.debug {
if let Some(ref out) = args.screenshot {
- debug::ui::run_headless::<SYSTEM>(file, args.file, start, end, out);
+ debug::ui::run_headless::<SYSTEM>(file, args.file, start, end, out, aspect);
} else {
debug::ui::run_debug::<SYSTEM>(file, args.file, start, end);
}
} else if let Some(ref out) = args.screenshot {
- self::run_headless::<SYSTEM>(&file, start, end, out, args.dump_textures.as_deref());
+ self::run_headless::<SYSTEM>(&file, start, end, out, args.dump_textures.as_deref(), aspect);
} else {
- self::run_windowed::<SYSTEM>(file, start, end, args.once, &args.aspect);
+ self::run_windowed::<SYSTEM>(file, start, end, args.once, aspect);
}
}
@@ -142,6 +143,7 @@ fn run_headless<const SYSTEM: gecko::SystemId>(
end: usize,
out: &PathBuf,
dump_textures: Option<&std::path::Path>,
+ aspect: TargetAspect,
) {
let (_instance, _adapter, device, queue) = self::init_wgpu();
@@ -172,7 +174,9 @@ fn run_headless<const SYSTEM: gecko::SystemId>(
});
let g = gx.lock().unwrap();
- let captured = backend_wgpu::capture::capture_texture(&device, &queue, &g.xfb_texture).expect("XFB capture failed");
+ let captured = backend_wgpu::capture::capture_texture(&device, &queue, &g.xfb_texture)
+ .expect("XFB capture failed")
+ .with_aspect(aspect);
backend_wgpu::capture::write_png(out, captured, true).expect("failed to write PNG");
eprintln!("wrote {}", out.display());
}
@@ -306,11 +310,16 @@ impl<const SYSTEM: gecko::SystemId> ApplicationHandler for WindowedApp<SYSTEM> {
}
}
-fn run_windowed<const SYSTEM: gecko::SystemId>(file: dff::DffFile, start: usize, end: usize, once: bool, aspect: &str) {
+fn run_windowed<const SYSTEM: gecko::SystemId>(
+ file: dff::DffFile,
+ start: usize,
+ end: usize,
+ once: bool,
+ target_aspect: TargetAspect,
+) {
let (instance, adapter, device, queue) = self::init_wgpu();
let surface_format = wgpu::TextureFormat::Bgra8Unorm;
- let target_aspect = TargetAspect::from_arg(aspect, file.is_wii);
let (renderer, sink) =
backend_wgpu::sink::Renderer::new(device.clone(), queue.clone(), surface_format, target_aspect, 1);
diff --git a/crates/mcp/src/server.rs b/crates/mcp/src/server.rs
index 59904a7..5e64e65 100644
--- a/crates/mcp/src/server.rs
+++ b/crates/mcp/src/server.rs
@@ -454,6 +454,7 @@ impl McpServer {
#[tool(description = "Capture the current frame from the emulator's XFB and return it as a base64 PNG.")]
async fn get_view(&self) -> Result<CallToolResult, McpError> {
require_loaded(&self.shared)?;
+ let is_wii = matches!(self.shared.state.lock().unwrap().backend, Some(Backend::Wii(_)));
let _ = self.shared.device.poll(wgpu::PollType::Wait {
submission_index: None,
timeout: None,
@@ -462,6 +463,7 @@ impl McpServer {
let gx = self.shared.gx.lock().unwrap();
backend_wgpu::capture::capture_texture(&self.shared.device, &self.shared.queue, &gx.xfb_texture)
.ok_or_else(|| McpError::internal_error("capture_texture returned None", None))?
+ .with_aspect(backend_wgpu::sink::TargetAspect::auto(is_wii))
};
let mut rgba = captured.rgba;
for px in rgba.chunks_exact_mut(4) {
diff --git a/crates/screenshotter/src/bin/screenshotter-worker.rs b/crates/screenshotter/src/bin/screenshotter-worker.rs
index 65387a5..aaf0d98 100644
--- a/crates/screenshotter/src/bin/screenshotter-worker.rs
+++ b/crates/screenshotter/src/bin/screenshotter-worker.rs
@@ -1,4 +1,4 @@
-use backend_wgpu::sink::InlineSink;
+use backend_wgpu::sink::{InlineSink, TargetAspect};
use backend_wgpu::{GxRenderer, capture};
use gecko::HostInput;
use gecko::flipper::si::pad;
@@ -14,29 +14,18 @@ const IPL: &[u8] = include_bytes!("../../../../private/IPL.decoded.bin");
const DSP: &[u8] = include_bytes!("../../../../private/dsp_rom.bin");
const COEF: &[u8] = include_bytes!("../../../../private/dsp_coef.bin");
-fn take_screenshot(device: &wgpu::Device, queue: &wgpu::Queue, gx: &GxRenderer, code: &str, frame: u32) {
+fn take_screenshot(device: &wgpu::Device, queue: &wgpu::Queue, gx: &GxRenderer, code: &str, frame: u32, is_wii: bool) {
let _ = device.poll(wgpu::PollType::Wait {
submission_index: None,
timeout: None,
});
- let mut captured = capture::capture_texture(device, queue, &gx.xfb_texture).expect("capture_texture returned None");
-
- for px in captured.rgba.chunks_exact_mut(4) {
- px[3] = 255;
- }
+ let captured = capture::capture_texture(device, queue, &gx.xfb_texture)
+ .expect("capture_texture returned None")
+ .with_aspect(TargetAspect::auto(is_wii));
let path = format!("screenshotdb/{}/{}.png", code, frame);
- let file = std::fs::File::create(&path).expect("Failed to create PNG file");
-
- let mut encoder = png::Encoder::new(std::io::BufWriter::new(file), captured.width, captured.height);
- encoder.set_color(png::ColorType::Rgba);
- encoder.set_depth(png::BitDepth::Eight);
-
- let mut writer = encoder.write_header().expect("Failed to write PNG header");
- writer
- .write_image_data(&captured.rgba)
- .expect("Failed to write PNG data");
+ capture::write_png(std::path::Path::new(&path), captured, true).expect("Failed to write PNG");
}
fn main() {
@@ -151,7 +140,7 @@ fn drive<const SYSTEM: SystemId>(
let mut frame: u32 = framerate * 2;
{
let g = gx.lock().unwrap();
- take_screenshot(device, queue, &g, code, frame);
+ take_screenshot(device, queue, &g, code, frame, SYSTEM == gecko::system::WII);
}
for idx in 0..20 {
@@ -164,7 +153,7 @@ fn drive<const SYSTEM: SystemId>(
}
let g = gx.lock().unwrap();
- take_screenshot(device, queue, &g, code, frame);
+ take_screenshot(device, queue, &g, code, frame, SYSTEM == gecko::system::WII);
}
}
diff --git a/images/ffcccb.png b/images/ffcccb.png
index a01c6d7..0c266b7 100644
Binary files a/images/ffcccb.png and b/images/ffcccb.png differ
diff --git a/images/galaxy.png b/images/galaxy.png
index 0a13126..bb372f4 100644
Binary files a/images/galaxy.png and b/images/galaxy.png differ