array
Function
An array groups a fixed number of values of one element type.
Compare examples
The listed elements fill the array in input order. With no arguments, the constructor fills each element with zero.
Example 1array(1., 2., 3.)
- Result
[1, 2, 3]
Example 2array<f32, 3>()
- Result
[0, 0, 0]
Example 3array(vec2(1., 0.), vec2(0., 1.))
- Result
[[1, 0], [0, 1]]
The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.
When to use it
Use it for storing a short kernel, lookup table, or set of samples.
Inputs and limits
The constructor count must match the fixed length. A zero constructor fills with the element type's zero, not an identity matrix or an arbitrary default.
For shader input rules and precision, consult WGSL and GLSL ES 3.00.
Related functions
The signature, the sentence and any message are the compiler's own text at commit af54ddad5.
Builds an array<T, N> from exactly N values of type T.
Syntax
declare function array<V extends readonly unknown[]>(...values: V): array<[(V[number] extends infer E ? (E extends number ? (typeof f32Tag extends keyof E ? E : typeof i32Tag extends keyof E ? E : typeof u32Tag extends keyof E ? E : typeof f64Tag extends keyof E ? E : never) : never) : never)] extends [never] ? V[number] : (V[number] extends infer E ? (E extends number ? (typeof f32Tag extends keyof E ? E : typeof i32Tag extends keyof E ? E : typeof u32Tag extends keyof E ? E : typeof f64Tag extends keyof E ? E : never) : never) : never), V['length']>declare function array<T, N extends number>(...values: readonly T[]): array<T, N>A number in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.
type f32 = number & { readonly [f32Tag]?: true }
A number you write as a type is inferred from its uses, and f32, i32 and u32 fix a width; the TypeScript constructs page carries that row with the compiler's emit.
Parameters
-
...valuesVreadonly T[]
Return value
array<[(V[number] extends infer E ? (E extends number ? (typeof f32Tag extends keyof E ? E : typeof i32Tag extends keyof E ? E : typeof u32Tag extends keyof E ? E : typeof f64Tag extends keyof E ? E : never) : never) : never)] extends [never] ? V[number] : (V[number] extends infer E ? (E extends number ? (typeof f32Tag extends keyof E ? E : typeof i32Tag extends keyof E ? E : typeof u32Tag extends keyof E ? E : typeof f64Tag extends keyof E ? E : never) : never) : never), V['length']>array<T, N>
Description
Without type arguments, array(v1, v2, ..., vN) reads T and N off the values, as WGSL does; array<T, N>(v1, ..., vN) names them, and array<T, N>() is the zero value, every element the zero of its type. An array has the methods map, forEach, some, every and reduce, each a counted loop over its elements that calls the function it is handed with the element, its i32 index and the array.
Examples
path/palette/palette.shade.tspath/double-array/double-array.shade.tspath/image-blur/image-blur.shade.tspath/sum-array/sum-array.shade.tspath/parallel-sum/parallel-sum.shade.tspath/histogram/histogram.shade.tsgallery/capsule-corp-namek-class/capsule-corp-namek-class.shade.tsgallery/path-tracer/path-tracer.shade.tsgallery/array-length/array-length.shade.tsgallery/array-literal-ramp/array-literal-ramp.shade.tsgallery/array-methods/array-methods.shade.tsgallery/atomic-histogram/atomic-histogram.shade.tsgallery/bare-position/bare-position.shade.tsgallery/bit-bump/bit-bump.shade.tsgallery/bool-select/bool-select.shade.tsgallery/clip-planes/clip-planes.shade.tsgallery/closures/closures.shade.tsgallery/compute-reduction-twin/compute-reduction-twin.shade.tsgallery/compute-sync/compute-sync.shade.tsgallery/cube-array-gather/cube-array-gather.shade.tsgallery/cube-env/cube-env.shade.tsgallery/default-args/default-args.shade.tsgallery/generic-class/generic-class.shade.tsgallery/generic-helpers/generic-helpers.shade.tsgallery/gpu-console/gpu-console.shade.tsgallery/higher-order/higher-order.shade.tsgallery/id-pick/id-pick.shade.tsgallery/integer-math/integer-math.shade.tsgallery/kernel-array-methods/kernel-array-methods.shade.tsgallery/kernel-push/kernel-push.shade.tsgallery/kernel-set-unique/kernel-set-unique.shade.tsgallery/loop-kernel/loop-kernel.shade.tsgallery/loop-on-cpu/loop-on-cpu.shade.tsgallery/loop-reduction/loop-reduction.shade.tsgallery/loop-struct-array/loop-struct-array.shade.tsgallery/loops-over-data/loops-over-data.shade.tsgallery/mixin-surface/mixin-surface.shade.tsgallery/msaa-resolve/msaa-resolve.shade.tsgallery/null-values/null-values.shade.tsgallery/optional-fields/optional-fields.shade.tsgallery/optional-values/optional-values.shade.tsgallery/orbit-inout/orbit-inout.shade.tsgallery/packed-bytes/packed-bytes.shade.tsgallery/packing-bitcast/packing-bitcast.shade.tsgallery/palette-const/palette-const.shade.tsgallery/particle-step/particle-step.shade.tsgallery/pick-composite/pick-composite.shade.tsgallery/ray-class/ray-class.shade.tsgallery/reference-parameters/reference-parameters.shade.tsgallery/shadow-compare/shadow-compare.shade.tsgallery/shape-inheritance/shape-inheritance.shade.tsgallery/subgroup-reduce/subgroup-reduce.shade.tsgallery/tuple-and-brand/tuple-and-brand.shade.tsgallery/uniform-array/uniform-array.shade.tsgallery/workgroup-override/workgroup-override.shade.tsgallery/workgroup-reduce/workgroup-reduce.shade.tsgallery/workgroup-scratch/workgroup-scratch.shade.tsgallery/workgroup-tile-2d/workgroup-tile-2d.shade.ts