wgsl select
- User
wgsl select
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
wgsl select
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The select function in WGSL (WebGPU Shading Language) is a built-in function used for component-wise conditional selection between two values. It has two primary overloads:
Selects between components of two vectors (or scalars) based on a boolean mask.
Signature:
select(f: T, t: T, cond: C) -> T
f: Value(s) returned where cond is false.t: Value(s) returned where cond is true.cond: Condition mask (bool for scalars, vecN<bool> for vectors).T must be f32, i32, u32, bool, or vectors thereof. C must match the dimension of T.Example:
let a = vec3<f32>(1.0, 2.0, 3.0);
let b = vec3<f32>(4.0, 5.0, 6.0);
let cond = vec3<bool>(true, false, true);
let result = select(a, b, cond); // Returns vec3<f32>(4.0, 2.0, 6.0)
Uses non-zero integers as a condition mask (only for integer types).
Signature:
select(f: T, t: T, cond: T) -> T
f: Value(s) returned where cond is 0.t: Value(s) returned where cond is non-zero.cond: Integer mask (scalar/vector of i32/u32).T must be i32, u32, or their vectors.Example:
let x = 10u;
let y = 20u;
let mask = 42u; // Non-zero → selects 'y'
let result = select(x, y, mask); // Returns 20u
f/t must be numeric or boolean (scalar/vector).let red = vec4<f32>(1.0, 0.0, 0.0, 1.0);
let blue = vec4<f32>(0.0, 0.0, 1.0, 1.0);
let use_red = true; // Boolean condition
let color = select(blue, red, use_red); // Returns 'red'
// Scalar equivalent of select(a, b, cond):
let result = cond ? b : a;
Use select for efficient, branchless operations in performance-critical shader code! 🚀