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

201 lines
6.1 KiB
Rust
Raw Normal View History

//! Descriptor heaps: zeroed memory, with handles that are plain addresses
//! (CPU and GPU alike, as on the console).
//!
//! Descriptor sizes: views 32 bytes and samplers 16, the size of RDNA 2's
//! image and sampler descriptors, so a title that copies descriptors itself
//! moves whole ones. Render target and depth views are ours alone; 128 bytes
//! leaves room for whatever stride a title assumes.
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use crate::object::{self, DESCRIPTOR_HEAP, E_INVALIDARG, HRESULT, Iid, Object, Table, This, guid};
use crate::resource::{Memory, PAGEABLE_IIDS};
const HEAP_IIDS: [Iid; 4] = [
guid(
0x8efb471d,
0x616c,
0x4f49,
[0x90, 0xf7, 0x12, 0x7b, 0xb7, 0x63, 0xfa, 0x51],
),
PAGEABLE_IIDS[0],
PAGEABLE_IIDS[1],
PAGEABLE_IIDS[2],
];
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub(crate) struct HeapDesc {
kind: u32,
count: u32,
flags: u32,
nodes: u32,
}
#[repr(C)]
struct Heap {
object: Object,
desc: HeapDesc,
memory: Memory,
}
fn heap<'a>(this: This) -> &'a Heap {
// SAFETY: called from descriptor heap methods only.
unsafe { &*this.cast::<Heap>() }
}
/// Bytes per descriptor, by heap type (views, samplers, render targets,
/// depth).
fn increment(kind: u32) -> Option<u32> {
[32, 16, 128, 128].get(kind as usize).copied()
}
/// Device method.
pub(crate) extern "system" fn increment_size(_: This, kind: u32) -> u32 {
increment(kind).unwrap_or(0)
}
/// What our render target views hold: the resource and the view
/// description, turned into a host view when a command uses them.
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct RtvRecord {
tag: u32,
described: u32,
pub(crate) resource: This,
desc: [u32; 6],
}
const RTV_TAG: u32 = u32::from_le_bytes(*b"vRTV");
const _: () = assert!(size_of::<RtvRecord>() <= 112);
impl RtvRecord {
/// The view description in the host's type (the same layout).
#[cfg(windows)]
pub(crate) fn desc(
&self,
) -> Option<windows::Win32::Graphics::Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC> {
// SAFETY: 24 bytes in the host's layout.
(self.described != 0).then(|| unsafe {
(&raw const self.desc)
.cast::<windows::Win32::Graphics::Direct3D12::D3D12_RENDER_TARGET_VIEW_DESC>()
.read_unaligned()
})
}
}
/// Device method: records the view at `handle` (descriptor memory).
pub(crate) extern "system" fn create_rtv(
_: This,
resource: This,
desc: *const [u32; 6],
handle: u64,
) {
if handle == 0 {
return;
}
// SAFETY: the title passes a description or null.
let described = unsafe { desc.as_ref() };
let record = RtvRecord {
tag: RTV_TAG,
described: u32::from(described.is_some()),
resource,
desc: described.copied().unwrap_or_default(),
};
// SAFETY: the handle is a descriptor slot, at least 112 bytes.
unsafe { (handle as *mut RtvRecord).write_unaligned(record) };
}
/// The view recorded at `handle`, if one is.
pub(crate) fn rtv_record(handle: u64) -> Option<RtvRecord> {
if handle == 0 {
return None;
}
// SAFETY: the handle is a descriptor slot the title passes back.
let record = unsafe { (handle as *const RtvRecord).read_unaligned() };
(record.tag == RTV_TAG).then_some(record)
}
/// Device method.
pub(crate) extern "system" fn create_heap(
_: This,
desc: *const HeapDesc,
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;
};
let Some(size) = increment(desc.kind).map(|i| u64::from(i) * u64::from(desc.count.max(1)))
else {
return E_INVALIDARG;
};
tracing::debug!(?desc, "descriptor heap");
let memory = match Memory::zeroed(size, 64) {
Ok(memory) => memory,
Err(hr) => return hr,
};
let heap = Box::new(Heap {
object: Object::new(&FUNCTIONS),
desc,
memory,
});
object::hand_out(DESCRIPTOR_HEAP, heap, iid, &HEAP_IIDS, out)
}
extern "system" fn query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
let found = object::is(iid, &HEAP_IIDS).then_some(this);
object::answer(DESCRIPTOR_HEAP, iid, found, out)
}
extern "system" fn release(this: This) -> u32 {
object::release_boxed::<Heap>(this)
}
extern "system" fn desc(this: This, out: *mut HeapDesc) {
if !out.is_null() {
// SAFETY: checked non-null; the title passes room for a description.
unsafe { out.write(heap(this).desc) };
}
}
/// The same address for the CPU and the GPU start.
extern "system" fn start(this: This) -> u64 {
heap(this).memory.address()
}
/// IUnknown 3, object 4, device child 1, heap 3.
static FUNCTIONS: Table<11> = {
let mut f = object::d3d12_object(object::missing_table::<DESCRIPTOR_HEAP, 11>());
f[0] = query as *const ();
f[2] = release as *const ();
f[7] = object::parent_device as *const (); // 0x38
f[8] = desc as *const (); // 0x40
f[9] = start as *const (); // 0x48 CPU
f[10] = start as *const (); // 0x50 GPU
Table(f)
};
#[cfg(test)]
mod tests {
use super::*;
use crate::object::{S_OK, slot};
#[test]
fn heaps_hold_count_descriptors_from_their_start() {
let null = std::ptr::null();
let desc = HeapDesc {
kind: 0,
count: 1000,
flags: 1,
nodes: 0,
};
let mut h = null;
assert_eq!(create_heap(null, &desc, &HEAP_IIDS[0], &mut h), S_OK);
let cpu = slot::<extern "system" fn(This) -> u64>(h, 0x48)(h);
assert_eq!(cpu % 64, 0);
assert_eq!(slot::<extern "system" fn(This) -> u64>(h, 0x50)(h), cpu);
assert_eq!(increment_size(null, 0), 32);
// SAFETY: the heap holds 1000 descriptors of 32 bytes.
unsafe { (cpu as *mut u8).add(999 * 32 + 31).write(1) };
assert_eq!(increment_size(null, 9), 0);
let bad = HeapDesc { kind: 9, ..desc };
assert_eq!(create_heap(null, &bad, &HEAP_IIDS[0], &mut h), E_INVALIDARG);
}
}