# UUSL Common Intrinsic Functions

This documentation article contains information functions of the UUSL. This information can be used as the reference document for writing shaders.

To start using common UUSL functions, include the core/shaders/common/common.h file.

UUSL
``#include <core/shaders/common/common.h>``

## Common Intrinsic Functions

### Double Functions

These functions take one or more arguments that are double expressions and return a double value.

Notice
It is not recommended to use these functions, unless there is no other option. Operations with doubles are significantly more time consuming (e.g. addition and multiplication operations are 8 times slower).

## doubledabs(double x)

Returns the absolute value of a given value.

### Arguments

• double x - Value.

### Return value

Absolute value of x.

## doubleacos_double(double x)

Returns the arccosine of a given value.

### Arguments

• double x - Value.

Arccosine of x.

## doubleddot(double3 vector1, double3 vector2)

Returns the dot product of two given vectors: ddot(vector1, vector2).

### Arguments

• double3 vector1 - First vector of 3 double components.
• double3 vector2 - Second vector of 3 double components.

### Return value

Calculated dot product.

## doubledlength(double2 v)

Returns the length of a given vector.

### Arguments

• double2 v - Vector of 2 double components.

Length of v.

## doubledlength(double3 v)

Returns the length of a given vector.

### Arguments

• double3 v - Vector of 3 double components.

Length of v.

## doubledlength2(double2 v)

Returns the squared length of a given vector.

### Arguments

• double2 v - Vector of 2 double components.

### Return value

Squared length of v.

## doubledlength2(double3 v)

Returns the squared length of a given vector.

### Arguments

• double3 v - Vector of 3 double components.

### Return value

Squared length of v.

## doubledilength(double2 v)

Returns the inverse length of a given vector.

### Arguments

• double2 v - Vector of 2 double components.

### Return value

Inverse length of v.

## doubledilength(double3 v)

Returns the inverse length of a given vector.

### Arguments

• double3 v - Vector of 3 double components.

### Return value

Inverse length of v.

## doubledmad(double x, double y, double z)

Returns the value of (x * y + z) for given values of x, y, and z.

### Arguments

• double x - x value.
• double y - y value.
• double z - z value.

### Return value

The value of (x * y + z).

## double2dnormalize(double2 v)

Normalizes a given vector.

### Arguments

• double2 v - Vector of 2 double components.

### Return value

Normalized vector.

## double3dnormalize(double3 v)

Normalizes a given vector.

### Arguments

• double3 v - Vector of 3 double components.

### Return value

Normalized vector.

## doubledrcp(double x)

Returns the reciprocal of a given value.

### Arguments

• double x - Value.

### Return value

Square root of x.

## doubledrsqrt(double x)

Returns the reciprocal of the square root of a given value.

### Arguments

• double x - Value.

### Return value

Reciprocal of the square root of x.

## doubledsign(double x)

Returns the sign of a given value.

### Arguments

• double x - Value.

### Return value

-1 if x is less than zero; 0 if x equals zero; and 1 if x is greater than zero.

## doubledsqrt(double x)

Returns the square root of a given value.

### Arguments

• double x - Value.

### Return value

Square root of x.

## overlay(value A, value B, value BLEND)

Performs overlay A over B with blending coefficient.

Equivalents

OpenGL
``saturate(A * lerp(float4_one * 0.5f,B,BLEND) * 2);``
Direct3D
``saturate(A * lerp(float4_one * 0.5f,B,BLEND) * 2);``

### Arguments

• value A - First value
• value B - Value for overlay.
• value BLEND - Blending coefficient.

## fmod(value X, value Y)

Returns the floating-point remainder of x/y.

Equivalents

OpenGL
``mod(X,Y);``
Direct3D
``fmod(X,Y);``

### Arguments

• value X - The dividend value.
• value Y - The divisor value.

## frac(value value)

Returns the fractional (or decimal) part of x; which is greater than or equal to 0 and less than 1.

Equivalents

OpenGL
``fract(value);``
Direct3D
``frac(value);``

### Arguments

• value value - The specified value.

## lerp(value X, value Y, value value)

Performs linear interpolation.

Equivalents

OpenGL
``mix(X,Y,value);``
Direct3D
``lerp(X,Y,value);``

### Arguments

• value X - The first value.
• value Y - The second value.
• value value - A value that linearly interpolates between the x parameter and the y parameter.

## float4lerp3(float4 v0, float4 v1, float4 v2, float a)

Performs linear interpolation between three vectors.

Equivalents

OpenGL
``````if (a < 0.5)
{
mix(v0, v1, a * 2.0f);
}
else
{
mix(v1, v2, a * 2.0f - 1.0f);
}``````
Direct3D
``````if (a < 0.5)
{
lerp(v0, v1, a * 2.0f);
}
else
{
lerp(v1, v2, a * 2.0f - 1.0f);
}``````

### Arguments

