Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- use std::ops::Deref;
- use ash::{vk, Entry, Instance as VkInstance, Device as VkDevice, InstanceError};
- use ash::prelude::VkResult;
- #[derive(Clone)]
- pub struct CommandPool {
- pub rc: Rc<RcCommandPool>,
- }
- impl CommandPool {
- pub fn new(device: &Device, create_info: &vk::CommandPoolCreateInfo) -> VkResult<Self> {
- Ok(Self {
- rc: Rc::new(RcCommandPool {
- obj: unsafe { device.create_command_pool(&create_info, None) }?,
- device: device.clone(),
- }),
- })
- }
- }
- impl Deref for CommandPool {
- type Target = RcCommandPool;
- fn deref(&self) -> &Self::Target {
- &self.rc
- }
- }
- #[derive(Clone)]
- pub struct RcCommandPool {
- pub obj: vk::CommandPool,
- pub device: Device,
- }
- impl Deref for RcCommandPool {
- type Target = vk::CommandPool;
- fn deref(&self) -> &Self::Target {
- &self.obj
- }
- }
- impl Drop for RcCommandPool {
- fn drop(&mut self) {
- unsafe {
- self.device.destroy_command_pool(**self, None);
- };
- }
- }
- #[derive(Clone)]
- pub struct DescriptorSetLayout {
- pub rc: Rc<RcDescriptorSetLayout>,
- }
- impl DescriptorSetLayout {
- pub fn new(device: &Device, create_info: &vk::DescriptorSetLayoutCreateInfo) -> VkResult<Self> {
- Ok(Self {
- rc: Rc::new(RcDescriptorSetLayout {
- obj: unsafe { device.create_descriptor_set_layout(&create_info, None) }?,
- device: device.clone(),
- }),
- })
- }
- }
- impl Deref for DescriptorSetLayout {
- type Target = RcDescriptorSetLayout;
- fn deref(&self) -> &Self::Target {
- &self.rc
- }
- }
- #[derive(Clone)]
- pub struct RcDescriptorSetLayout {
- pub obj: vk::DescriptorSetLayout,
- pub device: Device,
- }
- impl Deref for RcDescriptorSetLayout {
- type Target = vk::DescriptorSetLayout;
- fn deref(&self) -> &Self::Target {
- &self.obj
- }
- }
- impl Drop for RcDescriptorSetLayout {
- fn drop(&mut self) {
- unsafe {
- self.device.destroy_descriptor_set_layout(**self, None);
- };
- }
- }
- /* Extra defines */
- #[derive(Clone)]
- pub struct Instance {
- pub rc: Rc<RcInstance>,
- pub entry: Entry,
- }
- impl Instance {
- pub fn new_simple(create_info: &vk::InstanceCreateInfo) -> Result<Self, InstanceError> {
- let entry = Entry::new().unwrap();
- Ok((entry, Self::new(&entry, create_info)?))
- }
- pub fn new(entry: &Entry, create_info: &vk::InstanceCreateInfo) -> Result<Self, InstanceError> {
- Ok(Self {
- rc: Rc::new(RcInstance {
- obj: unsafe { entry.create_instance(create_info, None) }?,
- }),
- entry,
- })
- }
- }
- impl Deref for Instance {
- type Target = RcInstance;
- fn deref(&self) -> &Self::Target {
- &self.rc
- }
- }
- #[derive(Clone)]
- pub struct RcInstance {
- pub obj: VkInstance,
- }
- impl Deref for RcInstance {
- type Target = VkInstance;
- fn deref(&self) -> &Self::Target {
- &self.obj
- }
- }
- impl Drop for RcInstance {
- fn drop(&mut self) {
- unsafe { self.destroy_instance(None) };
- }
- }
- #[derive(Clone)]
- pub struct Device {
- pub rc: Rc<RcDevice>,
- }
- impl Device {
- pub fn new(
- instance: &Instance,
- physical_device: vk::PhysicalDevice,
- create_info: &vk::DeviceCreateInfo,
- ) -> Result<Self, vk::Result> {
- Ok(Self {
- rc: Rc::new(RcDevice {
- obj: unsafe { instance.create_device(physical_device, create_info, None) }?,
- instance: instance.clone(),
- }),
- })
- }
- }
- impl Deref for Device {
- type Target = RcDevice;
- fn deref(&self) -> &Self::Target {
- &self.rc
- }
- }
- #[derive(Clone)]
- pub struct RcDevice {
- pub obj: VkDevice,
- pub instance: Instance,
- }
- impl Deref for RcDevice {
- type Target = VkDevice;
- fn deref(&self) -> &Self::Target {
- &self.obj
- }
- }
- impl Drop for RcDevice {
- fn drop(&mut self) {
- unsafe {
- self.obj.destroy_device(None);
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement