Introducing a new way to review your PRs.

Read the announcement

visgl / luma.gl

High-performance Toolkit for WebGL-based Data Visualization

View on GitHub

30 documents · Updated Nov 20, 2026

docs / shader-systems

Shader systems

The shader systems give rendering features one way to author and extend GPU programs across WebGL and WebGPU. They combine reusable shader modules with application source, select language-specific code, and return the resource information that the rendering runtime needs.

System purpose

The @luma.gl/shaders package is the public home for shader modules, assemblers, plugins, injection contracts, inspection tools, and shared shader data types. It also exposes shared lighting and material features so the renderer can build related GPU programs from the same feature contracts.

The package exposes separate GLSL, WGSL, and type entry points. The optional @luma.gl/slang package provides a source-language path without making the core shader package depend on one compiler.

References: modules/…/package.json• L1-5 modules/…/package.json• L29-49 modules/…/index.ts• L5-16 modules/…/index.ts• L64-70 modules/…/index.ts• L156-171 modules/…/index.ts• L262-311 modules/…/package.json• L1-5 modules/…/package.json• L21-31

Authoring model

Shader authoring keeps a feature separate from a complete application program. A feature supplies reusable code and a host-side contract, while the application supplies entry points and chooses the pipeline.

Shader modules

The shader module is the main unit for reusable shader behavior. It gives a feature a stable name, source, dependencies, optional defines, resource declarations, and a function that maps feature properties to uniforms or bindings. A module can serve GLSL and WGSL through the same feature contract while using target-specific source where needed.

Module initialization prepares dependencies and default values before assembly. The combined uniform function then visits modules in dependency order so a module can use values produced by its dependencies.

The public shader package also owns reusable lighting and material modules that need the same portability. The current set includes unlit and PBR material paths, direct-light shadows, contact shadows, ambient occlusion, lightmap baking and filtering, surface-normal output, and clustered-light lookup. These modules let renderer passes share the same material and visibility rules instead of embedding them in separate programs.

The Shader modules page describes the reusable module surface in more detail.

References: modules/…/shader-module.ts• L34-100 modules/…/shader-module.ts• L113-182 modules/…/shader-assembly-shared.ts• L20-30 modules/…/index.ts• L18-50 modules/…/index.ts• L262-311 modules/…/pbr-material-glsl.ts• L58-75 modules/…/pbr-material-wgsl.ts• L155-203

Lighting modules

The lighting modules form a set of optional layers around the shared material model. The PBR material supplies the surface response, while shadow and contact-shadow modules supply direct-light visibility, ambient occlusion supplies indirect visibility, and surface and lightmap modules expose intermediate data for later passes. The unlit material is the simple path when lighting is not needed.

These layers use dependencies to share the PBR material contract. Their host properties describe the textures, transforms, light data, and strength controls that the renderer must provide. Most of the new lighting paths provide matching GLSL and WGSL source, so a render feature can select the backend without changing its high-level role.

The lighting stack supports two kinds of work. Material modules change the color or visibility used by a final surface shader. Pass modules create intermediate textures, such as traced contact visibility or filtered lightmap data, that later material modules consume.

References: modules/…/index.ts• L273-311 modules/…/pbr-shadow.ts• L21-60 modules/…/pbr-ambient-occlusion.ts• L18-77 modules/…/pbr-contact-shadow.ts• L18-77 modules/…/pbr-surface.ts• L20-102 modules/…/unlit-material.ts• L29-44

Shader plugins

Shader plugins add a feature to an existing program without owning the full program. A plugin can contribute a module, compile-time values, vertex inputs, values passed from vertex to fragment stages, and code that runs at named extension points.

Plugin resolution selects the source for the active language and checks that shared interfaces agree. This gives a feature one portable description while keeping GLSL and WGSL syntax separate.

References: modules/…/shader-plugin.ts• L14-63 modules/…/shader-plugin.ts• L65-90 modules/…/shader-plugin.ts• L125-230 modules/…/shader-plugin.spec.ts• L51-116

Shader injection targets

Shader injection targets are named callable extension points in a shader stage. A target declares its stage, parameters, optional return type, and language-specific wrapper code. Plugins or application code add ordered statements to the target instead of replacing arbitrary source text.

The same target name can have separate GLSL and WGSL declarations. Assembly rejects an injection that has no active target, so a spelling or stage error fails close to the source of the problem.

References: modules/…/shader-injection-targets.ts• L7-27 modules/…/shader-injection-targets.ts• L42-72 modules/…/shader-injections.ts• L20-71 modules/…/shader-plugin.ts• L164-212

Shader passes

A shader pass describes a separate rendering step. It can read the original input, a prior pass, or a named render target. Composite passes keep the order of steps and the links between their intermediate targets.

WebGPU can use an optional compute stage for a compatible step. The fragment path remains the portable form, so a feature can keep one pass description while the runtime selects a supported route.

References: modules/…/shader-pass.ts• L9-69 modules/…/composite-shader-pass.ts• L8-47

Composition flow

Composition turns application source and feature declarations into target shader code plus the host data needed to use it. The assembler owns the shared state for default modules, injection targets, and language compilers.

Module resolution

The system resolves the module graph before it emits source. Dependencies are ordered before their users, duplicate names are removed at the assembler boundary, and module initialization prepares the host contract for later assembly.

An assembler instance can hold default modules and injection targets for repeated requests. GLSL and WGSL assembler instances keep their compiler and binding state separate, so one backend does not change another backend's result.

References: modules/…/shader-assembler-base.ts• L21-39 modules/…/shader-assembler-base.ts• L101-168 modules/…/shader-module.ts• L113-159

Source assembly

Assembly combines application source with module code, plugin contributions, compile-time defines, and named injections. The result keeps the application entry points while adding the feature code and stage interfaces needed by the selected backend.

This diagram shows how feature declarations become executable shader code and a host resource contract.

flowchart LR
  A[Application source] --> C[Target assembler]
  M[Shader modules] --> C
  P[Plugins and injection targets] --> C
  T[Platform information] --> C
  C --> G[GLSL program]
  C --> W[WGSL program]
  C --> R[Uniforms bindings and layout]
  R --> H[Pipeline and resource setup]

GLSL assembly returns a vertex and fragment pair. WGSL assembly returns one source result with the selected entry points, binding assignments, and optional interface layout.

References: modules/…/glsl-shader-assembler.ts• L10-37 modules/…/wgsl-shader-assembler.ts• L18-86 modules/…/assemble-glsl-shaders.ts• L50-82 modules/…/assemble-wgsl-shaders.ts• L58-98

Backend portability

Portability comes from keeping feature properties and shader interfaces stable while the assembler selects native syntax and platform workarounds. PlatformInfo supplies the backend, language, GPU, features, and limits that can affect the generated program.

GLSL and WGSL targets

GLSL uses separate vertex and fragment sources. WGSL uses a unified source with selected entry points for render or compute work. Both paths apply the active language's module source, injection code, and stage interfaces before returning a result for pipeline creation.

WGSL module bindings can use automatic locations. The assembler keeps application locations separate from module locations and preserves module assignments across repeated use of one assembler. It returns the assignments as debug data so resource setup can explain the final layout.

References: modules/…/shader-assembly-types.ts• L18-69 modules/…/wgsl-shader-assembler.ts• L29-86 modules/…/assemble-wgsl-shaders.ts• L417-559 modules/…/shader-assembler.spec.ts• L365-443

Interface and layout metadata

Assembly metadata is the bridge between generated source and host resources. It describes the bindings, entry points, vertex inputs, and shader layout that the runtime uses to build a compatible pipeline.

WGSL interface scanning can discover a simple layout. When the source shape is not clear enough to scan, the result is absent and the caller must provide the layout. Uniform field names and order must also match the shader block or struct so host values keep the expected meaning.

Shader type decoding supplies the shared meaning of variable and vertex types used by these host contracts. It resolves WGSL aliases such as vec3f, reports primitive type and component count, and derives vertex byte length plus integer and signedness flags. Unsupported types fail instead of producing incomplete resource metadata.

References: modules/…/wgsl-shader-assembler.ts• L29-86 modules/…/shader-assembly-types.ts• L108-130 modules/…/index.ts• L11-16 modules/…/shader-type-decoder.ts• L13-31 modules/…/shader-type-decoder.ts• L33-74 modules/…/shader-type-decoder.ts• L109-129

Portable numeric modules

Numeric modules keep a shared math interface when GPU backends differ in precision or supported operations. The fp64 module is one example: it presents a stable feature contract while choosing device-aware arithmetic for the target language.

These modules let projection and other rendering features share numeric behavior instead of owning separate backend-specific math code.

References: modules/…/fp64.ts• L31-62 modules/…/fp64-arithmetic-wgsl.ts• L216-243 modules/…/fp64-functions-wgsl.ts• L58-157

Language tooling

