use crate::buffer::BufferAccess;
use crate::check_errors;
use crate::command_buffer::submit::SubmitAnyBuilder;
use crate::command_buffer::submit::SubmitPresentBuilder;
use crate::command_buffer::submit::SubmitPresentError;
use crate::command_buffer::submit::SubmitSemaphoresWaitBuilder;
use crate::device::Device;
use crate::device::DeviceOwned;
use crate::device::Queue;
use crate::format::Format;
use crate::image::swapchain::SwapchainImage;
use crate::image::sys::UnsafeImage;
use crate::image::ImageAccess;
use crate::image::ImageCreateFlags;
use crate::image::ImageDimensions;
use crate::image::ImageInner;
use crate::image::ImageLayout;
use crate::image::ImageTiling;
use crate::image::ImageType;
use crate::image::ImageUsage;
use crate::swapchain::CapabilitiesError;
use crate::swapchain::ColorSpace;
use crate::swapchain::CompositeAlpha;
use crate::swapchain::PresentMode;
use crate::swapchain::PresentRegion;
use crate::swapchain::Surface;
use crate::swapchain::SurfaceSwapchainLock;
use crate::swapchain::SurfaceTransform;
use crate::sync::AccessCheckError;
use crate::sync::AccessError;
use crate::sync::AccessFlags;
use crate::sync::Fence;
use crate::sync::FlushError;
use crate::sync::GpuFuture;
use crate::sync::PipelineStages;
use crate::sync::Semaphore;
use crate::sync::SharingMode;
use crate::vk;
use crate::Error;
use crate::OomError;
use crate::Success;
use crate::VulkanObject;
use std::error;
use std::fmt;
use std::mem;
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum FullscreenExclusive {
Default,
Allowed,
Disallowed,
AppControlled,
}
impl FullscreenExclusive {
fn vk_sys_enum(&self) -> u32 {
match self {
&Self::Default => vk::FULL_SCREEN_EXCLUSIVE_DEFAUlT_EXT,
&Self::Allowed => vk::FULL_SCREEN_EXCLUSIVE_ALLOWED_EXT,
&Self::Disallowed => vk::FULL_SCREEN_EXCLUSIVE_DISALLOWED_EXT,
&Self::AppControlled => vk::FULL_SCREEN_EXCLUSIVE_APPLICATION_CONTROLLED_EXT,
}
}
}
pub fn acquire_next_image<W>(
swapchain: Arc<Swapchain<W>>,
timeout: Option<Duration>,
) -> Result<(usize, bool, SwapchainAcquireFuture<W>), AcquireError> {
let semaphore = Semaphore::from_pool(swapchain.device.clone())?;
let fence = Fence::from_pool(swapchain.device.clone())?;
let AcquiredImage { id, suboptimal } = {
let stale = swapchain.stale.lock().unwrap();
if *stale {
return Err(AcquireError::OutOfDate);
}
let acquire_result =
unsafe { acquire_next_image_raw(&swapchain, timeout, Some(&semaphore), Some(&fence)) };
if let &Err(AcquireError::FullscreenExclusiveLost) = &acquire_result {
swapchain
.fullscreen_exclusive_held
.store(false, Ordering::SeqCst);
}
acquire_result?
};
Ok((
id,
suboptimal,
SwapchainAcquireFuture {
swapchain,
semaphore: Some(semaphore),
fence: Some(fence),
image_id: id,
finished: AtomicBool::new(false),
},
))
}
pub fn present<F, W>(
swapchain: Arc<Swapchain<W>>,
before: F,
queue: Arc<Queue>,
index: usize,
) -> PresentFuture<F, W>
where
F: GpuFuture,
{
assert!(index < swapchain.images.len());
PresentFuture {
previous: before,
queue,
swapchain,
image_id: index,
present_region: None,
flushed: AtomicBool::new(false),
finished: AtomicBool::new(false),
}
}
pub fn present_incremental<F, W>(
swapchain: Arc<Swapchain<W>>,
before: F,
queue: Arc<Queue>,
index: usize,
present_region: PresentRegion,
) -> PresentFuture<F, W>
where
F: GpuFuture,
{
assert!(index < swapchain.images.len());
PresentFuture {
previous: before,
queue,
swapchain,
image_id: index,
present_region: Some(present_region),
flushed: AtomicBool::new(false),
finished: AtomicBool::new(false),
}
}
pub struct Swapchain<W> {
device: Arc<Device>,
surface: Arc<Surface<W>>,
swapchain: vk::SwapchainKHR,
images: Vec<ImageEntry>,
stale: Mutex<bool>,
num_images: u32,
format: Format,
color_space: ColorSpace,
dimensions: [u32; 2],
layers: u32,
usage: ImageUsage,
sharing_mode: SharingMode,
transform: SurfaceTransform,
composite_alpha: CompositeAlpha,
present_mode: PresentMode,
fullscreen_exclusive: FullscreenExclusive,
fullscreen_exclusive_held: AtomicBool,
clipped: bool,
}
struct ImageEntry {
image: UnsafeImage,
undefined_layout: AtomicBool,
}
impl<W> Swapchain<W> {
#[inline]
pub fn start(device: Arc<Device>, surface: Arc<Surface<W>>) -> SwapchainBuilder<W> {
SwapchainBuilder {
device,
surface,
num_images: 2,
format: None,
color_space: ColorSpace::SrgbNonLinear,
dimensions: None,
layers: 1,
usage: ImageUsage::none(),
sharing_mode: SharingMode::Exclusive,
transform: Default::default(),
composite_alpha: CompositeAlpha::Opaque,
present_mode: PresentMode::Fifo,
fullscreen_exclusive: FullscreenExclusive::Default,
clipped: true,
old_swapchain: None,
}
}
#[inline]
pub fn recreate(self: &Arc<Self>) -> SwapchainBuilder<W> {
SwapchainBuilder {
device: self.device().clone(),
surface: self.surface().clone(),
num_images: self.images.len() as u32,
format: Some(self.format),
color_space: self.color_space,
dimensions: None,
layers: self.layers,
usage: self.usage,
sharing_mode: self.sharing_mode.clone(),
transform: self.transform,
composite_alpha: self.composite_alpha,
present_mode: self.present_mode,
fullscreen_exclusive: self.fullscreen_exclusive,
clipped: self.clipped,
old_swapchain: Some(self.clone()),
}
}
#[inline]
pub fn surface(&self) -> &Arc<Surface<W>> {
&self.surface
}
#[inline]
pub fn raw_image(&self, offset: usize) -> Option<ImageInner> {
self.images.get(offset).map(|i| ImageInner {
image: &i.image,
first_layer: 0,
num_layers: self.layers as usize,
first_mipmap_level: 0,
num_mipmap_levels: 1,
})
}
#[inline]
pub fn num_images(&self) -> u32 {
self.images.len() as u32
}
#[inline]
pub fn format(&self) -> Format {
self.format
}
#[inline]
pub fn dimensions(&self) -> [u32; 2] {
self.dimensions
}
#[inline]
pub fn layers(&self) -> u32 {
self.layers
}
#[inline]
pub fn transform(&self) -> SurfaceTransform {
self.transform
}
#[inline]
pub fn composite_alpha(&self) -> CompositeAlpha {
self.composite_alpha
}
#[inline]
pub fn present_mode(&self) -> PresentMode {
self.present_mode
}
#[inline]
pub fn clipped(&self) -> bool {
self.clipped
}
#[inline]
pub fn fullscreen_exclusive(&self) -> FullscreenExclusive {
self.fullscreen_exclusive
}
pub fn acquire_fullscreen_exclusive(&self) -> Result<(), FullscreenExclusiveError> {
if self.fullscreen_exclusive != FullscreenExclusive::AppControlled {
return Err(FullscreenExclusiveError::NotAppControlled);
}
if self.fullscreen_exclusive_held.swap(true, Ordering::SeqCst) {
return Err(FullscreenExclusiveError::DoubleAcquire);
}
unsafe {
check_errors(
self.device.pointers().AcquireFullScreenExclusiveModeEXT(
self.device.internal_object(),
self.swapchain,
),
)?;
}
Ok(())
}
pub fn release_fullscreen_exclusive(&self) -> Result<(), FullscreenExclusiveError> {
if self.fullscreen_exclusive != FullscreenExclusive::AppControlled {
return Err(FullscreenExclusiveError::NotAppControlled);
}
if !self.fullscreen_exclusive_held.swap(false, Ordering::SeqCst) {
return Err(FullscreenExclusiveError::DoubleRelease);
}
unsafe {
check_errors(
self.device.pointers().ReleaseFullScreenExclusiveModeEXT(
self.device.internal_object(),
self.swapchain,
),
)?;
}
Ok(())
}
pub fn is_fullscreen_exclusive(&self) -> bool {
if self.fullscreen_exclusive != FullscreenExclusive::AppControlled {
false
} else {
self.fullscreen_exclusive_held.load(Ordering::SeqCst)
}
}
pub(crate) fn image_layout_initialized(&self, image_offset: usize) {
let image_entry = self.images.get(image_offset);
if let Some(ref image_entry) = image_entry {
image_entry.undefined_layout.store(false, Ordering::SeqCst);
}
}
pub(crate) fn is_image_layout_initialized(&self, image_offset: usize) -> bool {
let image_entry = self.images.get(image_offset);
if let Some(ref image_entry) = image_entry {
!image_entry.undefined_layout.load(Ordering::SeqCst)
} else {
false
}
}
}
unsafe impl<W> VulkanObject for Swapchain<W> {
type Object = vk::SwapchainKHR;
const TYPE: vk::ObjectType = vk::OBJECT_TYPE_SWAPCHAIN_KHR;
#[inline]
fn internal_object(&self) -> vk::SwapchainKHR {
self.swapchain
}
}
unsafe impl<W> DeviceOwned for Swapchain<W> {
fn device(&self) -> &Arc<Device> {
&self.device
}
}
impl<W> fmt::Debug for Swapchain<W> {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "<Vulkan swapchain {:?}>", self.swapchain)
}
}
impl<W> Drop for Swapchain<W> {
#[inline]
fn drop(&mut self) {
unsafe {
let vk = self.device.pointers();
vk.DestroySwapchainKHR(self.device.internal_object(), self.swapchain, ptr::null());
self.surface.flag().store(false, Ordering::Release);
}
}
}
#[derive(Debug)]
pub struct SwapchainBuilder<W> {
device: Arc<Device>,
surface: Arc<Surface<W>>,
old_swapchain: Option<Arc<Swapchain<W>>>,
num_images: u32,
format: Option<Format>,
color_space: ColorSpace,
dimensions: Option<[u32; 2]>,
layers: u32,
usage: ImageUsage,
sharing_mode: SharingMode,
transform: SurfaceTransform,
composite_alpha: CompositeAlpha,
present_mode: PresentMode,
fullscreen_exclusive: FullscreenExclusive,
clipped: bool,
}
impl<W> SwapchainBuilder<W> {
pub fn build(
self,
) -> Result<(Arc<Swapchain<W>>, Vec<Arc<SwapchainImage<W>>>), SwapchainCreationError> {
let SwapchainBuilder {
device,
surface,
old_swapchain,
num_images,
format,
color_space,
dimensions,
layers,
usage,
sharing_mode,
transform,
composite_alpha,
present_mode,
fullscreen_exclusive,
clipped,
} = self;
assert_eq!(
device.instance().internal_object(),
surface.instance().internal_object()
);
let capabilities = surface.capabilities(device.physical_device())?;
if num_images < capabilities.min_image_count {
return Err(SwapchainCreationError::UnsupportedMinImagesCount);
}
if let Some(c) = capabilities.max_image_count {
if num_images > c {
return Err(SwapchainCreationError::UnsupportedMaxImagesCount);
}
}
let format = {
if let Some(format) = format {
if !capabilities
.supported_formats
.iter()
.any(|&(f, c)| f == format && c == color_space)
{
return Err(SwapchainCreationError::UnsupportedFormat);
}
format
} else {
if let Some(format) = [Format::R8G8B8A8Unorm, Format::B8G8R8A8Unorm]
.iter()
.copied()
.find(|&format| {
capabilities
.supported_formats
.iter()
.any(|&(f, c)| f == format && c == color_space)
})
{
format
} else {
return Err(SwapchainCreationError::UnsupportedFormat);
}
}
};
let dimensions = if let Some(dimensions) = dimensions {
if dimensions[0] < capabilities.min_image_extent[0] {
return Err(SwapchainCreationError::UnsupportedDimensions);
}
if dimensions[1] < capabilities.min_image_extent[1] {
return Err(SwapchainCreationError::UnsupportedDimensions);
}
if dimensions[0] > capabilities.max_image_extent[0] {
return Err(SwapchainCreationError::UnsupportedDimensions);
}
if dimensions[1] > capabilities.max_image_extent[1] {
return Err(SwapchainCreationError::UnsupportedDimensions);
}
dimensions
} else {
capabilities.current_extent.unwrap()
};
if layers < 1 || layers > capabilities.max_image_array_layers {
return Err(SwapchainCreationError::UnsupportedArrayLayers);
}
if (vk::ImageUsageFlags::from(usage)
& vk::ImageUsageFlags::from(capabilities.supported_usage_flags))
!= vk::ImageUsageFlags::from(usage)
{
return Err(SwapchainCreationError::UnsupportedUsageFlags);
}
if !capabilities.supported_transforms.supports(transform) {
return Err(SwapchainCreationError::UnsupportedSurfaceTransform);
}
if !capabilities
.supported_composite_alpha
.supports(composite_alpha)
{
return Err(SwapchainCreationError::UnsupportedCompositeAlpha);
}
if !capabilities.present_modes.supports(present_mode) {
return Err(SwapchainCreationError::UnsupportedPresentMode);
}
let flags = ImageCreateFlags::none();
match device.image_format_properties(
format,
ImageType::Dim2d,
ImageTiling::Optimal,
usage,
flags,
) {
Ok(_) => (),
Err(e) => {
eprintln!("{}", e);
return Err(SwapchainCreationError::UnsupportedImageConfiguration);
}
}
if let Some(ref sc) = old_swapchain {
if surface.internal_object() != sc.surface.internal_object() {
return Err(SwapchainCreationError::OldSwapchainSurfaceMismatch);
}
} else {
let has_already = surface.flag().swap(true, Ordering::AcqRel);
if has_already {
return Err(SwapchainCreationError::SurfaceInUse);
}
}
if !device.loaded_extensions().khr_swapchain {
return Err(SwapchainCreationError::MissingExtensionKHRSwapchain);
}
let mut surface_full_screen_exclusive_info = None;
if device.loaded_extensions().ext_full_screen_exclusive
&& surface
.instance()
.loaded_extensions()
.khr_get_physical_device_properties2
&& surface
.instance()
.loaded_extensions()
.khr_get_surface_capabilities2
{
surface_full_screen_exclusive_info = Some(vk::SurfaceFullScreenExclusiveInfoEXT {
sType: vk::STRUCTURE_TYPE_SURFACE_FULL_SCREEN_EXCLUSIVE_INFO_EXT,
pNext: ptr::null(),
fullScreenExclusive: fullscreen_exclusive.vk_sys_enum(),
});
}
let p_next = match surface_full_screen_exclusive_info.as_ref() {
Some(some) => unsafe { mem::transmute(some as *const _) },
None => ptr::null(),
};
assert_ne!(usage, ImageUsage::none());
if let Some(ref old_swapchain) = old_swapchain {
let mut stale = old_swapchain.stale.lock().unwrap();
if *stale {
return Err(SwapchainCreationError::OldSwapchainAlreadyUsed);
} else {
*stale = true;
}
}
let vk = device.pointers();
let swapchain = unsafe {
let (sh_mode, sh_count, sh_indices) = match sharing_mode {
SharingMode::Exclusive => (vk::SHARING_MODE_EXCLUSIVE, 0, ptr::null()),
SharingMode::Concurrent(ref ids) => {
(vk::SHARING_MODE_CONCURRENT, ids.len() as u32, ids.as_ptr())
}
};
let infos = vk::SwapchainCreateInfoKHR {
sType: vk::STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
pNext: p_next,
flags: 0,
surface: surface.internal_object(),
minImageCount: num_images,
imageFormat: format as u32,
imageColorSpace: color_space as u32,
imageExtent: vk::Extent2D {
width: dimensions[0],
height: dimensions[1],
},
imageArrayLayers: layers,
imageUsage: usage.into(),
imageSharingMode: sh_mode,
queueFamilyIndexCount: sh_count,
pQueueFamilyIndices: sh_indices,
preTransform: transform as u32,
compositeAlpha: composite_alpha as u32,
presentMode: present_mode as u32,
clipped: if clipped { vk::TRUE } else { vk::FALSE },
oldSwapchain: if let Some(ref old_swapchain) = old_swapchain {
old_swapchain.swapchain
} else {
0
},
};
let mut output = MaybeUninit::uninit();
check_errors(vk.CreateSwapchainKHR(
device.internal_object(),
&infos,
ptr::null(),
output.as_mut_ptr(),
))?;
output.assume_init()
};
let image_handles = unsafe {
let mut num = 0;
check_errors(vk.GetSwapchainImagesKHR(
device.internal_object(),
swapchain,
&mut num,
ptr::null_mut(),
))?;
let mut images = Vec::with_capacity(num as usize);
check_errors(vk.GetSwapchainImagesKHR(
device.internal_object(),
swapchain,
&mut num,
images.as_mut_ptr(),
))?;
images.set_len(num as usize);
images
};
let images = image_handles
.into_iter()
.map(|image| unsafe {
let dims = ImageDimensions::Dim2d {
width: dimensions[0],
height: dimensions[1],
array_layers: layers,
};
let img =
UnsafeImage::from_raw(device.clone(), image, usage, format, flags, dims, 1, 1);
ImageEntry {
image: img,
undefined_layout: AtomicBool::new(true),
}
})
.collect::<Vec<_>>();
let fullscreen_exclusive_held = old_swapchain
.as_ref()
.map(|old_swapchain| {
if old_swapchain.fullscreen_exclusive != FullscreenExclusive::AppControlled {
false
} else {
old_swapchain
.fullscreen_exclusive_held
.load(Ordering::SeqCst)
}
})
.unwrap_or(false);
let swapchain = Arc::new(Swapchain {
device: device.clone(),
surface: surface.clone(),
swapchain,
images,
stale: Mutex::new(false),
num_images,
format,
color_space,
dimensions,
layers,
usage: usage.clone(),
sharing_mode,
transform,
composite_alpha,
present_mode,
fullscreen_exclusive,
fullscreen_exclusive_held: AtomicBool::new(fullscreen_exclusive_held),
clipped,
});
let swapchain_images = unsafe {
let mut swapchain_images = Vec::with_capacity(swapchain.images.len());
for n in 0..swapchain.images.len() {
swapchain_images.push(SwapchainImage::from_raw(swapchain.clone(), n)?);
}
swapchain_images
};
Ok((swapchain, swapchain_images))
}
#[inline]
pub fn num_images(mut self, num_images: u32) -> Self {
self.num_images = num_images;
self
}
#[inline]
pub fn format(mut self, format: Format) -> Self {
self.format = Some(format);
self
}
#[inline]
pub fn color_space(mut self, color_space: ColorSpace) -> Self {
self.color_space = color_space;
self
}
#[inline]
pub fn dimensions(mut self, dimensions: [u32; 2]) -> Self {
self.dimensions = Some(dimensions);
self
}
#[inline]
pub fn layers(mut self, layers: u32) -> Self {
self.layers = layers;
self
}
#[inline]
pub fn usage(mut self, usage: ImageUsage) -> Self {
self.usage = usage;
self
}
#[inline]
pub fn sharing_mode<S>(mut self, sharing_mode: S) -> Self
where
S: Into<SharingMode>,
{
self.sharing_mode = sharing_mode.into();
self
}
#[inline]
pub fn transform(mut self, transform: SurfaceTransform) -> Self {
self.transform = transform;
self
}
#[inline]
pub fn composite_alpha(mut self, composite_alpha: CompositeAlpha) -> Self {
self.composite_alpha = composite_alpha;
self
}
#[inline]
pub fn present_mode(mut self, present_mode: PresentMode) -> Self {
self.present_mode = present_mode;
self
}
#[inline]
pub fn fullscreen_exclusive(mut self, fullscreen_exclusive: FullscreenExclusive) -> Self {
self.fullscreen_exclusive = fullscreen_exclusive;
self
}
#[inline]
pub fn clipped(mut self, clipped: bool) -> Self {
self.clipped = clipped;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SwapchainCreationError {
OomError(OomError),
DeviceLost,
SurfaceLost,
SurfaceInUse,
NativeWindowInUse,
MissingExtensionKHRSwapchain,
MissingExtensionExtFullScreenExclusive,
OldSwapchainSurfaceMismatch,
OldSwapchainAlreadyUsed,
UnsupportedMinImagesCount,
UnsupportedMaxImagesCount,
UnsupportedFormat,
UnsupportedDimensions,
UnsupportedArrayLayers,
UnsupportedUsageFlags,
UnsupportedSurfaceTransform,
UnsupportedCompositeAlpha,
UnsupportedPresentMode,
UnsupportedImageConfiguration,
}
impl error::Error for SwapchainCreationError {
#[inline]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
SwapchainCreationError::OomError(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for SwapchainCreationError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
SwapchainCreationError::OomError(_) => "not enough memory available",
SwapchainCreationError::DeviceLost => "the device was lost",
SwapchainCreationError::SurfaceLost => "the surface was lost",
SwapchainCreationError::SurfaceInUse => {
"the surface is already used by another swapchain"
}
SwapchainCreationError::NativeWindowInUse => {
"the window is already in use by another API"
}
SwapchainCreationError::MissingExtensionKHRSwapchain => {
"the `VK_KHR_swapchain` extension was not enabled"
}
SwapchainCreationError::MissingExtensionExtFullScreenExclusive => {
"the `VK_EXT_full_screen_exclusive` extension was not enabled"
}
SwapchainCreationError::OldSwapchainSurfaceMismatch => {
"surface mismatch between old and new swapchain"
}
SwapchainCreationError::OldSwapchainAlreadyUsed => {
"old swapchain has already been used to recreate a new one"
}
SwapchainCreationError::UnsupportedMinImagesCount => {
"the requested number of swapchain images is not supported by the surface"
}
SwapchainCreationError::UnsupportedMaxImagesCount => {
"the requested number of swapchain images is not supported by the surface"
}
SwapchainCreationError::UnsupportedFormat => {
"the requested image format is not supported by the surface"
}
SwapchainCreationError::UnsupportedDimensions => {
"the requested dimensions are not supported by the surface"
}
SwapchainCreationError::UnsupportedArrayLayers => {
"the requested array layers count is not supported by the surface"
}
SwapchainCreationError::UnsupportedUsageFlags => {
"the requested image usage is not supported by the surface"
}
SwapchainCreationError::UnsupportedSurfaceTransform => {
"the requested surface transform is not supported by the surface"
}
SwapchainCreationError::UnsupportedCompositeAlpha => {
"the requested composite alpha is not supported by the surface"
}
SwapchainCreationError::UnsupportedPresentMode => {
"the requested present mode is not supported by the surface"
}
SwapchainCreationError::UnsupportedImageConfiguration => {
"the requested image configuration is not supported by the physical device"
}
}
)
}
}
impl From<Error> for SwapchainCreationError {
#[inline]
fn from(err: Error) -> SwapchainCreationError {
match err {
err @ Error::OutOfHostMemory => SwapchainCreationError::OomError(OomError::from(err)),
err @ Error::OutOfDeviceMemory => SwapchainCreationError::OomError(OomError::from(err)),
Error::DeviceLost => SwapchainCreationError::DeviceLost,
Error::SurfaceLost => SwapchainCreationError::SurfaceLost,
Error::NativeWindowInUse => SwapchainCreationError::NativeWindowInUse,
_ => panic!("unexpected error: {:?}", err),
}
}
}
impl From<OomError> for SwapchainCreationError {
#[inline]
fn from(err: OomError) -> SwapchainCreationError {
SwapchainCreationError::OomError(err)
}
}
impl From<CapabilitiesError> for SwapchainCreationError {
#[inline]
fn from(err: CapabilitiesError) -> SwapchainCreationError {
match err {
CapabilitiesError::OomError(err) => SwapchainCreationError::OomError(err),
CapabilitiesError::SurfaceLost => SwapchainCreationError::SurfaceLost,
}
}
}
#[must_use]
pub struct SwapchainAcquireFuture<W> {
swapchain: Arc<Swapchain<W>>,
image_id: usize,
semaphore: Option<Semaphore>,
fence: Option<Fence>,
finished: AtomicBool,
}
impl<W> SwapchainAcquireFuture<W> {
#[inline]
pub fn image_id(&self) -> usize {
self.image_id
}
#[inline]
pub fn swapchain(&self) -> &Arc<Swapchain<W>> {
&self.swapchain
}
}
unsafe impl<W> GpuFuture for SwapchainAcquireFuture<W> {
#[inline]
fn cleanup_finished(&mut self) {}
#[inline]
unsafe fn build_submission(&self) -> Result<SubmitAnyBuilder, FlushError> {
if let Some(ref semaphore) = self.semaphore {
let mut sem = SubmitSemaphoresWaitBuilder::new();
sem.add_wait_semaphore(&semaphore);
Ok(SubmitAnyBuilder::SemaphoresWait(sem))
} else {
Ok(SubmitAnyBuilder::Empty)
}
}
#[inline]
fn flush(&self) -> Result<(), FlushError> {
Ok(())
}
#[inline]
unsafe fn signal_finished(&self) {
self.finished.store(true, Ordering::SeqCst);
}
#[inline]
fn queue_change_allowed(&self) -> bool {
true
}
#[inline]
fn queue(&self) -> Option<Arc<Queue>> {
None
}
#[inline]
fn check_buffer_access(
&self,
_: &dyn BufferAccess,
_: bool,
_: &Queue,
) -> Result<Option<(PipelineStages, AccessFlags)>, AccessCheckError> {
Err(AccessCheckError::Unknown)
}
#[inline]
fn check_image_access(
&self,
image: &dyn ImageAccess,
layout: ImageLayout,
_: bool,
_: &Queue,
) -> Result<Option<(PipelineStages, AccessFlags)>, AccessCheckError> {
let swapchain_image = self.swapchain.raw_image(self.image_id).unwrap();
if swapchain_image.image.internal_object() != image.inner().image.internal_object() {
return Err(AccessCheckError::Unknown);
}
if self.swapchain.images[self.image_id]
.undefined_layout
.load(Ordering::Relaxed)
&& layout != ImageLayout::Undefined
{
return Err(AccessCheckError::Denied(AccessError::ImageNotInitialized {
requested: layout,
}));
}
if layout != ImageLayout::Undefined && layout != ImageLayout::PresentSrc {
return Err(AccessCheckError::Denied(
AccessError::UnexpectedImageLayout {
allowed: ImageLayout::PresentSrc,
requested: layout,
},
));
}
Ok(None)
}
}
unsafe impl<W> DeviceOwned for SwapchainAcquireFuture<W> {
#[inline]
fn device(&self) -> &Arc<Device> {
&self.swapchain.device
}
}
impl<W> Drop for SwapchainAcquireFuture<W> {
fn drop(&mut self) {
if let Some(ref fence) = self.fence {
fence.wait(None).unwrap();
self.semaphore = None;
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum FullscreenExclusiveError {
OomError(OomError),
InitializationFailed,
SurfaceLost,
DoubleAcquire,
DoubleRelease,
NotAppControlled,
}
impl From<Error> for FullscreenExclusiveError {
#[inline]
fn from(err: Error) -> FullscreenExclusiveError {
match err {
err @ Error::OutOfHostMemory => FullscreenExclusiveError::OomError(OomError::from(err)),
err @ Error::OutOfDeviceMemory => {
FullscreenExclusiveError::OomError(OomError::from(err))
}
Error::SurfaceLost => FullscreenExclusiveError::SurfaceLost,
Error::InitializationFailed => FullscreenExclusiveError::InitializationFailed,
_ => panic!("unexpected error: {:?}", err),
}
}
}
impl From<OomError> for FullscreenExclusiveError {
#[inline]
fn from(err: OomError) -> FullscreenExclusiveError {
FullscreenExclusiveError::OomError(err)
}
}
impl error::Error for FullscreenExclusiveError {
#[inline]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
FullscreenExclusiveError::OomError(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for FullscreenExclusiveError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
FullscreenExclusiveError::OomError(_) => "not enough memory",
FullscreenExclusiveError::SurfaceLost => {
"the surface of this swapchain is no longer valid"
}
FullscreenExclusiveError::InitializationFailed => {
"operation could not be completed for driver specific reasons"
}
FullscreenExclusiveError::DoubleAcquire =>
"fullscreen exclusivity is already acquired",
FullscreenExclusiveError::DoubleRelease => "fullscreen exclusivity is not acquired",
FullscreenExclusiveError::NotAppControlled => {
"swapchain is not in fullscreen exclusive app controlled mode"
}
}
)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum AcquireError {
OomError(OomError),
DeviceLost,
Timeout,
SurfaceLost,
FullscreenExclusiveLost,
OutOfDate,
}
impl error::Error for AcquireError {
#[inline]
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
AcquireError::OomError(ref err) => Some(err),
_ => None,
}
}
}
impl fmt::Display for AcquireError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(
fmt,
"{}",
match *self {
AcquireError::OomError(_) => "not enough memory",
AcquireError::DeviceLost => "the connection to the device has been lost",
AcquireError::Timeout => "no image is available for acquiring yet",
AcquireError::SurfaceLost => "the surface of this swapchain is no longer valid",
AcquireError::OutOfDate => "the swapchain needs to be recreated",
AcquireError::FullscreenExclusiveLost => {
"the swapchain no longer has fullscreen exclusivity"
}
}
)
}
}
impl From<OomError> for AcquireError {
#[inline]
fn from(err: OomError) -> AcquireError {
AcquireError::OomError(err)
}
}
impl From<Error> for AcquireError {
#[inline]
fn from(err: Error) -> AcquireError {
match err {
err @ Error::OutOfHostMemory => AcquireError::OomError(OomError::from(err)),
err @ Error::OutOfDeviceMemory => AcquireError::OomError(OomError::from(err)),
Error::DeviceLost => AcquireError::DeviceLost,
Error::SurfaceLost => AcquireError::SurfaceLost,
Error::OutOfDate => AcquireError::OutOfDate,
Error::FullscreenExclusiveLost => AcquireError::FullscreenExclusiveLost,
_ => panic!("unexpected error: {:?}", err),
}
}
}
#[must_use = "Dropping this object will immediately block the thread until the GPU has finished processing the submission"]
pub struct PresentFuture<P, W>
where
P: GpuFuture,
{
previous: P,
queue: Arc<Queue>,
swapchain: Arc<Swapchain<W>>,
image_id: usize,
present_region: Option<PresentRegion>,
flushed: AtomicBool,
finished: AtomicBool,
}
impl<P, W> PresentFuture<P, W>
where
P: GpuFuture,
{
#[inline]
pub fn image_id(&self) -> usize {
self.image_id
}
#[inline]
pub fn swapchain(&self) -> &Arc<Swapchain<W>> {
&self.swapchain
}
}
unsafe impl<P, W> GpuFuture for PresentFuture<P, W>
where
P: GpuFuture,
{
#[inline]
fn cleanup_finished(&mut self) {
self.previous.cleanup_finished();
}
#[inline]
unsafe fn build_submission(&self) -> Result<SubmitAnyBuilder, FlushError> {
if self.flushed.load(Ordering::SeqCst) {
return Ok(SubmitAnyBuilder::Empty);
}
let queue = self.previous.queue().map(|q| q.clone());
Ok(match self.previous.build_submission()? {
SubmitAnyBuilder::Empty => {
let mut builder = SubmitPresentBuilder::new();
builder.add_swapchain(
&self.swapchain,
self.image_id as u32,
self.present_region.as_ref(),
);
SubmitAnyBuilder::QueuePresent(builder)
}
SubmitAnyBuilder::SemaphoresWait(sem) => {
let mut builder: SubmitPresentBuilder = sem.into();
builder.add_swapchain(
&self.swapchain,
self.image_id as u32,
self.present_region.as_ref(),
);
SubmitAnyBuilder::QueuePresent(builder)
}
SubmitAnyBuilder::CommandBuffer(cb) => {
self.previous.flush()?;
let mut builder = SubmitPresentBuilder::new();
builder.add_swapchain(
&self.swapchain,
self.image_id as u32,
self.present_region.as_ref(),
);
SubmitAnyBuilder::QueuePresent(builder)
}
SubmitAnyBuilder::BindSparse(cb) => {
self.previous.flush()?;
let mut builder = SubmitPresentBuilder::new();
builder.add_swapchain(
&self.swapchain,
self.image_id as u32,
self.present_region.as_ref(),
);
SubmitAnyBuilder::QueuePresent(builder)
}
SubmitAnyBuilder::QueuePresent(present) => {
unimplemented!()
}
})
}
#[inline]
fn flush(&self) -> Result<(), FlushError> {
unsafe {
let build_submission_result = self.build_submission();
if let &Err(FlushError::FullscreenExclusiveLost) = &build_submission_result {
self.swapchain
.fullscreen_exclusive_held
.store(false, Ordering::SeqCst);
}
match build_submission_result? {
SubmitAnyBuilder::Empty => {}
SubmitAnyBuilder::QueuePresent(present) => {
let present_result = present.submit(&self.queue);
if let &Err(SubmitPresentError::FullscreenExclusiveLost) = &present_result {
self.swapchain
.fullscreen_exclusive_held
.store(false, Ordering::SeqCst);
}
present_result?;
}
_ => unreachable!(),
}
self.flushed.store(true, Ordering::SeqCst);
Ok(())
}
}
#[inline]
unsafe fn signal_finished(&self) {
self.flushed.store(true, Ordering::SeqCst);
self.finished.store(true, Ordering::SeqCst);
self.previous.signal_finished();
}
#[inline]
fn queue_change_allowed(&self) -> bool {
false
}
#[inline]
fn queue(&self) -> Option<Arc<Queue>> {
debug_assert!(match self.previous.queue() {
None => true,
Some(q) => q.is_same(&self.queue),
});
Some(self.queue.clone())
}
#[inline]
fn check_buffer_access(
&self,
buffer: &dyn BufferAccess,
exclusive: bool,
queue: &Queue,
) -> Result<Option<(PipelineStages, AccessFlags)>, AccessCheckError> {
self.previous.check_buffer_access(buffer, exclusive, queue)
}
#[inline]
fn check_image_access(
&self,
image: &dyn ImageAccess,
layout: ImageLayout,
exclusive: bool,
queue: &Queue,
) -> Result<Option<(PipelineStages, AccessFlags)>, AccessCheckError> {
let swapchain_image = self.swapchain.raw_image(self.image_id).unwrap();
if swapchain_image.image.internal_object() == image.inner().image.internal_object() {
Err(AccessCheckError::Unknown)
} else {
self.previous
.check_image_access(image, layout, exclusive, queue)
}
}
}
unsafe impl<P, W> DeviceOwned for PresentFuture<P, W>
where
P: GpuFuture,
{
#[inline]
fn device(&self) -> &Arc<Device> {
self.queue.device()
}
}
impl<P, W> Drop for PresentFuture<P, W>
where
P: GpuFuture,
{
fn drop(&mut self) {
unsafe {
if !*self.finished.get_mut() {
match self.flush() {
Ok(()) => {
self.queue().unwrap().wait().unwrap();
self.previous.signal_finished();
}
Err(_) => {
}
}
}
}
}
}
pub struct AcquiredImage {
pub id: usize,
pub suboptimal: bool,
}
pub unsafe fn acquire_next_image_raw<W>(
swapchain: &Swapchain<W>,
timeout: Option<Duration>,
semaphore: Option<&Semaphore>,
fence: Option<&Fence>,
) -> Result<AcquiredImage, AcquireError> {
let vk = swapchain.device.pointers();
let timeout_ns = if let Some(timeout) = timeout {
timeout
.as_secs()
.saturating_mul(1_000_000_000)
.saturating_add(timeout.subsec_nanos() as u64)
} else {
u64::max_value()
};
let mut out = MaybeUninit::uninit();
let r = check_errors(vk.AcquireNextImageKHR(
swapchain.device.internal_object(),
swapchain.swapchain,
timeout_ns,
semaphore.map(|s| s.internal_object()).unwrap_or(0),
fence.map(|f| f.internal_object()).unwrap_or(0),
out.as_mut_ptr(),
))?;
let out = out.assume_init();
let (id, suboptimal) = match r {
Success::Success => (out as usize, false),
Success::Suboptimal => (out as usize, true),
Success::NotReady => return Err(AcquireError::Timeout),
Success::Timeout => return Err(AcquireError::Timeout),
s => panic!("unexpected success value: {:?}", s),
};
Ok(AcquiredImage { id, suboptimal })
}