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 2 12 LastLast
Results 1 to 10 of 11

Thread: GLSL Branching and Clamp function

  1. #1
    Junior Member Regular Contributor
    Join Date
    Jun 2009
    Location
    FL , USA
    Posts
    194

    GLSL Branching and Clamp function

    It is advisable to sparingly use branching in shader code.

    But glsl in built functions such as clamp , min or max inherently use "if" to determine final value.

    My question: If for clamp(x , 0.0, 1.0) we replace it by

    Code :
    x = (x < 0.0) ? 0.0 : (x > 1.0)? 1.0 : x;
    .

    Will this be faster than clamp() ???

    similarly, if min() and max() or step() are expanded using "if", will there be a speed slowdown? If yes, then why such built-in functions are fast?




  2. #2
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: GLSL Branching and Clamp function

    own functions will never be faster since built-in functions are optimized. you could do dot3 yourself as well but as built-in function on "dedicated" hardware it uses way less cycles to execute. always avoid writing your own code (reflect goes into same category and many others, ftransform). besides with a different GPU the speed of built-in functions might increase.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jun 2009
    Location
    FL , USA
    Posts
    194

    Re: GLSL Branching and Clamp function

    I see. it means there is some sort of hardware logic associated with in-built functions?

    For example: if clamp() is considered, it may be implemented through logic gates ... right ?

  4. #4
    Junior Member Regular Contributor
    Join Date
    Feb 2004
    Posts
    249

    Re: GLSL Branching and Clamp function

    Exactly. Clamping has been available since day one (or two...) in programmable shading hardware. Long long before branching got introduced and supported in hardware.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Jun 2009
    Location
    FL , USA
    Posts
    194

    Re: GLSL Branching and Clamp function

    thank you def and _NK47, now I understand.

  6. #6
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,261

    Re: GLSL Branching and Clamp function

    Not only clamp/min/max are implemented in hardware and do no branching, but if you have simple code like this:
    if(var1>1.33){
    var2 = 7;
    var3 = var4-5.0;
    }

    There will be no branching, either. Thanks to conditional execution (a flag specifying whether/when the instruction should be executed).
    x86 cpus have CMOVxx instructions that do the same (but are limited to "mov"), and ARM cpus have exactly the same flags on every instruction.
    Also, if real branching is done on all gpu cores at the same instruction (coherent branching), it only takes 2 gpu cycles. Coherent branching is obviously guaranteed if you loop uniform_N times. The slowness with uniform-looping comes mostly from the extra loop-preparation instructions that compilers still don't optimize well enough.

  7. #7
    Junior Member Regular Contributor
    Join Date
    Jun 2009
    Location
    FL , USA
    Posts
    194

    Re: GLSL Branching and Clamp function

    @llian Dinev:

    This is an interesting information.I never new that.
    Does special conditional flag exist for gpus like amd , nvidia?

    ok, if i use if-else pair and only "if" then will conditional execution take place for former?
    Thank you again.

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Sep 2004
    Location
    Prombaatu
    Posts
    1,401

    Re: GLSL Branching and Clamp function

    Cg's command line compiler cgc will generate an assembly listing for your inspection. Otherwise I think you're pretty much at the mercy of vendor perf documents and good old fashioned testing.


  9. #9
    Junior Member Newbie
    Join Date
    Oct 2008
    Posts
    13

    Re: GLSL Branching and Clamp function

    Do they really use if clauses ?, this is some basic math we learn at college, should this not be faster then an if clause and an compare operation ?

    max(x,y) = 1/2 * (x + y + |x - y|)
    min(x,y) = 1/2 * (x + y - |x - y|)

    someone compared original c++ max with these functions and achieved double performance with the above equations

  10. #10
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,261

    Re: GLSL Branching and Clamp function

    Ouch . No need for arithmetic like that.
    GPU hardware is not as ridiculous as a 386 cpu. The silicon logic's schematic for min/max/clamp is really easy, it's just been missing from Intel cpus until SSE came.

Posting Permissions

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