Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

depth copy cached gpu texture, half scale cached, rg8 channel order

Layle878a8f471a5a
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index 8df9434..ec1a761 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -6,7 +6,6 @@ use crate::{
};
use gecko::flipper::gx::draw::Primitive;
use gecko::flipper::gx::regs::{CullMode, MagFilter, MinFilter, WrapMode};
-use gecko::flipper::gx::texture::CopyFormat;
use gecko::host::{DrawData, GxAction};
use glam::{Mat4, UVec4, Vec4};
#[cfg(feature = "gx-stats")]
@@ -577,26 +576,9 @@ impl GxRenderer {
*mipmap,
*stride,
*depth_copy,
+ *is_intensity,
);
- if !*depth_copy
- && !*mipmap
- && let Some(copy_fmt) = CopyFormat::from_u8_color(*raw_copy_format)
- {
- self.cache_efb_copy_color(
- device,
- queue,
- *dest_addr,
- *src_x,
- *src_y,
- *src_w,
- *src_h,
- *mipmap,
- copy_fmt,
- *is_intensity,
- );
- }
-
if *clear {
self.clear_efb_region(
device,
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index d4a3f8f..4a5e0de 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -295,10 +295,11 @@ pub struct GxRenderer {
pub(crate) efb_copy_cache: FxHashMap<Address, EfbCopyEntry>,
pub(crate) efb_copy_pool: FxHashMap<(u32, u32), Vec<(wgpu::Texture, wgpu::TextureView)>>,
pub(crate) efb_pack_pipelines: render::EfbPackPipelines,
- pub(crate) efb_depth_resolve_bg_layout: wgpu::BindGroupLayout,
+ pub(crate) efb_depth_pack_pipelines: render::EfbDepthPackPipelines,
pub(crate) efb_depth_resolve_uniform_buffer: wgpu::Buffer,
-
- pub(crate) efb_depth_writeback_pipeline: wgpu::RenderPipeline,
+ pub(crate) efb_depth_resolve_bind_group: wgpu::BindGroup,
+ pub(crate) efb_depth_cache_uniform_buffer: wgpu::Buffer,
+ pub(crate) efb_depth_cache_bind_group: wgpu::BindGroup,
/// 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)>,
@@ -372,6 +373,7 @@ pub struct GxRenderer {
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_depth_cache_uniform_write_pending: bool,
pub(crate) efb_readback_staging_pool: FxHashMap<u64, Vec<wgpu::Buffer>>,
pub(crate) pending_writebacks: Vec<PendingWriteback>,
}
@@ -436,13 +438,18 @@ impl GxRenderer {
label: Some("xfb_copy_shader"),
source: wgpu::ShaderSource::Wgsl(include_str!("shaders/xfb_copy.wgsl").into()),
});
+ let pack_formats_wgsl = include_str!("shaders/efb_pack_formats.wgsl");
let efb_depth_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("efb_depth_shader"),
- source: wgpu::ShaderSource::Wgsl(include_str!("shaders/efb_depth.wgsl").into()),
+ source: wgpu::ShaderSource::Wgsl(
+ format!("{}\n{}", include_str!("shaders/efb_depth.wgsl"), pack_formats_wgsl).into(),
+ ),
});
let efb_pack_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("efb_pack_shader"),
- source: wgpu::ShaderSource::Wgsl(include_str!("shaders/efb_pack.wgsl").into()),
+ source: wgpu::ShaderSource::Wgsl(
+ format!("{}\n{}", include_str!("shaders/efb_pack.wgsl"), pack_formats_wgsl).into(),
+ ),
});
// Bindings: 0=FrameUniforms, 1=DrawUniforms, 2-9=textures 0-7, 10-17=samplers 0-7
@@ -724,19 +731,16 @@ impl GxRenderer {
..Default::default()
});
- let xfb_copy_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
- label: Some("xfb_copy_uniforms"),
- size: std::mem::size_of::<render::XfbCopyUniforms>() as u64,
- usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
- 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_copy_uniform_buffer = |label: &str| {
+ device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some(label),
+ size: std::mem::size_of::<render::XfbCopyUniforms>() as u64,
+ usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
+ mapped_at_creation: false,
+ })
+ };
+ let xfb_copy_uniform_buffer = make_copy_uniform_buffer("xfb_copy_uniforms");
+ let efb_color_readback_uniform_buffer = make_copy_uniform_buffer("efb_color_readback_uniforms");
let make_efb_sample_bg = |label: &str, buffer: &wgpu::Buffer| {
device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -792,12 +796,28 @@ impl GxRenderer {
bind_group_layouts: &[Some(&efb_depth_resolve_bg_layout)],
immediate_size: 0,
});
- let efb_depth_resolve_uniform_buffer = device.create_buffer(&wgpu::BufferDescriptor {
- label: Some("efb_depth_resolve_uniforms"),
- size: std::mem::size_of::<render::XfbCopyUniforms>() as u64,
- usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
- mapped_at_creation: false,
- });
+ let efb_depth_resolve_uniform_buffer = make_copy_uniform_buffer("efb_depth_resolve_uniforms");
+ let efb_depth_cache_uniform_buffer = make_copy_uniform_buffer("efb_depth_cache_uniforms");
+
+ let make_depth_pack_bg = |label: &str, buffer: &wgpu::Buffer| {
+ device.create_bind_group(&wgpu::BindGroupDescriptor {
+ label: Some(label),
+ layout: &efb_depth_resolve_bg_layout,
+ entries: &[
+ wgpu::BindGroupEntry {
+ binding: 0,
+ resource: buffer.as_entire_binding(),
+ },
+ wgpu::BindGroupEntry {
+ binding: 1,
+ resource: wgpu::BindingResource::TextureView(&efb_depth_view),
+ },
+ ],
+ })
+ };
+ let efb_depth_resolve_bind_group =
+ make_depth_pack_bg("efb_depth_resolve_bg", &efb_depth_resolve_uniform_buffer);
+ let efb_depth_cache_bind_group = make_depth_pack_bg("efb_depth_cache_bg", &efb_depth_cache_uniform_buffer);
let make_pack_pipeline =
|label: &str, entry: &str, layout: &wgpu::PipelineLayout, shader: &wgpu::ShaderModule| {
@@ -851,12 +871,21 @@ impl GxRenderer {
r8: color_pack("efb_pack_r8", "fs_r8"),
rg8: color_pack("efb_pack_rg8", "fs_rg8"),
};
- let efb_depth_writeback_pipeline = make_pack_pipeline(
- "efb_depth_writeback_pipeline",
- "fs_writeback_z24",
- &efb_depth_resolve_pipeline_layout,
- &efb_depth_shader,
- );
+ let depth_pack = |label: &str, entry: &str| {
+ make_pack_pipeline(label, entry, &efb_depth_resolve_pipeline_layout, &efb_depth_shader)
+ };
+ let efb_depth_pack_pipelines = render::EfbDepthPackPipelines {
+ z24x8: depth_pack("efb_depth_pack_z24x8", "fs_rgba8"),
+ i8: depth_pack("efb_depth_pack_i8", "fs_i8"),
+ i4: depth_pack("efb_depth_pack_i4", "fs_i4"),
+ ia8: depth_pack("efb_depth_pack_ia8", "fs_ia8"),
+ ia4: depth_pack("efb_depth_pack_ia4", "fs_ia4"),
+ rgb565: depth_pack("efb_depth_pack_rgb565", "fs_rgb565"),
+ rgb5a3: depth_pack("efb_depth_pack_rgb5a3", "fs_rgb5a3"),
+ a8: depth_pack("efb_depth_pack_a8", "fs_a8"),
+ r8: depth_pack("efb_depth_pack_r8", "fs_r8"),
+ rg8: depth_pack("efb_depth_pack_rg8", "fs_rg8"),
+ };
let efb_clear = clear::EfbClear::new(
device,
surface_format,
@@ -923,8 +952,11 @@ impl GxRenderer {
efb_copy_cache: FxHashMap::default(),
efb_copy_pool: FxHashMap::default(),
efb_pack_pipelines,
- efb_depth_resolve_bg_layout,
+ efb_depth_pack_pipelines,
efb_depth_resolve_uniform_buffer,
+ efb_depth_resolve_bind_group,
+ efb_depth_cache_uniform_buffer,
+ efb_depth_cache_bind_group,
fallback_view,
scratch_vertices: Vec::new(),
scratch_draws: Vec::new(),
@@ -975,11 +1007,11 @@ impl GxRenderer {
efb_color_readback_uniform_write_pending: false,
efb_clear_uniform_write_pending: false,
efb_depth_resolve_uniform_write_pending: false,
+ efb_depth_cache_uniform_write_pending: false,
efb_readback_staging_pool: FxHashMap::default(),
pending_writebacks: Vec::new(),
- efb_depth_writeback_pipeline,
efb_depth_writeback_target: None,
efb_color_readback_target: None,
}
@@ -1086,6 +1118,7 @@ impl GxRenderer {
self.efb_color_readback_uniform_write_pending = false;
self.efb_clear_uniform_write_pending = false;
self.efb_depth_resolve_uniform_write_pending = false;
+ self.efb_depth_cache_uniform_write_pending = false;
}
/// Append `rgba` (W*H*4 tight, no row padding) into
diff --git a/crates/backend-wgpu/src/render.rs b/crates/backend-wgpu/src/render.rs
index 6a08619..45832a7 100644
--- a/crates/backend-wgpu/src/render.rs
+++ b/crates/backend-wgpu/src/render.rs
@@ -49,6 +49,42 @@ impl EfbPackPipelines {
}
}
+pub(crate) struct EfbDepthPackPipelines {
+ pub(crate) z24x8: wgpu::RenderPipeline,
+ pub(crate) i8: wgpu::RenderPipeline,
+ pub(crate) i4: wgpu::RenderPipeline,
+ pub(crate) ia8: wgpu::RenderPipeline,
+ pub(crate) ia4: wgpu::RenderPipeline,
+ pub(crate) rgb565: wgpu::RenderPipeline,
+ pub(crate) rgb5a3: wgpu::RenderPipeline,
+ pub(crate) a8: wgpu::RenderPipeline,
+ pub(crate) r8: wgpu::RenderPipeline,
+ pub(crate) rg8: wgpu::RenderPipeline,
+}
+
+#[derive(Clone, Copy)]
+enum EfbCopySource {
+ Color { intensity: bool },
+ Depth,
+}
+
+impl EfbDepthPackPipelines {
+ pub(crate) fn for_format(&self, fmt: CopyFormat) -> &wgpu::RenderPipeline {
+ match fmt {
+ CopyFormat::RGBA8 | CopyFormat::Z24X8 => &self.z24x8,
+ CopyFormat::I8 => &self.i8,
+ CopyFormat::I4 => &self.i4,
+ CopyFormat::IA8 => &self.ia8,
+ CopyFormat::IA4 => &self.ia4,
+ CopyFormat::RGB565 => &self.rgb565,
+ CopyFormat::RGB5A3 => &self.rgb5a3,
+ CopyFormat::A8 => &self.a8,
+ CopyFormat::R8 => &self.r8,
+ CopyFormat::RG8 => &self.rg8,
+ }
+ }
+}
+
impl GxRenderer {
pub(crate) fn upload_buffers(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, frame_uniform_bytes: &[u8]) {
let num_draws = self.scratch_draws.len();
@@ -326,77 +362,38 @@ impl GxRenderer {
}
#[allow(clippy::too_many_arguments)]
- pub(crate) fn cache_efb_copy_color(
+ fn cache_efb_copy(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
dest_addr: Address,
src_x: u32,
src_y: u32,
- src_w: u32,
- src_h: u32,
+ width: u32,
+ height: u32,
half: bool,
copy_format: CopyFormat,
- is_intensity: bool,
+ source: EfbCopySource,
) {
- debug_assert!(
- self.efb_pack_pipelines.for_color(copy_format, is_intensity).is_some(),
- "cache_efb_copy_color called with depth-only copy format {copy_format:?}",
- );
-
- let width = src_w.min(crate::EFB_WIDTH.saturating_sub(src_x));
- let height = src_h.min(crate::EFB_HEIGHT.saturating_sub(src_y));
- if width == 0 || height == 0 {
- tracing::warn!(
- src_x,
- src_y,
- src_w,
- src_h,
- "efb_copy_cache: zero-area region after clamping, skipping"
+ if let EfbCopySource::Color { intensity } = source {
+ debug_assert!(
+ self.efb_pack_pipelines.for_color(copy_format, intensity).is_some(),
+ "cache_efb_copy called with depth-only copy format {copy_format:?}",
);
- return;
}
+
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);
+ let (tex, view) = self.acquire_efb_copy_target(device, dest_addr, scaled_dst_w, scaled_dst_h, copy_format);
- if let Some(entry) = self.efb_copy_cache.remove(&dest_addr) {
- self.return_to_pool(entry.texture, entry.view);
- }
- self.bind_group_cache
- .retain(|key, _| !key.tex_keys.iter().any(|k| k.map(|t| t.ram_addr) == Some(dest_addr)));
-
- let (tex, view) = self
- .efb_copy_pool
- .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={scaled_dst_w}x{scaled_dst_h} fmt={copy_format:?}");
- let tex = device.create_texture(&wgpu::TextureDescriptor {
- label: Some(&label),
- size: wgpu::Extent3d {
- width: scaled_dst_w,
- height: scaled_dst_h,
- depth_or_array_layers: 1,
- },
- mip_level_count: 1,
- sample_count: 1,
- dimension: wgpu::TextureDimension::D2,
- format: wgpu::TextureFormat::Rgba8Unorm,
- usage: wgpu::TextureUsages::TEXTURE_BINDING
- | wgpu::TextureUsages::RENDER_ATTACHMENT
- | wgpu::TextureUsages::COPY_DST
- | wgpu::TextureUsages::COPY_SRC,
- view_formats: &[],
- });
- let view = tex.create_view(&Default::default());
- (tex, view)
- });
-
- if self.xfb_copy_uniform_write_pending {
+ let write_pending = match source {
+ EfbCopySource::Color { .. } => self.xfb_copy_uniform_write_pending,
+ EfbCopySource::Depth => self.efb_depth_cache_uniform_write_pending,
+ };
+ if write_pending {
self.submit_pending(queue);
}
@@ -404,12 +401,23 @@ impl GxRenderer {
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,
+ filter_mode: match source {
+ EfbCopySource::Color { .. } => u32::from(half),
+ EfbCopySource::Depth => 0,
+ },
+ };
+ let uniform_buffer = match source {
+ EfbCopySource::Color { .. } => &self.xfb_copy_uniform_buffer,
+ EfbCopySource::Depth => &self.efb_depth_cache_uniform_buffer,
};
- queue.write_buffer(&self.xfb_copy_uniform_buffer, 0, bytemuck::bytes_of(&uniforms));
+ queue.write_buffer(uniform_buffer, 0, bytemuck::bytes_of(&uniforms));
+ let kind = match source {
+ EfbCopySource::Color { .. } => "CopyEfbToTexture",
+ EfbCopySource::Depth => "CopyEfbDepthToTexture",
+ };
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:?}"
+ "{kind} addr={dest_addr:#010x} src=({src_x},{src_y} {width}x{height}) dst={dst_w}x{dst_h} fmt={copy_format:?}"
);
let mut encoder = self.take_or_create_encoder(device);
encoder.push_debug_group(&group_label);
@@ -430,14 +438,27 @@ impl GxRenderer {
timestamp_writes: None,
multiview_mask: None,
});
- rpass.set_pipeline(self.efb_pack_pipelines.for_color(copy_format, is_intensity).unwrap());
- rpass.set_bind_group(0, &self.xfb_copy_bind_group, &[]);
+ let (pipeline, bind_group) = match source {
+ EfbCopySource::Color { intensity } => (
+ self.efb_pack_pipelines.for_color(copy_format, intensity).unwrap(),
+ &self.xfb_copy_bind_group,
+ ),
+ EfbCopySource::Depth => (
+ self.efb_depth_pack_pipelines.for_format(copy_format),
+ &self.efb_depth_cache_bind_group,
+ ),
+ };
+ rpass.set_pipeline(pipeline);
+ rpass.set_bind_group(0, bind_group, &[]);
rpass.insert_debug_marker("EFB copy: per-format pack into cache");
rpass.draw(0..3, 0..1);
}
encoder.pop_debug_group();
self.current_encoder = Some(encoder);
- self.xfb_copy_uniform_write_pending = true;
+ match source {
+ EfbCopySource::Color { .. } => self.xfb_copy_uniform_write_pending = true,
+ EfbCopySource::Depth => self.efb_depth_cache_uniform_write_pending = true,
+ }
self.efb_copy_cache.insert(
dest_addr,
@@ -451,6 +472,47 @@ impl GxRenderer {
);
}
+ fn acquire_efb_copy_target(
+ &mut self,
+ device: &wgpu::Device,
+ dest_addr: Address,
+ width: u32,
+ height: u32,
+ copy_format: CopyFormat,
+ ) -> (wgpu::Texture, wgpu::TextureView) {
+ if let Some(entry) = self.efb_copy_cache.remove(&dest_addr) {
+ self.return_to_pool(entry.texture, entry.view);
+ }
+ self.bind_group_cache
+ .retain(|key, _| !key.tex_keys.iter().any(|k| k.map(|t| t.ram_addr) == Some(dest_addr)));
+
+ self.efb_copy_pool
+ .get_mut(&(width, height))
+ .and_then(Vec::pop)
+ .unwrap_or_else(|| {
+ let label = format!("efb_copy addr={dest_addr:#010x} size={width}x{height} fmt={copy_format:?}");
+ let texture = device.create_texture(&wgpu::TextureDescriptor {
+ label: Some(&label),
+ size: wgpu::Extent3d {
+ width,
+ height,
+ depth_or_array_layers: 1,
+ },
+ mip_level_count: 1,
+ sample_count: 1,
+ dimension: wgpu::TextureDimension::D2,
+ format: wgpu::TextureFormat::Rgba8Unorm,
+ usage: wgpu::TextureUsages::TEXTURE_BINDING
+ | wgpu::TextureUsages::RENDER_ATTACHMENT
+ | wgpu::TextureUsages::COPY_DST
+ | wgpu::TextureUsages::COPY_SRC,
+ view_formats: &[],
+ });
+ let view = texture.create_view(&Default::default());
+ (texture, view)
+ })
+ }
+
pub(crate) fn return_to_pool(&mut self, tex: wgpu::Texture, view: wgpu::TextureView) {
const PER_BUCKET_CAP: usize = 8;
let size = tex.size();
@@ -647,6 +709,7 @@ impl GxRenderer {
mipmap: bool,
stride: u32,
depth_copy: bool,
+ is_intensity: bool,
) {
tracing::debug!(
dest_addr = format!("{dest_addr:#010X}"),
@@ -699,6 +762,18 @@ impl GxRenderer {
stride,
copy_format_enum,
);
+ self.cache_efb_copy(
+ device,
+ queue,
+ dest_addr,
+ src_x,
+ src_y,
+ width,
+ height,
+ mipmap,
+ copy_format_enum,
+ EfbCopySource::Depth,
+ );
return;
}
@@ -825,6 +900,21 @@ impl GxRenderer {
swap_bgra,
box_filter_downsample: mipmap,
});
+
+ self.cache_efb_copy(
+ device,
+ queue,
+ dest_addr,
+ src_x,
+ src_y,
+ width,
+ height,
+ mipmap,
+ copy_format_enum,
+ EfbCopySource::Color {
+ intensity: is_intensity,
+ },
+ );
}
fn execute_depth_writeback(
@@ -866,21 +956,6 @@ impl GxRenderer {
queue.write_buffer(&self.efb_depth_resolve_uniform_buffer, 0, bytemuck::bytes_of(&uniforms));
self.efb_depth_resolve_uniform_write_pending = true;
- let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
- label: Some("efb_depth_bg"),
- layout: &self.efb_depth_resolve_bg_layout,
- entries: &[
- wgpu::BindGroupEntry {
- binding: 0,
- resource: self.efb_depth_resolve_uniform_buffer.as_entire_binding(),
- },
- wgpu::BindGroupEntry {
- binding: 1,
- resource: wgpu::BindingResource::TextureView(&self.efb_depth_view),
- },
- ],
- });
-
let bytes_per_row = align_up(encode_w as u64 * 4, 256);
let staging_size = bytes_per_row * encode_h as u64;
let (staging, staging_capacity) = self.acquire_readback_staging(device, staging_size);
@@ -909,8 +984,8 @@ impl GxRenderer {
});
rpass.set_viewport(0.0, 0.0, encode_w as f32, encode_h as f32, 0.0, 1.0);
rpass.set_scissor_rect(0, 0, encode_w, encode_h);
- rpass.set_pipeline(&self.efb_depth_writeback_pipeline);
- rpass.set_bind_group(0, &bind_group, &[]);
+ rpass.set_pipeline(self.efb_depth_pack_pipelines.for_format(CopyFormat::Z24X8));
+ rpass.set_bind_group(0, &self.efb_depth_resolve_bind_group, &[]);
rpass.draw(0..3, 0..1);
}
encoder.copy_texture_to_buffer(
@@ -936,7 +1011,6 @@ impl GxRenderer {
);
encoder.pop_debug_group();
self.pending_command_buffers.push(encoder.finish());
- self.efb_depth_resolve_uniform_write_pending = false;
self.pending_writebacks.push(PendingWriteback {
dest_addr,
diff --git a/crates/backend-wgpu/src/shaders/efb_depth.wgsl b/crates/backend-wgpu/src/shaders/efb_depth.wgsl
index 18c0f6b..32ee106 100644
--- a/crates/backend-wgpu/src/shaders/efb_depth.wgsl
+++ b/crates/backend-wgpu/src/shaders/efb_depth.wgsl
@@ -11,38 +11,23 @@ var<uniform> u: Uniforms;
@group(0) @binding(1)
var efb_depth: texture_depth_multisampled_2d;
-struct VsOut {
- @builtin(position) position: vec4<f32>,
-};
-
-@vertex
-fn vs_main(@builtin(vertex_index) vi: u32) -> VsOut {
- let uv = vec2<f32>(f32((vi << 1u) & 2u), f32(vi & 2u));
- var out: VsOut;
- out.position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);
- return out;
+fn sample_depth(pos: vec2<f32>) -> f32 {
+ let dst_size = max(u.dst_size, vec2<f32>(1.0, 1.0));
+ let src_pixel = u.src_rect.xy + (pos / dst_size) * u.src_rect.zw;
+ return textureLoad(efb_depth, vec2<i32>(src_pixel), 0);
}
@fragment
fn fs_main(@builtin(position) position: vec4<f32>) -> @location(0) f32 {
- let dst_size = max(u.dst_size, vec2<f32>(1.0, 1.0));
- let src_pixel = u.src_rect.xy + (position.xy / dst_size) * u.src_rect.zw;
- let coord = vec2<i32>(src_pixel);
- return textureLoad(efb_depth, coord, 0);
+ return sample_depth(position.xy);
}
const Z24_SCALE: f32 = 16777216.0; // 2^24
-@fragment
-fn fs_writeback_z24(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> {
- let dst_size = max(u.dst_size, vec2<f32>(1.0, 1.0));
- let src_pixel = u.src_rect.xy + (position.xy / dst_size) * u.src_rect.zw;
- let coord = vec2<i32>(src_pixel);
- let depth = textureLoad(efb_depth, coord, 0);
- let z24 = u32(depth * Z24_SCALE);
+fn fetch(pos: vec2<f32>) -> vec4<f32> {
+ let z24 = u32(sample_depth(pos) * Z24_SCALE);
let r = f32((z24 >> 16u) & 0xFFu) / 255.0;
let g = f32((z24 >> 8u) & 0xFFu) / 255.0;
let b = f32( z24 & 0xFFu) / 255.0;
return vec4<f32>(r, g, b, 1.0);
}
-
diff --git a/crates/backend-wgpu/src/shaders/efb_pack.wgsl b/crates/backend-wgpu/src/shaders/efb_pack.wgsl
index 2c94739..c150d21 100644
--- a/crates/backend-wgpu/src/shaders/efb_pack.wgsl
+++ b/crates/backend-wgpu/src/shaders/efb_pack.wgsl
@@ -9,149 +9,22 @@ struct Uniforms {
@group(0) @binding(1) var efb_color: texture_2d<f32>;
@group(0) @binding(2) var efb_color_sampler: sampler;
-struct VsOut {
- @builtin(position) position: vec4<f32>,
-};
-
-@vertex
-fn vs_main(@builtin(vertex_index) vi: u32) -> VsOut {
- let uv = vec2<f32>(f32((vi << 1u) & 2u), f32(vi & 2u));
- var out: VsOut;
- out.position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);
- return out;
-}
-
fn fetch(pos: vec2<f32>) -> vec4<f32> {
let dst_size = max(u.dst_size, vec2<f32>(1.0, 1.0));
- let src_pixel = u.src_rect.xy + (pos / dst_size) * u.src_rect.zw;
- let coord = vec2<i32>(src_pixel);
- return textureLoad(efb_color, coord, 0);
-}
-
-fn luma_u8(s: vec4<f32>) -> u32 {
- let r = u32(round(s.r * 255.0));
- let g = u32(round(s.g * 255.0));
- let b = u32(round(s.b * 255.0));
- return min((299u * r + 587u * g + 114u * b) / 1000u, 255u);
-}
-
-fn expand3(v: u32) -> u32 { return (v << 5u) | (v << 2u) | (v >> 1u); }
-fn expand4(v: u32) -> u32 { return (v << 4u) | v; }
-fn expand5(v: u32) -> u32 { return (v << 3u) | (v >> 2u); }
-fn expand6(v: u32) -> u32 { return (v << 2u) | (v >> 4u); }
-
-@fragment
-fn fs_rgba8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- return fetch(p.xy);
-}
-
-@fragment
-fn fs_rgba8_intensity(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let s = fetch(p.xy);
- let yf = f32(luma_u8(s)) / 255.0;
- return vec4<f32>(yf, yf, yf, s.a);
-}
-
-@fragment
-fn fs_i8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let yf = f32(luma_u8(fetch(p.xy))) / 255.0;
- return vec4<f32>(yf, yf, yf, yf);
-}
-
-@fragment
-fn fs_i4(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let q = (luma_u8(fetch(p.xy)) >> 4u) & 0xFu;
- let vf = f32(expand4(q)) / 255.0;
- return vec4<f32>(vf, vf, vf, vf);
-}
-
-@fragment
-fn fs_ia8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let s = fetch(p.xy);
- let yf = f32(luma_u8(s)) / 255.0;
- return vec4<f32>(yf, yf, yf, s.a);
-}
-
-@fragment
-fn fs_ia4(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let s = fetch(p.xy);
- let iq = (luma_u8(s) >> 4u) & 0xFu;
- let aq = (u32(round(s.a * 255.0)) >> 4u) & 0xFu;
- let ivf = f32(expand4(iq)) / 255.0;
- let avf = f32(expand4(aq)) / 255.0;
- return vec4<f32>(ivf, ivf, ivf, avf);
-}
-
-@fragment
-fn fs_rgb565_intensity(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let y = luma_u8(fetch(p.xy));
- let r5 = (y >> 3u) & 0x1Fu;
- let g6 = (y >> 2u) & 0x3Fu;
- let b5 = (y >> 3u) & 0x1Fu;
- return vec4<f32>(f32(expand5(r5)) / 255.0, f32(expand6(g6)) / 255.0, f32(expand5(b5)) / 255.0, 1.0);
-}
-
-@fragment
-fn fs_rgb565(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let s = fetch(p.xy);
- let r5 = (u32(round(s.r * 255.0)) >> 3u) & 0x1Fu;
- let g6 = (u32(round(s.g * 255.0)) >> 2u) & 0x3Fu;
- let b5 = (u32(round(s.b * 255.0)) >> 3u) & 0x1Fu;
- return vec4<f32>(f32(expand5(r5)) / 255.0, f32(expand6(g6)) / 255.0, f32(expand5(b5)) / 255.0, 1.0);
-}
-
-@fragment
-fn fs_rgb5a3_intensity(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let s = fetch(p.xy);
- let y = luma_u8(s);
- let a = u32(round(s.a * 255.0));
-
- if (a == 255u) {
- let vf = f32(expand5((y >> 3u) & 0x1Fu)) / 255.0;
- return vec4<f32>(vf, vf, vf, 1.0);
- } else {
- let vf = f32(expand4((y >> 4u) & 0xFu)) / 255.0;
- let af = f32(expand3((a >> 5u) & 0x7u)) / 255.0;
- return vec4<f32>(vf, vf, vf, af);
- }
-}
-
-@fragment
-fn fs_rgb5a3(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let s = fetch(p.xy);
- let r = u32(round(s.r * 255.0));
- let g = u32(round(s.g * 255.0));
- let b = u32(round(s.b * 255.0));
- let a = u32(round(s.a * 255.0));
- if (a == 255u) {
- let r8 = expand5((r >> 3u) & 0x1Fu);
- let g8 = expand5((g >> 3u) & 0x1Fu);
- let b8 = expand5((b >> 3u) & 0x1Fu);
- return vec4<f32>(f32(r8) / 255.0, f32(g8) / 255.0, f32(b8) / 255.0, 1.0);
- } else {
- let r8 = expand4((r >> 4u) & 0xFu);
- let g8 = expand4((g >> 4u) & 0xFu);
- let b8 = expand4((b >> 4u) & 0xFu);
- let a8 = expand3((a >> 5u) & 0x7u);
- return vec4<f32>(f32(r8) / 255.0, f32(g8) / 255.0, f32(b8) / 255.0, f32(a8) / 255.0);
+ if (u.filter_mode == 1u) {
+ let src_step = u.src_rect.zw / dst_size;
+ let first = u.src_rect.xy + (pos - vec2<f32>(0.5)) * src_step;
+ let coord = vec2<i32>(floor(first));
+ return (
+ textureLoad(efb_color, coord, 0) +
+ textureLoad(efb_color, coord + vec2<i32>(1, 0), 0) +
+ textureLoad(efb_color, coord + vec2<i32>(0, 1), 0) +
+ textureLoad(efb_color, coord + vec2<i32>(1, 1), 0)
+ ) * 0.25;
}
-}
-
-@fragment
-fn fs_a8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let a = fetch(p.xy).a;
- return vec4<f32>(a, a, a, a);
-}
-@fragment
-fn fs_r8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let r = fetch(p.xy).r;
- return vec4<f32>(r, r, r, r);
-}
-
-@fragment
-fn fs_rg8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
- let s = fetch(p.xy);
- return vec4<f32>(s.g, s.g, s.g, s.r);
+ let src_pixel = u.src_rect.xy + (pos / dst_size) * u.src_rect.zw;
+ let coord = vec2<i32>(src_pixel);
+ return textureLoad(efb_color, coord, 0);
}
diff --git a/crates/backend-wgpu/src/shaders/efb_pack_formats.wgsl b/crates/backend-wgpu/src/shaders/efb_pack_formats.wgsl
new file mode 100644
index 0000000..a46b476
--- /dev/null
+++ b/crates/backend-wgpu/src/shaders/efb_pack_formats.wgsl
@@ -0,0 +1,139 @@
+struct VsOut {
+ @builtin(position) position: vec4<f32>,
+};
+
+@vertex
+fn vs_main(@builtin(vertex_index) vi: u32) -> VsOut {
+ let uv = vec2<f32>(f32((vi << 1u) & 2u), f32(vi & 2u));
+ var out: VsOut;
+ out.position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);
+ return out;
+}
+
+fn luma_u8(s: vec4<f32>) -> u32 {
+ let r = u32(round(s.r * 255.0));
+ let g = u32(round(s.g * 255.0));
+ let b = u32(round(s.b * 255.0));
+ return min((299u * r + 587u * g + 114u * b) / 1000u, 255u);
+}
+
+fn expand3(v: u32) -> u32 { return (v << 5u) | (v << 2u) | (v >> 1u); }
+fn expand4(v: u32) -> u32 { return (v << 4u) | v; }
+fn expand5(v: u32) -> u32 { return (v << 3u) | (v >> 2u); }
+fn expand6(v: u32) -> u32 { return (v << 2u) | (v >> 4u); }
+
+@fragment
+fn fs_rgba8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ return fetch(p.xy);
+}
+
+@fragment
+fn fs_rgba8_intensity(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let s = fetch(p.xy);
+ let yf = f32(luma_u8(s)) / 255.0;
+ return vec4<f32>(yf, yf, yf, s.a);
+}
+
+@fragment
+fn fs_i8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let yf = f32(luma_u8(fetch(p.xy))) / 255.0;
+ return vec4<f32>(yf, yf, yf, yf);
+}
+
+@fragment
+fn fs_i4(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let q = (luma_u8(fetch(p.xy)) >> 4u) & 0xFu;
+ let vf = f32(expand4(q)) / 255.0;
+ return vec4<f32>(vf, vf, vf, vf);
+}
+
+@fragment
+fn fs_ia8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let s = fetch(p.xy);
+ let yf = f32(luma_u8(s)) / 255.0;
+ return vec4<f32>(yf, yf, yf, s.a);
+}
+
+@fragment
+fn fs_ia4(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let s = fetch(p.xy);
+ let iq = (luma_u8(s) >> 4u) & 0xFu;
+ let aq = (u32(round(s.a * 255.0)) >> 4u) & 0xFu;
+ let ivf = f32(expand4(iq)) / 255.0;
+ let avf = f32(expand4(aq)) / 255.0;
+ return vec4<f32>(ivf, ivf, ivf, avf);
+}
+
+@fragment
+fn fs_rgb565_intensity(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let y = luma_u8(fetch(p.xy));
+ let r5 = (y >> 3u) & 0x1Fu;
+ let g6 = (y >> 2u) & 0x3Fu;
+ let b5 = (y >> 3u) & 0x1Fu;
+ return vec4<f32>(f32(expand5(r5)) / 255.0, f32(expand6(g6)) / 255.0, f32(expand5(b5)) / 255.0, 1.0);
+}
+
+@fragment
+fn fs_rgb565(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let s = fetch(p.xy);
+ let r5 = (u32(round(s.r * 255.0)) >> 3u) & 0x1Fu;
+ let g6 = (u32(round(s.g * 255.0)) >> 2u) & 0x3Fu;
+ let b5 = (u32(round(s.b * 255.0)) >> 3u) & 0x1Fu;
+ return vec4<f32>(f32(expand5(r5)) / 255.0, f32(expand6(g6)) / 255.0, f32(expand5(b5)) / 255.0, 1.0);
+}
+
+@fragment
+fn fs_rgb5a3_intensity(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let s = fetch(p.xy);
+ let y = luma_u8(s);
+ let a = u32(round(s.a * 255.0));
+
+ if (a == 255u) {
+ let vf = f32(expand5((y >> 3u) & 0x1Fu)) / 255.0;
+ return vec4<f32>(vf, vf, vf, 1.0);
+ } else {
+ let vf = f32(expand4((y >> 4u) & 0xFu)) / 255.0;
+ let af = f32(expand3((a >> 5u) & 0x7u)) / 255.0;
+ return vec4<f32>(vf, vf, vf, af);
+ }
+}
+
+@fragment
+fn fs_rgb5a3(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let s = fetch(p.xy);
+ let r = u32(round(s.r * 255.0));
+ let g = u32(round(s.g * 255.0));
+ let b = u32(round(s.b * 255.0));
+ let a = u32(round(s.a * 255.0));
+
+ if (a == 255u) {
+ let r8 = expand5((r >> 3u) & 0x1Fu);
+ let g8 = expand5((g >> 3u) & 0x1Fu);
+ let b8 = expand5((b >> 3u) & 0x1Fu);
+ return vec4<f32>(f32(r8) / 255.0, f32(g8) / 255.0, f32(b8) / 255.0, 1.0);
+ } else {
+ let r8 = expand4((r >> 4u) & 0xFu);
+ let g8 = expand4((g >> 4u) & 0xFu);
+ let b8 = expand4((b >> 4u) & 0xFu);
+ let a8 = expand3((a >> 5u) & 0x7u);
+ return vec4<f32>(f32(r8) / 255.0, f32(g8) / 255.0, f32(b8) / 255.0, f32(a8) / 255.0);
+ }
+}
+
+@fragment
+fn fs_a8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let a = fetch(p.xy).a;
+ return vec4<f32>(a, a, a, a);
+}
+
+@fragment
+fn fs_r8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let r = fetch(p.xy).r;
+ return vec4<f32>(r, r, r, r);
+}
+
+@fragment
+fn fs_rg8(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> {
+ let s = fetch(p.xy);
+ return vec4<f32>(s.r, s.r, s.r, s.g);
+}