Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

fix pipeline explosion

Layle62831aa03301
diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rs
index 331c6f4..1044219 100644
--- a/crates/backend-wgpu/src/pipeline.rs
+++ b/crates/backend-wgpu/src/pipeline.rs
@@ -62,7 +62,7 @@ pub(crate) struct FullPipelineKey {
pub(crate) const FULL_PIPELINE_KEY_BYTES: usize = SHADER_KEY_BYTES + PipelineKey::BYTES;
const PIPELINE_CACHE_MAGIC: [u8; 4] = *b"GPKC";
-const PIPELINE_CACHE_VERSION: u32 = 1;
+const PIPELINE_CACHE_VERSION: u32 = crate::shader_specialization::CACHE_VERSION;
pub(crate) const PIPELINE_CACHE_PATH: &str = "cache/pipeline_keys.bin";
impl FullPipelineKey {
diff --git a/crates/backend-wgpu/src/shader_specialization.rs b/crates/backend-wgpu/src/shader_specialization.rs
index 1c3a715..cfe5c0f 100644
--- a/crates/backend-wgpu/src/shader_specialization.rs
+++ b/crates/backend-wgpu/src/shader_specialization.rs
@@ -1,6 +1,5 @@
use gecko::flipper::gx::regs::{AlphaCompare, AlphaOp, CompareFunc};
use gecko::host::DrawData;
-use std::fmt::Write;
use std::fs::File;
use std::io::{BufWriter, Read, Write as IoWrite};
use std::path::Path;
@@ -14,13 +13,11 @@ 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,
-}
+pub(crate) const KEY_BYTES: usize = 5;
+const CACHE_MAGIC: [u8; 4] = *b"GSKC";
+pub(crate) const CACHE_VERSION: u32 = 2;
+pub(crate) const SHADER_CACHE_PATH: &str = "cache/shader_keys.bin";
+
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub(crate) struct ShaderKey {
@@ -29,7 +26,6 @@ pub(crate) struct ShaderKey {
pub has_lighting_c0: bool,
pub has_lighting_c1: bool,
pub alpha_test_enabled: bool,
- pub stages: [StageKey; 16],
}
impl ShaderKey {
@@ -44,23 +40,12 @@ impl ShaderKey {
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_c0,
has_lighting_c1,
alpha_test_enabled: !always_pass,
- stages,
}
}
}
@@ -73,65 +58,28 @@ 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) const KEY_BYTES: usize = 5 + 16 * 16;
-const CACHE_MAGIC: [u8; 4] = *b"GSKC";
-const CACHE_VERSION: u32 = 1;
-pub(crate) const SHADER_CACHE_PATH: &str = "cache/shader_keys.bin";
-
impl ShaderKey {
pub(crate) fn to_bytes(&self) -> [u8; KEY_BYTES] {
- let mut out = [0u8; KEY_BYTES];
- out[0] = self.num_tev_stages;
- out[1] = self.num_indirect_stages;
- out[2] = self.has_lighting_c0 as u8;
- out[3] = self.has_lighting_c1 as u8;
- out[4] = self.alpha_test_enabled as u8;
-
- for (i, s) in self.stages.iter().enumerate() {
- let off = 5 + i * 16;
- out[off..off + 4].copy_from_slice(&s.order.to_le_bytes());
- out[off + 4..off + 8].copy_from_slice(&s.color_env.to_le_bytes());
- out[off + 8..off + 12].copy_from_slice(&s.alpha_env.to_le_bytes());
- out[off + 12..off + 16].copy_from_slice(&s.ind_cmd.to_le_bytes());
- }
-
- out
+ [
+ self.num_tev_stages,
+ self.num_indirect_stages,
+ self.has_lighting_c0 as u8,
+ self.has_lighting_c1 as u8,
+ self.alpha_test_enabled as u8,
+ ]
}
pub(crate) fn from_bytes(b: &[u8; KEY_BYTES]) -> Self {
- let mut stages = [StageKey::default(); 16];
- for (i, s) in stages.iter_mut().enumerate() {
- let off = 5 + i * 16;
- *s = StageKey {
- order: u32::from_le_bytes(b[off..off + 4].try_into().unwrap()),
- color_env: u32::from_le_bytes(b[off + 4..off + 8].try_into().unwrap()),
- alpha_env: u32::from_le_bytes(b[off + 8..off + 12].try_into().unwrap()),
- ind_cmd: u32::from_le_bytes(b[off + 12..off + 16].try_into().unwrap()),
- };
- }
-
Self {
num_tev_stages: b[0],
num_indirect_stages: b[1],
has_lighting_c0: b[2] != 0,
has_lighting_c1: b[3] != 0,
alpha_test_enabled: b[4] != 0,
- stages,
}
}
}
@@ -181,10 +129,7 @@ pub(crate) fn save_keys(path: &Path, keys: &[ShaderKey]) -> std::io::Result<()>
}
pub(crate) fn compile_variant(key: ShaderKey) -> String {
- let mut resolver = make_resolver();
- let main_src = generate_main(&key);
- resolver.add_module("package::main".parse().unwrap(), main_src.into());
-
+ let resolver = make_resolver();
let mut compiler = Wesl::new("").set_custom_resolver(resolver);
for i in 1..=16u8 {
diff --git a/crates/backend-wgpu/src/shaders/main.wesl b/crates/backend-wgpu/src/shaders/main.wesl
index 8f7813e..2ae4cd4 100644
--- a/crates/backend-wgpu/src/shaders/main.wesl
+++ b/crates/backend-wgpu/src/shaders/main.wesl
@@ -5,8 +5,6 @@ import super::tev_combiners::{tev_combine_color, tev_combine_alpha};
import super::alpha_test::{alpha_compare, alpha_combine};
import super::lighting::{compute_lighting_c0, compute_lighting_c1};
-// __STAGE_CONSTS__
-
@vertex
fn vs_main(in: VsIn) -> VsOut {
var out: VsOut;
@@ -42,15 +40,18 @@ 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 row = stage / 4u;
+ let col = stage % 4u;
+ let order = frame.tev_orders[row][col];
+ let cenv = frame.tev_color_env[row][col];
+ let aenv = frame.tev_alpha_env[row][col];
+ let ind_cmd = frame.tev_indirect[row][col];
+
let texmap_idx = order & 7u;
let texcoord_idx = (order >> 3u) & 7u;
let tex_enabled = ((order >> 6u) & 1u) != 0u;
@@ -140,22 +141,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, 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); }
+ @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); }
let color = vec4<f32>(state.regs[state.last_color_dest].rgb, state.regs[state.last_alpha_dest].a);