Skip to content

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.
    float distanceToCenter = abs(uv.x - 0.5f); // 0.0 at center, rises towards sides
    
2026-06-04T16:15:54.531647 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −3 −2 −1 0 1 2 3 x −0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 y abs(x)

sqrt(x)#

  • Mathematical Definition: \(\sqrt{x}\)
  • Use case: Computing Euclidean distances or physical falloff curves.
    float dist = sqrt(x*x + y*y);
    
2026-06-04T16:15:54.571491 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0 1 2 3 4 5 x 0.0 0.5 1.0 1.5 2.0 2.5 y sqrt(x)

pow(x, y)#

  • Mathematical Definition: \(x^y\)
  • Use case: Applying gamma correction or adjusting falloff contrast curves.
    float spotlightIntensity = pow(dotProduct, 16.0f); // Tight, sharp spotlight beam
    
2026-06-04T16:15:54.618636 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 x 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 y pow(x, y) y = 0.5 y = 1.0 y = 2.0 y = 3.0

log(x)#

  • Mathematical Definition: \(\ln(x)\) (Natural logarithm, base \(e\))
  • Use case: Simulating human sensory perception (such as volume or exposure curves).
    float perceivedBrightness = log(hdrColor + 1.0f);
    
2026-06-04T16:15:54.661564 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0 1 2 3 4 5 x −3 −2 −1 0 1 2 y log(x) [Natural Log]

exp(x)#

  • Mathematical Definition: \(e^x\)
  • Use case: Modeling exponential growth or atmospheric attenuation.
    float atmosphericFog = exp(-density * distance);
    
2026-06-04T16:15:54.705001 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0 x 0 2 4 6 8 y exp(x)

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.
    float2 pixelatedUV = floor(uv * 128.0f) / 128.0f;
    
2026-06-04T16:15:54.748781 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −2 −1 0 1 2 x −3 −2 −1 0 1 2 3 y floor(x)

ceil(x)#

  • Mathematical Definition: \(\lceil x \rceil\) (Smallest integer \(\geq x\))
  • Use case: Generating stepped thresholds or bounds checks.
    float visibilityMask = ceil(opacity - 0.01f); // 1 if opacity > 0, else 0
    
2026-06-04T16:15:54.792528 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −2 −1 0 1 2 x −3 −2 −1 0 1 2 3 y ceil(x)

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.
    float2 snappedPixelPos = round(input.position.xy);
    
2026-06-04T16:15:54.835435 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −2 −1 0 1 2 x −3 −2 −1 0 1 2 3 y round(x)

frac(x)#

  • Mathematical Definition: \(x - \lfloor x \rfloor\)
  • Use case: Creating repeating patterns, tiled textures, or periodic wave motions.
    float repeatingStripes = frac(uv.y * 10.0f); // Ramps from 0 to 1 ten times
    
2026-06-04T16:15:54.879823 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −2 −1 0 1 2 x −0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 y frac(x)

Trigonometric Functions#

sin(x)#

  • Mathematical Definition: \(\sin(x)\) where \(x\) is in radians.
  • Use case: Generating oscillations for animation, wind sways, or blinking lights.
    float heartbeat = sin(u_time * 3.0f);
    

cos(x)#

  • Mathematical Definition: \(\cos(x)\) where \(x\) is in radians.
  • Use case: Combining with sine to perform 2D circular rotations.
    float2 offset = float2(sin(u_time), cos(u_time));
    
2026-06-04T16:15:54.977409 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −2 π − π 0 π 2 π x −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 y sin(x) & cos(x) sin(x) cos(x)

tan(x)#

  • Mathematical Definition: \(\tan(x) = \sin(x) / \cos(x)\)
  • Use case: Projecting coordinates or setting up camera field-of-view matrices.
    // Calculate physical vertical displacement based on projection angle
    float heightOffset = tan(angleRadians) * distance;
    
