Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

fix some gx blend and lighting

Laylee7a1d3217ad6
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index 6c57603..e1e4c4c 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -58,6 +58,8 @@ impl GxRenderer {
src_factor: blend.src_factor(),
dst_factor: blend.dst_factor(),
subtract: blend.subtract(),
+ logic_op_enable: blend.logic_op_enable(),
+ logic_op: blend.logic_op(),
z_enable: zmode.enable(),
z_func: zmode.func(),
z_write: zmode.update_enable(),
diff --git a/crates/backend-wgpu/src/helpers.rs b/crates/backend-wgpu/src/helpers.rs
index ac9864e..a5ec356 100644
--- a/crates/backend-wgpu/src/helpers.rs
+++ b/crates/backend-wgpu/src/helpers.rs
@@ -24,7 +24,20 @@ pub fn map_min_filter(filter: MinFilter) -> wgpu::FilterMode {
}
}
-pub fn map_blend_factor(f: BlendFactor) -> wgpu::BlendFactor {
+pub fn map_src_blend_factor(f: BlendFactor) -> wgpu::BlendFactor {
+ match f {
+ BlendFactor::Zero => wgpu::BlendFactor::Zero,
+ BlendFactor::One => wgpu::BlendFactor::One,
+ BlendFactor::SrcClr => wgpu::BlendFactor::Dst,
+ BlendFactor::InvSrcClr => wgpu::BlendFactor::OneMinusDst,
+ BlendFactor::SrcAlpha => wgpu::BlendFactor::SrcAlpha,
+ BlendFactor::InvSrcAlpha => wgpu::BlendFactor::OneMinusSrcAlpha,
+ BlendFactor::DstAlpha => wgpu::BlendFactor::DstAlpha,
+ BlendFactor::InvDstAlpha => wgpu::BlendFactor::OneMinusDstAlpha,
+ }
+}
+
+pub fn map_dst_blend_factor(f: BlendFactor) -> wgpu::BlendFactor {
match f {
BlendFactor::Zero => wgpu::BlendFactor::Zero,
BlendFactor::One => wgpu::BlendFactor::One,
diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rs
index 1044219..f089b94 100644
--- a/crates/backend-wgpu/src/pipeline.rs
+++ b/crates/backend-wgpu/src/pipeline.rs
@@ -1,7 +1,46 @@
use crate::shader_specialization::{KEY_BYTES as SHADER_KEY_BYTES, ShaderKey};
use crate::{GpuVertex, GxRenderer, helpers};
use chapa::BitField;
-use gecko::flipper::gx::regs::{BlendFactor, CompareFunc, CullMode};
+use gecko::flipper::gx::regs::{BlendFactor, CompareFunc, CullMode, LogicOp};
+
+fn make_blend_state(
+ src_factor: wgpu::BlendFactor,
+ dst_factor: wgpu::BlendFactor,
+ operation: wgpu::BlendOperation,
+) -> wgpu::BlendState {
+ let component = wgpu::BlendComponent {
+ src_factor,
+ dst_factor,
+ operation,
+ };
+ wgpu::BlendState {
+ color: component,
+ alpha: component,
+ }
+}
+
+// Blend factor approximations for logic ops.
+fn logic_op_approximation(op: LogicOp) -> (bool, bool, wgpu::BlendFactor, wgpu::BlendFactor) {
+ use wgpu::BlendFactor::*;
+ match op {
+ LogicOp::Clear => (true, false, Zero, Zero),
+ LogicOp::And => (true, false, Dst, Zero),
+ LogicOp::ReverseAnd => (true, true, One, OneMinusSrc),
+ LogicOp::Copy => (false, false, One, Zero),
+ LogicOp::InvertedAnd => (true, true, Dst, One),
+ LogicOp::Noop => (true, false, Zero, One),
+ LogicOp::Xor => (true, false, OneMinusDst, OneMinusSrc),
+ LogicOp::Or => (true, false, OneMinusDst, One),
+ LogicOp::Nor => (true, false, OneMinusDst, OneMinusSrc),
+ LogicOp::Equivalent => (true, false, OneMinusDst, Zero),
+ LogicOp::Invert => (true, false, OneMinusDst, Zero),
+ LogicOp::ReverseOr => (true, false, One, OneMinusDstAlpha),
+ LogicOp::InvertedCopy => (false, false, One, Zero),
+ LogicOp::InvertedOr => (true, false, OneMinusDst, One),
+ LogicOp::Nand => (true, false, OneMinusDst, OneMinusSrc),
+ LogicOp::Set => (false, false, One, Zero),
+ }
+}
use std::fs::File;
use std::io::{BufWriter, Read, Write};
use std::path::Path;
@@ -12,6 +51,8 @@ pub(crate) struct PipelineKey {
pub src_factor: BlendFactor,
pub dst_factor: BlendFactor,
pub subtract: bool,
+ pub logic_op_enable: bool,
+ pub logic_op: LogicOp,
pub z_enable: bool,
pub z_func: CompareFunc,
pub z_write: bool,
@@ -21,7 +62,7 @@ pub(crate) struct PipelineKey {
}
impl PipelineKey {
- const BYTES: usize = 10;
+ const BYTES: usize = 12;
fn to_bytes(self) -> [u8; Self::BYTES] {
[
@@ -29,6 +70,8 @@ impl PipelineKey {
self.src_factor.raw(),
self.dst_factor.raw(),
self.subtract as u8,
+ self.logic_op_enable as u8,
+ self.logic_op.raw(),
self.z_enable as u8,
self.z_func.raw(),
self.z_write as u8,
@@ -44,12 +87,14 @@ impl PipelineKey {
src_factor: BlendFactor::from_raw(b[1]),
dst_factor: BlendFactor::from_raw(b[2]),
subtract: b[3] != 0,
- z_enable: b[4] != 0,
- z_func: CompareFunc::from_raw(b[5]),
- z_write: b[6] != 0,
- color_update: b[7] != 0,
- alpha_update: b[8] != 0,
- cull_mode: CullMode::from_raw(b[9]),
+ logic_op_enable: b[4] != 0,
+ logic_op: LogicOp::from_raw(b[5]),
+ z_enable: b[6] != 0,
+ z_func: CompareFunc::from_raw(b[7]),
+ z_write: b[8] != 0,
+ color_update: b[9] != 0,
+ alpha_update: b[10] != 0,
+ cull_mode: CullMode::from_raw(b[11]),
}
}
}
@@ -222,22 +267,31 @@ impl GxRenderer {
};
let blend = if key.blend_enable {
- let operation = if key.subtract {
- wgpu::BlendOperation::ReverseSubtract
+ // In subtract mode the hardware ignores the configured src/dst
+ // factors and computes `dst = dst - src` with ONE/ONE.
+ let (src_factor, dst_factor, operation) = if key.subtract {
+ (
+ wgpu::BlendFactor::One,
+ wgpu::BlendFactor::One,
+ wgpu::BlendOperation::ReverseSubtract,
+ )
} else {
- wgpu::BlendOperation::Add
+ (
+ helpers::map_src_blend_factor(key.src_factor),
+ helpers::map_dst_blend_factor(key.dst_factor),
+ wgpu::BlendOperation::Add,
+ )
};
- Some(wgpu::BlendState {
- color: wgpu::BlendComponent {
- src_factor: helpers::map_blend_factor(key.src_factor),
- dst_factor: helpers::map_blend_factor(key.dst_factor),
- operation,
- },
- alpha: wgpu::BlendComponent {
- src_factor: helpers::map_blend_factor(key.src_factor),
- dst_factor: helpers::map_blend_factor(key.dst_factor),
- operation,
- },
+ Some(make_blend_state(src_factor, dst_factor, operation))
+ } else if key.logic_op_enable {
+ let (be, sub, sf, df) = logic_op_approximation(key.logic_op);
+ be.then(|| {
+ let operation = if sub {
+ wgpu::BlendOperation::ReverseSubtract
+ } else {
+ wgpu::BlendOperation::Add
+ };
+ make_blend_state(sf, df, operation)
})
} else {
None
diff --git a/crates/backend-wgpu/src/shader_specialization.rs b/crates/backend-wgpu/src/shader_specialization.rs
index e4c58bf..de0198b 100644
--- a/crates/backend-wgpu/src/shader_specialization.rs
+++ b/crates/backend-wgpu/src/shader_specialization.rs
@@ -15,7 +15,7 @@ const MAIN_WESL: &str = include_str!("shaders/main.wesl");
pub(crate) const KEY_BYTES: usize = 5;
const CACHE_MAGIC: [u8; 4] = *b"GSKC";
-pub(crate) const CACHE_VERSION: u32 = 2;
+pub(crate) const CACHE_VERSION: u32 = 3;
pub(crate) const SHADER_CACHE_PATH: &str = "cache/shader_keys.bin";
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
diff --git a/crates/backend-wgpu/src/shaders/lighting.wesl b/crates/backend-wgpu/src/shaders/lighting.wesl
index aa02419..dae3c14 100644
--- a/crates/backend-wgpu/src/shaders/lighting.wesl
+++ b/crates/backend-wgpu/src/shaders/lighting.wesl
@@ -31,15 +31,14 @@ fn compute_light_factor(ctrl: u32, light_idx: u32, normal: vec3<f32>, pos: vec3<
let angle_attn = max(cosatt.x + cosatt.y * cos_angle + cosatt.z * cos_angle * cos_angle, 0.0);
attn = safe_div(angle_attn, distatt.x + distatt.y * dist + distatt.z * dist * dist);
} else {
- let half_angle = normalize(light_pos);
- let norm_dot_half = dot(normal, half_angle);
- if norm_dot_half <= 0.0 {
- return 0.0;
- }
- let s = max(dot(normal, light_dir), 0.0);
+ ldir = normalize(ldir);
+ let s = select(0.0, max(dot(normal, light_dir), 0.0), dot(normal, ldir) >= 0.0);
let att_len = vec3(1.0, s, s * s);
- attn = safe_div(max(dot(att_len, cosatt), 0.0), dot(att_len, distatt));
- ldir = half_angle;
+ var distatt_eff = distatt;
+ if diff_fn != 0u {
+ distatt_eff = normalize(distatt);
+ }
+ attn = safe_div(max(dot(att_len, cosatt), 0.0), dot(att_len, distatt_eff));
}
let dif_attn = dot(ldir, normal);
diff --git a/crates/gecko/src/flipper/gx/regs.rs b/crates/gecko/src/flipper/gx/regs.rs
index 6027758..5454394 100644
--- a/crates/gecko/src/flipper/gx/regs.rs
+++ b/crates/gecko/src/flipper/gx/regs.rs
@@ -50,7 +50,7 @@ pub struct ZMode {
pub update_enable: bool,
}
-#[derive(Debug, BitEnum)]
+#[derive(Debug, PartialEq, Eq, Hash, BitEnum)]
pub enum LogicOp {
Clear = 0,
And = 1,
diff --git a/crates/gecko/src/hollywood/ipc/usb.rs b/crates/gecko/src/hollywood/ipc/usb.rs
index 30b8e19..316b30d 100644
--- a/crates/gecko/src/hollywood/ipc/usb.rs
+++ b/crates/gecko/src/hollywood/ipc/usb.rs
@@ -187,7 +187,7 @@ impl Bluetooth {
if !report_needed || self.host_hid_interrupt_cid.is_none() {
return;
}
-
+
let report = self.wiimote.make_input_report();
self.queue_hid_input_report(report);
}