New card memory is waited for until a queue of its own has run after it (VICTORY_WAIT_RESIDENT=0: not)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
0322b39abd
commit
f1d3eb2b64
3 files changed
+101
-3
No files matched your search
@@ -204,6 +204,102 @@ pub(crate) fn not_resident() -> D3D12_HEAP_FLAGS {
|
||||
if wait_resident() { D3D12_HEAP_FLAG_CREATE_NOT_RESIDENT } else { D3D12_HEAP_FLAG_NONE }
|
||||
}
|
||||
|
||||
/// A queue of its own that new memory is waited for with ([`mapped`]).
|
||||
struct Aside {
|
||||
queue: ID3D12CommandQueue,
|
||||
list: ID3D12CommandList,
|
||||
fence: ID3D12Fence,
|
||||
event: windows::Win32::Foundation::HANDLE,
|
||||
value: u64,
|
||||
_keep: (ID3D12CommandAllocator, [ID3D12Resource; 2]),
|
||||
}
|
||||
// SAFETY: D3D12 objects are free-threaded, the event is waited on by one
|
||||
// thread at a time (the lock).
|
||||
unsafe impl Send for Aside {}
|
||||
|
||||
/// New memory of the card is mapped into its address space when this
|
||||
/// returns: a copy of a few bytes is sent to a queue of ours after the
|
||||
/// memory was made, which the system starts only when all mappings asked
|
||||
/// for so far are done, and waited for. (`MakeResident` came back before
|
||||
/// that: run n11 was lost as n08 was with it alone.) Best done off a hot
|
||||
/// path: a tenth of a millisecond to some.
|
||||
pub(crate) fn mapped(device: &ID3D12Device) -> windows::core::Result<()> {
|
||||
static ASIDE: Mutex<Option<Aside>> = Mutex::new(None);
|
||||
if !wait_resident() {
|
||||
return Ok(());
|
||||
}
|
||||
let started = std::time::Instant::now();
|
||||
let mut aside = ASIDE.lock().unwrap_or_else(|e| e.into_inner());
|
||||
// SAFETY: plain object creation; the objects the list names are kept.
|
||||
unsafe {
|
||||
if aside.is_none() {
|
||||
let buffer = |kind: D3D12_HEAP_TYPE, state: D3D12_RESOURCE_STATES| -> windows::core::Result<ID3D12Resource> {
|
||||
let mut made: Option<ID3D12Resource> = None;
|
||||
device.CreateCommittedResource(
|
||||
&D3D12_HEAP_PROPERTIES { Type: kind, ..Default::default() },
|
||||
D3D12_HEAP_FLAG_NONE,
|
||||
&D3D12_RESOURCE_DESC {
|
||||
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
|
||||
Width: 256,
|
||||
Height: 1,
|
||||
DepthOrArraySize: 1,
|
||||
MipLevels: 1,
|
||||
SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
|
||||
Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
|
||||
..Default::default()
|
||||
},
|
||||
state,
|
||||
None,
|
||||
&mut made,
|
||||
)?;
|
||||
made.ok_or_else(windows::core::Error::empty)
|
||||
};
|
||||
let from = buffer(D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ)?;
|
||||
let to = buffer(D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST)?;
|
||||
let queue: ID3D12CommandQueue = device.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC {
|
||||
Type: D3D12_COMMAND_LIST_TYPE_COPY,
|
||||
..Default::default()
|
||||
})?;
|
||||
let allocator: ID3D12CommandAllocator = device.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COPY)?;
|
||||
let list: ID3D12GraphicsCommandList = device.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COPY, &allocator, None)?;
|
||||
list.CopyBufferRegion(&to, 0, &from, 0, 16);
|
||||
list.Close()?;
|
||||
*aside = Some(Aside {
|
||||
queue,
|
||||
list: list.cast()?,
|
||||
fence: device.CreateFence(0, D3D12_FENCE_FLAG_NONE)?,
|
||||
event: CreateEventW(None, false, false, None)?,
|
||||
value: 0,
|
||||
_keep: (allocator, [from, to]),
|
||||
});
|
||||
}
|
||||
let Some(a) = aside.as_mut() else { return Ok(()) };
|
||||
a.queue.ExecuteCommandLists(&[Some(a.list.clone())]);
|
||||
a.value += 1;
|
||||
a.queue.Signal(&a.fence, a.value)?;
|
||||
if a.fence.GetCompletedValue() < a.value {
|
||||
a.fence.SetEventOnCompletion(a.value, a.event)?;
|
||||
WaitForSingleObject(a.event, INFINITE);
|
||||
}
|
||||
}
|
||||
let us = started.elapsed().as_micros() as u64;
|
||||
MAPPED[0].fetch_add(1, Ordering::Relaxed);
|
||||
MAPPED[1].fetch_add(us, Ordering::Relaxed);
|
||||
MAPPED[2].fetch_max(us, Ordering::Relaxed);
|
||||
let n = MAPPED[0].load(Ordering::Relaxed);
|
||||
if n == 1 || n.is_multiple_of(200) {
|
||||
tracing::info!(
|
||||
"new card memory waited for until it is mapped (VICTORY_WAIT_RESIDENT=0: not), since the start: {n} times, {:.1} ms, the longest {:.1} ms",
|
||||
MAPPED[1].load(Ordering::Relaxed) as f64 / 1000.0,
|
||||
MAPPED[2].load(Ordering::Relaxed) as f64 / 1000.0
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Times, microseconds and the longest of [`mapped`].
|
||||
static MAPPED: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3];
|
||||
|
||||
/// New memory (made with [`not_resident`]) is resident when this returns.
|
||||
pub(crate) fn resident(device: &ID3D12Device, object: &impl Interface) -> windows::core::Result<()> {
|
||||
if !wait_resident() {
|
||||
@@ -8374,6 +8470,7 @@ impl Host {
|
||||
.and_then(|()| {
|
||||
let b = buffer.as_ref().ok_or_else(windows::core::Error::empty)?;
|
||||
resident(&self.device, b)?;
|
||||
mapped(&self.device)?;
|
||||
b.Map(0, None, Some(&mut data))
|
||||
})
|
||||
};
|
||||
@@ -8462,6 +8559,7 @@ impl Host {
|
||||
.and_then(|()| {
|
||||
let b = buffer.as_ref().ok_or_else(windows::core::Error::empty)?;
|
||||
resident(&self.device, b)?;
|
||||
mapped(&self.device)?;
|
||||
b.Map(0, None, Some(&mut data))
|
||||
})
|
||||
};
|
||||
@@ -8513,7 +8611,7 @@ impl Host {
|
||||
if let Some(b) = &buffer {
|
||||
crate::book::made(b, || "an unordered access buffer".into());
|
||||
}
|
||||
let made = made.and_then(|()| buffer.as_ref().map_or(Ok(()), |b| resident(&self.device, b)));
|
||||
let made = made.and_then(|()| buffer.as_ref().map_or(Ok(()), |b| resident(&self.device, b))).and_then(|()| mapped(&self.device));
|
||||
match made {
|
||||
Ok(()) => buffer,
|
||||
Err(e) => {
|
||||
|
||||
@@ -382,7 +382,7 @@ pub(crate) unsafe fn texture(
|
||||
tracing::warn!(error = %e, "no block of texture memory");
|
||||
}
|
||||
if let Some(h) = &heap
|
||||
&& let Err(e) = crate::host::resident(device, h)
|
||||
&& let Err(e) = crate::host::resident(device, h).and_then(|()| crate::host::mapped(device))
|
||||
{
|
||||
tracing::warn!(error = %e, "a block of texture memory is not resident");
|
||||
return None;
|
||||
|
||||
@@ -2510,7 +2510,7 @@ fn host_texture_as(
|
||||
if !reserved
|
||||
&& made.is_ok()
|
||||
&& let Some(t) = &texture
|
||||
&& let Err(e) = crate::host::resident(&host.device, t)
|
||||
&& let Err(e) = crate::host::resident(&host.device, t).and_then(|()| crate::host::mapped(&host.device))
|
||||
{
|
||||
tracing::warn!(error = %e, desc = ?d, "a host texture is not resident");
|
||||
texture = None;
|
||||
|
||||
Reference in new issue
Block a user