Slang shader integration
The Slang package gives luma.gl applications one shader authoring language that can become GLSL or WGSL. It connects shader source to luma.gl shader assembly, pipeline resource layouts, uniform data, and diagnostics while the application keeps control of devices and resources. See Shader systems for the wider shader model, Shadertools for the assembly layer, and Engine render loop for the rendering lifecycle.
Package role
The package is an experimental synchronous transpiler for a supported Slang authoring subset. It runs in browser and Node.js environments without a native compiler or WebAssembly runtime, then produces shader code that a luma.gl device can compile. The published @luma.gl/slang package exposes the core compiler through its main entry point and the luma.gl bridge through its ./luma entry point.
Shader authoring model
Slang source defines vertex, fragment, and compute entry points with shared types, helpers, resources, and stage interfaces. The package parses this source, checks types and calls, resolves source semantics, and lowers the reachable program into GLSL or WGSL. A language-neutral shader model keeps source concepts such as values, interfaces, resources, and canonical texture operations separate from target syntax.
The package is not the full upstream Slang compiler. Unsupported syntax or lowering fails with a source-aware error, and the generated GLSL or WGSL must still be accepted by the destination device.
References: modules/…/index.ts• L42-54 modules/…/ast.ts• L10-61 modules/…/ir.ts• L4-52 modules/…/diagnostics.ts• L6-19
Language frontends
The package now keeps source-language rules in separate frontends over a shared shader backend. The Slang frontend handles Slang parsing, module resolution, semantics, intrinsic names, and texture methods. A separate WGSL frontend supports a documented WGSL subset and can produce GLSL without importing the Slang parser or registry path.
This separation lets language tooling share target emission and shader metadata without making Slang source rules part of every compiler path. It also keeps the Slang-only and WGSL-only browser entry points independent.
References: modules/…/IR.md• L3-32 modules/…/wgsl.ts• L4-31 modules/…/wgsl-parser.ts• L4-10 modules/…/wgsl-analysis.ts• L21-29
Source modules
Applications can provide named Slang modules and native shader contracts to share code across source units or bridge to existing target-language shader code. Imports resolve from an application registry by exact name. Native modules provide target-specific code plus checked function contracts.
This model connects reusable shader authoring to luma.gl's existing shader modules without making Slang responsible for file or package loading. The application supplies the source and controls the public functions that form the contract between Slang code and native shader code.
References: modules/…/modules.ts• L12-70 modules/…/modules.ts• L95-173 modules/…/signatures.ts• L6-17
Compilation flow
Slang integration follows a source-to-pipeline path. The application gives luma.gl shader tooling a Slang transpiler. The transpiler returns target shader code and entry-point names. The rendering system then uses those results to create a render or compute pipeline.
Source analysis
Before target code is emitted, the compiler resolves declarations, entry points, value types, overloads, interfaces, resources, and reachable function calls. It also maps source spellings to common shader operations so target-specific names do not leak into the shared language model.
This analysis makes errors point to the authoring source instead of appearing only as failures in generated code. It also gives emission and reflection the same view of the shader contract.
References: modules/…/modules.ts• L71-174 modules/…/expression-analysis.ts• L17-42 modules/…/expression-analysis.ts• L95-308 modules/…/slang-operations.ts• L8-46
Target translation
The compilation target follows the active device path. WGSL serves WebGPU, while GLSL ES 300 serves WebGL 2 in the main rendering example. Unified WGSL compilation can keep selected stages in one shader module. Separate compilation remains available when the rendering workflow needs separate stage results.
The returned entry-point name is part of the compilation result and must be passed to pipeline creation. GLSL uses main. WGSL uses generated names, so callers must not assume that the source function name is the target entry point.
This diagram shows how authored Slang becomes a luma.gl pipeline input.
flowchart LR
A[Slang source] --> B[Application-owned transpiler]
B --> C{Rendering target}
C -->|WebGPU| D[WGSL module and entry points]
C -->|WebGL 2| E[GLSL ES 300 stages]
D --> F[luma.gl shader assembler]
E --> F
F --> G[Render or compute pipeline]
References: modules/…/index.ts• L42-54 modules/…/index.ts• L56-129 modules/…/ir-types.ts• L4-18
Reflection and diagnostics
Each compilation result carries reflection that describes the source interface after lowering. Luma.gl uses this information to connect source resource names to generated binding names. It also describes vertex attributes, resource visibility, and uniform buffer layouts.
The result carries a source map as well. When a destination shader compiler reports a line, the application can map the message back to Slang source or to a registered native module. Frontend errors use source names and line and column positions before generated code is returned.
References: modules/…/diagnostics.ts• L6-19 modules/…/index.ts• L14-15 modules/…/index.ts• L37-40 modules/…/ir.ts• L4-28
Luma rendering workflow
The Slang package does not install itself into luma.gl. An application registers a transpiler with a ShaderAssembler, selects sourceLanguage: 'slang', and lets luma.gl build the model or computation from the translated shader result.
Shader assembler registration
The application owns the bridge between Slang and @luma.gl/shadertools. It can configure imports, native contracts, and target policy before adding the transpiler to a GLSL or WGSL assembler. Models and computations then use the same high-level luma.gl entry points as other shader languages.
This separation keeps the Slang package optional. Applications that do not register it do not add Slang behavior to the core shader path. An application can also choose a different registry or transpiler policy for each rendering context.
References: examples/…/slang-transpiler.ts• L5-15 examples/…/slang-transpiler.ts• L25-57 examples/…/app.ts• L37-59 examples/…/app.ts• L63-83
Resource and uniform handoff
Reflection turns the source interface into data that the host can use to build bindings. The luma helper can produce a layout compatible with luma.gl pipeline metadata, translate source resource names to generated names, and expose the byte size and packing layout for uniform buffers.
The application still creates buffers, textures, samplers, and device features. It packs uniform values with the reflected layout, binds resources with the reflected names, and checks target requirements against the destination device. Slang describes the contract. It does not own the resources.
References: modules/…/resources.ts• L24-52 modules/…/IR.md• L17-19 modules/…/ir.ts• L22-28 modules/…/inspection-types.ts• L12-22 modules/…/luma.ts• L107-179 examples/…/app.ts• L61-70
Shared shader workflows
Slang supports more than isolated render stages. One source can hold shared declarations and helpers for a render pipeline. It can also describe compute work whose output feeds a later render pass.
Unified render stages
Unified WGSL compilation emits shared declarations once and keeps selected vertex, fragment, or compute entry points available by source name. This gives a render pipeline one coherent shader module. It also keeps shared resource and helper definitions aligned across stages.
When stages are compiled separately, the luma helpers combine compatible reflection by resource slot. Shared slots must describe the same resource. A pipeline must select at most one entry point for each stage.
References: modules/…/index.ts• L56-129 modules/…/ir.ts• L15-28 modules/…/resources.ts• L38-52 modules/…/luma.ts• L65-117
Compute-to-render pipelines
The particle example shows the package's compute role. One Slang source defines a compute simulation and the vertex and fragment stages that display its result. The application dispatches compute work, swaps its GPU buffers, and renders the new buffer without reading the data back to the CPU.
This pattern makes Slang a common authoring layer for a GPU data flow, not only a text conversion step. The same reflection-driven binding and uniform rules apply to both the compute and render pipelines.
References: modules/…/resources.ts• L8-22 examples/…/particles.slang• L4-67 examples/…/app.ts• L54-63 examples/…/app.ts• L101-162
Language tooling
The package also exposes source-level tools that help a host understand shader declarations without running the full target compiler. These tools support editor features, resource discovery, and checks that need logical types and interfaces.
Shader inspection
Shader inspection returns a language-neutral description of structures, globals, functions, entry points, interfaces, bindings, and address spaces. It skips function-body analysis where possible, so a host can inspect a shader contract without asking a device to compile it.
The inspection surface accepts WGSL and GLSL. GLSL inspection expects preprocessed source with only supported version and extension directives. The returned data keeps logical types separate from target byte offsets, which prevents inspection from being confused with uniform packing.
References: modules/…/inspect.ts• L3-14 modules/…/inspection-types.ts• L4-41 modules/…/glsl-inspect.ts• L43-75 modules/…/wgsl-inspect.ts• L14-21
WGSL-to-GLSL tooling
The WGSL path provides a bounded rendering subset that can be analyzed and lowered to GLSL without using the Slang frontend. It checks WGSL types, constants, interfaces, resources, and uniform layout before the shared emitter produces GLSL.
This path is a language tool rather than a replacement for WebGPU shader compilation. Its value is that host systems can use the same shader metadata and target conversion model when a workflow starts with WGSL and needs a GLSL target.
References: modules/…/wgsl.ts• L9-31 modules/…/wgsl-analysis.ts• L21-29 modules/…/wgsl-analysis.ts• L857-1007 modules/…/wgsl-layout.ts• L17-75
Integration boundaries
Slang handles language translation and reports compiler-known shader requirements. The application and the destination graphics API remain responsible for decisions that depend on the actual device, resource choices, or target-language validation.
Interface and resource contracts
Source semantics become explicit builtins, locations, interpolation modes, and sampling modes before target interface emission. Resource declarations become logical uniform, storage, texture, or sampler contracts with binding and access data.
These contracts are shared between code generation and reflection. A source interface must match the stage that uses it. A resource binding must remain consistent with the host pipeline and with any other stage that shares the binding.
References: modules/…/slang-interface.ts• L7-85 modules/…/resources.ts• L24-52 modules/…/ir.ts• L15-28
Device and target limits
The two targets do not provide the same capabilities. WebGL 2 uses GLSL ES 300 and has no compute or storage-texture path. WebGPU supports WGSL compute and storage features subject to device limits and optional feature requests. Reflection reports useful requirements, but it is not a complete device compatibility check.
Applications must validate generated code on the target device. They must also check limits, texture formats, sampler types, storage access, binding groups, and language features before pipeline creation. This matters when Slang source uses compute synchronization, storage resources, or target-specific texture behavior.
References: modules/…/emitter.ts• L300-319 modules/…/resources.ts• L8-30 modules/…/wgsl-analysis.ts• L1009-1049 modules/…/luma.ts• L205-253
Application ownership
The application owns compiler registration, module loading, resource creation, uniform packing, and device feature requests. This boundary lets luma.gl rendering workflows consume Slang results through normal shader assembler and pipeline interfaces. It keeps policy and lifetime decisions outside the package.
The examples make this ownership visible. The sculpture registers its compiler and shared modules. The particle example compiles selected entry points and creates the buffers used by simulation and rendering.
References: modules/…/modules.ts• L12-70 examples/…/app.ts• L43-70 examples/…/app.ts• L54-107
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to the shared shader metadata must preserve the contract between Slang analysis, WGSL analysis, target emission, reflection, and luma layout helpers.
- Changes to entry-point selection must preserve the assembler's stage-to-entry-point mapping for unified WGSL and separate GLSL stages.
- Changes to canonical operation names must keep Slang intrinsic and texture spellings aligned with both target emitters.
- Changes to interface lowering must preserve builtin stage rules, semantic locations, interpolation, and sampling metadata.
- Changes to uniform layout rules must remain compatible with host packing and with the WGSL-to-GLSL layout check.
- Changes to named module or native-contract rules must preserve source-map names, target-specific implementations, and signature matching.
- Changes to inspection types must keep logical declaration data separate from generated byte layout data.
- Changes to the package exports must keep the core compiler, inspection tools, and luma integration entry points available to their intended consumers.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The source uses syntax outside the supported Slang subset. | Transpilation stops with a source diagnostic. | Keep authoring within the supported subset and treat the package as an experimental compiler. |
| A caller uses the source function name instead of the returned WGSL entry point. | Pipeline creation cannot find the entry point. | Pass the entry point from the compilation result. |
| A source resource name is used after target lowering without name translation. | Luma.gl binds the wrong generated resource or fails to bind it. | Use reflection or the luma binding-name helper. |
| Host values are packed with handwritten offsets that do not match reflection. | Uniform values are read at the wrong locations. | Allocate and pack uniform buffers from the reflected layout. |
| A target uses a feature or storage format that the device does not support. | Shader or pipeline creation fails on that device. | Check reported requirements and destination-device limits before creation. |
| Separate stages describe different resources at the same binding slot. | Reflection merge rejects the pipeline as incompatible. | Keep shared binding slots consistent across stages. |
| A native module has no implementation for the selected target. | Module resolution fails before shader code is emitted. | Register target-specific native code for every target that the application selects. |
| WGSL uniform fields do not match the target layout rules. | The WGSL-to-GLSL path rejects the shader instead of silently repacking it. | Use explicitly compatible host-shareable fields and verify layout before translation. |
Key files
| File | Why to read it |
|---|---|
| modules/…/index.ts | Shows the public Slang compilation paths and unified WGSL program result. |
| modules/…/IR.md | Explains the shared metadata boundary between Slang, WGSL, and target emission. |
| modules/…/ir.ts | Defines the language-neutral shader types, interfaces, resources, and operations used by the backends. |
| modules/…/modules.ts | Shows how application-owned modules and native target contracts enter a program. |
| modules/…/slang-interface.ts | Shows how Slang semantics become stage interface metadata. |
| modules/…/resources.ts | Shows the logical resource kinds and access rules shared by reflection and emission. |
| modules/…/luma.ts | Shows how compiler reflection becomes luma-compatible layouts, binding names, uniform layouts, and device requirements. |
| modules/…/inspect.ts | Defines the public shader inspection surface for GLSL and WGSL. |
| modules/…/glsl-inspect.ts | Shows the declaration-level view available for GLSL source. |
| modules/…/wgsl-inspect.ts | Shows the declaration-level view available for WGSL source. |
| modules/…/wgsl.ts | Shows the independent WGSL-to-GLSL compilation path. |
| modules/…/wgsl-analysis.ts | Shows the checks that protect the shared backend from invalid WGSL contracts. |
| modules/…/wgsl-layout.ts | Shows why WGSL uniform layout must agree with the GLSL target layout. |
| examples/…/slang-transpiler.ts | Shows the application-owned bridge from Slang to shadertools. |
| examples/…/app.ts | Shows a shared Slang render source used by a luma.gl model with reflected uniform packing. |
examples/tutorials/slang-particles/particles.slang | Shows one Slang source defining compute, vertex, and fragment stages. |
| examples/…/app.ts | Shows how compute output moves directly into a luma.gl render workflow. |