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

Add support for image format lists #2287

Merged
merged 2 commits into from
Aug 23, 2023
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
20 changes: 20 additions & 0 deletions vulkano/src/device/physical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,7 @@ impl PhysicalDevice {
external_memory_handle_type,
image_view_type,
ref drm_format_modifier_info,
ref view_formats,
_ne: _,
} = image_format_info;

Expand All @@ -1042,6 +1043,8 @@ impl PhysicalDevice {
};
let mut drm_format_modifier_info_vk = None;
let mut external_info_vk = None;
let mut format_list_info_vk = None;
let format_list_view_formats_vk: Vec<_>;
let mut image_view_info_vk = None;
let mut stencil_usage_info_vk = None;

Expand Down Expand Up @@ -1087,6 +1090,23 @@ impl PhysicalDevice {
info2_vk.p_next = next as *const _ as *const _;
}

if !view_formats.is_empty() {
format_list_view_formats_vk = view_formats
.iter()
.copied()
.map(ash::vk::Format::from)
.collect();

let next = format_list_info_vk.insert(ash::vk::ImageFormatListCreateInfo {
view_format_count: format_list_view_formats_vk.len() as u32,
p_view_formats: format_list_view_formats_vk.as_ptr(),
..Default::default()
});

next.p_next = info2_vk.p_next;
info2_vk.p_next = next as *const _ as *const _;
}

