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

vk: detect prime-select forced to Nvidia #95

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all 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
15 changes: 10 additions & 5 deletions blade-graphics/src/vulkan/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ash::{
vk,
};
use naga::back::spv;
use std::{ffi, mem, sync::Mutex};
use std::{ffi, fs, mem, sync::Mutex};

mod db {
pub mod intel {
Expand All @@ -16,8 +16,6 @@ mod layer {
unsafe { CStr::from_bytes_with_nul_unchecked(b"VK_LAYER_KHRONOS_validation\0") };
pub const MESA_OVERLAY: &CStr =
unsafe { CStr::from_bytes_with_nul_unchecked(b"VK_LAYER_MESA_overlay\0") };
pub const NV_OPTIMUS: &CStr =
unsafe { CStr::from_bytes_with_nul_unchecked(b"VK_LAYER_NV_optimus\0") };
}

const REQUIRED_DEVICE_EXTENSIONS: &[&ffi::CStr] = &[
Expand All @@ -43,6 +41,14 @@ struct SystemBugs {
intel_unable_to_present_on_xorg: bool,
}

// See https://github.com/canonical/nvidia-prime/blob/587c5012be9dddcc17ab4d958f10a24fa3342b4d/prime-select#L56
fn is_nvidia_prime_forced() -> bool {
match fs::read_to_string("/etc/prime-discrete") {
Ok(contents) => contents == "on\n",
Err(_) => false,
}
}

unsafe fn inspect_adapter(
phd: vk::PhysicalDevice,
instance: &super::Instance,
Expand Down Expand Up @@ -341,8 +347,7 @@ impl super::Context {
_ => false,
};
let bugs = SystemBugs {
intel_unable_to_present_on_xorg: is_xorg
&& supported_layer_names.contains(&layer::NV_OPTIMUS),
intel_unable_to_present_on_xorg: is_xorg && is_nvidia_prime_forced(),
};

let vk_surface = surface_handles.map(|(rwh, rdh)| {
Expand Down
Loading