Does GPU.js Automatically Detect Hardware Acceleration?
GPU.js automatically detects whether hardware acceleration is available on the host machine and adjusts its execution pipeline accordingly. This article explains how GPU.js handles hardware detection by default, how its automatic fallback mechanism works, and how developers can programmatically check or enforce GPU acceleration in both browser and Node.js environments.
Default Detection Behavior
GPU.js is designed to run computations on the GPU using WebGL whenever possible, but it does not crash if graphics hardware is missing or unsupported. When you initialize a new instance without defining specific configuration settings, the library automatically probes the host environment in the following order:
- WebGL 2.0: GPU.js first tests for WebGL 2 support to achieve the highest performance and feature compatibility.
- WebGL 1.0: If WebGL 2 is unavailable, it attempts to fall back to WebGL 1.
- CPU Execution: If no functional WebGL context can be created—due to disabled hardware acceleration, missing graphics drivers, or an incompatible platform—GPU.js automatically falls back to running the kernel in pure JavaScript on the CPU.
Because of this cascading detection model, your code will execute regardless of the host's underlying hardware capabilities.
Checking Hardware Acceleration Programmatically
You can inspect the environment before or after running kernels to verify whether hardware acceleration is actively being used.
Pre-Check Using Static Methods
Before creating a GPU instance, you can check if the host supports GPU processing:
const isSupported = GPU.isGPUSupported;
if (isSupported) {
console.log("Hardware acceleration is available.");
} else {
console.log("No GPU acceleration available. Code will run on the CPU.");
}Checking Active Mode on an Instance
Once a GPU instance is initialized, you can check which
mode it resolved to:
const gpu = new GPU();
console.log(gpu.mode); // Outputs 'gpu' or 'cpu'Controlling the Detection Mode
While automatic detection is the default behavior, you can override
it using the mode option during instantiation:
- Auto (Default): Omitting the mode option or passing no arguments allows GPU.js to attempt WebGL execution and silently fall back to the CPU if hardware acceleration fails.
- Enforce GPU (
mode: 'gpu'): Forces the instance to use the GPU. If hardware acceleration is disabled or unsupported on the machine, GPU.js will throw an error rather than falling back to the CPU. - Enforce CPU (
mode: 'cpu'): Bypasses all hardware acceleration checks and runs the computation entirely on the CPU using standard JavaScript loops. - Development Mode (
mode: 'dev'): Runs the kernels directly in standard JavaScript to assist with debugging and breakpoint inspection.
// Force GPU execution; fails if acceleration is unavailable
const gpu = new GPU({ mode: 'gpu' });Environment Considerations
- Browsers: Automatic detection depends on the browser's internal hardware acceleration settings and the host machine's graphics drivers. If a user disables hardware acceleration in their browser settings, GPU.js detects that WebGL is disabled and switches to CPU mode.
- Node.js: Server-side environments do not have a
native display or built-in WebGL context. For GPU.js to detect and use
hardware acceleration in Node.js, headless GL packages (such as
gl) must be installed. If absent, GPU.js automatically switches to CPU mode.