ed751714e0
Compose NGX and HDR Vulkan device creation, retain one SDK per device and a context per output size, and evaluate DLSS with existing motion/depth/jitter. Bracket NGX with explicit display-linear color conversion and ordered command buffers. Native TAA/TAAU remains the fallback; Windows compile check passes, runtime acceptance still requires RTX. Co-Authored-By: Kimi Code <noreply@moonshot.ai>
143 lines
5.6 KiB
Rust
143 lines
5.6 KiB
Rust
//! HDR metadata for the swapchain: the Vulkan calls the game crate, which
|
|
//! forbids unsafe code, can't make itself. wgpu sets no HDR metadata, and
|
|
//! without it a compositor may clip or tone-map everything above 1.0 (80 nits)
|
|
//! of an scRGB swapchain.
|
|
|
|
/// Opens a Vulkan device with `VK_EXT_hdr_metadata` too, when the GPU offers
|
|
/// it, so [`set_metadata`] can tell the compositor the real peak brightness.
|
|
/// `None` leaves opening to wgpu.
|
|
#[cfg(not(target_os = "macos"))]
|
|
pub fn open_vulkan_device(
|
|
adapter: &wgpu::Adapter,
|
|
desc: &wgpu::DeviceDescriptor<'_>,
|
|
) -> Option<Result<(wgpu::Device, wgpu::Queue), wgpu::RequestDeviceError>> {
|
|
use wgpu::hal::api::Vulkan;
|
|
let open = {
|
|
// Safety: only read and opened from here; dropped before wgpu takes it back.
|
|
let hal = unsafe { adapter.as_hal::<Vulkan>() }?;
|
|
let instance = hal.shared_instance().raw_instance();
|
|
// Safety: the adapter's own physical device on its own instance.
|
|
let offered =
|
|
unsafe { instance.enumerate_device_extension_properties(hal.raw_physical_device()) }
|
|
.ok()?
|
|
.iter()
|
|
.any(|e| e.extension_name_as_c_str() == Ok(ash::ext::hdr_metadata::NAME));
|
|
if !offered {
|
|
return None;
|
|
}
|
|
// Safety: what wgpu's own request_device does, with one more extension.
|
|
unsafe {
|
|
hal.open_with_callback(
|
|
desc.required_features,
|
|
&desc.required_limits,
|
|
&desc.memory_hints,
|
|
Some(Box::new(
|
|
|args: wgpu::hal::vulkan::CreateDeviceCallbackArgs| {
|
|
args.extensions.push(ash::ext::hdr_metadata::NAME);
|
|
},
|
|
)),
|
|
)
|
|
}
|
|
.ok()?
|
|
};
|
|
// Safety: opened from this adapter with exactly `desc`'s features.
|
|
Some(unsafe { adapter.create_device_from_hal(open, desc) })
|
|
}
|
|
|
|
/// Opens a Vulkan device and lets the caller add required extensions in the same callback.
|
|
/// Used by vendor backends that must compose with, rather than replace, HDR device creation.
|
|
#[cfg(not(target_os = "macos"))]
|
|
pub fn open_vulkan_device_with_callback(
|
|
adapter: &wgpu::Adapter,
|
|
desc: &wgpu::DeviceDescriptor<'_>,
|
|
mut register: impl FnMut(&wgpu::hal::vulkan::Adapter, &mut wgpu::hal::vulkan::CreateDeviceCallbackArgs),
|
|
) -> Option<Result<(wgpu::Device, wgpu::Queue), wgpu::RequestDeviceError>> {
|
|
use wgpu::hal::api::Vulkan;
|
|
let open = {
|
|
// Safety: the adapter and callback stay borrowed for the duration of opening the device.
|
|
let hal = unsafe { adapter.as_hal::<Vulkan>() }?;
|
|
unsafe {
|
|
hal.open_with_callback(
|
|
desc.required_features,
|
|
&desc.required_limits,
|
|
&desc.memory_hints,
|
|
Some(Box::new(|mut args| {
|
|
if instance_supports_hdr(&hal) {
|
|
args.extensions.push(ash::ext::hdr_metadata::NAME);
|
|
}
|
|
register(&hal, &mut args);
|
|
})),
|
|
)
|
|
}
|
|
.ok()?
|
|
};
|
|
// Safety: opened from this adapter with exactly `desc`'s features and the registered extensions.
|
|
Some(unsafe { adapter.create_device_from_hal(open, desc) })
|
|
}
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
fn instance_supports_hdr(adapter: &wgpu::hal::vulkan::Adapter) -> bool {
|
|
// Safety: only extension metadata of this adapter's live instance is queried.
|
|
unsafe {
|
|
adapter
|
|
.shared_instance()
|
|
.raw_instance()
|
|
.enumerate_device_extension_properties(adapter.raw_physical_device())
|
|
.is_ok_and(|extensions| {
|
|
extensions
|
|
.iter()
|
|
.any(|e| e.extension_name_as_c_str() == Ok(ash::ext::hdr_metadata::NAME))
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Tells the compositor, through `VK_EXT_hdr_metadata`, that the swapchain's
|
|
/// content is BT.709 and peaks at `peak` nits. Needs a device from
|
|
/// `open_vulkan_device` and a configured surface; does nothing otherwise.
|
|
pub fn set_metadata(
|
|
device: &wgpu::Device,
|
|
surface: &wgpu::Surface<'_>,
|
|
paper_white: f32,
|
|
peak: f32,
|
|
) {
|
|
#[cfg(not(target_os = "macos"))]
|
|
{
|
|
use wgpu::hal::api::Vulkan;
|
|
// Safety: read-only access to handles wgpu keeps alive while these guards live.
|
|
let (Some(hal_device), Some(hal_surface)) =
|
|
(unsafe { device.as_hal::<Vulkan>() }, unsafe {
|
|
surface.as_hal::<Vulkan>()
|
|
})
|
|
else {
|
|
return;
|
|
};
|
|
if !hal_device
|
|
.enabled_device_extensions()
|
|
.contains(&ash::ext::hdr_metadata::NAME)
|
|
{
|
|
return;
|
|
}
|
|
let Some(swapchain) = hal_surface.raw_native_swapchain() else {
|
|
return;
|
|
};
|
|
let xy = |x, y| ash::vk::XYColorEXT { x, y };
|
|
let metadata = ash::vk::HdrMetadataEXT::default()
|
|
.display_primary_red(xy(0.64, 0.33))
|
|
.display_primary_green(xy(0.30, 0.60))
|
|
.display_primary_blue(xy(0.15, 0.06))
|
|
.white_point(xy(0.3127, 0.3290))
|
|
.max_luminance(peak)
|
|
.min_luminance(0.0)
|
|
.max_content_light_level(peak)
|
|
.max_frame_average_light_level(paper_white.min(peak));
|
|
let loader = ash::ext::hdr_metadata::Device::new(
|
|
hal_device.shared_instance().raw_instance(),
|
|
hal_device.raw_device(),
|
|
);
|
|
// Safety: a live swapchain of this device, one metadata entry for it.
|
|
unsafe { loader.set_hdr_metadata(&[swapchain], &[metadata]) };
|
|
}
|
|
#[cfg(target_os = "macos")]
|
|
let _ = (device, surface, paper_white, peak);
|
|
}
|