query.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //! Query heaps. Nothing runs on a GPU yet, so no query ever produces a
  2. //! result; the heap only remembers its description.
  3. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  4. use crate::object::{self, E_INVALIDARG, HRESULT, Iid, Object, QUERY_HEAP, Table, This, guid};
  5. use crate::resource::PAGEABLE_IIDS;
  6. const HEAP_IIDS: [Iid; 4] = [
  7. guid(
  8. 0x0d9658ae,
  9. 0xed45,
  10. 0x469e,
  11. [0xa6, 0x1d, 0x97, 0x0e, 0xc5, 0x83, 0xca, 0xb4],
  12. ),
  13. PAGEABLE_IIDS[0],
  14. PAGEABLE_IIDS[1],
  15. PAGEABLE_IIDS[2],
  16. ];
  17. #[repr(C)]
  18. #[derive(Debug, Clone, Copy)]
  19. pub(crate) struct Desc {
  20. kind: u32,
  21. count: u32,
  22. nodes: u32,
  23. }
  24. #[repr(C)]
  25. struct Heap {
  26. object: Object,
  27. desc: Desc,
  28. }
  29. /// Device method.
  30. pub(crate) extern "system" fn create(
  31. _: This,
  32. desc: *const Desc,
  33. iid: *const Iid,
  34. out: *mut This,
  35. ) -> HRESULT {
  36. // SAFETY: the title passes a description (null is tolerated).
  37. let Some(&desc) = (unsafe { desc.as_ref() }) else {
  38. return E_INVALIDARG;
  39. };
  40. tracing::debug!(?desc, "query heap");
  41. let heap = Box::new(Heap {
  42. object: Object::new(&FUNCTIONS),
  43. desc,
  44. });
  45. object::hand_out(QUERY_HEAP, heap, iid, &HEAP_IIDS, out)
  46. }
  47. extern "system" fn query(this: This, iid: *const Iid, out: *mut This) -> HRESULT {
  48. object::answer(
  49. QUERY_HEAP,
  50. iid,
  51. object::is(iid, &HEAP_IIDS).then_some(this),
  52. out,
  53. )
  54. }
  55. extern "system" fn release(this: This) -> u32 {
  56. object::release_boxed::<Heap>(this)
  57. }
  58. /// IUnknown 3, object 4, device child 1.
  59. static FUNCTIONS: Table<24> = {
  60. let mut f = object::d3d12_object(object::missing_table::<QUERY_HEAP, 24>());
  61. f[0] = query as *const ();
  62. f[2] = release as *const ();
  63. f[7] = object::parent_device as *const (); // 0x38
  64. Table(f)
  65. };
  66. #[cfg(test)]
  67. mod tests {
  68. use super::*;
  69. use crate::object::{E_NOINTERFACE, E_POINTER, S_FALSE, S_OK, slot};
  70. #[test]
  71. fn null_descriptions_are_rejected() {
  72. let mut heap = std::ptr::null();
  73. assert_eq!(
  74. create(std::ptr::null(), std::ptr::null(), &HEAP_IIDS[0], &mut heap),
  75. E_INVALIDARG
  76. );
  77. assert!(heap.is_null());
  78. // The description is checked first: a probe with no room for the
  79. // heap fails on it rather than answering S_FALSE.
  80. assert_eq!(
  81. create(
  82. std::ptr::null(),
  83. std::ptr::null(),
  84. &HEAP_IIDS[0],
  85. std::ptr::null_mut()
  86. ),
  87. E_INVALIDARG
  88. );
  89. }
  90. #[test]
  91. fn creation_without_an_out_pointer_only_probes() {
  92. let desc = Desc {
  93. kind: 1, // timestamps
  94. count: 64,
  95. nodes: 0,
  96. };
  97. // A known interface with no out pointer asks whether creation would
  98. // succeed; the heap is dropped instead of handed out.
  99. assert_eq!(
  100. create(std::ptr::null(), &desc, &HEAP_IIDS[0], std::ptr::null_mut()),
  101. S_FALSE
  102. );
  103. // A null interface id is tolerated: no interface, no heap.
  104. let mut heap = std::ptr::null();
  105. assert_eq!(
  106. create(std::ptr::null(), &desc, std::ptr::null(), &mut heap),
  107. E_NOINTERFACE
  108. );
  109. assert!(heap.is_null());
  110. }
  111. #[test]
  112. fn heaps_answer_their_interfaces_and_count_references() {
  113. let desc = Desc {
  114. kind: 0, // occlusion
  115. count: 8,
  116. nodes: 0,
  117. };
  118. let null = std::ptr::null();
  119. let mut heap = null;
  120. assert_eq!(create(null, &desc, &HEAP_IIDS[0], &mut heap), S_OK);
  121. assert!(!heap.is_null());
  122. let query = slot::<extern "system" fn(This, *const Iid, *mut This) -> HRESULT>(heap, 0x0);
  123. // Every interface the heap answers to hands out the same object.
  124. for iid in &HEAP_IIDS {
  125. let mut out = null;
  126. assert_eq!(query(heap, iid, &mut out), S_OK);
  127. assert_eq!(out as usize, heap as usize);
  128. }
  129. // An unknown interface writes null behind and reports it missing.
  130. let unknown = [0u8; 16];
  131. let mut out = heap;
  132. assert_eq!(query(heap, &unknown, &mut out), E_NOINTERFACE);
  133. assert!(out.is_null());
  134. // A null interface id is tolerated rather than matched.
  135. let mut out = heap;
  136. assert_eq!(query(heap, std::ptr::null(), &mut out), E_NOINTERFACE);
  137. assert!(out.is_null());
  138. // No room for the answer is a pointer error.
  139. assert_eq!(query(heap, &unknown, std::ptr::null_mut()), E_POINTER);
  140. // References: one from creation, four from the queries, one added;
  141. // each release hands back what is left, the last frees the heap.
  142. assert_eq!(slot::<extern "system" fn(This) -> u32>(heap, 0x8)(heap), 6);
  143. let release = slot::<extern "system" fn(This) -> u32>(heap, 0x10);
  144. for left in [5, 4, 3, 2, 1, 0] {
  145. assert_eq!(release(heap), left);
  146. }
  147. }
  148. }