Skip to main content
GameDev.net gamedev.net
🔒 Locked

"Switching" bump mapping on and of

Started by Wilhelm van Huyssteen Sep 7, 2010 at 2:17 AM 5 replies 900+ views
Original Post
Wilhelm van Huyssteen
Wilhelm van Huyssteen
Hi.

If i had some surfaces that had bump mapping and some surfaces that dont how should i handle both of them in the same shader.

Currently i do so by using a flag. i sent a integer as a uniform and i branch the flow of the shader based on its value (0 or 1). but as i understand this is far from optimal.

Instead i thought i should always keep bump mapping "on" and then create "default" tangent and bitangent vertex attributes for the vertices that should not be bump mapped while also using a 1x1 "default" bump texture that will have no effect on the end result.

Does such "defualt" values exists? if not show should i do it?

Thnx in Advance!
YoYoFreakCJ
YoYoFreakCJ
AFAIK branching is no good in shaders. Instead, if you want to disable bump mapping, I would use a white or black texture, so bump mapping is actually applied, but with a map that doesn't contain any bump mapping data.
Or, if you want to use a "switch" variable in the shader, you could multiply the value of that variable [which should be 0 or 1] with the interpolated value of the bump map.

Hope I could help. Because I never actually applied such shaders, this post is just my theory :D
macnihilist
macnihilist
If it just has to be 'the same code' and not 'the same shader' you could of course use a define and solve the problem with conditional compilation. I think this is really the best solution.

With really one shader you could always contruct an orthonormal tangent frame for each vertex of your mesh (even without texture coordinates) an pass in these.
If you use only a dummy normal map (normal always 'up', tangent direction doesn't really matter except they have to be perpendicular to the vertex normal) it's even possible to do this in the vertex shader (but unnecessary slow).

Another way would be to drop the tangents as vertex attributes and calculate them in the fragment shader with the dFdx/dFdy instructions (or whatever they are called in OpenGL). But I'm pretty sure this is slower than a branch with a uniform bool.
SuperRad
SuperRad
Branching in shaders is only a big deal if your shader involves nearby pixels taking different branches.
In your case all the pixels shaded for the one object you'd be drawing would all take the same branch as you're only changing the variable effecting the conditional statement on a per object/draw call basis.
Cutting out the normal map calculation and the texture read to the normal map might actually be quite beneficial.
To be honest you should really test both ways to see which offers you the best performance, but only do this if you really need the extra performance as otherwise its a waste of your time.
Wilhelm van Huyssteen
Wilhelm van Huyssteen
Quote:
Original post by SuperRad
Branching in shaders is only a big deal if your shader involves nearby pixels taking different branches.
In your case all the pixels shaded for the one object you'd be drawing would all take the same branch as you're only changing the variable effecting the conditional statement on a per object/draw call basis.


Thnx thats very good to know!

Hodgman
Hodgman
Quote:
Original post by SuperRad
Branching in shaders is only a big deal if your shader involves nearby pixels taking different branches.
Yeah, blocks of pixels that take diverging paths can be very expensive, but other branches still do add cycles. On older cards the branch may still be equivalent to a dozen arithmetic instructions.
However, on other cards, switching from one shader to another is an expensive operation...
...so the choice of whether to remove functionality by changing shaders, or by changing a uniform (and branching) can be a difficult choice, and possibly even depends on the generation of card that you're targeting.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.