Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

lighting on gpu and refactored a bit more

Layle5d6da7b6da3f
diff --git a/Cargo.lock b/Cargo.lock
index b21efd7..d5d0e01 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -226,7 +226,9 @@ name = "backend-wgpu"
version = "0.1.0"
dependencies = [
"bytemuck",
+ "encase",
"gecko",
+ "glam",
"wesl",
"wgpu",
]
@@ -574,6 +576,15 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "const_panic"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e262cdaac42494e3ae34c43969f9cdeb7da178bdb4b66fa6a1ea2edb4c8ae652"
+dependencies = [
+ "typewit",
+]
+
[[package]]
name = "convert_case"
version = "0.10.0"
@@ -958,6 +969,37 @@ dependencies = [
"log",
]
+[[package]]
+name = "encase"
+version = "0.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6e3e0ff2ee0b7aa97428308dd9e1e42369cb22f5fb8dc1c55546637443a60f1e"
+dependencies = [
+ "const_panic",
+ "encase_derive",
+ "thiserror 2.0.18",
+]
+
+[[package]]
+name = "encase_derive"
+version = "0.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4d90c5d7d527c6cb8a3b114efd26a6304d9ab772656e73d8f4e32b1f3d601a2"
+dependencies = [
+ "encase_derive_impl",
+]
+
+[[package]]
+name = "encase_derive_impl"
+version = "0.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c8bad72d8308f7a382de2391ec978ddd736e0103846b965d7e2a63a75768af30"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "epaint"
version = "0.33.3"
@@ -1249,6 +1291,15 @@ dependencies = [
"xml-rs",
]
+[[package]]
+name = "glam"
+version = "0.32.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f70749695b063ecbf6b62949ccccde2e733ec3ecbbd71d467dca4e5c6c97cca0"
+dependencies = [
+ "encase",
+]
+
[[package]]
name = "glow"
version = "0.16.0"
@@ -3348,6 +3399,12 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
+[[package]]
+name = "typewit"
+version = "1.14.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8c1ae7cc0fdb8b842d65d127cb981574b0d2b249b74d1c7a2986863dc134f71"
+
[[package]]
name = "unicode-ident"
version = "1.0.24"
diff --git a/crates/backend-wgpu/Cargo.toml b/crates/backend-wgpu/Cargo.toml
index 3f36cb8..cc3c056 100644
--- a/crates/backend-wgpu/Cargo.toml
+++ b/crates/backend-wgpu/Cargo.toml
@@ -8,6 +8,8 @@ wgpu = "28.0.0"
bytemuck = { version = "1.25.0", features = ["derive"] }
gecko = { path = "../gecko" }
wesl = "0.3.2"
+encase = "0.12.0"
+glam = { version = "0.32.1", features = ["encase"] }
[build-dependencies]
wesl = "0.3.2"
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index f7abb65..6bb4a3d 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -4,33 +4,40 @@ mod render;
pub mod texture;
mod triangulate;
+use encase::ShaderType as _;
use gecko::flipper::gx::draw::TextureFormat;
use gecko::flipper::gx::regs::{MagFilter, MinFilter, WrapMode};
use pipeline::PipelineKey;
use std::collections::HashMap;
use triangulate::{GpuVertex, align_up};
-#[repr(C)]
-#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+#[derive(encase::ShaderType)]
pub(crate) struct FrameUniforms {
- tev_color_regs: [[f32; 4]; 4],
- tev_konst_colors: [[f32; 4]; 16],
- tev_color_env: [u32; 16],
- tev_alpha_env: [u32; 16],
- tev_orders: [u32; 16],
+ tev_color_regs: [glam::Vec4; 4],
+ tev_konst_colors: [glam::Vec4; 16],
+ tev_color_env: [glam::UVec4; 4],
+ tev_alpha_env: [glam::UVec4; 4],
+ tev_orders: [glam::UVec4; 4],
num_tev_stages: u32,
alpha_ref0: f32,
alpha_ref1: f32,
alpha_comp0: u32,
alpha_comp1: u32,
alpha_op: u32,
- _padding: [u32; 2],
+ light_colors: [glam::Vec4; 8],
+ light_cosatt: [glam::Vec4; 8],
+ light_distatt: [glam::Vec4; 8],
+ light_pos: [glam::Vec4; 8],
+ light_dir: [glam::Vec4; 8],
+ color_ctrl: u32,
+ alpha_ctrl: u32,
+ ambient_color: glam::Vec4,
+ material_color: glam::Vec4,
}
-#[repr(C)]
-#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
+#[derive(encase::ShaderType)]
pub(crate) struct DrawUniforms {
- mvp: [[f32; 4]; 4],
+ mvp: glam::Mat4,
}
const SHADER: &str = wesl::include_wesl!("gx_shader");
@@ -67,8 +74,8 @@ impl GxRenderer {
width: u32,
height: u32,
) -> Self {
- let frame_uniform_size = std::mem::size_of::<FrameUniforms>() as u64;
- let draw_uniform_size = std::mem::size_of::<DrawUniforms>() as u64;
+ let frame_uniform_size = FrameUniforms::min_size().get();
+ let draw_uniform_size = DrawUniforms::min_size().get();
let draw_uniform_stride = align_up(
draw_uniform_size,
device.limits().min_uniform_buffer_offset_alignment as u64,
@@ -87,7 +94,7 @@ impl GxRenderer {
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
- min_binding_size: wgpu::BufferSize::new(frame_uniform_size),
+ min_binding_size: Some(FrameUniforms::min_size()),
},
count: None,
},
@@ -97,7 +104,7 @@ impl GxRenderer {
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
- min_binding_size: wgpu::BufferSize::new(draw_uniform_size),
+ min_binding_size: Some(DrawUniforms::min_size()),
},
count: None,
},
@@ -170,7 +177,6 @@ impl GxRenderer {
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
view_formats: &[],
});
- // White pixel
queue.write_texture(
fallback_texture.as_image_copy(),
&[255u8, 255, 255, 255],
diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rs
index eb099e9..daf2c35 100644
--- a/crates/backend-wgpu/src/pipeline.rs
+++ b/crates/backend-wgpu/src/pipeline.rs
@@ -1,4 +1,5 @@
-use crate::{GxRenderer, helpers, triangulate::GpuVertex};
+use crate::triangulate::GpuVertex;
+use crate::{GxRenderer, helpers};
use gecko::flipper::gx::draw::DrawCall;
use gecko::flipper::gx::regs::{BlendFactor, CompareFunc};
@@ -47,47 +48,59 @@ impl GxRenderer {
offset: 12,
shader_location: 1,
},
- // tex0-tex7: vec2<f32> each
+ // normal: vec3<f32>
wgpu::VertexAttribute {
- format: wgpu::VertexFormat::Float32x2,
+ format: wgpu::VertexFormat::Float32x3,
offset: 28,
shader_location: 2,
},
+ // pos_view: vec3<f32>
wgpu::VertexAttribute {
- format: wgpu::VertexFormat::Float32x2,
- offset: 36,
+ format: wgpu::VertexFormat::Float32x3,
+ offset: 40,
shader_location: 3,
},
+ // tex0-tex7: vec2<f32> each
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
- offset: 44,
+ offset: 52,
shader_location: 4,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
- offset: 52,
+ offset: 60,
shader_location: 5,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
- offset: 60,
+ offset: 68,
shader_location: 6,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
- offset: 68,
+ offset: 76,
shader_location: 7,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
- offset: 76,
+ offset: 84,
shader_location: 8,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
- offset: 84,
+ offset: 92,
shader_location: 9,
},
+ wgpu::VertexAttribute {
+ format: wgpu::VertexFormat::Float32x2,
+ offset: 100,
+ shader_location: 10,
+ },
+ wgpu::VertexAttribute {
+ format: wgpu::VertexFormat::Float32x2,
+ offset: 108,
+ shader_location: 11,
+ },
],
};
diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rs
index 41adfbf..bb0c048 100644
--- a/crates/backend-wgpu/src/render.rs
+++ b/crates/backend-wgpu/src/render.rs
@@ -1,11 +1,10 @@
-use crate::{
- DrawUniforms, FrameUniforms, GxRenderer, helpers,
- pipeline::PipelineKey,
- texture,
- triangulate::{self, GpuVertex, align_up},
-};
+use crate::pipeline::PipelineKey;
+use crate::triangulate::{self, GpuVertex, align_up};
+use crate::{DrawUniforms, FrameUniforms, GxRenderer, helpers, texture};
+use encase::{ShaderType as _, UniformBuffer};
use gecko::flipper::gx::draw::DrawCommands;
use gecko::flipper::gx::regs::{MagFilter, MinFilter, WrapMode};
+use glam::{Mat4, UVec4, Vec4};
impl GxRenderer {
pub fn render(
@@ -64,7 +63,6 @@ impl GxRenderer {
}
}
- // Ensure fallback sampler exists
let fallback_sampler_key = (WrapMode::Clamp, WrapMode::Clamp, MagFilter::Linear, MinFilter::Linear);
if !self.sampler_cache.contains_key(&fallback_sampler_key) {
let s = device.create_sampler(&wgpu::SamplerDescriptor {
@@ -81,12 +79,12 @@ impl GxRenderer {
self.scratch_uniform_bytes.clear();
let draw_stride = self.draw_uniform_stride as usize;
- let draw_size = std::mem::size_of::<DrawUniforms>();
+ let draw_encase_size = DrawUniforms::min_size().get() as usize;
let frame_stride = align_up(
- std::mem::size_of::<FrameUniforms>() as u64,
+ FrameUniforms::min_size().get(),
device.limits().min_uniform_buffer_offset_alignment as u64,
) as usize;
- let frame_size = std::mem::size_of::<FrameUniforms>();
+ let frame_encase_size = FrameUniforms::min_size().get() as usize;
let mut frame_uniform_bytes: Vec<u8> = Vec::new();
let mut draw_call_indices: Vec<usize> = Vec::new();
@@ -99,29 +97,43 @@ impl GxRenderer {
}
let mvp = commands.projection * dc.modelview;
- let draw_uniform = DrawUniforms { mvp: mvp.0 };
+ let draw_uniform = DrawUniforms {
+ mvp: Mat4::from_cols_array_2d(&mvp.0),
+ };
+
let start = self.scratch_draws.len() * draw_stride;
self.scratch_uniform_bytes.resize(start + draw_stride, 0);
- self.scratch_uniform_bytes[start..start + draw_size].copy_from_slice(bytemuck::bytes_of(&draw_uniform));
+ let mut draw_buf = UniformBuffer::new(&mut self.scratch_uniform_bytes[start..start + draw_encase_size]);
+ draw_buf.write(&draw_uniform).unwrap();
let alpha_cmp = dc.bp_alpha_compare;
let frame_uniform = FrameUniforms {
- tev_color_regs: dc.tev_color_regs,
- tev_konst_colors: dc.tev_konst_colors,
- tev_color_env: dc.tev_color_env.map(|e| e.raw()),
- tev_alpha_env: dc.tev_alpha_env.map(|e| e.raw()),
- tev_orders: dc.tev_orders.map(|o| o.raw()),
+ tev_color_regs: dc.tev_color_regs.map(Vec4::from),
+ tev_konst_colors: dc.tev_konst_colors.map(Vec4::from),
+ tev_color_env: pack_u32x16_to_uvec4x4(&dc.tev_color_env.map(|e| e.raw())),
+ tev_alpha_env: pack_u32x16_to_uvec4x4(&dc.tev_alpha_env.map(|e| e.raw())),
+ tev_orders: pack_u32x16_to_uvec4x4(&dc.tev_orders.map(|o| o.raw())),
num_tev_stages: dc.num_tev_stages as u32,
alpha_ref0: alpha_cmp.ref0() as f32 / 255.0,
alpha_ref1: alpha_cmp.ref1() as f32 / 255.0,
alpha_comp0: alpha_cmp.comp0() as u32,
alpha_comp1: alpha_cmp.comp1() as u32,
alpha_op: alpha_cmp.op() as u32,
- _padding: [0; 2],
+ light_colors: dc.light_colors.map(Vec4::from),
+ light_cosatt: dc.light_cosatt.map(Vec4::from),
+ light_distatt: dc.light_distatt.map(Vec4::from),
+ light_pos: dc.light_pos.map(Vec4::from),
+ light_dir: dc.light_dir.map(Vec4::from),
+ color_ctrl: dc.color_ctrl.raw(),
+ alpha_ctrl: dc.alpha_ctrl.raw(),
+ ambient_color: Vec4::from(dc.ambient_color),
+ material_color: Vec4::from(dc.material_color),
};
+
let fstart = self.scratch_draws.len() * frame_stride;
frame_uniform_bytes.resize(fstart + frame_stride, 0);
- frame_uniform_bytes[fstart..fstart + frame_size].copy_from_slice(bytemuck::bytes_of(&frame_uniform));
+ let mut frame_buf = UniformBuffer::new(&mut frame_uniform_bytes[fstart..fstart + frame_encase_size]);
+ frame_buf.write(&frame_uniform).unwrap();
self.scratch_draws.push((prev_len as u32, added as u32));
draw_call_indices.push(dc_idx);
@@ -145,7 +157,7 @@ impl GxRenderer {
}
let frame_stride = align_up(
- std::mem::size_of::<FrameUniforms>() as u64,
+ FrameUniforms::min_size().get(),
device.limits().min_uniform_buffer_offset_alignment as u64,
) as usize;
let needed_frame_size = (num_draws * frame_stride) as u64;
@@ -173,7 +185,7 @@ impl GxRenderer {
) {
let fallback_sampler_key = (WrapMode::Clamp, WrapMode::Clamp, MagFilter::Linear, MinFilter::Linear);
let frame_stride = align_up(
- std::mem::size_of::<FrameUniforms>() as u64,
+ FrameUniforms::min_size().get(),
device.limits().min_uniform_buffer_offset_alignment as u64,
) as usize;
@@ -210,7 +222,6 @@ impl GxRenderer {
let pipeline = &self.pipeline_cache[&pipeline_key];
rpass.set_pipeline(pipeline);
- // Resolve all 8 texture views + samplers for this draw call
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];
for slot in 0..8 {
@@ -231,7 +242,7 @@ impl GxRenderer {
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &self.frame_uniform_buffer,
offset: frame_offset,
- size: wgpu::BufferSize::new(std::mem::size_of::<FrameUniforms>() as u64),
+ size: Some(FrameUniforms::min_size()),
}),
},
wgpu::BindGroupEntry {
@@ -239,7 +250,7 @@ impl GxRenderer {
resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding {
buffer: &self.draw_uniform_buffer,
offset: 0,
- size: wgpu::BufferSize::new(std::mem::size_of::<DrawUniforms>() as u64),
+ size: Some(DrawUniforms::min_size()),
}),
},
];
@@ -283,3 +294,12 @@ impl GxRenderer {
});
}
}
+
+fn pack_u32x16_to_uvec4x4(data: &[u32; 16]) -> [UVec4; 4] {
+ [
+ UVec4::new(data[0], data[1], data[2], data[3]),
+ UVec4::new(data[4], data[5], data[6], data[7]),
+ UVec4::new(data[8], data[9], data[10], data[11]),
+ UVec4::new(data[12], data[13], data[14], data[15]),
+ ]
+}
diff --git a/crates/backend-wgpu/src/shaders/common.wesl b/crates/backend-wgpu/src/shaders/common.wesl
index 00325ec..0f8a794 100644
--- a/crates/backend-wgpu/src/shaders/common.wesl
+++ b/crates/backend-wgpu/src/shaders/common.wesl
@@ -13,19 +13,26 @@ struct FrameUniforms {
alpha_comp0: u32,
alpha_comp1: u32,
alpha_op: u32,
+ // XF lighting state
+ light_colors: array<vec4<f32>, 8>,
+ light_cosatt: array<vec4<f32>, 8>,
+ light_distatt: array<vec4<f32>, 8>,
+ light_pos: array<vec4<f32>, 8>,
+ light_dir: array<vec4<f32>, 8>,
+ color_ctrl: u32,
+ alpha_ctrl: u32,
+ ambient_color: vec4<f32>,
+ material_color: vec4<f32>,
};
struct DrawUniforms {
mvp: mat4x4<f32>,
};
-@group(0) @binding(0)
-var<uniform> frame: FrameUniforms;
+@group(0) @binding(0) var<uniform> frame: FrameUniforms;
+@group(0) @binding(1) var<uniform> draw: DrawUniforms;
-@group(0) @binding(1)
-var<uniform> draw: DrawUniforms;
-
-// 8 texture maps (texmap 0-7)
+// Texmaps 0-7
@group(0) @binding(2) var tex0: texture_2d<f32>;
@group(0) @binding(3) var tex1: texture_2d<f32>;
@group(0) @binding(4) var tex2: texture_2d<f32>;
@@ -35,7 +42,7 @@ var<uniform> draw: DrawUniforms;
@group(0) @binding(8) var tex6: texture_2d<f32>;
@group(0) @binding(9) var tex7: texture_2d<f32>;
-// 8 samplers (paired with texmaps)
+// Samplers 0-7
@group(0) @binding(10) var samp0: sampler;
@group(0) @binding(11) var samp1: sampler;
@group(0) @binding(12) var samp2: sampler;
@@ -48,14 +55,16 @@ var<uniform> draw: DrawUniforms;
struct VsIn {
@location(0) position: vec3<f32>,
@location(1) color: vec4<f32>,
- @location(2) tex0: vec2<f32>,
- @location(3) tex1: vec2<f32>,
- @location(4) tex2: vec2<f32>,
- @location(5) tex3: vec2<f32>,
- @location(6) tex4: vec2<f32>,
- @location(7) tex5: vec2<f32>,
- @location(8) tex6: vec2<f32>,
- @location(9) tex7: vec2<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>,
};
struct VsOut {
diff --git a/crates/backend-wgpu/src/shaders/lighting.wesl b/crates/backend-wgpu/src/shaders/lighting.wesl
new file mode 100644
index 0000000..9735c9b
--- /dev/null
+++ b/crates/backend-wgpu/src/shaders/lighting.wesl
@@ -0,0 +1,161 @@
+import super::common::{FrameUniforms, frame};
+import super::tev_helpers::extract_bits;
+
+// ChanCtrl bit layout (lsb0):
+// [0] mat_src
+// [1] enable
+// [2..5] lit_mask_lo (4 bits)
+// [6] amb_src
+// [7..8] diff_fn (0=None, 1=Signed, 2=Clamp)
+// [9] attn_enable
+// [10] attn_select (1=Spot, 0=Spec, only when attn_enable)
+// [11..14] lit_mask_hi (4 bits)
+
+fn safe_div(n: f32, d: f32) -> f32 {
+ if abs(d) < 1e-10 {
+ return 0.0;
+ }
+ return n / d;
+}
+
+fn compute_light_factor(ctrl: u32, light_idx: u32, normal: vec3<f32>, pos: vec3<f32>) -> f32 {
+ let cosatt = frame.light_cosatt[light_idx].xyz;
+ let distatt = frame.light_distatt[light_idx].xyz;
+ let light_pos = frame.light_pos[light_idx].xyz;
+ let light_dir = frame.light_dir[light_idx].xyz;
+
+ let attn_enable = extract_bits(ctrl, 9u, 1u) != 0u;
+ let attn_select = extract_bits(ctrl, 10u, 1u) != 0u;
+ let diff_fn = extract_bits(ctrl, 7u, 2u);
+
+ var ldir = light_pos - pos;
+ var attn: f32;
+
+ if !attn_enable {
+ // AttnFn::None
+ ldir = normalize(ldir);
+ attn = 1.0;
+ } else if attn_select {
+ // AttnFn::Spot
+ let dist = length(ldir);
+ ldir = normalize(ldir);
+ let cos_angle = dot(light_dir, ldir);
+ 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 {
+ // AttnFn::Spec
+ ldir = normalize(ldir);
+ let half_dot = dot(ldir, normal);
+ if half_dot < 0.0 {
+ return 0.0;
+ }
+ let s = max(dot(normal, light_dir), 0.0);
+ let att_len = vec3(1.0, s, s * s);
+ var da: vec3<f32>;
+ if diff_fn != 0u {
+ da = normalize(distatt);
+ } else {
+ da = distatt;
+ }
+ attn = safe_div(max(dot(att_len, cosatt), 0.0), dot(att_len, da));
+ }
+
+ let dif_attn = dot(ldir, normal);
+ var diffuse: f32;
+ switch diff_fn {
+ case 0u: { diffuse = 1.0; } // None
+ case 1u: { diffuse = dif_attn; } // Signed
+ case 2u: { diffuse = max(dif_attn, 0.0); } // Clamp
+ default: { diffuse = 1.0; }
+ }
+
+ return attn * diffuse;
+}
+
+fn compute_channel_lighting(
+ color_ctrl: u32,
+ alpha_ctrl: u32,
+ ambient: vec4<f32>,
+ material: vec4<f32>,
+ vertex_color: vec4<f32>,
+ normal: vec3<f32>,
+ pos: vec3<f32>,
+) -> vec4<f32> {
+ // RGB material/ambient source selection
+ var mat_rgb: vec3<f32>;
+ if extract_bits(color_ctrl, 0u, 1u) != 0u {
+ mat_rgb = vertex_color.rgb;
+ } else {
+ mat_rgb = material.rgb;
+ }
+
+ var amb_rgb: vec3<f32>;
+ if extract_bits(color_ctrl, 6u, 1u) != 0u {
+ amb_rgb = vertex_color.rgb;
+ } else {
+ amb_rgb = ambient.rgb;
+ }
+
+ // Alpha material/ambient source selection
+ var mat_a: f32;
+ if extract_bits(alpha_ctrl, 0u, 1u) != 0u {
+ mat_a = vertex_color.a;
+ } else {
+ mat_a = material.a;
+ }
+
+ var amb_a: f32;
+ if extract_bits(alpha_ctrl, 6u, 1u) != 0u {
+ amb_a = vertex_color.a;
+ } else {
+ amb_a = ambient.a;
+ }
+
+ let color_lit_off = extract_bits(color_ctrl, 1u, 1u) == 0u;
+ let alpha_lit_off = extract_bits(alpha_ctrl, 1u, 1u) == 0u;
+
+ // When lighting disabled, return material directly
+ if color_lit_off && alpha_lit_off {
+ return vec4(mat_rgb, mat_a);
+ }
+
+ // RGB lighting accumulator
+ var rgb_out = mat_rgb;
+ if !color_lit_off {
+ let mask_lo = extract_bits(color_ctrl, 2u, 4u);
+ let mask_hi = extract_bits(color_ctrl, 11u, 4u);
+ let light_mask = mask_lo | (mask_hi << 4u);
+
+ var acc = amb_rgb;
+ for (var i = 0u; i < 8u; i++) {
+ if ((light_mask >> i) & 1u) == 0u {
+ continue;
+ }
+ let lc = frame.light_colors[i];
+ let factor = compute_light_factor(color_ctrl, i, normal, pos);
+ acc += lc.rgb * factor;
+ }
+ rgb_out = mat_rgb * clamp(acc, vec3(0.0), vec3(1.0));
+ }
+
+ // Alpha lighting accumulator
+ var a_out = mat_a;
+ if !alpha_lit_off {
+ let mask_lo = extract_bits(alpha_ctrl, 2u, 4u);
+ let mask_hi = extract_bits(alpha_ctrl, 11u, 4u);
+ let light_mask = mask_lo | (mask_hi << 4u);
+
+ var acc_a = amb_a;
+ for (var i = 0u; i < 8u; i++) {
+ if ((light_mask >> i) & 1u) == 0u {
+ continue;
+ }
+ let lc = frame.light_colors[i];
+ let factor = compute_light_factor(alpha_ctrl, i, normal, pos);
+ acc_a += lc.a * factor;
+ }
+ a_out = mat_a * clamp(acc_a, 0.0, 1.0);
+ }
+
+ return vec4(rgb_out, a_out);
+}
diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.wesl
index d5ec303..422780a 100644
--- a/crates/backend-wgpu/src/shaders/main.wesl
+++ b/crates/backend-wgpu/src/shaders/main.wesl
@@ -2,6 +2,7 @@ import super::common::{FrameUniforms, DrawUniforms, VsIn, VsOut, frame, draw, te
import super::tev_helpers::{read_packed, extract_bits, select_texcoord, 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;
@vertex
fn vs_main(in: VsIn) -> VsOut {
@@ -9,7 +10,11 @@ fn vs_main(in: VsIn) -> 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.color = in.color;
+ out.color = compute_channel_lighting(
+ frame.color_ctrl, frame.alpha_ctrl,
+ frame.ambient_color, frame.material_color,
+ in.color, in.normal, in.pos_view,
+ );
out.uv0 = in.tex0;
out.uv1 = in.tex1;
out.uv2 = in.tex2;
diff --git a/crates/backend-wgpu/src/shaders/tev_combiners.wesl b/crates/backend-wgpu/src/shaders/tev_combiners.wesl
index 2d9caf9..1c205ee 100644
--- a/crates/backend-wgpu/src/shaders/tev_combiners.wesl
+++ b/crates/backend-wgpu/src/shaders/tev_combiners.wesl
@@ -1,4 +1,4 @@
-// TEV color combiner: result = (d (+/-) ((1-c)*a + c*b) + bias) * scale
+// TEV color combiner: result = (d (+/-) lerp(a, b, c) + bias) * scale
fn tev_combine_color(a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>,
bias: u32, sub: bool, scale: u32, do_clamp: bool) -> vec3<f32> {
let lerp = a * (vec3(1.0) - c) + b * c;
@@ -10,19 +10,17 @@ fn tev_combine_color(a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>,
result = d + lerp;
}
- // Bias
switch bias {
case 1u: { result += vec3(0.5); } // AddHalf
case 2u: { result -= vec3(0.5); } // SubHalf
- default: {} // Zero
+ default: {}
}
- // Scale
switch scale {
case 1u: { result *= 2.0; } // Scale2
case 2u: { result *= 4.0; } // Scale4
case 3u: { result *= 0.5; } // Divide2
- default: {} // Scale1
+ default: {}
}
if do_clamp {
@@ -32,7 +30,7 @@ fn tev_combine_color(a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>,
return result;
}
-// TEV alpha combiner: same formula as color but per-channel
+// Scalar version for the alpha channel
fn tev_combine_alpha(a: f32, b: f32, c: f32, d: f32, bias: u32, sub: bool, scale: u32, do_clamp: bool) -> f32 {
let lerp = a * (1.0 - c) + b * c;
diff --git a/crates/backend-wgpu/src/shaders/tev_helpers.wesl b/crates/backend-wgpu/src/shaders/tev_helpers.wesl
index f2ec84a..8d9a0b6 100644
--- a/crates/backend-wgpu/src/shaders/tev_helpers.wesl
+++ b/crates/backend-wgpu/src/shaders/tev_helpers.wesl
@@ -1,18 +1,16 @@
import super::common::{VsOut, tex0, tex1, tex2, tex3, tex4, tex5, tex6, tex7, samp0, samp1, samp2, samp3, samp4, samp5, samp6, samp7};
-// Read a single u32 from a packed array<vec4<u32>, 4> by flat index (0-15)
+// Index into a packed array<vec4<u32>, 4> by flat index
fn read_packed(arr: array<vec4<u32>, 4>, idx: u32) -> u32 {
let vi = idx / 4u;
let ci = idx % 4u;
return arr[vi][ci];
}
-// Extract `width` bits starting at bit `lo` from `val`
fn extract_bits(val: u32, lo: u32, width: u32) -> u32 {
return (val >> lo) & ((1u << width) - 1u);
}
-// Select texcoord by index (from TevOrder.texcoord)
fn select_texcoord(in: VsOut, idx: u32) -> vec2<f32> {
switch idx {
case 0u: { return in.uv0; }
@@ -27,7 +25,6 @@ fn select_texcoord(in: VsOut, idx: u32) -> vec2<f32> {
}
}
-// Sample from texmap by index (from TevOrder.texmap)
fn sample_texmap(texmap: u32, uv: vec2<f32>) -> vec4<f32> {
switch texmap {
case 0u: { return textureSample(tex0, samp0, uv); }
@@ -42,7 +39,7 @@ fn sample_texmap(texmap: u32, uv: vec2<f32>) -> vec4<f32> {
}
}
-// TEV color input selector (TevColorIn, 4-bit, 16 variants)
+// TevColorIn: 4-bit selector, 16 variants
fn tev_color_in(sel: u32, tex_color: vec4<f32>, ras_color: vec4<f32>, regs: array<vec4<f32>, 4>, konst_color: vec4<f32>) -> vec3<f32> {
switch sel {
case 0u: { return regs[0].rgb; } // PrevColor
@@ -65,7 +62,7 @@ fn tev_color_in(sel: u32, tex_color: vec4<f32>, ras_color: vec4<f32>, regs: arra
}
}
-// TEV alpha input selector (TevAlphaIn, 3-bit, 8 variants)
+// TevAlphaIn: 3-bit selector, 8 variants
fn tev_alpha_in(sel: u32, tex_color: vec4<f32>, ras_color: vec4<f32>, regs: array<vec4<f32>, 4>, konst_color: vec4<f32>) -> f32 {
switch sel {
case 0u: { return regs[0].a; } // PrevAlpha
diff --git a/crates/backend-wgpu/src/triangulate.rs b/crates/backend-wgpu/src/triangulate.rs
index f30bbf5..2b6138b 100644
--- a/crates/backend-wgpu/src/triangulate.rs
+++ b/crates/backend-wgpu/src/triangulate.rs
@@ -5,6 +5,8 @@ use gecko::flipper::gx::draw::{DrawCall, Primitive};
pub(crate) struct GpuVertex {
pub position: [f32; 3],
pub color: [f32; 4],
+ pub normal: [f32; 3],
+ pub pos_view: [f32; 3],
pub tex0: [f32; 2],
pub tex1: [f32; 2],
pub tex2: [f32; 2],
@@ -21,6 +23,8 @@ impl From<&gecko::flipper::gx::draw::Vertex> for GpuVertex {
Self {
position: v.position,
color: v.color0,
+ normal: v.normal,
+ pos_view: v.pos_view,
tex0: tc(0),
tex1: tc(1),
tex2: tc(2),
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index a651b11..3c5d891 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -1,16 +1,15 @@
use clap::Parser;
use std::sync::Arc;
-use winit::{
- application::ApplicationHandler,
- event::WindowEvent,
- event_loop::{ActiveEventLoop, EventLoop},
- keyboard::{KeyCode, PhysicalKey},
- window::{Window, WindowId},
-};
+use winit::application::ApplicationHandler;
+use winit::event::WindowEvent;
+use winit::event_loop::{ActiveEventLoop, EventLoop};
+use winit::keyboard::{KeyCode, PhysicalKey};
+use winit::window::{Window, WindowId};
use gecko::flipper::si::pad::{self, PadStatus, STICK_CENTER};
use gecko::gamecube::GameCube;
-use image::{Dol, dvd::Dvd};
+use image::Dol;
+use image::dvd::Dvd;
use crate::debugger::DebuggerUi;
use crate::render::RenderState;
diff --git a/crates/gctool/src/dol.rs b/crates/gctool/src/dol.rs
index 66dab12..f89ef72 100644
--- a/crates/gctool/src/dol.rs
+++ b/crates/gctool/src/dol.rs
@@ -1,4 +1,5 @@
-use comfy_table::{Table, presets::ASCII_MARKDOWN};
+use comfy_table::Table;
+use comfy_table::presets::ASCII_MARKDOWN;
use image::{Dol, Executable};
fn section_table(sections: &[image::Section]) -> Table {
diff --git a/crates/gctool/src/ipl.rs b/crates/gctool/src/ipl.rs
index bbc5db2..c4bd613 100644
--- a/crates/gctool/src/ipl.rs
+++ b/crates/gctool/src/ipl.rs
@@ -1,8 +1,7 @@
use crate::cli::IplAction;
-use std::fs;
use std::path::Path;
-use std::process;
+use std::{fs, process};
const IPL_ROM_SIZE: usize = 0x200000;
const SCRAMBLE_START: usize = 0x100;
diff --git a/crates/gctool/src/iso.rs b/crates/gctool/src/iso.rs
index 51eea0d..162fe06 100644
--- a/crates/gctool/src/iso.rs
+++ b/crates/gctool/src/iso.rs
@@ -1,7 +1,5 @@
-use image::{
- Executable,
- dvd::{DVD_APPLOADER_OFFSET, FstNode},
-};
+use image::Executable;
+use image::dvd::{DVD_APPLOADER_OFFSET, FstNode};
use owo_colors::OwoColorize;
use termtree::Tree;
diff --git a/crates/gctool/src/main.rs b/crates/gctool/src/main.rs
index 66bc9e8..ed2af9d 100644
--- a/crates/gctool/src/main.rs
+++ b/crates/gctool/src/main.rs
@@ -8,8 +8,7 @@ use crate::cli::{Args, Command, DisasmArch};
use crate::disassembly::{disassemble_dsp, disassemble_ppc};
use clap::Parser;
-use std::fs;
-use std::process;
+use std::{fs, process};
fn read_file_or_exit(file: &str) -> Vec<u8> {
fs::read(file).unwrap_or_else(|e| {
diff --git a/crates/gecko/src/cpu/irq.rs b/crates/gecko/src/cpu/irq.rs
index 8268368..687ff68 100644
--- a/crates/gecko/src/cpu/irq.rs
+++ b/crates/gecko/src/cpu/irq.rs
@@ -1,4 +1,6 @@
-use crate::{cpu::spr::Srr0, gamecube::GameCube, mmio::Mmio};
+use crate::cpu::spr::Srr0;
+use crate::gamecube::GameCube;
+use crate::mmio::Mmio;
// Exception vectors
#[rustfmt::skip] pub const IRQ_SYSTEM_RESET: u32 = Mmio::virt_to_phys(0x8000_0100);
diff --git a/crates/gecko/src/flipper/dsp/interpreter.rs b/crates/gecko/src/flipper/dsp/interpreter.rs
index f03b619..313f43e 100644
--- a/crates/gecko/src/flipper/dsp/interpreter.rs
+++ b/crates/gecko/src/flipper/dsp/interpreter.rs
@@ -1,8 +1,6 @@
-use crate::flipper::dsp::{
- condition::BranchControl,
- core::{SignExtensionMode, StatusRegister, reg},
- lut::*,
-};
+use crate::flipper::dsp::condition::BranchControl;
+use crate::flipper::dsp::core::{SignExtensionMode, StatusRegister, reg};
+use crate::flipper::dsp::lut::*;
#[inline(always)]
pub fn add_sub<const OP: u32>(
diff --git a/crates/gecko/src/flipper/dsp/regs.rs b/crates/gecko/src/flipper/dsp/regs.rs
index 0f60709..7f83cb2 100644
--- a/crates/gecko/src/flipper/dsp/regs.rs
+++ b/crates/gecko/src/flipper/dsp/regs.rs
@@ -1,6 +1,7 @@
use chapa::BitEnum;
-use crate::{flipper::dsp::Dsp, mmio::traits::MmioAccess};
+use crate::flipper::dsp::Dsp;
+use crate::mmio::traits::MmioAccess;
// 0xCC00500A 2 [R/W] CSR - DSP Control/Status Register
diff --git a/crates/gecko/src/flipper/gx.rs b/crates/gecko/src/flipper/gx.rs
index 6444798..fd61b0c 100644
--- a/crates/gecko/src/flipper/gx.rs
+++ b/crates/gecko/src/flipper/gx.rs
@@ -2,47 +2,42 @@ mod bp;
pub mod constants;
pub mod draw;
pub mod fifo;
-mod lighting;
pub mod math;
pub mod regs;
-mod tev;
+pub mod tev;
mod texgen;
mod vertex;
mod xf;
-use crate::{
- flipper::gx::{
- constants::{BP_REG_SIZE, CP_REG_SIZE, XF_MEM_SIZE},
- draw::DrawCommands,
- regs::{AlphaCompare, BlendMode, TevAlphaEnv, TevColorEnv, TevRegisterH, TevRegisterL, ZMode},
- },
- gamecube::GameCube,
- mmio::Mmio,
-};
+use crate::flipper::gx::constants::{BP_REG_SIZE, CP_REG_SIZE, XF_MEM_SIZE};
+use crate::flipper::gx::draw::DrawCommands;
+use crate::flipper::gx::regs::{AlphaCompare, BlendMode, TevAlphaEnv, TevColorEnv, TevRegisterH, TevRegisterL, ZMode};
+use crate::gamecube::GameCube;
+use crate::mmio::Mmio;
use fifo::FifoCmd;
pub struct GraphicsProcessor {
pub raise_interrupt: bool,
pub draw_commands: DrawCommands,
- pub(crate) bp_regs: Vec<u32>,
- pub(crate) cp_regs: Vec<u32>,
- pub(crate) xf_mem: Vec<u32>,
- pub(crate) fifo: Vec<u8>,
+ pub bp_regs: Vec<u32>,
+ pub cp_regs: Vec<u32>,
+ pub xf_mem: Vec<u32>,
+ pub fifo: Vec<u8>,
// Current GX state to snapshot into a DrawCall later
- pub(crate) cur_textures: [Option<draw::TextureDescriptor>; 8],
- pub(crate) cur_tev_color_env: [TevColorEnv; 16],
- pub(crate) cur_tev_alpha_env: [TevAlphaEnv; 16],
- pub(crate) cur_tev_color_regs_lo: [TevRegisterL; 4],
- pub(crate) cur_tev_color_regs_hi: [TevRegisterH; 4],
- pub(crate) cur_tev_const_regs_lo: [TevRegisterL; 4],
- pub(crate) cur_tev_const_regs_hi: [TevRegisterH; 4],
- pub(crate) cur_tev_orders: [regs::TevOrder; 8],
- pub(crate) cur_num_tev_stages: u8,
- pub(crate) cur_tev_konst_colors: [[f32; 4]; 16],
- pub(crate) cur_zmode: ZMode,
- pub(crate) cur_blend_mode: BlendMode,
- pub(crate) cur_alpha_compare: AlphaCompare,
+ pub cur_textures: [Option<draw::TextureDescriptor>; 8],
+ pub cur_tev_color_env: [TevColorEnv; 16],
+ pub cur_tev_alpha_env: [TevAlphaEnv; 16],
+ pub cur_tev_color_regs_lo: [TevRegisterL; 4],
+ pub cur_tev_color_regs_hi: [TevRegisterH; 4],
+ pub cur_tev_const_regs_lo: [TevRegisterL; 4],
+ pub cur_tev_const_regs_hi: [TevRegisterH; 4],
+ pub cur_tev_orders: [regs::TevOrder; 8],
+ pub cur_num_tev_stages: u8,
+ pub cur_tev_konst_colors: [[f32; 4]; 16],
+ pub cur_zmode: ZMode,
+ pub cur_blend_mode: BlendMode,
+ pub cur_alpha_compare: AlphaCompare,
}
impl GraphicsProcessor {
diff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rs
index 9f2c20a..b675bbd 100644
--- a/crates/gecko/src/flipper/gx/bp.rs
+++ b/crates/gecko/src/flipper/gx/bp.rs
@@ -1,19 +1,16 @@
-use super::{
- GraphicsProcessor,
- constants::{
- BP_GEN_MODE, BP_PE_ALPHA_COMPARE, BP_PE_CMODE0, BP_PE_DONE, BP_PE_DONE_FINISH_BIT, BP_PE_ZMODE,
- BP_RAS1_TREF_COUNT, BP_RAS1_TREF0, BP_TEV_COLOR_ENV_0, BP_TEV_REGISTERL_0, BP_TX_SETIMAGE0_I0,
- BP_TX_SETIMAGE0_I4, BP_TX_SETIMAGE3_I0, BP_TX_SETIMAGE3_I4, BP_TX_SETMODE0_I0, BP_TX_SETMODE0_I4,
- },
- draw,
- regs::{
- AlphaCompare, BlendMode, GenMode, TevAlphaEnv, TevColorEnv, TevOrder, TevRegType, TevRegisterH, TevRegisterL,
- TxSetImage0, TxSetImage3, TxSetMode0, ZMode,
- },
+use super::constants::{
+ BP_GEN_MODE, BP_PE_ALPHA_COMPARE, BP_PE_CMODE0, BP_PE_DONE, BP_PE_DONE_FINISH_BIT, BP_PE_ZMODE, BP_RAS1_TREF_COUNT,
+ BP_RAS1_TREF0, BP_TEV_COLOR_ENV_0, BP_TEV_REGISTERL_0, BP_TX_SETIMAGE0_I0, BP_TX_SETIMAGE0_I4, BP_TX_SETIMAGE3_I0,
+ BP_TX_SETIMAGE3_I4, BP_TX_SETMODE0_I0, BP_TX_SETMODE0_I4,
};
+use super::regs::{
+ AlphaCompare, BlendMode, GenMode, TevAlphaEnv, TevColorEnv, TevOrder, TevRegType, TevRegisterH, TevRegisterL,
+ TxSetImage0, TxSetImage3, TxSetMode0, ZMode,
+};
+use super::{GraphicsProcessor, draw};
impl GraphicsProcessor {
- pub(crate) fn load_bp(&mut self, data: &[u8]) {
+ pub fn load_bp(&mut self, data: &[u8]) {
let idx = data[0] as usize;
let val = u32::from_be_bytes([0, data[1], data[2], data[3]]);
self.bp_regs[idx] = val;
diff --git a/crates/gecko/src/flipper/gx/draw.rs b/crates/gecko/src/flipper/gx/draw.rs
index d66d896..b2f3f4b 100644
--- a/crates/gecko/src/flipper/gx/draw.rs
+++ b/crates/gecko/src/flipper/gx/draw.rs
@@ -1,5 +1,5 @@
use super::regs::{
- AlphaCompare, BlendMode, MagFilter, MinFilter, TevAlphaEnv, TevColorEnv, TevStageOrder, WrapMode, ZMode,
+ AlphaCompare, BlendMode, ChanCtrl, MagFilter, MinFilter, TevAlphaEnv, TevColorEnv, TevStageOrder, WrapMode, ZMode,
};
use chapa::BitEnum;
@@ -38,6 +38,8 @@ impl Primitive {
pub struct Vertex {
pub position: [f32; 3],
pub color0: [f32; 4],
+ pub normal: [f32; 3],
+ pub pos_view: [f32; 3],
pub texcoords: [Option<[f32; 2]>; 8],
}
@@ -90,6 +92,17 @@ pub struct DrawCall {
pub bp_zmode: ZMode,
pub bp_blend_mode: BlendMode,
pub bp_alpha_compare: AlphaCompare,
+
+ // Lighting state snapshot (XF)
+ pub light_colors: [[f32; 4]; 8],
+ pub light_cosatt: [[f32; 4]; 8],
+ pub light_distatt: [[f32; 4]; 8],
+ pub light_pos: [[f32; 4]; 8],
+ pub light_dir: [[f32; 4]; 8],
+ pub color_ctrl: ChanCtrl,
+ pub alpha_ctrl: ChanCtrl,
+ pub ambient_color: [f32; 4],
+ pub material_color: [f32; 4],
}
#[derive(Debug, Clone, Copy)]
diff --git a/crates/gecko/src/flipper/gx/fifo.rs b/crates/gecko/src/flipper/gx/fifo.rs
index dbee1df..5480d03 100644
--- a/crates/gecko/src/flipper/gx/fifo.rs
+++ b/crates/gecko/src/flipper/gx/fifo.rs
@@ -1,9 +1,9 @@
use super::constants::{BP_CMD, CALL_DL_CMD, CP_CMD, INV_VTX_CACHE_CMD, NOP_CMD, XF_CMD};
-use crate::flipper::gx::{
- GraphicsProcessor,
- constants::{DRAW_COMMANDS_END, DRAW_COMMANDS_START, VATA_REG, VATB_REG, VATC_REG, VCD_HI_REG, VCD_LO_REG},
- regs::{AttributeType, VatA, VatB, VatC, VcdHi, VcdLo},
+use crate::flipper::gx::GraphicsProcessor;
+use crate::flipper::gx::constants::{
+ DRAW_COMMANDS_END, DRAW_COMMANDS_START, VATA_REG, VATB_REG, VATC_REG, VCD_HI_REG, VCD_LO_REG,
};
+use crate::flipper::gx::regs::{AttributeType, VatA, VatB, VatC, VcdHi, VcdLo};
use std::io::{Cursor, Read};
impl GraphicsProcessor {
diff --git a/crates/gecko/src/flipper/gx/lighting.rs b/crates/gecko/src/flipper/gx/lighting.rs
deleted file mode 100644
index 3d50daa..0000000
--- a/crates/gecko/src/flipper/gx/lighting.rs
+++ /dev/null
@@ -1,137 +0,0 @@
-use super::{
- GraphicsProcessor,
- constants::{XF_LIGHT_A0, XF_LIGHT_BASE, XF_LIGHT_COLOR, XF_LIGHT_K0, XF_LIGHT_NX, XF_LIGHT_PX, XF_LIGHT_STRIDE},
- math::{Vec3, saturating_div, unpack_rgba},
- regs::{AttnFn, ChanCtrl},
-};
-
-impl GraphicsProcessor {
- pub(crate) fn compute_channel_lighting(
- &self,
- color_ctrl: &ChanCtrl,
- alpha_ctrl: &ChanCtrl,
- ambient_reg: [f32; 4],
- material_reg: [f32; 4],
- vertex_color: [f32; 4],
- normal: Vec3,
- pos: Vec3,
- ) -> [f32; 4] {
- let mat_rgb: [f32; 3] = if color_ctrl.mat_src() {
- [vertex_color[0], vertex_color[1], vertex_color[2]]
- } else {
- [material_reg[0], material_reg[1], material_reg[2]]
- };
- let amb_rgb: [f32; 3] = if color_ctrl.amb_src() {
- [vertex_color[0], vertex_color[1], vertex_color[2]]
- } else {
- [ambient_reg[0], ambient_reg[1], ambient_reg[2]]
- };
-
- let mat_a = if alpha_ctrl.mat_src() {
- vertex_color[3]
- } else {
- material_reg[3]
- };
- let amb_a = if alpha_ctrl.amb_src() {
- vertex_color[3]
- } else {
- ambient_reg[3]
- };
-
- // When lighting is disabled, return material color directly
- let color_lit_off = !color_ctrl.enable();
- let alpha_lit_off = !alpha_ctrl.enable();
- if color_lit_off && alpha_lit_off {
- return [mat_rgb[0], mat_rgb[1], mat_rgb[2], mat_a];
- }
-
- // Compute RGB lighting accumulator
- let mut rgb_out = mat_rgb;
- if !color_lit_off {
- let light_mask = color_ctrl.light_mask();
- let mut acc = amb_rgb;
- for light_id in 0..8u32 {
- if (light_mask >> light_id) & 1 == 0 {
- continue;
- }
- let base = XF_LIGHT_BASE + (light_id as usize) * XF_LIGHT_STRIDE;
- let light_color = unpack_rgba(self.xf_mem[base + XF_LIGHT_COLOR]);
- let factor = self.compute_light_factor(color_ctrl, base, normal, pos);
- acc[0] += light_color[0] * factor;
- acc[1] += light_color[1] * factor;
- acc[2] += light_color[2] * factor;
- }
- rgb_out = std::array::from_fn(|i| mat_rgb[i] * acc[i].clamp(0.0, 1.0));
- }
-
- // Compute Alpha lighting accumulator
- let mut a_out = mat_a;
- if !alpha_lit_off {
- let light_mask = alpha_ctrl.light_mask();
- let mut acc_a = amb_a;
- for light_id in 0..8u32 {
- if (light_mask >> light_id) & 1 == 0 {
- continue;
- }
- let base = XF_LIGHT_BASE + (light_id as usize) * XF_LIGHT_STRIDE;
- let light_color = unpack_rgba(self.xf_mem[base + XF_LIGHT_COLOR]);
- let factor = self.compute_light_factor(alpha_ctrl, base, normal, pos);
- acc_a += light_color[3] * factor;
- }
- a_out = mat_a * acc_a.clamp(0.0, 1.0);
- }
-
- [rgb_out[0], rgb_out[1], rgb_out[2], a_out]
- }
-
- fn compute_light_factor(&self, ctrl: &ChanCtrl, base: usize, normal: Vec3, pos: Vec3) -> f32 {
- let cosatt = self.xf_vec3(base + XF_LIGHT_A0);
- let distatt = self.xf_vec3(base + XF_LIGHT_K0);
- let light_pos = self.xf_vec3(base + XF_LIGHT_PX);
- let light_dir = self.xf_vec3(base + XF_LIGHT_NX);
-
- // ldir starts as light_pos - vertex_pos for all modes
- // In Spot mode, light_pos is a world position
- // In Spec mode, light_pos is repurposed as the half-angle direction vector
- let mut ldir = light_pos - pos;
-
- let attn = match ctrl.attn_fn() {
- AttnFn::None => {
- ldir = ldir.normalize();
- 1.0
- }
- AttnFn::Spot => {
- let dist = ldir.length();
- ldir = ldir.normalize();
- let cos_angle = light_dir.dot(ldir);
- let angle_attn = (cosatt.0 + cosatt.1 * cos_angle + cosatt.2 * cos_angle * cos_angle).max(0.0);
- saturating_div(angle_attn, distatt.0 + distatt.1 * dist + distatt.2 * dist * dist)
- }
- AttnFn::Spec => {
- ldir = ldir.normalize();
- let half_dot = ldir.dot(normal);
- if half_dot < 0.0 {
- return 0.0;
- }
- let s = normal.dot(light_dir).max(0.0);
- let att_len = Vec3(1.0, s, s * s);
- let da = if ctrl.diff_fn() != super::regs::DiffuseFn::None {
- distatt.normalize()
- } else {
- distatt
- };
- saturating_div(att_len.dot(cosatt).max(0.0), att_len.dot(da))
- }
- };
-
- // Diffuse is computed from ldir dotted with normal
- let dif_attn = ldir.dot(normal);
- let diffuse = match ctrl.diff_fn() {
- super::regs::DiffuseFn::None => 1.0,
- super::regs::DiffuseFn::Signed => dif_attn,
- super::regs::DiffuseFn::Clamp => dif_attn.max(0.0),
- };
-
- attn * diffuse
- }
-}
diff --git a/crates/gecko/src/flipper/gx/tev.rs b/crates/gecko/src/flipper/gx/tev.rs
index ab5e5a9..0fe637a 100644
--- a/crates/gecko/src/flipper/gx/tev.rs
+++ b/crates/gecko/src/flipper/gx/tev.rs
@@ -1,7 +1,9 @@
-use super::{GraphicsProcessor, constants::BP_TEV_KSEL_0, regs::TevStageOrder};
+use super::GraphicsProcessor;
+use super::constants::BP_TEV_KSEL_0;
+use super::regs::TevStageOrder;
impl GraphicsProcessor {
- pub(crate) fn resolve_tev_orders(&self) -> [TevStageOrder; 16] {
+ pub fn resolve_tev_orders(&self) -> [TevStageOrder; 16] {
let mut orders = [TevStageOrder::default(); 16];
for i in 0..8 {
let reg = self.cur_tev_orders[i];
@@ -17,7 +19,7 @@ impl GraphicsProcessor {
orders
}
- pub(crate) fn resolve_tev_color_regs(&self) -> [[f32; 4]; 4] {
+ pub fn resolve_tev_color_regs(&self) -> [[f32; 4]; 4] {
std::array::from_fn(|i| {
let lo = self.cur_tev_color_regs_lo[i];
let hi = self.cur_tev_color_regs_hi[i];
@@ -30,7 +32,7 @@ impl GraphicsProcessor {
})
}
- pub(crate) fn resolve_konst_colors(&mut self) {
+ pub fn resolve_konst_colors(&mut self) {
let kregs: [[f32; 4]; 4] = std::array::from_fn(|i| {
let lo = self.cur_tev_const_regs_lo[i];
let hi = self.cur_tev_const_regs_hi[i];
@@ -57,7 +59,7 @@ impl GraphicsProcessor {
}
}
-pub(crate) fn s11_to_f32(val: u16) -> f32 {
+pub fn s11_to_f32(val: u16) -> f32 {
let signed = if val & 0x400 != 0 {
val as i32 - 0x800
} else {
diff --git a/crates/gecko/src/flipper/gx/texgen.rs b/crates/gecko/src/flipper/gx/texgen.rs
index e07903b..8d8b2bf 100644
--- a/crates/gecko/src/flipper/gx/texgen.rs
+++ b/crates/gecko/src/flipper/gx/texgen.rs
@@ -1,14 +1,12 @@
-use super::{
- GraphicsProcessor,
- constants::{
- XF_DUAL_TEX_ENABLE, XF_DUALTEX_BASE, XF_MATRIX_INDEX_A, XF_MATRIX_INDEX_B, XF_POS_MTX_STRIDE, XF_POST_MTX_BASE,
- XF_TEXGEN_BASE,
- },
- regs::{DualTexGenReg, MatrixIndex0, MatrixIndex1, TexGenReg},
+use super::GraphicsProcessor;
+use super::constants::{
+ XF_DUAL_TEX_ENABLE, XF_DUALTEX_BASE, XF_MATRIX_INDEX_A, XF_MATRIX_INDEX_B, XF_POS_MTX_STRIDE, XF_POST_MTX_BASE,
+ XF_TEXGEN_BASE,
};
+use super::regs::{DualTexGenReg, MatrixIndex0, MatrixIndex1, TexGenReg};
impl GraphicsProcessor {
- pub(crate) fn compute_texgen(
+ pub fn compute_texgen(
&self,
texgen_idx: usize,
position: [f32; 3],
diff --git a/crates/gecko/src/flipper/gx/vertex.rs b/crates/gecko/src/flipper/gx/vertex.rs
index 6bc2e93..6ed822b 100644
--- a/crates/gecko/src/flipper/gx/vertex.rs
+++ b/crates/gecko/src/flipper/gx/vertex.rs
@@ -1,14 +1,12 @@
-use super::{
- GraphicsProcessor,
- 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_MATERIAL_COLOR0,
- XF_MATRIX_INDEX_A, XF_NRM_MTX_BASE, XF_NUM_TEXGENS, XF_POS_MTX_STRIDE,
- },
- draw,
- math::{Vec3, unpack_rgba},
- regs::{self, AttributeType, ChanCtrl, ComponentFormat, MatrixIndex0, TexCount, VatA, VatB, VatC, VcdHi, VcdLo},
+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,
+ 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,
};
+use super::math::{Vec3, unpack_rgba};
+use super::regs::{self, AttributeType, ComponentFormat, MatrixIndex0, TexCount, VatA, VatB, VatC, VcdHi, VcdLo};
+use super::{GraphicsProcessor, draw};
use crate::mmio::Mmio;
use std::io::{Cursor, Read};
@@ -42,7 +40,7 @@ struct VertexFormat {
}
impl GraphicsProcessor {
- pub(crate) fn create_draw_call(&mut self, mmio: &mut Mmio, cmd: u8, data: Vec<u8>) {
+ pub fn create_draw_call(&mut self, mmio: &mut Mmio, cmd: u8, data: Vec<u8>) {
let Some(primitive) = draw::Primitive::from_cmd(cmd) else {
tracing::error!(cmd, "goofy draw command");
return;
@@ -57,27 +55,11 @@ impl GraphicsProcessor {
let vertex_count = data.len() / vf.vertex_stride;
- // Channel 0 lighting state (COLOR0 and ALPHA0 have separate controls)
- let color_ctrl = ChanCtrl::from_raw(self.xf_mem[XF_COLOR_CTRL0]);
- let alpha_ctrl = ChanCtrl::from_raw(self.xf_mem[XF_ALPHA_CTRL0]);
- let ambient_reg = unpack_rgba(self.xf_mem[XF_AMBIENT_COLOR0]);
- let material_reg = unpack_rgba(self.xf_mem[XF_MATERIAL_COLOR0]);
-
let mut vertices: Vec<draw::Vertex> = Vec::with_capacity(vertex_count);
let mut cur = Cursor::new(&data);
for i in 0..vertex_count {
- let vertex = self.decode_vertex(
- &mut cur,
- &data,
- &mmio.ram,
- &vf,
- &color_ctrl,
- &alpha_ctrl,
- ambient_reg,
- material_reg,
- i,
- );
+ let vertex = self.decode_vertex(&mut cur, &data, &mmio.ram, &vf, i);
vertices.push(vertex);
}
@@ -109,6 +91,15 @@ impl GraphicsProcessor {
bp_zmode: self.cur_zmode,
bp_blend_mode: self.cur_blend_mode,
bp_alpha_compare: self.cur_alpha_compare,
+ light_colors: self.snapshot_light_field(XF_LIGHT_COLOR),
+ light_cosatt: self.snapshot_light_field(XF_LIGHT_A0),
+ light_distatt: self.snapshot_light_field(XF_LIGHT_K0),
+ light_pos: self.snapshot_light_field(XF_LIGHT_PX),
+ light_dir: self.snapshot_light_field(XF_LIGHT_NX),
+ color_ctrl: regs::ChanCtrl::from_raw(self.xf_mem[XF_COLOR_CTRL0]),
+ alpha_ctrl: regs::ChanCtrl::from_raw(self.xf_mem[XF_ALPHA_CTRL0]),
+ ambient_color: unpack_rgba(self.xf_mem[XF_AMBIENT_COLOR0]),
+ material_color: unpack_rgba(self.xf_mem[XF_MATERIAL_COLOR0]),
});
}
@@ -238,17 +229,12 @@ impl GraphicsProcessor {
}
}
- #[allow(clippy::too_many_arguments)]
fn decode_vertex(
&self,
cur: &mut Cursor<&Vec<u8>>,
data: &[u8],
ram: &[u8],
vf: &VertexFormat,
- color_ctrl: &ChanCtrl,
- alpha_ctrl: &ChanCtrl,
- ambient_reg: [f32; 4],
- material_reg: [f32; 4],
vertex_idx: usize,
) -> draw::Vertex {
// Read per-vertex position/normal matrix index, or use register default
@@ -345,18 +331,9 @@ impl GraphicsProcessor {
});
}
- // Compute per-vertex lighting
+ // Transform position and normal to view space (per-vertex matrix dependent)
let normal_view = Vec3::from(normal).transform(&nrm_mtx).normalize();
let pos_view = self.xf_transform_3x4(pos_mtx_base, position);
- let final_color = self.compute_channel_lighting(
- color_ctrl,
- alpha_ctrl,
- ambient_reg,
- material_reg,
- color0,
- normal_view,
- pos_view,
- );
// Texture coordinate generation (XF texgen)
let num_texgens = (self.xf_mem[XF_NUM_TEXGENS] as usize).min(8);
@@ -372,13 +349,15 @@ impl GraphicsProcessor {
tracing::debug!(
vertex = vertex_idx,
position = format!("{:02X?}", position),
- color0 = format!("{:?}", final_color),
+ color0 = format!("{:?}", color0),
"Vertex"
);
draw::Vertex {
position,
- color0: final_color,
+ color0,
+ normal: [normal_view.0, normal_view.1, normal_view.2],
+ pos_view: [pos_view.0, pos_view.1, pos_view.2],
texcoords,
}
}
@@ -412,6 +391,19 @@ impl GraphicsProcessor {
],
])
}
+
+ fn snapshot_light_field(&self, field_offset: usize) -> [[f32; 4]; 8] {
+ std::array::from_fn(|i| {
+ let base = XF_LIGHT_BASE + i * XF_LIGHT_STRIDE + field_offset;
+ if field_offset == XF_LIGHT_COLOR {
+ // Color is stored as packed RGBA, not float
+ unpack_rgba(self.xf_mem[base])
+ } else {
+ // Float vec3, w = 0
+ [self.xf_f32(base), self.xf_f32(base + 1), self.xf_f32(base + 2), 0.0]
+ }
+ })
+ }
}
fn read_index(cur: &mut Cursor<&Vec<u8>>, attr: AttributeType) -> usize {
diff --git a/crates/gecko/src/flipper/gx/xf.rs b/crates/gecko/src/flipper/gx/xf.rs
index a4bc14d..2c77d98 100644
--- a/crates/gecko/src/flipper/gx/xf.rs
+++ b/crates/gecko/src/flipper/gx/xf.rs
@@ -1,20 +1,13 @@
-use super::{
- GraphicsProcessor,
- constants::{XF_PROJECTION_BASE, XF_PROJECTION_END},
- draw,
- math::Vec3,
-};
+use super::constants::{XF_PROJECTION_BASE, XF_PROJECTION_END};
+use super::math::Vec3;
+use super::{GraphicsProcessor, draw};
impl GraphicsProcessor {
- pub(crate) fn xf_f32(&self, reg: usize) -> f32 {
+ pub fn xf_f32(&self, reg: usize) -> f32 {
f32::from_bits(self.xf_mem[reg])
}
- pub(crate) fn xf_vec3(&self, reg: usize) -> Vec3 {
- Vec3(self.xf_f32(reg), self.xf_f32(reg + 1), self.xf_f32(reg + 2))
- }
-
- pub(crate) fn xf_transform_3x4(&self, base: usize, v: [f32; 3]) -> Vec3 {
+ pub fn xf_transform_3x4(&self, base: usize, v: [f32; 3]) -> Vec3 {
Vec3(
self.xf_f32(base) * v[0]
+ self.xf_f32(base + 1) * v[1]
@@ -31,7 +24,7 @@ impl GraphicsProcessor {
)
}
- pub(crate) fn rebuild_projection(&mut self) {
+ pub fn rebuild_projection(&mut self) {
let pm1 = self.xf_f32(XF_PROJECTION_BASE);
let pm2 = self.xf_f32(XF_PROJECTION_BASE + 1);
let pm3 = self.xf_f32(XF_PROJECTION_BASE + 2);
@@ -59,7 +52,7 @@ impl GraphicsProcessor {
};
}
- pub(crate) fn load_cp(&mut self, data: &[u8]) {
+ pub fn load_cp(&mut self, data: &[u8]) {
let idx = data[0] as usize;
let val = u32::from_be_bytes([data[1], data[2], data[3], data[4]]);
self.cp_regs[idx] = val;
@@ -71,7 +64,7 @@ impl GraphicsProcessor {
);
}
- pub(crate) fn load_xf(&mut self, data: &[u8]) {
+ pub fn load_xf(&mut self, data: &[u8]) {
let length = u16::from_be_bytes([data[0], data[1]]) as usize;
let addr = u16::from_be_bytes([data[2], data[3]]) as usize;
let n = length + 1;
diff --git a/crates/gecko/src/flipper/pe.rs b/crates/gecko/src/flipper/pe.rs
index f930ab4..42f55c7 100644
--- a/crates/gecko/src/flipper/pe.rs
+++ b/crates/gecko/src/flipper/pe.rs
@@ -1,12 +1,8 @@
pub mod regs;
-use crate::{
- flipper::pi::InterruptFlag,
- gamecube::GameCube,
- mmio::{
- constants::PE_BASE,
- traits::{MmioAccess, MmioRegister, MmioRw},
- },
-};
+use crate::flipper::pi::InterruptFlag;
+use crate::gamecube::GameCube;
+use crate::mmio::constants::PE_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};
pub struct PixelEngine {
pub zconf: regs::ZConfig,
diff --git a/crates/gecko/src/flipper/pe/regs.rs b/crates/gecko/src/flipper/pe/regs.rs
index 805dc6f..5af78a5 100644
--- a/crates/gecko/src/flipper/pe/regs.rs
+++ b/crates/gecko/src/flipper/pe/regs.rs
@@ -1,7 +1,5 @@
-use crate::{
- flipper::pe::PixelEngine,
- mmio::traits::{MmioAccess, MmioRegister},
-};
+use crate::flipper::pe::PixelEngine;
+use crate::mmio::traits::{MmioAccess, MmioRegister};
// 0xCC001000 2 R/W Z Configuration
diff --git a/crates/gecko/src/flipper/vi.rs b/crates/gecko/src/flipper/vi.rs
index b763808..dfcd5c8 100644
--- a/crates/gecko/src/flipper/vi.rs
+++ b/crates/gecko/src/flipper/vi.rs
@@ -1,14 +1,10 @@
pub mod regs;
-use crate::{
- flipper::pi::InterruptFlag,
- gamecube::GameCube,
- mmio::{
- constants::VI_BASE,
- traits::{MmioAccess, MmioRegister, MmioRw},
- },
- scheduler::EventKind,
-};
+use crate::flipper::pi::InterruptFlag;
+use crate::gamecube::GameCube;
+use crate::mmio::constants::VI_BASE;
+use crate::mmio::traits::{MmioAccess, MmioRegister, MmioRw};
+use crate::scheduler::EventKind;
const CPU_CORE_CLOCK: u64 = 486_000_000;
const CLOCK_FREQUENCIES: [u64; 2] = [27_000_000, 54_000_000];
diff --git a/crates/gecko/src/gamecube.rs b/crates/gecko/src/gamecube.rs
index 7ea91c5..201d386 100644
--- a/crates/gecko/src/gamecube.rs
+++ b/crates/gecko/src/gamecube.rs
@@ -1,25 +1,23 @@
+use crate::cpu::instruction::Instruction;
+use crate::cpu::{self, Cpu, IPL_RESET_VECTOR};
+use crate::dvd::DvdInterface;
+use crate::flipper::ai::AudioInterface;
+use crate::flipper::cp::CommandProcessor;
+use crate::flipper::dsp::Dsp;
+use crate::flipper::exi::ExternalInterface;
+use crate::flipper::exi::macronix::ExiMacronix;
+use crate::flipper::gx::GraphicsProcessor;
+use crate::flipper::mi::MemoryInterface;
+use crate::flipper::pe::PixelEngine;
+use crate::flipper::pi::ProcessorInterface;
+use crate::flipper::si::{SerialInterface, pad};
+use crate::flipper::vi::VideoInterface;
#[cfg(feature = "idle-skip")]
use crate::idle::{IDLE_LOOP_MAX_INSTRS, IdleCheck, IdleDetector};
+use crate::mmio::Mmio;
+use crate::scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler};
#[cfg(feature = "scripting")]
use crate::scripting::{HookFlags, ScriptHookFilters, ScriptHookState, ScriptHost};
-use crate::{
- cpu::{self, Cpu, IPL_RESET_VECTOR, instruction::Instruction},
- dvd::DvdInterface,
- flipper::{
- ai::AudioInterface,
- cp::CommandProcessor,
- dsp::Dsp,
- exi::{ExternalInterface, macronix::ExiMacronix},
- gx::GraphicsProcessor,
- mi::MemoryInterface,
- pe::PixelEngine,
- pi::ProcessorInterface,
- si::{SerialInterface, pad},
- vi::VideoInterface,
- },
- mmio::Mmio,
- scheduler::{CYCLES_PER_VSYNC, EventKind, Scheduler},
-};
use image::Executable;
pub struct GameCube {
@@ -52,14 +50,14 @@ pub struct GameCube {
impl GameCube {
#[cfg(feature = "scripting")]
#[inline(always)]
- pub(crate) fn apply_script_hook_state(&mut self, state: ScriptHookState) {
+ pub fn apply_script_hook_state(&mut self, state: ScriptHookState) {
self.script_hook_flags = state.flags;
self.script_hook_filters = state.filters;
}
#[cfg(feature = "scripting")]
#[inline(always)]
- pub(crate) fn sync_pending_script_hook_state(&mut self, host: &mut dyn ScriptHost) {
+ pub fn sync_pending_script_hook_state(&mut self, host: &mut dyn ScriptHost) {
#[cfg(feature = "scripting-mut-traps")]
match host.take_pending_hook_state() {
Ok(Some(state)) => self.apply_script_hook_state(state),
diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rs
index 0409210..c687616 100644
--- a/crates/gecko/src/mmio/bus.rs
+++ b/crates/gecko/src/mmio/bus.rs
@@ -1,16 +1,12 @@
+use crate::gamecube::GameCube;
+use crate::mmio::constants::{
+ AI_BASE, AI_END, CP_BASE, CP_END, DI_BASE, DI_END, DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE, GX_FIFO_END,
+ IPL_BASE, IPL_END, MI_BASE, MI_END, PE_BASE, PE_END, PI_BASE, PI_END, RAM_BASE, RAM_END, SI_BASE, SI_END, VI_BASE,
+ VI_END,
+};
+use crate::mmio::{Mmio, MmioRw};
#[cfg(feature = "scripting")]
use crate::scripting::HookFlags;
-use crate::{
- gamecube::GameCube,
- mmio::{
- Mmio, MmioRw,
- constants::{
- AI_BASE, AI_END, CP_BASE, CP_END, DI_BASE, DI_END, DSP_BASE, DSP_END, EXI_BASE, EXI_END, GX_FIFO_BASE,
- GX_FIFO_END, IPL_BASE, IPL_END, MI_BASE, MI_END, PE_BASE, PE_END, PI_BASE, PI_END, RAM_BASE, RAM_END,
- SI_BASE, SI_END, VI_BASE, VI_END,
- },
- },
-};
enum BusTarget {
Ram,
diff --git a/crates/gecko/src/mmio/fp.rs b/crates/gecko/src/mmio/fp.rs
index 5cc9dd1..486dfc1 100644
--- a/crates/gecko/src/mmio/fp.rs
+++ b/crates/gecko/src/mmio/fp.rs
@@ -1,7 +1,6 @@
-use crate::{
- gamecube::GameCube,
- mmio::{Mmio, constants::RAM_END},
-};
+use crate::gamecube::GameCube;
+use crate::mmio::Mmio;
+use crate::mmio::constants::RAM_END;
impl GameCube {
// Load a 64-bit double from memory
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index dcbf436..4bdfa4b 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -4,17 +4,16 @@ use egui_plot::{Line, Plot, PlotPoints};
use gecko::flipper::si::pad::{self, PadStatus, STICK_CENTER};
use gecko::flipper::vi::regs::RefreshRate;
use gecko::gamecube::GameCube;
-use image::{Dol, dvd::Dvd};
+use image::Dol;
+use image::dvd::Dvd;
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::Instant;
-use winit::{
- application::ApplicationHandler,
- event::WindowEvent,
- event_loop::{ActiveEventLoop, EventLoop},
- keyboard::{KeyCode, PhysicalKey},
- window::{Window, WindowId},
-};
+use winit::application::ApplicationHandler;
+use winit::event::WindowEvent;
+use winit::event_loop::{ActiveEventLoop, EventLoop};
+use winit::keyboard::{KeyCode, PhysicalKey};
+use winit::window::{Window, WindowId};
const SHADER: &str = include_str!("xfb.wgsl");
diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rs
index dea5b6a..8bb0006 100644
--- a/crates/web/src/lib.rs
+++ b/crates/web/src/lib.rs
@@ -9,14 +9,12 @@ use gecko::flipper::vi::regs::RefreshRate;
use gecko::gamecube::GameCube;
use image::Dol;
use wasm_bindgen::prelude::*;
-use winit::{
- application::ApplicationHandler,
- event::WindowEvent,
- event_loop::{ActiveEventLoop, EventLoop},
- keyboard::{KeyCode, PhysicalKey},
- platform::web::{EventLoopExtWebSys, WindowAttributesExtWebSys},
- window::{Window, WindowId},
-};
+use winit::application::ApplicationHandler;
+use winit::event::WindowEvent;
+use winit::event_loop::{ActiveEventLoop, EventLoop};
+use winit::keyboard::{KeyCode, PhysicalKey};
+use winit::platform::web::{EventLoopExtWebSys, WindowAttributesExtWebSys};
+use winit::window::{Window, WindowId};
#[cfg(feature = "debug")]
mod debug_ui;
diff --git a/rustfmt.toml b/rustfmt.toml
index 7530651..18fa062 100644
--- a/rustfmt.toml
+++ b/rustfmt.toml
@@ -1 +1,2 @@
max_width = 120
+imports_granularity = "Module"