Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

implemented quads

Layle99f5bc7cb932
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index 4d00834..524414c 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -263,6 +263,40 @@ fn triangulate(draw_calls: &[DrawCall]) -> Vec<GpuVertex> {
});
}
}
+ Primitive::Quads => {
+ for quad in dc.vertices.chunks(4) {
+ if quad.len() < 4 {
+ println!("quad with less than 4 vertices");
+ continue;
+ }
+
+ out.push(GpuVertex {
+ position: quad[0].position,
+ color: quad[0].color0,
+ });
+ out.push(GpuVertex {
+ position: quad[1].position,
+ color: quad[1].color0,
+ });
+ out.push(GpuVertex {
+ position: quad[2].position,
+ color: quad[2].color0,
+ });
+ out.push(GpuVertex {
+ position: quad[0].position,
+ color: quad[0].color0,
+ });
+ out.push(GpuVertex {
+ position: quad[2].position,
+ color: quad[2].color0,
+ });
+ out.push(GpuVertex {
+ position: quad[3].position,
+ color: quad[3].color0,
+ });
+ }
+ }
+ _ => unimplemented!("triangulation for {:?}", dc.primitive),
}
}
out
diff --git a/crates/gekko/src/cpu/interpreter/fp_ops.rs b/crates/gekko/src/cpu/interpreter/fp_ops.rs
index 95bfa86..e9d1bda 100644
--- a/crates/gekko/src/cpu/interpreter/fp_ops.rs
+++ b/crates/gekko/src/cpu/interpreter/fp_ops.rs
@@ -166,8 +166,12 @@ pub fn fp_ops<const OP: u32>(ctx: &mut crate::gekko::Gekko, instr: crate::cpu::s
as f64,
),
crate::cpu::lut::OP_FSQRTX => fp_write(ctx, &instr, ctx.cpu.read_fpr(instr.rb()).sqrt()),
- crate::cpu::lut::OP_FSQRTSX => fp_write_single(ctx, &instr, (ctx.cpu.read_fpr(instr.rb()).sqrt()) as f32 as f64),
- crate::cpu::lut::OP_FRESX => fp_write_single(ctx, &instr, (1.0f32 / ctx.cpu.read_fpr(instr.rb()) as f32) as f64),
+ crate::cpu::lut::OP_FSQRTSX => {
+ fp_write_single(ctx, &instr, (ctx.cpu.read_fpr(instr.rb()).sqrt()) as f32 as f64)
+ }
+ crate::cpu::lut::OP_FRESX => {
+ fp_write_single(ctx, &instr, (1.0f32 / ctx.cpu.read_fpr(instr.rb()) as f32) as f64)
+ }
crate::cpu::lut::OP_FRSQRTEX => fp_write(ctx, &instr, 1.0 / ctx.cpu.read_fpr(instr.rb()).sqrt()),
crate::cpu::lut::OP_FSELX => {
let fa = ctx.cpu.read_fpr(instr.ra());
diff --git a/crates/gekko/src/flipper/gx.rs b/crates/gekko/src/flipper/gx.rs
index 2b70c31..18d633c 100644
--- a/crates/gekko/src/flipper/gx.rs
+++ b/crates/gekko/src/flipper/gx.rs
@@ -7,12 +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, XF_MODELVIEW_BASE, XF_MODELVIEW_END,
- XF_PROJECTION_BASE, XF_PROJECTION_END,
+ ARRAY_BASE_REG, ARRAY_CLR0, ARRAY_POS, ARRAY_STRIDE_REG, BP_REG_SIZE, 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,
},
draw::DrawCommands,
- regs::{VatA, VcdLo},
+ regs::{VatA, VcdHi, VcdLo},
},
gekko::Gekko,
mmio::Mmio,
@@ -67,9 +66,10 @@ impl Gx {
}
fn create_draw_call(&mut self, mmio: &mut Mmio, cmd: FifoCmd) {
- if let FifoCmd::DrawTriangles(cmd, data) = cmd {
+ if let FifoCmd::DrawCall(cmd, data) = cmd {
let fmt = (cmd & 0b111) as usize;
- let vcd_lo = VcdLo::from_raw(self.cp_regs[VCD_LO_REG]);
+ let vcd_lo = VcdLo::from_raw(self.cp_regs[VCD_LO_REG + fmt]);
+ let vcd_hi = VcdHi::from_raw(self.cp_regs[VCD_HI_REG + fmt]);
let vat_a = VatA::from_raw(self.cp_regs[VATA_REG + fmt]);
let pos_base = self.cp_regs[ARRAY_BASE_REG + ARRAY_POS] as usize;
@@ -77,28 +77,35 @@ impl Gx {
let clr0_base = self.cp_regs[ARRAY_BASE_REG + ARRAY_CLR0] as usize;
let clr0_stride = self.cp_regs[ARRAY_STRIDE_REG + ARRAY_CLR0] as usize;
- let vertex_stride = vcd_lo.position().size() + vcd_lo.color0().size();
+ let tex0_size = match vcd_hi.tex0() {
+ regs::AttributeType::Direct => vat_a.tex0_data_size(),
+ regs::AttributeType::Index8 => 1,
+ regs::AttributeType::Index16 => 2,
+ regs::AttributeType::None => 0,
+ };
+
+ let vertex_stride = vcd_lo.position().size() + vcd_lo.color0().size() + tex0_size;
let vertex_count = data.len() / vertex_stride;
let mut vertices: Vec<draw::Vertex> = Vec::with_capacity(vertex_count);
+ let mut cursor = 0;
for i in 0..vertex_count {
- let offset = i * vertex_stride;
- let mut cursor = offset;
-
- // Read position index and look up from RAM
- let pos_index = data[cursor] as usize;
- cursor += vcd_lo.position().size();
+ // Read position index
+ let pos_index = read_index(&data, &mut cursor, vcd_lo.position());
let pos_addr = pos_base + pos_index * pos_stride;
let pos_size = vat_a.pos_data_size();
let pos_data = &mmio.ram[pos_addr..pos_addr + pos_size];
- // Read color0 index and look up from RAM
- let clr0_index = data[cursor] as usize;
+ // Read color0 index
+ let clr0_index = read_index(&data, &mut cursor, vcd_lo.color0());
let clr0_addr = clr0_base + clr0_index * clr0_stride;
let clr0_size = vat_a.clr0_data_size();
let clr0_data = &mmio.ram[clr0_addr..clr0_addr + clr0_size];
+ // Skip tex0 bytes
+ cursor += tex0_size;
+
tracing::debug!(
vertex = i,
pos_index,
@@ -226,8 +233,8 @@ impl Gx {
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), 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],
]);
@@ -322,6 +329,22 @@ impl Gx {
}
}
+fn read_index(data: &[u8], cursor: &mut usize, attr: regs::AttributeType) -> usize {
+ match attr {
+ regs::AttributeType::Index8 => {
+ let idx = data[*cursor] as usize;
+ *cursor += 1;
+ idx
+ }
+ regs::AttributeType::Index16 => {
+ let idx = u16::from_be_bytes([data[*cursor], data[*cursor + 1]]) as usize;
+ *cursor += 2;
+ idx
+ }
+ _ => 0,
+ }
+}
+
impl Gekko {
/// Check if the GX stub detected a finish command and assert the PI interrupt
pub fn check_gx_pe_finish(&mut self) {
diff --git a/crates/gekko/src/flipper/gx/constants.rs b/crates/gekko/src/flipper/gx/constants.rs
index 8ef8a37..6f98cd4 100644
--- a/crates/gekko/src/flipper/gx/constants.rs
+++ b/crates/gekko/src/flipper/gx/constants.rs
@@ -2,13 +2,23 @@ pub const CP_CMD: u8 = 0x08;
pub const XF_CMD: u8 = 0x10;
pub const BP_CMD: u8 = 0x61;
+pub const DRAW_COMMANDS_START: u8 = 0x80;
+pub const DRAW_COMMANDS_END: u8 = 0xBF; // TODO: Double check this
+
+pub const DRAW_QUADS_CMD: u8 = 0x80;
pub const DRAW_TRIANGLES_CMD: u8 = 0x90;
+pub const DRAW_TRIANGLE_STRIP_CMD: u8 = 0x98;
+pub const DRAW_TRIANGLE_FAN_CMD: u8 = 0xA0;
+pub const DRAW_LINES_CMD: u8 = 0xA8;
+pub const DRAW_LINE_STRIP_CMD: u8 = 0xB0;
+pub const DRAW_POINTS_CMD: u8 = 0xB8;
pub const BP_REG_SIZE: usize = 0x100;
pub const CP_REG_SIZE: usize = 0xc0;
pub const XF_MEM_SIZE: usize = 0x1058;
pub const VCD_LO_REG: usize = 0x50;
+pub const VCD_HI_REG: usize = 0x60;
pub const VATA_REG: usize = 0x70;
pub const ARRAY_BASE_REG: usize = 0xA0;
pub const ARRAY_STRIDE_REG: usize = 0xB0;
@@ -22,4 +32,4 @@ pub const ARRAY_CLR1: usize = 3;
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
+pub const XF_PROJECTION_END: usize = 0x1026;
diff --git a/crates/gekko/src/flipper/gx/draw.rs b/crates/gekko/src/flipper/gx/draw.rs
index 8be5f77..c21412c 100644
--- a/crates/gekko/src/flipper/gx/draw.rs
+++ b/crates/gekko/src/flipper/gx/draw.rs
@@ -1,14 +1,26 @@
-use crate::flipper::gx::constants::DRAW_TRIANGLES_CMD;
-
#[derive(Debug)]
pub enum Primitive {
- Triangles
+ Quads,
+ Triangles,
+ TriangleStrip,
+ TriangleFan,
+ Lines,
+ LineStrip,
+ Points,
}
impl Primitive {
pub fn from_cmd(cmd: u8) -> Option<Self> {
+ use super::constants::*;
+
match cmd & !0b111 {
+ DRAW_QUADS_CMD => Some(Primitive::Quads),
DRAW_TRIANGLES_CMD => Some(Primitive::Triangles),
+ DRAW_TRIANGLE_STRIP_CMD => Some(Primitive::TriangleStrip),
+ DRAW_TRIANGLE_FAN_CMD => Some(Primitive::TriangleFan),
+ DRAW_LINES_CMD => Some(Primitive::Lines),
+ DRAW_LINE_STRIP_CMD => Some(Primitive::LineStrip),
+ DRAW_POINTS_CMD => Some(Primitive::Points),
_ => {
tracing::error!(cmd = format!("{:02X}", cmd), "unknown primitive command");
None
@@ -45,10 +57,8 @@ impl std::ops::Mul for Matrix4 {
let mut out = [[0.0f32; 4]; 4];
for col in 0..4 {
for row in 0..4 {
- out[col][row] = a[0][row] * b[col][0]
- + a[1][row] * b[col][1]
- + a[2][row] * b[col][2]
- + a[3][row] * b[col][3];
+ out[col][row] =
+ a[0][row] * b[col][0] + a[1][row] * b[col][1] + a[2][row] * b[col][2] + a[3][row] * b[col][3];
}
}
Matrix4(out)
@@ -60,4 +70,4 @@ pub struct DrawCommands {
pub modelview: Matrix4,
pub projection: Matrix4,
pub commands: Vec<DrawCall>,
-}
\ No newline at end of file
+}
diff --git a/crates/gekko/src/flipper/gx/fifo.rs b/crates/gekko/src/flipper/gx/fifo.rs
index 30ed2cb..b9cd5c3 100644
--- a/crates/gekko/src/flipper/gx/fifo.rs
+++ b/crates/gekko/src/flipper/gx/fifo.rs
@@ -1,5 +1,9 @@
-use crate::flipper::gx::{Gx, constants::{DRAW_TRIANGLES_CMD, VCD_LO_REG}, regs::VcdLo};
use super::constants::{BP_CMD, CP_CMD, XF_CMD};
+use crate::flipper::gx::{
+ Gx,
+ constants::{DRAW_COMMANDS_END, DRAW_COMMANDS_START, VATA_REG, VCD_HI_REG, VCD_LO_REG},
+ regs::{AttributeType, VatA, VcdHi, VcdLo},
+};
impl Gx {
pub fn push_u8(&mut self, val: u8) {
@@ -60,7 +64,7 @@ impl Gx {
cmds.push(FifoCmd::Bp(data));
pos += 5;
}
- DRAW_TRIANGLES_CMD => {
+ DRAW_COMMANDS_START..=DRAW_COMMANDS_END => {
// 1 command + minimum 2 vertex count
// [cmd_byte] [count_hi] [count_lo] [vertex_0_data...] [vertex_1_data...] ...
if remaining < 3 {
@@ -75,7 +79,7 @@ impl Gx {
}
let vertex_data = self.fifo[pos + 3..pos + total].to_vec();
- cmds.push(FifoCmd::DrawTriangles(cmd, vertex_data));
+ cmds.push(FifoCmd::DrawCall(cmd, vertex_data));
pos += total;
}
@@ -95,7 +99,17 @@ impl Gx {
fn vertex_stride(&self, vertex_format_index: usize) -> usize {
let vcd_lo = VcdLo::from_raw(self.cp_regs[VCD_LO_REG + vertex_format_index]);
- vcd_lo.position().size() + vcd_lo.color0().size()
+ let vcd_hi = VcdHi::from_raw(self.cp_regs[VCD_HI_REG + vertex_format_index]);
+ let vat_a = VatA::from_raw(self.cp_regs[VATA_REG + vertex_format_index]);
+
+ let tex0_size = match vcd_hi.tex0() {
+ AttributeType::Direct => vat_a.tex0_data_size(),
+ AttributeType::Index8 => 1,
+ AttributeType::Index16 => 2,
+ AttributeType::None => 0,
+ };
+
+ vcd_lo.position().size() + vcd_lo.color0().size() + tex0_size
}
}
@@ -104,5 +118,5 @@ pub enum FifoCmd {
Cp([u8; 5]),
Xf(Vec<u8>),
Bp([u8; 4]),
- DrawTriangles(u8, Vec<u8>),
+ DrawCall(u8, Vec<u8>),
}
diff --git a/crates/gekko/src/flipper/gx/regs.rs b/crates/gekko/src/flipper/gx/regs.rs
index 18641b1..6266e8a 100644
--- a/crates/gekko/src/flipper/gx/regs.rs
+++ b/crates/gekko/src/flipper/gx/regs.rs
@@ -1,5 +1,20 @@
use chapa::BitEnum;
+#[derive(Debug, PartialEq, BitEnum)]
+pub enum TexCount {
+ S, // 1D coordinate
+ St, // 2D coordinate
+}
+
+impl TexCount {
+ pub fn components(&self) -> usize {
+ match self {
+ TexCount::S => 1,
+ TexCount::St => 2,
+ }
+ }
+}
+
#[derive(Debug, PartialEq, BitEnum)]
pub enum PosCount {
Xy,
@@ -78,7 +93,7 @@ impl AttributeType {
AttributeType::None => 0,
AttributeType::Index8 => 1,
AttributeType::Index16 => 2,
- AttributeType::Direct => unimplemented!("from VAT?"),
+ AttributeType::Direct => unimplemented!("illegal?"),
}
}
}
@@ -101,6 +116,15 @@ pub struct VatA {
#[bits(14..=16)]
pub clr0_fmt: ColorFormat,
+
+ #[bits(21)]
+ pub tex0_cnt: TexCount,
+
+ #[bits(22..=24)]
+ pub tex0_fmt: ComponentFormat,
+
+ #[bits(25..=29)]
+ pub tex0_shift: u8,
}
impl VatA {
@@ -111,6 +135,18 @@ impl VatA {
pub fn clr0_data_size(&self) -> usize {
self.clr0_fmt().data_size(self.clr0_cnt())
}
+
+ pub fn tex0_data_size(&self) -> usize {
+ self.tex0_cnt().components() * self.tex0_fmt().size()
+ }
+}
+
+// CP 0x60-0x67 (one per vertex format)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy)]
+pub struct VcdHi {
+ #[bits(0..=1)]
+ pub tex0: AttributeType,
}
// CP 0x50
@@ -122,4 +158,4 @@ pub struct VcdLo {
#[bits(13..=14)]
pub color0: AttributeType,
-}
\ No newline at end of file
+}
diff --git a/crates/gekko/src/mmio/bus.rs b/crates/gekko/src/mmio/bus.rs
index 1948702..d485414 100644
--- a/crates/gekko/src/mmio/bus.rs
+++ b/crates/gekko/src/mmio/bus.rs
@@ -3,7 +3,7 @@ use crate::{
mmio::{
Mmio,
constants::{
- DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_END, GX_FIFO_BASE, PI_BASE, PI_END, VI_BASE, VI_END,
+ DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE, GX_FIFO_END, PI_BASE, PI_END, VI_BASE, VI_END,
},
},
};
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index 865d52e..d7285d3 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -128,12 +128,8 @@ impl State {
});
let (texture, bind_group) = create_xfb_texture(&device, &bind_group_layout, w, h);
- let gx_renderer = backend_wgpu::GxRenderer::new(
- &device,
- surface_format,
- surface_config.width,
- surface_config.height,
- );
+ let gx_renderer =
+ backend_wgpu::GxRenderer::new(&device, surface_format, surface_config.width, surface_config.height);
State {
surface,