• float4 v0 - First vector.
• float4 v1 - Second vector.
• float4 v2 - Third vector.
• float a - Interpolation factor in the range [0.0f, 1.0f].

### Return value

Interpolated vector.

## valuelerpOne(value value, float factor)

Performs such interpolation:

Equivalents

OpenGL
``value * (1.0f - factor) + factor;``
Direct3D
``value * (1.0f - factor) + factor;``

### Arguments

• value value - Value for interpolation. Can be one of the following types:
• float
• float2
• float3
• float4
• float factor - Interpolation factor.

### Return value

Interpolated value. Can be one of the following types:
• float
• float2
• float3
• float4

## floatlength2(float3 vector)

Returns dot product of the vector: dot(vector, vector).

### Arguments

• float3 vector - Vector.

### Return value

Calculated dot product.

## floatsmoothClamp(float value, float x, float y)

Clamps a given value in the range [x; (x + y)] with a smooth Hermite interpolation between 0.0f and 1.0f.

Equivalents

OpenGL
``smoothstep(x, x + y, value);``
Direct3D
``smoothstep(x, x + y, value);``

### Arguments

• float value - Value to be clamped.
• float x - Lower bound of the clamping range.
• float y - Clamping range size.

### Return value

Clamped value with smoothing.

Returns a gradient value for a given value using four key components.

Equivalents

OpenGL
``smoothstep(gradient.x, gradient.y, a) - smoothstep(gradient.z, gradient.w, a);``
Direct3D
``smoothstep(gradient.x, gradient.y, a) - smoothstep(gradient.z, gradient.w, a);``

### Arguments

• float a - Value to find the gradient value for.
• float4 gradient - Vector with four key components.

## floatcalculate_dither_pattern(float2 uv)

Returns a dither value for a given UV value. Used for better gradients.

### Arguments

• float2 uv - UV value.

Dither value.

## floatgetBasisX(float3x3 matrix)

Returns x-basis of the given 3x3 matrix.

Equivalents

OpenGL
``m[0];``
Direct3D
``m._m00_m10_m20;``

### Arguments

• float3x3 matrix - 3x3 matrix.

Matrix basis.

## floatgetBasisX(float4x4 matrix)

Returns x-basis of the given 4x4 matrix.

Equivalents

OpenGL
``m[0].xyz;``
Direct3D
``m._m00_m10_m20;``

### Arguments

• float4x4 matrix - 3x3 matrix.

Matrix basis.

## floatgetBasisY(float3x3 matrix)

Returns y-basis of the given 3x3 matrix.

Equivalents

OpenGL
``m[1];``
Direct3D
``m._m01_m11_m21;``

### Arguments

• float3x3 matrix - 3x3 matrix.

Matrix basis.

## floatgetBasisY(float4x4 matrix)

Returns y-basis of the given 4x4 matrix.

Equivalents

OpenGL
``m[1].xyz;``
Direct3D
``m._m01_m11_m21;``

### Arguments

• float4x4 matrix - 3x3 matrix.

Matrix basis.

## floatgetBasisZ(float3x3 matrix)

Returns z-basis of the given 3x3 matrix.

Equivalents

OpenGL
``m[2];``
Direct3D
``m._m02_m12_m22;``

### Arguments

• float3x3 matrix - 3x3 matrix.

Matrix basis.

## floatgetBasisZ(float4x4 matrix)

Returns z-basis of the given 4x4 matrix.

Equivalents

OpenGL
``m[2].xyz;``
Direct3D
``m._m02_m12_m22;``

### Arguments

• float4x4 matrix - 3x3 matrix.

Matrix basis.

## rsqrt(value value)

Returns the reciprocal of the square root of the specified value.

Equivalents

OpenGL
``inversesqrt(value)``
Direct3D
``rsqrt(value)``

### Arguments

• value value - The specified value.

## saturate(value value)

Clamps the specified value within the range of 0 to 1.

Equivalents

OpenGL
``clamp(value,0.0f,1.0f)``
Direct3D
``saturate(value)``

### Arguments

• value value - The specified value.

## ddx(value value)

Returns the partial derivative of the specified value with respect to the screen-space x-coordinate.

Equivalents

OpenGL
``dFdx(value)``
Direct3D
``ddx(value)``

### Arguments

• value value - The specified value.

## ddy(value value)

Returns the partial derivative of the specified value with respect to the screen-space y-coordinate.

Equivalents

OpenGL
``dFdy(value)``
Direct3D
``ddy(value)``

### Arguments

• value value - The specified value.

## rcp(value value)

Calculates a fast, approximate, per-component reciprocal.

Equivalents

OpenGL
``(1.0f / (value))``
Direct3D
``rcp(value)``

### Arguments

• value value - The specified value.

## equal(value X, value Y)

Performs a component-wise equal-to comparison of two vectors.

Equivalents

OpenGL
``equal(X,Y)``
Direct3D
``(X == Y)``

### Arguments

• value X - The first specified value.
• value Y - The second specified value.

## greaterThan(value X, value Y)

