Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

wtf

Laylece627736c52b
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index dbd022f..407bf3e 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -51,6 +51,9 @@ pub(crate) struct DrawUniforms {
const SHADER: &str = wesl::include_wesl!("gx_shader");
+pub const EFB_WIDTH: u32 = 640;
+pub const EFB_HEIGHT: u32 = 480;
+
pub struct GxRenderer {
pub(crate) pipeline_cache: HashMap<PipelineKey, wgpu::RenderPipeline>,
pub(crate) shader: wgpu::ShaderModule,
@@ -63,10 +66,11 @@ pub struct GxRenderer {
pub(crate) draw_uniform_capacity: usize,
pub(crate) vertex_buffer: wgpu::Buffer,
pub(crate) vertex_capacity: usize,
- pub(crate) depth_texture: wgpu::Texture,
- pub(crate) depth_view: wgpu::TextureView,
- pub(crate) depth_width: u32,
- pub(crate) depth_height: u32,
+ pub(crate) efb_texture: wgpu::Texture,
+ pub(crate) efb_view: wgpu::TextureView,
+ pub(crate) efb_depth_texture: wgpu::Texture,
+ pub(crate) efb_depth_view: wgpu::TextureView,
+ pub(crate) efb_needs_clear: bool,
pub(crate) sampler_cache: HashMap<(WrapMode, WrapMode, MagFilter, MinFilter), wgpu::Sampler>,
pub(crate) texture_cache: HashMap<(usize, u32, u32, TextureFormat), (wgpu::Texture, wgpu::TextureView)>,
pub(crate) fallback_view: wgpu::TextureView,
@@ -81,8 +85,6 @@ impl GxRenderer {
device: &wgpu::Device,
queue: &wgpu::Queue,
surface_format: wgpu::TextureFormat,
- width: u32,
- height: u32,
) -> Self {
let frame_uniform_size = FrameUniforms::min_size().get();
let draw_uniform_size = DrawUniforms::min_size().get();
@@ -211,9 +213,41 @@ impl GxRenderer {
mapped_at_creation: false,
});
- let depth_width = width.max(1);
- let depth_height = height.max(1);
- let (depth_texture, depth_view) = create_depth_texture(device, depth_width, depth_height);
+ // Create fixed-size EFB
+ let efb_texture = device.create_texture(&wgpu::TextureDescriptor {
+ label: Some("efb_color"),
+ size: wgpu::Extent3d {
+ width: EFB_WIDTH,
+ height: EFB_HEIGHT,
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: surface_format,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT
+ | wgpu::TextureUsages::COPY_SRC
+ | wgpu::TextureUsages::COPY_DST
+ | wgpu::TextureUsages::TEXTURE_BINDING,
+ view_formats: &[],
+ });
+ let efb_view = efb_texture.create_view(&Default::default());
+
+ let efb_depth_texture = device.create_texture(&wgpu::TextureDescriptor {
+ label: Some("efb_depth"),
+ size: wgpu::Extent3d {
+ width: EFB_WIDTH,
+ height: EFB_HEIGHT,
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Depth24Plus,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
+ view_formats: &[],
+ });
+ let efb_depth_view = efb_depth_texture.create_view(&Default::default());
GxRenderer {
pipeline_cache: HashMap::new(),
@@ -227,10 +261,11 @@ impl GxRenderer {
draw_uniform_capacity: 1,
vertex_buffer,
vertex_capacity: initial_capacity,
- depth_texture,
- depth_view,
- depth_width,
- depth_height,
+ efb_texture,
+ efb_view,
+ efb_depth_texture,
+ efb_depth_view,
+ efb_needs_clear: true,
sampler_cache: HashMap::from([(
(WrapMode::Clamp, WrapMode::Clamp, MagFilter::Linear, MinFilter::Linear),
sampler,
@@ -244,39 +279,4 @@ impl GxRenderer {
}
}
- pub fn resize(&mut self, device: &wgpu::Device, width: u32, height: u32) {
- self.ensure_depth_texture(device, width, height);
- }
-
- pub(crate) fn ensure_depth_texture(&mut self, device: &wgpu::Device, width: u32, height: u32) {
- let width = width.max(1);
- let height = height.max(1);
- if (width, height) == (self.depth_width, self.depth_height) {
- return;
- }
- let (tex, view) = create_depth_texture(device, width, height);
- self.depth_texture = tex;
- self.depth_view = view;
- self.depth_width = width;
- self.depth_height = height;
- }
-}
-
-fn create_depth_texture(device: &wgpu::Device, w: u32, h: u32) -> (wgpu::Texture, wgpu::TextureView) {
- let tex = device.create_texture(&wgpu::TextureDescriptor {
- label: Some("gx_depth"),
- size: wgpu::Extent3d {
- width: w.max(1),
- height: h.max(1),
- depth_or_array_layers: 1,
- },
- mip_level_count: 1,
- sample_count: 1,
- dimension: wgpu::TextureDimension::D2,
- format: wgpu::TextureFormat::Depth24Plus,
- usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
- view_formats: &[],
- });
- let view = tex.create_view(&Default::default());
- (tex, view)
}
diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rs
index dffe600..149c472 100644
--- a/crates/backend-wgpu/src/pipeline.rs
+++ b/crates/backend-wgpu/src/pipeline.rs
@@ -169,16 +169,7 @@ impl GxRenderer {
targets: &[Some(wgpu::ColorTargetState {
format: self.surface_format,
blend,
- write_mask: {
- let mut mask = wgpu::ColorWrites::empty();
- if key.color_update {
- mask |= wgpu::ColorWrites::RED | wgpu::ColorWrites::GREEN | wgpu::ColorWrites::BLUE;
- }
- if key.alpha_update {
- mask |= wgpu::ColorWrites::ALPHA;
- }
- mask
- },
+ write_mask: wgpu::ColorWrites::ALL, // TODO: re-enable color_update/alpha_update masking
})],
compilation_options: Default::default(),
}),
diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rs
index 6b3e2d9..1721a2f 100644
--- a/crates/backend-wgpu/src/render.rs
+++ b/crates/backend-wgpu/src/render.rs
@@ -13,27 +13,34 @@ impl GxRenderer {
device: &wgpu::Device,
queue: &wgpu::Queue,
commands: &DrawCommands,
- ram: &[u8],
+ ram: &mut [u8],
target: &wgpu::TextureView,
- target_width: u32,
- target_height: u32,
) {
- self.ensure_depth_texture(device, target_width, target_height);
-
if commands.commands.is_empty() {
return;
}
self.prepare_resources(device, queue, commands, ram);
+ // Render all draws into EFB in one pass, then process copies (readback to RAM + clear).
+ // The caller displays from the XFB data written to RAM, not from the EFB directly.
let (frame_uniform_bytes, draw_call_indices) = self.aggregate_draw_data(device, commands);
- if self.scratch_draws.is_empty() {
- return;
+ if !self.scratch_draws.is_empty() {
+ self.upload_buffers(device, queue, &frame_uniform_bytes);
+ let num_draws = self.scratch_draws.len();
+ self.execute_render_pass_range(device, queue, commands, &draw_call_indices, 0, num_draws);
}
- self.upload_buffers(device, queue, &frame_uniform_bytes);
- self.execute_render_pass(device, queue, commands, target, target_width, target_height, &draw_call_indices);
+ // Blit EFB to screen
+ self.blit_efb_to_target(device, queue, target);
+
+ // Process copies (readback EFB to RAM as YUV422 + clear EFB for next frame)
+ for cmd in &commands.commands {
+ if let GxCommand::CopyEfb(copy) = cmd {
+ self.execute_efb_copy(device, queue, copy, ram);
+ }
+ }
}
fn prepare_resources(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, commands: &DrawCommands, ram: &[u8]) {
@@ -177,16 +184,17 @@ impl GxRenderer {
queue.write_buffer(&self.vertex_buffer, 0, bytemuck::cast_slice(&self.scratch_vertices));
}
- fn execute_render_pass(
+ fn execute_render_pass_range(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
commands: &DrawCommands,
- target: &wgpu::TextureView,
- target_width: u32,
- target_height: u32,
draw_call_indices: &[usize],
+ scratch_start: usize,
+ scratch_end: usize,
) {
+ let target_width = crate::EFB_WIDTH;
+ let target_height = crate::EFB_HEIGHT;
let fallback_sampler_key = (WrapMode::Clamp, WrapMode::Clamp, MagFilter::Linear, MinFilter::Linear);
let frame_stride = align_up(
FrameUniforms::min_size().get(),
@@ -255,21 +263,35 @@ impl GxRenderer {
let mut encoder = device.create_command_encoder(&Default::default());
{
+ // TODO: lol?
+ let needs_initial_clear = self.efb_needs_clear;
+ self.efb_needs_clear = false;
+ let color_load = if needs_initial_clear {
+ wgpu::LoadOp::Clear(wgpu::Color::BLACK)
+ } else {
+ wgpu::LoadOp::Load
+ };
+ let depth_load = if needs_initial_clear {
+ wgpu::LoadOp::Clear(1.0)
+ } else {
+ wgpu::LoadOp::Load
+ };
+
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("gx_render_pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
- view: target,
+ view: &self.efb_view,
resolve_target: None,
ops: wgpu::Operations {
- load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
+ load: color_load,
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
- view: &self.depth_view,
+ view: &self.efb_depth_view,
depth_ops: Some(wgpu::Operations {
- load: wgpu::LoadOp::Clear(1.0),
+ load: depth_load,
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
@@ -280,7 +302,7 @@ impl GxRenderer {
});
rpass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
- for (index, (first_vertex, vertex_count)) in self.scratch_draws.iter().copied().enumerate() {
+ for (index, (first_vertex, vertex_count)) in self.scratch_draws.iter().copied().enumerate().skip(scratch_start).take(scratch_end - scratch_start) {
let GxCommand::Draw(dc) = &commands.commands[draw_call_indices[index]] else {
continue;
};
@@ -324,6 +346,140 @@ impl GxRenderer {
queue.submit([encoder.finish()]);
}
+ fn execute_efb_copy(
+ &mut self,
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ copy: &gecko::flipper::gx::draw::EfbCopyCmd,
+ ram: &mut [u8],
+ ) {
+ let x = copy.src_x.min(crate::EFB_WIDTH);
+ let y = copy.src_y.min(crate::EFB_HEIGHT);
+ let w = copy.src_w.min(crate::EFB_WIDTH.saturating_sub(x));
+ let h = copy.src_h.min(crate::EFB_HEIGHT.saturating_sub(y));
+
+ if w > 0 && h > 0 {
+ let bytes_per_row = align_up(w as u64 * 4, wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as u64) as u32;
+ let buffer_size = bytes_per_row as u64 * h as u64;
+
+ let staging = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("efb_copy_staging"),
+ size: buffer_size,
+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
+ mapped_at_creation: false,
+ });
+
+ let mut encoder = device.create_command_encoder(&Default::default());
+ encoder.copy_texture_to_buffer(
+ wgpu::TexelCopyTextureInfo {
+ texture: &self.efb_texture,
+ mip_level: 0,
+ origin: wgpu::Origin3d { x, y, z: 0 },
+ aspect: wgpu::TextureAspect::default(),
+ },
+ wgpu::TexelCopyBufferInfo {
+ buffer: &staging,
+ layout: wgpu::TexelCopyBufferLayout {
+ offset: 0,
+ bytes_per_row: Some(bytes_per_row),
+ rows_per_image: Some(h),
+ },
+ },
+ wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 },
+ );
+ queue.submit([encoder.finish()]);
+
+ let slice = staging.slice(..);
+ slice.map_async(wgpu::MapMode::Read, |_| {});
+ let _ = device.poll(wgpu::PollType::wait_indefinitely());
+
+ {
+ let data = slice.get_mapped_range();
+ if copy.copy_to_xfb {
+ let is_bgra = matches!(
+ self.surface_format,
+ wgpu::TextureFormat::Bgra8Unorm | wgpu::TextureFormat::Bgra8UnormSrgb
+ );
+ self::encode_xfb_yuv422(&data, bytes_per_row, w, h, ram, copy.dest_addr as usize, is_bgra);
+ }
+ // TODO: tiled texture encode for CopyTex
+ drop(data);
+ }
+ staging.unmap();
+ }
+
+ if copy.clear {
+ // Clear the entire EFB with the game's clear color.
+ // TODO: scoped clear (only the copied region) for proper multi-pass support.
+ let mut encoder = device.create_command_encoder(&Default::default());
+ {
+ let _rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("efb_clear_after_copy"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view: &self.efb_view,
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color {
+ r: copy.clear_color[0] as f64,
+ g: copy.clear_color[1] as f64,
+ b: copy.clear_color[2] as f64,
+ a: copy.clear_color[3] as f64,
+ }),
+ store: wgpu::StoreOp::Store,
+ },
+ depth_slice: None,
+ })],
+ depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
+ view: &self.efb_depth_view,
+ depth_ops: Some(wgpu::Operations {
+ load: wgpu::LoadOp::Clear(copy.clear_z),
+ store: wgpu::StoreOp::Store,
+ }),
+ stencil_ops: None,
+ }),
+ occlusion_query_set: None,
+ timestamp_writes: None,
+ multiview_mask: None,
+ });
+ }
+ queue.submit([encoder.finish()]);
+ }
+ }
+
+ fn blit_efb_to_target(
+ &self,
+ _device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ target: &wgpu::TextureView,
+ ) {
+ let mut encoder = _device.create_command_encoder(&Default::default());
+ let target_size = target.texture().size();
+ let copy_w = crate::EFB_WIDTH.min(target_size.width);
+ let copy_h = crate::EFB_HEIGHT.min(target_size.height);
+
+ encoder.copy_texture_to_texture(
+ wgpu::TexelCopyTextureInfo {
+ texture: &self.efb_texture,
+ mip_level: 0,
+ origin: wgpu::Origin3d::ZERO,
+ aspect: wgpu::TextureAspect::default(),
+ },
+ wgpu::TexelCopyTextureInfo {
+ texture: target.texture(),
+ mip_level: 0,
+ origin: wgpu::Origin3d::ZERO,
+ aspect: wgpu::TextureAspect::default(),
+ },
+ wgpu::Extent3d {
+ width: copy_w,
+ height: copy_h,
+ depth_or_array_layers: 1,
+ },
+ );
+
+ queue.submit([encoder.finish()]);
+ }
+
fn ensure_draw_capacity(&mut self, device: &wgpu::Device, count: usize) {
if count <= self.draw_uniform_capacity {
return;
@@ -348,3 +504,49 @@ fn pack_u32x16_to_uvec4x4(data: &[u32; 16]) -> [UVec4; 4] {
UVec4::new(data[12], data[13], data[14], data[15]),
]
}
+
+/// Encode EFB readback pixels as YUV422 into guest RAM (XFB format).
+/// Pixel data may be RGBA or BGRA depending on the surface format.
+fn encode_xfb_yuv422(
+ pixels: &[u8],
+ bytes_per_row: u32,
+ width: u32,
+ height: u32,
+ ram: &mut [u8],
+ dest: usize,
+ is_bgra: bool,
+) {
+ let row_bytes = width as usize * 2;
+ for y in 0..height as usize {
+ let src_row = &pixels[y * bytes_per_row as usize..];
+ let dst_row_off = dest + y * row_bytes;
+ for px_pair in 0..(width as usize / 2) {
+ let x = px_pair * 2;
+ let s0 = x * 4;
+ let s1 = (x + 1) * 4;
+ let (r1, g1, b1) = if is_bgra {
+ (src_row[s0 + 2] as i32, src_row[s0 + 1] as i32, src_row[s0] as i32)
+ } else {
+ (src_row[s0] as i32, src_row[s0 + 1] as i32, src_row[s0 + 2] as i32)
+ };
+ let (r2, g2, b2) = if is_bgra {
+ (src_row[s1 + 2] as i32, src_row[s1 + 1] as i32, src_row[s1] as i32)
+ } else {
+ (src_row[s1] as i32, src_row[s1 + 1] as i32, src_row[s1 + 2] as i32)
+ };
+
+ let y1 = ((77 * r1 + 150 * g1 + 29 * b1) / 256).clamp(0, 255) as u8;
+ let y2 = ((77 * r2 + 150 * g2 + 29 * b2) / 256).clamp(0, 255) as u8;
+ let cb = ((112 * (b1 + b2) - 74 * (g1 + g2) - 38 * (r1 + r2)) / 512 + 128).clamp(0, 255) as u8;
+ let cr = ((112 * (r1 + r2) - 94 * (g1 + g2) - 18 * (b1 + b2)) / 512 + 128).clamp(0, 255) as u8;
+
+ let off = dst_row_off + px_pair * 4;
+ if off + 3 < ram.len() {
+ ram[off] = y1;
+ ram[off + 1] = cb;
+ ram[off + 2] = y2;
+ ram[off + 3] = cr;
+ }
+ }
+ }
+}
diff --git a/crates/debugger/src/render.rs b/crates/debugger/src/render.rs
index 915fa51..e2c3775 100644
--- a/crates/debugger/src/render.rs
+++ b/crates/debugger/src/render.rs
@@ -57,7 +57,7 @@ impl RenderState {
.unwrap_or(surface_caps.formats[0]);
let surface_config = wgpu::SurfaceConfiguration {
- usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
format: surface_format,
width: size.width.max(1),
height: size.height.max(1),
@@ -128,13 +128,7 @@ impl RenderState {
});
let (texture, bind_group) = create_xfb_texture(&device, &bind_group_layout, w, h);
- let gx_renderer = backend_wgpu::GxRenderer::new(
- &device,
- &queue,
- surface_format,
- surface_config.width,
- surface_config.height,
- );
+ let gx_renderer = backend_wgpu::GxRenderer::new(&device, &queue, surface_format);
let egui_ctx = egui::Context::default();
let mut fonts = egui::FontDefinitions::default();
@@ -172,7 +166,6 @@ impl RenderState {
self.surface_config.width = width;
self.surface_config.height = height;
self.surface.configure(&self.device, &self.surface_config);
- self.gx_renderer.resize(&self.device, width, height);
}
pub fn render(&mut self, emulator: &mut GameCube, debugger_ui: &mut DebuggerUi, window: &Window) {
@@ -449,10 +442,8 @@ impl RenderState {
&self.device,
&self.queue,
&emulator.gx.draw_commands,
- &emulator.mmio.ram,
+ &mut emulator.mmio.ram,
view,
- self.surface_config.width,
- self.surface_config.height,
);
}
diff --git a/crates/gecko/src/flipper/gx/bp.rs b/crates/gecko/src/flipper/gx/bp.rs
index 0fdf298..069b6f6 100644
--- a/crates/gecko/src/flipper/gx/bp.rs
+++ b/crates/gecko/src/flipper/gx/bp.rs
@@ -1,8 +1,11 @@
use super::constants::{
- BP_GEN_MODE, BP_PE_ALPHA_COMPARE, BP_PE_CMODE0, BP_PE_DONE, BP_PE_DONE_FINISH_BIT, BP_PE_TOKEN, BP_PE_TOKEN_INT,
- BP_PE_ZMODE, BP_RAS1_TREF_COUNT, BP_RAS1_TREF0, BP_SU_SCIS_BR, BP_SU_SCIS_OFFSET, BP_SU_SCIS_TL,
- 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, EFB_HEIGHT, EFB_WIDTH,
+ BP_GEN_MODE, BP_PE_ALPHA_COMPARE, BP_PE_CMODE0, BP_PE_COPY_CLEAR_AR, BP_PE_COPY_CLEAR_GB, BP_PE_COPY_CLEAR_Z,
+ BP_PE_COPY_CMD, BP_PE_COPY_DIMS, BP_PE_COPY_DST, BP_PE_COPY_DST_STRIDE, BP_PE_COPY_SRC, BP_PE_DONE,
+ BP_PE_DONE_FINISH_BIT, BP_PE_TOKEN, BP_PE_TOKEN_INT, BP_PE_ZMODE, BP_RAS1_TREF_COUNT, BP_RAS1_TREF0,
+ BP_SU_SCIS_BR, BP_SU_SCIS_OFFSET, BP_SU_SCIS_TL, BP_TEV_COLOR_ENV_0, BP_TEV_KSEL_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, EFB_HEIGHT,
+ EFB_WIDTH,
};
use super::regs::{
AlphaCompare, BlendMode, GenMode, TevAlphaEnv, TevColorEnv, TevOrder, TevRegType, TevRegisterH, TevRegisterL,
@@ -69,6 +72,16 @@ impl GraphicsProcessor {
self.resolve_konst_colors();
}
+ // TEV KSEL (constant color selection) registers: recalculate konst colors
+ if idx >= BP_TEV_KSEL_0 && idx <= BP_TEV_KSEL_0 + 7 {
+ self.resolve_konst_colors();
+ }
+
+ // TEV color register writes also affect konst colors
+ if idx >= BP_TEV_REGISTERL_0 && idx <= BP_TEV_REGISTERL_0 + 7 {
+ self.resolve_konst_colors();
+ }
+
// PE finish
if idx == BP_PE_DONE && (val & BP_PE_DONE_FINISH_BIT) != 0 {
self.raise_interrupt = true;
@@ -92,6 +105,11 @@ impl GraphicsProcessor {
if idx == BP_SU_SCIS_TL || idx == BP_SU_SCIS_BR || idx == BP_SU_SCIS_OFFSET {
self.recompute_scissor();
}
+
+ // EFB copy trigger (BP 0x52)
+ if idx == BP_PE_COPY_CMD {
+ self.efb_copy(val);
+ }
}
fn snapshot_texture(&mut self, slot: usize, image3_val: u32) {
@@ -204,6 +222,64 @@ impl GraphicsProcessor {
}
}
+ fn efb_copy(&mut self, trigger: u32) {
+ // Decode source rect from BP 0x49/0x4A
+ let src_reg = self.bp_regs[BP_PE_COPY_SRC];
+ let dims_reg = self.bp_regs[BP_PE_COPY_DIMS];
+ let src_x = (src_reg >> 10) & 0x3FF;
+ let src_y = src_reg & 0x3FF;
+ let src_w = ((dims_reg >> 10) & 0x3FF) + 1;
+ let src_h = (dims_reg & 0x3FF) + 1;
+
+ // Destination address (BP 0x4B, shifted left by 5)
+ let dest_addr = (self.bp_regs[BP_PE_COPY_DST] & 0x00FFFFFF) << 5;
+ let dest_stride = self.bp_regs[BP_PE_COPY_DST_STRIDE] & 0x3FF;
+
+ // Trigger word bits
+ let copy_to_xfb = (trigger >> 14) & 1 != 0;
+ let clear = (trigger >> 11) & 1 != 0;
+ let half = (trigger >> 9) & 1 != 0;
+
+ // Clear color from BP 0x4F/0x50
+ let clear_ar = self.bp_regs[BP_PE_COPY_CLEAR_AR];
+ let clear_gb = self.bp_regs[BP_PE_COPY_CLEAR_GB];
+ let a = ((clear_ar >> 8) & 0xFF) as f32 / 255.0;
+ let r = (clear_ar & 0xFF) as f32 / 255.0;
+ let g = ((clear_gb >> 8) & 0xFF) as f32 / 255.0;
+ let b = (clear_gb & 0xFF) as f32 / 255.0;
+
+ // Clear Z from BP 0x51
+ let clear_z_raw = self.bp_regs[BP_PE_COPY_CLEAR_Z] & 0x00FFFFFF;
+ let clear_z = clear_z_raw as f32 / 16777215.0;
+
+ tracing::debug!(
+ src_x,
+ src_y,
+ src_w,
+ src_h,
+ dest_addr = format!("{dest_addr:#010X}"),
+ copy_to_xfb,
+ clear,
+ half,
+ "EFB copy triggered"
+ );
+
+ // Snapshot all copy state at enqueue time
+ self.draw_commands.commands.push(draw::GxCommand::CopyEfb(draw::EfbCopyCmd {
+ src_x,
+ src_y,
+ src_w,
+ src_h,
+ dest_addr,
+ dest_stride,
+ copy_to_xfb,
+ clear,
+ clear_color: [r, g, b, a],
+ clear_z,
+ half,
+ }));
+ }
+
fn recompute_scissor(&mut self) {
let tl = self.bp_regs[BP_SU_SCIS_TL];
let br = self.bp_regs[BP_SU_SCIS_BR];
diff --git a/crates/gecko/src/flipper/gx/constants.rs b/crates/gecko/src/flipper/gx/constants.rs
index a3b5d00..6f2d7f8 100644
--- a/crates/gecko/src/flipper/gx/constants.rs
+++ b/crates/gecko/src/flipper/gx/constants.rs
@@ -130,6 +130,17 @@ pub const BP_SU_SCIS_TL: usize = 0x20;
pub const BP_SU_SCIS_BR: usize = 0x21;
pub const BP_SU_SCIS_OFFSET: usize = 0x59;
+// BP EFB copy registers
+pub const BP_PE_COPY_SRC: usize = 0x49;
+pub const BP_PE_COPY_DIMS: usize = 0x4A;
+pub const BP_PE_COPY_DST: usize = 0x4B;
+pub const BP_PE_COPY_DST_STRIDE: usize = 0x4D;
+pub const BP_PE_COPY_YSCALE: usize = 0x4E;
+pub const BP_PE_COPY_CLEAR_AR: usize = 0x4F;
+pub const BP_PE_COPY_CLEAR_GB: usize = 0x50;
+pub const BP_PE_COPY_CLEAR_Z: usize = 0x51;
+pub const BP_PE_COPY_CMD: usize = 0x52;
+
// EFB dimensions
pub const EFB_WIDTH: u32 = 640;
pub const EFB_HEIGHT: u32 = 480;
diff --git a/crates/tinyapp/src/app.rs b/crates/tinyapp/src/app.rs
index 806e01e..6543ac3 100644
--- a/crates/tinyapp/src/app.rs
+++ b/crates/tinyapp/src/app.rs
@@ -71,7 +71,7 @@ impl State {
.unwrap_or(surface_caps.formats[0]);
let surface_config = wgpu::SurfaceConfiguration {
- usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
format: surface_format,
width: size.width.max(1),
height: size.height.max(1),
@@ -142,13 +142,7 @@ impl State {
});
let (texture, bind_group) = create_xfb_texture(&device, &bind_group_layout, w, h);
- let gx_renderer = backend_wgpu::GxRenderer::new(
- &device,
- &queue,
- surface_format,
- surface_config.width,
- surface_config.height,
- );
+ let gx_renderer = backend_wgpu::GxRenderer::new(&device, &queue, surface_format);
let egui_ctx = egui::Context::default();
let egui_renderer = egui_wgpu::Renderer::new(&device, surface_format, egui_wgpu::RendererOptions::default());
@@ -183,7 +177,6 @@ impl State {
self.surface_config.width = width;
self.surface_config.height = height;
self.surface.configure(&self.device, &self.surface_config);
- self.gx_renderer.resize(&self.device, width, height);
}
fn render(&mut self, frame_rx: &Receiver<FrameMessage>, window: &Window) {
@@ -192,7 +185,7 @@ impl State {
msg = Some(newer); // drain
}
- let msg = match msg {
+ let mut msg = match msg {
Some(m) => m,
None => return,
};
@@ -219,7 +212,7 @@ impl State {
let view = frame.texture.create_view(&Default::default());
// Render emulator output
- match &msg.data {
+ match &mut msg.data {
FrameData::Gx { commands, ram } => {
self.render_gx(commands, ram, &view);
}
@@ -309,16 +302,9 @@ impl State {
frame.present();
}
- fn render_gx(&mut self, commands: &DrawCommands, ram: &[u8], view: &wgpu::TextureView) {
- self.gx_renderer.render(
- &self.device,
- &self.queue,
- commands,
- ram,
- view,
- self.surface_config.width,
- self.surface_config.height,
- );
+ fn render_gx(&mut self, commands: &DrawCommands, ram: &mut [u8], view: &wgpu::TextureView) {
+ self.gx_renderer
+ .render(&self.device, &self.queue, commands, ram, view);
}
fn render_xfb(&mut self, pixels: &[u32], w: u32, h: u32, view: &wgpu::TextureView) {
@@ -490,3 +476,4 @@ impl ApplicationHandler for App {
}
}
}
+
diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rs
index bd49f8d..15d4a32 100644
--- a/crates/web/src/lib.rs
+++ b/crates/web/src/lib.rs
@@ -88,7 +88,7 @@ impl State {
let max_dim = adapter.limits().max_texture_dimension_2d;
let surface_config = wgpu::SurfaceConfiguration {
- usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
format: surface_format,
width: size.width.max(1).min(max_dim),
height: size.height.max(1).min(max_dim),
@@ -159,13 +159,7 @@ impl State {
});
let (texture, bind_group) = create_xfb_texture(&device, &bind_group_layout, w, h);
- let gx_renderer = backend_wgpu::GxRenderer::new(
- &device,
- &queue,
- surface_format,
- surface_config.width,
- surface_config.height,
- );
+ let gx_renderer = backend_wgpu::GxRenderer::new(&device, &queue, surface_format);
let egui_ctx = egui::Context::default();
#[cfg(feature = "debug")]
@@ -327,10 +321,8 @@ impl State {
&self.device,
&self.queue,
&emulator.gx.draw_commands,
- &emulator.mmio.ram,
+ &mut emulator.mmio.ram,
view,
- self.surface_config.width,
- self.surface_config.height,
);
}