2026-06-04T16:15:55.036656 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ − π 0 π x −4 −2 0 2 4 y tan(x)

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.
    // Convert normalized vertical height coordinate back to radians
    float verticalAngle = asin(normalizedY);
    
2026-06-04T16:15:55.076224 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −1 0 1 x −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 y asin(x)

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.
    float angleRadians = acos(dot(vectorA, vectorB));
    
2026-06-04T16:15:55.112982 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −1 0 1 x 0.0 0.5 1.0 1.5 2.0 2.5 3.0 y acos(x)

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.
    // Calculate slope angle from vertical and horizontal offsets
    float slopeAngle = atan(verticalOffset / horizontalOffset);
    
2026-06-04T16:15:55.150819 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −4 −2 0 2 4 x −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 y atan(x)

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.
    float angle = atan2(centeredUV.y, centeredUV.x); // Useful for radial blur
    
2026-06-04T16:15:55.195833 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −1 0 1 x −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 y P(x, y) θyx =atan2(,) atan2(y, x)

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.
    float brightness = dot(normal, lightDirection);
    
2026-06-04T16:15:55.766208 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −1.0 −0.5 0.0 0.5 1.0 x −0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 y ⃗ N ⃗ L θ dot(N, L) =  cos() θ

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).
    float3 tangent = cross(normal, float3(0, 1, 0));
    
  • 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).
2026-06-04T16:15:55.832406 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −0.50 −0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 X −0.50 −0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 Y −0.50 −0.25 0.00 0.25 0.50 0.75 1.00 1.25 1.50 Z cross(A, B) [Perpendicular Vector] A B A x B

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.
    float distToCenter = length(centeredUV); // Center vignette mask
    

normalize(v)#

  • Mathematical Definition: \(\frac{\vec{v}}{\|\vec{v}\|_2}\)
  • Use case: Gives a unit vector in the direction of \(\vec{v}\)
    float3 lightDir = normalize(lightPos - fragmentPos);
    
2026-06-04T16:15:55.605941 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0.0 0.5 1.0 1.5 x 0.00 0.25 0.50 0.75 1.00 1.25 1.50 y ⃗=(1.2,0.9) v normalize()⃗ v ‖‖⃗=1.5 v length(v) & normalize(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.
    float force = 1.0f - saturate(distance(impactPos, currentPos) / radius);
    
2026-06-04T16:15:55.693365 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0 1 2 3 x −0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 y ⃗ p 1 ⃗ p 2 distance(⃗,⃗) pp 12 distance(p1, p2)

reflect(i, n)#

  • Mathematical Definition: \(\vec{I} - 2(\vec{N} \cdot \vec{I})\vec{N}\)
  • Use case: Computing mirror reflection directions for specular maps.
    float3 reflectDir = reflect(incidentLightVector, surfaceNormal);
    
2026-06-04T16:15:55.458238 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 x −0.5 0.0 0.5 1.0 1.5 y N I R = reflect(I, N) reflect(I, N)

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.
    float3 refractDir = refract(ray, normal, indexRefraction);
    
2026-06-04T16:15:55.525417 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −1 0 1 x −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 y Medium 1 () η 1 Medium 2 () η 2 ⃗ N ⃗ I ⃗=refract(,⃗,⃗) TINη refract(I, N, eta)

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.
    float3 finalColor = lerp(sceneColor, fogColor, fogFactor);
    
2026-06-04T16:15:55.243147 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0.0 0.2 0.4 0.6 0.8 1.0 t 0 1 2 3 4 5 6 Value a b lerp(a, b, t)

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.
    float isLeftHalf = step(0.5f, uv.x);
    

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.
    float vignette = smoothstep(0.4f, 0.8f, distFromCenter);
    
2026-06-04T16:15:55.291585 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 x −0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 y step(0.5, x) vs smoothstep(0, 1, x) step(0.5, x) smoothstep(0, 1, x)

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].
    float3 safeColor = saturate(computedColor);
    

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.
    float exposure = clamp(autoExposure, 0.2f, 2.5f);
    
