dxgi.rs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /// The DXGI device also answers for the D3D12 device.
  88. extern "system" fn device_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  89. if crate::device::is_device(iid) {
  90. return object::answer(DXGI_DEVICE, iid, Some(crate::device::device()), out);
  91. }
  92. query(DXGI_DEVICE, iid, &DEVICE_IIDS, this, out)
  93. }
  94. extern "system" fn device_parent(_: This, iid: *const Iid, out: *mut This) -> HRESULT {
  95. query(DXGI_DEVICE, iid, &ADAPTER_IIDS, &ADAPTER_OBJECT, out)
  96. }
  97. extern "system" fn device_adapter(_: This, out: *mut This) -> HRESULT {
  98. object::answer(DXGI_DEVICE, &ADAPTER_IIDS[0], Some(&ADAPTER_OBJECT), out)
  99. }
  100. extern "system" fn adapter_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  101. query(ADAPTER, iid, &ADAPTER_IIDS, this, out)
  102. }
  103. extern "system" fn adapter_parent(_: This, iid: *const Iid, out: *mut This) -> HRESULT {
  104. query(ADAPTER, iid, &FACTORY_IIDS, &FACTORY_OBJECT, out)
  105. }
  106. extern "system" fn adapter_outputs(_: This, index: u32, out: *mut This) -> HRESULT {
  107. if index != 0 {
  108. return NOT_FOUND;
  109. }
  110. object::answer(ADAPTER, &OUTPUT_IIDS[0], Some(&OUTPUT_OBJECT), out)
  111. }
  112. #[repr(C)]
  113. struct AdapterDescription {
  114. name: [u16; 128],
  115. vendor: u32,
  116. device: u32,
  117. subsystem: u32,
  118. revision: u32,
  119. dedicated_video_memory: usize,
  120. dedicated_system_memory: usize,
  121. shared_system_memory: usize,
  122. luid: [u32; 2],
  123. }
  124. const _: () = assert!(size_of::<AdapterDescription>() == 0x130);
  125. const AMD: u32 = 0x1002;
  126. extern "system" fn adapter_description(_: This, out: *mut AdapterDescription) -> HRESULT {
  127. if out.is_null() {
  128. return E_INVALIDARG;
  129. }
  130. let mut name = [0; 128];
  131. for (slot, unit) in name.iter_mut().zip("VIctory console GPU".encode_utf16()) {
  132. *slot = unit;
  133. }
  134. // shortcut: memory sizes are the title's split (GPU-optimal, standard),
  135. // not measured; revisit once memory is emulated.
  136. let (gpu_optimal, standard) = crate::console::current().title_memory();
  137. let description = AdapterDescription {
  138. name,
  139. vendor: AMD,
  140. device: 0,
  141. subsystem: 0,
  142. revision: 0,
  143. dedicated_video_memory: gpu_optimal,
  144. dedicated_system_memory: 0,
  145. shared_system_memory: standard,
  146. luid: [1, 0],
  147. };
  148. // SAFETY: checked non-null; the title passes room for the description.
  149. unsafe { out.write(description) };
  150. S_OK
  151. }
  152. extern "system" fn factory_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  153. query(FACTORY, iid, &FACTORY_IIDS, this, out)
  154. }
  155. extern "system" fn factory_adapters(_: This, index: u32, out: *mut This) -> HRESULT {
  156. if index != 0 {
  157. return NOT_FOUND;
  158. }
  159. object::answer(FACTORY, &ADAPTER_IIDS[0], Some(&ADAPTER_OBJECT), out)
  160. }
  161. extern "system" fn output_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  162. query(OUTPUT, iid, &OUTPUT_IIDS, this, out)
  163. }
  164. extern "system" fn output_parent(_: This, iid: *const Iid, out: *mut This) -> HRESULT {
  165. query(OUTPUT, iid, &ADAPTER_IIDS, &ADAPTER_OBJECT, out)
  166. }
  167. /// IUnknown 3, object 4, device 5 + 2 + 3.
  168. static DEVICE_FUNCTIONS: Table<32> = {
  169. let mut f = object::missing_table::<DXGI_DEVICE, 32>();
  170. f[0] = device_query as *const ();
  171. f[6] = device_parent as *const (); // 0x30
  172. f[7] = device_adapter as *const (); // 0x38
  173. Table(f)
  174. };
  175. /// IUnknown 3, object 4, adapter 3 + 1 + 1.
  176. static ADAPTER_FUNCTIONS: Table<24> = {
  177. let mut f = object::missing_table::<ADAPTER, 24>();
  178. f[0] = adapter_query as *const ();
  179. f[6] = adapter_parent as *const (); // 0x30
  180. f[7] = adapter_outputs as *const (); // 0x38
  181. f[8] = adapter_description as *const (); // 0x40
  182. Table(f)
  183. };
  184. /// IUnknown 3, object 4, factory 5 + 2 + 11.
  185. static FACTORY_FUNCTIONS: Table<32> = {
  186. let mut f = object::missing_table::<FACTORY, 32>();
  187. f[0] = factory_query as *const ();
  188. f[7] = factory_adapters as *const (); // 0x38
  189. f[12] = factory_adapters as *const (); // 0x60, the same for version 1
  190. Table(f)
  191. };
  192. /// A display mode as the console lists it: size, refresh rate (numerator,
  193. /// denominator), format, scanline order, scaling, flags.
  194. #[repr(C)]
  195. struct Mode {
  196. width: u32,
  197. height: u32,
  198. refresh: [u32; 2],
  199. format: u32,
  200. scanlines: u32,
  201. scaling: u32,
  202. flags: u32,
  203. }
  204. const _: () = assert!(size_of::<Mode>() == 32);
  205. /// The console's mode list: one 4K mode at 60 Hz (Grand Theft Auto VI looks
  206. /// for a 120 Hz one to allow 120 Hz frame intervals).
  207. /// shortcut: fixed mode, not the host display's; report the host's rates if
  208. /// a title paces by them.
  209. extern "system" fn modes(
  210. _: This,
  211. _format: u32,
  212. _flags: u32,
  213. count: *mut u32,
  214. out: *mut Mode,
  215. ) -> HRESULT {
  216. // SAFETY: the title passes its count (null is tolerated).
  217. let Some(count) = (unsafe { count.as_mut() }) else {
  218. return E_INVALIDARG;
  219. };
  220. if out.is_null() {
  221. *count = 1;
  222. return S_OK;
  223. }
  224. if *count < 1 {
  225. return MORE_DATA;
  226. }
  227. *count = 1;
  228. let mode = Mode {
  229. width: 3840,
  230. height: 2160,
  231. refresh: [60, 1],
  232. format: 0,
  233. scanlines: 1, // progressive
  234. scaling: 0,
  235. flags: 0,
  236. };
  237. // SAFETY: checked non-null; room for `count` >= 1 modes.
  238. unsafe { out.write(mode) };
  239. S_OK
  240. }
  241. /// IUnknown 3, object 4, output 15 (12 as on PC, then 3 console ones).
  242. static OUTPUT_FUNCTIONS: Table<32> = {
  243. let mut f = object::missing_table::<OUTPUT, 32>();
  244. f[0] = output_query as *const ();
  245. f[6] = output_parent as *const (); // 0x30
  246. f[21] = modes as *const (); // 0xa8
  247. Table(f)
  248. };
  249. #[cfg(test)]
  250. mod tests {
  251. use super::*;
  252. use crate::object::{E_NOINTERFACE, slot};
  253. type Query = extern "system" fn(This, *const Iid, *mut This) -> HRESULT;
  254. type Get = extern "system" fn(This, *mut This) -> HRESULT;
  255. type Indexed = extern "system" fn(This, u32, *mut This) -> HRESULT;
  256. type Describe = extern "system" fn(This, *mut AdapterDescription) -> HRESULT;
  257. /// Minecraft's startup: device → DXGI device (newest first) → adapter →
  258. /// description, factory, first output.
  259. #[test]
  260. fn startup_walks_from_device_to_output() {
  261. let d3d = crate::device::device();
  262. let newest_pc = guid(
  263. 0x6007896c,
  264. 0x3244,
  265. 0x4afd,
  266. [0xbf, 0x18, 0xa6, 0xd3, 0xbe, 0xda, 0x50, 0x23],
  267. );
  268. let mut dxgi = std::ptr::null();
  269. let qi = slot::<Query>(d3d, 0);
  270. assert_eq!(qi(d3d, &newest_pc, &mut dxgi), E_NOINTERFACE);
  271. assert!(dxgi.is_null());
  272. assert_eq!(qi(d3d, &DEVICE_IIDS[2], &mut dxgi), S_OK);
  273. assert_eq!(dxgi, device());
  274. let mut adapter = std::ptr::null();
  275. assert_eq!(slot::<Get>(dxgi, 0x38)(dxgi, &mut adapter), S_OK);
  276. let mut description = std::mem::MaybeUninit::<AdapterDescription>::uninit();
  277. assert_eq!(
  278. slot::<Describe>(adapter, 0x40)(adapter, description.as_mut_ptr()),
  279. S_OK
  280. );
  281. // SAFETY: written above.
  282. assert_eq!(unsafe { description.assume_init() }.vendor, AMD);
  283. let mut factory = std::ptr::null();
  284. let parent = slot::<Query>(adapter, 0x30);
  285. assert_eq!(parent(adapter, &FACTORY_IIDS[2], &mut factory), S_OK);
  286. let mut again = std::ptr::null();
  287. assert_eq!(slot::<Indexed>(factory, 0x38)(factory, 0, &mut again), S_OK);
  288. assert_eq!(again, adapter);
  289. let mut output = std::ptr::null();
  290. let outputs = slot::<Indexed>(adapter, 0x38);
  291. assert_eq!(outputs(adapter, 0, &mut output), S_OK);
  292. assert!(!output.is_null());
  293. assert_eq!(outputs(adapter, 1, &mut output), NOT_FOUND);
  294. }
  295. }