Skip to content
Open
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
168 changes: 152 additions & 16 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6378,11 +6378,14 @@ unsafe fn compute_container(this: *mut c_void, f: PsoFn, d: &D3D12_COMPUTE_PIPEL
let mut persist_took = in_driver;
if hr < 0 && cached.is_some() {
// a blob the driver does not take (another root signature at the same address…): compiled
if let Some(expected) = rejected_generation.as_ref() {
forget_compute_blob_if(key, expected);
}
if from_disk {
driver_cache::note(false, loaded.take());
// (a lost device or a driver out of memory says nothing about the blob: it is kept)
if stale_cached_pso_refusal(hr) {
if let Some(expected) = rejected_generation.as_ref() {
forget_compute_blob_if(key, expected);
}
if from_disk {
driver_cache::note(false, loaded.take());
}
}
if mode == ComputeBuild::CachedWhole {
return (hr, in_driver, false);
Expand Down Expand Up @@ -7892,6 +7895,42 @@ mod graphics_blob_tests {
assert!(!copy.GS.pShaderBytecode.is_null());
}

#[test]
fn cached_graphics_blob_is_evicted_only_for_a_refusal_and_retried_either_way() {
let blob = [0xEFu8; 32];
// a removed or hung device, a driver out of memory, a failure that names nothing
for failure in [0x887a_0005u32 as i32, 0x887a_0006u32 as i32, E_OUTOFMEMORY, 0x8000_4005u32 as i32] {
let mut copy = D3D12_GRAPHICS_PIPELINE_STATE_DESC::default();
let (mut calls, mut evictions) = (0, 0);
let build = create_graphics_pso_cached(
&mut copy,
Some(&blob),
false,
|| evictions += 1,
|_, _, f| (f(), std::time::Duration::ZERO),
|_| { calls += 1; failure },
);
assert_eq!(build.hr, failure);
assert_eq!((calls, evictions), (2, 0), "{failure:#x} is not evidence of a stale blob");
}

for stale in [0x8007_0057u32 as i32, 0x887e_0001u32 as i32] {
let mut copy = D3D12_GRAPHICS_PIPELINE_STATE_DESC::default();
let (mut calls, mut evictions) = (0, 0);
let build = create_graphics_pso_cached(
&mut copy,
Some(&blob),
false,
|| evictions += 1,
|_, _, f| (f(), std::time::Duration::ZERO),
|c| { calls += 1; if c.CachedPSO.pCachedBlob.is_null() { 0 } else { stale } },
);
assert_eq!(build.hr, 0);
assert_eq!((calls, evictions), (2, 1), "{stale:#x}");
assert!(build.cache_eligible);
}
}

/// Where the description's parts are (shaders, input layout, semantic names, root signature,
/// a cached blob) never enters the key, only what is there.
#[test]
Expand Down Expand Up @@ -8016,7 +8055,10 @@ fn create_graphics_pso_cached(
if hr >= 0 {
return GraphicsPsoBuild { hr, built_in_driver: in_driver, reused: true, cache_eligible: false };
}
on_stale_cache();
// (a lost device or a driver out of memory says nothing about the blob: it is kept)
if stale_cached_pso_refusal(hr) {
on_stale_cache();
}
copy.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default();
let (retried, took) = run("graphics retry", false, &mut || retry_without_rect_gs(copy, rect_gs, &mut create));
let dropped_rect_gs = rect_gs && copy.GS.pShaderBytecode.is_null();
Expand Down Expand Up @@ -9491,11 +9533,15 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
let mut greused = mesh_from_library || gcached.is_some();
if !mesh_from_library && (hr < 0 || mesh_pso.is_none()) && gcached.is_some() {
drop(mesh_pso.take());
if let (Some(key), Some(expected)) = (gkey, rejected_generation.as_ref()) {
forget_graphics_blob_if(key, expected);
}
if gfrom_disk {
driver_cache::note(false, gloaded.take());
// (a lost device, a driver out of memory or a success without a pipeline says nothing
// about the blob: it is kept)
if stale_cached_pso_refusal(hr) {
if let (Some(key), Some(expected)) = (gkey, rejected_generation.as_ref()) {
forget_graphics_blob_if(key, expected);
}
if gfrom_disk {
driver_cache::note(false, gloaded.take());
}
}
take_cold_slot();
(hr, mesh_pso, gin_driver) = create_mesh("mesh pass retry", None);
Expand Down Expand Up @@ -9559,11 +9605,14 @@ unsafe fn finish_two_pass(this: *mut c_void, stream: &StreamOut, tp: crate::gpu:
}) };
let mut cpersist_took = cin_driver;
if hc < 0 && ccached.is_some() {
if let Some(expected) = rejected_cgeneration.as_ref() {
forget_compute_blob_if(ckey, expected);
}
if cfrom_disk {
driver_cache::note(false, cloaded.take());
// (a lost device or a driver out of memory says nothing about the blob: it is kept)
if stale_cached_pso_refusal(hc) {
if let Some(expected) = rejected_cgeneration.as_ref() {
forget_compute_blob_if(ckey, expected);
}
if cfrom_disk {
driver_cache::note(false, cloaded.take());
}
}
take_cold_slot();
cdesc.CachedPSO = D3D12_CACHED_PIPELINE_STATE::default();
Expand Down Expand Up @@ -14232,6 +14281,93 @@ mod mesh_stream_tests {
assert_eq!(hr, E_INVALIDARG);
}
}

thread_local! {
/// the stream sizes `fake_create_pso_null_ok_seen` was called with on this thread
static NULL_OK_STREAMS: std::cell::RefCell<Vec<usize>> = const { std::cell::RefCell::new(Vec::new()) };
}

unsafe extern "system" fn fake_create_pso_null_ok_seen(
this: *mut c_void,
desc: *const c_void,
riid: *const GUID,
ppv: *mut *mut c_void,
) -> HRESULT {
NULL_OK_STREAMS.with(|s| s.borrow_mut().push(*(desc as *const usize)));
fake_create_pso_null_ok(this, desc, riid, ppv)
}

/// (the first adapter's LUID: `driver_identity` has an identity for it, so the mesh pass a key)
unsafe extern "system" fn fake_adapter_luid_first(_: *mut c_void, luid: *mut windows::Win32::Foundation::LUID) {
use windows::Win32::Graphics::Dxgi::*;
*luid = CreateDXGIFactory1::<IDXGIFactory1>().and_then(|f| f.EnumAdapters1(0)).and_then(|a| a.GetDesc1())
.map_or_else(|_| Default::default(), |d| d.AdapterLuid);
}

#[test]
fn a_mesh_blob_is_kept_when_the_driver_gives_success_without_a_pipeline() {
let iface = layout::find("ID3D12Device2").expect("ID3D12Device2 layout");
let mut pc = vec![0usize; iface.methods.len()];
pc[layout::slot(iface, "CreatePipelineState").unwrap()] = fake_create_pso_null_ok_seen as usize;
pc[layout::slot(iface, "CreateComputePipelineState").unwrap()] = fake_create_compute_unexpected as usize;
let mut ext_buf = vec![0usize; 1 + iface.methods.len()];
ext_buf[0] = pc.as_ptr() as usize;
ext_buf[1 + layout::slot(iface, "GetAdapterLuid").unwrap()] = fake_adapter_luid_first as usize;
let ext_vtbl = ext_buf.as_ptr() as usize + std::mem::size_of::<usize>();
let mut fake_dev = [ext_vtbl];
let this = fake_dev.as_mut_ptr() as *mut c_void;
let Some(driver) = driver_identity(this) else {
crate::gpu::recomp::runner::skip_test("no GPU");
return;
};

let sampler: crate::gpu::recomp::pso::StaticSampler = ([0x55AA_2426, 0xCAFE_2426, 0xBEEF_2426, 0x0001_2426], true);
let rs_key = (this as usize, sampler.0.iter().copied().chain([1u32]).collect::<Vec<_>>());
CLASSES.lock().unwrap().push(Class {
orig: pc.as_ptr() as usize,
kind: Kind::Device,
ext: ext_vtbl,
iface,
public: iface.methods.len(),
scanned: iface.methods.len(),
});
ABI_RS_SAMPLERS.lock().unwrap().get_or_insert_with(HashMap::new).insert(rs_key.clone(), 0x2426_5678);

let tp = crate::gpu::recomp::pso::TwoPass {
cs: vec![0; 16],
ms: vec![0; 16],
record: crate::gpu::recomp::MsInterface { outputs: Vec::new(), threads: 32, max_vertices: 3, max_primitives: 1 },
mesh_groups_per_record: 1,
samplers: vec![sampler],
};
let ms = [0x24u8, 0x26, 0x4D, 0x53];
let mut stream = StreamOut::default();
stream.shader(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS, &ms);
let mesh_rs = 0x2426_5678usize;
let (hash, bytes) = unsafe {
two_pass_identity::two_pass_stream_key(&stream.0, &[], driver, crate::gpu::recomp::pso::mem_windows())
}.expect("mesh stream key");
let gkey = (hash, bytes, mesh_rs);
let blob = std::sync::Arc::new(vec![0xB1u8; 32]);
assert!(GRAPHICS_BLOBS.admit(gkey, blob.clone(), GRAPHICS_BLOBS_MAX));

NULL_OK_STREAMS.with(|s| s.borrow_mut().clear());
let mut out: *mut c_void = std::ptr::null_mut();
let iid = pc_iid(Kind::PipelineState);
let hr = unsafe { finish_two_pass(this, &stream, tp, mesh_rs as *mut c_void, &[], 0, "MS", &iid, &mut out) };
let kept = graphics_blob(gkey);
forget_graphics_blob_if(gkey, &blob);
CLASSES.lock().unwrap().retain(|c| c.ext != ext_vtbl);
if let Some(m) = ABI_RS_SAMPLERS.lock().unwrap().as_mut() { m.remove(&rs_key); }

// offered with the blob, then without it; neither call gave a pipeline
let seen = NULL_OK_STREAMS.with(|s| s.take());
assert_eq!(seen.len(), 2, "{seen:?}");
assert!(seen[0] > seen[1] && seen[1] == stream.0.len(), "{seen:?}");
assert_eq!(hr, E_INVALIDARG);
assert!(out.is_null());
assert!(kept.is_some_and(|k| std::sync::Arc::ptr_eq(&k, &blob)), "a success without a pipeline is not a refusal of the blob");
}
}

#[cfg(test)]
Expand Down
Loading