2026-11-23 23:10:01 +01:00
//! Command queues. Submitted lists run on the host GPU and are waited for,
//! so fence signals complete at once.
2026-11-23 14:42:21 +01:00
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std ::sync ::atomic ::{ AtomicBool , AtomicU64 , Ordering };
use crate ::object ::{ self , E_INVALIDARG , HRESULT , Iid , Object , QUEUE , S_OK , Table , This , guid };
use crate ::resource ::PAGEABLE_IIDS ;
2026-11-25 05:38:51 -06:00
#[cfg(windows)]
mod behind ;
2026-11-23 14:42:21 +01:00
const QUEUE_IIDS : [ Iid ; 4 ] = [
guid (
0x0ec870a6 ,
0x5d7e ,
0x4c22 ,
[ 0x8c , 0xfc , 0x5b , 0xaa , 0xe0 , 0x76 , 0x16 , 0xed ],
),
PAGEABLE_IIDS [ 0 ],
PAGEABLE_IIDS [ 1 ],
PAGEABLE_IIDS [ 2 ],
];
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub ( crate ) struct QueueDesc {
kind : u32 ,
priority : i32 ,
flags : u32 ,
nodes : u32 ,
}
/// The title reads the list type right after the header.
#[repr(C)]
struct Queue {
object : Object ,
kind : u32 ,
desc : QueueDesc ,
2026-11-24 13:02:18 -05:00
/// What the queue was given after a wait for a fence that had not been
/// reached, in order; it runs when the fence gets there.
#[cfg(windows)]
held : std ::sync ::Mutex < Vec < Held >> ,
2026-11-23 14:42:21 +01:00
}
const _ : () = assert! ( std ::mem ::offset_of! ( Queue , kind ) == 0x10 );
2026-11-24 13:02:18 -05:00
/// One thing a waiting queue holds back.
#[cfg(windows)]
enum Held {
/// The queue waits for the fence (its address) to reach the value.
Wait ( usize , u64 ),
/// A list of the next submission waits for a memory value.
Value ( crate ::list ::ValueWait ),
/// A submission: the lists' host sides, taken from the title's lists
/// (which it may reset meanwhile), and the order they run in.
Run ( Vec < crate ::list ::Detached > , Vec < usize > ),
/// A signal of the queue's, due after what it holds before it.
Signal ( usize , u64 ),
}
/// The queues that hold something (their addresses).
#[cfg(windows)]
static WAITING : std ::sync ::Mutex < Vec < usize >> = std ::sync ::Mutex ::new ( Vec ::new ());
#[cfg(windows)]
fn lock < T > ( mutex : & std ::sync ::Mutex < T > ) -> std ::sync ::MutexGuard < '_ , T > {
mutex . lock (). unwrap_or_else ( | e | e . into_inner ())
}
/// Whether queues wait for fences. `VICTORY_QUEUE_WAIT=0`: they do not,
/// and every submission runs when it is made (as before).
#[cfg(windows)]
fn waits () -> bool {
static ON : std ::sync ::OnceLock < bool > = std ::sync ::OnceLock ::new ();
* ON . get_or_init ( || std ::env ::var_os ( "VICTORY_QUEUE_WAIT" ). is_none_or ( | v | v != "0" ))
}
/// Whether a submission is held list by list: the lists before the first
/// one whose wait for a memory value is not met run at once, and each list
/// that waits is held with the lists after it, behind its own waits.
/// Holding a submission whole made the title's queues wait for each other:
/// a graphics submission was held for a value a compute list writes, and
/// that compute list for a value an earlier list of the same graphics
/// submission writes. Nothing moved until the present forced it, and the
/// title reached its present only after waiting about 2.5 s, every frame
/// (runs 27-30: 2.4-2.9 s from the last submission to the present call,
/// 13-32 ms before waits were honoured; the frame's main graphics
/// submissions, 88 lists, ran forced, the compute lists before them).
/// On by default; `VICTORY_SPLIT_WAITS=0` holds whole submissions, as
/// before.
#[cfg(windows)]
fn split () -> bool {
static ON : std ::sync ::OnceLock < bool > = std ::sync ::OnceLock ::new ();
* ON . get_or_init ( || std ::env ::var_os ( "VICTORY_SPLIT_WAITS" ). is_none_or ( | v | v != "0" ))
}
/// Whether a list is cut into host lists at its waits for memory values
/// (`crate::list::cut`), and a queue runs a submission part by part: it
/// stands at each wait until the value is in memory, with everything
/// recorded before the wait run and its bottom-of-pipe writes done, as on
/// the console. Held list by list ([`split`]) a wait still held its whole
/// list: the title's lists are chains of jobs, each behind a wait for a
/// value another queue's list writes in the middle of its own chain
/// (frame 858 of run 33: a compute list of 433 commands with 13 waits;
/// graphics lists of the form "341 dispatches, wait, draws"), so two lists
/// each waited for a write behind the other's wait and only the present
/// moved them, in the wrong order.
/// On by default where waits are honoured list by list;
/// `VICTORY_CUT_WAITS=0` holds whole lists, as before.
#[cfg(windows)]
pub ( crate ) fn cuts () -> bool {
static ON : std ::sync ::OnceLock < bool > = std ::sync ::OnceLock ::new ();
waits () && split () && * ON . get_or_init ( || std ::env ::var_os ( "VICTORY_CUT_WAITS" ). is_none_or ( | v | v != "0" ))
}
/// Whether a present runs what queues still hold (on by default: a wait
/// is not kept across frames). `VICTORY_WAIT_FORCE=0` keeps it held: for
/// finding out whether the title's waits reach across its presents; a
/// wait nothing ever meets then stops the title for good.
#[cfg(windows)]
fn forces () -> bool {
static ON : std ::sync ::OnceLock < bool > = std ::sync ::OnceLock ::new ();
* ON . get_or_init ( || std ::env ::var_os ( "VICTORY_WAIT_FORCE" ). is_none_or ( | v | v != "0" ))
}
/// A submission to a queue whose lists are cut at their waits ([`cuts`]).
/// The lists before the first one that waits run at once if the queue
/// holds nothing; the rest goes behind what the queue holds, part by part
/// (`crate::list::detach_parts`): the waits of a part, then its host
/// lists. [`release_held`] runs the queue's line as far as the values are
/// there, now and whenever a submission has run.
#[cfg(windows)]
fn submit_cut ( this : This , lists : & [ This ]) {
let Some ( host ) = crate ::host ::host () else {
return ;
};
let first = if lock ( & queue ( this ). held ). is_empty () {
lists
. iter ()
. position ( |& l | crate ::list ::cut_list ( l ))
. unwrap_or ( lists . len ())
} else {
0
};
#[cfg(feature = "diagnostics" )]
crate ::capture ::op ( this as usize , || {
let named : Vec < String > = lists
. iter ()
. map ( |& l | match crate ::list ::host_list ( l ) {
Some ( h ) => format! ( " {:#x} " , std ::ptr ::from_ref ( h ) as usize ),
None => format! ( "title {l:?} " ),
})
. collect ();
format! (
"SUBMIT queue kind {:?} held {} lists {named:?} (the first {first} run at once, the rest part by part)" ,
Some ( queue ( this ). kind ),
first < lists . len ()
)
});
if first > 0 && crate ::host ::late () {
// Sent to the card and not waited for: their host sides are taken
// from the title's lists (which it may reset before they have run)
// and come back, with their tables' slots, when they are settled.
let ( taken , order ) = crate ::list ::detach ( & lists [ .. first ]);
2026-11-26 20:00:25 +05:30
crate ::host ::two_queues ::for_title ( Some ( queue ( this ). kind ), 0 , || host . execute_late ( taken , order ));
2026-11-24 13:02:18 -05:00
} else if first > 0 {
let now : Vec < _ > = lists [ .. first ]
. iter ()
. filter_map ( |& l | crate ::list ::host_list ( l ))
. collect ();
host . execute ( & now );
// They have run: their descriptor tables' slots are free again, as
// on the path without cuts ([`execute`]). This path replaced that
// one without the call, so the lists that run at once kept their
// slots until the title's reset and the ring ran out as before
// (run 45: 3,143 of 15,942 tables refused in one frame).
crate ::list ::tables_done ( & lists [ .. first ]);
}
if first < lists . len () {
let parts = crate ::list ::detach_parts ( & lists [ first .. ]);
static SEEN : AtomicU64 = AtomicU64 ::new ( 0 );
if SEEN . fetch_add ( 1 , Ordering ::Relaxed ) < 8 {
tracing ::info! (
kind = queue ( this ). kind ,
lists = lists . len (),
run_at_once = first ,
parts = parts . len (),
waits = parts . iter (). map ( | p | p . 0. len ()). sum ::< usize > (),
"a submission is given to its queue part by part (its lists wait for memory values)"
);
}
{
let mut held = lock ( & queue ( this ). held );
for ( waits , taken , order ) in parts {
held . extend ( waits . into_iter (). map ( Held ::Value ));
held . push ( Held ::Run ( taken , order ));
}
}
let mut waiting = lock ( & WAITING );
if ! waiting . contains ( & ( this as usize )) {
waiting . push ( this as usize );
}
}
// What ran may be what another queue waits for, and what was just
// given to the queue may be free to run already.
release_held ( false );
}
/// A submission to a queue that honours waits, list by list ([`split`]).
/// shortcut: a wait still holds its whole list, not only the commands
/// after it; and lists named twice among those held stay in one piece.
#[cfg(windows)]
fn submit_split ( this : This , lists : & [ This ]) {
let Some ( host ) = crate ::host ::host () else {
return ;
};
let by = crate ::list ::waits_by_list ( lists );
// Behind what the queue already holds, nothing runs now.
let cut = if lock ( & queue ( this ). held ). is_empty () {
( 0 .. lists . len ())
. find ( |& i | by [ i ]. iter (). any ( | w | ! w . met ()))
. unwrap_or ( lists . len ())
} else {
0
};
#[cfg(feature = "diagnostics" )]
crate ::capture ::op ( this as usize , || {
let named : Vec < String > = lists
. iter ()
. map ( |& l | match crate ::list ::host_list ( l ) {
Some ( h ) => format! ( " {:#x} " , std ::ptr ::from_ref ( h ) as usize ),
None => format! ( "title {l:?} " ),
})
. collect ();
format! (
"SUBMIT queue kind {:?} held {} lists {named:?} (the first {cut} run at once)" ,
Some ( queue ( this ). kind ),
cut < lists . len ()
)
});
if cut > 0 {
let now : Vec < _ > = lists [ .. cut ]
. iter ()
. filter_map ( |& l | crate ::list ::host_list ( l ))
. collect ();
host . execute ( & now );
}
if cut < lists . len () {
static SEEN : AtomicU64 = AtomicU64 ::new ( 0 );
if SEEN . fetch_add ( 1 , Ordering ::Relaxed ) < 8 {
tracing ::info! (
kind = queue ( this ). kind ,
lists = lists . len (),
run_at_once = cut ,
waits = ? by [ cut ],
"lists of a submission are held for a memory value"
);
}
let rest = & lists [ cut .. ];
// A list's host side can be taken once.
let twice = rest . iter (). enumerate (). any ( | ( i , l ) | rest [ .. i ]. contains ( l ));
let mut pieces : Vec < Held > = Vec ::new ();
let mut from = cut ;
while from < lists . len () {
// Up to the next list that waits for anything (met or not: it
// is looked at again when its turn comes).
let to = if twice {
lists . len ()
} else {
( from + 1 .. lists . len ())
. find ( |& i | ! by [ i ]. is_empty ())
. unwrap_or ( lists . len ())
};
let waits : Vec < crate ::list ::ValueWait > = by [ from .. to ]. concat ();
pieces . extend ( waits . into_iter (). map ( Held ::Value ));
let ( taken , order ) = crate ::list ::detach ( & lists [ from .. to ]);
pieces . push ( Held ::Run ( taken , order ));
from = to ;
}
lock ( & queue ( this ). held ). extend ( pieces );
let mut waiting = lock ( & WAITING );
if ! waiting . contains ( & ( this as usize )) {
waiting . push ( this as usize );
}
}
// What ran may be what another queue waits for, and what was just held
// may be free to run already.
release_held ( false );
}
/// Adds `what()` to what the queue holds, if it holds anything (it waits
/// for a fence): whether it was added.
#[cfg(windows)]
fn hold ( this : This , what : impl FnOnce () -> Held ) -> bool {
if this . is_null () {
return false ;
}
let mut held = lock ( & queue ( this ). held );
if held . is_empty () {
return false ;
}
held . push ( what ());
true
}
/// Runs what waiting queues hold as far as their fences have been reached
/// (`all`: all of it), each queue's in the order it was given. Called
/// where a fence may have moved: after a signal and after a submission (a
/// list's bottom-of-pipe write may be into a fence), never from inside
/// one.
#[cfg(windows)]
pub ( crate ) fn release_held ( all : bool ) {
2026-11-25 05:38:51 -06:00
if behind ::on () {
// What queues stand at is looked at by the thread that runs them.
behind ::wake ();
return ;
}
2026-11-24 13:02:18 -05:00
// One caller at a time, so a queue's submissions keep their order.
static RELEASING : std ::sync ::Mutex < () > = std ::sync ::Mutex ::new (());
if lock ( & WAITING ). is_empty () {
return ;
}
2026-11-24 23:48:39 -06:00
let _looking = crate ::timeline ::span ( "looking at what queues hold" );
2026-11-24 13:02:18 -05:00
let _one = lock ( & RELEASING );
loop {
let queues = lock ( & WAITING ). clone ();
let mut moved = false ;
for q in queues {
// SAFETY: a live queue: one released is taken off the list.
let held = unsafe { & ( * ( q as * const Queue )). held };
let mut held = lock ( held );
loop {
match held . first () {
None => break ,
Some ( & Held ::Wait ( fence , value ))
if ! all && crate ::fence ::completed ( fence as This ) < value =>
{
break ;
}
Some ( Held ::Value ( wait )) if ! all && ! value_met ( wait ) => break ,
Some ( _ ) => {}
}
match held . remove ( 0 ) {
Held ::Wait ( .. ) => {}
Held ::Value ( wait ) => {
// Passed at a present without being met: what the
// queue stood at.
if all && ! value_met ( & wait ) {
static SEEN : AtomicU64 = AtomicU64 ::new ( 0 );
if SEEN . fetch_add ( 1 , Ordering ::Relaxed ) < 16 {
tracing ::warn! (
queue = format_args! ( " {q:#x} " ),
? wait ,
"a wait for a memory value was not met at a present: the queue runs on"
);
}
}
}
Held ::Run ( lists , order ) => {
#[cfg(feature = "diagnostics" )]
crate ::capture ::op ( q , || {
format! (
"RUN held submission of {} lists (forced {all} )" ,
order . len ()
)
});
// Diagnostics: the part behind this one stands at
// waits that are met already, so the two could have
// been one host submission (one wait for the card,
// each range shaders write compared once).
#[cfg(feature = "diagnostics" )]
{
let waits = held . iter (). take_while ( | h | matches! ( h , Held ::Value ( _ ))). count ();
if waits > 0
&& matches! ( held . get ( waits ), Some ( Held ::Run ( .. )))
&& held [ .. waits ]. iter (). all ( | h | matches! ( h , Held ::Value ( w ) if w . met ()))
{
crate ::copystat ::add ( crate ::copystat ::C ::PartsTogether , 1 );
}
}
if crate ::host ::late ()
&& let Some ( host ) = crate ::host ::host ()
{
// Not waited for: the host keeps the lists
// until they are settled.
2026-11-26 20:00:25 +05:30
crate ::host ::two_queues ::for_title ( kind_of ( q ), 0 , || host . execute_late ( lists , order ));
2026-11-24 13:02:18 -05:00
} else {
let ran = {
let runs : Vec <& crate ::host ::HostList > =
order . iter (). map ( |& i | lists [ i ]. host ()). collect ();
match crate ::host ::host () {
Some ( host ) => {
host . execute ( & runs );
true
}
None => false ,
}
};
// They have run: their host lists are used again.
if ran {
crate ::list ::recycle ( lists , & order );
}
}
if all {
#[cfg(feature = "diagnostics" )]
crate ::copystat ::add ( crate ::copystat ::C ::Forced , 1 );
static ONCE : AtomicBool = AtomicBool ::new ( false );
if ! ONCE . swap ( true , Ordering ::Relaxed ) {
tracing ::warn! (
"a queue still held a submission at a present: its fence was not reached"
);
}
}
}
// Behind what was sent to the card and not waited
// for, if anything was.
Held ::Signal ( fence , value ) => {
if ! crate ::host ::late_signal ( fence , value ) {
crate ::fence ::set ( fence as This , value );
}
}
}
moved = true ;
}
if held . is_empty () {
lock ( & WAITING ). retain ( |& w | w != q );
}
}
if ! moved {
break ;
}
}
}
2026-11-25 05:38:51 -06:00
/// Before a submission of ours that is waited for, or a wait for flights by
/// a thread of the title's: what the title gave its queues before goes to the
/// card first ([`behind`]).
#[cfg(windows)]
pub ( crate ) fn before_sync () {
if behind ::on () {
behind ::drain ();
}
}
2026-11-25 11:05:14 -06:00
/// Whether submissions go into the line of a thread of ours ([`behind`]).
#[cfg(windows)]
pub ( crate ) fn line_on () -> bool {
behind ::on ()
}
/// Lists in the line may have been translated ([`crate::list::thin`]).
#[cfg(windows)]
pub ( crate ) fn line_moved () {
behind ::wake ();
}
/// This thread does work the line waits for: it does not wait for the line.
#[cfg(windows)]
pub ( crate ) fn no_drain_here () {
behind ::no_drain_here ();
}
2026-11-25 05:38:51 -06:00
/// Submissions the title made that the host has not begun ([`behind`]).
#[cfg(windows)]
pub ( crate ) fn queued () -> u64 {
behind ::queued ()
}
/// For the frame-rate line.
#[cfg(windows)]
pub ( crate ) fn behind_line ( frames : f64 ) {
if behind ::on () {
2026-11-25 11:05:14 -06:00
crate ::list ::thin ::line ( frames );
2026-11-25 05:38:51 -06:00
behind ::line ( frames );
}
}
2026-11-24 13:02:18 -05:00
/// Whether a queue may pass `wait`: memory holds the value, or, with
/// submissions sent to the card without a wait (`crate::host::late`), it
/// will once those in the air have run.
#[cfg(windows)]
fn value_met ( wait : & crate ::list ::ValueWait ) -> bool {
wait . met () || ( crate ::host ::late () && crate ::host ::late_met ( wait ))
}
2026-11-23 14:42:21 +01:00
fn queue < 'a > ( this : This ) -> & 'a Queue {
// SAFETY: called from queue methods only.
unsafe { &* this . cast ::< Queue > () }
}
pub ( crate ) extern "system" fn create (
_ : This ,
desc : * const QueueDesc ,
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 , "command queue" );
2026-11-26 20:00:25 +05:30
#[cfg(windows)]
let kind = desc . kind ;
2026-11-23 14:42:21 +01:00
let queue = Box ::new ( Queue {
2026-11-23 23:10:01 +01:00
object : if crate ::device ::newer () {
Object ::new ( & NEWER_FUNCTIONS )
} else {
Object ::new ( & FUNCTIONS )
},
2026-11-23 14:42:21 +01:00
kind : desc . kind ,
desc ,
2026-11-24 13:02:18 -05:00
#[cfg(windows)]
held : Default ::default (),
2026-11-23 14:42:21 +01:00
});
2026-11-26 20:00:25 +05:30
let made = object ::hand_out ( QUEUE , queue , iid , & QUEUE_IIDS , out );
#[cfg(windows)]
if made == S_OK
// SAFETY: the out pointer was written.
&& let Some ( & queue_at ) = unsafe { out . as_ref () }
{
let mut kinds = lock ( & KINDS );
kinds . retain ( | k | k . 0 != queue_at as usize );
kinds . push (( queue_at as usize , kind ));
}
made
}
/// The title's queues (their addresses) and their list types, for what is
/// done with a queue's submission later, on another thread, when the
/// queue may have been let go ([`kind_of`]).
#[cfg(windows)]
static KINDS : std ::sync ::Mutex < Vec < ( usize , u32 ) >> = std ::sync ::Mutex ::new ( Vec ::new ());
/// The list type of the title's queue at `queue`, if it is one.
#[cfg(windows)]
pub ( crate ) fn kind_of ( queue : usize ) -> Option < u32 > {
lock ( & KINDS ). iter (). find ( | k | k . 0 == queue ). map ( | k | k . 1 )
2026-11-23 14:42:21 +01:00
}
2026-11-23 23:10:01 +01:00
/// The console's queue description: an engine or pipe and a queue on it
/// in place of PC's priority and node mask.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub ( crate ) struct QueueDescX {
kind : u32 ,
flags : u32 ,
engine_or_pipe : u32 ,
queue : u32 ,
}
/// Device method `CreateCommandQueueX`. The host picks engines itself, so
/// the queue is created as on PC.
pub ( crate ) extern "system" fn create_x (
this : This ,
desc : * const QueueDescX ,
iid : * const Iid ,
out : * mut This ,
) -> HRESULT {
// SAFETY: the title passes a description (null is tolerated).
let Some ( & x ) = ( unsafe { desc . as_ref () }) else {
return E_INVALIDARG ;
};
tracing ::debug! ( desc = ? x , "console command queue" );
let desc = QueueDesc {
kind : x . kind ,
priority : 0 ,
flags : x . flags ,
nodes : 1 ,
};
create ( this , & desc , iid , out )
}
2026-11-23 14:42:21 +01:00
extern "system" fn query ( this : This , iid : * const Iid , out : * mut This ) -> HRESULT {
object ::answer (
QUEUE ,
iid ,
object ::is ( iid , & QUEUE_IIDS ). then_some ( this ),
out ,
)
}
extern "system" fn release ( this : This ) -> u32 {
2026-11-24 13:02:18 -05:00
let left = object ::release_boxed ::< Queue > ( this );
#[cfg(windows)]
if left == 0 {
lock ( & WAITING ). retain ( |& q | q != this as usize );
2026-11-26 20:00:25 +05:30
lock ( & KINDS ). retain ( | k | k . 0 != this as usize );
2026-11-24 13:02:18 -05:00
}
left
2026-11-23 14:42:21 +01:00
}
2026-11-24 13:02:18 -05:00
/// Runs the lists' host lists and waits for them (decision 0007). A queue
/// that waits for a fence keeps them until the fence is reached.
extern "system" fn execute ( this : This , count : u32 , lists : * const This ) {
2026-11-23 14:42:21 +01:00
if lists . is_null () {
return ;
}
2026-11-24 23:48:39 -06:00
let _called = crate ::timeline ::span ( "the title's ExecuteCommandLists" );
#[cfg(all(windows, feature = "diagnostics" ))]
crate ::timeline ::title_thread ();
2026-11-23 14:42:21 +01:00
#[cfg(windows)]
if let Some ( host ) = crate ::host ::host () {
// SAFETY: the title passes `count` lists.
let lists = unsafe { std ::slice ::from_raw_parts ( lists , count as usize ) };
2026-11-25 05:38:51 -06:00
if behind ::on () && ! this . is_null () {
behind ::submit ( this , lists );
return ;
}
2026-11-24 13:02:18 -05:00
if cuts () && ! this . is_null () {
submit_cut ( this , lists );
return ;
}
if waits () && split () && ! this . is_null () {
submit_split ( this , lists );
return ;
}
// A list that waits for a memory value not there yet: the queue
// holds the submission, as it does behind a fence.
if waits () && ! this . is_null () {
let unmet = crate ::list ::unmet_waits ( lists );
if ! unmet . is_empty () {
static SEEN : AtomicU64 = AtomicU64 ::new ( 0 );
if SEEN . fetch_add ( 1 , Ordering ::Relaxed ) < 8 {
tracing ::info! ( kind = queue ( this ). kind , ? unmet , "a submission is held for a memory value" );
}
lock ( & queue ( this ). held ). extend ( unmet . into_iter (). map ( Held ::Value ));
let mut waiting = lock ( & WAITING );
if ! waiting . contains ( & ( this as usize )) {
waiting . push ( this as usize );
}
}
}
let kept = hold ( this , || {
let ( taken , order ) = crate ::list ::detach ( lists );
Held ::Run ( taken , order )
});
// Diagnostics: the order submissions are made in, next to the
// commands of the frame's `ops` file (which are in recording order).
#[cfg(feature = "diagnostics" )]
crate ::capture ::op ( this as usize , || {
let kind = ( ! this . is_null ()). then ( || queue ( this ). kind );
// The lists as the frame's other lines name them (their host sides).
let named : Vec < String > = lists
. iter ()
. map ( |& l | match crate ::list ::host_list ( l ) {
Some ( h ) => format! ( " {:#x} " , std ::ptr ::from_ref ( h ) as usize ),
None => format! ( "title {l:?} " ),
})
. collect ();
format! ( "SUBMIT queue kind {kind:?} held {kept} lists {named:?} " )
});
if kept {
return ;
}
2026-11-23 14:42:21 +01:00
let host_lists : Vec < _ > = lists
. iter ()
. filter_map ( |& l | crate ::list ::host_list ( l ))
. collect ();
host . execute ( & host_lists );
2026-11-24 13:02:18 -05:00
crate ::list ::tables_done ( lists );
// What the lists wrote at the bottom of the pipe may be a fence
// another queue waits for.
release_held ( false );
2026-11-23 14:42:21 +01:00
return ;
}
2026-11-24 13:02:18 -05:00
let _ = this ;
2026-11-23 14:42:21 +01:00
static ONCE : AtomicBool = AtomicBool ::new ( false );
if ! ONCE . swap ( true , Ordering ::Relaxed ) {
tracing ::warn! ( count , "command lists submitted; no host GPU runs them" );
}
}
/// The start of a present plane: frame token, then the plane's resources.
#[repr(C)]
pub ( crate ) struct Plane {
token : u64 ,
count : u32 ,
resources : * const This ,
}
2026-11-25 14:55:26 -08:00
/// Retail PresentX adds source-rectangle and destination pointers, a color
/// space and seven option words after the common prefix. Older callers pass
/// only the prefix for single-plane presents.
#[cfg(windows)]
#[repr(C)]
struct RetailPlane {
prefix : Plane ,
source_rects : * const (),
destination : * const (),
color_space : u32 ,
options : [ u32 ; 7 ],
}
#[cfg(windows)]
const _ : () = assert! ( size_of ::< RetailPlane > () == 72 );
#[cfg(windows)]
fn overlay_resource ( parameters : * const Plane ) -> Option < This > {
// Read only the second prefix at the 72-byte retail PresentX stride. A
// caller using a different layout can still present its base plane safely.
let second = parameters . cast ::< RetailPlane > (). wrapping_add ( 1 ). cast ::< u8 > ();
let mut prefix = [ 0 ; size_of ::< Plane > ()];
if ! crate ::host ::read_into ( second , & mut prefix ) {
return None ;
}
// SAFETY: the prefix has the C layout above and its bytes were copied.
let plane = unsafe { prefix . as_ptr (). cast ::< Plane > (). read_unaligned () };
let mut pointer = [ 0 ; size_of ::< This > ()];
if plane . count == 0
|| plane . resources . is_null ()
|| ! crate ::host ::read_into ( plane . resources . cast (), & mut pointer )
{
return None ;
}
let resource = usize ::from_ne_bytes ( pointer ) as This ;
crate ::resource ::is_resource ( resource ). then_some ( resource )
}
2026-11-23 23:10:01 +01:00
/// Submission tagged with the frame it belongs to `(count, lists, frame
/// token, flags)`; frames are paced elsewhere, so it runs as a plain one.
extern "system" fn execute_x ( this : This , count : u32 , lists : * const This , _token : u64 , _flags : u32 ) {
execute ( this , count , lists );
}
/// Signal with sync settings for the top and bottom of the pipe; our
/// submissions are complete when they return, so it signals at once.
extern "system" fn signal2_x (
this : This ,
fence : This ,
value : u64 ,
_bottom : * const u8 ,
_top : * const u8 ,
) -> HRESULT {
signal ( this , fence , value )
}
2026-11-23 14:42:21 +01:00
/// Markers and events only matter to profilers.
extern "system" fn marker ( _ : This ) {}
2026-11-24 13:02:18 -05:00
extern "system" fn signal ( this : This , fence : This , value : u64 ) -> HRESULT {
2026-11-23 14:42:21 +01:00
if fence . is_null () {
return E_INVALIDARG ;
}
2026-11-24 23:48:39 -06:00
let _called = crate ::timeline ::span ( "the title's queue Signal" );
2026-11-25 05:38:51 -06:00
#[cfg(windows)]
if behind ::on () && ! this . is_null () {
behind ::signal ( this , fence , value );
return S_OK ;
}
2026-11-24 13:02:18 -05:00
// A queue that waits signals after what it holds.
#[cfg(windows)]
if hold ( this , || Held ::Signal ( fence as usize , value )) {
return S_OK ;
}
// Behind the submissions sent to the card and not waited for: the
// fence is set when they are settled.
#[cfg(windows)]
if crate ::host ::late_signal ( fence as usize , value ) {
return S_OK ;
}
let _ = this ;
2026-11-23 14:42:21 +01:00
crate ::fence ::signal ( fence , value )
}
2026-11-24 13:02:18 -05:00
/// From here on the queue runs nothing it is given until `fence` reaches
/// `value`: how the console orders work between queues (the title's
/// compute queue waits for the depth the graphics queue draws). Our
/// submissions run when they are made, so a wait for a value already
/// reached is nothing; otherwise what the queue is given next is held
/// ([`Held`]) until a signal or a list's bottom-of-pipe write gets the
/// fence there.
extern "system" fn wait ( this : This , fence : This , value : u64 ) -> HRESULT {
if fence . is_null () {
return E_INVALIDARG ;
}
2026-11-24 23:48:39 -06:00
let _called = crate ::timeline ::span ( "the title's queue Wait" );
2026-11-24 13:02:18 -05:00
#[cfg(windows)]
if ! this . is_null () {
let now = crate ::fence ::completed ( fence );
// Whether the title uses this at all (the first calls).
{
static CALLS : AtomicU64 = AtomicU64 ::new ( 0 );
if CALLS . fetch_add ( 1 , Ordering ::Relaxed ) < 8 {
tracing ::info! ( kind = queue ( this ). kind , now , value , "a queue is told to wait for a fence" );
}
}
#[cfg(feature = "diagnostics" )]
crate ::capture ::op ( this as usize , || format! ( "WAIT queue kind {} for fence {fence:?} value {value} (now {now} )" , queue ( this ). kind ));
2026-11-25 05:38:51 -06:00
if behind ::on () && waits () {
behind ::wait ( this , fence , value );
return S_OK ;
}
2026-11-24 13:02:18 -05:00
let behind = {
let mut held = lock ( & queue ( this ). held );
let behind = ! held . is_empty () || now < value ;
if behind && waits () {
held . push ( Held ::Wait ( fence as usize , value ));
}
behind
};
if behind {
static SEEN : AtomicU64 = AtomicU64 ::new ( 0 );
if SEEN . fetch_add ( 1 , Ordering ::Relaxed ) < 8 {
tracing ::info! (
kind = queue ( this ). kind ,
now ,
value ,
held = waits (),
"a queue waits for a fence not reached yet"
);
}
}
if behind && waits () {
{
let mut waiting = lock ( & WAITING );
if ! waiting . contains ( & ( this as usize )) {
waiting . push ( this as usize );
}
}
// The fence may have got there meanwhile.
release_held ( false );
}
}
let _ = ( this , value );
S_OK
2026-11-23 14:42:21 +01:00
}
2026-11-23 23:10:01 +01:00
/// Ticks per second of GPU timestamps (see [`Written::GpuTimestamp`]).
2026-11-23 14:42:21 +01:00
extern "system" fn timestamp_frequency ( _ : This , out : * mut u64 ) -> HRESULT {
if out . is_null () {
return E_INVALIDARG ;
}
// SAFETY: checked non-null.
2026-11-23 23:10:01 +01:00
unsafe { out . write ( TIMESTAMP_FREQUENCY ) };
2026-11-23 14:42:21 +01:00
S_OK
}
2026-11-23 23:10:01 +01:00
const TIMESTAMP_FREQUENCY : u64 = 100_000_000 ;
/// What a list's bottom-of-pipe write stores when the list has run.
#[derive(Debug, Clone, Copy)]
pub ( crate ) enum Written {
Value ( u64 ),
/// The GPU clock: [`TIMESTAMP_FREQUENCY`] ticks per second since the
/// process started (our GPU work is done on the CPU's timeline).
GpuTimestamp ,
/// The CPU's performance counter.
SystemTimestamp ,
}
impl Written {
/// Stores the low `bytes` (4 or 8) of the value at `at`.
pub ( crate ) fn write ( self , at : * mut u8 , bytes : usize ) {
let value = match self {
Written ::Value ( v ) => v ,
Written ::GpuTimestamp => {
static START : std ::sync ::OnceLock < std ::time ::Instant > = std ::sync ::OnceLock ::new ();
let elapsed = START . get_or_init ( std ::time ::Instant ::now ). elapsed ();
2026-11-25 03:35:03 -06:00
let now = ( elapsed . as_nanos () / ( 1_000_000_000 / u128 ::from ( TIMESTAMP_FREQUENCY ))) as u64 ;
#[cfg(all(windows, feature = "diagnostics" ))]
crate ::host ::spans ::stamped ( at as usize , now );
as_loaded ( now , at as usize , gpu_load ())
2026-11-23 23:10:01 +01:00
}
Written ::SystemTimestamp => system_timestamp (),
};
2026-11-24 23:48:39 -06:00
#[cfg(all(windows, feature = "diagnostics" ))]
crate ::timeline ::told ( "written when its list had run" , at as usize , value );
2026-11-23 23:10:01 +01:00
// SAFETY: the title names writable GPU memory (unified with ours).
unsafe {
if bytes == 4 {
at . cast ::< u32 > (). write_unaligned ( value as u32 );
} else {
at . cast ::< u64 > (). write_unaligned ( value );
}
2026-11-25 14:43:52 -08:00
#[cfg(windows)]
windows_sys ::Win32 ::System ::Threading ::WakeByAddressAll ( at . cast ());
2026-11-23 23:10:01 +01:00
}
}
}
2026-11-25 03:35:03 -06:00
/// What the title's GPU time is made to look like, in ticks
/// (`VICTORY_GPU_LOAD_MS`; unset: as it is).
///
/// The title asks for four GPU timestamps a frame, in pairs of 16 bytes, the
/// start in the second eight and the end in the first. All of them are written
/// when the frame's last submissions land, within microseconds, so every pair
/// says the card did nothing.
fn gpu_load () -> u64 {
static LOAD : std ::sync ::OnceLock < u64 > = std ::sync ::OnceLock ::new ();
* LOAD . get_or_init ( || {
let ms = std ::env ::var ( "VICTORY_GPU_LOAD_MS" ). ok (). and_then ( | v | v . trim (). parse ::< u64 > (). ok ()). unwrap_or ( 0 );
ms * ( TIMESTAMP_FREQUENCY / 1000 )
})
}
/// The timestamp written at `at` when the clock says `now`, if a pair's start
/// is to lie `load` ticks before its end.
fn as_loaded ( now : u64 , at : usize , load : u64 ) -> u64 {
if at & 8 != 0 {
now . saturating_sub ( load ). max ( 1 )
} else {
now
}
}
2026-11-23 23:10:01 +01:00
fn system_timestamp () -> u64 {
#[cfg(windows)]
{
let mut count = 0 ;
// SAFETY: a valid out pointer.
unsafe { windows_sys ::Win32 ::System ::Performance ::QueryPerformanceCounter ( & mut count ) };
count as u64
}
#[cfg(not(windows))]
0
}
/// Frames presented so far.
static FRAMES : AtomicU64 = AtomicU64 ::new ( 0 );
#[cfg(feature = "diagnostics" )]
pub ( crate ) fn presented () -> u64 {
FRAMES . load ( Ordering ::Relaxed )
}
2026-11-23 14:42:21 +01:00
/// Shows the first plane's first resource in our window.
///
/// shortcut: one plane, no scaling or placement, the source expected in
/// the common state.
extern "system" fn present (
2026-11-25 05:38:51 -06:00
this : This ,
2026-11-23 14:42:21 +01:00
planes : u32 ,
plane_parameters : * const Plane ,
_parameters : * const u8 ,
) -> HRESULT {
2026-11-25 05:38:51 -06:00
#[cfg(not(windows))]
let _ = this ;
2026-11-24 13:02:18 -05:00
#[cfg(all(windows, feature = "diagnostics" ))]
crate ::copystat ::present_begins ();
2026-11-24 23:48:39 -06:00
let called = crate ::timeline ::span ( "the title's Present" );
#[cfg(all(windows, feature = "diagnostics" ))]
crate ::timeline ::title_thread ();
2026-11-24 13:02:18 -05:00
// Submissions sent to the card and not waited for are settled first,
// and what that frees runs in its own order, before anything is forced.
#[cfg(windows)]
2026-11-25 05:38:51 -06:00
if behind ::on () {
// The present goes into the line of what the queues were given.
} else if crate ::host ::late () && ! crate ::host ::late_free_present () {
2026-11-24 13:02:18 -05:00
crate ::host ::settle_for_present ();
release_held ( false );
crate ::host ::settle_for_present ();
} else if crate ::host ::late () {
// The present is sent ahead too: what waits for a value a flight
// will write runs behind that flight, and nothing is settled here.
release_held ( false );
}
// shortcut: what a queue still holds at a present runs now, whatever
// its fence: a wait is not kept across frames.
#[cfg(windows)]
2026-11-25 05:38:51 -06:00
if ! behind ::on () {
release_held ( forces ());
}
2026-11-23 14:42:21 +01:00
let frame = FRAMES . fetch_add ( 1 , Ordering ::Relaxed ) + 1 ;
if frame . is_power_of_two () {
2026-11-23 23:10:01 +01:00
let ( draws , dispatches ) = crate ::list ::take_counts ();
#[cfg(windows)]
let ( drawn , dispatched ) = {
static DRAWN : AtomicU64 = AtomicU64 ::new ( 0 );
static DISPATCHED : AtomicU64 = AtomicU64 ::new ( 0 );
let since = | last : & AtomicU64 , now : u64 | now - last . swap ( now , Ordering ::Relaxed );
(
since ( & DRAWN , crate ::draw ::drawn ()),
since ( & DISPATCHED , crate ::draw ::dispatched ()),
)
};
#[cfg(not(windows))]
let ( drawn , dispatched ) = ( 0 , 0 );
tracing ::info! (
frame ,
draws ,
drawn ,
dispatches ,
dispatched ,
"frame presented (since the last line: draws and dispatches, and those run on the host)"
);
#[cfg(windows)]
if frame >= 256 {
let ( hits , misses ) = crate ::shader_cache ::counts ();
tracing ::info! ( hits , misses , "shaders from the cache and translated so far" );
2026-11-27 01:25:28 +05:30
// `VICTORY_EARLY_DEPTH`: what became of the console's early depth
// test in the pixel programs translated so far.
let [ given , off , exports ] = victory_shader ::emit ::early_depth_counts ();
if given + off + exports > 0 {
tracing ::info! (
given ,
off ,
exports ,
"pixel programs translated so far whose console code tests depth before them: given the host's early test (VICTORY_EARLY_DEPTH=1), left with the late test (switch off), left with it as they write depth or stencil"
);
}
2026-11-23 23:10:01 +01:00
}
}
#[cfg(all(windows, feature = "diagnostics" ))]
2026-11-24 13:02:18 -05:00
{
crate ::capture ::frame_drew ( frame , crate ::draw ::drawn ());
if crate ::capture ::listed ( frame ) {
2026-11-25 05:38:51 -06:00
before_sync ();
2026-11-24 13:02:18 -05:00
crate ::capture ::dump_targets ( frame );
}
2026-11-23 14:42:21 +01:00
}
#[cfg(windows)]
2026-11-25 05:38:51 -06:00
if behind ::on () && ! this . is_null () {
const COPY_SOURCE : u32 = 0x800 ;
2026-11-25 14:55:26 -08:00
let base_res = if planes > 0
2026-11-25 05:38:51 -06:00
// SAFETY: the title passes `planes` plane descriptions.
2026-11-25 14:55:26 -08:00
&& let Some ( plane ) = unsafe { plane_parameters . as_ref () }
&& plane . count > 0
&& ! plane . resources . is_null ()
{
2026-11-25 05:38:51 -06:00
// SAFETY: the plane lists `count` resources.
2026-11-25 14:55:26 -08:00
Some ( unsafe { * plane . resources })
} else {
None
};
let take_copy = | resource : This | {
2026-11-25 05:38:51 -06:00
crate ::resource ::host_copy ( resource ). map ( | copy | {
let state = crate ::resource ::host_transition ( resource , COPY_SOURCE ). map_or ( COPY_SOURCE , | ( _ , before ) | before );
crate ::resource ::host_transition ( resource , state );
2026-11-25 14:55:26 -08:00
// (Lists not translated yet leave it in no state yet: the
// one it is in when its turn in the line comes.)
2026-11-25 11:05:14 -06:00
let state = if crate ::list ::thin ::wanted () { u32 ::MAX } else { state };
2026-11-25 05:38:51 -06:00
( resource as usize , copy , state )
})
};
2026-11-25 14:55:26 -08:00
let texture = base_res . and_then ( take_copy );
let overlay = ( planes > 1 && ! plane_parameters . is_null ())
. then ( || overlay_resource ( plane_parameters ))
. flatten ()
. filter ( |& over_res | base_res != Some ( over_res ))
. and_then ( take_copy );
2026-11-25 05:38:51 -06:00
let resource = texture . as_ref (). map_or ( 0 , | t | t . 0 );
2026-11-25 14:43:52 -08:00
behind ::present (
this ,
texture . map ( | ( _ , copy , state ) | ( copy , state )),
overlay . map ( | ( _ , copy , state ) | ( copy , state )),
Shown { frame , resource },
);
2026-11-24 23:48:39 -06:00
called . end ();
#[cfg(feature = "diagnostics" )]
frame_ends ();
2026-11-25 05:38:51 -06:00
return S_OK ;
}
#[cfg(windows)]
2026-11-23 14:42:21 +01:00
if planes > 0
// SAFETY: the title passes `planes` plane descriptions.
&& let Some ( plane ) = unsafe { plane_parameters . as_ref () }
&& plane . count > 0
&& ! plane . resources . is_null ()
// SAFETY: the plane lists `count` resources.
&& let resource = unsafe { * plane . resources }
&& let Some ( texture ) = crate ::resource ::host_resource ( resource )
&& let Some ( host ) = crate ::host ::host ()
{
const COPY_SOURCE : u32 = 0x800 ;
let state = crate ::resource ::host_transition ( resource , COPY_SOURCE )
. map_or ( COPY_SOURCE , | ( _ , before ) | before );
2026-11-25 14:55:26 -08:00
let overlay = ( planes > 1 )
. then ( || overlay_resource ( plane_parameters ))
. flatten ()
. filter ( |& over_res | over_res != resource )
. and_then ( | over_res | {
2026-11-25 14:43:52 -08:00
crate ::resource ::host_resource ( over_res ). map ( | over_tex | {
let over_state = crate ::resource ::host_transition ( over_res , COPY_SOURCE )
. map_or ( COPY_SOURCE , | ( _ , before ) | before );
( over_res , over_tex , over_state )
})
2026-11-25 14:55:26 -08:00
});
2026-11-25 14:43:52 -08:00
show_planes (
texture ,
state ,
overlay . as_ref (). map ( | ( _ , tex , st ) | ( * tex , * st )),
Shown { frame , resource : resource as usize },
);
2026-11-23 14:42:21 +01:00
crate ::resource ::host_transition ( resource , state );
2026-11-25 14:43:52 -08:00
if let Some (( over_res , _ , over_state )) = overlay {
crate ::resource ::host_transition ( over_res , over_state );
}
2026-11-25 05:38:51 -06:00
let _ = host ;
2026-11-23 14:42:21 +01:00
}
#[cfg(not(windows))]
let _ = ( planes , plane_parameters );
2026-11-24 23:48:39 -06:00
called . end ();
#[cfg(all(windows, feature = "diagnostics" ))]
frame_ends ();
2026-11-23 14:42:21 +01:00
S_OK
}
2026-11-24 23:48:39 -06:00
/// Diagnostics: the title's present is over.
#[cfg(all(windows, feature = "diagnostics" ))]
fn frame_ends () {
crate ::timeline ::presented ();
2026-11-25 11:36:09 -06:00
crate ::faultbook ::frame ();
2026-11-24 23:48:39 -06:00
crate ::ab ::presented ( | switch , was , will | {
if switch == "VICTORY_ASYNC_SUBMIT" && was && ! will {
behind ::leave ();
}
});
}
2026-11-25 05:38:51 -06:00
/// The frame a present shows, and the title's resource it shows.
#[cfg(windows)]
#[derive(Clone, Copy)]
struct Shown {
frame : u64 ,
resource : usize ,
}
2026-11-25 14:43:52 -08:00
/// Shows `texture` (with optional `overlay` plane blended over it) in our window.
2026-11-25 05:38:51 -06:00
#[cfg(windows)]
2026-11-25 14:43:52 -08:00
fn show_planes (
texture : & windows ::Win32 ::Graphics ::Direct3D12 ::ID3D12Resource ,
state : u32 ,
overlay : Option < ( & windows ::Win32 ::Graphics ::Direct3D12 ::ID3D12Resource , u32 ) > ,
what : Shown ,
) {
2026-11-25 05:38:51 -06:00
let Some ( host ) = crate ::host ::host () else {
return ;
};
2026-11-25 14:43:52 -08:00
let shown = host . present_planes ( texture , state , overlay , what . frame );
2026-11-25 05:38:51 -06:00
#[cfg(feature = "diagnostics" )]
blinks ( what . frame , what . resource as This , state , shown );
#[cfg(not(feature = "diagnostics" ))]
let _ = ( shown , what . resource );
}
2026-11-23 23:10:01 +01:00
/// Diagnostics: logs a presented frame that is black after one that was
/// not, with what was presented and how many draws ran since.
#[cfg(all(windows, feature = "diagnostics" ))]
fn blinks ( frame : u64 , resource : This , state : u32 , shown : Option < bool > ) {
static LAST : std ::sync ::Mutex < ( bool , u64 , u64 ) > = std ::sync ::Mutex ::new (( false , 0 , 0 ));
let mut last = LAST . lock (). unwrap_or_else ( | e | e . into_inner ());
let drawn = crate ::draw ::drawn ();
let black = shown . unwrap_or ( false );
let draws = crate ::draw ::take_frame ();
if black && ! last . 0 {
last . 2 += 1 ;
tracing ::warn! (
frame ,
resource = ? resource ,
state = format_args! ( " {state:#x} " ),
draws = drawn - last . 1 ,
copied = shown . is_some (),
black_frames = last . 2 ,
levels = ? crate ::capture ::recent_levels (),
"a black frame after a frame that was not"
);
for d in draws {
tracing ::warn! ( "black frame {frame}: {d}" );
}
AFTER . store ( frame + 6 , Ordering ::Relaxed );
}
// Six frames on: the levels around it (a fade or a single drop).
static AFTER : AtomicU64 = AtomicU64 ::new ( 0 );
if AFTER . load ( Ordering ::Relaxed ) == frame {
tracing ::warn! ( frame , levels = ? crate ::capture ::recent_levels (), "levels after the black frame" );
}
* last = ( black , drawn , last . 2 );
}
/// A run of destination pages: how they are mapped (0 pool pages from
/// `start` on, 1 pool page `start` each, 2 unmapped, 3 left as they are)
/// and how many.
#[repr(C)]
pub ( crate ) struct PageRange {
kind : u32 ,
start : u32 ,
count : u32 ,
}
/// The ranges mapped from a destination address on.
#[repr(C)]
pub ( crate ) struct PageBatch {
destination : u64 ,
count : u32 ,
ranges : * const PageRange ,
}
/// Bytes per mapped page.
const PAGE : u64 = 64 << 10 ;
/// `CopyPageMappingsBatchX(count, batches, pool, pool pages, flags)`: maps
/// pages of a pool (memory the title mapped physical pages into) at the
2026-11-24 15:45:05 -05:00
/// batches' addresses. The title streams textures into pool pages
2026-11-23 23:10:01 +01:00
/// and maps them into its tiled heap this way (title+0xf2c470, ranges of
/// one constant page or of unmapped pages). Done at once: every earlier
/// submission has run.
extern "system" fn copy_page_mappings (
_ : This ,
count : u32 ,
batches : * const PageBatch ,
pool : u64 ,
pool_pages : u32 ,
_flags : u32 ,
) -> HRESULT {
let Some ( map ) = crate ::resource ::page_mapper () else {
return E_INVALIDARG ;
};
if batches . is_null () && count > 0 {
return E_INVALIDARG ;
}
2026-11-24 23:48:39 -06:00
let _called = crate ::timeline ::span ( "the title's CopyPageMappings" );
2026-11-23 23:10:01 +01:00
let batches = ( count > 0 ). then ( || {
// SAFETY: the title passes `count` batches.
unsafe { std ::slice ::from_raw_parts ( batches , count as usize ) }
});
let mut ok = true ;
2026-11-25 04:31:31 -06:00
let many = crate ::resource ::many_page_mapper ();
let mut gathered : Vec < ( u64 , u64 , u64 ) > = Vec ::new ();
let mut map = | to : u64 , from : u64 , len : u64 | match many {
Some ( _ ) => {
gathered . push (( to , from , len ));
true
}
None => map ( to , from , len ),
};
2026-11-23 23:10:01 +01:00
for b in batches . into_iter (). flatten () {
if b . ranges . is_null () {
continue ;
}
// SAFETY: each batch holds `count` ranges.
let ranges = unsafe { std ::slice ::from_raw_parts ( b . ranges , b . count as usize ) };
let mut at = b . destination ;
for r in ranges {
let len = u64 ::from ( r . count ) * PAGE ;
let inside = | pages : u32 | r . start . checked_add ( pages ). is_some_and ( | e | e <= pool_pages );
let source = pool + u64 ::from ( r . start ) * PAGE ;
ok &= match r . kind {
0 if inside ( r . count ) => map ( at , source , len ),
1 if inside ( 1 ) => ( 0 .. u64 ::from ( r . count )). all ( | k | map ( at + k * PAGE , source , PAGE )),
2 => map ( at , 0 , len ),
3 => true ,
_ => false ,
};
at += len ;
}
}
2026-11-25 04:31:31 -06:00
if let Some ( many ) = many {
ok &= many ( & gathered );
}
2026-11-23 23:10:01 +01:00
if ! ok {
static ONCE : std ::sync ::Once = std ::sync ::Once ::new ();
ONCE . call_once ( || tracing ::warn! ( pool = format_args! ( " {pool:#x} " ), "a page mapping failed" ));
}
S_OK
}
2026-11-23 14:42:21 +01:00
extern "system" fn desc ( this : This , out : * mut QueueDesc ) {
if ! out . is_null () {
// SAFETY: checked non-null; the title passes room for a description.
unsafe { out . write ( queue ( this ). desc ) };
}
}
2026-11-23 23:10:01 +01:00
/// The queue methods by their slot in older titles' queues (the 2020-era
/// layout): IUnknown 3, object 4, device child 1, queue 26 (12 as on PC,
/// then console ones).
const METHODS : [( usize , * const ()); 15 ] = [
( 0 , query as * const ()),
( 2 , release as * const ()),
( 7 , object ::parent_device as * const ()), // 0x38
( 10 , execute as * const ()), // 0x50
( 11 , marker as * const ()), // 0x58 set marker
( 12 , marker as * const ()), // 0x60 begin event
( 13 , marker as * const ()), // 0x68 end event
( 14 , signal as * const ()), // 0x70
( 15 , wait as * const ()), // 0x78
( 16 , timestamp_frequency as * const ()), // 0x80
( 26 , desc as * const ()), // 0xd0
( 29 , copy_page_mappings as * const ()), // 0xe8 page mapping batch
( 30 , present as * const ()), // 0xf0
( 33 , execute_x as * const ()), // 0x108
( 32 , signal2_x as * const ()), // 0x100
];
/// Newer titles' queues have two more methods before Signal, which moved
/// from 0x70 to 0x80: every slot from 0x70 on is two later (taken here as
/// starting exactly at 0x70, the methods before it kept in place).
2026-11-24 15:45:05 -05:00
/// Inferred from the title's calls: Signal 0x80 `(fence,
2026-11-23 23:10:01 +01:00
/// value)`, timestamp frequency 0x90 (title+0x24841a6), suspend and resume
/// 0xb8/0xc0, PresentX 0x100, the frame-token submission 0x118 `(count,
/// lists, token, flags)` (title+0x24821dd).
const fn newer_slot ( slot : usize ) -> usize {
if slot >= 14 { slot + 2 } else { slot }
}
const fn queue_table ( newer : bool ) -> [ * const (); 36 ] {
let mut f = object ::d3d12_object ( object ::missing_table ::< QUEUE , 36 > ());
let mut i = 0 ;
while i < METHODS . len () {
let ( slot , method ) = METHODS [ i ];
f [ if newer { newer_slot ( slot ) } else { slot }] = method ;
i += 1 ;
}
f
}
static FUNCTIONS : Table < 36 > = Table ( queue_table ( false ));
static NEWER_FUNCTIONS : Table < 36 > = Table ( queue_table ( true ));
2026-11-23 14:42:21 +01:00
#[cfg(test)]
mod tests {
use super ::* ;
use crate ::object ::slot ;
2026-11-23 23:10:01 +01:00
/// Where a queue's signal is: newer layouts have it two slots later.
/// Another test may switch layouts while this one runs, so the queue's
/// own table decides.
fn signal_slot ( q : This ) -> usize {
// SAFETY: one of our queues; its first field is the table.
let table = unsafe { * q . cast ::<* const * const () > () };
if table == NEWER_FUNCTIONS . 0. as_ptr () {
0x80
} else {
0x70
}
}
2026-11-23 14:42:21 +01:00
#[test]
fn queue_signals_complete_at_once () {
let null = std ::ptr ::null ();
let desc = QueueDesc {
kind : 0 ,
priority : 0 ,
flags : 0 ,
nodes : 0 ,
};
let mut q = null ;
assert_eq! ( create ( null , & desc , & QUEUE_IIDS [ 0 ], & mut q ), S_OK );
// SAFETY: the title reads the type at +0x10.
assert_eq! ( unsafe { q . cast ::< u8 > (). add ( 0x10 ). cast ::< u32 > (). read () }, 0 );
let mut fence = null ;
let fence_iid = & crate ::fence ::FENCE_IIDS [ 0 ];
assert_eq! (
crate ::fence ::create ( null , 0 , 0 , fence_iid , & mut fence ),
S_OK
);
2026-11-23 23:10:01 +01:00
let signal = slot ::< extern "system" fn ( This , This , u64 ) -> HRESULT > ( q , signal_slot ( q ));
2026-11-23 14:42:21 +01:00
assert_eq! ( signal ( q , fence , 7 ), S_OK );
assert_eq! (
slot ::< extern "system" fn ( This ) -> u64 > ( fence , 0x40 )( fence ),
7
);
assert_eq! ( slot ::< extern "system" fn ( This ) -> u32 > ( q , 0x10 )( q ), 0 );
}
2026-11-23 23:10:01 +01:00
2026-11-24 15:45:05 -05:00
/// The title's compute queue: `{COMPUTE, 0, pipe 1, queue 0}`.
2026-11-23 23:10:01 +01:00
#[test]
fn console_queues_are_created_as_on_pc () {
let null = std ::ptr ::null ();
let desc = QueueDescX {
kind : 2 ,
flags : 0 ,
engine_or_pipe : 1 ,
queue : 0 ,
};
let mut q = null ;
assert_eq! ( create_x ( null , & desc , & QUEUE_IIDS [ 0 ], & mut q ), S_OK );
assert_eq! ( queue ( q ). desc . kind , 2 );
let none = std ::ptr ::null ();
assert_eq! ( create_x ( null , none , & QUEUE_IIDS [ 0 ], & mut q ), E_INVALIDARG );
}
#[test]
fn newer_queues_have_their_methods_two_slots_later () {
let ( old , new ) = ( queue_table ( false ), queue_table ( true ));
assert_eq! ( old [ 10 ], new [ 10 ]); // submission stays at 0x50
assert_eq! ( old [ 14 ], new [ 16 ]); // signal 0x70 → 0x80
assert_eq! ( old [ 30 ], new [ 32 ]); // present 0xf0 → 0x100
assert_eq! ( new [ 35 ], execute_x as * const ()); // 0x118
}
2026-11-25 03:35:03 -06:00
#[test]
fn a_pair_of_timestamps_shows_the_load_it_is_given () {
let ( start , end ) = ( 0xd0419_6698 , 0xd0419_6690 );
assert_eq! ( as_loaded ( 5_000_000 , end , 4_000_000 ) - as_loaded ( 5_000_000 , start , 4_000_000 ), 4_000_000 );
assert_eq! ( as_loaded ( 5_000_000 , start , 0 ), 5_000_000 );
assert_eq! ( as_loaded ( 5 , start , 100 ), 1 );
}
2026-11-23 23:10:01 +01:00
#[test]
fn timestamps_and_values_are_written () {
let mut at = [ 0 u64 ; 2 ];
Written ::Value ( 0x1_0000_0002 ). write ( at . as_mut_ptr (). cast (), 4 );
assert_eq! ( at [ 0 ], 2 );
Written ::GpuTimestamp . write ( at [ 1 .. ]. as_mut_ptr (). cast (), 8 );
let first = at [ 1 ];
std ::thread ::sleep ( std ::time ::Duration ::from_millis ( 2 ));
Written ::GpuTimestamp . write ( at [ 1 .. ]. as_mut_ptr (). cast (), 8 );
assert! ( at [ 1 ] > first );
}
2026-11-23 14:42:21 +01:00
}