A GameCube/Wii emulator and debugger written in Rust.
xfb fallback (untested)
Layle3395a6616135
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rsindex a2b182c..f9f4467 100644--- a/crates/backend-wgpu/src/action.rs+++ b/crates/backend-wgpu/src/action.rs@@ -449,6 +449,11 @@ impl GxRenderer {self.execute_present_xfb(device, queue, *width, *height, parts);}+ GxAction::PresentRawXfb { width, height, pixels } => {+ self.flush_pending_draws(device, queue);+ self.execute_present_raw_xfb(device, queue, *width, *height, pixels);+ }+GxAction::CopyEfbToTexture {dest_addr,src_x,diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rsindex 8e64416..6a08619 100644--- a/crates/backend-wgpu/src/render.rs+++ b/crates/backend-wgpu/src/render.rs@@ -478,6 +478,33 @@ impl GxRenderer {}}+ fn ensure_xfb_texture(&mut self, device: &wgpu::Device, width: u32, height: u32, label_prefix: &str) {+ let cur = self.xfb_texture.size();+ if cur.width == width && cur.height == height {+ return;+ }++ let texture_label = format!("{label_prefix} size={width}x{height}");+ self.xfb_texture = device.create_texture(&wgpu::TextureDescriptor {+ label: Some(&texture_label),+ size: wgpu::Extent3d {+ width,+ height,+ depth_or_array_layers: 1,+ },+ mip_level_count: 1,+ sample_count: 1,+ dimension: wgpu::TextureDimension::D2,+ format: self.surface_format,+ usage: wgpu::TextureUsages::COPY_DST+ | wgpu::TextureUsages::COPY_SRC+ | wgpu::TextureUsages::TEXTURE_BINDING+ | wgpu::TextureUsages::RENDER_ATTACHMENT,+ view_formats: &[],+ });+ self.xfb_view = self.xfb_texture.create_view(&Default::default());+ }+pub(crate) fn execute_present_xfb(&mut self,device: &wgpu::Device,@@ -489,29 +516,7 @@ impl GxRenderer {let width = self.scaled(width.max(1));let height = self.scaled(height.max(1));- // Resize the XFB output texture if the frame dimensions changed.- let cur = self.xfb_texture.size();- if cur.width != width || cur.height != height {- let texture_label = format!("xfb_accum size={width}x{height}");- self.xfb_texture = device.create_texture(&wgpu::TextureDescriptor {- label: Some(&texture_label),- size: wgpu::Extent3d {- width,- height,- depth_or_array_layers: 1,- },- mip_level_count: 1,- sample_count: 1,- dimension: wgpu::TextureDimension::D2,- format: self.surface_format,- usage: wgpu::TextureUsages::COPY_DST- | wgpu::TextureUsages::COPY_SRC- | wgpu::TextureUsages::TEXTURE_BINDING- | wgpu::TextureUsages::RENDER_ATTACHMENT,- view_formats: &[],- });- self.xfb_view = self.xfb_texture.create_view(&Default::default());- }+ self.ensure_xfb_texture(device, width, height, "xfb_accum");let group_label = format!("PresentXfb size={width}x{height} parts={}", parts.len());let mut encoder = self.take_or_create_encoder(device);@@ -583,6 +588,49 @@ impl GxRenderer {self.xfb_has_content = true;}+ pub(crate) fn execute_present_raw_xfb(+ &mut self,+ device: &wgpu::Device,+ queue: &wgpu::Queue,+ width: u32,+ height: u32,+ pixels: &[u32],+ ) {+ let width = width.max(1);+ let height = height.max(1);++ if (pixels.len() as u64) < width as u64 * height as u64 {+ tracing::warn!(+ width,+ height,+ len = pixels.len(),+ "present_raw_xfb: pixel buffer too small, skipping"+ );+ return;+ }++ // The raw path never upscales: these pixels are the final image.+ self.ensure_xfb_texture(device, width, height, "xfb_raw");++ queue.write_texture(+ self.xfb_texture.as_image_copy(),+ bytemuck::cast_slice(&pixels[..(width as usize * height as usize)]),+ wgpu::TexelCopyBufferLayout {+ offset: 0,+ bytes_per_row: Some(width * 4),+ rows_per_image: Some(height),+ },+ wgpu::Extent3d {+ width,+ height,+ depth_or_array_layers: 1,+ },+ );++ self.submit_pending(queue);+ self.xfb_has_content = true;+ }+/// Queue an EFB region readback into `pending_writebacks`. The actual/// map+encode+ship happens at the next frame boundary via/// `drain_pending_writebacks`.diff --git a/crates/backend-wgpu/src/sink.rs b/crates/backend-wgpu/src/sink.rsindex d0d07e6..a649d0f 100644--- a/crates/backend-wgpu/src/sink.rs+++ b/crates/backend-wgpu/src/sink.rs@@ -129,7 +129,7 @@ impl RenderWorker {self.gx.process_action(&self.device, &self.queue, &message.action);match message.action {- GxAction::PresentXfb { .. } => {+ GxAction::PresentXfb { .. } | GxAction::PresentRawXfb { .. } => {#[cfg(feature = "renderdoc-capture")]if let Ok(mut rd) = self.renderdoc.lock() {rd.end_emulated_frame();@@ -303,6 +303,7 @@ pub fn action_resets_vertex_scratch(action: &GxAction) -> bool {GxAction::InvalidateCaches| GxAction::CopyXfb { .. }| GxAction::PresentXfb { .. }+ | GxAction::PresentRawXfb { .. }| GxAction::CopyEfbToTexture { .. } => true,#[cfg(not(target_arch = "wasm32"))]GxAction::DumpTextures { .. } => true,diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rsindex 996e85a..8c2e1b3 100644--- a/crates/gecko/src/flipper/gx.rs+++ b/crates/gecko/src/flipper/gx.rs@@ -242,6 +242,12 @@ pub fn present_xfb<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {let (frame_w, frame_h) = sys.vi.frame_dimensions();+ if sys.gx.xfb_copy_seq == 0 {+ let base = sys.vi.latched_xfb_base;+ self::present_raw_xfb(sys, base, frame_w, frame_h);+ return;+ }+let Some(bytes_per_row) = sys.gx.xfb_regions@@ -458,6 +464,34 @@ pub fn present_xfb<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>) {sys.gx.xfb_last_present_base = frame_base;}+fn present_raw_xfb<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, base: u32, width: u32, height: u32) {+ if base == 0 || width == 0 || height == 0 {+ return;+ }++ let pixel_count = (width as usize) * (height as usize);+ let mut pixels = vec![0u32; pixel_count];++ let to_bgra = |y: f32, cb: f32, cr: f32| -> u32 {+ let r = (1.164 * y + 1.596 * cr).clamp(0.0, 255.0) as u32;+ let g = (1.164 * y - 0.813 * cr - 0.391 * cb).clamp(0.0, 255.0) as u32;+ let b = (1.164 * y + 2.018 * cb).clamp(0.0, 255.0) as u32;+ 0xFF00_0000 | (r << 16) | (g << 8) | b+ };++ for i in 0..pixel_count / 2 {+ let word = sys.mmio.phys_read_u32(base + (i as u32) * 4);+ let y0 = ((word >> 24) & 0xFF) as f32 - 16.0;+ let cb = ((word >> 16) & 0xFF) as f32 - 128.0;+ let y1 = ((word >> 8) & 0xFF) as f32 - 16.0;+ let cr = (word & 0xFF) as f32 - 128.0;+ pixels[i * 2] = to_bgra(y0, cb, cr);+ pixels[i * 2 + 1] = to_bgra(y1, cb, cr);+ }++ sys.render_sink.exec(GxAction::PresentRawXfb { width, height, pixels });+}+impl<const SYSTEM: SystemId> System<SYSTEM> {/// Check if the GX stub detected a finish or token command and signal PEpub fn check_gx_pe_interrupts(&mut self) {diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rsindex 9b08dcc..90ce0de 100644--- a/crates/gecko/src/host.rs+++ b/crates/gecko/src/host.rs@@ -106,6 +106,14 @@ pub enum GxAction {parts: Vec<XfbPart>,},+ /// Present a CPU-drawn external framebuffer read straight from guest RAM+ /// (no GX EFB copy happened, e.g. consoletest).+ PresentRawXfb {+ width: u32,+ height: u32,+ pixels: Vec<u32>,+ },+/// Copy an EFB region back into system RAM, encoded in a GX texture/// format. The renderer does a GPU readback, converts the pixels to/// `copy_format`, and ships the encoded bytes back over the writebackdiff --git a/crates/mcp/src/sink.rs b/crates/mcp/src/sink.rsindex be52188..519f98c 100644--- a/crates/mcp/src/sink.rs+++ b/crates/mcp/src/sink.rs@@ -62,7 +62,7 @@ impl RenderSink for McpSink {i.bound[*slot] = Some(*id);}}- GxAction::PresentXfb { width, height, .. } => {+ GxAction::PresentXfb { width, height, .. } | GxAction::PresentRawXfb { width, height, .. } => {let mut i = self.introspect.lock().unwrap();i.last_xfb_size = (*width, *height);i.frame_index = i.frame_index.wrapping_add(1);