Device
The Device
class manages the application's connection with the GPU,
providing methods to:
- create GPU resources
- query the capabilities of the GPU
- detect GPU error conditions.
A Device
instance is created through the luma.createDevice(...)
method.
Note that the actual Device
returned by luma.createDevice()
will be either
a WebGLDevice
wrapping a WebGL context or a WebGPUDevice
wrapping a WebGPU device
based on what the run-time environment supports.
The Device
API is intentionally designed to be similar to the
WebGPU GPUDevice
class API
with changes to enable a WebGL2 implementation.
Usage
Create a new Device
, auto creating a canvas and a new WebGL 2 context. See luma.createDevice()
.
import {Device} from '@luma.gl/core';
const device = new luma.createDevice({type: 'webgl2', ...});
Attaching a Device
to an externally created WebGL2RenderingContext
.
import {Device} from '@luma.gl/core';
import {Model} from '@luma.gl/engine';
const gl = canvas.getContext('webgl2', ...);
const device = Device.attach(gl);
const model = new Model(device, options);
Handle GPU disconnections:
if (!device.isLost) {
console.error('Device lost');
}
const {message} = await device.lost;
console.error(message);
Types
DeviceProps
This object can also include all CanvasContextProps
properties to configure how a new canvas is created. If a canvas is provided, these are ignored.
Specifies props to use when luma creates the device.
Property | Default | Description |
---|---|---|
id?: string | null | Optional string id, mainly intended for debugging. |
createCanvasContext?: CanvasContextProps | true | CanvasContexProps | Create a default CanvasContext for the new Device . true creates a context with default props. |
powerPreference?: string | 'high-performance' | 'default' | 'high-performance' | 'low-power' (WebGL). |
webgl?: WebGLContextAttributes | WebGLContextAttributes | Attributes passed on to WebGL (canvas.getContext('webgl2', props.webgl) |
onError?: (error: Error) => unknown | log.error() | Called if an unhandled error is generated by luma.gl. |
onResize?: (ctx: CanvasContext) | log.info(1) | Called if the size of the "device pixel content box" changes. |
onVisibilityChange?: (ctx: CanvasContext) | log.info(1) | Called if the visibility of the canvas changes (window is closed or occluded). |
onDevicePixelRatioChange?: (ctx: CanvasContext) | log.info(1) | Called if the DPR changes (perhaps by moving the window to another screen |
debug? : boolean | false | Extra checks (wait for shader compilation, framebuffer completion, WebGL API errors will throw exceptions). |
debugShaders? : 'errors' 'warnings' 'always' 'never' | 'error' | Display shader source code with inline errors in the canvas. |
debugFramebuffers?: boolean | false | Show small copy of the contents of updated Framebuffers in the canvas. |
debugWebGL?: boolean | false | traces WebGL API calls to the console (via Khronos WebGLDeveloperTools). |
debugSpectorJS?: boolean | false | Initialize the SpectorJS WebGL debugger. |
debugSpectorJSUrl?: string | CDN url | SpectorJS URL. Override if different SpectorJS version is desired (or if CDN is down). |
Learn more GPU debugging in our Debugging guide.
WebGLContextAttributes
For detailed control over WebGL context can specify what WebGLContextAttributes
to use if luma creates the WebGL context.
WebGLContextAttributes | Default | Description |
---|---|---|
webgl.preserveDrawingBuffers?: boolean | true | Default render target buffers will preserve their values until overwritten. Useful for screen capture. |
webgl.alpha?: boolean | true | Default render target has an alpha buffer. |
webgl.antialias?: boolean | true | Boolean that indicates whether or not to perform anti-aliasing. |
webgl.depth?: boolean | true | Default render target has a depth buffer of at least 16 bits. |
webgl.premultipliedAlpha?: boolean | true | The page compositor will assume the drawing buffer contains colors with pre-multiplied alpha. |
webgl.stencil?: boolean | false | Default render target has a stencil buffer of at least 8 bits. |
webgl.desynchronized?: boolean | false | Hint to reduce latency by desynchronizing the canvas paint cycle from the event loop (WebGL). |
webgl.failIfMajorPerformanceCaveat? | false | Do not create a Device if the system performance is low (WebGL). |
Note that luma.gl v9.1 and onwards set webgl.preserveDrawingBuffers
to true
by default. This can be disabled for some memory savings and a minor performance boost on resource limited devices, such as mobile phones, at the cost of not being able to take screenshots or render to screen without clearing.
Fields
id
readonly id: string
A string identifier, for debug purposes.
statsManager
statsManager: StatsManager
Provides access to bags of stats containing information about resource usage and performance of the device.
props
props: Required<DeviceProps>
A readonly copy of the props that were used to create this device.