| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- //! Translated shaders kept on disk (decision 0003): under the configured
- //! cache folder, keyed by the container's content and by the translator
- //! (the BLAKE3 of its sources) and its settings, so a changed translator
- //! never serves an old translation. Entries go through
- //! `victory-pipeline`'s content-addressed cache, which writes them
- //! atomically; an entry that does not read back is translated again.
- //!
- //! The entry is our own encoding of [`HostShader`]: a field added to it
- //! must be added to `encode` and `decode` too (entries of older builds
- //! are ignored anyway).
- use std::path::PathBuf;
- use std::sync::OnceLock;
- use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
- use victory_pipeline::{Cache, Converter};
- use victory_shader::emit::{HostShader, StaticSampler, UserData};
- use victory_shader::resource::RootIndexedView;
- use victory_shader::{HwStage, RootType};
- static CACHE: OnceLock<Cache> = OnceLock::new();
- static HITS: AtomicU64 = AtomicU64::new(0);
- static MISSES: AtomicU64 = AtomicU64::new(0);
- /// Keeps translated shaders in `dir` from now on.
- pub fn use_shader_cache(dir: PathBuf) {
- match Cache::open(dir) {
- Ok(cache) => {
- let _ = CACHE.set(cache);
- }
- Err(e) => tracing::warn!(error = %e, "no shader cache"),
- }
- }
- /// Shaders found in the cache and translated since the start.
- pub(crate) fn counts() -> (u64, u64) {
- (HITS.load(Ordering::Relaxed), MISSES.load(Ordering::Relaxed))
- }
- /// What identifies a translation beside its container: the translator
- /// (the BLAKE3 of its sources, `build.rs`), the settings it runs with, and
- /// what it was told of the host. Not our executable: a rebuild of anything
- /// else used to translate every shader again.
- fn build() -> [u8; 32] {
- // The view heap's null slot differs by host GPU: a translation cached
- // without the clamp must not be served (`host_settings` below).
- crate::descriptor::host::probe_view_limit();
- static FIXED: OnceLock<Vec<u8>> = OnceLock::new();
- let fixed = FIXED.get_or_init(|| {
- let limit = victory_shader::emit::loop_limit();
- if limit != victory_shader::emit::DEFAULT_LOOP_LIMIT {
- tracing::warn!(
- limit,
- "VICTORY_SHADER_LOOP_LIMIT: translated loops stop sooner"
- );
- }
- let mut bytes = env!("VICTORY_TRANSLATOR").as_bytes().to_vec();
- bytes.extend_from_slice(&limit.to_le_bytes());
- // Which group barriers wait is part of the translation too.
- bytes.push(victory_shader::emit::group_sync());
- // So are the switches that pick the form of a wave64 program:
- // without them here a run with `VICTORY_WAVE_WIDE=0` was
- // served the translations an earlier run of the same
- // executable had cached with the switch unset.
- for name in [
- "VICTORY_WAVE_WIDE",
- "VICTORY_WAVE_DEEP_SKIPS",
- "VICTORY_WAVE_OWN_BITS",
- "VICTORY_WAVE_LOCAL",
- "VICTORY_F16_NEAREST",
- "VICTORY_HELPER_OWN",
- "VICTORY_FIRSTLANE_OWN",
- "VICTORY_FIRST_HALF",
- "VICTORY_STRICT_JOINS",
- "VICTORY_MESH_OUTPUT_STRICT",
- ] {
- bytes.extend_from_slice(name.as_bytes());
- bytes.push(b'=');
- if let Some(value) = std::env::var_os(name) {
- bytes.extend_from_slice(value.as_encoded_bytes());
- }
- bytes.push(0);
- }
- // And which clean-up passes ran over the translated code.
- bytes.extend_from_slice(&victory_shader::emit::shader_opt().to_le_bytes());
- bytes
- });
- let mut hash = blake3::Hasher::new();
- hash.update(fixed);
- for value in victory_shader::emit::host_settings() {
- hash.update(&value.to_le_bytes());
- }
- *hash.finalize().as_bytes()
- }
- /// Puts the translation of `container` into the cache before any title asks
- /// for it (`victory shaders`): whether it was there already, or why it does
- /// not translate. The host is opened first, as a run does: what the card
- /// can do decides the form of some programs.
- pub fn precompile(container: &[u8]) -> Result<bool, String> {
- let _ = crate::host::host();
- let misses = MISSES.load(Ordering::Relaxed);
- host_shader(container)?;
- // (Counts other threads' misses too: the caller only adds them up.)
- Ok(MISSES.load(Ordering::Relaxed) == misses)
- }
- /// The translation of a console shader container, from the cache when
- /// there is one.
- pub(crate) fn host_shader(container: &[u8]) -> Result<HostShader, String> {
- let translate = |c: &[u8]| victory_shader::emit::translate(c).map_err(|e| e.to_string());
- match CACHE.get() {
- Some(cache) => cached(cache, &build(), container, translate),
- None => translate(container),
- }
- }
- /// The cache entry's converter: the build and the container in, the
- /// encoded translation out. `ran` tells a miss from a hit.
- struct Translate<F> {
- translate: F,
- ran: AtomicBool,
- }
- impl<F: Fn(&[u8]) -> Result<HostShader, String> + Send + Sync> Converter for Translate<F> {
- fn id(&self) -> &'static str {
- "shader.dxil"
- }
- fn version(&self) -> u32 {
- // The build fingerprint in the input does the versioning.
- 1
- }
- fn convert(&self, input: &[u8]) -> anyhow::Result<Vec<u8>> {
- self.ran.store(true, Ordering::Relaxed);
- let shader = (self.translate)(&input[32..]).map_err(anyhow::Error::msg)?;
- Ok(encode(&shader))
- }
- }
- fn cached(
- cache: &Cache,
- build: &[u8; 32],
- container: &[u8],
- translate: impl Fn(&[u8]) -> Result<HostShader, String> + Send + Sync,
- ) -> Result<HostShader, String> {
- let input = [&build[..], container].concat();
- let converter = Translate {
- translate,
- ran: AtomicBool::new(false),
- };
- for _ in 0..2 {
- let path = cache
- .get_or_convert(&converter, &input)
- .map_err(|e| format!("{e:#}"))?;
- if let Some(shader) = std::fs::read(&path).ok().and_then(|b| decode(&b)) {
- let (counter, what) = if converter.ran.load(Ordering::Relaxed) {
- (&MISSES, "translated and cached")
- } else {
- (&HITS, "from the cache")
- };
- counter.fetch_add(1, Ordering::Relaxed);
- tracing::debug!(path = %path.display(), "shader {what}");
- return Ok(shader);
- }
- tracing::debug!(path = %path.display(), "unreadable shader cache entry; translating again");
- let _ = std::fs::remove_file(&path);
- }
- MISSES.fetch_add(1, Ordering::Relaxed);
- (converter.translate)(container)
- }
- const MAGIC: &[u8; 4] = b"VSH1";
- const STAGES: [HwStage; 8] = [
- HwStage::Ls,
- HwStage::Hs,
- HwStage::Es,
- HwStage::Gs,
- HwStage::Vs,
- HwStage::Ps,
- HwStage::Ms,
- HwStage::Cs,
- ];
- fn root_code(r: RootType) -> u32 {
- match r {
- RootType::Constants => 0,
- RootType::Descriptor => 1,
- RootType::Table => 2,
- RootType::ImmediateSampler => 3,
- RootType::ResourceHeap => 4,
- RootType::SamplerHeap => 5,
- RootType::Other(x) => 6 + x,
- }
- }
- fn root_type(code: u32) -> RootType {
- match code {
- 0 => RootType::Constants,
- 1 => RootType::Descriptor,
- 2 => RootType::Table,
- 3 => RootType::ImmediateSampler,
- 4 => RootType::ResourceHeap,
- 5 => RootType::SamplerHeap,
- x => RootType::Other(x - 6),
- }
- }
- /// Little-endian words and length-prefixed lists.
- fn encode(s: &HostShader) -> Vec<u8> {
- let mut out = MAGIC.to_vec();
- let word = |out: &mut Vec<u8>, v: u32| out.extend(v.to_le_bytes());
- let stage = STAGES.iter().position(|&x| x == s.stage).unwrap_or(0) as u32;
- word(&mut out, stage);
- word(&mut out, s.dxil.len() as u32);
- out.extend(&s.dxil);
- word(&mut out, s.user_data.len() as u32);
- for u in &s.user_data {
- for v in [u.index, u32::from(u.slot), root_code(u.root), u.dword] {
- word(&mut out, v);
- }
- }
- word(&mut out, s.samplers.len() as u32);
- for sampler in &s.samplers {
- for &v in sampler {
- word(&mut out, v);
- }
- }
- word(&mut out, s.static_samplers.len() as u32);
- for t in &s.static_samplers {
- let [u, v, w] = t.address;
- let words = [
- t.filter,
- u,
- v,
- w,
- t.mip_lod_bias.to_bits(),
- t.max_anisotropy,
- t.comparison,
- t.border,
- t.min_lod.to_bits(),
- t.max_lod.to_bits(),
- ];
- for v in words {
- word(&mut out, v);
- }
- }
- for slots in [&s.root_cbvs, &s.root_srvs, &s.root_uavs] {
- word(&mut out, slots.len() as u32);
- out.extend(slots);
- }
- word(&mut out, s.root_indexed_views.len() as u32);
- for view in &s.root_indexed_views {
- for value in [
- u32::from(view.table_slot),
- u32::from(view.constant_slot),
- view.constant_dword,
- view.byte_offset,
- u32::from(view.shift),
- ] {
- word(&mut out, value);
- }
- }
- out
- }
- fn decode(bytes: &[u8]) -> Option<HostShader> {
- struct Reader<'a>(&'a [u8]);
- impl<'a> Reader<'a> {
- fn take(&mut self, n: usize) -> Option<&'a [u8]> {
- let (head, rest) = self.0.split_at_checked(n)?;
- self.0 = rest;
- Some(head)
- }
- fn word(&mut self) -> Option<u32> {
- Some(u32::from_le_bytes(self.take(4)?.try_into().ok()?))
- }
- fn list(&mut self) -> Option<Vec<u8>> {
- let n = self.word()? as usize;
- Some(self.take(n)?.to_vec())
- }
- }
- let mut r = Reader(bytes);
- if r.take(4)? != MAGIC {
- return None;
- }
- let stage = *STAGES.get(r.word()? as usize)?;
- let dxil = r.list()?;
- let n = r.word()? as usize;
- let mut user_data = Vec::with_capacity(n.min(256));
- for _ in 0..n {
- user_data.push(UserData {
- index: r.word()?,
- slot: u8::try_from(r.word()?).ok()?,
- root: root_type(r.word()?),
- dword: r.word()?,
- });
- }
- let n = r.word()? as usize;
- let mut samplers = Vec::with_capacity(n.min(256));
- for _ in 0..n {
- samplers.push([r.word()?, r.word()?, r.word()?, r.word()?]);
- }
- let n = r.word()? as usize;
- let mut static_samplers = Vec::with_capacity(n.min(256));
- for _ in 0..n {
- static_samplers.push(StaticSampler {
- filter: r.word()?,
- address: [r.word()?, r.word()?, r.word()?],
- mip_lod_bias: f32::from_bits(r.word()?),
- max_anisotropy: r.word()?,
- comparison: r.word()?,
- border: r.word()?,
- min_lod: f32::from_bits(r.word()?),
- max_lod: f32::from_bits(r.word()?),
- });
- }
- let root_cbvs = r.list()?;
- let root_srvs = r.list()?;
- let root_uavs = r.list()?;
- let n = r.word()? as usize;
- let mut root_indexed_views = Vec::with_capacity(n.min(256));
- for _ in 0..n {
- root_indexed_views.push(RootIndexedView {
- table_slot: u8::try_from(r.word()?).ok()?,
- constant_slot: u8::try_from(r.word()?).ok()?,
- constant_dword: r.word()?,
- byte_offset: r.word()?,
- shift: u8::try_from(r.word()?).ok().filter(|&s| s < 32)?,
- });
- }
- r.0.is_empty().then_some(HostShader {
- stage,
- dxil,
- user_data,
- samplers,
- static_samplers,
- root_cbvs,
- root_srvs,
- root_uavs,
- root_indexed_views,
- })
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- fn shader() -> HostShader {
- HostShader {
- stage: HwStage::Ps,
- dxil: b"DXBC and the rest".to_vec(),
- user_data: vec![UserData {
- index: 8,
- slot: 1,
- root: RootType::Table,
- dword: 0,
- }],
- samplers: vec![[0x400, 0xc0_0000, 0x18f0_0000, 0]],
- static_samplers: vec![StaticSampler::from_words([
- 0x400,
- 0xc0_0000,
- 0x18f0_0000,
- 0,
- ])],
- root_cbvs: vec![2],
- root_srvs: vec![0, 3],
- root_uavs: vec![1],
- root_indexed_views: vec![RootIndexedView {
- table_slot: 1,
- constant_slot: 2,
- constant_dword: 3,
- byte_offset: 32,
- shift: 5,
- }],
- }
- }
- #[test]
- fn translations_come_back_from_the_cache() {
- let dir = tempfile::tempdir().unwrap();
- let cache = Cache::open(dir.path()).unwrap();
- let build = [7u8; 32];
- let container = b"a console container".as_slice();
- let runs = AtomicU64::new(0);
- let translate = |_: &[u8]| {
- runs.fetch_add(1, Ordering::Relaxed);
- Ok(shader())
- };
- assert_eq!(cached(&cache, &build, container, translate), Ok(shader()));
- assert_eq!(cached(&cache, &build, container, translate), Ok(shader()));
- assert_eq!(runs.load(Ordering::Relaxed), 1, "the second came from disk");
- // Another build translates again.
- assert_eq!(cached(&cache, &[8; 32], container, translate), Ok(shader()));
- assert_eq!(runs.load(Ordering::Relaxed), 2);
- // A damaged entry is a miss, translated and stored again.
- for e in walk(dir.path()) {
- std::fs::write(e, b"VSH1 broken").unwrap();
- }
- assert_eq!(cached(&cache, &build, container, translate), Ok(shader()));
- assert_eq!(runs.load(Ordering::Relaxed), 3);
- assert_eq!(cached(&cache, &build, container, translate), Ok(shader()));
- assert_eq!(runs.load(Ordering::Relaxed), 3);
- // Failures are not stored.
- let fail = |_: &[u8]| Err::<HostShader, _>("unsupported".to_owned());
- assert!(cached(&cache, &build, b"other", fail).is_err());
- }
- fn walk(dir: &std::path::Path) -> Vec<PathBuf> {
- let mut files = Vec::new();
- for e in std::fs::read_dir(dir).unwrap().flatten() {
- if e.path().is_dir() {
- files.extend(walk(&e.path()));
- } else {
- files.push(e.path());
- }
- }
- files
- }
- #[test]
- fn entries_round_trip_and_reject_damage() {
- let bytes = encode(&shader());
- assert_eq!(decode(&bytes), Some(shader()));
- assert_eq!(decode(&bytes[..bytes.len() - 1]), None);
- assert_eq!(decode(&[bytes.as_slice(), &[0]].concat()), None);
- assert_eq!(decode(b"VSH0"), None);
- }
- }
|