Performs a component-wise greater-than comparison of two vectors.

Equivalents

OpenGL
``greaterThan(X,Y)``
Direct3D
``(X > Y)``

### Arguments

• value X - The first specified value.
• value Y - The second specified value.

## atan2(value X, value Y)

Returns the arctangent of two values (x,y).

Equivalents

OpenGL
``atan(X,Y)``
Direct3D
``atan2(X,Y)``

### Arguments

• value X - The first specified value.
• value Y - The second specified value.

## any(value value)

Determines if any components of the specified value are non-zero.

Equivalents

OpenGL
``(value)``
Direct3D
``any(value)``

### Arguments

• value value - The specified value.

## floatmax2(value value)

Selects the greater of the first two vector components.

Equivalents

OpenGL
``max(value.r,value.g)``
Direct3D
``max(value.r,value.g)``

### Arguments

• value value - The specified vector (can be float2, float3 or float4 vector).

### Return value

The maximum value.

## floatmax3(value value)

Selects the greater of the first three vector components.

Equivalents

OpenGL
``max(max(value.r,value.g),value.b)``
Direct3D
``max(max(value.r,value.g),value.b)``

### Arguments

• value value - The specified vector (can be float3 or float4).

### Return value

The maximum value.

## floatmax4(float4 value)

Selects the greater of the four vector components.

Equivalents

OpenGL
``max(max(max(value.r,value.g),value.b),value.a)``
Direct3D
``max(max(max(value.r,value.g),value.b),value.a)``

### Arguments

• float4 value - The specified vector.

### Return value

The maximum value.

## floatmin2(value value)

Selects the lesser of the first two vector components.

Equivalents

OpenGL
``min(value.r,value.g)``
Direct3D
``min(value.r,value.g)``

### Arguments

• value value - The specified vector (can be float2, float3 or float4 vector).

### Return value

The minimum value.

## floatmin3(value value)

Selects the lesser of the first three vector components.

Equivalents

OpenGL
``min(min(value.r,value.g),value.b)``
Direct3D
``min(min(value.r,value.g),value.b)``

### Arguments

• value value - The specified vector (can be float3 or float4 vector).

### Return value

The minimum value.

## floatmin4(float4 value)

Selects the lesser of the four vector components.

Equivalents

OpenGL
``min(min(min(value.r,value.g),value.b),value.a)``
Direct3D
``min(min(min(value.r,value.g),value.b),value.a)``

### Arguments

• float4 value - The specified vector.

### Return value

The minimum value.

## valuepow2(value value)

Returns squared value.

Equivalents

OpenGL
``value * value``
Direct3D
``value * value``

### Arguments

• value value - The specified value to be powered. Can be one of the following types:
• float
• float2
• float3
• float4

### Return value

Squared value (can be float, float2, float3 or float4).

## valuepowMirror(value value, value power)

Perform the following operation:

Implementation

UUSL
``1.0f - pow(1.0f - value,power);``

### Arguments

• value value - The specified value to be powered. Can be one of the following types:
• float
• float2
• float3
• float4
• value power - Power. Can be one of the following types:
• float
• float2
• float3
• float4

### Return value

Mirrored powered value (can be float, float2, float3 or float4).

## float3srgb(float3 color)

Converts RGB color to sRGB.

### Arguments

• float3 color - RGB color to convert.

sRBG color.

## floatsrgbInv(float value)

Performs the following operation:

Implementation

UUSL
``pow(value,float_isrgb);``

### Arguments

• float value - Value to convert.

### Return value

Inversed sRGB value.

## float2srgbInv(float2 value)

Performs the following operation:

Implementation

UUSL
``pow(value,float2_isrgb);``

### Arguments

• float2 value - Value to convert.

### Return value

Inversed sRGB value.

## float3srgbInv(float3 value)

Performs the following operation:

Implementation

UUSL
``pow(value,float3_isrgb);``

### Arguments

• float3 value - Value to convert.

### Return value

Inversed sRGB value.

## float4srgbInv(float4 value)

Performs the following operation:

Implementation

UUSL
``pow(value,float4_isrgb);``

### Arguments

• float4 value - Value to convert.

### Return value

Inversed sRGB value.

## floatnrand(float2 seed)

Returns the random value within the range of [0;1].

Equivalents

OpenGL
``frac(sin(dot(seed,float2(12.9898f,78.233f))) * 43758.5453f)``
Direct3D
``frac(sin(dot(seed,float2(12.9898f,78.233f))) * 43758.5453f)``

### Arguments

• float2 seed - The random seed.

## float2nrand(float2 seed_0, float2 seed_1)

Returns the float2 vector with random values within the range of [0;1].

### Arguments

• float2 seed_0 - The first random seed.
• float2 seed_1 - The second random seed.

## float2nrand2(float2 seed)

Returns the random value within the range of [0;1] (For the second seed, the function shifts the vector: float2(x,y) -> float2(y,x))

### Arguments

• float2 seed - The random seed.

