Kaynağa Gözat

Card memory the CPU maps: scratch switch gone, what the others measured; a test

Co-Authored-By: Claude Opus 5.5 <[email protected]>
local 2 gün önce
ebeveyn
işleme
2ded49e8da

+ 1 - 14
crates/victory-gpu/src/host.rs

@@ -1579,15 +1579,6 @@ impl<T> Spare<T> {
 /// Scratch chunks no list holds.
 static SPARE: Mutex<Spare<Chunk>> = Mutex::new(Spare::new());
 
-/// Whether lists' scratch memory (constants and tables, which shaders read)
-/// lies in the card's memory, written by the CPU through the bus
-/// (`VICTORY_SCRATCH_BAR=1`; the default is upload memory, which the card
-/// reads through it).
-fn scratch_on_card() -> bool {
-    static ON: OnceLock<bool> = OnceLock::new();
-    *ON.get_or_init(|| std::env::var_os("VICTORY_SCRATCH_BAR").is_some_and(|v| v == "1"))
-}
-
 /// A scratch chunk of `size` bytes: one kept ([`SPARE`]) or a new one.
 fn take_chunk(host: &Host, size: u64) -> Option<Chunk> {
     if scratch_pool()
@@ -1598,11 +1589,7 @@ fn take_chunk(host: &Host, size: u64) -> Option<Chunk> {
         ALIVE[5].fetch_add(1, Ordering::Relaxed);
         return Some(chunk);
     }
-    let mapped = if scratch_on_card() { host.mapped_card_buffer(size, false) } else { None };
-    let (buffer, data) = match mapped {
-        Some(made) => made,
-        None => host.upload_buffer(size)?,
-    };
+    let (buffer, data) = host.upload_buffer(size)?;
     name(&buffer, || "list scratch".into());
     ALIVE[1].fetch_add(1, Ordering::Relaxed);
     ALIVE[2].fetch_add(size, Ordering::Relaxed);

+ 47 - 0
crates/victory-gpu/src/host/awake.rs

@@ -202,3 +202,50 @@ fn run(device: &ID3D12Device, wanted: &dyn Fn() -> bool, stop: &AtomicBool) -> w
         result
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    /// Card memory the CPU maps holds what the CPU wrote, with the card kept
+    /// awake meanwhile; the thread ends when it is let go.
+    #[test]
+    fn mapped_card_memory_is_written_by_the_cpu_and_read_by_the_card() {
+        let Some(host) = crate::host::host() else {
+            return;
+        };
+        let awake = start(&host.device, || true);
+        assert!(awake.is_some());
+        let Some((buffer, data)) = host.mapped_card_buffer(4096, true) else {
+            return;
+        };
+        let (seen, back) = host.shared_buffer(4096).unwrap();
+        // SAFETY: mappings of 4096 bytes; a list, a queue and a fence of the
+        // test's own, and nothing is read before the copy has run.
+        let read = unsafe {
+            for i in 0..4096 {
+                data.add(i).write((i * 7 + i / 251) as u8);
+            }
+            let device = &host.device;
+            let queue: ID3D12CommandQueue = device
+                .CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC {
+                    Type: D3D12_COMMAND_LIST_TYPE_DIRECT,
+                    ..Default::default()
+                })
+                .unwrap();
+            let allocator: ID3D12CommandAllocator = device.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap();
+            let list: ID3D12GraphicsCommandList =
+                device.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &allocator, None).unwrap();
+            list.CopyBufferRegion(&seen, 0, &buffer, 0, 4096);
+            list.Close().unwrap();
+            queue.ExecuteCommandLists(&[Some(list.cast().unwrap())]);
+            let fence: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
+            queue.Signal(&fence, 1).unwrap();
+            // No event: returns when the fence is there.
+            fence.SetEventOnCompletion(1, windows::Win32::Foundation::HANDLE::default()).unwrap();
+            std::slice::from_raw_parts(back, 4096).to_vec()
+        };
+        assert!(read.iter().enumerate().all(|(i, &b)| b == (i * 7 + i / 251) as u8));
+        drop(awake);
+    }
+}

+ 4 - 1
crates/victory-gpu/src/host/send.rs

@@ -96,7 +96,10 @@ fn kept() -> u64 {
 /// Whether the memory lies in the card's memory, written by the CPU through
 /// the bus (`VICTORY_SEND_BAR=1`): the copies on the card's drawing queue, which
 /// nothing overlaps, then cross no bus. In a captured frame they took 5.4 ms of
-/// the card's 30 (342 copies).
+/// the card's 30 (342 copies), and take 0.4 this way; but filling it takes the
+/// submitting thread 8-9 ms a frame instead of 3 (73-93 MB, run card-3 against
+/// card-1), so it is a loss while the card waits for that thread: off by
+/// default.
 fn on_card() -> bool {
     static ON: OnceLock<bool> = OnceLock::new();
     *ON.get_or_init(|| std::env::var_os("VICTORY_SEND_BAR").is_some_and(|v| v == "1"))

+ 3 - 1
crates/victory-gpu/src/shadow.rs

@@ -456,7 +456,9 @@ fn window_card_for(size: u64) -> Option<ID3D12Resource> {
 /// default, none): one buffer (`D3D12_HEAP_TYPE_GPU_UPLOAD`), no CPU's side, no
 /// copy to send and no list of changes, unlike [`window_card_most`]. The CPU
 /// only writes them, as no shader is declared to write a window; if one were,
-/// its snapshot and compare would read through the bus, right but slow.
+/// its snapshot and compare would read through the bus, right but slow. In a
+/// captured frame the lists take 26 ms of the card instead of 29, the frame 32
+/// instead of 36; filling them costs the CPU what it did.
 ///
 /// Windows go there while a fifth of the card's budget is free, as with
 /// [`window_room_for`], which this comes before.