Vendor wgpu-hal 30.0.1 unchanged (examples left out)

The crates.io sources of wgpu-hal 30.0.1, minus its examples, lock file and
VCS metadata, so the next commit can apply one upstream fix that has no
release yet. Excluded from the workspace: it keeps its own lints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-19 06:40:35 +02:00
parent 21ac62a085
commit 79f78f904b
78 changed files with 53637 additions and 1 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+339
View File
@@ -0,0 +1,339 @@
use alloc::vec::Vec;
use arrayvec::ArrayVec;
use ash::vk;
use hashbrown::{HashMap, HashSet};
const POOL_MIN_SETS: u32 = 64;
const POOL_MAX_SETS: u32 = 512;
/// The dynamic counts are tracked separately since dynamic textures/buffers
/// have distinct descriptor types compared to their non-dynamic counterparts.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub struct DescriptorCounts {
pub sampler: u32,
pub sampled_image: u32,
pub storage_image: u32,
pub uniform_buffer: u32,
pub uniform_buffer_dynamic: u32,
pub storage_buffer: u32,
pub storage_buffer_dynamic: u32,
pub acceleration_structure: u32,
}
impl DescriptorCounts {
fn total(&self) -> u32 {
self.sampler
+ self.sampled_image
+ self.storage_image
+ self.uniform_buffer
+ self.uniform_buffer_dynamic
+ self.storage_buffer
+ self.storage_buffer_dynamic
+ self.acceleration_structure
}
}
#[derive(Debug)]
pub struct DescriptorSet {
raw: vk::DescriptorSet,
bucket_key: BucketKey,
pool_index: usize,
}
impl DescriptorSet {
pub fn raw(&self) -> vk::DescriptorSet {
self.raw
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
struct BucketKey {
counts: DescriptorCounts,
update_after_bind: bool,
}
struct Pool {
raw: vk::DescriptorPool,
capacity: u32,
available: u32,
}
/// Keeps track of all pools created with this bucket's [`BucketKey`].
#[derive(Default)]
struct Bucket {
/// We keep track of all descriptor set layouts that might use this bucket.
/// If the set becomes empty, the bucket is destroyed.
layouts: HashSet<vk::DescriptorSetLayout>,
pools: Vec<Pool>,
available_sets: u32,
allocated_sets: u32,
}
impl Bucket {
fn create_pool(
&mut self,
device: &ash::Device,
key: &BucketKey,
capacity_hint: u32,
) -> Result<(usize, &mut Pool), crate::DeviceError> {
let index = self.pools.len();
let pool = create_descriptor_pool(device, key, capacity_hint)?;
self.available_sets += pool.capacity;
self.pools.push(pool);
Ok((index, self.pools.last_mut().unwrap()))
}
}
pub struct DescriptorAllocator {
buckets: HashMap<BucketKey, Bucket>,
max_update_after_bind_descriptors_in_all_pools: u32,
update_after_bind_descriptors_in_all_pools: u32,
}
impl super::BindGroupLayout {
fn bucket_key(&self) -> BucketKey {
let update_after_bind = self.contains_binding_arrays;
let counts = self.desc_count.clone();
BucketKey {
counts,
update_after_bind,
}
}
}
impl DescriptorAllocator {
pub fn new(max_update_after_bind_descriptors_in_all_pools: u32) -> Self {
DescriptorAllocator {
buckets: HashMap::default(),
max_update_after_bind_descriptors_in_all_pools,
update_after_bind_descriptors_in_all_pools: 0,
}
}
pub fn register_layout(
&mut self,
device: &ash::Device,
layout: &super::BindGroupLayout,
) -> Result<(), crate::DeviceError> {
let key = layout.bucket_key();
let bucket = match self.buckets.entry(key) {
hashbrown::hash_map::Entry::Occupied(occupied_entry) => occupied_entry.into_mut(),
hashbrown::hash_map::Entry::Vacant(vacant_entry) => {
let mut bucket = Bucket::default();
// Create at least 1 pool upfront instead of doing this when
// creating the first bind group.
bucket.create_pool(device, vacant_entry.key(), POOL_MIN_SETS)?;
vacant_entry.insert(bucket)
}
};
assert!(bucket.layouts.insert(layout.raw));
Ok(())
}
pub fn unregister_layout(&mut self, device: &ash::Device, layout: &super::BindGroupLayout) {
let key = layout.bucket_key();
let bucket = self.buckets.get_mut(&key).unwrap();
assert!(bucket.layouts.remove(&layout.raw));
if bucket.layouts.is_empty() {
// Remove the bucket and destroy any remaining pools.
let bucket = self.buckets.remove(&key).unwrap();
for pool in bucket.pools {
assert_eq!(
pool.available, pool.capacity,
"pool is not empty, at least one DescriptorSet has not been freed"
);
unsafe { device.destroy_descriptor_pool(pool.raw, None) };
}
}
}
pub unsafe fn alloc(
&mut self,
device: &ash::Device,
layout: &super::BindGroupLayout,
) -> Result<DescriptorSet, crate::DeviceError> {
let update_after_bind = layout.contains_binding_arrays;
let total_descriptors = layout.desc_count.total();
if update_after_bind
&& self.max_update_after_bind_descriptors_in_all_pools
- self.update_after_bind_descriptors_in_all_pools
< total_descriptors
{
return Err(crate::DeviceError::OutOfMemory);
}
let key = layout.bucket_key();
let bucket = self.buckets.get_mut(&key).unwrap();
// Prefer smaller/older/fuller pools for new allocations to prevent
// fragmentation and possibly fragmentation of hardware resources
// (VK_ERROR_FRAGMENTATION)
let pool = bucket
.pools
.iter_mut()
.enumerate()
.find(|(_, pool)| pool.available != 0);
let (pool_index, pool) = if let Some(pool) = pool {
pool
} else {
let capacity_hint = bucket.allocated_sets;
bucket.create_pool(device, &key, capacity_hint)?
};
let vk_info = vk::DescriptorSetAllocateInfo::default()
.descriptor_pool(pool.raw)
.set_layouts(core::slice::from_ref(&layout.raw));
let raw = match unsafe { device.allocate_descriptor_sets(&vk_info) } {
Ok(sets) => Ok(sets[0]),
// We make sure not to exceed the size of the pool.
Err(vk::Result::ERROR_OUT_OF_POOL_MEMORY) => unreachable!(),
// We only allocate from a pool if the nr and type of descriptors
// used by a layout is the same as those specified at pool
// creation time.
//
// > Additionally, if all sets allocated from the pool since it was
// created or most recently reset use the same number of
// descriptors (of each type) and the requested allocation also
// uses that same number of descriptors (of each type),
// then fragmentation must not cause an allocation failure.
//
// from https://docs.vulkan.org/refpages/latest/refpages/source/VkDescriptorPoolCreateInfo.html#_description
Err(vk::Result::ERROR_FRAGMENTED_POOL) => unreachable!(),
Err(err) => Err(super::map_host_device_oom_err(err)),
}?;
pool.available -= 1;
bucket.available_sets -= 1;
bucket.allocated_sets += 1;
if update_after_bind {
self.update_after_bind_descriptors_in_all_pools += total_descriptors;
}
Ok(DescriptorSet {
raw,
bucket_key: key,
pool_index,
})
}
pub unsafe fn free(&mut self, device: &ash::Device, set: DescriptorSet) {
let bucket = self.buckets.get_mut(&set.bucket_key).unwrap();
let pool = bucket.pools.get_mut(set.pool_index).unwrap();
let result =
unsafe { device.free_descriptor_sets(pool.raw, core::slice::from_ref(&set.raw())) };
if let Err(err) = result {
// vkFreeDescriptorSets is documented to return:
// - VK_ERROR_UNKNOWN
// - VK_ERROR_VALIDATION_FAILED (we shouldn't encounter this one)
// wgpu-hal doesn't currently report errors in destroy functions.
// Panic here for now. It might be ok to ignore the error but the
// proper solution would probably be to lose the device.
panic!("vkFreeDescriptorSets error: {err}, please report this error");
}
pool.available += 1;
bucket.available_sets += 1;
bucket.allocated_sets -= 1;
if set.bucket_key.update_after_bind {
self.update_after_bind_descriptors_in_all_pools -= set.bucket_key.counts.total();
}
// Do not immediately destroy empty pools since they might have
// been recently created.
// Destroy a pool only if it's empty and we have 1/4th its capacity
// in other pools.
// Note that this logic will never destroy the last pool.
let pool = bucket.pools.last().unwrap();
if pool.available == pool.capacity
&& bucket.available_sets - pool.capacity > pool.capacity / 4
{
let pool = bucket.pools.pop().unwrap();
unsafe { device.destroy_descriptor_pool(pool.raw, None) };
bucket.available_sets -= pool.capacity;
}
}
}
impl Drop for DescriptorAllocator {
fn drop(&mut self) {
if !std::thread::panicking() {
assert!(
self.buckets.is_empty(),
"buckets are not empty, at least one BGL has not been unregistered"
)
}
}
}
fn create_descriptor_pool(
device: &ash::Device,
key: &BucketKey,
capacity_hint: u32,
) -> Result<Pool, crate::DeviceError> {
let counts = &key.counts;
const NR_OF_DESCRIPTOR_TYPES: usize = 8;
use vk::DescriptorType as Dt;
let counts: [_; NR_OF_DESCRIPTOR_TYPES] = [
(Dt::SAMPLER, counts.sampler),
(Dt::SAMPLED_IMAGE, counts.sampled_image),
(Dt::STORAGE_IMAGE, counts.storage_image),
(Dt::UNIFORM_BUFFER, counts.uniform_buffer),
(Dt::UNIFORM_BUFFER_DYNAMIC, counts.uniform_buffer_dynamic),
(Dt::STORAGE_BUFFER, counts.storage_buffer),
(Dt::STORAGE_BUFFER_DYNAMIC, counts.storage_buffer_dynamic),
(
Dt::ACCELERATION_STRUCTURE_KHR,
counts.acceleration_structure,
),
];
// bounded doubling growth strategy
let mut capacity = capacity_hint
.clamp(POOL_MIN_SETS, POOL_MAX_SETS)
.next_power_of_two();
// Lower capacity to avoid overflowing `count * capacity` calculations
// below. The resulting `capacity` might not be a power of 2 but that's
// ok.
for (_, count) in counts {
capacity = (u32::MAX / count.max(1)).min(capacity);
}
let pool_sizes = counts
.into_iter()
.filter(|&(_, count)| count != 0)
.map(|(ty, count)| vk::DescriptorPoolSize {
ty,
descriptor_count: count * capacity,
})
.collect::<ArrayVec<_, NR_OF_DESCRIPTOR_TYPES>>();
let mut flags = vk::DescriptorPoolCreateFlags::FREE_DESCRIPTOR_SET;
if key.update_after_bind {
flags |= vk::DescriptorPoolCreateFlags::UPDATE_AFTER_BIND;
};
let vk_info = vk::DescriptorPoolCreateInfo::default()
.flags(flags)
.max_sets(capacity)
.pool_sizes(&pool_sizes);
let raw = unsafe { device.create_descriptor_pool(&vk_info, None) }
.map_err(super::map_host_device_oom_and_fragmentation_err)?;
Ok(Pool {
raw,
capacity,
available: capacity,
})
}
File diff suppressed because it is too large Load Diff
+252
View File
@@ -0,0 +1,252 @@
#![cfg(drm)]
use alloc::{string::ToString, vec::Vec};
use core::{mem::MaybeUninit, num::NonZeroU32};
use std::os::fd::{AsFd, BorrowedFd};
use ash::{ext, khr, vk};
use drm::{
self,
control::{self, connector, Device as _},
};
struct Card(i32);
impl AsFd for Card {
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.0) }
}
}
impl control::Device for Card {}
impl drm::Device for Card {}
macro_rules! to_u64 {
($expr:expr) => {{
#[allow(trivial_numeric_casts)]
let expr = $expr as u64;
assert!(size_of_val(&expr) <= size_of::<u64>());
expr
}};
}
impl super::Instance {
/// Creates a new surface from the given drm fd and plane, deriving the connector and mode.
///
/// # Safety
///
/// - All parameters must point to valid DRM values.
pub fn create_surface_from_drm_plane(
&self,
fd: i32,
plane: u32,
) -> Result<super::Surface, crate::InstanceError> {
let card = Card(fd);
let plane_info = card
.get_plane(
NonZeroU32::new(plane)
.ok_or(crate::InstanceError::new("Invalid drm plane".to_string()))?
.into(),
)
.map_err(|e| crate::InstanceError::with_source("drm plane not found".to_string(), e))?;
let crtc_handle = plane_info.crtc().ok_or(crate::InstanceError::new(
"No CRTC for drm plane".to_string(),
))?;
let crtc_info = card
.get_crtc(crtc_handle)
.map_err(|e| crate::InstanceError::with_source("drm CRTC not found".to_string(), e))?;
let mode = crtc_info.mode().ok_or(crate::InstanceError::new(
"No mode for drm CRTC".to_string(),
))?;
let mut connector_and_mode = None;
let resources = card.resource_handles().map_err(|e| {
crate::InstanceError::with_source("No drm resource handles found".to_string(), e)
})?;
for connector_handle in resources.connectors() {
let Ok(connector_info) = card.get_connector(*connector_handle, false) else {
continue;
};
if connector_info.state() != connector::State::Connected {
continue;
}
if let Some(encoder_handle) = connector_info.current_encoder() {
if let Ok(encoder_info) = card.get_encoder(encoder_handle) {
if encoder_info.crtc() == Some(crtc_handle) {
connector_and_mode = Some((*connector_handle, mode));
break;
}
}
}
}
let (connector_handle, mode) = connector_and_mode.ok_or(crate::InstanceError::new(
"Failed to derive drm connector and mode for plane".to_string(),
))?;
let (width, height) = mode.size();
// Rate in millihertz
let refresh_rate = (((mode.clock() as f64 * 1000.0)
/ (mode.hsync().2 as f64 * mode.vsync().2 as f64))
* 1000.0)
.round() as u32;
unsafe {
self.create_surface_from_drm(
fd,
plane,
connector_handle.into(),
width as u32,
height as u32,
refresh_rate,
)
}
}
/// Creates a new surface from the given drm configuration.
///
/// # Safety
///
/// - All parameters must point to valid DRM values.
pub unsafe fn create_surface_from_drm(
&self,
fd: i32,
plane: u32,
connector_id: u32,
width: u32,
height: u32,
refresh_rate: u32,
) -> Result<super::Surface, crate::InstanceError> {
if !self
.shared
.extensions
.contains(&ext::acquire_drm_display::NAME)
{
return Err(crate::InstanceError::new(
"Vulkan driver does not support VK_EXT_acquire_drm_display".to_string(),
));
}
let drm_stat = {
let mut stat = MaybeUninit::<libc::stat>::uninit();
if unsafe { libc::fstat(fd, stat.as_mut_ptr()) } != 0 {
return Err(crate::InstanceError::new(
"Unable to fstat drm device".to_string(),
));
}
unsafe { stat.assume_init() }
};
let raw_devices = match unsafe { self.shared.raw.enumerate_physical_devices() } {
Ok(devices) => devices,
Err(err) => {
log::error!("enumerate_adapters: {err}");
Vec::new()
}
};
let mut physical_device = None;
for device in raw_devices {
let properties2 = vk::PhysicalDeviceProperties2KHR::default();
let mut drm_props = vk::PhysicalDeviceDrmPropertiesEXT::default();
let mut properties2 = properties2.push_next(&mut drm_props);
unsafe {
self.shared
.raw
.get_physical_device_properties2(device, &mut properties2)
};
/*
The makedev call is just bit manipulation to combine major and minor device numbers into a Unix device ID.
It doesn't perform any filesystem operations, only bitshifting.
See: https://github.com/rust-lang/libc/blob/268e1b3810ac07ed637d9005bc1a54e49218c958/src/unix/linux_like/linux/mod.rs#L6049
We use the resulting device IDs to check if the Vulkan raw device from enumerate_physical_devices
matches the DRM device referred to by our file descriptor.
*/
let primary_devid =
libc::makedev(drm_props.primary_major as _, drm_props.primary_minor as _);
let render_devid =
libc::makedev(drm_props.render_major as _, drm_props.render_minor as _);
// On most platforms, both `*_devid`s and `st_rdev` are `dev_t`s (which is generally
// observed to be an unsigned integral type no greater than 64 bits). However, on some
// platforms, there divergences from this pattern:
//
// - `armv7-linux-androideabi`: `dev_t` is `c_ulong`, and `*_devid`s are `dev_t`, but
// `st_rdev` is `c_ulonglong`. So, we can't just do a `==` comparison.
// - OpenBSD has `dev_t` on both sides, but is `i32` (N.B., unsigned). Therefore, we
// can't just use `u64::from`.
#[allow(clippy::useless_conversion)]
if [primary_devid, render_devid]
.map(|devid| to_u64!(devid))
.contains(&to_u64!(drm_stat.st_rdev))
{
physical_device = Some(device)
}
}
let physical_device = physical_device.ok_or(crate::InstanceError::new(
"Failed to find suitable drm device".to_string(),
))?;
let acquire_drm_display_instance =
ext::acquire_drm_display::Instance::new(&self.shared.entry, &self.shared.raw);
let display = unsafe {
acquire_drm_display_instance
.get_drm_display(physical_device, fd, connector_id)
.expect("Failed to get drm display")
};
unsafe {
acquire_drm_display_instance
.acquire_drm_display(physical_device, fd, display)
.expect("Failed to acquire drm display")
}
let display_instance = khr::display::Instance::new(&self.shared.entry, &self.shared.raw);
let modes = unsafe {
display_instance
.get_display_mode_properties(physical_device, display)
.expect("Failed to get display modes")
};
let mut mode = None;
for current_mode in modes {
log::trace!(
"Comparing mode {}x{}@{} with {width}x{height}@{refresh_rate}",
current_mode.parameters.visible_region.width,
current_mode.parameters.visible_region.height,
current_mode.parameters.refresh_rate
);
if current_mode.parameters.refresh_rate == refresh_rate
&& current_mode.parameters.visible_region.width == width
&& current_mode.parameters.visible_region.height == height
{
mode = Some(current_mode)
}
}
let mode = mode.ok_or(crate::InstanceError::new(
"Failed to find suitable display mode".to_string(),
))?;
let create_info = vk::DisplaySurfaceCreateInfoKHR::default()
.display_mode(mode.display_mode)
.image_extent(mode.parameters.visible_region)
.transform(vk::SurfaceTransformFlagsKHR::IDENTITY)
.alpha_mode(vk::DisplayPlaneAlphaFlagsKHR::OPAQUE)
.plane_index(plane);
let surface = unsafe { display_instance.create_display_plane_surface(&create_info, None) }
.expect("Failed to create DRM surface");
Ok(self.create_surface_from_vk_surface_khr(surface, None))
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+177
View File
@@ -0,0 +1,177 @@
//! Sampler cache for Vulkan backend.
//!
//! Nearly identical to the DX12 sampler cache, without descriptor heap management.
use ash::vk;
use hashbrown::{hash_map::Entry, HashMap};
use ordered_float::OrderedFloat;
/// If the allowed sampler count is above this value, the sampler cache is disabled.
const ENABLE_SAMPLER_CACHE_CUTOFF: u32 = 1 << 20;
/// [`vk::SamplerCreateInfo`] is not hashable, so we wrap it in a newtype that is.
///
/// We use [`OrderedFloat`] to allow for floating point values to be compared and
/// hashed in a defined way.
#[derive(Copy, Clone)]
struct HashableSamplerCreateInfo(vk::SamplerCreateInfo<'static>);
impl PartialEq for HashableSamplerCreateInfo {
fn eq(&self, other: &Self) -> bool {
self.0.flags == other.0.flags
&& self.0.mag_filter == other.0.mag_filter
&& self.0.min_filter == other.0.min_filter
&& self.0.mipmap_mode == other.0.mipmap_mode
&& self.0.address_mode_u == other.0.address_mode_u
&& self.0.address_mode_v == other.0.address_mode_v
&& self.0.address_mode_w == other.0.address_mode_w
&& OrderedFloat(self.0.mip_lod_bias) == OrderedFloat(other.0.mip_lod_bias)
&& self.0.anisotropy_enable == other.0.anisotropy_enable
&& OrderedFloat(self.0.max_anisotropy) == OrderedFloat(other.0.max_anisotropy)
&& self.0.compare_enable == other.0.compare_enable
&& self.0.compare_op == other.0.compare_op
&& OrderedFloat(self.0.min_lod) == OrderedFloat(other.0.min_lod)
&& OrderedFloat(self.0.max_lod) == OrderedFloat(other.0.max_lod)
&& self.0.border_color == other.0.border_color
&& self.0.unnormalized_coordinates == other.0.unnormalized_coordinates
}
}
impl Eq for HashableSamplerCreateInfo {}
impl core::hash::Hash for HashableSamplerCreateInfo {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.0.flags.hash(state);
self.0.mag_filter.hash(state);
self.0.min_filter.hash(state);
self.0.mipmap_mode.hash(state);
self.0.address_mode_u.hash(state);
self.0.address_mode_v.hash(state);
self.0.address_mode_w.hash(state);
OrderedFloat(self.0.mip_lod_bias).hash(state);
self.0.anisotropy_enable.hash(state);
OrderedFloat(self.0.max_anisotropy).hash(state);
self.0.compare_enable.hash(state);
self.0.compare_op.hash(state);
OrderedFloat(self.0.min_lod).hash(state);
OrderedFloat(self.0.max_lod).hash(state);
self.0.border_color.hash(state);
self.0.unnormalized_coordinates.hash(state);
}
}
/// Entry in the sampler cache.
struct CacheEntry {
sampler: vk::Sampler,
ref_count: u32,
}
/// Global sampler cache.
///
/// As some devices have a low limit (4000) on the number of unique samplers that can be created,
/// we need to cache samplers to avoid running out if people eagerly create duplicate samplers.
pub(crate) struct SamplerCache {
/// Mapping from the sampler description to sampler and reference count.
samplers: HashMap<HashableSamplerCreateInfo, CacheEntry>,
/// Maximum number of unique samplers that can be created.
total_capacity: u32,
/// If true, the sampler cache is disabled and all samplers are created on demand.
passthrough: bool,
}
impl SamplerCache {
pub fn new(total_capacity: u32) -> Self {
let passthrough = total_capacity >= ENABLE_SAMPLER_CACHE_CUTOFF;
Self {
samplers: HashMap::new(),
total_capacity,
passthrough,
}
}
/// Create a sampler, or return an existing one if it already exists.
///
/// If the sampler already exists, the reference count is incremented.
///
/// If the sampler does not exist, a new sampler is created and inserted into the cache.
///
/// If the cache is full, an error is returned.
pub fn create_sampler(
&mut self,
device: &ash::Device,
create_info: vk::SamplerCreateInfo<'static>,
) -> Result<vk::Sampler, crate::DeviceError> {
if self.passthrough {
return unsafe { device.create_sampler(&create_info, None) }
.map_err(super::map_host_device_oom_and_ioca_err);
};
// Get the number of used samplers. Needs to be done before to appease the borrow checker.
let used_samplers = self.samplers.len();
match self.samplers.entry(HashableSamplerCreateInfo(create_info)) {
Entry::Occupied(occupied_entry) => {
// We have found a match, so increment the refcount and return the index.
let value = occupied_entry.into_mut();
value.ref_count += 1;
Ok(value.sampler)
}
Entry::Vacant(vacant_entry) => {
// We need to create a new sampler.
// We need to check if we can create more samplers.
if used_samplers >= self.total_capacity as usize {
log::error!("There is no more room in the global sampler heap for more unique samplers. Your device supports a maximum of {} unique samplers.", self.samplers.len());
return Err(crate::DeviceError::OutOfMemory);
}
// Create the sampler.
let sampler = unsafe { device.create_sampler(&create_info, None) }
.map_err(super::map_host_device_oom_and_ioca_err)?;
// Insert the new sampler into the mapping.
vacant_entry.insert(CacheEntry {
sampler,
ref_count: 1,
});
Ok(sampler)
}
}
}
/// Decrease the reference count of a sampler and destroy it if the reference count reaches 0.
///
/// The provided sampler is checked against the sampler in the cache to ensure there is no clerical error.
pub fn destroy_sampler(
&mut self,
device: &ash::Device,
create_info: vk::SamplerCreateInfo<'static>,
provided_sampler: vk::Sampler,
) {
if self.passthrough {
unsafe { device.destroy_sampler(provided_sampler, None) };
return;
};
let Entry::Occupied(mut hash_map_entry) =
self.samplers.entry(HashableSamplerCreateInfo(create_info))
else {
log::error!("Trying to destroy a sampler that does not exist.");
return;
};
let cache_entry = hash_map_entry.get_mut();
assert_eq!(
cache_entry.sampler, provided_sampler,
"Provided sampler does not match the sampler in the cache."
);
cache_entry.ref_count -= 1;
if cache_entry.ref_count == 0 {
unsafe { device.destroy_sampler(cache_entry.sampler, None) };
hash_map_entry.remove();
}
}
}
+218
View File
@@ -0,0 +1,218 @@
//! Definition of the [`SemaphoreList`] type.
use alloc::vec::Vec;
use ash::vk;
use core::mem::MaybeUninit;
#[derive(Debug, PartialEq)]
pub enum SemaphoreListMode {
Wait,
Signal,
}
/// A list of Vulkan semaphores to wait for or signal.
///
/// This represents a list of binary or timeline semaphores, together
/// with values for the timeline semaphores, and stage masks, if these
/// are used for waiting.
///
/// This type ensures that the array of semaphores to be signaled
/// stays aligned with the array of values for timeline semaphores
/// appearing in that list. The [`add_to_submit`] method prepares the
/// `vkQueueSubmit` arguments appropriately for whatever semaphores we
/// actually have.
///
/// [`add_to_submit`]: SemaphoreList::add_to_submit
#[derive(Debug)]
pub struct SemaphoreList {
/// Mode of the semaphore list. Used for validation.
mode: SemaphoreListMode,
/// Semaphores to use.
///
/// This can be a mix of binary and timeline semaphores.
semaphores: Vec<vk::Semaphore>,
/// Values for the timeline semaphores.
///
/// If no timeline semaphores are present in [`semaphores`], this
/// is empty. If any timeline semaphores are present, then this
/// has the same length as [`semaphores`], with dummy !0 values
/// in the elements corresponding to binary semaphores, since
/// Vulkan ignores these.
///
/// [`semaphores`]: Self::semaphores
values: Vec<u64>,
/// Stage masks for wait semaphores.
///
/// This is only used if `mode` is `Wait`.
pub stage_masks: Vec<vk::PipelineStageFlags>,
}
impl SemaphoreList {
pub fn new(mode: SemaphoreListMode) -> Self {
Self {
mode,
semaphores: Vec::new(),
values: Vec::new(),
stage_masks: Vec::new(),
}
}
pub fn is_empty(&self) -> bool {
self.semaphores.is_empty()
}
/// Add this list to the semaphores to be signalled by a `vkQueueSubmit` call.
///
/// - Set `submit_info`'s `pSignalSemaphores` list to this list's
/// semaphores.
///
/// - If this list contains any timeline semaphores, then initialize
/// `timeline_info`, set its `pSignalSemaphoreValues` to this
/// list's values, and add it to `submit_info`s extension chain.
///
/// Return the revised `submit_info` value.
pub fn add_to_submit<'info, 'semaphores: 'info>(
wait_semaphores: &'semaphores mut Self,
signal_semaphores: &'semaphores mut Self,
submit_info: vk::SubmitInfo<'info>,
timeline_info: &'info mut MaybeUninit<vk::TimelineSemaphoreSubmitInfo<'info>>,
) -> vk::SubmitInfo<'info> {
wait_semaphores.check();
signal_semaphores.check();
assert!(matches!(wait_semaphores.mode, SemaphoreListMode::Wait));
assert!(matches!(signal_semaphores.mode, SemaphoreListMode::Signal));
let timeline_info = timeline_info.write(vk::TimelineSemaphoreSubmitInfo::default());
let mut uses_timeline = false;
if !wait_semaphores.values.is_empty() {
*timeline_info = timeline_info.wait_semaphore_values(&wait_semaphores.values);
uses_timeline = true;
}
if !signal_semaphores.values.is_empty() {
*timeline_info = timeline_info.signal_semaphore_values(&signal_semaphores.values);
uses_timeline = true;
}
let mut submit_info = submit_info
.wait_semaphores(&wait_semaphores.semaphores)
.wait_dst_stage_mask(&wait_semaphores.stage_masks)
.signal_semaphores(&signal_semaphores.semaphores);
if uses_timeline {
submit_info = submit_info.push_next(timeline_info);
}
submit_info
}
/// Add a semaphore to be signaled. Panics if this is a list of semaphores to wait.
pub fn push_signal(&mut self, semaphore: SemaphoreType) {
assert!(matches!(self.mode, SemaphoreListMode::Signal));
self.push_inner(semaphore);
}
/// Add a semaphore to be waited for. Panics if this is a list of semaphores to signal.
pub fn push_wait(&mut self, semaphore: SemaphoreType, stage: vk::PipelineStageFlags) {
assert!(matches!(self.mode, SemaphoreListMode::Wait));
self.stage_masks.push(stage);
self.push_inner(semaphore);
}
fn push_inner(&mut self, semaphore: SemaphoreType) {
match semaphore {
SemaphoreType::Binary(semaphore) => {
self.semaphores.push(semaphore);
// Push a dummy value if necessary.
if !self.values.is_empty() {
self.values.push(!0);
}
}
SemaphoreType::Timeline(semaphore, value) => {
// We may be the first timeline semaphore, ensure that the values
// array is filled with dummy values for existing binary semaphores.
self.pad_values();
self.semaphores.push(semaphore);
self.values.push(value);
}
}
self.check();
}
/// Remove all entries matching `semaphore` from the list. Returns `true`
/// if anything was removed.
pub fn remove(&mut self, semaphore: vk::Semaphore) -> bool {
let mut removed = false;
let mut i = 0;
while i < self.semaphores.len() {
if self.semaphores[i] == semaphore {
self.semaphores.swap_remove(i);
if !self.values.is_empty() {
self.values.swap_remove(i);
}
if !self.stage_masks.is_empty() {
self.stage_masks.swap_remove(i);
}
removed = true;
} else {
i += 1;
}
}
self.check();
removed
}
/// Append `other` to `self`, leaving `other` empty.
pub fn append(&mut self, other: &mut Self) {
assert_eq!(self.mode, other.mode);
// If we're about to receive values, ensure we're aligned first.
if !other.values.is_empty() {
self.pad_values();
}
self.semaphores.append(&mut other.semaphores);
self.values.append(&mut other.values);
// If we had values, but `other` did not, re-align.
if !self.values.is_empty() {
self.pad_values();
}
self.stage_masks.append(&mut other.stage_masks);
self.check();
}
/// Pad `self.values` with dummy values for binary semaphores,
/// in preparation for adding a timeline semaphore value.
///
/// This is a no-op if we already have values.
fn pad_values(&mut self) {
self.values.resize(self.semaphores.len(), !0);
}
#[track_caller]
fn check(&self) {
debug_assert!(self.values.is_empty() || self.values.len() == self.semaphores.len());
match self.mode {
SemaphoreListMode::Wait => {
debug_assert!(
self.stage_masks.is_empty() || self.stage_masks.len() == self.semaphores.len()
);
}
SemaphoreListMode::Signal => {
debug_assert!(self.stage_masks.is_empty());
}
}
}
}
pub enum SemaphoreType {
Binary(vk::Semaphore),
Timeline(vk::Semaphore, u64),
}
+119
View File
@@ -0,0 +1,119 @@
use alloc::boxed::Box;
use core::{any::Any, fmt::Debug, time::Duration};
use crate::vulkan::{semaphore_list::SemaphoreType, DeviceShared};
pub(super) use native::*;
mod native;
/// Win32 `HWND`, handed to a Win32 [`NativeSurface`] at construction so it can
/// build its [`DxgiHdrSource`](crate::auxil::dxgi::hdr::DxgiHdrSource) for the
/// display-HDR query. Borrowed from the app's window.
///
/// A newtype rather than a bare `isize` so the cfg lives in one place: off Windows
/// it is an uninhabited enum, so the non-Windows surface paths can't construct one
/// and the constructor signatures stay cfg-free.
#[cfg(windows)]
#[derive(Clone, Copy)]
pub(crate) struct WindowHandle(pub(crate) windows::Win32::Foundation::HWND);
#[cfg(not(windows))]
#[derive(Clone, Copy)]
pub(crate) enum WindowHandle {}
pub(super) trait Surface: Send + Sync + 'static {
/// Returns the surface capabilities for the given adapter.
///
/// Returns `None` if the surface is not compatible with the adapter.
fn surface_capabilities(&self, adapter: &super::Adapter) -> Option<crate::SurfaceCapabilities>;
/// Creates a swapchain for the surface with the given configuration.
///
/// If this is not the first swapchain created for the surface, the old swapchain
/// must be provided. [`Swapchain::release_resources`] must be called on the old swapchain
/// before calling this method.
unsafe fn create_swapchain(
&self,
device: &super::Device,
config: &crate::SurfaceConfiguration,
provided_old_swapchain: Option<Box<dyn Swapchain>>,
) -> Result<Box<dyn Swapchain>, crate::SurfaceError>;
/// This surface's current display HDR info, if it can report it.
///
/// `Some` only for Win32 surfaces (read through DXGI); `None` otherwise
/// (Wayland / X11 / Android / Metal), which is the default.
fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {
None
}
/// Allows downcasting to the concrete type.
fn as_any(&self) -> &dyn Any;
}
pub(super) trait Swapchain: Send + Sync + 'static {
/// Releases all resources associated with the swapchain, without
/// destroying the swapchain itself. Must be called before calling
/// either [`Surface::create_swapchain`] or dropping the swapchain.
///
/// The swapchain must not be in use when this is called.
unsafe fn release_resources(&mut self, device: &super::Device);
/// Acquires the next available surface texture for rendering.
///
/// `timeout` specifies the maximum time to wait for an image to become available.
/// If `None` is specified, this function will wait indefinitely.
///
/// Returns `Err(SurfaceError::Timeout)` if the timeout elapsed before an image became available.
unsafe fn acquire(
&mut self,
timeout: Option<Duration>,
fence: &super::Fence,
) -> Result<crate::AcquiredSurfaceTexture<crate::api::Vulkan>, crate::SurfaceError>;
/// Tries to discard the acquired texture without presenting it.
///
/// In practice, this doesn't really work in the current implementations.
unsafe fn discard_texture(
&mut self,
texture: super::SurfaceTexture,
) -> Result<(), crate::SurfaceError>;
/// Presents the given surface texture using the queue.
unsafe fn present(
&mut self,
queue: &super::Queue,
texture: crate::vulkan::SurfaceTexture,
) -> Result<(), crate::SurfaceError>;
/// Allows downcasting to the concrete type.
fn as_any(&self) -> &dyn Any;
/// Allows downcasting to the concrete type mutably.
fn as_any_mut(&mut self) -> &mut dyn Any;
}
/// Swapchain specific metadata associated with a surface texture.
pub(super) trait SurfaceTextureMetadata: Debug + Send + Sync + 'static {
/// Returns a guard which can yield the semaphores needed for submission using this swapchain texture.
fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_>;
/// Allows downcasting to the concrete type.
fn as_any(&self) -> &dyn Any;
}
/// Guard type for managing swapchain submission semaphores.
pub(super) trait SwapchainSubmissionSemaphoreGuard {
/// Sets the Fence value for this submission.
fn set_used_fence_value(&mut self, value: u64);
/// Gets semaphores to wait on before doing GPU work for this swapchain texture.
fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType>;
/// Gets the semaphore to signal when GPU work for this swapchain texture is complete.
fn get_submit_signal_semaphore(
&mut self,
device: &DeviceShared,
) -> Result<SemaphoreType, crate::DeviceError>;
}
+937
View File
@@ -0,0 +1,937 @@
//! Vulkan Surface and Swapchain implementation using native Vulkan surfaces.
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use core::any::Any;
use ash::{khr, vk};
use parking_lot::{Mutex, MutexGuard};
use crate::vulkan::{
conv, map_host_device_oom_and_lost_err,
semaphore_list::SemaphoreType,
swapchain::{
Surface, SurfaceTextureMetadata, Swapchain, SwapchainSubmissionSemaphoreGuard, WindowHandle,
},
DeviceShared, InstanceShared,
};
pub(crate) struct NativeSurface {
raw: vk::SurfaceKHR,
functor: khr::surface::Instance,
instance: Arc<InstanceShared>,
/// Built from the window's `HWND` (Windows only) to answer the display-HDR
/// query; `None` for non-Win32 surfaces.
#[cfg(windows)]
hdr_source: Option<crate::auxil::dxgi::hdr::DxgiHdrSource>,
}
impl NativeSurface {
pub fn from_vk_surface_khr(
instance: &crate::vulkan::Instance,
raw: vk::SurfaceKHR,
hwnd: Option<WindowHandle>,
) -> Self {
#[cfg(not(windows))]
let _ = hwnd;
let functor = khr::surface::Instance::new(&instance.shared.entry, &instance.shared.raw);
Self {
raw,
functor,
instance: Arc::clone(&instance.shared),
#[cfg(windows)]
hdr_source: hwnd.map(|wh| crate::auxil::dxgi::hdr::DxgiHdrSource::new(wh.0)),
}
}
pub fn as_raw(&self) -> vk::SurfaceKHR {
self.raw
}
}
impl Drop for NativeSurface {
fn drop(&mut self) {
unsafe {
self.functor.destroy_surface(self.raw, None);
}
}
}
impl Surface for NativeSurface {
fn surface_capabilities(
&self,
adapter: &crate::vulkan::Adapter,
) -> Option<crate::SurfaceCapabilities> {
if !adapter.private_caps.can_present {
return None;
}
let queue_family_index = 0; //TODO
{
profiling::scope!("vkGetPhysicalDeviceSurfaceSupportKHR");
match unsafe {
self.functor.get_physical_device_surface_support(
adapter.raw,
queue_family_index,
self.raw,
)
} {
Ok(true) => (),
Ok(false) => return None,
Err(e) => {
log::error!("get_physical_device_surface_support: {e}");
return None;
}
}
}
let caps = {
profiling::scope!("vkGetPhysicalDeviceSurfaceCapabilitiesKHR");
match unsafe {
self.functor
.get_physical_device_surface_capabilities(adapter.raw, self.raw)
} {
Ok(caps) => caps,
Err(e) => {
log::error!("get_physical_device_surface_capabilities: {e}");
return None;
}
}
};
// If image count is 0, the support number of images is unlimited.
let max_image_count = if caps.max_image_count == 0 {
!0
} else {
caps.max_image_count
};
// `0xFFFFFFFF` indicates that the extent depends on the created swapchain.
let current_extent = if caps.current_extent.width != !0 && caps.current_extent.height != !0
{
Some(wgt::Extent3d {
width: caps.current_extent.width,
height: caps.current_extent.height,
depth_or_array_layers: 1,
})
} else {
None
};
let raw_present_modes = {
profiling::scope!("vkGetPhysicalDeviceSurfacePresentModesKHR");
match unsafe {
self.functor
.get_physical_device_surface_present_modes(adapter.raw, self.raw)
} {
Ok(present_modes) => present_modes,
Err(e) => {
log::error!("get_physical_device_surface_present_modes: {e}");
// Per definition of `SurfaceCapabilities`, there must be at least one present mode.
return None;
}
}
};
let raw_surface_formats = {
profiling::scope!("vkGetPhysicalDeviceSurfaceFormatsKHR");
match unsafe {
self.functor
.get_physical_device_surface_formats(adapter.raw, self.raw)
} {
Ok(formats) => formats,
Err(e) => {
log::error!("get_physical_device_surface_formats: {e}");
// Per definition of `SurfaceCapabilities`, there must be at least one present format.
return None;
}
}
};
// Group the driver's (format, color space) pairs into one entry per
// format, preserving the driver's format order.
let mut formats: Vec<wgt::SurfaceFormatCapabilities> = Vec::new();
for (format, color_space) in raw_surface_formats
.into_iter()
.filter_map(conv::map_vk_surface_formats)
{
let color_spaces = color_space.to_color_spaces().unwrap();
match formats.iter_mut().find(|fc| fc.format == format) {
Some(fc) => fc.color_spaces |= color_spaces,
None => formats.push(wgt::SurfaceFormatCapabilities {
format,
color_spaces,
}),
}
}
Some(crate::SurfaceCapabilities {
formats,
// TODO: Right now we're always truncating the swap chain
// (presumably - we're actually setting the min image count which isn't necessarily the swap chain size)
// Instead, we should use extensions when available to wait in present.
// See https://github.com/gfx-rs/wgpu/issues/2869
maximum_frame_latency: (caps.min_image_count - 1)..=(max_image_count - 1), // Note this can't underflow since both `min_image_count` is at least one and we already patched `max_image_count`.
current_extent,
usage: conv::map_vk_image_usage(caps.supported_usage_flags),
present_modes: raw_present_modes
.into_iter()
.flat_map(conv::map_vk_present_mode)
.collect(),
composite_alpha_modes: conv::map_vk_composite_alpha(caps.supported_composite_alpha),
})
}
unsafe fn create_swapchain(
&self,
device: &crate::vulkan::Device,
config: &crate::SurfaceConfiguration,
provided_old_swapchain: Option<Box<dyn Swapchain>>,
) -> Result<Box<dyn Swapchain>, crate::SurfaceError> {
profiling::scope!("Device::create_swapchain");
let functor = khr::swapchain::Device::new(&self.instance.raw, &device.shared.raw);
let old_swapchain = provided_old_swapchain
.as_ref()
.map(|osc| osc.as_any().downcast_ref::<NativeSwapchain>().unwrap().raw)
.unwrap_or(vk::SwapchainKHR::null());
let color_space = conv::map_surface_color_space(config.color_space);
let original_format = device.shared.private_caps.map_texture_format(config.format);
let mut raw_flags = vk::SwapchainCreateFlagsKHR::empty();
let mut raw_view_formats: Vec<vk::Format> = vec![];
if !config.view_formats.is_empty() {
raw_flags |= vk::SwapchainCreateFlagsKHR::MUTABLE_FORMAT;
raw_view_formats = config
.view_formats
.iter()
.map(|f| device.shared.private_caps.map_texture_format(*f))
.collect();
raw_view_formats.push(original_format);
}
let mut info = vk::SwapchainCreateInfoKHR::default()
.flags(raw_flags)
.surface(self.raw)
.min_image_count(config.maximum_frame_latency + 1) // TODO: https://github.com/gfx-rs/wgpu/issues/2869
.image_format(original_format)
.image_color_space(color_space)
.image_extent(vk::Extent2D {
width: config.extent.width,
height: config.extent.height,
})
.image_array_layers(config.extent.depth_or_array_layers)
.image_usage(conv::map_texture_usage(config.usage))
.image_sharing_mode(vk::SharingMode::EXCLUSIVE)
.pre_transform(vk::SurfaceTransformFlagsKHR::IDENTITY)
.composite_alpha(conv::map_composite_alpha_mode(config.composite_alpha_mode))
.present_mode(conv::map_present_mode(config.present_mode))
.clipped(true)
.old_swapchain(old_swapchain);
let mut format_list_info = vk::ImageFormatListCreateInfo::default();
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) }
};
let raw = match result {
Ok(swapchain) => swapchain,
Err(error) => {
return Err(match error {
vk::Result::ERROR_SURFACE_LOST_KHR
| vk::Result::ERROR_INITIALIZATION_FAILED => crate::SurfaceError::Lost,
vk::Result::ERROR_NATIVE_WINDOW_IN_USE_KHR => {
crate::SurfaceError::Other("Native window is in use")
}
// We don't use VK_EXT_image_compression_control
// VK_ERROR_COMPRESSION_EXHAUSTED_EXT
other => map_host_device_oom_and_lost_err(other).into(),
});
}
};
let images = unsafe { functor.get_swapchain_images(raw) }
.map_err(crate::vulkan::map_host_device_oom_err)?;
// This fence is only used to throttle acquisition on Windows. It is very important to
// avoid bad frame pacing when the Vulkan driver is using a DXGI swapchain. See
// https://github.com/gfx-rs/wgpu/issues/8310 and
// https://github.com/gfx-rs/wgpu/issues/8354 for more details.
let fence = if cfg!(target_os = "windows") {
let raw = unsafe {
device
.shared
.raw
.create_fence(&vk::FenceCreateInfo::default(), None)
.map_err(crate::vulkan::map_host_device_oom_err)?
};
Some(raw)
} else {
None
};
// NOTE: It's important that we define the same number of acquire/present semaphores
// as we will need to index into them with the image index.
let acquire_semaphores = (0..images.len())
.map(|i| {
SwapchainAcquireSemaphore::new(&device.shared, i)
.map(Mutex::new)
.map(Arc::new)
})
.collect::<Result<Vec<_>, _>>()?;
let present_semaphores = (0..images.len())
.map(|i| Arc::new(Mutex::new(SwapchainPresentSemaphores::new(i))))
.collect::<Vec<_>>();
Ok(Box::new(NativeSwapchain {
raw,
functor,
device: Arc::clone(&device.shared),
images,
fence,
config: config.clone(),
acquire_semaphores,
next_acquire_index: 0,
present_semaphores,
next_present_time: None,
}))
}
#[cfg(windows)]
fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {
self.hdr_source.as_ref()?.display_hdr_info()
}
fn as_any(&self) -> &dyn Any {
self
}
}
pub(crate) struct NativeSwapchain {
raw: vk::SwapchainKHR,
functor: khr::swapchain::Device,
device: Arc<DeviceShared>,
images: Vec<vk::Image>,
/// Fence used to wait on the acquired image.
fence: Option<vk::Fence>,
config: crate::SurfaceConfiguration,
/// Semaphores used between image acquisition and the first submission
/// that uses that image. This is indexed using [`next_acquire_index`].
///
/// Because we need to provide this to [`vkAcquireNextImageKHR`], we haven't
/// received the swapchain image index for the frame yet, so we cannot use
/// that to index it.
///
/// Before we pass this to [`vkAcquireNextImageKHR`], we ensure that we wait on
/// the submission indicated by [`previously_used_submission_index`]. This ensures
/// the semaphore is no longer in use before we use it.
///
/// [`next_acquire_index`]: NativeSwapchain::next_acquire_index
/// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR
/// [`previously_used_submission_index`]: SwapchainAcquireSemaphore::previously_used_submission_index
acquire_semaphores: Vec<Arc<Mutex<SwapchainAcquireSemaphore>>>,
/// The index of the next acquire semaphore to use.
///
/// This is incremented each time we acquire a new image, and wraps around
/// to 0 when it reaches the end of [`acquire_semaphores`].
///
/// [`acquire_semaphores`]: NativeSwapchain::acquire_semaphores
next_acquire_index: usize,
/// Semaphore sets used between all submissions that write to an image and
/// the presentation of that image.
///
/// This is indexed by the swapchain image index returned by
/// [`vkAcquireNextImageKHR`].
///
/// We know it is safe to use these semaphores because use them
/// _after_ the acquire semaphore. Because the acquire semaphore
/// has been signaled, the previous presentation using that image
/// is known-finished, so this semaphore is no longer in use.
///
/// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR
present_semaphores: Vec<Arc<Mutex<SwapchainPresentSemaphores>>>,
/// The present timing information which will be set in the next call to [`present()`](crate::Queue::present()).
///
/// # Safety
///
/// This must only be set if [`wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING`] is enabled, and
/// so the VK_GOOGLE_display_timing extension is present.
next_present_time: Option<vk::PresentTimeGOOGLE>,
}
impl Drop for NativeSwapchain {
fn drop(&mut self) {
unsafe {
self.functor.destroy_swapchain(self.raw, None);
}
}
}
impl Swapchain for NativeSwapchain {
unsafe fn release_resources(&mut self, device: &crate::vulkan::Device) {
profiling::scope!("Swapchain::release_resources");
{
profiling::scope!("vkDeviceWaitIdle");
// We need to also wait until all presentation work is done. Because there is no way to portably wait until
// the presentation work is done, we are forced to wait until the device is idle.
let _ = unsafe {
device
.shared
.raw
.device_wait_idle()
.map_err(map_host_device_oom_and_lost_err)
};
};
if let Some(fence) = self.fence {
unsafe { device.shared.raw.destroy_fence(fence, None) }
}
// We cannot take this by value, as the function returns `self`.
for semaphore in self.acquire_semaphores.drain(..) {
let arc_removed = Arc::into_inner(semaphore).expect(
"Trying to destroy a SwapchainAcquireSemaphore that is still in use by a SurfaceTexture",
);
let mutex_removed = arc_removed.into_inner();
unsafe { mutex_removed.destroy(&device.shared.raw) };
}
for semaphore in self.present_semaphores.drain(..) {
let arc_removed = Arc::into_inner(semaphore).expect(
"Trying to destroy a SwapchainPresentSemaphores that is still in use by a SurfaceTexture",
);
let mutex_removed = arc_removed.into_inner();
unsafe { mutex_removed.destroy(&device.shared.raw) };
}
}
unsafe fn acquire(
&mut self,
timeout: Option<core::time::Duration>,
fence: &crate::vulkan::Fence,
) -> Result<crate::AcquiredSurfaceTexture<crate::api::Vulkan>, crate::SurfaceError> {
let mut timeout_ns = match timeout {
Some(duration) => duration.as_nanos() as u64,
None => u64::MAX,
};
// AcquireNextImageKHR on Android (prior to Android 11) doesn't support timeouts
// and will also log verbose warnings if tying to use a timeout.
//
// Android 10 implementation for reference:
// https://android.googlesource.com/platform/frameworks/native/+/refs/tags/android-mainline-10.0.0_r13/vulkan/libvulkan/swapchain.cpp#1426
// Android 11 implementation for reference:
// https://android.googlesource.com/platform/frameworks/native/+/refs/tags/android-mainline-11.0.0_r45/vulkan/libvulkan/swapchain.cpp#1438
//
// Android 11 corresponds to an SDK_INT/ro.build.version.sdk of 30
if cfg!(target_os = "android") && self.device.instance.android_sdk_version < 30 {
timeout_ns = u64::MAX;
}
let acquire_semaphore_arc = self.get_acquire_semaphore();
// Nothing should be using this, so we don't block, but panic if we fail to lock.
let acquire_semaphore_guard = acquire_semaphore_arc
.try_lock()
.expect("Failed to lock a SwapchainSemaphores.");
// Wait for all commands writing to the previously acquired image to
// complete.
//
// Almost all the steps in the usual acquire-draw-present flow are
// asynchronous: they get something started on the presentation engine
// or the GPU, but on the CPU, control returns immediately. Without some
// sort of intervention, the CPU could crank out frames much faster than
// the presentation engine can display them.
//
// This is the intervention: if any submissions drew on this image, and
// thus waited for `locked_swapchain_semaphores.acquire`, wait for all
// of them to finish, thus ensuring that it's okay to pass `acquire` to
// `vkAcquireNextImageKHR` again.
let completed = self.device.wait_for_fence(
fence,
acquire_semaphore_guard.previously_used_submission_index,
timeout_ns,
)?;
if !completed {
return Err(crate::SurfaceError::Timeout);
}
let acquire_fence = self.fence.unwrap_or_else(vk::Fence::null);
// will block if no image is available
let (index, suboptimal) = match unsafe {
profiling::scope!("vkAcquireNextImageKHR");
self.functor.acquire_next_image(
self.raw,
timeout_ns,
acquire_semaphore_guard.acquire,
acquire_fence,
)
} {
// We treat `VK_SUBOPTIMAL_KHR` as `VK_SUCCESS` on Android.
// See the comment in `Queue::present`.
#[cfg(target_os = "android")]
Ok((index, _)) => (index, false),
#[cfg(not(target_os = "android"))]
Ok(pair) => pair,
Err(error) => {
return match error {
vk::Result::TIMEOUT => Err(crate::SurfaceError::Timeout),
vk::Result::NOT_READY | vk::Result::ERROR_OUT_OF_DATE_KHR => {
Err(crate::SurfaceError::Outdated)
}
vk::Result::ERROR_SURFACE_LOST_KHR => Err(crate::SurfaceError::Lost),
// We don't use VK_EXT_full_screen_exclusive
// VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT
other => Err(map_host_device_oom_and_lost_err(other).into()),
};
}
};
if let Some(fence) = self.fence {
unsafe {
// The `wait_all` argument must be `true` to avoid crash on some Android devices. See https://github.com/gfx-rs/wgpu/pull/8769
self.device
.raw
.wait_for_fences(&[fence], true, timeout_ns)
.map_err(map_host_device_oom_and_lost_err)?;
self.device
.raw
.reset_fences(&[fence])
.map_err(map_host_device_oom_and_lost_err)?;
}
}
drop(acquire_semaphore_guard);
// We only advance the surface semaphores if we successfully acquired an image, otherwise
// we should try to re-acquire using the same semaphores.
self.advance_acquire_semaphore();
let present_semaphore_arc = self.get_present_semaphores(index);
// special case for Intel Vulkan returning bizarre values (ugh)
if self.device.vendor_id == crate::auxil::db::intel::VENDOR && index > 0x100 {
return Err(crate::SurfaceError::Outdated);
}
let identity = self.device.texture_identity_factory.next();
let texture = crate::vulkan::SurfaceTexture {
index,
texture: crate::vulkan::Texture {
raw: self.images[index as usize],
drop_guard: None,
memory: crate::vulkan::TextureMemory::External,
format: self.config.format,
copy_size: crate::CopyExtent {
width: self.config.extent.width,
height: self.config.extent.height,
depth: 1,
},
identity,
},
metadata: Box::new(NativeSurfaceTextureMetadata {
acquire_semaphores: acquire_semaphore_arc,
present_semaphores: present_semaphore_arc,
}),
};
Ok(crate::AcquiredSurfaceTexture {
texture,
suboptimal,
})
}
unsafe fn discard_texture(
&mut self,
_texture: crate::vulkan::SurfaceTexture,
) -> Result<(), crate::SurfaceError> {
// TODO: Current implementation no-ops
Ok(())
}
unsafe fn present(
&mut self,
queue: &crate::vulkan::Queue,
texture: crate::vulkan::SurfaceTexture,
) -> Result<(), crate::SurfaceError> {
let metadata = texture
.metadata
.as_any()
.downcast_ref::<NativeSurfaceTextureMetadata>()
.unwrap();
let mut acquire_semaphore = metadata.acquire_semaphores.lock();
let mut present_semaphores = metadata.present_semaphores.lock();
let wait_semaphores = present_semaphores.get_present_wait_semaphores();
// Reset the acquire and present semaphores internal state
// to be ready for the next frame.
//
// We do this before the actual call to present to ensure that
// even if this method errors and early outs, we have reset
// the state for next frame.
acquire_semaphore.end_semaphore_usage();
present_semaphores.end_semaphore_usage();
drop(acquire_semaphore);
let swapchains = [self.raw];
let image_indices = [texture.index];
let vk_info = vk::PresentInfoKHR::default()
.swapchains(&swapchains)
.image_indices(&image_indices)
.wait_semaphores(&wait_semaphores);
let mut display_timing;
let present_times;
let vk_info = if let Some(present_time) = self.next_present_time.take() {
debug_assert!(
self.device
.features
.contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING),
"`next_present_time` should only be set if `VULKAN_GOOGLE_DISPLAY_TIMING` is enabled"
);
present_times = [present_time];
display_timing = vk::PresentTimesInfoGOOGLE::default().times(&present_times);
// SAFETY: We know that VK_GOOGLE_display_timing is present because of the safety contract on `next_present_time`.
vk_info.push_next(&mut display_timing)
} else {
vk_info
};
let suboptimal = {
profiling::scope!("vkQueuePresentKHR");
unsafe { self.functor.queue_present(queue.raw, &vk_info) }.map_err(|error| {
match error {
vk::Result::ERROR_OUT_OF_DATE_KHR => crate::SurfaceError::Outdated,
vk::Result::ERROR_SURFACE_LOST_KHR => crate::SurfaceError::Lost,
// We don't use VK_EXT_full_screen_exclusive
// VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT
_ => map_host_device_oom_and_lost_err(error).into(),
}
})?
};
if suboptimal {
// We treat `VK_SUBOPTIMAL_KHR` as `VK_SUCCESS` on Android.
// On Android 10+, libvulkan's `vkQueuePresentKHR` implementation returns `VK_SUBOPTIMAL_KHR` if not doing pre-rotation
// (i.e `VkSwapchainCreateInfoKHR::preTransform` not being equal to the current device orientation).
// This is always the case when the device orientation is anything other than the identity one, as we unconditionally use `VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR`.
#[cfg(not(target_os = "android"))]
log::debug!("Suboptimal present of frame {}", texture.index);
}
Ok(())
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl NativeSwapchain {
pub(crate) fn as_raw(&self) -> vk::SwapchainKHR {
self.raw
}
pub fn set_next_present_time(&mut self, present_timing: vk::PresentTimeGOOGLE) {
let features = wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING;
if self.device.features.contains(features) {
self.next_present_time = Some(present_timing);
} else {
// Ideally we'd use something like `device.required_features` here, but that's in `wgpu-core`, which we are a dependency of
panic!(
concat!(
"Tried to set display timing properties ",
"without the corresponding feature ({:?}) enabled."
),
features
);
}
}
/// Mark the current frame finished, advancing to the next acquire semaphore.
fn advance_acquire_semaphore(&mut self) {
let semaphore_count = self.acquire_semaphores.len();
self.next_acquire_index = (self.next_acquire_index + 1) % semaphore_count;
}
/// Get the next acquire semaphore that should be used with this swapchain.
fn get_acquire_semaphore(&self) -> Arc<Mutex<SwapchainAcquireSemaphore>> {
self.acquire_semaphores[self.next_acquire_index].clone()
}
/// Get the set of present semaphores that should be used with the given image index.
fn get_present_semaphores(&self, index: u32) -> Arc<Mutex<SwapchainPresentSemaphores>> {
self.present_semaphores[index as usize].clone()
}
}
/// Semaphore used to acquire a swapchain image.
#[derive(Debug)]
struct SwapchainAcquireSemaphore {
/// A semaphore that is signaled when this image is safe for us to modify.
///
/// When [`vkAcquireNextImageKHR`] returns the index of the next swapchain
/// image that we should use, that image may actually still be in use by the
/// presentation engine, and is not yet safe to modify. However, that
/// function does accept a semaphore that it will signal when the image is
/// indeed safe to begin messing with.
///
/// This semaphore is:
///
/// - waited for by the first queue submission to operate on this image
/// since it was acquired, and
///
/// - signaled by [`vkAcquireNextImageKHR`] when the acquired image is ready
/// for us to use.
///
/// [`vkAcquireNextImageKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkAcquireNextImageKHR
acquire: vk::Semaphore,
/// True if the next command submission operating on this image should wait
/// for [`acquire`].
///
/// We must wait for `acquire` before drawing to this swapchain image, but
/// because `wgpu-hal` queue submissions are always strongly ordered, only
/// the first submission that works with a swapchain image actually needs to
/// wait. We set this flag when this image is acquired, and clear it the
/// first time it's passed to [`Queue::submit`] as a surface texture.
///
/// Additionally, semaphores can only be waited on once, so we need to ensure
/// that we only actually pass this semaphore to the first submission that
/// uses that image.
///
/// [`acquire`]: SwapchainAcquireSemaphore::acquire
/// [`Queue::submit`]: crate::Queue::submit
should_wait_for_acquire: bool,
/// The fence value of the last command submission that wrote to this image.
///
/// The next time we try to acquire this image, we'll block until
/// this submission finishes, proving that [`acquire`] is ready to
/// pass to `vkAcquireNextImageKHR` again.
///
/// [`acquire`]: SwapchainAcquireSemaphore::acquire
previously_used_submission_index: crate::FenceValue,
}
impl SwapchainAcquireSemaphore {
fn new(device: &DeviceShared, index: usize) -> Result<Self, crate::DeviceError> {
Ok(Self {
acquire: device
.new_binary_semaphore(&format!("SwapchainImageSemaphore: Index {index} acquire"))?,
should_wait_for_acquire: true,
previously_used_submission_index: 0,
})
}
/// Sets the fence value which the next acquire will wait for. This prevents
/// the semaphore from being used while the previous submission is still in flight.
fn set_used_fence_value(&mut self, value: crate::FenceValue) {
self.previously_used_submission_index = value;
}
/// Return the semaphore that commands drawing to this image should wait for, if any.
///
/// This only returns `Some` once per acquisition; see
/// [`SwapchainAcquireSemaphore::should_wait_for_acquire`] for details.
fn get_acquire_wait_semaphore(&mut self) -> Option<vk::Semaphore> {
if self.should_wait_for_acquire {
self.should_wait_for_acquire = false;
Some(self.acquire)
} else {
None
}
}
/// Indicates the cpu-side usage of this semaphore has finished for the frame,
/// so reset internal state to be ready for the next frame.
fn end_semaphore_usage(&mut self) {
// Reset the acquire semaphore, so that the next time we acquire this
// image, we can wait for it again.
self.should_wait_for_acquire = true;
}
unsafe fn destroy(&self, device: &ash::Device) {
unsafe {
device.destroy_semaphore(self.acquire, None);
}
}
}
#[derive(Debug)]
struct SwapchainPresentSemaphores {
/// A pool of semaphores for ordering presentation after drawing.
///
/// The first [`present_index`] semaphores in this vector are:
///
/// - all waited on by the call to [`vkQueuePresentKHR`] that presents this
/// image, and
///
/// - each signaled by some [`vkQueueSubmit`] queue submission that draws to
/// this image, when the submission finishes execution.
///
/// This vector accumulates one semaphore per submission that writes to this
/// image. This is awkward, but hard to avoid: [`vkQueuePresentKHR`]
/// requires a semaphore to order it with respect to drawing commands, and
/// we can't attach new completion semaphores to a command submission after
/// it's been submitted. This means that, at submission time, we must create
/// the semaphore we might need if the caller's next action is to enqueue a
/// presentation of this image.
///
/// An alternative strategy would be for presentation to enqueue an empty
/// submit, ordered relative to other submits in the usual way, and
/// signaling a single presentation semaphore. But we suspect that submits
/// are usually expensive enough, and semaphores usually cheap enough, that
/// performance-sensitive users will avoid making many submits, so that the
/// cost of accumulated semaphores will usually be less than the cost of an
/// additional submit.
///
/// Only the first [`present_index`] semaphores in the vector are actually
/// going to be signalled by submitted commands, and need to be waited for
/// by the next present call. Any semaphores beyond that index were created
/// for prior presents and are simply being retained for recycling.
///
/// [`present_index`]: SwapchainPresentSemaphores::present_index
/// [`vkQueuePresentKHR`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueuePresentKHR
/// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit
present: Vec<vk::Semaphore>,
/// The number of semaphores in [`present`] to be signalled for this submission.
///
/// [`present`]: SwapchainPresentSemaphores::present
present_index: usize,
/// Which image this semaphore set is used for.
frame_index: usize,
}
impl SwapchainPresentSemaphores {
pub fn new(frame_index: usize) -> Self {
Self {
present: Vec::new(),
present_index: 0,
frame_index,
}
}
/// Return the semaphore that the next submission that writes to this image should
/// signal when it's done.
///
/// See [`SwapchainPresentSemaphores::present`] for details.
fn get_submit_signal_semaphore(
&mut self,
device: &DeviceShared,
) -> Result<vk::Semaphore, crate::DeviceError> {
// Try to recycle a semaphore we created for a previous presentation.
let sem = match self.present.get(self.present_index) {
Some(sem) => *sem,
None => {
let sem = device.new_binary_semaphore(&format!(
"SwapchainImageSemaphore: Image {} present semaphore {}",
self.frame_index, self.present_index
))?;
self.present.push(sem);
sem
}
};
self.present_index += 1;
Ok(sem)
}
/// Indicates the cpu-side usage of this semaphore has finished for the frame,
/// so reset internal state to be ready for the next frame.
fn end_semaphore_usage(&mut self) {
// Reset the index to 0, so that the next time we get a semaphore, we
// start from the beginning of the list.
self.present_index = 0;
}
/// Return the semaphores that a presentation of this image should wait on.
///
/// Return a slice of semaphores that the call to [`vkQueueSubmit`] that
/// ends this image's acquisition should wait for. See
/// [`SwapchainPresentSemaphores::present`] for details.
///
/// Reset `self` to be ready for the next acquisition cycle.
///
/// [`vkQueueSubmit`]: https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#vkQueueSubmit
fn get_present_wait_semaphores(&mut self) -> Vec<vk::Semaphore> {
self.present[0..self.present_index].to_vec()
}
unsafe fn destroy(&self, device: &ash::Device) {
unsafe {
for sem in &self.present {
device.destroy_semaphore(*sem, None);
}
}
}
}
#[derive(Debug)]
struct NativeSurfaceTextureMetadata {
acquire_semaphores: Arc<Mutex<SwapchainAcquireSemaphore>>,
present_semaphores: Arc<Mutex<SwapchainPresentSemaphores>>,
}
impl SurfaceTextureMetadata for NativeSurfaceTextureMetadata {
fn get_semaphore_guard(&self) -> Box<dyn SwapchainSubmissionSemaphoreGuard + '_> {
Box::new(NativeSwapchainSubmissionSemaphoreGuard {
acquire_semaphore_guard: self
.acquire_semaphores
.try_lock()
.expect("Failed to lock surface acquire semaphore"),
present_semaphores_guard: self
.present_semaphores
.try_lock()
.expect("Failed to lock surface present semaphores"),
})
}
fn as_any(&self) -> &dyn Any {
self
}
}
struct NativeSwapchainSubmissionSemaphoreGuard<'a> {
acquire_semaphore_guard: MutexGuard<'a, SwapchainAcquireSemaphore>,
present_semaphores_guard: MutexGuard<'a, SwapchainPresentSemaphores>,
}
impl<'a> SwapchainSubmissionSemaphoreGuard for NativeSwapchainSubmissionSemaphoreGuard<'a> {
fn set_used_fence_value(&mut self, value: u64) {
self.acquire_semaphore_guard.set_used_fence_value(value);
}
fn get_acquire_wait_semaphore(&mut self) -> Option<SemaphoreType> {
self.acquire_semaphore_guard
.get_acquire_wait_semaphore()
.map(SemaphoreType::Binary)
}
fn get_submit_signal_semaphore(
&mut self,
device: &DeviceShared,
) -> Result<SemaphoreType, crate::DeviceError> {
self.present_semaphores_guard
.get_submit_signal_semaphore(device)
.map(SemaphoreType::Binary)
}
}