use crate::descriptor_set::layout::DescriptorSetLayout;
use crate::descriptor_set::update::{DescriptorWriteInfo, WriteDescriptorSet};
use crate::device::DeviceOwned;
use crate::VulkanObject;
use smallvec::SmallVec;
use std::fmt;
use std::ptr;
pub struct UnsafeDescriptorSet {
handle: ash::vk::DescriptorSet,
}
impl UnsafeDescriptorSet {
pub(crate) fn new(handle: ash::vk::DescriptorSet) -> Self {
Self { handle }
}
pub unsafe fn write<'a>(
&mut self,
layout: &DescriptorSetLayout,
writes: impl IntoIterator<Item = &'a WriteDescriptorSet>,
) {
let (infos, mut writes): (SmallVec<[_; 8]>, SmallVec<[_; 8]>) = writes
.into_iter()
.map(|write| {
let descriptor_type = layout.bindings()[&write.binding()].descriptor_type;
(
write.to_vulkan_info(descriptor_type),
write.to_vulkan(self.handle, descriptor_type),
)
})
.unzip();
if writes.is_empty() {
return;
}
for (info, write) in infos.iter().zip(writes.iter_mut()) {
match info {
DescriptorWriteInfo::Image(info) => {
write.descriptor_count = info.len() as u32;
write.p_image_info = info.as_ptr();
}
DescriptorWriteInfo::Buffer(info) => {
write.descriptor_count = info.len() as u32;
write.p_buffer_info = info.as_ptr();
}
DescriptorWriteInfo::BufferView(info) => {
write.descriptor_count = info.len() as u32;
write.p_texel_buffer_view = info.as_ptr();
}
}
debug_assert!(write.descriptor_count != 0);
}
let fns = layout.device().fns();
fns.v1_0.update_descriptor_sets(
layout.device().internal_object(),
writes.len() as u32,
writes.as_ptr(),
0,
ptr::null(),
);
}
}
unsafe impl VulkanObject for UnsafeDescriptorSet {
type Object = ash::vk::DescriptorSet;
#[inline]
fn internal_object(&self) -> ash::vk::DescriptorSet {
self.handle
}
}
impl fmt::Debug for UnsafeDescriptorSet {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "<Vulkan descriptor set {:?}>", self.handle)
}
}