Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/xcl/src/eh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ unsafe extern "system" fn hk_nt_terminate_process(h: isize, status: i32) -> i32
crate::xlog_info!("process ends (thread {tid}); on the stack: {stack}");
}
crate::log::flush();
crate::hle::d3d12x::pso_async::flush_used_bounded(std::time::Duration::from_millis(100));
}
let orig: unsafe extern "system" fn(isize, i32) -> i32 = std::mem::transmute(ORIG_NT_TERMINATE.load(Ordering::Acquire));
orig(h, status)
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod standin;
pub mod title_list;
pub mod title_textures;
mod uma_driver;
mod used_journal;
pub mod vram;

use std::ffi::c_void;
Expand Down
45 changes: 28 additions & 17 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,17 @@ pub fn earlier_used_keys() -> std::collections::HashSet<u64> {
used::parse(&std::fs::read_to_string(crate::gpu::recomp::dxc::cache_dir().join("used-pipelines.txt")).unwrap_or_default())
}

/// Ask the used-pipeline writer to finish without ever joining it unboundedly.
pub fn shutdown_used() {
used::shutdown(std::time::Duration::from_millis(500));
}

/// Best effort at process termination: only the one writer touches the append handle, and a
/// loader-lock-blocked writer cannot hold termination longer than `timeout`.
pub fn flush_used_bounded(timeout: std::time::Duration) {
used::shutdown(timeout);
}

/// The pipelines a title used, by a hash of their shaders, kept between runs
/// (`used-pipelines.txt` beside the DXIL cache, a key a line).
///
Expand All @@ -806,16 +817,21 @@ mod used {
use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::sync::{Mutex, OnceLock, RwLock};
use std::time::Duration;

use super::super::used_journal::Journal;

struct Used {
earlier: RwLock<HashMap<u64, usize>>,
now: Mutex<(HashSet<u64>, Option<std::fs::File>)>,
now: Journal,
/// the shaders of each set made in this run, by the set's key; the shaders of the sets
/// used so far and `used-shaders.txt`, where they are kept for the next run
parts: Mutex<std::collections::HashMap<u64, Vec<u64>>>,
shaders: Mutex<(HashSet<u64>, Option<std::fs::File>)>,
}

static U: OnceLock<Used> = OnceLock::new();

/// The keys in a `used-pipelines.txt` (lines that are not a hexadecimal key are passed over).
pub fn parse(text: &str) -> HashSet<u64> {
parse_ranked(text).into_keys().collect()
Expand All @@ -832,13 +848,12 @@ mod used {
}

fn get() -> &'static Used {
static U: OnceLock<Used> = OnceLock::new();
U.get_or_init(|| {
// (a test neither reads nor writes the user's file)
if cfg!(test) {
return Used {
earlier: RwLock::new(HashMap::new()),
now: Mutex::new((HashSet::new(), None)),
now: Journal::memory(HashSet::new()),
parts: Default::default(),
shaders: Mutex::new((HashSet::new(), None)),
};
Expand All @@ -848,22 +863,25 @@ mod used {
if !earlier.is_empty() {
crate::xlog_info!("D3D12.X: the pipelines of {} shader sets an earlier run used are built ahead of their first use", earlier.len());
}
if let Some(dir) = path.parent() {
let _ = std::fs::create_dir_all(dir);
}
let file = std::fs::OpenOptions::new().create(true).append(true).open(&path).ok();
let now = Journal::file(path.clone(), earlier.keys().copied().collect());
let shaders_path = path.with_file_name("used-shaders.txt");
let shaders = parse(&std::fs::read_to_string(&shaders_path).unwrap_or_default());
let shaders_file = std::fs::OpenOptions::new().create(true).append(true).open(&shaders_path).ok();
Used {
now: Mutex::new((earlier.keys().copied().collect(), file)),
now,
earlier: RwLock::new(earlier),
parts: Default::default(),
shaders: Mutex::new((shaders, shaders_file)),
}
})
}

pub fn shutdown(timeout: Duration) {
if let Some(used) = U.get() {
let _ = used.now.shutdown_bounded(timeout);
}
}

/// First-use rank when an earlier run used a pipeline of these shaders.
pub fn rank(key: u64) -> Option<usize> {
(key != 0).then(|| get().earlier.read().unwrap().get(&key).copied()).flatten()
Expand All @@ -874,14 +892,7 @@ mod used {
if key == 0 {
return;
}
{
let mut g = get().now.lock().unwrap();
if g.0.insert(key) {
if let Some(f) = g.1.as_mut() {
let _ = writeln!(f, "{key:016x}");
}
}
}
get().now.note(key);
// its shaders, each once (also those of a set an earlier run used: the list of
// shaders is younger than the list of sets)
let parts = get().parts.lock().unwrap().get(&key).cloned().unwrap_or_default();
Expand Down Expand Up @@ -911,7 +922,7 @@ mod used {
/// (tests) This run used a pipeline of these shaders.
#[cfg(test)]
pub fn noted(key: u64) -> bool {
get().now.lock().unwrap().0.contains(&key)
get().now.contains(key)
}

#[cfg(test)]
Expand Down
Loading