Skip to content
Closed
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
105 changes: 92 additions & 13 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4855,23 +4855,68 @@ fn forget_graphics_blob(key: (u64, usize), rejected: &std::sync::Arc<Vec<u8>>) -
true
}

unsafe fn keep_graphics_blob(key: (u64, usize), pso: *mut c_void, dxil_len: usize) {
if (dxil_len < COMPUTE_BLOB_MIN_DXIL && !driver_cache::keep_fixtures()) || pso.is_null() || graphics_blob(key).is_some() {
return;
fn remember_graphics_blob_into(
blobs: &mut HashMap<(u64, usize), std::sync::Arc<Vec<u8>>>,
total: &mut usize,
max_bytes: usize,
key: (u64, usize),
blob: std::sync::Arc<Vec<u8>>,
) -> bool {
if blob.is_empty() || blobs.contains_key(&key) || blob.len() > max_bytes.saturating_sub(*total) {
return false;
}
*total += blob.len();
blobs.insert(key, blob);
true
}

fn keep_graphics_blob_with(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new tests call keep_graphics_blob_with, a #[cfg(test)] copy of the logic, while production runs the separate unsafe fn keep_graphics_blob. The two have already drifted (this one has the bytes.is_empty() check, production has the pso.is_null() check), and the tests would pass on main too, so they don't guard the fix (AGENTS.md: a bug fix comes with a test that fails without the fix).

Please have keep_graphics_blob delegate to this function: take the GRAPHICS_BLOBS lock, pass its map, total and GRAPHICS_BLOBS_MAX, and use || read_pso_cached_blob(pso) as read_blob, keeping the null check in the wrapper. Then drop the #[cfg(test)] gate so the tests cover the code that runs.

blobs: &mut HashMap<(u64, usize), std::sync::Arc<Vec<u8>>>,
total: &mut usize,
max_bytes: usize,
key: (u64, usize),
dxil_len: usize,
slow: bool,
keep_fixtures: bool,
read_blob: impl FnOnce() -> Option<Vec<u8>>,
) -> Option<std::sync::Arc<Vec<u8>>> {
if let Some(existing) = blobs.get(&key) {
return Some(std::sync::Arc::clone(existing));
}
if !slow && dxil_len < COMPUTE_BLOB_MIN_DXIL && !keep_fixtures {
return None;
}
if let Some(bytes) = read_pso_cached_blob(pso) {
remember_graphics_blob(key, std::sync::Arc::new(bytes));
let bytes = read_blob()?;
if bytes.is_empty() {
return None;
}
let blob = std::sync::Arc::new(bytes);
remember_graphics_blob_into(blobs, total, max_bytes, key, std::sync::Arc::clone(&blob));
Some(blob)
}

unsafe fn keep_graphics_blob(key: (u64, usize), pso: *mut c_void, dxil_len: usize, slow: bool) -> Option<std::sync::Arc<Vec<u8>>> {
if pso.is_null() {
return None;
}
let mut cache = GRAPHICS_BLOBS.lock().unwrap();
let (blobs, total) = cache.get_or_insert_with(|| (HashMap::new(), 0));
keep_graphics_blob_with(
blobs,
total,
GRAPHICS_BLOBS_MAX,
key,
dxil_len,
slow,
driver_cache::keep_fixtures(),
|| read_pso_cached_blob(pso),
)
}

fn remember_graphics_blob(key: (u64, usize), blob: std::sync::Arc<Vec<u8>>) {
let mut cache = GRAPHICS_BLOBS.lock().unwrap();
let (blobs, total) = cache.get_or_insert_with(|| (HashMap::new(), 0));
if blob.is_empty() || blobs.contains_key(&key) || blob.len() > GRAPHICS_BLOBS_MAX.saturating_sub(*total) {
return;
}
*total += blob.len();
blobs.insert(key, blob);
remember_graphics_blob_into(blobs, total, GRAPHICS_BLOBS_MAX, key, blob);
}

/// Deterministic FNV-1a digest of every field in a translated PC graphics PSO (shaders, static
Expand Down Expand Up @@ -5305,9 +5350,11 @@ unsafe fn translated_graphics_pso_once(this: *mut c_void, f: PsoFn, desc: *const
);
let hr = build.hr;
if build.cache_eligible {
keep_graphics_blob(gkey, *ppv, bytes);
if let (Some(p), Some(b)) = (disk.as_deref(), graphics_blob(gkey)) {
driver_cache::keep(p, &b, build.in_driver);
let slow = disk.is_some() && driver_cache::worth_keeping(build.in_driver);
if let Some(b) = keep_graphics_blob(gkey, *ppv, bytes, slow) {
if let Some(p) = disk.as_deref() {
driver_cache::keep(p, &b, build.in_driver);
}
}
} else if build.reused && from_disk {
driver_cache::note(true, None);
Expand Down Expand Up @@ -9141,3 +9188,35 @@ void main() {
}
}

#[cfg(test)]
mod graphics_blob_cache_tests {
use super::*;

#[test]
fn keep_graphics_blob_returns_blob_for_disk_even_when_ram_cache_is_full() {
let mut blobs = HashMap::new();
let mut total = 0usize;
let max_bytes = 16usize;

let first = keep_graphics_blob_with(&mut blobs, &mut total, max_bytes, (1, 1024), COMPUTE_BLOB_MIN_DXIL, true, false, || Some(vec![0x11; 16]));
assert_eq!(first.as_deref().map(Vec::as_slice), Some(&[0x11u8; 16][..]));
assert_eq!(total, 16);
assert!(blobs.contains_key(&(1, 1024)));

// RAM cache is now full; a second slow PSO must still return its blob so caller persists it to disk.
let second = keep_graphics_blob_with(&mut blobs, &mut total, max_bytes, (2, 2048), 512, true, false, || Some(vec![0x22; 12]));
assert_eq!(second.as_deref().map(Vec::as_slice), Some(&[0x22u8; 12][..]));
assert_eq!(total, 16, "RAM cache cap must still be respected");
assert!(!blobs.contains_key(&(2, 2048)));
}

#[test]
fn keep_graphics_blob_skips_fast_small_pso() {
let mut blobs = HashMap::new();
let mut total = 0usize;
let got = keep_graphics_blob_with(&mut blobs, &mut total, 1024, (3, 128), 128, false, false, || panic!("fast small PSO should not extract blob"));
assert!(got.is_none());
assert_eq!(total, 0);
}
}

Loading