Will the fragment shader automatically clamp the color value to its range?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 20:06:35

问题


Will the fragment shader automatically clamp the color value to its range?

Do I need to explicitly clam the value int he shader code? If I do not, and the shader automatically does clampping, does it mean it will save some processing time?


回答1:


Yes, they are clamped automatically if the color buffer is in a normalized fixed-point format. Copied from the OpenGL 3.3 spec:

Color values written by a fragment shader may be floating-point, signed integer, or unsigned integer. If the color buffer has an signed or unsigned normalized fixed-point format, color values are assumed to be floating-point and are converted to fixed-point as described in equations 2.6 or 2.4, respectively; otherwise no type conversion is applied.

The referenced section "Conversion from Floating-Point to Normalized Fixed-Point" says (emphasis added):

The conversion from a floating-point value f to the corresponding unsigned normalized fixed-point value c is defined by first clamping f to the range [0, 1], then ...

Explicitly clamping in the fragment shader would be a waste of operations if your frame buffer format is of a normalized fixed-point type (like the typical GL_RGBA8). Extra clamping operations in the shader would most likely be very cheap, but certainly unnecessary.

The situation is different if you use floating point color buffers. As implied by the spec quote above, no clamping is applied in this case. Which is expected, because one main motivation for using floating point color buffers is to have an extended range of values. Floating point color buffers are created by rendering to an FBO with an attachment type of GL_RGBA16F, GL_RGBA32F, or similar.



来源:https://stackoverflow.com/questions/26786711/will-the-fragment-shader-automatically-clamp-the-color-value-to-its-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!