Language tooling separates shader composition from the language used to author source. Applications can provide native GLSL or WGSL, or register a compiler that converts another source language into the assembler's target language.

Transpiler boundary

The transpiler boundary receives source, target language, shader stage, entry-point names, and platform information. It returns target code and can return updated entry-point names for pipeline selection. The shader package therefore does not need to own a particular source compiler.

The Slang integration page describes the repository's Slang path and its reflection results.

References: modules/…/shader-assembler-base.ts• L41-99 modules/…/shader-assembly-types.ts• L29-69 modules/…/package.json• L1-5

Source diagnostics

The tooling can preserve source-level diagnostics and source maps across translation. The destination device must still compile the final GLSL or WGSL because source checks cannot prove every device limit or native resource rule.

References: modules/…/index.ts• L72-92 modules/…/shader-assembly-types.ts• L65-69

Runtime contracts

The shader systems end with portable program source and metadata. The rendering runtime remains responsible for pipeline creation, resource binding, device limits, and final compilation on the selected backend. The lighting relationship is described in Rendering and lighting, while the frame-level handoff is described in Engine render loop.

Resource contracts

Module properties affect both generated code and the resources that host code binds. The assembler combines module uniform functions into one host-facing function, while WGSL assembly reports binding assignments and layout data for pipeline setup.

Texture format types provide a shared vocabulary for color, depth, stencil, and compressed texture resources. The runtime still checks whether the selected device supports the requested format and feature.

References: modules/…/shader-assembly-shared.ts• L20-30 modules/…/wgsl-shader-assembler.ts• L29-86 modules/…/shader-module.ts• L71-86 modules/…/texture-formats.ts• L9-23

Lighting resource contracts

Lighting modules connect shader code to renderer-produced textures and light state. The shadow module accepts resolved directional, spot, and point-light data plus their depth maps. PBR shadow integration turns those visibility factors into direct-light inputs, while contact shadows and ambient occlusion consume screen-space textures with a receiver viewport.

Lightmap baking uses a second UV set and chart identity to produce reusable irradiance. Its filter pass can accumulate results or fill uncovered chart pixels before the PBR material reads the lightmap. This makes baked lighting a producer-consumer flow rather than a property of the final material alone.

This diagram shows the main relationship between lighting producers, visibility modules, and the final PBR surface.

flowchart LR
  L[Light and shadow state] --> S[Shadow module]
  D[Depth and normal textures] --> C[Contact shadow passes]
  C --> PC[PBR contact shadow]
  A[Ambient occlusion texture] --> AO[PBR ambient occlusion]
  B[Lightmap bake and filter] --> LM[Lightmap texture]
  S --> PS[PBR shadow]
  PC --> P[PBR material]
  AO --> P
  PS --> P
  LM --> P
  P --> O[Lit surface output]

References: modules/…/shadow-types.ts• L21-68 modules/…/shadow.ts• L381-453 modules/…/contact-shadow.ts• L10-32 modules/…/pbr-contact-shadow.ts• L8-77 modules/…/lightmap.ts• L8-76 modules/…/lightmap-filter.ts• L6-73

Change checklist

When these files or sections are changed, remember to consider these:

  • When module source, uniforms, bindings, or dependencies change, keep the host property mapping, shader layout, dependency order, and target-language source aligned.
  • When plugin inputs, stage values, injection targets, or injection order change, check both shader stages and both GLSL and WGSL declarations.
  • When WGSL binding rules change, check application locations, module locations, stable assignments, and the metadata consumed by pipeline setup together.
  • When a source-language or entry-point contract changes, update the registered compiler and the runtime pipeline selection.
  • When platform workarounds or numeric modules change, check the device conditions that select them and the features that depend on their precision.
  • When texture format types change, check the resource validation and backend mappings that consume the shared format names.
  • When a public shader module moves or changes its resource contract, preserve its package export and keep its GLSL and WGSL resource names aligned.
  • When a lighting producer changes its texture or viewport meaning, check every material module that consumes the visibility or lightmap result.
  • When PBR material fields or helper names change, check the PBR surface, shadow, ambient-occlusion, contact-shadow, and lightmap dependencies together.
  • When shadow light limits or packed arrays change, keep the resolved renderer data, depth-texture layers, matrix order, and GLSL/WGSL sampling rules aligned.
  • When a PBR visibility module is enabled without its texture or with a stale viewport, the module can read an invalid resource or apply visibility to the wrong pixels. Treat bindings and viewport data as one runtime contract and disable the layer when it is incomplete.
  • When shader type aliases or primitive sizes change, keep decoded component counts, byte lengths, integer flags, signedness, vertex layouts, and uniform metadata aligned.

