Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

fix depth precision, fixes wind waker shoreline foam flickering in intro scene

Layle31e09eba53fd
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index f9c47f6..bde7a5f 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -857,7 +857,7 @@ impl GxRenderer {
wgpu::LoadOp::Load
};
let depth_load = if needs_initial_clear {
- wgpu::LoadOp::Clear(1.0)
+ wgpu::LoadOp::Clear(0.0)
} else {
wgpu::LoadOp::Load
};
@@ -897,7 +897,7 @@ impl GxRenderer {
#[cfg(feature = "renderdoc-capture")]
{
if needs_initial_clear {
- rpass.insert_debug_marker("EFB initial clear: color=black depth=1.0");
+ rpass.insert_debug_marker("EFB initial clear: color=black reversed_depth=0.0");
} else {
rpass.insert_debug_marker("EFB load existing color/depth");
}
@@ -1008,8 +1008,8 @@ impl GxRenderer {
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() {
- let mut min_d = vp.min_depth.clamp(0.0, 1.0);
- let mut max_d = vp.max_depth.clamp(0.0, 1.0);
+ let mut min_d = 1.0 - vp.max_depth.clamp(0.0, 1.0);
+ let mut max_d = 1.0 - vp.min_depth.clamp(0.0, 1.0);
if min_d > max_d {
std::mem::swap(&mut min_d, &mut max_d);
}
diff --git a/crates/backend-wgpu/src/clear.rs b/crates/backend-wgpu/src/clear.rs
index d411683..9b5d37e 100644
--- a/crates/backend-wgpu/src/clear.rs
+++ b/crates/backend-wgpu/src/clear.rs
@@ -268,7 +268,7 @@ impl EfbClear {
let uniforms = ClearUniforms {
color,
- depth,
+ depth: 1.0 - depth,
_pad: [0.0; 3],
};
queue.write_buffer(&self.uniform_buffer, 0, bytemuck::bytes_of(&uniforms));
diff --git a/crates/backend-wgpu/src/helpers.rs b/crates/backend-wgpu/src/helpers.rs
index a5ec356..7e85f52 100644
--- a/crates/backend-wgpu/src/helpers.rs
+++ b/crates/backend-wgpu/src/helpers.rs
@@ -50,15 +50,15 @@ pub fn map_dst_blend_factor(f: BlendFactor) -> wgpu::BlendFactor {
}
}
-pub fn map_compare_func(f: CompareFunc) -> wgpu::CompareFunction {
+pub fn map_depth_compare_func(f: CompareFunc) -> wgpu::CompareFunction {
match f {
CompareFunc::Never => wgpu::CompareFunction::Never,
- CompareFunc::Less => wgpu::CompareFunction::Less,
+ CompareFunc::Less => wgpu::CompareFunction::Greater,
CompareFunc::Equal => wgpu::CompareFunction::Equal,
- CompareFunc::LessEqual => wgpu::CompareFunction::LessEqual,
- CompareFunc::Greater => wgpu::CompareFunction::Greater,
+ CompareFunc::LessEqual => wgpu::CompareFunction::GreaterEqual,
+ CompareFunc::Greater => wgpu::CompareFunction::Less,
CompareFunc::NotEqual => wgpu::CompareFunction::NotEqual,
- CompareFunc::GreaterEqual => wgpu::CompareFunction::GreaterEqual,
+ CompareFunc::GreaterEqual => wgpu::CompareFunction::LessEqual,
CompareFunc::Always => wgpu::CompareFunction::Always,
}
}
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index f843a5f..cc3a3b0 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -285,7 +285,7 @@ pub struct GxRenderer {
// EFB: multisampled (4x) color, actual render target for draws.
pub(crate) _efb_msaa_texture: wgpu::Texture,
pub(crate) efb_msaa_view: wgpu::TextureView,
- // EFB: multisampled (4x) depth.
+ // EFB: multisampled (4x) reversed depth (near = 1, far = 0).
pub(crate) efb_depth_view: wgpu::TextureView,
pub(crate) efb_needs_clear: bool,
pub(crate) sampler_cache: FxHashMap<(WrapMode, WrapMode, MagFilter, MinFilter), wgpu::Sampler>,
diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rs
index 0a2c417..943ce69 100644
--- a/crates/backend-wgpu/src/pipeline.rs
+++ b/crates/backend-wgpu/src/pipeline.rs
@@ -344,7 +344,7 @@ fn create_pipeline(
Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth24Plus,
depth_write_enabled: Some(key.z_write),
- depth_compare: Some(helpers::map_compare_func(key.z_func)),
+ depth_compare: Some(helpers::map_depth_compare_func(key.z_func)),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
})
diff --git a/crates/backend-wgpu/src/shaders/efb_depth.wgsl b/crates/backend-wgpu/src/shaders/efb_depth.wgsl
index 77c5752..15763cc 100644
--- a/crates/backend-wgpu/src/shaders/efb_depth.wgsl
+++ b/crates/backend-wgpu/src/shaders/efb_depth.wgsl
@@ -14,7 +14,7 @@ var efb_depth: texture_depth_multisampled_2d;
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);
+ return 1.0 - textureLoad(efb_depth, vec2<i32>(src_pixel), 0);
}
@fragment
diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.wesl
index 34044bb..85457ab 100644
--- a/crates/backend-wgpu/src/shaders/main.wesl
+++ b/crates/backend-wgpu/src/shaders/main.wesl
@@ -118,7 +118,7 @@ fn dynamic_tev_indirect(stage: u32) -> u32 {
fn vs_main(in: VsIn) -> VsOut {
var out: VsOut;
out.clip_pos = draw.mvp * vec4<f32>(in.pos_view, 1.0);
- out.clip_pos.z = out.clip_pos.z + out.clip_pos.w;
+ out.clip_pos.z = -out.clip_pos.z;
out.color = compute_lighting_c0(
shader_state(gx_color_ctrl_0, frame.color_ctrl0),
shader_state(gx_alpha_ctrl_0, frame.alpha_ctrl0),
@@ -340,12 +340,12 @@ fn fs_main_ztex(in: VsOut) -> FsOutZtex {
default: { z += (texel.r << 16u) | (texel.g << 8u) | texel.b; }
}
if shader_state(gx_ztex_op, frame.ztex_op) == 1u {
- z += u32(round(in.clip_pos.z * 16777215.0));
+ z += u32(round((1.0 - in.clip_pos.z) * 16777215.0));
}
z = z & 0x00FFFFFFu;
var out_: FsOutZtex;
out_.color = r.color;
- out_.depth = f32(z) / 16777215.0;
+ out_.depth = 1.0 - f32(z) / 16777215.0;
return out_;
}