• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
irreversible

Design issue: managing branching in the rendering pipeline

12 posts in this topic

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

 

 

 

1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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!

1

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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!

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0