Intrinsics1 min read

round

Rounds a value to the nearest integer.

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

Test round in a live shader

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

Open Playground

Live Demo

The round function returns the integer nearest to the input value. For values exactly halfway between integers, it rounds to the nearest even integer (banker's rounding).

Signature

bwsl
round :: (T x) -> T {...}

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

Parameters

ParameterTypeDescription
xTThe input value

Return Value

Returns the integer nearest to x, using round-to-even for halfway cases.

Example

bwsl
pipeline QuantizedColors {
fragment {
float4 texColor = sample(inputTex, input.uv);
// Quantize to 16 levels per channel
float3 quantized = round(texColor.rgb * 15.0) / 15.0;
output.color = float4(quantized, 1.0);
}
}

Common Use Cases

Color Quantization

bwsl
// Reduce color precision for retro effect
float3 posterized = round(color * levels) / levels;

Snapping to Grid

bwsl
// Snap position to grid
float3 snappedPos = round(worldPos / gridSize) * gridSize;

Index Calculation

bwsl
// Convert continuous value to nearest index
int index = int(round(normalizedValue * float(arraySize - 1)));

Dithered Quantization Visualization

bwsl
// Show quantization steps
float level = round(value * 10.0) / 10.0;
float error = abs(value - level);

Animation Frame Selection

bwsl
// Round to nearest frame
int frame = int(round(animTime * fps));

Round to Even

BWSL uses "round to even" (banker's rounding) for halfway cases. This means round(0.5) returns 0.0, round(1.5) returns 2.0, round(2.5) returns 2.0. This reduces cumulative rounding bias.

Alternative Rounding

If you need traditional rounding where 0.5 always rounds up, use floor(x + 0.5) instead.

Compiled Output

When compiled to GLSL:

glsl
roundEven(x)

When compiled to HLSL:

hlsl
round(x)

When compiled to Metal:

metal
rint(x)

See Also