select
On this page

select

Function

Selection chooses the false or true value from a condition, component by component when the condition is a vector.

Compare examples

A false condition selects the first value; a true condition selects the second. With a vector condition, each component can select a different value.

Example 1select(10, 20, false)
Result
10
Example 2select(10, 20, true)
Result
20
Example 3select(vec2(10, 10), vec2(20, 20), vec2b(true, false))
Result
vec2(20, 10)

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 choosing colours or values from a mask.

Inputs and limits

The condition is the last argument. Do not rely on selection to guard an invalid memory access or undefined expression.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

WGSL
select(a, b, c)
GLSL ES 3.00
(c ? b : a)

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Returns trueValue where cond is true and falseValue where it is false, in WGSL's argument order: the condition comes last.

Syntax

declare function select<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u | vec2b | vec3b | vec4b>(a0: V, a1: V, a2: (V extends { readonly [vecTag]: readonly [unknown, 2] } ? vec2b : V extends { readonly [vecTag]: readonly [unknown, 3] } ? vec3b : vec4b)): V
declare function select<V extends vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u | vec2b | vec3b | vec4b>(a0: V, a1: V, a2: bool): V
declare function select<P0 extends number | boolean, P1 extends number | boolean>(a0: P0, a1: P1, a2: bool): ScalarOf<P0, P1>
declare function select<T extends Numeric | bool | BoolVec | Vec64Any | f64>(
falseValue: T,
trueValue: T,
cond: bool | BoolVec,
): T

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.

A signature may name a type the ambient file declares for its own use. These are those declarations.

BoolVec
type BoolVec = vec2b | vec3b | vec4b
Vec64Any
type Vec64Any = vec2f64 | vec3f64 | vec4f64
ScalarOf
type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>
Numeric
type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u

Parameters

  • falseValue VP0T
  • trueValue VP1T
  • cond (V extends { readonly [vecTag]: readonly [unknown, 2] } ? vec2b : V extends { readonly [vecTag]: readonly [unknown, 3] } ? vec3b : vec4b)boolbool | BoolVec

Return value

VScalarOf<P0, P1>T

Description

A call: falseValue, trueValue and cond are all evaluated, in that order. Write cond ? trueValue : falseValue to evaluate one arm.

Targets

WGSL
select(a, b, c)
GLSL ES 3.00
(c ? b : a)

Examples

See also

Edit this page Report a problem