increasing performance drawing thousands of cubes

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
greenman
Posts: 7
Joined: Mon Dec 30, 2019 3:43 am

increasing performance drawing thousands of cubes

Post by greenman »

So I tried drawing 4000 cubes in irrlicht using addCubeSceneNode and drawAll and it reduced the performance by a lot from lik 100 fps to 20 - 40
I tried googling it and it was pretty hard to find stuff but i found something stating that every meshbuffer would have it's own draw call and i could
improve the performance by combining the everything into one meshbuffer couldn't really find anything explaining how to do it
but after a lot of trial and error i managed to get it to work like this:

Code: Select all

 
        irr::scene::SMesh* tmesh2 = new irr::scene::SMesh();
        IMeshSceneNode* node2 = smgr_->addCubeSceneNode();
        SMeshBuffer* newMeshBuffer2 = new SMeshBuffer();
 
        for (size_t i = 0; i < meshBufferCount; i++)
        {
            SMeshBuffer* meshBuffer = (SMeshBuffer*)node2->getMesh()->getMeshBuffer(i);
            for (size_t k = 0; k < 400; k++)
            {
                for (size_t j = 0; j < meshBuffer->getVertexCount(); j++)
                {
                    S3DVertex& vertex = meshBuffer->Vertices[j];
                    vertex.Pos = vector3df(vertex.Pos.X + 100, vertex.Pos.Y + 10, vertex.Pos.Z + 10);
                }
                newMeshBuffer2->append(meshBuffer->getVertices(), meshBuffer->getVertexCount(), meshBuffer->getIndices(), meshBuffer->getIndexCount());
                tmesh2->addMeshBuffer(newMeshBuffer2);
            }
        }
        IMeshSceneNode* newNode2 = smgr_->addMeshSceneNode(tmesh2);
        newNode2->setMaterialFlag(EMF_LIGHTING, false);
        newNode2->setMaterialTexture(0, driver_->getTexture("resources/hoplitecard.jpg"));
 
        node2->remove();
 
unfortunately this made the performance a lot worse even with only 400 cubes it was significantly worse only like 10 to 20 fps
I don't know if i did something wrong or if this is just stupid and using the previous method was the best performance i can expect.
I don't really know what i'm doing since im still not that familiar with the engine or 3d rendering any help would be appreciated.
sorry for any typos my screen is beyond repair so i can't really always see what im typing.
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: increasing performance drawing thousands of cubes

Post by CuteAlien »

Sorry, not even sure if append still works, I remember that was disabled by someone at some point.
I know IrrExt had 2 implementations to merge meshes - CBatchingMesh and CMeshCombiner: https://sourceforge.net/p/irrext/code/H ... cene/IMesh
Again I don't know too much about either of them.

Irrlicht is unfortunately not particularly good at drawing lots of instances of a mesh fast. Thought 4000 should still be possible with good speed even on pretty old systems. But always can depend on stuff like size of objects and materials.

I just updated a very simple speed-test of mine so you can enable cubes in there now. Check profile_pins.cpp in here: https://bitbucket.org/mzeilfelder/irr-p ... c/default/
You can just copy the code over some example. Then set the line which says #if 1 // using some loaded mesh (a pin model)
to #if 0 instead.
It's a simple test for drawing lots of cubes in different ways. If you change the #if 0 // using nodes to #if 1 you draw with nodes, otherwise you draw all cubes in a single node (but not a single meshbuffer).
This one doesn't use textures and materials have no lighting, so it's pretty much as fast as you'll get it with Irrlicht.

For faster speed I think you'd need instancing - which Irrlicht doesn't support I think (thought I remember someone... maybe Bitplane .... coding it years ago somehow, but I don't know details).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
greenman
Posts: 7
Joined: Mon Dec 30, 2019 3:43 am

Re: increasing performance drawing thousands of cubes

Post by greenman »

thank you very much I'll start testing all this stuff i did find something about a meshcombiner before but the link was dead.
Also I'm not actually gonna be drawing cubes it was just a simple placeholder they will eventually be complicated models but i figured if i can't even get it to perform with cubes that what chance did more complicated models have to perform well.
I'm trying to make total war style game do you think something like that is possible with irrlicht.
I mean rome total war came out in 2004 and it managed pretty well with like 6000+ units at the same time i've never seen any game that does something similiar outside of the franchise.
I get that it's a pretty niche genre so i doubt there's an engine that will work without some modifications even an rts engine is generally only made for ~400 units.
So I'm fully expecting having to make some modifications to get it to work.
Anyway the mesh batching/combiner works great huge increase in performance I'll look at the code and see if can figure why my shitty hacky mesh combiner made the performance worse and these don't
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: increasing performance drawing thousands of cubes

Post by CuteAlien »

Problem is that you need all the units animated I guess. So they are not just instances of the same mesh but can be in different states. This should really be done with animation on the gpu - which Irrlicht is missing. Because otherwise with animated meshes you have 6000 mesh uploads each frame with Irrlicht.

Sorry, but I don't think Irrlicht is a good choice for this :-( You would probably have to do pretty big modifications to the engine for this.

If DevSH is around... do you have gpu animations in your engine? (edit: I just asked and he has, so checking IrrBAW might also be an option for you).

Also maybe check-out Urho, I think that one has support for hardware instancing.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
greenman
Posts: 7
Joined: Mon Dec 30, 2019 3:43 am

Re: increasing performance drawing thousands of cubes

Post by greenman »

thanks for the information I'll check it out.
Post Reply