Skip to main content

@luma.gl/shaders/types

Use this entry point to describe what a shader reads, calculate how its fields fit in a buffer, and inspect a pipeline's shader interface. The layout helpers run in JavaScript without a GPU device.

Shader values​

TypeValues it describes
AttributeShaderTypeScalar and vector vertex inputs, such as 'f32' and 'vec3<f32>'.
VariableShaderTypeScalars, vectors, and matrices declared in shader code.
CompositeShaderTypeA variable type, a struct keyed by field name, or a fixed-length array expressed as [elementType, length].

Use resolveVariableShaderTypeAlias('vec4f') to obtain the canonical spelling 'vec4<f32>'. Use getVariableShaderTypeInfo() or getAttributeShaderTypeInfo() to inspect a declared type.

Buffer layout​

makeShaderBlockLayout(fields, options?) calculates the packed size and offset of each leaf field. Choose 'std140' (the default), 'wgsl-uniform', or 'wgsl-storage' to match the declaration in the shader.

import {makeShaderBlockLayout} from '@luma.gl/shaders/types';

const layout = makeShaderBlockLayout(
{opacity: 'f32', tint: 'vec4<f32>'},
{layout: 'wgsl-uniform'}
);

const values = new Float32Array(layout.byteLength / 4);
values[layout.fields.opacity.offset] = 0.5;
values.set([1, 0.8, 0.6, 1], layout.fields.tint.offset);

Here opacity starts at word 0 and tint at word 4 because a four-component vector requires 16-byte alignment. The block occupies 32 bytes. Field offsets, sizes, and matrix column strides are measured in 32-bit words. The block's byteLength is measured in bytes.

Shader interfaces​

ShaderLayout lists a render pipeline's vertex attributes and resource bindings. Each AttributeDeclaration describes a named input and its location. A BindingDeclaration identifies a resource such as a uniform buffer, storage buffer, texture, or sampler. ComputeShaderLayout describes bindings for a compute pipeline.

Use scanWGSLInterface from @luma.gl/shaders/wgsl to inspect bindings in WGSL source, or the assembler to compose shader modules and plugins.