Intrinsics1 min read

min

Returns the minimum of two or more values.

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

Test min in a live shader

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

Open Playground

Live Demo

The min function returns the smallest of the input values. It supports both two-argument and variadic forms, accepting any number of arguments.

Signature

bwsl
min :: (T a, T b) -> T {...}
min :: (T a, T b, T c, ...) -> T {...}

Where T can be float, float2, float3, float4, int, int2, int3, int4, uint, uint2, uint3, or uint4.

Parameters

ParameterTypeDescription
a, b, ...TValues to compare

Return Value

Returns the minimum value among all inputs.

Example

bwsl
pipeline ShadowBlending {
fragment {
// Sample multiple shadow maps and take the darkest
float shadow1 = sample(shadowMap1, input.shadowUV1).r;
float shadow2 = sample(shadowMap2, input.shadowUV2).r;
float shadow3 = sample(shadowMap3, input.shadowUV3).r;
// Minimum shadow = darkest shadow wins
float combinedShadow = min(shadow1, shadow2, shadow3);
float3 lit = baseColor * (ambient + combinedShadow * diffuse);
output.color = float4(lit, 1.0);
}
}

Common Use Cases

Clamping Maximum

bwsl
// Cap a value at maximum threshold
float capped = min(value, maxThreshold);

Component-wise Minimum

bwsl
// Find darkest color channel
float darkest = min(color.r, min(color.g, color.b));
// Or with variadic form:
float darkest = min(color.r, color.g, color.b);

Distance Field Operations

bwsl
// Union of two shapes (SDF)
float shape = min(sphereDist, boxDist);

Bounds Limiting

bwsl
// Limit light attenuation
float attenuation = 1.0 / (distance * distance);
attenuation = min(attenuation, 1.0); // Cap at 1

Safe Division

bwsl
// Prevent division by zero while keeping values bounded
float safeDiv = value / max(divisor, 0.001);
float result = min(safeDiv, maxResult);

Variadic Support

BWSL's min function accepts 2 to 255 arguments. When more than 2 arguments are provided, they are combined pairwise during compilation.

Vector Operations

When used with vectors, min operates component-wise. Use with a scalar to clamp all components: min(color, float3(1.0)).

Compiled Output

When compiled to GLSL:

glsl
min(a, b)

When compiled to HLSL:

hlsl
min(a, b)

See Also

  • max - Maximum of values
  • clamp - Clamp to range