KhronosGroup / GLSL

GLSL Shading Language Issue Tracker
328 stars 96 forks source link

What is the result of normalize(vec3(0.0))? #158

Closed ianromanick closed 2 years ago

ianromanick commented 3 years ago

The GLSL 4.60 specification and the SPIR-V Extended Instructions for GLSL specification both say

Returns a vector in the same direction as x but with a length of 1.

However, what is the expected result when x is the zero vector? When all the components of x are subnormal values?

At least one implementation implements normalize in the obvious way: return x / length(x);. In the exceptional cases mentioned above, this will result in NaN. See https://gitlab.freedesktop.org/mesa/mesa/-/issues/4678 and https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10481.

I think that's clearly unacceptable, but what should the result be? The zero vector? A "random" unit vector?

ianromanick commented 3 years ago

Cases where isinf(dot(x, x)) are also potentially problematic.

pdaniell-nv commented 3 years ago

We discussed this in today's OpenGL/ES meeting and we're not inclined to add any special language, or require implementations to add special handling, for these cases and will allow implementations to return inf, nan or whatever. The advice to applications is - don't do that.