Skip to content

Commit

Permalink
Handle vulkan backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jan 22, 2023
1 parent 895100a commit d30bc2c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
24 changes: 18 additions & 6 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5261,12 +5261,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Err(_) => break E::InvalidSurface,
};

for format in config.view_formats.iter() {
if config.format.remove_srgb_suffix() != format.remove_srgb_suffix() {
break 'outter E::InvalidViewFormat(*format, config.format);
}
}

let caps = unsafe {
let suf = A::get_surface(surface);
let adapter = &adapter_guard[device.adapter_id.value];
Expand All @@ -5276,6 +5270,23 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}
};

let mut hal_view_formats = vec![config.format];
for format in config.view_formats.iter() {
if *format == config.format {
continue;
}
if !caps.formats.contains(&config.format) {
break 'outter E::UnsupportedFormat {
requested: config.format,
available: caps.formats.clone(),
};
}
if config.format.remove_srgb_suffix() != format.remove_srgb_suffix() {
break 'outter E::InvalidViewFormat(*format, config.format);
}
hal_view_formats.push(*format);
}

let num_frames = present::DESIRED_NUM_FRAMES
.clamp(*caps.swap_chain_sizes.start(), *caps.swap_chain_sizes.end());
let mut hal_config = hal::SurfaceConfiguration {
Expand All @@ -5289,6 +5300,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
depth_or_array_layers: 1,
},
usage: conv::map_texture_usage(config.usage, hal::FormatAspects::COLOR),
view_formats: hal_view_formats,
};

if let Err(error) = validate_surface_configuration(&mut hal_config, &caps) {
Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl<A: hal::Api> Example<A> {
depth_or_array_layers: 1,
},
usage: hal::TextureUses::COLOR_TARGET,
view_formats: vec![],
};
unsafe {
surface.configure(&device, &surface_config).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,9 @@ pub struct SurfaceConfiguration {
pub extent: wgt::Extent3d,
/// Allowed usage of surface textures,
pub usage: TextureUses,
/// Allows views of swapchain texture to have a different format
/// than the texture does.
pub view_formats: Vec<wgt::TextureFormat>,
}

#[derive(Debug, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ impl PhysicalDeviceCapabilities {

// Require `VK_KHR_swapchain`
extensions.push(vk::KhrSwapchainFn::name());
// `KhrSwapchainMutableFormatFn` extension is requires support for Vulkan 1.0:
// https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_swapchain_mutable_format.html
extensions.push(vk::KhrSwapchainMutableFormatFn::name());

if self.effective_api_version < vk::API_VERSION_1_1 {
// Require either `VK_KHR_maintenance1` or `VK_AMD_negative_viewport_height`
Expand Down
27 changes: 25 additions & 2 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,25 @@ impl super::Device {
} else {
vk::ColorSpaceKHR::SRGB_NONLINEAR
};
let info = vk::SwapchainCreateInfoKHR::builder()
.flags(vk::SwapchainCreateFlagsKHR::empty())

let mut raw_flags = vk::SwapchainCreateFlagsKHR::empty();
let mut raw_view_formats: Vec<vk::Format> = vec![];
if config.view_formats.len() > 1
&& (self.shared_instance().driver_api_version >= vk::API_VERSION_1_2
|| self
.enabled_device_extensions()
.contains(&vk::KhrImageFormatListFn::name()))
{
raw_flags |= vk::SwapchainCreateFlagsKHR::MUTABLE_FORMAT;
raw_view_formats = config
.view_formats
.iter()
.map(|f| self.shared.private_caps.map_texture_format(*f))
.collect();
}

let mut info = vk::SwapchainCreateInfoKHR::builder()
.flags(raw_flags)
.surface(surface.raw)
.min_image_count(config.swap_chain_size)
.image_format(self.shared.private_caps.map_texture_format(config.format))
Expand All @@ -565,6 +582,12 @@ impl super::Device {
.clipped(true)
.old_swapchain(old_swapchain);

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

let result = {
profiling::scope!("vkCreateSwapchainKHR");
unsafe { functor.create_swapchain(&info, None) }
Expand Down

0 comments on commit d30bc2c

Please sign in to comment.