Skip to main content

luma

The luma namespace provides luma.gl applications with the ability to create Device class instances against GPU Adapters that bring support for different GPU backends such as WebGPU and WebGL.

The returned Device instances provide luma.gl applications with a complete GPU API.

Device Registration​

The @luma.gl/core module defines abstract API interfaces such as Device, Buffer etc and is not usable on its own.

One or more GPU backend modules must be imported from a corresponding GPU API backend module (@luma.gl/core/webgl and/or @luma.gl/core/webgpu) and then registered with luma.gl.

Usage​

Create a WebGL 2 context (throws if WebGL2 not supported)

import {luma} from '@luma.gl/core/api';
import {webgl2Adapter} from '@luma.gl/core/webgl';

const webgpuDevice = luma.createDevice({type: 'webgl', adapters: [webgl2Adapter], createCanvasContext: true});
const webgpuDevice = luma.createDevice({
type: 'best-available',
createCanvasContext: true,
adapters: [webgl2Adapter, WebGPUDevice]
});

To pre-register a device backend, import the corresponding device backend module and then call luma.registerAdapters()

Register the WebGL backend, then create a WebGL2 context, auto creating a canvas

import {luma} from '@luma.gl/core/api';
import {webgl2Adapter} from '@luma.gl/core/webgl';
luma.registerAdapters([webgl2Adapter]);
const webglDevice = luma.createDevice({type: 'webgl', createCanvasContext: {canvas: ...}});

It is possible to register more than one device to create an application that can work in both WebGL and WebGPU environments (choosing what to use depending on what the current browser supports).

import {luma} from '@luma.gl/core/api';
import {webgl2Adapter} from '@luma.gl/core/webgl';
import {webgpuDevice} from '@luma.gl/core/webgl';
luma.registerAdapters([webgl2Adapter, webgpuDevice]);
const device = luma.createDevice({type: 'best-available', createCanvasContext: {canvas: ...}});

Registering Adapters​

Install device modules and register adapters to access backends

yarn add @luma.gl/core
yarn add @luma.gl/core
yarn add @luma.gl/core

To create a WebGPU device:

import {luma} from '@luma.gl/core/api';
import {webgpuAdapter} from '@luma.gl/core/webgpu';

luma.registerAdapters([webgpuAdapter]);
const device = await luma.createDevice({type: 'webgpu', createCanvasContext: ...});

Pre-register devices

import {luma} from '@luma.gl/core/api';
import {webgl2Adapter} from '@luma.gl/core/webgl';
import {webgpuAdapter} from '@luma.gl/core/webgpu';

luma.registerAdapters([webgl2Adapter, webgpuAdapter]);
const webgpuDevice = luma.createDevice({type: 'best-available', createCanvasContext: ...});

Types​

CreateDeviceProps​

Properties for creating a new device. See DeviceProps for device specific options.

type CreateDeviceProps = {
/** Selects the type of device. `best-available` uses webgpu if available, then webgl. */
type?: 'webgl' | 'webgpu' | 'unknown' | 'best-available';
/** List of device types. Will also search any pre-registered device backends */
adapters?: Adapter[];
/** Whether to wait for page to be loaded, which ensures that canvas contexts can refer to existing canvases by id (defaults to true) */
waitForPageLoad?: boolean;
} & DeviceProps

AttachDeviceProps​

Properties for attaching an existing WebGL context or WebGPU device to a new luma Device. See DeviceProps for device specific options.

export type AttachDeviceProps = {
/** Externally created WebGL context or WebGPU device */
handle: WebGL2RenderingContext | GPUDevice | null;
/** List of device types. Will also search any pre-registered device backends */
adapters?: Adapter[];
} & DeviceProps;

Methods​

luma.createDevice()​

luma.createDevice({type, adapters, ...deviceProps}: CreateDeviceProps);

To create a Device instance, the application calls luma.createDevice().

  • type: 'webgl' \| 'webgpu' \| 'best-available'
  • adapters: list of Adapter instances providing support for different GPU backends. Can be omitted if luma.registerAdapters() has been called.
  • ...deviceProps: See DeviceProps for device specific options.

Unless a device type is specified a Device will be created using the 'best-available' adapter. luma.gl prefers WebGPU. If the browser exposes WebGPU but navigator.gpu.requestAdapter() returns null, luma.gl uses a supported, registered WebGL2 adapter when available. An explicit type: 'webgpu' reports the adapter error. Errors after acquiring a WebGPU adapter, such as a rejected device request, propagate for either selection mode.

When debug is enabled in a browser and device creation fails, luma.gl displays the error in a browser alert. To enable debugging from a browser URL, including on a mobile device, append the debug query parameter (for example, ?debug). An explicit debug prop passed to createDevice() takes precedence over this default.

Note: A specific device type is available and supported if both of the following are true:

  1. The backend module has been registered
  2. The browser supports that GPU API

