Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

parsing matrices

Layled692d1649468
diff --git a/crates/gekko/src/flipper/gx.rs b/crates/gekko/src/flipper/gx.rs
index 4c626f6..08006ef 100644
--- a/crates/gekko/src/flipper/gx.rs
+++ b/crates/gekko/src/flipper/gx.rs
@@ -7,10 +7,11 @@ use super::pi::InterruptFlag;
use crate::{
flipper::gx::{
constants::{
- ARRAY_BASE_REG, ARRAY_CLR0, ARRAY_POS, ARRAY_STRIDE_REG, BP_REG_SIZE, CP_REG_SIZE, VATA_REG, VCD_LO_REG,
- XF_MEM_SIZE,
+ ARRAY_BASE_REG, ARRAY_CLR0, ARRAY_POS, ARRAY_STRIDE_REG, BP_REG_SIZE, CP_REG_SIZE,
+ VATA_REG, VCD_LO_REG, XF_MEM_SIZE, XF_MODELVIEW_BASE, XF_MODELVIEW_END,
+ XF_PROJECTION_BASE, XF_PROJECTION_END,
},
- draw::DrawCall,
+ draw::DrawCommands,
regs::{VatA, VcdLo},
},
gekko::Gekko,
@@ -20,7 +21,7 @@ use fifo::FifoCmd;
pub struct Gx {
pub raise_interrupt: bool,
- pub draw_commands: Vec<DrawCall>,
+ pub draw_commands: DrawCommands,
bp_regs: Vec<u32>,
cp_regs: Vec<u32>,
xf_mem: Vec<u32>,
@@ -35,7 +36,7 @@ impl Gx {
cp_regs: vec![0; CP_REG_SIZE],
xf_mem: vec![0; XF_MEM_SIZE],
fifo: Vec::with_capacity(256),
- draw_commands: Vec::new(),
+ draw_commands: DrawCommands::default(),
}
}
@@ -117,9 +118,11 @@ impl Gx {
tracing::debug!(
primitive = format!("{:?}", primitive),
vertices = format!("{:?}", vertices),
+ modelview = format!("{:?}", self.draw_commands.modelview),
+ projection = format!("{:?}", self.draw_commands.projection),
"draw call created"
);
- self.draw_commands.push(draw::DrawCall { primitive, vertices });
+ self.draw_commands.commands.push(draw::DrawCall { primitive, vertices });
}
}
}
@@ -216,6 +219,49 @@ impl Gx {
}
}
+ fn xf_f32(&self, reg: usize) -> f32 {
+ f32::from_bits(self.xf_mem[reg])
+ }
+
+ fn rebuild_modelview(&mut self) {
+ let b = XF_MODELVIEW_BASE;
+ self.draw_commands.modelview = [
+ [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);
+ let pm2 = self.xf_f32(b + 1);
+ let pm3 = self.xf_f32(b + 2);
+ let pm4 = self.xf_f32(b + 3);
+ let pm5 = self.xf_f32(b + 4);
+ let pm6 = self.xf_f32(b + 5);
+ let proj_type = self.xf_mem[XF_PROJECTION_END];
+
+ self.draw_commands.projection = if proj_type == 0 {
+ // Perspective
+ [
+ [pm1, 0.0, 0.0, 0.0],
+ [0.0, pm3, 0.0, 0.0],
+ [pm2, pm4, pm5, -1.0],
+ [0.0, 0.0, pm6, 0.0],
+ ]
+ } else {
+ // Orthographic
+ [
+ [pm1, 0.0, 0.0, 0.0],
+ [0.0, pm3, 0.0, 0.0],
+ [0.0, 0.0, pm5, 0.0],
+ [pm2, pm4, pm6, 1.0],
+ ]
+ };
+ }
+
fn load_bp(&mut self, data: &[u8]) {
let idx = data[0] as usize;
let val = u32::from_be_bytes([0, data[1], data[2], data[3]]);
@@ -249,6 +295,7 @@ impl Gx {
let length = u16::from_be_bytes([data[0], data[1]]) as usize;
let addr = u16::from_be_bytes([data[2], data[3]]) as usize;
let n = length + 1;
+ let end = addr + n;
for i in 0..n {
let offset = 4 + i * 4;
@@ -264,6 +311,14 @@ impl Gx {
"XF register write"
);
}
+
+ // Rebuild matrices if the write touched their address ranges
+ if addr <= XF_MODELVIEW_END && end > XF_MODELVIEW_BASE {
+ self.rebuild_modelview();
+ }
+ 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 1fc5f9c..8ef8a37 100644
--- a/crates/gekko/src/flipper/gx/constants.rs
+++ b/crates/gekko/src/flipper/gx/constants.rs
@@ -13,7 +13,13 @@ pub const VATA_REG: usize = 0x70;
pub const ARRAY_BASE_REG: usize = 0xA0;
pub const ARRAY_STRIDE_REG: usize = 0xB0;
-pub const ARRAY_POS: usize = 0;
-pub const ARRAY_NRM: usize = 1;
+pub const ARRAY_POS: usize = 0;
+pub const ARRAY_NRM: usize = 1;
pub const ARRAY_CLR0: usize = 2;
-pub const ARRAY_CLR1: usize = 3;
\ No newline at end of file
+pub const ARRAY_CLR1: usize = 3;
+
+// XF memory addresses
+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;
\ No newline at end of file
diff --git a/crates/gekko/src/flipper/gx/draw.rs b/crates/gekko/src/flipper/gx/draw.rs
index 3841674..032f56b 100644
--- a/crates/gekko/src/flipper/gx/draw.rs
+++ b/crates/gekko/src/flipper/gx/draw.rs
@@ -28,4 +28,11 @@ pub struct DrawCall {
pub vertices: Vec<Vertex>,
}
-pub type Matrix4 = [[f32; 4]; 4];
\ No newline at end of file
+pub type Matrix4 = [[f32; 4]; 4];
+
+#[derive(Default)]
+pub struct DrawCommands {
+ pub modelview: Matrix4, // where is this triangle relative to the camera?
+ pub projection: Matrix4, // how is the 3D scene projected onto the 2D screen?
+ pub commands: Vec<DrawCall>, // the actual draw calls to execute
+}
\ No newline at end of file