Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constness of ImageUsage constructors and more utility methods for SampleCounts #1924

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion vulkano/src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use crate::memory::ExternalMemoryHandleType;
use crate::memory::ExternalMemoryProperties;
use crate::DeviceSize;
use std::cmp;
use std::ops::BitAnd;
use std::ops::Range;

mod aspect;
Expand Down Expand Up @@ -155,7 +156,7 @@ pub struct SampleCounts {
impl SampleCounts {
/// Returns true if `self` has the `sample_count` value set.
#[inline]
pub fn contains(&self, sample_count: SampleCount) -> bool {
pub const fn contains(&self, sample_count: SampleCount) -> bool {
match sample_count {
SampleCount::Sample1 => self.sample1,
SampleCount::Sample2 => self.sample2,
Expand All @@ -166,6 +167,51 @@ impl SampleCounts {
SampleCount::Sample64 => self.sample64,
}
}

/// Returns the intersection of these sample counts and another set of sample counts.
///
/// # Examples
///
/// If you're using both a color and depth buffer, and want to use multisampling, then you
/// should check the intersection of the supported sample counts because they don't have to
/// match. You could similarily apply this to the stencil counts.
/// ```no_run
/// # use vulkano::instance::Instance;
/// # use vulkano::device::physical::PhysicalDevice;
/// # let instance = Instance::new(Default::default()).unwrap();
/// # let physical_device = PhysicalDevice::from_index(&instance, 0).unwrap();
/// let properties = physical_device.properties();
/// let color_counts = properties.framebuffer_color_sample_counts;
/// let depth_counts = properties.framebuffer_depth_sample_counts;
///
/// let counts = color_counts.intersection(&depth_counts);
/// ```
#[inline]
pub const fn intersection(&self, other: &SampleCounts) -> SampleCounts {
SampleCounts {
sample1: self.sample1 && other.sample1,
sample2: self.sample2 && other.sample2,
sample4: self.sample4 && other.sample4,
sample8: self.sample8 && other.sample8,
sample16: self.sample16 && other.sample16,
sample32: self.sample32 && other.sample32,
sample64: self.sample64 && other.sample64,
}
}

/// Returns the maximum sample count supported by `self`.
#[inline]
pub const fn max_count(&self) -> SampleCount {
match self {
Self { sample64: true, .. } => SampleCount::Sample64,
Self { sample32: true, .. } => SampleCount::Sample32,
Self { sample16: true, .. } => SampleCount::Sample16,
Self { sample8: true, .. } => SampleCount::Sample8,
Self { sample4: true, .. } => SampleCount::Sample4,
Self { sample2: true, .. } => SampleCount::Sample2,
_ => SampleCount::Sample1,
}
}
}

impl From<ash::vk::SampleCountFlags> for SampleCounts {
Expand Down Expand Up @@ -212,6 +258,22 @@ impl From<SampleCounts> for ash::vk::SampleCountFlags {
}
}

impl BitAnd for SampleCounts {
type Output = SampleCounts;

fn bitand(self, rhs: Self) -> Self::Output {
SampleCounts {
sample1: self.sample1 && rhs.sample1,
sample2: self.sample2 && rhs.sample2,
sample4: self.sample4 && rhs.sample4,
sample8: self.sample8 && rhs.sample8,
sample16: self.sample16 && rhs.sample16,
sample32: self.sample32 && rhs.sample32,
sample64: self.sample64 && rhs.sample64,
}
}
}

/// Specifies how many mipmaps must be allocated.
///
/// Note that at least one mipmap must be allocated, to store the main level of the image.
Expand Down
14 changes: 7 additions & 7 deletions vulkano/src/image/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl ImageUsage {
/// Builds a `ImageUsage` with all values set to true. Note that using the returned value will
/// produce an error because of `transient_attachment` being true.
#[inline]
pub fn all() -> ImageUsage {
pub const fn all() -> ImageUsage {
ImageUsage {
transfer_src: true,
transfer_dst: true,
Expand All @@ -79,7 +79,7 @@ impl ImageUsage {
/// };
/// ```
#[inline]
pub fn none() -> ImageUsage {
pub const fn none() -> ImageUsage {
ImageUsage {
transfer_src: false,
transfer_dst: false,
Expand All @@ -94,7 +94,7 @@ impl ImageUsage {

/// Builds a ImageUsage with color_attachment set to true and the rest to false.
#[inline]
pub fn color_attachment() -> ImageUsage {
pub const fn color_attachment() -> ImageUsage {
ImageUsage {
transfer_src: false,
transfer_dst: false,
Expand All @@ -109,7 +109,7 @@ impl ImageUsage {

/// Builds a ImageUsage with depth_stencil_attachment set to true and the rest to false.
#[inline]
pub fn depth_stencil_attachment() -> ImageUsage {
pub const fn depth_stencil_attachment() -> ImageUsage {
ImageUsage {
transfer_src: false,
transfer_dst: false,
Expand All @@ -124,7 +124,7 @@ impl ImageUsage {

/// Builds a ImageUsage with color_attachment and transient_attachment set to true and the rest to false.
#[inline]
pub fn transient_color_attachment() -> ImageUsage {
pub const fn transient_color_attachment() -> ImageUsage {
ImageUsage {
transfer_src: false,
transfer_dst: false,
Expand All @@ -139,7 +139,7 @@ impl ImageUsage {

/// Builds a ImageUsage with depth_stencil_attachment and transient_attachment set to true and the rest to false.
#[inline]
pub fn transient_depth_stencil_attachment() -> ImageUsage {
pub const fn transient_depth_stencil_attachment() -> ImageUsage {
ImageUsage {
transfer_src: false,
transfer_dst: false,
Expand All @@ -154,7 +154,7 @@ impl ImageUsage {

/// Builds a ImageUsage with input_attachment and transient_attachment set to true and the rest to false.
#[inline]
pub fn transient_input_attachment() -> ImageUsage {
pub const fn transient_input_attachment() -> ImageUsage {
ImageUsage {
transfer_src: false,
transfer_dst: false,
Expand Down