| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391 |
- //! Command allocators and command lists.
- //!
- //! The title's inlined code writes packets into a list only while its put
- //! pointer is below a limit, and calls the list's method otherwise. Our
- //! lists keep the pointer and every limit at zero, so every command reaches
- //! the table as a call.
- //!
- //! Each list records onto a host command list as its methods are called
- //! (decision 0007); draw state lives in `crate::draw`. Methods not
- //! written yet log once with their caller, so a run lists what to do next.
- #![allow(clippy::not_unsafe_ptr_arg_deref)]
- use std::mem::offset_of;
- use crate::object::{
- self, ALLOCATOR, E_INVALIDARG, HRESULT, Iid, LIST, Object, S_OK, Table, This, guid,
- };
- use crate::queue::Written;
- use crate::resource::PAGEABLE_IIDS;
- const ALLOCATOR_IIDS: [Iid; 4] = [
- guid(
- 0x6102dee4,
- 0xaf59,
- 0x4b09,
- [0xb9, 0x99, 0xb4, 0x4d, 0x73, 0xf0, 0x9b, 0x24],
- ),
- PAGEABLE_IIDS[0],
- PAGEABLE_IIDS[1],
- PAGEABLE_IIDS[2],
- ];
- /// The list interfaces up to `ID3D12GraphicsCommandList6` (the table has
- /// all their slots): those whose ids are the same as on PC, the console's
- /// own List5 and List6 (the title asks for List6), then the
- /// device child's.
- const LIST_IIDS: [Iid; 10] = [
- guid(
- 0x7116d91c,
- 0xe7e4,
- 0x47ce,
- [0xb8, 0xc6, 0xec, 0x81, 0x68, 0xf4, 0x37, 0xe5],
- ),
- guid(
- 0x5b160d0f,
- 0xac1b,
- 0x4185,
- [0x8b, 0xa8, 0xb3, 0xae, 0x42, 0xa5, 0xa4, 0x55],
- ),
- guid(
- 0x553103fb,
- 0x1fe7,
- 0x4557,
- [0xbb, 0x38, 0x94, 0x6d, 0x7d, 0x0e, 0x7c, 0xa7],
- ),
- guid(
- 0x38c3e585,
- 0xff17,
- 0x412c,
- [0x91, 0x50, 0x4f, 0xc6, 0xf9, 0xd7, 0x2a, 0x28],
- ),
- guid(
- 0x6fda83a7,
- 0xb84c,
- 0x4e38,
- [0x9a, 0xc8, 0xc7, 0xbd, 0x22, 0x01, 0x6b, 0x3d],
- ),
- guid(
- 0x8754318e,
- 0xd3a9,
- 0x4541,
- [0x98, 0xcf, 0x64, 0x5b, 0x50, 0xdc, 0x48, 0x74],
- ),
- guid(
- 0x55050859,
- 0x4024,
- 0x474c,
- [0x98, 0xcf, 0x64, 0x5b, 0x51, 0xdc, 0x48, 0x74],
- ),
- guid(
- 0xc3827890,
- 0x4024,
- 0x474c,
- [0x98, 0xcf, 0x64, 0x5b, 0x51, 0xdc, 0x48, 0x74],
- ),
- PAGEABLE_IIDS[1],
- PAGEABLE_IIDS[2],
- ];
- #[repr(C)]
- struct Allocator {
- object: Object,
- kind: u32,
- }
- /// The layout the title's inlined code reads.
- #[repr(C)]
- struct List {
- object: Object,
- kind: u32,
- /// The put pointer, then the plain, draw, API and unbiased API limits:
- /// all zero, so every inline check fails.
- put: [u64; 5],
- reserved: [u32; 64],
- /// State the inline code keeps for building packets; unused by us.
- graphics_root_headers: [u32; 128],
- compute_root_headers: [u32; 128],
- pipeline_shadow: [u32; 8],
- /// Newer titles' lists are larger: the title's state flush
- /// (title+0x32786) copies a pipeline's 32 bytes to +0x544..+0x564 and
- /// tests +0x548, so its layout runs past 0x560. Its size is unknown;
- /// this keeps our fields clear of whatever the title writes there.
- newer: [u32; 256],
- /// Ours, after the console layout: memory handed out by
- /// [`allocate_upload`], kept until the list is reset; the host list the
- /// calls record into (decision 0007), and the draw state the title set.
- uploads: std::sync::Mutex<Vec<Vec<u8>>>,
- #[cfg(windows)]
- host: Option<crate::host::HostList>,
- #[cfg(windows)]
- state: std::sync::Mutex<crate::draw::State>,
- /// The parts of the recording before `host`, in order, each closed: the
- /// list is cut at its waits for memory values ([`cut`]).
- #[cfg(windows)]
- earlier: Vec<Part>,
- /// The waits recorded before the first work of `host`: the queue stands
- /// at them before it runs `host`.
- #[cfg(windows)]
- gate: Vec<ValueWait>,
- }
- /// One part of a title list cut at its waits: the waits the queue stands
- /// at, then the host list that runs once they are met.
- #[cfg(windows)]
- struct Part {
- waits: Vec<ValueWait>,
- host: crate::host::HostList,
- }
- const _: () = assert!(offset_of!(List, put) == 0x18);
- const _: () = assert!(offset_of!(List, pipeline_shadow) + 32 == 0x560);
- /// Runs `f` with the host list and draw state of a title list, if there
- /// is a host GPU.
- #[cfg(windows)]
- fn with_state(this: This, f: impl FnOnce(&crate::host::HostList, &mut crate::draw::State)) {
- let Some(list) = host_list(this) else { return };
- #[cfg(feature = "diagnostics")]
- let started = std::time::Instant::now();
- // SAFETY: the title passes one of our lists.
- let state = unsafe { &(*this.cast::<List>()).state };
- f(list, &mut state.lock().unwrap_or_else(|e| e.into_inner()));
- #[cfg(feature = "diagnostics")]
- crate::copystat::recorded(started);
- }
- /// The submission of `lists` has run: each gives its descriptor tables'
- /// slots back ([`crate::draw::State::tables_done`]). A list cut at waits
- /// keeps them: its state goes with its last part.
- #[cfg(windows)]
- pub(crate) fn tables_done(lists: &[This]) {
- for &this in lists {
- if this.is_null() {
- continue;
- }
- // SAFETY: the title passes our lists.
- let list = unsafe { &*this.cast::<List>() };
- if list.earlier.is_empty() {
- list.state.lock().unwrap_or_else(|e| e.into_inner()).tables_done();
- }
- }
- }
- /// The host list behind a title list, if there is a host GPU.
- #[cfg(windows)]
- pub(crate) fn host_list<'a>(this: This) -> Option<&'a crate::host::HostList> {
- if this.is_null() {
- return None;
- }
- // SAFETY: the title passes one of our lists.
- unsafe { (*this.cast::<List>()).host.as_ref() }
- }
- /// The host side of a submitted list, taken from it: a queue that waits
- /// for a fence runs it later, and the title may reset the list meanwhile.
- /// With it go the upload memory and the dynamic descriptor slots its
- /// commands name, kept until it has run.
- #[cfg(windows)]
- pub(crate) struct Detached {
- host: crate::host::HostList,
- _uploads: Vec<Vec<u8>>,
- _state: crate::draw::State,
- }
- #[cfg(windows)]
- impl Detached {
- pub(crate) fn host(&self) -> &crate::host::HostList {
- &self.host
- }
- }
- /// A host list that has run and waits to be used again, and the bytes of
- /// upload memory it keeps.
- #[cfg(windows)]
- struct Idle(crate::host::HostList, u64);
- // SAFETY: D3D12 objects are free-threaded, and an idle list is touched by
- // nobody until it is taken out again.
- #[cfg(windows)]
- unsafe impl Send for Idle {}
- /// Host lists that have run, reset and ready to record: the host side of
- /// a list a queue held, or of a part of a list cut at a wait, is taken
- /// from the title's list at its submission, and the title's list needs
- /// another. Each was made new and thrown away after its run: a hundred a
- /// frame in gameplay, each two command allocators and two lists, and for
- /// four in ten a budget and an argument buffer (run 61: "104 host lists
- /// made 21.2 ms" a frame, and the graphics kernel's time to free them).
- #[cfg(windows)]
- static IDLE: std::sync::Mutex<(Vec<Idle>, u64)> = std::sync::Mutex::new((Vec::new(), 0));
- /// The most host lists kept idle, and the most upload memory they may keep
- /// between them (a list keeps scratch chunks over a reset only while lists
- /// do not share them, `VICTORY_SCRATCH_POOL=0`).
- #[cfg(windows)]
- const IDLE_MOST: (usize, u64) = (128, 256 << 20);
- /// Whether host lists that have run are used again (on by default;
- /// `VICTORY_LIST_POOL=0`: each is made new and thrown away, as before).
- #[cfg(windows)]
- fn pools() -> bool {
- static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
- *ON.get_or_init(|| std::env::var_os("VICTORY_LIST_POOL").is_none_or(|v| v != "0"))
- }
- /// A host list open for recording: one that has run, as a title's list is
- /// after its reset, or a new one.
- #[cfg(windows)]
- fn fresh_list() -> Option<crate::host::HostList> {
- let idle = {
- let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
- let taken = idle.0.pop();
- if let Some(Idle(_, bytes)) = &taken {
- idle.1 -= bytes;
- }
- taken
- };
- match idle {
- Some(Idle(list, _)) => Some(list),
- None => crate::host::host().and_then(|h| h.new_list().ok()),
- }
- }
- /// The host sides of lists whose submission has run (submissions are
- /// waited for; `ran` names the ones it ran, by their place in `lists`):
- /// what they held is let go, as before, and the host lists are reset and
- /// kept for [`fresh_list`].
- #[cfg(windows)]
- pub(crate) fn recycle(lists: Vec<Detached>, ran: &[usize]) {
- if !pools() {
- return;
- }
- for (i, Detached { host, _uploads, _state }) in lists.into_iter().enumerate() {
- // The descriptor slots and the upload memory its commands named.
- drop(_state);
- drop(_uploads);
- if !ran.contains(&i) || IDLE.lock().unwrap_or_else(|e| e.into_inner()).0.len() >= IDLE_MOST.0 {
- continue;
- }
- // As the title's reset does: what the list kept for its commands
- // goes, its allocators stay.
- host.reset();
- let bytes = host.scratch_kept();
- let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
- if idle.0.len() < IDLE_MOST.0 && idle.1 + bytes <= IDLE_MOST.1 {
- idle.0.push(Idle(host, bytes));
- idle.1 += bytes;
- }
- }
- }
- /// A wait for a memory value recorded into list `this`, with waits cut
- /// into lists (`crate::queue::cuts`). On the console the queue runs the
- /// list up to the wait, stands there until the value is in memory, and
- /// runs on; the list's bottom-of-pipe writes before the wait are done by
- /// then, and other queues may be waiting for exactly those. So the
- /// recording is cut here: the host list so far is closed and kept as a
- /// part of its own ([`Part`]), the title's calls record into a new host
- /// list from here on, and the wait stands before that one. A submission
- /// then gives the queue the parts in order ([`detach_parts`]).
- ///
- /// A wait before any work (the first call after a reset, or a wait right
- /// after another) cuts nothing: it joins the waits before the current
- /// host list.
- /// shortcut: a bottom-of-pipe write is done when its part has run, not at
- /// its place in the part: a queue waiting for it starts later than on the
- /// console, never earlier, and nothing can wait for itself that way (a
- /// part has no wait after its first work).
- #[cfg(windows)]
- fn cut(this: This, wait: ValueWait) {
- if this.is_null() {
- return;
- }
- // SAFETY: the title passes one of our lists; a list is recorded by one
- // thread at a time and is not submitted while it records.
- let list = unsafe { &mut *this.cast_mut().cast::<List>() };
- let Some(current) = list.host.as_ref() else {
- return;
- };
- if current.untouched() {
- list.gate.push(wait);
- return;
- }
- let Some(fresh) = fresh_list() else {
- // shortcut: without a new host list the wait stands before the part
- // it was recorded in.
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| tracing::warn!("no host list for the part after a wait: the wait moves to the part's start"));
- list.gate.push(wait);
- return;
- };
- current.close();
- static CUTS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- if CUTS.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 {
- tracing::info!(parts = list.earlier.len() + 2, "a list is cut at a wait for a memory value");
- }
- if let Some(done) = list.host.replace(fresh) {
- let waits = std::mem::replace(&mut list.gate, vec![wait]);
- list.earlier.push(Part { waits, host: done });
- }
- // What the title set so far, on the new host list.
- if let Some(host) = list.host.as_ref() {
- list.state
- .lock()
- .unwrap_or_else(|e| e.into_inner())
- .replay(host);
- }
- }
- /// Whether list `this` recorded a wait for a memory value (with waits cut
- /// into lists).
- #[cfg(windows)]
- pub(crate) fn cut_list(this: This) -> bool {
- if this.is_null() {
- return false;
- }
- // SAFETY: one of our lists, closed and submitted: nothing records
- // into it.
- let list = unsafe { &*this.cast::<List>() };
- !list.earlier.is_empty() || !list.gate.is_empty()
- }
- /// What a queue is given for submitted lists that were cut at their waits:
- /// the waits it stands at, the host lists it runs once they are met, and
- /// the order those run in.
- #[cfg(windows)]
- pub(crate) type Parts = Vec<(Vec<ValueWait>, Vec<Detached>, Vec<usize>)>;
- /// Takes the host sides of the submitted `lists`, part by part ([`cut`]),
- /// and leaves each list a new, closed host list for its next reset, as
- /// [`detach`] does. What comes back is in the order the queue runs it: a
- /// new entry starts at every part that has waits; host lists without
- /// waits join the entry before them. The list's upload memory and draw
- /// state (its dynamic descriptor slots) go with its last part, which runs
- /// last.
- /// shortcut: a list the submission names a second time runs again only if
- /// it has one part and that part is in the entry being filled; otherwise
- /// the second naming is dropped (logged once).
- #[cfg(windows)]
- pub(crate) fn detach_parts(lists: &[This]) -> Parts {
- let mut parts: Parts = vec![(Vec::new(), Vec::new(), Vec::new())];
- // The lists taken: where its one host list is (entry, place), or none
- // for a list of several parts.
- let mut seen: Vec<(This, Option<(usize, usize)>)> = Vec::new();
- for &this in lists {
- if this.is_null() {
- continue;
- }
- if let Some(&(_, at)) = seen.iter().find(|(t, _)| *t == this) {
- match at {
- Some((entry, place)) if entry + 1 == parts.len() => parts[entry].2.push(place),
- _ => {
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| {
- tracing::warn!("a list named twice by a submission that waits for memory values runs once")
- });
- }
- }
- continue;
- }
- let Some(fresh) = fresh_list() else {
- continue;
- };
- // A new host list is open; the title's next call on its list is a
- // reset, which wants it closed.
- fresh.close();
- // SAFETY: one of our lists, closed and submitted: nothing records
- // into it until the title resets it.
- let list = unsafe { &mut *this.cast_mut().cast::<List>() };
- let Some(host) = list.host.replace(fresh) else {
- continue;
- };
- let earlier = std::mem::take(&mut list.earlier);
- let gate = std::mem::take(&mut list.gate);
- let uploads = std::mem::take(&mut *list.uploads.lock().unwrap_or_else(|e| e.into_inner()));
- let state = std::mem::take(&mut *list.state.lock().unwrap_or_else(|e| e.into_inner()));
- let single = earlier.is_empty() && gate.is_empty();
- let mut pieces: Vec<(Vec<ValueWait>, Detached)> = earlier
- .into_iter()
- .map(|part| {
- (
- part.waits,
- Detached {
- host: part.host,
- _uploads: Vec::new(),
- _state: Default::default(),
- },
- )
- })
- .collect();
- pieces.push((
- gate,
- Detached {
- host,
- _uploads: uploads,
- _state: state,
- },
- ));
- let mut last = None;
- for (waits, detached) in pieces {
- if !waits.is_empty() {
- parts.push((waits, Vec::new(), Vec::new()));
- }
- let entry = parts.len() - 1;
- let (_, taken, order) = &mut parts[entry];
- let place = taken.len();
- order.push(place);
- taken.push(detached);
- last = Some((entry, place));
- }
- seen.push((this, if single { last } else { None }));
- }
- // (The first entry is empty if the first list begins with a wait.)
- parts.retain(|(_, taken, _)| !taken.is_empty());
- parts
- }
- /// Takes the host sides of submitted lists (each once, however often the
- /// submission names it) and leaves each list a new, closed host list for
- /// its next reset: what was taken, and which of them run in what order.
- #[cfg(windows)]
- pub(crate) fn detach(lists: &[This]) -> (Vec<Detached>, Vec<usize>) {
- let mut taken: Vec<(This, Detached)> = Vec::new();
- let mut order = Vec::new();
- for &this in lists {
- if this.is_null() {
- continue;
- }
- if let Some(i) = taken.iter().position(|(t, _)| *t == this) {
- order.push(i);
- continue;
- }
- let Some(fresh) = fresh_list() else {
- continue;
- };
- // A new host list is open; the title's next call on its list is a
- // reset, which wants it closed.
- fresh.close();
- let list = this.cast_mut().cast::<List>();
- // SAFETY: one of our lists, closed and submitted: nothing records
- // into it until the title resets it.
- let (host, uploads, state) = unsafe {
- let Some(host) = (*list).host.replace(fresh) else {
- continue;
- };
- let uploads =
- std::mem::take(&mut *(*list).uploads.lock().unwrap_or_else(|e| e.into_inner()));
- let state =
- std::mem::take(&mut *(*list).state.lock().unwrap_or_else(|e| e.into_inner()));
- (host, uploads, state)
- };
- order.push(taken.len());
- taken.push((
- this,
- Detached {
- host,
- _uploads: uploads,
- _state: state,
- },
- ));
- }
- (taken.into_iter().map(|(_, d)| d).collect(), order)
- }
- pub(crate) extern "system" fn create_allocator(
- _: This,
- kind: u32,
- iid: *const Iid,
- out: *mut This,
- ) -> HRESULT {
- let allocator = Box::new(Allocator {
- object: Object::new(&ALLOCATOR_FUNCTIONS),
- kind,
- });
- object::hand_out(ALLOCATOR, allocator, iid, &ALLOCATOR_IIDS, out)
- }
- /// Lists start open, as in D3D12.
- pub(crate) extern "system" fn create_list(
- _: This,
- _nodes: u32,
- kind: u32,
- allocator: This,
- pipeline: This,
- iid: *const Iid,
- out: *mut This,
- ) -> HRESULT {
- new_list(kind, allocator, pipeline, iid, out)
- }
- /// The console's list creation: type, flags, node mask and barrier batch
- /// size in a description.
- #[repr(C)]
- #[derive(Debug)]
- pub(crate) struct ListDesc {
- kind: u32,
- flags: u32,
- nodes: u32,
- barrier_batch: u32,
- }
- pub(crate) extern "system" fn create_list_x(
- _: This,
- desc: *const ListDesc,
- allocator: This,
- pipeline: This,
- iid: *const Iid,
- out: *mut This,
- ) -> HRESULT {
- // SAFETY: the title passes a description (null is tolerated).
- let Some(desc) = (unsafe { desc.as_ref() }) else {
- return E_INVALIDARG;
- };
- tracing::debug!(?desc, "console command list");
- new_list(desc.kind, allocator, pipeline, iid, out)
- }
- /// `pipeline` is the list's initial pipeline (null: none), bound until the
- /// title sets another, as after a reset.
- fn new_list(
- kind: u32,
- allocator: This,
- pipeline: This,
- iid: *const Iid,
- out: *mut This,
- ) -> HRESULT {
- if allocator.is_null() {
- return E_INVALIDARG;
- }
- tracing::debug!(kind, "command list");
- let list = Box::new(List {
- object: if crate::device::newer() {
- Object::new(&NEWER_LIST_FUNCTIONS)
- } else {
- Object::new(&LIST_FUNCTIONS)
- },
- kind,
- put: [0; 5],
- reserved: [0; 64],
- graphics_root_headers: [0; 128],
- compute_root_headers: [0; 128],
- pipeline_shadow: [0; 8],
- newer: [0; 256],
- uploads: Default::default(),
- #[cfg(windows)]
- host: crate::host::host().and_then(|h| match h.new_list() {
- Ok(list) => Some(list),
- Err(e) => {
- tracing::warn!(error = %e, "no host command list");
- None
- }
- }),
- #[cfg(windows)]
- state: {
- let mut state = crate::draw::State::default();
- state.set_pipeline(pipeline);
- std::sync::Mutex::new(state)
- },
- #[cfg(windows)]
- earlier: Vec::new(),
- #[cfg(windows)]
- gate: Vec::new(),
- });
- #[cfg(not(windows))]
- let _ = pipeline;
- object::hand_out(LIST, list, iid, &LIST_IIDS, out)
- }
- extern "system" fn allocator_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
- let found = object::is(iid, &ALLOCATOR_IIDS).then_some(this);
- object::answer(ALLOCATOR, iid, found, out)
- }
- extern "system" fn allocator_release(this: This) -> u32 {
- object::release_boxed::<Allocator>(this)
- }
- /// Nothing is recorded, so there is nothing to free.
- extern "system" fn allocator_reset(_: This) -> HRESULT {
- S_OK
- }
- extern "system" fn list_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
- object::answer(LIST, iid, object::is(iid, &LIST_IIDS).then_some(this), out)
- }
- extern "system" fn list_release(this: This) -> u32 {
- object::release_boxed::<List>(this)
- }
- extern "system" fn list_type(this: This) -> u32 {
- // SAFETY: `this` is one of our lists.
- unsafe { (*this.cast::<List>()).kind }
- }
- extern "system" fn close(this: This) -> HRESULT {
- #[cfg(windows)]
- if let Some(host) = host_list(this) {
- host.close();
- }
- S_OK
- }
- extern "system" fn reset(this: This, allocator: This, pipeline: This) -> HRESULT {
- if allocator.is_null() {
- return E_INVALIDARG;
- }
- // A wait recorded and never submitted goes with the recording.
- VALUE_WAITS
- .lock()
- .unwrap_or_else(|e| e.into_inner())
- .retain(|&(list, _)| list != this as usize);
- // Every submission of the list has run (submissions are waited for).
- // SAFETY: the title passes one of our lists.
- let uploads = unsafe { &(*this.cast::<List>()).uploads };
- uploads.lock().unwrap_or_else(|e| e.into_inner()).clear();
- // The parts of a recording that was cut and never submitted go with it
- // (a submission takes the parts it runs).
- #[cfg(windows)]
- {
- // SAFETY: the title passes one of our lists, and neither records
- // into it nor submits it during its reset.
- let list = unsafe { &mut *this.cast_mut().cast::<List>() };
- list.earlier.clear();
- list.gate.clear();
- }
- #[cfg(windows)]
- with_state(this, |host, state| {
- host.reset();
- state.reset(pipeline);
- });
- crate::pipeline::bound(pipeline);
- #[cfg(not(windows))]
- let _ = this;
- S_OK
- }
- /// A resource barrier as titles pass it: type, flags, then a resource and
- /// either a subresource and two states (transition) or a second resource
- /// (aliasing). The same layout as the host's.
- #[repr(C)]
- #[derive(Clone, Copy)]
- pub(crate) struct Barrier {
- kind: u32,
- flags: u32,
- first: This,
- second: u64,
- last: u64,
- }
- /// The barrier on host resources, or `None` if it names a resource the host
- /// GPU does not have (buffers so far).
- #[cfg(windows)]
- fn host_barrier(list: &crate::host::HostList, b: &Barrier) -> Option<Barrier> {
- use windows::core::Interface;
- let host = |r: This| crate::resource::host_resource(r).map(|h| h.as_raw().cast_const().cast());
- match b.kind {
- 0 => {
- // A split barrier's first half: its second half transitions.
- const BEGIN_ONLY: u32 = 1;
- if b.flags & BEGIN_ONLY != 0 {
- return None;
- }
- // shortcut: whole resources, from the state the list tracked
- // (`HostList::transition`); subresources are not told apart.
- let wanted = crate::host::texture_state(b.last as u32);
- let (texture, before) = list.transition(b.first, wanted)?;
- Some(Barrier {
- kind: 0,
- flags: 0,
- first: texture.as_raw().cast_const().cast(),
- second: 0xffff_ffff | (u64::from(before) << 32),
- last: u64::from(crate::host::legal_state(texture, wanted)),
- })
- }
- 1 => Some(Barrier {
- first: if b.first.is_null() {
- b.first
- } else {
- host(b.first)?
- },
- second: if b.second == 0 {
- 0
- } else {
- host(b.second as This)? as u64
- },
- ..*b
- }),
- 2 => Some(Barrier {
- first: if b.first.is_null() {
- b.first
- } else {
- // A depth texture's unordered access is its colour twin's
- // (the host gives the depth texture none): the barrier is
- // on the texture shaders write.
- match crate::resource::twin_resource(b.first) {
- Some(twin) => twin.as_raw().cast_const().cast(),
- None => host(b.first)?,
- }
- },
- ..*b
- }),
- _ => None,
- }
- }
- /// An unordered access barrier on every resource: earlier shader writes
- /// finish before later work reads or writes.
- const UAV_BARRIER: Barrier = Barrier {
- kind: 2,
- flags: 0,
- first: std::ptr::null(),
- second: 0,
- last: 0,
- };
- /// Returns the console's barrier "actions" (none).
- extern "system" fn barrier(this: This, count: u32, barriers: *const Barrier) -> u32 {
- #[cfg(windows)]
- if let Some(list) = host_list(this)
- && !barriers.is_null()
- {
- if count > 0 {
- with_state(this, |_, state| state.synced());
- }
- // SAFETY: the title passes `count` barriers.
- let barriers = unsafe { std::slice::from_raw_parts(barriers, count as usize) };
- let mut host: Vec<Barrier> = barriers
- .iter()
- .filter_map(|b| host_barrier(list, b))
- .collect();
- // Buffers have host copies, not resources the barrier can name:
- // a barrier on one waits for every shader write before it.
- if barriers.iter().any(|b| {
- b.kind != 1 && !b.first.is_null() && crate::resource::buffer_bytes(b.first).is_some()
- }) {
- host.push(UAV_BARRIER);
- #[cfg(feature = "diagnostics")]
- crate::copystat::add(crate::copystat::C::WaitsForBuffers, 1);
- }
- if !host.is_empty() {
- use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_BARRIER;
- const _: () = assert!(size_of::<Barrier>() == size_of::<D3D12_RESOURCE_BARRIER>());
- // SAFETY: the same layout; borrowed resource pointers, never
- // released through this slice.
- let host = unsafe {
- std::slice::from_raw_parts(
- host.as_ptr().cast::<D3D12_RESOURCE_BARRIER>(),
- host.len(),
- )
- };
- // SAFETY: an open list and live resources.
- unsafe { list.list.ResourceBarrier(host) };
- }
- }
- 0
- }
- /// Moves a title texture's host copy to `state` in the list, if needed.
- #[cfg(windows)]
- pub(crate) fn ensure_state(list: &crate::host::HostList, resource: This, state: u32) {
- use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_STATES as State;
- if let Some((texture, before)) = list.transition(resource, state) {
- let state = crate::host::legal_state(texture, state);
- let barrier = crate::host::transition(texture, State(before as i32), State(state as i32));
- // SAFETY: an open list and a live texture.
- unsafe { list.list.ResourceBarrier(&[barrier]) };
- }
- }
- #[cfg(windows)]
- const COPY_DEST: u32 = 0x400;
- #[cfg(windows)]
- const COPY_SOURCE: u32 = 0x800;
- /// `len` bytes at `offset` of a title buffer, if they are inside it.
- #[cfg(windows)]
- fn buffer_range(buffer: This, offset: u64, len: u64) -> Option<*mut u8> {
- let (data, size) = crate::resource::buffer_bytes(buffer)?;
- let end = offset.checked_add(len)?;
- if end > size {
- tracing::warn!(offset, len, size, "copy outside its buffer");
- return None;
- }
- // SAFETY: inside the buffer, checked above.
- Some(unsafe { data.add(offset as usize) })
- }
- /// A copy of title memory that `list` records: on the card at its place in
- /// the list ([`crate::host::HostList::place_copy`]), or at submission.
- #[cfg(windows)]
- fn buffer_copy(this: This, list: &crate::host::HostList, to: *mut u8, from: *const u8, len: usize) {
- if len == 0 {
- return;
- }
- if list.place_copy(to as usize, from as usize, len) {
- with_state(this, |_, state| state.copied());
- } else {
- list.defer_copy(to, from, len);
- }
- }
- /// Buffer to buffer: both are host memory, copied at submission.
- extern "system" fn copy_buffer_region(
- this: This,
- dst: This,
- dst_offset: u64,
- src: This,
- src_offset: u64,
- len: u64,
- ) {
- #[cfg(windows)]
- if let Some(list) = host_list(this)
- && let Some(to) = buffer_range(dst, dst_offset, len)
- && let Some(from) = buffer_range(src, src_offset, len)
- {
- buffer_copy(this, list, to, from, len as usize);
- }
- #[cfg(not(windows))]
- let _ = (this, dst, dst_offset, src, src_offset, len);
- }
- /// Whether one of two resources is a target in a stand-in format and the
- /// other is not ([`crate::resource::target_format`]): their texels differ
- /// in size, so the host cannot copy between them (logged once).
- #[cfg(windows)]
- fn unlike(dst: This, src: This) -> bool {
- let unlike = crate::resource::stands_in(dst) != crate::resource::stands_in(src);
- if unlike {
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| {
- tracing::warn!(?dst, ?src, "copy between a stand-in target and title-format texels skipped")
- });
- }
- unlike
- }
- /// Copies left out because both sides were one host texture
- /// ([`copy_resource`], [`copy_texture_region`]), since the start.
- #[cfg(windows)]
- pub(crate) static SELF_COPIES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- extern "system" fn copy_resource(this: This, dst: This, src: This) {
- #[cfg(windows)]
- {
- let Some(list) = host_list(this) else {
- return;
- };
- if let (Some(to), Some(from)) = (
- crate::resource::host_resource(dst),
- crate::resource::host_resource(src),
- ) {
- if unlike(dst, src) {
- return;
- }
- // Two title textures over the same memory have one host
- // texture between them: the copy would be of that texture onto
- // itself, which the host does not allow, and it would change
- // nothing. It was recorded all the same, with the texture left
- // in the source state as the copy's destination (run 205's
- // debug layer: "CopyResource with its destination in the
- // COPY_SOURCE state", 37 times, a 2560x1440 depth texture).
- // Only the two state changes and the copy are left out: what
- // is noted of the textures stays as it was. After
- // triippywhale 6e502b3b.
- let same = windows::core::Interface::as_raw(to) == windows::core::Interface::as_raw(from);
- // A source the title fills itself is uploaded first (upstream
- // PR #671): a copy read whatever the host texture held.
- if crate::texture::title_written(from) {
- crate::texture::upload(list, src, from);
- }
- // Depth textures shaders also write: what they wrote, first.
- list.twin_use(dst, false, true);
- list.twin_use(src, false, false);
- // A destination made level by level gets its levels' memory
- // ahead of this list (`crate::tiles`).
- crate::texture::card_writes(list, dst, to, None);
- crate::texture::host_written(dst);
- if same {
- SELF_COPIES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| {
- tracing::info!(
- ?dst,
- ?src,
- "a copy between two title textures that share one host texture is left out: the first one"
- );
- });
- return;
- }
- ensure_state(list, dst, COPY_DEST);
- ensure_state(list, src, COPY_SOURCE);
- #[cfg(feature = "diagnostics")]
- list.note_op(|| format!("copy resource {src:?} to {dst:?}"));
- // SAFETY: an open list and live textures.
- unsafe { list.list.CopyResource(to, from) };
- } else if let (Some((to, a)), Some((from, b))) = (
- crate::resource::buffer_bytes(dst),
- crate::resource::buffer_bytes(src),
- ) {
- buffer_copy(this, list, to, from, a.min(b) as usize);
- }
- }
- #[cfg(not(windows))]
- let _ = (this, dst, src);
- }
- extern "system" fn copy_resource_x(this: This, dst: This, src: This, _flags: u32) {
- copy_resource(this, dst, src);
- }
- /// A copy location as titles pass it, laid out as the host's: a resource,
- /// then a subresource index (type 0) or a placed footprint (type 1).
- #[repr(C)]
- #[derive(Clone, Copy)]
- pub(crate) struct CopyLocation {
- resource: This,
- kind: u32,
- footprint: crate::resource::PlacedFootprint,
- }
- /// Texture to texture on the GPU; buffer to texture through an upload
- /// buffer filled at submission. Texture to buffer (read back) is not done
- /// yet.
- extern "system" fn copy_texture_region(
- this: This,
- dst: *const CopyLocation,
- x: u32,
- y: u32,
- z: u32,
- src: *const CopyLocation,
- source_box: *const [u32; 6],
- ) {
- #[cfg(windows)]
- {
- use windows::Win32::Graphics::Direct3D12::{D3D12_BOX, D3D12_TEXTURE_COPY_LOCATION};
- use windows::core::Interface;
- const FOOTPRINT: u32 = 1;
- // SAFETY: the title passes both locations (null is tolerated).
- let (Some(list), Some(&dst), Some(&src)) =
- (host_list(this), unsafe { dst.as_ref() }, unsafe {
- src.as_ref()
- })
- else {
- return;
- };
- let Some(to) = crate::resource::host_resource(dst.resource) else {
- // Texture to buffer: a read back. The list copies the texels
- // into a host buffer of ours; once it has run they go to the
- // title's buffer (`HostList::defer_read_back`).
- // The title reads two tiny float targets back every frame (5x1
- // and 1x1 RGBA32F, 80 and 16 bytes: run 90), and read a buffer
- // nobody wrote. `VICTORY_READ_BACK=0` leaves them unmade, as
- // before.
- static COUNT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- let n = COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
- let made = read_back(list, dst, x, y, z, src, source_box);
- if n <= 16 || n.is_multiple_of(4096) || made.is_err() {
- static REFUSED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- if made.is_err() && REFUSED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) >= 16 {
- return;
- }
- let source = crate::resource::host_resource(src.resource).map(|r| {
- // SAFETY: a live resource.
- let d = unsafe { r.GetDesc() };
- format!("{}x{} f{} mips {}", d.Width, d.Height, d.Format.0, d.MipLevels)
- });
- tracing::info!(
- copies = n,
- made = ?made,
- source = ?source,
- source_kind = src.kind,
- destination_kind = dst.kind,
- destination_offset = dst.footprint.offset,
- destination_bytes = ?dst.footprint.bytes(),
- "a texture to buffer copy (a read back)"
- );
- }
- return;
- };
- if unlike(dst.resource, src.resource) {
- return;
- }
- let mut from = src;
- // Both sides are the same subresource of one host texture (set below).
- let mut same = false;
- let _upload; // keeps the source alive while this call records it
- #[cfg(feature = "diagnostics")]
- let mut read = None;
- if src.kind == FOOTPRINT {
- let (Some(host), Some(bytes)) = (crate::host::host(), src.footprint.bytes()) else {
- return;
- };
- let Some(data) = buffer_range(src.resource, src.footprint.offset, bytes) else {
- return;
- };
- let buffer = match list.place_source(data as usize, bytes as usize) {
- // What shaders wrote to it, at this place of the list.
- Some(buffer) => buffer,
- None => {
- let Some((buffer, staging)) = list.upload(host, bytes) else {
- return;
- };
- // The list owns this mapped D3D12 upload buffer. Wine can
- // report its address as MEM_FREE, so use the host-upload
- // path.
- list.defer_fill_buffer(&buffer, staging, data, bytes as usize);
- buffer
- }
- };
- from.resource = buffer.as_raw().cast_const().cast();
- from.footprint.offset = 0;
- #[cfg(feature = "diagnostics")]
- {
- // SAFETY: a live buffer.
- read = Some((unsafe { buffer.GetGPUVirtualAddress() }, bytes));
- }
- _upload = buffer;
- } else {
- let Some(texture) = crate::resource::host_resource(src.resource) else {
- return;
- };
- // The same subresource of one host texture on both sides (two
- // title textures over one memory): not a copy the host allows.
- // The states and the copy are left out below; the rest is
- // noted as before.
- same = texture.as_raw() == to.as_raw()
- && src.kind != FOOTPRINT
- && dst.kind != FOOTPRINT
- && src.footprint.offset as u32 == dst.footprint.offset as u32;
- list.twin_use(src.resource, false, false);
- if crate::texture::title_written(texture) {
- crate::texture::upload(list, src.resource, texture);
- }
- if !same {
- ensure_state(list, src.resource, COPY_SOURCE);
- }
- from.resource = texture.as_raw().cast_const().cast();
- }
- // A depth texture shaders also write: what they wrote, first (the
- // source above too).
- list.twin_use(dst.resource, false, true);
- // The first copy into a texture of the title's: what it does not
- // write is zeros, as in a texture that was an allocation of its own
- // (`VICTORY_COPY_OVER_TITLE=title`: what title memory holds, which
- // may be another texture's: specks on the waitress's forehead were
- // left in run n04). (The host
- // copy is cut from a block and held whatever lay there, and no
- // upload is made once a copy wrote it: the atlas the title copies
- // its baked decorations into, a 2048x2048 BC3 texture of which a
- // bake writes 256x256 at most, was drawn over a character's face
- // with other textures' bytes wherever nothing was baked, runs n01 and
- // textures-1.)
- static OVER: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
- let over = *OVER.get_or_init(|| match std::env::var("VICTORY_COPY_OVER_TITLE").as_deref() {
- Ok("0") => 0,
- Ok("title") => 1,
- _ => 2,
- });
- if over != 0 && crate::texture::title_written(to) && !crate::texture::host_wrote(dst.resource as usize) {
- // One a list uploaded, or a draw sampled, holds the title's.
- if over == 2 && !list.used(dst.resource as usize) && !crate::texture::is_staged(dst.resource) {
- if let Some(host) = crate::host::host() {
- ensure_state(list, dst.resource, COPY_DEST);
- let _ = list.first_use(dst.resource as usize);
- crate::texture::zeros(list, host, to);
- }
- } else {
- crate::texture::upload(list, dst.resource, to);
- }
- // (Done before the title's copy writes over it.)
- if list.used(dst.resource as usize) {
- ensure_state(list, dst.resource, COPY_SOURCE);
- }
- }
- // A destination made level by level gets the level's memory ahead
- // of this list (`crate::tiles`): the title's bake copies each level
- // of its result into a block-compressed texture of that kind, and
- // a copy into a level without memory is dropped.
- crate::texture::card_writes(list, dst.resource, to, Some(dst.footprint.offset as u32));
- crate::texture::host_written(dst.resource);
- if same {
- SELF_COPIES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
- return;
- }
- ensure_state(list, dst.resource, COPY_DEST);
- let mut to_location = dst;
- to_location.resource = to.as_raw().cast_const().cast();
- let host_box = (!source_box.is_null()).then(|| source_box.cast::<D3D12_BOX>());
- #[cfg(feature = "diagnostics")]
- {
- let text = || {
- // SAFETY: a live texture; the box is the title's or null.
- let (d, b) = unsafe { (to.GetDesc(), host_box.map(|b| *b)) };
- format!(
- "copy to {:?} {}x{} f{} sub/kind {} at {x},{y},{z} from {:?} kind {} footprint {:?} box {b:?}",
- dst.resource,
- d.Width,
- d.Height,
- d.Format.0,
- dst.kind,
- src.resource,
- src.kind,
- (src.kind == FOOTPRINT).then_some(src.footprint)
- )
- };
- match read {
- Some(range) => list.note_read(text, range),
- None => list.note_op(text),
- }
- }
- const _: () =
- assert!(size_of::<CopyLocation>() == size_of::<D3D12_TEXTURE_COPY_LOCATION>());
- // SAFETY: an open list; both locations in the host's layout, naming
- // live resources; the box is the title's (same layout).
- unsafe {
- list.list.CopyTextureRegion(
- (&raw const to_location).cast(),
- x,
- y,
- z,
- (&raw const from).cast(),
- host_box,
- )
- };
- }
- #[cfg(not(windows))]
- let _ = (this, dst, x, y, z, src, source_box);
- }
- /// Records a texture to buffer copy (a read back) of [`copy_texture_region`]
- /// into `list`, or says why not.
- #[cfg(windows)]
- #[allow(clippy::too_many_arguments)] // the method's own arguments
- fn read_back(
- list: &crate::host::HostList,
- dst: CopyLocation,
- x: u32,
- y: u32,
- z: u32,
- src: CopyLocation,
- source_box: *const [u32; 6],
- ) -> Result<(), &'static str> {
- use windows::Win32::Graphics::Direct3D12::D3D12_BOX;
- use windows::core::Interface;
- const FOOTPRINT: u32 = 1;
- static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
- if !*ON.get_or_init(|| std::env::var_os("VICTORY_READ_BACK").is_none_or(|v| v != "0")) {
- return Err("switched off");
- }
- if dst.kind != FOOTPRINT || src.kind == FOOTPRINT {
- return Err("not a texture subresource into a placed footprint");
- }
- if (x, y, z) != (0, 0, 0) {
- return Err("a destination corner other than the footprint's");
- }
- let host = crate::host::host().ok_or("no host")?;
- let (footprint, [size, row, rows, pitch]) = dst.footprint.host_rows().ok_or("an unknown format")?;
- // The title's buffer holds every row at its own pitch.
- let bytes = pitch * (rows - 1) + row;
- let data = buffer_range(dst.resource, dst.footprint.offset, bytes).ok_or("a destination outside its buffer")?;
- let texture = crate::resource::host_resource(src.resource).ok_or("no host texture")?;
- let buffer = host.read_back_buffer(size).ok_or("no read-back buffer")?;
- // As a texture source of any copy: what shaders wrote to a depth twin
- // and what the title wrote itself first, then the state.
- list.twin_use(src.resource, false, false);
- if crate::texture::title_written(texture) {
- crate::texture::upload(list, src.resource, texture);
- }
- ensure_state(list, src.resource, COPY_SOURCE);
- let mut from = src;
- from.resource = texture.as_raw().cast_const().cast();
- let mut to = dst;
- to.resource = buffer.as_raw().cast_const().cast();
- to.footprint = footprint;
- let host_box = (!source_box.is_null()).then(|| source_box.cast::<D3D12_BOX>());
- #[cfg(feature = "diagnostics")]
- list.note_op(|| {
- format!(
- "read back of {:?} sub {} into buffer {:?}+{:#x}: {rows} rows of {row} bytes",
- src.resource, src.footprint.offset as u32, dst.resource, dst.footprint.offset
- )
- });
- // SAFETY: an open list; both locations in the host's layout (asserted
- // in `copy_texture_region`), naming a live texture and our buffer, which
- // the list keeps until it is reset; the box is the title's.
- unsafe {
- list.list
- .CopyTextureRegion((&raw const to).cast(), 0, 0, 0, (&raw const from).cast(), host_box)
- };
- list.defer_read_back(buffer, data, [row as usize, rows as usize, pitch as usize]);
- Ok(())
- }
- #[allow(clippy::too_many_arguments)] // the method's own arguments
- extern "system" fn copy_texture_region_x(
- this: This,
- dst: *const CopyLocation,
- x: u32,
- y: u32,
- z: u32,
- src: *const CopyLocation,
- source_box: *const [u32; 6],
- _flags: u32,
- ) {
- copy_texture_region(this, dst, x, y, z, src, source_box);
- }
- extern "system" fn resolve(
- this: This,
- dst: This,
- dst_sub: u32,
- src: This,
- src_sub: u32,
- format: u32,
- ) {
- #[cfg(windows)]
- if let Some(list) = host_list(this)
- && let (Some(to), Some(from)) = (
- crate::resource::host_resource(dst),
- crate::resource::host_resource(src),
- )
- {
- use windows::Win32::Foundation::RECT;
- use windows::Win32::Graphics::Direct3D12::{
- D3D12_RESOLVE_MODE_AVERAGE, ID3D12GraphicsCommandList1,
- };
- use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
- use windows::core::Interface;
- const RESOLVE_DEST: u32 = 0x1000;
- const RESOLVE_SOURCE: u32 = 0x2000;
- if unlike(dst, src) {
- return;
- }
- // Depth textures shaders also write: what they wrote, first.
- list.twin_use(dst, false, true);
- list.twin_use(src, false, false);
- // As for a copy (`crate::tiles`).
- crate::texture::card_writes(list, dst, to, Some(dst_sub));
- ensure_state(list, dst, RESOLVE_DEST);
- ensure_state(list, src, RESOLVE_SOURCE);
- crate::texture::host_written(dst);
- #[cfg(feature = "diagnostics")]
- list.note_op(|| format!("resolve {src:?} to {dst:?} format {format}"));
- // SAFETY: an open list and live textures; the region is inside both.
- unsafe {
- let (a, b) = (to.GetDesc(), from.GetDesc());
- let format = if crate::resource::stands_in(dst) { a.Format } else { DXGI_FORMAT(format as i32) };
- if (a.Width, a.Height) == (b.Width, b.Height) {
- list.list
- .ResolveSubresource(to, dst_sub, from, src_sub, format);
- } else if let Ok(list1) = list.list.cast::<ID3D12GraphicsCommandList1>() {
- // The console clips a larger source to the destination.
- let rect = RECT {
- left: 0,
- top: 0,
- right: a.Width.min(b.Width) as i32,
- bottom: a.Height.min(b.Height) as i32,
- };
- list1.ResolveSubresourceRegion(
- to,
- dst_sub,
- 0,
- 0,
- from,
- src_sub,
- Some(&rect),
- format,
- D3D12_RESOLVE_MODE_AVERAGE,
- );
- }
- }
- }
- #[cfg(not(windows))]
- let _ = (this, dst, dst_sub, src, src_sub, format);
- }
- #[repr(C)]
- pub(crate) struct Rect {
- left: i32,
- top: i32,
- right: i32,
- bottom: i32,
- }
- extern "system" fn clear_rtv(
- this: This,
- handle: u64,
- color: *const [f32; 4],
- count: u32,
- rects: *const Rect,
- ) {
- #[cfg(windows)]
- {
- let (Some(list), Some(record)) = (host_list(this), crate::descriptor::rtv_record(handle))
- else {
- return;
- };
- let (Some(texture), Some(host)) = (
- crate::resource::host_resource(record.resource),
- crate::host::host(),
- ) else {
- return;
- };
- // SAFETY: the title passes four floats (null is tolerated).
- let Some(color) = (unsafe { color.as_ref() }) else {
- return;
- };
- let rects = (count > 0 && !rects.is_null()).then(|| {
- // SAFETY: the title passes `count` rectangles, laid out as the host's.
- unsafe { std::slice::from_raw_parts(rects.cast(), count as usize) }
- });
- const RENDER_TARGET: u32 = 0x4;
- let rtv = host.rtv(Some(texture), record.desc().as_ref());
- // SAFETY: an open list, a fresh view of a live texture.
- unsafe {
- if let Some((_, before)) = list.transition(record.resource, RENDER_TARGET) {
- use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_STATES as State;
- list.list.ResourceBarrier(&[crate::host::transition(
- texture,
- State(before as i32),
- State(RENDER_TARGET as i32),
- )]);
- }
- list.list.ClearRenderTargetView(rtv, color, rects);
- }
- }
- #[cfg(not(windows))]
- let _ = (this, handle, color, count, rects);
- }
- /// `ClearDepthStencilView(handle, flags, depth, stencil, count, rects)`;
- /// flags: 1 depth, 2 stencil.
- extern "system" fn clear_dsv(
- this: This,
- handle: u64,
- flags: u32,
- depth: f32,
- stencil: u8,
- count: u32,
- rects: *const Rect,
- ) {
- #[cfg(windows)]
- {
- use windows::Win32::Graphics::Direct3D12::D3D12_CLEAR_FLAGS;
- let (Some(list), Some(record)) = (host_list(this), crate::descriptor::dsv_record(handle))
- else {
- return;
- };
- let (Some(texture), Some(host)) = (
- crate::resource::host_resource(record.resource),
- crate::host::host(),
- ) else {
- return;
- };
- let rects = (count > 0 && !rects.is_null()).then(|| {
- // SAFETY: the title passes `count` rectangles, laid out as the host's.
- unsafe { std::slice::from_raw_parts(rects.cast(), count as usize) }
- });
- const DEPTH_WRITE: u32 = 0x10;
- // A depth texture shaders also write: what they wrote, first (the
- // clear may be of one slice or of some rectangles).
- list.twin_use(record.resource, false, true);
- ensure_state(list, record.resource, DEPTH_WRITE);
- if crate::resource::depth_and_unordered(record.resource) {
- crate::resource::DEPTH_UNORDERED[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
- }
- let dsv = host.dsv(texture, record.depth_desc().as_ref());
- // SAFETY: an open list, a fresh view of a live texture.
- unsafe {
- list.list
- .ClearDepthStencilView(dsv, D3D12_CLEAR_FLAGS(flags as i32 & 3), depth, stencil, rects)
- };
- }
- #[cfg(not(windows))]
- let _ = (this, handle, flags, depth, stencil, count, rects);
- }
- /// `CopyMemoryToMemoryX(destination, source, size, flags)`: GPU addresses
- /// are title memory, copied at submission like buffer copies (if mapped).
- extern "system" fn copy_memory(this: This, dst: u64, src: u64, len: u64, _flags: u32) {
- #[cfg(windows)]
- if let Some(list) = host_list(this)
- && dst != 0
- && src != 0
- {
- let len = len as usize;
- if list.place_copy(dst as usize, src as usize, len) {
- with_state(this, |_, state| state.copied());
- return;
- }
- list.defer(move || {
- if crate::texture::committed(src as *const u8, len)
- && crate::texture::committed(dst as *const u8, len)
- {
- // SAFETY: both ranges are committed title memory.
- unsafe { std::ptr::copy(src as *const u8, dst as *mut u8, len) };
- }
- });
- }
- #[cfg(not(windows))]
- let _ = (this, dst, src, len);
- }
- /// `DriverHintX(feature, value)`: no hints for the host.
- extern "system" fn driver_hint(_: This, _feature: u64, _value: u64) -> u64 {
- 0
- }
- /// `DispatchX(x, y, z, flags)`: a dispatch with console flags, which the
- /// host has no use for (the ray-tracing builder dispatches this way).
- extern "system" fn dispatch_x(this: This, x: u32, y: u32, z: u32, _flags: u32) {
- #[cfg(windows)]
- state::dispatch(this, x, y, z);
- #[cfg(not(windows))]
- {
- let _ = (this, x, y, z);
- count_dispatch();
- }
- }
- /// `BeginQuery`/`EndQuery(heap, type, index)`: queries are answered when
- /// resolved.
- extern "system" fn query_mark(_: This, _heap: This, _kind: u32, _index: u32) {}
- /// `ResolveQueryData(heap, type, first, count, buffer, offset)`: writes
- /// each query's result into the buffer once the list has run. Timestamps
- /// (type 2) are the GPU clock then; occlusion counts (0) say a million
- /// samples passed and binary occlusion (1) visible, so nothing is culled
- /// by a query.
- /// shortcut: no query runs on the host GPU; results are plausible values,
- /// not measured ones (time spans come out as the submission's length).
- extern "system" fn resolve_queries(
- this: This,
- _heap: This,
- kind: u32,
- _first: u32,
- count: u32,
- buffer: This,
- offset: u64,
- ) {
- const OCCLUSION: u32 = 0;
- const BINARY: u32 = 1;
- const TIMESTAMP: u32 = 2;
- const PIPELINE_STATISTICS: u32 = 3;
- let Some((data, size)) = crate::resource::buffer_bytes(buffer) else {
- return;
- };
- let (what, bytes) = match kind {
- OCCLUSION => (Written::Value(1 << 20), 8),
- BINARY => (Written::Value(1), 8),
- TIMESTAMP => (Written::GpuTimestamp, 8),
- PIPELINE_STATISTICS => (Written::Value(0), 88),
- _ => (Written::Value(0), 16),
- };
- let Some(end) = offset.checked_add(u64::from(count) * bytes) else {
- return;
- };
- if end > size {
- return;
- }
- for i in 0..u64::from(count) {
- // Wider results are written as 8-byte pieces.
- for piece in 0..bytes / 8 {
- // SAFETY: inside the buffer, checked above.
- let at = unsafe { data.add((offset + i * bytes + piece * 8) as usize) };
- let what = if piece == 0 { what } else { Written::Value(0) };
- defer_write(this, at as u64, what, 8);
- }
- }
- }
- /// IUnknown 3, object 4, device child 1, allocator 2.
- static ALLOCATOR_FUNCTIONS: Table<24> = {
- let mut f = object::d3d12_object(object::missing_table::<ALLOCATOR, 24>());
- f[0] = allocator_query as *const ();
- f[2] = allocator_release as *const ();
- f[7] = object::parent_device as *const (); // 0x38
- f[8] = allocator_reset as *const (); // 0x40
- f[9] = object::ignore as *const (); // 0x48 allocation callback
- Table(f)
- };
- extern "system" fn set_pipeline(this: This, pipeline: This) {
- crate::pipeline::bound(pipeline);
- #[cfg(windows)]
- with_state(this, |_, state| state.set_pipeline(pipeline));
- #[cfg(not(windows))]
- let _ = this;
- }
- /// Pipeline syncs: waits for earlier work and cache flushes, at the top or
- /// bottom of the pipe `(flags, control, address, size)`. The host orders
- /// nothing between dispatches by itself: what shaders wrote is waited for
- /// here as at the title's barriers (the ray-tracing structure builder has
- /// no other wait between its stages).
- /// (Arguments may be what earlier shaders wrote from here on:
- /// `State::synced`.)
- extern "system" fn sync(this: This, flags: u32, control: u32, address: u64, size: u64) {
- #[cfg(windows)]
- {
- with_state(this, |_, state| state.synced());
- if let Some(list) = host_list(this) {
- use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_BARRIER;
- #[cfg(feature = "diagnostics")]
- {
- // The shapes of the title's calls, each once.
- static SEEN: std::sync::Mutex<Vec<(u32, u32, bool)>> = std::sync::Mutex::new(Vec::new());
- let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
- let shape = (flags, control, address == 0);
- if seen.len() < 64 && !seen.contains(&shape) {
- seen.push(shape);
- tracing::info!(
- flags = format_args!("{flags:#x}"),
- control = format_args!("{control:#x}"),
- address = format_args!("{address:#x}"),
- size = format_args!("{size:#x}"),
- "a pipeline sync"
- );
- }
- crate::copystat::add(crate::copystat::C::WaitsForGroups, 1);
- }
- // SAFETY: the same layout; no resource named.
- let barrier = unsafe { std::mem::transmute::<Barrier, D3D12_RESOURCE_BARRIER>(UAV_BARRIER) };
- // SAFETY: an open list.
- unsafe { list.list.ResourceBarrier(&[barrier]) };
- }
- }
- let _ = (this, flags, control, address, size);
- }
- /// Reset with console flags `(allocator, pipeline, flags)`; the flags only
- /// tune the console's command memory.
- extern "system" fn reset_x(this: This, allocator: This, pipeline: This, _flags: u32) -> HRESULT {
- reset(this, allocator, pipeline)
- }
- /// Newer lists' 0x4a0 `(size, alignment) -> pointer`: memory the list
- /// owns until it is reset, which the CPU writes and the GPU reads at the
- /// same address; null on failure. The title's BVH builder puts
- /// its instance descriptions there (and wrote through our old
- /// `E_NOTIMPL` as a pointer).
- extern "system" fn allocate_upload(this: This, size: u64, alignment: u64) -> *mut u8 {
- let align = usize::try_from(alignment).unwrap_or(0).max(16);
- let Some(len) = usize::try_from(size).ok().and_then(|s| s.checked_add(align)) else {
- return std::ptr::null_mut();
- };
- if this.is_null() || !align.is_power_of_two() {
- return std::ptr::null_mut();
- }
- let mut block = vec![0u8; len];
- let start = block.as_mut_ptr();
- // SAFETY: the offset is below `align`, so `size` bytes fit after it.
- let at = unsafe { start.add(start.align_offset(align)) };
- // SAFETY: the title passes one of our lists.
- let uploads = unsafe { &(*this.cast::<List>()).uploads };
- // Moving the vector keeps its heap block where it is.
- uploads.lock().unwrap_or_else(|e| e.into_inner()).push(block);
- at
- }
- /// Draw state the host has no use for (root signatures: ours come with
- /// the pipeline; depth order and shading rate hints): any arguments,
- /// nothing returned.
- extern "system" fn ignored_state(_: This) {}
- /// The title's draw state (decision 0007, step 3), kept for its draws or
- /// set on the host list at once. Without a host GPU these do nothing.
- #[cfg(windows)]
- mod state {
- use super::{This, with_state};
- use crate::draw::{IndexView, VertexView};
- use windows::Win32::Foundation::RECT;
- use windows::Win32::Graphics::Direct3D12::D3D12_VIEWPORT;
- /// `count` items at `items`, or none.
- fn items<'a, T>(items: *const T, count: u32) -> &'a [T] {
- if items.is_null() || count == 0 {
- return &[];
- }
- // SAFETY: the title passes `count` items.
- unsafe { std::slice::from_raw_parts(items, count as usize) }
- }
- pub(super) extern "system" fn topology(this: This, topology: u32) {
- with_state(this, |list, s| s.set_topology(list, topology));
- }
- pub(super) extern "system" fn viewports(
- this: This,
- count: u32,
- viewports: *const D3D12_VIEWPORT,
- ) {
- // (Through the draw state, which keeps them for the host list
- // that continues this one after a cut: `State::replay`.)
- with_state(this, |list, s| s.set_viewports(list, items(viewports, count)));
- }
- pub(super) extern "system" fn scissors(this: This, count: u32, rects: *const RECT) {
- with_state(this, |list, s| s.set_scissors(list, items(rects, count)));
- }
- pub(super) extern "system" fn blend_factor(this: This, factor: *const [f32; 4]) {
- with_state(this, |list, s| {
- // SAFETY: the title passes four floats or null.
- s.set_blend_factor(list, unsafe { factor.as_ref() })
- });
- }
- pub(super) extern "system" fn stencil_ref(this: This, reference: u32) {
- with_state(this, |list, s| s.set_stencil_ref(list, reference));
- }
- pub(super) extern "system" fn depth_bounds(this: This, min: f32, max: f32) {
- with_state(this, |list, s| s.set_depth_bounds(list, min, max));
- }
- pub(super) extern "system" fn heaps(this: This, count: u32, heaps: *const This) {
- with_state(this, |_, s| s.set_heaps(items(heaps, count)));
- }
- pub(super) extern "system" fn compute_table(this: This, slot: u32, handle: u64) {
- with_state(this, |_, s| s.set_table(true, slot, handle));
- }
- pub(super) extern "system" fn graphics_table(this: This, slot: u32, handle: u64) {
- with_state(this, |_, s| s.set_table(false, slot, handle));
- }
- pub(super) extern "system" fn compute_constant(this: This, slot: u32, value: u32, offset: u32) {
- with_state(this, |_, s| s.set_constants(true, slot, &[value], offset));
- }
- pub(super) extern "system" fn graphics_constant(
- this: This,
- slot: u32,
- value: u32,
- offset: u32,
- ) {
- with_state(this, |_, s| s.set_constants(false, slot, &[value], offset));
- }
- pub(super) extern "system" fn compute_constants(
- this: This,
- slot: u32,
- count: u32,
- values: *const u32,
- offset: u32,
- ) {
- with_state(this, |_, s| {
- s.set_constants(true, slot, items(values, count), offset)
- });
- }
- pub(super) extern "system" fn graphics_constants(
- this: This,
- slot: u32,
- count: u32,
- values: *const u32,
- offset: u32,
- ) {
- with_state(this, |_, s| {
- s.set_constants(false, slot, items(values, count), offset)
- });
- }
- /// Root constant buffer, shader resource and unordered access views.
- pub(super) extern "system" fn compute_view(this: This, slot: u32, address: u64) {
- with_state(this, |_, s| s.set_view(true, slot, address));
- }
- pub(super) extern "system" fn graphics_view(this: This, slot: u32, address: u64) {
- with_state(this, |_, s| s.set_view(false, slot, address));
- }
- pub(super) extern "system" fn index_buffer(this: This, view: *const IndexView) {
- // SAFETY: the title passes a view or null.
- with_state(this, |_, s| s.set_index_buffer(unsafe { view.as_ref() }));
- }
- pub(super) extern "system" fn vertex_buffers(
- this: This,
- start: u32,
- count: u32,
- views: *const VertexView,
- ) {
- with_state(this, |_, s| {
- if views.is_null() {
- s.clear_vertex_buffers(start, count);
- } else {
- s.set_vertex_buffers(start, items(views, count));
- }
- });
- }
- /// `(count, handles, one range, depth)`: with one range, the handles
- /// follow the first at the render target view size (128 bytes).
- pub(super) extern "system" fn targets(
- this: This,
- count: u32,
- handles: *const u64,
- one_range: i32,
- depth: *const u64,
- ) {
- let handles: Vec<u64> = if one_range != 0 && !handles.is_null() {
- // SAFETY: the title passes the first handle.
- let first = unsafe { *handles };
- (0..u64::from(count)).map(|i| first + 128 * i).collect()
- } else {
- items(handles, count).to_vec()
- };
- // SAFETY: the title passes a handle or null.
- let depth = unsafe { depth.as_ref() }.copied().unwrap_or(0);
- with_state(this, |list, s| s.set_targets(list, &handles, depth));
- }
- pub(super) extern "system" fn draw(
- this: This,
- vertices: u32,
- instances: u32,
- first: u32,
- first_instance: u32,
- ) {
- super::count_draw();
- with_state(this, |list, s| {
- s.draw(list, vertices, instances, first, first_instance)
- });
- }
- pub(super) extern "system" fn dispatch(this: This, x: u32, y: u32, z: u32) {
- super::count_dispatch();
- with_state(this, |list, s| s.dispatch(list, x, y, z));
- }
- /// `DispatchMesh(x, y, z)` (the PC method of List6); the title's mesh
- /// programs take `x` groups only.
- pub(super) extern "system" fn dispatch_mesh(this: This, x: u32, y: u32, z: u32) {
- super::count_draw();
- if y * z != 1 {
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| tracing::warn!(x, y, z, "a mesh dispatch with y or z groups"));
- }
- with_state(this, |list, s| s.dispatch_mesh(list, x));
- }
- /// `DispatchMeshX(x, flags, indirect arguments, payload)`.
- /// shortcut: dispatches whose group count is in GPU memory (indirect)
- /// are dropped; make them ExecuteIndirect when the title uses them.
- pub(super) extern "system" fn dispatch_mesh_x(
- this: This,
- x: u32,
- _flags: u64,
- indirect: u64,
- _payload: u64,
- ) {
- super::count_draw();
- if indirect != 0 {
- static ONCE: std::sync::Once = std::sync::Once::new();
- ONCE.call_once(|| tracing::warn!("indirect mesh dispatches dropped"));
- return;
- }
- with_state(this, |list, s| s.dispatch_mesh(list, x));
- }
- pub(super) extern "system" fn draw_indexed(
- this: This,
- indices: u32,
- instances: u32,
- first: u32,
- base: i32,
- first_instance: u32,
- ) {
- super::count_draw();
- with_state(this, |list, s| {
- s.draw_indexed(list, indices, instances, first, base, first_instance)
- });
- }
- }
- /// Without a host GPU every draw state call is ignored.
- #[cfg(not(windows))]
- mod state {
- pub(super) use super::dispatch;
- pub(super) use super::draw;
- pub(super) use super::draw as draw_indexed;
- pub(super) use super::draw as dispatch_mesh;
- pub(super) use super::draw as dispatch_mesh_x;
- pub(super) use super::ignored_state as blend_factor;
- pub(super) use super::ignored_state as compute_constant;
- pub(super) use super::ignored_state as compute_constants;
- pub(super) use super::ignored_state as compute_table;
- pub(super) use super::ignored_state as compute_view;
- pub(super) use super::ignored_state as graphics_constant;
- pub(super) use super::ignored_state as graphics_constants;
- pub(super) use super::ignored_state as graphics_table;
- pub(super) use super::ignored_state as graphics_view;
- pub(super) use super::ignored_state as heaps;
- pub(super) use super::ignored_state as index_buffer;
- pub(super) use super::ignored_state as scissors;
- pub(super) use super::ignored_state as stencil_ref;
- pub(super) use super::ignored_state as depth_bounds;
- pub(super) use super::ignored_state as targets;
- pub(super) use super::ignored_state as topology;
- pub(super) use super::ignored_state as vertex_buffers;
- pub(super) use super::ignored_state as viewports;
- }
- fn count_draw() {
- DRAWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
- }
- /// Draws of the kinds the host does not run yet (indirect, mesh), and
- /// dispatches, counted per frame for the log.
- /// shortcut: dropped until their shaders translate (decision 0007).
- #[cfg(not(windows))]
- extern "system" fn draw(_: This) {
- count_draw();
- }
- /// `ExecuteIndirect(signature, max count, arguments, offset, count buffer,
- /// offset)`. The title's signatures (title+0x2479466, 2026-11-24)
- /// hold one argument each: draw (stride 16), indexed draw (20), dispatch
- /// (12), mesh dispatch (12), and the console's single-pass dispatch, type
- /// 105 (stride 12: the three group counts; its flags live in the
- /// signature, the title passes none), run as a plain dispatch. They run
- /// through the host's indirect execution (`State::execute_indirect`).
- /// shortcut: signatures that change root arguments are dropped (logged
- /// once per signature).
- extern "system" fn execute_indirect(
- this: This,
- signature: This,
- max: u32,
- arguments: This,
- offset: u64,
- count: This,
- count_offset: u64,
- ) {
- count_draw();
- let Some((stride, args)) = crate::signature::layout(signature) else {
- return;
- };
- let address = crate::resource::buffer_bytes(arguments).map(|(p, _)| p as u64 + offset);
- let counter = crate::resource::buffer_bytes(count).map(|(p, _)| p as u64 + count_offset);
- let mut ran = false;
- #[cfg(windows)]
- if let ([[kind, ..]], Some(address)) = (args, address)
- && (count.is_null() || counter.is_some())
- {
- const DISPATCH: u32 = 2;
- const DISPATCH_SINGLE: u32 = 105;
- let kind = if *kind == DISPATCH_SINGLE { DISPATCH } else { *kind };
- with_state(this, |list, s| {
- ran = s.execute_indirect(list, (kind, stride, max), address, counter);
- });
- } else {
- #[cfg(all(windows, feature = "diagnostics"))]
- crate::pipeline::note_why(format!(
- "indirect signature {args:?}: arguments in a buffer {}, count in a buffer {}",
- address.is_some(),
- count.is_null() || counter.is_some()
- ));
- }
- if ran {
- return;
- }
- INDIRECT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
- static SEEN: std::sync::Mutex<Vec<usize>> = std::sync::Mutex::new(Vec::new());
- let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
- if !seen.contains(&(signature as usize)) {
- seen.push(signature as usize);
- tracing::info!(
- stride,
- ?args,
- max,
- address = ?address.map(|a| format!("{a:#x}")),
- counted = !count.is_null(),
- "indirect execution dropped (first time for this signature)"
- );
- }
- }
- /// Indirect executions dropped since the last call.
- pub(crate) fn take_indirect() -> u64 {
- INDIRECT.swap(0, std::sync::atomic::Ordering::Relaxed)
- }
- static INDIRECT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- fn count_dispatch() {
- DISPATCHES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
- }
- #[cfg(not(windows))]
- extern "system" fn dispatch(_: This) {
- count_dispatch();
- }
- static DRAWS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- static DISPATCHES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- /// Draws (any kind, mesh and indirect included) and compute dispatches
- /// recorded since the last call.
- pub(crate) fn take_counts() -> (u64, u64) {
- use std::sync::atomic::Ordering::Relaxed;
- (DRAWS.swap(0, Relaxed), DISPATCHES.swap(0, Relaxed))
- }
- /// A GPU wait for a memory value: the list's commands after it run when
- /// the 32 or 64 bits at `address` compare as asked with `reference`.
- #[derive(Clone, Copy, Debug)]
- pub(crate) struct ValueWait {
- address: u64,
- comparison: u32,
- reference: u64,
- wide: bool,
- }
- impl ValueWait {
- /// Whether the memory holds what is waited for now.
- /// shortcut: the comparison codes are taken to be the comparison
- /// functions' (1 never .. 8 always, value against reference); one not
- /// known counts as met, and so does memory that is not mapped.
- ///
- /// The value is read through the kernel, which reports memory that is
- /// not there instead of faulting (as the copies of title memory are
- /// read, `crate::host::copy_mapped`). Asking Windows first whether the
- /// memory is committed (`VirtualQuery`) walks the page tables to the
- /// end of the region: the title keeps these values in a 1.1 GB
- /// allocation (0xd00000000), where one question took about 8 ms, and a
- /// queue asks at every submission and every signal, for each queue that
- /// stands at a wait (the title has five queues). That was
- /// 1.0-2.2 s of every cutscene frame on the thread that submits (runs
- /// 36-49: "between its submissions ... kernel"; 47 of 110 looks at
- /// that thread in run 49 found it in this question).
- /// `VICTORY_WAIT_ASK=1` asks first, as before.
- pub(crate) fn met(&self) -> bool {
- #[cfg(all(windows, feature = "diagnostics"))]
- let started = std::time::Instant::now();
- let met = self.holds();
- #[cfg(all(windows, feature = "diagnostics"))]
- {
- use crate::copystat::{C, add};
- add(C::WaitChecks, 1);
- add(C::WaitCheckNs, started.elapsed().as_nanos() as u64);
- }
- met
- }
- fn holds(&self) -> bool {
- let bytes = if self.wide { 8 } else { 4 };
- if self.address == 0 {
- return true;
- }
- static ASK: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
- if *ASK.get_or_init(|| std::env::var_os("VICTORY_WAIT_ASK").is_some_and(|v| v == "1"))
- && !crate::texture::committed(self.address as *const u8, bytes)
- {
- return true;
- }
- // The low `bytes` bytes; the rest stays zero.
- let mut raw = [0u8; 8];
- if !crate::host::read_into(self.address as *const u8, &mut raw[..bytes]) {
- return true;
- }
- self.holds_for(u64::from_le_bytes(raw))
- }
- /// Where the value waited for is: address and bytes.
- #[cfg(windows)]
- pub(crate) fn place(&self) -> (usize, usize) {
- (self.address as usize, if self.wide { 8 } else { 4 })
- }
- /// Whether the wait is met with `value` at its address (its low half
- /// for a 32-bit wait).
- pub(crate) fn holds_for(&self, value: u64) -> bool {
- let (value, reference) = if self.wide {
- (value, self.reference)
- } else {
- (value & 0xFFFF_FFFF, self.reference & 0xFFFF_FFFF)
- };
- match self.comparison {
- 1 => false,
- 2 => value < reference,
- 3 => value == reference,
- 4 => value <= reference,
- 5 => value > reference,
- 6 => value != reference,
- 7 => value >= reference,
- _ => true,
- }
- }
- }
- /// The waits lists recorded since their last reset, by list.
- static VALUE_WAITS: std::sync::Mutex<Vec<(usize, ValueWait)>> = std::sync::Mutex::new(Vec::new());
- /// The waits of the submitted `lists` that are not met yet (taken from
- /// them): what the console's GPU would stop at. The title orders work
- /// between its queues this way (the title: the compute queue's
- /// lighting passes wait for a value the graphics queue's depth pass writes
- /// at the bottom of the pipe; run at once, they read a cleared depth
- /// buffer and nothing was lit).
- /// shortcut: a wait holds its whole list (and submission) back, not only
- /// the commands after it.
- pub(crate) fn unmet_waits(lists: &[This]) -> Vec<ValueWait> {
- let mut waits = VALUE_WAITS.lock().unwrap_or_else(|e| e.into_inner());
- let mut unmet = Vec::new();
- waits.retain(|&(list, wait)| {
- if !lists.iter().any(|&l| l as usize == list) {
- return true;
- }
- if !wait.met() {
- unmet.push(wait);
- }
- false
- });
- unmet
- }
- /// The waits each of the submitted `lists` recorded (taken from them),
- /// met or not, at the list's first place in the submission.
- #[cfg(windows)]
- pub(crate) fn waits_by_list(lists: &[This]) -> Vec<Vec<ValueWait>> {
- let mut waits = VALUE_WAITS.lock().unwrap_or_else(|e| e.into_inner());
- let mut by = vec![Vec::new(); lists.len()];
- waits.retain(|&(list, wait)| {
- match lists.iter().position(|&l| l as usize == list) {
- Some(i) => {
- by[i].push(wait);
- false
- }
- None => true,
- }
- });
- by
- }
- fn wait_for(this: This, address: u64, comparison: u32, reference: u64, wide: bool) {
- let wait = ValueWait {
- address,
- comparison,
- reference,
- wide,
- };
- static SEEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
- if SEEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 12 {
- tracing::info!(
- address = format_args!("{address:#x}"),
- comparison,
- reference,
- wide,
- met = wait.met(),
- "a list waits for a memory value"
- );
- }
- #[cfg(all(windows, feature = "diagnostics"))]
- if let Some(host) = host_list(this) {
- crate::capture::op(std::ptr::from_ref(host) as usize, || {
- format!(
- "WAIT VALUE at {address:#x} comparison {comparison} reference {reference:#x} wide {wide} met {}",
- wait.met()
- )
- });
- }
- // The list is cut here, and the wait stands before what follows.
- #[cfg(windows)]
- if crate::queue::cuts() {
- cut(this, wait);
- return;
- }
- VALUE_WAITS
- .lock()
- .unwrap_or_else(|e| e.into_inner())
- .push((this as usize, wait));
- }
- extern "system" fn wait_value(this: This, address: u64, comparison: u32, reference: u64) {
- wait_for(this, address, comparison, reference, false);
- }
- extern "system" fn wait_value64(this: This, address: u64, comparison: u32, reference: u64) {
- wait_for(this, address, comparison, reference, true);
- }
- /// The enhanced barriers `(group count, groups)`: buffer, texture and
- /// global barriers with layouts instead of states. Every host use of a
- /// title texture (clears, copies) moves its host copy to the state it needs
- /// first, so only their ordering matters: one barrier on every shader
- /// write (the title's buffer barriers order compute passes).
- extern "system" fn barrier_groups(this: This, count: u32, _groups: *const u8) {
- // Diagnostics: counted, and left out under `VICTORY_BARRIER_TEST=groups`.
- #[cfg(all(windows, feature = "diagnostics"))]
- if count > 0 {
- crate::copystat::add(crate::copystat::C::WaitsForGroups, 1);
- if crate::copystat::barrier_test() & 1 != 0 {
- return;
- }
- }
- #[cfg(windows)]
- if count > 0
- && let Some(list) = host_list(this)
- {
- use windows::Win32::Graphics::Direct3D12::D3D12_RESOURCE_BARRIER;
- with_state(this, |_, state| state.synced());
- // SAFETY: the same layout; no resource named.
- let barrier = unsafe { std::mem::transmute::<Barrier, D3D12_RESOURCE_BARRIER>(UAV_BARRIER) };
- // SAFETY: an open list.
- unsafe { list.list.ResourceBarrier(&[barrier]) };
- }
- #[cfg(not(windows))]
- let _ = (this, count);
- }
- /// The bottom-of-pipe value writes `(address, value, flush, flags)`, 32 and
- /// 64 bits: done when the list has run, as the GPU would. Flag 1 writes the
- /// GPU timestamp instead (64-bit form), flag 2 the system timestamp.
- extern "system" fn write32(this: This, address: u64, value: u32, _flush: u32, _flags: u32) {
- defer_write(this, address, Written::Value(u64::from(value)), 4);
- value_written(this, address, u64::from(value));
- }
- extern "system" fn write64(this: This, address: u64, value: u64, _flush: u32, flags: u32) {
- const GPU_TIMESTAMP: u32 = 1;
- const SYSTEM_TIMESTAMP: u32 = 2;
- let what = match flags {
- GPU_TIMESTAMP => Written::GpuTimestamp,
- SYSTEM_TIMESTAMP => Written::SystemTimestamp,
- _ => Written::Value(value),
- };
- defer_write(this, address, what, 8);
- if let Written::Value(value) = what {
- value_written(this, address, value);
- }
- }
- /// The title wrote a value at the bottom of the pipe (not a timestamp, not
- /// a query result): listed (diagnostics), and with `VICTORY_CUT_WRITES=1`
- /// the host list is cut right behind it, so that the value is stored when
- /// the card has run the list up to there, as on the console, and not when
- /// the whole list has run. Lists of other queues wait for these values
- /// (the title: the compute list that builds the depth pyramid
- /// waits for one, and the meshlet culling of the same frame reads that
- /// pyramid). Off by default: every such write then ends a host list, and
- /// each part is a submission of its own.
- fn value_written(this: This, address: u64, value: u64) {
- #[cfg(windows)]
- {
- if address == 0 || this.is_null() {
- return;
- }
- #[cfg(not(feature = "diagnostics"))]
- let _ = value;
- #[cfg(feature = "diagnostics")]
- if let Some(host) = host_list(this) {
- crate::capture::op(std::ptr::from_ref(host) as usize, || {
- format!("WRITE VALUE at {address:#x} value {value:#x}")
- });
- }
- static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
- if !*ON.get_or_init(|| std::env::var_os("VICTORY_CUT_WRITES").is_some_and(|v| v == "1"))
- || !crate::queue::cuts()
- {
- return;
- }
- // SAFETY: the title passes one of our lists; a list is recorded by
- // one thread at a time and is not submitted while it records.
- let list = unsafe { &mut *this.cast_mut().cast::<List>() };
- let Some(current) = list.host.as_ref() else {
- return;
- };
- let Some(fresh) = fresh_list() else {
- return;
- };
- current.close();
- if let Some(done) = list.host.replace(fresh) {
- // The part so far keeps its waits; nothing stands before the
- // next one but this part itself.
- let waits = std::mem::take(&mut list.gate);
- list.earlier.push(Part { waits, host: done });
- }
- if let Some(host) = list.host.as_ref() {
- list.state
- .lock()
- .unwrap_or_else(|e| e.into_inner())
- .replay(host);
- }
- }
- #[cfg(not(windows))]
- let _ = (this, address, value);
- }
- fn defer_write(this: This, address: u64, what: Written, bytes: usize) {
- if address == 0 {
- return;
- }
- #[cfg(windows)]
- if let Some(list) = host_list(this) {
- list.defer_write(address as *mut u8, what, bytes);
- return;
- }
- let _ = this;
- // Without a host GPU nothing runs later: write now.
- what.write(address as *mut u8, bytes);
- }
- /// The list methods by their slot in older titles' lists (the 2020-era
- /// layout): IUnknown 3, object 4, device child 1, list 1, graphics list 96
- /// (59 as on PC, then console ones), then versions 1-6 (6 + 1 + 1 + 11 + 3
- /// + 13): 140 slots.
- const METHODS: [(usize, *const ()); 52] = [
- (0, list_query as *const ()),
- (2, list_release as *const ()),
- (7, object::parent_device as *const ()), // 0x38
- (8, list_type as *const ()), // 0x40
- (9, close as *const ()), // 0x48
- (10, reset as *const ()), // 0x50
- (12, state::draw as *const ()), // 0x60 instanced
- (13, state::draw_indexed as *const ()), // 0x68 indexed
- (14, state::dispatch as *const ()), // 0x70
- (15, copy_buffer_region as *const ()), // 0x78
- (16, copy_texture_region as *const ()), // 0x80
- (17, copy_resource as *const ()), // 0x88
- (19, resolve as *const ()), // 0x98
- (20, state::topology as *const ()), // 0xa0
- (21, state::viewports as *const ()), // 0xa8
- (22, state::scissors as *const ()), // 0xb0
- (23, state::blend_factor as *const ()), // 0xb8
- (24, state::stencil_ref as *const ()), // 0xc0
- (25, set_pipeline as *const ()), // 0xc8
- (26, barrier as *const ()), // 0xd0
- (32, state::heaps as *const ()), // 0x100
- (33, ignored_state as *const ()), // 0x108 compute root signature
- (34, ignored_state as *const ()), // 0x110 graphics root signature
- (35, state::compute_table as *const ()), // 0x118
- (36, state::graphics_table as *const ()), // 0x120
- (37, state::compute_constant as *const ()), // 0x128
- (38, state::graphics_constant as *const ()), // 0x130
- (39, state::compute_constants as *const ()), // 0x138
- (40, state::graphics_constants as *const ()), // 0x140
- (41, state::compute_view as *const ()), // 0x148 CBV
- (42, state::graphics_view as *const ()), // 0x150 CBV
- (43, state::compute_view as *const ()), // 0x158 SRV
- (44, state::graphics_view as *const ()), // 0x160 SRV
- (45, state::compute_view as *const ()), // 0x168 UAV
- (46, state::graphics_view as *const ()), // 0x170 UAV
- (47, state::index_buffer as *const ()), // 0x178
- (48, state::vertex_buffers as *const ()), // 0x180
- (50, state::targets as *const ()), // 0x190
- (51, clear_dsv as *const ()), // 0x198
- (52, clear_rtv as *const ()), // 0x1a0
- (59, execute_indirect as *const ()), // 0x1d8 indirect
- (60, copy_memory as *const ()), // 0x1e0
- (68, state::depth_bounds as *const ()), // 0x220 depth bounds
- (69, write32 as *const ()), // 0x228
- (70, write64 as *const ()), // 0x230
- (71, wait_value as *const ()), // 0x238, 32 bits
- (72, wait_value64 as *const ()), // 0x240, 64 bits
- (73, ignored_state as *const ()), // 0x248 pixel shader depth order
- (83, copy_texture_region_x as *const ()), // 0x298
- (84, copy_resource_x as *const ()), // 0x2a0
- (124, ignored_state as *const ()), // 0x3e0 shading rate
- (127, state::dispatch_mesh as *const ()), // 0x3f8 mesh dispatch
- ];
- /// The rest of [`METHODS`], kept apart only to stay readable.
- const MORE_METHODS: [(usize, *const ()); 9] = [
- (28, query_mark as *const ()), // 0xe0 BeginQuery
- (29, query_mark as *const ()), // 0xe8 EndQuery
- (30, resolve_queries as *const ()), // 0xf0
- (78, dispatch_x as *const ()), // 0x270
- (98, state::dispatch_mesh_x as *const ()), // 0x310
- (99, driver_hint as *const ()), // 0x318
- // Pipeline syncs at the top and bottom of the pipe, 0x408 and 0x410.
- (129, sync as *const ()),
- (130, sync as *const ()),
- (137, reset_x as *const ()), // 0x448
- ];
- /// Newer titles' lists have two more methods in the console part of the
- /// base list, after the clears (0x198/0x1a0 as before) and before
- /// ExecuteIndirect, which moved from 0x1d8 to 0x1e8: every slot from 0x1d8
- /// on is two later (taken here as starting exactly at 0x1d8). Inferred from
- /// The title's call shapes: ExecuteIndirect 0x1e8, bottom-of-pipe
- /// writes 0x238/0x240, waits 0x248/0x250, depth bounds 0x230, shading rate
- /// 0x3f0/0x400, syncs 0x418/0x420, ResetX 0x458 (asserts on failure).
- const fn newer_slot(slot: usize) -> usize {
- if slot >= 59 { slot + 2 } else { slot }
- }
- /// Methods only newer titles' lists have. 0x490: the enhanced barrier
- /// `(group count, groups)`, with 16-byte groups `(type, count, barriers)`
- /// and 40-byte buffer barriers (title+0x95e0 builds them). 0x470 `(u64)`,
- /// called from the title's state flush with its result unused.
- /// shortcut: 0x470's purpose is unknown; ignored until a title shows it
- /// matters.
- const NEWER_METHODS: [(usize, *const ()); 3] = [
- (142, ignored_state as *const ()),
- (146, barrier_groups as *const ()),
- (148, allocate_upload as *const ()),
- ];
- const fn list_table(newer: bool) -> [*const (); 160] {
- let mut f = object::d3d12_object(object::missing_table::<LIST, 160>());
- let mut i = 0;
- while i < METHODS.len() + MORE_METHODS.len() {
- let (slot, method) = if i < METHODS.len() {
- METHODS[i]
- } else {
- MORE_METHODS[i - METHODS.len()]
- };
- f[if newer { newer_slot(slot) } else { slot }] = method;
- i += 1;
- }
- let mut i = 0;
- while newer && i < NEWER_METHODS.len() {
- f[NEWER_METHODS[i].0] = NEWER_METHODS[i].1;
- i += 1;
- }
- f
- }
- /// Every slot up to the limit exists and logs, for the methods newer
- /// titles add.
- static LIST_FUNCTIONS: Table<160> = Table(list_table(false));
- static NEWER_LIST_FUNCTIONS: Table<160> = Table(list_table(true));
- #[cfg(test)]
- mod tests {
- use super::*;
- use crate::object::slot;
- #[test]
- fn every_inline_command_falls_back_to_the_table() {
- let null = std::ptr::null();
- let mut allocator = null;
- assert_eq!(
- create_allocator(null, 0, &ALLOCATOR_IIDS[0], &mut allocator),
- S_OK
- );
- let mut list = null;
- let hr = create_list(null, 0, 0, allocator, null, &LIST_IIDS[1], &mut list);
- assert_eq!(hr, S_OK);
- // What the title's inline draw checks: put < draw limit.
- // SAFETY: the list is 0x560 bytes.
- let (put, draw_limit, api_limit) = unsafe {
- let at = |o: usize| list.cast::<u8>().add(o).cast::<u64>().read();
- (at(0x18), at(0x28), at(0x30))
- };
- assert!(put >= draw_limit && put >= api_limit);
- // The console's ID3D12GraphicsCommandList6, as the title asks.
- let mut same = null;
- assert_eq!(list_query(list, &LIST_IIDS[7], &mut same), S_OK);
- assert_eq!(same, list);
- assert_eq!(slot::<extern "system" fn(This) -> u32>(list, 0x10)(list), 1);
- assert_eq!(slot::<extern "system" fn(This) -> u32>(list, 0x40)(list), 0);
- assert_eq!(
- slot::<extern "system" fn(This) -> HRESULT>(list, 0x48)(list),
- S_OK
- );
- // List-owned upload memory: aligned, writable, gone at reset.
- let p = allocate_upload(list, 40, 0x100);
- assert!(!p.is_null() && (p as usize).is_multiple_of(0x100));
- // SAFETY: 40 bytes were handed out.
- unsafe { p.write_bytes(0xab, 40) };
- assert!(allocate_upload(list, 8, 0x30).is_null(), "not a power of two");
- assert_eq!(reset(list, allocator, null), S_OK);
- // SAFETY: the list is ours.
- assert!(unsafe { &(*list.cast::<List>()).uploads }.lock().unwrap().is_empty());
- let release = |o: This| slot::<extern "system" fn(This) -> u32>(o, 0x10)(o);
- assert_eq!(release(list), 0);
- assert_eq!(release(allocator), 0);
- }
- /// An offset near `u64::MAX` must not overflow and write outside the
- /// query destination buffer.
- #[test]
- fn resolve_queries_rejects_overflowing_offset() {
- let null = std::ptr::null();
- let mut allocator = null;
- assert_eq!(
- create_allocator(null, 0, &ALLOCATOR_IIDS[0], &mut allocator),
- S_OK
- );
- let mut list = null;
- assert_eq!(
- create_list(null, 0, 0, allocator, null, &LIST_IIDS[1], &mut list),
- S_OK
- );
- let buffer = crate::resource::test_buffer(64);
- // Used to overflow `offset + count * 8` (panic in debug, wrap in release).
- resolve_queries(list, null, 0, 0, 2, buffer, u64::MAX - 8);
- let release = |o: This| slot::<extern "system" fn(This) -> u32>(o, 0x10)(o);
- for object in [buffer, list, allocator] {
- assert_eq!(release(object), 0);
- }
- }
- }
|