wave_product
Computes the product of a value across all active lanes in a wave.
Test wave_product in a live shader
Open the playground, start from a visual preset, and wire wave_product into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo Pending
The wave_product function calculates the product of a value across all active lanes in a wave/subgroup.
Signature
wave_product :: (T x) -> T {...}
Where T can be float, float2, float3, or float4.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | Value to multiply across lanes |
Return Value
Returns the product of x from all active lanes in the wave.
Example
pipeline CumulativeGain {
pass "Main" {
compute "Main" [64, 1, 1] {
uint localIdx = input.local_index;
// Each lane has a multiplier
float multiplier = getLaneMultiplier(localIdx);
// Compute product across wave
float totalMultiplier = wave_product(multiplier);
// All lanes now have the combined multiplier
float result = baseValue * totalMultiplier;
float _ = result;
}
}
}Common Use Cases
Cumulative Probability
// Calculate joint probability across samples
float probability = computeProbability(sample);
float jointProb = wave_product(probability);Chained Transformations
// Combine scale factors
float scale = getScaleFactor(input.local_index);
float combinedScale = wave_product(scale);Ray Transmission
// Calculate combined transmission through materials
float transmission = 1.0 - absorption;
float totalTransmission = wave_product(transmission);Boolean Combination
// Check if all lanes pass condition (product of 1s = 1)
float pass = condition ? 1.0 : 0.0;
float allPass = wave_product(pass); // 0.0 if any failNumerical Stability
Products of many values can quickly overflow (if > 1) or underflow (if < 1). Consider using wave_sum(log(x)) and exp(result) for better numerical stability with many factors.
Alternative for AND
For boolean AND across wave, wave_all(condition) is clearer and more efficient than product-based approaches.
Compiled Output
When compiled to GLSL:
// Requires GL_KHR_shader_subgroup_arithmetic
subgroupMul(x)
When compiled to HLSL:
WaveActiveProduct(x)
When compiled to SPIR-V:
Uses OpGroupNonUniformFMul with Reduce operation.