One shader that does everything or multiple shaders?

Hi all,

I was wondering what could be the best solution performance-wise:

> to use one shader which does my blinn-lighting OR bump-mapping, according to a parameter, or
> to use one shader per specific use-case, keeping in mind changing shaders at render-time is really time-consuming?

I guess you’re all going to tell me “render the blinn faces first, change your shader, then render the bump ones”, but my file-format provides objects in which there are faces, some maybe blinn and some bump… So I can’t definately sort them by “shader-type”.

What could be the most interesting solution?

Thanks! :slight_smile:


If the file format tells you which faces have a certain shader, how can you NOT sort by shader?

Because I don’t actually loop on the faces, but on the objects, each containing a list of faces…

And you can’t transform the object into a data structure that allows you to do it?

Originally posted by hardtop

> to use one shader which does my blinn-lighting OR bump-mapping, according to a parameter, or

The OR part won’t serve much purpose if you want to support hardware that does not provide dynamic branching support, in which case the entire heavy shader will be executed.
I would really advise you to sort if your meshes can contain triangles that need to be rendered with different shaders (which is not a recommended scenario altogether). Supposing that you will be rendering multiple meshes in any given scene and that you can extract material information from each mesh then i would recommend batching triangles together in the form of triangle lists and rendering them in one go with a particular shader. For example you can batch all the faces of all the meshes using shader A and render them in one go. Similarly you can batch all the triangles of all the meshes using shader B and render them in one go. Although the technique can be restrictive for a relatively general purpose scene graph such as those used in game engines, it can work well enough for smaller, more focused scene graphs.

Yes, I know that the OR part will still execute the whole shader on pre-SM3.0 boards, such as mine.

So you all mean the main idea is to (basically) loop through all the scene, render faces with shader A, then re-loop through all the scene, and render faces with shader B?

This is a first idea, I guess if I do this, I’ll have to perform some optimizationto reduce CPU consumption because of the loops. But could this be the point?

Thanks :wink:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.