2026-06-04T16:15:55.345548 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 x −0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 y saturate(x) vs clamp(x, 0.2, 2.5) saturate(x) clamp(x, 0.2, 2.5)

min(a, b)#

  • Mathematical Definition: \(a \text{ when } a < b,\; \text{otherwise } b\)
  • Use case: Capping color parameter limits.
    float highlightCap = min(colorVal, 1.5f);
    

max(a, b)#

  • Mathematical Definition: \(a \text{ when } a > b,\; \text{otherwise } b\)
  • Use case: Preventing divisions by zero or negative color values.
    float brightness = max(diffuseLighting, 0.0f);
    
2026-06-04T16:15:55.400918 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ −2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0 x −2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0 y min(x, 0.5) & max(x, -0.5) min(x, 0.5) max(x, -0.5)

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 x with 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.
    float edge = abs(ddx(input.position.z)) + abs(ddy(input.position.z));
    

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.
    float4 clipPos = mul(projectionMatrix, viewSpacePos);
    
2026-06-04T16:15:55.895871 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0.0 0.5 1.0 1.5 x 0.00 0.25 0.50 0.75 1.00 1.25 1.50 y ⃗ v M ⃗ v Transform mul(M, v) [Transformation]

transpose(m)#

  • Mathematical Definition: Transposition of matrix \(\mathbf{M}\) (\(\mathbf{M}^T\))
  • Use case: Swapping row-major to column-major layout formats.
    float3x3 colMajorMatrix = transpose(rowMajorMatrix);
    
2026-06-04T16:15:55.958212 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ Matrix M a b c d Transpose (Swap rows/cols) Matrix M T a c b d transpose(M) = M T

determinant(m)#

  • Mathematical Definition: Determinant of matrix \(\mathbf{M}\) (\(|\mathbf{M}|\))
  • Use case: Measuring scaling factors to determine volume/area changes of transformations.
    float scalingFactor = determinant(localToWorldMatrix);
    
2026-06-04T16:15:56.029641 image/svg+xml Matplotlib v3.9.4, https://matplotlib.org/ 0.0 0.5 1.0 1.5 2.0 x 0.00 0.25 0.50 0.75 1.00 1.25 1.50 y ⃗ v 1 ⃗ v 2 Area =  || M (Scaling Factor) determinant(M) = M ||

Miscellaneous & Control#

any(v)#

  • Mathematical Definition: Returns true if any element in the vector v is non-zero.
  • Use case: Quickly testing boolean flags or masks.
    // Run post-processing filter only if the pixel has any color value
    if (any(prev.rgb)) {
        prev.rgb = prev.rgb * 1.1f;
    }
    

all(v)#

  • Mathematical Definition: Returns true if all elements in the vector v are non-zero.
  • Use case: Verifying if multi-channel thresholds are fully met.
    // Highlight pixels only if all color channels have saturated to maximum white
    bool isWhite = all(prev.rgb >= 1.0f);
    

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.
    // Determine vertical direction offset relative to the center line (returns -1.0f or 1.0f)
    float verticalDirection = sign(uv.y - 0.5f);
    

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.
    // Scale and offset UV coordinates in one hardware instruction
    float2 shiftedUV = mad(uv, scaleFactor, offsetVector); // uv * scaleFactor + offsetVector
    

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.
    // Wrap the game clock time to cycle an animation loop every 5 seconds
    float animationTimeline = fmod(u_time, 5.0f); // Ramps from 0.0f to 5.0f over 5 seconds, then resets and repeats
    

modf(x, out ipart)#

  • Mathematical Definition: Splits a floating-point value x into its fractional part (returned) and integer part (stored in ipart), both keeping the sign of x.
  • Use case: Extracting the local grid coordinates (fpart) and cell indices (ipart) from a continuous UV position.
    float ipart;
    float fpart = modf(uv.x * 10.0f, ipart); // e.g. 7.35f -> fpart = 0.35f, ipart = 7.0f