Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

wario world made me add this

Layle91c35b3c817b
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index fb0f368..09c37c3 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -358,6 +358,11 @@ impl GxRenderer {
self.drain_pending_writebacks(device, queue);
}
+ GxAction::FlushEfbWritebacks => {
+ self.flush_pending_draws(device, queue);
+ self.drain_pending_writebacks(device, queue);
+ }
+
GxAction::CopyEfbToTexture {
dest_addr,
src_x,
diff --git a/crates/backend-wgpu/src/sink.rs b/crates/backend-wgpu/src/sink.rs
index 7faeff3..f11ecd6 100644
--- a/crates/backend-wgpu/src/sink.rs
+++ b/crates/backend-wgpu/src/sink.rs
@@ -498,7 +498,10 @@ impl RendererWorker {
impl RenderSink for BatchingSink {
fn exec(&mut self, action: GxAction) {
- let force_flush = matches!(action, GxAction::PresentXfb { .. } | GxAction::CopyEfbToTexture { .. });
+ let force_flush = matches!(
+ action,
+ GxAction::PresentXfb { .. } | GxAction::CopyEfbToTexture { .. } | GxAction::FlushEfbWritebacks
+ );
self.batch.push(action);
diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rs
index 895a3f7..a1accba 100644
--- a/crates/gecko/src/flipper/gx.rs
+++ b/crates/gecko/src/flipper/gx.rs
@@ -98,6 +98,7 @@ pub struct GraphicsProcessor {
// Receiver for encoded EFB-to-texture bytes coming back from the
// renderer worker.
pub efb_writeback_rx: Option<crossbeam_channel::Receiver<EfbWriteback>>,
+ pub pending_efb_writebacks: u32,
pub draw_box_pool: Vec<Box<DrawData>>,
pub draw_box_recycle_rx: Option<crossbeam_channel::Receiver<Box<DrawData>>>,
}
@@ -183,6 +184,7 @@ impl GraphicsProcessor {
stats: GxStats::default(),
texture_hashes: FxHashMap::default(),
efb_writeback_rx: None,
+ pending_efb_writebacks: 0,
draw_box_pool: {
const POOL_PREALLOC: usize = 1024;
let mut v: Vec<Box<crate::host::DrawData>> = Vec::with_capacity(POOL_PREALLOC);
diff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rs
index ddc7503..8325406 100644
--- a/crates/gecko/src/flipper/gx/bp.rs
+++ b/crates/gecko/src/flipper/gx/bp.rs
@@ -1,9 +1,25 @@
use super::constants::*;
use super::regs::*;
use super::{GraphicsProcessor, draw, texture};
-use crate::host::{GxAction, RenderSink, TextureKey};
+use crate::host::{EfbWriteback, GxAction, RenderSink, TextureKey};
use crate::mmio::{RamView, RamViewMut};
+const EFB_WRITEBACK_WAIT_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(500);
+
+fn efb_copy_will_writeback(src_x: u32, src_y: u32, src_w: u32, src_h: u32, copy_format: u8, depth: bool) -> bool {
+ let w = src_w.min(EFB_WIDTH.saturating_sub(src_x));
+ let h = src_h.min(EFB_HEIGHT.saturating_sub(src_y));
+ if w == 0 || h == 0 {
+ return false;
+ }
+
+ if depth {
+ texture::CopyFormat::from_u8_depth(copy_format).is_some()
+ } else {
+ texture::CopyFormat::from_u8_color(copy_format).is_some()
+ }
+}
+
impl GraphicsProcessor {
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn load_bp(&mut self, renderer: &mut dyn RenderSink, ram: &mut RamViewMut<'_>, data: &[u8]) {
@@ -186,14 +202,14 @@ impl GraphicsProcessor {
// PE finish
if idx == BP_PE_DONE && (val & BP_PE_DONE_FINISH_BIT) != 0 {
self.raise_interrupt = true;
- self.drain_efb_writebacks(ram);
+ self.wait_for_efb_writebacks(renderer, ram);
}
// PE token (0x47): store token value only
if idx == BP_PE_TOKEN {
self.pending_token = (val & 0xFFFF) as u16;
self.token_dirty = true;
- self.drain_efb_writebacks(ram);
+ self.wait_for_efb_writebacks(renderer, ram);
}
// PE token interrupt (0x48): store token value + raise interrupt
@@ -202,7 +218,7 @@ impl GraphicsProcessor {
self.pending_token = (val & 0xFFFF) as u16;
self.token_dirty = true;
self.raise_token_interrupt = true;
- self.drain_efb_writebacks(ram);
+ self.wait_for_efb_writebacks(renderer, ram);
}
// Scissor registers (BP 0x20 = TL, 0x21 = BR, 0x59 = OFFSET).
@@ -512,6 +528,9 @@ impl GraphicsProcessor {
let z_update = self.cur_zmode.update_enable();
let depth_copy = self.cur_pe_control.pixel_format().is_depth_only();
+ if efb_copy_will_writeback(src_x, src_y, src_w, src_h, copy_format, depth_copy) {
+ self.pending_efb_writebacks += 1;
+ }
renderer.exec(GxAction::CopyEfbToTexture {
dest_addr,
src_x,
@@ -546,19 +565,52 @@ impl GraphicsProcessor {
let Some(rx) = self.efb_writeback_rx.as_ref() else {
return;
};
-
while let Ok(wb) = rx.try_recv() {
- texture::write_strided_copy_to_ram(
- ram,
- wb.dest_addr,
- &wb.bytes,
- wb.row_bytes,
- wb.row_count,
- wb.dest_stride_bytes,
- );
+ Self::apply_efb_writeback(&mut self.pending_efb_writebacks, ram, wb);
+ }
+ }
+
+ #[cfg_attr(feature = "hotpath", hotpath::measure)]
+ pub fn wait_for_efb_writebacks(&mut self, renderer: &mut dyn RenderSink, ram: &mut RamViewMut<'_>) {
+ if self.pending_efb_writebacks == 0 {
+ return;
+ }
+ renderer.exec(GxAction::FlushEfbWritebacks);
+
+ let Some(rx) = self.efb_writeback_rx.as_ref() else {
+ self.pending_efb_writebacks = 0;
+ return;
+ };
+
+ while self.pending_efb_writebacks > 0 {
+ match rx.recv_timeout(EFB_WRITEBACK_WAIT_TIMEOUT) {
+ Ok(wb) => Self::apply_efb_writeback(&mut self.pending_efb_writebacks, ram, wb),
+ Err(err) => {
+ tracing::warn!(
+ ?err,
+ outstanding = self.pending_efb_writebacks,
+ "efb writeback wait timed out"
+ );
+ self.pending_efb_writebacks = 0;
+ return;
+ }
+ }
}
}
+ #[inline]
+ fn apply_efb_writeback(pending: &mut u32, ram: &mut RamViewMut<'_>, wb: EfbWriteback) {
+ texture::write_strided_copy_to_ram(
+ ram,
+ wb.dest_addr,
+ &wb.bytes,
+ wb.row_bytes,
+ wb.row_count,
+ wb.dest_stride_bytes,
+ );
+ *pending = pending.saturating_sub(1);
+ }
+
fn recompute_scissor(&mut self) {
let tl = SuScisRect::from_raw(self.bp_regs[BP_SU_SCIS_TL]);
let br = SuScisRect::from_raw(self.bp_regs[BP_SU_SCIS_BR]);
diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rs
index 56004fc..df6a65c 100644
--- a/crates/gecko/src/host.rs
+++ b/crates/gecko/src/host.rs
@@ -98,6 +98,9 @@ pub enum GxAction {
alpha_supported: bool,
},
+ /// I onyl really added this cause of fuckass GC games acting up (Wario World).
+ FlushEfbWritebacks,
+
/// Composite all XFB copies from this frame into the output framebuffer.
/// Emitted once per vblank by `present_xfb()`. Each [`XfbPart`]
/// identifies a copy by `id` and places it at `(offset_x, offset_y)`.