Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

modelview matrix used wrong XF slot (always PNMTX 0)

Layledbce9af99da5
diff --git a/crates/gekko/src/flipper/gx.rs b/crates/gekko/src/flipper/gx.rs
index 49f5b73..baf5bee 100644
--- a/crates/gekko/src/flipper/gx.rs
+++ b/crates/gekko/src/flipper/gx.rs
@@ -10,13 +10,13 @@ use crate::{
ARRAY_BASE_REG, ARRAY_CLR0, ARRAY_POS, ARRAY_STRIDE_REG, BP_GEN_MODE, BP_PE_ALPHA_COMPARE, BP_PE_CMODE0,
BP_PE_DONE, BP_PE_DONE_FINISH_BIT, BP_PE_ZMODE, BP_RAS1_TREF_COUNT, BP_RAS1_TREF0, BP_REG_SIZE,
BP_TEV_COLOR_ENV_0, BP_TEV_REGISTERL_0, BP_TX_SETIMAGE0_I0, BP_TX_SETIMAGE0_I4, BP_TX_SETIMAGE3_I0,
- BP_TX_SETIMAGE3_I4, CP_REG_SIZE, VATA_REG, VCD_HI_REG, VCD_LO_REG, XF_MEM_SIZE, XF_MODELVIEW_BASE,
- XF_MODELVIEW_END, XF_PROJECTION_BASE, XF_PROJECTION_END,
+ BP_TX_SETIMAGE3_I4, CP_REG_SIZE, VATA_REG, VCD_HI_REG, VCD_LO_REG, XF_MATRIX_INDEX_A, XF_MEM_SIZE,
+ XF_POS_MTX_STRIDE, XF_PROJECTION_BASE, XF_PROJECTION_END,
},
draw::DrawCommands,
regs::{
- AlphaCompare, BlendMode, GenMode, TevAlphaEnv, TevColorEnv, TevRegType, TevRegisterH, TevRegisterL,
- TxSetImage0, TxSetImage3, VatA, VcdHi, VcdLo, ZMode,
+ AlphaCompare, BlendMode, GenMode, MatrixIndex0, TevAlphaEnv, TevColorEnv, TevRegType, TevRegisterH,
+ TevRegisterL, TxSetImage0, TxSetImage3, VatA, VcdHi, VcdLo, ZMode,
},
},
gekko::Gekko,
@@ -165,17 +165,49 @@ impl Gx {
}
if let Some(primitive) = draw::Primitive::from_cmd(cmd) {
+ // Read the current position matrix index from MatrixIndex0[0:5]
+ // and compute the modelview from the correct XF memory slot
+ let mtx_index_a = MatrixIndex0::from_raw(self.xf_mem[XF_MATRIX_INDEX_A]);
+ let pos_mtx_base = mtx_index_a.pos_mtx_idx() as usize * XF_POS_MTX_STRIDE;
+ let modelview = draw::Matrix4([
+ [
+ self.xf_f32(pos_mtx_base),
+ self.xf_f32(pos_mtx_base + 4),
+ self.xf_f32(pos_mtx_base + 8),
+ 0.0,
+ ],
+ [
+ self.xf_f32(pos_mtx_base + 1),
+ self.xf_f32(pos_mtx_base + 5),
+ self.xf_f32(pos_mtx_base + 9),
+ 0.0,
+ ],
+ [
+ self.xf_f32(pos_mtx_base + 2),
+ self.xf_f32(pos_mtx_base + 6),
+ self.xf_f32(pos_mtx_base + 10),
+ 0.0,
+ ],
+ [
+ self.xf_f32(pos_mtx_base + 3),
+ self.xf_f32(pos_mtx_base + 7),
+ self.xf_f32(pos_mtx_base + 11),
+ 1.0,
+ ],
+ ]);
+
tracing::debug!(
primitive = format!("{:?}", primitive),
vertices = format!("{:?}", vertices),
- modelview = format!("{:?}", self.draw_commands.modelview),
+ pos_mtx_idx = mtx_index_a.pos_mtx_idx(),
+ modelview = format!("{:?}", modelview),
projection = format!("{:?}", self.draw_commands.projection),
"draw call created"
);
self.draw_commands.commands.push(draw::DrawCall {
primitive,
vertices,
- modelview: self.draw_commands.modelview,
+ modelview,
});
}
}
@@ -277,16 +309,6 @@ impl Gx {
f32::from_bits(self.xf_mem[reg])
}
- fn rebuild_modelview(&mut self) {
- let b = XF_MODELVIEW_BASE;
- self.draw_commands.modelview = draw::Matrix4([
- [self.xf_f32(b), self.xf_f32(b + 4), self.xf_f32(b + 8), 0.0],
- [self.xf_f32(b + 1), self.xf_f32(b + 5), self.xf_f32(b + 9), 0.0],
- [self.xf_f32(b + 2), self.xf_f32(b + 6), self.xf_f32(b + 10), 0.0],
- [self.xf_f32(b + 3), self.xf_f32(b + 7), self.xf_f32(b + 11), 1.0],
- ]);
- }
-
fn rebuild_projection(&mut self) {
let b = XF_PROJECTION_BASE;
let pm1 = self.xf_f32(b);
@@ -497,10 +519,8 @@ impl Gx {
);
}
- // Rebuild matrices if the write touched their address ranges
- if addr <= XF_MODELVIEW_END && end > XF_MODELVIEW_BASE {
- self.rebuild_modelview();
- }
+ // Rebuild projection if the write touched its address range
+ // (modelview is resolved lazily at draw call time from the current position matrix slot)
if addr <= XF_PROJECTION_END && end > XF_PROJECTION_BASE {
self.rebuild_projection();
}
diff --git a/crates/gekko/src/flipper/gx/constants.rs b/crates/gekko/src/flipper/gx/constants.rs
index 743ba7b..288cd12 100644
--- a/crates/gekko/src/flipper/gx/constants.rs
+++ b/crates/gekko/src/flipper/gx/constants.rs
@@ -33,6 +33,8 @@ pub const XF_MODELVIEW_BASE: usize = 0x0000;
pub const XF_MODELVIEW_END: usize = 0x000B;
pub const XF_PROJECTION_BASE: usize = 0x1020;
pub const XF_PROJECTION_END: usize = 0x1026;
+pub const XF_MATRIX_INDEX_A: usize = 0x1018;
+pub const XF_POS_MTX_STRIDE: usize = 4;
// BP texture register base addresses (maps 0-3: base, maps 4-7: base + 0x20)
pub const BP_TX_SETMODE0_I0: usize = 0x80; // TX_SETMODE0 maps 0-3
diff --git a/crates/gekko/src/flipper/gx/draw.rs b/crates/gekko/src/flipper/gx/draw.rs
index 4366279..8ad130f 100644
--- a/crates/gekko/src/flipper/gx/draw.rs
+++ b/crates/gekko/src/flipper/gx/draw.rs
@@ -95,7 +95,6 @@ impl std::ops::Mul for Matrix4 {
#[derive(Default)]
pub struct DrawCommands {
- pub modelview: Matrix4,
pub projection: Matrix4,
pub commands: Vec<DrawCall>,
pub textures: [Option<TextureDescriptor>; 8],
diff --git a/crates/gekko/src/flipper/gx/regs.rs b/crates/gekko/src/flipper/gx/regs.rs
index dec84f5..c44aba5 100644
--- a/crates/gekko/src/flipper/gx/regs.rs
+++ b/crates/gekko/src/flipper/gx/regs.rs
@@ -310,7 +310,6 @@ impl TxSetImage3 {
}
}
-
// TEV color combiner input select (SELA-SELD)
#[derive(Debug, PartialEq, BitEnum)]
pub enum TevColorIn {
@@ -474,4 +473,12 @@ pub struct TevRegisterH {
pub struct GenMode {
#[bits(10..=13)]
pub num_tev_stages: u8, // num stages - 1
-}
\ No newline at end of file
+}
+
+// XF 0x1018 Matrix Index A (position/tex0-3 matrix indices)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct MatrixIndex0 {
+ #[bits(0..=5)]
+ pub pos_mtx_idx: u8,
+}
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index f537a54..1f9852d 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -477,8 +477,7 @@ fn main() {
.any(|arg| arg == "--immediate")
.then(|| wgpu::PresentMode::Immediate)
.unwrap_or(wgpu::PresentMode::Fifo);
- let idle_skip = std::env::args()
- .any(|arg| arg == "--idle-skip");
+ let idle_skip = std::env::args().any(|arg| arg == "--idle-skip");
let rom_data = std::fs::read(&args[1]).expect("failed to read ROM");
let dol = Dol::parse(rom_data);