HLSL intrinsics#
Back to shader guides.
HLSL (High-Level Shader Language) provides built-in intrinsic functions that are highly optimized for execution on GPU hardware. Originally, these shaders target DirectX. In Rainfall, SPIRV-Cross compiles them to .spirv, which is then translated to other shading languages for the active rendering backend. Below is the complete mathematical definition and a practical use case for each essential function of HLSL.
Math Functions#
abs(x)#
- Mathematical Definition: \(|x| = x \text{ when } x \geq 0,\; |x| = -x \text{ when } x < 0\)
- Use case: Calculating symmetrical gradients or absolute differences.
sqrt(x)#
- Mathematical Definition: \(\sqrt{x}\)
- Use case: Computing Euclidean distances or physical falloff curves.
pow(x, y)#
- Mathematical Definition: \(x^y\)
- Use case: Applying gamma correction or adjusting falloff contrast curves.
log(x)#
- Mathematical Definition: \(\ln(x)\) (Natural logarithm, base \(e\))
- Use case: Simulating human sensory perception (such as volume or exposure curves).
exp(x)#
- Mathematical Definition: \(e^x\)
- Use case: Modeling exponential growth or atmospheric attenuation.
floor(x)#
- Mathematical Definition: \(\lfloor x \rfloor\) (Greatest integer \(\leq x\))
- Use case: Quantizing colors or UV coordinates to achieve pixelated or cell-shaded styles.
ceil(x)#
- Mathematical Definition: \(\lceil x \rceil\) (Smallest integer \(\geq x\))
- Use case: Generating stepped thresholds or bounds checks.
round(x)#
- Mathematical Definition: Evaluates to the nearest integer. If the fraction is exactly
0.5, rounds to the nearest even integer. - Use case: Snapping coordinates to integer screen pixel grids.
frac(x)#
- Mathematical Definition: \(x - \lfloor x \rfloor\)
- Use case: Creating repeating patterns, tiled textures, or periodic wave motions.
Trigonometric Functions#
sin(x)#
- Mathematical Definition: \(\sin(x)\) where \(x\) is in radians.
- Use case: Generating oscillations for animation, wind sways, or blinking lights.
cos(x)#
- Mathematical Definition: \(\cos(x)\) where \(x\) is in radians.
- Use case: Combining with sine to perform 2D circular rotations.
tan(x)#
- Mathematical Definition: \(\tan(x) = \sin(x) / \cos(x)\)
- Use case: Projecting coordinates or setting up camera field-of-view matrices.
asin(x)#
- Mathematical Definition: \(\operatorname{arcsin}(x) = y \text{ such that } \sin(y) = x \text{ for } y \in [-\pi/2,\, \pi/2]\)
- Use case: Converting rectangular spatial positions to angular coordinate dimensions.
acos(x)#
- Mathematical Definition: \(\operatorname{arccos}(x) = y \text{ such that } \cos(y) = x \text{ for } y \in [0,\, \pi]\)
- Use case: Direct calculation of angles between normalized vectors.
atan(x)#
- Mathematical Definition: \(\operatorname{arctan}(x) = y \text{ such that } \tan(y) = x \text{ for } y \in \left(-\pi/2,\, \pi/2\right)\)
- Use case: Calculating angles of continuous scalar slopes.
atan2(y, x)#
- Mathematical Definition: Computes the angle \(\theta \in ]-\pi, \pi]\) of the point \((x,y)\) relative to the positive x-axis.
- Use case: Converting Cartesian coordinates to radial polar angles.
Vector Operations#
dot(v1, v2)#
- Mathematical Definition: \(\vec{a} \cdot \vec{b} = \sum a_i b_i\)
- Use case: Computing diffuse lighting intensities or projection lengths.
cross(v1, v2)#
- Mathematical Definition: \(\vec{a} \times \vec{b} = (a_y b_z - a_z b_y, a_z b_x - a_x b_z, a_x b_y - a_y b_x)\)
- Use case: Calculating perpendicular vectors (e.g. generating tangent planes).
- Protip: to know which way the cross product point, put your right hand on the first vector and rotate it to the second vector, your thumb will point to the direction of the cross product (Right Hand Rule Video).
length(v)#
- Mathematical Definition: Length of the vector \(\|\vec{v}\|_2 = \sqrt{v_x^2 + v_y^2 + \dots}\)
- Use case: Determining distance from a coordinate point to a central source.
normalize(v)#
- Mathematical Definition: \(\frac{\vec{v}}{\|\vec{v}\|_2}\)
- Use case: Gives a unit vector in the direction of \(\vec{v}\)
distance(p1, p2)#
- Mathematical Definition: \(\|\vec{p_1} - \vec{p_2}\| = \sqrt{\sum (p_{1i} - p_{2i})^2}\)
- Use case: Generating point-light range masks or impact circles.
reflect(i, n)#
- Mathematical Definition: \(\vec{I} - 2(\vec{N} \cdot \vec{I})\vec{N}\)
- Use case: Computing mirror reflection directions for specular maps.
refract(i, n, eta)#
- Mathematical Definition: \(\eta \vec{I} - (\eta (\vec{N} \cdot \vec{I}) + \sqrt{1 - \eta^2 (1 - (\vec{N} \cdot \vec{I})^2)}) \vec{N}\) (Snell's Law refraction direction)
- Use case: Simulating light bending through glass or water.
Interpolation & Selection#
lerp(a, b, t)#
- Mathematical Definition: \(a + t(b - a)\) (Linear interpolation)
- Use case: Blending between two colors or texture profiles based on a mask.
step(edge, x)#
- Mathematical Definition: \(0 \text{ when } x < \text{edge},\; 1 \text{ when } x \geq \text{edge}\)
- Use case: Creating a sharp vertical split to divide a texture or screen in half.
smoothstep(edge0, edge1, x)#
- Mathematical Definition: \(t = \text{saturate}\left(\frac{x - \text{edge}_0}{\text{edge}_1 - \text{edge}_0}\right)\), returning \(3t^2 - 2t^3\) (the cubic polynomial \(t^2(3 - 2t)\) creates a smooth S-curve transition between \(\text{edge}_0\) and \(\text{edge}_1\) with flat zero-gradients/derivatives at both endpoints \(t=0\) and \(t=1\))
- Use case: Creating smooth transitions, vignettes, or soft fog boundaries.
Clamping & Limiting#
saturate(x)#
- Mathematical Definition: \(\min(\max(x, 0.0), 1.0)\)
- Use case: Clamping computed ranges to standard color parameters
[0.0, 1.0].
clamp(x, minVal, maxVal)#
- Mathematical Definition: \(\min(\max(x, \text{minVal}), \text{maxVal})\)
- Use case: Restricting HDR auto-exposure levels within a safe functional window between 0.2 and 2.5.
min(a, b)#
- Mathematical Definition: \(a \text{ when } a < b,\; \text{otherwise } b\)
- Use case: Capping color parameter limits.
max(a, b)#
- Mathematical Definition: \(a \text{ when } a > b,\; \text{otherwise } b\)
- Use case: Preventing divisions by zero or negative color values.
Texturing#
Note: Standard texture sampling intrinsics (such as tex2D or Sample) are not needed in this framework, as the target frame buffer color is dynamically exposed directly via the prev register.
Screen Derivatives#
ddx(x) & ddy(x)#
- Mathematical Definition: Approximation of the partial derivative of
xwith respect to screen-space coordinates \(x\) and \(y\). Derived by measuring values in \(2 \times 2\) pixel fragments. - Use case: Sharpness calculation, custom procedural anti-aliasing, and edge detection.
Matrix Math#
mul(a, b)#
- Mathematical Definition: Matrix multiplication (\(\mathbf{A} \times \mathbf{B}\))
- Use case: Transforming positions between local/world space and projection clip space.
transpose(m)#
- Mathematical Definition: Transposition of matrix \(\mathbf{M}\) (\(\mathbf{M}^T\))
- Use case: Swapping row-major to column-major layout formats.
determinant(m)#
- Mathematical Definition: Determinant of matrix \(\mathbf{M}\) (\(|\mathbf{M}|\))
- Use case: Measuring scaling factors to determine volume/area changes of transformations.
Miscellaneous & Control#
any(v)#
- Mathematical Definition: Returns
trueif any element in the vectorvis non-zero. - Use case: Quickly testing boolean flags or masks.
all(v)#
- Mathematical Definition: Returns
trueif all elements in the vectorvare non-zero. - Use case: Verifying if multi-channel thresholds are fully met.
sign(x)#
- Mathematical Definition: \(1 \text{ when } x > 0,\; 0 \text{ when } x = 0,\; -1 \text{ when } x < 0\)
- Use case: Extracting direction offsets from relative distances.
mad(a, b, c)#
- Mathematical Definition: \((a \cdot b) + c\)
- Use case: Optimized arithmetic operation (Fused Multiply-Add) that executes in a single GPU instruction cycle.
fmod(x, y)#
- Mathematical Definition: Returns the floating-point remainder of \(\frac{x}{y}\), matching the sign of \(x\).
- Use case: Time-wrapped periodic event tracking.
modf(x, out ipart)#
- Mathematical Definition: Splits a floating-point value
xinto its fractional part (returned) and integer part (stored inipart), both keeping the sign ofx. - Use case: Extracting the local grid coordinates (
fpart) and cell indices (ipart) from a continuous UV position.