Design issue: managing branching in the rendering pipeline

Started by
11 comments, last by MJP 11 years, 2 months ago

My current shading solution is relatively simple - the idea is to provide two generic paths that are toggled by branching in the shader:

- ShadeFull(), computes all terms and by default does specular & texture lookups, even if the maps are unbound or empty; multitexturing is branched to up to 2 TUs

- ShadeBasic(), does basic lighting, but doesn't do any other lookups other than basic texturing

Things get a lot more interesting, however, when adding support for full multitexturing and effects like detail and bump mapping. These are really easy to do as branches in the shader, but could also be branched on the CPU by creating separate shaders for major pipelines. Since I'll be generating the main shading component from pre-written modules on the fly anyway, it wouldn't be overly difficult to implement this either with minimal impact on maintenance.

Sorting geometry based on any number of surface properties on the CPU isn't going to be an issue, since I'll be making use of a low poly environment with tessellation. However, at the end of the day I'm having trouble coming up with a balanced and robust approach.

As there are plenty of industry-savvy people around to have tackled these choices before, I'd appreciate some insight :)

Advertisement

I can give some advice, but it depends on what level of hardware you're targeting. Do you have any idea what your target spec is for your game/engine?

OpenGL 3.0/GLSL 1.3, I'd like the game to run smoothly on GF2xx series laptops. This is the realistic target, although I do keep my eyes open for even lower specs.

Sorry for the delay. In terms of performance of a single shader, statically compiling different permutations of that shader will always win over having a runtime branch. When you use a branch the compiler will need to allocate registers for any code inside of the branch, and those additional registers can limit your shader occupancy even if the branch is never taken. There is also some cost associated with executing the branch instruction at all, although this should be fairly minimal for the hardware that you're targeting.

In practice, you'll probably need to find a balance between permutations and branches. Adding permutations for everything tends not to scale too well if you add too many, since you can spend a lot of time just compiling them all (this can be especially problematic when using OpenGL since you can only pre-compile shaders for a given hardware/driver combination). You can also hit additional overhead from switching shaders too many times during a frame. So in general I would say you should use permutations when you need to, and use branches when you can get away with it. The best candidates for using permutation are:

1. Complex features that could increase register pressure

2. Features that are difficult to localize inside of a single branch

3. Features that require the vertex shader to output additional values (since you can't avoid this with branching, and additional VS outputs can degrade performance even if the fragment shader doesn't use them).

Hope this helps!

Hey - thanks for the reply!

Can you give some more specific advice on, let's say, implementing material system, though? Based on your ideas I've laid out a specific example of minimally branched permutations for basic setups like:

Diffuse + Specular

Diffuse + Specular + Normal

Diffuse + Normal

With branches for :

[ + Emissive ]

[ + Opacity/Mask ]

Fork the above permutations to create duplicates that also make use of:

+ Lightmap

as lightmaps generally use a different set of UVs.

If the material uses only multitexturing/texture blending (assuming up to 3 texture units), the above would fork again, creating (up to):

3 base versions [with 2 branches] * 2 (lightmap) * 2 (2 TU multitexture) * 2 (3 TU multitexture) = 24 permutations [with 2 branches each]

In addition to this, all "custom" textures (eg animated or otherwise requiring a modified shader) get their own permutation.

In terms of design, does this look "like it"?

I guess based on your reply, I'm just not sure how to test this balance. Furthermore, on a practical note - I'm developing on the go on a GL4.2 capable laptop (GF 640M) and I genuinely don't have access to my target hardware to test out different combinations.

It may be instructive to look at how this kind of problem was solved in the past - Doom 3. There a decision was made that every lit surface has the same combination of shaders with the same inputs and outputs. If the surface didn't have a normal map then a generic one was provided, likewise with specular/etc. This could be as basic as providing a 1x1 texture procedurally generated during startup.

True, it targetted a different class of hardware and branching wasn't possible in the shading language it used, but if there are some of your current branch/permute cases which you can use this approach on (your diffuse/specular/normal basic setup seems a candidate, for example) it may help with keeping things more manageable.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Dummy inputs is actually something I've been wondering about. Thing about them is that regardless of the size of the texture and the fact that textures that are using dummies can be collated, the number of texture lookups won't change. Although it does make a lot of sense for diffuse/normal/specular as most textures can pretty much be assumed to have all three (and if they don't, the assumption won't affect speed expectations).

PS - I didn't know D3 was as generalized as this!

Using default textures is an interesting option -- despite the number of texture fetches being the same, fetching from a 1x1 texture will be faster on average (than fetching from a large texture), because every pixel is fetching the same value, meaning it will very likely be cached.

If you happen to be doing your lighting in tangent-space (which I admit, is rarer these days), then normal mapping vs not normal mapping has no cost difference either (besides the texture fetch).

One issue that I've had with it though, is that the common way of decoding normals from a texture doesn't allow you to ever represent a perfectly flat surface (i.e. a normal of [0,0,1]).

The usual process of decoding a normal map is ((float)8bitTexValue/255.0f)*2-1, and if you put 0 through the reverse of that, it's encoding is 127.5, which either gets rounded to 127 or 128, slightly tilting your normal map in one direction or the other... it's so small that you might not notice, but I have seen it reported as a bug in an engine that followed this design.

But tangent space normal maps naturally hold only z-values in the range [0..1]. I wonder why it is so common to compress the z-value also (and therefore waste precission). Is it necessary for texture compression?

It's a tradeoff - if you compress all of the RGB values in the same way, in the old days it would be faster to decompress them all in your shader; nowadays you may even be able to get away with just storing 2 values (at the cost of some extra math ops shader-side) or using something like a 10/10/10/2 format (those extra 2 bits of precision can make a huge difference).

Many of the hacks in Doom 3 should really come with a health warning that they were a valid tradeoff in the days of 128mb gfx cards, but that these days burning the extra video RAM may be a better choice. At the same time, the generalization of surfaces and use of default textures still seems like a good approach.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement