WebGL rendering resources
WebGL rendering resources are the browser client's GPU and display resources for turning a chart payload into a responsive plot. They coordinate WebGL2 contexts, mark data, color scales, themes, axes, picks, and cleanup while leaving text and chart controls to DOM and 2D layers. This page is a child of Browser rendering runtime; Browser worker and compute kernels and Browser interaction and animation cover the compute and event flows that use these resources.
Rendering resource model
The chart view separates data rendering from chart chrome. WebGL2 paints trace data into a plot surface, while 2D and DOM surfaces provide the background, grid, labels, legends, colorbars, tooltips, and controls.
The resource model connects one chart's retained payload to short-lived GPU state. A chart can therefore redraw a new view by changing mapping values instead of rewriting its data, and it can rebuild that state after a context loss.
flowchart LR
P[Chart specification and binary columns] --> V[Chart view]
V --> T[Trace GPU resources]
V --> S[WebGL color pass]
V --> K[WebGL pick pass]
S --> C[Visible chart surface]
K --> H[Hover and selection result]
V --> D[DOM and 2D chart chrome]
G[Shared GL host] --> S
G --> K
Chart view ownership
ChartView is the owner of chart-specific rendering state. It keeps the payload, trace records, WebGL programs, buffers, color textures, pick target, and the visible surfaces that present the result.
The view also owns the rules for when resources are valid. A resize changes the drawing size, a view change changes uniforms and pick state, and a data update either extends compatible buffers or replaces the affected trace. References: js/…/50_chartview.ts• L586-753, js/…/50_chartview.ts• L4610-4679, js/…/50_chartview.ts• L6438-6538
Shared host ownership
The shared GL host is a document-scoped owner for one reusable WebGL2 context. It uses a detached canvas and lets several charts take turns drawing into that context, then copies each color result into the chart's visible 2D canvas.
The host owns resources that are safe to share across charts, such as the fullscreen quad and compiled shaders. Each chart still owns mutable programs, uniforms, trace buffers, color textures, and pick resources. This boundary prevents one chart's style or view state from changing another chart's draw. The host registry also survives duplicate evaluations of the bundle in one browser realm, while keeping one host per document. References: js/…/42_glhost.ts• L29-58, js/…/42_glhost.ts• L88-109, js/…/42_glhost.ts• L166-216, js/…/42_glhost.ts• L218-285, js/…/50_chartview.ts• L4372-4453
Rendering contexts
The client chooses between a shared document context and a per-chart context. The shared path reduces the number of live browser contexts, while the per-chart path remains available for child frames, explicit fallback, and host allocation failure.
Frame scope and fallback
The shared host is enabled by default for a top-level document. A child frame uses the governed per-chart path unless it explicitly opts in, because a host inside each frame would still consume a process-wide browser context budget. The XY_SHARED_WEBGL switch can disable sharing or opt a frame into its own document-scoped host. References: js/…/42_glhost.ts• L68-81, js/…/42_glhost.ts• L624-649, tests/test_shared_glhost.py• L2018-2196
Context budget
Browsers impose a limit on live WebGL contexts. The context governor keeps fallback contexts below a chart budget by releasing the least recently visible charts first. Same-origin frames can share their live-count view through a browser channel, while cross-origin frames use their local view of the budget.
An intentional release keeps a static snapshot so a chart can remain visible without a live context. Pointer entry or a later visibility event can restore the context when the chart needs active rendering again. References: js/…/50_chartview.ts• L312-355, js/…/50_chartview.ts• L357-408, js/…/50_chartview.ts• L413-527, js/…/50_chartview.ts• L2090-2182
Context loss recovery
Context loss can come from a driver reset, browser pressure, or a controlled release. The client stops deferred GPU work, preserves the settled view, and marks the chart as lost so late work cannot write into dead resources.
Recovery rebuilds programs, buffers, textures, and pick resources from the retained specification and payload. A shared-host loss is fanned out to all registered charts. A failed or evicted per-chart context is replaced with a fresh canvas and retried with backoff when the chart is visible. Repeated rebuild failure on a live but unusable shared context escalates to a fresh shared surface. References: js/…/42_glhost.ts• L115-164, js/…/42_glhost.ts• L287-313, js/…/42_glhost.ts• L496-572, js/…/50_chartview.ts• L1877-2087, js/…/50_chartview.ts• L2190-2346, tests/test_shared_glhost.py• L487-1342
Mark rendering
Mark rendering turns each trace into a GPU drawing behavior. The chart-kind registry keeps this dispatch separate from the central view so each supported mark can define its resource build, draw, color refresh, and picking capabilities.
Mark dispatch
The registry maps related chart kinds to shared behavior. Rectangular marks share one path, bars add orientation and baseline rules, and point, line, area, mesh, ribbon, funnel, density, and heatmap traces use the resource shape that matches their geometry.
The central view supplies the current axis mapping and draw window. A mark renderer uses those values to draw its resident representation, while the level-of-detail layer can select a density, decimated, sampled, or drilled representation before dispatch. References: js/…/55_marks.ts• L3-35, js/…/55_marks.ts• L36-156, js/…/55_marks.ts• L158-282, js/…/50_chartview.ts• L6477-6507
Color and pick passes
The color pass draws the visible data with the current view, style, selection state, and transition state. The pick pass draws point-capable marks into a separate color-coded target so a pixel can identify a data row without changing the visible image.
Pickability is a capability of a mark, not a promise made by every trace. Non-point geometry uses its own browser-side hover path when available. The pick target is created only when a current trace can use it, and it is invalidated when geometry or view state changes. References: js/…/50_chartview.ts• L4471-4480, js/…/50_chartview.ts• L5985-6007, js/…/50_chartview.ts• L8510-8647
Color and theme resources
Color resources keep GPU paint, browser chrome, and responsive chart elements tied to the same visual theme. The client resolves CSS colors at the chart root, turns data color scales into reusable GPU lookup textures, and refreshes theme-dependent paint when the CSS cascade changes.
Colormap resources
A colormap is a mapping from a normalized data value to color. The client accepts a built-in name, a reversed name, or explicit RGB stops, then turns the stops into a 256-entry lookup texture for continuous data. Categorical palettes use a similar lookup texture that repeats the palette across its address range.
The same color scale also drives legends and colorbars. Density and heatmap resources use the lookup texture for count-only data, while grids that carry mean point colors can keep those colors in the grid texture itself. References: js/…/10_colormaps.ts• L22-87, js/…/50_chartview.ts• L4517-4571, js/…/50_chartview.ts• L6837-6889, js/…/45_lod.ts• L59-138
Theme resources
The theme reader takes chart background, grid, axis, and text values from CSS tokens such as --chart-bg and --chart-text. It also resolves authored CSS color expressions for mark paint, so a CSS theme can affect both GPU marks and browser-rendered chrome.
Theme changes come from the preferred color scheme or from class, data-theme, and style changes on the chart or its ancestors. The client refreshes mark colors and redraws the chart. It uses the painted backdrop when it dims legend entries, which keeps dimmed colors moving toward the actual page background. References: js/…/20_theme.ts• L17-24, js/…/20_theme.ts• L59-89, js/…/20_theme.ts• L213-234, js/…/50_chartview.ts• L733-747, js/…/50_chartview.ts• L9063-9089
Axis and colorbar ticks
Tick resources turn axis ranges into readable labels that match the chart's scale and geometry. The client generates ticks on the CPU and uses those values for both DOM labels and layout decisions, while explicit tick values and labels take precedence over automatic generation.
Axis tick generation
The axis path chooses a tick policy from the axis kind and scale. It supports linear, logarithmic, symmetric-log, time, category, and angular axes, with special handling for polar sectors and category spokes.
Formatting uses the selected step or the authored format. This keeps decimal precision, logarithmic magnitudes, time units, category names, and angular notation readable without sending tick work through the GPU. References: js/…/30_ticks.ts• L5-26, js/…/30_ticks.ts• L29-65, js/…/30_ticks.ts• L76-162, js/…/30_ticks.ts• L178-235, js/…/50_chartview.ts• L1418-1482
Colorbar scale
The colorbar is a browser surface that presents the active color scale beside or below the plot. It builds a continuous or banded gradient, places optional value lines and minor ticks, and formats automatic or explicit labels according to the colorbar scale.
Colorbar placement and compact responsive layouts share the plot's reserved space. On a narrow chart, the client keeps the scale and extreme labels while hiding lower-priority title and minor-tick chrome. References: js/…/50_chartview.ts• L761-870, js/…/50_chartview.ts• L4085-4282, js/…/50_chartview.ts• L4284-4356
Graphics resource lifecycle
The lifecycle treats GPU objects as disposable projections of retained chart state. This makes data changes, animation, context recovery, and chart destruction safe without keeping stale resources alive.
Allocation and reuse
Programs compile on first use, and lookup textures are cached by their color-scale value. Trace builds upload the geometry and style channels needed by the selected mark. Compatible streaming appends can extend existing buffers, while changes to encoding, style, or geometry use a full trace rebuild.
During transitions, the view may retain old and new trace resources long enough to draw both states. Level-of-detail caches retain only bounded point windows or density textures so a zoom path can keep a valid representation while a refined result is pending. Shared-host presentation grows its detached drawing buffer only as needed, then copies each chart's rendered rectangle into that chart's own 2D canvas. References: js/…/40_gl.ts• L5-20, js/…/40_gl.ts• L55-105, js/…/42_glhost.ts• L218-257, js/…/42_glhost.ts• L357-371, js/…/50_chartview.ts• L4482-4499, js/…/50_chartview.ts• L4610-4660, js/…/54_kernel.ts• L515-639, js/…/45_lod.ts• L6-27
Release and teardown
Trace teardown releases vertex arrays, buffers, textures, programs, density resources, transition resources, and pick targets. A shared host releases its pooled objects only after its last chart unregisters, while a native chart releases its own context during destruction.
The destroy path also removes listeners and stops pending work before releasing graphics resources. Context-loss paths clear handles without issuing delete calls against a dead context, then rebuild from retained state when recovery is possible. References: js/…/00_header.ts• L66-86, js/…/50_chartview.ts• L9091-9170, js/…/50_chartview.ts• L9186-9257, js/…/42_glhost.ts• L287-313, js/…/42_glhost.ts• L574-621
Change checklist
When these files or sections are changed, remember to consider these:
- Keep shared-host resources and chart-owned resources distinct. A chart teardown must not delete a shader, quad, or context still used by another chart.
- Keep the host's pass reset contract aligned with every texture unit, vertex attribute slot, uniform-buffer binding, and framebuffer that a mark renderer uses.
- Keep color lookup textures, legend swatches, colorbars, density grids, and theme refreshes aligned. A scale can look correct in one surface while being wrong in another.
- Keep the color pass and pick pass on the same coordinate projection and visible representation. Otherwise the chart can look right while hover or selection identifies the wrong row.
- Add every new trace buffer or texture owner to the matching rebuild and teardown paths. Retained transition and level-of-detail resources need the same review.
- Keep retained payload and settled view state sufficient to rebuild a chart after either shared-host loss or per-chart context replacement.
- Keep automatic and explicit tick rules aligned with axis scale, polar seam handling, colorbar placement, and any matching static-render behavior.
- Keep the shared-host gate aligned with document scope. A new frame or bundle-evaluation path must not create an uncounted host or bypass the intended native fallback.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A chart deletes a resource owned by the shared host | Other charts fail during their next pass | Host and client ownership split with final-client release |
| A shared pass leaves a texture, VAO, or framebuffer binding behind | The next chart draws with another chart's state | Host pass reset clears the tracked mutable GL state |
| A context is lost while old GPU work or a kernel reply is pending | Stale work targets dead handles or repaints an old view | Loss handling cancels work, advances the request sequence, and rebuilds from retained state |
| A live context keeps failing rebuilds without reporting loss | All charts can remain stuck in retry instead of recovering | Consecutive rebuild failures recycle the shared surface; a genuine loss starts a fresh failure count |
| A new mark buffer is not included in teardown | Repeated updates leak GPU memory | Central buffer ownership list and trace teardown |
| Pick geometry uses a different view or mark representation than color geometry | Hover and selection report the wrong item | Shared projection setup and pick invalidation |
| A theme changes after CSS colors were resolved | Marks, legends, or dimmed colors use the old palette | Theme observers refresh tokens and mark paint |
| A custom or reversed colormap is cached by object identity | Repeated specs create stale or unbounded lookup textures | Stable colormap keys based on names or stop values |
| Automatic ticks ignore the axis scale or polar seam | Labels do not describe the displayed geometry | Scale-specific tick generation and explicit-tick precedence |
| A child frame creates a shared host by default | Context use grows across frames and can exceed the browser budget | Top-level default gate with explicit frame opt-in and governed native fallback |
Key files
| File | Why to read it |
|---|---|
| js/…/50_chartview.ts | Central owner of chart GPU state, draw passes, theme refresh, context recovery, colorbars, and teardown. |
| js/…/42_glhost.ts | Defines document-level WebGL sharing, pass isolation, shader pooling, presentation copies, and host recovery. |
| js/…/40_gl.ts | Defines shader programs, fixed attribute slots, and the shared GPU drawing vocabulary. |
| js/…/55_marks.ts | Shows how trace kinds map to build, draw, color-refresh, and picking capabilities. |
| js/…/00_header.ts | Defines the renderer protocol and the complete trace buffer ownership list used for cleanup. |
| js/…/10_colormaps.ts | Defines built-in, reversed, and custom color-stop handling and lookup-table creation. |
| js/…/20_theme.ts | Defines CSS color resolution, chart theme tokens, backdrop selection, and chrome styles. |
| js/…/30_ticks.ts | Defines scale-aware tick generation and label formatting. |
| js/…/45_lod.ts | Defines density texture resources and bounded level-of-detail resource behavior. |
| js/…/54_kernel.ts | Shows how refined data and streaming updates replace or extend GPU resources. |
| js/…/60_entries.ts | Shows how widget and standalone entry points create and later destroy the chart view. |
| tests/test_shared_glhost.py | Verifies shared ownership, state isolation, shader reuse, mixed-size presentation, context recovery, fallback budgeting, and frame gates in a real browser. |