control flow error ??

Hi everybody, this is my first post…

I am experimenting with fragment shaders that modify polygon shape by selectively discarding fragments, I am using texture coords to compute the position of the fragment inside a quad.
While this might not be the best method for what I want to do, this example code might behave correctly, that is discard some fragments at the left/right of the quad, and color it depending on viewing angle:


const float sqrt2 = 1.41421356237;

varying vec3 v_eye_vector;
varying vec3 v_normal;

float distance_from_middle( in float tex_s )
 {
  return abs( tex_s - 0.5 ) * 2. * sqrt2;
 }

void main()
 {
  float angle = ( dot( normalize( v_normal ), normalize( v_eye_vector ) ) );
  float dist = distance_from_middle( gl_TexCoord[0].s );
  if ( dist > 1.0 )
    discard;
  else
    gl_FragColor = vec4( angle, 0.0, 0.0, 1.0 );
 }

Color is correct, but fragments are not discarded

But if I replace last line with a random constant


gl_FragColor = vec4( 0.5, 0.0, 0.0, 1.0 );

then some fragments are actually discarded as they should be (obviously color is not what I want)

Either I didn’t understand some fundamental feature of GLSL or the GLSL compiler is buggy (“aggressive” optimizations?), can you help me?

Using gf7600go with 180.22 drivers on 64bit linux

This looks really strange at first sight indeed… Have you tried to set angle with the value 0.5 in the first code snippet? Maybe something occurs badly in the normalization, but I don’t think so.
Try to downlaod the cg compiler from the nivia developers site, and compile it using various profiles, like the arbfp1 to see if your fragment shader code comply with the arb.

strange i can reproduce the error …
if you set
const float sqrt2 = 1.0;
the code is working corectly (except the wrong dist value) … very strange

may be yo kan change the Compare value? It is a vary bad hack :slight_smile:

It turns out (quite surprisingly), that changing


  if ( dist > 1.0 )
    discard;
  else
    gl_FragColor = vec4( angle, 0.0, 0.0, 1.0 );

into the equivalent


  if ( dist > 1.0 )
    discard;
  gl_FragColor = vec4( angle, 0.0, 0.0, 1.0 );

(removing else, because code after discard has no effect) actually produces the expected correct result!!!

If anybody knows a good reason for this behaviour, please tell me, otherwise I shall fill a bug report against nvidia compiler…

aus Nvidias GLSL release Notes:

and

so the 7000 go is G7x gpu but full suport auf brancing and looping is first fith g80 …

So…the compiler doesn’t really support it on some hardware. Does it at least give a warning in the info log? If it doesn’t at least do that, I’d still submit a bug report.

I use RenderMonkey (1.71) with a GeForce 6xxx an get no warning.