the cost of loop in glsl

Started by
4 comments, last by Sik_the_hedgehog 11 years ago
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...
Advertisement
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.

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.

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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
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...

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.

This topic is closed to new replies.

Advertisement