Skip to content
Open
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
134 changes: 83 additions & 51 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,11 +1113,38 @@ impl State {
}
}

struct NativeSelection {
raw: Option<usize>,
generation: Option<super::pso_async::NativeGeneration>,
fallback: Option<ID3D12PipelineState>,
}

fn select_native_choice(
resolution: Option<&super::pso_async::PipelineResolution>,
mode: super::pso_async::NativePipelineBindMode,
untracked: Option<usize>,
) -> NativeSelection {
use super::pso_async::NativePipelineChoice;
match resolution.map(|r| r.select_for_native_bind(mode)) {
Some(NativePipelineChoice::Exact { raw, generation }) => {
NativeSelection { raw: Some(raw), generation: Some(generation), fallback: None }
}
Some(NativePipelineChoice::InitialFallback { raw, .. }) => NativeSelection {
raw: Some(raw),
generation: None,
fallback: super::pso_async::retain_com(raw),
},
Some(NativePipelineChoice::Unavailable) => NativeSelection { raw: None, generation: None, fallback: None },
None => NativeSelection { raw: untracked, generation: None, fallback: None },
}
}

pub(super) struct PreparedNativePipeline {
original: usize,
raw: usize,
resolution: Option<super::pso_async::PipelineResolution>,
generation: Option<super::pso_async::NativeGeneration>,
fallback: Option<ID3D12PipelineState>,
poison_on_commit: bool,
}

Expand All @@ -1130,37 +1157,49 @@ impl PreparedNativePipeline {
original: usize,
resolution: Option<super::pso_async::PipelineResolution>,
) -> Self {
use super::pso_async::{NativePipelineBindMode, NativePipelineChoice};
let choice = resolution.as_ref().map(|r| r.select_for_native_bind(NativePipelineBindMode::ForBind));
let (raw, generation, poison_on_commit) = match choice {
Some(NativePipelineChoice::Exact { raw, generation }) => (raw, Some(generation), false),
Some(NativePipelineChoice::InitialFallback { raw, .. }) => (raw, None, false),
Some(NativePipelineChoice::Unavailable) => {
(0, None, !resolution.as_ref().unwrap().unavailable_is_emission_guarded())
}
None => (original, None, false),
};
Self { original, raw, resolution, generation, poison_on_commit }
let sel = select_native_choice(
resolution.as_ref(),
super::pso_async::NativePipelineBindMode::ForBind,
Some(original),
);
let poison_on_commit = sel.raw.is_none()
&& resolution.as_ref().is_some_and(|r| !r.unavailable_is_emission_guarded());
Self {
original,
raw: sel.raw.unwrap_or(0),
resolution,
generation: sel.generation,
fallback: sel.fallback,
poison_on_commit,
}
}

pub(super) fn raw(&self) -> usize { self.raw }

pub(super) fn record_generation(self, list: usize) {
super::list_x::record_native_generation(list, self.generation);
if self.poison_on_commit {
super::list_x::poison_recording(list);
}
}

pub(super) fn commit(self, list: *mut c_void, allocator: usize) {
super::list_x::record_native_generation(list as usize, self.generation);
super::list_x::record_native_pipeline(list as usize, self.generation, self.fallback);
install_reset(list, allocator, self.original, self.resolution);
if self.poison_on_commit {
super::list_x::poison_recording(list as usize);
}
}
}

/// Resolve `expected_pso` through this command list's retained `PipelineResolution` when it
/// matches the currently tracked pipeline, avoiding registry re-lookup of a released address.
pub(super) fn resolve_bound_native_pipeline(
this: *mut c_void,
expected_pso: usize,
) -> Option<(Option<usize>, Option<super::pso_async::NativeGeneration>, Option<ID3D12PipelineState>)> {
let resolution = with(this, |s| (s.pso == expected_pso).then(|| s.resolution.clone()).flatten())?;
let sel = select_native_choice(
Some(&resolution),
super::pso_async::NativePipelineBindMode::ForBind,
None,
);
Some((sel.raw, sel.generation, sel.fallback))
}

