dxgi.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //! The DXGI side of the device: one adapter with one output, reached from
  2. //! the device by interface query. Tables follow the PC interfaces' order
  3. //! (IUnknown, the object methods, then each interface's methods).
  4. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  5. use crate::object::{
  6. self, ADAPTER, DXGI_DEVICE, E_INVALIDARG, FACTORY, HRESULT, Iid, MORE_DATA, NOT_FOUND, OUTPUT,
  7. Object, S_OK, Table, This, guid,
  8. };
  9. /// The DXGI device interfaces, base first (as on PC).
  10. pub(crate) const DEVICE_IIDS: [Iid; 3] = [
  11. guid(
  12. 0x54ec77fa,
  13. 0x1377,
  14. 0x44e6,
  15. [0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c],
  16. ),
  17. guid(
  18. 0x77db970f,
  19. 0x6276,
  20. 0x48ba,
  21. [0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c],
  22. ),
  23. guid(
  24. 0x05008617,
  25. 0xfbfd,
  26. 0x4051,
  27. [0xa7, 0x90, 0x14, 0x48, 0x84, 0xb4, 0xf6, 0xa9],
  28. ),
  29. ];
  30. const ADAPTER_IIDS: [Iid; 3] = [
  31. guid(
  32. 0x2411e7e1,
  33. 0x12ac,
  34. 0x4ccf,
  35. [0xbd, 0x14, 0x97, 0x98, 0xe8, 0x53, 0x4d, 0xc0],
  36. ),
  37. guid(
  38. 0x29038f61,
  39. 0x3839,
  40. 0x4626,
  41. [0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a, 0x05],
  42. ),
  43. guid(
  44. 0x0aa1ae0a,
  45. 0xfa0e,
  46. 0x4b84,
  47. [0x86, 0x44, 0xe0, 0x5f, 0xf8, 0xe5, 0xac, 0xb5],
  48. ),
  49. ];
  50. const FACTORY_IIDS: [Iid; 3] = [
  51. guid(
  52. 0x7b7166ec,
  53. 0x21c7,
  54. 0x44ae,
  55. [0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3, 0x69],
  56. ),
  57. guid(
  58. 0x770aae78,
  59. 0xf26f,
  60. 0x4dba,
  61. [0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87],
  62. ),
  63. guid(
  64. 0x50c83a1c,
  65. 0xe072,
  66. 0x4c48,
  67. [0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0],
  68. ),
  69. ];
  70. const OUTPUT_IIDS: [Iid; 1] = [guid(
  71. 0xae02eedb,
  72. 0xc735,
  73. 0x4690,
  74. [0x8d, 0x52, 0x5a, 0x8d, 0xc2, 0x02, 0x13, 0xaa],
  75. )];
  76. static DEVICE: Object = Object::new(&DEVICE_FUNCTIONS);
  77. static ADAPTER_OBJECT: Object = Object::new(&ADAPTER_FUNCTIONS);
  78. static FACTORY_OBJECT: Object = Object::new(&FACTORY_FUNCTIONS);
  79. static OUTPUT_OBJECT: Object = Object::new(&OUTPUT_FUNCTIONS);
  80. pub(crate) fn device() -> This {
  81. &DEVICE
  82. }
  83. /// `found` if `iid` is one of `known`.
  84. fn query(kind: usize, iid: *const Iid, known: &[Iid], found: This, out: *mut This) -> HRESULT {
  85. object::answer(kind, iid, object::is(iid, known).then_some(found), out)
  86. }
  87. fn enumeration_not_found(out: *mut This) -> HRESULT {
  88. if !out.is_null() {
  89. // SAFETY: the caller supplies a writable output slot when non-null.
  90. unsafe { out.write(std::ptr::null()) };
  91. }
  92. NOT_FOUND
  93. }
  94. /// The DXGI device also answers for the D3D12 device.
  95. extern "system" fn device_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  96. if crate::device::is_device(iid) {
  97. return object::answer(DXGI_DEVICE, iid, Some(crate::device::device()), out);
  98. }
  99. query(DXGI_DEVICE, iid, &DEVICE_IIDS, this, out)
  100. }
  101. extern "system" fn device_parent(_: This, iid: *const Iid, out: *mut This) -> HRESULT {
  102. query(DXGI_DEVICE, iid, &ADAPTER_IIDS, &ADAPTER_OBJECT, out)
  103. }
  104. extern "system" fn device_adapter(_: This, out: *mut This) -> HRESULT {
  105. object::answer(DXGI_DEVICE, &ADAPTER_IIDS[0], Some(&ADAPTER_OBJECT), out)
  106. }
  107. extern "system" fn adapter_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  108. query(ADAPTER, iid, &ADAPTER_IIDS, this, out)
  109. }
  110. extern "system" fn adapter_parent(_: This, iid: *const Iid, out: *mut This) -> HRESULT {
  111. query(ADAPTER, iid, &FACTORY_IIDS, &FACTORY_OBJECT, out)
  112. }
  113. extern "system" fn adapter_outputs(_: This, index: u32, out: *mut This) -> HRESULT {
  114. if index != 0 {
  115. return enumeration_not_found(out);
  116. }
  117. object::answer(ADAPTER, &OUTPUT_IIDS[0], Some(&OUTPUT_OBJECT), out)
  118. }
  119. #[repr(C)]
  120. struct AdapterDescription {
  121. name: [u16; 128],
  122. vendor: u32,
  123. device: u32,
  124. subsystem: u32,
  125. revision: u32,
  126. dedicated_video_memory: usize,
  127. dedicated_system_memory: usize,
  128. shared_system_memory: usize,
  129. luid: [u32; 2],
  130. }
  131. const _: () = assert!(size_of::<AdapterDescription>() == 0x130);
  132. const AMD: u32 = 0x1002;
  133. extern "system" fn adapter_description(_: This, out: *mut AdapterDescription) -> HRESULT {
  134. if out.is_null() {
  135. return E_INVALIDARG;
  136. }
  137. let mut name = [0; 128];
  138. for (slot, unit) in name.iter_mut().zip("VIctory console GPU".encode_utf16()) {
  139. *slot = unit;
  140. }
  141. // shortcut: memory sizes are the title's split (GPU-optimal, standard),
  142. // not measured; revisit once memory is emulated.
  143. let (gpu_optimal, standard) = crate::console::current().title_memory();
  144. let description = AdapterDescription {
  145. name,
  146. vendor: AMD,
  147. device: 0,
  148. subsystem: 0,
  149. revision: 0,
  150. dedicated_video_memory: gpu_optimal,
  151. dedicated_system_memory: 0,
  152. shared_system_memory: standard,
  153. luid: [1, 0],
  154. };
  155. // SAFETY: checked non-null; the title passes room for the description.
  156. unsafe { out.write(description) };
  157. S_OK
  158. }
  159. extern "system" fn factory_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  160. query(FACTORY, iid, &FACTORY_IIDS, this, out)
  161. }
  162. extern "system" fn factory_adapters(_: This, index: u32, out: *mut This) -> HRESULT {
  163. if index != 0 {
  164. return enumeration_not_found(out);
  165. }
  166. object::answer(FACTORY, &ADAPTER_IIDS[0], Some(&ADAPTER_OBJECT), out)
  167. }
  168. extern "system" fn output_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  169. query(OUTPUT, iid, &OUTPUT_IIDS, this, out)
  170. }
  171. extern "system" fn output_parent(_: This, iid: *const Iid, out: *mut This) -> HRESULT {
  172. query(OUTPUT, iid, &ADAPTER_IIDS, &ADAPTER_OBJECT, out)
  173. }
  174. /// IUnknown 3, object 4, device 5 + 2 + 3.
  175. static DEVICE_FUNCTIONS: Table<32> = {
  176. let mut f = object::missing_table::<DXGI_DEVICE, 32>();
  177. f[0] = device_query as *const ();
  178. f[6] = device_parent as *const (); // 0x30
  179. f[7] = device_adapter as *const (); // 0x38
  180. Table(f)
  181. };
  182. /// IUnknown 3, object 4, adapter 3 + 1 + 1.
  183. static ADAPTER_FUNCTIONS: Table<24> = {
  184. let mut f = object::missing_table::<ADAPTER, 24>();
  185. f[0] = adapter_query as *const ();
  186. f[6] = adapter_parent as *const (); // 0x30
  187. f[7] = adapter_outputs as *const (); // 0x38
  188. f[8] = adapter_description as *const (); // 0x40
  189. Table(f)
  190. };
  191. /// IUnknown 3, object 4, factory 5 + 2 + 11.
  192. static FACTORY_FUNCTIONS: Table<32> = {
  193. let mut f = object::missing_table::<FACTORY, 32>();
  194. f[0] = factory_query as *const ();
  195. f[7] = factory_adapters as *const (); // 0x38
  196. f[12] = factory_adapters as *const (); // 0x60, the same for version 1
  197. Table(f)
  198. };
  199. /// A display mode as the console lists it: size, refresh rate (numerator,
  200. /// denominator), format, scanline order, scaling, flags.
  201. #[repr(C)]
  202. struct Mode {
  203. width: u32,
  204. height: u32,
  205. refresh: [u32; 2],
  206. format: u32,
  207. scanlines: u32,
  208. scaling: u32,
  209. flags: u32,
  210. }
  211. const _: () = assert!(size_of::<Mode>() == 32);
  212. /// The size of the display the console reports: `VICTORY_DISPLAY_MODE=WxH`
  213. /// if set (as a console reports the television it is plugged into: a
  214. /// Series X on a 1080p set lists a 1080p mode), else 3840x2160.
  215. /// shortcut: not yet the host display's own size by default; make it so
  216. /// once a title is seen to render for the mode it is given.
  217. fn display_size() -> (u32, u32) {
  218. static SIZE: std::sync::OnceLock<(u32, u32)> = std::sync::OnceLock::new();
  219. *SIZE.get_or_init(|| {
  220. let asked = std::env::var("VICTORY_DISPLAY_MODE").ok().and_then(|v| {
  221. let (w, h) = v.trim().split_once(['x', 'X'])?;
  222. Some((w.trim().parse::<u32>().ok()?, h.trim().parse::<u32>().ok()?))
  223. });
  224. let size = match asked {
  225. Some((w, h)) if (640..=7680).contains(&w) && (360..=4320).contains(&h) => (w, h),
  226. _ => (3840, 2160),
  227. };
  228. tracing::info!(width = size.0, height = size.1, "display mode reported to the title");
  229. size
  230. })
  231. }
  232. /// The console's mode list: one 4K mode at 60 Hz (the title looks
  233. /// for a 120 Hz one to allow 120 Hz frame intervals).
  234. /// shortcut: fixed mode, not the host display's; report the host's rates if
  235. /// a title paces by them.
  236. extern "system" fn modes(
  237. _: This,
  238. _format: u32,
  239. _flags: u32,
  240. count: *mut u32,
  241. out: *mut Mode,
  242. ) -> HRESULT {
  243. // SAFETY: the title passes its count (null is tolerated).
  244. let Some(count) = (unsafe { count.as_mut() }) else {
  245. return E_INVALIDARG;
  246. };
  247. if out.is_null() {
  248. *count = 1;
  249. return S_OK;
  250. }
  251. if *count < 1 {
  252. return MORE_DATA;
  253. }
  254. *count = 1;
  255. let (width, height) = display_size();
  256. let mode = Mode {
  257. width,
  258. height,
  259. refresh: [60, 1],
  260. format: 0,
  261. scanlines: 1, // progressive
  262. scaling: 0,
  263. flags: 0,
  264. };
  265. // SAFETY: checked non-null; room for `count` >= 1 modes.
  266. unsafe { out.write(mode) };
  267. S_OK
  268. }
  269. /// IUnknown 3, object 4, output 15 (12 as on PC, then 3 console ones).
  270. static OUTPUT_FUNCTIONS: Table<32> = {
  271. let mut f = object::missing_table::<OUTPUT, 32>();
  272. f[0] = output_query as *const ();
  273. f[6] = output_parent as *const (); // 0x30
  274. f[21] = modes as *const (); // 0xa8
  275. Table(f)
  276. };
  277. #[cfg(test)]
  278. mod tests {
  279. use super::*;
  280. use crate::object::{E_NOINTERFACE, slot};
  281. type Query = extern "system" fn(This, *const Iid, *mut This) -> HRESULT;
  282. type Get = extern "system" fn(This, *mut This) -> HRESULT;
  283. type Indexed = extern "system" fn(This, u32, *mut This) -> HRESULT;
  284. type Describe = extern "system" fn(This, *mut AdapterDescription) -> HRESULT;
  285. /// Minecraft's startup: device → DXGI device (newest first) → adapter →
  286. /// description, factory, first output.
  287. #[test]
  288. fn startup_walks_from_device_to_output() {
  289. let d3d = crate::device::device();
  290. let newest_pc = guid(
  291. 0x6007896c,
  292. 0x3244,
  293. 0x4afd,
  294. [0xbf, 0x18, 0xa6, 0xd3, 0xbe, 0xda, 0x50, 0x23],
  295. );
  296. let mut dxgi = std::ptr::null();
  297. let qi = slot::<Query>(d3d, 0);
  298. assert_eq!(qi(d3d, &newest_pc, &mut dxgi), E_NOINTERFACE);
  299. assert!(dxgi.is_null());
  300. assert_eq!(qi(d3d, &DEVICE_IIDS[2], &mut dxgi), S_OK);
  301. assert_eq!(dxgi, device());
  302. let mut adapter = std::ptr::null();
  303. assert_eq!(slot::<Get>(dxgi, 0x38)(dxgi, &mut adapter), S_OK);
  304. let mut description = std::mem::MaybeUninit::<AdapterDescription>::uninit();
  305. assert_eq!(
  306. slot::<Describe>(adapter, 0x40)(adapter, description.as_mut_ptr()),
  307. S_OK
  308. );
  309. // SAFETY: written above.
  310. assert_eq!(unsafe { description.assume_init() }.vendor, AMD);
  311. let mut factory = std::ptr::null();
  312. let parent = slot::<Query>(adapter, 0x30);
  313. assert_eq!(parent(adapter, &FACTORY_IIDS[2], &mut factory), S_OK);
  314. let mut again = std::ptr::null();
  315. assert_eq!(slot::<Indexed>(factory, 0x38)(factory, 0, &mut again), S_OK);
  316. assert_eq!(again, adapter);
  317. let mut output = std::ptr::null();
  318. let outputs = slot::<Indexed>(adapter, 0x38);
  319. assert_eq!(outputs(adapter, 0, &mut output), S_OK);
  320. assert!(!output.is_null());
  321. assert_eq!(outputs(adapter, 1, &mut output), NOT_FOUND);
  322. }
  323. #[test]
  324. fn out_of_range_enumeration_clears_output() {
  325. for (object, offset, method) in [
  326. (&FACTORY_OBJECT, 0x38, "EnumAdapters"),
  327. (&FACTORY_OBJECT, 0x60, "EnumAdapters1"),
  328. (&ADAPTER_OBJECT, 0x38, "EnumOutputs"),
  329. ] {
  330. let enumerate = slot::<Indexed>(object, offset);
  331. let mut out = object as This;
  332. assert_eq!(enumerate(object, 1, &mut out), NOT_FOUND, "{method}");
  333. assert!(out.is_null(), "{method} left a stale output pointer");
  334. }
  335. }
  336. }