Best way to detect NaN's in OpenGL shaders

前端 未结 2 1456
孤城傲影
孤城傲影 2021-02-12 23:21

I ran into what seemed a mysterious bug this morning that I feel very lucky to have stumbled upon a solution for quite quickly.

I was dividing by a counter to produce a

相关标签:
2条回答
  • 2021-02-12 23:40

    I was dividing by a counter to produce an average inside of a fragment shader, and of course when the counter is zero, the resulting color value became NaN.

    That's not how floats work. When you divide by zero, you get INF, not NaN. Unless the numerator is also zero, in which case you do get NaN.

    In any case, GLSL offers the isinf and isnan functions, which do what they say.

    0 讨论(0)
  • 2021-02-12 23:43

    Lack of isnan is problem for WebGL and Opengl ES 2.0. Polyfill which works for all GPUs I had an opportunity to try:

    bool isnan( float val )
    {
      return ( val < 0.0 || 0.0 < val || val == 0.0 ) ? false : true;
      // important: some nVidias failed to cope with version below.
      // Probably wrong optimization.
      /*return ( val <= 0.0 || 0.0 <= val ) ? false : true;*/
    }
    
    0 讨论(0)
提交回复
热议问题