luma.attachDevice()​

luma.attachDevice(handle: WebGL2RenderingContext | GPUDevice | null, {adapters, ...deviceProps}: AttachDeviceProps);

A luma.gl Device can be attached to an externally created WebGL2RenderingContext or GPUDevice. This allows applications to use the luma.gl API to "interleave" rendering with other GPU libraries.

  • handle - The externally created WebGL2RenderingContext or GPUDevice that should be attached to a luma Device.
  • adapters - list of Device backend classes. Can be omitted if luma.registerAdapters() has been called.
  • ...deviceProps: See DeviceProps for device specific options.

When attaching a GPUDevice:

  • device.limits and device.features come from the GPUDevice, so limits and features the application requested are preserved. Creation-time props such as featureLevel: 'core-max' and optionalFeatures cannot change an existing device.
  • Pass createCanvasContext: {canvas} if the device will render to a canvas, for example when passing it to deck.gl's new Deck({device}). Unlike WebGL attachment, which wraps the context's own canvas and disables autoResize because the application already sizes it, a WebGPU canvas context is only created when requested and uses the same defaults as luma.createDevice(). Pass autoResize: false if the application sizes the canvas itself.
  • If the application already configured the canvas, luma.gl reconfigures it for rendering and device.destroy() restores the application's configuration. Otherwise device.destroy() unconfigures the canvas.
  • device.destroy() releases luma.gl's canvas context and error listener but does not destroy the GPUDevice, which the application still owns. Call gpuDevice.destroy() to release it.
  • Because the GPUDevice stays alive, buffers, textures, and other resources created through the luma.gl Device also stay allocated after device.destroy(). Destroy them before destroying the device. (A device created by luma.gl releases them when its GPUDevice is destroyed.)
  • Attaching the same GPUDevice again returns the existing luma.gl Device until that device is destroyed. Attaching a lost GPUDevice throws.
  • device.info.featureLevel is 'core' if the GPUDevice has the core-features-and-limits feature or featureLevel: 'core' is passed, and 'compatibility' otherwise.
  • luma.gl calls preventDefault() on uncapturederror events, which suppresses the browser's console warning for those errors, until device.destroy().

Note that while you cannot directly attach a luma.gl Device to a WebGL 1 WebGLRenderingContext, you may be able to work around it using luma.enforceWebGL2().

luma.registerAdapters()​

luma.registerAdapters(adapters?: Adapter[]): void;

Pre-registers one or more adapters so that they can be used to create Device instances against those GPU backends. The registered adapters types will be available to luma.createDevice() and luma.attachDevice() calls.

luma.registerAdapters() enables separation of the application code that registers GPU backends from the application code that creates adapters, so that device types do not have to be provided at Device create or attach time.

luma.enforceWebGL2()​

luma.enforceWebGL2(enforce: boolean = true, adapters: Adapter[]);

Overrides HTMLCanvasElement.prototype.getContext() to return WebGL2 contexts even when WebGL1 context are requested. Reversible with luma.enforceWebGL2(false);

Since luma.gl only supports WebGL2 contexts (WebGL2RenderingContext), it is not possible to callluma.attachDevice() on a WebGL1 context (WebGLRenderingContext).

This becomes a problem when using luma.gl with a WebGL library that always creates WebGL1 contexts (such as Mapbox GL JS v1). Calling luma.enforceWebGL2() before initializing the external library makes that library create a WebGL2 context, that luma.gl can then attach a Device to.

Note that the webgl2Adapter must either be pre-registered or supplied to the luma.enforceWebGL2() call.

caution

Since WebGL2 is a essentially a superset of WebGL1, a library written for WebGL 1 will often still work with a WebGL 2 context. However there may be issues if the external library relies on WebGL1 extensions that are not available in WebGL2. To make a WebGL 2 context support WebGL1-only extensions, those extensions would also need to be emulated on top of the WebGL 2 API, and this is not currently done.

Remarks​

  • At least one backend must be imported and registered with luma.registerAdapters() for luma.createDevice() or luma.attachDevice() calls to succeed (unless Device implementations are supplied to those calls).

Device ownership​

device.ownsHandle reports whether the wrapper owns its native handle. A newly created device owns its handle by default, except when using implicit WebGL device reuse. A newly attached device borrows the handle by default. Pass ownsHandle: true when attaching to transfer ownership, or ownsHandle: false when creating to retain the native handle after wrapper teardown. Reusing an existing wrapper uses its original ownership setting. When multiple consumers share a canvas, leave destruction to the canvas owner.

Call device.destroy() once all consumers have finished. An owned WebGPU device destroys its native GPUDevice. An owned WebGL device requests context loss if the extension is available. Destroying a borrowed wrapper leaves its native handle available for another wrapper. Existing WebGL callers relying on implicit device reuse retain their previous teardown behavior until they specify ownership.