Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

shader warm up, cache

Layle632efdce8f89
diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rs
index a024389..ea0f383 100644
--- a/crates/backend-wgpu/src/lib.rs
+++ b/crates/backend-wgpu/src/lib.rs
@@ -617,9 +617,19 @@ impl GxRenderer {
EFB_SAMPLE_COUNT,
);
+ let cache_path = std::path::Path::new(shader_specialization::SHADER_CACHE_PATH);
+ let cached_keys = shader_specialization::load_cached_keys(cache_path);
+ let prewarmed = prewarm_shader_variants(device, &cached_keys);
+
+ let mut shader_cache: FxHashMap<ShaderKey, wgpu::ShaderModule> =
+ FxHashMap::with_capacity_and_hasher(prewarmed.len().max(64), Default::default());
+ for (k, m) in prewarmed {
+ shader_cache.insert(k, m);
+ }
+
GxRenderer {
pipeline_cache: FxHashMap::default(),
- shader_cache: FxHashMap::default(),
+ shader_cache,
pipeline_layout,
surface_format,
bind_group_layout,
@@ -704,4 +714,57 @@ impl GxRenderer {
pub fn set_efb_writeback_tx(&mut self, tx: crossbeam_channel::Sender<EfbWriteback>) {
self.efb_writeback_tx = Some(tx);
}
+
+ pub fn save_shader_cache(&self) -> std::io::Result<usize> {
+ let path = std::path::Path::new(shader_specialization::SHADER_CACHE_PATH);
+ let keys: Vec<ShaderKey> = self.shader_cache.keys().copied().collect();
+ shader_specialization::save_keys(path, &keys)?;
+ Ok(keys.len())
+ }
+}
+
+fn prewarm_shader_variants(device: &wgpu::Device, keys: &[ShaderKey]) -> Vec<(ShaderKey, wgpu::ShaderModule)> {
+ if keys.is_empty() {
+ return Vec::new();
+ }
+ let t0 = std::time::Instant::now();
+ let num_threads = std::thread::available_parallelism()
+ .map(|n| n.get())
+ .unwrap_or(4)
+ .min(keys.len());
+
+ let chunk_size = keys.len().div_ceil(num_threads);
+ let compiled: Vec<(ShaderKey, String)> = std::thread::scope(|s| {
+ let handles: Vec<_> = keys
+ .chunks(chunk_size)
+ .map(|chunk| {
+ s.spawn(move || {
+ chunk
+ .iter()
+ .map(|&k| (k, shader_specialization::compile_variant(k)))
+ .collect::<Vec<_>>()
+ })
+ })
+ .collect();
+ handles.into_iter().flat_map(|h| h.join().unwrap()).collect()
+ });
+
+ let modules: Vec<(ShaderKey, wgpu::ShaderModule)> = compiled
+ .into_iter()
+ .map(|(key, wgsl)| {
+ let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
+ label: Some(&format!("gx_shader_{key:?}")),
+ source: wgpu::ShaderSource::Wgsl(wgsl.into()),
+ });
+ (key, module)
+ })
+ .collect();
+
+ tracing::info!(
+ num_variants = modules.len(),
+ elapsed_ms = t0.elapsed().as_millis() as u64,
+ "prewarmed shader variants from cache"
+ );
+
+ modules
}
diff --git a/crates/backend-wgpu/src/shader_specialization.rs b/crates/backend-wgpu/src/shader_specialization.rs
index d425a24..a29d4f5 100644
--- a/crates/backend-wgpu/src/shader_specialization.rs
+++ b/crates/backend-wgpu/src/shader_specialization.rs
@@ -1,6 +1,9 @@
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;
use wesl::{VirtualResolver, Wesl};
const COMMON_WESL: &str = include_str!("shaders/common.wesl");
@@ -83,20 +86,117 @@ fn generate_main(key: &ShaderKey) -> String {
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
+ }
+
+ 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,
+ }
+ }
+}
+
+pub(crate) fn load_cached_keys(path: &Path) -> Vec<ShaderKey> {
+ let mut f = match File::open(path) {
+ Ok(f) => f,
+ Err(_) => return Vec::new(),
+ };
+
+ let mut header = [0u8; 8];
+ if f.read_exact(&mut header).is_err() {
+ return Vec::new();
+ }
+
+ if header[..4] != CACHE_MAGIC {
+ return Vec::new();
+ }
+
+ let version = u32::from_le_bytes(header[4..8].try_into().unwrap());
+ if version != CACHE_VERSION {
+ return Vec::new();
+ }
+
+ let mut keys = Vec::new();
+ let mut buf = [0u8; KEY_BYTES];
+ while f.read_exact(&mut buf).is_ok() {
+ keys.push(ShaderKey::from_bytes(&buf));
+ }
+
+ keys
+}
+
+pub(crate) fn save_keys(path: &Path, keys: &[ShaderKey]) -> std::io::Result<()> {
+ if let Some(parent) = path.parent() {
+ std::fs::create_dir_all(parent)?;
+ }
+ let f = File::create(path)?;
+ let mut w = BufWriter::new(f);
+ w.write_all(&CACHE_MAGIC)?;
+ w.write_all(&CACHE_VERSION.to_le_bytes())?;
+ for k in keys {
+ w.write_all(&k.to_bytes())?;
+ }
+ w.flush()?;
+ Ok(())
+}
+
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 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_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
.compile(&entry)
diff --git a/crates/backend-wgpu/src/sink.rs b/crates/backend-wgpu/src/sink.rs
index 949cb92..f1a7575 100644
--- a/crates/backend-wgpu/src/sink.rs
+++ b/crates/backend-wgpu/src/sink.rs
@@ -121,6 +121,11 @@ fn worker(
// a fresh batch on its next flush.
let _ = recyclers.batches.try_send(batch);
}
+
+ match gx.save_shader_cache() {
+ Ok(n) => tracing::info!(num_variants = n, "saved shader cache"),
+ Err(err) => tracing::warn!(?err, "failed to save shader cache"),
+ }
}
#[cfg(feature = "renderdoc-capture")]