Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

ppl seem to like freecam so we give them freecam (old, unmerged test, experimental)

Layle0125fbe1284d
diff --git a/Cargo.lock b/Cargo.lock
index 19b536f..36e8816 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1525,6 +1525,7 @@ dependencies = [
"egui",
"egui-phosphor",
"gecko",
+ "glam 0.33.2",
"image 0.1.0",
]
@@ -2456,6 +2457,12 @@ dependencies = [
"bytemuck",
]
+[[package]]
+name = "glam"
+version = "0.33.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f22fb22f065b308be0d8724e3706c7fa3fc2a6c7d6899df4cad7860e7a75436"
+
[[package]]
name = "glob"
version = "0.3.3"
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index ec1a761..cde4fa8 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -138,8 +138,13 @@ impl GxRenderer {
let frame_struct_size = FRAME_UNIFORMS_SIZE.get() as usize;
match action {
- GxAction::SetProjection { matrix, .. } => {
+ GxAction::SetProjection { matrix, is_perspective } => {
self.current_projection = Mat4::from_cols_array_2d(matrix);
+ self.current_projection_perspective = *is_perspective;
+ }
+ GxAction::SetFreelook { matrix, enabled } => {
+ self.freelook = Mat4::from_cols_array_2d(matrix);
+ self.freelook_enabled = *enabled;
}
GxAction::SetViewport(vp) => {
self.current_viewport = *vp;
@@ -334,8 +339,14 @@ impl GxRenderer {
return;
}
- // Build per-draw uniform using tracked projection.
- let mvp = self.current_projection;
+ // Build per-draw uniform using tracked projection. The
+ // freecam view transform only applies to perspective draws
+ // so 2D/HUD passes stay glued to the screen.
+ let mvp = if self.freelook_enabled && self.current_projection_perspective {
+ self.current_projection * self.freelook
+ } else {
+ self.current_projection
+ };
let active = draw_active_slot_mask(draw);
let mut tex_dims = [UVec4::splat(1); 4];
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index 4a5e0de..41572f0 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -331,6 +331,9 @@ pub struct GxRenderer {
pub(crate) frame_stride: usize,
// Tracked GX state (updated by state-change actions)
pub(crate) current_projection: Mat4,
+ pub(crate) current_projection_perspective: bool,
+ pub(crate) freelook: Mat4,
+ pub(crate) freelook_enabled: bool,
pub(crate) current_viewport: Viewport,
pub(crate) current_scissor: Scissor,
pub(crate) current_zmode: ZMode,
@@ -976,6 +979,9 @@ impl GxRenderer {
device.limits().min_uniform_buffer_offset_alignment as u64,
) as usize,
current_projection: Mat4::IDENTITY,
+ current_projection_perspective: false,
+ freelook: Mat4::IDENTITY,
+ freelook_enabled: false,
current_viewport: Viewport::default(),
current_scissor: Scissor::default(),
current_zmode: ZMode::default(),
diff --git a/crates/dbglib/Cargo.toml b/crates/dbglib/Cargo.toml
index c66633a..71e75a0 100644
--- a/crates/dbglib/Cargo.toml
+++ b/crates/dbglib/Cargo.toml
@@ -8,4 +8,5 @@ gecko = { path = "../gecko", default-features = false }
disasm = { path = "../disasm" }
image = { path = "../image", default-features = false }
egui = "0.34.2"
+glam = "0.33.2"
egui-phosphor = "0.12.0"
diff --git a/crates/dbglib/src/windows/gx.rs b/crates/dbglib/src/windows/gx.rs
index aabf510..98905a2 100644
--- a/crates/dbglib/src/windows/gx.rs
+++ b/crates/dbglib/src/windows/gx.rs
@@ -3,6 +3,107 @@ use gecko::SystemId;
use gecko::flipper::gx::GraphicsProcessor;
use gecko::flipper::gx::draw::{TextureDescriptor, TlutRef};
use gecko::mmio::Mmio;
+use glam::{Mat3, Mat4, Vec3};
+
+pub struct Freecam {
+ pub enabled: bool,
+ pub speed: f32,
+ pos: Vec3,
+ yaw: f32,
+ pitch: f32,
+ sent_disable: bool,
+}
+
+impl Default for Freecam {
+ fn default() -> Self {
+ Freecam {
+ enabled: false,
+ speed: 50.0,
+ pos: Vec3::ZERO,
+ yaw: 0.0,
+ pitch: 0.0,
+ sent_disable: true,
+ }
+ }
+}
+
+impl Freecam {
+ pub fn reset(&mut self) {
+ self.pos = Vec3::ZERO;
+ self.yaw = 0.0;
+ self.pitch = 0.0;
+ }
+
+ pub fn update_input(&mut self, ctx: &Context) {
+ if !self.enabled {
+ return;
+ }
+
+ let pointer_free = !ctx.egui_wants_pointer_input();
+ let keys_free = !ctx.egui_wants_keyboard_input();
+
+ ctx.input(|i| {
+ if pointer_free && i.pointer.secondary_down() {
+ let d = i.pointer.delta();
+ self.yaw -= d.x * 0.005;
+ self.pitch = (self.pitch - d.y * 0.005).clamp(-1.55, 1.55);
+ }
+
+ if pointer_free && i.smooth_scroll_delta.y != 0.0 {
+ self.speed = (self.speed * 1.2f32.powf(i.smooth_scroll_delta.y / 40.0)).clamp(0.01, 1e6);
+ }
+
+ if !keys_free {
+ return;
+ }
+
+ let mut dir = Vec3::ZERO;
+ if i.key_down(egui::Key::W) {
+ dir.z -= 1.0;
+ }
+ if i.key_down(egui::Key::S) {
+ dir.z += 1.0;
+ }
+ if i.key_down(egui::Key::A) {
+ dir.x -= 1.0;
+ }
+ if i.key_down(egui::Key::D) {
+ dir.x += 1.0;
+ }
+ if i.key_down(egui::Key::Q) {
+ dir.y -= 1.0;
+ }
+ if i.key_down(egui::Key::E) {
+ dir.y += 1.0;
+ }
+ if dir == Vec3::ZERO {
+ return;
+ }
+
+ let boost = if i.modifiers.shift { 5.0 } else { 1.0 };
+ let dt = i.stable_dt.min(0.1);
+ self.pos += self.rotation() * dir.normalize() * self.speed * boost * dt;
+ });
+ }
+
+ pub fn take_action(&mut self) -> Option<([[f32; 4]; 4], bool)> {
+ if self.enabled {
+ self.sent_disable = false;
+
+ let view = Mat4::from_mat3(self.rotation().transpose()) * Mat4::from_translation(-self.pos);
+ Some((view.to_cols_array_2d(), true))
+ } else if !self.sent_disable {
+ self.sent_disable = true;
+ Some((Mat4::IDENTITY.to_cols_array_2d(), false))
+ } else {
+ None
+ }
+ }
+
+ fn rotation(&self) -> Mat3 {
+ Mat3::from_rotation_y(self.yaw) * Mat3::from_rotation_x(self.pitch)
+ }
+}
fn texture_preview(ui: &mut egui::Ui, tex: &TextureDescriptor, ram: &[u8], palette: &[u16], tlut: TlutRef) {
let rgba = gecko::flipper::gx::texture::decode_to_rgba(ram, tex, palette, tlut.format);
@@ -29,6 +130,7 @@ pub fn show_gx<const SYSTEM: SystemId>(
mmio: &Mmio<SYSTEM>,
invalidate_caches: &mut bool,
dump_textures: &mut bool,
+ freecam: &mut Freecam,
) {
egui::Window::new("GX").open(open).show(ctx, |ui| {
ScrollArea::vertical().show(ui, |ui| {
@@ -46,6 +148,22 @@ pub fn show_gx<const SYSTEM: SystemId>(
});
ui.separator();
+ // Freecam
+ ui.collapsing("Freecam", |ui| {
+ ui.checkbox(&mut freecam.enabled, "Unlock camera");
+
+ ui.horizontal(|ui| {
+ ui.label("Speed");
+ ui.add(egui::Slider::new(&mut freecam.speed, 0.01..=1_000_000.0).logarithmic(true));
+ });
+
+ if ui.button("Reset view").clicked() {
+ freecam.reset();
+ }
+
+ ui.label("Hold RMB to look, WASD + QE to fly, Shift boosts, scroll tunes speed");
+ });
+
// Projection matrix
ui.collapsing("Projection", |ui| {
Grid::new("proj").num_columns(4).show(ui, |ui| {
diff --git a/crates/debugger/src/debugger.rs b/crates/debugger/src/debugger.rs
index 8181c6f..b0bc834 100644
--- a/crates/debugger/src/debugger.rs
+++ b/crates/debugger/src/debugger.rs
@@ -37,6 +37,7 @@ pub struct DebuggerUi {
pub fifo_path_input: String,
pub fifo_last_result: String,
pub fifo_action: Option<dbglib::windows::fifo::FifoRecorderAction>,
+ pub freecam: dbglib::windows::gx::Freecam,
}
impl Default for DebuggerUi {
@@ -70,6 +71,7 @@ impl Default for DebuggerUi {
fifo_path_input: "fifo_dump.dff".to_string(),
fifo_last_result: String::new(),
fifo_action: None,
+ freecam: Default::default(),
}
}
}
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index e1084e6..e7cbdf3 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -115,6 +115,7 @@ struct App {
state: Option<RenderState>,
present_mode: wgpu::PresentMode,
init: Option<AppInit>,
+ freecam_input_blocked: bool,
}
struct AppInit {
@@ -146,6 +147,23 @@ impl App {
EmulatorVariant::Gc(emu) => state.render(emu, &mut self.ui, window),
EmulatorVariant::Wii(emu) => state.render(emu, &mut self.ui, window),
}
+
+ if self.ui.freecam.enabled != self.freecam_input_blocked {
+ self.freecam_input_blocked = self.ui.freecam.enabled;
+
+ if self.freecam_input_blocked {
+ let neutral = self.emulator.neutral_input();
+ self.emulator.apply_host_input(&neutral);
+ } else {
+ self.emulator.apply_host_input(&self.input);
+ }
+ }
+ }
+
+ fn apply_game_input(&mut self) {
+ if !self.ui.freecam.enabled {
+ self.emulator.apply_host_input(&self.input);
+ }
}
}
@@ -271,7 +289,7 @@ impl ApplicationHandler<UserEvent> for App {
update_nunchuk_keys(nunchuk_buttons, nunchuk_stick_x, nunchuk_stick_y, key, pressed);
}
}
- self.emulator.apply_host_input(&self.input);
+ self.apply_game_input();
}
}
}
@@ -287,7 +305,7 @@ impl ApplicationHandler<UserEvent> for App {
&& let HostInput::Wii { wiimote_buttons, .. } = &mut self.input
{
set_bit(wiimote_buttons, mask, pressed);
- self.emulator.apply_host_input(&self.input);
+ self.apply_game_input();
}
}
WindowEvent::CursorMoved { position, .. } => {
@@ -317,13 +335,13 @@ impl ApplicationHandler<UserEvent> for App {
&& *ir_pointer != Some((ir_x, ir_y))
{
*ir_pointer = Some((ir_x, ir_y));
- self.emulator.apply_host_input(&self.input);
+ self.apply_game_input();
}
}
WindowEvent::CursorLeft { .. } => {
if let HostInput::Wii { ir_pointer, .. } = &mut self.input {
*ir_pointer = None;
- self.emulator.apply_host_input(&self.input);
+ self.apply_game_input();
}
}
WindowEvent::RedrawRequested => {
@@ -554,6 +572,7 @@ fn main() {
window: None,
state: None,
present_mode,
+ freecam_input_blocked: false,
init: Some(AppInit {
instance,
adapter,
diff --git a/crates/debugger/src/render.rs b/crates/debugger/src/render.rs
index 6737e72..f3f1ad9 100644
--- a/crates/debugger/src/render.rs
+++ b/crates/debugger/src/render.rs
@@ -276,8 +276,10 @@ impl RenderState {
mmio,
&mut debugger_ui.gx_invalidate_requested,
&mut debugger_ui.gx_dump_requested,
+ &mut debugger_ui.freecam,
);
}
+ debugger_ui.freecam.update_input(&ctx);
if debugger_ui.show_mmio {
dbglib::windows::mmio::show_mmio(
&ctx,
@@ -380,6 +382,10 @@ impl RenderState {
emulator.gx.texture_hashes.clear();
emulator.render_sink.exec(gecko::host::GxAction::InvalidateCaches);
}
+
+ if let Some((matrix, enabled)) = debugger_ui.freecam.take_action() {
+ emulator.render_sink.exec(gecko::host::GxAction::SetFreelook { matrix, enabled });
+ }
#[cfg(not(target_arch = "wasm32"))]
if std::mem::take(&mut debugger_ui.gx_dump_requested) {
emulator.render_sink.exec(gecko::host::GxAction::DumpTextures {
diff --git a/crates/gecko/src/host.rs b/crates/gecko/src/host.rs
index 245d47f..96cd375 100644
--- a/crates/gecko/src/host.rs
+++ b/crates/gecko/src/host.rs
@@ -34,6 +34,14 @@ pub enum GxAction {
},
SetViewport(Viewport),
+ /// Debug action: extra free-look view transform composed with the
+ /// projection on perspective draws (freecam). Disabled restores normal
+ /// rendering.
+ SetFreelook {
+ matrix: [[f32; 4]; 4],
+ enabled: bool,
+ },
+
// BP
SetScissor(Scissor),
SetDepthMode(ZMode),
diff --git a/crates/web/Cargo.lock b/crates/web/Cargo.lock
index a90df50..9a8242a 100644
--- a/crates/web/Cargo.lock
+++ b/crates/web/Cargo.lock
@@ -533,6 +533,7 @@ dependencies = [
"egui",
"egui-phosphor",
"gecko",
+ "glam",
"image",
]
diff --git a/crates/web/src/debug_ui.rs b/crates/web/src/debug_ui.rs
index ccefa5d..8fbe722 100644
--- a/crates/web/src/debug_ui.rs
+++ b/crates/web/src/debug_ui.rs
@@ -22,6 +22,7 @@ pub struct DebugState {
pub dvd_cover_open: Option<bool>,
pub gx_invalidate_requested: bool,
pub gx_dump_requested: bool,
+ pub freecam: dbglib::windows::gx::Freecam,
}
impl Default for DebugState {
@@ -48,6 +49,7 @@ impl Default for DebugState {
dvd_cover_open: None,
gx_invalidate_requested: false,
gx_dump_requested: false,
+ freecam: Default::default(),
}
}
}
@@ -69,6 +71,11 @@ impl DebugState {
// Dump action is for desktop only.
self.gx_dump_requested = false;
+
+ if let Some((matrix, enabled)) = self.freecam.take_action() {
+ emulator.render_sink.exec(gecko::host::GxAction::SetFreelook { matrix, enabled });
+ }
+
self.debugger.tick(emulator);
}
@@ -145,8 +152,10 @@ impl DebugState {
&emulator.mmio,
&mut self.gx_invalidate_requested,
&mut self.gx_dump_requested,
+ &mut self.freecam,
);
}
+ self.freecam.update_input(ctx);
if self.show_mmio {
dbglib::windows::mmio::show_mmio(
ctx,
diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rs
index e8010ab..6085a00 100644
--- a/crates/web/src/lib.rs
+++ b/crates/web/src/lib.rs
@@ -483,6 +483,8 @@ struct App {
state: SharedState,
#[cfg(feature = "debug")]
debug_state: debug_ui::DebugState,
+ #[cfg(feature = "debug")]
+ freecam_input_blocked: bool,
}
impl ApplicationHandler for App {
@@ -530,6 +532,12 @@ impl ApplicationHandler for App {
if let HostInput::Gc(pad) = &mut self.input {
update_pad(pad, key, pressed);
}
+
+ #[cfg(feature = "debug")]
+ if self.debug_state.freecam.enabled {
+ return;
+ }
+
self.emulator.apply_host_input(&self.input);
}
}
@@ -544,6 +552,18 @@ impl ApplicationHandler for App {
&window,
);
}
+
+ #[cfg(feature = "debug")]
+ if self.debug_state.freecam.enabled != self.freecam_input_blocked {
+ self.freecam_input_blocked = self.debug_state.freecam.enabled;
+
+ if self.freecam_input_blocked {
+ self.emulator.apply_host_input(&HostInput::gc_connected());
+ } else {
+ self.emulator.apply_host_input(&self.input);
+ }
+ }
+
window.request_redraw();
}
}
@@ -592,6 +612,8 @@ pub fn start_emulator(rom_data: &[u8], filename: String, dsp_irom: Option<Vec<u8
state: Rc::new(RefCell::new(None)),
#[cfg(feature = "debug")]
debug_state: debug_ui::DebugState::default(),
+ #[cfg(feature = "debug")]
+ freecam_input_blocked: false,
};
event_loop.spawn_app(app);