Intrinsics1 min read

ceil

Rounds a value up to the nearest integer.

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

Test ceil in a live shader

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

Open Playground

Live Demo

The ceil function returns the smallest integer that is greater than or equal to the input value. This rounds toward positive infinity.

Signature

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

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

Parameters

ParameterTypeDescription
xTThe input value

Return Value

Returns the smallest integer greater than or equal to x.

Example

bwsl
pipeline PixelArt {
fragment {
// Snap to pixel grid with ceiling for upward bias
float2 pixelSize = float2(1.0 / 320.0, 1.0 / 240.0);
float2 snappedUV = ceil(input.uv / pixelSize) * pixelSize;
float4 color = sample(spriteTex, snappedUV);
output.color = color;
}
}

Common Use Cases

Upward Rounding

bwsl
// Calculate minimum buffer size needed
int bufferSize = int(ceil(dataCount / float(groupSize))) * groupSize;

Tile Count Calculation

bwsl
// How many tiles needed to cover an area
int2 tilesNeeded = int2(ceil(areaSize / tileSize));

LOD Level Selection

bwsl
// Select LOD level rounding up (more conservative)
float lodLevel = ceil(log2(maxDimension));

Progress Bar Segments

bwsl
// Fill segments fully before moving to next
int filledSegments = int(ceil(progress * totalSegments));

Mipmap Size Calculation

bwsl
// Calculate mip level dimensions (round up)
int mipWidth = int(ceil(float(baseWidth) / pow(2.0, float(mipLevel))));

Negative Values

For negative numbers, ceil rounds toward positive infinity: ceil(-1.5) returns -1.0, not -2.0.

Compiled Output

When compiled to GLSL:

glsl
ceil(x)

When compiled to HLSL:

hlsl
ceil(x)

When compiled to Metal:

metal
ceil(x)

See Also