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:

  1. WebGL 2.0: GPU.js first tests for WebGL 2 support to achieve the highest performance and feature compatibility.
  2. WebGL 1.0: If WebGL 2 is unavailable, it attempts to fall back to WebGL 1.
  3. 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:

// Force GPU execution; fails if acceleration is unavailable
const gpu = new GPU({ mode: 'gpu' });

Environment Considerations