Skip to content

Commit

Permalink
vk: detect prime-select forced to Nvidia
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Mar 18, 2024
1 parent 1f00a40 commit 94ed644
Showing 1 changed file with 10 additions and 5 deletions.
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

0 comments on commit 94ed644

Please sign in to comment.