use crate::buffer::sys::UnsafeBuffer;
use crate::check_errors;
use crate::device::Queue;
use crate::image::sys::UnsafeImage;
use crate::memory::DeviceMemory;
use crate::sync::Fence;
use crate::sync::Semaphore;
use crate::DeviceSize;
use crate::Error;
use crate::OomError;
use crate::SynchronizedVulkanObject;
use crate::VulkanObject;
use smallvec::SmallVec;
use std::error;
use std::fmt;
use std::marker::PhantomData;
pub struct SubmitBindSparseBuilder<'a> {
infos: SmallVec<[SubmitBindSparseBatchBuilder<'a>; 1]>,
fence: ash::vk::Fence,
}
impl<'a> SubmitBindSparseBuilder<'a> {
#[inline]
pub fn new() -> SubmitBindSparseBuilder<'a> {
SubmitBindSparseBuilder {
infos: SmallVec::new(),
fence: ash::vk::Fence::null(),
}
}
#[inline]
pub fn add(&mut self, builder: SubmitBindSparseBatchBuilder<'a>) {
self.infos.push(builder);
}
#[inline]
pub fn has_fence(&self) -> bool {
self.fence != ash::vk::Fence::null()
}
#[inline]
pub unsafe fn set_fence_signal(&mut self, fence: &'a Fence) {
self.fence = fence.internal_object();
}
#[inline]
pub fn merge(
&mut self,
other: SubmitBindSparseBuilder<'a>,
) -> Result<(), SubmitBindSparseBuilder<'a>> {
if self.fence != ash::vk::Fence::null() && other.fence != ash::vk::Fence::null() {
return Err(other);
}
self.infos.extend(other.infos.into_iter());
Ok(())
}
pub fn submit(self, queue: &Queue) -> Result<(), SubmitBindSparseError> {
unsafe {
debug_assert!(queue.family().supports_sparse_binding());
let fns = queue.device().fns();
let queue = queue.internal_object_guard();
let buffer_binds_storage: SmallVec<[_; 4]> = self
.infos
.iter()
.flat_map(|infos| infos.buffer_binds.iter())
.map(|buf_bind| ash::vk::SparseBufferMemoryBindInfo {
buffer: buf_bind.buffer,
bind_count: buf_bind.binds.len() as u32,
p_binds: buf_bind.binds.as_ptr(),
})
.collect();
let image_opaque_binds_storage: SmallVec<[_; 4]> = self
.infos
.iter()
.flat_map(|infos| infos.image_opaque_binds.iter())
.map(|img_bind| ash::vk::SparseImageOpaqueMemoryBindInfo {
image: img_bind.image,
bind_count: img_bind.binds.len() as u32,
p_binds: img_bind.binds.as_ptr(),
})
.collect();
let image_binds_storage: SmallVec<[_; 4]> = self
.infos
.iter()
.flat_map(|infos| infos.image_binds.iter())
.map(|img_bind| ash::vk::SparseImageMemoryBindInfo {
image: img_bind.image,
bind_count: img_bind.binds.len() as u32,
p_binds: img_bind.binds.as_ptr(),
})
.collect();
let bs_infos = {
let mut bs_infos: SmallVec<[_; 4]> = SmallVec::new();
let mut next_buffer_bind = 0;
let mut next_image_opaque_bind = 0;
let mut next_image_bind = 0;
for builder in self.infos.iter() {
bs_infos.push(ash::vk::BindSparseInfo {
wait_semaphore_count: builder.wait_semaphores.len() as u32,
p_wait_semaphores: builder.wait_semaphores.as_ptr(),
buffer_bind_count: builder.buffer_binds.len() as u32,
p_buffer_binds: if next_buffer_bind != 0 {
buffer_binds_storage.as_ptr().offset(next_buffer_bind)
} else {
buffer_binds_storage.as_ptr()
},
image_opaque_bind_count: builder.image_opaque_binds.len() as u32,
p_image_opaque_binds: if next_image_opaque_bind != 0 {
image_opaque_binds_storage
.as_ptr()
.offset(next_image_opaque_bind)
} else {
image_opaque_binds_storage.as_ptr()
},
image_bind_count: builder.image_binds.len() as u32,
p_image_binds: if next_image_bind != 0 {
image_binds_storage.as_ptr().offset(next_image_bind)
} else {
image_binds_storage.as_ptr()
},
signal_semaphore_count: builder.signal_semaphores.len() as u32,
p_signal_semaphores: builder.signal_semaphores.as_ptr(),
..Default::default()
});
next_buffer_bind += builder.buffer_binds.len() as isize;
next_image_opaque_bind += builder.image_opaque_binds.len() as isize;
next_image_bind += builder.image_binds.len() as isize;
}
debug_assert_eq!(next_buffer_bind as usize, buffer_binds_storage.len());
debug_assert_eq!(
next_image_opaque_bind as usize,
image_opaque_binds_storage.len()
);
debug_assert_eq!(next_image_bind as usize, image_binds_storage.len());
bs_infos
};
check_errors(fns.v1_0.queue_bind_sparse(
*queue,
bs_infos.len() as u32,
bs_infos.as_ptr(),
self.fence,
))?;
Ok(())
}
}
}
impl<'a> fmt::Debug for SubmitBindSparseBuilder<'a> {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "<Bind sparse operation>")
}
}
pub struct SubmitBindSparseBatchBuilder<'a> {
wait_semaphores: SmallVec<[ash::vk::Semaphore; 8]>,
buffer_binds: SmallVec<[SubmitBindSparseBufferBindBuilder<'a>; 2]>,
image_opaque_binds: SmallVec<[SubmitBindSparseImageOpaqueBindBuilder<'a>; 2]>,
image_binds: SmallVec<[SubmitBindSparseImageBindBuilder<'a>; 2]>,
signal_semaphores: SmallVec<[ash::vk::Semaphore; 8]>,
marker: PhantomData<&'a ()>,
}
impl<'a> SubmitBindSparseBatchBuilder<'a> {
#[inline]
pub fn new() -> SubmitBindSparseBatchBuilder<'a> {
SubmitBindSparseBatchBuilder {
wait_semaphores: SmallVec::new(),
buffer_binds: SmallVec::new(),
image_opaque_binds: SmallVec::new(),
image_binds: SmallVec::new(),
signal_semaphores: SmallVec::new(),
marker: PhantomData,
}
}
pub fn add_buffer(&mut self, cmd: SubmitBindSparseBufferBindBuilder<'a>) {
self.buffer_binds.push(cmd);
}
pub fn add_image_opaque(&mut self, cmd: SubmitBindSparseImageOpaqueBindBuilder<'a>) {
self.image_opaque_binds.push(cmd);
}
pub fn add_image(&mut self, cmd: SubmitBindSparseImageBindBuilder<'a>) {
self.image_binds.push(cmd);
}
#[inline]
pub unsafe fn add_wait_semaphore(&mut self, semaphore: &'a Semaphore) {
self.wait_semaphores.push(semaphore.internal_object());
}
#[inline]
pub fn num_signal_semaphores(&self) -> usize {
self.signal_semaphores.len()
}
#[inline]
pub unsafe fn add_signal_semaphore(&mut self, semaphore: &'a Semaphore) {
self.signal_semaphores.push(semaphore.internal_object());
}
}
pub struct SubmitBindSparseBufferBindBuilder<'a> {
buffer: ash::vk::Buffer,
binds: SmallVec<[ash::vk::SparseMemoryBind; 1]>,
marker: PhantomData<&'a ()>,
}
impl<'a> SubmitBindSparseBufferBindBuilder<'a> {
pub unsafe fn new(buffer: &'a UnsafeBuffer) -> SubmitBindSparseBufferBindBuilder {
SubmitBindSparseBufferBindBuilder {
buffer: buffer.internal_object(),
binds: SmallVec::new(),
marker: PhantomData,
}
}
pub unsafe fn add_bind(
&mut self,
offset: DeviceSize,
size: DeviceSize,
memory: &DeviceMemory,
memory_offset: DeviceSize,
) {
self.binds.push(ash::vk::SparseMemoryBind {
resource_offset: offset,
size,
memory: memory.internal_object(),
memory_offset,
flags: ash::vk::SparseMemoryBindFlags::empty(), });
}
pub unsafe fn add_unbind(&mut self, offset: DeviceSize, size: DeviceSize) {
self.binds.push(ash::vk::SparseMemoryBind {
resource_offset: offset,
size,
memory: ash::vk::DeviceMemory::null(),
memory_offset: 0,
flags: ash::vk::SparseMemoryBindFlags::empty(),
});
}
}
pub struct SubmitBindSparseImageOpaqueBindBuilder<'a> {
image: ash::vk::Image,
binds: SmallVec<[ash::vk::SparseMemoryBind; 1]>,
marker: PhantomData<&'a ()>,
}
impl<'a> SubmitBindSparseImageOpaqueBindBuilder<'a> {
pub unsafe fn new(image: &'a UnsafeImage) -> SubmitBindSparseImageOpaqueBindBuilder {
SubmitBindSparseImageOpaqueBindBuilder {
image: image.internal_object(),
binds: SmallVec::new(),
marker: PhantomData,
}
}
pub unsafe fn add_bind(
&mut self,
offset: DeviceSize,
size: DeviceSize,
memory: &DeviceMemory,
memory_offset: DeviceSize,
bind_metadata: bool,
) {
self.binds.push(ash::vk::SparseMemoryBind {
resource_offset: offset,
size,
memory: memory.internal_object(),
memory_offset,
flags: if bind_metadata {
ash::vk::SparseMemoryBindFlags::METADATA
} else {
ash::vk::SparseMemoryBindFlags::empty()
},
});
}
pub unsafe fn add_unbind(&mut self, offset: DeviceSize, size: DeviceSize) {
self.binds.push(ash::vk::SparseMemoryBind {
resource_offset: offset,
size,
memory: ash::vk::DeviceMemory::null(),
memory_offset: 0,
flags: ash::vk::SparseMemoryBindFlags::empty(), });
}
}
pub struct SubmitBindSparseImageBindBuilder<'a> {
image: ash::vk::Image,
binds: SmallVec<[ash::vk::SparseImageMemoryBind; 1]>,
marker: PhantomData<&'a ()>,
}
impl<'a> SubmitBindSparseImageBindBuilder<'a> {
pub unsafe fn new(image: &'a UnsafeImage) -> SubmitBindSparseImageBindBuilder {
SubmitBindSparseImageBindBuilder {
image: image.internal_object(),
binds: SmallVec::new(),
marker: PhantomData,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum SubmitBindSparseError {
OomError(OomError),
DeviceLost,
}
impl error::Error for SubmitBindSparseError {
#[inline]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
SubmitBindSparseError::OomError(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for SubmitBindSparseError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
SubmitBindSparseError::OomError(_) => "not enough memory",
SubmitBindSparseError::DeviceLost => "the connection to the device has been lost",
}
)
}
}
impl From<Error> for SubmitBindSparseError {
#[inline]
fn from(err: Error) -> SubmitBindSparseError {
match err {
err @ Error::OutOfHostMemory => SubmitBindSparseError::OomError(OomError::from(err)),
err @ Error::OutOfDeviceMemory => SubmitBindSparseError::OomError(OomError::from(err)),
Error::DeviceLost => SubmitBindSparseError::DeviceLost,
_ => panic!("unexpected error: {:?}", err),
}
}
}