bort_vk/
descriptor_set.rs

1use crate::{DescriptorPool, DescriptorSetLayout, Device, DeviceOwned};
2use ash::{
3    prelude::VkResult,
4    vk::{self, Handle},
5};
6use std::sync::Arc;
7
8// Note: no destructor needed. Just drop pool.
9pub struct DescriptorSet {
10    handle: vk::DescriptorSet,
11    layout: Arc<DescriptorSetLayout>,
12
13    // dependencies
14    descriptor_pool: Arc<DescriptorPool>,
15}
16
17impl DescriptorSet {
18    pub fn new(
19        descriptor_pool: Arc<DescriptorPool>,
20        layout: Arc<DescriptorSetLayout>,
21    ) -> VkResult<Self> {
22        descriptor_pool.allocate_descriptor_set(layout)
23    }
24
25    /// Safetey: make sure `handle` was allocated from `descriptor_pool` using `layout`.
26    pub(crate) unsafe fn from_handle(
27        handle: vk::DescriptorSet,
28        layout: Arc<DescriptorSetLayout>,
29        descriptor_pool: Arc<DescriptorPool>,
30    ) -> Self {
31        Self {
32            handle,
33            layout,
34            descriptor_pool,
35        }
36    }
37
38    // Getters
39
40    pub fn handle(&self) -> vk::DescriptorSet {
41        self.handle
42    }
43
44    pub fn layout(&self) -> &Arc<DescriptorSetLayout> {
45        &self.layout
46    }
47
48    #[inline]
49    pub fn descriptor_pool(&self) -> &Arc<DescriptorPool> {
50        &self.descriptor_pool
51    }
52}
53
54impl Drop for DescriptorSet {
55    fn drop(&mut self) {
56        let reset_flag_set = self
57            .descriptor_pool
58            .properties()
59            .flags
60            .contains(vk::DescriptorPoolCreateFlags::FREE_DESCRIPTOR_SET);
61        if !reset_flag_set {
62            return; // this set can only be freed by the pool
63        }
64
65        unsafe {
66            let _res = self
67                .device()
68                .inner()
69                .free_descriptor_sets(self.descriptor_pool.handle(), &[self.handle]);
70        }
71    }
72}
73
74impl DeviceOwned for DescriptorSet {
75    #[inline]
76    fn device(&self) -> &Arc<Device> {
77        self.descriptor_pool.device()
78    }
79
80    #[inline]
81    fn handle_raw(&self) -> u64 {
82        self.handle.as_raw()
83    }
84}