In a lot of tutorials I see lines like these:
foreach(EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 3);
}
Which lead me to believe that the right way to draw a 3d mesh is the following:
- Create an effect (instance can be made once in constructor and reused).
- Update its world & projection matrix.
- Iterate its passes and draw the mesh vertices.
For example, I wrote a 3d cube class with a Draw() function that looks something like this:
_effect.World = _world;
device.SetVertexBuffer(_shapeBuffer);
foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawPrimitives(PrimitiveType.TriangleList, 0, NUM_TRIANGLES);
}
But is this the appropriate way to use effects? I’m asking because the code above seem to load the effect shaders to the GPU per object, so if I have 1000 cubes I will be uploading the shaders 1000 times? Does it make sense to work like that?
And if the answer is yes, eg it make sense to apply effect x passes per object, will I get any performance boost from grouping together objects that share the same effects, even if I apply the passes for every render anyway? And is it smart to keep the same effect instance alive and reuse it as long as the object live, or better to just create it before rendering and toss it after?
In general if there are thumb rules of whats the right way to use effects with multiple objects in MonoGame I’d love to hear more…
Thanks,