A GameCube/Wii emulator and debugger written in Rust.
color1 & tev channel order
Layle43659ed7c3b8
diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rsindex daf2c35..c366646 100644--- a/crates/backend-wgpu/src/pipeline.rs+++ b/crates/backend-wgpu/src/pipeline.rs@@ -42,65 +42,71 @@ impl GxRenderer {offset: 0,shader_location: 0,},- // color: vec4<f32>+ // color0: vec4<f32>wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x4,offset: 12,shader_location: 1,},- // normal: vec3<f32>+ // color1: vec4<f32>wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x3,+ format: wgpu::VertexFormat::Float32x4,offset: 28,shader_location: 2,},- // pos_view: vec3<f32>+ // normal: vec3<f32>wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x3,- offset: 40,+ offset: 44,shader_location: 3,},- // tex0-tex7: vec2<f32> each+ // pos_view: vec3<f32>wgpu::VertexAttribute {- format: wgpu::VertexFormat::Float32x2,- offset: 52,+ format: wgpu::VertexFormat::Float32x3,+ offset: 56,shader_location: 4,},+ // tex0-tex7: vec2<f32> eachwgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x2,- offset: 60,+ offset: 68,shader_location: 5,},wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x2,- offset: 68,+ offset: 76,shader_location: 6,},wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x2,- offset: 76,+ offset: 84,shader_location: 7,},wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x2,- offset: 84,+ offset: 92,shader_location: 8,},wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x2,- offset: 92,+ offset: 100,shader_location: 9,},wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x2,- offset: 100,+ offset: 108,shader_location: 10,},wgpu::VertexAttribute {format: wgpu::VertexFormat::Float32x2,- offset: 108,+ offset: 116,shader_location: 11,},+ wgpu::VertexAttribute {+ format: wgpu::VertexFormat::Float32x2,+ offset: 124,+ shader_location: 12,+ },],};diff --git a/crates/backend-wgpu/src/shaders/common.wesl b/crates/backend-wgpu/src/shaders/common.weslindex 0f8a794..fe6980c 100644--- a/crates/backend-wgpu/src/shaders/common.wesl+++ b/crates/backend-wgpu/src/shaders/common.wesl@@ -55,27 +55,29 @@ struct DrawUniforms {struct VsIn {@location(0) position: vec3<f32>,@location(1) color: vec4<f32>,- @location(2) normal: vec3<f32>,- @location(3) pos_view: vec3<f32>,- @location(4) tex0: vec2<f32>,- @location(5) tex1: vec2<f32>,- @location(6) tex2: vec2<f32>,- @location(7) tex3: vec2<f32>,- @location(8) tex4: vec2<f32>,- @location(9) tex5: vec2<f32>,- @location(10) tex6: vec2<f32>,- @location(11) tex7: vec2<f32>,+ @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>,};struct VsOut {@builtin(position) clip_pos: vec4<f32>,@location(0) color: vec4<f32>,- @location(1) uv0: vec2<f32>,- @location(2) uv1: vec2<f32>,- @location(3) uv2: vec2<f32>,- @location(4) uv3: vec2<f32>,- @location(5) uv4: vec2<f32>,- @location(6) uv5: vec2<f32>,- @location(7) uv6: vec2<f32>,- @location(8) uv7: vec2<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>,};diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.weslindex 422780a..bb21cf5 100644--- a/crates/backend-wgpu/src/shaders/main.wesl+++ b/crates/backend-wgpu/src/shaders/main.wesl@@ -15,6 +15,7 @@ fn vs_main(in: VsIn) -> VsOut {frame.ambient_color, frame.material_color,in.color, in.normal, in.pos_view,);+ out.color1 = in.color1;out.uv0 = in.tex0;out.uv1 = in.tex1;out.uv2 = in.tex2;@@ -35,7 +36,7 @@ fn fs_main(in: VsOut) -> @location(0) vec4<f32> {regs[2] = frame.tev_color_reg2;regs[3] = frame.tev_color_reg3;- let ras_color = in.color;+ let color0 = in.color;let num_stages = frame.num_tev_stages;var last_color_dest = 0u;@@ -47,11 +48,26 @@ fn fs_main(in: VsOut) -> @location(0) vec4<f32> {}// Resolve per-stage texture from TevOrder- // TevStageOrder bit layout: texmap(0-2), texcoord(3-5), tex_enable(6)+ // TevStageOrder bit layout: texmap(0-2), texcoord(3-5), tex_enable(6), channel(7-9)let order = read_packed(frame.tev_orders, stage);let texmap_idx = order & 7u;let texcoord_idx = (order >> 3u) & 7u;let tex_enabled = ((order >> 6u) & 1u) != 0u;+ let ras_channel = (order >> 7u) & 7u;++ // Select raster color based on channel:+ // 0=Color0, 1=Color1, 2=Alpha0, 3=Alpha1, 4=Color0A0, 5=Color1A1, 6=Zero, 7=Bump+ var ras_color: vec4<f32>;+ switch ras_channel {+ case 0u: { ras_color = color0; } // Color0+ case 1u: { ras_color = in.color1; } // Color1+ case 2u: { ras_color = vec4(color0.a, color0.a, color0.a, color0.a); } // Alpha0+ case 3u: { ras_color = vec4(in.color1.a, in.color1.a, in.color1.a, in.color1.a); } // Alpha1+ case 4u: { ras_color = color0; } // Color0A0+ case 5u: { ras_color = in.color1; } // Color1A1+ case 6u: { ras_color = vec4(0.0, 0.0, 0.0, 0.0); } // Zero+ default: { ras_color = vec4(0.0, 0.0, 0.0, 0.0); } // Bump (TODO: not implemented)+ }var tex_color: vec4<f32>;if tex_enabled {diff --git a/crates/backend-wgpu/src/triangulate.rs b/crates/backend-wgpu/src/triangulate.rsindex 8696438..11eecc2 100644--- a/crates/backend-wgpu/src/triangulate.rs+++ b/crates/backend-wgpu/src/triangulate.rs@@ -5,6 +5,7 @@ use gecko::flipper::gx::draw::{DrawCall, Primitive};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; 2],@@ -23,6 +24,7 @@ impl From<&gecko::flipper::gx::draw::Vertex> for GpuVertex {Self {position: v.position,color: v.color0,+ color1: v.color1,normal: v.normal,pos_view: v.pos_view,tex0: tc(0),diff --git a/crates/gecko/src/flipper/gx/draw.rs b/crates/gecko/src/flipper/gx/draw.rsindex 073ff6e..31d0f06 100644--- a/crates/gecko/src/flipper/gx/draw.rs+++ b/crates/gecko/src/flipper/gx/draw.rs@@ -38,6 +38,7 @@ impl Primitive {pub struct Vertex {pub position: [f32; 3],pub color0: [f32; 4],+ pub color1: [f32; 4],pub normal: [f32; 3],pub pos_view: [f32; 3],pub texcoords: [Option<[f32; 2]>; 8],diff --git a/crates/gecko/src/flipper/gx/regs.rs b/crates/gecko/src/flipper/gx/regs.rsindex 0d2ce43..37f5ef2 100644--- a/crates/gecko/src/flipper/gx/regs.rs+++ b/crates/gecko/src/flipper/gx/regs.rs@@ -622,6 +622,9 @@ pub struct TevStageOrder {#[bits(6)]pub tex_enable: bool,++ #[bits(7..=9)]+ pub channel: RasterChannel,}#[chapa::bitfield(u32, order = lsb0)]diff --git a/crates/gecko/src/flipper/gx/tev.rs b/crates/gecko/src/flipper/gx/tev.rsindex 0fe637a..7cfef44 100644--- a/crates/gecko/src/flipper/gx/tev.rs+++ b/crates/gecko/src/flipper/gx/tev.rs@@ -10,11 +10,13 @@ impl GraphicsProcessor {orders[2 * i] = TevStageOrder::default().with_texmap(reg.texmap0()).with_texcoord(reg.texcoord0())- .with_tex_enable(reg.tex_enable0());+ .with_tex_enable(reg.tex_enable0())+ .with_channel(reg.channel0());orders[2 * i + 1] = TevStageOrder::default().with_texmap(reg.texmap1()).with_texcoord(reg.texcoord1())- .with_tex_enable(reg.tex_enable1());+ .with_tex_enable(reg.tex_enable1())+ .with_channel(reg.channel1());}orders}diff --git a/crates/gecko/src/flipper/gx/vertex.rs b/crates/gecko/src/flipper/gx/vertex.rsindex 954cfea..f37b0af 100644--- a/crates/gecko/src/flipper/gx/vertex.rs+++ b/crates/gecko/src/flipper/gx/vertex.rs@@ -1,6 +1,6 @@use super::constants::{- ARRAY_BASE_REG, ARRAY_CLR0, ARRAY_NRM, ARRAY_POS, ARRAY_STRIDE_REG, ARRAY_TEX0, VATA_REG, VATB_REG, VATC_REG,- VCD_HI_REG, VCD_LO_REG, XF_ALPHA_CTRL0, XF_AMBIENT_COLOR0, XF_COLOR_CTRL0, XF_LIGHT_A0, XF_LIGHT_BASE,+ ARRAY_BASE_REG, ARRAY_CLR0, ARRAY_CLR1, ARRAY_NRM, ARRAY_POS, ARRAY_STRIDE_REG, ARRAY_TEX0, VATA_REG, VATB_REG,+ VATC_REG, VCD_HI_REG, VCD_LO_REG, XF_ALPHA_CTRL0, XF_AMBIENT_COLOR0, XF_COLOR_CTRL0, XF_LIGHT_A0, XF_LIGHT_BASE,XF_LIGHT_COLOR, XF_LIGHT_K0, XF_LIGHT_NX, XF_LIGHT_PX, XF_LIGHT_STRIDE, XF_MATERIAL_COLOR0, XF_MATRIX_INDEX_A,XF_NRM_MTX_BASE, XF_NUM_TEXGENS, XF_POS_MTX_STRIDE,};@@ -16,9 +16,6 @@ struct VertexFormat {pos_base: usize,pos_stride: usize,pos_data_size: usize,- clr0_base: usize,- clr0_stride: usize,- clr0_data_size: usize,nrm_base_addr: usize,nrm_stride: usize,nrm_data_size: usize,@@ -36,7 +33,13 @@ struct VertexFormat {pos_attr: AttributeType,nrm_attr: AttributeType,clr0_attr: AttributeType,- clr1_stream_size: usize,+ clr0_base: usize,+ clr0_stride: usize,+ clr0_data_size: usize,+ clr1_attr: AttributeType,+ clr1_base: usize,+ clr1_stride: usize,+ clr1_data_size: usize,}impl GraphicsProcessor {@@ -131,7 +134,9 @@ impl GraphicsProcessor {let pos_stream_size = attr_stream_size(pos_attr, pos_data_size);let nrm_stream_size = vat_a.nrm_stream_size(nrm_attr);let clr0_stream_size = attr_stream_size(clr0_attr, clr0_data_size);- let clr1_stream_size = attr_stream_size(vcd_lo.color1(), vat_a.clr1_data_size());+ let clr1_attr = vcd_lo.color1();+ let clr1_data_size = vat_a.clr1_data_size();+ let clr1_stream_size = attr_stream_size(clr1_attr, clr1_data_size);let tex_attrs = [vcd_hi.tex0(),@@ -225,7 +230,10 @@ impl GraphicsProcessor {pos_attr,nrm_attr,clr0_attr,- clr1_stream_size,+ clr1_attr,+ clr1_base: self.cp_regs[ARRAY_BASE_REG + ARRAY_CLR1] as usize,+ clr1_stride: self.cp_regs[ARRAY_STRIDE_REG + ARRAY_CLR1] as usize,+ clr1_data_size,}}@@ -292,15 +300,25 @@ impl GraphicsProcessor {} else if vf.clr0_attr == AttributeType::Direct {let start = cur.position() as usize;cur.set_position(cur.position() + vf.clr0_data_size as u64);- decode_color(&data[start..start + vf.clr0_data_size], &vf.vat_a)+ decode_color(&data[start..start + vf.clr0_data_size], vf.vat_a.clr0_fmt(), vf.vat_a.clr0_cnt())} else {let clr0_index = read_index(cur, vf.clr0_attr);let clr0_addr = vf.clr0_base + clr0_index * vf.clr0_stride;- decode_color(&ram[clr0_addr..clr0_addr + vf.clr0_data_size], &vf.vat_a)+ decode_color(&ram[clr0_addr..clr0_addr + vf.clr0_data_size], vf.vat_a.clr0_fmt(), vf.vat_a.clr0_cnt())};- // Skip color1 (TODO: not yet used for rendering)- cur.set_position(cur.position() + vf.clr1_stream_size as u64);+ // Read color1+ let color1 = if vf.clr1_attr == AttributeType::None {+ [1.0, 1.0, 1.0, 1.0]+ } else if vf.clr1_attr == AttributeType::Direct {+ let start = cur.position() as usize;+ cur.set_position(cur.position() + vf.clr1_data_size as u64);+ decode_color(&data[start..start + vf.clr1_data_size], vf.vat_a.clr1_fmt(), vf.vat_a.clr1_cnt())+ } else {+ let clr1_index = read_index(cur, vf.clr1_attr);+ let clr1_addr = vf.clr1_base + clr1_index * vf.clr1_stride;+ decode_color(&ram[clr1_addr..clr1_addr + vf.clr1_data_size], vf.vat_a.clr1_fmt(), vf.vat_a.clr1_cnt())+ };// Read all texcoords (tex0-tex7)let mut raw_texcoords: [Option<[f32; 2]>; 8] = [None; 8];@@ -356,6 +374,7 @@ impl GraphicsProcessor {draw::Vertex {position,color0,+ color1,normal: [normal_view.0, normal_view.1, normal_view.2],pos_view: [pos_view.0, pos_view.1, pos_view.2],texcoords,@@ -466,9 +485,9 @@ fn decode_position(data: &[u8], vat: &VatA) -> [f32; 3] {result}-fn decode_color(data: &[u8], vat: &VatA) -> [f32; 4] {- let has_alpha = vat.clr0_cnt() == regs::ColorCount::Rgba;- match vat.clr0_fmt() {+fn decode_color(data: &[u8], fmt: regs::ColorFormat, cnt: regs::ColorCount) -> [f32; 4] {+ let has_alpha = cnt == regs::ColorCount::Rgba;+ match fmt {regs::ColorFormat::Rgb565 => {let raw = u16::from_be_bytes([data[0], data[1]]);let r = ((raw >> 11) & 0x1F) as f32 / 31.0;