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
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3619,6 +3619,7 @@ unsafe fn mapped_after_wait(queue: *mut c_void) {
super::sparse::PreparationOutcome::Ready => {}
super::sparse::PreparationOutcome::RetryRequired => stop_for_unresolved_wait("sparse upload preparation after a wait inside a submit still failed after bounded retries"),
super::sparse::PreparationOutcome::ExecutedUntracked => stop_for_unresolved_wait("a sparse upload after a wait inside a submit executed without a retirement signal"),
super::sparse::PreparationOutcome::MappingUnmarked => stop_for_unresolved_wait("a texture page mapping after a wait inside a submit has no Signal of the mapping queue"),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/xcl/src/hle/d3d12x/lut3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,23 @@ const DUMPED_MOST: usize = 256;
const WRITES_LOGGED: u64 = 12;

pub fn on() -> bool {
#[cfg(test)]
if FORCE_ON.load(Ordering::Relaxed) {
return true;
}
super::targets::enabled()
}

/// Tests: track 3D textures as `XCL_DUMP_TARGETS` does.
#[cfg(test)]
pub static FORCE_ON: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Tests: `res` is one of the 3D textures a dump enumerates.
#[cfg(test)]
pub fn tracked(res: usize) -> bool {
VOLS.lock().unwrap().contains_key(&res)
}

/// Whether `created` is to be told of a resource made (`on`, or `XCL_CAPTURE_FRAME`).
pub fn created_on() -> bool {
on() || super::capture::enabled()
Expand Down
14 changes: 14 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4607,6 +4607,19 @@ pub(super) unsafe fn note_reserved(hr: HRESULT, ppv: *mut *mut c_void) -> HRESUL
hr
}

/// `res` is no longer one of the reserved resources `note_reserved` keeps (a spare or an evicted one
/// of `vram`'s pool, which goes without the title's Release).
pub(super) fn forget_reserved(res: usize) {
if let Some(s) = RESERVED.lock().unwrap().as_mut() {
s.remove(&res);
}
}

#[cfg(test)]
pub(super) fn is_noted_reserved(res: usize) -> bool {
RESERVED.lock().unwrap().as_ref().is_some_and(|m| m.contains_key(&res))
}

unsafe fn is_reserved(res: *mut c_void) -> bool {
let kept = RESERVED.lock().unwrap().as_ref().and_then(|m| m.get(&(res as usize)).copied());
kept.is_some_and(|k| desc_of(res) == Some(k))
Expand Down Expand Up @@ -12077,6 +12090,7 @@ pub(super) unsafe fn prepare_title_submission(queue: *mut c_void, lists: &[*mut
let reason = match outcome {
PreparationOutcome::RetryRequired => "sparse upload preparation still failed after bounded retries",
PreparationOutcome::ExecutedUntracked => "a sparse upload executed without a retirement signal",
PreparationOutcome::MappingUnmarked => "could not queue a Signal or Wait for texture page mappings",
PreparationOutcome::Ready => unreachable!(),
};
let message = format!("{reason} – the title is stopped before its GPU submission");
Expand Down
3 changes: 3 additions & 0 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2412,6 +2412,9 @@ pub(super) enum PreparationOutcome {
Ready,
RetryRequired,
ExecutedUntracked,
/// a tile mapping is queued that no Signal of the mapping queue covers: its own Signal failed
/// and so did a retry (`vram::before_execute`), so nothing may run that reads it
MappingUnmarked,
}

impl From<SubmitResult> for PreparationOutcome {
Expand Down
50 changes: 40 additions & 10 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2063,7 +2063,9 @@ unsafe fn refresh_parts(queue: *mut c_void, look: bool, mut sample: Option<&mut
if outcome != super::sparse::PreparationOutcome::Ready {
return outcome;
}
super::vram::before_execute(q);
if !super::vram::before_execute(q) {
return super::sparse::PreparationOutcome::MappingUnmarked;
}
}
// a write where a page the queue's page mappings show is kept is a write to the GPU-view pages
// that show it: the textures read through their pages find it by their own range (sparse had
Expand Down Expand Up @@ -2181,9 +2183,9 @@ unsafe fn refresh_parts(queue: *mut c_void, look: bool, mut sample: Option<&mut
}
seed_stream_recheck(&todo, &ranges, recheck_frame);
let t_tex = std::time::Instant::now();
DIRTY_BUF.with(|dcell| upload_all(todo, &dcell.borrow(), page as usize, ID3D12CommandQueue::from_raw_borrowed(&queue)));
let outcome = DIRTY_BUF.with(|dcell| upload_all(todo, &dcell.borrow(), page as usize, ID3D12CommandQueue::from_raw_borrowed(&queue)));
super::submit_cost::textures(t_tex.elapsed());
super::sparse::PreparationOutcome::Ready
outcome
}

#[cfg(feature = "texture-selection-bench")]
Expand Down Expand Up @@ -2653,14 +2655,28 @@ unsafe fn upload_target(title: Option<&ID3D12CommandQueue>) -> Target {
/// textures with something to send again (`texture_upload::sent`)
static AGAIN: Mutex<std::collections::BTreeSet<usize>> = Mutex::new(std::collections::BTreeSet::new());

#[derive(Debug)]
enum UploadError {
MappingUnmarked,
Other(String),
}

impl From<String> for UploadError {
fn from(error: String) -> Self {
Self::Other(error)
}
}

/// Upload `items` on `queue`, a DIRECT one: the ones that could not be, the bytes of the others and
/// where they went (None: there were none).
unsafe fn submit_on(dev: &ID3D12Device, queue: &ID3D12CommandQueue, items: &[texture_upload::Item]) -> Result<(Vec<(usize, String)>, u64, Option<texture_upload::Sent>), String> {
unsafe fn submit_on(dev: &ID3D12Device, queue: &ID3D12CommandQueue, items: &[texture_upload::Item]) -> Result<(Vec<(usize, String)>, u64, Option<texture_upload::Sent>), UploadError> {
// The textures' tile mappings made since the queue last waited for them (a texture made on
// another thread after `refresh_once`'s wait, or its levels mapped just now by
// `map_levels_with_texels`): a copy into a tile still unmapped is a copy-engine page fault
// on NVIDIA (`DmaPageFaultWrite`).
super::vram::before_execute(queue);
if !super::vram::before_execute(queue) {
return Err(UploadError::MappingUnmarked);
}
let key = queue.as_raw() as usize;
let state = queue_upload_state(key);
let mut slot = state.lock().unwrap();
Expand All @@ -2669,7 +2685,7 @@ unsafe fn submit_on(dev: &ID3D12Device, queue: &ID3D12CommandQueue, items: &[tex
let fence = dev.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE).map_err(|e| e.to_string())?;
*slot = Some(QueueUploads { fence, value: 0, in_flight: std::collections::VecDeque::new(), bytes: 0 });
}
let Some(q) = slot.as_mut() else { return Err("no fence for the queue".into()) };
let Some(q) = slot.as_mut() else { return Err("no fence for the queue".to_string().into()) };
// (a piece that panicked on the way into the upload buffer: the batch fails before anything
// is executed and is tried again, `RETRY`, rather than going up with what the buffer held)
let submitted = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| texture_upload::submit(dev, queue, items)));
Expand All @@ -2683,7 +2699,7 @@ unsafe fn submit_on(dev: &ID3D12Device, queue: &ID3D12CommandQueue, items: &[tex
Ok(()) => drop(f),
Err(_) => std::mem::forget(f),
}
return Err(e.to_string());
return Err(e.to_string().into());
}
q.value += 1;
q.bytes += f.bytes;
Expand Down Expand Up @@ -2771,7 +2787,10 @@ fn requeue_generations(table: &mut Table, keys: &[(usize, u64)]) -> Vec<usize> {

/// What changed of the textures `todo` (written pages: `dirty`, sorted) goes up, in batches of up to
/// `BATCH_BYTES` and `BATCH_ITEMS` (one list each).
unsafe fn upload_all(todo: Vec<Due>, dirty: &[usize], page: usize, title: Option<&ID3D12CommandQueue>) {
unsafe fn upload_all(todo: Vec<Due>, dirty: &[usize], page: usize, title: Option<&ID3D12CommandQueue>) -> super::sparse::PreparationOutcome {
use super::sparse::PreparationOutcome;
let mut outcome = PreparationOutcome::Ready;
let mut prepared_lazy_levels = false;
static N: AtomicU64 = AtomicU64::new(0);
let warn = |e: &str| {
if N.fetch_add(1, Ordering::Relaxed) < 4 {
Expand Down Expand Up @@ -2845,6 +2864,7 @@ unsafe fn upload_all(todo: Vec<Due>, dirty: &[usize], page: usize, title: Option
if super::vram::level_pages() && super::vram::has_lazy_levels(d.res.as_raw() as usize) {
// (the queue waits for these mappings before the copies: `vram::before_execute`
// below; what a level's fresh pages held is another texture's, so all of it goes up)
prepared_lazy_levels = true;
if !map_levels_with_texels_in(&d.res, d.src, &d.tex, d.pages.as_deref()).is_empty() {
changed = None;
}
Expand Down Expand Up @@ -2908,14 +2928,22 @@ unsafe fn upload_all(todo: Vec<Due>, dirty: &[usize], page: usize, title: Option
super::submit_cost::textures_uploaded(items.len() - failed.len(), bytes as usize, t_submit.elapsed());
}
Err(e) => {
warn(&e);
match e {
UploadError::MappingUnmarked => outcome = PreparationOutcome::MappingUnmarked,
UploadError::Other(e) => warn(&e),
}
known.iter().for_each(texture_upload::forget);
// (no memory for a buffer, most likely: again in a moment)
let at = std::time::Instant::now() + RETRY_AFTER;
RETRY.lock().unwrap().extend(items.iter().zip(&generations).map(|(it, &generation)| (at, it.res.as_raw() as usize, generation)));
}
}
}
// Preparing lazy levels can map or unmap tiles after the title queue's first Wait.
if prepared_lazy_levels && outcome == PreparationOutcome::Ready && title.is_some_and(|q| !super::vram::before_execute(q)) {
return PreparationOutcome::MappingUnmarked;
}
outcome
}

/// textures of batches that could not be uploaded, with their registration generation, and when
Expand Down Expand Up @@ -3065,7 +3093,9 @@ pub unsafe fn probe_gpu_content(res: usize) {
dev.GetCopyableFootprints(&rd, 0, levels as u32, 0, Some(fps.as_mut_ptr()), Some(rows.as_mut_ptr()), Some(row_bytes.as_mut_ptr()), Some(&mut total));
let result = own_work(|| -> Result<Vec<Vec<u8>>, String> {
let queue = upload_queue(dev, device)?;
super::vram::before_execute(&queue);
if !super::vram::before_execute(&queue) {
return Err("could not queue a Signal or Wait for texture page mappings".into());
}
let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).map_err(|e| e.to_string())?;
let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).map_err(|e| e.to_string())?;
let mut rb: Option<ID3D12Resource> = None;
Expand Down
Loading
Loading