A GameCube/Wii emulator and debugger written in Rust.
texture decode via compute shaders
Layleab0926e7c3f2
diff --git a/.gitignore b/.gitignoreindex 76747d0..1dc6dac 100644--- a/.gitignore+++ b/.gitignore@@ -20,4 +20,5 @@ system/*.sram*.dffdocs/*.md-internal/\ No newline at end of file+internal/+texturepacks/diff --git a/crates/app/src/banner.rs b/crates/app/src/banner.rsindex 0cd85cf..51f9518 100644--- a/crates/app/src/banner.rs+++ b/crates/app/src/banner.rs@@ -1,5 +1,4 @@-use gecko::flipper::gx::draw::{TextureDescriptor, TextureFormat, TlutFormat};-use gecko::flipper::gx::regs::{MagFilter, MinFilter, WrapMode};+use gecko::flipper::gx::draw::{TextureFormat, TlutFormat};use gecko::flipper::gx::texture;pub fn decode(encoded: image::banner::Texture<'_>) -> Option<Vec<u8>> {@@ -39,15 +38,12 @@ pub fn decode(encoded: image::banner::Texture<'_>) -> Option<Vec<u8>> {None => (Vec::new(), TlutFormat::IA8),};- let desc = TextureDescriptor {- ram_addr: 0,- width: encoded.width,- height: encoded.height,+ Some(texture::decode_to_rgba(+ pixels,+ encoded.width,+ encoded.height,format,- wrap_s: WrapMode::Clamp,- wrap_t: WrapMode::Clamp,- mag_filter: MagFilter::Nearest,- min_filter: MinFilter::Nearest,- };- Some(texture::decode_to_rgba(pixels, &desc, &palette, tlut))+ &palette,+ tlut,+ ))}diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rsindex 303e706..f1aa2d0 100644--- a/crates/backend-wgpu/src/action.rs+++ b/crates/backend-wgpu/src/action.rs@@ -270,7 +270,7 @@ impl GxRenderer {height,fmt,mip_levels,- rgba,+ data,} => {let tid = *id;let copy_size = wgpu::Extent3d {@@ -283,7 +283,7 @@ impl GxRenderer {.efb_copy_cache.get(&tid.ram_addr).is_some_and(|e| *mip_levels == 1 && e.matches(*fmt, *width, *height));- let reference_only = rgba.is_empty();+ let reference_only = data.is_none();assert!(!reference_only || keep_cached,@@ -307,9 +307,9 @@ impl GxRenderer {if let Some((_, cached_tex, _)) = self.texture_cache.get(&tid) {let size = cached_tex.size();if size.width == *width && size.height == *height && cached_tex.mip_level_count() == *mip_levels {- if !reference_only {+ if let Some(data) = data {let cached_tex = cached_tex.clone();- self.upload_texture_levels(device, queue, &cached_tex, rgba);+ self.decode_texture_levels(device, queue, &cached_tex, *fmt, data);}if let Some((cached_fmt, _, _)) = self.texture_cache.get_mut(&tid) {@@ -337,15 +337,13 @@ impl GxRenderer {sample_count: 1,dimension: wgpu::TextureDimension::D2,format: wgpu::TextureFormat::Rgba8Unorm,- usage: wgpu::TextureUsages::TEXTURE_BINDING- | wgpu::TextureUsages::COPY_DST- | wgpu::TextureUsages::COPY_SRC,+ usage: crate::LOAD_TEXTURE_USAGE,view_formats: &[],})});- if !reference_only {- self.upload_texture_levels(device, queue, &tex, rgba);+ if let Some(data) = data {+ self.decode_texture_levels(device, queue, &tex, *fmt, data);}let view = tex.create_view(&Default::default());@@ -406,10 +404,7 @@ impl GxRenderer {sample_count: 1,dimension: wgpu::TextureDimension::D2,format: wgpu::TextureFormat::Rgba8Unorm,- usage: wgpu::TextureUsages::TEXTURE_BINDING- | wgpu::TextureUsages::RENDER_ATTACHMENT- | wgpu::TextureUsages::COPY_SRC- | wgpu::TextureUsages::COPY_DST,+ usage: crate::LOAD_TEXTURE_USAGE | wgpu::TextureUsages::RENDER_ATTACHMENT,view_formats: &[],});let view = texture.create_view(&Default::default());@@ -1298,49 +1293,6 @@ impl GxRenderer {}}- fn upload_texture_levels(- &mut self,- device: &wgpu::Device,- queue: &wgpu::Queue,- texture: &wgpu::Texture,- rgba: &[u8],- ) {- let mut offset = 0;-- for (level, (width, height)) in- gecko::flipper::gx::texture::mip_dimensions(texture.width(), texture.height(), texture.mip_level_count())- .enumerate()- {- let end = offset + (width * height * 4) as usize;- let pixels = &rgba[offset..end];- if !self.stage_texture_upload(device, texture, level as u32, pixels, width, height) {- let _ = self.submit_pending(queue);-- queue.write_texture(- wgpu::TexelCopyTextureInfo {- mip_level: level as u32,- ..texture.as_image_copy()- },- pixels,- wgpu::TexelCopyBufferLayout {- offset: 0,- bytes_per_row: Some(width * 4),- rows_per_image: None,- },- wgpu::Extent3d {- width,- height,- depth_or_array_layers: 1,- },- );- }-- offset = end;- }-- debug_assert_eq!(offset, rgba.len());- }-fn ensure_sampler(&mut self, device: &wgpu::Device, key: &SamplerKey) {self.sampler_cache.entry(*key).or_insert_with(|| {device.create_sampler(&wgpu::SamplerDescriptor {diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rsindex a84993e..ad5db6d 100644--- a/crates/backend-wgpu/src/lib.rs+++ b/crates/backend-wgpu/src/lib.rs@@ -11,6 +11,7 @@ mod render;mod renderdoc_capture;mod shader_specialization;pub mod sink;+mod texture_decode;use gecko::common::Address;#[cfg(feature = "renderdoc-capture")]@@ -110,6 +111,11 @@ impl RendererStats {}}+pub(crate) const LOAD_TEXTURE_USAGE: wgpu::TextureUsages = wgpu::TextureUsages::TEXTURE_BINDING+ .union(wgpu::TextureUsages::STORAGE_BINDING)+ .union(wgpu::TextureUsages::COPY_DST)+ .union(wgpu::TextureUsages::COPY_SRC);+pub(crate) fn align_up(value: u64, alignment: u64) -> u64 {(value + alignment - 1) & !(alignment - 1)}@@ -377,18 +383,12 @@ 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_decoder: texture_decode::TextureDecoder,+ /// Shared storage buffer for raw texture bytes, palettes and decode+ /// parameters. All staged input is uploaded once at submission time.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>,@@ -603,7 +603,7 @@ impl GxRenderer {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,+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::STORAGE,mapped_at_creation: false,});@@ -1036,10 +1036,10 @@ impl GxRenderer {efb_color_readback_bind_group,efb_clear,pending_command_buffers: Vec::with_capacity(8),+ texture_decoder: texture_decode::TextureDecoder::new(device),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,@@ -1111,10 +1111,10 @@ impl GxRenderer {}pub(crate) fn submit_pending(&mut self, queue: &wgpu::Queue) -> Option<wgpu::SubmissionIndex> {- // Ship any staged texture-upload bytes through ONE `write_buffer_with`+ // Ship any staged texture-decode input 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+ // submitted commands, so the encoder's compute decode dispatches+ // (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)@@ -1162,88 +1162,14 @@ impl GxRenderer {Some(submission_index)}- /// 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,- mip_level: u32,- 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,- },- },- wgpu::TexelCopyTextureInfo {- mip_level,- ..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);+ /// Reallocate the staging buffer to fit `needed` bytes. Safe to call only+ /// when no encoder commands reference the existing buffer.+ pub(crate) fn grow_texture_staging(&mut self, device: &wgpu::Device, needed: u64) {+ let new_cap = needed.next_power_of_two().min(device.limits().max_buffer_size);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,+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::STORAGE,mapped_at_creation: false,});self.texture_staging_capacity = new_cap;diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rsindex d409321..b4e1931 100644--- a/crates/backend-wgpu/src/render.rs+++ b/crates/backend-wgpu/src/render.rs@@ -525,9 +525,7 @@ impl GxRenderer {debug_assert_eq!(tex.format(), wgpu::TextureFormat::Rgba8Unorm);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,- ));+ debug_assert!(tex.usage().contains(crate::LOAD_TEXTURE_USAGE));let size = tex.size();let bucket = self@@ -643,9 +641,6 @@ impl GxRenderer {encoder.pop_debug_group();self.current_encoder = Some(encoder);let _ = 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;}diff --git a/crates/backend-wgpu/src/shaders/texture_decode.wgsl b/crates/backend-wgpu/src/shaders/texture_decode.wgslnew file mode 100644index 0000000..14135b8--- /dev/null+++ b/crates/backend-wgpu/src/shaders/texture_decode.wgsl@@ -0,0 +1,116 @@+struct DecodeInput {+ width: u32,+ height: u32,+ format: u32,+ tlut_format: u32,+ tile_w: u32,+ tile_h: u32,+ tile_bytes: u32,+ data_offset: u32,+ palette_offset: u32,+ palette_len: u32,+ words: array<u32>,+}++@group(0) @binding(0) var<storage, read> source: DecodeInput;+@group(0) @binding(1) var dest: texture_storage_2d<rgba8unorm, write>;++fn byte_at(offset: u32) -> u32 {+ let address = source.data_offset + offset;+ return (source.words[address / 4u] >> ((address % 4u) * 8u)) & 255u;+}++fn be16(offset: u32) -> u32 {+ return (byte_at(offset) << 8u) | byte_at(offset + 1u);+}++fn expand3(v: u32) -> u32 { return (v << 5u) | (v << 2u) | (v >> 1u); }+fn expand4(v: u32) -> u32 { return (v << 4u) | v; }+fn expand5(v: u32) -> u32 { return (v << 3u) | (v >> 2u); }+fn expand6(v: u32) -> u32 { return (v << 2u) | (v >> 4u); }++fn rgb565(v: u32) -> vec4<u32> {+ return vec4<u32>(expand5((v >> 11u) & 31u), expand6((v >> 5u) & 63u), expand5(v & 31u), 255u);+}++fn rgb5a3(v: u32) -> vec4<u32> {+ if (v & 32768u) != 0u {+ return vec4<u32>(expand5((v >> 10u) & 31u), expand5((v >> 5u) & 31u), expand5(v & 31u), 255u);+ }+ return vec4<u32>(expand4((v >> 8u) & 15u), expand4((v >> 4u) & 15u), expand4(v & 15u), expand3((v >> 12u) & 7u));+}++fn ia8(v: u32) -> vec4<u32> {+ return vec4<u32>(vec3<u32>(v & 255u), v >> 8u);+}++fn palette(index: u32) -> vec4<u32> {+ var entry = 0u;+ if index < source.palette_len {+ entry = source.words[source.palette_offset + index];+ }++ switch source.tlut_format {+ case 1u: { return rgb565(entry); }+ case 2u: { return rgb5a3(entry); }+ default: { return ia8(entry); }+ }+}++fn cmpr(block: u32, local: vec2<u32>) -> vec4<u32> {+ let sub = block + ((local.y / 4u) * 2u + local.x / 4u) * 8u;+ let c0 = be16(sub);+ let c1 = be16(sub + 2u);+ let p0 = rgb565(c0);+ let p1 = rgb565(c1);+ let index = (byte_at(sub + 4u + local.y % 4u) >> ((3u - local.x % 4u) * 2u)) & 3u;++ if index == 0u { return p0; }+ if index == 1u { return p1; }++ if c0 > c1 {+ if index == 2u { return vec4<u32>((2u * p0.rgb + p1.rgb) / 3u, 255u); }+ return vec4<u32>((p0.rgb + 2u * p1.rgb) / 3u, 255u);+ }++ return vec4<u32>((p0.rgb + p1.rgb) / 2u, select(255u, 0u, index == 3u));+}++fn texel(pos: vec2<u32>) -> vec4<u32> {+ let tile = vec2<u32>(source.tile_w, source.tile_h);+ let blocks_x = (source.width + tile.x - 1u) / tile.x;+ let block = ((pos.y / tile.y) * blocks_x + pos.x / tile.x) * source.tile_bytes;+ let local = pos % tile;+ let index = local.y * tile.x + local.x;++ switch source.format {+ case 0u, 8u: {+ let nibble = (byte_at(block + index / 2u) >> select(0u, 4u, index % 2u == 0u)) & 15u;+ if source.format == 8u { return palette(nibble); }+ return vec4<u32>(expand4(nibble));+ }+ case 1u: { return vec4<u32>(byte_at(block + index)); }+ case 2u: {+ let v = byte_at(block + index);+ return vec4<u32>(vec3<u32>(expand4(v & 15u)), expand4(v >> 4u));+ }+ case 3u: { return ia8(be16(block + index * 2u)); }+ case 4u: { return rgb565(be16(block + index * 2u)); }+ case 5u: { return rgb5a3(be16(block + index * 2u)); }+ case 6u: {+ let ar = block + index * 2u;+ let gb = ar + 32u;+ return vec4<u32>(byte_at(ar + 1u), byte_at(gb), byte_at(gb + 1u), byte_at(ar));+ }+ case 9u: { return palette(byte_at(block + index)); }+ case 10u: { return palette(be16(block + index * 2u) & 16383u); }+ case 14u: { return cmpr(block, local); }+ default: { return vec4<u32>(0u); }+ }+}++@compute @workgroup_size(8, 8)+fn decode(@builtin(global_invocation_id) id: vec3<u32>) {+ if id.x >= source.width || id.y >= source.height { return; }+ textureStore(dest, vec2<i32>(id.xy), vec4<f32>(texel(id.xy)) / 255.0);+}diff --git a/crates/backend-wgpu/src/texture_decode.rs b/crates/backend-wgpu/src/texture_decode.rsnew file mode 100644index 0000000..15b3dc9--- /dev/null+++ b/crates/backend-wgpu/src/texture_decode.rs@@ -0,0 +1,218 @@+use crate::{GxRenderer, align_up};+use gecko::flipper::gx::draw::TextureFormat;+use gecko::flipper::gx::texture::{EncodedTexture, block_dims, mip_dimensions, raw_data_size};+use std::num::NonZeroU64;++const PARAMS_SIZE: u64 = size_of::<DecodeParams>() as u64;++pub(crate) struct TextureDecoder {+ layout: wgpu::BindGroupLayout,+ pipeline: wgpu::ComputePipeline,+ offset_alignment: u64,+ max_binding_size: u64,+}++impl TextureDecoder {+ pub(crate) fn new(device: &wgpu::Device) -> Self {+ let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {+ label: Some("gx_texture_decode"),+ source: wgpu::ShaderSource::Wgsl(include_str!("shaders/texture_decode.wgsl").into()),+ });+ let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {+ label: Some("gx_texture_decode"),+ entries: &[+ wgpu::BindGroupLayoutEntry {+ binding: 0,+ visibility: wgpu::ShaderStages::COMPUTE,+ ty: wgpu::BindingType::Buffer {+ ty: wgpu::BufferBindingType::Storage { read_only: true },+ has_dynamic_offset: false,+ min_binding_size: NonZeroU64::new(PARAMS_SIZE + 4),+ },+ count: None,+ },+ wgpu::BindGroupLayoutEntry {+ binding: 1,+ visibility: wgpu::ShaderStages::COMPUTE,+ ty: wgpu::BindingType::StorageTexture {+ access: wgpu::StorageTextureAccess::WriteOnly,+ format: wgpu::TextureFormat::Rgba8Unorm,+ view_dimension: wgpu::TextureViewDimension::D2,+ },+ count: None,+ },+ ],+ });+ let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {+ label: Some("gx_texture_decode"),+ bind_group_layouts: &[Some(&layout)],+ immediate_size: 0,+ });+ let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {+ label: Some("gx_texture_decode"),+ layout: Some(&pipeline_layout),+ module: &shader,+ entry_point: Some("decode"),+ compilation_options: Default::default(),+ cache: None,+ });+ let limits = device.limits();++ Self {+ layout,+ pipeline,+ offset_alignment: u64::from(limits.min_storage_buffer_offset_alignment),+ max_binding_size: u64::from(limits.max_storage_buffer_binding_size),+ }+ }++ fn bind_group(+ &self,+ device: &wgpu::Device,+ buffer: &wgpu::Buffer,+ offset: u64,+ end: u64,+ texture: &wgpu::Texture,+ level: u32,+ ) -> wgpu::BindGroup {+ let view = texture.create_view(&wgpu::TextureViewDescriptor {+ label: Some("gx_texture_decode_mip"),+ base_mip_level: level,+ mip_level_count: Some(1),+ usage: Some(wgpu::TextureUsages::STORAGE_BINDING),+ ..Default::default()+ });++ device.create_bind_group(&wgpu::BindGroupDescriptor {+ label: Some("gx_texture_decode"),+ layout: &self.layout,+ entries: &[+ wgpu::BindGroupEntry {+ binding: 0,+ resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {+ buffer,+ offset,+ size: NonZeroU64::new(end - offset),+ }),+ },+ wgpu::BindGroupEntry {+ binding: 1,+ resource: wgpu::BindingResource::TextureView(&view),+ },+ ],+ })+ }+}++#[repr(C)]+#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]+struct DecodeParams {+ width: u32,+ height: u32,+ format: u32,+ tlut_format: u32,+ tile_w: u32,+ tile_h: u32,+ tile_bytes: u32,+ data_offset: u32,+ palette_offset: u32,+ palette_len: u32,+}++struct DecodeUpload {+ start: u64,+ header_stride: u64,+ bytes_start: u64,+ palette_start: u64,+ end: u64,+}++impl DecodeUpload {+ fn new(offset: u64, alignment: u64, levels: u32, data: &EncodedTexture) -> Self {+ let start = align_up(offset, alignment);+ let header_stride = align_up(PARAMS_SIZE, alignment);+ let bytes_start = start + header_stride * u64::from(levels);+ let palette_start = align_up(bytes_start + data.bytes.len() as u64, 4);++ Self {+ start,+ header_stride,+ bytes_start,+ palette_start,+ end: palette_start + data.palette.len() as u64 * 4,+ }+ }+}++impl GxRenderer {+ pub(crate) fn decode_texture_levels(+ &mut self,+ device: &wgpu::Device,+ queue: &wgpu::Queue,+ texture: &wgpu::Texture,+ format: TextureFormat,+ data: &EncodedTexture,+ ) {+ let alignment = self.texture_decoder.offset_alignment;+ let levels = texture.mip_level_count();++ let mut upload = DecodeUpload::new(self.texture_staging_scratch.len() as u64, alignment, levels, data);+ if upload.end > self.texture_staging_capacity {+ let _ = self.submit_pending(queue);+ self.grow_texture_staging(device, upload.end);+ upload = DecodeUpload::new(0, alignment, levels, data);+ }+ assert!(upload.end <= self.texture_staging_capacity);+ assert!(upload.end - upload.start <= self.texture_decoder.max_binding_size);++ let scratch = &mut self.texture_staging_scratch;+ scratch.resize(upload.bytes_start as usize, 0);+ scratch.extend_from_slice(&data.bytes);+ scratch.resize(upload.palette_start as usize, 0);+ scratch.extend(data.palette.iter().flat_map(|&entry| u32::from(entry).to_le_bytes()));++ let mut encoder = self.take_or_create_encoder(device);+ let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {+ label: Some("gx_texture_decode"),+ timestamp_writes: None,+ });+ pass.set_pipeline(&self.texture_decoder.pipeline);++ let tile = block_dims(format);+ let mut raw_offset = 0;+ for (level, (width, height)) in mip_dimensions(texture.width(), texture.height(), levels).enumerate() {+ let header = upload.start + level as u64 * upload.header_stride;+ let words_start = header + PARAMS_SIZE;+ let params = DecodeParams {+ width,+ height,+ format: format as u32,+ tlut_format: data.tlut_format as u32,+ tile_w: tile.tile_w,+ tile_h: tile.tile_h,+ tile_bytes: tile.bytes_per_tile,+ data_offset: (upload.bytes_start + raw_offset - words_start) as u32,+ palette_offset: ((upload.palette_start - words_start) / 4) as u32,+ palette_len: data.palette.len() as u32,+ };+ self.texture_staging_scratch[header as usize..words_start as usize]+ .copy_from_slice(bytemuck::bytes_of(¶ms));++ let bind_group = self.texture_decoder.bind_group(+ device,+ &self.texture_staging_buffer,+ header,+ upload.end,+ texture,+ level as u32,+ );+ pass.set_bind_group(0, &bind_group, &[]);+ pass.dispatch_workgroups(width.div_ceil(8), height.div_ceil(8), 1);++ raw_offset += raw_data_size(width, height, format) as u64;+ }++ drop(pass);+ self.current_encoder = Some(encoder);+ }+}diff --git a/crates/dbglib/src/windows/gx.rs b/crates/dbglib/src/windows/gx.rsindex 98905a2..4e6a844 100644--- a/crates/dbglib/src/windows/gx.rs+++ b/crates/dbglib/src/windows/gx.rs@@ -106,7 +106,8 @@ impl Freecam {}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 rgba =+ gecko::flipper::gx::texture::decode_to_rgba(ram, tex.width, tex.height, tex.format, 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(diff --git a/crates/fifoplayer/src/debug/geometry.rs b/crates/fifoplayer/src/debug/geometry.rsindex 0aac4a0..a9f48b8 100644--- a/crates/fifoplayer/src/debug/geometry.rs+++ b/crates/fifoplayer/src/debug/geometry.rs@@ -82,13 +82,15 @@ impl GeometryCapture {id,width,height,- rgba,+ fmt,+ data,..} => {let size = [*width as usize, *height as usize];- if !rgba.is_empty() && rgba.len() >= size[0] * size[1] * 4 {- let image = egui::ColorImage::from_rgba_unmultiplied(size, &rgba[..size[0] * size[1] * 4]);+ if let Some(data) = data {+ let rgba = data.decode_base_level(*width, *height, *fmt);+ let image = egui::ColorImage::from_rgba_unmultiplied(size, &rgba);self.textures.insert(*id, Arc::new(image));} else {self.textures.remove(id);diff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rsindex 20b71aa..6295987 100644--- a/crates/gecko/src/flipper/gx/bp.rs+++ b/crates/gecko/src/flipper/gx/bp.rs@@ -363,22 +363,11 @@ impl GraphicsProcessor {height,fmt: format,mip_levels,- rgba: Vec::new(),+ data: None,});}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(),- };-#[cfg(feature = "gx-stats")]{self.stats.texture_loads += 1;@@ -390,7 +379,11 @@ impl GraphicsProcessor {height,fmt: format,mip_levels,- rgba: texture::decode_mips_to_rgba(tex_slice.unwrap(), &desc, palette, tlut.format, mip_levels),+ data: Some(texture::EncodedTexture {+ bytes: tex_slice.unwrap().to_vec(),+ palette: palette[..format.palette_entries().min(palette.len())].to_vec(),+ tlut_format: tlut.format,+ }),});}@@ -818,14 +811,7 @@ fn texture_cache_id(mip_levels: u32,) -> 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 palette_bytes: &[u8] = bytemuck::cast_slice(&palette[..format.palette_entries().min(palette.len())]);let mut h = twox_hash::xxhash3_64::Hasher::oneshot(palette_bytes);h ^= tlut.format as u64;h ^= (tlut.tmem_offset as u64) << 8;@@ -864,14 +850,7 @@ fn texture_data_changed(let mut hash = twox_hash::xxhash3_64::Hasher::oneshot(tex);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 palette_bytes: &[u8] = bytemuck::cast_slice(&palette[..format.palette_entries().min(palette.len())]);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.diff --git a/crates/gecko/src/flipper/gx/draw.rs b/crates/gecko/src/flipper/gx/draw.rsindex 8f87767..c2cf05d 100644--- a/crates/gecko/src/flipper/gx/draw.rs+++ b/crates/gecko/src/flipper/gx/draw.rs@@ -101,6 +101,16 @@ impl TextureFormat {pub fn is_paletted(self) -> bool {matches!(self, TextureFormat::CI4 | TextureFormat::CI8 | TextureFormat::CI14)}++ #[inline(always)]+ pub fn palette_entries(self) -> usize {+ match self {+ TextureFormat::CI4 => 16,+ TextureFormat::CI8 => 256,+ TextureFormat::CI14 => 16384,+ _ => 0,+ }+ }}#[derive(Debug, Clone, Copy, Default)]diff --git a/crates/gecko/src/flipper/gx/texture.rs b/crates/gecko/src/flipper/gx/texture.rsindex d12ba0c..3471da2 100644--- a/crates/gecko/src/flipper/gx/texture.rs+++ b/crates/gecko/src/flipper/gx/texture.rs@@ -1,22 +1,44 @@-use super::draw::{TextureDescriptor, TextureFormat, TlutFormat};+use super::draw::{TextureFormat, TlutFormat};use multiversion::multiversion;+/// An immutable snapshot of guest texture bytes and its bound TLUT. The renderer+/// decodes this on the GPU; debug consumers can explicitly request a CPU preview.+#[derive(Debug)]+pub struct EncodedTexture {+ pub bytes: Vec<u8>,+ pub palette: Vec<u16>,+ pub tlut_format: TlutFormat,+}++impl EncodedTexture {+ pub fn decode_base_level(&self, width: u32, height: u32, format: TextureFormat) -> Vec<u8> {+ self::decode_to_rgba(&self.bytes, width, height, format, &self.palette, self.tlut_format)+ }+}+/// Decode a GX-format texture from a raw RAM slice into RGBA8 pixels.////// `tex` must already point at the texture's raw bytes (i.e. the caller has-/// resolved `desc.ram_addr` against MEM1/MEM2 and produced the slice). The+/// resolved the texture address against MEM1/MEM2 and produced the slice). The/// decoders below offset relative to the start of `tex`.////// `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(tex: &[u8], desc: &TextureDescriptor, palette: &[u16], tlut_format: TlutFormat) -> Vec<u8> {- let w = desc.width as usize;- let h = desc.height as usize;+pub fn decode_to_rgba(+ tex: &[u8],+ width: u32,+ height: u32,+ format: TextureFormat,+ palette: &[u16],+ tlut_format: TlutFormat,+) -> Vec<u8> {+ let w = width as usize;+ let h = height as usize;let mut rgba = vec![0u8; w * h * 4];- match desc.format {+ match format {TextureFormat::I4 => decode_i4(tex, &mut rgba, w, h),TextureFormat::I8 => decode_i8(tex, &mut rgba, w, h),TextureFormat::IA4 => decode_ia4(tex, &mut rgba, w, h),@@ -51,39 +73,6 @@ pub fn mip_data_size(width: u32, height: u32, format: TextureFormat, levels: u32.sum()}-pub fn decode_mips_to_rgba(- tex: &[u8],- desc: &TextureDescriptor,- palette: &[u16],- tlut_format: TlutFormat,- levels: u32,-) -> Vec<u8> {- if levels == 1 {- return self::decode_to_rgba(tex, desc, palette, tlut_format);- }-- let mut rgba = Vec::with_capacity(- self::mip_dimensions(desc.width, desc.height, levels)- .map(|(w, h)| (w * h * 4) as usize)- .sum(),- );-- let mut offset = 0;- for (width, height) in self::mip_dimensions(desc.width, desc.height, levels) {- let size = self::raw_data_size(width, height, desc.format);- let level = TextureDescriptor { width, height, ..*desc };- rgba.extend(self::decode_to_rgba(- tex.get(offset..offset + size).unwrap_or(&[]),- &level,- palette,- tlut_format,- ));- offset += size;- }-- rgba-}-#[derive(Clone, Copy)]pub struct TextureHash {pub hash: u64,diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rsindex be72729..e4a2b57 100644--- a/crates/gecko/src/host.rs+++ b/crates/gecko/src/host.rs@@ -57,15 +57,16 @@ pub enum GxAction {SetAlphaCompare(AlphaCompare),SetCullMode(CullMode),- /// Upload pre-decoded texture data. Emitted when texture content at a- /// given address changes (detected by hash).+ /// Decode and upload a snapshot of GX texture bytes and palette. Emitted+ /// when texture content at a given address changes (detected by hash).LoadTexture {id: TextureKey,width: u32,height: u32,fmt: TextureFormat,mip_levels: u32,- rgba: Vec<u8>,+ /// `None` references an existing GPU EFB copy without uploading RAM.+ data: Option<crate::flipper::gx::texture::EncodedTexture>,},LoadEfbPalette {diff --git a/crates/mcp/src/server.rs b/crates/mcp/src/server.rsindex 5e64e65..7a8369a 100644--- a/crates/mcp/src/server.rs+++ b/crates/mcp/src/server.rs@@ -527,7 +527,10 @@ impl McpServer {None,)})?;- let png = encode_png(rec.width, rec.height, &rec.rgba)?;+ let rgba = rec.data.as_ref().map_or_else(Vec::new, |data| {+ data.decode_base_level(rec.width, rec.height, rec.format)+ });+ let png = encode_png(rec.width, rec.height, &rgba)?;ok_json(&json!({"ram_addr": args.ram_addr,"variant": key.variant,diff --git a/crates/mcp/src/sink.rs b/crates/mcp/src/sink.rsindex 2cc54c0..305fabf 100644--- a/crates/mcp/src/sink.rs+++ b/crates/mcp/src/sink.rs@@ -9,7 +9,7 @@ pub struct TextureRecord {pub width: u32,pub height: u32,pub format: TextureFormat,- pub rgba: Vec<u8>,+ pub data: Option<gecko::flipper::gx::texture::EncodedTexture>,pub last_seen_frame: u64,}@@ -35,40 +35,43 @@ pub struct McpSink {impl RenderSink for McpSink {fn exec(&mut self, action: GxAction) {- match &action {+ self.gx.lock().unwrap().process_action_with_external_scratch(+ &self.device,+ &self.queue,+ &action,+ &mut self.scratch,+ );+ match action {GxAction::LoadTexture {id,width,height,fmt,- rgba,+ data,..} => {let mut i = self.introspect.lock().unwrap();let frame = i.frame_index;i.textures.insert(- *id,+ id,TextureRecord {- width: *width,- height: *height,- format: *fmt,- rgba: rgba- .get(..(*width as usize * *height as usize * 4))- .unwrap_or(&[])- .to_vec(),+ width,+ height,+ format: fmt,+ data,last_seen_frame: frame,},);}GxAction::SetTexture { slot, id, .. } => {let mut i = self.introspect.lock().unwrap();- if *slot < 8 {- i.bound[*slot] = Some(*id);+ if slot < 8 {+ i.bound[slot] = Some(id);}}GxAction::PresentXfb { width, height, .. } | GxAction::PresentRawXfb { width, height, .. } => {let mut i = self.introspect.lock().unwrap();- i.last_xfb_size = (*width, *height);+ i.last_xfb_size = (width, height);i.frame_index = i.frame_index.wrapping_add(1);i.draw_count_this_frame = 0;}@@ -77,12 +80,6 @@ impl RenderSink for McpSink {}_ => {}}- self.gx.lock().unwrap().process_action_with_external_scratch(- &self.device,- &self.queue,- &action,- &mut self.scratch,- );}fn vertex_scratch(&mut self) -> &mut Vec<DrawVertex> {