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
109 changes: 99 additions & 10 deletions crates/xcl/src/hle/d3d12x/device_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ unsafe fn create_graphics_pso_tracked(this: *mut c_void, desc: *const c_void, ri

/// size of D3D12_GRAPHICS_PIPELINE_STATE_DESC
const GFX_DESC: usize = 0x290;
/// `pRootSignature` pointer in D3D12_GRAPHICS_PIPELINE_STATE_DESC
const ROOT_SIGNATURE: usize = 0;
/// VS, PS, DS, HS, GS { bytecode; length } in the desc
const STAGES: [usize; 5] = [0x08, 0x18, 0x28, 0x38, 0x48];
const SO_ENTRIES: usize = 0x60;
Expand Down Expand Up @@ -325,17 +327,17 @@ fn clear_dynamic_depth_bias(d: &mut [u8; GFX_DESC]) {
struct GfxDesc([u8; GFX_DESC]);

/// A title graphics pipeline's description, kept for CreateDerivedPipelineStateX and the
/// patched-PSO fallback of RSSetDepthBiasX. Input layout is copied and stage shader bytecodes are
/// interned into a shared pool (`Arc<[u8]>`) so tens of thousands of PSOs sharing a few thousand
/// unique shaders hold only one copy per unique shader and stay valid after the title frees its
/// original bytecode buffers.
/// patched-PSO fallback of RSSetDepthBiasX. The root signature is retained, the input layout is
/// copied, and stage shader bytecode is interned into a shared pool (`Arc<[u8]>`) so PSOs share
/// one copy per unique shader and stay valid after the title frees its original bytecode buffers.
struct Base {
desc: GfxDesc,
shader_blobs: [Option<Arc<[u8]>>; 5],
/// Whether this PSO was created with `D3D12_PIPELINE_STATE_FLAG_DYNAMIC_DEPTH_BIAS`.
dynamic_depth_bias: bool,
/// Original static `RasterizerState.{DepthBias, DepthBiasClamp, SlopeScaledDepthBias}`.
static_bias: DepthBias,
_root_sig: Option<ID3D12RootSignature>,
_elems: Vec<[u8; 32]>,
_names: Vec<CString>,
}
Expand Down Expand Up @@ -458,8 +460,19 @@ unsafe fn note_base(pso: *mut c_void, desc: *const c_void, dynamic_depth_bias: b
let at = if elems.is_empty() { 0 } else { elems.as_ptr() as u64 };
d.0[INPUT_LAYOUT..INPUT_LAYOUT + 8].copy_from_slice(&at.to_le_bytes());
d.0[CACHED_PSO..CACHED_PSO + 16].fill(0);
let base = Arc::new(Base { desc: d, shader_blobs, dynamic_depth_bias, static_bias, _elems: elems, _names: names });
BASES.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso as usize, base);
let rs_raw = rd64(&d.0, ROOT_SIGNATURE) as *mut c_void;
let root_sig = ID3D12RootSignature::from_raw_borrowed(&rs_raw).cloned();
let base = Arc::new(Base {
desc: d,
shader_blobs,
dynamic_depth_bias,
static_bias,
_root_sig: root_sig,
_elems: elems,
_names: names,
});
let prev = BASES.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso as usize, base);
drop(prev);
}

/// Depth-bias metadata for a tracked graphics PSO: `(created_with_dynamic_depth_bias, static_bias)`.
Expand All @@ -482,17 +495,19 @@ pub(super) fn register_test_base(pso: usize, dynamic_depth_bias: bool, static_bi
shader_blobs: Default::default(),
dynamic_depth_bias,
static_bias,
_root_sig: None,
_elems: Vec::new(),
_names: Vec::new(),
});
BASES.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso, base);
let prev = BASES.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso, base);
drop(prev);
}

