问题描述
如果未设置,哪个是GLSL中输出颜色的默认值?
Which is the default value for the output color in GLSL in case you dont set it?
#version 330
uniform sampler2DRect colorTex;
uniform vec3 backgroundColor;
out vec4 outputColor;
void main(void)
{
vec4 frontColor = texture(colorTex, gl_FragCoord.xy);
outputColor.rgb = frontColor + backgroundColor * frontColor.a;
}
是(0,0,0,1)吗?
Is it (0, 0, 0, 1)?
Ps:该代码属于旧的GL,试图与GL3一起使用,我遇到以下错误
Ps: that code belongs to the old GL, trying to use it with the GL3, I get the following error
我猜对了
这是Nvidia前后深度剥离示例的片段着色器,您可以找到代码,org.jogl.demos.dualdepthpeeling\org.jogl.demos.dualdepthpeeling \src\demos\dualDepthPeeling\shaders\front_peeling_final_fragment.glsl
This is the fragment shader of the front to back depth peeling example of Nvidia, you can find the code here, org.jogl.demos.dualdepthpeeling\org.jogl.demos.dualdepthpeeling\src\demos\dualDepthPeeling\shaders\front_peeling_final_fragment.glsl
推荐答案
关于默认值片段着色器输出:
如果没有设置默认值,则结果为未定义。看到其他答案。
With regard to the default fragment shader output:There is no default and the result is undefined if you don't set one. See other answers.
我注意到您具有纹理,并采样了未绑定 /不完整的纹理不同 。 是默认值,但实际上我不会对所有驱动程序都依赖它!
I notice you have a texture, and sampling an 'unbound'/incomplete texture is different [1][2]. There is a default, but in practice I would not rely on this for all drivers!:
还有一个统一值,也可能无法设置。这些值在整个绘制调用中保持不变,并且与采样器一样,也具有默认值(尽管这更多是初始值)。
There's also a uniform value which may not be set either. These remain constant for the entire draw call, and like samplers, also have a default (although this is more of an "initial" value).
但是当您不设置片段颜色时,实际上会发生什么?
在规范中未定义意味着(0,0,0,1)
是一个有效值,实际上可能是您得到的值。如果GL实现(例如Nvidia或ATI随驱动程序+硬件提供的一种)使该返回值保持一致,则生成的着色器代码需要设置一个默认值,或者在不设置默认值时加以区分。这只会增加开销。
But what actually happens when you don't set a fragment colour?
Being undefined in the spec means (0, 0, 0, 1)
is a valid value and may in fact be the one you get. If a GL implementation (such as the one Nvidia or ATI provide with their driver + hardware) were to make this the consistent returned value, the generated shader code needs to set a default value or catch the case when you don't set one. This just adds overhead. It's faster to do nothing instead.
尽管如此,着色器仍必须返回一个值,并且片段着色器线程的寄存器中的所有值都将返回。这是未初始化的数据(从着色器程序的角度来看)。就像CPU程序中的未初始化内存一样,该值可以是任何值,通常取决于之前的内容。完全为零,甚至完全一致的情况并不罕见,这取决于GPU正在执行的先前任务(即您运行的另一个着色器)。尽管我在片段着色器中发现了未初始化的值,但通常会以闪烁的形式出现,并且可以创建如下模式:
The shader must still return a value though, and what ever value is in the register/s for your fragment shader thread gets returned. This is uninitialized data (from the point of view of your shader program). Just like uninitialized memory in a CPU program, the value could be anything, generally depending on what was there beforehand. It's not uncommon for this to be all zeroes, or even something pretty consistent depending on the previous task the GPU was doing (i.e. another shader you run). Although I've found uninitialized values in the fragment shader quite commonly presents as flickering and can make patterns like this:
这篇关于GLSL,输出颜色的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!