## floatnrandTiled(float2 seed_0, flaot tiled)

Returns the random value within the range of [0;1] divided by the tiled seed.

### Arguments

• float2 seed_0 - The first random seed.
• flaot tiled - The seed of tiling (the number should be the power of two).

## float2nrandTiled(float2 seed_0, float2 seed_1, float tiled)

Returns the float2 vector with random values within the range of [0;1] divided by the tiled seed.

### Arguments

• float2 seed_0 - The first random seed.
• float2 seed_1 - The second random seed.
• float tiled - The seed of tiling (the number should be the power of two).

## float2nrand2Tiled(float2 seed, float tiled)

Returns the float2 vector with random values within the range of [0;1] divided by the tiled seed (For the second seed, the function shifts the vector: float2(x,y) -> float2(y,x))

### Arguments

• float2 seed - The random seed.
• float tiled - The seed of tiling (the number should be the power of two).

## floatnrandTemporal(float2 seed_0, flaot tiled)

Returns the value (that changed each frame) within the range of [0;1] divided by the tiled seed.

### Arguments

• float2 seed_0 - The first random seed.
• flaot tiled - The seed of tiling (the number should be the power of two).

## float2nrandTemporal(float2 seed_0, float2 seed_1, float tiled)

Returns the float2 vector with random values (that changed each frame) within the range of [0;1] divided by the tiled seed.

### Arguments

• float2 seed_0 - The first random seed.
• float2 seed_1 - The second random seed.
• float tiled - The seed of tiling (the number should be the power of two).

## float2nrand2Temporal(float2 seed, float tiled)

Returns the float2 vector with random values (that changed each frame) within the range of [0;1] divided by the tiled seed (For the second seed, the function shifts the vector: float2(x,y) -> float2(y,x))

### Arguments

• float2 seed - The random seed.
• float tiled - The seed of tiling (the number should be the power of two).

## floatnrandTAA(float2 seed_0, flaot tiled)

Returns the value (that changed each frame) within the range of [0;1] divided by the tiled seed.
Notice
Works only if USE_TAA enabled. Otherwise, nrandTiled is used.

### Arguments

• float2 seed_0 - The first random seed.
• flaot tiled - The seed of tiling (the number should be the power of two).

## float2nrandTAA(float2 seed_0, float2 seed_1, float tiled)

Returns the float2 vector with random values (that changed each frame) within the range of [0;1] divided by the tiled seed.
Notice
Works only if USE_TAA enabled. Otherwise, nrandTiled is used.

### Arguments

• float2 seed_0 - The first random seed.
• float2 seed_1 - The second random seed.
• float tiled - The seed of tiling (the number should be the power of two).

## float2nrand2TAA(float2 seed, float tiled)

Returns the float2 vector with random values (that changed each frame) within the range of [0;1] divided by the tiled seed (For the second seed, the function shifts the vector: float2(x,y) -> float2(y,x))
Notice
Works only if USE_TAA enabled. Otherwise, nrand2Tiled is used.

### Arguments

• float2 seed - The random seed.
• float tiled - The seed of tiling (the number should be the power of two).

## dotFixed(value X, value Y)

Returns the dot product of two vectors within the range of 0 to 1.

Equivalents

OpenGL
``saturate(dot(X,Y))``
Direct3D
``saturate(dot(X,Y)))``

### Arguments

• value X - The first specified vector.
• value Y - The second specified vector.

## lerpFixed(value X, value Y, value FACTOR)

Performs a linear interpolation of two vectors with the factor in the range of 0 to 1.

Equivalents

OpenGL
``lerp(X,Y,saturate(FACTOR))``
Direct3D
``lerp(X,Y,saturate(FACTOR))``

### Arguments

• value X - The first specified vector.
• value Y - The second specified vector.
• value FACTOR - A value that linearly interpolates between the x parameter and the y parameter.

## float4getPosition(value vertex)

Returns the projected position of the vertex.

### Arguments

• value vertex - The first specified vertex. Can be one of the following types:
• float3
• float4

### Return value

The projected position of the vertex.

## float3getDepthToPosition(float depth, float2 uv, float4x4 iprojection)

Returns the position according to depth value.

### Arguments

• float depth - Depth value.
• float2 uv - UV position.
• float4x4 iprojection - Inversed projection matrix.

Position vector.

## float3getDepthToPosition(float depth, float2 uv)

Returns the position according to depth value. The function uses s_camera_iprojection shader parameter.

### Arguments

• float depth - Depth value.
• float2 uv - UV position.

Position vector.

## floatgetLinearizedDepth(float native_depth, float2 uv)

Returns linearized depth value.

### Arguments

• float native_depth - Native depth value.
• float2 uv - UV position.

### Return value

Linearized depth value.

## floatgetLinearizedDepth(TEXTURE_IN (depth_map), float2 uv)

Returns linearized depth value.

### Arguments

• TEXTURE_IN (depth_map) - Depth map texture.
• float2 uv - UV position.