/// A pipeline released for good is no longer a base; its cached depth-bias variants go with it.
pub(super) fn forget_base(pso: usize) {
if let Some(m) = BASES.lock().unwrap().as_mut() {
m.remove(&pso);
}
// drop outside the lock so releasing the retained root signature never runs under BASES
let old = BASES.lock().unwrap().as_mut().and_then(|m| m.remove(&pso));
drop(old);
let gone: Vec<usize> = {
let mut g = DEPTH_BIAS_PSOS.lock().unwrap();
match g.as_mut() {
Expand Down Expand Up @@ -2038,4 +2053,78 @@ mod tests {
forget_base(pso_key2);
assert!(weak_vs.upgrade().is_none(), "shared blob drops once all owning PSOs are released");
}

#[test]
fn base_retains_root_signature_until_forgotten() {
use std::sync::atomic::AtomicU32;

#[repr(C)]
struct FakeRootSig {
vtbl: *const [usize; 3],
refs: AtomicU32,
}
unsafe extern "system" fn qi(_: *mut c_void, _: *const GUID, out: *mut *mut c_void) -> HRESULT {
if !out.is_null() {
*out = std::ptr::null_mut();
}
E_NOTIMPL
}
unsafe extern "system" fn add_ref(this: *mut c_void) -> u32 {
(*(this as *const FakeRootSig)).refs.fetch_add(1, Ordering::Relaxed) + 1
}
unsafe extern "system" fn release(this: *mut c_void) -> u32 {
(*(this as *const FakeRootSig)).refs.fetch_sub(1, Ordering::Relaxed) - 1
}
let vtbl: [usize; 3] = [qi as usize, add_ref as usize, release as usize];

assert_eq!(std::mem::offset_of!(D3D12_GRAPHICS_PIPELINE_STATE_DESC, pRootSignature), ROOT_SIGNATURE);

let fake = FakeRootSig { vtbl: &vtbl, refs: AtomicU32::new(1) };
let fake2 = FakeRootSig { vtbl: &vtbl, refs: AtomicU32::new(1) };
let rs_ptr = &fake as *const FakeRootSig as u64;
let rs_ptr2 = &fake2 as *const FakeRootSig as u64;
let pso_key = 0x9300_0001usize;
let pso_null_rs = 0x9300_0002usize;

unsafe {
// Stream-output PSOs are skipped by note_base and must not retain pRootSignature.
let mut desc = GfxDesc([0u8; GFX_DESC]);
desc.0[ROOT_SIGNATURE..ROOT_SIGNATURE + 8].copy_from_slice(&rs_ptr.to_le_bytes());
desc.0[SO_ENTRIES..SO_ENTRIES + 4].copy_from_slice(&1u32.to_le_bytes());
note_base(pso_key as *mut c_void, desc.0.as_ptr() as *const c_void, false);
assert_eq!(fake.refs.load(Ordering::Relaxed), 1);

// Regular graphics PSO: note_base AddRefs pRootSignature.
desc.0[SO_ENTRIES..SO_ENTRIES + 4].fill(0);
note_base(pso_key as *mut c_void, desc.0.as_ptr() as *const c_void, false);
assert_eq!(fake.refs.load(Ordering::Relaxed), 2);

// Caller releases its own reference; Base keeps the root signature alive.
assert_eq!(release(&fake as *const FakeRootSig as *mut c_void), 1);

// Holding an Arc<Base> (as create_derived_pso_x / depth_bias_pso does) keeps it
// alive across a concurrent forget_base until the in-flight use finishes.
let inflight = BASES.lock().unwrap().as_ref().and_then(|m| m.get(&pso_key).cloned()).unwrap();
forget_base(pso_key);
assert_eq!(fake.refs.load(Ordering::Relaxed), 1);
drop(inflight);
assert_eq!(fake.refs.load(Ordering::Relaxed), 0);

// Replacing an entry for the same PSO key releases the previous root signature.
let mut desc2 = GfxDesc([0u8; GFX_DESC]);
desc2.0[ROOT_SIGNATURE..ROOT_SIGNATURE + 8].copy_from_slice(&rs_ptr2.to_le_bytes());
note_base(pso_key as *mut c_void, desc2.0.as_ptr() as *const c_void, false);
assert_eq!(fake2.refs.load(Ordering::Relaxed), 2);
let null_desc = GfxDesc([0u8; GFX_DESC]);
note_base(pso_key as *mut c_void, null_desc.0.as_ptr() as *const c_void, false);
assert_eq!(fake2.refs.load(Ordering::Relaxed), 1);
forget_base(pso_key);

// Null pRootSignature is accepted and cleaned up cleanly.
note_base(pso_null_rs as *mut c_void, null_desc.0.as_ptr() as *const c_void, false);
assert!(base_depth_bias_info(pso_null_rs).is_some());
forget_base(pso_null_rs);
assert!(base_depth_bias_info(pso_null_rs).is_none());
}
}
}
Loading