Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

more spec

Layle702f233bb16c
diff --git a/crates/backend-wgpu/Cargo.toml b/crates/backend-wgpu/Cargo.toml
index c0b5cac..ea9cfcc 100644
--- a/crates/backend-wgpu/Cargo.toml
+++ b/crates/backend-wgpu/Cargo.toml
@@ -18,6 +18,3 @@ tracing = "0.1.44"
rustc-hash = "2.1.2"
png = "0.18.1"
renderdoc = { version = "0.12.1", optional = true }
-
-[build-dependencies]
-wesl = "0.3.2"
diff --git a/crates/backend-wgpu/build.rs b/crates/backend-wgpu/build.rs
deleted file mode 100644
index dc0fdea..0000000
--- a/crates/backend-wgpu/build.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-fn main() {
- wesl::Wesl::new("src/shaders").build_artifact(&"package::main".parse().unwrap(), "gx_shader");
-}
diff --git a/crates/backend-wgpu/src/action.rs b/crates/backend-wgpu/src/action.rs
index ee4eb94..e895216 100644
--- a/crates/backend-wgpu/src/action.rs
+++ b/crates/backend-wgpu/src/action.rs
@@ -175,7 +175,10 @@ impl GxRenderer {
tracing::info!(?shader_key, "compiled specialized shader variant");
}
let pipeline_key = self.current_pipeline_key();
- let full_key = FullPipelineKey { shader: shader_key, fixed: pipeline_key };
+ let full_key = FullPipelineKey {
+ shader: shader_key,
+ fixed: pipeline_key,
+ };
if !self.pipeline_cache.contains_key(&full_key) {
let module = &self.shader_cache[&shader_key];
let pipeline = self.create_pipeline(device, module, &pipeline_key);
diff --git a/crates/backend-wgpu/src/shader_specialization.rs b/crates/backend-wgpu/src/shader_specialization.rs
index 51015f6..d425a24 100644
--- a/crates/backend-wgpu/src/shader_specialization.rs
+++ b/crates/backend-wgpu/src/shader_specialization.rs
@@ -1,5 +1,6 @@
use gecko::flipper::gx::regs::{AlphaCompare, AlphaOp, CompareFunc};
use gecko::host::DrawData;
+use std::fmt::Write;
use wesl::{VirtualResolver, Wesl};
const COMMON_WESL: &str = include_str!("shaders/common.wesl");
@@ -10,33 +11,51 @@ const ALPHA_TEST_WESL: &str = include_str!("shaders/alpha_test.wesl");
const LIGHTING_WESL: &str = include_str!("shaders/lighting.wesl");
const MAIN_WESL: &str = include_str!("shaders/main.wesl");
+#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, Default)]
+pub(crate) struct StageKey {
+ pub order: u32,
+ pub color_env: u32,
+ pub alpha_env: u32,
+ pub ind_cmd: u32,
+}
+
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub(crate) struct ShaderKey {
pub num_tev_stages: u8,
pub num_indirect_stages: u8,
- pub has_lighting: bool,
+ pub has_lighting_c0: bool,
+ pub has_lighting_c1: bool,
pub alpha_test_enabled: bool,
+ pub stages: [StageKey; 16],
}
impl ShaderKey {
pub(crate) fn from_draw(draw: &DrawData, alpha_cmp: AlphaCompare) -> Self {
let num_tev_stages = draw.num_tev_stages.clamp(1, 16);
let num_indirect_stages = draw.num_indirect_stages.min(4);
- let has_lighting = draw.color_ctrl[0].enable()
- || draw.alpha_ctrl[0].enable()
- || draw.color_ctrl[1].enable()
- || draw.alpha_ctrl[1].enable();
+ let has_lighting_c0 = draw.color_ctrl[0].enable() || draw.alpha_ctrl[0].enable();
+ let has_lighting_c1 = draw.color_ctrl[1].enable() || draw.alpha_ctrl[1].enable();
let comp0 = alpha_cmp.comp0();
let comp1 = alpha_cmp.comp1();
let op = alpha_cmp.op();
- let always_pass = comp0 == CompareFunc::Always
- && comp1 == CompareFunc::Always
- && matches!(op, AlphaOp::And | AlphaOp::Or);
+ let always_pass =
+ comp0 == CompareFunc::Always && comp1 == CompareFunc::Always && matches!(op, AlphaOp::And | AlphaOp::Or);
+ let mut stages = [StageKey::default(); 16];
+ for i in 0..num_tev_stages as usize {
+ stages[i] = StageKey {
+ order: draw.tev_orders[i],
+ color_env: draw.tev_color_env[i],
+ alpha_env: draw.tev_alpha_env[i],
+ ind_cmd: draw.tev_indirect[i],
+ };
+ }
Self {
num_tev_stages,
num_indirect_stages,
- has_lighting,
+ has_lighting_c0,
+ has_lighting_c1,
alpha_test_enabled: !always_pass,
+ stages,
}
}
}
@@ -49,19 +68,34 @@ fn make_resolver() -> VirtualResolver<'static> {
r.add_module("package::tev_indirect".parse().unwrap(), TEV_INDIRECT_WESL.into());
r.add_module("package::alpha_test".parse().unwrap(), ALPHA_TEST_WESL.into());
r.add_module("package::lighting".parse().unwrap(), LIGHTING_WESL.into());
- r.add_module("package::main".parse().unwrap(), MAIN_WESL.into());
r
}
+fn generate_main(key: &ShaderKey) -> String {
+ let mut consts = String::with_capacity(2048);
+ for i in 0..16 {
+ let s = &key.stages[i];
+ let _ = writeln!(consts, "const STAGE_{i}_ORDER: u32 = {}u;", s.order);
+ let _ = writeln!(consts, "const STAGE_{i}_COLOR_ENV: u32 = {}u;", s.color_env);
+ let _ = writeln!(consts, "const STAGE_{i}_ALPHA_ENV: u32 = {}u;", s.alpha_env);
+ let _ = writeln!(consts, "const STAGE_{i}_IND_CMD: u32 = {}u;", s.ind_cmd);
+ }
+ MAIN_WESL.replace("// __STAGE_CONSTS__", &consts)
+}
+
pub(crate) fn compile_variant(key: ShaderKey) -> String {
- let mut compiler = Wesl::new("").set_custom_resolver(make_resolver());
+ let mut resolver = make_resolver();
+ let main_src = generate_main(&key);
+ resolver.add_module("package::main".parse().unwrap(), main_src.into());
+ let mut compiler = Wesl::new("").set_custom_resolver(resolver);
for i in 1..=16u8 {
compiler.set_feature(&format!("TEV_STAGE_{i}_ENABLED"), i <= key.num_tev_stages);
}
for i in 0..4u8 {
compiler.set_feature(&format!("IND_STAGE_{i}_ENABLED"), i < key.num_indirect_stages);
}
- compiler.set_feature("HAS_LIGHTING", key.has_lighting);
+ compiler.set_feature("HAS_LIGHTING_C0", key.has_lighting_c0);
+ compiler.set_feature("HAS_LIGHTING_C1", key.has_lighting_c1);
compiler.set_feature("ALPHA_TEST_ENABLED", key.alpha_test_enabled);
let entry = "package::main".parse().expect("valid module path");
compiler
diff --git a/crates/backend-wgpu/src/shaders/lighting.wesl b/crates/backend-wgpu/src/shaders/lighting.wesl
index 55ddc13..aa02419 100644
--- a/crates/backend-wgpu/src/shaders/lighting.wesl
+++ b/crates/backend-wgpu/src/shaders/lighting.wesl
@@ -1,16 +1,6 @@
import super::common::{FrameUniforms, frame};
import super::tev_helpers::extract_bits;
-// ChanCtrl bit layout (lsb0):
-// [0] mat_src
-// [1] enable
-// [2..5] lit_mask_lo (4 bits)
-// [6] amb_src
-// [7..8] diff_fn (0=None, 1=Signed, 2=Clamp)
-// [9] attn_enable
-// [10] attn_select (1=Spot, 0=Spec, only when attn_enable)
-// [11..14] lit_mask_hi (4 bits)
-
fn safe_div(n: f32, d: f32) -> f32 {
if abs(d) < 1e-10 {
return 0.0;
@@ -32,18 +22,15 @@ fn compute_light_factor(ctrl: u32, light_idx: u32, normal: vec3<f32>, pos: vec3<
var attn: f32;
if !attn_enable {
- // AttnFn::None
ldir = normalize(ldir);
attn = 1.0;
} else if attn_select {
- // AttnFn::Spot
let dist = length(ldir);
ldir = normalize(ldir);
let cos_angle = dot(light_dir, ldir);
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 {
- // AttnFn::Spec
let half_angle = normalize(light_pos);
let norm_dot_half = dot(normal, half_angle);
if norm_dot_half <= 0.0 {
@@ -58,102 +45,125 @@ fn compute_light_factor(ctrl: u32, light_idx: u32, normal: vec3<f32>, pos: vec3<
let dif_attn = dot(ldir, normal);
var diffuse: f32;
switch diff_fn {
- case 0u: { diffuse = 1.0; } // None
- case 1u: { diffuse = dif_attn; } // Signed
- case 2u: { diffuse = max(dif_attn, 0.0); } // Clamp
+ case 0u: { diffuse = 1.0; }
+ case 1u: { diffuse = dif_attn; }
+ case 2u: { diffuse = max(dif_attn, 0.0); }
default: { diffuse = 1.0; }
}
return attn * diffuse;
}
-fn compute_channel_lighting(
- color_ctrl: u32,
- alpha_ctrl: u32,
- ambient: vec4<f32>,
- material: vec4<f32>,
+fn material_select(
+ color_ctrl: u32, alpha_ctrl: u32,
+ ambient: vec4<f32>, material: vec4<f32>,
vertex_color: vec4<f32>,
- normal: vec3<f32>,
- pos: vec3<f32>,
) -> vec4<f32> {
- // RGB material/ambient source selection
var mat_rgb: vec3<f32>;
- if extract_bits(color_ctrl, 0u, 1u) != 0u {
- mat_rgb = vertex_color.rgb;
- } else {
- mat_rgb = material.rgb;
- }
+ if extract_bits(color_ctrl, 0u, 1u) != 0u { mat_rgb = vertex_color.rgb; }
+ else { mat_rgb = material.rgb; }
+ var mat_a: f32;
+ if extract_bits(alpha_ctrl, 0u, 1u) != 0u { mat_a = vertex_color.a; }
+ else { mat_a = material.a; }
+ return vec4(mat_rgb, mat_a);
+}
+
+fn compute_lighting_body(
+ color_ctrl: u32, alpha_ctrl: u32,
+ ambient: vec4<f32>, material: vec4<f32>,
+ vertex_color: vec4<f32>, normal: vec3<f32>, pos: vec3<f32>,
+) -> vec4<f32> {
+ var mat_rgb: vec3<f32>;
+ if extract_bits(color_ctrl, 0u, 1u) != 0u { mat_rgb = vertex_color.rgb; }
+ else { mat_rgb = material.rgb; }
var amb_rgb: vec3<f32>;
- if extract_bits(color_ctrl, 6u, 1u) != 0u {
- amb_rgb = vertex_color.rgb;
- } else {
- amb_rgb = ambient.rgb;
- }
+ if extract_bits(color_ctrl, 6u, 1u) != 0u { amb_rgb = vertex_color.rgb; }
+ else { amb_rgb = ambient.rgb; }
- // Alpha material/ambient source selection
var mat_a: f32;
- if extract_bits(alpha_ctrl, 0u, 1u) != 0u {
- mat_a = vertex_color.a;
- } else {
- mat_a = material.a;
- }
+ if extract_bits(alpha_ctrl, 0u, 1u) != 0u { mat_a = vertex_color.a; }
+ else { mat_a = material.a; }
var amb_a: f32;
- if extract_bits(alpha_ctrl, 6u, 1u) != 0u {
- amb_a = vertex_color.a;
- } else {
- amb_a = ambient.a;
- }
+ if extract_bits(alpha_ctrl, 6u, 1u) != 0u { amb_a = vertex_color.a; }
+ else { amb_a = ambient.a; }
- @if(!HAS_LIGHTING) {
+ let color_lit_off = extract_bits(color_ctrl, 1u, 1u) == 0u;
+ let alpha_lit_off = extract_bits(alpha_ctrl, 1u, 1u) == 0u;
+ if color_lit_off && alpha_lit_off {
return vec4(mat_rgb, mat_a);
}
- @if(HAS_LIGHTING) {
- let color_lit_off = extract_bits(color_ctrl, 1u, 1u) == 0u;
- let alpha_lit_off = extract_bits(alpha_ctrl, 1u, 1u) == 0u;
-
- if color_lit_off && alpha_lit_off {
- return vec4(mat_rgb, mat_a);
- }
+ var rgb_out = mat_rgb;
+ if !color_lit_off {
+ let mask_lo = extract_bits(color_ctrl, 2u, 4u);
+ let mask_hi = extract_bits(color_ctrl, 11u, 4u);
+ let light_mask = mask_lo | (mask_hi << 4u);
- var rgb_out = mat_rgb;
- if !color_lit_off {
- let mask_lo = extract_bits(color_ctrl, 2u, 4u);
- let mask_hi = extract_bits(color_ctrl, 11u, 4u);
- let light_mask = mask_lo | (mask_hi << 4u);
-
- var acc = amb_rgb;
- for (var i = 0u; i < 8u; i++) {
- if ((light_mask >> i) & 1u) == 0u {
- continue;
- }
- let lc = frame.light_colors[i];
- let factor = compute_light_factor(color_ctrl, i, normal, pos);
- acc += lc.rgb * factor;
+ var acc = amb_rgb;
+ for (var i = 0u; i < 8u; i++) {
+ if ((light_mask >> i) & 1u) == 0u {
+ continue;
}
- rgb_out = mat_rgb * clamp(acc, vec3(0.0), vec3(1.0));
+ let lc = frame.light_colors[i];
+ let factor = compute_light_factor(color_ctrl, i, normal, pos);
+ acc += lc.rgb * factor;
}
+ rgb_out = mat_rgb * clamp(acc, vec3(0.0), vec3(1.0));
+ }
- var a_out = mat_a;
- if !alpha_lit_off {
- let mask_lo = extract_bits(alpha_ctrl, 2u, 4u);
- let mask_hi = extract_bits(alpha_ctrl, 11u, 4u);
- let light_mask = mask_lo | (mask_hi << 4u);
-
- var acc_a = amb_a;
- for (var i = 0u; i < 8u; i++) {
- if ((light_mask >> i) & 1u) == 0u {
- continue;
- }
- let lc = frame.light_colors[i];
- let factor = compute_light_factor(alpha_ctrl, i, normal, pos);
- acc_a += lc.a * factor;
+ var a_out = mat_a;
+ if !alpha_lit_off {
+ let mask_lo = extract_bits(alpha_ctrl, 2u, 4u);
+ let mask_hi = extract_bits(alpha_ctrl, 11u, 4u);
+ let light_mask = mask_lo | (mask_hi << 4u);
+
+ var acc_a = amb_a;
+ for (var i = 0u; i < 8u; i++) {
+ if ((light_mask >> i) & 1u) == 0u {
+ continue;
}
- a_out = mat_a * clamp(acc_a, 0.0, 1.0);
+ let lc = frame.light_colors[i];
+ let factor = compute_light_factor(alpha_ctrl, i, normal, pos);
+ acc_a += lc.a * factor;
}
-
- return vec4(rgb_out, a_out);
+ a_out = mat_a * clamp(acc_a, 0.0, 1.0);
}
+
+ return vec4(rgb_out, a_out);
+}
+
+@if(HAS_LIGHTING_C0)
+fn compute_lighting_c0(
+ color_ctrl: u32, alpha_ctrl: u32,
+ ambient: vec4<f32>, material: vec4<f32>,
+ vertex_color: vec4<f32>, normal: vec3<f32>, pos: vec3<f32>,
+) -> vec4<f32> {
+ return compute_lighting_body(color_ctrl, alpha_ctrl, ambient, material, vertex_color, normal, pos);
+}
+@else
+fn compute_lighting_c0(
+ color_ctrl: u32, alpha_ctrl: u32,
+ ambient: vec4<f32>, material: vec4<f32>,
+ vertex_color: vec4<f32>, normal: vec3<f32>, pos: vec3<f32>,
+) -> vec4<f32> {
+ return material_select(color_ctrl, alpha_ctrl, ambient, material, vertex_color);
+}
+
+@if(HAS_LIGHTING_C1)
+fn compute_lighting_c1(
+ color_ctrl: u32, alpha_ctrl: u32,
+ ambient: vec4<f32>, material: vec4<f32>,
+ vertex_color: vec4<f32>, normal: vec3<f32>, pos: vec3<f32>,
+) -> vec4<f32> {
+ return compute_lighting_body(color_ctrl, alpha_ctrl, ambient, material, vertex_color, normal, pos);
+}
+@else
+fn compute_lighting_c1(
+ color_ctrl: u32, alpha_ctrl: u32,
+ ambient: vec4<f32>, material: vec4<f32>,
+ vertex_color: vec4<f32>, normal: vec3<f32>, pos: vec3<f32>,
+) -> vec4<f32> {
+ return material_select(color_ctrl, alpha_ctrl, ambient, material, vertex_color);
}
diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.wesl
index 4db79f2..8f7813e 100644
--- a/crates/backend-wgpu/src/shaders/main.wesl
+++ b/crates/backend-wgpu/src/shaders/main.wesl
@@ -1,21 +1,23 @@
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::{read_packed, extract_bits, select_texcoord, stq_to_uv, sample_texmap, swizzle_tev_color, tev_color_in, tev_alpha_in};
+import super::tev_helpers::{extract_bits, select_texcoord, stq_to_uv, sample_texmap, swizzle_tev_color, tev_color_in, tev_alpha_in};
import super::tev_indirect::{IndirectSamples, IndirectResult, sample_indirect_textures, apply_indirect, texmap_size};
import super::tev_combiners::{tev_combine_color, tev_combine_alpha};
import super::alpha_test::{alpha_compare, alpha_combine};
-import super::lighting::compute_channel_lighting;
+import super::lighting::{compute_lighting_c0, compute_lighting_c1};
+
+// __STAGE_CONSTS__
@vertex
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.color = compute_channel_lighting(
+ out.color = compute_lighting_c0(
frame.color_ctrl0, frame.alpha_ctrl0,
frame.ambient_color0, frame.material_color0,
in.color, in.normal, in.pos_view,
);
- out.color1 = compute_channel_lighting(
+ out.color1 = compute_lighting_c1(
frame.color_ctrl1, frame.alpha_ctrl1,
frame.ambient_color1, frame.material_color1,
in.color1, in.normal, in.pos_view,
@@ -40,22 +42,24 @@ struct TevState {
fn run_stage(
stage: u32,
+ order: u32,
+ cenv: u32,
+ aenv: u32,
+ ind_cmd: u32,
in: VsOut,
color0: vec4<f32>,
presampled: IndirectSamples,
state: ptr<function, TevState>,
) {
- let order = read_packed(frame.tev_orders, stage);
let texmap_idx = order & 7u;
let texcoord_idx = (order >> 3u) & 7u;
let tex_enabled = ((order >> 6u) & 1u) != 0u;
let ras_channel = (order >> 7u) & 7u;
- let aenv = read_packed(frame.tev_alpha_env, stage);
let stq = select_texcoord(in, texcoord_idx);
let direct_uv = stq_to_uv(stq);
let direct_size = texmap_size(texmap_idx);
- let indirect = apply_indirect(stage, direct_uv, direct_size, presampled, (*state).running_uv);
+ let indirect = apply_indirect(ind_cmd, direct_uv, direct_size, presampled, (*state).running_uv);
(*state).running_uv = indirect.uv;
var ras_color: vec4<f32>;
@@ -80,7 +84,6 @@ fn run_stage(
ras_color = swizzle_tev_color(extract_bits(aenv, 0u, 2u), ras_color);
tex_color = swizzle_tev_color(extract_bits(aenv, 2u, 2u), tex_color);
- let cenv = read_packed(frame.tev_color_env, stage);
let c_d = extract_bits(cenv, 0u, 4u);
let c_c = extract_bits(cenv, 4u, 4u);
let c_b = extract_bits(cenv, 8u, 4u);
@@ -137,22 +140,22 @@ fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
let color0 = in.color;
let presampled = sample_indirect_textures(in);
- @if(TEV_STAGE_1_ENABLED) { run_stage( 0u, in, color0, presampled, &state); }
- @if(TEV_STAGE_2_ENABLED) { run_stage( 1u, in, color0, presampled, &state); }
- @if(TEV_STAGE_3_ENABLED) { run_stage( 2u, in, color0, presampled, &state); }
- @if(TEV_STAGE_4_ENABLED) { run_stage( 3u, in, color0, presampled, &state); }
- @if(TEV_STAGE_5_ENABLED) { run_stage( 4u, in, color0, presampled, &state); }
- @if(TEV_STAGE_6_ENABLED) { run_stage( 5u, in, color0, presampled, &state); }
- @if(TEV_STAGE_7_ENABLED) { run_stage( 6u, in, color0, presampled, &state); }
- @if(TEV_STAGE_8_ENABLED) { run_stage( 7u, in, color0, presampled, &state); }
- @if(TEV_STAGE_9_ENABLED) { run_stage( 8u, in, color0, presampled, &state); }
- @if(TEV_STAGE_10_ENABLED) { run_stage( 9u, in, color0, presampled, &state); }
- @if(TEV_STAGE_11_ENABLED) { run_stage(10u, in, color0, presampled, &state); }
- @if(TEV_STAGE_12_ENABLED) { run_stage(11u, in, color0, presampled, &state); }
- @if(TEV_STAGE_13_ENABLED) { run_stage(12u, in, color0, presampled, &state); }
- @if(TEV_STAGE_14_ENABLED) { run_stage(13u, in, color0, presampled, &state); }
- @if(TEV_STAGE_15_ENABLED) { run_stage(14u, in, color0, presampled, &state); }
- @if(TEV_STAGE_16_ENABLED) { run_stage(15u, in, color0, presampled, &state); }
+ @if(TEV_STAGE_1_ENABLED) { run_stage( 0u, STAGE_0_ORDER, STAGE_0_COLOR_ENV, STAGE_0_ALPHA_ENV, STAGE_0_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_2_ENABLED) { run_stage( 1u, STAGE_1_ORDER, STAGE_1_COLOR_ENV, STAGE_1_ALPHA_ENV, STAGE_1_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_3_ENABLED) { run_stage( 2u, STAGE_2_ORDER, STAGE_2_COLOR_ENV, STAGE_2_ALPHA_ENV, STAGE_2_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_4_ENABLED) { run_stage( 3u, STAGE_3_ORDER, STAGE_3_COLOR_ENV, STAGE_3_ALPHA_ENV, STAGE_3_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_5_ENABLED) { run_stage( 4u, STAGE_4_ORDER, STAGE_4_COLOR_ENV, STAGE_4_ALPHA_ENV, STAGE_4_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_6_ENABLED) { run_stage( 5u, STAGE_5_ORDER, STAGE_5_COLOR_ENV, STAGE_5_ALPHA_ENV, STAGE_5_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_7_ENABLED) { run_stage( 6u, STAGE_6_ORDER, STAGE_6_COLOR_ENV, STAGE_6_ALPHA_ENV, STAGE_6_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_8_ENABLED) { run_stage( 7u, STAGE_7_ORDER, STAGE_7_COLOR_ENV, STAGE_7_ALPHA_ENV, STAGE_7_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_9_ENABLED) { run_stage( 8u, STAGE_8_ORDER, STAGE_8_COLOR_ENV, STAGE_8_ALPHA_ENV, STAGE_8_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_10_ENABLED) { run_stage( 9u, STAGE_9_ORDER, STAGE_9_COLOR_ENV, STAGE_9_ALPHA_ENV, STAGE_9_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_11_ENABLED) { run_stage(10u, STAGE_10_ORDER, STAGE_10_COLOR_ENV, STAGE_10_ALPHA_ENV, STAGE_10_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_12_ENABLED) { run_stage(11u, STAGE_11_ORDER, STAGE_11_COLOR_ENV, STAGE_11_ALPHA_ENV, STAGE_11_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_13_ENABLED) { run_stage(12u, STAGE_12_ORDER, STAGE_12_COLOR_ENV, STAGE_12_ALPHA_ENV, STAGE_12_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_14_ENABLED) { run_stage(13u, STAGE_13_ORDER, STAGE_13_COLOR_ENV, STAGE_13_ALPHA_ENV, STAGE_13_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_15_ENABLED) { run_stage(14u, STAGE_14_ORDER, STAGE_14_COLOR_ENV, STAGE_14_ALPHA_ENV, STAGE_14_IND_CMD, in, color0, presampled, &state); }
+ @if(TEV_STAGE_16_ENABLED) { run_stage(15u, STAGE_15_ORDER, STAGE_15_COLOR_ENV, STAGE_15_ALPHA_ENV, STAGE_15_IND_CMD, in, color0, presampled, &state); }
let color = vec4<f32>(state.regs[state.last_color_dest].rgb, state.regs[state.last_alpha_dest].a);
diff --git a/crates/backend-wgpu/src/shaders/tev_indirect.wesl b/crates/backend-wgpu/src/shaders/tev_indirect.wesl
index cf1a995..fde827e 100644
--- a/crates/backend-wgpu/src/shaders/tev_indirect.wesl
+++ b/crates/backend-wgpu/src/shaders/tev_indirect.wesl
@@ -110,7 +110,7 @@ struct IndirectResult {
}
fn apply_indirect(
- stage: u32,
+ cmd: u32,
direct_uv: vec2<f32>,
direct_tex_size: vec2<f32>,
presampled: IndirectSamples,
@@ -120,72 +120,76 @@ fn apply_indirect(
result.uv = direct_uv;
result.bump_alpha = 0.0;
- let cmd = read_packed(frame.tev_indirect, stage);
- let source_stage = cmd & 3u;
- let format = (cmd >> 2u) & 3u;
- let bias_s = (cmd >> 4u) & 1u;
- let bias_t = (cmd >> 5u) & 1u;
- let bias_u = (cmd >> 6u) & 1u;
- let bump_source = (cmd >> 7u) & 3u;
- let matrix_index = (cmd >> 9u) & 3u;
- let matrix_mode = (cmd >> 11u) & 3u;
- let wrap_s = (cmd >> 13u) & 7u;
- let wrap_t = (cmd >> 16u) & 7u;
- let add_previous = ((cmd >> 20u) & 1u) != 0u;
- // TODO: bit 19?
-
- let sample = presampled.samples[source_stage];
-
- if bump_source != 0u {
- var raw_byte: f32 = 0.0;
- switch bump_source {
- case 1u: { raw_byte = sample.x; }
- case 2u: { raw_byte = sample.y; }
- case 3u: { raw_byte = sample.z; }
- default: {}
- }
- let byte_u8 = u32(floor(raw_byte * 255.0));
- result.bump_alpha = f32(ind_bump_alpha_byte(byte_u8, format)) / 255.0;
- }
-
- let has_offset = matrix_index != 0u || wrap_s != 0u || wrap_t != 0u || add_previous;
- if !has_offset {
+ @if(!IND_STAGE_0_ENABLED) {
return result;
}
- let quantized = ind_quantize_and_bias(sample, format, bias_s, bias_t, bias_u);
-
- var offset_uv = vec2<f32>(0.0);
- if matrix_index != 0u {
- let idx = matrix_index - 1u;
- let row0 = vec3<f32>(frame.indirect_matrices[idx * 2u].xyz) * (1.0 / 1024.0);
- let row1 = vec3<f32>(frame.indirect_matrices[idx * 2u + 1u].xyz) * (1.0 / 1024.0);
- let scale_exp = frame.indirect_matrices[idx * 2u].w;
- let inv_scale = exp2(-f32(scale_exp));
- switch matrix_mode {
- case 0u: {
- offset_uv = vec2<f32>(dot(row0, quantized), dot(row1, quantized))
- * inv_scale
- / direct_tex_size;
- }
- case 1u: {
- offset_uv = direct_uv * (quantized.x * (1.0 / 256.0) * inv_scale);
+ @if(IND_STAGE_0_ENABLED) {
+ let source_stage = cmd & 3u;
+ let format = (cmd >> 2u) & 3u;
+ let bias_s = (cmd >> 4u) & 1u;
+ let bias_t = (cmd >> 5u) & 1u;
+ let bias_u = (cmd >> 6u) & 1u;
+ let bump_source = (cmd >> 7u) & 3u;
+ let matrix_index = (cmd >> 9u) & 3u;
+ let matrix_mode = (cmd >> 11u) & 3u;
+ let wrap_s = (cmd >> 13u) & 7u;
+ let wrap_t = (cmd >> 16u) & 7u;
+ let add_previous = ((cmd >> 20u) & 1u) != 0u;
+
+ let sample = presampled.samples[source_stage];
+
+ if bump_source != 0u {
+ var raw_byte: f32 = 0.0;
+ switch bump_source {
+ case 1u: { raw_byte = sample.x; }
+ case 2u: { raw_byte = sample.y; }
+ case 3u: { raw_byte = sample.z; }
+ default: {}
}
- case 2u: {
- offset_uv = direct_uv * (quantized.y * (1.0 / 256.0) * inv_scale);
+ let byte_u8 = u32(floor(raw_byte * 255.0));
+ result.bump_alpha = f32(ind_bump_alpha_byte(byte_u8, format)) / 255.0;
+ }
+
+ let has_offset = matrix_index != 0u || wrap_s != 0u || wrap_t != 0u || add_previous;
+ if !has_offset {
+ return result;
+ }
+
+ let quantized = ind_quantize_and_bias(sample, format, bias_s, bias_t, bias_u);
+
+ var offset_uv = vec2<f32>(0.0);
+ if matrix_index != 0u {
+ let idx = matrix_index - 1u;
+ let row0 = vec3<f32>(frame.indirect_matrices[idx * 2u].xyz) * (1.0 / 1024.0);
+ let row1 = vec3<f32>(frame.indirect_matrices[idx * 2u + 1u].xyz) * (1.0 / 1024.0);
+ let scale_exp = frame.indirect_matrices[idx * 2u].w;
+ let inv_scale = exp2(-f32(scale_exp));
+ switch matrix_mode {
+ case 0u: {
+ offset_uv = vec2<f32>(dot(row0, quantized), dot(row1, quantized))
+ * inv_scale
+ / direct_tex_size;
+ }
+ case 1u: {
+ offset_uv = direct_uv * (quantized.x * (1.0 / 256.0) * inv_scale);
+ }
+ case 2u: {
+ offset_uv = direct_uv * (quantized.y * (1.0 / 256.0) * inv_scale);
+ }
+ default: {}
}
- default: {}
}
- }
- let wrapped = vec2<f32>(
- ind_wrap_axis(direct_uv.x, wrap_s, direct_tex_size.x),
- ind_wrap_axis(direct_uv.y, wrap_t, direct_tex_size.y),
- );
- var final_uv = wrapped + offset_uv;
- if add_previous {
- final_uv = final_uv + previous_uv;
+ let wrapped = vec2<f32>(
+ ind_wrap_axis(direct_uv.x, wrap_s, direct_tex_size.x),
+ ind_wrap_axis(direct_uv.y, wrap_t, direct_tex_size.y),
+ );
+ var final_uv = wrapped + offset_uv;
+ if add_previous {
+ final_uv = final_uv + previous_uv;
+ }
+ result.uv = final_uv;
+ return result;
}
- result.uv = final_uv;
- return result;
}
diff --git a/crates/gecko/src/flipper/gx/vertex.rs b/crates/gecko/src/flipper/gx/vertex.rs
index b096936..4c2d91d 100644
--- a/crates/gecko/src/flipper/gx/vertex.rs
+++ b/crates/gecko/src/flipper/gx/vertex.rs
@@ -82,10 +82,11 @@ impl GraphicsProcessor {
}
}
let mut boxed = self.draw_box_pool.pop().unwrap_or_default();
- std::mem::swap(&mut boxed.vertices, &mut self.draw_vertices_scratch);
self.draw_vertices_scratch.clear();
- self.draw_vertices_scratch.reserve(vertex_count);
+ if self.draw_vertices_scratch.capacity() < vertex_count {
+ self.draw_vertices_scratch.reserve(vertex_count);
+ }
self::dispatch_decode(self, mmio, cmd, data, vertex_count, &vf);
@@ -101,7 +102,8 @@ impl GraphicsProcessor {
self.konst_dirty = false;
}
- let draw_vertices = std::mem::take(&mut self.draw_vertices_scratch);
+ boxed.vertices.clear();
+ boxed.vertices.append(&mut self.draw_vertices_scratch);
// Flatten the three indirect matrices to 6 rows. Row 2M is row
// 0 of matrix M, row 2M+1 is row 1. The .w lane carries the
@@ -133,7 +135,6 @@ impl GraphicsProcessor {
];
boxed.primitive = primitive;
- boxed.vertices = draw_vertices;
boxed.modelview = modelview.0;
boxed.tev_color_env = std::array::from_fn(|i| self.cur_tev_color_env[i].raw());
boxed.tev_alpha_env = std::array::from_fn(|i| self.cur_tev_alpha_env[i].raw());
@@ -725,9 +726,6 @@ fn dispatch_decode<const SYSTEM: SystemId>(
};
if let Some(parser) = parser {
- gp.draw_vertices_scratch.clear();
- gp.draw_vertices_scratch.reserve(vertex_count);
-
let gp_raw = gp as *mut GraphicsProcessor as *mut std::ffi::c_void;
let xf_mem_ptr = gp.xf_mem.as_ptr();
let arrays_ptr = gp.jit_vtx_arrays.0.as_ptr();
diff --git a/crates/tinyapp/src/app.rs b/crates/tinyapp/src/app.rs
index 6da233f..a77ac3d 100644
--- a/crates/tinyapp/src/app.rs
+++ b/crates/tinyapp/src/app.rs
@@ -90,7 +90,11 @@ impl State {
let view = frame.texture.create_view(&Default::default());
#[cfg(feature = "fps-counter")]
- let (fps, native_pct) = if waiting { (0.0, 0.0) } else { fps::read(&self.fps_shared) };
+ let (fps, native_pct) = if waiting {
+ (0.0, 0.0)
+ } else {
+ fps::read(&self.fps_shared)
+ };
#[cfg(not(feature = "fps-counter"))]
let fps = if waiting {
0.0