Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. use std::rc::Rc;
  2. use std::ops::Deref;
  3. use ash::{vk, Entry, Instance as VkInstance, Device as VkDevice, InstanceError};
  4. use ash::prelude::VkResult;
  5.  
  6. #[derive(Clone)]
  7. pub struct CommandPool {
  8. pub rc: Rc<RcCommandPool>,
  9. }
  10.  
  11. impl CommandPool {
  12. pub fn new(device: &Device, create_info: &vk::CommandPoolCreateInfo) -> VkResult<Self> {
  13. Ok(Self {
  14. rc: Rc::new(RcCommandPool {
  15. obj: unsafe { device.create_command_pool(&create_info, None) }?,
  16. device: device.clone(),
  17. }),
  18. })
  19. }
  20. }
  21.  
  22. impl Deref for CommandPool {
  23. type Target = RcCommandPool;
  24.  
  25. fn deref(&self) -> &Self::Target {
  26. &self.rc
  27. }
  28. }
  29.  
  30. #[derive(Clone)]
  31. pub struct RcCommandPool {
  32. pub obj: vk::CommandPool,
  33. pub device: Device,
  34. }
  35.  
  36. impl Deref for RcCommandPool {
  37. type Target = vk::CommandPool;
  38.  
  39. fn deref(&self) -> &Self::Target {
  40. &self.obj
  41. }
  42. }
  43.  
  44. impl Drop for RcCommandPool {
  45. fn drop(&mut self) {
  46. unsafe {
  47. self.device.destroy_command_pool(**self, None);
  48. };
  49. }
  50. }
  51.  
  52. #[derive(Clone)]
  53. pub struct DescriptorSetLayout {
  54. pub rc: Rc<RcDescriptorSetLayout>,
  55. }
  56.  
  57. impl DescriptorSetLayout {
  58. pub fn new(device: &Device, create_info: &vk::DescriptorSetLayoutCreateInfo) -> VkResult<Self> {
  59. Ok(Self {
  60. rc: Rc::new(RcDescriptorSetLayout {
  61. obj: unsafe { device.create_descriptor_set_layout(&create_info, None) }?,
  62. device: device.clone(),
  63. }),
  64. })
  65. }
  66. }
  67.  
  68. impl Deref for DescriptorSetLayout {
  69. type Target = RcDescriptorSetLayout;
  70.  
  71. fn deref(&self) -> &Self::Target {
  72. &self.rc
  73. }
  74. }
  75.  
  76. #[derive(Clone)]
  77. pub struct RcDescriptorSetLayout {
  78. pub obj: vk::DescriptorSetLayout,
  79. pub device: Device,
  80. }
  81.  
  82. impl Deref for RcDescriptorSetLayout {
  83. type Target = vk::DescriptorSetLayout;
  84.  
  85. fn deref(&self) -> &Self::Target {
  86. &self.obj
  87. }
  88. }
  89.  
  90. impl Drop for RcDescriptorSetLayout {
  91. fn drop(&mut self) {
  92. unsafe {
  93. self.device.destroy_descriptor_set_layout(**self, None);
  94. };
  95. }
  96. }
  97.  
  98. /* Extra defines */
  99. #[derive(Clone)]
  100. pub struct Instance {
  101. pub rc: Rc<RcInstance>,
  102. pub entry: Entry,
  103. }
  104.  
  105. impl Instance {
  106. pub fn new_simple(create_info: &vk::InstanceCreateInfo) -> Result<Self, InstanceError> {
  107. let entry = Entry::new().unwrap();
  108.  
  109. Ok((entry, Self::new(&entry, create_info)?))
  110. }
  111.  
  112. pub fn new(entry: &Entry, create_info: &vk::InstanceCreateInfo) -> Result<Self, InstanceError> {
  113. Ok(Self {
  114. rc: Rc::new(RcInstance {
  115. obj: unsafe { entry.create_instance(create_info, None) }?,
  116. }),
  117. entry,
  118. })
  119. }
  120. }
  121.  
  122. impl Deref for Instance {
  123. type Target = RcInstance;
  124.  
  125. fn deref(&self) -> &Self::Target {
  126. &self.rc
  127. }
  128. }
  129.  
  130. #[derive(Clone)]
  131. pub struct RcInstance {
  132. pub obj: VkInstance,
  133. }
  134.  
  135. impl Deref for RcInstance {
  136. type Target = VkInstance;
  137.  
  138. fn deref(&self) -> &Self::Target {
  139. &self.obj
  140. }
  141. }
  142.  
  143. impl Drop for RcInstance {
  144. fn drop(&mut self) {
  145. unsafe { self.destroy_instance(None) };
  146. }
  147. }
  148.  
  149. #[derive(Clone)]
  150. pub struct Device {
  151. pub rc: Rc<RcDevice>,
  152. }
  153.  
  154. impl Device {
  155. pub fn new(
  156. instance: &Instance,
  157. physical_device: vk::PhysicalDevice,
  158. create_info: &vk::DeviceCreateInfo,
  159. ) -> Result<Self, vk::Result> {
  160. Ok(Self {
  161. rc: Rc::new(RcDevice {
  162. obj: unsafe { instance.create_device(physical_device, create_info, None) }?,
  163. instance: instance.clone(),
  164. }),
  165. })
  166. }
  167. }
  168.  
  169. impl Deref for Device {
  170. type Target = RcDevice;
  171.  
  172. fn deref(&self) -> &Self::Target {
  173. &self.rc
  174. }
  175. }
  176.  
  177. #[derive(Clone)]
  178. pub struct RcDevice {
  179. pub obj: VkDevice,
  180. pub instance: Instance,
  181. }
  182.  
  183. impl Deref for RcDevice {
  184. type Target = VkDevice;
  185.  
  186. fn deref(&self) -> &Self::Target {
  187. &self.obj
  188. }
  189. }
  190.  
  191. impl Drop for RcDevice {
  192. fn drop(&mut self) {
  193. unsafe {
  194. self.obj.destroy_device(None);
  195. };
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement