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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use {
    crate::{
        descriptor,
        escape::Handle,
        util::{device_owned, Device, DeviceId},
    },
    gfx_hal::{pso::DescriptorSetLayoutBinding, Backend, Device as _},
    relevant::Relevant,
    smallvec::SmallVec,
};

/// Descriptor set layout info.
#[derive(Clone, Debug)]
pub struct DescriptorSetInfo {
    /// Bindings.
    pub bindings: Vec<DescriptorSetLayoutBinding>,
}

impl DescriptorSetInfo {
    /// Get descriptor ranges of the layout.
    pub fn ranges(&self) -> descriptor::DescriptorRanges {
        descriptor::DescriptorRanges::from_bindings(&self.bindings)
    }
}

/// Generic descriptor set layout resource wrapper.
#[derive(Debug)]
pub struct DescriptorSetLayout<B: Backend> {
    device: DeviceId,
    raw: B::DescriptorSetLayout,
    info: DescriptorSetInfo,
    relevant: Relevant,
}

device_owned!(DescriptorSetLayout<B>);

impl<B> DescriptorSetLayout<B>
where
    B: Backend,
{
    /// Create new descriptor set layout
    pub unsafe fn create(
        device: &Device<B>,
        info: DescriptorSetInfo,
    ) -> Result<Self, gfx_hal::device::OutOfMemory> {
        let raw = device
            .create_descriptor_set_layout(&info.bindings, std::iter::empty::<B::Sampler>())?;

        Ok(DescriptorSetLayout {
            device: device.id(),
            raw,
            info,
            relevant: Relevant,
        })
    }

    /// Destroy descriptor set layout resource.
    pub unsafe fn dispose(self, device: &Device<B>) {
        self.assert_device_owner(device);
        device.destroy_descriptor_set_layout(self.raw);
        self.relevant.dispose();
    }

    /// Get reference to raw descriptor set layout resource.
    pub fn raw(&self) -> &B::DescriptorSetLayout {
        &self.raw
    }

    /// Get mutable reference to raw descriptor set layout resource.
    pub unsafe fn raw_mut(&mut self) -> &mut B::DescriptorSetLayout {
        &mut self.raw
    }

    /// Get descriptor set layout info.
    pub fn info(&self) -> &DescriptorSetInfo {
        &self.info
    }
}

/// Generic descriptor set resource wrapper.
#[derive(Debug)]
pub struct DescriptorSet<B: Backend> {
    device: DeviceId,
    set: descriptor::DescriptorSet<B>,
    layout: Handle<DescriptorSetLayout<B>>,
    relevant: Relevant,
}

device_owned!(DescriptorSet<B>);

impl<B> DescriptorSet<B>
where
    B: Backend,
{
    /// Create new descriptor set.
    pub unsafe fn create(
        device: &Device<B>,
        allocator: &mut descriptor::DescriptorAllocator<B>,
        layout: Handle<DescriptorSetLayout<B>>,
    ) -> Result<Self, gfx_hal::device::OutOfMemory> {
        let mut sets = SmallVec::<[_; 1]>::new();

        allocator.allocate(device, layout.raw(), layout.info().ranges(), 1, &mut sets)?;

        assert_eq!(sets.len() as u32, 1);
        Ok(DescriptorSet {
            device: device.id(),
            set: sets.swap_remove(0),
            layout: layout.clone(),
            relevant: Relevant,
        })
    }

    /// Create new descriptor sets.
    pub unsafe fn create_many(
        device: &Device<B>,
        allocator: &mut descriptor::DescriptorAllocator<B>,
        layout: Handle<DescriptorSetLayout<B>>,
        count: u32,
        extend: &mut impl Extend<Self>,
    ) -> Result<(), gfx_hal::device::OutOfMemory> {
        let mut sets = SmallVec::<[_; 32]>::new();

        allocator.allocate(
            device,
            layout.raw(),
            layout.info().ranges(),
            count,
            &mut sets,
        )?;

        assert_eq!(sets.len() as u32, count);

        extend.extend(sets.into_iter().map(|set| DescriptorSet {
            device: device.id(),
            set,
            layout: layout.clone(),
            relevant: Relevant,
        }));

        Ok(())
    }

    /// Destroy descriptor set resource.
    pub unsafe fn dispose(self, allocator: &mut descriptor::DescriptorAllocator<B>) {
        allocator.free(Some(self.set));
        self.relevant.dispose();
    }

    /// Get reference to raw descriptor set resource.
    pub fn raw(&self) -> &B::DescriptorSet {
        self.set.raw()
    }

    /// Get mutable reference to raw descriptor set resource.
    pub unsafe fn raw_mut(&mut self) -> &mut B::DescriptorSet {
        self.set.raw_mut()
    }

    /// Get layout of descriptor set.
    pub fn layout(&mut self) -> &Handle<DescriptorSetLayout<B>> {
        &self.layout
    }
}