### Return value

Linearized depth value.

## float3getViewDirection(value uv)

Returns the view direction vector.

Implementation

UUSL
``mul4(s_camera_iprojection,float4(uv,0.0f,1.0f)).xyz``

### Arguments

• value uv - UV position. Can be one of the following types:
• float2
• float3
• float4
Notice
In case of float3 or float4 vectors, vector.xy values will be taken.

### Return value

The view direction vector.

## float2getPositionToUV(float4 position)

Returns UV position by using given fragment position.

### Arguments

• float4 position - Fragment position.

UV position.

## float2getPositionToUV(float3 position)

Returns UV position by using given fragment position.

### Arguments

• float3 position - Fragment position.

UV position.

## float2getAnimationOffset(float angle)

Returns animation offset by using given angle.

### Arguments

• float angle - Angle

### Return value

Animation offset.

## voidgetTangentBasis(float4 basis, float3 out tangent, float3 out binormal, float3 out normal)

Returns the tangent basis.
UUSL
``````// Get normals
float3 tangent,binormal,normal;

// Getting normal in object-space
getTangentBasis(IN_ATTRIBUTE(2),tangent,binormal,normal);

// Transform object-space TBN into camera-space TBN
normal = normalize(mul3(row_0,row_1,row_2,normal));
tangent = normalize(mul3(row_0,row_1,row_2,tangent));
binormal = normalize(mul3(row_0,row_1,row_2,binormal));``````

### Arguments

• float4 basis - Basis vector.
• float3 out tangent - Empty vector for tangent.
• float3 out binormal - Empty vector for binormal.
• float3 out normal - Empty vector for normal.

## voidgetTangentBasis(float4 basis, float3 out tangent, float3 out normal)

Returns the tangent basis.

### Arguments

• float4 basis - Basis vector.
• float3 out tangent - Empty vector for tangent.
• float3 out normal - Empty vector for normal.

## float2uvTransform(float2 uv, float2 tiling, float2 offset)

Transforms the UV by using given parameters.

Implementation

UUSL
``uv * tiling + offset;``

### Arguments

• float2 uv - UV position value.
• float2 tiling - Tiling value.
• float2 offset - Offset value.

### Return value

Transformed UV position.

## float2uvTransform(float2 uv, float4 transform)

Transforms the UV by using given parameters.

Implementation

UUSL
``uv * transform.xy + transform.zw;``

### Arguments

• float2 uv - UV position value.
• float4 transform - Transform vector value.

### Return value

Transformed UV position.

## float4uvTransform(float4 uv, float2 tiling, float2 offset)

Transforms the UV by using given parameters.

Implementation

UUSL
``uv * tiling.xyxy + offset.xyxy;``

### Arguments

• float4 uv - UV position value.
• float2 tiling - Tiling value.
• float2 offset - Offset value.

### Return value

Transformed UV position.

## float4uvTransform(float4 uv, float4 transform)

Transforms the UV by using given parameters.

Implementation

UUSL
``uv * transform.xyxy + transform.zwzw;``

### Arguments

• float4 uv - UV position value.
• float4 transform - Transform vector value.

### Return value

Transformed UV position.

## boolcheckUV(float2 uv)

Checks if the UV position is correct (is it in [0;1] range).

### Arguments

• float2 uv - UV position.

### Return value

1 if the UV is correct, otherwise, 0.

## voidnormalizationTBN(inout float3 T, inout float3 B, inout float3 N, float sign_binormal, float front_face)

Calculates normalized TBN vectors.

### Arguments

• inout float3 T - Tangent vector.
• inout float3 B - Binormal vector.
• inout float3 N - Normal vector.
• float sign_binormal - Binormal vector sign (ATTRIBUTE_BASIS.w).
• float front_face - Flag indicated is the mesh two-sided or not. (1 is for front face, -1 for back face).

## float3mul(float3 vector, float3x3 matrix)

Performs the vector * matrix multiplication.
Notice
Works only for OpenGL.

### Arguments

• float3 vector - The 3-component vector.
• float3x3 matrix - The 3x3 matrix.

### Return value

The result of multiplication.

## float3mul(float3x3 matrix, float3 vector)

Performs the matrix * vector multiplication.
Notice
Works only for OpenGL.

### Arguments

• float3x3 matrix - The 3x3 matrix.
• float3 vector - The 3-component vector.

### Return value

The result of multiplication.

## float4mul(float4 vector, float4x4 matrix)

Performs the vector * matrix multiplication.
Notice
Works only for OpenGL.

### Arguments

• float4 vector - The 4-component vector.
• float4x4 matrix - The 4x4 matrix.

### Return value

The result of multiplication.

## float4mul(float4x4 matrix, float4 vector)

Performs the matrix * vector multiplication.
Notice
Works only for OpenGL.

### Arguments

• float4x4 matrix - The 4x4 matrix.
• float4 vector - The 4-component vector.

### Return value

The result of multiplication.

