Layer lifecycle and state
Layers turn application settings and data into GPU-ready attributes and draw calls. The layer manager matches new layer descriptions to existing layers so state and resources can survive updates. It also waits for view and coordinate context when needed, creates composite children, and removes layers that are no longer present.
Layer model
Layer construction
The base Layer describes one renderable or composite unit. Its props define the requested data, visibility, picking, coordinate, render, and update behavior. The manager supplies shared device, viewport, resource, timing, and error context before the layer enters its lifecycle. If a viewport or source coordinate system is not ready, the manager keeps the layer from running lifecycle work until that context is available.
The layer retains the resolved source coordinate system in its internal state. A source coordinate system that is still loading blocks lifecycle and draw work until the manager installs the resolved value.
Layer data can be a collection, a length-based binary source, a URL, a promise, or a stream of batches. The layer props also define how data equality, partial row changes, loading, and accessor dependencies affect later updates. These inputs let the runtime distinguish a new data source from a new view of the same data.
References: modules/…/layer.ts• L97-215 modules/…/layer.ts• L244-264 modules/…/layer.ts• L373-395 modules/…/layer-manager.ts• L239-277 modules/…/layer-manager.ts• L294-337 modules/…/layer-props.ts• L19-24 modules/…/layer-props.ts• L88-102 modules/…/layer-props.ts• L107-177
Coordinate conversion
Layers convert points from their own coordinate system into the runtime's common space. The manager resolves the source coordinate system for each layer and can pause initialization or updates while that resolution is pending. The layer tracks a preprojection signature for source-to-packed conversion and a separate tessellation signature for view geometry, so position attributes and shader-side camera conversion respond to the right changes.
Coordinate settings describe the data after its origin and model transform. The default setting follows the active view: it uses a geospatial source in a geospatial view and a cartesian source in another view. When the source and target systems need conversion, the layer refreshes the conversion functions and marks projection-dependent attributes for update. Camera precision and target common space do not by themselves change the source-to-packed conversion.
References: modules/…/layer.ts• L382-439 modules/…/layer.ts• L483-491 modules/…/layer.ts• L1364-1395 modules/…/layer-manager.ts• L500-539 modules/…/layer-props.ts• L172-185
Projection resources
The layer manager owns shared scale resources for custom geospatial projections. The active viewport selects the resource used by projection shader inputs, so layers share the viewport's local scale instead of creating separate copies. The manager releases these resources during shutdown.
References: modules/…/layer-manager.ts• L83-110 modules/…/layer-manager.ts• L134-143 modules/…/projection-scale-resources.ts• L12-54 modules/…/layers-pass.ts• L466-479
Layer matching
The manager matches each new layer to the previous layer list by id. A match transfers state to the new layer object and diffs its props. A new layer is initialized, and an old unmatched layer is finalized. This lets the current layer description change while its models and other state remain available.
The manager also expands composite layers into child layers during the same pass. It flattens the result into the render list while preserving parent links that control visibility, filtering, order, and picking. A change to the manager's default shader modules or plugins marks existing layers for a coordinated update.
The diagram shows how a new layer list becomes the active render list.
flowchart TD
A[New layer list] --> B{Viewport and source CRS ready?}
B -->|No| C[Keep input pending]
B -->|Yes| D[Match by id]
D -->|No match| E[Initialize layer state]
D -->|Match| F[Transfer state and diff props]
E --> G[Generate composite children]
F --> G
G --> H[Active render list]
D -->|Old layer not matched| I[Finalize old state]
References: modules/…/layer-manager.ts• L384-422 modules/…/layer-manager.ts• L424-477 modules/…/layer-manager.ts• L541-599
Layer state
State initialization
Initialization creates the state for a new layer. This state tracks attributes, redraw and update needs, the active viewport, source and packed coordinate systems, coordinate conversion, uniform transitions, asynchronous props, picking, SQL accessor work, and values owned by the layer. The layer then runs initializeState so it can prepare models, attributes, and other resources.
Initialization marks the first data and props as changed and runs the first update. A new layer therefore uses the same update path as a later change before it draws. If SQL accessors are still being evaluated, the layer keeps its current attributes until the new data and columns are ready together.
References: modules/…/layer.ts• L1364-1430 modules/…/layer.ts• L1436-1459 modules/…/layer-state.ts• L19-126
Layer-owned state
Props describe the requested configuration. Layer state stores values produced or retained by the layer, such as models and instance information, so they can persist across matched layer objects. setState records a state change and requests a redraw. setNeedsUpdate asks the manager to run a deeper update when the layer or its children need new structure.
Use redraw work when existing resources can render with new values. Use update work when the layer must run its update path or rebuild child layers. The manager collects both requests for the next render cycle.
References: modules/…/layer.ts• L336-367 modules/…/layer.ts• L916-930 modules/…/layer-manager.ts• L145-188 modules/…/layer-manager.ts• L262-277 modules/…/layer-state.ts• L64-88
Prop and data updates
Change detection
When a matched layer receives new props, its lifecycle compares the new values with the old values and records change flags. Data has a separate comparison because it can be large. A layer can define custom data equality or identify changed row ranges. Other props use their type's equality rule or a shallow comparison.
Update triggers form a separate path for accessor inputs. A trigger change invalidates only the attributes that use it. A data change normally invalidates all attributes, while a row-range diff limits the affected data. Viewport, source coordinate, model matrix, coordinate origin, and projection changes invalidate position work that depends on the active view.
The main change flags are:
| Flag | Meaning |
|---|---|
dataChanged | Data changed, with optional row ranges. |
propsChanged | A regular layer prop changed. |
updateTriggersChanged | Accessor inputs changed for named attributes. |
viewportChanged | The layer is active in a different viewport. |
projectionChanged | The viewport projection or its conversion to common space changed. |
stateChanged | Layer-owned state changed. |
extensionsChanged | Layer extensions or default shader modules changed. |
References: modules/…/layer.ts• L1675-1730 modules/…/layer.ts• L1749-1782 modules/…/layer-state.ts• L19-33 modules/…/props.ts• L19-55 modules/…/props.ts• L92-142 modules/…/props.ts• L169-222
Update decision
The layer uses shouldUpdateState to decide whether to run updateState. The normal rule responds to changed props or data. Projection changes, active uniform transitions, and explicit deep-update requests can also require an update. A subclass can add other dependencies, such as viewport or layer-owned state.
After the decision, the layer gives the change flags to its state update, refreshes attributes and shared settings, and invalidates projection-dependent attributes when needed. It clears the flags and old-prop snapshot only after this work, so one update sees a consistent change record.
The diagram shows the path from new inputs to refreshed layer resources.
flowchart LR
A[New props data view or coordinate context] --> B[Diff against old props]
B --> C[Set change flags]
C --> D{Should update state?}
D -->|No| E[Keep resources]
D -->|Yes| F[Run updateState]
F --> G[Refresh attributes and model settings]
G --> H[Request redraw and clear flags]
References: modules/…/layer.ts• L798-805 modules/…/layer.ts• L1463-1564 modules/…/layer.ts• L916-930
Asynchronous prop updates
Async props let data or other values arrive from a URL, promise, or supported async iterable. Layer state keeps the input and the latest resolved value. A newer load takes priority over an older load, and a resolved value starts a new prop diff with a deep-update request.
For data, the state also tracks the effective query, query parser, fetch function, loaders, load mode, and load options. A change in that request context can reload unchanged input. The data path can apply a query before the layer uses the loaded value. For streamed data, state records the new row range so supported layers can update only that part. Finalization ends the resource subscriptions.
References: modules/…/layer-state.ts• L49-57 modules/…/layer-state.ts• L134-165 modules/…/layer-state.ts• L167-223 modules/…/layer.ts• L1941-1946 modules/…/layer-props.ts• L119-149
Attribute and draw contributions
Attribute generation
An attribute is a shader input for each vertex or instance. The attribute manager owns its values and knows which data or prop inputs can rebuild them. A layer registers its attribute rules during initialization and invalidates them when change detection marks those inputs as stale.
Before a draw, the manager chooses the best source for each attribute: an external GPU buffer, packed data, a constant prop, or an updater that reads layer data. It updates only invalidated ranges and reports changed values back to the layer. Attribute transitions can replace the source values during animation, keep redraws active while values move to their new state, and restore the model layout when a transition ends or changes type.
References: modules/…/layer.ts• L967-1005 modules/…/attribute-manager.ts• L160-259 modules/…/attribute.ts• L127-217 modules/…/attribute.ts• L219-287 modules/…/attribute-transition-manager.ts• L61-95 modules/…/attribute-transition-manager.ts• L118-181
Column preparation
Column preparation provides a data path for layers that receive named columns or binary attributes. It converts supported column inputs into shader-ready vectors, fills missing columns from layer accessors, and keeps all columns aligned to one row count before a draw.
The preparation path can retain compatible external buffers or create owned GPU data. It also supports position conversion, position bounds, row indexes for picking, and batched draws. It materializes one-shot row iterators so accessor fallback work can be retried or completed without consuming the data twice. A newer request replaces the prior prepared result only after conversion succeeds, and discarded results release their owned resources.
Column preparation validates both row lengths and chunk boundaries. This keeps fallback accessor buffers aligned with converted columns when the draw binds each batch separately. It reports conversion errors to the layer instead of replacing a valid prior result. Uniform-only changes do not restart column conversion.
References: modules/…/layer-columns.ts• L80-148 modules/…/layer-columns.ts• L150-205 modules/…/layer-columns.ts• L207-435 modules/…/layer-columns.ts• L437-505 modules/…/layer-columns.ts• L508-529
Model contribution
After attributes update, the layer sends changed buffer and constant values to its rendering models. A model is a GPU draw resource owned by a layer. Models can use regular or typed GPU attribute data. If an attribute changes its buffer layout, the layer refreshes the model layout before applying new values.
The layer also keeps model instance counts aligned with current data. The count can come from a prop, layer state, or the data container. This connects the amount of data to the number of instances in a draw call.
References: modules/…/layer.ts• L941-1005 modules/…/layer.ts• L1119-1222 modules/…/layer.ts• L1830-1839
Draw participation
A drawable layer contributes one or more model draws to a render pass. Before the draw, the layer applies current shader inputs, render settings, viewport values, and active prop transitions. It also aligns model settings with the active render target before submitting the draw. The default draw method draws every model returned by the layer, while a subclass can choose another sequence.
The layers pass decides whether the layer can draw. It applies visibility, filtering, parent state, operation, viewport, projection inputs, and render-pass state before submitting the draw. A composite layer is not itself drawable. Its child layers provide the draw calls.
References: modules/…/layer.ts• L1582-1671 modules/…/layers-pass.ts• L341-394 modules/…/layers-pass.ts• L416-453 modules/…/layers-pass.ts• L466-479
Composite layer contribution
A composite layer is a non-drawable parent that turns one logical layer into child layers. Its child-generation step creates the children during an update. The manager then matches and renders those children as normal layers, while the parent passes shared settings such as visibility, opacity, picking, coordinates, and update triggers.
Because the parent does not submit a model draw, a viewport change can require a deeper update when it changes the child set. Parent visibility and filtering still control whether descendant draws reach the render pass.
References: modules/…/composite-layer.ts• L19-47 modules/…/composite-layer.ts• L79-107 modules/…/composite-layer.ts• L145-232 modules/…/composite-layer.ts• L247-268 modules/…/layer-manager.ts• L443-477 modules/…/layers-pass.ts• L416-453
Layer finalization
Resource finalization
When a layer does not match the new layer list, the manager finalizes it. Finalization destroys models, releases attributes, clears uniform and attribute transitions, ends async subscriptions, and removes the internal state link. Extension finalizers run after the layer finalizer.
Finalization is separate from construction and state transfer. A finalized layer cannot be reused by a later match, and the manager requests a redraw because the active layer set changed. When the manager shuts down, it also destroys shared projection resources and other manager-owned resources.
References: modules/…/layer.ts• L848-864 modules/…/layer.ts• L1568-1579 modules/…/layer-state.ts• L112-126 modules/…/attribute-manager.ts• L103-109 modules/…/attribute-transition-manager.ts• L53-57 modules/…/layer-manager.ts• L134-143 modules/…/layer-manager.ts• L541-599 modules/…/projection-scale-resources.ts• L45-54
Related systems
The core rendering engine gives the wider device, pass, and manager context. The deck instance and render loop explains when updates and redraws run. The interaction and picking page explains how layer attributes and draw passes support selection. The custom projections page explains how projection settings affect layer coordinates and draws.
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to layer matching must keep IDs stable for state transfer and must finalize every old layer that is not matched.
- Changes to viewport or source CRS readiness must preserve pending layer input and must resume lifecycle work only with the correct projection context.
- Changes to
diffPropsorChangeFlagsmust preserve the link between update triggers, projection changes, attribute invalidation, andshouldUpdateState. - Changes to async prop resolution must keep newer resolved values ahead of late results and must stop subscriptions during finalization.
- Changes to query or loader request tracking must reload data when the effective parser, query, fetch path, loader set, mode, or load options change.
- Changes to SQL accessor evaluation must keep pending results from replacing current attributes until the matching data and expressions are ready.
- Changes to attribute registration or model layout must keep shader values and buffer layouts aligned with model instance counts.
- Changes to column definitions or preparation must preserve equal row lengths, compatible chunk boundaries, accessor fallbacks, and ownership of replaced GPU resources.
- Changes to composite child creation must preserve parent visibility, picking, coordinate settings, and update-trigger propagation.
- Changes to draw participation must keep operation, viewport activation, projection inputs, filtering, and render-pass state aligned across draw passes.
- Changes to default shader modules or plugins must trigger updates for every existing layer that uses the shared shader context.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
Two layers use the same id in one update | State can match the wrong layer and one layer can be lost. | Keep IDs unique in the rendered layer tree. |
| A layer update runs before a viewport or source CRS is available | Matching, position packing, or child generation can use missing projection context. | Keep lifecycle work pending until the required context is ready. |
| SQL accessor evaluation is still pending when a draw starts | The layer can skip its draw until the accessor columns are ready. | Keep current attributes and schedule the update after the matching query result resolves. |
| A data container changes in place without a comparator or data diff | The layer can keep stale attributes because no data change is detected. | Supply a new data container or define dataComparator and _dataDiff. |
| An accessor input changes without its update trigger | Existing attribute values remain in GPU buffers. | Include the input in the relevant updateTriggers. |
| A position transform changes without a projection or coordinate update | Position attributes can use stale projected values. | Keep projection signatures, model transforms, and coordinate settings in sync with invalidation. |
| Column inputs have different row counts or chunk layouts | A draw can bind rows to the wrong values or fail during preparation. | Keep column lengths and chunk structures aligned before drawing. |
| A newer column request completes after a later request | Old vectors can replace current columns or leak owned buffers. | Accept only the current request version and release superseded results. |
| An async load resolves after finalization | An old layer can try to update state or resources. | Clear the component link during finalization and ignore completed async state. |
| A query parser or loader option changes while the data input stays the same | The layer can keep results made with an old request. | Track the effective data request and reload when its inputs change. |
| An attribute transition changes type or is removed without a layout refresh | A model can keep a stale transition buffer or layout. | Remove the old transition and mark the attribute for redraw and layout update. |
| A composite parent hides or filters a child | The child produces no draw call even when the child is visible. | Check the full parent chain when diagnosing missing output. |
| A shared shader module or plugin changes without a layer update | Existing models can use stale shader inputs or code. | Mark all active layers for an update when the shared shader context changes. |
Key files
| File | Why to read it |
|---|---|
| modules/…/layer.ts | Defines layer props, lifecycle order, coordinate readiness, change flags, attribute refresh, model setup, and draw behavior. |
| modules/…/layer-manager.ts | Shows viewport and source CRS readiness, layer matching, state transfer, child expansion, update scheduling, shared resource ownership, and finalization. |
| modules/…/layer-columns.ts | Shows the column-oriented data path from named inputs and accessors to GPU vectors and batched draws. |
| modules/…/layer-props.ts | Defines the layer input contract for data sources, change detection, coordinates, rendering, transitions, and picking. |
| modules/…/layer-state.ts | Defines state that persists across layer objects and tracks redraw, updates, viewports, picking, change flags, async values, and data requests. |
| modules/…/props.ts | Defines prop, data, update-trigger, extension, and transition comparisons. |
| modules/…/component-state.ts | Explains async loading, streamed data, resolved values, and late-result handling. |
| modules/…/attribute-manager.ts | Explains attribute invalidation, data updates, transitions, and changed-value reporting. |
| modules/…/attribute.ts | Shows how an attribute gets values from buffers, constants, or data updaters. |
| modules/…/attribute-transition-manager.ts | Shows how attribute animations start, run during render cycles, and release their buffers. |
| modules/…/composite-layer.ts | Defines parent layers that create child layers and pass shared settings. |
| modules/…/layers-pass.ts | Shows how visibility, filtering, viewport activation, projection inputs, and draw calls reach the render pass. |