Fix allocating multiple sets from desc pool

This commit is contained in:
hodasemi 2023-04-16 08:41:33 +02:00
parent d64bc53d9a
commit 9b6342ecc2
3 changed files with 28 additions and 15 deletions

View file

@ -6,6 +6,7 @@ use std::sync::Arc;
pub struct DescriptorPoolBuilder {
layout: Option<Arc<DescriptorSetLayout>>,
descriptor_count: u32,
flags: VkDescriptorPoolCreateFlagBits,
}
@ -16,6 +17,12 @@ impl DescriptorPoolBuilder {
self
}
pub fn set_descriptor_set_count(mut self, count: u32) -> Self {
self.descriptor_count = count;
self
}
pub fn set_layout(mut self, layout: Arc<DescriptorSetLayout>) -> Self {
self.layout = Some(layout);
@ -27,24 +34,29 @@ impl DescriptorPoolBuilder {
if self.layout.is_none() {
panic!("no layout set!");
}
}
let layout = self.layout.expect("descriptor set layout was not set!");
let descriptor_count: u32 = layout
.pool_sizes()
.iter()
.map(|pool| pool.descriptorCount)
.sum();
if cfg!(debug_assertions) {
if descriptor_count == 0 {
if self.descriptor_count == 0 {
panic!("descriptor count must be greater than 0");
}
}
let descriptor_pool_ci =
VkDescriptorPoolCreateInfo::new(self.flags, descriptor_count, layout.pool_sizes());
let layout = self.layout.expect("descriptor set layout was not set!");
let pool_sizes: Vec<VkDescriptorPoolSize> = layout
.pool_sizes()
.iter()
.cloned()
.map(|mut pool_size| {
pool_size.descriptorCount *= self.descriptor_count;
pool_size
})
.collect();
let descriptor_pool_ci = VkDescriptorPoolCreateInfo::new(
self.flags,
self.descriptor_count,
pool_sizes.as_slice(),
);
let descriptor_pool = device.create_descriptor_pool(&descriptor_pool_ci)?;
@ -67,6 +79,7 @@ impl DescriptorPool {
pub fn builder() -> DescriptorPoolBuilder {
DescriptorPoolBuilder {
layout: None,
descriptor_count: 1,
flags: VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT.into(),
}
}

View file

@ -359,11 +359,11 @@ mod test {
VK_SHADER_STAGE_FRAGMENT_BIT,
0,
)
.change_descriptor_count(DESCRIPTOR_COUNT)
.build(device.clone())?;
let descriptor_pool = DescriptorPool::builder()
.set_layout(descriptor_layout.clone())
.set_descriptor_set_count(DESCRIPTOR_COUNT)
.build(device.clone())?;
let descriptors: Vec<Arc<DescriptorSet>> = (0..DESCRIPTOR_COUNT)

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[repr(C)]
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct VkDescriptorPoolSize {
pub ty: VkDescriptorType,
pub descriptorCount: u32,