Skip to content

Commit

Permalink
support nv12 texture for vulkan
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopengli89 committed Oct 27, 2023
1 parent 9f86d44 commit 7d8b405
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
14 changes: 14 additions & 0 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,20 @@ impl PhysicalDeviceFeatures {
supports_bgra8unorm_storage(instance, phd, caps.device_api_version),
);

features.set(
F::TEXTURE_FORMAT_NV12,
supports_format(
instance,
phd,
vk::Format::G8_B8R8_2PLANE_420_UNORM,
vk::ImageTiling::OPTIMAL,
vk::FormatFeatureFlags::SAMPLED_IMAGE
| vk::FormatFeatureFlags::STORAGE_IMAGE
| vk::FormatFeatureFlags::TRANSFER_SRC
| vk::FormatFeatureFlags::TRANSFER_DST,
),
);

(features, dl_flags)
}

Expand Down
23 changes: 18 additions & 5 deletions wgpu-hal/src/vulkan/conv.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use ash::vk;

// https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkImageAspectFlagBits.html
const VK_IMAGE_ASPECT_NONE: vk::Flags = 0;
const VK_IMAGE_ASPECT_PLANE_0_BIT: vk::Flags = 0x00000010;
const VK_IMAGE_ASPECT_PLANE_1_BIT: vk::Flags = 0x00000020;
const VK_IMAGE_ASPECT_PLANE_2_BIT: vk::Flags = 0x00000040;

impl super::PrivateCapabilities {
pub fn map_texture_format(&self, format: wgt::TextureFormat) -> vk::Format {
use ash::vk::Format as F;
Expand Down Expand Up @@ -74,7 +80,7 @@ impl super::PrivateCapabilities {
}
}
Tf::Depth16Unorm => F::D16_UNORM,
Tf::NV12 => unreachable!(),
Tf::NV12 => F::G8_B8R8_2PLANE_420_UNORM,
Tf::Rgb9e5Ufloat => F::E5B9G9R9_UFLOAT_PACK32,
Tf::Bc1RgbaUnorm => F::BC1_RGBA_UNORM_BLOCK,
Tf::Bc1RgbaUnormSrgb => F::BC1_RGBA_SRGB_BLOCK,
Expand Down Expand Up @@ -402,7 +408,7 @@ pub fn map_vertex_format(vertex_format: wgt::VertexFormat) -> vk::Format {
}
}

pub fn map_aspects(aspects: crate::FormatAspects) -> vk::ImageAspectFlags {
pub fn map_aspects(aspects: crate::FormatAspects, plane: Option<u32>) -> vk::ImageAspectFlags {
let mut flags = vk::ImageAspectFlags::empty();
if aspects.contains(crate::FormatAspects::COLOR) {
flags |= vk::ImageAspectFlags::COLOR;
Expand All @@ -413,6 +419,12 @@ pub fn map_aspects(aspects: crate::FormatAspects) -> vk::ImageAspectFlags {
if aspects.contains(crate::FormatAspects::STENCIL) {
flags |= vk::ImageAspectFlags::STENCIL;
}
flags |= vk::ImageAspectFlags::from_raw(match plane {
Some(0) => VK_IMAGE_ASPECT_PLANE_0_BIT,
Some(1) => VK_IMAGE_ASPECT_PLANE_1_BIT,
Some(2) => VK_IMAGE_ASPECT_PLANE_2_BIT,
_ => VK_IMAGE_ASPECT_NONE,
});
flags
}

Expand Down Expand Up @@ -588,9 +600,10 @@ pub fn map_copy_extent(extent: &crate::CopyExtent) -> vk::Extent3D {
pub fn map_subresource_range(
range: &wgt::ImageSubresourceRange,
format: wgt::TextureFormat,
plane: Option<u32>,
) -> vk::ImageSubresourceRange {
vk::ImageSubresourceRange {
aspect_mask: map_aspects(crate::FormatAspects::new(format, range.aspect)),
aspect_mask: map_aspects(crate::FormatAspects::new(format, range.aspect), plane),
base_mip_level: range.base_mip_level,
level_count: range.mip_level_count.unwrap_or(vk::REMAINING_MIP_LEVELS),
base_array_layer: range.base_array_layer,
Expand All @@ -607,7 +620,7 @@ pub(super) fn map_subresource_range_combined_aspect(
format: wgt::TextureFormat,
private_caps: &super::PrivateCapabilities,
) -> vk::ImageSubresourceRange {
let mut range = map_subresource_range(range, format);
let mut range = map_subresource_range(range, format, None);
if !private_caps.texture_s8 && format == wgt::TextureFormat::Stencil8 {
range.aspect_mask |= vk::ImageAspectFlags::DEPTH;
}
Expand All @@ -623,7 +636,7 @@ pub fn map_subresource_layers(
z: base.origin.z as i32,
};
let subresource = vk::ImageSubresourceLayers {
aspect_mask: map_aspects(base.aspect),
aspect_mask: map_aspects(base.aspect, None),
mip_level: base.mip_level,
base_array_layer: base.array_layer,
layer_count: 1,
Expand Down
3 changes: 2 additions & 1 deletion wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,8 @@ impl crate::Device<super::Api> for super::Device {
texture: &super::Texture,
desc: &crate::TextureViewDescriptor,
) -> Result<super::TextureView, crate::DeviceError> {
let subresource_range = conv::map_subresource_range(&desc.range, desc.format);
let subresource_range =
conv::map_subresource_range(&desc.range, desc.format, Some(desc.plane));
let mut vk_info = vk::ImageViewCreateInfo::builder()
.flags(vk::ImageViewCreateFlags::empty())
.image(texture.raw)
Expand Down
1 change: 1 addition & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ bitflags::bitflags! {
///
/// Supported platforms:
/// - DX12
/// - Vulkan
///
/// This is a native only feature.
const TEXTURE_FORMAT_NV12 = 1 << 55;
Expand Down

0 comments on commit 7d8b405

Please sign in to comment.