Skip to content

Commit

Permalink
vk: improve view_formats setting (#3412)
Browse files Browse the repository at this point in the history
* vk: improve view_formats setting

* Fix extension detection

* Update wgpu-hal/src/vulkan/device.rs

Co-authored-by: Teodor Tanasoaia <28601907+teoxoy@users.noreply.github.com>

* Follow the suggestion

* Tiny doc fix

* Follow the suggestion

Co-authored-by: Teodor Tanasoaia <28601907+teoxoy@users.noreply.github.com>
  • Loading branch information
jinleili and teoxoy authored Jan 23, 2023
1 parent 5acb706 commit 4cd753b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
6 changes: 3 additions & 3 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,15 @@ impl<A: HalApi> Device<A> {
));
}

let mut allow_different_view_format = false;
let mut hal_view_formats = vec![];
for format in desc.view_formats.iter() {
if desc.format == *format {
continue;
}
if desc.format.remove_srgb_suffix() != format.remove_srgb_suffix() {
return Err(CreateTextureError::InvalidViewFormat(*format, desc.format));
}
allow_different_view_format = true;
hal_view_formats.push(*format);
}

// Enforce having COPY_DST/DEPTH_STENCIL_WRIT/COLOR_TARGET otherwise we
Expand Down Expand Up @@ -893,7 +893,7 @@ impl<A: HalApi> Device<A> {
format: desc.format,
usage: hal_usage,
memory_flags: hal::MemoryFlags::empty(),
allow_different_view_format,
view_formats: hal_view_formats,
};

let raw_texture = unsafe {
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl<A: hal::Api> Example<A> {
format: wgt::TextureFormat::Rgba8UnormSrgb,
usage: hal::TextureUses::COPY_DST | hal::TextureUses::RESOURCE,
memory_flags: hal::MemoryFlags::empty(),
allow_different_view_format: false,
view_formats: vec![],
};
let texture = unsafe { device.create_texture(&texture_desc).unwrap() };

Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,8 @@ pub struct TextureDescriptor<'a> {
pub usage: TextureUses,
pub memory_flags: MemoryFlags,
/// Allows views of this texture to have a different format
/// than the this texture does.
pub allow_different_view_format: bool,
/// than the texture does.
pub view_formats: Vec<wgt::TextureFormat>,
}

/// TextureView descriptor.
Expand Down
7 changes: 5 additions & 2 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,14 @@ impl PhysicalDeviceCapabilities {
}

if self.effective_api_version < vk::API_VERSION_1_2 {
// Optional `VK_KHR_image_format_list`
if self.supports_extension(vk::KhrImageFormatListFn::name()) {
extensions.push(vk::KhrImageFormatListFn::name());
}

// Optional `VK_KHR_imageless_framebuffer`
if self.supports_extension(vk::KhrImagelessFramebufferFn::name()) {
extensions.push(vk::KhrImagelessFramebufferFn::name());
// Require `VK_KHR_image_format_list` due to it being a dependency
extensions.push(vk::KhrImageFormatListFn::name());
// Require `VK_KHR_maintenance2` due to it being a dependency
if self.effective_api_version < vk::API_VERSION_1_1 {
extensions.push(vk::KhrMaintenance2Fn::name());
Expand Down
26 changes: 23 additions & 3 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,14 +895,28 @@ impl crate::Device<super::Api> for super::Device {
raw_flags |= vk::ImageCreateFlags::CUBE_COMPATIBLE;
}

if desc.allow_different_view_format {
let original_format = self.shared.private_caps.map_texture_format(desc.format);
let mut hal_view_formats: Vec<vk::Format> = vec![];
if !desc.view_formats.is_empty() {
raw_flags |= vk::ImageCreateFlags::MUTABLE_FORMAT;
if self.shared_instance().driver_api_version >= vk::API_VERSION_1_2
|| self
.enabled_device_extensions()
.contains(&vk::KhrImageFormatListFn::name())
{
hal_view_formats = desc
.view_formats
.iter()
.map(|f| self.shared.private_caps.map_texture_format(*f))
.collect();
hal_view_formats.push(original_format)
}
}

let vk_info = vk::ImageCreateInfo::builder()
let mut vk_info = vk::ImageCreateInfo::builder()
.flags(raw_flags)
.image_type(conv::map_texture_dimension(desc.dimension))
.format(self.shared.private_caps.map_texture_format(desc.format))
.format(original_format)
.extent(vk::Extent3D {
width: copy_size.width,
height: copy_size.height,
Expand All @@ -916,6 +930,12 @@ impl crate::Device<super::Api> for super::Device {
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.initial_layout(vk::ImageLayout::UNDEFINED);

let mut format_list_info = vk::ImageFormatListCreateInfo::builder();
if !hal_view_formats.is_empty() {
format_list_info = format_list_info.view_formats(&hal_view_formats);
vk_info = vk_info.push_next(&mut format_list_info);
}

let raw = unsafe { self.shared.raw.create_image(&vk_info, None)? };
let req = unsafe { self.shared.raw.get_image_memory_requirements(raw) };

Expand Down

0 comments on commit 4cd753b

Please sign in to comment.