Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

UPSCALING

Layle6604914dfd12
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rs
index bd00605..8b2fc7a 100644
--- a/crates/app/src/app.rs
+++ b/crates/app/src/app.rs
@@ -43,6 +43,7 @@ pub enum Message {
MenuToggleCpu(CpuMode),
MenuToggleSkipIpl,
MenuSetTheme(ThemePreference),
+ MenuSetUpscale(u32),
MenuAbout,
AboutClose,
MenuInputSettings,
@@ -370,6 +371,11 @@ impl App {
self.refresh_palette();
Task::none()
}
+ Message::MenuSetUpscale(scale) => {
+ self.config.upscale = scale;
+ self.persist_config();
+ Task::none()
+ }
Message::MenuAbout => {
self.about_open = true;
Task::none()
@@ -609,7 +615,13 @@ impl App {
let bg = palette.bg;
let text_color = palette.text;
let main = column![
- menubar::menubar(palette, self.config.cpu_mode, self.config.theme, self.config.skip_ipl),
+ menubar::menubar(
+ palette,
+ self.config.cpu_mode,
+ self.config.theme,
+ self.config.skip_ipl,
+ self.config.upscale,
+ ),
search_bar::search_bar(palette, &self.search),
body,
statusbar::statusbar(
diff --git a/crates/app/src/config.rs b/crates/app/src/config.rs
index e992692..272e7d1 100644
--- a/crates/app/src/config.rs
+++ b/crates/app/src/config.rs
@@ -17,6 +17,8 @@ pub struct Config {
pub ipl: Option<PathBuf>,
#[serde(default = "self::default_skip_ipl")]
pub skip_ipl: bool,
+ #[serde(default = "self::default_upscale")]
+ pub upscale: u32,
pub input: hostinput::InputConfig,
}
@@ -24,6 +26,10 @@ fn default_skip_ipl() -> bool {
true
}
+fn default_upscale() -> u32 {
+ 1
+}
+
impl Default for Config {
fn default() -> Self {
Self {
@@ -36,6 +42,7 @@ impl Default for Config {
dsp_coef: None,
ipl: None,
skip_ipl: self::default_skip_ipl(),
+ upscale: self::default_upscale(),
input: hostinput::InputConfig::default(),
}
}
diff --git a/crates/app/src/player/state.rs b/crates/app/src/player/state.rs
index ee70ae2..71c2214 100644
--- a/crates/app/src/player/state.rs
+++ b/crates/app/src/player/state.rs
@@ -38,6 +38,7 @@ pub struct PlayerState {
shutdown: Arc<AtomicBool>,
throttle: Arc<AtomicBool>,
aspect: TargetAspect,
+ upscale: u32,
input: Arc<Mutex<HostInput>>,
platform: Platform,
boot_error: Option<String>,
@@ -89,6 +90,7 @@ impl PlayerState {
shutdown: Arc::new(AtomicBool::new(false)),
throttle: Arc::new(AtomicBool::new(true)),
aspect,
+ upscale: config.upscale,
input: Arc::new(Mutex::new(neutral)),
platform: game.platform,
boot_error,
@@ -246,7 +248,7 @@ fn player_thread(
let dvd = image::load_dvd(dvd_data);
let game_id = dvd.header().game_id();
- let (renderer, sink) = Renderer::new(device, queue, format, state.aspect);
+ let (renderer, sink) = Renderer::new(device, queue, format, state.aspect, state.upscale);
{
let first_frame = state.first_frame.clone();
renderer.set_frame_ready_callback(move |_| {
diff --git a/crates/app/src/widgets/menubar.rs b/crates/app/src/widgets/menubar.rs
index 116223f..0d2f9c6 100644
--- a/crates/app/src/widgets/menubar.rs
+++ b/crates/app/src/widgets/menubar.rs
@@ -15,6 +15,7 @@ pub fn menubar(
cpu: CpuMode,
theme_pref: ThemePreference,
skip_ipl: bool,
+ upscale: u32,
) -> Element<'static, Message> {
let bar_bg = palette.bg_2;
let border = palette.border;
@@ -24,7 +25,7 @@ pub fn menubar(
Item::with_menu(self::top_label(palette, "File"), self::file_menu(palette)),
Item::with_menu(
self::top_label(palette, "Settings"),
- self::settings_menu(palette, cpu, theme_pref, skip_ipl),
+ self::settings_menu(palette, cpu, theme_pref, skip_ipl, upscale),
),
Item::with_menu(
self::top_label(palette, "Compatibility"),
@@ -210,6 +211,7 @@ fn settings_menu(
cpu: CpuMode,
theme_pref: ThemePreference,
skip_ipl: bool,
+ upscale: u32,
) -> Menu<'static, Message, iced::Theme, iced::Renderer> {
Menu::new(vec![
Item::new(self::section_header(palette, "Execution Engine")),
@@ -234,6 +236,32 @@ fn settings_menu(
Some(skip_ipl),
)),
Item::new(self::separator(palette)),
+ Item::new(self::section_header(palette, "Internal Resolution")),
+ Item::new(self::menu_item(
+ palette,
+ "1x (Native)",
+ Message::MenuSetUpscale(1),
+ Some(upscale == 1),
+ )),
+ Item::new(self::menu_item(
+ palette,
+ "2x",
+ Message::MenuSetUpscale(2),
+ Some(upscale == 2),
+ )),
+ Item::new(self::menu_item(
+ palette,
+ "3x",
+ Message::MenuSetUpscale(3),
+ Some(upscale == 3),
+ )),
+ Item::new(self::menu_item(
+ palette,
+ "4x",
+ Message::MenuSetUpscale(4),
+ Some(upscale == 4),
+ )),
+ Item::new(self::separator(palette)),
Item::new(self::section_header(palette, "Theme")),
Item::new(self::menu_item(
palette,
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index f36ab55..a2b182c 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -293,7 +293,29 @@ impl GxRenderer {
// Build per-draw uniform using tracked projection.
let mvp = self.current_projection;
- let draw_uniform = DrawUniforms { mvp };
+ let active = draw_active_slot_mask(draw);
+
+ let mut tex_dims = [UVec4::splat(1); 4];
+ if draw.num_indirect_stages > 0 {
+ for slot in 0..8 {
+ if (active >> slot) & 1 == 0 {
+ continue;
+ }
+ let Some(tid) = &self.current_texture_ids[slot] else {
+ continue;
+ };
+ let Some((_, tex, _)) = self.texture_cache.get(tid) else {
+ continue;
+ };
+
+ let size = tex.size();
+ let c = (slot % 2) * 2;
+ tex_dims[slot / 2][c] = size.width;
+ tex_dims[slot / 2][c + 1] = size.height;
+ }
+ }
+
+ let draw_uniform = DrawUniforms { mvp, tex_dims };
let start = self.scratch_draws.len() * draw_stride;
self.scratch_uniform_bytes.resize(start + draw_stride, 0);
@@ -356,7 +378,6 @@ impl GxRenderer {
self.last_frame_uniform_index.unwrap()
};
- let active = draw_active_slot_mask(draw);
self.draw_pipeline_keys.push(full_key);
self.draw_bg_keys
.push(trim_bg_key(self.current_bind_group_key(), active));
@@ -531,8 +552,9 @@ impl GxRenderer {
}
fn execute_action_render_pass(&mut self, device: &wgpu::Device) {
- let target_width = crate::EFB_WIDTH;
- let target_height = crate::EFB_HEIGHT;
+ let target_width = self.scaled(crate::EFB_WIDTH);
+ let target_height = self.scaled(crate::EFB_HEIGHT);
+ let scale = self.efb_scale as f32;
let fallback_sampler_key = (WrapMode::Clamp, WrapMode::Clamp, MagFilter::Linear, MinFilter::Linear);
let frame_stride = self.frame_stride;
@@ -757,25 +779,27 @@ impl GxRenderer {
}
let vp = &self.draw_viewports[index];
+ let vp_x = vp.x * scale;
+ let vp_y = vp.y * scale;
let max_dim = target_width.max(target_height) as f32;
- let vp_w = vp.w.clamp(1.0, max_dim);
- let vp_h = vp.h.clamp(1.0, max_dim);
+ let vp_w = (vp.w * scale).clamp(1.0, max_dim);
+ let vp_h = (vp.h * scale).clamp(1.0, max_dim);
- if vp.x.is_finite() && vp.y.is_finite() && vp_w.is_finite() && vp_h.is_finite() {
+ if vp_x.is_finite() && vp_y.is_finite() && vp_w.is_finite() && vp_h.is_finite() {
let mut min_d = vp.min_depth.clamp(0.0, 1.0);
let mut max_d = vp.max_depth.clamp(0.0, 1.0);
if min_d > max_d {
std::mem::swap(&mut min_d, &mut max_d);
}
- let vp_tuple = (vp.x, vp.y, vp_w, vp_h, min_d, max_d);
+ let vp_tuple = (vp_x, vp_y, vp_w, vp_h, min_d, max_d);
if last_viewport != Some(vp_tuple) {
- rpass.set_viewport(vp.x, vp.y, vp_w, vp_h, min_d, max_d);
+ rpass.set_viewport(vp_x, vp_y, vp_w, vp_h, min_d, max_d);
last_viewport = Some(vp_tuple);
}
} else {
tracing::warn!(
- x = vp.x,
- y = vp.y,
+ x = vp_x,
+ y = vp_y,
w = vp_w,
h = vp_h,
"non-finite viewport, skipping set_viewport"
@@ -783,10 +807,10 @@ impl GxRenderer {
}
let sc = &self.draw_scissors[index];
- let sc_x = sc.x.min(target_width);
- let sc_y = sc.y.min(target_height);
- let sc_w = sc.w.min(target_width - sc_x);
- let sc_h = sc.h.min(target_height - sc_y);
+ let sc_x = self.scaled(sc.x).min(target_width);
+ let sc_y = self.scaled(sc.y).min(target_height);
+ let sc_w = self.scaled(sc.w).min(target_width - sc_x);
+ let sc_h = self.scaled(sc.h).min(target_height - sc_y);
let sc_tuple = (sc_x, sc_y, sc_w, sc_h);
if last_scissor != Some(sc_tuple) {
rpass.set_scissor_rect(sc_x, sc_y, sc_w, sc_h);
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index a7d3f5c..e398e2f 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -125,6 +125,11 @@ const _: () = assert!(std::mem::size_of::<FrameUniforms>() == 1552);
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct DrawUniforms {
pub mvp: glam::Mat4,
+ /// Guest texture dimensions for texmaps 0-7, two slots per
+ /// vector (slot i at `tex_dims[i / 2]`, xy for even i, zw for odd).
+ /// Indirect texturing needs these instead of `textureDimensions`
+ /// because scaled EFB copies bind a larger GPU texture.
+ pub tex_dims: [glam::UVec4; 4],
}
pub(crate) const DRAW_UNIFORMS_SIZE: NonZeroU64 = match NonZeroU64::new(std::mem::size_of::<DrawUniforms>() as u64) {
@@ -135,17 +140,21 @@ pub(crate) const DRAW_UNIFORMS_SIZE: NonZeroU64 = match NonZeroU64::new(std::mem
pub const EFB_WIDTH: u32 = 640;
pub const EFB_HEIGHT: u32 = 528;
pub const EFB_SAMPLE_COUNT: u32 = 4;
+pub const MAX_EFB_SCALE: u32 = 4;
pub(crate) struct EfbCopyEntry {
pub(crate) format: gecko::flipper::gx::texture::CopyFormat,
+ /// Guest dimensions. The GPU texture itself is `efb_scale`
+ /// times larger.
+ pub(crate) native_w: u32,
+ pub(crate) native_h: u32,
pub(crate) texture: wgpu::Texture,
pub(crate) view: wgpu::TextureView,
}
impl EfbCopyEntry {
pub(crate) fn matches(&self, fmt: TextureFormat, w: u32, h: u32) -> bool {
- let size = self.texture.size();
- self.format.base_texture_format() == fmt && size.width == w && size.height == h
+ self.format.base_texture_format() == fmt && self.native_w == w && self.native_h == h
}
}
@@ -177,6 +186,7 @@ pub struct GxRenderer {
pub(crate) draw_uniform_stride: u64,
pub(crate) draw_uniform_capacity: usize,
pub(crate) scratch_indices: Vec<u32>,
+ pub(crate) efb_scale: u32,
// EFB: resolved (1x) color used for CopyXfb reads + texture binding.
pub(crate) efb_texture: wgpu::Texture,
pub(crate) efb_view: wgpu::TextureView,
@@ -200,6 +210,9 @@ pub struct GxRenderer {
/// Reusable Rgba8Unorm intermediate the Z24 pack writes into before
/// being copied to staging for the deferred RAM writeback.
pub(crate) efb_depth_writeback_target: Option<(wgpu::Texture, wgpu::TextureView)>,
+ /// Reusable native-resolution intermediate the scaled EFB is downsampled
+ /// into before color RAM readbacks when `efb_scale > 1`.
+ pub(crate) efb_color_readback_target: Option<(wgpu::Texture, wgpu::TextureView)>,
pub(crate) fallback_view: wgpu::TextureView,
pub(crate) scratch_vertices: Vec<GpuVertex>,
pub(crate) scratch_draws: Vec<DrawRecord>,
@@ -240,9 +253,10 @@ pub struct GxRenderer {
// Per-copy temporary textures stored by CopyXfb, composited by PresentXfb.
pub(crate) xfb_copies: FxHashMap<u32, (wgpu::Texture, wgpu::TextureView)>,
pub(crate) xfb_copy_pipeline: wgpu::RenderPipeline,
- pub(crate) xfb_copy_bind_group_layout: wgpu::BindGroupLayout,
- pub(crate) xfb_copy_sampler: wgpu::Sampler,
pub(crate) xfb_copy_uniform_buffer: wgpu::Buffer,
+ pub(crate) xfb_copy_bind_group: wgpu::BindGroup,
+ pub(crate) efb_color_readback_uniform_buffer: wgpu::Buffer,
+ pub(crate) efb_color_readback_bind_group: wgpu::BindGroup,
// Region-scoped EFB clear.
pub(crate) efb_clear: clear::EfbClear,
pub(crate) pending_command_buffers: Vec<wgpu::CommandBuffer>,
@@ -263,6 +277,7 @@ pub struct GxRenderer {
pub(crate) current_encoder: Option<wgpu::CommandEncoder>,
pub(crate) draw_bufs_write_pending: bool,
pub(crate) xfb_copy_uniform_write_pending: bool,
+ pub(crate) efb_color_readback_uniform_write_pending: bool,
pub(crate) efb_clear_uniform_write_pending: bool,
pub(crate) efb_depth_resolve_uniform_write_pending: bool,
pub(crate) efb_readback_staging_pool: FxHashMap<u64, Vec<wgpu::Buffer>>,
@@ -302,7 +317,22 @@ pub(crate) struct PendingWriteback {
}
impl GxRenderer {
- pub fn new(device: &wgpu::Device, queue: &wgpu::Queue, surface_format: wgpu::TextureFormat) -> Self {
+ pub fn new(
+ device: &wgpu::Device,
+ queue: &wgpu::Queue,
+ surface_format: wgpu::TextureFormat,
+ efb_scale: u32,
+ ) -> Self {
+ let max_scale = (device.limits().max_texture_dimension_2d / EFB_WIDTH).max(1);
+ let requested_scale = efb_scale;
+ let efb_scale = efb_scale.clamp(1, MAX_EFB_SCALE).min(max_scale);
+ if efb_scale != requested_scale {
+ tracing::warn!(requested = requested_scale, effective = efb_scale, "EFB scale clamped");
+ }
+
+ let efb_w = EFB_WIDTH * efb_scale;
+ let efb_h = EFB_HEIGHT * efb_scale;
+
let frame_uniform_size = FRAME_UNIFORMS_SIZE.get();
let draw_uniform_size = DRAW_UNIFORMS_SIZE.get();
let draw_uniform_stride = align_up(
@@ -337,7 +367,7 @@ impl GxRenderer {
},
wgpu::BindGroupLayoutEntry {
binding: 1,
- visibility: wgpu::ShaderStages::VERTEX,
+ visibility: wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
@@ -452,8 +482,8 @@ impl GxRenderer {
let efb_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("efb_color_resolved"),
size: wgpu::Extent3d {
- width: EFB_WIDTH,
- height: EFB_HEIGHT,
+ width: efb_w,
+ height: efb_h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
@@ -471,8 +501,8 @@ impl GxRenderer {
let efb_msaa_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("efb_color_msaa"),
size: wgpu::Extent3d {
- width: EFB_WIDTH,
- height: EFB_HEIGHT,
+ width: efb_w,
+ height: efb_h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
@@ -488,8 +518,8 @@ impl GxRenderer {
let efb_depth_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("efb_depth"),
size: wgpu::Extent3d {
- width: EFB_WIDTH,
- height: EFB_HEIGHT,
+ width: efb_w,
+ height: efb_h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
@@ -505,8 +535,8 @@ impl GxRenderer {
let xfb_texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("xfb_accum"),
size: wgpu::Extent3d {
- width: EFB_WIDTH,
- height: EFB_HEIGHT,
+ width: efb_w,
+ height: efb_h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
@@ -609,6 +639,37 @@ impl GxRenderer {
mapped_at_creation: false,
});
+ let efb_color_readback_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("efb_color_readback_uniforms"),
+ size: std::mem::size_of::<render::XfbCopyUniforms>() as u64,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ });
+
+ let make_efb_sample_bg = |label: &str, buffer: &wgpu::Buffer| {
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some(label),
+ layout: &xfb_copy_bind_group_layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: buffer.as_entire_binding(),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::TextureView(&efb_view),
+ },
+ wgpu::BindGroupEntry {
+ binding: 2,
+ resource: wgpu::BindingResource::Sampler(&xfb_copy_sampler),
+ },
+ ],
+ })
+ };
+ let xfb_copy_bind_group = make_efb_sample_bg("xfb_copy_bg", &xfb_copy_uniform_buffer);
+ let efb_color_readback_bind_group =
+ make_efb_sample_bg("efb_color_readback_bg", &efb_color_readback_uniform_buffer);
+
let efb_depth_resolve_bg_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("efb_depth_resolve_bgl"),
entries: &[
@@ -736,6 +797,7 @@ impl GxRenderer {
draw_uniform_stride,
draw_uniform_capacity: initial_draw_slot_count as usize,
scratch_indices: Vec::new(),
+ efb_scale,
efb_texture,
efb_view,
_efb_msaa_texture: efb_msaa_texture,
@@ -791,9 +853,10 @@ impl GxRenderer {
xfb_has_content: false,
xfb_copies: FxHashMap::default(),
xfb_copy_pipeline,
- xfb_copy_bind_group_layout,
- xfb_copy_sampler,
xfb_copy_uniform_buffer,
+ xfb_copy_bind_group,
+ efb_color_readback_uniform_buffer,
+ efb_color_readback_bind_group,
efb_clear,
pending_command_buffers: Vec::with_capacity(8),
texture_staging_buffer,
@@ -803,6 +866,7 @@ impl GxRenderer {
current_encoder: None,
draw_bufs_write_pending: false,
xfb_copy_uniform_write_pending: false,
+ efb_color_readback_uniform_write_pending: false,
efb_clear_uniform_write_pending: false,
efb_depth_resolve_uniform_write_pending: false,
@@ -811,6 +875,7 @@ impl GxRenderer {
efb_depth_writeback_pipeline,
efb_depth_writeback_target: None,
+ efb_color_readback_target: None,
}
}
@@ -834,6 +899,19 @@ impl GxRenderer {
&self.efb_view
}
+ pub(crate) fn scaled(&self, v: u32) -> u32 {
+ v * self.efb_scale
+ }
+
+ pub(crate) fn scaled_src_rect(&self, x: u32, y: u32, w: u32, h: u32) -> [f32; 4] {
+ [
+ self.scaled(x) as f32,
+ self.scaled(y) as f32,
+ self.scaled(w) as f32,
+ self.scaled(h) as f32,
+ ]
+ }
+
pub(crate) fn submit_pending(&mut self, queue: &wgpu::Queue) {
// Ship any staged texture-upload bytes through ONE `write_buffer_with`
// before finishing the encoder. wgpu orders queue writes ahead of
@@ -862,6 +940,7 @@ impl GxRenderer {
self.draw_bufs_write_pending = false;
self.xfb_copy_uniform_write_pending = false;
+ self.efb_color_readback_uniform_write_pending = false;
self.efb_clear_uniform_write_pending = false;
self.efb_depth_resolve_uniform_write_pending = false;
}
@@ -975,12 +1054,12 @@ impl GxRenderer {
&self.efb_msaa_view,
&self.efb_view,
&self.efb_depth_view,
- EFB_WIDTH,
- EFB_HEIGHT,
- x,
- y,
- w,
- h,
+ self.scaled(EFB_WIDTH),
+ self.scaled(EFB_HEIGHT),
+ self.scaled(x),
+ self.scaled(y),
+ self.scaled(w),
+ self.scaled(h),
color,
depth,
color_update,
diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rs
index 7640951..8e64416 100644
--- a/crates/backend-wgpu/src/render.rs
+++ b/crates/backend-wgpu/src/render.rs
@@ -165,6 +165,12 @@ impl GxRenderer {
return;
}
+ let scaled_src_x = self.scaled(src_x);
+ let scaled_src_y = self.scaled(src_y);
+ let scaled_w = self.scaled(width);
+ let scaled_h = self.scaled(height);
+ let scaled_dst_h = self.scaled(dst_h);
+
let needs_shader_copy = dst_h != height || (gamma - 1.0).abs() > f32::EPSILON;
if needs_shader_copy && self.xfb_copy_uniform_write_pending {
self.submit_pending(queue);
@@ -173,12 +179,12 @@ impl GxRenderer {
let mut encoder = self.take_or_create_encoder(device);
let entry = self.xfb_copies.entry(id).or_insert_with(|| {
- let texture_label = format!("xfb_copy_tmp id={id} size={width}x{dst_h}");
+ let texture_label = format!("xfb_copy_tmp id={id} size={scaled_w}x{scaled_dst_h}");
let tex = device.create_texture(&wgpu::TextureDescriptor {
label: Some(&texture_label),
size: wgpu::Extent3d {
- width,
- height: dst_h,
+ width: scaled_w,
+ height: scaled_dst_h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
@@ -196,13 +202,13 @@ impl GxRenderer {
// Recreate if size changed.
let existing_size = entry.0.size();
- if existing_size.width != width || existing_size.height != dst_h {
- let texture_label = format!("xfb_copy_tmp id={id} size={width}x{dst_h}");
+ if existing_size.width != scaled_w || existing_size.height != scaled_dst_h {
+ let texture_label = format!("xfb_copy_tmp id={id} size={scaled_w}x{scaled_dst_h}");
let tex = device.create_texture(&wgpu::TextureDescriptor {
label: Some(&texture_label),
size: wgpu::Extent3d {
- width,
- height: dst_h,
+ width: scaled_w,
+ height: scaled_dst_h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
@@ -226,32 +232,18 @@ impl GxRenderer {
if needs_shader_copy {
encoder.insert_debug_marker("CopyXfb path: shader copy for scale/gamma");
let uniforms = XfbCopyUniforms {
- src_rect: [src_x as f32, src_y as f32, width as f32, height as f32],
- dst_size: [width as f32, dst_h as f32],
+ src_rect: [
+ scaled_src_x as f32,
+ scaled_src_y as f32,
+ scaled_w as f32,
+ scaled_h as f32,
+ ],
+ dst_size: [scaled_w as f32, scaled_dst_h as f32],
gamma,
filter_mode: 0,
};
queue.write_buffer(&self.xfb_copy_uniform_buffer, 0, bytemuck::bytes_of(&uniforms));
- let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("xfb_copy_bg"),
- layout: &self.xfb_copy_bind_group_layout,
- entries: &[
- wgpu::BindGroupEntry {
- binding: 0,
- resource: self.xfb_copy_uniform_buffer.as_entire_binding(),
- },
- wgpu::BindGroupEntry {
- binding: 1,
- resource: wgpu::BindingResource::TextureView(&self.efb_view),
- },
- wgpu::BindGroupEntry {
- binding: 2,
- resource: wgpu::BindingResource::Sampler(&self.xfb_copy_sampler),
- },
- ],
- });
-
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("xfb_copy"),
@@ -270,7 +262,7 @@ impl GxRenderer {
multiview_mask: None,
});
rpass.set_pipeline(&self.xfb_copy_pipeline);
- rpass.set_bind_group(0, &bind_group, &[]);
+ rpass.set_bind_group(0, &self.xfb_copy_bind_group, &[]);
let marker = format!(
"XFB shader uniforms: src=({src_x},{src_y} {width}x{height}) dst={width}x{dst_h} gamma={gamma:.3}"
);
@@ -289,8 +281,8 @@ impl GxRenderer {
texture: &self.efb_texture,
mip_level: 0,
origin: wgpu::Origin3d {
- x: src_x,
- y: src_y,
+ x: scaled_src_x,
+ y: scaled_src_y,
z: 0,
},
aspect: wgpu::TextureAspect::default(),
@@ -302,8 +294,8 @@ impl GxRenderer {
aspect: wgpu::TextureAspect::default(),
},
wgpu::Extent3d {
- width,
- height,
+ width: scaled_w,
+ height: scaled_h,
depth_or_array_layers: 1,
},
);
@@ -367,6 +359,8 @@ impl GxRenderer {
let divisor = if half { 2 } else { 1 };
let dst_w = (width / divisor).max(1);
let dst_h = (height / divisor).max(1);
+ let scaled_dst_w = self.scaled(dst_w);
+ let scaled_dst_h = self.scaled(dst_h);
if let Some(entry) = self.efb_copy_cache.remove(&dest_addr) {
self.return_to_pool(entry.texture, entry.view);
@@ -376,15 +370,16 @@ impl GxRenderer {
let (tex, view) = self
.efb_copy_pool
- .get_mut(&(dst_w, dst_h))
+ .get_mut(&(scaled_dst_w, scaled_dst_h))
.and_then(Vec::pop)
.unwrap_or_else(|| {
- let label = format!("efb_copy addr={dest_addr:#010x} size={dst_w}x{dst_h} fmt={copy_format:?}");
+ let label =
+ format!("efb_copy addr={dest_addr:#010x} size={scaled_dst_w}x{scaled_dst_h} fmt={copy_format:?}");
let tex = device.create_texture(&wgpu::TextureDescriptor {
label: Some(&label),
size: wgpu::Extent3d {
- width: dst_w,
- height: dst_h,
+ width: scaled_dst_w,
+ height: scaled_dst_h,
depth_or_array_layers: 1,
},
mip_level_count: 1,
@@ -406,32 +401,13 @@ impl GxRenderer {
}
let uniforms = XfbCopyUniforms {
- src_rect: [src_x as f32, src_y as f32, width as f32, height as f32],
- dst_size: [dst_w as f32, dst_h as f32],
+ src_rect: self.scaled_src_rect(src_x, src_y, width, height),
+ dst_size: [scaled_dst_w as f32, scaled_dst_h as f32],
gamma: 1.0,
filter_mode: 0,
};
queue.write_buffer(&self.xfb_copy_uniform_buffer, 0, bytemuck::bytes_of(&uniforms));
- let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("efb_pack_bg"),
- layout: &self.xfb_copy_bind_group_layout,
- entries: &[
- wgpu::BindGroupEntry {
- binding: 0,
- resource: self.xfb_copy_uniform_buffer.as_entire_binding(),
- },
- wgpu::BindGroupEntry {
- binding: 1,
- resource: wgpu::BindingResource::TextureView(&self.efb_view),
- },
- wgpu::BindGroupEntry {
- binding: 2,
- resource: wgpu::BindingResource::Sampler(&self.xfb_copy_sampler),
- },
- ],
- });
-
let group_label = format!(
"CopyEfbToTexture addr={dest_addr:#010x} src=({src_x},{src_y} {width}x{height}) dst={dst_w}x{dst_h} fmt={copy_format:?}"
);
@@ -455,7 +431,7 @@ impl GxRenderer {
multiview_mask: None,
});
rpass.set_pipeline(self.efb_pack_pipelines.for_color(copy_format, is_intensity).unwrap());
- rpass.set_bind_group(0, &bind_group, &[]);
+ rpass.set_bind_group(0, &self.xfb_copy_bind_group, &[]);
rpass.insert_debug_marker("EFB copy: per-format pack into cache");
rpass.draw(0..3, 0..1);
}
@@ -467,6 +443,8 @@ impl GxRenderer {
dest_addr,
crate::EfbCopyEntry {
format: copy_format,
+ native_w: dst_w,
+ native_h: dst_h,
texture: tex,
view,
},
@@ -508,8 +486,8 @@ impl GxRenderer {
height: u32,
parts: &[XfbPart],
) {
- let width = width.max(1);
- let height = height.max(1);
+ let width = self.scaled(width.max(1));
+ let height = self.scaled(height.max(1));
// Resize the XFB output texture if the frame dimensions changed.
let cur = self.xfb_texture.size();
@@ -553,18 +531,22 @@ impl GxRenderer {
encoder.insert_debug_marker(&marker);
continue;
};
+
let src_size = tex.size();
- let width = src_size.width.min(xfb_size.width.saturating_sub(part.offset_x));
- let height = src_size.height.min(xfb_size.height.saturating_sub(part.offset_y));
+ let offset_x = self.scaled(part.offset_x);
+ let offset_y = self.scaled(part.offset_y);
+ let width = src_size.width.min(xfb_size.width.saturating_sub(offset_x));
+ let height = src_size.height.min(xfb_size.height.saturating_sub(offset_y));
if width == 0 || height == 0 {
tracing::warn!(id = part.id, "present_xfb: zero-area XFB part after clamping, skipping");
let marker = format!("PresentXfb skip: zero-area part id={}", part.id);
encoder.insert_debug_marker(&marker);
continue;
}
+
let marker = format!(
"XFB part id={} dst=({},{} {}x{})",
- part.id, part.offset_x, part.offset_y, width, height
+ part.id, offset_x, offset_y, width, height
);
encoder.insert_debug_marker(&marker);
encoder.copy_texture_to_texture(
@@ -578,8 +560,8 @@ impl GxRenderer {
texture: &self.xfb_texture,
mip_level: 0,
origin: wgpu::Origin3d {
- x: part.offset_x,
- y: part.offset_y,
+ x: offset_x,
+ y: offset_y,
z: 0,
},
aspect: wgpu::TextureAspect::default(),
@@ -680,17 +662,81 @@ impl GxRenderer {
let group_label = format!(
"CopyEfbToTexture addr={dest_addr:#010x} src=({src_x},{src_y} {width}x{height}) fmt={copy_format_enum:?} mip={mipmap} stride={stride} depth={depth_copy}"
);
+
+ if self.efb_scale > 1 {
+ if self.efb_color_readback_uniform_write_pending {
+ self.submit_pending(queue);
+ }
+
+ Self::ensure_writeback_target(
+ device,
+ &mut self.efb_color_readback_target,
+ width,
+ height,
+ self.surface_format,
+ "efb_color_readback_target",
+ );
+
+ let uniforms = XfbCopyUniforms {
+ src_rect: self.scaled_src_rect(src_x, src_y, width, height),
+ dst_size: [width as f32, height as f32],
+ gamma: 1.0,
+ filter_mode: 0,
+ };
+ queue.write_buffer(
+ &self.efb_color_readback_uniform_buffer,
+ 0,
+ bytemuck::bytes_of(&uniforms),
+ );
+ }
+
let mut encoder = self.take_or_create_encoder(device);
encoder.push_debug_group(&group_label);
- encoder.copy_texture_to_buffer(
- wgpu::TexelCopyTextureInfo {
- texture: &self.efb_texture,
- mip_level: 0,
- origin: wgpu::Origin3d {
+
+ let (src_texture, src_origin) = if self.efb_scale == 1 {
+ (
+ &self.efb_texture,
+ wgpu::Origin3d {
x: src_x,
y: src_y,
z: 0,
},
+ )
+ } else {
+ let (readback_tex, readback_view) = self.efb_color_readback_target.as_ref().unwrap();
+
+ {
+ let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
+ label: Some("efb_color_readback_downsample"),
+ color_attachments: &[Some(wgpu::RenderPassColorAttachment {
+ view: readback_view,
+ resolve_target: None,
+ ops: wgpu::Operations {
+ load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
+ store: wgpu::StoreOp::Store,
+ },
+ depth_slice: None,
+ })],
+ depth_stencil_attachment: None,
+ occlusion_query_set: None,
+ timestamp_writes: None,
+ multiview_mask: None,
+ });
+ rpass.set_viewport(0.0, 0.0, width as f32, height as f32, 0.0, 1.0);
+ rpass.set_scissor_rect(0, 0, width, height);
+ rpass.set_pipeline(&self.xfb_copy_pipeline);
+ rpass.set_bind_group(0, &self.efb_color_readback_bind_group, &[]);
+ rpass.draw(0..3, 0..1);
+ }
+
+ (readback_tex, wgpu::Origin3d::ZERO)
+ };
+
+ encoder.copy_texture_to_buffer(
+ wgpu::TexelCopyTextureInfo {
+ texture: src_texture,
+ mip_level: 0,
+ origin: src_origin,
aspect: wgpu::TextureAspect::default(),
},
wgpu::TexelCopyBufferInfo {
@@ -710,6 +756,10 @@ impl GxRenderer {
encoder.pop_debug_group();
self.current_encoder = Some(encoder);
+ if self.efb_scale > 1 {
+ self.efb_color_readback_uniform_write_pending = true;
+ }
+
let swap_bgra = matches!(
self.surface_format,
wgpu::TextureFormat::Bgra8Unorm | wgpu::TextureFormat::Bgra8UnormSrgb
@@ -746,38 +796,21 @@ impl GxRenderer {
let encode_w = (width / divisor).max(1);
let encode_h = (height / divisor).max(1);
- let (target_w, target_h) = self
- .efb_depth_writeback_target
- .as_ref()
- .map(|(t, _)| (t.size().width, t.size().height))
- .unwrap_or((0, 0));
- if self.efb_depth_writeback_target.is_none() || encode_w > target_w || encode_h > target_h {
- let new_w = target_w.max(encode_w).max(64);
- let new_h = target_h.max(encode_h).max(64);
- let tex = device.create_texture(&wgpu::TextureDescriptor {
- label: Some("efb_depth_writeback_target"),
- size: wgpu::Extent3d {
- width: new_w,
- height: new_h,
- depth_or_array_layers: 1,
- },
- mip_level_count: 1,
- sample_count: 1,
- dimension: wgpu::TextureDimension::D2,
- format: wgpu::TextureFormat::Rgba8Unorm,
- usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
- view_formats: &[],
- });
- let view = tex.create_view(&Default::default());
- self.efb_depth_writeback_target = Some((tex, view));
- }
+ Self::ensure_writeback_target(
+ device,
+ &mut self.efb_depth_writeback_target,
+ encode_w,
+ encode_h,
+ wgpu::TextureFormat::Rgba8Unorm,
+ "efb_depth_writeback_target",
+ );
if self.efb_depth_resolve_uniform_write_pending {
self.submit_pending(queue);
}
let uniforms = XfbCopyUniforms {
- src_rect: [src_x as f32, src_y as f32, width as f32, height as f32],
+ src_rect: self.scaled_src_rect(src_x, src_y, width, height),
dst_size: [encode_w as f32, encode_h as f32],
gamma: 1.0,
filter_mode: 0,
@@ -872,6 +905,40 @@ impl GxRenderer {
});
}
+ pub(crate) fn ensure_writeback_target(
+ device: &wgpu::Device,
+ slot: &mut Option<(wgpu::Texture, wgpu::TextureView)>,
+ width: u32,
+ height: u32,
+ format: wgpu::TextureFormat,
+ label: &str,
+ ) {
+ let (target_w, target_h) = slot
+ .as_ref()
+ .map(|(t, _)| (t.size().width, t.size().height))
+ .unwrap_or((0, 0));
+ if slot.is_some() && width <= target_w && height <= target_h {
+ return;
+ }
+
+ let tex = device.create_texture(&wgpu::TextureDescriptor {
+ label: Some(label),
+ size: wgpu::Extent3d {
+ width: target_w.max(width).max(64),
+ height: target_h.max(height).max(64),
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format,
+ usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC,
+ view_formats: &[],
+ });
+ let view = tex.create_view(&Default::default());
+ *slot = Some((tex, view));
+ }
+
pub(crate) fn acquire_readback_staging(&mut self, device: &wgpu::Device, staging_size: u64) -> (wgpu::Buffer, u64) {
let capacity = staging_size.next_power_of_two().max(4096);
if let Some(bucket) = self.efb_readback_staging_pool.get_mut(&capacity) {
diff --git a/crates/backend-wgpu/src/shaders/common.wesl b/crates/backend-wgpu/src/shaders/common.wesl
index fa2d127..ddcaf62 100644
--- a/crates/backend-wgpu/src/shaders/common.wesl
+++ b/crates/backend-wgpu/src/shaders/common.wesl
@@ -45,6 +45,11 @@ struct FrameUniforms {
struct DrawUniforms {
mvp: mat4x4<f32>,
+ // Guest-native texmap dimensions, two slots per vector (slot i at
+ // tex_dims[i / 2], xy for even i, zw for odd). Scaled EFB copies bind
+ // a larger GPU texture, so textureDimensions must not be used for
+ // guest texel math.
+ tex_dims: array<vec4<u32>, 4>,
};
@group(0) @binding(0) var<uniform> frame: FrameUniforms;
diff --git a/crates/backend-wgpu/src/shaders/tev_indirect.wesl b/crates/backend-wgpu/src/shaders/tev_indirect.wesl
index fde827e..d527f1d 100644
--- a/crates/backend-wgpu/src/shaders/tev_indirect.wesl
+++ b/crates/backend-wgpu/src/shaders/tev_indirect.wesl
@@ -1,17 +1,10 @@
-import super::common::{VsOut, FrameUniforms, frame, tex0, tex1, tex2, tex3, tex4, tex5, tex6, tex7, samp0, samp1, samp2, samp3, samp4, samp5, samp6, samp7};
+import super::common::{VsOut, FrameUniforms, frame, draw};
import super::tev_helpers::{select_texcoord, stq_to_uv, sample_texmap, read_packed};
fn texmap_size(texmap: u32) -> vec2<f32> {
- switch texmap {
- case 0u: { return vec2<f32>(textureDimensions(tex0)); }
- case 1u: { return vec2<f32>(textureDimensions(tex1)); }
- case 2u: { return vec2<f32>(textureDimensions(tex2)); }
- case 3u: { return vec2<f32>(textureDimensions(tex3)); }
- case 4u: { return vec2<f32>(textureDimensions(tex4)); }
- case 5u: { return vec2<f32>(textureDimensions(tex5)); }
- case 6u: { return vec2<f32>(textureDimensions(tex6)); }
- default: { return vec2<f32>(textureDimensions(tex7)); }
- }
+ let pair = draw.tex_dims[texmap / 2u];
+ let dims = select(pair.xy, pair.zw, (texmap & 1u) == 1u);
+ return vec2<f32>(max(dims, vec2<u32>(1u, 1u)));
}
fn ind_quantize_and_bias(
diff --git a/crates/backend-wgpu/src/sink.rs b/crates/backend-wgpu/src/sink.rs
index 837f316..6f63f0d 100644
--- a/crates/backend-wgpu/src/sink.rs
+++ b/crates/backend-wgpu/src/sink.rs
@@ -251,7 +251,7 @@ impl InlineSink {
queue: wgpu::Queue,
surface_format: wgpu::TextureFormat,
) -> (Arc<Mutex<GxRenderer>>, Self) {
- let gx = Arc::new(Mutex::new(GxRenderer::new(&device, &queue, surface_format)));
+ let gx = Arc::new(Mutex::new(GxRenderer::new(&device, &queue, surface_format, 1)));
let sink = InlineSink {
gx: gx.clone(),
device,
@@ -328,8 +328,9 @@ impl Renderer {
queue: wgpu::Queue,
surface_format: wgpu::TextureFormat,
target_aspect: TargetAspect,
+ efb_scale: u32,
) -> (Self, ThreadedSink) {
- let mut gx = GxRenderer::new(&device, &queue, surface_format);
+ let mut gx = GxRenderer::new(&device, &queue, surface_format, efb_scale);
gx.prewarm_pipeline_cache(&device);
// Initial shared output: the XFB texture (black until first PresentXfb).
@@ -488,8 +489,8 @@ impl Renderer {
self.target_aspect
}
- /// Read back the most recently presented XFB frame at native emulated
- /// resolution. Blocks until the GPU copy completes.
+ /// Read back the most recently presented XFB frame at the emulated
+ /// resolution times the EFB scale. Blocks until the GPU copy completes.
pub fn capture_xfb(&self) -> Option<crate::capture::CapturedFrame> {
let texture = self.shared.output.lock().unwrap().clone();
crate::capture::capture_texture(&self.device, &self.queue, &texture)
diff --git a/crates/debugger/src/main.rs b/crates/debugger/src/main.rs
index 3b0a17d..cfe9354 100644
--- a/crates/debugger/src/main.rs
+++ b/crates/debugger/src/main.rs
@@ -513,7 +513,7 @@ fn main() {
let target_aspect = TargetAspect::from_arg(&args.aspect, matches!(emulator, EmulatorVariant::Wii(_)));
let (renderer, sink) =
- backend_wgpu::sink::Renderer::new(device.clone(), queue.clone(), surface_format, target_aspect);
+ backend_wgpu::sink::Renderer::new(device.clone(), queue.clone(), surface_format, target_aspect, 1);
emulator.install_render_sink(Box::new(sink));
diff --git a/crates/fifoplayer/src/main.rs b/crates/fifoplayer/src/main.rs
index 6c52d47..b97a7d9 100644
--- a/crates/fifoplayer/src/main.rs
+++ b/crates/fifoplayer/src/main.rs
@@ -310,7 +310,7 @@ fn run_windowed<const SYSTEM: gecko::SystemId>(file: dff::DffFile, start: usize,
let surface_format = wgpu::TextureFormat::Bgra8Unorm;
let target_aspect = TargetAspect::from_arg(aspect, file.is_wii);
let (renderer, sink) =
- backend_wgpu::sink::Renderer::new(device.clone(), queue.clone(), surface_format, target_aspect);
+ backend_wgpu::sink::Renderer::new(device.clone(), queue.clone(), surface_format, target_aspect, 1);
let mut player_sink = PlayerSink::new(Box::new(sink));
let mut playback = Playback::<SYSTEM>::new();
diff --git a/crates/mcp/src/main.rs b/crates/mcp/src/main.rs
index 81de508..223e79d 100644
--- a/crates/mcp/src/main.rs
+++ b/crates/mcp/src/main.rs
@@ -89,7 +89,7 @@ fn main() -> Result<()> {
.context("acquire wgpu device")?;
let surface_format = wgpu::TextureFormat::Rgba8Unorm;
- let gx = Arc::new(Mutex::new(GxRenderer::new(&device, &queue, surface_format)));
+ let gx = Arc::new(Mutex::new(GxRenderer::new(&device, &queue, surface_format, 1)));
let introspect = Arc::new(Mutex::new(Introspection::default()));
let shared = Arc::new(Shared {
diff --git a/crates/tinyapp/src/main.rs b/crates/tinyapp/src/main.rs
index d0ad101..a6fd996 100644
--- a/crates/tinyapp/src/main.rs
+++ b/crates/tinyapp/src/main.rs
@@ -109,6 +109,10 @@ struct Args {
#[arg(long, default_value = "auto")]
aspect: String,
+ /// Integer internal resolution multiplier: 1 (native) to 4
+ #[arg(long, default_value_t = 1)]
+ upscale: u32,
+
/// Disable audio output
#[arg(long)]
no_audio: bool,
@@ -338,8 +342,13 @@ fn run<const SYSTEM: SystemId>(
let surface_format = wgpu::TextureFormat::Bgra8Unorm;
- let (renderer, sink) =
- backend_wgpu::sink::Renderer::new(device.clone(), queue.clone(), surface_format, target_aspect);
+ let (renderer, sink) = backend_wgpu::sink::Renderer::new(
+ device.clone(),
+ queue.clone(),
+ surface_format,
+ target_aspect,
+ args.upscale,
+ );
emulator.render_sink = Box::new(sink);
diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rs
index 70106b5..e8010ab 100644
--- a/crates/web/src/lib.rs
+++ b/crates/web/src/lib.rs
@@ -188,7 +188,7 @@ impl State {
};
surface.configure(&device, &surface_config);
- let gx_renderer = backend_wgpu::GxRenderer::new(&device, &queue, surface_format);
+ let gx_renderer = backend_wgpu::GxRenderer::new(&device, &queue, surface_format, 1);
// Blit pipeline (same as sink::Renderer)
let blit_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
diff --git a/other/example.conf b/other/example.conf
index e9da808..2795513 100644
--- a/other/example.conf
+++ b/other/example.conf
@@ -8,6 +8,7 @@ ipl = "/path/to/IPL.bin"
cpu_mode = "jit" # jit | interpreter
theme = "system" # system | light | dark
skip_ipl = true
+upscale = 1 # internal resolution multiplier, 1-4
[input]
gamepads = true