Rendering runtime platforms
luma.gl provides one rendering runtime for WebGL and WebGPU. Core defines the shared device, resource, command, canvas, capability, and loss model, and contains the platform adapters that connect this model to each native API. The engine prepares rendering work, while the selected platform turns that work into native GPU operations.
Runtime architecture
The runtime has three cooperating layers. The core layer defines the portable contract. The engine layer turns application data into frame work. A platform device owns the native GPU session and executes that work.
This diagram shows how application work moves through the shared contract to a selected native platform and then to the canvas.
flowchart LR
App[Application] --> Engine[Render engine and loop]
Engine --> Core[Core device and resource contract]
Core --> Select[Platform selection]
Select --> WebGL[WebGL device]
Select --> WebGPU[WebGPU device]
WebGL --> GL[WebGL API]
WebGPU --> GPU[WebGPU API]
GL --> Canvas[Canvas presentation]
GPU --> Canvas
Device selection
Device selection chooses a platform that the environment can run and returns a common Device. The selection layer can use an explicit platform, choose the best available platform, or attach to a resource that the application already created.
Adapters answer whether a platform is available and create the device. Device creation applies shared defaults, including the WebGPU feature profile and debug behavior. The device then owns or references the active GPU session and reports its type, limits, features, presentation formats, and loss state to the rest of the runtime.
WebGPU creation can request a portable core profile, compatibility mode, the best available profile, or the adapter's supported feature and limit set. An attached WebGPU device keeps one luma.gl wrapper for each native device, while WebGL detects an existing wrapper on its context and can reuse it only when reuse is enabled.
References: modules/…/luma.ts• L22-45 modules/…/luma.ts• L92-125 modules/…/luma.ts• L147-167 modules/…/device-defaults.ts• L8-59 modules/…/device.ts• L383-410 modules/…/device.ts• L510-518 modules/…/webgl-device.ts• L152-229 modules/…/webgl-device.ts• L284-307 modules/…/webgpu-adapter.ts• L82-185 modules/…/webgpu-adapter.ts• L187-359 modules/…/webgpu-device.ts• L77-83 modules/…/webgpu-device.ts• L152-168
Portable GPU contract
The core Device contract gives the engine one way to create resources, encode work, run render or compute passes, present frames, and observe capability or loss state. Resource types also carry the shared rules for shader bindings, sampler and texture use, render state, uniforms, and numeric data. This keeps platform choice out of normal application rendering code while giving both backends the same input shapes.
The contract defines the main resource roles. Buffers carry data, textures hold image data, samplers describe texture lookup, pipelines describe shader and draw state, and passes group work against a target. A render pass also defines portable draw, indirect-draw, query, and debug operations. A render bundle records reusable draw commands for compatible WebGPU passes. Shader layouts and binding scans describe how shader inputs connect to those resources. Vertex arrays keep the relationship between shader attributes and vertex buffers portable while each platform binds that relationship in its own way. Some roles are platform-specific: compute pipelines and render bundles are available to WebGPU, while transform feedback is available to WebGL.
Every resource belongs to a device and has a clear lifetime. Core resources can wrap an owned native handle or reference a borrowed external handle. Parent resources can own attached child resources, so destroying the parent releases the children that luma.gl created. Resource creation and destruction also feed the runtime's resource and memory statistics.
References: modules/…/device.ts• L522-570 modules/…/device.ts• L749-843 modules/…/device.ts• L804-823 modules/…/render-pass.ts• L54-161 modules/…/render-bundle.ts• L45-117 modules/…/vertex-array.ts• L21-83 modules/…/sampler.ts• L9-46 modules/…/texture.ts• L179-221 modules/…/parameters.ts• L12-196 modules/…/shader-layout.ts• L11-26 modules/…/wgsl-binding-scan.ts• L5-16 modules/…/wgsl-interface-scan.ts• L5 modules/…/types.ts• L5-44 modules/…/is-array.ts• L7-28 modules/…/resource.ts• L80-168 modules/…/resource.ts• L194-239 modules/…/resource.ts• L265-343
Render engine
The render engine converts scene or application inputs into work that uses the core contract. A model joins shaders, geometry, bindings, and pipeline state. The render loop supplies frame timing and controls when encoded work is submitted.
Model draw work
The engine model is the bridge between application data and the device. It prepares resource updates, obtains a pipeline, starts a render pass, and issues draw work without requiring application code to call WebGL or WebGPU directly.
Uploads must remain ordered before the draw that consumes them. The shared device exposes a command-encoder upload path so each platform can preserve that order within its native execution model.
References: modules/…/model.ts• L169-207 modules/…/model.ts• L352-505 modules/…/device.ts• L840-869
Frame orchestration
The animation loop coordinates repeated rendering. It waits for the device, initializes application state, schedules frames, updates timing and size data, calls the render callback, and submits work when the callback encoded a frame.
See Render-loop orchestration for the frame lifecycle and redraw rules.
References: modules/…/animation-loop.ts• L31-51 modules/…/animation-loop.ts• L199-239 modules/…/animation-loop.ts• L354-404
Command submission
The command encoder records render, compute, and copy work until the frame reaches the device submit point. It can assign timestamp slots to passes and return a snapshot that belongs to that encoding, so later encoders can reuse the query set without changing earlier measurements.
WebGPU sends a finished command buffer to its queue. WebGL finishes the active encoder and flushes its ordered command buffer against the context. Immediate WebGL writes first flush earlier queued copies, so the write keeps its place in the command stream.
This boundary lets the engine submit a frame in one way while each platform handles its own timing and data layout. WebGPU records uploads as ordered staging-buffer copies before submission, while WebGL preserves the same order while executing native work against its context. Texture reads and writes use the shared region and row-layout rules before the backend performs the native transfer.
References: modules/…/command-encoder.ts• L15-28 modules/…/command-encoder.ts• L155-180 modules/…/command-encoder.ts• L216-294 modules/…/texture.ts• L326-423 modules/…/device.ts• L858-869 modules/…/webgl-device.ts• L400-454 modules/…/webgpu-device.ts• L285-314 modules/…/webgpu-device.ts• L382-489
Canvas presentation
Canvas handling connects page layout to GPU render targets. The shared canvas surface supports HTML canvases, offscreen canvases, and non-browser surfaces while tracking CSS size, physical pixel size, drawing-buffer size, visibility, and device-pixel-ratio changes.
The surface can use exact physical-pixel measurements or derive pixels from CSS size and device pixel ratio. Resize and pixel-ratio changes update the drawing buffer through a deferred resize, then the platform configures the native target when the current framebuffer is requested. Observers start only after the device is ready and stop when the surface is destroyed.
WebGL uses its context-backed target. Its canvas context keeps a wrapper for the default framebuffer and aligns that wrapper with the current drawing-buffer size. WebGPU acquires the current canvas texture through its configured context. Presentation settings also carry alpha mode, color space, color format, and tone mapping into the platform surface.
References: modules/…/canvas-surface.ts• L16-53 modules/…/canvas-surface.ts• L65-146 modules/…/canvas-surface.ts• L222-228 modules/…/canvas-surface.ts• L317-355 modules/…/canvas-surface.ts• L372-505 modules/…/webgl-canvas-context.ts• L11-57
Backend cooperation
WebGL and WebGPU implement the same core device contract, but they retain different native execution models. The engine uses common resources and passes. Each platform device translates those requests into native resources, state, commands, and presentation work.
WebGL execution
The WebGL device wraps a WebGL 2 context and exposes WebGL-backed core resources. It requires a canvas context, rejects a context that is already attached unless device reuse is explicitly enabled, and reports the selected context's features, limits, and loss state through the shared device surface. The WebGL adapter and resource classes are exported from core so applications can use the shared device surface without a separate backend package.
Context creation first asks for a hardware-capable WebGL 2 context. If the caller allows a software renderer, it retries without the hardware requirement. A WebGL 1 result is reported as unsupported rather than used as a WebGL device. The device tracks native state so repeated state changes can avoid redundant calls, and it refreshes that state when an external context is attached.
Render work runs against the context. A render pass maps the shared pipeline, bindings, vertex array, viewport, and clear rules to WebGL state before each draw. It supports direct draws and transform feedback, but WebGL has no native indirect drawing or render-bundle replay in this runtime. Submit flushes the ordered command buffer. An immediate buffer write flushes earlier queued copies first. A WebGL fence observes completion with the context's sync object.
The adapter maps shared texture formats to WebGL formats and extensions, then probes native renderability when needed. It rejects standalone stencil8 textures because WebGL exposes that format for renderbuffers rather than texture storage. Shader resources compile GLSL and expose sync or async compiler status and parsed diagnostics. WebGL supports the common render path and WebGL-specific resources such as transform feedback. It does not support compute pipelines, render bundles, indirect drawing, or external textures. After context loss, the device reports loss and rejects allocation through its main buffer, texture, sampler, and shader creation paths.
See WebGL backend for the WebGL resource, state, and context model.
References: modules/…/index.ts• L40-67 modules/…/webgl-device.ts• L81-107 modules/…/webgl-device.ts• L152-282 modules/…/webgl-device.ts• L284-353 modules/…/webgl-device.ts• L400-454 modules/…/create-browser-context.ts• L21-90 modules/…/webgl-state-tracker.ts• L24-136 modules/…/webgl-state-tracker.ts• L304-331 modules/…/webgl-texture-table.ts• L316-365 modules/…/webgl-texture-table.ts• L367-418 modules/…/webgl-fence.ts• L9-57 modules/…/webgl-shader.ts• L13-115 modules/…/webgl-render-pass.ts• L45-132 modules/…/webgl-render-pass.ts• L138-303 modules/…/webgl-vertex-array.ts• L17-27 modules/…/webgl-vertex-array.ts• L97-182
WebGPU execution
The WebGPU device wraps a native GPUDevice and exposes WebGPU-backed core resources. It records render and compute commands, submits command buffers to the GPU queue, and tracks the features and limits available from the selected adapter.
The adapter chooses the requested WebGPU feature profile and forwards supported optional features and required limits. It rejects an unavailable or already-lost device. When an application attaches an existing GPUDevice, the runtime reuses its wrapper and treats ownership as explicit, so luma.gl does not destroy an external device unless asked to do so.
A WebGPU render pass owns the native pass for a framebuffer. It builds color, depth, stencil, and resolve settings from the shared pass description, applies the active pipeline and bindings, lets the vertex array provide draw-time buffer bindings, and can replay compatible render bundles. Dynamic viewport, scissor, blend, and stencil state is kept as a pass snapshot and applied to the native encoder. A WebGPU fence represents completion of submitted queue work.
WebGPU shader resources accept WGSL and report validation and compiler messages through the device error path. The vertex-array abstraction is a draw-time helper: it resolves logical luma.gl buffer slots to native WebGPU slots and binds the matching buffers for each draw. WebGPU also supports asynchronous pipeline creation, compute work, render bundles, bind groups, indirect drawing, and native presentation textures. CPU uploads use temporary staging resources when needed so upload order remains part of the submitted command stream. Device loss and uncaptured errors are reported through the common device surface.
See WebGPU backend for the WebGPU device, feature, resource, and presentation model.
References: modules/…/webgpu-adapter.ts• L62-185 modules/…/webgpu-adapter.ts• L187-359 modules/…/webgpu-device.ts• L77-170 modules/…/webgpu-device.ts• L189-213 modules/…/webgpu-device.ts• L224-314 modules/…/webgpu-device.ts• L382-475 modules/…/webgpu-device.ts• L593-653 modules/…/webgpu-fence.ts• L9-37 modules/…/webgpu-shader.ts• L12-57 modules/…/webgpu-vertex-array.ts• L19-40 modules/…/webgpu-vertex-array.ts• L66-134 modules/…/webgpu-render-pass.ts• L34-170 modules/…/webgpu-render-pass.ts• L174-249 modules/…/webgpu-render-pass.ts• L252-333 modules/…/webgpu-render-pass.ts• L341-401 modules/…/get-vertex-buffer-layout.ts• L49-117
API boundaries
Core APIs are the shared boundary for devices, resources, passes, command encoding, and presentation. Engine APIs add models and frame control. Backend APIs provide the platform adapter and native behavior behind the shared contract. The WebGL and WebGPU implementations now live under core and remain selected through the same adapter boundary.
See Core APIs for the portable resource and device surface. See Render-loop orchestration for frame control. Use WebGL backend or WebGPU backend when behavior depends on one platform.
Change checklist
When these files or sections are changed, remember to consider these:
- A change to the core
Devicecontract must remain implementable by both platform devices. Platform-only capabilities must still fail clearly on the other platform. - A change to device defaults or WebGPU feature profiles must keep adapter requests, attached-device reporting, and application capability checks aligned.
- A change to render-pass draw options, dynamic parameters, or render-bundle compatibility must keep engine calls aligned with both native pass models.
- A change to resource handle ownership, borrowed handles, attached resources, or resource statistics must preserve destruction behavior for both native APIs.
- A change to shader binding scans, shader layouts, uniforms, or typed-array rules must keep engine inputs aligned with WebGL attribute setup and WebGPU bind-group layout.
- A change to texture format support or capability probing must keep shared format claims aligned with WebGL extensions, texture-storage limits, and native renderability.
- A change to texture copy, read, or write layout rules must preserve both WebGL emulation and WebGPU buffer-copy alignment.
- A change to upload, fence, timestamp-query, or submit ordering must preserve WebGL flush behavior and WebGPU queue and staging-copy behavior.
- A change to shader validation or compiler-message handling must keep WebGL GLSL diagnostics and WebGPU WGSL diagnostics visible through the shared shader surface.
- A change to vertex layout resolution must keep logical engine slots aligned with WebGL attributes and WebGPU draw-time buffer slots.
- A change to canvas pixel-size or observer rules must keep drawing-buffer size and native target configuration aligned after resize and device-pixel-ratio changes.
- A change to context or device attachment, reuse, or loss must keep wrapper identity, ownership, and resource allocation rules clear for both platforms.
- A new platform must provide adapter selection, capability reporting, resource creation, command submission, and loss handling through the shared contract.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A platform device does not implement a new core resource or command contract | Engine work fails only after that platform is selected | Update both device implementations and keep platform-only methods explicit |
| A WebGPU feature profile requests an unsupported feature or limit | Device creation fails even when WebGPU is present | Request only supported optional features and expose the selected effective profile |
| A platform wrapper destroys a borrowed native handle | An external owner loses a resource that luma.gl only referenced | Preserve the owned-versus-borrowed handle rule during resource cleanup |
| A resource or pass is used with a device from another platform | Native handles are cast to the wrong API and rendering fails | Keep resources bound to their creating device and use adapter-created wrappers |
| A render bundle uses attachment formats or sample settings that do not match its pass | WebGPU validation rejects bundle replay | Keep bundle encoder settings compatible with every executing render pass |
| A shader binding or vertex layout maps to different resource or attribute slots on one backend | A draw can use the wrong data or fail native validation | Keep shader scans, logical layouts, WebGL attribute setup, and WebGPU slot resolution in agreement |
| A texture format is reported as supported without its WebGL extension, texture-storage support, or renderability probe succeeding | Texture creation or framebuffer use fails on the selected device | Derive WebGL capabilities from extension checks and native format probes |
A WebGL stencil8 texture is treated as valid storage | Texture creation follows an invalid WebGL path | Keep standalone stencil textures unsupported while allowing valid renderbuffer use |
| A texture copy uses invalid row stride or region bounds | The upload or readback fails or returns incorrectly placed image data | Use shared texture normalization and memory-layout rules before backend transfer |
| A WebGL write or pass runs before an earlier queued copy is flushed | A draw can read stale data | Preserve the ordered command path in writeBufferViaCommandEncoder and submit |
| A WebGPU upload is performed outside the submitted command stream | A draw can run before its data reaches the destination buffer | Record the upload as a staging-buffer copy on the active command encoder |
| A fence or timestamp query is read with the wrong platform completion model | Work appears complete too early or profiling data is missing | Use WebGL sync polling and WebGPU queue completion through the shared fence and query contracts |
| A canvas changes size without matching native target configuration | The frame has the wrong pixel size or aspect | Apply the pending shared canvas resize before acquiring the next platform target |
| A WebGL context or WebGPU device is lost | Existing resources and later frame work are no longer usable | Observe device.lost, stop or rebuild the render path, and recreate resources |
| WebGL context creation accepts a software fallback when the caller forbids it | The runtime uses a low-performance renderer against the caller's policy | Keep the hardware-first request and honor failIfMajorPerformanceCaveat |
| A WebGPU device is attached twice or an already-lost device is wrapped | Multiple wrappers split ownership or later work fails immediately | Reuse the live wrapper and reject already-lost handles before attachment |
| WebGL code requests a WebGPU-only capability or WebGPU receives GLSL shader source | Resource or shader creation throws | Check device features and shader language before selecting platform-specific work |
Key files
| File | Why to read it |
|---|---|
| modules/…/luma.ts | Shows adapter registration and platform selection. |
| modules/…/device-defaults.ts | Shows the shared creation defaults and debug policy. |
| modules/…/device.ts | Defines the shared device, capability, resource, pass, submit, and loss contract. |
| modules/…/resource.ts | Shows shared resource ownership, attached lifetimes, and runtime statistics. |
| modules/…/command-encoder.ts | Defines shared command recording, copy, and optional GPU timing behavior. |
| modules/…/render-pass.ts | Defines the portable render target, binding, draw, indirect-draw, query, and timing surface. |
| modules/…/render-bundle.ts | Defines reusable WebGPU draw command recording and pass compatibility. |
| modules/…/texture.ts | Defines shared texture transfer, layout, and readback semantics. |
| modules/…/parameters.ts | Defines portable pipeline and dynamic render-pass state. |
| modules/…/shader-layout.ts | Defines portable resource and shader binding shapes. |
| modules/…/canvas-surface.ts | Shows how canvas layout, pixel size, and resize events become a GPU drawing surface. |
| modules/…/model.ts | Shows how application rendering inputs become draw work. |
| modules/…/animation-loop.ts | Shows frame scheduling and submission control. |
| modules/…/index.ts | Shows the public WebGL adapter and resource surface consolidated in core. |
| modules/…/webgl-device.ts | Shows WebGL context creation, reuse and ownership, loss handling, ordered submission, and the shared device implementation. |
| modules/…/webgl-canvas-context.ts | Shows how the WebGL default framebuffer follows the drawing-buffer size. |
| modules/…/create-browser-context.ts | Shows hardware-first WebGL context selection and controlled software fallback. |
| modules/…/webgl-state-tracker.ts | Shows WebGL state caching, restoration, and deleted-binding invalidation. |
| modules/…/webgl-texture-table.ts | Shows WebGL format mapping, extension support, texture exclusions, and renderability checks. |
| modules/…/webgl-shader.ts | Shows GLSL compilation status and diagnostics. |
| modules/…/webgl-fence.ts | Shows WebGL completion signaling. |
| modules/…/webgl-render-pass.ts | Shows how shared passes become WebGL state and draw calls. |
| modules/…/webgl-vertex-array.ts | Shows how shared vertex layouts become WebGL VAO and attribute state. |
| modules/…/webgpu-adapter.ts | Shows WebGPU profile selection, limits, adapter creation, attachment, and wrapper reuse. |
| modules/…/webgpu-device.ts | Shows the WebGPU implementation, feature reporting, ordered uploads, submission, ownership, and device loss handling. |
| modules/…/webgpu-shader.ts | Shows WGSL validation and compiler-message handling. |
| modules/…/webgpu-fence.ts | Shows queue-based WebGPU completion signaling. |
| modules/…/webgpu-render-pass.ts | Shows how a shared render pass becomes native WebGPU attachments, state, draw work, and bundle replay. |
| modules/…/webgpu-vertex-array.ts | Shows how logical vertex bindings become WebGPU draw-time buffer bindings. |