Files
Iris/studio/src/main/discreteGpu.ts
T
piexel ebd31db06f Draw the 3D world on the discrete GPU, off the main thread
The studio drew on the integrated GPU of a hybrid laptop, where a view
of the near ground block by block ran at a few frames a second. On
Linux it now starts again, once, with NVIDIA's EGL driver and PRIME
offload, its window's buffers made on the NVIDIA render node, unless
the environment already names an EGL driver.

Tiles are built on worker threads and sent back packed small:
positions in sixteenths of a block, colours a byte a channel. A frame
hands the GPU at most 16 MB of new meshes, and a tile keeps its old
mesh until the finer ones filling its holes are drawn.

The ground is drawn block by block until a block is a quarter of a
pixel, with up to 256 tiles in sight. Runs now record the plants on
their tops, which tint them as the near ground's plants do, and faces
of neighbouring columns in one plane over the same heights merge.
2026-11-12 09:20:20 +01:00

48 lines
2.3 KiB
TypeScript

import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
const NVIDIA_VENDOR_ID = '0x10de'
const DRM_FOLDER = '/sys/class/drm'
const DEVICE_FOLDER = '/dev/dri'
const RENDER_NODE_PREFIX = 'renderD'
// Where glvnd looks for EGL drivers, NixOS's first.
const EGL_VENDOR_FOLDERS = ['/run/opengl-driver/share/glvnd/egl_vendor.d', '/usr/share/glvnd/egl_vendor.d', '/etc/glvnd/egl_vendor.d']
const NVIDIA_EGL_VENDOR_FILE = '10_nvidia.json'
/**
* On Linux, Chromium draws with the default EGL driver, which on a laptop with an NVIDIA GPU beside the integrated one
* is the integrated GPU's: there the 3D world, tens of millions of triangles, runs at a few frames a second where the
* NVIDIA GPU keeps 60. Where there is an NVIDIA GPU and its EGL driver, this sets the environment that points the GPU
* process at that driver with PRIME render offload. An environment that already names an EGL driver is left as it is,
* which is also how to keep the integrated GPU.
*
* @return the NVIDIA GPU's render node, on which the window's buffers must be made too, as the NVIDIA driver cannot draw
* into buffers of the integrated GPU; null where nothing changed. The GPU process has read the environment by the
* time the app's code runs, so the app has to start again, with the render node, for the change to count.
*/
export function preferDiscreteGpu(env: NodeJS.ProcessEnv): string | null {
if (process.platform !== 'linux' || env.__EGL_VENDOR_LIBRARY_FILENAMES !== undefined) {
return null
}
const renderNode = nvidiaRenderNode()
const driver = EGL_VENDOR_FOLDERS.map((folder) => join(folder, NVIDIA_EGL_VENDOR_FILE)).find((file) => existsSync(file))
if (renderNode === null || driver === undefined) {
return null
}
env.__EGL_VENDOR_LIBRARY_FILENAMES = driver
env.__NV_PRIME_RENDER_OFFLOAD = '1'
env.__GLX_VENDOR_LIBRARY_NAME = 'nvidia'
return renderNode
}
function nvidiaRenderNode(): string | null {
try {
const node = readdirSync(DRM_FOLDER)
.filter((name) => name.startsWith(RENDER_NODE_PREFIX))
.find((name) => readFileSync(join(DRM_FOLDER, name, 'device', 'vendor'), 'utf8').trim() === NVIDIA_VENDOR_ID)
return node === undefined ? null : join(DEVICE_FOLDER, node)
} catch {
return null
}
}