From 5e921d283396002cb8b9f71f9a84f947bfc814d9 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Thu, 19 Mar 2026 20:55:18 +0545 Subject: [PATCH] feat(src/commands): add run_on_dgpu command to launch applications on discrete GPU MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Running applications on the dGPU now requires proper environment variable setup and runtime mode checking. The implementation includes warnings when the system is not in hybrid mode and logs the launched command and its exit status. - New Feature: Implemented `run_on_dgpu` function to execute commands with NVIDIA offload environment variables. - Enhancement: Added runtime mode verification and warning for non‑hybrid configurations. - Enhancement: Added informational logging for launched command and process exit status. --- src/commands/gpu.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/commands/gpu.rs b/src/commands/gpu.rs index cb80a18..4807750 100644 --- a/src/commands/gpu.rs +++ b/src/commands/gpu.rs @@ -485,6 +485,43 @@ fn switch_nvidia( println!("Please reboot your computer for changes to take effect!"); } +fn run_on_dgpu(command: &[String]) { + if command.is_empty() { + error!("No command provided to run."); + return; + } + + if get_current_mode() != "hybrid" { + log::warn!( + "Running on dGPU is typically only effective in Hybrid mode. \ + Your current mode is '{}'. The application may fail to start or \ + not use the NVIDIA GPU.", + get_current_mode() + ); + } + + log::info!("Launching '{}' on the discrete GPU...", command.join(" ")); + + let mut child = Command::new(&command[0]); + if command.len() > 1 { + child.args(&command[1..]); + } + + // Set PRIME offload variables natively + child.env("__NV_PRIME_RENDER_OFFLOAD", "1"); + child.env("__GLX_VENDOR_LIBRARY_NAME", "nvidia"); + child.env("__VK_LAYER_NV_optimus", "NVIDIA_only"); + + match child.status() { + Ok(status) => { + log::info!("Application exited with: {}", status); + } + Err(e) => { + error!("Failed to launch application: {}", e); + } + } +} + pub fn execute(cmd: &GpuCommands) { match cmd { GpuCommands::Query => { @@ -551,5 +588,6 @@ pub fn execute(cmd: &GpuCommands) { *use_nvidia_current, *wayland, ), + GpuCommands::Run { command } => run_on_dgpu(command), } }