How to batch

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
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

How to batch

Post by whythishard »

How do you batch IMesh's? There is no concrete example on the forum or on google, just posts about "You need to batch!", "Batching will solve that!", "Draw everything in one draw call by batching!", etc. I found https://sourceforge.net/p/irrext/code/H ... ene/IMesh/ that contains CBatchingMesh that some people reference but I am not yet skilled enough to just wing it and use it properly. Can anybody explain and give a concrete code on how to batch?

My problem: I have got about 10 000+ 3d planes, putting me on about 45 FPS, enabling EHM_STATIC by doing plane->setHardwareMappingHint(EHM_STATIC) bumps up the FPS to about 50 FPS, but I want to have a stable 60 FPS even on a more worse computer.
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to batch

Post by CuteAlien »

100000 nodes are indeed a problem as that creates a lot of tiny draw calls which are far more expensive than a few big ones.

The best solution is generally to write your own SceneNode and create your own geometry. You likely want to do more than just have plain planes and for example also put textures on them. And probably not always the same one. In which case you want to also sort the planes into meshbuffers which share the same texture (because if you flip between different textures all the time your speed goes down again).

With CBatchingMesh - first you have to add the source and header file to your project.
Then create a CBatchingMesh with new. Then you can for example create a Mesh with IGeomatryCreate::createPlaneMesh and add that one to the CBatchingMesh with it's add addMesh function. In the end create a MeshSceneNode with the CBatchingMesh.
Note that I have not that much experience with it's features... but I think basically just puts all meshbuffers which have the same material into a single meshbuffer.
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
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

Re: How to batch

Post by whythishard »

I tried creating a CBatchingMesh but using the addMesh function requires that you know the rotation and the scale of the mesh, which I cannot find in the IMesh object. There is no ->getRotation() nor a ->getScale() / ->getSize(), etc... any ideas?

Edit: whenever I try to apply the rotation, scale and position by using the ISceneNode as a reference I get the following error:

Code: Select all

undefined reference to 'irr::scene::CBatchingMesh::addMesh(irr::scene::IMesh*, irr::core::vector3d<float>, irr::core::vector3d<float>, irr::core::vector3d<float>)

despite it being the correct type and all. I add the CBatchingMesh header file on the top of the main.cpp. I have the CBatchingMesh.cpp in the same folder, as well as the header file so finding the files should not be an issue, not does it seem to be. Any ideas on why when I look at the addMesh function inside the header and I pass an IMesh* and three vector3df I get the above error?
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

Re: How to batch

Post by whythishard »

Managed to solve my problem by doing the following

Code: Select all

 
#include "CBatchingMesh.cpp" // NOT the header file
...
code
...
CBatchingMesh *BatchingMesh = new CBatchingMesh(); // no error with .cpp file, error with .h file
 
for(int x = 0; x < 10000; x++) {
    irr::scene::IMesh* plane = geomentryCreator->createPlaneMesh(irr::core::dimension2d<irr::f32>(5, 5), irr::core::dimension2d<irr::u32>(1, 1));
    BatchingMesh->addMesh(plane, core::vector3df(5*x, 0, 5*z)); // z is another for loop inside the x for loop
}
 
BatchingMesh->update();
BatchingMesh->setMaterialFlag(EMF_LIGHTING, false);
 
ISceneNode *node = smgr->addMeshSceneNode(BatchingMesh);
node->setScale(core::vector3df(1, 1, 1));
node->setDebugDataVisible(EDS_BBOX_BUFFERS);
BatchingMesh->drop();
 
and my frame, despite having Vsync on, bumped up from 45 - 50 fps to 80 fps!
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to batch

Post by CuteAlien »

OK, this is more about c++. What you got is a linker error. You probably didn't put the other .cpp file into your project.
Headers (or the class and function declarations in them to be more exact) allow you to use code which is implemented in other .cpp files. But you need to have the other .cpp file in your project as well so it's also compiled and can be linked together later by the linker.
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
whythishard
Posts: 9
Joined: Tue Feb 25, 2020 1:53 pm

Re: How to batch

Post by whythishard »

I did have the .cpp file in the same directory as the project. I also had the header file in the project folder. Both the CBatchingMesh.cpp and CBatchingMesh.h was in the same folder as the main.cpp file. I don't know why it gave me that error haha
CuteAlien
Admin
Posts: 9652
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to batch

Post by CuteAlien »

It's not about where you place the file. It's about what your compiler&linker get as input. You have to add the files to your project. How you do that depends on which build-system (like Makefiles) or IDE (like Visual Studio or Code::Blocks) you are using.

Assuming you use VisualStudio you add the files in the Solution Explorer to your project. You can right-click beside the project name in there (the main.cpp should already be below it) and then click "add" - "existing item". Then you can select your file.
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
Post Reply