Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Is the if-statement particularly slow ?

  1. #1
    Junior Member Regular Contributor
    Join Date
    Apr 2003
    Location
    France
    Posts
    114

    Is the if-statement particularly slow ?

    Hi !

    I tried different things on Humus' Portal demo to see what i could do with GLSL.

    i replaced the fragment shader code by this one :

    void main(){
    vec4 base = texture2D(Base, texCoord);

    float distSqr = dot(lightVec, lightVec);
    float atten = clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0);

    float diffuse = 0.0;
    float specular = 0.0;

    if(atten != 0.0){

    vec3 bump = texture2D(Bump, texCoord).xyz * 2.0 - 1.0;
    bump = normalize(bump);

    vec3 lVec = lightVec * inversesqrt(distSqr);


    diffuse = clamp(dot(lVec, bump), 0.0, 1.0);
    specular = pow(clamp(dot(reflect(normalize(-viewVec), bump), lVec), 0.0, 1.0), 16.0);

    }

    gl_FragColor = ambient * base + (diffuse * base + 0.6 * specular) * atten;
    }

    but the frame rate dropped by 10. I though light radius may be large enough to get every fragment lit, thus i added the original shader code the if-statement workload.

    So i divided the light radius by 2 (multiplied the invRadius variable by 2) and then it was clear fragment should not be taken care of, but the frame rate stayed at around 90, while it was 105 initialy

    thx in advance
    wizzo
    You snooze, you loose

  2. #2
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: Is the if-statement particularly slow ?

    If you don't use a graphic card which supports Shader Model 3.0 (GeForce 6800) this if won't give you any performance boost. This is because both pathes of an if-statement (the if-path and the else-path) have to be computed. After that the GPU decides which values should be taken, the ones from the if-path or the ones from the else-path. Pre-SM3.0 hardware isn't able to decide which code to execute.
    So back to your example: Unless you use a GeForce 6800 the code within the if-path will be exectued. It doesn't matter if atten == 0 or atten != 0. So you won't get any extra performance.
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2003
    Location
    France
    Posts
    114

    Re: Is the if-statement particularly slow ?

    alright ! this does explain why
    too bad i just bought a Radeon 9800 =)

    thx for your answer
    wizzo
    You snooze, you loose

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2002
    Posts
    3

    Re: Is the if-statement particularly slow ?

    Wizzo, did you tried Humus implementation of
    "if" statement for SM2 ?

    http://www.beyond3d.com/forum/viewto...094b4e04a55392

  5. #5
    Junior Member Regular Contributor
    Join Date
    Apr 2003
    Location
    France
    Posts
    114

    Re: Is the if-statement particularly slow ?

    well, no i didn't, but now that i sneaked a peek, i'm definetly gonna try this.

    but i can't right now, because MY engine doesn't support any shading language yet, i was just playing around with one of his' demo =)

    thanks for the tip anyway jpeter,
    wizzo
    You snooze, you loose

  6. #6
    Advanced Member Frequent Contributor yooyo's Avatar
    Join Date
    Apr 2003
    Location
    Belgrade, Serbia
    Posts
    883

    Re: Is the if-statement particularly slow ?

    About branching... It seems that NV40 have only one instruction pointer (Im not sure for this) for all 16 pipes. Can someone with NV40 test this? For exmple, make texture:
    Code :
    oxxx... (repeat this block)
    xxxx...
    xxxx...
    xxxx...
    .......
     
    and another texture
    oooo...
    oooo...
    oooo...
    oooo...
    .......
    then draw a fullscreen quad (map texel to pixel)
    with some fragment shader like:
    Code :
    ...
    vec4 col = texture2D(mask, uv);
    if (col.r == 'o') gl_FragColor = DoVeryExpensiveCalculation();
    else discard;
    ...
    and check speed for both textures.

    Current drivers doesn't support SM3.0 in GLSL, so for this test should use ARBFP.

    yooyo

  7. #7
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: Is the if-statement particularly slow ?

    AFAIK does the NV branching work on a block of pixels. Take a look at the posting of tb at
    http://www.forum-3dcenter.org/vbulle...39#post1954339
    Warning, this is a german forum site.
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

  8. #8
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,444

    Re: Is the if-statement particularly slow ?

    Originally posted by jpeter:
    Wizzo, did you tried Humus implementation of
    "if" statement for SM2 ?

    http://www.beyond3d.com/forum/viewtopic.php?t=13716&sid=7c7fd4b25cf6b6e623094b4e 04a553 92
    While the technique is interesting, that thread you linked to went straight to hell. There are some interesting technical discussion though squeezed in between the flaming ...

  9. #9
    Junior Member Newbie
    Join Date
    Jul 2004
    Location
    Australia
    Posts
    20

    Re: Is the if-statement particularly slow ?

    Humus, your work is amazing. Don't let anyone flaming you (not here but elsewhere) put you off, keep up the good work.

    BTW the dynamic branching doesn't seem to work with my NV5900 (forceware 62.71).

    Is there something I should do to help it run properly?

  10. #10
    Member Regular Contributor
    Join Date
    Apr 2002
    Location
    Austria
    Posts
    328

    Re: Is the if-statement particularly slow ?

    The GeForce FX series isn't able to do dynamic branching in pixel shaders. You won't get it work on your card. But you can either buy a new graphic card or use a software rasterizer (NVemmulate).
    There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.

    There is another theory which states that this has already happened...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •