mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
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.
This commit is contained in:
1 parent
88c43590fe
commit
e550c5c77b
6 files changed
+174
-9
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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<Record<string, unknown>> {
|
||||
}
|
||||
}
|
||||
|
||||
async function saveShortcuts(shortcuts: StoredShortcuts): Promise<void> {
|
||||
async function saveSetting(key: string, value: unknown): Promise<void> {
|
||||
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<string, unknown>)[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<void> {
|
||||
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)
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-[560px] bg-card">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="text-[15px]">Shortcuts</DialogTitle>
|
||||
<DialogTitle className="text-[15px]">Settings</DialogTitle>
|
||||
<DialogDescription>Click a shortcut's keys, then press the new key. Esc can be chosen too; click the keys again to cancel.</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="flex max-h-[60vh] flex-col gap-3 overflow-y-auto pr-1">
|
||||
<GraphicsSection />
|
||||
{GROUPS.map((group) => (
|
||||
<section key={group}>
|
||||
<h3 className="mb-1 text-[11px] font-semibold text-muted-foreground">{group}</h3>
|
||||
@@ -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<GraphicsInfo | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
void window.studio.graphics().then(setGraphics)
|
||||
}, [])
|
||||
|
||||
if (graphics === null || !graphics.canUseDedicatedGpu) {
|
||||
return null
|
||||
}
|
||||
const dedicated = graphics.usesDedicatedGpu
|
||||
return (
|
||||
<section>
|
||||
<h3 className="mb-1 text-[11px] font-semibold text-muted-foreground">Graphics</h3>
|
||||
<div className="flex items-center gap-2 py-0.5">
|
||||
<span className="min-w-0 flex-1 truncate text-secondary-text">Draw with the NVIDIA GPU (restarts)</span>
|
||||
<Button variant="outline" size="xs" className="w-36" onClick={() => window.studio.useDedicatedGpu(!dedicated)}>
|
||||
{dedicated ? 'On' : 'Off'}
|
||||
</Button>
|
||||
<span className="w-14" />
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
interface ShortcutRowViewProps {
|
||||
row: ShortcutRow
|
||||
keys: string[]
|
||||
|
||||
@@ -44,7 +44,7 @@ export function StudioToolbar({ world, theme, onHelp, onSettings, onDisconnect }
|
||||
<ToolButton action="help" hint="Show every mouse and key shortcut" onClick={onHelp}>
|
||||
<IdeIcon name="help" />
|
||||
</ToolButton>
|
||||
<IconHint hint="Change the studio's keys">
|
||||
<IconHint hint="Settings">
|
||||
<Button variant="ghost" size="icon-sm" onClick={onSettings}>
|
||||
<IdeIcon name="settings" />
|
||||
</Button>
|
||||
|
||||
@@ -11,6 +11,12 @@ export type StoredShortcuts = Record<string, string[]>
|
||||
|
||||
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<BridgeConnection[]>
|
||||
@@ -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<unknown>
|
||||
saveShortcuts(shortcuts: StoredShortcuts): void
|
||||
graphics(): Promise<GraphicsInfo>
|
||||
/** Saves the choice and restarts the studio, which only picks its GPU when it starts. */
|
||||
useDedicatedGpu(dedicated: boolean): void
|
||||
}
|
||||
|
||||
const LINK_PROTOCOL = 'iris-studio:'
|
||||
|
||||
Reference in new issue
Block a user