Jump to content

  • Log In with Google      Sign In   
  • Create Account

Design issue: managing branching in the rendering pipeline


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
12 replies to this topic

#1 irreversible   Crossbones+   -  Reputation: 1348

Like
1Likes
Like

Posted 19 January 2013 - 06:38 PM

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 :)

 

 

 



Sponsor:

#2 MJP   Moderators   -  Reputation: 11368

Like
0Likes
Like

Posted 20 January 2013 - 01:16 AM

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?



#3 irreversible   Crossbones+   -  Reputation: 1348

Like
1Likes
Like

Posted 20 January 2013 - 02:08 AM

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.



#4 MJP   Moderators   -  Reputation: 11368

Like
1Likes
Like

Posted 24 January 2013 - 04:13 PM

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!



#5 irreversible   Crossbones+   -  Reputation: 1348

Like
1Likes
Like

Posted 24 January 2013 - 08:23 PM

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.


Edited by irreversible, 25 January 2013 - 05:06 AM.


#6 mhagain   Crossbones+   -  Reputation: 7979

Like
1Likes
Like

Posted 25 January 2013 - 04:13 AM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#7 irreversible   Crossbones+   -  Reputation: 1348

Like
1Likes
Like

Posted 25 January 2013 - 05:11 AM

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!



#8 Hodgman   Moderators   -  Reputation: 30387

Like
0Likes
Like

Posted 25 January 2013 - 09:08 AM

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.



#9 Tasty Texel   Members   -  Reputation: 1295

Like
1Likes
Like

Posted 25 January 2013 - 11:11 AM

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?



#10 mhagain   Crossbones+   -  Reputation: 7979

Like
0Likes
Like

Posted 25 January 2013 - 05:37 PM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#11 MJP   Moderators   -  Reputation: 11368

Like
0Likes
Like

Posted 25 January 2013 - 06:58 PM

Almost any game with normal maps that came out in the last few years will only store the XY components, either in the G and A channels of a DXT5/BC3 texture or in the R and B channels of an ATI2N/3Dc/BC5 texture.



#12 Hodgman   Moderators   -  Reputation: 30387

Like
0Likes
Like

Posted 25 January 2013 - 08:10 PM

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?

I noticed that when you bake a tangent-space normal map out of Blender, it does take advantage of this fact -- the x/y channels represent values from -1 to +1, but the z channel only represents values from 0 to 1, gaining you a bit of extra precision. So to decode a blender tangent-space normal map, you'd use tex2D(...).xyz*float3(2,2,1)-float3(1,1,0)

Almost any game with normal maps that came out in the last few years will only store the XY components, either in the G and A channels of a DXT5/BC3 texture or in the R and B channels of an ATI2N/3Dc/BC5 texture.

I'm getting a bit off topic now, but do you know how widespread use of the Toksvig factor is? IIRC, the original paper relied on using full 3-component normals so you could measure how much their length had changed from 1.0 during interpolation/filtering (whereas reconstruction methods assume length must be 1). But I've also seen approaches where you pre-compute the Toksvig factor and bake into your spec-power/roughness maps, which would allow for 2-component normals?


Edited by Hodgman, 25 January 2013 - 08:13 PM.


#13 MJP   Moderators   -  Reputation: 11368

Like
2Likes
Like

Posted 26 January 2013 - 03:11 PM

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?

I noticed that when you bake a tangent-space normal map out of Blender, it does take advantage of this fact -- the x/y channels represent values from -1 to +1, but the z channel only represents values from 0 to 1, gaining you a bit of extra precision. So to decode a blender tangent-space normal map, you'd use tex2D(...).xyz*float3(2,2,1)-float3(1,1,0)

>Almost any game with normal maps that came out in the last few years will only store the XY components, either in the G and A channels of a DXT5/BC3 texture or in the R and B channels of an ATI2N/3Dc/BC5 texture.

I'm getting a bit off topic now, but do you know how widespread use of the Toksvig factor is? IIRC, the original paper relied on using full 3-component normals so you could measure how much their length had changed from 1.0 during interpolation/filtering (whereas reconstruction methods assume length must be 1). But I've also seen approaches where you pre-compute the Toksvig factor and bake into your spec-power/roughness maps, which would allow for 2-component normals?

 

 

Everyone I know that is doing that (or something similar) is either pre-baking the factor into their roughness maps, or making use of pixel shader derivatives to compute the normal variation on the fly.






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