| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143 |
- //! Title memory by address, for translated shaders that read and write
- //! through raw 40-bit addresses (`victory_shader::emit::SLOT_MEMORY`: the
- //! console's ray-tracing structure builder and the programs that walk its
- //! structures; the structures themselves lie in plain title memory, in no
- //! buffer object).
- //!
- //! A 64 KiB page in a title buffer that holds all of it is in that buffer's
- //! copy ([`crate::shadow`]), which every other access uses. A page outside
- //! buffers has a page of its own in an **arena**: memory of ours with a copy
- //! on the card ([`crate::shadow::by_address`]). The streamed structures lie
- //! anywhere between other data (6.6 GB of title memory in 4 MiB windows, run
- //! light-1; 8 GB in windows of 256 KiB, light-2), so a page is what is kept.
- //!
- //! This module keeps what a shader needs to find the pages:
- //!
- //! - the **directory**: per 64 KiB page of title address space the number
- //! of the copy that holds the page (0: none) and the page's byte offset
- //! in it; one for all lists, entries only ever go from empty to filled.
- //! The CPU writes it in memory of its own and shaders read a copy of that
- //! on the card, brought up to date as title memory is (over the bus, every
- //! step of every ray waited for it);
- //! - per view heap a run of slots with a raw unordered access view of each
- //! numbered copy (slot 0 of the run: a null view, for pages without a
- //! copy);
- //! - the **faults**: the page numbers shaders found empty, read back here
- //! once a frame, so the pages are there from the next frame on. A pointer a
- //! shader takes out of memory cannot be known before it runs.
- //!
- //! **A page that is not there when a shader touches it is a lost access**:
- //! the read gives 0, the store is dropped, and the fault note only helps
- //! the next frame. For the structure builder that is the same as a dropped
- //! stage, so before one of its dispatches the host makes ready what it can
- //! see the dispatch name:
- //!
- //! - 256 KiB from each root view outside buffers, and where title memory has a
- //! packed ("dehydrated") structure there, all of it: the title ships its
- //! structures packed, and the builder's rehydration stages unpack each in
- //! place once it is streamed in;
- //! - 4 MiB from each unordered access view outside buffers (structures built);
- //! - the page of each pair of root constants that is a title address and of
- //! each root constant that is one in 256-byte units;
- //! - the first page of every structure a list's upload memory names
- //! (a top-level build's instances).
- //!
- //! **What comes back to title memory**: what the builder stores in title
- //! buffers of at most [`BACK`] bytes it names (the sizes the title reads to
- //! make a structure smaller). Structures and scratch memory stay on the card
- //! (the first whole run of the builder wrote garbage over the title's
- //! objects). So an arena's page is the only place a structure is whole, and
- //! title memory must not go over it: of a page the title wrote only the
- //! dwords that changed since they were last taken go up. A packed structure
- //! goes up whole when the builder comes to it, whatever is known of it: the
- //! same bytes streamed in again do not differ from those last taken, and the
- //! card holds them unpacked (run light-3: a quarter of the structures
- //! unpacked a second time, into garbage all over their neighbours).
- //!
- //! **Stores are bounded**: a structure the builder misreads makes it store
- //! anywhere. Pages of title buffers take its stores only once one of its
- //! dispatches was given that buffer as an unordered access view (its scratch
- //! memory, its results); what else a store reaches is an arena's.
- //!
- //! A packed structure is gone when title memory has no header of it any more
- //! (looked at over [`SWEEP`] frames); the pages it alone has are given back.
- //!
- //! shortcut: a page held for anything else is kept for the rest of the run.
- //! shortcut: pages a list does not name are looked at again in turn, over
- //! [`SWEEP`] frames.
- use std::collections::HashMap;
- use std::sync::{Arc, Mutex};
- use victory_shader::emit::{MEMORY_FAULTS, MEMORY_PAGES, MEMORY_READ_ONLY};
- use windows::Win32::Graphics::Direct3D12::*;
- use crate::descriptor::host::HostHeap;
- use crate::host::{HostList, host};
- use crate::shadow::Shadow;
- /// Copies that can be numbered (the run of heap slots is one longer).
- const COPIES: u32 = (1 << 12) - 1;
- /// A directory page.
- const PAGE: u64 = 1 << 16;
- /// An arena's bytes.
- const ARENA: u64 = 64 << 20;
- /// What is compared at a time.
- const PIECE: usize = 4096;
- /// Pages held, at most (3 GiB).
- const MOST: usize = 3 << 14;
- /// Pages of title memory with small title buffers in them, at most.
- const WINDOWS: usize = 512;
- /// The biggest packed structure taken for one.
- const PACKED: u64 = 64 << 20;
- /// For so many frames what a page holds may still be on its way.
- const YOUNG: u64 = 4;
- /// Free pages there are, at least, when a list is recorded.
- const SPARE: usize = 1024;
- /// A packed structure not named for so many frames is another one.
- const AGAIN: u64 = 8;
- /// From a root view; from one that is written (a structure being built).
- const VIEW: u64 = 256 << 10;
- const BUILT: u64 = 4 << 20;
- /// The biggest title buffer whose builder results come back.
- const BACK: u64 = 256 << 10;
- /// Pages taken from the faults, of one frame and in all, at most: a ray in a
- /// structure that is not right goes anywhere, and 2,048 a frame took all the
- /// pages there are in the first seconds (run light-4).
- const FAULTS: usize = 128;
- const FAULTS_ALL: usize = 2048;
- /// Frames over which every page is looked at once.
- const SWEEP: usize = 128;
- /// What a draw or dispatch does with title memory by address.
- #[derive(Clone, Copy, PartialEq)]
- pub(crate) enum Use {
- Reads,
- /// Stores by address, as the structure builder; `back`: its results in
- /// small title buffers come back.
- Builds { back: bool },
- }
- /// A page of title memory in an arena.
- struct Held {
- /// Where in the arena's memory, and the arena's place in `copies`.
- at: usize,
- copy: usize,
- /// The write-watch asking after which it was last taken (0: never).
- taken: u64,
- /// The frame it was last looked at in, and the one it came in.
- looked: u64,
- born: u64,
- /// Packed structures that have bytes in it, and whether it is held for
- /// them alone.
- owners: u32,
- theirs: bool,
- }
- /// A packed structure the builder came to.
- struct Packed {
- /// The frame it last did.
- frame: u64,
- len: u64,
- /// Its first two dwords in title memory.
- header: u64,
- }
- struct Memory {
- /// The CPU's entries (number, offset), and the copy shaders read.
- directory: (*mut u32, Arc<Shadow>),
- faults: (ID3D12Resource, *mut u32, u64),
- /// Copy `n` is `copies[n - 1]`.
- copies: Vec<Arc<Shadow>>,
- /// Places of title buffers' copies among `copies`.
- numbers: HashMap<usize, usize>,
- /// Arenas: place in `copies`, address.
- arenas: Vec<(usize, usize)>,
- /// By title page.
- pages: HashMap<u64, Held>,
- /// Their numbers in the order they came, and where the sweep is.
- order: Vec<u64>,
- sweep: usize,
- /// Windows of title memory itself ([`Memory::map`]).
- windows: usize,
- /// Packed structures the builder came to: the frame it last did.
- packed: HashMap<u64, Packed>,
- /// Their addresses in the order they came, and where the sweep is.
- packed_order: Vec<u64>,
- packed_sweep: usize,
- /// Places of pages that are free: the copy, the address in the arena.
- free: Vec<(usize, usize)>,
- /// Copies of title buffers the builder may store in.
- open: std::collections::HashSet<usize>,
- /// Pages taken from faults.
- faulted: usize,
- /// Per host view heap (by address): the first slot of its run, and how
- /// many copies have their view there.
- heaps: HashMap<usize, (u32, u32)>,
- /// The frame the faults were last read in.
- frame: u64,
- /// Pages asked for this frame that are not the title's memory.
- refused: std::collections::HashSet<u64>,
- stats: Stats,
- #[cfg(feature = "diagnostics")]
- dump: dump::Dump,
- }
- /// Since the last report.
- #[derive(Default)]
- struct Stats {
- from: Option<(std::time::Instant, u64)>,
- uses: [u64; 2],
- faults: u64,
- unknown: u64,
- back: u64,
- /// Pages looked at, bytes that changed, packed structures sent.
- looked: u64,
- changed: u64,
- packed: u64,
- packed_bytes: u64,
- gone: u64,
- /// Pages that came for an address in constants, a written view, another
- /// view, a structure named in upload memory.
- came: [u64; 4],
- micros: u64,
- }
- // SAFETY: the mappings are of memory the struct owns; all access is under
- // the mutex.
- unsafe impl Send for Memory {}
- static MEMORY: Mutex<Option<Memory>> = Mutex::new(None);
- /// What a draw binds.
- pub(crate) struct Ready {
- pub(crate) directory: u64,
- pub(crate) faults: u64,
- /// The view heap slot of the null view; copy `n`'s view is `n` further.
- pub(crate) base: u32,
- }
- impl Memory {
- fn new() -> Option<Memory> {
- let host = host()?;
- let bytes = u64::from(MEMORY_PAGES) * 8;
- // Zeroed, and watched for writes as the title's memory is.
- let entries = crate::upload::reserve(bytes as usize)?;
- let directory = crate::shadow::by_address(entries as u64, bytes)?;
- crate::host::name(&directory.buffer, || "title memory by address: directory".into());
- let (faults, data) = host.shared_buffer(u64::from(MEMORY_FAULTS) * 4)?;
- crate::host::name(&faults, || "title memory by address: faults".into());
- // SAFETY: a fresh mapping of at least that many bytes.
- unsafe { std::ptr::write_bytes(data, 0, MEMORY_FAULTS as usize * 4) };
- // SAFETY: a live buffer.
- let gpu = unsafe { faults.GetGPUVirtualAddress() };
- tracing::info!("title memory by address: a shader reads or writes through raw addresses");
- Some(Memory {
- directory: (entries as *mut u32, directory),
- faults: (faults, data.cast::<u32>(), gpu),
- copies: Vec::new(),
- numbers: HashMap::new(),
- arenas: Vec::new(),
- pages: HashMap::new(),
- order: Vec::new(),
- sweep: 0,
- windows: 0,
- packed: HashMap::new(),
- packed_order: Vec::new(),
- packed_sweep: 0,
- free: Vec::new(),
- open: Default::default(),
- faulted: 0,
- heaps: HashMap::new(),
- frame: u64::MAX,
- refused: Default::default(),
- stats: Stats::default(),
- #[cfg(feature = "diagnostics")]
- dump: Default::default(),
- })
- }
- /// The directory's entry of `page`.
- fn entry(&self, page: u64) -> *mut u32 {
- debug_assert!(page < u64::from(MEMORY_PAGES));
- // SAFETY: inside the directory.
- unsafe { self.directory.0.add(page as usize * 2) }
- }
- /// The place in `copies` of the copy that holds `page`, if one does.
- fn copy_of(&self, page: u64) -> Option<usize> {
- if page >= u64::from(MEMORY_PAGES) {
- return None;
- }
- // SAFETY: an entry of the directory.
- ((unsafe { self.entry(page).read() } & !MEMORY_READ_ONLY) as usize).checked_sub(1)
- }
- /// Whether `page` can be given a place: in the directory, not turned down
- /// this frame, and the title's GPU memory. (A shader's pointer, or a
- /// constant taken for an address, may be anything.)
- fn possible(&mut self, page: u64) -> bool {
- // Not the last entry: addresses past the directory read it, and it
- // stays empty.
- if page < 0x1_0000 || page >= u64::from(MEMORY_PAGES) - 1 || self.refused.contains(&page) {
- return false;
- }
- let at = (page * PAGE) as usize;
- let ours = crate::texture::committed(at as *const u8, PAGE as usize)
- && crate::watch::writes(at, at + PAGE as usize, true).is_some();
- if !ours {
- self.refused.insert(page);
- self.stats.unknown += 1;
- }
- ours
- }
- /// Gives a copy a number, if one is left.
- fn number(&mut self, shadow: Arc<Shadow>) -> Option<usize> {
- let key = Arc::as_ptr(&shadow) as usize;
- if let Some(&n) = self.numbers.get(&key) {
- return Some(n);
- }
- if self.copies.len() >= COPIES as usize {
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| tracing::warn!(COPIES, "title memory by address: no number left for another copy; its pages read zeros"));
- return None;
- }
- // SAFETY: a live buffer.
- let flags = unsafe { shadow.buffer.GetDesc() }.Flags;
- if flags.0 & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS.0 == 0 {
- return None;
- }
- self.copies.push(shadow);
- let n = self.copies.len() - 1;
- self.numbers.insert(key, n);
- Some(n)
- }
- /// Gives `page` a place, if it has none and the memory is the title's;
- /// whether it has one then.
- fn map(&mut self, page: u64) -> bool {
- if self.copy_of(page).is_some() {
- return true;
- }
- if !self.possible(page) {
- return false;
- }
- let start = page * PAGE;
- if crate::shadow::in_buffer(start, PAGE).is_some() {
- let Some(shadow) = crate::shadow::find(start, PAGE) else {
- return false;
- };
- let fresh = !self.numbers.contains_key(&(Arc::as_ptr(&shadow) as usize));
- let Some(n) = self.number(shadow.clone()) else {
- return false;
- };
- // Every page the copy holds whole, not only the one asked for.
- // shortcut: a page of a title buffer inside this one that has a
- // copy of its own is given this copy too.
- if fresh {
- let (first, len) = shadow.span();
- let mut at = first.next_multiple_of(PAGE);
- while at + PAGE <= first + len && at / PAGE < u64::from(MEMORY_PAGES) - 1 {
- let entry = self.entry(at / PAGE);
- // SAFETY: an entry of the directory.
- if unsafe { entry.read() } == 0 {
- close(entry, n, at - first);
- }
- at += PAGE;
- }
- }
- // SAFETY: as above.
- if unsafe { self.entry(page).read() } == 0 {
- close(self.entry(page), n, start - shadow.span().0);
- }
- return true;
- }
- // Small title buffers in it: what the builder stores in them comes
- // back, so title memory itself.
- if self.windows < WINDOWS && crate::shadow::small_buffer_in(start, PAGE, BACK) {
- if let Some(shadow) = crate::shadow::by_address(start, PAGE)
- && let Some(n) = self.number(shadow)
- {
- self.windows += 1;
- fill(self.entry(page), n, 0);
- return true;
- }
- return false;
- }
- if self.pages.len() >= MOST {
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| tracing::warn!(MOST, "title memory by address: no more pages are taken; others read zeros"));
- return false;
- }
- if let Some((copy, at)) = self.free.pop() {
- self.hold(page, at, copy);
- return true;
- }
- if !self.another() {
- return false;
- }
- let Some((copy, at)) = self.free.pop() else {
- return false;
- };
- self.hold(page, at, copy);
- true
- }
- /// Another arena's pages.
- fn another(&mut self) -> bool {
- let Some(at) = crate::upload::reserve(ARENA as usize) else {
- return false;
- };
- let Some(n) = crate::shadow::by_address(at as u64, ARENA).and_then(|s| self.number(s)) else {
- return false;
- };
- self.arenas.push((n, at));
- // The lowest first.
- self.free.extend((0..(ARENA / PAGE) as usize).rev().map(|i| (n, at + i * PAGE as usize)));
- true
- }
- /// `page` is held at `at` in the arena that is `copy`.
- fn hold(&mut self, page: u64, at: usize, copy: usize) {
- self.pages.insert(page, Held { at, copy, taken: 0, looked: u64::MAX, born: self.frame, owners: 0, theirs: false });
- self.order.push(page);
- fill(self.entry(page), copy, at as u64 - self.copies[copy].span().0);
- }
- /// The packed structure at `address` is gone: the pages it alone had are
- /// given back.
- fn gone(&mut self, address: u64) {
- let Some(packed) = self.packed.remove(&address) else { return };
- for page in address / PAGE..=(address + packed.len - 1) / PAGE {
- let Some(held) = self.pages.get_mut(&page) else { continue };
- held.owners = held.owners.saturating_sub(1);
- if held.owners == 0 && held.theirs {
- self.free.push((held.copy, held.at));
- self.pages.remove(&page);
- // SAFETY: an entry of the directory, under the mutex.
- unsafe { self.entry(page).write_volatile(0) };
- }
- }
- self.stats.gone += 1;
- }
- /// Whether the packed structure at `address` is still in title memory.
- fn there(&self, address: u64) -> bool {
- let mut header = [0u8; 8];
- crate::texture::committed(address as *const u8, 8) && {
- crate::host::copy_mapped(header.as_mut_ptr(), address as *const u8, 8, false);
- self.packed.get(&address).is_some_and(|p| p.header == u64::from_le_bytes(header))
- }
- }
- /// Looks at some of the pages and packed structures again.
- fn look_again(&mut self) {
- for _ in 0..self.order.len().div_ceil(SWEEP) {
- self.sweep = (self.sweep + 1) % self.order.len();
- let page = self.order[self.sweep];
- if self.pages.contains_key(&page) {
- self.bring(page, page + 1);
- } else {
- self.order.swap_remove(self.sweep);
- if self.order.is_empty() {
- break;
- }
- }
- }
- for _ in 0..self.packed_order.len().div_ceil(SWEEP) {
- self.packed_sweep = (self.packed_sweep + 1) % self.packed_order.len();
- let address = self.packed_order[self.packed_sweep];
- if !self.packed.contains_key(&address) {
- self.packed_order.swap_remove(self.packed_sweep);
- if self.packed_order.is_empty() {
- break;
- }
- } else if !self.there(address) {
- self.gone(address);
- }
- }
- }
- /// A dispatch of the builder was given `address` to write: if it is in a
- /// title buffer's copy, stores by address reach that from here on.
- fn opens(&mut self, address: u64) {
- let Some(n) = self.map(address / PAGE).then(|| self.copy_of(address / PAGE)).flatten() else {
- return;
- };
- if self.pages.contains_key(&(address / PAGE)) || !self.open.insert(n) {
- return;
- }
- // (Its pages hold what the CPU writes beside the builder's scratch.)
- self.copies[n].keep_stores();
- let (first, len) = self.copies[n].span();
- tracing::info!(
- start = format_args!("{first:#x}"),
- len,
- "title memory by address: the structure builder may store in a title buffer"
- );
- for page in first.div_ceil(PAGE)..(first + len) / PAGE {
- if self.copy_of(page) == Some(n) {
- fill(self.entry(page), n, page * PAGE - first);
- }
- }
- }
- /// [`Memory::map`] of the pages of `[address, address + len)`, up to the
- /// first that is not the title's, and what the title wrote there since
- /// they were taken goes into the arenas.
- fn ready(&mut self, address: u64, len: u64) {
- let first = address / PAGE;
- let mut end = first;
- while end * PAGE < address + len && self.map(end) {
- end += 1;
- }
- self.bring(first, end);
- }
- /// Takes the pages `[first, end)` that are in arenas from title memory
- /// where it wrote them.
- fn bring(&mut self, first: u64, end: u64) {
- // Once a frame: the builder names the same pages list after list.
- let frame = self.frame;
- if !(first..end).any(|page| self.pages.get(&page).is_some_and(|h| h.looked != frame)) {
- return;
- }
- let started = std::time::Instant::now();
- let answer = crate::watch::writes((first * PAGE) as usize, (end * PAGE) as usize, true);
- for page in first..end {
- let Some(held) = self.pages.get_mut(&page) else {
- continue;
- };
- if held.looked == frame {
- continue;
- }
- held.looked = frame;
- self.stats.looked += 1;
- let wrote = answer
- .as_ref()
- .map(|(_, w)| w.binary_search_by_key(&(page as usize), |w| w.0).map_or(0, |i| w[i].1));
- let now = answer.as_ref().map_or(1, |a| a.0);
- let stale = held.taken == 0 || wrote.is_none_or(|w| w > held.taken);
- if !stale {
- continue;
- }
- let never = std::mem::replace(&mut held.taken, now.max(1)) == 0;
- let shadow = &self.copies[held.copy];
- let base = held.at - shadow.span().0 as usize;
- let mut title = [0u8; PIECE];
- for piece in (0..PAGE as usize).step_by(PIECE) {
- let from = (page * PAGE) as usize + piece;
- // (Pages that are not there read as zeros.)
- crate::host::copy_mapped(title.as_mut_ptr(), from as *const u8, PIECE, false);
- // SAFETY: a page of an arena of ours, under the mutex.
- let ours = unsafe { std::slice::from_raw_parts_mut((held.at + piece) as *mut u8, PIECE) };
- self.stats.changed += merge(&title, ours, never) as u64 * 4;
- }
- if never {
- shadow.resend(base, base + PAGE as usize);
- }
- }
- self.stats.micros += started.elapsed().as_micros() as u64;
- }
- /// The bytes of a packed structure title memory has at `address` by now,
- /// and its first two dwords.
- fn packed_at(&self, address: u64) -> Option<(u64, u64)> {
- if address & 0xFF != 0 {
- return None;
- }
- let mut header = [0u8; 8];
- crate::host::copy_mapped(header.as_mut_ptr(), address as *const u8, 8, false);
- let header = u64::from_le_bytes(header);
- Some((packed_len(header)?, header))
- }
- /// At the submission of a list whose builder dispatch was given `address`
- /// outside buffers, in the order of its copies: what title memory has
- /// there now goes up. When the dispatch is recorded it may not be there
- /// yet: the title's queue waits for its streaming. (Nothing is asked of
- /// the write watch here: the submission's asking stands.)
- fn land(&mut self, address: u64) {
- let page = address / PAGE;
- if !crate::texture::committed((page * PAGE) as *const u8, PAGE as usize) {
- return;
- }
- let Some((len, header)) = self.packed_at(address) else {
- // Anything else: what came since its page was first taken.
- for page in page..=(address + VIEW - 1) / PAGE {
- if self.pages.get(&page).is_some_and(|h| h.born + YOUNG >= self.frame) {
- self.take(page, 0, PAGE as usize, false);
- }
- }
- return;
- };
- if let Some(known) = self.packed.get_mut(&address) {
- let soon = known.frame + AGAIN >= self.frame;
- known.frame = self.frame;
- if known.header == header && soon {
- return;
- }
- if known.header != header {
- self.gone(address);
- }
- }
- let last = (address + len - 1) / PAGE;
- if !self.packed.contains_key(&address) {
- self.packed.insert(address, Packed { frame: self.frame, len, header });
- self.packed_order.push(address);
- for page in page..=last {
- let new = self.copy_of(page).is_none();
- if new && !self.place(page) {
- continue;
- }
- if let Some(held) = self.pages.get_mut(&page) {
- held.owners += 1;
- held.theirs |= new;
- }
- }
- self.stats.packed_bytes += len;
- }
- for page in page..=last {
- let from = address.max(page * PAGE) - page * PAGE;
- let to = (address + len).min((page + 1) * PAGE) - page * PAGE;
- self.take(page, from as usize, to as usize, true);
- }
- self.stats.packed += 1;
- }
- /// A place in an arena for a page of committed title memory, at a
- /// submission: only one there is already (the list brings up the arenas
- /// it knew when it was recorded).
- fn place(&mut self, page: u64) -> bool {
- if page < 0x1_0000
- || page >= u64::from(MEMORY_PAGES) - 1
- || !crate::texture::committed((page * PAGE) as *const u8, PAGE as usize)
- || crate::shadow::in_buffer(page * PAGE, PAGE).is_some()
- {
- return false;
- }
- let Some((copy, at)) = self.free.pop() else {
- return false;
- };
- self.hold(page, at, copy);
- crate::watch::wrote(self.entry(page) as usize, 8);
- self.take(page, 0, PAGE as usize, true);
- true
- }
- /// Bytes `from..to` of `page` from title memory into its arena's page, at
- /// a submission; `whole`: sent whether they differ from those last taken
- /// or not.
- fn take(&mut self, page: u64, from: usize, to: usize, whole: bool) {
- let Some(held) = self.pages.get(&page) else { return };
- let shadow = &self.copies[held.copy];
- let base = held.at - shadow.span().0 as usize;
- let mut title = [0u8; PIECE];
- let mut at = from;
- while at < to {
- let n = (PIECE - at % PIECE).min(to - at);
- crate::host::copy_mapped(title.as_mut_ptr(), (page as usize * PAGE as usize + at) as *const u8, n, false);
- // SAFETY: a page of an arena of ours, under the mutex.
- let ours = unsafe { std::slice::from_raw_parts_mut((held.at + at) as *mut u8, n) };
- if *ours != title[..n] {
- ours.copy_from_slice(&title[..n]);
- crate::watch::wrote(held.at + at, n);
- }
- at += n;
- }
- if whole {
- shadow.resend(base + from, base + to);
- }
- }
- /// The pages shaders found empty since the last frame.
- fn take_faults(&mut self) {
- let mut pages = Vec::new();
- for i in 1..MEMORY_FAULTS as usize {
- // SAFETY: inside the faults' mapping; the GPU may be writing,
- // a number missed now is written again next frame.
- let at = unsafe { self.faults.1.add(i) };
- // SAFETY: as above.
- let page = unsafe { at.read_volatile() };
- if page != 0 {
- pages.push(u64::from(page));
- // SAFETY: as above.
- unsafe { at.write_volatile(0) };
- }
- }
- if pages.is_empty() {
- return;
- }
- pages.sort_unstable();
- let mut made = 0;
- for &page in &pages {
- if made < FAULTS && self.faulted < FAULTS_ALL && self.copy_of(page).is_none() && self.map(page) {
- made += 1;
- self.faulted += 1;
- self.bring(page, page + 1);
- }
- }
- self.stats.faults += pages.len() as u64;
- // Each is an access that read 0 or was dropped.
- static SAID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
- let said = SAID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
- if said < 40 || said.is_multiple_of(100) {
- let some: Vec<String> = pages
- .iter()
- .step_by(pages.len().div_ceil(12))
- .map(|&p| format!("{:#x}", p * PAGE))
- .collect();
- tracing::warn!(
- frame = self.frame,
- pages = pages.len(),
- made,
- held = self.pages.len(),
- ?some,
- "title memory by address: shaders touched pages that had no copy (those accesses read 0 or were dropped); those that are the title's are there now"
- );
- }
- }
- /// The structures named in a list's upload memory around `address`
- /// (instances of a top-level build: a structure's address is their last
- /// 8 bytes): the first page of each.
- fn named_in_upload(&mut self, address: u64) {
- let Some((from, len)) = crate::upload::block(address as usize) else {
- return;
- };
- // SAFETY: the block is handed out and stays mapped; the title wrote
- // it before it recorded the dispatch.
- let words = unsafe { std::slice::from_raw_parts(from as *const u64, len / 8) };
- let mut seen = std::collections::HashSet::new();
- for &word in words {
- if word & 0xFF == 0 && (1 << 32..1 << 37).contains(&word) && seen.insert(word / PAGE) && self.map(word / PAGE) {
- let page = word / PAGE;
- self.bring(page, page + 1);
- }
- }
- }
- /// A small title buffer whose results come back, if its pages are
- /// windows of title memory itself (or a copy of the buffer's own).
- fn small(&mut self, list: &HostList, start: u64, len: u64) {
- let mut at = start;
- while at < start + len {
- let page = at / PAGE;
- let n = ((page + 1) * PAGE).min(start + len) - at;
- self.map(page);
- if let Some(k) = self.copy_of(page)
- && !self.pages.contains_key(&page)
- {
- self.copies[k].refresh(list, at, n);
- self.copies[k].gpu_writes(list, at, n);
- self.stats.back += 1;
- }
- at += n;
- }
- }
- /// Every copy is brought up to date when `list` is submitted, after what
- /// it deferred so far (once until a copy of the title's intervenes).
- fn bring_up(&self, list: &HostList) {
- let (start, len) = self.directory.1.span();
- self.directory.1.refresh(list, start, len);
- for shadow in &self.copies {
- let (start, len) = shadow.span();
- shadow.refresh(list, start, len);
- }
- }
- /// Every ten seconds or so, what was used.
- fn report(&mut self) {
- let now = std::time::Instant::now();
- let (since, frame) = *self.stats.from.get_or_insert((now, self.frame));
- if now.duration_since(since).as_secs_f64() < 10.0 {
- return;
- }
- let frames = self.frame.wrapping_sub(frame).max(1) as f64;
- let s = std::mem::take(&mut self.stats);
- tracing::info!(
- "title memory by address: {} pages ({} MB) in {} arenas, {} copies in all; per frame {:.0} dispatches that read, {:.0} that build, \
- {:.1} pages that had no copy, {:.1} pages turned down, {:.1} results brought back; {:.0} pages looked at, {:.1} KB changed, \
- {:.1} packed structures ({} there), {:.2} ms; {} windows; since the last line {} KB of packed structures came and {} of them went, \
- and pages for constants {}, views written {}, others {}, upload memory {}, faults in all {}",
- self.pages.len(),
- (self.pages.len() as u64 * PAGE) >> 20,
- self.arenas.len(),
- self.copies.len(),
- s.uses[0] as f64 / frames,
- s.uses[1] as f64 / frames,
- s.faults as f64 / frames,
- s.unknown as f64 / frames,
- s.back as f64 / frames,
- s.looked as f64 / frames,
- s.changed as f64 / 1024.0 / frames,
- s.packed as f64 / frames,
- self.packed.len(),
- s.micros as f64 / 1000.0 / frames,
- self.windows,
- s.packed_bytes >> 10,
- s.gone,
- s.came[0],
- s.came[1],
- s.came[2],
- s.came[3],
- self.faulted
- );
- self.stats.from = Some((now, self.frame));
- }
- }
- /// [`fill`] of a page stores by address do not reach.
- fn close(entry: *mut u32, n: usize, offset: u64) {
- fill(entry, n | MEMORY_READ_ONLY as usize, offset);
- }
- /// The offset first: a shader that sees the number sees it too.
- fn fill(entry: *mut u32, n: usize, offset: u64) {
- // SAFETY: an entry of the directory, under the mutex.
- unsafe {
- entry.add(1).write_volatile(offset as u32);
- entry.write_volatile(n as u32 + 1);
- }
- }
- /// The bytes of a packed structure whose first two dwords these are: the
- /// version (6) with the marks of a bottom level and of one that is packed,
- /// and its size unpacked in 64-byte nodes.
- fn packed_len(header: u64) -> Option<u64> {
- let (version, nodes) = (header as u32, header >> 32);
- let marks = version >> 16;
- (version & 0xFFFF == 6 && marks & 0x8100 == 0x8100 && nodes > 0 && nodes * 64 <= PACKED).then_some(nodes * 64)
- }
- /// Puts what `title` holds into `ours` (what was last taken): the dwords that
- /// differ. How many.
- fn merge(title: &[u8; PIECE], ours: &mut [u8], never: bool) -> usize {
- if never {
- ours.copy_from_slice(title);
- return PIECE / 4;
- }
- if ours == title {
- return 0;
- }
- let mut changed = 0;
- for (t, o) in title.chunks_exact(4).zip(ours.chunks_exact_mut(4)) {
- if t != o {
- o.copy_from_slice(t);
- changed += 1;
- }
- }
- changed
- }
- /// Before a draw whose shader reads title memory by address: what its
- /// root arguments name is made ready (`addresses`: its root views, of them
- /// `written` those it writes; `constants`: its root constants, see the
- /// module), every numbered copy
- /// has its view in `heap`'s run and is brought up to date when `list` is
- /// submitted. None if the host cannot hold it; the draw is then dropped.
- pub(crate) fn ready(
- list: &HostList,
- heap: &HostHeap,
- addresses: &[u64],
- written: &[u64],
- constants: &[u32],
- use_: Use,
- ) -> Option<Ready> {
- let mut guard = MEMORY.lock().unwrap_or_else(|e| e.into_inner());
- if guard.is_none() {
- *guard = Memory::new();
- }
- let m = guard.as_mut()?;
- let frame = crate::frame::current();
- if m.frame != frame {
- m.frame = frame;
- m.refused.clear();
- m.take_faults();
- m.look_again();
- m.report();
- }
- let builds = use_ != Use::Reads;
- while builds && m.free.len() < SPARE && m.pages.len() + m.free.len() < MOST && m.another() {}
- m.stats.uses[usize::from(builds)] += 1;
- // Once per list and page named: the builder dispatches hundreds of times
- // a frame with the same arguments.
- const READ: usize = usize::MAX - 0x6d;
- let named = |kind: usize, at: u64| list.first_use(usize::MAX - (kind << 26) - (at / PAGE) as usize);
- // A title address in two constants (low dword, then bits 32 to 39), or
- // in one in 256-byte units (the builder's alone). A constant that only
- // looks like one names memory nothing reads; one that is not the title's
- // memory is refused.
- let held = |m: &Memory| m.pages.len() as u64;
- let before = held(m);
- for (i, &c) in constants.iter().enumerate() {
- if let Some(&high) = constants.get(i + 1)
- && (1..=0x1F).contains(&high)
- && c >= 0x1_0000
- {
- let at = u64::from(c) | u64::from(high) << 32;
- if named(1, at) {
- m.ready(at, 1);
- }
- }
- if builds && (0x0100_0000..0x2000_0000).contains(&c) {
- let at = u64::from(c) << 8;
- if named(1, at) {
- m.ready(at, 1);
- }
- }
- }
- let mut came = [held(m) - before, 0, 0, 0];
- for &at in addresses.iter().filter(|&&a| a >= 1 << 32) {
- // (Several structures to a page.)
- let mut packed = false;
- if builds
- && list.first_use(usize::MAX - (3 << 33) - (at >> 8) as usize)
- && crate::shadow::in_buffer(at, 1).is_none()
- && m.possible(at / PAGE)
- {
- // What it needs, as far as it is known now; the rest when the
- // list is submitted ([`Memory::land`]).
- if let Some((len, _)) = m.packed_at(at) {
- packed = true;
- for page in at / PAGE..=(at + len - 1) / PAGE {
- let new = m.copy_of(page).is_none();
- if m.map(page)
- && new
- && let Some(held) = m.pages.get_mut(&page)
- {
- held.theirs = true;
- }
- }
- m.bring(at / PAGE, (at + len - 1) / PAGE + 1);
- }
- list.defer_host(move || {
- if let Some(m) = MEMORY.lock().unwrap_or_else(|e| e.into_inner()).as_mut() {
- m.land(at);
- }
- });
- m.bring_up(list);
- }
- if builds && written.contains(&at) && list.first_use(usize::MAX - (4 << 33) - (at >> 2 & 0x7FFF_FFFF) as usize) {
- if let Some((start, len)) = crate::shadow::in_buffer(at, 1)
- && len <= BACK
- && use_ == (Use::Builds { back: true })
- && list.first_use(usize::MAX - (5 << 33) - (start >> 2 & 0x7FFF_FFFF) as usize)
- {
- m.small(list, start, len);
- }
- m.opens(at);
- if !packed && crate::shadow::in_buffer(at, 1).is_none() {
- #[cfg(feature = "diagnostics")]
- {
- let built = m.dump.built.entry(at).or_insert((frame, frame, 0));
- (built.1, built.2) = (frame, built.2 + 1);
- }
- let before = held(m);
- m.ready(at, BUILT);
- came[1] += held(m).saturating_sub(before);
- }
- }
- if packed || !named(2, at) {
- continue;
- }
- let before = held(m);
- match crate::shadow::in_buffer(at, 1) {
- Some(_) => {
- m.ready(at, 1);
- m.named_in_upload(at);
- came[3] += held(m).saturating_sub(before);
- }
- None => {
- m.ready(at, if builds { VIEW } else { PAGE });
- came[2] += held(m).saturating_sub(before);
- }
- }
- }
- for (sum, n) in m.stats.came.iter_mut().zip(came) {
- *sum += n;
- }
- let key = std::ptr::from_ref(heap) as usize;
- let (base, made) = match m.heaps.get(&key) {
- Some(&run) => run,
- None => {
- // Held for the heap's life: not given back with a list's slots.
- let base = heap.dynamic(COPIES + 1)?;
- // Every slot of the run: a number shows no view of another kind.
- for n in 0..=COPIES {
- heap.null_raw_uav_at(base + n);
- }
- (base, 0)
- }
- };
- for (n, shadow) in m.copies.iter().enumerate().skip(made as usize) {
- let (_, len) = shadow.span();
- heap.raw_uav_at(base + 1 + n as u32, &shadow.buffer, 0, len);
- }
- m.heaps.insert(key, (base, m.copies.len() as u32));
- // Every copy by every list that has come to by-address memory, whoever
- // numbered it: an arena another list made while this one was being
- // recorded was brought up to date by that list's submission alone, which
- // may run after this one's (structures unpacked from zeros, and then
- // written over by the arena's first upload: in frames that stream much in
- // half of them, run light-8).
- let first_read = list.first_use(READ);
- if list.first_use(usize::MAX - (6 << 33) - m.copies.len()) {
- m.bring_up(list);
- }
- if first_read {
- list.keep(&m.faults.0);
- #[cfg(feature = "diagnostics")]
- m.dump(list, addresses);
- }
- #[cfg(feature = "diagnostics")]
- if use_ == Use::Reads {
- m.dump.walked(addresses);
- }
- let directory = &m.directory.1;
- Some(Ready {
- directory: directory.gpu(directory.span().0),
- faults: m.faults.2,
- base,
- })
- }
- /// `VICTORY_MEMORY_DUMP=<folder>`: when a file `go` appears there, the arenas
- /// as the card holds them are copied out and written (`arena-<n>.bin`), with
- /// `pages.txt` (title page, arena, offset) and the structures the walking
- /// programs were given: to look at the structures offline.
- #[cfg(feature = "diagnostics")]
- mod dump {
- use super::*;
- #[derive(Default)]
- pub(super) struct Dump {
- walked: std::collections::BTreeSet<u64>,
- /// Views the builder was given to write outside buffers: the first
- /// and the last frame, how often.
- pub(super) built: std::collections::BTreeMap<u64, (u64, u64, u64)>,
- /// The frame it is written at, and the copies.
- due: Option<(u64, Vec<(ID3D12Resource, usize)>)>,
- done: bool,
- }
- impl Dump {
- pub(super) fn walked(&mut self, addresses: &[u64]) {
- if !self.done && self.walked.len() < 64 {
- self.walked.extend(addresses);
- }
- }
- }
- fn folder() -> Option<&'static std::path::PathBuf> {
- static FOLDER: std::sync::OnceLock<Option<std::path::PathBuf>> = std::sync::OnceLock::new();
- FOLDER.get_or_init(|| std::env::var_os("VICTORY_MEMORY_DUMP").map(Into::into)).as_ref()
- }
- fn turn(buffer: &ID3D12Resource, before: D3D12_RESOURCE_STATES, after: D3D12_RESOURCE_STATES) -> D3D12_RESOURCE_BARRIER {
- D3D12_RESOURCE_BARRIER {
- Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION,
- Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
- Anonymous: D3D12_RESOURCE_BARRIER_0 {
- Transition: std::mem::ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER {
- // SAFETY: the same pointer, not counted; the barrier is
- // used while the buffer lives.
- pResource: unsafe { std::mem::transmute_copy(buffer) },
- Subresource: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
- StateBefore: before,
- StateAfter: after,
- }),
- },
- }
- }
- impl Memory {
- /// At a list's first use: the copies are recorded into it.
- pub(super) fn dump(&mut self, list: &HostList, _: &[u64]) {
- let Some(folder) = folder() else { return };
- if let Some((due, _)) = &self.dump.due {
- if self.frame < *due {
- return;
- }
- let copies = self.dump.due.take().map(|d| d.1).unwrap_or_default();
- let mut text = String::new();
- for a in &self.dump.walked {
- text += &format!("walked {a:#x}\n");
- }
- for (a, (first, last, n)) in &self.dump.built {
- text += &format!("built {a:#x} {first} {last} {n}\n");
- }
- for (a, p) in &self.packed {
- text += &format!("packed {a:#x} {} {}\n", p.len, p.frame);
- }
- text += &format!("frame {}\n", self.frame);
- for (page, held) in &self.pages {
- let arena = self.arenas.iter().position(|a| a.0 == held.copy).unwrap_or(usize::MAX);
- let at = held.at - self.arenas[arena].1;
- text += &format!("{:#x} {arena} {at:#x}\n", page * PAGE);
- }
- let _ = std::fs::write(folder.join("pages.txt"), text);
- for (n, (_, data)) in copies.iter().enumerate() {
- // SAFETY: a mapping of an arena's bytes; the copies ran
- // frames ago.
- let bytes = unsafe { std::slice::from_raw_parts(*data as *const u8, ARENA as usize) };
- let _ = std::fs::write(folder.join(format!("arena-{n}.bin")), bytes);
- }
- tracing::info!(arenas = copies.len(), "title memory by address: the arenas are written out");
- return;
- }
- if self.dump.done || !folder.join("go").exists() {
- return;
- }
- self.dump.done = true;
- let Some(host) = host() else { return };
- let mut copies = Vec::new();
- for &(n, _) in &self.arenas {
- let buffer = &self.copies[n].buffer;
- let Some((to, data)) = host.shared_buffer(ARENA) else { break };
- // SAFETY: an open list; live buffers of those sizes. The list
- // has not used the arena yet.
- unsafe {
- list.list.ResourceBarrier(&[turn(buffer, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_SOURCE)]);
- list.list.CopyBufferRegion(&to, 0, buffer, 0, ARENA);
- list.list.ResourceBarrier(&[turn(buffer, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_COMMON)]);
- }
- list.keep(&to);
- copies.push((to, data as usize));
- }
- self.dump.due = Some((self.frame + 12, copies));
- }
- }
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn a_piece_takes_what_changed() {
- let mut title = [7u8; PIECE];
- let mut ours = vec![0u8; PIECE];
- assert_eq!(merge(&title, &mut ours, true), PIECE / 4);
- assert_eq!(ours, title);
- assert_eq!(merge(&title, &mut ours, false), 0);
- // A dword of it.
- title[41] = 8;
- assert_eq!(merge(&title, &mut ours, false), 1);
- assert_eq!(ours, title);
- }
- #[test]
- fn a_packed_structure_is_known_by_its_header() {
- let header = |version: u32, nodes: u32| u64::from(version) | u64::from(nodes) << 32;
- assert_eq!(packed_len(header(0x8102_0006, 0x9a1a)), Some(0x9a1a * 64));
- // Unpacked, a top level, another version, no size, too big.
- assert_eq!(packed_len(header(0x8002_0006, 0x9a1a)), None);
- assert_eq!(packed_len(header(0x0000_0006, 5)), None);
- assert_eq!(packed_len(header(0x8102_0007, 5)), None);
- assert_eq!(packed_len(header(0x8102_0006, 0)), None);
- assert_eq!(packed_len(header(0x8102_0006, 1 << 21)), None);
- }
- }
|