Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

reuse textures in LoadTexture

Layle2d917420df12
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index 691edb5..6c57603 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -110,47 +110,17 @@ impl GxRenderer {
fmt,
rgba,
} => {
- 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 {
- width: *width,
- height: *height,
- depth_or_array_layers: 1,
- },
- mip_level_count: 1,
- sample_count: 1,
- dimension: wgpu::TextureDimension::D2,
- format: wgpu::TextureFormat::Rgba8Unorm,
- usage: wgpu::TextureUsages::TEXTURE_BINDING
- | wgpu::TextureUsages::COPY_DST
- | wgpu::TextureUsages::COPY_SRC,
- view_formats: &[],
- });
- queue.write_texture(
- tex.as_image_copy(),
- rgba,
- wgpu::TexelCopyBufferLayout {
- offset: 0,
- bytes_per_row: Some(*width * 4),
- rows_per_image: None,
- },
- wgpu::Extent3d {
- width: *width,
- height: *height,
- depth_or_array_layers: 1,
- },
- );
- let view = tex.create_view(&Default::default());
-
- // Invalidate bind groups that referenced the old entry for
- // this exact cache id (variant-specific).
let tid = *id;
- self.bind_group_cache
- .retain(|key, _| !key.tex_keys.iter().any(|k| *k == Some(tid)));
+ let copy_size = wgpu::Extent3d {
+ width: *width,
+ height: *height,
+ depth_or_array_layers: 1,
+ };
+ let copy_layout = wgpu::TexelCopyBufferLayout {
+ offset: 0,
+ bytes_per_row: Some(*width * 4),
+ rows_per_image: None,
+ };
// A fresh RAM upload means the game wrote this address
// after a prior EFB copy. Return the stale GPU copy to
@@ -160,7 +130,48 @@ impl GxRenderer {
self.return_to_pool(old_tex, old_view);
}
- self.texture_cache.insert(*id, (*fmt, tex, view));
+ if let Some((cached_fmt, cached_tex, _)) = self.texture_cache.get_mut(&tid) {
+ let size = cached_tex.size();
+ if size.width == *width && size.height == *height {
+ queue.write_texture(cached_tex.as_image_copy(), rgba, copy_layout, copy_size);
+ *cached_fmt = *fmt;
+ return;
+ }
+ }
+
+ let pooled = self.texture_pool.get_mut(&(*width, *height)).and_then(|v| v.pop());
+
+ let tex = pooled.unwrap_or_else(|| {
+ let texture_label = format!(
+ "gx_tex addr={:#010x}/{:08x} fmt={:?} size={}x{}",
+ id.ram_addr, id.variant, *fmt, *width, *height
+ );
+ device.create_texture(&wgpu::TextureDescriptor {
+ label: Some(&texture_label),
+ size: copy_size,
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Rgba8Unorm,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING
+ | wgpu::TextureUsages::COPY_DST
+ | wgpu::TextureUsages::COPY_SRC,
+ view_formats: &[],
+ })
+ });
+
+ queue.write_texture(tex.as_image_copy(), rgba, copy_layout, copy_size);
+ let view = tex.create_view(&Default::default());
+
+ // Cached bind groups still hold the old TextureView for this
+ // key. Drop them so they get rebuilt against the new one.
+ self.bind_group_cache
+ .retain(|key, _| !key.tex_keys.iter().any(|k| *k == Some(tid)));
+
+ let prior = self.texture_cache.insert(tid, (*fmt, tex, view));
+ if let Some((_, old_tex, _)) = prior {
+ self.return_load_texture_to_pool(old_tex);
+ }
}
GxAction::InvalidateCaches => {
self.flush_pending_draws(device, queue);
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index 0112621..1fe5e0d 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -131,6 +131,8 @@ pub struct GxRenderer {
pub(crate) efb_needs_clear: bool,
pub(crate) sampler_cache: FxHashMap<(WrapMode, WrapMode, MagFilter, MinFilter), wgpu::Sampler>,
pub(crate) texture_cache: FxHashMap<TextureKey, (TextureFormat, wgpu::Texture, wgpu::TextureView)>,
+ // Retired LoadTexture allocations grouped by (w, h).
+ pub(crate) texture_pool: FxHashMap<(u32, u32), Vec<wgpu::Texture>>,
// GPU textures holding EFB copy results, checked before `texture_cache` on every texture bind.
pub(crate) efb_copy_cache: FxHashMap<Address, (wgpu::Texture, wgpu::TextureView)>,
// Retired EFB-copy textures grouped by size, popped by the next copy to skip reallocating.
@@ -664,6 +666,7 @@ impl GxRenderer {
m
},
texture_cache: FxHashMap::default(),
+ texture_pool: FxHashMap::default(),
efb_copy_cache: FxHashMap::default(),
efb_copy_pool: FxHashMap::default(),
efb_copy_pipeline,
diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rs
index 51e0010..65a7af4 100644
--- a/crates/backend-wgpu/src/render.rs
+++ b/crates/backend-wgpu/src/render.rs
@@ -414,6 +414,24 @@ impl GxRenderer {
}
}
+ pub(crate) fn return_load_texture_to_pool(&mut self, tex: wgpu::Texture) {
+ const PER_BUCKET_CAP: usize = 8;
+
+ debug_assert_eq!(tex.format(), wgpu::TextureFormat::Rgba8Unorm);
+ debug_assert_eq!(tex.mip_level_count(), 1);
+ debug_assert_eq!(tex.sample_count(), 1);
+ debug_assert_eq!(tex.dimension(), wgpu::TextureDimension::D2);
+ debug_assert!(tex.usage().contains(
+ wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC,
+ ));
+
+ let size = tex.size();
+ let bucket = self.texture_pool.entry((size.width, size.height)).or_default();
+ if bucket.len() < PER_BUCKET_CAP {
+ bucket.push(tex);
+ }
+ }
+
/// Resolves the 4x MSAA `efb_depth_view` into a single-sample R32Float texture keyed by `dest_addr`.
pub(crate) fn cache_efb_copy_depth(
&mut self,
diff --git a/crates/backend-wgpu/src/shader_specialization.rs b/crates/backend-wgpu/src/shader_specialization.rs
index 99f0802..1c3a715 100644
--- a/crates/backend-wgpu/src/shader_specialization.rs
+++ b/crates/backend-wgpu/src/shader_specialization.rs
@@ -43,7 +43,7 @@ impl ShaderKey {
let op = alpha_cmp.op();
let always_pass =
comp0 == CompareFunc::Always && comp1 == CompareFunc::Always && matches!(op, AlphaOp::And | AlphaOp::Or);
-
+
let mut stages = [StageKey::default(); 16];
for i in 0..num_tev_stages as usize {
stages[i] = StageKey {