A GameCube/Wii emulator and debugger written in Rust.
more gx shit
Layle184d86cca9d8
diff --git a/Cargo.lock b/Cargo.lockindex 4b14ce4..1af1d30 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -269,6 +269,7 @@ name = "backend-wgpu"version = "0.1.0"dependencies = ["bytemuck",+ "chapa","crossbeam-channel","gecko","glam",diff --git a/crates/backend-wgpu/Cargo.toml b/crates/backend-wgpu/Cargo.tomlindex ea9cfcc..aa95b03 100644--- a/crates/backend-wgpu/Cargo.toml+++ b/crates/backend-wgpu/Cargo.toml@@ -11,6 +11,7 @@ renderdoc-capture = ["dep:renderdoc"]wgpu = "28.0.0"bytemuck = { version = "1.25.0", features = ["derive"] }gecko = { path = "../gecko" }+chapa = "0.1.2"wesl = "0.3.2"glam = { version = "0.32.1", features = ["bytemuck"] }crossbeam-channel = "0.5.15"diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rsindex e895216..eb7b7d9 100644--- a/crates/backend-wgpu/src/action.rs+++ b/crates/backend-wgpu/src/action.rs@@ -185,12 +185,12 @@ impl GxRenderer {self.pipeline_cache.insert(full_key, pipeline);}- // Triangulate.- let prev_len = self.scratch_vertices.len();- triangulate_draw_data(draw, &mut self.scratch_vertices);- let added = self.scratch_vertices.len() - prev_len;- if added == 0 {- tracing::warn!("draw call produced zero triangulated vertices, skipping");+ let first_vertex = self.scratch_vertices.len() as u32;+ let first_index = self.scratch_indices.len() as u32;+ let (vertex_count, index_count) =+ triangulate_draw_data(draw, &mut self.scratch_vertices, &mut self.scratch_indices);+ if vertex_count == 0 {+ tracing::warn!("draw call produced zero vertices, skipping");return;}@@ -264,7 +264,8 @@ impl GxRenderer {#[cfg(feature = "renderdoc-capture")]self.draw_primitives.push(draw.primitive);- self.scratch_draws.push((prev_len as u32, added as u32));+ self.scratch_draws+ .push((first_vertex, vertex_count, first_index, index_count));}GxAction::CopyXfb {@@ -397,6 +398,7 @@ impl GxRenderer {self.execute_action_render_pass(device, queue);self.scratch_vertices.clear();+ self.scratch_indices.clear();self.scratch_draws.clear();self.scratch_uniform_bytes.clear();self.frame_uniform_bytes.clear();@@ -416,9 +418,14 @@ impl GxRenderer {let fallback_sampler_key = (WrapMode::Clamp, WrapMode::Clamp, MagFilter::Linear, MinFilter::Linear);let frame_stride = self.frame_stride;- // Pre-build bind groups from tracked texture snapshots.- for bg_key in &self.draw_bg_keys {- self.bind_group_cache.entry(bg_key.clone()).or_insert_with(|| {+ let num_draws = self.draw_bg_keys.len();+ for i in 0..num_draws {+ let bg_key = &self.draw_bg_keys[i];+ if self.bind_group_cache.contains_key(bg_key) {+ continue;+ }++ let new_bg = {let mut tex_views: [&wgpu::TextureView; 8] = [&self.fallback_view; 8];let mut tex_samplers: [&wgpu::Sampler; 8] = [&self.sampler_cache[&fallback_sampler_key]; 8];@@ -475,7 +482,8 @@ impl GxRenderer {layout: &self.bind_group_layout,entries: &entries,})- });+ };+ self.bind_group_cache.insert(self.draw_bg_keys[i].clone(), new_bg);}let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {@@ -549,12 +557,15 @@ impl GxRenderer {}}- for (index, (first_vertex, vertex_count)) in self.scratch_draws.iter().copied().enumerate() {+ rpass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint32);+ for (index, (first_vertex, vertex_count, first_index, index_count)) in+ self.scratch_draws.iter().copied().enumerate()+ {#[cfg(feature = "renderdoc-capture")]{let primitive = self.draw_primitives[index];let draw_label = format!(- "GX Primitive Batch {index}: primitive={primitive:?} first_vertex={first_vertex} vertex_count={vertex_count}"+ "GX Primitive Batch {index}: primitive={primitive:?} first_vertex={first_vertex} vertex_count={vertex_count} indices={index_count}");rpass.push_debug_group(&draw_label);}@@ -618,7 +629,11 @@ impl GxRenderer {rpass.insert_debug_marker(&raster_marker);}- rpass.draw(first_vertex..first_vertex + vertex_count, 0..1);+ if index_count == 0 {+ rpass.draw(first_vertex..first_vertex + vertex_count, 0..1);+ } else {+ rpass.draw_indexed(first_index..first_index + index_count, first_vertex as i32, 0..1);+ }#[cfg(feature = "renderdoc-capture")]rpass.pop_debug_group();}@@ -643,69 +658,61 @@ impl GxRenderer {}}-/// Triangulate a [`DrawData`] into GPU vertices.-fn triangulate_draw_data(draw: &DrawData, out: &mut Vec<GpuVertex>) {+fn triangulate_draw_data(draw: &DrawData, verts_out: &mut Vec<GpuVertex>, indices_out: &mut Vec<u32>) -> (u32, u32) {use gecko::flipper::gx::draw::Primitive;- let verts = &draw.vertices;- let to_gpu = |v: &gecko::host::DrawVertex| -> GpuVertex {- GpuVertex {- position: v.position,- color: v.color0,- color1: v.color1,- normal: v.normal,- pos_view: v.pos_view,- tex0: v.texcoords[0],- tex1: v.texcoords[1],- tex2: v.texcoords[2],- tex3: v.texcoords[3],- tex4: v.texcoords[4],- tex5: v.texcoords[5],- tex6: v.texcoords[6],- tex7: v.texcoords[7],- }- };+ let verts: &[GpuVertex] = &draw.vertices;+ let n = verts.len() as u32;match draw.primitive {Primitive::Triangles => {- out.extend(verts.iter().map(to_gpu));+ verts_out.extend_from_slice(verts);+ (n, 0)}Primitive::Quads => {- for quad in verts.chunks(4) {- if quad.len() < 4 {- tracing::error!(count = quad.len(), "quad primitive with less than 4 vertices, skipping");- continue;- }- out.push(to_gpu(&quad[0]));- out.push(to_gpu(&quad[1]));- out.push(to_gpu(&quad[2]));- out.push(to_gpu(&quad[0]));- out.push(to_gpu(&quad[2]));- out.push(to_gpu(&quad[3]));+ verts_out.extend_from_slice(verts);++ let chunks = n / 4;+ for q in 0..chunks {+ let base = q * 4;+ indices_out.extend_from_slice(&[base, base + 1, base + 2, base, base + 2, base + 3]);}++ (n, chunks * 6)}Primitive::TriangleStrip => {- for i in 2..verts.len() {- if i % 2 == 0 {- out.push(to_gpu(&verts[i - 2]));- out.push(to_gpu(&verts[i - 1]));- out.push(to_gpu(&verts[i]));+ if n < 3 {+ return (0, 0);+ }++ verts_out.extend_from_slice(verts);++ let tris = n - 2;+ for i in 0..tris {+ if i & 1 == 0 {+ indices_out.extend_from_slice(&[i, i + 1, i + 2]);} else {- out.push(to_gpu(&verts[i - 1]));- out.push(to_gpu(&verts[i - 2]));- out.push(to_gpu(&verts[i]));+ indices_out.extend_from_slice(&[i + 1, i, i + 2]);}}++ (n, tris * 3)}Primitive::TriangleFan => {- for i in 2..verts.len() {- out.push(to_gpu(&verts[0]));- out.push(to_gpu(&verts[i - 1]));- out.push(to_gpu(&verts[i]));+ if n < 3 {+ return (0, 0);+ }++ verts_out.extend_from_slice(verts);+ let tris = n - 2;+ for i in 0..tris {+ indices_out.extend_from_slice(&[0, i + 1, i + 2]);}+ (n, tris * 3)}_ => {tracing::error!(?draw.primitive, "triangulate: skipping unsupported primitive");+ (0, 0)}}}diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rsindex ea0f383..679b579 100644--- a/crates/backend-wgpu/src/lib.rs+++ b/crates/backend-wgpu/src/lib.rs@@ -25,23 +25,7 @@ use rustc_hash::FxHashMap;use shader_specialization::ShaderKey;use std::num::NonZeroU64;-#[repr(C)]-#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]-pub(crate) struct GpuVertex {- pub position: [f32; 3],- pub color: [f32; 4],- pub color1: [f32; 4],- pub normal: [f32; 3],- pub pos_view: [f32; 3],- pub tex0: [f32; 3],- pub tex1: [f32; 3],- pub tex2: [f32; 3],- pub tex3: [f32; 3],- pub tex4: [f32; 3],- pub tex5: [f32; 3],- pub tex6: [f32; 3],- pub tex7: [f32; 3],-}+pub(crate) type GpuVertex = gecko::host::DrawVertex;pub(crate) fn align_up(value: u64, alignment: u64) -> u64 {(value + alignment - 1) & !(alignment - 1)@@ -132,6 +116,10 @@ pub struct GxRenderer {pub(crate) draw_uniform_capacity: usize,pub(crate) vertex_buffer: wgpu::Buffer,pub(crate) vertex_capacity: usize,+ // Index buffers.+ pub(crate) index_buffer: wgpu::Buffer,+ pub(crate) index_buffer_capacity: usize,+ pub(crate) scratch_indices: Vec<u32>,// EFB: resolved (1x) color used for CopyXfb reads + texture binding.pub(crate) efb_texture: wgpu::Texture,pub(crate) efb_view: wgpu::TextureView,@@ -157,7 +145,11 @@ pub struct GxRenderer {pub(crate) efb_depth_resolve_uniform_buffer: wgpu::Buffer,pub(crate) fallback_view: wgpu::TextureView,pub(crate) scratch_vertices: Vec<GpuVertex>,- pub(crate) scratch_draws: Vec<(u32, u32)>,+ /// `(first_vertex, vertex_count, first_index, index_count)`. When+ /// `index_count == 0` the draw is non-indexed (input was Triangles,+ /// no expansion needed). Otherwise `draw_indexed` is used with+ /// `base_vertex = first_vertex`.+ pub(crate) scratch_draws: Vec<(u32, u32, u32, u32)>,pub(crate) scratch_uniform_bytes: Vec<u8>,pub(crate) bind_group_cache: FxHashMap<BindGroupCacheKey, wgpu::BindGroup>,// Per-frame draw accumulation (persists across process_action calls,@@ -344,6 +336,13 @@ impl GxRenderer {usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,mapped_at_creation: false,});+ let initial_index_capacity = 4096;+ let index_buffer = device.create_buffer(&wgpu::BufferDescriptor {+ label: Some("gx_indices"),+ size: (initial_index_capacity * std::mem::size_of::<u32>()) as u64,+ usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,+ mapped_at_creation: false,+ });// Create fixed-size EFB with 4x MSAA.// Resolved (1x) color: CopyXfb reads from here.@@ -627,8 +626,11 @@ impl GxRenderer {shader_cache.insert(k, m);}+ let pipeline_cache: FxHashMap<pipeline::FullPipelineKey, wgpu::RenderPipeline> =+ FxHashMap::with_capacity_and_hasher(64, Default::default());+GxRenderer {- pipeline_cache: FxHashMap::default(),+ pipeline_cache,shader_cache,pipeline_layout,surface_format,@@ -639,6 +641,9 @@ impl GxRenderer {draw_uniform_capacity: 1,vertex_buffer,vertex_capacity: initial_capacity,+ index_buffer,+ index_buffer_capacity: initial_index_capacity,+ scratch_indices: Vec::new(),efb_texture,efb_view,_efb_msaa_texture: efb_msaa_texture,@@ -721,12 +726,69 @@ impl GxRenderer {shader_specialization::save_keys(path, &keys)?;Ok(keys.len())}++ pub fn save_pipeline_cache(&self) -> std::io::Result<usize> {+ let path = std::path::Path::new(pipeline::PIPELINE_CACHE_PATH);+ let keys: Vec<pipeline::FullPipelineKey> = self.pipeline_cache.keys().copied().collect();+ pipeline::save_pipeline_keys(path, &keys)?;+ Ok(keys.len())+ }++ pub fn prewarm_pipeline_cache(&mut self, device: &wgpu::Device) {+ let path = std::path::Path::new(pipeline::PIPELINE_CACHE_PATH);+ let keys: Vec<pipeline::FullPipelineKey> = pipeline::load_cached_pipeline_keys(path)+ .into_iter()+ .filter(|k| !self.pipeline_cache.contains_key(k) && self.shader_cache.contains_key(&k.shader))+ .collect();++ if keys.is_empty() {+ return;+ }++ let t0 = std::time::Instant::now();++ let num_threads = std::thread::available_parallelism()+ .map(|n| n.get())+ .unwrap_or(4)+ .min(keys.len());+ let chunk_size = keys.len().div_ceil(num_threads);++ let self_ref = &*self;+ let compiled: Vec<(pipeline::FullPipelineKey, wgpu::RenderPipeline)> = std::thread::scope(|s| {+ let handles: Vec<_> = keys+ .chunks(chunk_size)+ .map(|chunk| {+ s.spawn(move || {+ chunk+ .iter()+ .map(|&k| {+ let module = &self_ref.shader_cache[&k.shader];+ (k, self_ref.create_pipeline(device, module, &k.fixed))+ })+ .collect::<Vec<_>>()+ })+ })+ .collect();+ handles.into_iter().flat_map(|h| h.join().unwrap()).collect()+ });++ for (k, p) in compiled {+ self.pipeline_cache.insert(k, p);+ }++ tracing::info!(+ num_pipelines = keys.len(),+ elapsed_ms = t0.elapsed().as_millis() as u64,+ "prewarmed pipeline cache",+ );+ }}fn prewarm_shader_variants(device: &wgpu::Device, keys: &[ShaderKey]) -> Vec<(ShaderKey, wgpu::ShaderModule)> {if keys.is_empty() {return Vec::new();}+let t0 = std::time::Instant::now();let num_threads = std::thread::available_parallelism().map(|n| n.get())diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rsindex 1ae5d0b..331c6f4 100644--- a/crates/backend-wgpu/src/pipeline.rs+++ b/crates/backend-wgpu/src/pipeline.rs@@ -1,6 +1,10 @@-use crate::shader_specialization::ShaderKey;+use crate::shader_specialization::{KEY_BYTES as SHADER_KEY_BYTES, ShaderKey};use crate::{GpuVertex, GxRenderer, helpers};+use chapa::BitField;use gecko::flipper::gx::regs::{BlendFactor, CompareFunc, CullMode};+use std::fs::File;+use std::io::{BufWriter, Read, Write};+use std::path::Path;#[derive(Clone, Copy, Hash, PartialEq, Eq)]pub(crate) struct PipelineKey {@@ -16,12 +20,122 @@ pub(crate) struct PipelineKey {pub cull_mode: CullMode,}+impl PipelineKey {+ const BYTES: usize = 10;++ fn to_bytes(self) -> [u8; Self::BYTES] {+ [+ self.blend_enable as u8,+ self.src_factor.raw(),+ self.dst_factor.raw(),+ self.subtract as u8,+ self.z_enable as u8,+ self.z_func.raw(),+ self.z_write as u8,+ self.color_update as u8,+ self.alpha_update as u8,+ self.cull_mode.raw(),+ ]+ }++ fn from_bytes(b: &[u8; Self::BYTES]) -> Self {+ Self {+ blend_enable: b[0] != 0,+ src_factor: BlendFactor::from_raw(b[1]),+ dst_factor: BlendFactor::from_raw(b[2]),+ subtract: b[3] != 0,+ z_enable: b[4] != 0,+ z_func: CompareFunc::from_raw(b[5]),+ z_write: b[6] != 0,+ color_update: b[7] != 0,+ alpha_update: b[8] != 0,+ cull_mode: CullMode::from_raw(b[9]),+ }+ }+}+#[derive(Clone, Copy, Hash, PartialEq, Eq)]pub(crate) struct FullPipelineKey {pub shader: ShaderKey,pub fixed: PipelineKey,}+pub(crate) const FULL_PIPELINE_KEY_BYTES: usize = SHADER_KEY_BYTES + PipelineKey::BYTES;+const PIPELINE_CACHE_MAGIC: [u8; 4] = *b"GPKC";+const PIPELINE_CACHE_VERSION: u32 = 1;+pub(crate) const PIPELINE_CACHE_PATH: &str = "cache/pipeline_keys.bin";++impl FullPipelineKey {+ fn to_bytes(self) -> [u8; FULL_PIPELINE_KEY_BYTES] {+ let mut out = [0u8; FULL_PIPELINE_KEY_BYTES];+ out[..SHADER_KEY_BYTES].copy_from_slice(&self.shader.to_bytes());+ out[SHADER_KEY_BYTES..].copy_from_slice(&self.fixed.to_bytes());+ out+ }++ fn from_bytes(b: &[u8; FULL_PIPELINE_KEY_BYTES]) -> Self {+ let mut shader_bytes = [0u8; SHADER_KEY_BYTES];+ shader_bytes.copy_from_slice(&b[..SHADER_KEY_BYTES]);++ let mut fixed_bytes = [0u8; PipelineKey::BYTES];+ fixed_bytes.copy_from_slice(&b[SHADER_KEY_BYTES..]);++ Self {+ shader: ShaderKey::from_bytes(&shader_bytes),+ fixed: PipelineKey::from_bytes(&fixed_bytes),+ }+ }+}++pub(crate) fn load_cached_pipeline_keys(path: &Path) -> Vec<FullPipelineKey> {+ let mut f = match File::open(path) {+ Ok(f) => f,+ Err(_) => return Vec::new(),+ };++ let mut header = [0u8; 8];+ if f.read_exact(&mut header).is_err() {+ return Vec::new();+ }++ if header[..4] != PIPELINE_CACHE_MAGIC {+ return Vec::new();+ }++ let version = u32::from_le_bytes(header[4..8].try_into().unwrap());+ if version != PIPELINE_CACHE_VERSION {+ return Vec::new();+ }++ let mut keys = Vec::new();+ let mut buf = [0u8; FULL_PIPELINE_KEY_BYTES];+ while f.read_exact(&mut buf).is_ok() {+ keys.push(FullPipelineKey::from_bytes(&buf));+ }++ keys+}++pub(crate) fn save_pipeline_keys(path: &Path, keys: &[FullPipelineKey]) -> std::io::Result<()> {+ if let Some(parent) = path.parent() {+ std::fs::create_dir_all(parent)?;+ }++ let f = File::create(path)?;++ let mut w = BufWriter::new(f);+ w.write_all(&PIPELINE_CACHE_MAGIC)?;+ w.write_all(&PIPELINE_CACHE_VERSION.to_le_bytes())?;++ for k in keys {+ w.write_all(&k.to_bytes())?;+ }++ w.flush()?;++ Ok(())+}+impl GxRenderer {pub(crate) fn create_pipeline(&self,diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rsindex 73c8de7..b63ca60 100644--- a/crates/backend-wgpu/src/render.rs+++ b/crates/backend-wgpu/src/render.rs@@ -45,6 +45,16 @@ impl GxRenderer {});}+ if self.scratch_indices.len() > self.index_buffer_capacity {+ self.index_buffer_capacity = self.scratch_indices.len().next_power_of_two();+ self.index_buffer = device.create_buffer(&wgpu::BufferDescriptor {+ label: Some("gx_indices"),+ size: (self.index_buffer_capacity * std::mem::size_of::<u32>()) as u64,+ usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,+ mapped_at_creation: false,+ });+ }+let needed_frame_size = frame_uniform_bytes.len() as u64;if needed_frame_size > self.frame_uniform_buffer.size() {self.frame_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {@@ -59,6 +69,9 @@ impl GxRenderer {write_buffer(queue, &self.frame_uniform_buffer, frame_uniform_bytes);write_buffer(queue, &self.draw_uniform_buffer, &self.scratch_uniform_bytes);write_buffer(queue, &self.vertex_buffer, bytemuck::cast_slice(&self.scratch_vertices));+ if !self.scratch_indices.is_empty() {+ write_buffer(queue, &self.index_buffer, bytemuck::cast_slice(&self.scratch_indices));+ }}pub(crate) fn execute_copy_xfb(diff --git a/crates/backend-wgpu/src/sink.rs b/crates/backend-wgpu/src/sink.rsindex f1a7575..3cc932d 100644--- a/crates/backend-wgpu/src/sink.rs+++ b/crates/backend-wgpu/src/sink.rs@@ -126,6 +126,11 @@ fn worker(Ok(n) => tracing::info!(num_variants = n, "saved shader cache"),Err(err) => tracing::warn!(?err, "failed to save shader cache"),}++ match gx.save_pipeline_cache() {+ Ok(n) => tracing::info!(num_pipelines = n, "saved pipeline cache"),+ Err(err) => tracing::warn!(?err, "failed to save pipeline cache"),+ }}#[cfg(feature = "renderdoc-capture")]@@ -167,10 +172,8 @@ impl Renderer {surface_format: wgpu::TextureFormat,target_aspect: TargetAspect,) -> Self {- #[cfg(feature = "efb-writeback")]let mut gx = GxRenderer::new(&device, &queue, surface_format);- #[cfg(not(feature = "efb-writeback"))]- let gx = GxRenderer::new(&device, &queue, surface_format);+ gx.prewarm_pipeline_cache(&device);// Writeback channel: GxRenderer sends encoded EFB-to-texture bytes,// GraphicsProcessor consumes them synchronously on the emu thread.diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rsindex a59f9b5..c45bde1 100644--- a/crates/gecko/src/flipper/gx.rs+++ b/crates/gecko/src/flipper/gx.rs@@ -188,7 +188,14 @@ impl GraphicsProcessor {texture_hashes: FxHashMap::default(),#[cfg(feature = "efb-writeback")]efb_writeback_rx: None,- draw_box_pool: Vec::with_capacity(64),+ draw_box_pool: {+ const POOL_PREALLOC: usize = 1024;+ let mut v: Vec<Box<crate::host::DrawData>> = Vec::with_capacity(POOL_PREALLOC);+ for _ in 0..POOL_PREALLOC {+ v.push(Box::new(crate::host::DrawData::default()));+ }+ v+ },draw_box_recycle_rx: None,}}diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rsindex 67e0438..4bc033c 100644--- a/crates/gecko/src/host.rs+++ b/crates/gecko/src/host.rs@@ -199,14 +199,17 @@ pub struct DrawData {pub frame_dirty: bool,}-/// Per-vertex data after decode, ready for the renderer.-#[derive(Debug, Clone, Default)]+/// Per-vertex data after decode, ready for the renderer. Field order+/// matches `backend_wgpu::GpuVertex` and the wgpu vertex attribute layout+/// in `backend_wgpu::pipeline`, so a slice of `DrawVertex` can be uploaded+/// directly via `bytemuck::cast_slice` without any field-shuffle copy.+#[derive(Debug, Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)]#[repr(C)]pub struct DrawVertex {pub position: [f32; 3],- pub normal: [f32; 3],pub color0: [f32; 4],pub color1: [f32; 4],+ pub normal: [f32; 3],pub pos_view: [f32; 3],pub texcoords: [[f32; 3]; 8],}