Important failure modes

TriggerConsequenceGuard
A module name or dependency contract conflictsAssembly selects the wrong feature or cannot form a valid module orderKeep module names stable and resolve dependencies before source assembly
A plugin declares a conflicting input, stage value, or injection targetShader stages cannot share a valid interfaceResolve plugin declarations for the active language and reject conflicts
An injection names a target that is not registered for its stageAssembly stops instead of producing an ambiguous programRegister the target for the active language and stage
Module uniform metadata differs from the shader block or structHost values use the wrong fields or layoutKeep field names, order, types, and packing rules aligned
An unsupported variable or attribute type reaches the decoderResource metadata is not produced and assembly or setup stopsUse a supported canonical type or registered alias
Application and module WGSL bindings overlapPipeline creation sees duplicate or unstable resource locationsReserve application locations first and inspect binding assignments
A foreign source language has no registered compilerAssembly stops before target code existsRegister a transpiler on the matching assembler
Translated entry points do not match pipeline selectionThe runtime cannot select the intended shader entry pointPass returned entry-point names through to pipeline creation
WGSL layout scanning cannot resolve the source shapeAutomatic pipeline layout data is unavailableSupply an explicit layout when scanning returns no result
The device lacks a required format, feature, or limitFinal shader or pipeline creation failsCheck platform information and device support before resource creation
A lighting pass uses a texture with the wrong viewport, UV set, chart, or coordinate conventionVisibility or baked light appears shifted or affects the wrong surfaceKeep producer metadata and the consuming PBR module's coordinate contract aligned
Shadow light arrays, cascade splits, or matrix order do not match the shadow texturesDirect-light visibility is sampled from the wrong light or cascadeResolve and pad the same light order for uniforms, texture layers, and matrices
A PBR visibility module is enabled without its texture or with a stale viewportThe module can read an invalid resource or apply visibility to the wrong pixelsTreat bindings and viewport data as one runtime contract and disable the layer when it is incomplete

Key files

FileWhy to read it
modules/…/index.tsPublic shader module, assembler, plugin, inspection, and feature surface
modules/…/shader-module.tsCore feature, resource, and host-value contract
modules/…/shader-assembler-base.tsShared assembler state and compiler boundary
modules/…/glsl-shader-assembler.tsGLSL assembly entry point
modules/…/wgsl-shader-assembler.tsWGSL source, entry points, bindings, and layout result
modules/…/assemble-glsl-shaders.tsGLSL module and injection composition
modules/…/assemble-wgsl-shaders.tsWGSL composition, binding relocation, and assignment metadata
modules/…/shader-plugin.tsCross-backend plugin interfaces and conflict checks
modules/…/shader-injection-targets.tsNamed extension-point contract for GLSL and WGSL
modules/…/shader-injections.tsLanguage selection and ordered code injection behavior
modules/…/shader-assembly-shared.tsShared uniform composition and module source handling
modules/…/pbr-material-glsl.tsGLSL PBR surface contract and material lighting flow
modules/…/pbr-material-wgsl.tsWGSL PBR surface contract and material lighting flow
modules/…/shadow.tsBackend-aware visibility module for directional, spot, and point shadows
modules/…/shadow-types.tsHost-side light, texture, and shadow resource contract
modules/…/pbr-shadow.tsPBR integration for direct-light shadow visibility
modules/…/contact-shadow.tsScreen-space contact-shadow trace, blur, and composite passes
modules/…/pbr-contact-shadow.tsPBR integration for contact-shadow visibility
modules/…/pbr-ambient-occlusion.tsPBR integration for ambient-occlusion textures
modules/…/lightmap.tsTexture-space lightmap baking inputs and irradiance output
modules/…/lightmap-filter.tsLightmap accumulation, copy, and chart-fill behavior
modules/…/pbr-surface.tsIntermediate PBR normal-surface output
modules/…/unlit-material.tsBackend-neutral no-light material path
modules/…/clustered-light-grid.tsScreen and depth lookup used to select a clustered-light cell
modules/…/prefiltered-environment.tsPortable PBR environment filtering module and its GLSL and WGSL shader sources
modules/…/shader-type-decoder.tsCanonical shader type aliases and decoded vertex and uniform metadata
modules/…/texture-formats.tsBackend-neutral texture resource vocabulary
modules/…/package.jsonScope and public entry points of the optional Slang package