Skip to main content

@luma.gl/core/api

@luma.gl/core/api is the portable interface to the GPU. A Device creates buffers, textures, and pipelines and submits work that uses them. Create the device once and pass it to the parts of your application that share GPU resources. A buffer created on one device can only be used on that device.

Choose a backend​

An adapter connects luma.gl to a browser GPU implementation. Import the adapters your application supports and pass them to luma.createDevice(). With 'best-available', luma.gl prefers WebGPU and can use WebGL 2 when WebGPU is unavailable. An application that relies on compute passes can request 'webgpu' explicitly. Device creation · Adapter reference

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

const device = await luma.createDevice({
type: 'best-available',
adapters: [webgpuAdapter, webgl2Adapter],
createCanvasContext: {canvas}
});

Here canvas is the canvas element the application renders into. The resulting device exposes the same resource creation and rendering methods on either backend. Device reference

Find out what the device supports​

The backend name alone tells you little about optional GPU capabilities. Three properties answer different questions about the active device.

PropertyWhen to use it
device.infoInclude the GPU and driver information available to the browser in a diagnostic report.
device.featuresCheck an optional operation before enabling it. For example, device.features.has('timestamp-query') tells a profiler whether it can measure GPU time.
device.limitsSize work to fit the hardware. For example, cap each dimension of a 2D texture at device.limits.maxTextureDimension2D.

Record and submit work​

Resources can survive across frames. A render pass records the work for one set of attachments. For example, this pass clears the canvas attached above. Ending the pass finishes recording it, and device.submit() sends the recorded commands to the GPU.

const pass = device.beginRenderPass({clearColor: [0.05, 0.1, 0.2, 1]});
pass.end();
device.submit();

To draw geometry, bind a pipeline and vertex data inside the pass before issuing the draw. The same command workflow also supports drawing to textures for use in later passes. Commands, passes, and presentation