Allow chaining extension structs onto VkPresentInfoKHR at present time (#9847)

This commit is contained in:
Stuart Parmenter authored and GitHub committed 2026-08-14 17:53:15 -04:00
1 parent 2d6ba336cf
commit 1d92fee75a
3 files changed
+88 -2

No files matched your search

+1
View File
@@ -53,6 +53,7 @@ Bottom level categories:
#### Hal
- Add `BufferBinding::buffer`, a public read accessor for the bound buffer, which was previously inaccessible to out-of-tree `wgpu_hal::Api` implementations. By @danlehmann in [#9820](https://github.com/gfx-rs/wgpu/pull/9820).
- Add `wgpu_hal::vulkan::Surface::set_next_present_chain`, which attaches a caller-provided `pNext` chain to the `VkPresentInfoKHR` of the surface's next presentation. With `Adapter::open_with_callback` to enable the device extension, this supports presentation extensions wgpu has no dedicated support for, such as [VK_NV_present_metering](https://registry.khronos.org/vulkan/specs/latest/man/html/VK_NV_present_metering.html) for metering the display timing of frame-generation frames. By @stuartparmenter in [#9847](https://github.com/gfx-rs/wgpu/pull/9847).
#### Metal
+46 -1
View File
@@ -38,7 +38,14 @@ mod swapchain;
pub use adapter::PhysicalDeviceFeatures;
use alloc::{boxed::Box, ffi::CString, sync::Arc, vec::Vec};
use core::{borrow::Borrow, ffi::CStr, fmt, marker::PhantomData, mem, num::NonZeroU32};
use core::{
borrow::Borrow,
ffi::{c_void, CStr},
fmt,
marker::PhantomData,
mem,
num::NonZeroU32,
};
use arrayvec::ArrayVec;
use ash::{ext, khr, vk};
@@ -275,6 +282,44 @@ impl Surface {
.expect("Surface should have a native Vulkan swapchain")
.set_next_present_time(present_timing);
}
/// Set a `pNext` chain of extension structs to be attached to the [`vk::PresentInfoKHR`]
/// of the next [presentation](crate::Queue::present()) of this surface.
///
/// This supports presentation extensions that `wgpu-hal` has no dedicated support
/// for, such as [VK_NV_present_metering]. The corresponding device extension must be
/// enabled at device creation, e.g. with
/// [`Adapter::open_with_callback()`](super::vulkan::Adapter::open_with_callback).
///
/// The chain is consumed by the next presentation: it replaces any chain set by a
/// previous call that hasn't been presented yet, and is discarded unread if the
/// surface is reconfigured first.
///
/// # Safety
///
/// - `chain` must point to a valid `pNext` chain of structs that can extend
/// [`vk::PresentInfoKHR`], whose extensions are enabled on the device.
/// - The chain must stay valid, and must not be read or written, until the next
/// presentation of this surface completes or the surface is reconfigured. Fields
/// the driver wrote during presentation may be read afterwards.
///
/// # Panics
///
/// - If the surface hasn't been configured.
/// - If the surface has been configured for a DXGI swapchain.
///
/// [VK_NV_present_metering]: https://registry.khronos.org/vulkan/specs/latest/man/html/VK_NV_present_metering.html
#[track_caller]
pub unsafe fn set_next_present_chain(&self, chain: *mut c_void) {
let mut swapchain = self.swapchain.write();
let swapchain = swapchain
.as_mut()
.expect("Surface should have been configured")
.as_any_mut()
.downcast_mut::<swapchain::NativeSwapchain>()
.expect("Surface should have a native Vulkan swapchain");
unsafe { swapchain.set_next_present_chain(chain) };
}
}
#[derive(Debug)]
+41 -1
View File
@@ -299,6 +299,7 @@ impl Surface for NativeSurface {
next_acquire_index: 0,
present_semaphores,
next_present_time: None,
next_present_chain: None,
}))
}
@@ -365,8 +366,26 @@ pub(crate) struct NativeSwapchain {
/// 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>,
/// A caller-provided `pNext` chain to attach to the [`vk::PresentInfoKHR`] of the next
/// call to [`present()`](crate::Queue::present()).
///
/// # Safety
///
/// Set only through
/// [`Surface::set_next_present_chain()`](crate::vulkan::Surface::set_next_present_chain),
/// whose contract keeps the chain valid and unaliased until the next present.
next_present_chain: Option<PresentChain>,
}
/// See [`NativeSwapchain::next_present_chain`].
struct PresentChain(*mut vk::BaseOutStructure<'static>);
// SAFETY: The pointer is only dereferenced during the next present, and the contract on
// `Surface::set_next_present_chain()` keeps the chain valid and unaliased until then.
unsafe impl Send for PresentChain {}
unsafe impl Sync for PresentChain {}
impl Drop for NativeSwapchain {
fn drop(&mut self) {
unsafe {
@@ -595,7 +614,7 @@ impl Swapchain for NativeSwapchain {
let mut display_timing;
let present_times;
let vk_info = if let Some(present_time) = self.next_present_time.take() {
let mut vk_info = if let Some(present_time) = self.next_present_time.take() {
debug_assert!(
self.device
.features
@@ -610,6 +629,20 @@ impl Swapchain for NativeSwapchain {
vk_info
};
if let Some(PresentChain(chain)) = self.next_present_chain.take() {
// Splice the caller's chain in front of anything already chained onto `vk_info`.
// SAFETY: The contract on `Surface::set_next_present_chain()` keeps the chain
// valid and unaliased until this present completes.
unsafe {
let mut tail = chain;
while !(*tail).p_next.is_null() {
tail = (*tail).p_next;
}
(*tail).p_next = vk_info.p_next.cast_mut().cast();
vk_info.p_next = chain.cast();
}
}
let suboptimal = {
profiling::scope!("vkQueuePresentKHR");
unsafe { self.functor.queue_present(queue.raw, &vk_info) }.map_err(|error| {
@@ -663,6 +696,13 @@ impl NativeSwapchain {
}
}
/// # Safety
///
/// See [`Surface::set_next_present_chain()`](crate::vulkan::Surface::set_next_present_chain).
pub unsafe fn set_next_present_chain(&mut self, chain: *mut core::ffi::c_void) {
self.next_present_chain = Some(PresentChain(chain.cast()));
}
/// Mark the current frame finished, advancing to the next acquire semaphore.
fn advance_acquire_semaphore(&mut self) {
let semaphore_count = self.acquire_semaphores.len();