Merge branch 'local/next' into night/cpu

This commit is contained in:
local committed 2026-11-25 09:03:29 -06:00
commit e7c7210657
5 files changed
+655 -26

No files matched your search

+154 -4
View File
@@ -202,6 +202,150 @@ fn vram_idle_for(video_memory: Option<[u64; 4]>, keep: bool) -> u64 {
}
}
/// Whether new memory is made resident, and waited for, before anything can
/// name it (on by default; `VICTORY_WAIT_RESIDENT=0`: as before). A heap or
/// resource of the default kind is resident once it is made, but its
/// mapping into the card's address space is work of the system's that runs
/// later: only lists sent after it wait for it. Lists sent before run on,
/// and read our descriptor heaps as they are: a slot that shows a texture
/// in a new block is a page fault for them until the block is mapped. The
/// device losses of the night of 2026-11-25 were that (run n08: the fault
/// lay in a texture made in the same tenth of a second, in a new block).
fn wait_resident() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_WAIT_RESIDENT").is_none_or(|v| v != "0"))
}
/// The flag memory is made with that [`resident`] waits for.
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() {
return Ok(());
}
let started = std::time::Instant::now();
let object: ID3D12Pageable = object.cast()?;
// SAFETY: a live heap or resource of the device's.
let made = unsafe { device.MakeResident(&[Some(object)]) };
let us = started.elapsed().as_micros() as u64;
RESIDENT[0].fetch_add(1, Ordering::Relaxed);
RESIDENT[1].fetch_add(us, Ordering::Relaxed);
RESIDENT[2].fetch_max(us, Ordering::Relaxed);
static SAID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let n = RESIDENT[0].load(Ordering::Relaxed);
if n >= 1000 * (SAID.load(Ordering::Relaxed) + 1) {
SAID.store(n / 1000, Ordering::Relaxed);
tracing::info!(
"new memory waited for (VICTORY_WAIT_RESIDENT=0: not), since the start: {n} times, {:.1} ms, the longest {:.1} ms",
RESIDENT[1].load(Ordering::Relaxed) as f64 / 1000.0,
RESIDENT[2].load(Ordering::Relaxed) as f64 / 1000.0
);
}
made
}
/// Times, microseconds and the longest of [`resident`].
static RESIDENT: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3];
/// Whether memory the card was given stays with it while there is room (on
/// by default; `VICTORY_KEEP_FREED=0`: it is given back as before). A shader
/// that reads past a view (a raw load through a typed, structured or
@@ -7762,7 +7906,7 @@ impl Host {
self.device
.CreateCommittedResource(
&heap,
D3D12_HEAP_FLAG_NONE,
not_resident(),
&desc,
D3D12_RESOURCE_STATE_GENERIC_READ,
None,
@@ -7770,6 +7914,7 @@ impl Host {
)
.and_then(|()| {
let b = buffer.as_ref().ok_or_else(windows::core::Error::empty)?;
resident(&self.device, b)?;
b.Map(0, None, Some(&mut data))
})
};
@@ -8466,7 +8611,7 @@ impl Host {
self.device
.CreateCommittedResource(
&heap,
D3D12_HEAP_FLAG_NONE,
not_resident(),
&desc,
D3D12_RESOURCE_STATE_COMMON,
None,
@@ -8474,6 +8619,8 @@ 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))
})
};
@@ -8553,7 +8700,7 @@ impl Host {
self.device
.CreateCommittedResource(
&heap,
D3D12_HEAP_FLAG_NONE,
not_resident(),
&desc,
D3D12_RESOURCE_STATE_COMMON,
None,
@@ -8561,6 +8708,8 @@ 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))
})
};
@@ -8601,7 +8750,7 @@ impl Host {
let made = unsafe {
self.device.CreateCommittedResource(
&heap,
D3D12_HEAP_FLAG_NONE,
not_resident(),
&desc,
D3D12_RESOURCE_STATE_COMMON,
None,
@@ -8612,6 +8761,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))).and_then(|()| mapped(&self.device));
match made {
Ok(()) => buffer,
Err(e) => {