1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::sync::Arc;

use erupt::{vk, ExtendableFromConst};
#[cfg(feature = "tracing")]
use tracing1::error;

use crate::context::Context;
use crate::{AscheError, Result};

/// Wraps a descriptor pool.
#[derive(Debug)]
pub struct DescriptorPool {
    /// The raw Vulkan descriptor pool.
    pub raw: vk::DescriptorPool,
    context: Arc<Context>,
}

impl Drop for DescriptorPool {
    fn drop(&mut self) {
        unsafe {
            self.context
                .device
                .destroy_descriptor_pool(Some(self.raw), None);
        };
    }
}

impl DescriptorPool {
    pub(crate) fn new(context: Arc<Context>, pool: vk::DescriptorPool) -> Self {
        Self { context, raw: pool }
    }

    /// Creates a new descriptor set with the given `DescriptorSetLayout``.
    pub fn create_descriptor_set(
        &self,
        name: &str,
        layout: &DescriptorSetLayout,
        descriptor_count: Option<u32>,
    ) -> Result<DescriptorSet> {
        let counts = if let Some(count) = descriptor_count {
            vec![count]
        } else {
            vec![]
        };

        let layouts = [layout.raw];
        let info = vk::DescriptorSetAllocateInfoBuilder::new()
            .descriptor_pool(self.raw)
            .set_layouts(&layouts);

        let variable_info = vk::DescriptorSetVariableDescriptorCountAllocateInfoBuilder::new()
            .descriptor_counts(&counts);

        let info = if descriptor_count.is_some() {
            info.extend_from(&variable_info)
        } else {
            info
        };

        let sets =
            unsafe { self.context.device.allocate_descriptor_sets(&info) }.map_err(|err| {
                #[cfg(feature = "tracing")]
                error!("Unable to allocate a descriptor set: {}", err);
                AscheError::VkResult(err)
            })?;

        debug_assert_eq!(sets.len(), 1);
        let set = sets[0];

        self.context
            .set_object_name(name, vk::ObjectType::DESCRIPTOR_SET, set.0)?;

        Ok(DescriptorSet {
            context: self.context.clone(),
            pool: self.raw,
            raw: set,
        })
    }

    /// Frees all descriptor sets allocated from the pool. Invalidates all descriptor sets from the pool.
    pub fn free_sets(&self) -> Result<()> {
        unsafe { self.context.device.reset_descriptor_pool(self.raw, None) }.map_err(|err| {
            #[cfg(feature = "tracing")]
            error!("Unable reset a descriptor pool: {}", err);
            AscheError::VkResult(err)
        })?;
        Ok(())
    }
}

/// Wraps a descriptor set layout.
#[derive(Debug)]
pub struct DescriptorSetLayout {
    /// The raw Vulkan descriptor set layout.
    pub raw: vk::DescriptorSetLayout,
    context: Arc<Context>,
}

impl Drop for DescriptorSetLayout {
    fn drop(&mut self) {
        unsafe {
            self.context
                .device
                .destroy_descriptor_set_layout(Some(self.raw), None);
        };
    }
}

impl DescriptorSetLayout {
    pub(crate) fn new(context: Arc<Context>, layout: vk::DescriptorSetLayout) -> Self {
        Self {
            context,
            raw: layout,
        }
    }
}

/// Wraps a descriptor set.
#[derive(Debug)]
pub struct DescriptorSet {
    /// The raw Vulkan descriptor pool.
    pub raw: vk::DescriptorSet,
    pool: vk::DescriptorPool,
    context: Arc<Context>,
}

impl DescriptorSet {
    /// Frees the descriptor set. Needs to be created from a pool with the
    /// `vk::DescriptorPoolCreateFlags::FREE_DESCRIPTOR_SET` flag set.
    pub fn free(&mut self) -> Result<()> {
        let sets = [self.raw];
        unsafe { self.context.device.free_descriptor_sets(self.pool, &sets) }.map_err(|err| {
            #[cfg(feature = "tracing")]
            error!("Unable to free a descriptor set: {}", err);
            AscheError::VkResult(err)
        })?;

        Ok(())
    }
}