A GameCube/Wii emulator and debugger written in Rust.
fix lighting calculation + implement lighting based tex coords
Layle21c2048f8025
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rsindex e845ce7..303e706 100644--- a/crates/backend-wgpu/src/action.rs+++ b/crates/backend-wgpu/src/action.rs@@ -84,7 +84,7 @@ fn build_frame_uniform(indirect_refs: draw.indirect_refs,num_indirect_stages: draw.num_indirect_stages as u32,bump_imask: draw.bump_imask,- _pad1: 0,+ texgen_types: draw.texgen_types,tev_indirect: pack_u32_slice_to_uvec4x4(&draw.tev_indirect),light_colors: draw.lights.each_ref().map(|l| Vec4::from(l.color)),light_cosatt: draw.lights.each_ref().map(|l| Vec4::from(l.cosatt)),diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rsindex bba76a1..a84993e 100644--- a/crates/backend-wgpu/src/lib.rs+++ b/crates/backend-wgpu/src/lib.rs@@ -182,7 +182,7 @@ pub(crate) struct FrameUniforms {pub indirect_refs: u32,pub num_indirect_stages: u32,pub bump_imask: u32,- pub _pad1: u32,+ pub texgen_types: u32,// Per-TEV-stage IND_CMD, packed four per UVec4.pub tev_indirect: [glam::UVec4; 4],pub light_colors: [glam::Vec4; 8],diff --git a/crates/backend-wgpu/src/shaders/common.wesl b/crates/backend-wgpu/src/shaders/common.weslindex b49bd2c..31611b0 100644--- a/crates/backend-wgpu/src/shaders/common.wesl+++ b/crates/backend-wgpu/src/shaders/common.wesl@@ -20,6 +20,7 @@ struct FrameUniforms {indirect_refs: u32,num_indirect_stages: u32,bump_imask: u32,+ texgen_types: u32,tev_indirect: array<vec4<u32>, 4>,// XF lighting statelight_colors: array<vec4<f32>, 8>,diff --git a/crates/backend-wgpu/src/shaders/lighting.wesl b/crates/backend-wgpu/src/shaders/lighting.weslindex dae3c14..8313265 100644--- a/crates/backend-wgpu/src/shaders/lighting.wesl+++ b/crates/backend-wgpu/src/shaders/lighting.wesl@@ -27,7 +27,7 @@ fn compute_light_factor(ctrl: u32, light_idx: u32, normal: vec3<f32>, pos: vec3<} else if attn_select {let dist = length(ldir);ldir = normalize(ldir);- let cos_angle = dot(light_dir, ldir);+ let cos_angle = max(dot(light_dir, ldir), 0.0);let angle_attn = max(cosatt.x + cosatt.y * cos_angle + cosatt.z * cos_angle * cos_angle, 0.0);attn = safe_div(angle_attn, distatt.x + distatt.y * dist + distatt.z * dist * dist);} else {diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.weslindex 5a3a539..d8749d6 100644--- a/crates/backend-wgpu/src/shaders/main.wesl+++ b/crates/backend-wgpu/src/shaders/main.wesl@@ -114,6 +114,14 @@ fn dynamic_tev_indirect(stage: u32) -> u32 {return frame.tev_indirect[stage / 4u][stage % 4u];}+fn color_texgen(slot: u32, regular: vec3<f32>, color0: vec4<f32>, color1: vec4<f32>) -> vec3<f32> {+ switch extract_bits(frame.texgen_types, slot * 3u, 3u) {+ case 2u: { return vec3(color0.rg, 1.0); }+ case 3u: { return vec3(color1.rg, 1.0); }+ default: { return regular; }+ }+}+@vertexfn vs_main(in: VsIn) -> VsOut {var out: VsOut;@@ -132,14 +140,14 @@ fn vs_main(in: VsIn) -> VsOut {in.color1, in.normal, in.pos_view,);- @if(TEXCOORD_0_ENABLED) { out.uv0 = in.tex0; } @else { out.uv0 = vec3<f32>(0.0, 0.0, 1.0); }- @if(TEXCOORD_1_ENABLED) { out.uv1 = in.tex1; } @else { out.uv1 = vec3<f32>(0.0, 0.0, 1.0); }- @if(TEXCOORD_2_ENABLED) { out.uv2 = in.tex2; } @else { out.uv2 = vec3<f32>(0.0, 0.0, 1.0); }- @if(TEXCOORD_3_ENABLED) { out.uv3 = in.tex3; } @else { out.uv3 = vec3<f32>(0.0, 0.0, 1.0); }- @if(TEXCOORD_4_ENABLED) { out.uv4 = in.tex4; } @else { out.uv4 = vec3<f32>(0.0, 0.0, 1.0); }- @if(TEXCOORD_5_ENABLED) { out.uv5 = in.tex5; } @else { out.uv5 = vec3<f32>(0.0, 0.0, 1.0); }- @if(TEXCOORD_6_ENABLED) { out.uv6 = in.tex6; } @else { out.uv6 = vec3<f32>(0.0, 0.0, 1.0); }- @if(TEXCOORD_7_ENABLED) { out.uv7 = in.tex7; } @else { out.uv7 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_0_ENABLED) { out.uv0 = color_texgen(0u, in.tex0, out.color, out.color1); } @else { out.uv0 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_1_ENABLED) { out.uv1 = color_texgen(1u, in.tex1, out.color, out.color1); } @else { out.uv1 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_2_ENABLED) { out.uv2 = color_texgen(2u, in.tex2, out.color, out.color1); } @else { out.uv2 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_3_ENABLED) { out.uv3 = color_texgen(3u, in.tex3, out.color, out.color1); } @else { out.uv3 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_4_ENABLED) { out.uv4 = color_texgen(4u, in.tex4, out.color, out.color1); } @else { out.uv4 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_5_ENABLED) { out.uv5 = color_texgen(5u, in.tex5, out.color, out.color1); } @else { out.uv5 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_6_ENABLED) { out.uv6 = color_texgen(6u, in.tex6, out.color, out.color1); } @else { out.uv6 = vec3<f32>(0.0, 0.0, 1.0); }+ @if(TEXCOORD_7_ENABLED) { out.uv7 = color_texgen(7u, in.tex7, out.color, out.color1); } @else { out.uv7 = vec3<f32>(0.0, 0.0, 1.0); }return out;}diff --git a/crates/gecko/src/flipper/gx/vertex.rs b/crates/gecko/src/flipper/gx/vertex.rsindex c2a21e4..cd44db2 100644--- a/crates/gecko/src/flipper/gx/vertex.rs+++ b/crates/gecko/src/flipper/gx/vertex.rs@@ -278,6 +278,9 @@ impl GraphicsProcessor {material_color: self.cached_material_color,lights: self.cached_lights,active_texcoords: (self.xf_mem[crate::flipper::gx::constants::XF_NUM_TEXGENS] as u8).min(8),+ texgen_types: (0..8).fold(0, |types, i| {+ types | ((TexGenReg::from_raw(self.xf_mem[XF_TEXGEN_BASE + i]).texgen_type() as u32) << (i * 3))+ }),texcoord_scales: std::array::from_fn(|i| (self.bp_regs[BP_SU_SSIZE0 + i] & 0xffff) + 1),ztex_bias: TevZtex1::from_raw(self.bp_regs[BP_TEV_ZTEX1]).bias(),ztex_type: ztex2.tex_type(),diff --git a/crates/gecko/src/flipper/gx/xf.rs b/crates/gecko/src/flipper/gx/xf.rsindex 0dda05a..16df4ec 100644--- a/crates/gecko/src/flipper/gx/xf.rs+++ b/crates/gecko/src/flipper/gx/xf.rs@@ -7,6 +7,7 @@ use crate::mmio::RamView;const XF_LIGHT_END: usize = XF_LIGHT_BASE + 8 * XF_LIGHT_STRIDE;const XF_CHAN_CFG_BEGIN: usize = XF_AMBIENT_COLOR0;const XF_CHAN_CFG_END: usize = XF_ALPHA_CTRL1 + 1;+const XF_TEXGEN_END: usize = XF_TEXGEN_BASE + 8;#[inline(always)]fn ranges_overlap(a_begin: usize, a_end: usize, b_begin: usize, b_end: usize) -> bool {@@ -18,6 +19,7 @@ struct XfDirty {projection: bool,viewport: bool,lighting: bool,+ texgen: bool,}impl GraphicsProcessor {@@ -27,7 +29,8 @@ impl GraphicsProcessor {let tracked = self::ranges_overlap(base, end, XF_PROJECTION_BASE, XF_PROJECTION_END + 1)|| self::ranges_overlap(base, end, XF_VIEWPORT_BASE, XF_VIEWPORT_END + 1)|| self::ranges_overlap(base, end, XF_LIGHT_BASE, XF_LIGHT_END)- || self::ranges_overlap(base, end, XF_CHAN_CFG_BEGIN, XF_CHAN_CFG_END);+ || self::ranges_overlap(base, end, XF_CHAN_CFG_BEGIN, XF_CHAN_CFG_END)+ || self::ranges_overlap(base, end, XF_NUM_TEXGENS, XF_TEXGEN_END);let mut dirty = XfDirty::default();@@ -35,6 +38,7 @@ impl GraphicsProcessor {let reg = base + i;if reg < self.xf_mem.len() {if tracked && self.xf_mem[reg] != val {+ dirty.texgen |= (XF_NUM_TEXGENS..XF_TEXGEN_END).contains(®);dirty.projection |= (XF_PROJECTION_BASE..=XF_PROJECTION_END).contains(®);dirty.viewport |= (XF_VIEWPORT_BASE..=XF_VIEWPORT_END).contains(®);dirty.lighting |= (XF_LIGHT_BASE..XF_LIGHT_END).contains(®)@@ -67,6 +71,10 @@ impl GraphicsProcessor {renderer.exec(GxAction::SetViewport(self.cur_viewport));}+ if dirty.texgen {+ self.frame_state_dirty = true;+ }+if dirty.lighting {self.lighting_dirty = true;self.frame_state_dirty = true;diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rsindex e056f92..30185ba 100644--- a/crates/gecko/src/host.rs+++ b/crates/gecko/src/host.rs@@ -200,6 +200,7 @@ pub struct DrawData {pub struct DrawState {pub dst_alpha: Option<u8>,pub active_texcoords: u8,+ pub texgen_types: u32,pub texcoord_scales: [u32; 16],pub zfreeze: Option<crate::flipper::gx::depth::DepthPlane>,// TEV combiner state