## float3mul3(float4x4 matrix, float3 vector)

Performs the vector * matrix multiplication.

### Arguments

• float4x4 matrix - The 4x4 matrix.
• float3 vector - The 3-component vector.

### Return value

The result of multiplication.

## float3mul3(float3 vector, float4x4 matrix)

Performs the matrix * vector multiplication.

### Arguments

• float3 vector - The 3-component vector.
• float4x4 matrix - The 4x4 matrix.

### Return value

The result of multiplication.

## float3mul3(float4 row_0, float4 row_1, float4 row_2, float3 vector)

Performs the matrix * vector multiplication.

### Arguments

• float4 row_0 - The first row of the matrix
• float4 row_1 - The second row of the matrix
• float4 row_2 - The third row of the matrix
• float3 vector - The 3-component vector.

### Return value

The result of multiplication.

## float3mul3(float3x3 matrix, float3 vector)

Performs the matrix * vector multiplication.

### Arguments

• float3x3 matrix - The 3x3 matrix.
• float3 vector - The 3-component vector.

### Return value

The result of multiplication.

## float4mul4(float4x4 matrix, float3 vector)

Performs the matrix * vector multiplication.
OpenGL
``(m * float4(v,1.0f)).xyz;``
Direct3D
``(m * float4(v,1.0f)).xyz;``

### Arguments

• float4x4 matrix - The 4x4 matrix.
• float3 vector - The 3-component vector.

### Return value

The result of multiplication.

## float4mul4(float4x4 matrix, float4 vector)

Performs the matrix * vector multiplication.

### Arguments

• float4x4 matrix - The 4x4 matrix.
• float4 vector - The 4-component vector.

### Return value

The result of multiplication.

## float4mul4(float4 row_0, float4 row_1, float4 row_2, float3 vector)

Performs the matrix * vector multiplication.
OpenGL
``float4(dot(row_0,v),dot(row_1,v),dot(row_2,v),v.w);``
Direct3D
``float4(dot(row_0,v),dot(row_1,v),dot(row_2,v),v.w);``

### Arguments

• float4 row_0 - The first row of the matrix
• float4 row_1 - The second row of the matrix
• float4 row_2 - The third row of the matrix
• float3 vector - The 4-component vector.

### Return value

The result of multiplication.

## floatfloatPack88To16(float2 value)

Packs RG8 into R16.

### Arguments

• float2 value - Value to pack.

## float2floatPack16To88(float value)

Unpacks R16 into RG8.

### Arguments

• float value - Value to pack.

## float2floatPack8888To1616(float4 value)

Packs normalized RGBA8 into R16G16.

### Arguments

• float4 value - Value to pack.

## float4floatPack1616To8888(float2 value)

Unpacks R16G16 into RGBA8.

### Arguments

• float2 value - Value to pack.

## float3floatPack1212To888(float2 value)

Packs RG12 to RGB8.

### Arguments

• float2 value - Value to pack.

## float2floatPack888To1212(float3 value)

Packs RGB8 to RG12.

### Arguments

• float3 value - Value to pack.

## floatfloatPack44To8(float x, float y)

Packs two 4-bit values into to 8-bit.

### Arguments

• float x - Value to pack.
• float y - Value to pack.

## float2floatPack8To44(float value)

Unpacks 8-bit value into two 4-bit values.

### Arguments

• float value - Value to pack.

## voidloadEnvironmentCubeMap(inout float3 env, inout float3 ref, GBuffer gbuffer, Data data, TEXTURE_IN_CUBE (TEX_REFLECTION))

### Arguments

• inout float3 env - ---
• inout float3 ref - ---
• GBuffer gbuffer - GBuffer struct.
• Data data - Data struct.
• TEXTURE_IN_CUBE (TEX_REFLECTION) - Environment cubemap texture.

## float3getEnvironmentReflectVector(GBuffer gbuffer, Data data)

Returns environment reflection vector.

### Arguments

• GBuffer gbuffer - GBuffer struct.
• Data data - Data struct.

### Return value

Environment reflection vector.

## voidenvironmentReflectionShading(inout float3 reflection, GBuffer gbuffer, Data data)

### Arguments

• inout float3 reflection - Environment reflection.
• GBuffer gbuffer - GBuffer struct.
• Data data - Data struct

## voidgbufferSRGB(GBuffer gbuffer)

Converts GBuffer's albedo and f0 fields to sRGB.

## floatgetFresnelSchlick(float3 F0, float dotLH)