if let Some(image_view_type) = image_view_type {
let next = image_view_info_vk.insert(
ash::vk::PhysicalDeviceImageViewImageFormatInfoEXT {
Expand Down
157 changes: 103 additions & 54 deletions vulkano/src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ impl Image {
swapchain: Arc<Swapchain>,
image_index: u32,
) -> Result<Self, VulkanError> {
// Per https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/vkCreateSwapchainKHR.html#_description
let create_info = ImageCreateInfo {
flags: ImageCreateFlags::empty(),
flags: swapchain.flags().into(),
image_type: ImageType::Dim2d,
format: swapchain.image_format(),
view_formats: swapchain.image_view_formats().to_vec(),
extent: [swapchain.image_extent()[0], swapchain.image_extent()[1], 1],
array_layers: swapchain.image_array_layers(),
mip_levels: 1,
Expand All @@ -220,7 +222,10 @@ impl Image {
stencil_usage: None,
sharing: swapchain.image_sharing().clone(),
initial_layout: ImageLayout::Undefined,
..Default::default()
drm_format_modifiers: Vec::new(),
drm_format_modifier_plane_layouts: Vec::new(),
external_memory_handle_types: ExternalMemoryHandleTypes::empty(),
_ne: crate::NonExhaustive(()),
};

Ok(Self::from_raw(
Expand Down Expand Up @@ -279,6 +284,12 @@ impl Image {
self.inner.format_features()
}

/// Returns the formats that an image view created from this image can have.
#[inline]
pub fn view_formats(&self) -> &[Format] {
self.inner.view_formats()
}

/// Returns the extent of the image.
#[inline]
pub fn extent(&self) -> [u32; 3] {
Expand Down Expand Up @@ -971,7 +982,7 @@ impl FusedIterator for SubresourceRangeIterator {}
vulkan_bitflags! {
#[non_exhaustive]

/// Flags that can be set when creating a new image.
/// Flags specifying additional properties of an image.
ImageCreateFlags = ImageCreateFlags(u32);

/* TODO: enable
Expand Down Expand Up @@ -1647,6 +1658,16 @@ pub struct ImageFormatInfo {
/// The default value is `Format::UNDEFINED`.
pub format: Format,

/// The image view formats that will be allowed for the image.
///
/// If this is not empty, then the physical device API version must be at least 1.2, or the
/// [`khr_image_format_list`] extension must be supported by the physical device.
///
/// The default value is empty.
///
/// [`khr_image_format_list`]: crate::device::DeviceExtensions::khr_image_format_list
pub view_formats: Vec<Format>,

/// The dimension type that the image will have.
///
/// The default value is [`ImageType::Dim2d`].
Expand All @@ -1671,6 +1692,15 @@ pub struct ImageFormatInfo {
/// The default value is `None`.
pub stencil_usage: Option<ImageUsage>,

/// The Linux DRM format modifier information to query.
///
/// If this is `Some`, then the
/// [`ext_image_drm_format_modifier`](crate::device::DeviceExtensions::ext_image_drm_format_modifier)
/// extension must be supported by the physical device.
///
/// The default value is `None`.
pub drm_format_modifier_info: Option<ImageDrmFormatModifierInfo>,

/// An external memory handle type that will be imported to or exported from the image.
///
/// This is needed to retrieve the
Expand All @@ -1693,15 +1723,6 @@ pub struct ImageFormatInfo {
/// The default value is `None`.
pub image_view_type: Option<ImageViewType>,

/// The Linux DRM format modifier information to query.
///
/// If this is `Some`, then the
/// [`ext_image_drm_format_modifier`](crate::device::DeviceExtensions::ext_image_drm_format_modifier)
/// extension must be supported by the physical device.
///
/// The default value is `None`.
pub drm_format_modifier_info: Option<ImageDrmFormatModifierInfo>,

pub _ne: crate::NonExhaustive,
}

Expand All @@ -1711,13 +1732,14 @@ impl Default for ImageFormatInfo {
Self {
flags: ImageCreateFlags::empty(),
format: Format::UNDEFINED,
view_formats: Vec::new(),
image_type: ImageType::Dim2d,
tiling: ImageTiling::Optimal,
usage: ImageUsage::empty(),
stencil_usage: None,
drm_format_modifier_info: None,
external_memory_handle_type: None,
image_view_type: None,
drm_format_modifier_info: None,
_ne: crate::NonExhaustive(()),
}
}
Expand All @@ -1731,13 +1753,14 @@ impl ImageFormatInfo {
let &Self {
flags,
format,
ref view_formats,
image_type,
tiling,
usage,
stencil_usage,
ref drm_format_modifier_info,
external_memory_handle_type,
image_view_type,
ref drm_format_modifier_info,
_ne: _,
} = self;

Expand Down Expand Up @@ -1837,52 +1860,29 @@ impl ImageFormatInfo {
}
}

if let Some(handle_type) = external_memory_handle_type {
if !(physical_device.api_version() >= Version::V1_1
|| physical_device
.instance()
.enabled_extensions()
.khr_external_memory_capabilities)
if !view_formats.is_empty() {
if !(physical_device.api_version() >= Version::V1_2
|| physical_device.supported_extensions().khr_image_format_list)
{
return Err(Box::new(ValidationError {
problem: "`external_memory_handle_type` is `Some`".into(),
context: "view_formats".into(),
problem: "is not empty".into(),
requires_one_of: RequiresOneOf(&[
RequiresAllOf(&[Requires::APIVersion(Version::V1_1)]),
RequiresAllOf(&[Requires::InstanceExtension(
"khr_external_memory_capabilities",
)]),
RequiresAllOf(&[Requires::APIVersion(Version::V1_2)]),
RequiresAllOf(&[Requires::DeviceExtension("khr_image_format_list")]),
]),
..Default::default()
}));
}

handle_type
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context("handle_type").set_vuids(&[
"VUID-VkPhysicalDeviceExternalImageFormatInfo-handleType-parameter",
])
})?;
}

if let Some(image_view_type) = image_view_type {
if !physical_device.supported_extensions().ext_filter_cubic {
return Err(Box::new(ValidationError {
problem: "`image_view_type` is `Some`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"ext_filter_cubic",
)])]),
..Default::default()
}));
for (index, view_format) in view_formats.iter().enumerate() {
view_format
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context(format!("view_formats[{}]", index))
.set_vuids(&["VUID-VkImageFormatListCreateInfo-pViewFormats-parameter"])
})?;
}

image_view_type
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context("image_view_type").set_vuids(&[
"VUID-VkPhysicalDeviceImageViewImageFormatInfoEXT-imageViewType-parameter",
])
})?;
}

if let Some(drm_format_modifier_info) = drm_format_modifier_info {
Expand Down Expand Up @@ -1914,10 +1914,11 @@ impl ImageFormatInfo {
}));
}

if flags.intersects(ImageCreateFlags::MUTABLE_FORMAT) {
if flags.intersects(ImageCreateFlags::MUTABLE_FORMAT) && view_formats.is_empty() {
return Err(Box::new(ValidationError {
problem: "`tiling` is `ImageTiling::DrmFormatModifier`, but \
`flags` contains `ImageCreateFlags::MUTABLE_FORMAT`"
problem: "`tiling` is `ImageTiling::DrmFormatModifier`, and \
`flags` contains `ImageCreateFlags::MUTABLE_FORMAT`, but \
`view_formats` is empty"
.into(),
vuids: &["VUID-VkPhysicalDeviceImageFormatInfo2-tiling-02313"],
..Default::default()
Expand All @@ -1933,6 +1934,54 @@ impl ImageFormatInfo {
}));
}

if let Some(handle_type) = external_memory_handle_type {
if !(physical_device.api_version() >= Version::V1_1
|| physical_device
.instance()
.enabled_extensions()
.khr_external_memory_capabilities)
{
return Err(Box::new(ValidationError {
problem: "`external_memory_handle_type` is `Some`".into(),
requires_one_of: RequiresOneOf(&[
RequiresAllOf(&[Requires::APIVersion(Version::V1_1)]),
RequiresAllOf(&[Requires::InstanceExtension(
"khr_external_memory_capabilities",
)]),
]),
..Default::default()
}));
}

handle_type
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context("handle_type").set_vuids(&[
"VUID-VkPhysicalDeviceExternalImageFormatInfo-handleType-parameter",
])
})?;
}

if let Some(image_view_type) = image_view_type {
if !physical_device.supported_extensions().ext_filter_cubic {
return Err(Box::new(ValidationError {
problem: "`image_view_type` is `Some`".into(),
requires_one_of: RequiresOneOf(&[RequiresAllOf(&[Requires::DeviceExtension(
"ext_filter_cubic",
)])]),
..Default::default()
}));
}

image_view_type
.validate_physical_device(physical_device)
.map_err(|err| {
err.add_context("image_view_type").set_vuids(&[
"VUID-VkPhysicalDeviceImageViewImageFormatInfoEXT-imageViewType-parameter",
])
})?;
}

Ok(())
}
}
Expand Down
Loading