A GameCube/Wii emulator and debugger written in Rust.
palette cache fix. final fantasy crystal bearers menu looks perfect besides missing sky
Layle05a9fc02f9b9
diff --git a/.gitignore b/.gitignoreindex 1133b92..c8d6050 100644--- a/.gitignore+++ b/.gitignore@@ -8,4 +8,6 @@ private/*.logout/__pycache__/-fs/\ No newline at end of file+fs/+screenshots/+textures/\ No newline at end of filediff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rsindex 438760a..b930ae0 100644--- a/crates/backend-wgpu/src/action.rs+++ b/crates/backend-wgpu/src/action.rs@@ -1,5 +1,5 @@use crate::pipeline::PipelineKey;-use crate::{BindGroupCacheKey, DrawUniforms, FrameUniforms, GpuVertex, GxRenderer, SamplerKey, helpers, ram_addr_of};+use crate::{BindGroupCacheKey, DrawUniforms, FrameUniforms, GpuVertex, GxRenderer, SamplerKey, helpers};use encase::{ShaderType as _, UniformBuffer};use gecko::flipper::gx::regs::{MagFilter, MinFilter, WrapMode};use gecko::host::{DrawData, GxAction};@@ -76,7 +76,10 @@ impl GxRenderer {fmt,rgba,} => {- let texture_label = format!("gx_tex id={:#018x} fmt={:?} size={}x{}", *id, *fmt, *width, *height);+ let texture_label = format!(+ "gx_tex addr={:#010x}/{:08x} fmt={:?} size={}x{}",+ id.ram_addr, id.variant, *fmt, *width, *height+ );let tex = device.create_texture(&wgpu::TextureDescriptor {label: Some(&texture_label),size: wgpu::Extent3d {@@ -118,10 +121,8 @@ impl GxRenderer {// A fresh RAM upload means the game wrote this address// after a prior EFB copy. Return the stale GPU copy to// its pool and let the RAM-decoded entry win. EFB copies- // are keyed by the bare ram_addr, so strip the TLUT- // variant bits before looking up.- let ram_addr = ram_addr_of(tid);- if let Some((old_tex, old_view)) = self.efb_copy_cache.remove(&ram_addr) {+ // are keyed by bare ram_addr (no TLUT variant).+ if let Some((old_tex, old_view)) = self.efb_copy_cache.remove(&tid.ram_addr) {self.return_to_pool(old_tex, old_view);}@@ -390,10 +391,11 @@ impl GxRenderer {for slot in 0..8 {if let Some(tid) = &bg_key.tex_keys[slot] {- let ram_addr = ram_addr_of(*tid);+ // EFB copies (keyed by bare ram_addr) win over the+ // RAM-decoded `texture_cache` (keyed by full TextureKey).let view = self.efb_copy_cache- .get(&ram_addr)+ .get(&tid.ram_addr).map(|(_, v)| v).or_else(|| self.texture_cache.get(tid).map(|(_, _, v)| v));if let Some(view) = view {diff --git a/crates/backend-wgpu/src/dump.rs b/crates/backend-wgpu/src/dump.rsindex 5e26dbb..5b6efe7 100644--- a/crates/backend-wgpu/src/dump.rs+++ b/crates/backend-wgpu/src/dump.rs@@ -27,7 +27,7 @@ impl GxRenderer {if tex.format() != wgpu::TextureFormat::Rgba8Unorm {tracing::warn!(- cache_id = *cache_id,+ cache_id = ?cache_id,wgpu_fmt = ?tex.format(),"dump_textures: skipping non-Rgba8Unorm texture");@@ -75,7 +75,7 @@ impl GxRenderer {submission_index: None,timeout: Some(Duration::from_secs(5)),}) {- tracing::warn!(cache_id = *cache_id, ?err, "dump_textures: device poll failed");+ tracing::warn!(cache_id = ?cache_id, ?err, "dump_textures: device poll failed");continue;}@@ -92,9 +92,11 @@ impl GxRenderer {}staging.unmap();- let ram_addr = (*cache_id) as u32;- let variant = (*cache_id >> 32) as u32;- let filename = format!("{millis}_{ram_addr:08X}_{variant:08X}_{fmt:?}_{width}x{height}.png");+ let filename = format!(+ "{millis}_{ram_addr:08X}_{variant:08X}_{fmt:?}_{width}x{height}.png",+ ram_addr = cache_id.ram_addr,+ variant = cache_id.variant,+ );let path: PathBuf = dir.join(filename);capture::save_png_async(path, CapturedFrame { width, height, rgba }, false);saved += 1;diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rsindex 9be22c2..5e8633c 100644--- a/crates/backend-wgpu/src/lib.rs+++ b/crates/backend-wgpu/src/lib.rs@@ -14,6 +14,7 @@ use gecko::flipper::gx::draw::{Scissor, TextureFormat, Viewport};use gecko::flipper::gx::regs::{AlphaCompare, BlendMode, CompareFunc, CullMode, MagFilter, MinFilter, WrapMode, ZMode};#[cfg(feature = "efb-writeback")]use gecko::host::EfbWriteback;+use gecko::host::TextureKey;use glam::Mat4;use pipeline::PipelineKey;use rustc_hash::FxHashMap;@@ -42,13 +43,6 @@ pub(crate) fn align_up(value: u64, alignment: u64) -> u64 {type SamplerKey = (WrapMode, WrapMode, MagFilter, MinFilter);-pub(crate) type TextureKey = u64;--#[inline(always)]-pub(crate) fn ram_addr_of(key: TextureKey) -> Address {- key as Address-}-#[derive(Clone, PartialEq, Eq, Hash)]pub(crate) struct BindGroupCacheKey {tex_keys: [Option<TextureKey>; 8],diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rsindex 41ef1f0..7caa64e 100644--- a/crates/backend-wgpu/src/render.rs+++ b/crates/backend-wgpu/src/render.rs@@ -1,4 +1,4 @@-use crate::{FrameUniforms, GpuVertex, GxRenderer, align_up, ram_addr_of};+use crate::{FrameUniforms, GpuVertex, GxRenderer, align_up};use encase::ShaderType as _;use gecko::common::Address;#[cfg(feature = "efb-writeback")]@@ -283,7 +283,7 @@ impl GxRenderer {self.return_to_pool(old_tex, old_view);}self.bind_group_cache- .retain(|key, _| !key.tex_keys.iter().any(|k| k.map(ram_addr_of) == Some(dest_addr)));+ .retain(|key, _| !key.tex_keys.iter().any(|k| k.map(|t| t.ram_addr) == Some(dest_addr)));let (tex, view) = self.efb_copy_pool@@ -414,7 +414,7 @@ impl GxRenderer {self.return_to_pool(old_tex, old_view);}self.bind_group_cache- .retain(|key, _| !key.tex_keys.iter().any(|k| k.map(ram_addr_of) == Some(dest_addr)));+ .retain(|key, _| !key.tex_keys.iter().any(|k| k.map(|t| t.ram_addr) == Some(dest_addr)));let (tex, view) = self.efb_depth_pooldiff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rsindex d320c2c..12cf5c1 100644--- a/crates/gecko/src/flipper/gx.rs+++ b/crates/gecko/src/flipper/gx.rs@@ -15,7 +15,7 @@ use crate::flipper::gx::draw::Matrix4;use crate::flipper::gx::regs::{AlphaCompare, BlendMode, TevAlphaEnv, TevColorEnv, TevRegisterH, TevRegisterL, ZMode};#[cfg(feature = "efb-writeback")]use crate::host::EfbWriteback;-use crate::host::{GxAction, RenderSink, XfbPart};+use crate::host::{GxAction, RenderSink, TextureKey, XfbPart};use crate::mmio::Mmio;use crate::system::{System, SystemId};use rustc_hash::FxHashMap;@@ -70,11 +70,11 @@ pub struct GraphicsProcessor {// XFB copies accumulated since the last vblank. `present_xfb()` drains// this at each field boundary to emit a PresentXfb action.pub xfb_copies: Vec<XfbCopy>,- // Hash of the raw texture data at each (ram_addr, tlut_variant) cache id;- // used to detect when texture content changes and avoid redundant- // decodes + LoadTexture sends. Keyed by the same u64 cache id sent to- // the renderer in [`GxAction::LoadTexture`].- pub texture_hashes: FxHashMap<u64, u64>,+ // Hash of the raw texture data at each cache key; used to detect when+ // texture content changes and avoid redundant decodes + LoadTexture+ // sends. Keyed by the same `TextureKey` sent to the renderer in+ // [`GxAction::LoadTexture`].+ pub texture_hashes: FxHashMap<TextureKey, u64>,// Receiver for encoded EFB-to-texture bytes coming back from the// renderer worker. `efb_copy` drains this synchronously right after// emitting the copy action, so the next FIFO command in the same burstdiff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rsindex 5d3c08a..6cbe241 100644--- a/crates/gecko/src/flipper/gx/bp.rs+++ b/crates/gecko/src/flipper/gx/bp.rs@@ -1,7 +1,7 @@use super::constants::*;use super::regs::*;use super::{GraphicsProcessor, draw, texture};-use crate::host::{GxAction, RenderSink};+use crate::host::{GxAction, RenderSink, TextureKey};use crate::mmio::{RamView, RamViewMut};#[cfg(feature = "efb-writeback")]use std::time::Duration;@@ -237,12 +237,14 @@ impl GraphicsProcessor {let tlut = self.cur_tluts[slot];let palette = slot_palette(&self.palette_mem, tlut.tmem_offset);- // Cache id mixes the bound TLUT identity into the high 32 bits so- // the same CI* texture bound with two different palettes (FFCC- // title logo binds the same RAM bytes via slot 0 RGB5A3@512 and- // slot 1 IA8@528 every frame) doesn't clobber a single GPU side- // entry on each rebind.- let cache_id = self::texture_cache_id(ram_addr, format, tlut);+ // Cache id mixes a hash of the bound palette content + tlut identity+ // into the high 32 bits so each distinct palette state for a CI*+ // texture lands in its own renderer cache slot. Without that, games+ // (FFCC title logo) that load several palettes per frame at the same+ // tmem_offset would silently let the latest load clobber the earlier+ // ones, since bind groups are built lazily and resolve to whichever+ // GPU texture is current at render-pass time.+ let cache_id = self::texture_cache_id(ram_addr, format, tlut, palette);// Resolve the texture's raw bytes once, against MEM1 or MEM2. If the// address doesn't fall in either bank, leave the slot's last binding@@ -500,10 +502,7 @@ impl GraphicsProcessor {// Writeback overwrites RAM, so drop every cached hash that// points at this ram_addr (any TLUT variant) to force a redecode.#[cfg(feature = "efb-writeback")]- {- let dest_addr_lo = dest_addr as u64;- self.texture_hashes.retain(|k, _| (*k & 0xFFFFFFFF) != dest_addr_lo);- }+ self.texture_hashes.retain(|k, _| k.ram_addr != dest_addr);let _ = ram;// With `efb-writeback`: block until the renderer finishes the@@ -574,30 +573,50 @@ fn slot_palette(palette_mem: &[u16], tmem_offset: u16) -> &[u16] {palette_mem.get(base..).unwrap_or(&[])}-/// Mint the `(ram_addr, tlut_variant)` cache key sent to the renderer.-/// Low 32 bits = ram_addr, high 32 bits = TLUT identity (0 for non-paletted).-/// Variant fits in 12 bits (10 for tmem_offset, 2 for format) but we leave-/// room for future expansion.+/// Mint the renderer cache key for the given texture binding.+///+/// `variant` is `0` for non-paletted formats. For paletted (CI*) formats it's+/// a 32-bit hash of `(palette content, tlut.format, tmem_offset)` so the+/// same RAM index stream sampled through different palettes lands+/// in distinct cache slots. FFCC's title-logo fade animation alternates+/// between palettes at a fixed `tmem_offset`; without the variant in the+/// key, later uploads would silently overwrite earlier ones in the+/// renderer's cache and bind groups built at render-pass time would all+/// resolve to whichever decode landed last.#[inline(always)]-fn texture_cache_id(ram_addr: usize, format: draw::TextureFormat, tlut: draw::TlutRef) -> u64 {- let lo = (ram_addr as u32) as u64;- let hi = if format.is_paletted() {- ((tlut.format as u32) << 16 | (tlut.tmem_offset as u32 & 0x3FF)) as u64+fn texture_cache_id(ram_addr: usize, format: draw::TextureFormat, tlut: draw::TlutRef, palette: &[u16]) -> TextureKey {+ let variant = 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]);+ let mut h = twox_hash::xxhash3_64::Hasher::oneshot(palette_bytes);+ h ^= tlut.format as u64;+ h ^= (tlut.tmem_offset as u64) << 8;+ // Fold the 64-bit hash to 32 bits.+ (h ^ (h >> 32)) as u32} else {0};- (hi << 32) | lo+ TextureKey {+ ram_addr: ram_addr as u32,+ variant,+ }}/// Returns `true` when the raw texture data in `tex` differs from the last-/// hash recorded for this cache id. `tex` is the already resolved slice for+/// hash recorded for this cache key. `tex` is the already resolved slice for/// the texture (caller has already mapped MEM1/MEM2). The hash is keyed by-/// cache_id so paletted textures bound with multiple TLUTs are tracked+/// `cache_id` so paletted textures bound with multiple TLUTs are tracked/// independently.fn texture_data_changed(- hashes: &mut rustc_hash::FxHashMap<u64, u64>,+ hashes: &mut rustc_hash::FxHashMap<TextureKey, u64>,tex: &[u8],- cache_id: u64,+ cache_id: TextureKey,palette: &[u16],tlut: draw::TlutRef,format: draw::TextureFormat,diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rsindex 14839ac..4831ffa 100644--- a/crates/gecko/src/host.rs+++ b/crates/gecko/src/host.rs@@ -4,6 +4,27 @@ use crate::flipper::gx::regs::{AlphaCompare, BlendMode, ChanCtrl, CullMode, MagF#[cfg(not(target_arch = "wasm32"))]use std::path::PathBuf;+/// Renderer-side texture cache key. Identifies one unique decoded RGBA+/// texture: same `ram_addr` sampled through different palettes must occupy+/// distinct cache slots, otherwise alternating palette uploads (FFCC fade /+/// settle animations swap palette content at a fixed `tmem_offset` each+/// frame) silently overwrite each other and bind groups built lazily at+/// render-pass time all resolve to whichever decode landed last.+///+/// `variant` is `0` for non-paletted formats and a 32-bit hash of+/// `(palette content, tlut.format, tmem_offset)` for paletted ones.+#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]+pub struct TextureKey {+ pub ram_addr: Address,+ pub variant: u32,+}++impl TextureKey {+ pub const fn non_paletted(ram_addr: Address) -> Self {+ Self { ram_addr, variant: 0 }+ }+}+#[derive(Debug)]pub enum GxAction {// XF@@ -22,14 +43,8 @@ pub enum GxAction {/// Upload pre-decoded texture data. Emitted when texture content at a/// given address changes (detected by hash).- ///- /// `id` is the renderer side cache key. The low 32 bits are the source- /// `ram_addr`; for paletted (CI*) textures the high 32 bits encode the- /// bound TLUT (format + tmem_offset), so the same RAM-resident index- /// stream bound with two different palettes occupies two distinct cache- /// entries instead of clobbering each other.LoadTexture {- id: u64,+ id: TextureKey,width: u32,height: u32,fmt: TextureFormat,@@ -51,7 +66,7 @@ pub enum GxAction {/// the cache key used in [`Self::LoadTexture`].SetTexture {slot: usize,- id: u64,+ id: TextureKey,wrap_s: WrapMode,wrap_t: WrapMode,mag_filter: MagFilter,