Calculates Fresnel factor (Schlick's approximation).

### Arguments

• float3 F0 - Reflectance for H·V = 1.
• float dotLH - Dot product of 2 vectors: vector to light source and normalized halfway vector (H = (V + L) / |V + L|).

## float3getFresnel(float3 specular_color, float3 dotLH)

Calculates Fresnel for specular workflow.

### Arguments

• float3 specular_color - Specular color vector.
• float3 dotLH - Dot product of 2 vectors: vector to light source and normalized halfway vector (H = (V + L) / |V + L|).

## floatgetGGX(float roughness, float dotNV, float dotNL, float dotNH)

Calculates specular lighting.

### Arguments

• float roughness - Roughness value.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float dotNH - Dot product of 2 vectors: normal vector and normalized halfway vector (H = (V + L) / |V + L|).

## floatgetGGX(float roughness, float dotNV, float dotNL, float dotNH, float translucent_scale)

Calculates specular lighting.

### Arguments

• float roughness - Roughness value.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float dotNH - Dot product of 2 vectors: normal vector and normalized halfway vector (H = (V + L) / |V + L|).
• float translucent_scale - Translucent scale value.

## floatgetAreaLightGGX(float roughness, float dotNV, float dotNL, float dotLR, float size)

Calculates area light specular lighting.

### Arguments

• float roughness - Roughness value.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float dotLR - Dot product of 2 vectors: normal vector and reflected vector.
• float size - The size of specular lighting.

## floatgetAreaLightGGX(float roughness, float dotNV, float dotNL, float dotLR, float size, float translucent_scale)

Calculates area light specular lighting.

### Arguments

• float roughness - Roughness value.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float dotLR - Dot product of 2 vectors: vector to light source and reflected vector.
• float size - The size of specular lighting.
• float translucent_scale - Translucent scale value.

## floatgetPhong(float dotLR, float power)

### Arguments

• float dotLR - Dot product of 2 vectors: normal vector and reflected vector.
• float power - The specular power value.

## floatgetBlinn(float dotNH, float power)

### Arguments

• float dotNH - Dot product of 2 vectors: normal vector and normalized halfway vector (H = (V + L) / |V + L|).
• float power - The specular power value.

## floatgetBurley12(float roughness, float dotNV, float dotVH, float dotNL)

Calculates diffuse lighting.
Notice

### Arguments

• float roughness - Roughness value.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotVH - Dot product of 2 vectors: vector to camera and normalized halfway vector (H = (V + L) / |V + L|).
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.

## floatgetWrapAround(float dotNL, float factor)

Calculates Energy-Conserving Wrapped Diffuse.

### Arguments

• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float factor - Energy conservation factor.

## floatgetTranslucent(float translucent_scale, float dotVL, float dotNL)

Calculates translucent.

### Arguments

• float translucent_scale - Translucent scale value.
• float dotVL - Dot product of 2 vectors: vector to camera and vector to light source.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.

## float3getSpecularBRDF(Gbuffer gbuffer, float3 specular_color, float dotNV, float dotLH, float dotNL, float dotNH)

Calculates BRDF for specular workflow.

### Arguments

• Gbuffer gbuffer - GBuffer struct.
• float3 specular_color - Specular color vector.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotLH - Dot product of 2 vectors: vector to light source and normalized halfway vector.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float dotNH - Dot product of 2 vectors: normal vector and normalized halfway vector (H = (V + L) / |V + L|).

## float3getAreaLightSpecularBRDF(Gbuffer gbuffer, float3 specular_color, float dotNV, float dotLH, float dotNL, float dotNH, float size)

Calculates area light specular BRDF.

### Arguments

• Gbuffer gbuffer - GBuffer struct.
• float3 specular_color - Specular color vector.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotLH - Dot product of 2 vectors: vector to light source and normalized halfway vector.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float dotNH - Dot product of 2 vectors: normal vector and normalized halfway vector (H = (V + L) / |V + L|).
• float size - The size of specular lighting.

## float2getDiffuseBRDF(Gbuffer gbuffer, float dotNV, float dotLH, float dotNL, float dotVL)

Calculates diffuse BRDF

### Arguments

• Gbuffer gbuffer - GBuffer struct.
• float dotNV - Dot product of 2 vectors: normal vector and vector to camera.
• float dotLH - Dot product of 2 vectors: vector to light source and normalized halfway vector.
• float dotNL - Dot product of 2 vectors: normal vector and vector to light source.
• float dotVL - Dot product of 2 vectors: vector to camera and vector to light source.

## voidgetBRDF(float3 diffuse, float specular, Gbuffer gbuffer, Data data, float3 light_direction)

Calculates BRDF.

### Arguments

• float3 diffuse - Diffuse value.
• float specular - Specular value.
• Gbuffer gbuffer - GBuffer struct.
• Data data - Data struct.
• float3 light_direction - Light direction vector.

## floatgetLightAttenuation(float distance, float light_attenuation)

Calculates sRGB Light Attenuation by using calculated attenuation value and distance.

### Arguments

• float distance - Light distance value.
• float light_attenuation - Light attenuation value.

### Return value

Light attenuation value.

## floatgetLightAttenuation(float3 position)

Calculates Light Attenuation by given position.

### Arguments

• float3 position - Position value.

### Return value

Light attenuation.

## floatgetLightAttenuation(float light_distance)

Calculates Light Attenuation by distance.

### Arguments

• float light_distance - Light Distance value.

### Return value

Light attenuation.

## float3sphereLightToLight(float3 L, float3 direction)

Returns normalized shift of L vector.

### Arguments

• float3 L - Vector to the light source.
• float3 direction - Shift direction vector.

## float3capsuleLightToLight(float3 L, float3 direction, float3 axis, float3 dotLA)

Returns normalized shift of L vector (depends on given axis and direction vector).

### Arguments

• float3 L - Vector to the light source.
• float3 direction - Shift direction vector.
• float3 axis - Axis of shift.
• float3 dotLA - Dot product of 2 vectors: axis vector and vector to light source.

## float3getMicrofiber(color color, Data data, Gbuffer gbuffer)

Calculates microfiber.

### Arguments

• color color - Color vector.
• Data data - Data struct.
• Gbuffer gbuffer - GBuffer struct.

### Return value

Microfiber color vector.

## float3specularReflection(float4 specular, float dotVN, float exponent)

Calculates specular reflection with exponent.

### Arguments

• float4 specular - Specular value.
• float dotVN - Dot product of 2 vectors: normal vector and vector to camera.
• float exponent - Exponent value for pow(dotVN, exponent) operation.

### Return value

Specular reflection vector.

## float3specularReflection(float4 specular, float dotVN)

Calculates specular reflection.

### Arguments

• float4 specular - Specular value.
• float dotVN - Dot product of 2 vectors: normal vector and vector to camera.

### Return value

Specular reflection vector.

## Scattering Functions

Here is an example of scattering functions usage:

UUSL
``````#ifdef USE_HAZE
#ifdef USE_HAZE_SCATTERING
float4 haze = hazeScattering(depth,camera_dir,TEXTURE_OUT_3(TEX_BASE_LUT,TEX_MIE_SUN_LUT,TEX_MIE_MOON_LUT));
#elif USE_HAZE_SOLID
float4 haze = hazeSolid(depth);
#endif

OUT_COLOR.rgb = OUT_COLOR.rgb * haze.a + haze.rgb;
#endif

// forward
#ifdef USE_HAZE && !STAR_AMBIENT
OUT_COLOR = hazeForward(OUT_COLOR,depth,camera_dir,TEXTURE_OUT_3(TEX_BASE_LUT,TEX_MIE_SUN_LUT,TEX_MIE_MOON_LUT));
#endif``````

## float4hazeScattering(float depth, float3 camera_dir, TEXTURE_IN_3 (base,mie_sun,mie_moon))

Calculates the haze in the scattering mode.

### Arguments

• float depth - Depth value.
• float3 camera_dir - Camera direction vector.
• TEXTURE_IN_3 (base,mie_sun,mie_moon) - Set of 3 LUT textures: base, mie sun and mie moon.

Haze vector.

## float4hazeForward(float4 color, float depth, float3 camera_dir, TEXTURE_IN_3 (base,mie_sun,mie_moon))

Calculates the haze for objects rendered in the forward mode.

### Arguments

• float4 color - Color
• float depth - Depth value.
• float3 camera_dir - Camera direction vector.
• TEXTURE_IN_3 (base,mie_sun,mie_moon) - Set of 3 LUT textures: base, mie sun and mie moon.

Haze vector.

## floathazeAlpha(float depth)

Calculates transparency alpha value of the haze.

### Arguments

• float depth - Depth value.

### Return value

Haze alpha value.

## floathazeAlpha(float3 position)

Calculates transparency alpha value of the haze.

### Arguments

• float3 position - Position.

### Return value

Haze alpha value.

## floathazeAlpha(float4 position)

Calculates transparency alpha value of the haze.

### Arguments

• float4 position - Position.

### Return value

Haze alpha value.

## float4hazeSolid(float depth)

Calculates completely solid haze.

### Arguments

• float depth - Depth value.

### Return value

Haze solid vector.

## float4hazeSolid(float position)

Calculates completely solid haze.

### Arguments

• float position - Position.

### Return value

Haze solid vector.

## float4hazeSolid(float position)

Calculates completely solid haze.

### Arguments

• float position - Position.

### Return value

Haze solid vector.

## float4hazeForwardSimple(float4 color, float depth)

Calculates haze for objects rendered in the forward mode, can be used in the vertex shader, turns objects to transparency.

### Arguments

• float4 color - Color vector.
• float depth - Depth value.

### Return value

Haze solid vector.

## float4hazeForwardSimple(float4 color, float3 position)

Calculates haze for objects rendered in the forward mode, can be used in the vertex shader, turns objects to transparency.

### Arguments

• float4 color - Color vector.
• float3 position - Position.

### Return value

Haze solid vector.

## float4hazeForwardSimple(float4 color, float4 position)

Calculates haze for objects rendered in the forward mode, can be used in the vertex shader, turns objects to transparency.

### Arguments

• float4 color - Color vector.
• float4 position - Position.

### Return value

Haze solid vector.
Last update: 2018-04-26