WebGPU backend
The WebGPU backend maps luma.gl's shared GPU model to the browser's WebGPU API. It is packaged with core and manages device access, converts shared resources into native WebGPU resources, and records render or compute work for queue submission. It also defines WebGPU-specific rules for canvas output, shader bindings, memory access, render bundles, and device loss.
This page extends Rendering runtime platforms and complements the core API, engine render loop, and WebGL backend pages.
Device integration
The WebGPU adapter is the entry point for selecting a native adapter and exposing its device through luma.gl's common Device interface. Applications can let luma.gl create the native device or attach a device that the application already owns (modules/…/webgpu-adapter.ts• L187-369).
Device creation maps application preferences to WebGPU adapter options and a device request. The feature level controls this request: core uses the portable core profile, compatibility uses the compatibility profile, best-available uses core when the adapter supports the required upgrade, and max requests the adapter's available features and limits. Optional features are requested only when the adapter supports them. The created device then reports the effective feature level and capabilities (modules/…/webgpu-adapter.ts• L82-166, modules/…/webgpu-device.ts• L538-653).
The adapter checks for a device that is already lost before it exposes the wrapper. A device created by luma.gl can be retried once when the browser returns an already-lost device. An attached device is shared by default, but callers can transfer ownership through the device properties. Destroying a shared wrapper releases the wrapper and its listeners without destroying the application-owned WebGPU device (modules/…/webgpu-adapter.ts• L257-358, modules/…/webgpu-device.ts• L130-209).
The device keeps one luma.gl wrapper for each live native GPUDevice. This makes repeated attachment return the same wrapper and removes the wrapper from the cache after device loss (modules/…/webgpu-adapter.ts• L318-347, modules/…/webgpu-device.ts• L77-85, modules/…/webgpu-device.ts• L152-168).
References: modules/…/webgpu-adapter.ts• L82-185 modules/…/webgpu-adapter.ts• L187-369 modules/…/webgpu-device.ts• L77-216 modules/…/webgpu-device.ts• L538-653
Resource translation
The backend gives each shared luma.gl GPU concept a WebGPU wrapper. Each wrapper keeps the common lifetime and validation model while exposing the native handle required by command encoders, passes, and queues (modules/…/webgpu-device.ts• L224-287).
| Luma.gl concept | WebGPU role |
|---|---|
| Buffer | Storage for vertex, index, uniform, staging, and readback data |
| Texture and texture view | Image storage and access view for sampling or attachment use |
| Shader | WGSL shader module |
| Render pipeline | Vertex and fragment stages with fixed render state |
| Compute pipeline | Compute stage and workgroup entry point |
| Framebuffer | Attachment view set for a render pass |
| Vertex array | Logical vertex-input mapping applied to physical WebGPU slots at draw time |
| Query set and fence | Timing, occlusion, and completion tracking |
WebGPU treats a vertex array as a draw-time helper rather than a native object. The helper resolves luma.gl's logical buffer and attribute layout into the physical WebGPU slots required by the pipeline. It validates that each resolved slot has a buffer and can bind one logical buffer to more than one physical slot when the resolved layout needs that expansion (modules/…/webgpu-vertex-array.ts• L20-40, modules/…/webgpu-vertex-array.ts• L70-85, modules/…/get-vertex-buffer-layout.ts• L17-25, modules/…/get-vertex-buffer-layout.ts• L49-117).
Buffers use WebGPU's alignment rules. When a buffer cannot be mapped for the requested read or write, the backend uses a temporary mappable buffer and copies the data through the GPU. This lets the common buffer API support both direct access and WebGPU's restricted mapping model (modules/…/webgpu-buffer.ts• L20-79, modules/…/webgpu-buffer.ts• L117-227).
Texture transfers use the WebGPU path that matches the source and destination. Buffer copies use aligned rows, while queue texture writes can use tightly packed source rows. External image copies use the native queue operation on normal adapters. On software WebGPU adapters, supported RGBA8 images can use a CPU upload when the native external-image path is unstable (modules/…/webgpu-texture.ts• L136-209, modules/…/webgpu-texture.ts• L327-462).
References: modules/…/webgpu-device.ts• L224-287 modules/…/webgpu-vertex-array.ts• L20-138 modules/…/get-vertex-buffer-layout.ts• L17-129 modules/…/webgpu-buffer.ts• L20-79 modules/…/webgpu-buffer.ts• L117-227 modules/…/webgpu-texture.ts• L136-209 modules/…/webgpu-texture.ts• L327-462
Shader and binding model
WebGPU shader work uses WGSL. The backend rejects GLSL input and reports asynchronous shader messages through the device error path. When the caller does not provide a shader layout, the backend can infer supported vertex inputs and resource bindings from WGSL (modules/…/webgpu-shader.ts• L12-57, modules/…/get-shader-layout-wgsl.ts• L11-21).
Shader compilation is asynchronous. The shader exposes compilation status and compiler messages through the common shader interface. If the shader is destroyed while compilation information is pending, the backend treats the missing native handle as having no messages instead of reading a released resource (modules/…/webgpu-shader.ts• L41-88).
The common luma.gl binding description becomes WebGPU bind-group layouts and bind groups. Resource type, shader visibility, group number, and binding location must agree because WebGPU validates them when the pipeline and pass use the resources (modules/…/webgpu-pipeline-layout.ts• L20-147).
References: modules/…/webgpu-shader.ts• L12-88 modules/…/get-shader-layout-wgsl.ts• L11-21 modules/…/webgpu-pipeline-layout.ts• L20-147
Render and compute execution
The command encoder is the boundary between shared luma.gl work and WebGPU command recording. It opens render or compute passes, records resource copies and query work, and finishes into a native command buffer. The device submits that buffer to the WebGPU queue (modules/…/webgpu-command-encoder.ts• L21-251, modules/…/webgpu-device.ts• L382-489).
Pipeline parameters translate luma.gl's flat state model into WebGPU pipeline state. Raster, depth, stencil, multisample, color-mask, and blend settings are applied to the matching WebGPU descriptor. An explicit request to disable blending removes blend state even when blend factors also appear in the parameter set (modules/…/webgpu-parameters.ts• L32-245, modules/…/webgpu-parameters.ts• L286-317, modules/…/webgpu-blending.node.spec.ts• L7-33).
The execution path converts shared commands into native work and then into updates to the canvas or other GPU resources.
flowchart LR
A[Luma.gl resources and commands] --> B[WebGPU command encoder]
B --> C[Render or compute pass]
C --> D[Native command buffer]
D --> E[WebGPU queue]
E --> F[Canvas or GPU resources]
Render passes select a pipeline, apply bind groups and vertex inputs, and issue direct or indirect draws. A pass can begin from the active command encoder or use a supplied native render-pass handle. Before each draw, the selected vertex array resolves logical buffers into the physical slots expected by the pipeline. It can repeat logical buffers across multiple WebGPU slots when the resolved layout was expanded. WebGPU does not support clearing these slots during the pass, so the backend leaves them bound until the pass ends. Compute passes use the same binding path and dispatch direct or indirect workgroups (modules/…/webgpu-render-pass.ts• L34-250, modules/…/webgpu-vertex-array.ts• L70-140, modules/…/get-vertex-buffer-layout.ts• L49-117, modules/…/webgpu-compute-pass.ts• L19-121).
Render pass descriptors apply clear and store rules per attachment. Depth and stencil are handled as separate aspects, so a read-only aspect omits its load, store, and clear operations. The pass uses the attachment view format when deciding which aspect is active (modules/…/webgpu-render-pass.ts• L341-401).
Pass parameters are kept as complete snapshots of the active viewport, scissor rectangle, blend constant, and stencil reference. A null scissor value restores the pass render area, which comes from the current attachment size. This lets higher-level draw code scope parameters and later restore the full pass state without sharing caller-owned arrays (modules/…/webgpu-render-pass.ts• L64-80, modules/…/webgpu-render-pass.ts• L252-297).
Direct and indirect draws preserve zero counts and the exact indirect byte offset. A render bundle records reusable draw commands with compatible attachment formats. A render pass can replay those bundles, but a bundle encoder cannot replay another bundle. Native bundle replay clears the pass draw state, so the backend clears its active pipeline and bindings and callers must establish them again before more draws (modules/…/webgpu-render-pass.ts• L211-249, modules/…/webgpu-render-pass.ts• L316-333, modules/…/webgpu-render-bundle.ts• L53-212).
WebGPU submission is explicit. Temporary upload and readback resources must remain valid while queued work uses them, and the backend replaces finished command encoders after submission. A WebGPU fence represents completion of submitted queue work. Its signaled promise resolves from queue.onSubmittedWorkDone(), and its state becomes signaled only after that promise resolves (modules/…/webgpu-device.ts• L289-314, modules/…/webgpu-device.ts• L382-489, modules/…/webgpu-fence.ts• L8-39).
References: modules/…/webgpu-command-encoder.ts• L21-251 modules/…/webgpu-render-pass.ts• L34-401 modules/…/webgpu-vertex-array.ts• L70-140 modules/…/get-vertex-buffer-layout.ts• L49-117 modules/…/webgpu-render-bundle.ts• L53-267 modules/…/webgpu-compute-pass.ts• L19-121 modules/…/webgpu-device.ts• L382-489 modules/…/webgpu-fence.ts• L8-39 modules/…/webgpu-parameters.ts• L32-317 modules/…/webgpu-blending.node.spec.ts• L7-33 modules/…/webgpu-render-pass.spec.ts• L10-161
Canvas presentation
The canvas context connects WebGPU's current canvas texture to luma.gl's framebuffer and render-pass model. Each frame acquires the current texture and pairs its view with the attachments needed by the pass (modules/…/webgpu-canvas-context.ts• L134-240).
Canvas configuration records the accepted color format, color space, alpha mode, and tone mapping. If the browser does not accept an extended high-dynamic-range configuration, the backend uses the accepted standard configuration instead. Presentation then submits work to the configured canvas (modules/…/webgpu-canvas-context.ts• L27-132, modules/…/webgpu-presentation-context.ts• L15-69).
References: modules/…/webgpu-canvas-context.ts• L20-240 modules/…/webgpu-presentation-context.ts• L15-69
WebGPU capability boundaries
The backend exposes the shared GPU model where WebGPU has a matching capability. It supports render and compute pipelines, bind groups, render bundles, query sets, external textures, texture copies, and WGSL. It rejects features that have no WebGPU equivalent instead of hiding the difference behind a simulation (modules/…/webgpu-device.ts• L318-353).
| Capability boundary | Backend behavior |
|---|---|
| Transform feedback | Not supported |
| GLSL shaders | Rejected; use WGSL |
| WebGL mipmap generation entry point | Not supported |
| Direct CPU writes inside a command encoder | Ordered through staging-buffer copies |
| Non-mappable buffer read or write | Uses a temporary mappable buffer |
| External image copy on software adapters | Uses a CPU upload only for supported RGBA8 images |
Shared code must check capabilities or choose a backend-specific path before execution. The WebGL and WebGPU backends do not provide the same feature set (modules/…/webgpu-device.ts• L318-353, modules/…/webgpu-shader.ts• L20-23, modules/…/webgpu-texture.ts• L136-209, modules/…/webgpu-texture.ts• L265-267).
References: modules/…/webgpu-device.ts• L318-353 modules/…/webgpu-buffer.ts• L117-227 modules/…/webgpu-shader.ts• L20-23 modules/…/webgpu-texture.ts• L136-209 modules/…/webgpu-texture.ts• L265-267
Change checklist
When these files or sections are changed, remember to consider these:
- Keep feature selection, requested features, effective feature level, and reported device capabilities aligned. Otherwise device information can disagree with the native device.
- Keep attached-device ownership, wrapper caching, device-loss cleanup, and destroy behavior aligned.
- Keep already-lost device checks aligned with the one-time retry path for luma-created devices.
- Keep canvas configuration, accepted canvas format, framebuffer attachments, render-pass formats, and pipeline color targets compatible.
- Keep WGSL declarations, layout inference, pipeline layouts, bind groups, and render-bundle attachment formats consistent.
- Keep logical vertex buffer mappings, resolved physical WebGPU slots, pipeline vertex layouts, and draw-time vertex-array validation and binding consistent.
- Keep temporary buffer lifetime tied to queue use when changing mapping, copy, or submission paths.
- Keep depth and stencil aspect rules aligned between render-pass descriptors and read-only attachment state.
- Keep direct and indirect draw argument forwarding unchanged when altering pass or bundle encoding.
- Treat render-bundle replay as a draw-state boundary. Restore the pipeline and bindings before issuing later draws.
- Keep blend enablement and blend-factor translation consistent so explicit disabling cannot be reintroduced by parameter order.
- Keep external-image fallback limits aligned with the texture format and copy options supported by the CPU upload path.
- Keep fence signaling tied to submitted queue work and preserve dropped-device-error handling.
- Keep shader destruction safe for asynchronous compilation-info requests.
- Keep render-pass parameter snapshots complete and detached from caller-owned arrays when changing state scoping or restoration.
- Keep a null scissor value mapped to the attachment-derived render area when changing pass-size or parameter handling.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| WebGPU is unavailable or no adapter can be selected | Device creation fails before a luma.gl device exists | Check support and handle adapter errors |
| Requested features or limits exceed adapter support | Native device request fails | Request optional features only when supported and keep required limits within adapter limits |
| The browser returns an already-lost device | Device creation or attachment fails before normal use | Reject lost attached devices and retry a newly created device only once |
| A shared attached device is destroyed through its wrapper | The application-owned native device is lost unexpectedly | Keep attachment non-owning unless the caller explicitly transfers ownership |
| WGSL bindings do not match the pipeline layout | Pipeline or bind-group validation fails | Keep shader declarations and luma.gl bindings in agreement |
| A shader is destroyed while compilation information is pending | Compilation reporting can access a released native handle | Treat a missing shader handle as an empty compiler-message result |
| A logical vertex buffer is missing or its layout resolves to the wrong physical slot | The draw fails validation or reads incorrect vertex data | Keep buffer mappings, shader locations, pipeline layouts, and draw-time vertex-array binding in agreement |
| A draw runs without its pipeline or required bindings | The pass or bundle reports invalid state | Set the pipeline first and set bindings again after a pipeline change |
| A render bundle is replayed with incompatible attachments | WebGPU validation fails | Create bundles for the same color, depth, stencil, and sample configuration as the target pass |
| A draw follows render-bundle replay without restoring pass state | The draw can run without the pipeline or bind groups that the pass needs | Set the pipeline and bindings again after bundle replay |
| A mapped range violates WebGPU alignment or size rules | Mapping fails or reads the wrong byte range | Align the native range and slice the requested range from the mapped data |
| The native device is lost during work | Future commands and attachment access become invalid | Observe device loss and recreate the device |
| The canvas rejects an HDR configuration | Presentation uses a different accepted format | Use the resolved canvas configuration |
| A software adapter cannot use external-image copying for an unsupported format or source | The copy fails because no CPU fallback applies | Use a supported RGBA8 image path or handle the native copy error |
| A scoped draw restores only part of the render-pass state | Later draws inherit an old viewport, scissor, blend constant, or stencil reference | Restore the complete snapshot returned by getParameters() |
| A null scissor is resolved against a stale or guessed pass size | Work is clipped to the wrong area | Derive the default scissor from the active attachment dimensions |
Key files
| File | Why to read it |
|---|---|
| modules/…/webgpu-adapter.ts | Device selection, feature levels, limits, loss checks, and native-device attachment |
| modules/…/webgpu-device.ts | Device wrapper, resource factory, feature reporting, loss handling, and queue submission |
| modules/…/webgpu-canvas-context.ts | Canvas configuration and per-frame framebuffer attachment |
| modules/…/webgpu-vertex-array.ts | Logical vertex-array validation and draw-time WebGPU buffer binding |
| modules/…/get-vertex-buffer-layout.ts | Translation from logical attribute mappings to physical WebGPU vertex slots |
| modules/…/webgpu-buffer.ts | WebGPU alignment and mapped or staging buffer access |
| modules/…/webgpu-texture.ts | Texture lifetime, transfer paths, external-image fallback, and canvas texture rebinding |
| modules/…/webgpu-parameters.ts | Translation from shared pipeline parameters to WebGPU state |
| modules/…/webgpu-command-encoder.ts | Command recording, pass creation, copies, and query work |
| modules/…/webgpu-render-pass.ts | Render-pass attachments, parameter snapshots, state, binding, draw execution, and bundle replay state |
| modules/…/webgpu-render-bundle.ts | Reusable draw-command recording and bundle replay contract |
| modules/…/webgpu-compute-pass.ts | Compute binding and dispatch execution |
| modules/…/webgpu-fence.ts | Completion tracking for submitted WebGPU queue work |
| modules/…/webgpu-pipeline-layout.ts | Translation from shared bindings to WebGPU bind-group layouts |
| modules/…/get-shader-layout-wgsl.ts | WGSL interface scanning for layout inference |
| modules/…/webgpu-shader.ts | WGSL validation, asynchronous compilation status, and compiler messages |
| modules/…/webgpu-blending.node.spec.ts | Behavioral check that explicit blend disabling wins over blend factors |
| modules/…/webgpu-render-pass.spec.ts | Behavioral checks for attachment rules and draw forwarding |