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
8 changes: 1 addition & 7 deletions crates/xcl/src/hle/d3d12x/late_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,7 @@ pub unsafe fn drain_known(seal_now: bool) {
if HELD.lock().unwrap().as_ref().is_none_or(Held::is_empty) {
return;
}
let mut queues: Vec<ID3D12CommandQueue> = Vec::new();
for raw in super::present::known_queues() {
let raw = raw as *mut c_void;
if let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&raw) {
queues.push(q.clone());
}
}
let mut queues = super::present::known_queues_held();
queues.extend(super::sparse::queue());
queues.extend(super::title_textures::upload_queues());
let gone = match HELD.lock().unwrap().as_mut() {
Expand Down
66 changes: 56 additions & 10 deletions crates/xcl/src/hle/d3d12x/present.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,38 @@ unsafe fn backbuffer_like(res: usize) -> Option<D3D12_RESOURCE_DESC> {
ok.then_some(d)
}

/// queues the title submitted work to (AddRef'd: they stay valid for `drain`)
static QUEUES: Mutex<Vec<usize>> = Mutex::new(Vec::new());
struct OwnedQueues<T> {
entries: Vec<(usize, T)>,
}

impl<T: Clone> OwnedQueues<T> {
const fn new() -> Self {
Self { entries: Vec::new() }
}

/// Add the first owner for `key`. `make` is not called for a duplicate, so no temporary
/// owner can be dropped while the registry lock is held.
fn ensure_with(&mut self, key: usize, make: impl FnOnce() -> Option<T>) -> bool {
if self.entries.iter().any(|&(k, _)| k == key) {
return false;
}
let Some(owner) = make() else { return false };
self.entries.push((key, owner));
true
}

fn snapshot(&self) -> Vec<T> {
self.entries.iter().map(|(_, owner)| owner.clone()).collect()
}
}

/// Queues the title submitted work to. One process-lifetime owner per raw queue, as before.
static QUEUES: Mutex<OwnedQueues<ID3D12CommandQueue>> = Mutex::new(OwnedQueues::new());

/// The queues the title submitted work to.
pub fn known_queues() -> Vec<usize> {
QUEUES.lock().unwrap().clone()
/// Strong snapshot of the queues the title submitted work to. Every AddRef happens while the
/// registry is locked; callers own the returned references and drop them after the lock is gone.
pub fn known_queues_held() -> Vec<ID3D12CommandQueue> {
QUEUES.lock().unwrap().snapshot()
}

static EXECUTES: AtomicU64 = AtomicU64::new(0);
Expand All @@ -307,11 +333,8 @@ pub fn activity() -> (u64, u64) {
pub unsafe fn note_execute(queue: *mut c_void, lists: &[*mut c_void]) {
EXECUTES.fetch_add(1, Ordering::Relaxed);
{
let mut q = QUEUES.lock().unwrap();
if !q.contains(&(queue as usize)) {
add_ref(queue as usize);
q.push(queue as usize);
}
let mut queues = QUEUES.lock().unwrap();
queues.ensure_with(queue as usize, || ID3D12CommandQueue::from_raw_borrowed(&queue).cloned());
}
super::late_release::drain_known(false);
if !enabled() || IN_PRESENT.with(|c| c.get()) || PRESENTX.load(Ordering::Relaxed) {
Expand Down Expand Up @@ -1481,6 +1504,29 @@ impl Drop for Presenter {
mod tests {
use super::*;

#[test]
fn owned_queue_registry_deduplicates_without_making_a_temporary_owner() {
let mut registry = OwnedQueues::new();
assert!(registry.ensure_with(0xA, || Some(std::sync::Arc::new(1))));
assert!(!registry.ensure_with(0xA, || -> Option<std::sync::Arc<i32>> { panic!("duplicate owner was made") }));
assert!(registry.ensure_with(0xB, || Some(std::sync::Arc::new(2))));
let values: Vec<_> = registry.snapshot().iter().map(|v| **v).collect();
assert_eq!(values, [1, 2]);
}

#[test]
fn owned_queue_snapshot_has_its_own_lifetime() {
let owner = std::sync::Arc::new(());
let weak = std::sync::Arc::downgrade(&owner);
let mut registry = OwnedQueues::new();
assert!(registry.ensure_with(0xA, || Some(owner)));
let snapshot = registry.snapshot();
drop(registry);
assert!(weak.upgrade().is_some(), "the snapshot did not keep its owner alive");
drop(snapshot);
assert!(weak.upgrade().is_none(), "the caller did not release its snapshot owner");
}

#[test]
fn present_ring_overlaps_previous_frame_and_drains_cleanly() {
let mut ring = PresentRing::new(PRESENT_SLOTS);
Expand Down
4 changes: 1 addition & 3 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7906,9 +7906,7 @@ pub fn drain_gpu() {
unsafe {
// a fence per queue (they finish in any order)
let mut fences = Vec::new();
for q in super::present::known_queues() {
let qr = q as *mut c_void;
let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&qr) else { continue };
for q in super::present::known_queues_held() {
let submit = || {
let f: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE)
.map_err(|e| fence::api_error(dev, "CreateFence", e))?;
Expand Down
9 changes: 3 additions & 6 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,12 +1445,9 @@ impl State {

unsafe fn sync_known_queues(&mut self) -> bool {
let mut ok = true;
for raw in super::present::known_queues() {
if !self.queue_fences.contains_key(&raw) {
let ptr = raw as *mut c_void;
if let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&ptr) {
ok &= self.note_queue(q);
}
for q in super::present::known_queues_held() {
if !self.queue_fences.contains_key(&(q.as_raw() as usize)) {
ok &= self.note_queue(&q);
}
}
ok
Expand Down
5 changes: 2 additions & 3 deletions crates/xcl/src/hle/d3d12x/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,9 +1160,8 @@ unsafe fn collect_device_queues(dev_key: usize, extra: Option<&ID3D12CommandQueu
out.push((key, q));
}
};
for raw in super::present::known_queues() {
let raw = raw as *mut c_void;
if let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&raw) { add(q.clone()); }
for q in super::present::known_queues_held() {
add(q);
}
for q in extra.into_iter().cloned().chain(super::title_textures::upload_queues()).chain(super::sparse::queue()) {
add(q);
Expand Down