A GameCube/Wii emulator and debugger written in Rust.
tev transparency wasnt wrapping correctly, fixes mario sunshine save selection background ocean (was just white before)
Layle14aae5a5c406
diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.weslindex 2e86345..5a3a539 100644--- a/crates/backend-wgpu/src/shaders/main.wesl+++ b/crates/backend-wgpu/src/shaders/main.wesl@@ -1,7 +1,7 @@import super::common::{FrameUniforms, DrawUniforms, VsIn, VsOut, frame, draw, tex0, tex1, tex2, tex3, tex4, tex5, tex6, tex7, samp0, samp1, samp2, samp3, samp4, samp5, samp6, samp7};import super::tev_helpers::{extract_bits, select_texcoord, texmap_size, texmap_uv, sample_texmap, swizzle_tev_color, tev_color_in, tev_alpha_in};import super::tev_indirect::{IndirectSamples, IndirectResult, sample_indirect_textures, apply_indirect};-import super::tev_combiners::{tev_combine_color, tev_combine_alpha};+import super::tev_combiners::{tev_combine_color, tev_combine_alpha, tev_wrap_rgb, tev_wrap_alpha};import super::alpha_test::{alpha_compare, alpha_combine};import super::lighting::{compute_lighting_c0, compute_lighting_c1};@@ -237,9 +237,9 @@ fn run_stage(let konst_color = frame.tev_konst_colors[stage];- let in_a = tev_color_in(c_a, tex_color, ras_color, (*state).regs, konst_color);- let in_b = tev_color_in(c_b, tex_color, ras_color, (*state).regs, konst_color);- let in_c = tev_color_in(c_c, tex_color, ras_color, (*state).regs, konst_color);+ let in_a = tev_wrap_rgb(tev_color_in(c_a, tex_color, ras_color, (*state).regs, konst_color));+ let in_b = tev_wrap_rgb(tev_color_in(c_b, tex_color, ras_color, (*state).regs, konst_color));+ let in_c = tev_wrap_rgb(tev_color_in(c_c, tex_color, ras_color, (*state).regs, konst_color));let in_d = tev_color_in(c_d, tex_color, ras_color, (*state).regs, konst_color);let color_result = tev_combine_color(in_a, in_b, in_c, in_d, c_bias, c_sub, c_scale, c_clamp);@@ -255,9 +255,9 @@ fn run_stage(let a_scale = extract_bits(aenv, 20u, 2u);let a_dest = extract_bits(aenv, 22u, 2u);- let ain_a = tev_alpha_in(a_a, tex_color, ras_color, (*state).regs, konst_color);- let ain_b = tev_alpha_in(a_b, tex_color, ras_color, (*state).regs, konst_color);- let ain_c = tev_alpha_in(a_c, tex_color, ras_color, (*state).regs, konst_color);+ let ain_a = tev_wrap_alpha(tev_alpha_in(a_a, tex_color, ras_color, (*state).regs, konst_color));+ let ain_b = tev_wrap_alpha(tev_alpha_in(a_b, tex_color, ras_color, (*state).regs, konst_color));+ let ain_c = tev_wrap_alpha(tev_alpha_in(a_c, tex_color, ras_color, (*state).regs, konst_color));let ain_d = tev_alpha_in(a_d, tex_color, ras_color, (*state).regs, konst_color);let alpha_result = tev_combine_alpha(ain_a, ain_b, ain_c, ain_d, in_a, in_b, a_bias, a_sub, a_scale, a_clamp);@@ -303,7 +303,10 @@ fn run_tev(in: VsOut) -> TevResult {@if(TEV_STAGE_15_ENABLED) { run_stage(14u, shader_state(gx_tev_order_14, dynamic_tev_order(14u)), shader_state(gx_tev_color_env_14, dynamic_tev_color_env(14u)), shader_state(gx_tev_alpha_env_14, dynamic_tev_alpha_env(14u)), shader_state(gx_tev_indirect_14, dynamic_tev_indirect(14u)), in, color0, presampled, &state); }@if(TEV_STAGE_16_ENABLED) { run_stage(15u, shader_state(gx_tev_order_15, dynamic_tev_order(15u)), shader_state(gx_tev_color_env_15, dynamic_tev_color_env(15u)), shader_state(gx_tev_alpha_env_15, dynamic_tev_alpha_env(15u)), shader_state(gx_tev_indirect_15, dynamic_tev_indirect(15u)), in, color0, presampled, &state); }- let color = vec4<f32>(state.regs[state.last_color_dest].rgb, state.regs[state.last_alpha_dest].a);+ let color = vec4<f32>(+ tev_wrap_rgb(state.regs[state.last_color_dest].rgb),+ tev_wrap_alpha(state.regs[state.last_alpha_dest].a),+ );@if(ALPHA_TEST_ENABLED) {let c0 = alpha_compare(color.a, frame.alpha_ref0, shader_state(gx_alpha_comp_0, frame.alpha_comp0));diff --git a/crates/backend-wgpu/src/shaders/tev_combiners.wesl b/crates/backend-wgpu/src/shaders/tev_combiners.weslindex 75c0c68..3558b62 100644--- a/crates/backend-wgpu/src/shaders/tev_combiners.wesl+++ b/crates/backend-wgpu/src/shaders/tev_combiners.wesl@@ -1,3 +1,30 @@+fn tev_wrap_rgb(value: vec3<f32>) -> vec3<f32> {+ return vec3<f32>(vec3<i32>(round(value * 255.0)) & vec3<i32>(255)) / 255.0;+}++fn tev_wrap_alpha(value: f32) -> f32 {+ return f32(i32(round(value * 255.0)) & 255) / 255.0;+}++fn tev_regular(a: vec4<f32>, b: vec4<f32>, c: vec4<f32>, d: vec4<f32>,+ bias: u32, sub: bool, scale: u32) -> vec4<f32> {+ let ai = vec4<i32>(round(a * 255.0));+ let bi = vec4<i32>(round(b * 255.0));+ let ci = vec4<i32>(round(c * 255.0));+ var di = vec4<i32>(round(d * 255.0));+ if bias == 1u { di += vec4<i32>(128); }+ if bias == 2u { di -= vec4<i32>(128); }++ let shift = select(scale, 0u, scale == 3u);+ let factor = ci + (ci >> vec4<u32>(7u));+ var lerp = ((ai << vec4<u32>(8u)) + (bi - ai) * factor) << vec4<u32>(shift);+ if scale != 3u { lerp += vec4<i32>(select(128, 127, sub)); }+ lerp >>= vec4<u32>(8u);+ var result = (di << vec4<u32>(shift)) + select(lerp, -lerp, sub);+ if scale == 3u { result >>= vec4<u32>(1u); }+ return vec4<f32>(result) / 255.0;+}+// TEV color combiner.//// In regular mode (bias != 3): result = (d (+/-) lerp(a, b, c) + bias) * scale@@ -11,12 +38,8 @@ fn tev_combine_color(a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>,var result: vec3<f32>;if bias == 3u {- let ai = vec3<u32>(u32(round(clamp(a.r, 0.0, 1.0) * 255.0)),- u32(round(clamp(a.g, 0.0, 1.0) * 255.0)),- u32(round(clamp(a.b, 0.0, 1.0) * 255.0)));- let bi = vec3<u32>(u32(round(clamp(b.r, 0.0, 1.0) * 255.0)),- u32(round(clamp(b.g, 0.0, 1.0) * 255.0)),- u32(round(clamp(b.b, 0.0, 1.0) * 255.0)));+ let ai = vec3<u32>(round(a * 255.0));+ let bi = vec3<u32>(round(b * 255.0));// scale is the TevCompareMode selector.var mask: vec3<f32>;@@ -52,26 +75,7 @@ fn tev_combine_color(a: vec3<f32>, b: vec3<f32>, c: vec3<f32>, d: vec3<f32>,result = d + c * mask;} else {- let lerp = a * (vec3(1.0) - c) + b * c;-- if sub {- result = d - lerp;- } else {- result = d + lerp;- }-- switch bias {- case 1u: { result += vec3(0.5); } // AddHalf- case 2u: { result -= vec3(0.5); } // SubHalf- default: {}- }-- switch scale {- case 1u: { result *= 2.0; } // Scale2- case 2u: { result *= 4.0; } // Scale4- case 3u: { result *= 0.5; } // Divide2- default: {}- }+ result = tev_regular(vec4(a, 0.0), vec4(b, 0.0), vec4(c, 0.0), vec4(d, 0.0), bias, sub, scale).rgb;}if do_clamp {@@ -90,14 +94,10 @@ fn tev_combine_alpha(a: f32, b: f32, c: f32, d: f32,var result: f32;if bias == 3u {- let ai = vec3<u32>(u32(round(clamp(a_rgb.r, 0.0, 1.0) * 255.0)),- u32(round(clamp(a_rgb.g, 0.0, 1.0) * 255.0)),- u32(round(clamp(a_rgb.b, 0.0, 1.0) * 255.0)));- let bi = vec3<u32>(u32(round(clamp(b_rgb.r, 0.0, 1.0) * 255.0)),- u32(round(clamp(b_rgb.g, 0.0, 1.0) * 255.0)),- u32(round(clamp(b_rgb.b, 0.0, 1.0) * 255.0)));- let aa = u32(round(clamp(a, 0.0, 1.0) * 255.0));- let ba = u32(round(clamp(b, 0.0, 1.0) * 255.0));+ let ai = vec3<u32>(round(a_rgb * 255.0));+ let bi = vec3<u32>(round(b_rgb * 255.0));+ let aa = u32(round(a * 255.0));+ let ba = u32(round(b * 255.0));var hit: f32;switch scale {@@ -117,26 +117,7 @@ fn tev_combine_alpha(a: f32, b: f32, c: f32, d: f32,result = d + c * hit;} else {- let lerp = a * (1.0 - c) + b * c;-- if sub {- result = d - lerp;- } else {- result = d + lerp;- }-- switch bias {- case 1u: { result += 0.5; }- case 2u: { result -= 0.5; }- default: {}- }-- switch scale {- case 1u: { result *= 2.0; }- case 2u: { result *= 4.0; }- case 3u: { result *= 0.5; }- default: {}- }+ result = tev_regular(vec4(a), vec4(b), vec4(c), vec4(d), bias, sub, scale).x;}if do_clamp {