Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

texture copy

Layled3fcaa8cabd2
diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rs
index b4f3e41..0e2e4e7 100644
--- a/crates/backend-wgpu/src/render.rs
+++ b/crates/backend-wgpu/src/render.rs
@@ -2,7 +2,7 @@ use crate::{FrameUniforms, GxRenderer};
use crate::{GpuVertex, align_up};
use encase::ShaderType as _;
use gecko::flipper::gx::draw::EfbCopyCmd;
-use gecko::host::XfbPart;
+use gecko::host::{TextureId, XfbPart};
impl GxRenderer {
pub(crate) fn upload_buffers(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, frame_uniform_bytes: &[u8]) {
@@ -40,6 +40,59 @@ impl GxRenderer {
}
pub(crate) fn execute_efb_copy(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, copy: &EfbCopyCmd) {
+ // Snapshot the EFB region into texture_cache so that a later
+ // SetTexture referencing dest_addr finds the GPU-side copy
+ // instead of stale/unwritten RAM data.
+ let width = copy.src_w.min(crate::EFB_WIDTH.saturating_sub(copy.src_x));
+ let height = copy.src_h.min(crate::EFB_HEIGHT.saturating_sub(copy.src_y));
+ if width > 0 && height > 0 {
+ let id = TextureId(copy.dest_addr);
+ let tex = device.create_texture(&wgpu::TextureDescriptor {
+ label: Some("efb_copy_tex"),
+ size: wgpu::Extent3d {
+ width,
+ height,
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: self.surface_format,
+ usage: wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ });
+ let mut encoder = device.create_command_encoder(&Default::default());
+ encoder.copy_texture_to_texture(
+ wgpu::TexelCopyTextureInfo {
+ texture: &self.efb_texture,
+ mip_level: 0,
+ origin: wgpu::Origin3d {
+ x: copy.src_x,
+ y: copy.src_y,
+ z: 0,
+ },
+ aspect: wgpu::TextureAspect::default(),
+ },
+ wgpu::TexelCopyTextureInfo {
+ texture: &tex,
+ mip_level: 0,
+ origin: wgpu::Origin3d::ZERO,
+ aspect: wgpu::TextureAspect::default(),
+ },
+ wgpu::Extent3d {
+ width,
+ height,
+ depth_or_array_layers: 1,
+ },
+ );
+ queue.submit([encoder.finish()]);
+
+ let view = tex.create_view(&Default::default());
+ self.bind_group_cache
+ .retain(|key, _| !key.tex_keys.iter().any(|k| *k == Some(id)));
+ self.texture_cache.insert(id, (tex, view));
+ }
+
if copy.clear {
self.efb_clear.clear_region(
device,
diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rs
index d8a2f72..acac938 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 std::collections::HashMap;
+use std::collections::{HashMap, HashSet};
use crate::flipper::gx::constants::{BP_REG_SIZE, CP_REG_SIZE, XF_MEM_SIZE};
use crate::flipper::gx::draw::Matrix4;
@@ -58,6 +58,11 @@ pub struct GraphicsProcessor {
// Hash of the raw texture data at each RAM address; used to detect when
// texture content changes and avoid redundant decodes + LoadTexture sends.
pub texture_hashes: HashMap<u32, u64>,
+ // RAM addresses that hold GPU-side EFB copy textures. When a texture is
+ // bound at one of these addresses, the renderer already has the correct
+ // content, so we skip the hash check and LoadTexture to avoid overwriting
+ // the GPU copy with stale RAM data.
+ pub efb_copy_addrs: HashSet<u32>,
}
/// A single EFB-to-XFB copy, stored until `present_xfb` computes the layout.
@@ -98,6 +103,7 @@ impl GraphicsProcessor {
cur_scissor_offset_y: 0,
xfb_copies: Vec::new(),
texture_hashes: HashMap::new(),
+ efb_copy_addrs: HashSet::new(),
}
}
diff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rs
index 994dd63..0610279 100644
--- a/crates/gecko/src/flipper/gx/bp.rs
+++ b/crates/gecko/src/flipper/gx/bp.rs
@@ -166,25 +166,30 @@ impl GraphicsProcessor {
"texture descriptor updated"
);
- let changed = self::texture_data_changed(&mut self.texture_hashes, ram, ram_addr, width, height, format);
-
- if changed {
- let desc = draw::TextureDescriptor {
- ram_addr,
- width,
- height,
- format,
- wrap_s: mode0.wrap_s(),
- wrap_t: mode0.wrap_t(),
- mag_filter: mode0.mag_filter(),
- min_filter: mode0.min_filter(),
- };
- renderer.exec(GxAction::LoadTexture {
- id,
- width,
- height,
- rgba: texture::decode_to_rgba(ram, &desc),
- });
+ // If the renderer already holds a GPU-side EFB copy for this
+ // address, skip the hash check and LoadTexture so we don't
+ // overwrite it with stale/unwritten RAM data.
+ if !self.efb_copy_addrs.contains(&(ram_addr as u32)) {
+ let changed = self::texture_data_changed(&mut self.texture_hashes, ram, ram_addr, width, height, format);
+
+ if changed {
+ let desc = draw::TextureDescriptor {
+ ram_addr,
+ width,
+ height,
+ format,
+ wrap_s: mode0.wrap_s(),
+ wrap_t: mode0.wrap_t(),
+ mag_filter: mode0.mag_filter(),
+ min_filter: mode0.min_filter(),
+ };
+ renderer.exec(GxAction::LoadTexture {
+ id,
+ width,
+ height,
+ rgba: texture::decode_to_rgba(ram, &desc),
+ });
+ }
}
self.cur_textures[slot] = Some(draw::TextureDescriptor {
@@ -331,7 +336,10 @@ impl GraphicsProcessor {
clear_z,
});
} else {
- // Texture copy (non-XFB).
+ // Texture copy (non-XFB). Track the destination so that
+ // snapshot_texture skips the hash check / LoadTexture for
+ // this address (the renderer holds the real content).
+ self.efb_copy_addrs.insert(dest_addr);
let efb_cmd = draw::EfbCopyCmd {
src_x,
src_y,