# deferred rendering and a few shading functions

How to use a few shading functions together with deferred rendering (for example some objects are shaded based on a lighting equation, other get a fixed color) ? I draw a full screen quad when shading (later I want to add optimizations for point lights). I have some idea how to solve that problem: during a gbuffer stage a shading function id should be saved in one of the render targets. Then we can do something like this:

if(id == 0)
{
fragColor = shadingFun0(...);
}
else
{
fragColor = shadingFun1(...);
}


What do you think about that ?

## 1 Answer

I believe having a lot of ifs in a fragment shader could have a serious performance impact. You could probably use the stencil buffer for this purpose.

Or save up to 4 light types in the color channels of a separate rendertarget and do this: fragColor = shadingFun0(...) * texColor.r + shadingFun1(...) * texColor.b and so on.

Or as a hack, you can manipulate the normals of some objects, so the light equation returns a different color.

Or just do multiple passes, one for each light type. So one deferred shading pass, and then you just draw the other objects on the resulting FBO (keeping the depth buffer, I guess).

Also I'm not sure about this, but isn't the whole point of using deferred shading that you can use the best lighting model for everything?

• I agree. Not only is branching like that defeating the point, it's probably a lot less efficient. – jmegaffin Jan 31 '13 at 22:43