A GameCube/Wii emulator and debugger written in Rust.
per-vertex position matrix, depth remap and 3-component texcoords
Layle6ef2502a72f2
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rsindex 2c9d42b..78820fb 100644--- a/crates/backend-wgpu/src/action.rs+++ b/crates/backend-wgpu/src/action.rs@@ -145,9 +145,8 @@ impl GxRenderer {return;}- // Build per-draw uniform (MVP) using tracked projection.- let modelview = Mat4::from_cols_array_2d(&draw.modelview);- let mvp = self.current_projection * modelview;+ // Build per-draw uniform using tracked projection.+ let mvp = self.current_projection;let draw_uniform = DrawUniforms { mvp };let start = self.scratch_draws.len() * draw_stride;diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rsindex 373b1db..252f44d 100644--- a/crates/backend-wgpu/src/lib.rs+++ b/crates/backend-wgpu/src/lib.rs@@ -21,14 +21,14 @@ pub(crate) struct GpuVertex {pub color1: [f32; 4],pub normal: [f32; 3],pub pos_view: [f32; 3],- pub tex0: [f32; 2],- pub tex1: [f32; 2],- pub tex2: [f32; 2],- pub tex3: [f32; 2],- pub tex4: [f32; 2],- pub tex5: [f32; 2],- pub tex6: [f32; 2],- pub tex7: [f32; 2],+ 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) fn align_up(value: u64, alignment: u64) -> u64 {diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rsindex a4d7244..2758fb4 100644--- a/crates/backend-wgpu/src/pipeline.rs+++ b/crates/backend-wgpu/src/pipeline.rs@@ -52,45 +52,45 @@ impl GxRenderer {offset: 56,shader_location: 4,},- // tex0-tex7: vec2<f32> each+ // tex0-tex7: vec3<f32> each (s, t, q for perspective-correct interpolation)wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,+ format: wgpu::VertexFormat::Float32x3,offset: 68,shader_location: 5,},wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 76,+ format: wgpu::VertexFormat::Float32x3,+ offset: 80,shader_location: 6,},wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 84,+ format: wgpu::VertexFormat::Float32x3,+ offset: 92,shader_location: 7,},wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 92,+ format: wgpu::VertexFormat::Float32x3,+ offset: 104,shader_location: 8,},wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 100,+ format: wgpu::VertexFormat::Float32x3,+ offset: 116,shader_location: 9,},wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 108,+ format: wgpu::VertexFormat::Float32x3,+ offset: 128,shader_location: 10,},wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 116,+ format: wgpu::VertexFormat::Float32x3,+ offset: 140,shader_location: 11,},wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 124,+ format: wgpu::VertexFormat::Float32x3,+ offset: 152,shader_location: 12,},],diff --git a/crates/backend-wgpu/src/shaders/common.wesl b/crates/backend-wgpu/src/shaders/common.weslindex 96398a0..9d5db0c 100644--- a/crates/backend-wgpu/src/shaders/common.wesl+++ b/crates/backend-wgpu/src/shaders/common.wesl@@ -62,26 +62,26 @@ struct VsIn {@location(2) color1: vec4<f32>,@location(3) normal: vec3<f32>,@location(4) pos_view: vec3<f32>,- @location(5) tex0: vec2<f32>,- @location(6) tex1: vec2<f32>,- @location(7) tex2: vec2<f32>,- @location(8) tex3: vec2<f32>,- @location(9) tex4: vec2<f32>,- @location(10) tex5: vec2<f32>,- @location(11) tex6: vec2<f32>,- @location(12) tex7: vec2<f32>,+ @location(5) tex0: vec3<f32>,+ @location(6) tex1: vec3<f32>,+ @location(7) tex2: vec3<f32>,+ @location(8) tex3: vec3<f32>,+ @location(9) tex4: vec3<f32>,+ @location(10) tex5: vec3<f32>,+ @location(11) tex6: vec3<f32>,+ @location(12) tex7: vec3<f32>,};struct VsOut {@builtin(position) clip_pos: vec4<f32>,@location(0) color: vec4<f32>,@location(1) color1: vec4<f32>,- @location(2) uv0: vec2<f32>,- @location(3) uv1: vec2<f32>,- @location(4) uv2: vec2<f32>,- @location(5) uv3: vec2<f32>,- @location(6) uv4: vec2<f32>,- @location(7) uv5: vec2<f32>,- @location(8) uv6: vec2<f32>,- @location(9) uv7: vec2<f32>,+ @location(2) uv0: vec3<f32>,+ @location(3) uv1: vec3<f32>,+ @location(4) uv2: vec3<f32>,+ @location(5) uv3: vec3<f32>,+ @location(6) uv4: vec3<f32>,+ @location(7) uv5: vec3<f32>,+ @location(8) uv6: vec3<f32>,+ @location(9) uv7: vec3<f32>,};diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.weslindex 9ad88e6..5899824 100644--- a/crates/backend-wgpu/src/shaders/main.wesl+++ b/crates/backend-wgpu/src/shaders/main.wesl@@ -1,5 +1,5 @@import super::common::{FrameUniforms, DrawUniforms, VsIn, VsOut, frame, draw, tex0, tex1, tex2, tex3, tex4, tex5, tex6, tex7, samp0, samp1, samp2, samp3, samp4, samp5, samp6, samp7};-import super::tev_helpers::{read_packed, extract_bits, select_texcoord, sample_texmap, tev_color_in, tev_alpha_in};+import super::tev_helpers::{read_packed, extract_bits, select_texcoord, stq_to_uv, sample_texmap, tev_color_in, tev_alpha_in};import super::tev_combiners::{tev_combine_color, tev_combine_alpha};import super::alpha_test::{alpha_compare, alpha_combine};import super::lighting::compute_channel_lighting;@@ -7,9 +7,9 @@ import super::lighting::compute_channel_lighting;@vertexfn vs_main(in: VsIn) -> VsOut {var out: VsOut;- out.clip_pos = draw.mvp * vec4<f32>(in.position, 1.0);- // Remap depth: GameCube/OpenGL uses [-1,1], wgpu uses [0,1]- out.clip_pos.z = out.clip_pos.z * 0.5 + out.clip_pos.w * 0.5;+ out.clip_pos = draw.mvp * vec4<f32>(in.pos_view, 1.0);+ // Remap depth: GameCube z is [-1,0], wgpu uses [0,1]+ out.clip_pos.z = out.clip_pos.z + out.clip_pos.w;out.color = compute_channel_lighting(frame.color_ctrl0, frame.alpha_ctrl0,frame.ambient_color0, frame.material_color0,@@ -75,7 +75,8 @@ fn fs_main(in: VsOut) -> @location(0) vec4<f32> {var tex_color: vec4<f32>;if tex_enabled {- let uv = select_texcoord(in, texcoord_idx);+ let stq = select_texcoord(in, texcoord_idx);+ let uv = stq_to_uv(stq);tex_color = sample_texmap(texmap_idx, uv);} else {tex_color = vec4<f32>(1.0, 1.0, 1.0, 1.0);diff --git a/crates/backend-wgpu/src/shaders/tev_helpers.wesl b/crates/backend-wgpu/src/shaders/tev_helpers.weslindex 8d9a0b6..c9e85ba 100644--- a/crates/backend-wgpu/src/shaders/tev_helpers.wesl+++ b/crates/backend-wgpu/src/shaders/tev_helpers.wesl@@ -11,7 +11,7 @@ fn extract_bits(val: u32, lo: u32, width: u32) -> u32 {return (val >> lo) & ((1u << width) - 1u);}-fn select_texcoord(in: VsOut, idx: u32) -> vec2<f32> {+fn select_texcoord(in: VsOut, idx: u32) -> vec3<f32> {switch idx {case 0u: { return in.uv0; }case 1u: { return in.uv1; }@@ -25,6 +25,17 @@ fn select_texcoord(in: VsOut, idx: u32) -> vec2<f32> {}}+// Perform the perspective divide on 3-component (s, t, q) texcoords.+// For non-projective texcoords q == 1 and the divide is a no-op.+// The q == 0 special case was already handled on the CPU (values are+// clamped, so we use them directly).+fn stq_to_uv(stq: vec3<f32>) -> vec2<f32> {+ if abs(stq.z) < 0.00001 {+ return stq.xy;+ }+ return stq.xy / stq.z;+}+fn sample_texmap(texmap: u32, uv: vec2<f32>) -> vec4<f32> {switch texmap {case 0u: { return textureSample(tex0, samp0, uv); }diff --git a/crates/gecko/src/flipper/gx/draw.rs b/crates/gecko/src/flipper/gx/draw.rsindex 0258bcc..01c625c 100644--- a/crates/gecko/src/flipper/gx/draw.rs+++ b/crates/gecko/src/flipper/gx/draw.rs@@ -39,7 +39,7 @@ pub struct Vertex {pub color1: [f32; 4],pub normal: [f32; 3],pub pos_view: [f32; 3],- pub texcoords: [Option<[f32; 2]>; 8],+ pub texcoords: [Option<[f32; 3]>; 8],}#[derive(BitEnum, Debug, PartialEq, Eq, Hash)]diff --git a/crates/gecko/src/flipper/gx/texgen.rs b/crates/gecko/src/flipper/gx/texgen.rsindex 06cf954..9ad957e 100644--- a/crates/gecko/src/flipper/gx/texgen.rs+++ b/crates/gecko/src/flipper/gx/texgen.rs@@ -10,7 +10,7 @@ impl GraphicsProcessor {normal: [f32; 3],raw_texcoords: &[Option<[f32; 2]>; 8],tex_mtx_indices: &[u8; 8],- ) -> [f32; 2] {+ ) -> [f32; 3] {let tg = TexGenReg::from_raw(self.xf_mem[XF_TEXGEN_BASE + texgen_idx]);let dt = DualTexGenReg::from_raw(self.xf_mem[XF_DUALTEX_BASE + texgen_idx]);@@ -32,11 +32,17 @@ impl GraphicsProcessor {// Dual texgen post-transform (normalization + post-matrix multiply)let (s, t, q) = self.apply_dual_texgen(s, t, q, &dt);- // When q is 0, the GameCube has a special case (Dolphin VertexShaderGen.cpp)+ // Return (s, t, q) as a 3-component vector. The perspective divide+ // (s/q, t/q) is deferred to the fragment shader so the rasterizer+ // can perform correct perspective-correct interpolation.+ //+ // When q is 0, the GameCube uses a special path (Dolphin+ // VertexShaderGen.cpp): clamp(xy / 2, -1, 1) with q set to 0.+ // The fragment shader checks for q~=0 and uses xy directly.if q.abs() < f32::EPSILON {- [(s / 2.0).clamp(-1.0, 1.0), (t / 2.0).clamp(-1.0, 1.0)]+ [(s / 2.0).clamp(-1.0, 1.0), (t / 2.0).clamp(-1.0, 1.0), 0.0]} else {- [s / q, t / q]+ [s, t, q]}}diff --git a/crates/gecko/src/flipper/gx/vertex.rs b/crates/gecko/src/flipper/gx/vertex.rsindex cfcfb63..f4c0a67 100644--- a/crates/gecko/src/flipper/gx/vertex.rs+++ b/crates/gecko/src/flipper/gx/vertex.rs@@ -119,7 +119,7 @@ impl GraphicsProcessor {color0: v.color0,color1: v.color1,pos_view: v.pos_view,- texcoords: std::array::from_fn(|i| v.texcoords[i].unwrap_or([0.0, 0.0])),+ texcoords: std::array::from_fn(|i| v.texcoords[i].unwrap_or([0.0, 0.0, 1.0])),}).collect();@@ -441,14 +441,16 @@ impl GraphicsProcessor {let pos_view = self.xf_transform_3x4(pos_mtx_base, position);// Texture coordinate generation (XF texgen)+ // compute_texgen now returns [f32; 3] (s, t, q) with perspective+ // divide deferred to the fragment shader.let num_texgens = (self.xf_mem[XF_NUM_TEXGENS] as usize).min(8);- let mut texcoords: [Option<[f32; 2]>; 8] = [None; 8];+ let mut texcoords: [Option<[f32; 3]>; 8] = [None; 8];for tg_idx in 0..num_texgens {texcoords[tg_idx] = Some(self.compute_texgen(tg_idx, position, normal, &raw_texcoords, &tex_mtx_idx));}- // For texcoords beyond num_texgens, pass through raw values+ // For texcoords beyond num_texgens, pass through raw values with q=1for tg_idx in num_texgens..8 {- texcoords[tg_idx] = raw_texcoords[tg_idx];+ texcoords[tg_idx] = raw_texcoords[tg_idx].map(|st| [st[0], st[1], 1.0]);}tracing::debug!(diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rsindex b26d1c6..f20c435 100644--- a/crates/gecko/src/host.rs+++ b/crates/gecko/src/host.rs@@ -108,7 +108,7 @@ pub struct DrawVertex {pub color0: [f32; 4],pub color1: [f32; 4],pub pos_view: [f32; 3],- pub texcoords: [[f32; 2]; 8],+ pub texcoords: [[f32; 3]; 8],}/// Per-light snapshot for the draw call.