/// `ClearState(pso)`: resets pipeline and root-argument state back to creation defaults
/// with `pso` bound, keeping the current recording's allocator and UMA arena intact.
#[cfg(test)]
Expand Down Expand Up @@ -3559,6 +3598,7 @@ fn resolve_indirect_pipeline(
Some((raw, false))
}
super::pso_async::IndirectPipelineChoice::InitialFallback { raw, bound_unbuilt } => {
super::list_x::record_fallback_pipeline(list, super::pso_async::retain_com(raw));
Some((raw, bound_unbuilt))
}
super::pso_async::IndirectPipelineChoice::Unavailable => None,
Expand Down Expand Up @@ -4695,34 +4735,29 @@ unsafe fn native_pipeline_choice(
this: *mut c_void,
pso: usize,
resolution: Option<&super::pso_async::PipelineResolution>,
) -> (Option<usize>, Option<super::pso_async::NativeGeneration>) {
use super::pso_async::{NativePipelineBindMode, NativePipelineChoice};
let choice = resolution.map(|r| r.select_for_native_bind(NativePipelineBindMode::Lazy));
match choice {
Some(NativePipelineChoice::Exact { raw, generation }) => (Some(raw), Some(generation)),
Some(NativePipelineChoice::InitialFallback { raw, .. }) => (Some(raw), None),
Some(NativePipelineChoice::Unavailable) if resolution.unwrap().unavailable_is_emission_guarded() => {
(None, None)
}
Some(NativePipelineChoice::Unavailable) => {
// Mesh/raw native consumers can bypass retained draw resolution. Preserve root and
// dynamic state, but make Close/submission reject this whole physical recording.
super::list_x::poison_recording(this as usize);
(None, None)
}
None => (Some(pso), None),
) -> NativeSelection {
let sel = select_native_choice(
resolution,
super::pso_async::NativePipelineBindMode::Lazy,
Some(pso),
);
if sel.raw.is_none() && resolution.is_some_and(|r| !r.unavailable_is_emission_guarded()) {
// Mesh/raw native consumers can bypass retained draw resolution. Preserve root and
// dynamic state, but make Close/submission reject this whole physical recording.
super::list_x::poison_recording(this as usize);
}
sel
}

unsafe fn set_pipeline_state_retained(
this: *mut c_void,
pso: usize,
resolution: Option<super::pso_async::PipelineResolution>,
) {
let (raw, generation) = native_pipeline_choice(this, pso, resolution.as_ref());
super::list_x::record_native_generation(this as usize, generation);
let sel = native_pipeline_choice(this, pso, resolution.as_ref());
super::list_x::record_native_pipeline(this as usize, sel.generation, sel.fallback);
install_tracked_pipeline_state(this, pso, resolution);
if let Some(raw) = raw {
if let Some(raw) = sel.raw {
super::real::bind_pipeline_exact(this, pso, raw);
}
super::list_x::on_set_pipeline_state(this, pso);
Expand All @@ -4732,9 +4767,9 @@ unsafe fn set_pipeline_state_retained(
/// the run records it: as `set_pipeline_state_retained`, the list's tracked state having taken
/// `pso` and `resolution` when the title made the call.
unsafe fn replay_pipeline_bind(this: *mut c_void, pso: usize, resolution: Option<&super::pso_async::PipelineResolution>) {
let (raw, generation) = native_pipeline_choice(this, pso, resolution);
super::list_x::record_native_generation(this as usize, generation);
if let Some(raw) = raw {
let sel = native_pipeline_choice(this, pso, resolution);
super::list_x::record_native_pipeline(this as usize, sel.generation, sel.fallback);
if let Some(raw) = sel.raw {
super::real::bind_pipeline_exact(this, pso, raw);
}
super::list_x::on_set_pipeline_state(this, pso);
Expand All @@ -4752,16 +4787,13 @@ unsafe fn clear_pipeline_state_retained(
pso: usize,
resolution: Option<super::pso_async::PipelineResolution>,
) {
use super::pso_async::{NativePipelineBindMode, NativePipelineChoice};
let choice = resolution.as_ref().map(|r| r.select_for_native_bind(NativePipelineBindMode::ForBind));
let (raw, generation) = match choice {
Some(NativePipelineChoice::Exact { raw, generation }) => (raw, Some(generation)),
Some(NativePipelineChoice::InitialFallback { raw, .. }) => (raw, None),
Some(NativePipelineChoice::Unavailable) => (0, None),
None => (pso, None),
};
super::list_x::record_native_generation(this as usize, generation);
super::real::clear_pipeline_exact(this, pso, raw);
let sel = select_native_choice(
resolution.as_ref(),
super::pso_async::NativePipelineBindMode::ForBind,
Some(pso),
);
super::list_x::record_native_pipeline(this as usize, sel.generation, sel.fallback);
super::real::clear_pipeline_exact(this, pso, sel.raw.unwrap_or(0));
super::list_x::on_clear_state(this, pso);
install_clear_state(this, pso, resolution);
}
Expand Down
70 changes: 70 additions & 0 deletions crates/xcl/src/hle/d3d12x/late_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ enum Dead {
Resource(usize),
/// a pipeline state XCL owns (`hold_pipeline`)
Pipeline(usize),
/// an adopted pipeline state whose last reference waits here (`hold_adopted_pipeline`,
/// released via `real::orig_fn` without re-entering `pso_release`)
AdoptedPipeline(usize),
/// a heap XCL owns (`hold_heap`)
Heap(usize),
/// a reference of XCL's own to a superseded target, whose tiles then leave the shared pages
Expand Down Expand Up @@ -275,6 +278,24 @@ pub fn hold_pipeline(p: usize) {
start_thread();
}

/// An adopted pipeline state (`real::pso_release`) whose final reference is deferred until every
/// queue passes a fence signalled after this, released via its original driver `Release` without
/// re-entering `pso_release`. `XCL_LATE_RELEASE=0`: now.
pub fn hold_adopted_pipeline(p: usize) {
if p == 0 {
return;
}
if !enabled() {
unsafe {
let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(super::real::orig_fn(p as *mut c_void, 2));
f(p as *mut c_void);
}
return;
}
HELD.lock().unwrap().get_or_insert_with(Held::default).open.push(Dead::AdoptedPipeline(p));
start_thread();
}

/// A heap XCL owns that reserved resources mapped tiles of (`vram`'s page pools) is let go: a
/// resource the title released maps them until it goes here, and work queued before still reads
/// them – with the heap gone under that work the GPU faults. So it goes as such a resource does,
Expand Down Expand Up @@ -333,6 +354,11 @@ pub unsafe fn drain_known(seal_now: bool) {
super::pso_async::release(p);
continue;
}
Dead::AdoptedPipeline(p) => {
let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(super::real::orig_fn(p as *mut c_void, 2));
f(p as *mut c_void);
continue;
}
Dead::Heap(h) => {
drop(ID3D12Heap::from_raw(h as *mut c_void));
continue;
Expand Down Expand Up @@ -363,6 +389,12 @@ pub unsafe fn drain_known(seal_now: bool) {
super::vram::drain_recycled();
}

#[cfg(test)]
pub(super) fn is_held(p: usize) -> bool {
let matches_p = |d: &Dead| matches!(*d, Dead::Resource(x) | Dead::Pipeline(x) | Dead::AdoptedPipeline(x) | Dead::Heap(x) | Dead::Unmap(x) if x == p);
HELD.lock().unwrap().as_ref().is_some_and(|h| h.open.iter().any(matches_p) || h.sealed.iter().any(|b| b.dead.iter().any(matches_p)))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -567,4 +599,42 @@ mod tests {
assert!(seal_due(false, false, true, Some(Duration::ZERO)));
assert!(!seal_due(false, false, false, Some(Duration::ZERO)) && seal_due(false, false, false, Some(SEAL_EVERY)));
}

/// An adopted pipeline held by `hold_adopted_pipeline` is released through its original driver
/// `Release` (`orig_fn`) rather than re-entering the adopted override slot.
#[test]
fn an_adopted_pipeline_goes_via_original_release_when_queues_are_past() {
use std::sync::atomic::AtomicU32;
#[repr(C)]
struct Fake {
vt: *const usize,
refs: AtomicU32,
hook_calls: AtomicU32,
}
unsafe extern "system" fn add_ref(this: *mut c_void) -> u32 {
(*(this as *const Fake)).refs.fetch_add(1, Ordering::SeqCst) + 1
}
unsafe extern "system" fn orig_release(this: *mut c_void) -> u32 {
(*(this as *const Fake)).refs.fetch_sub(1, Ordering::SeqCst) - 1
}
unsafe extern "system" fn hook_release(this: *mut c_void) -> u32 {
(*(this as *const Fake)).hook_calls.fetch_add(1, Ordering::SeqCst);
0
}
let orig = [0usize, add_ref as usize, orig_release as usize];
let ext = [orig.as_ptr() as usize, 0, add_ref as usize, hook_release as usize];
let fake = Box::new(Fake {
vt: unsafe { ext.as_ptr().add(1) },
refs: AtomicU32::new(1),
hook_calls: AtomicU32::new(0),
});
let p = &*fake as *const Fake as usize;
hold_adopted_pipeline(p);
let start = Instant::now();
while fake.refs.load(Ordering::SeqCst) != 0 && start.elapsed() < Duration::from_secs(10) {
std::thread::sleep(Duration::from_millis(5));
}
assert_eq!(fake.refs.load(Ordering::SeqCst), 0);
assert_eq!(fake.hook_calls.load(Ordering::SeqCst), 0, "original Release must not re-enter pso_release");
}
}
Loading
Loading