Fragment shader without gl_FragColor explicitly set?

泄露秘密 提交于 2019-12-24 01:53:57

问题


Is not writing anything to gl_FragColor defined as equivalent of doing a discard or simply setting gl_FragColor to vec4(0, 0, 0, 0)?

(For the record, I'm asking this as I'm creating shaders which only writes to gl_FragDepth, for lighting purposes.)


回答1:


From the GLSL 4.5 specs...

The following fragment output variables are available in a fragment shader when using the compatibility profile:

out vec4 gl_FragColor;
out vec4 gl_FragData[gl_MaxDrawBuffers];

Writing to gl_FragColor specifies the fragment color that will be used by the subsequent fixed functionality pipeline. If subsequent fixed functionality consumes fragment color and an execution of the fragment shader executable does not write a value to gl_FragColor then the fragment color consumed is undefined.

So no, it's neither a discard, nor setting all zeroes. Different things can happen with different drivers and video cards; it's "undefined".

Pure speculation on my part but it's likely that what ever data happens to be in the physical registers when the shader returns ends up as the output. This would be largely dependent on the previous things the GPU was doing. It might be common for it to be all black, just like neglecting to initialize int *myPointer; in C can quite often end up with zero because it happens to be the initial state of the RAM.



来源:https://stackoverflow.com/questions/26797736/fragment-shader-without-gl-fragcolor-explicitly-set

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