Core GPU programming
A Core application creates resources on a device, records commands that use them, and submits those commands to the GPU. Keep resources such as buffers and pipelines across frames. Create a new pass for each unit of work. If you want luma.gl to manage geometry, shader inputs, and draw state for you, start with the Engine guide.
See WebGPU vs WebGL for the capabilities and limits of each backend.
Create a device​
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}
});
console.log(device.type); // 'webgpu' or 'webgl'
Here canvas is the application's canvas element. Use best-available for a renderer that supports both backends. Request webgpu explicitly if your algorithm requires compute passes or storage buffers. Inspect device.features and device.limits before choosing optional paths. See Packages and backends for adapter selection.
Record and submit a frame​
The smallest render pass clears the canvas. This example also creates a vertex buffer for subsequent draws. The buffer can survive many passes.
import {Buffer} from '@luma.gl/core/api';
const positions = device.createBuffer({
data: new Float32Array([0, 0, 1, 0, 0, 1]),
usage: Buffer.VERTEX | Buffer.COPY_DST
});
const pass = device.beginRenderPass({clearColor: [0, 0, 0, 1]});
pass.end();
device.submit();
To draw the triangle, bind a compatible pipeline and vertex array before calling draw and ending the pass. Commands, passes, and presentation shows the draw sequence, offscreen targets, and presentation. The triangle walkthrough uses an Engine model to manage that state.
Release owned resources​
When the application stops producing frames, call positions.destroy(). Destroy the device when its owner shuts down. A component that borrows the buffer or device leaves destruction to its owner. After device loss, stop submitting work and recreate device-owned resources on a new device.
Continue by task​
| Task | Guide |
|---|---|
| Decide who destroys a resource | Resource ownership |
| Upload, copy, or read back data | Buffers and memory |
| Describe what the shader reads | Layouts and shader bindings |
| Upload images or use a video frame | Textures and sampling |
| Encode a draw or dispatch and present a frame | Commands, passes, and presentation |
| Configure visibility and masks | Depth and Stencil |
| Smooth edges and reduce shimmer | Antialiasing |
| Inspect a validation error or measure frame time | Debugging and profiling |