Jump to content

  • Log In with Google      Sign In   
  • Create Account


the cost of loop in glsl


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Triad_prague   Members   -  Reputation: 136

Like
0Likes
Like

Posted 06 April 2013 - 06:29 AM

Hi all I plan to use hardware skinning, and thus I need to use loop inside the vertex shader. Last time I heard ut was bad to use loop inside shader and that I'd better unroll it. Also I heard looping and branching was badly supported. Is it true? any keyword for looking such info? thx
the hardest part is the beginning...

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 24014

Like
5Likes
Like

Posted 06 April 2013 - 06:37 AM

You don't need a loop to implement vertex skinning - usually you just process your maximum bones per vertex, e.g. 4.
The cost of a loop depends greatly on the GPU itself, rather than GL. Older GPUs will have very high overheads for any kind of branching, while newer ones will be a bit more forgiving. Also, generally if many different items being shaded at once (pixels/verts/etc) all take different paths (if vs else, different number of loop iterations) then those branches will be more expensive than if every item too the same path.

#3 Erik Rufelt   Crossbones+   -  Reputation: 2791

Like
1Likes
Like

Posted 06 April 2013 - 07:08 AM

I plan to use hardware skinning, and thus I need to use loop inside the vertex shader

 

Hardware skinning is usually done without loops by always skinning for a static number of bones per vertex, and using weights of 0.0 when less than that are needed for a particular vertex.

In general branching and looping on variables are bad, while looping over constants will be unrolled automatically by the shader compiler.

 

 

If you haven't written a skinning shader before, then start by writing one that works on your development system, and worry about support on other platforms later. If you know how to do it with loops then do it with loops, and then post the code here and people can comment on it.

Finding the information you need and iteratively changing your method will be easy once you've written one that works.

 

If you need information on how to start, then post information on your GLSL version and hardware so we can recommend a tutorial.


Edited by Erik Rufelt, 06 April 2013 - 07:10 AM.


#4 slicer4ever   Crossbones+   -  Reputation: 2602

Like
0Likes
Like

Posted 06 April 2013 - 08:02 AM

Erik Rufelt, on 06 Apr 2013 - 09:12, said:
If you haven't written a skinning shader before, then start by writing one that works on your development system, and worry about support on other platforms later. If you know how to do it with loops then do it with loops, and then post the code here and people can comment on it.

I do agree with first building a working version on your dev pc, before worrying about performance. However I just wanna toss my experience with writing dynamic loops on older gpu's. Don't, as i've had the gpu "optimize" the loop by unrolling it to the first value specified as the stop point. I.E. if you do this:
 
in int NbrBones;
void main(){
  for(int i=0;i<;NbrBones;i++)[
   //Do stuff.
  }
};
the gpu decided to unroll the loop to w/e my first value of NbrBones was, and had me scratching my head for a good while. Knowing this, i've tried to stay away from dynamic loops for the most part.

Edited by slicer4ever, 06 April 2013 - 08:03 AM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#5 Triad_prague   Members   -  Reputation: 136

Like
0Likes
Like

Posted 06 April 2013 - 10:00 PM

to all, thx for the suggestion. I tried to gove +1 to all your comments, but I'm on mobile :(
anyway, I plan to use gl 2.0 to support a lil bit older hardwarr, and I heard the number of uniform is limited to 256*vec4. so I plan to pass quaternion as vec4 and position as vec3. I havent written the shader loader yet, no access to pc. I'm basically imprisoned in this barrack for the next 3 weeks. I'm curious if my quaternion multiplication will be faster than the built in mattix multiplication. I hope it would be.
the hardest part is the beginning...

#6 Sik_the_hedgehog   Crossbones+   -  Reputation: 1411

Like
0Likes
Like

Posted 07 April 2013 - 02:06 PM

the gpu decided to unroll the loop to w/e my first value of NbrBones was, and had me scratching my head for a good while. Knowing this, i've tried to stay away from dynamic loops for the most part.

Nvidia drivers were known for doing stuff like this. Apparently this was a serious issue whenever an uniform was set to 0 or 1 because the driver would attempt to recompile the shader optimizing for that. No idea how much truth was in this, but yeah it isn't just loops the issue.


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS