Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hal: cargo feature to allow using VK_GOOGLE_display_timing unsafely #6149

Merged
merged 17 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ By @wumpf in [#6069](https://github.com/gfx-rs/wgpu/pull/6069), [#6099](https://

* Support constant evaluation for `firstLeadingBit` and `firstTrailingBit` numeric built-ins in WGSL. Front-ends that translate to these built-ins also benefit from constant evaluation. By @ErichDonGubler in [#5101](https://github.com/gfx-rs/wgpu/pull/5101).

#### Vulkan

- Allow using [VK_GOOGLE_display_timing](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_GOOGLE_display_timing.html) unsafely with the `VULKAN_GOOGLE_DISPLAY_TIMING` feature. By @DJMcNab in [#6149](https://github.com/gfx-rs/wgpu/pull/6149)

### Bug Fixes

- Fix incorrect hlsl image output type conversion. By @atlv24 in [#6123](https://github.com/gfx-rs/wgpu/pull/6123)
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ features = ["wgsl-in"]
[dev-dependencies]
cfg-if.workspace = true
env_logger.workspace = true
glam.workspace = true # for ray-traced-triangle example
winit.workspace = true # for "halmark" example
glam.workspace = true # for ray-traced-triangle example
winit.workspace = true # for "halmark" example

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
glutin.workspace = true # for "gles" example
12 changes: 11 additions & 1 deletion wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::conv;

use ash::{amd, ext, khr, vk};
use ash::{amd, ext, google, khr, vk};
use parking_lot::Mutex;

use std::{collections::BTreeMap, ffi::CStr, sync::Arc};
Expand Down Expand Up @@ -771,6 +771,11 @@ impl PhysicalDeviceFeatures {
);
}

features.set(
F::VULKAN_GOOGLE_DISPLAY_TIMING,
caps.supports_extension(google::display_timing::NAME),
);

(features, dl_flags)
}

Expand Down Expand Up @@ -1004,6 +1009,11 @@ impl PhysicalDeviceProperties {
extensions.push(khr::shader_atomic_int64::NAME);
}

// Require `VK_GOOGLE_display_timing` if the associated feature was requested
if requested_features.contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING) {
extensions.push(ash::google::display_timing::NAME);
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
}

extensions
}

Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/vulkan/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ impl super::Device {
view_formats: wgt_view_formats,
surface_semaphores,
next_semaphore_index: 0,
next_present_times: None,
})
}

Expand Down
54 changes: 54 additions & 0 deletions wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ struct Swapchain {
/// index as the image index, but we need to specify the semaphore as an argument
/// to the acquire_next_image function which is what tells us which image to use.
next_semaphore_index: usize,
/// The times which will be set in the next present times.
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
///
/// SAFETY: This is only set if [wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING] is enabled, and
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
/// so the `VK_GOOGLE_display_timing` extension is present.
next_present_times: Option<vk::PresentTimeGOOGLE>,
}

impl Swapchain {
Expand All @@ -375,6 +380,37 @@ pub struct Surface {
swapchain: RwLock<Option<Swapchain>>,
}

impl Surface {
/// Get the raw Vulkan swapchain associated with this surface.
///
/// Returns `None` if the surface is not configured.
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
pub fn raw_swapchain(&self) -> Option<vk::SwapchainKHR> {
let read = self.swapchain.read();
read.as_ref().map(|it| it.raw)
}

/// Set the present timing information which will be used for the next presentation using `VK_GOOGLE_display_timing`.
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Panics
///
/// - If the surface hasn't been configured.
/// - If the device doesn't [support present timing](wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING).
#[track_caller]
pub fn set_next_present_times(&self, present_timing: vk::PresentTimeGOOGLE) {
let mut swapchain = self.swapchain.write();
let swapchain = swapchain
.as_mut()
.expect("Surface should have been configured");
let features = wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING;
if swapchain.device.features.contains(features) {
swapchain.next_present_times = 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!("Tried to set display timing properties without the corresponding feature ({features:?}) enabled.");
}
}
}

#[derive(Debug)]
pub struct SurfaceTexture {
index: u32,
Expand Down Expand Up @@ -1158,6 +1194,24 @@ impl crate::Queue for Queue {
.image_indices(&image_indices)
.wait_semaphores(swapchain_semaphores.get_present_wait_semaphores());

let mut display_timing;
let present_times;
let vk_info = if let Some(present_time) = ssc.next_present_times.take() {
debug_assert!(
ssc.device
.features
.contains(wgt::Features::VULKAN_GOOGLE_DISPLAY_TIMING),
"`next_present_times` should only be set if `VULKAN_GOOGLE_DISPLAY_TIMING` is enabled"
);
display_timing = vk::PresentTimesInfoGOOGLE::default();
present_times = [present_time];
display_timing = display_timing.times(&present_times);
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
// Safety: We know that VK_GOOGLE_display_timing is present because of the safety contract on `next_present_times`.
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
vk_info.push_next(&mut display_timing)
} else {
vk_info
};

let suboptimal = {
profiling::scope!("vkQueuePresentKHR");
unsafe { self.swapchain_fn.queue_present(self.raw, &vk_info) }.map_err(|error| {
Expand Down
13 changes: 13 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,19 @@ bitflags::bitflags! {
///
/// This is a native only feature.
const SHADER_INT64_ATOMIC_ALL_OPS = 1 << 61;
/// Allows using the [VK_GOOGLE_display_timing] Vulkan extension.
/// This is used for frame pacing to reduce latency, and is generally only available on Android.
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
///
/// This feature does not have a `wgpu`-level API, and must be accessed via `wgpu-hal`,
/// through various `as_hal` functions.
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
///
/// Supported platforms:
/// - Vulkan (with [VK_GOOGLE_display_timing])
///
/// This is a native only feature.
///
/// [VK_GOOGLE_display_timing]: https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_GOOGLE_display_timing.html
const VULKAN_GOOGLE_DISPLAY_TIMING = 1 << 62;
}
}

Expand Down