Intrinsics1 min read

pow

Raises a value to a power.

Reading Time
1 min
Word Count
142
Sections
12
Try It Live

Test pow in a live shader

Open the playground, start from a visual preset, and wire pow into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo

The pow function returns x raised to the power y, computed as x^y.

Signature

bwsl
pow :: (T x, T y) -> T {...}

Where T can be float, float2, float3, or float4.

Parameters

ParameterTypeDescription
xTThe base value
yTThe exponent

Return Value

Returns x raised to the power y.

Example

bwsl
pipeline SpecularLighting {
fragment {
float3 normal = normalize(input.normal);
float3 viewDir = normalize(cameraPos - input.worldPos);
float3 lightDir = normalize(lightPos - input.worldPos);
// Blinn-Phong specular
float3 halfDir = normalize(lightDir + viewDir);
float NdotH = max(dot(normal, halfDir), 0.0);
float specular = pow(NdotH, shininess);
float3 color = diffuseColor + specularColor * specular;
output.color = float4(color, 1.0);
}
}

Common Use Cases

Specular Highlights

bwsl
// Phong specular exponent
float specular = pow(max(dot(reflect(-lightDir, normal), viewDir), 0.0), shininess);

Gamma Correction

bwsl
// Linear to sRGB approximation
float3 srgb = pow(linearColor, float3(1.0 / 2.2));
// sRGB to linear
float3 linear = pow(srgbColor, float3(2.2));

Fresnel Effect

bwsl
// Schlick's approximation
float fresnel = pow(1.0 - saturate(dot(normal, viewDir)), 5.0);

Exponential Falloff

bwsl
// Smooth falloff with adjustable curve
float falloff = pow(1.0 - dist / maxDist, falloffExponent);

Contrast Adjustment

bwsl
// Simple contrast curve
float contrasted = pow(color, float3(contrast));

Undefined Behavior

pow(x, y) is undefined when x < 0 or when x = 0 and y <= 0. Always ensure the base is non-negative when the exponent could be fractional.

Special Cases

For integer exponents, direct multiplication may be faster than pow. For pow(x, 2.0), use x * x. For pow(x, 0.5), use sqrt(x).

Compiled Output

When compiled to GLSL:

glsl
pow(x, y)

When compiled to HLSL:

hlsl
pow(x, y)

See Also

  • sqrt - Square root (pow x 0.5)
  • exp - e^x
  • exp2 - 2^x
  • log - Natural logarithm