From e550c5c77b2a71e77da73ca0fe94778677db5994 Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 12 Nov 2026 14:15:20 +0100 Subject: [PATCH] Let the user draw the studio with the NVIDIA GPU A setting in the settings dialog, off by default and shown only on Linux with an NVIDIA GPU and its Vulkan driver. On Wayland, Chromium hands the desktop buffers from the GPU it draws with, so a desktop composited on the integrated GPU shows an empty window. With the setting on, the studio restarts through XWayland with Chromium on Vulkan and only NVIDIA's Vulkan driver visible; that driver copies each frame across itself. --- studio/src/main/dedicatedGpu.ts | 80 +++++++++++++++++++ studio/src/main/index.ts | 58 ++++++++++++-- studio/src/preload/index.ts | 4 +- .../renderer/src/studio/ShortcutSettings.tsx | 30 ++++++- .../src/renderer/src/studio/StudioToolbar.tsx | 2 +- studio/src/shared/bridge.ts | 9 +++ 6 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 studio/src/main/dedicatedGpu.ts diff --git a/studio/src/main/dedicatedGpu.ts b/studio/src/main/dedicatedGpu.ts new file mode 100644 index 000000000..4c9ebf1d8 --- /dev/null +++ b/studio/src/main/dedicatedGpu.ts @@ -0,0 +1,80 @@ +import { existsSync, readdirSync, readFileSync } from 'node:fs' +import { join } from 'node:path' + +const NVIDIA_VENDOR_ID = '0x10de' +const DRM_FOLDER = '/sys/class/drm' +const RENDER_NODE_PREFIX = 'renderD' +// Where the Vulkan loader looks for drivers, NixOS's first. +const VULKAN_DRIVER_FOLDERS = ['/run/opengl-driver/share/vulkan/icd.d', '/usr/share/vulkan/icd.d', '/etc/vulkan/icd.d'] +const NVIDIA_VULKAN_DRIVER_PREFIX = 'nvidia_icd' +const VULKAN_DRIVER_VARIABLES = ['VK_DRIVER_FILES', 'VK_ICD_FILENAMES'] + +/** + * Chromium on Wayland hands the desktop buffers made on the GPU it draws with, and a desktop that composites on the + * integrated GPU cannot show buffers of an NVIDIA GPU: the window stays empty. Through XWayland with Chromium drawing + * on Vulkan, NVIDIA's Vulkan driver copies each frame to the integrated GPU itself, as it does for games. + */ +export const DEDICATED_GPU_SWITCHES = ['--ozone-platform=x11', '--use-angle=vulkan', '--enable-features=Vulkan,VulkanFromANGLE,DefaultANGLEVulkan'] + +/** NVIDIA's Vulkan driver file on a Linux machine with an NVIDIA GPU; null where there is no such GPU or driver. */ +export function dedicatedGpuDriver(): string | null { + if (process.platform !== 'linux' || !hasNvidiaGpu()) { + return null + } + for (const folder of VULKAN_DRIVER_FOLDERS) { + const driver = driverIn(folder) + if (driver !== null) { + return driver + } + } + return null +} + +/** Shows Chromium only the given Vulkan driver, so it draws on that driver's GPU. */ +export function showOnlyVulkanDriver(env: NodeJS.ProcessEnv, driver: string): void { + for (const variable of VULKAN_DRIVER_VARIABLES) { + env[variable] = driver + } +} + +export function showEveryVulkanDriver(env: NodeJS.ProcessEnv): void { + for (const variable of VULKAN_DRIVER_VARIABLES) { + delete env[variable] + } +} + +export function runsOnDedicatedGpu(argv: string[]): boolean { + return DEDICATED_GPU_SWITCHES.every((option) => argv.includes(option)) +} + +/** The arguments with the dedicated GPU's switches in place of any the studio was started with under the same names. */ +export function withDedicatedGpuSwitches(argv: string[]): string[] { + const names = DEDICATED_GPU_SWITCHES.map(switchName) + return [...argv.filter((option) => !names.includes(switchName(option))), ...DEDICATED_GPU_SWITCHES] +} + +export function withoutDedicatedGpuSwitches(argv: string[]): string[] { + return argv.filter((option) => !DEDICATED_GPU_SWITCHES.includes(option)) +} + +function switchName(option: string): string { + return option.split('=')[0] +} + +function hasNvidiaGpu(): boolean { + try { + return readdirSync(DRM_FOLDER) + .filter((name) => name.startsWith(RENDER_NODE_PREFIX)) + .some((name) => readFileSync(join(DRM_FOLDER, name, 'device', 'vendor'), 'utf8').trim() === NVIDIA_VENDOR_ID) + } catch { + return false + } +} + +function driverIn(folder: string): string | null { + if (!existsSync(folder)) { + return null + } + const file = readdirSync(folder).find((name) => name.startsWith(NVIDIA_VULKAN_DRIVER_PREFIX)) + return file === undefined ? null : join(folder, file) +} diff --git a/studio/src/main/index.ts b/studio/src/main/index.ts index f2213f1c2..749351ccb 100644 --- a/studio/src/main/index.ts +++ b/studio/src/main/index.ts @@ -1,12 +1,22 @@ import { app, BrowserWindow, clipboard, ipcMain, nativeTheme, shell } from 'electron' +import { readFileSync } from 'node:fs' import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises' import { homedir } from 'node:os' import { dirname, join } from 'node:path' -import type { BridgeConnection, StoredShortcuts, ThemeChoice } from '../shared/bridge' +import type { BridgeConnection, GraphicsInfo, StoredShortcuts, ThemeChoice } from '../shared/bridge' +import { + dedicatedGpuDriver, + runsOnDedicatedGpu, + showEveryVulkanDriver, + showOnlyVulkanDriver, + withDedicatedGpuSwitches, + withoutDedicatedGpuSwitches +} from './dedicatedGpu' const CONNECTION_FOLDER = join(homedir(), '.iris', 'studio', 'bridges') const SETTINGS_FILE = 'studio.json' const SHORTCUTS_KEY = 'shortcuts' +const DEDICATED_GPU_KEY = 'dedicatedGpu' const SETTINGS_INDENT = 2 const LINK_PREFIX = 'iris-studio://' const DARK_BACKGROUND = '#1e1f22' @@ -14,6 +24,7 @@ const WINDOW_SIZE = { width: 1440, height: 880, minWidth: 820, minHeight: 520 } let window: BrowserWindow | null = null let pendingLink: string | null = findLink(process.argv) +const dedicatedGpu = dedicatedGpuDriver() function findLink(argv: string[]): string | null { return argv.find((argument) => argument.startsWith(LINK_PREFIX)) ?? null @@ -70,16 +81,47 @@ async function readSettings(): Promise> { } } -async function saveShortcuts(shortcuts: StoredShortcuts): Promise { +async function saveSetting(key: string, value: unknown): Promise { try { - const settings = { ...(await readSettings()), [SHORTCUTS_KEY]: shortcuts } + const settings = { ...(await readSettings()), [key]: value } await mkdir(dirname(settingsPath()), { recursive: true }) await writeFile(settingsPath(), JSON.stringify(settings, null, SETTINGS_INDENT)) } catch (failure) { - console.error(`Could not save the studio shortcuts to ${settingsPath()}`, failure) + console.error(`Could not save the studio setting ${key} to ${settingsPath()}`, failure) } } +/** Read before the app is ready, as the GPU the studio draws on is chosen by how it was started. */ +function choosesDedicatedGpu(): boolean { + try { + const settings: unknown = JSON.parse(readFileSync(settingsPath(), 'utf8')) + return typeof settings === 'object' && settings !== null && (settings as Record)[DEDICATED_GPU_KEY] === true + } catch { + return false + } +} + +function restartOnGpu(dedicated: boolean): void { + const args = process.argv.slice(1) + if (dedicated && dedicatedGpu !== null) { + showOnlyVulkanDriver(process.env, dedicatedGpu) + app.relaunch({ args: withDedicatedGpuSwitches(args) }) + } else { + showEveryVulkanDriver(process.env) + app.relaunch({ args: withoutDedicatedGpuSwitches(args) }) + } + app.exit(0) +} + +async function useDedicatedGpu(dedicated: boolean): Promise { + await saveSetting(DEDICATED_GPU_KEY, dedicated) + restartOnGpu(dedicated) +} + +function graphicsInfo(): GraphicsInfo { + return { canUseDedicatedGpu: dedicatedGpu !== null, usesDedicatedGpu: runsOnDedicatedGpu(process.argv) } +} + function createWindow(): void { window = new BrowserWindow({ ...WINDOW_SIZE, @@ -117,7 +159,9 @@ function deliverLink(link: string): void { window.focus() } -if (!app.requestSingleInstanceLock()) { +if (dedicatedGpu !== null && choosesDedicatedGpu() && !runsOnDedicatedGpu(process.argv)) { + restartOnGpu(true) +} else if (!app.requestSingleInstanceLock()) { app.quit() } else { app.setAsDefaultProtocolClient('iris-studio') @@ -144,7 +188,9 @@ if (!app.requestSingleInstanceLock()) { nativeTheme.themeSource = theme }) ipcMain.handle('studio:load-shortcuts', async () => (await readSettings())[SHORTCUTS_KEY] ?? {}) - ipcMain.on('studio:save-shortcuts', (_event, shortcuts: StoredShortcuts) => void saveShortcuts(shortcuts)) + ipcMain.on('studio:save-shortcuts', (_event, shortcuts: StoredShortcuts) => void saveSetting(SHORTCUTS_KEY, shortcuts)) + ipcMain.handle('studio:graphics', graphicsInfo) + ipcMain.on('studio:use-dedicated-gpu', (_event, dedicated: boolean) => void useDedicatedGpu(dedicated)) ipcMain.on('studio:copy-text', (_event, text: string) => clipboard.writeText(text)) void app.whenReady().then(createWindow) diff --git a/studio/src/preload/index.ts b/studio/src/preload/index.ts index 1aea7c553..684dc4516 100644 --- a/studio/src/preload/index.ts +++ b/studio/src/preload/index.ts @@ -12,7 +12,9 @@ const host: StudioHost = { setTheme: (theme: ThemeChoice) => ipcRenderer.send('studio:set-theme', theme), copyText: (text: string) => ipcRenderer.send('studio:copy-text', text), loadShortcuts: () => ipcRenderer.invoke('studio:load-shortcuts'), - saveShortcuts: (shortcuts: StoredShortcuts) => ipcRenderer.send('studio:save-shortcuts', shortcuts) + saveShortcuts: (shortcuts: StoredShortcuts) => ipcRenderer.send('studio:save-shortcuts', shortcuts), + graphics: () => ipcRenderer.invoke('studio:graphics'), + useDedicatedGpu: (dedicated: boolean) => ipcRenderer.send('studio:use-dedicated-gpu', dedicated) } contextBridge.exposeInMainWorld('studio', host) diff --git a/studio/src/renderer/src/studio/ShortcutSettings.tsx b/studio/src/renderer/src/studio/ShortcutSettings.tsx index 6573ec878..474470daa 100644 --- a/studio/src/renderer/src/studio/ShortcutSettings.tsx +++ b/studio/src/renderer/src/studio/ShortcutSettings.tsx @@ -2,6 +2,7 @@ import { useEffect, useMemo, useState } from 'react' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { cn } from '@/lib/utils' +import type { GraphicsInfo } from '../../../shared/bridge' import type { ViewInfo } from '../bridge/types' import { chordOf, chordText, chordsOf, chordsText, clashesWith, defaultChords, shortcutRows, type ShortcutGroup, type ShortcutRow } from './shortcuts' import { shortcutStore, useBindings } from './useBindings' @@ -62,10 +63,11 @@ export function ShortcutSettings({ open, onOpenChange, views }: ShortcutSettings - Shortcuts + Settings Click a shortcut's keys, then press the new key. Esc can be chosen too; click the keys again to cancel.
+ {GROUPS.map((group) => (

{group}

@@ -107,6 +109,32 @@ export function ShortcutSettings({ open, onOpenChange, views }: ShortcutSettings ) } +/** The GPU choice, shown only where there is an NVIDIA GPU to choose. Changing it restarts the studio. */ +function GraphicsSection(): React.JSX.Element | null { + const [graphics, setGraphics] = useState(null) + + useEffect(() => { + void window.studio.graphics().then(setGraphics) + }, []) + + if (graphics === null || !graphics.canUseDedicatedGpu) { + return null + } + const dedicated = graphics.usesDedicatedGpu + return ( +
+

Graphics

+
+ Draw with the NVIDIA GPU (restarts) + + +
+
+ ) +} + interface ShortcutRowViewProps { row: ShortcutRow keys: string[] diff --git a/studio/src/renderer/src/studio/StudioToolbar.tsx b/studio/src/renderer/src/studio/StudioToolbar.tsx index f6b4c9e98..3d4f41a4d 100644 --- a/studio/src/renderer/src/studio/StudioToolbar.tsx +++ b/studio/src/renderer/src/studio/StudioToolbar.tsx @@ -44,7 +44,7 @@ export function StudioToolbar({ world, theme, onHelp, onSettings, onDisconnect } - + diff --git a/studio/src/shared/bridge.ts b/studio/src/shared/bridge.ts index 2fe5b05cf..31448d371 100644 --- a/studio/src/shared/bridge.ts +++ b/studio/src/shared/bridge.ts @@ -11,6 +11,12 @@ export type StoredShortcuts = Record export type ThemeChoice = 'system' | 'light' | 'dark' +export interface GraphicsInfo { + /** Whether this machine has an NVIDIA GPU the studio can be asked to draw on. */ + canUseDedicatedGpu: boolean + usesDedicatedGpu: boolean +} + /** What the preload script hands the renderer. */ export interface StudioHost { listBridges(): Promise @@ -22,6 +28,9 @@ export interface StudioHost { /** The rebound shortcuts from the studio settings, as the file holds them, for the caller to check. */ loadShortcuts(): Promise saveShortcuts(shortcuts: StoredShortcuts): void + graphics(): Promise + /** Saves the choice and restarts the studio, which only picks its GPU when it starts. */ + useDedicatedGpu(dedicated: boolean): void } const LINK_PROTOCOL = 'iris-studio:'