conditional returns

I haven’t found it written anywhere that I shouldn’t be able to conditionally return from a fragment shader (GLSL)

if(gl_Color.a == 0.0) return;

my shader will compile but refuses to link if i have a statement like that.

any ideas/information?

thanks.

Fragment shaders, like vertex shaders, are required to do certain things before ending. Fragment shaders must write to the fragment color (and the depth, if any condition writes to it).

Given your condition, however, you’re probably looking for “discard”, not return. It tells the system to ignore the results of fragment processing.

the fragment still needs to be able to have the opportunity to update the frame buffer so i can’t discard it.

my statement is actually exactly this:

if(gl_Color.a == 0.0)
{
gl_FragColor = sample (a texture sample)
return;
}

so, im updating the fragment color…

Try it this way:

if (gl_Color.a == 0.0){
gl_FragColor = sample (a texture sample)
} else {
// remaining code …

}

Originally posted by Korval:
Fragment shaders, like vertex shaders, are required to do certain things before ending. Fragment shaders must write to the fragment color (and the depth, if any condition writes to it).
Uh, no.

void main(void) {} // NULL fragment shader

Is perfectly fine.

-mr. bill

Originally posted by Humus:
[b]Try it this way:

if (gl_Color.a == 0.0){
gl_FragColor = sample (a texture sample)
} else {
// remaining code …

}[/b]
NO!

Texture fetches, except in certain circumstances, implicitly require derivatives to compute LOD!

dFdx, dFdy are UNDEFINED in the body of a non-uniform conditional.

Therefore, PLEASE code this way:

// ...
vec4 texel = texture2D( map, tc );   // Implicit derivative outside varying conditional
if ( gl_Color.a == 0.0 )
    gl_FragColor = texel;
else
    gl_FragColor = vec4( someColor );
// ...

-mr. bill

I can see how the pseudocode was misleading but
sample(a texture sample) is the sample that has already been obtained from the texture lookup before the branching code.

MrBill,
I still see no ‘return’ in the ‘if’ part of your code sample, is it illegal? Sure, I can code it another way, but being able to return there would trim a bunch of instructions and significantly improve performance (I think).

Originally posted by Aeluned:

MrBill,
I still see no ‘return’ in the ‘if’ part of your code sample, is it illegal? Sure, I can code it another way, but being able to return there would trim a bunch of instructions and significantly improve performance (I think).

It’s PERFECTLY legal to return from main, even in conditionals and loops. Any performance effect from your return ranges from “makes it better” to “makes it worse” and is implementation dependent.

-mr. bill

ah hah!
but my return is in another function (not main).

is that a problem? the friggin’ shader won’t link with a conditional return in this function.

…I’m going back to sprite rendering. I don’t think the future of computer graphics is 3D anyway.

You can return anywhere, but your return type has to match the function type.

-mr. bill

What graphics card do you have?

Not all chips fully support the language. I have an nVidia 5800, and have similar problems under both Linux and Windows (with the latest drivers). Basically the problems occur with conditional flow control like your example, as well as loops.

Also, from what I have read, the nVidia drivers delay compilation until linking.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.