Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

cull mode, alpha and clean up from stash

Laylec03769d4c1e4
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index 7fcb204..18bf583 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -31,6 +31,7 @@ impl GxRenderer {
z_write: zmode.update_enable(),
color_update: blend.color_update(),
alpha_update: blend.alpha_update(),
+ cull_mode: self.current_cull_mode,
}
}
@@ -94,6 +95,9 @@ impl GxRenderer {
GxAction::SetTexture { slot, descriptor } => {
self.current_textures[*slot] = Some(*descriptor);
}
+ GxAction::SetCullMode(mode) => {
+ self.current_cull_mode = *mode;
+ }
GxAction::Draw(draw) => {
// Copy tracked textures to avoid borrow conflict with &mut self.
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index ff669f0..f2d2adc 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -9,7 +9,7 @@ pub mod texture;
use encase::ShaderType as _;
use gecko::flipper::gx::draw::TextureFormat;
use gecko::flipper::gx::draw::{Scissor, TextureDescriptor, Viewport};
-use gecko::flipper::gx::regs::{AlphaCompare, BlendMode, CompareFunc, MagFilter, MinFilter, WrapMode, ZMode};
+use gecko::flipper::gx::regs::{AlphaCompare, BlendMode, CompareFunc, CullMode, MagFilter, MinFilter, WrapMode, ZMode};
use glam::Mat4;
use pipeline::PipelineKey;
use std::collections::HashMap;
@@ -119,6 +119,7 @@ pub struct GxRenderer {
pub(crate) current_zmode: ZMode,
pub(crate) current_blend_mode: BlendMode,
pub(crate) current_alpha_compare: AlphaCompare,
+ pub(crate) current_cull_mode: CullMode,
pub(crate) current_textures: [Option<TextureDescriptor>; 8],
// Blit pipeline (EFB -> swapchain)
pub(crate) blit_pipeline: wgpu::RenderPipeline,
@@ -443,6 +444,7 @@ impl GxRenderer {
current_scissor: Scissor::default(),
current_zmode: ZMode::default(),
current_blend_mode: BlendMode::from_raw(0).with_color_update(true).with_alpha_update(true),
+ current_cull_mode: CullMode::None,
current_alpha_compare: AlphaCompare::from_raw(0)
.with_comp0(CompareFunc::Always)
.with_comp1(CompareFunc::Always),
diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rs
index 0a4ae03..a4d7244 100644
--- a/crates/backend-wgpu/src/pipeline.rs
+++ b/crates/backend-wgpu/src/pipeline.rs
@@ -1,6 +1,6 @@
use crate::GpuVertex;
use crate::{GxRenderer, helpers};
-use gecko::flipper::gx::regs::{BlendFactor, CompareFunc};
+use gecko::flipper::gx::regs::{BlendFactor, CompareFunc, CullMode};
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub(crate) struct PipelineKey {
@@ -13,6 +13,7 @@ pub(crate) struct PipelineKey {
pub z_write: bool,
pub color_update: bool,
pub alpha_update: bool,
+ pub cull_mode: CullMode,
}
impl GxRenderer {
@@ -157,7 +158,11 @@ impl GxRenderer {
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
front_face: wgpu::FrontFace::Cw,
- cull_mode: None,
+ cull_mode: match key.cull_mode {
+ CullMode::Back => Some(wgpu::Face::Back),
+ CullMode::Front => Some(wgpu::Face::Front),
+ CullMode::None | CullMode::All => None,
+ },
..Default::default()
},
depth_stencil,
diff --git a/crates/backend-wgpu/src/shaders/efb_blit.wgsl b/crates/backend-wgpu/src/shaders/efb_blit.wgsl
index 17f0df9..7db05e1 100644
--- a/crates/backend-wgpu/src/shaders/efb_blit.wgsl
+++ b/crates/backend-wgpu/src/shaders/efb_blit.wgsl
@@ -19,5 +19,6 @@ fn vs_main(@builtin(vertex_index) vi: u32) -> VsOut {
@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
- return textureSample(efb_tex, efb_samp, in.uv);
+ let color = textureSample(efb_tex, efb_samp, in.uv);
+ return vec4<f32>(color.rgb, 1.0);
}
diff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rs
index bc558d5..36c1ab8 100644
--- a/crates/gecko/src/flipper/gx/bp.rs
+++ b/crates/gecko/src/flipper/gx/bp.rs
@@ -7,8 +7,9 @@ use super::constants::{
EFB_WIDTH,
};
use super::regs::{
- AlphaCompare, BlendMode, GenMode, TevAlphaEnv, TevColorEnv, TevOrder, TevRegType, TevRegisterH, TevRegisterL,
- TxSetImage0, TxSetImage3, TxSetMode0, ZMode,
+ AlphaCompare, BlendMode, EfbCopyDims, EfbCopyDst, EfbCopyDstStride, EfbCopySrc, GenMode, PeClearAr, PeClearGb,
+ PeClearZ, PeCopyCmd, SuScisOffset, SuScisRect, TevAlphaEnv, TevColorEnv, TevOrder, TevRegType, TevRegisterH,
+ TevRegisterL, TxSetImage0, TxSetImage3, TxSetMode0, ZMode,
};
use super::{GraphicsProcessor, draw};
use crate::host::{GxAction, RenderSink};
@@ -72,13 +73,14 @@ impl GraphicsProcessor {
self.load_tev_register(idx, val);
}
- // GEN_MODE, extract num TEV stages
+ // GEN_MODE, extract num TEV stages + cull mode
if idx == BP_GEN_MODE {
let gen_mode = GenMode::from_raw(val);
let stages = gen_mode.num_tev_stages() + 1;
tracing::debug!(num_tev_stages = stages, "GEN_MODE");
self.cur_num_tev_stages = stages;
self.resolve_konst_colors();
+ renderer.exec(GxAction::SetCullMode(gen_mode.cull_mode()));
}
// TEV KSEL (constant color selection) registers: recalculate konst colors
@@ -246,42 +248,29 @@ impl GraphicsProcessor {
}
fn efb_copy(&mut self, renderer: &mut dyn RenderSink, trigger: u32) {
- // Decode source rect from BP 0x49/0x4A.
- // BPMEM_EFB_TL (0x49): x in bits 0..10, y in bits 10..20.
- // BPMEM_EFB_BR (0x4A): width-1 in bits 0..10, height-1 in bits 10..20.
- let src_reg = self.bp_regs[BP_PE_COPY_SRC];
- let dims_reg = self.bp_regs[BP_PE_COPY_DIMS];
- let src_x = src_reg & 0x3FF;
- let src_y = (src_reg >> 10) & 0x3FF;
- let src_w = (dims_reg & 0x3FF) + 1;
- let src_h = ((dims_reg >> 10) & 0x3FF) + 1;
-
- // Destination address (BP 0x4B, shifted left by 5)
- let dest_addr = (self.bp_regs[BP_PE_COPY_DST] & 0x00FFFFFF) << 5;
- let dest_stride = self.bp_regs[BP_PE_COPY_DST_STRIDE] & 0x3FF;
-
- // Trigger word bits (PE_COPY_EXECUTE, BP 0x52):
- // bit 0-1: clamp
- // bit 9: half (1/2 vertical scale)
- // bit 10: scale_invert
- // bit 11: clear EFB after copy
- // bit 12-13: frame-to-field (interlacing)
- // bit 14: copy-to-xfb (0 = copy-to-texture)
- let copy_to_xfb = (trigger >> 14) & 1 != 0;
- let clear = (trigger >> 11) & 1 != 0;
- let half = (trigger >> 9) & 1 != 0;
-
- // Clear color from BP 0x4F/0x50
- let clear_ar = self.bp_regs[BP_PE_COPY_CLEAR_AR];
- let clear_gb = self.bp_regs[BP_PE_COPY_CLEAR_GB];
- let a = ((clear_ar >> 8) & 0xFF) as f32 / 255.0;
- let r = (clear_ar & 0xFF) as f32 / 255.0;
- let g = ((clear_gb >> 8) & 0xFF) as f32 / 255.0;
- let b = (clear_gb & 0xFF) as f32 / 255.0;
-
- // Clear Z from BP 0x51
- let clear_z_raw = self.bp_regs[BP_PE_COPY_CLEAR_Z] & 0x00FFFFFF;
- let clear_z = clear_z_raw as f32 / 16777215.0;
+ let src = EfbCopySrc::from_raw(self.bp_regs[BP_PE_COPY_SRC]);
+ let dims = EfbCopyDims::from_raw(self.bp_regs[BP_PE_COPY_DIMS]);
+ let src_x = src.left() as u32;
+ let src_y = src.top() as u32;
+ let src_w = dims.width_minus1() as u32 + 1;
+ let src_h = dims.height_minus1() as u32 + 1;
+
+ let dest_addr = EfbCopyDst::from_raw(self.bp_regs[BP_PE_COPY_DST]).addr();
+ let dest_stride = EfbCopyDstStride::from_raw(self.bp_regs[BP_PE_COPY_DST_STRIDE]).stride() as u32;
+
+ let cmd = PeCopyCmd::from_raw(trigger);
+ let copy_to_xfb = cmd.copy_to_xfb();
+ let clear = cmd.clear();
+ let half = cmd.half();
+
+ let clear_ar = PeClearAr::from_raw(self.bp_regs[BP_PE_COPY_CLEAR_AR]);
+ let clear_gb = PeClearGb::from_raw(self.bp_regs[BP_PE_COPY_CLEAR_GB]);
+ let a = clear_ar.alpha() as f32 / 255.0;
+ let r = clear_ar.red() as f32 / 255.0;
+ let g = clear_gb.green() as f32 / 255.0;
+ let b = clear_gb.blue() as f32 / 255.0;
+
+ let clear_z = PeClearZ::from_raw(self.bp_regs[BP_PE_COPY_CLEAR_Z]).z() as f32 / 16777215.0;
tracing::debug!(
src_x,
@@ -334,13 +323,13 @@ impl GraphicsProcessor {
}
fn recompute_scissor(&mut self) {
- let tl = self.bp_regs[BP_SU_SCIS_TL];
- let br = self.bp_regs[BP_SU_SCIS_BR];
+ let tl = SuScisRect::from_raw(self.bp_regs[BP_SU_SCIS_TL]);
+ let br = SuScisRect::from_raw(self.bp_regs[BP_SU_SCIS_BR]);
- let tl_y = (tl & 0x7FF) as i32 - 342;
- let tl_x = ((tl >> 12) & 0x7FF) as i32 - 342;
- let br_y = (br & 0x7FF) as i32 - 342 + 1; // BR is inclusive
- let br_x = ((br >> 12) & 0x7FF) as i32 - 342 + 1;
+ let tl_x = tl.x() as i32 - 342;
+ let tl_y = tl.y() as i32 - 342;
+ let br_x = br.x() as i32 - 342 + 1; // BR is inclusive
+ let br_y = br.y() as i32 - 342 + 1;
// BP_SU_SCIS_OFFSET shifts the scissor (and viewport) origin in the
// EFB. Games use it for tiled rendering: the logical scissor stays
@@ -364,13 +353,8 @@ impl GraphicsProcessor {
}
fn recompute_scissor_offset(&mut self) {
- // BP_SU_SCIS_OFFSET layout:
- // bits 0..10: (x + 342) / 2
- // bits 10..20: (y + 342) / 2
- let raw = self.bp_regs[BP_SU_SCIS_OFFSET];
- let raw_x = (raw & 0x3FF) as i32;
- let raw_y = ((raw >> 10) & 0x3FF) as i32;
- self.cur_scissor_offset_x = raw_x * 2 - 342;
- self.cur_scissor_offset_y = raw_y * 2 - 342;
+ let reg = SuScisOffset::from_raw(self.bp_regs[BP_SU_SCIS_OFFSET]);
+ self.cur_scissor_offset_x = reg.x() as i32 * 2 - 342;
+ self.cur_scissor_offset_y = reg.y() as i32 * 2 - 342;
}
}
diff --git a/crates/gecko/src/flipper/gx/regs.rs b/crates/gecko/src/flipper/gx/regs.rs
index 37f5ef2..720f228 100644
--- a/crates/gecko/src/flipper/gx/regs.rs
+++ b/crates/gecko/src/flipper/gx/regs.rs
@@ -1,6 +1,5 @@
-use chapa::BitEnum;
-
use crate::flipper::gx::draw::TextureFormat;
+use chapa::BitEnum;
// GX compare function (shared by Z-mode and alpha compare)
#[derive(Debug, PartialEq, BitEnum, Hash, Eq)]
@@ -855,6 +854,16 @@ pub struct TevRegisterH {
pub struct GenMode {
#[bits(10..=13)]
pub num_tev_stages: u8, // num stages - 1
+ #[bits(14..=15)]
+ pub cull_mode: CullMode,
+}
+
+#[derive(BitEnum, Debug, PartialEq, Eq, Hash)]
+pub enum CullMode {
+ None = 0,
+ Back = 1,
+ Front = 2,
+ All = 3,
}
// XF 0x1018 Matrix Index A (position/tex0-3 matrix indices)
@@ -1049,3 +1058,123 @@ pub struct DualTexGenReg {
#[bits(6)]
pub normalize: bool,
}
+
+// BP 0x49 BPMEM_EFB_TL (EFB copy source top-left)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct EfbCopySrc {
+ #[bits(0..=9)]
+ pub left: u16,
+
+ #[bits(10..=19)]
+ pub top: u16,
+}
+
+// BP 0x4A BPMEM_EFB_WH (EFB copy source dimensions, stored as size-1)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct EfbCopyDims {
+ #[bits(0..=9)]
+ pub width_minus1: u16,
+
+ #[bits(10..=19)]
+ pub height_minus1: u16,
+}
+
+// BP 0x4B BPMEM_EFB_ADDR (EFB copy destination base address)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct EfbCopyDst {
+ #[bits(0..=23)]
+ pub addr_base: u32,
+}
+
+impl EfbCopyDst {
+ /// Physical RAM address (addr_base << 5).
+ pub fn addr(&self) -> u32 {
+ self.addr_base() << 5
+ }
+}
+
+// BP 0x4D BPMEM_EFB_STRIDE (EFB copy destination stride)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct EfbCopyDstStride {
+ #[bits(0..=9)]
+ pub stride: u16,
+}
+
+// BP 0x52 BPMEM_TRIGGER_EFB_COPY (PE copy execute/trigger)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct PeCopyCmd {
+ #[bits(0..=1)]
+ pub clamp: u8,
+
+ #[bits(9)]
+ pub half: bool,
+
+ #[bits(10)]
+ pub scale_invert: bool,
+
+ #[bits(11)]
+ pub clear: bool,
+
+ #[bits(12..=13)]
+ pub frame_to_field: u8,
+
+ #[bits(14)]
+ pub copy_to_xfb: bool,
+}
+
+// BP 0x4F BPMEM_CLEAR_AR (PE clear alpha/red)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct PeClearAr {
+ #[bits(0..=7)]
+ pub red: u8,
+
+ #[bits(8..=15)]
+ pub alpha: u8,
+}
+
+// BP 0x50 BPMEM_CLEAR_GB (PE clear green/blue)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct PeClearGb {
+ #[bits(0..=7)]
+ pub blue: u8,
+
+ #[bits(8..=15)]
+ pub green: u8,
+}
+
+// BP 0x51 BPMEM_CLEAR_Z (PE clear depth)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct PeClearZ {
+ #[bits(0..=23)]
+ pub z: u32,
+}
+
+// BP 0x20/0x21 SU_SCIS_TL / SU_SCIS_BR (scissor rect corner)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct SuScisRect {
+ #[bits(0..=10)]
+ pub y: u16,
+
+ #[bits(12..=22)]
+ pub x: u16,
+}
+
+// BP 0x59 SU_SCIS_OFFSET (scissor offset, encoded as (val + 342) / 2)
+#[chapa::bitfield(u32, order = lsb0)]
+#[derive(Debug, Clone, Copy, Default)]
+pub struct SuScisOffset {
+ #[bits(0..=9)]
+ pub x: u16,
+
+ #[bits(10..=19)]
+ pub y: u16,
+}
diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rs
index ff3af83..5163b76 100644
--- a/crates/gecko/src/host.rs
+++ b/crates/gecko/src/host.rs
@@ -1,5 +1,5 @@
use crate::flipper::gx::draw::{EfbCopyCmd, Primitive, Scissor, TextureDescriptor, Viewport};
-use crate::flipper::gx::regs::{AlphaCompare, BlendMode, ChanCtrl, ZMode};
+use crate::flipper::gx::regs::{AlphaCompare, BlendMode, ChanCtrl, CullMode, ZMode};
#[derive(Debug)]
pub enum GxAction {
@@ -15,6 +15,7 @@ pub enum GxAction {
SetDepthMode(ZMode),
SetBlendMode(BlendMode),
SetAlphaCompare(AlphaCompare),
+ SetCullMode(CullMode),
SetTexture {
slot: usize,
descriptor: TextureDescriptor,