Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

wgpu validation fixes and ci4, ci8 and ci14 (resi4)

Layle203aeba91b54
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index bb75383..b0fd5d8 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -504,12 +504,14 @@ impl GxRenderer {
}
let sc = &self.draw_scissors[index];
- let sc_w = sc.w.max(1).min(target_width.saturating_sub(sc.x));
- let sc_h = sc.h.max(1).min(target_height.saturating_sub(sc.y));
- rpass.set_scissor_rect(sc.x, sc.y, sc_w, sc_h);
+ let sc_x = sc.x.min(target_width);
+ let sc_y = sc.y.min(target_height);
+ let sc_w = sc.w.min(target_width - sc_x);
+ let sc_h = sc.h.min(target_height - sc_y);
+ rpass.set_scissor_rect(sc_x, sc_y, sc_w, sc_h);
let raster_marker = format!(
"Raster state: viewport=({:.1},{:.1} {:.1}x{:.1}) scissor=({},{} {}x{})",
- vp.x, vp.y, vp_w, vp_h, sc.x, sc.y, sc_w, sc_h
+ vp.x, vp.y, vp_w, vp_h, sc_x, sc_y, sc_w, sc_h
);
rpass.insert_debug_marker(&raster_marker);
diff --git a/crates/dbglib/src/windows/gx.rs b/crates/dbglib/src/windows/gx.rs
index 9603dba..fc943e4 100644
--- a/crates/dbglib/src/windows/gx.rs
+++ b/crates/dbglib/src/windows/gx.rs
@@ -1,10 +1,10 @@
use egui::{Context, Grid, ScrollArea};
use gecko::flipper::gx::GraphicsProcessor;
-use gecko::flipper::gx::draw::TextureDescriptor;
+use gecko::flipper::gx::draw::{TextureDescriptor, TlutRef};
use gecko::mmio::Mmio;
-fn texture_preview(ui: &mut egui::Ui, tex: &TextureDescriptor, ram: &[u8]) {
- let rgba = gecko::flipper::gx::texture::decode_to_rgba(ram, tex);
+fn texture_preview(ui: &mut egui::Ui, tex: &TextureDescriptor, ram: &[u8], palette: &[u16], tlut: TlutRef) {
+ let rgba = gecko::flipper::gx::texture::decode_to_rgba(ram, tex, palette, tlut.format);
let size = [tex.width as usize, tex.height as usize];
let image = egui::ColorImage::from_rgba_unmultiplied(size, &rgba);
let handle = ui.ctx().load_texture(
@@ -146,8 +146,11 @@ pub fn show_gx(ctx: &Context, open: &mut bool, gx: &GraphicsProcessor, mmio: &Mm
"Slot {slot}: {:?} {}x{} @ 0x{:08X}",
tex.format, tex.width, tex.height, tex.ram_addr
);
+ let tlut = gx.cur_tluts[slot];
+ let base = (tlut.tmem_offset as usize) * 256;
+ let palette = gx.palette_mem.get(base..).unwrap_or(&[]);
ui.collapsing(heading, |ui| {
- texture_preview(ui, tex, &mmio.ram);
+ texture_preview(ui, tex, &mmio.ram, palette, tlut);
});
}
}
diff --git a/crates/gecko/src/cpu/dec.rs b/crates/gecko/src/cpu/dec.rs
index 71584ba..26242af 100644
--- a/crates/gecko/src/cpu/dec.rs
+++ b/crates/gecko/src/cpu/dec.rs
@@ -59,7 +59,7 @@ pub fn underflow_handler(gc: &mut GameCube) {
gc.cpu.spr.dec = u32::MAX;
gc.scheduler
.schedule_in(cycles_until_underflow(u32::MAX), self::underflow_handler);
- tracing::info!(
+ tracing::debug!(
cycles = gc.scheduler.cycles,
ee = gc.cpu.msr.external_interrupt_enable(),
pi_pending = gc.pi.interrupt_pending(),
diff --git a/crates/gecko/src/cpu/interpreter/system.rs b/crates/gecko/src/cpu/interpreter/system.rs
index aa4377a..e111442 100644
--- a/crates/gecko/src/cpu/interpreter/system.rs
+++ b/crates/gecko/src/cpu/interpreter/system.rs
@@ -34,7 +34,7 @@ pub fn spr<const OP: u32>(ctx: &mut crate::gamecube::GameCube, instr: crate::cpu
crate::cpu::dec::cycles_until_underflow(val),
crate::cpu::dec::underflow_handler,
);
- tracing::info!(cycles = ctx.scheduler.cycles, value = val, "decrementer set");
+ tracing::debug!(cycles = ctx.scheduler.cycles, value = val, "decrementer set");
}
284 => ctx.scheduler.set_timebase_lower(val),
285 => ctx.scheduler.set_timebase_upper(val),
diff --git a/crates/gecko/src/cpu/irq.rs b/crates/gecko/src/cpu/irq.rs
index 1a3455d..b9d5916 100644
--- a/crates/gecko/src/cpu/irq.rs
+++ b/crates/gecko/src/cpu/irq.rs
@@ -81,7 +81,7 @@ impl GameCube {
self.cpu.pc = base | IRQ_DECREMENTER;
- tracing::info!(addr = format!("{:08X}", self.cpu.pc), "decrementer IRQ triggered");
+ tracing::debug!(addr = format!("{:08X}", self.cpu.pc), "decrementer IRQ triggered");
}
pub fn cause_trap_exception(&mut self) {
diff --git a/crates/gecko/src/flipper/dsp.rs b/crates/gecko/src/flipper/dsp.rs
index a9543e3..0e8d891 100644
--- a/crates/gecko/src/flipper/dsp.rs
+++ b/crates/gecko/src/flipper/dsp.rs
@@ -194,7 +194,7 @@ impl GameCube {
let pc = self.dsp.registers.pc as usize;
let w0 = self.dsp.read_imem(pc as u16);
- let w1 = self.dsp.read_imem(pc as u16 + 1);
+ let w1 = self.dsp.read_imem((pc as u16).wrapping_add(1));
let buf = [(w0 >> 8) as u8, w0 as u8, (w1 >> 8) as u8, w1 as u8];
let instr = Instruction::from_be_bytes(&buf);
self.dsp.registers.cia = self.dsp.registers.pc;
diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rs
index 3197abf..ca70d9e 100644
--- a/crates/gecko/src/flipper/gx.rs
+++ b/crates/gecko/src/flipper/gx.rs
@@ -10,7 +10,7 @@ pub mod texture;
mod vertex;
mod xf;
-use crate::flipper::gx::constants::{BP_REG_SIZE, CP_REG_SIZE, XF_MEM_SIZE};
+use crate::flipper::gx::constants::{BP_REG_SIZE, CP_REG_SIZE, TLUT_MEM_ENTRIES, XF_MEM_SIZE};
use crate::flipper::gx::draw::Matrix4;
use crate::flipper::gx::regs::{AlphaCompare, BlendMode, TevAlphaEnv, TevColorEnv, TevRegisterH, TevRegisterL, ZMode};
use crate::gamecube::GameCube;
@@ -34,6 +34,13 @@ pub struct GraphicsProcessor {
// Current GX state to snapshot into a Draw action later
pub cur_textures: [Option<draw::TextureDescriptor>; 8],
+ // Per-texture-slot TLUT binding (tmem offset + palette pixel format),
+ // populated by BP_TX_SETTLUT writes.
+ pub cur_tluts: [draw::TlutRef; 8],
+ // Palette TMEM: backing store for indexed texture palettes. Addressed as
+ // u16 entries; a LOADTLUT copies count*16 entries starting at
+ // (tmem_offset * 256). Fixed-size so indexing is branch-free.
+ pub palette_mem: Vec<u16>,
pub cur_tev_color_env: [TevColorEnv; 16],
pub cur_tev_alpha_env: [TevAlphaEnv; 16],
pub cur_tev_color_regs_lo: [TevRegisterL; 4],
@@ -89,6 +96,8 @@ impl GraphicsProcessor {
fifo: Vec::with_capacity(256),
projection: Matrix4::default(),
cur_textures: Default::default(),
+ cur_tluts: [draw::TlutRef::default(); 8],
+ palette_mem: vec![0u16; TLUT_MEM_ENTRIES],
cur_tev_color_env: Default::default(),
cur_tev_alpha_env: Default::default(),
cur_tev_color_regs_lo: Default::default(),
diff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rs
index 3cb42f4..f365973 100644
--- a/crates/gecko/src/flipper/gx/bp.rs
+++ b/crates/gecko/src/flipper/gx/bp.rs
@@ -1,10 +1,11 @@
use super::constants::{
- BP_GEN_MODE, BP_PE_ALPHA_COMPARE, BP_PE_CMODE0, BP_PE_COPY_CLEAR_AR, BP_PE_COPY_CLEAR_GB, BP_PE_COPY_CLEAR_Z,
- BP_PE_COPY_CMD, BP_PE_COPY_DIMS, BP_PE_COPY_DST, BP_PE_COPY_DST_STRIDE, BP_PE_COPY_SRC, BP_PE_COPY_YSCALE,
- BP_PE_DONE, BP_PE_DONE_FINISH_BIT, BP_PE_TOKEN, BP_PE_TOKEN_INT, BP_PE_ZCOMPARE, BP_PE_ZMODE, BP_RAS1_TREF_COUNT,
- BP_RAS1_TREF0, BP_SU_SCIS_BR, BP_SU_SCIS_OFFSET, BP_SU_SCIS_TL, BP_TEV_COLOR_ENV_0, BP_TEV_KSEL_0,
- BP_TEV_REGISTERL_0, BP_TX_SETIMAGE0_I0, BP_TX_SETIMAGE0_I4, BP_TX_SETIMAGE3_I0, BP_TX_SETIMAGE3_I4,
- BP_TX_SETMODE0_I0, BP_TX_SETMODE0_I4, EFB_HEIGHT, EFB_WIDTH,
+ BP_GEN_MODE, BP_LOAD_TLUT0, BP_LOAD_TLUT1, BP_PE_ALPHA_COMPARE, BP_PE_CMODE0, BP_PE_COPY_CLEAR_AR,
+ BP_PE_COPY_CLEAR_GB, BP_PE_COPY_CLEAR_Z, BP_PE_COPY_CMD, BP_PE_COPY_DIMS, BP_PE_COPY_DST, BP_PE_COPY_DST_STRIDE,
+ BP_PE_COPY_SRC, BP_PE_COPY_YSCALE, BP_PE_DONE, BP_PE_DONE_FINISH_BIT, BP_PE_TOKEN, BP_PE_TOKEN_INT, BP_PE_ZCOMPARE,
+ BP_PE_ZMODE, BP_RAS1_TREF_COUNT, BP_RAS1_TREF0, BP_SU_SCIS_BR, BP_SU_SCIS_OFFSET, BP_SU_SCIS_TL,
+ BP_TEV_COLOR_ENV_0, BP_TEV_KSEL_0, BP_TEV_REGISTERL_0, BP_TX_SETIMAGE0_I0, BP_TX_SETIMAGE0_I4, BP_TX_SETIMAGE3_I0,
+ BP_TX_SETIMAGE3_I4, BP_TX_SETMODE0_I0, BP_TX_SETMODE0_I4, BP_TX_SETTLUT_I0, BP_TX_SETTLUT_I4, EFB_HEIGHT,
+ EFB_WIDTH, TLUT_ENTRIES_PER_UNIT, TLUT_LOAD_ENTRIES_PER_UNIT,
};
use super::regs::{
AlphaCompare, BlendMode, DispCopyYScale, EfbCopyDims, EfbCopyDst, EfbCopyDstStride, EfbCopySrc, GenMode, PeClearAr,
@@ -43,6 +44,21 @@ impl GraphicsProcessor {
self.snapshot_texture(renderer, ram, slot, val);
}
+ // TX_SETTLUT: per-texture-slot binding of the palette (tmem offset +
+ // palette pixel format). Layout mirrors TX_SETIMAGE*: slots 0-3 at
+ // BP_TX_SETTLUT_I0, slots 4-7 at BP_TX_SETTLUT_I4.
+ if idx >= BP_TX_SETTLUT_I0 && idx < BP_TX_SETTLUT_I0 + 4 {
+ self.cur_tluts[idx - BP_TX_SETTLUT_I0] = draw::TlutRef::from_raw(val);
+ } else if idx >= BP_TX_SETTLUT_I4 && idx < BP_TX_SETTLUT_I4 + 4 {
+ self.cur_tluts[idx - BP_TX_SETTLUT_I4 + 4] = draw::TlutRef::from_raw(val);
+ }
+
+ // LOADTLUT: writing TLUT1 triggers a copy from main RAM (address in
+ // TLUT0, stored pre-shifted by 5) into our palette TMEM.
+ if idx == BP_LOAD_TLUT1 {
+ self.load_tlut(ram, val);
+ }
+
// Forward PE render state
match idx {
BP_PE_ZMODE => {
@@ -171,7 +187,19 @@ impl GraphicsProcessor {
"texture descriptor updated"
);
- let changed = self::texture_data_changed(&mut self.texture_hashes, ram, ram_addr, width, height, format);
+ let tlut = self.cur_tluts[slot];
+ let palette = slot_palette(&self.palette_mem, tlut.tmem_offset);
+
+ let changed = self::texture_data_changed(
+ &mut self.texture_hashes,
+ ram,
+ ram_addr,
+ width,
+ height,
+ format,
+ palette,
+ tlut,
+ );
if changed {
let desc = draw::TextureDescriptor {
@@ -188,7 +216,7 @@ impl GraphicsProcessor {
id: ram_addr as Address,
width,
height,
- rgba: texture::decode_to_rgba(ram, &desc),
+ rgba: texture::decode_to_rgba(ram, &desc, palette, tlut.format),
});
}
@@ -459,6 +487,11 @@ impl GraphicsProcessor {
}
}
+fn slot_palette(palette_mem: &[u16], tmem_offset: u16) -> &[u16] {
+ let base = (tmem_offset as usize) * TLUT_ENTRIES_PER_UNIT;
+ palette_mem.get(base..).unwrap_or(&[])
+}
+
/// Returns `true` when the raw texture data in RAM differs from the last
/// hash recorded for the given address.
fn texture_data_changed(
@@ -468,13 +501,72 @@ fn texture_data_changed(
width: u32,
height: u32,
format: draw::TextureFormat,
+ palette: &[u16],
+ tlut: draw::TlutRef,
) -> bool {
let size = texture::raw_data_size(width, height, format);
let Some(slice) = ram.get(addr..addr + size) else {
tracing::warn!(addr, size, "texture_changed: RAM slice OOB, assuming changed");
return true;
};
- let hash = twox_hash::xxhash3_64::Hasher::oneshot(slice);
+ let mut hash = twox_hash::xxhash3_64::Hasher::oneshot(slice);
+ if format.is_paletted() {
+ let max_entries = match format {
+ draw::TextureFormat::CI4 => 16,
+ draw::TextureFormat::CI8 => 256,
+ draw::TextureFormat::CI14 => 16384,
+ _ => 0,
+ };
+ let take = max_entries.min(palette.len());
+ let palette_bytes: &[u8] = bytemuck::cast_slice(&palette[..take]);
+ hash ^= twox_hash::xxhash3_64::Hasher::oneshot(palette_bytes);
+ // Palette pixel format is part of the visual state too, so fold it
+ // into the hash or a format switch alone wouldn't force a redecode.
+ hash ^= tlut.format as u64;
+ }
let prev = hashes.insert(addr as u32, hash);
prev != Some(hash)
}
+
+impl GraphicsProcessor {
+ /// Service a write to BP_LOAD_TLUT1: pulls `count * 16` big-endian u16
+ /// palette entries from main RAM (source address is BP_LOAD_TLUT0 << 5)
+ /// into palette TMEM starting at `tmem_offset * 256`.
+ fn load_tlut(&mut self, ram: &[u8], load_val: u32) {
+ let tmem_offset = (load_val & 0x3FF) as usize;
+ let count = ((load_val >> 10) & 0x7FF) as usize;
+ let ram_base = ((self.bp_regs[BP_LOAD_TLUT0] as usize) & 0x00FF_FFFF) << 5;
+ let entries = count * TLUT_LOAD_ENTRIES_PER_UNIT;
+ let byte_count = entries * 2;
+ let dst_base = tmem_offset * TLUT_ENTRIES_PER_UNIT;
+
+ if entries == 0 {
+ return;
+ }
+ if ram_base.saturating_add(byte_count) > ram.len() {
+ tracing::warn!(
+ ram_base = format!("{ram_base:#010X}"),
+ byte_count,
+ "LOADTLUT: source RAM range OOB, skipping"
+ );
+ return;
+ }
+ if dst_base.saturating_add(entries) > self.palette_mem.len() {
+ tracing::warn!(
+ dst_base,
+ entries,
+ tmem_limit = self.palette_mem.len(),
+ "LOADTLUT: destination palette range OOB, skipping"
+ );
+ return;
+ }
+
+ let src = &ram[ram_base..ram_base + byte_count];
+ let dst = &mut self.palette_mem[dst_base..dst_base + entries];
+ for (entry, chunk) in dst.iter_mut().zip(src.chunks_exact(2)) {
+ *entry = u16::from_be_bytes([chunk[0], chunk[1]]);
+ }
+
+ tracing::debug!(ram_base = format!("{ram_base:#010X}"), tmem_offset, entries, "LOADTLUT");
+ }
+}
diff --git a/crates/gecko/src/flipper/gx/constants.rs b/crates/gecko/src/flipper/gx/constants.rs
index 8c56ee1..6fda03d 100644
--- a/crates/gecko/src/flipper/gx/constants.rs
+++ b/crates/gecko/src/flipper/gx/constants.rs
@@ -96,12 +96,26 @@ pub const BP_TX_SETIMAGE0_I0: usize = 0x88; // TX_SETIMAGE0 maps 0-3 (width/heig
pub const BP_TX_SETIMAGE1_I0: usize = 0x8C; // TX_SETIMAGE1 maps 0-3
pub const BP_TX_SETIMAGE2_I0: usize = 0x90; // TX_SETIMAGE2 maps 0-3
pub const BP_TX_SETIMAGE3_I0: usize = 0x94; // TX_SETIMAGE3 maps 0-3 (IMAGE_BASE = addr >> 5)
+pub const BP_TX_SETTLUT_I0: usize = 0x98; // TX_SETTLUT maps 0-3 (tmem_offset + clut format)
pub const BP_TX_SETMODE0_I4: usize = 0xA0; // TX_SETMODE0 maps 4-7
pub const BP_TX_SETMODE1_I4: usize = 0xA4; // TX_SETMODE1 maps 4-7
pub const BP_TX_SETIMAGE0_I4: usize = 0xA8; // TX_SETIMAGE0 maps 4-7
pub const BP_TX_SETIMAGE1_I4: usize = 0xAC; // TX_SETIMAGE1 maps 4-7
pub const BP_TX_SETIMAGE2_I4: usize = 0xB0; // TX_SETIMAGE2 maps 4-7
pub const BP_TX_SETIMAGE3_I4: usize = 0xB4; // TX_SETIMAGE3 maps 4-7 (IMAGE_BASE = addr >> 5)
+pub const BP_TX_SETTLUT_I4: usize = 0xB8; // TX_SETTLUT maps 4-7
+
+// BP TLUT load registers: LOAD_TLUT0 holds the source RAM addr (>> 5 in the
+// register), LOAD_TLUT1 triggers the copy and carries tmem_offset / count.
+pub const BP_LOAD_TLUT0: usize = 0x64;
+pub const BP_LOAD_TLUT1: usize = 0x65;
+
+// Palette TMEM layout: tmem_offset is in 256-entry (512-byte) units.
+pub const TLUT_ENTRIES_PER_UNIT: usize = 256;
+// LOADTLUT `count` is in 32-byte (16 u16 entries) units.
+pub const TLUT_LOAD_ENTRIES_PER_UNIT: usize = 16;
+// Full palette TMEM area, in u16 entries. u10 offset * 256 entries = 262144.
+pub const TLUT_MEM_ENTRIES: usize = 1024 * TLUT_ENTRIES_PER_UNIT;
// BP PE (Pixel Engine) registers
pub const BP_PE_ZMODE: usize = 0x40;
diff --git a/crates/gecko/src/flipper/gx/draw.rs b/crates/gecko/src/flipper/gx/draw.rs
index 01c625c..44654f4 100644
--- a/crates/gecko/src/flipper/gx/draw.rs
+++ b/crates/gecko/src/flipper/gx/draw.rs
@@ -57,6 +57,49 @@ pub enum TextureFormat {
CMPR = 0xE,
}
+impl TextureFormat {
+ #[inline(always)]
+ pub fn is_paletted(self) -> bool {
+ matches!(self, TextureFormat::CI4 | TextureFormat::CI8 | TextureFormat::CI14)
+ }
+}
+
+#[derive(Debug, Clone, Copy, Default)]
+pub enum TlutFormat {
+ #[default]
+ IA8,
+ RGB565,
+ RGB5A3,
+}
+
+impl TlutFormat {
+ #[inline(always)]
+ pub fn from_raw(val: u32) -> Self {
+ match val & 0x3 {
+ 0 => Self::IA8,
+ 1 => Self::RGB565,
+ 2 => Self::RGB5A3,
+ _ => Self::IA8,
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy, Default)]
+pub struct TlutRef {
+ pub tmem_offset: u16,
+ pub format: TlutFormat,
+}
+
+impl TlutRef {
+ #[inline(always)]
+ pub fn from_raw(val: u32) -> Self {
+ Self {
+ tmem_offset: (val & 0x3FF) as u16,
+ format: TlutFormat::from_raw(val >> 10),
+ }
+ }
+}
+
#[derive(Debug, Clone, Copy)]
pub struct TextureDescriptor {
pub ram_addr: usize,
diff --git a/crates/gecko/src/flipper/gx/texture.rs b/crates/gecko/src/flipper/gx/texture.rs
index 69b369b..9428699 100644
--- a/crates/gecko/src/flipper/gx/texture.rs
+++ b/crates/gecko/src/flipper/gx/texture.rs
@@ -1,8 +1,13 @@
-use super::draw::{TextureDescriptor, TextureFormat};
+use super::draw::{TextureDescriptor, TextureFormat, TlutFormat};
use multiversion::multiversion;
/// Decode a GX-format texture from RAM into RGBA8 pixels.
-pub fn decode_to_rgba(ram: &[u8], desc: &TextureDescriptor) -> Vec<u8> {
+///
+/// `palette` is the slice of the palette TMEM starting at the bound TLUT's
+/// tmem_offset. It is only consulted for paletted (CI*) formats; callers may
+/// pass `&[]` for non-paletted textures. `tlut_format` specifies how each
+/// 16-bit palette entry should be expanded to RGBA8.
+pub fn decode_to_rgba(ram: &[u8], desc: &TextureDescriptor, palette: &[u16], tlut_format: TlutFormat) -> Vec<u8> {
let w = desc.width as usize;
let h = desc.height as usize;
@@ -16,7 +21,9 @@ pub fn decode_to_rgba(ram: &[u8], desc: &TextureDescriptor) -> Vec<u8> {
TextureFormat::RGB5A3 => decode_rgb5a3(ram, desc, &mut rgba, w, h),
TextureFormat::RGBA8 => decode_rgba8(ram, desc, &mut rgba, w, h),
TextureFormat::CMPR => decode_cmpr(ram, desc, &mut rgba, w, h),
- _ => tracing::error!(?desc.format, "unsupported texture format"),
+ TextureFormat::CI4 => decode_ci4(ram, desc, &mut rgba, w, h, palette, tlut_format),
+ TextureFormat::CI8 => decode_ci8(ram, desc, &mut rgba, w, h, palette, tlut_format),
+ TextureFormat::CI14 => decode_ci14(ram, desc, &mut rgba, w, h, palette, tlut_format),
}
rgba
@@ -422,6 +429,180 @@ fn decode_cmpr(ram: &[u8], desc: &TextureDescriptor, rgba: &mut [u8], w: usize,
}
}
+#[inline(always)]
+fn tlut_entry_to_rgba(packed: u16, format: TlutFormat) -> [u8; 4] {
+ match format {
+ TlutFormat::IA8 => {
+ // Palette IA8 is stored big-endian in TMEM; after our from_be_bytes
+ // load, high byte = alpha, low byte = intensity.
+ let a = (packed >> 8) as u8;
+ let i = (packed & 0xFF) as u8;
+ [i, i, i, a]
+ }
+ TlutFormat::RGB565 => self::rgb565_to_rgba(packed),
+ TlutFormat::RGB5A3 => {
+ if packed & 0x8000 != 0 {
+ let r = expand_to_8::<5>(((packed >> 10) & 0x1F) as u8);
+ let g = expand_to_8::<5>(((packed >> 5) & 0x1F) as u8);
+ let b = expand_to_8::<5>((packed & 0x1F) as u8);
+ [r, g, b, 255]
+ } else {
+ let a = expand_to_8::<3>(((packed >> 12) & 0x7) as u8);
+ let r = expand_to_8::<4>(((packed >> 8) & 0xF) as u8);
+ let g = expand_to_8::<4>(((packed >> 4) & 0xF) as u8);
+ let b = expand_to_8::<4>((packed & 0xF) as u8);
+ [r, g, b, a]
+ }
+ }
+ }
+}
+
+#[inline(always)]
+fn palette_lookup(palette: &[u16], index: usize, format: TlutFormat) -> [u8; 4] {
+ let entry = palette.get(index).copied().unwrap_or(0);
+ tlut_entry_to_rgba(entry, format)
+}
+
+#[multiversion(targets = "simd")]
+fn decode_ci4(
+ ram: &[u8],
+ desc: &TextureDescriptor,
+ rgba: &mut [u8],
+ w: usize,
+ h: usize,
+ palette: &[u16],
+ tlut_format: TlutFormat,
+) {
+ const BW: usize = 8;
+ const BH: usize = 8;
+ const BB: usize = 32;
+
+ let bcx = w.div_ceil(BW);
+ let bcy = h.div_ceil(BH);
+ if desc.ram_addr + bcx * bcy * BB > ram.len() {
+ tracing::warn!(addr = desc.ram_addr, w, h, "decode_ci4: texture OOB, skipping");
+ return;
+ }
+
+ let src = ram.as_ptr();
+ let dst = rgba.as_mut_ptr();
+
+ for by in 0..bcy {
+ let base_y = by * BH;
+ let th = BH.min(h - base_y);
+ for bx in 0..bcx {
+ let base_x = bx * BW;
+ let tw = BW.min(w - base_x);
+ let blk = desc.ram_addr + (by * bcx + bx) * BB;
+
+ for ty in 0..th {
+ for tx in 0..tw {
+ unsafe {
+ let byte = *src.add(blk + (ty * BW + tx) / 2);
+ let nibble = if tx & 1 == 0 { byte >> 4 } else { byte & 0x0F };
+ let pixel = self::palette_lookup(palette, nibble as usize, tlut_format);
+ std::ptr::write(dst.add(((base_y + ty) * w + base_x + tx) * 4).cast::<[u8; 4]>(), pixel);
+ }
+ }
+ }
+ }
+ }
+}
+
+#[multiversion(targets = "simd")]
+fn decode_ci8(
+ ram: &[u8],
+ desc: &TextureDescriptor,
+ rgba: &mut [u8],
+ w: usize,
+ h: usize,
+ palette: &[u16],
+ tlut_format: TlutFormat,
+) {
+ const BW: usize = 8;
+ const BH: usize = 4;
+ const BB: usize = 32;
+
+ let bcx = w.div_ceil(BW);
+ let bcy = h.div_ceil(BH);
+ if desc.ram_addr + bcx * bcy * BB > ram.len() {
+ tracing::warn!(addr = desc.ram_addr, w, h, "decode_ci8: texture OOB, skipping");
+ return;
+ }
+
+ let src = ram.as_ptr();
+ let dst = rgba.as_mut_ptr();
+
+ for by in 0..bcy {
+ let base_y = by * BH;
+ let th = BH.min(h - base_y);
+ for bx in 0..bcx {
+ let base_x = bx * BW;
+ let tw = BW.min(w - base_x);
+ let blk = desc.ram_addr + (by * bcx + bx) * BB;
+
+ for ty in 0..th {
+ for tx in 0..tw {
+ unsafe {
+ let index = *src.add(blk + ty * BW + tx);
+ let pixel = self::palette_lookup(palette, index as usize, tlut_format);
+ std::ptr::write(dst.add(((base_y + ty) * w + base_x + tx) * 4).cast::<[u8; 4]>(), pixel);
+ }
+ }
+ }
+ }
+ }
+}
+
+#[multiversion(targets = "simd")]
+fn decode_ci14(
+ ram: &[u8],
+ desc: &TextureDescriptor,
+ rgba: &mut [u8],
+ w: usize,
+ h: usize,
+ palette: &[u16],
+ tlut_format: TlutFormat,
+) {
+ const BW: usize = 4;
+ const BH: usize = 4;
+ const BB: usize = 32;
+
+ let bcx = w.div_ceil(BW);
+ let bcy = h.div_ceil(BH);
+ if desc.ram_addr + bcx * bcy * BB > ram.len() {
+ tracing::warn!(addr = desc.ram_addr, w, h, "decode_ci14: texture OOB, skipping");
+ return;
+ }
+
+ let src = ram.as_ptr();
+ let dst = rgba.as_mut_ptr();
+
+ for by in 0..bcy {
+ let base_y = by * BH;
+ let th = BH.min(h - base_y);
+ for bx in 0..bcx {
+ let base_x = bx * BW;
+ let tw = BW.min(w - base_x);
+ let blk = desc.ram_addr + (by * bcx + bx) * BB;
+
+ for ty in 0..th {
+ for tx in 0..tw {
+ unsafe {
+ let off = blk + (ty * BW + tx) * 2;
+ let hi = *src.add(off);
+ let lo = *src.add(off + 1);
+ // Bottom 14 bits index the palette; top 2 bits unused.
+ let index = (((hi as u16) << 8) | lo as u16) & 0x3FFF;
+ let pixel = self::palette_lookup(palette, index as usize, tlut_format);
+ std::ptr::write(dst.add(((base_y + ty) * w + base_x + tx) * 4).cast::<[u8; 4]>(), pixel);
+ }
+ }
+ }
+ }
+ }
+}
+
#[inline(always)]
fn build_dxt1_palette(c0: u16, c1: u16) -> [[u8; 4]; 4] {
let [r0, g0, b0, _] = self::rgb565_to_rgba(c0);
diff --git a/crates/gecko/src/flipper/gx/xf.rs b/crates/gecko/src/flipper/gx/xf.rs
index 99b021e..986038c 100644
--- a/crates/gecko/src/flipper/gx/xf.rs
+++ b/crates/gecko/src/flipper/gx/xf.rs
@@ -46,8 +46,8 @@ impl GraphicsProcessor {
let x = x - self.cur_scissor_offset_x as f32;
let y = y - self.cur_scissor_offset_y as f32;
- let far = offset_z / DEPTH_24_BIT_MAX;
- let near = far - scale_z / DEPTH_24_BIT_MAX;
+ let far = (offset_z / DEPTH_24_BIT_MAX).clamp(0.0, 1.0);
+ let near = (far - scale_z / DEPTH_24_BIT_MAX).clamp(0.0, 1.0);
self.cur_viewport = draw::Viewport {
x,