Results 1 to 3 of 3

Thread: shader efficiency

  1. #1
    Member Contributor
    Join Date
    Jul 2018
    Posts
    63

    shader efficiency

    Hi

    I have read a few posts online and a powerpoint by Nvidia that state that if you use if-statement branches in the shader, it slows things down.
    In particular, using a if statement on something like the threadid would be a no-no.

    Since a threadid would be composed of things like gl_WorkGroupID and gl_LocalInvocationID, I would be correct to assume that the same restriction on having if statements would apply to these.Am i right to make such an assumption?


    So to implement something akin to the pseudo code below, how could one go about to do it efficiently on a compute shader?

    //if in a particular spot in the 3d grid (using gl_WorkGroupID and gl_LocalInvocationID
    //do something
    //else
    //do nothing

    I have also read some contradictory online posts where modern gpus are able to handle if statements because they effectivey execute both branches (by masking, only relevant is executed) . So branching is not supposed to be very costly.

    i have a gtx570 GF114 archittecture - is this considered to be a modern gpu?
    thanks

  2. #2
    Senior Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    281
    The problem with branches on GPUs is that GPU architectures typically have groups of processing cores with a central programm memory and instruction decoder per group.

    An instruction is loaded from memory and decoded and the decoder lines are connected to a lot of cores that have an ALU, register set and local data memory. One instruction is executed by a lot of cores in parallel, working on different data, which is really really good for triangle rasterization or other data parallel tasks.

    When a branch comes along in the code, that depends on local data (or anything that could potentially be different for different cores), the program flow somehow has to split if the branch condition evaluates different for the individual cores. I don't really known how graphics cards handle that (split the program to two groups perhaps?), but it costs a lot of performance.

    What you can use those ID values for is to fetch data depending on the ID of the core, so every core fetches different data from a buffer while executing the same fetch instruction in parallel.
    So your "if in particular spot in grid" is basically "compute grid position from IDs; compute buffer offset from grid position; fetch; do something interesting".

    If your problem cannot be solved by an algorithm that reads elements from a stream of data and generates elements in an output stream of data (where elements can be grouped in a way that they can be processed independendly), then current GPU architectures probably won't help you make it faster.

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,111
    Quote Originally Posted by driver View Post
    I have also read some contradictory online posts where modern gpus are able to handle if statements because they effectivey execute both branches (by masking, only relevant is executed) . So branching is not supposed to be very costly.
    What makes branching "costly" is that the GPU typically executes both branches.

    Modern GPUs can actually skip over one of the branches if the condition is the same for all invocations within a workgroup. But that isn't likely to be the case if you're using gl_LocalInvocationID in the condition.

Similar Threads

  1. VBO Efficiency
    By bcthund in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-20-2011, 12:34 PM
  2. Is there anything more I can do for efficiency?
    By imported_tobindax in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-22-2011, 05:47 AM
  3. Blending in fragmen shader and efficiency
    By Godlike in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 10-30-2009, 05:39 AM
  4. Efficiency
    By dt in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 10-27-2009, 04:49 AM
  5. Efficiency in C++
    By dt in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-14-2005, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean