在OpenGL着色器中检测NaN的最佳方法 [英] Best way to detect NaN's in OpenGL shaders

查看:322
本文介绍了在OpenGL着色器中检测NaN的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早晨,我遇到了一个似乎神秘的错误,我很幸运能很快找到一个解决方案.

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

我正在用一个计数器除以在片段着色器内部产生平均值,当然,当计数器为零时,所得的颜色值将变为NaN.

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.

在混合过程中,NVidia会适当地将NaN视为0值,但是Intel不会,并且似乎会级联NaN,从而产生黑色碎片.

During blending, NVidia gracefully treats a NaN as a 0 value, but Intel does not and appears to cascade the NaN, resulting in black fragments.

因此,这个错误一直存在,直到我在Intel机器上测试了代码.

And so this bug persisted until I tested the code on an Intel machine.

我想知道是否可以做些陷阱"无效值的事情.看起来,就像在常规编程中一样,解决此问题的唯一肯定方法(即使这样也感觉不透),是在除数时仔细考虑所有可能的情况.

I wonder if there is something I could do to "trap" invalid values. It seems that, just as in regular programming, the only surefire (and even then it doesn't feel bulletproof) way to deal with this is to carefully consider all possible cases when dividing numbers.

检测NaN的标准方法是查看数字是否不等于其自身.也许我可以构建一个调试着色器,以检查每个片段是否与自身不相等,如果满足该条件,则设置闪烁的,醒目的颜色? GLSL是否允许我以这种方式检测到NaN,或者当值无效时我是否会陷入不确定的行为?

The standard way to detect a NaN is to see if a number is not equal to itself. Could I perhaps build a debug shader which checks each fragment to see if it is not equal to itself, and if that condition is met, set a flashing, conspicuous color? Does GLSL allow me to detect a NaN this way or am I stuck with undefined behavior whenever a value is invalid?

推荐答案

我正在用一个计数器除以在片段着色器内部产生平均值,当然,当计数器为零时,所得的颜色值将变为NaN.

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.

浮动不是这样工作的.除以零时,您得到的是 INF ,而不是NaN.除非分子也为零,否则您将获得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.

无论如何,GLSL提供 isinf isnan 函数,照他们说的做.

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

这篇关于在OpenGL着色器中检测NaN的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