device.rs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. //! The device titles get from device creation, laid out as their inlined
  2. //! code expects: the object header, then device fields. Its table starts
  3. //! like the PC device interface (IUnknown, the object methods, then the
  4. //! device methods in PC order); console additions follow. Methods we have
  5. //! not written log their table offset and fail, so a run shows what to
  6. //! write next.
  7. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  8. use std::ffi::c_void;
  9. use crate::object::{
  10. self, E_INVALIDARG, E_NOINTERFACE, E_NOTIMPL, HRESULT, Iid, Object, S_FALSE, S_OK, Table, This,
  11. guid, guid_text,
  12. };
  13. use crate::{
  14. descriptor, dxgi, fence, frame, list, pipeline, query, queue, resource, root, signature,
  15. };
  16. /// What D3D returns when none of the requested feature levels is supported.
  17. const UNSUPPORTED: HRESULT = 0x887A_0004_u32 as i32;
  18. /// The device interface id (the same as on PC). Titles also import it as
  19. /// data.
  20. pub static IID: Iid = guid(
  21. 0x189819f1,
  22. 0x1db6,
  23. 0x4b57,
  24. [0xbe, 0x54, 0x18, 0x21, 0x33, 0x9b, 0x85, 0xf7],
  25. );
  26. /// The levels the device answers to, as on PC: `ID3D12Device1` to
  27. /// `ID3D12Device8`, and `ID3D12Device11`, which the title creates
  28. /// its device as. Each level's table is the one before plus its methods.
  29. const LEVEL_IIDS: [Iid; 9] = [
  30. guid(
  31. 0x77acce80,
  32. 0x638e,
  33. 0x4e65,
  34. [0x88, 0x95, 0xc1, 0xf2, 0x33, 0x86, 0x86, 0x3e],
  35. ),
  36. guid(
  37. 0x30baa41e,
  38. 0xb15b,
  39. 0x475c,
  40. [0xa0, 0xbb, 0x1a, 0xf5, 0xc5, 0xb6, 0x43, 0x28],
  41. ),
  42. guid(
  43. 0x81dadc15,
  44. 0x2bad,
  45. 0x4392,
  46. [0x93, 0xc5, 0x10, 0x13, 0x45, 0xc4, 0xaa, 0x98],
  47. ),
  48. guid(
  49. 0xe865df17,
  50. 0xa9ee,
  51. 0x46f9,
  52. [0xa4, 0x63, 0x30, 0x98, 0x31, 0x5a, 0xa2, 0xe5],
  53. ),
  54. guid(
  55. 0x8b4f173b,
  56. 0x2fea,
  57. 0x4b80,
  58. [0x8f, 0x58, 0x43, 0x07, 0x19, 0x1a, 0xb9, 0x5d],
  59. ),
  60. guid(
  61. 0xc70b221b,
  62. 0x40e4,
  63. 0x4a17,
  64. [0x89, 0xaf, 0x02, 0x5a, 0x07, 0x27, 0xa6, 0xdc],
  65. ),
  66. guid(
  67. 0x5c014b53,
  68. 0x68a1,
  69. 0x4b9b,
  70. [0x8b, 0xd1, 0xdd, 0x60, 0x46, 0xb9, 0x35, 0x8b],
  71. ),
  72. guid(
  73. 0x728f1fa2,
  74. 0xea11,
  75. 0x4a9f,
  76. [0x9b, 0x52, 0x9b, 0x42, 0xa4, 0x84, 0xd5, 0xc9],
  77. ),
  78. guid(
  79. 0x5405c344,
  80. 0xd457,
  81. 0x444e,
  82. [0xb4, 0xdd, 0x23, 0x66, 0xe4, 0x5a, 0xee, 0x39],
  83. ),
  84. ];
  85. /// Whether `iid` names the device: its first interface or a newer level.
  86. pub(crate) fn is_device(iid: *const Iid) -> bool {
  87. object::is(iid, &[IID]) || object::is(iid, &LEVEL_IIDS)
  88. }
  89. /// The interface version these layouts were checked against (2.4). Both
  90. /// Minecraft (March 2022) and the title (April 2026) pass it.
  91. pub const CHECKED_VERSION: u32 = 0x0002_0004;
  92. /// Feature level 12_2, what the console hardware supports.
  93. const HIGHEST_FEATURE_LEVEL: u32 = 0xc200;
  94. #[repr(C)]
  95. pub struct Device {
  96. object: Object,
  97. /// GPU address of a null constant buffer; none yet.
  98. null_constant_buffer: u64,
  99. /// Byte 0 non-zero: calls the title would otherwise inline come to the
  100. /// table. The rest is reserved.
  101. out_of_line: [u32; 16],
  102. }
  103. const _: () = assert!(size_of::<Device>() == 0x58);
  104. /// One device per process, as on the console.
  105. pub(crate) static DEVICE: Device = Device {
  106. object: Object::new(&FUNCTIONS),
  107. null_constant_buffer: 0,
  108. out_of_line: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  109. };
  110. pub(crate) fn device() -> This {
  111. (&raw const DEVICE).cast()
  112. }
  113. /// Device creation: `(adapter, parameters, iid, out)`. The parameters start
  114. /// with the interface version; the rest (ring and scratch sizes, features
  115. /// to leave out) does not matter to us yet.
  116. pub extern "system" fn create(
  117. _adapter: *const c_void,
  118. parameters: *const u32,
  119. iid: *const Iid,
  120. out: *mut This,
  121. ) -> HRESULT {
  122. if parameters.is_null() || iid.is_null() {
  123. return E_INVALIDARG;
  124. }
  125. // SAFETY: checked non-null; the title passes its parameters and an id.
  126. let (version, iid) = unsafe { (parameters.read(), &*iid) };
  127. tracing::info!(version = format_args!("{version:#x}"), "D3D12.X device");
  128. if version != CHECKED_VERSION {
  129. tracing::warn!(
  130. version = format_args!("{version:#x}"),
  131. "device layouts were checked against {CHECKED_VERSION:#x} only"
  132. );
  133. }
  134. if !is_device(iid) {
  135. tracing::warn!(iid = %guid_text(iid), "device created as an unknown interface");
  136. return E_NOINTERFACE;
  137. }
  138. if out.is_null() {
  139. return S_FALSE;
  140. }
  141. if *iid == LEVEL_IIDS[8] {
  142. NEWER.store(true, std::sync::atomic::Ordering::Relaxed);
  143. }
  144. object::answer(object::DEVICE, iid, Some(device()), out)
  145. }
  146. /// Set when the title creates the device as `ID3D12Device11`. Its lists and
  147. /// queues then have the newer layouts (two more methods each; see
  148. /// `list` and `queue`), although the interface version stays 2.4.
  149. /// shortcut: the creation interface is the only sign of the newer layouts
  150. /// we have (the title, April 2026, versus Minecraft, March 2022);
  151. /// look for a better one if a title breaks the rule.
  152. static NEWER: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
  153. pub(crate) fn newer() -> bool {
  154. NEWER.load(std::sync::atomic::Ordering::Relaxed)
  155. }
  156. /// The device, or its DXGI side.
  157. extern "system" fn query_interface(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  158. let found = if is_device(iid) {
  159. Some(this)
  160. } else if object::is(iid, &dxgi::DEVICE_IIDS) {
  161. Some(dxgi::device())
  162. } else {
  163. None
  164. };
  165. object::answer(object::DEVICE, iid, found, out)
  166. }
  167. /// What `D3D12ConfigureVirtualMemoryX` takes and fills in.
  168. #[repr(C)]
  169. pub struct VirtualMemory {
  170. flags: u32,
  171. /// GPU page table size in 4 MiB pages.
  172. page_table_pages: u32,
  173. /// Memory kept across title processes for the frame buffer.
  174. preserved_bytes: u32,
  175. /// Out: the range graphics addresses fall in.
  176. lowest: u64,
  177. highest: u64,
  178. }
  179. const _: () = assert!(size_of::<VirtualMemory>() == 0x20);
  180. /// `d3d12_xs!D3D12ConfigureVirtualMemoryX`, called before any graphics
  181. /// allocation. The inputs size console structures we do not have; the
  182. /// range out is all of user space, since GPU addresses are CPU addresses.
  183. pub extern "system" fn configure_virtual_memory(config: *mut VirtualMemory) -> HRESULT {
  184. // SAFETY: the title passes its configuration (null is tolerated).
  185. let Some(config) = (unsafe { config.as_mut() }) else {
  186. return E_INVALIDARG;
  187. };
  188. tracing::info!(
  189. flags = config.flags,
  190. page_table_pages = config.page_table_pages,
  191. preserved_bytes = config.preserved_bytes,
  192. "graphics virtual memory"
  193. );
  194. (config.lowest, config.highest) = user_space();
  195. S_OK
  196. }
  197. fn user_space() -> (u64, u64) {
  198. #[cfg(windows)]
  199. {
  200. use windows_sys::Win32::System::SystemInformation::{GetSystemInfo, SYSTEM_INFO};
  201. // SAFETY: plain data, filled in by the call.
  202. let mut info: SYSTEM_INFO = unsafe { std::mem::zeroed() };
  203. // SAFETY: a valid out pointer.
  204. unsafe { GetSystemInfo(&mut info) };
  205. (
  206. info.lpMinimumApplicationAddress as u64,
  207. info.lpMaximumApplicationAddress as u64,
  208. )
  209. }
  210. #[cfg(not(windows))]
  211. (0x1_0000, 0x7fff_fffe_ffff)
  212. }
  213. extern "system" fn node_count(_: This) -> u32 {
  214. 1
  215. }
  216. /// Feature query 2: the highest of the requested feature levels we support.
  217. #[repr(C)]
  218. struct FeatureLevels {
  219. count: u32,
  220. requested: *const u32,
  221. highest: u32,
  222. }
  223. /// Feature query 0, what an RDNA 2 GPU reports. The console's layout has
  224. /// one field more than PC's (index 11).
  225. const OPTIONS: [u32; 16] = [
  226. 1, // double-precision shader operations
  227. 1, // output-merger logic operations
  228. 2, // minimum precision: 16-bit
  229. 3, // tiled resources tier
  230. 3, // resource binding tier
  231. 1, // stencil reference from pixel shaders
  232. 1, // typed UAV loads of more formats
  233. 1, // rasterizer-ordered views
  234. 3, // conservative rasterization tier
  235. 40, // GPU address bits per resource
  236. 0, // 64 KiB standard swizzle
  237. 0, // console-only field
  238. 0, // cross-node sharing tier
  239. 0, // cross-adapter row-major textures
  240. 1, // viewport and render target index from any stage
  241. 2, // resource heap tier
  242. ];
  243. extern "system" fn check_feature_support(
  244. _: This,
  245. feature: u32,
  246. data: *mut c_void,
  247. size: u32,
  248. ) -> HRESULT {
  249. const OPTIONS_QUERY: u32 = 0;
  250. const ARCHITECTURE: u32 = 1;
  251. const FEATURE_LEVELS: u32 = 2;
  252. const FORMAT_SUPPORT: u32 = 3;
  253. const MULTISAMPLE: u32 = 4;
  254. let expected = match feature {
  255. OPTIONS_QUERY => size_of_val(&OPTIONS),
  256. ARCHITECTURE => 16,
  257. FEATURE_LEVELS => size_of::<FeatureLevels>(),
  258. FORMAT_SUPPORT => 12,
  259. MULTISAMPLE => 16,
  260. _ => {
  261. tracing::warn!(feature, size, "device feature query not implemented");
  262. return E_NOTIMPL;
  263. }
  264. };
  265. if data.is_null() || size as usize != expected {
  266. return E_INVALIDARG;
  267. }
  268. match feature {
  269. OPTIONS_QUERY => {
  270. // SAFETY: checked size and non-null.
  271. unsafe { data.cast::<[u32; 16]>().write_unaligned(OPTIONS) };
  272. S_OK
  273. }
  274. ARCHITECTURE => {
  275. // SAFETY: checked size and non-null.
  276. let a = unsafe { &mut *data.cast::<[u32; 4]>() };
  277. if a[0] != 0 {
  278. return E_INVALIDARG; // one node only
  279. }
  280. // Not tile-based; unified, cache-coherent memory.
  281. a[1..].copy_from_slice(&[0, 1, 1]);
  282. S_OK
  283. }
  284. FORMAT_SUPPORT => {
  285. // SAFETY: checked size and non-null.
  286. let q = unsafe { &mut *data.cast::<[u32; 3]>() };
  287. // shortcut: every use of every format; answer from the host
  288. // device once there is one.
  289. q[1] = 0x7fff_ffff;
  290. q[2] = 0xffff;
  291. S_OK
  292. }
  293. MULTISAMPLE => {
  294. // SAFETY: checked size and non-null.
  295. let q = unsafe { &mut *data.cast::<[u32; 4]>() };
  296. // Format, sample count, flags in; one quality level out for the
  297. // counts the hardware has.
  298. q[3] = u32::from(matches!(q[1], 1 | 2 | 4 | 8));
  299. S_OK
  300. }
  301. _ => feature_levels(data),
  302. }
  303. }
  304. fn feature_levels(data: *mut c_void) -> HRESULT {
  305. // SAFETY: the caller checked size and non-null.
  306. let query = unsafe { &mut *data.cast::<FeatureLevels>() };
  307. if query.requested.is_null() {
  308. return E_INVALIDARG;
  309. }
  310. // SAFETY: the title passes `count` levels.
  311. let requested = unsafe { std::slice::from_raw_parts(query.requested, query.count as usize) };
  312. match requested
  313. .iter()
  314. .copied()
  315. .filter(|&l| l <= HIGHEST_FEATURE_LEVEL)
  316. .max()
  317. {
  318. Some(level) => {
  319. query.highest = level;
  320. S_OK
  321. }
  322. None => UNSUPPORTED,
  323. }
  324. }
  325. /// Device methods 0x1c8 and 0x218 (`SLOT`). The title calls both
  326. /// (title+0x2477578 and title+0x24800d5) as `(this, description, interface
  327. /// id, out)`, asks for `ID3D12PipelineState`, and on success keeps the
  328. /// object `out` names and draws with it; on failure it goes without that
  329. /// pipeline, silently. What the descriptions hold is not known: the calls
  330. /// fail as they always did, with `out` null, and the first ones log the
  331. /// description's words and what its first word points at, so that the two
  332. /// methods can be written.
  333. /// shortcut: not implemented. Answering `S_OK` here without making the
  334. /// pipeline would hand the title whatever its stack held as an object.
  335. extern "system" fn unknown_pipeline<const SLOT: usize>(
  336. _: This,
  337. desc: *const u8,
  338. _iid: *const Iid,
  339. out: *mut This,
  340. ) -> HRESULT {
  341. use std::sync::atomic::{AtomicU32, Ordering};
  342. static CALLS: [AtomicU32; 2] = [const { AtomicU32::new(0) }; 2];
  343. let calls = CALLS[usize::from(SLOT == 0x218)].fetch_add(1, Ordering::Relaxed) + 1;
  344. if !out.is_null() {
  345. // SAFETY: the title's out pointer (a local of its own).
  346. unsafe { *out = std::ptr::null() };
  347. }
  348. if calls <= 4 || calls.is_power_of_two() {
  349. // Sixteen 64-bit words at `at`, read through the kernel: neither
  350. // pointer is known to be mapped.
  351. #[cfg(windows)]
  352. let words = |at: usize| -> Option<Vec<u64>> {
  353. let mut bytes = [0u8; 128];
  354. (at != 0 && crate::host::read_into(at as *const u8, &mut bytes)).then(|| {
  355. bytes
  356. .as_chunks::<8>().0.iter()
  357. .map(|c| c.iter().rev().fold(0u64, |v, &b| v << 8 | u64::from(b)))
  358. .collect()
  359. })
  360. };
  361. #[cfg(not(windows))]
  362. let words = |_: usize| None::<Vec<u64>>;
  363. let description = words(desc as usize);
  364. let first = description
  365. .as_ref()
  366. .and_then(|w| words(w[0] as usize));
  367. tracing::warn!(
  368. slot = format_args!("{:#x}", SLOT),
  369. calls,
  370. description = format_args!("{description:x?}"),
  371. first_points_at = format_args!("{first:x?}"),
  372. "a pipeline creation method that is not implemented: the title goes without this pipeline"
  373. );
  374. }
  375. E_NOTIMPL
  376. }
  377. /// The host device's state: the console's device is removed when the host's is
  378. /// (VIctory 13185f5: the default answer, E_NOTIMPL, reads as a lost device).
  379. extern "system" fn removed_reason(_: This) -> HRESULT {
  380. #[cfg(windows)]
  381. if let Some(host) = crate::host::host() {
  382. // SAFETY: a plain query.
  383. if let Err(e) = unsafe { host.device.GetDeviceRemovedReason() } {
  384. return e.code().0;
  385. }
  386. }
  387. S_OK
  388. }
  389. /// IUnknown 3, object methods 4, device methods 76 (0x18-0x290), then the
  390. /// methods `ID3D12Device1` to `ID3D12Device8` add, in order: 3, 1, 3, 6, 9,
  391. /// 1, 2, 9 (0x298-0x3a0, the console's own among them), then those of
  392. /// `ID3D12Device9` to `ID3D12Device11`, whose console layout is unknown.
  393. /// Every slot exists so that a call to one we have not written logs it.
  394. static FUNCTIONS: Table<160> = {
  395. let mut f = object::d3d12_object(object::missing_table::<{ object::DEVICE }, 160>());
  396. f[0] = query_interface as *const (); // 0x00
  397. f[7] = node_count as *const (); // 0x38
  398. f[8] = queue::create as *const (); // 0x40
  399. f[9] = list::create_allocator as *const (); // 0x48
  400. f[12] = list::create_list as *const (); // 0x60
  401. f[10] = pipeline::create_graphics as *const (); // 0x50
  402. f[11] = pipeline::create_compute as *const (); // 0x58
  403. f[13] = check_feature_support as *const (); // 0x68
  404. f[14] = descriptor::create_heap as *const (); // 0x70
  405. f[15] = descriptor::increment_size as *const (); // 0x78
  406. f[16] = root::create as *const (); // 0x80
  407. f[17] = descriptor::create_cbv as *const (); // 0x88
  408. f[18] = descriptor::create_srv as *const (); // 0x90
  409. f[19] = descriptor::create_uav as *const (); // 0x98
  410. f[20] = descriptor::create_rtv as *const (); // 0xa0
  411. f[21] = descriptor::create_dsv as *const (); // 0xa8
  412. f[22] = descriptor::create_sampler as *const (); // 0xb0
  413. f[23] = descriptor::copy as *const (); // 0xb8
  414. f[24] = descriptor::copy_simple as *const (); // 0xc0
  415. f[25] = resource::allocation_info as *const (); // 0xc8
  416. f[26] = resource::custom_heap_properties as *const (); // 0xd0
  417. f[27] = resource::create_committed as *const (); // 0xd8
  418. f[28] = resource::create_heap as *const (); // 0xe0
  419. f[29] = resource::create_placed as *const (); // 0xe8
  420. f[36] = fence::create as *const (); // 0x120
  421. f[37] = removed_reason as *const (); // 0x128
  422. f[38] = resource::copyable_footprints as *const (); // 0x130
  423. f[39] = query::create as *const (); // 0x138
  424. f[41] = signature::create as *const (); // 0x148
  425. f[48] = queue::create_x as *const (); // 0x180
  426. f[57] = unknown_pipeline::<0x1c8> as *const (); // 0x1c8
  427. f[61] = descriptor::create_sampler_x as *const (); // 0x1e8
  428. f[62] = pipeline::create_compute_x as *const (); // 0x1f0
  429. f[67] = unknown_pipeline::<0x218> as *const (); // 0x218
  430. f[69] = object::ignore as *const (); // 0x228 debug error filter
  431. f[70] = descriptor::create_placed_srv as *const (); // 0x230
  432. f[71] = descriptor::create_placed_uav as *const (); // 0x238
  433. f[73] = list::create_list_x as *const (); // 0x248
  434. f[74] = resource::register_page_pool as *const (); // 0x250
  435. f[75] = resource::unregister_page_pool as *const (); // 0x258
  436. // CreateCommittedResourceX: its description is PC's plus DCC options,
  437. // which we have no use for (the AMD upscaler's, on Series S).
  438. f[76] = resource::create_committed as *const (); // 0x260
  439. f[78] = frame::set_interval as *const (); // 0x270
  440. f[79] = frame::schedule as *const (); // 0x278
  441. f[80] = frame::wait as *const (); // 0x280
  442. f[81] = frame::statistics as *const (); // 0x288
  443. f[86] = pipeline::create_stream as *const (); // 0x2b0
  444. f[108] = resource::allocation_info2 as *const (); // 0x360
  445. f[127] = resource::create_committed3 as *const (); // 0x3f8
  446. f[133] = resource::create_at_address as *const (); // 0x428
  447. Table(f)
  448. };
  449. #[cfg(test)]
  450. mod tests {
  451. use super::*;
  452. use crate::object::slot;
  453. fn create_as(iid: &Iid) -> (HRESULT, This) {
  454. let version = CHECKED_VERSION;
  455. let mut device = std::ptr::null();
  456. let hr = create(std::ptr::null(), &version, iid, &mut device);
  457. (hr, device)
  458. }
  459. #[test]
  460. fn creation_hands_out_the_device() {
  461. let (hr, device) = create_as(&IID);
  462. assert_eq!(hr, S_OK);
  463. assert_eq!(device, super::device());
  464. // The title tests byte 0x18 before inlining a device call.
  465. // SAFETY: the device is 0x58 bytes.
  466. assert_ne!(unsafe { device.cast::<u8>().add(0x18).read() }, 0);
  467. assert_eq!(create_as(&[0; 16]).0, E_NOINTERFACE);
  468. assert_eq!(
  469. slot::<extern "system" fn(This) -> u32>(device, 0x38)(device),
  470. 1
  471. );
  472. let missing = slot::<extern "system" fn() -> HRESULT>(device, 0xf0);
  473. assert_eq!(missing(), E_NOTIMPL);
  474. }
  475. #[test]
  476. fn newer_device_levels_are_the_same_device() {
  477. // ID3D12Device11, as the title asks for it.
  478. let (hr, device) = create_as(&LEVEL_IIDS[8]);
  479. assert_eq!((hr, device), (S_OK, super::device()));
  480. let mut same = std::ptr::null();
  481. assert_eq!(query_interface(device, &LEVEL_IIDS[4], &mut same), S_OK);
  482. assert_eq!(same, device);
  483. // A Device8 method we have not written fails without crashing.
  484. let missing = slot::<extern "system" fn() -> HRESULT>(device, 0x3a0);
  485. assert_eq!(missing(), E_NOTIMPL);
  486. }
  487. /// A device child, and the DXGI device, hand out the device as any
  488. /// interface the device itself answers to.
  489. #[test]
  490. fn the_device_is_found_at_every_level_through_others() {
  491. type Get = extern "system" fn(This, *const Iid, *mut This) -> HRESULT;
  492. let fence_iid = &crate::fence::FENCE_IIDS[0];
  493. let mut fence = std::ptr::null();
  494. assert_eq!(
  495. crate::fence::create(std::ptr::null(), 0, 0, fence_iid, &mut fence),
  496. S_OK
  497. );
  498. let get_device = slot::<Get>(fence, 0x38);
  499. let dxgi = dxgi::device();
  500. let query = slot::<Get>(dxgi, 0);
  501. for iid in std::iter::once(&IID).chain(&LEVEL_IIDS) {
  502. let mut found = std::ptr::null();
  503. assert_eq!(get_device(fence, iid, &mut found), S_OK, "{iid:02x?}");
  504. assert_eq!(found, device());
  505. let mut found = std::ptr::null();
  506. assert_eq!(query(dxgi, iid, &mut found), S_OK, "{iid:02x?}");
  507. assert_eq!(found, device());
  508. }
  509. // What the device is not, it is not through them either.
  510. let mut found = device();
  511. assert_eq!(get_device(fence, fence_iid, &mut found), E_NOINTERFACE);
  512. assert!(found.is_null());
  513. slot::<extern "system" fn(This) -> u32>(fence, 0x10)(fence);
  514. }
  515. /// The host's: a fence's `GetDevice` for `ID3D12Device5`.
  516. #[cfg(windows)]
  517. #[test]
  518. fn the_hosts_children_hand_out_newer_device_levels() {
  519. use windows::Win32::Graphics::Direct3D12::{
  520. D3D12_FENCE_FLAG_NONE, ID3D12Device5, ID3D12Fence,
  521. };
  522. let Some(host) = crate::host::host() else {
  523. return; // no host GPU here
  524. };
  525. // SAFETY: plain calls on the host device and its fence.
  526. unsafe {
  527. let fence: ID3D12Fence = host.device.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
  528. let mut device: Option<ID3D12Device5> = None;
  529. assert!(fence.GetDevice(&mut device).is_ok());
  530. assert!(device.is_some());
  531. }
  532. }
  533. #[test]
  534. fn virtual_memory_configuration_reports_user_space() {
  535. let mut config = VirtualMemory {
  536. flags: 0,
  537. page_table_pages: 7,
  538. preserved_bytes: 0,
  539. lowest: 0,
  540. highest: 0,
  541. };
  542. assert_eq!(configure_virtual_memory(&mut config), S_OK);
  543. assert!(0 < config.lowest && config.lowest < config.highest);
  544. let heap = Box::new(0u8);
  545. assert!((config.lowest..=config.highest).contains(&(&raw const *heap as u64)));
  546. assert_eq!(configure_virtual_memory(std::ptr::null_mut()), E_INVALIDARG);
  547. }
  548. #[test]
  549. fn feature_levels_pick_the_highest_supported() {
  550. type Check = extern "system" fn(This, u32, *mut c_void, u32) -> HRESULT;
  551. let device = device();
  552. let check = slot::<Check>(device, 0x68);
  553. let query = |levels: &[u32], size| {
  554. let mut query = FeatureLevels {
  555. count: levels.len() as u32,
  556. requested: levels.as_ptr(),
  557. highest: 0,
  558. };
  559. let hr = check(device, 2, (&raw mut query).cast(), size);
  560. (hr, query.highest)
  561. };
  562. let levels = [0xb000, 0xb100, 0xc000, 0xc100, 0xd000];
  563. assert_eq!(query(&levels, 0x18), (S_OK, 0xc100));
  564. assert_eq!(query(&levels, 0x10).0, E_INVALIDARG);
  565. assert_eq!(query(&levels[4..], 0x18).0, UNSUPPORTED);
  566. }
  567. }