round
Rounds a value to the nearest integer.
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 PlaygroundLive 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
round :: (T x) -> T {...}
Where T can be float, float2, float3, or float4.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | The input value |
Return Value
Returns the integer nearest to x, using round-to-even for halfway cases.
Example
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
// Reduce color precision for retro effect
float3 posterized = round(color * levels) / levels;Snapping to Grid
// Snap position to grid
float3 snappedPos = round(worldPos / gridSize) * gridSize;Index Calculation
// Convert continuous value to nearest index
int index = int(round(normalizedValue * float(arraySize - 1)));Dithered Quantization Visualization
// Show quantization steps
float level = round(value * 10.0) / 10.0;
float error = abs(value - level);Animation Frame Selection
// 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:
roundEven(x)
When compiled to HLSL:
round(x)
When compiled to Metal:
rint(x)