COMMON FUNCTIONS

Absolute value

float abs(float x)  
float2 abs(float2 x)  
float3 abs(float3 x)  
float4 abs(float4 x)

The function returns the absolute value of x, i.e. x when x is positive or zero and -x for negative x. The input parameter can be a float scalar or a float vector. In case of a float vector the operation is done component-wise.

Sign

float sign(float x)  
float2 sign(float2 x)  
float3 sign(float3 x)  
float4 sign(float4 x)

The function returns 1.0 when x is positive, 0.0 when x is zero and -1.0 when x is negative. The input parameter can be a float scalar or a float vector. In case of a float vector the operation is done component-wise.

Floor

float floor(float x)  
float2 floor(float2 x)  
float3 floor(float3 x)  
float4 floor(float4 x)

The function returns the largest integer number that is smaller or equal to x. The input parameter can be a float scalar or a float vector. In case of a float vector the operation is done component-wise.

The return value is of type float scalar or float vector although the result of the operation is an integer.

Ceiling

float ceil(float x)  
float2 ceil(float2 x)  
float3 ceil(float3 x)  
float4 ceil(float4 x)

The function returns the smallest number that is larger or equal to x. The input parameter can be a float scalar or a float vector. In case of a float vector the operation is done component-wise.

The return value is of type float scalar or float vector although the result of the operation is an integer.

Truncate

float trunc(float x)  
float2 trunc(float2 x)  
float3 trunc(float3 x)  
float4 trunc(float4 x)

The function returns the nearest integer value of x (i.e. x rounded to zero). The input parameter can be a float scalar or a float vector. In case of a float vector the operation is done component-wise.

The return value is of type float scalar or float vector although the result of the operation is an integer.

Fractional part

float fract(float x)  
float2 fract(float2 x)  
float3 fract(float3 x)  
float4 fract(float4 x)

The function returns the fractional part of x, i.e. x minus floor(x). The input parameter can be a float scalar or a float vector. In case of a float vector the operation is done component-wise.

Modulo

float fmod(float x, float y)  
float2 fmod(float2 x, float2 y)  
float3 fmod(float3 x, float3 y)  
float4 fmod(float4 x, float4 y)

The function returns x minus the product of y and trunc(x/y). The input parameters can be float scalars or float vectors. In case of float vectors the operation is done component-wise.

If x and y are integers the return value is the remainder of the division of x by y as expected.

Minimum

float min(float x, float y)  
float2 min(float2 x, float2 y)  
float3 min(float3 x, float3 y)  
float4 min(float4 x, float4 y)

The function returns the smaller of the two arguments. The input parameters can be float scalars or float vectors. In case of float vectors the operation is done component-wise.

Maximum

float max(float x, float y)  
float2 max(float2 x, float2 y)  
float3 max(float3 x, float3 y)  
float4 max(float4 x, float4 y)

The function returns the larger of the two arguments. The input parameters can be float scalars or float vectors. In case of float vectors the operation is done component-wise.

Clamp

float clamp(float x, float min, float max)  
float2 clamp(float2 x, float2 min, float2 max)  
float3 clamp(float3 x, float3 min, float3 max)  
float4 clamp(float4 x, float4 min, float4 max)

The function returns x if it is larger than or equal to min and smaller than or equal to max. In case x is smaller than min, min is returned. If x is larger than max, max is returned. The input parameters can be float scalars or float vectors. In case of float vectors the operation is done component-wise.

Saturate

float saturate(float x)  
float2 saturate(float2)  
float3 saturate(float3)  
float4 saturate(float4)

The function returns x clamped on values between 0.0 and 1.0.

See the clamp function for more information.

Mix

float mix(float x, float y, float a)  
float2 mix(float2 x, float2 y, float2 a)  
float3 mix(float3 x, float3 y, float3 a)  
float4 mix(float4 x, float4 y, float4 a)

The function returns the linear blend of x and y, i.e. the product of x and (1 - a) plus the product of y and a. The input parameters can be float scalars or float vectors. In case of float vectors the operation is done component-wise.

Step

float step(float edge, float x)  
float2 step(float2 edge, float2 x)  
float3 step(float3 edge, float3 x)  
float4 step(float4 edge, float4 x)

The function returns 0.0 if x is smaller than edge and otherwise 1.0. The input parameters can be float scalars or float vectors. In case of float vectors the operation is done component-wise.

Smoothstep

float smoothstep(float edge0, float edge1, float x)  
float2 smoothstep(float2 edge0, float2 edge1, float2 x)  
float3 smoothstep(float3 edge0, float3 edge1, float3 x)  
float4 smoothstep(float4 edge0, float4 edge1, float4 x)

The function returns 0.0 if x is smaller than or equal to edge0 and 1.0 if x is larger than or equal to edge1. Otherwise the return value is interpolated between 0.0 and 1.0 using Hermite polynomials. The input parameters can be float scalars or float vectors. In case of float vectors the operation is done component-wise.