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.
Of course. The select built-in function in WGSL is a powerful and efficient way to choose between two values based on a boolean condition. It is the WGSL equivalent of the ternary operator (condition ? true_value : false_value) found in languages like C++, Java, and JavaScript.
It's particularly important on GPUs because it can often be compiled into a single, non-branching instruction, avoiding the performance cost of thread divergence that can occur with if statements.
The function signature is:
select(false_value, true_value, condition) -> T
Parameters:
false_value: The value to return if condition is false.true_value: The value to return if condition is true.condition: A boolean (bool) or a vector of booleans (vecN<bool>).Rules:
false_value and true_value must be of the same type (e.g., both f32, both vec3<f32>, etc.). The return type of select will be this type.condition must be a boolean. If false_value and true_value are vectors, the condition can be either a single bool (which applies to all components) or a vector of bools of the same dimension.bool)This is the most straightforward case, identical to a ternary operator.
// If my_condition is true, result will be 1.0.
// If my_condition is false, result will be 0.0.
let my_condition: bool = /* ... some logic ... */;
let result: f32 = select(0.0, 1.0, my_condition);
You can use it with any type, including vectors:
let is_shiny: bool = true;
let color = select(
vec3<f32>(0.2, 0.2, 0.2), // non-shiny color (false_value)
vec3<f32>(1.0, 0.8, 0.2), // shiny gold color (true_value)
is_shiny
);
// color will be vec3<f32>(1.0, 0.8, 0.2)
This is where select becomes particularly powerful for graphics programming. When you provide a vector of booleans as the condition, the selection happens per-component.
let a = vec4<f32>(1.0, 2.0, 3.0, 4.0);
let b = vec4<f32>(10.0, 20.0, 30.0, 40.0);
// Condition: a component is selected if the corresponding
// value in 'a' is less than 2.5
let cond = vec4<bool>(
a.x < 2.5, // true
a.y < 2.5, // true
a.z < 2.5, // false
a.w < 2.5 // false
);
// cond is vec4<bool>(true, true, false, false)
// For each component:
// If cond is true, pick from 'b'.
// If cond is false, pick from 'a'.
let result = select(a, b, cond);
// result will be vec4<f32>(10.0, 20.0, 3.0, 4.0)
// ^ ^ ^ ^
// from b (true) ----------|-----| | |
// from a (false) ---------------------|----|
select vs. if StatementThis is a critical distinction for GPU performance.
| Feature | select(f, t, c) | if (c) { ... } else { ... } |
|---|---|---|
| Purpose | Value Selection: Chooses one of two pre-existing values. | Control Flow: Executes one of two different blocks of code. |
| Evaluation | Both true_value and false_value are always evaluated. The hardware just picks one. | Only one block is executed. |
| Performance | Avoids thread divergence. All threads in a warp/subgroup execute the same path. Faster for simple value choices. | Can cause thread divergence. If threads in a warp take different if/else paths, the GPU must run both paths sequentially, with inactive threads masked off. This can be slow. |
| Use Case | Ideal for simple, ternary-like operations, especially component-wise logic on vectors. | Necessary for complex logic, early return or discard, or when one branch is computationally expensive and should be skipped. |
| Example | let x = select(0.0, 1.0, a > b); | if (a > b) { return; } else { do_complex_work(); } |
What is Thread Divergence?
GPUs execute threads in groups (e.g., "warps" of 32 threads on NVIDIA). For maximum efficiency, all threads in a group should execute the same instruction at the same time. If an if statement causes some threads to go down the if path and others to go down the else path, the group "diverges." The GPU has to execute the if block for the first set of threads (while the others wait), and then execute the else block for the second set (while the first set waits). This effectively serializes the work and reduces performance.
select avoids this by having all threads compute both potential outcomes and then just picking the correct one based on the condition. This keeps all threads executing in lock-step.
select for simple, A ? B : C style choices.select(false_value, true_value, condition).if statement for value selection because it prevents thread divergence.if when you need true control flow (e.g., early discard or return) or to avoid executing a very expensive block of code.