Add SupportedPresentModes and SupportedPresentModesIter

This commit is contained in:
Pierre Krieger 2016-05-03 10:54:50 +02:00
parent b5e64bd967
commit cd7cda9d2d
5 changed files with 73 additions and 16 deletions

View File

@ -58,7 +58,7 @@ fn main() {
let caps = window.surface().get_capabilities(&physical).expect("failed to get surface capabilities");
let dimensions = caps.current_extent.unwrap_or([1280, 1024]);
let present = caps.present_modes[0];
let present = caps.present_modes.iter().next().unwrap();
let usage = caps.supported_usage_flags;
vulkano::swapchain::Swapchain::new(&device, &window.surface(), 3,

View File

@ -63,7 +63,7 @@ fn main() {
let caps = window.surface().get_capabilities(&physical).expect("failed to get surface capabilities");
let dimensions = caps.current_extent.unwrap_or([1280, 1024]);
let present = caps.present_modes[0];
let present = caps.present_modes.iter().next().unwrap();
let usage = caps.supported_usage_flags;
let format = caps.supported_formats[0].0;

View File

@ -173,7 +173,7 @@ fn main() {
// The present mode determines the way the images will be presented on the screen. This
// includes things such as vsync and will affect the framerate of your application. We just
// use the first supported value, but you probably want to leave that choice to the user.
let present = caps.present_modes[0];
let present = caps.present_modes.iter().next().unwrap();
// The alpha mode indicates how the alpha value of the final image will behave. For example
// you can choose whether the window will be opaque or transparent.

View File

@ -386,7 +386,7 @@ impl Surface {
));
modes.set_len(num as usize);
debug_assert!(modes.iter().find(|&&m| m == vk::PRESENT_MODE_FIFO_KHR).is_some());
modes
SupportedPresentModes::from_list(modes.into_iter())
};
Ok(Capabilities {
@ -412,7 +412,7 @@ impl Surface {
supported_formats: formats.into_iter().map(|f| {
(Format::from_num(f.format).unwrap(), ColorSpace::from_num(f.colorSpace))
}).collect(),
present_modes: modes.into_iter().map(|mode| PresentMode::from_num(mode)).collect(),
present_modes: modes,
})
}
}
@ -514,7 +514,7 @@ pub struct Capabilities {
pub supported_formats: Vec<(Format, ColorSpace)>, // FIXME: driver can return FORMAT_UNDEFINED which indicates that it has no preferred format, so that field should be an Option
/// List of present modes that are supported. `Fifo` is always guaranteed to be supported.
pub present_modes: Vec<PresentMode>,
pub present_modes: SupportedPresentModes,
}
/// The way presenting a swapchain is accomplished.
@ -544,18 +544,75 @@ pub enum PresentMode {
Relaxed = vk::PRESENT_MODE_FIFO_RELAXED_KHR,
}
impl PresentMode {
/// Panicks if the mode is unrecognized.
/// List of `PresentMode`s that are supported.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct SupportedPresentModes {
pub immediate: bool,
pub mailbox: bool,
pub fifo: bool,
pub relaxed: bool,
}
impl SupportedPresentModes {
/// Builds a `SupportedPresentModes` with all fields set to false.
#[inline]
fn from_num(num: u32) -> PresentMode {
match num {
vk::PRESENT_MODE_IMMEDIATE_KHR => PresentMode::Immediate,
vk::PRESENT_MODE_MAILBOX_KHR => PresentMode::Mailbox,
vk::PRESENT_MODE_FIFO_KHR => PresentMode::Fifo,
vk::PRESENT_MODE_FIFO_RELAXED_KHR => PresentMode::Relaxed,
m => panic!("unrecognized present mode: {:?}", m)
pub fn none() -> SupportedPresentModes {
SupportedPresentModes {
immediate: false,
mailbox: false,
fifo: false,
relaxed: false,
}
}
#[inline]
fn from_list<I>(elem: I) -> SupportedPresentModes where I: Iterator<Item = vk::PresentModeKHR> {
let mut result = SupportedPresentModes::none();
for e in elem {
match e {
vk::PRESENT_MODE_IMMEDIATE_KHR => result.immediate = true,
vk::PRESENT_MODE_MAILBOX_KHR => result.mailbox = true,
vk::PRESENT_MODE_FIFO_KHR => result.fifo = true,
vk::PRESENT_MODE_FIFO_RELAXED_KHR => result.relaxed = true,
_ => panic!("Wrong value for vk::PresentModeKHR")
}
}
result
}
/// Returns true if the given present mode is in this list of supported modes.
#[inline]
pub fn supports(&self, mode: PresentMode) -> bool {
match mode {
PresentMode::Immediate => self.immediate,
PresentMode::Mailbox => self.mailbox,
PresentMode::Fifo => self.fifo,
PresentMode::Relaxed => self.relaxed,
}
}
/// Returns an iterator to the list of supported present modes.
#[inline]
pub fn iter(&self) -> SupportedPresentModesIter {
SupportedPresentModesIter(self.clone())
}
}
/// Enumeration of the `PresentMode`s that are supported.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct SupportedPresentModesIter(SupportedPresentModes);
impl Iterator for SupportedPresentModesIter {
type Item = PresentMode;
#[inline]
fn next(&mut self) -> Option<PresentMode> {
if self.0.immediate { self.0.immediate = false; return Some(PresentMode::Immediate); }
if self.0.mailbox { self.0.mailbox = false; return Some(PresentMode::Mailbox); }
if self.0.fifo { self.0.fifo = false; return Some(PresentMode::Fifo); }
if self.0.relaxed { self.0.relaxed = false; return Some(PresentMode::Relaxed); }
None
}
}
/// A transformation to apply to the image before showing it on the screen.

View File

@ -122,7 +122,7 @@ impl Swapchain {
assert!((usage.to_usage_bits() & capabilities.supported_usage_flags.to_usage_bits()) == usage.to_usage_bits());
assert!(capabilities.supported_transforms.iter().find(|&&t| t == transform).is_some());
assert!(capabilities.supported_composite_alpha.iter().find(|&&a| a == alpha).is_some());
assert!(capabilities.present_modes.iter().find(|&&m| m == mode).is_some());
assert!(capabilities.present_modes.supports(mode));
// FIXME: check that the device and the surface belong to the same instance
let vk = device.pointers();