home/ atoms/ glsl-nan-from-undefined-math

GLSL operations with undefined results produce NaN/Inf that spread and turn pixels black

Several GLSL operations are undefined in the spec and typically yield NaN or Inf on the GPU: pow(x, e) with x < 0, 1.0/0.0, log(0.0), sqrt(negative), and normalize(vec3(0)). NaN and Inf propagate through subsequent arithmetic and usually render as black pixels — often as random black clusters where inputs occasionally cross the bad case. Mitigations: clamp bases before pow (pow(max(x,0.0), e)), add a small epsilon before dividing, and avoid zero-length vectors.

Examples

pow(max(d, 0.0), 2.0) // safe pow(d, 2.0) // NaN when d < 0 1.0/(x + 1e-6) // epsilon guards divide-by-zero

Assessment

A shape shader shows random black-pixel clusters. Name two GLSL expressions that can produce NaN and explain how to guard each.

“`pow(negative, x)` is undefined** → NaN → black pixels. Clamp the base: `pow(max(x,0.0), e)`.”
context/ · L1-instruments/glsl/gotchas.md · chunk 1