A GameCube/Wii emulator and debugger written in Rust.
1 staging upload for texture
Layle3f8c6205de96
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rsindex 03c3c9a..ac68415 100644--- a/crates/backend-wgpu/src/action.rs+++ b/crates/backend-wgpu/src/action.rs@@ -158,7 +158,7 @@ impl GxRenderer {height: *height,depth_or_array_layers: 1,};- let copy_layout = wgpu::TexelCopyBufferLayout {+ let direct_copy_layout = wgpu::TexelCopyBufferLayout {offset: 0,bytes_per_row: Some(*width * 4),rows_per_image: None,@@ -172,11 +172,20 @@ impl GxRenderer {self.return_to_pool(entry.texture, entry.view);}- if let Some((cached_fmt, cached_tex, _)) = self.texture_cache.get_mut(&tid) {+ if let Some((_, cached_tex, _)) = self.texture_cache.get(&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;+ let cached_tex = cached_tex.clone();+ let staged = self.stage_texture_upload(device, &cached_tex, rgba, *width, *height);++ if !staged {+ queue.write_texture(cached_tex.as_image_copy(), rgba, direct_copy_layout, copy_size);+ }++ if let Some((cached_fmt, _, _)) = self.texture_cache.get_mut(&tid) {+ *cached_fmt = *fmt;+ }+return;}}@@ -202,7 +211,10 @@ impl GxRenderer {})});- queue.write_texture(tex.as_image_copy(), rgba, copy_layout, copy_size);+ let staged = self.stage_texture_upload(device, &tex, rgba, *width, *height);+ if !staged {+ queue.write_texture(tex.as_image_copy(), rgba, direct_copy_layout, copy_size);+ }let view = tex.create_view(&Default::default());// Cached bind groups still hold the old TextureView for thisdiff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rsindex 2c0e599..4ca3b73 100644--- a/crates/backend-wgpu/src/lib.rs+++ b/crates/backend-wgpu/src/lib.rs@@ -239,6 +239,18 @@ pub struct GxRenderer {// Region-scoped EFB clear.pub(crate) efb_clear: clear::EfbClear,pub(crate) pending_command_buffers: Vec<wgpu::CommandBuffer>,+ /// Shared staging buffer for `LoadTexture` uploads. Each upload appends+ /// padded RGBA into `texture_staging_scratch` and records a+ /// `copy_buffer_to_texture` into the persistent encoder; at submit time+ /// the whole scratch is shipped through one `write_buffer_with` call.+ pub(crate) texture_staging_buffer: wgpu::Buffer,+ pub(crate) texture_staging_capacity: u64,+ pub(crate) texture_staging_scratch: Vec<u8>,+ /// Largest scratch size observed within the current run. Used at submit+ /// boundaries to decide whether to grow [`Self::texture_staging_buffer`]+ /// for the next frame (so growth never invalidates a buffer the+ /// in-flight encoder still references).+ pub(crate) texture_staging_peak: u64,/// Persistent encoder accumulating GPU commands across operations within/// a frame.pub(crate) current_encoder: Option<wgpu::CommandEncoder>,@@ -420,6 +432,14 @@ impl GxRenderer {mapped_at_creation: false,});+ let initial_texture_staging_capacity: u64 = 4 * 1024 * 1024;+ let texture_staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {+ label: Some("gx_texture_staging"),+ size: initial_texture_staging_capacity,+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC,+ mapped_at_creation: false,+ });+// Create fixed-size EFB with 4x MSAA.// Resolved (1x) color: CopyXfb reads from here.let efb_texture = device.create_texture(&wgpu::TextureDescriptor {@@ -769,6 +789,10 @@ impl GxRenderer {xfb_copy_uniform_buffer,efb_clear,pending_command_buffers: Vec::with_capacity(8),+ texture_staging_buffer,+ texture_staging_capacity: initial_texture_staging_capacity,+ texture_staging_scratch: Vec::with_capacity(initial_texture_staging_capacity as usize / 4),+ texture_staging_peak: 0,current_encoder: None,draw_bufs_write_pending: false,xfb_copy_uniform_write_pending: false,@@ -795,6 +819,22 @@ impl GxRenderer {}pub(crate) fn submit_pending(&mut self, queue: &wgpu::Queue) {+ // Ship any staged texture-upload bytes through ONE `write_buffer_with`+ // before finishing the encoder. wgpu orders queue writes ahead of+ // submitted commands, so the encoder's `copy_buffer_to_texture`+ // commands (recorded earlier with offsets into this buffer) will+ // read the freshly-written data.+ if !self.texture_staging_scratch.is_empty()+ && let Some(size) = std::num::NonZeroU64::new(self.texture_staging_scratch.len() as u64)+ {+ let mut view = queue+ .write_buffer_with(&self.texture_staging_buffer, 0, size)+ .expect("texture staging buffer too small");+ view.copy_from_slice(&self.texture_staging_scratch);+ drop(view);+ self.texture_staging_scratch.clear();+ }+if let Some(encoder) = self.current_encoder.take() {self.pending_command_buffers.push(encoder.finish());}@@ -810,6 +850,89 @@ impl GxRenderer {self.efb_depth_resolve_uniform_write_pending = false;}+ /// Append `rgba` (W*H*4 tight, no row padding) into+ /// [`Self::texture_staging_scratch`] with COPY_BYTES_PER_ROW_ALIGNMENT+ /// (256 bytes) row padding, and record a `copy_buffer_to_texture` into the+ /// persistent encoder that copies the staged bytes into `dest`.+ /// Returns `false` if the upload won't fit the current staging capacity.+ /// Caller should fall back to `queue.write_texture` for that upload+ /// and rely on the next submit boundary to grow the buffer via+ /// [`Self::maybe_grow_texture_staging`].+ pub(crate) fn stage_texture_upload(+ &mut self,+ device: &wgpu::Device,+ dest: &wgpu::Texture,+ rgba: &[u8],+ width: u32,+ height: u32,+ ) -> bool {+ const ROW_ALIGNMENT: u64 = 256;+ const COPY_BUFFER_ALIGNMENT: u64 = 4;+ let row_bytes = (width as u64) * 4;+ let padded_row_bytes = align_up(row_bytes, ROW_ALIGNMENT);+ let upload_size = padded_row_bytes * height as u64;+ let offset = align_up(self.texture_staging_scratch.len() as u64, COPY_BUFFER_ALIGNMENT);+ let end = offset + upload_size;+ self.texture_staging_peak = self.texture_staging_peak.max(end);+ if end > self.texture_staging_capacity {+ return false;+ }++ // Pad to aligned start offset, then append each row + per-row padding.+ self.texture_staging_scratch.resize(offset as usize, 0);+ let pad = (padded_row_bytes - row_bytes) as usize;+ for row in 0..height as usize {+ let src_start = row * row_bytes as usize;+ let src_end = src_start + row_bytes as usize;+ self.texture_staging_scratch+ .extend_from_slice(&rgba[src_start..src_end]);+ if pad > 0 {+ let new_len = self.texture_staging_scratch.len() + pad;+ self.texture_staging_scratch.resize(new_len, 0);+ }+ }++ let mut encoder = self.take_or_create_encoder(device);+ encoder.copy_buffer_to_texture(+ wgpu::TexelCopyBufferInfo {+ buffer: &self.texture_staging_buffer,+ layout: wgpu::TexelCopyBufferLayout {+ offset,+ bytes_per_row: Some(padded_row_bytes as u32),+ rows_per_image: None,+ },+ },+ dest.as_image_copy(),+ wgpu::Extent3d {+ width,+ height,+ depth_or_array_layers: 1,+ },+ );+ self.current_encoder = Some(encoder);+ true+ }++ /// If the per-frame staging peak has exceeded the buffer's capacity,+ /// reallocate (next power of two of peak). Safe to call only when no+ /// encoder commands reference the existing buffer.+ pub(crate) fn maybe_grow_texture_staging(&mut self, device: &wgpu::Device) {+ if self.texture_staging_peak <= self.texture_staging_capacity {+ return;+ }+ let new_cap = self+ .texture_staging_peak+ .next_power_of_two()+ .max(self.texture_staging_peak);+ self.texture_staging_buffer = device.create_buffer(&wgpu::BufferDescriptor {+ label: Some("gx_texture_staging"),+ size: new_cap,+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC,+ mapped_at_creation: false,+ });+ self.texture_staging_capacity = new_cap;+ }+#[allow(clippy::too_many_arguments)]pub(crate) fn clear_efb_region(&mut self,diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rsindex 30d0783..3b5d108 100644--- a/crates/backend-wgpu/src/render.rs+++ b/crates/backend-wgpu/src/render.rs@@ -591,6 +591,9 @@ impl GxRenderer {encoder.pop_debug_group();self.current_encoder = Some(encoder);self.submit_pending(queue);+ // The staging buffer's in-flight references have just been submitted;+ // now's the safe moment to grow it if this frame exceeded capacity.+ self.maybe_grow_texture_staging(device);self.xfb_has_content = true;}