Search code examples
c++directxhlsldirectx-10

Managing HLSL effects


Let say, for this example, that I have World class. World has collection of Models :

class World
{
    public:
      map<string, Model*> Models;
};

Model would be abstract class for all entities in application, for example car, ball, mountain, or cube.

And the question is - what's the best practice for managing effects for that models. Let say, that I've got 4 effect files : base1.fx, base2.fx, base3.fx, bas4.fx. Each of them has class interface, which might be bound to model ( IBaseEffect1, IBaseEffect2, IBaseEffect3, IBaseEffect4 ).

Now, should I consider each model independent, and do something like:

Cube1.Bind(IBaseEffect2);
Car.Bind(IBaseEffect3);

Where model would have ability to manage effect, so I would make:

World::Render()
{
    foreach model -> Render();
}

Which is actual solution for my engine.

Or better would be to manage effects independent, and do something like:

IBaseEffect1.SetAsActual();

foreach Model which should be considered for effect1 -> Render();

IBaseEffect2.SetAsActual();

foreach Model which should be considered for effect2 -> Render();

etc...

Maybe my whole point of view is wrong, and it should be made other way ?


Solution

  • I think you should use both of your methods. That is:

    1. Each model has got its own effect bound: model->Bind(effect);
    2. You manage the effects separately (for efficiency reasons):
      1. Sort models by the effects they're using
      2. For each effect:
        • Set up the effect
        • Render all models associated with the current effect

    Models which have to be rendered in a fixed order (e.g. transparent objects) should be processed in a different way unless all of them share the same effect.