Files
6gbvram---16gb-ram/crates/victory-gpu/src/queue.rs
T

220 lines
6.8 KiB
Rust
Raw Normal View History

//! Command queues. Nothing is executed yet: submitted lists are dropped and
//! fence signals complete at once, as if the GPU were infinitely fast.
#![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;
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,
}
const _: () = assert!(std::mem::offset_of!(Queue, kind) == 0x10);
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");
let queue = Box::new(Queue {
object: Object::new(&FUNCTIONS),
kind: desc.kind,
desc,
});
object::hand_out(QUEUE, queue, iid, &QUEUE_IIDS, out)
}
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 {
object::release_boxed::<Queue>(this)
}
/// Runs the lists' host lists and waits for them (decision 0007).
extern "system" fn execute(_: This, count: u32, lists: *const This) {
if lists.is_null() {
return;
}
#[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) };
let host_lists: Vec<_> = lists
.iter()
.filter_map(|&l| crate::list::host_list(l))
.collect();
host.execute(&host_lists);
return;
}
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,
}
/// Markers and events only matter to profilers.
extern "system" fn marker(_: This) {}
extern "system" fn signal(_: This, fence: This, value: u64) -> HRESULT {
if fence.is_null() {
return E_INVALIDARG;
}
crate::fence::signal(fence, value)
}
/// Nothing runs on the queue, so waiting for a fence never blocks it.
extern "system" fn wait(_: This, fence: This, _value: u64) -> HRESULT {
if fence.is_null() { E_INVALIDARG } else { S_OK }
}
/// Ticks per second of GPU timestamps; none are written yet.
extern "system" fn timestamp_frequency(_: This, out: *mut u64) -> HRESULT {
if out.is_null() {
return E_INVALIDARG;
}
// SAFETY: checked non-null.
unsafe { out.write(100_000_000) };
S_OK
}
/// 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(
_: This,
planes: u32,
plane_parameters: *const Plane,
_parameters: *const u8,
) -> HRESULT {
static FRAMES: AtomicU64 = AtomicU64::new(0);
let frame = FRAMES.fetch_add(1, Ordering::Relaxed) + 1;
if frame.is_power_of_two() {
tracing::info!(frame, "frame presented");
}
#[cfg(windows)]
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);
host.present(texture, state);
crate::resource::host_transition(resource, state);
}
#[cfg(not(windows))]
let _ = (planes, plane_parameters);
S_OK
}
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) };
}
}
/// IUnknown 3, object 4, device child 1, queue 26 (12 as on PC, then
/// console ones).
static FUNCTIONS: Table<34> = {
let mut f = object::d3d12_object(object::missing_table::<QUEUE, 34>());
f[0] = query as *const ();
f[2] = release as *const ();
f[7] = object::parent_device as *const (); // 0x38
f[10] = execute as *const (); // 0x50
f[11] = marker as *const (); // 0x58 set marker
f[12] = marker as *const (); // 0x60 begin event
f[13] = marker as *const (); // 0x68 end event
f[14] = signal as *const (); // 0x70
f[15] = wait as *const (); // 0x78
f[16] = timestamp_frequency as *const (); // 0x80
f[26] = desc as *const (); // 0xd0
f[30] = present as *const (); // 0xf0
Table(f)
};
#[cfg(test)]
mod tests {
use super::*;
use crate::object::slot;
#[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
);
let signal = slot::<extern "system" fn(This, This, u64) -> HRESULT>(q, 0x70);
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);
}
}