Poor performance while creating meshes manually.

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
Locien
Posts: 25
Joined: Wed Jul 10, 2013 2:43 am

Poor performance while creating meshes manually.

Post by Locien »

Hello everyone, I'm new to Irrlicht and these forums. I'm also new to graphics and game engines. I've been playing around with Ogre to make a voxel based game like Minecraft. The problem with Ogre is that it has extremely poor performance with direct polygon ray casts, the FPS literally drops from 150~ to 5~. So I figured I'd try out Irrlicht since it's getting a lot of praise for having good performance.

The problem I'm having is converting the code(note that it's not my code) on this page to Irrlicht in such a way that it'll yield good performance: http://dave.uesp.net/wiki/Block_Land_5

This is the solution I came up with:

Code: Select all

u32 NumVertices = 0;
 
void chunk::CreateMeshChunk()
{
 
    SMeshBuffer *buf = nullptr;
    buf = new SMeshBuffer();
 
    Mesh->addMeshBuffer(buf);
 
    buf->drop();    
 
    u32 Block;
 
    u32 MaxSize = 256;
 
    if(NumVertices) buf->Vertices.reallocate(NumVertices);
 
    for (u32 BlockType = 1; BlockType <= 1; ++BlockType )
    {
        NumVertices = 0;
 
        for (u32 z = MinZ; z <  CHUNK_SIZE + MinZ; ++z)
        {
            for (u32 y = MinY; y <  CHUNK_SIZE + MinY; ++y)
            {
                for (u32 x = MinX; x <  CHUNK_SIZE + MinZ; ++x)
                {
                    
                    if(GetBlock(x,y,z) != BlockType) continue;
 
 
                Block = BlockType;
                if (x > 0) Block = GetBlock(x-1,y,z);
 
                if (Block == 0)
                {
                    buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices] = S3DVertex(vector3df(x, y, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x, y+1, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x, y+1, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                    //x+1
                Block = BlockType;
                if (x < 0 + MaxSize - 1) Block = GetBlock(x+1,y,z);
 
                if (Block == 0)
                {
                    buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices] = S3DVertex(vector3df(x+1, y, z), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x+1, y, z+1), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                    //y-1
                Block = BlockType;
                if (y > 0) Block = GetBlock(x,y-1,z);
 
                if (Block == 0)
                {
                    buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y, z), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y, z), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y, z+1), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z+1), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
 
                    //y+1
                Block = BlockType;
                if (y < 0 + MaxSize - 1) Block = GetBlock(x,y+1,z);
 
                if (Block == 0)
                {
                    buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y+1, z+1), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y+1, z), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                    //z-1
                Block = BlockType;
                if (z > 0) Block = GetBlock(x,y,z-1);
 
                if (Block == 0)
                {
                    buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y+1, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
 
                    //z+1
                Block = BlockType;
                if (z < 0 + MaxSize - 1) Block = GetBlock(x,y,z+1);
 
                if (Block == 0)
                {
                    buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices] = S3DVertex(vector3df(x, y, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y+1, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                }
 
                
            }
        }
 
    }
 
    Mesh->recalculateBoundingBox();
 
}
It comes with one major problem though. It takes from one second up to twenty seconds to create the mesh of a 16x16x16 chunk with voxels in Irrlicht with this code. While in Ogre the longest time it has taken to create the mesh of a 16x16x16 chunk AND attach it to a scene node in a few tests is 24 milliseconds. I suspect I'm doing something wrong and I was wondering if someone can help me optimize the code or give me some hints towards how to do it? Note that the code functions exactly how it's supposed to, it just has very poor performance. Basically what the function does is to cull sides of the blocks that can't be seen. It is called once for every chunk that's created.

The second problem is that I get poor FPS. 30 FPS when I've rendered only 3100 out of 4096 chunks and the second thread is sleeped for 100 milliseconds constantly in a while loop. In Ogre I got around 150 FPS. I assume that Ogre automatically took care of some kind of culling or hardware settings that I should have done myself in Irrlicht. Any ideas? Here's the function that renders the meshes:

Code: Select all

void Main::RenderChunk(SMesh* Mesh)
{
 
        IMeshSceneNode* MeshNode = IrrScnMgr->addMeshSceneNode(Mesh);
        MeshNode->setMaterialFlag(video::EMF_LIGHTING, false);
        MeshNode->setMaterialType(video::EMT_SOLID);   
        MeshNode->setAutomaticCulling(EAC_FRUSTUM_SPHERE);
        MeshNode->setMaterialTexture(0, IrrDriver->getTexture("C:/Users/Zodiac/Desktop/Irrlicht/Irrlicht Engine/media/grass_1024.jpg"));
 
}
The program is structured as having one main thread which renders meshes and one thread that creates meshes and sends it to the main thread. It's mutexless, so mutexes are not behind the FPS problem.

I would be very grateful if someone could help me. Thanks!
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Poor performance while creating meshes manually.

Post by hendu »

Frustum sphere culling is not implemented in trunk. So specifying it actually disables culling. (I have a patch for that in the tracker, if you're interested)

You might also want to specify the mesh as EHM_STATIC.

For your creating function, it's impossible to say without you profiling it, but it seems like you have some faulty logic in there. You use unsigned values, yet check for 0. Your linked page uses signed ints.
Locien
Posts: 25
Joined: Wed Jul 10, 2013 2:43 am

Re: Poor performance while creating meshes manually.

Post by Locien »

hendu wrote:Frustum sphere culling is not implemented in trunk. So specifying it actually disables culling. (I have a patch for that in the tracker, if you're interested)
Sure, sounds good.
hendu wrote:You might also want to specify the mesh as EHM_STATIC.
I tried to call Mesh->setHardwareMappingHint(EHM_STATIC, EBT_VERTEX_AND_INDEX); on all meshes, but there was no significant amount of performance increase. It also makes the meshes invisible from some angles though. Weird.
hendu wrote:For your creating function, it's impossible to say without you profiling it, but it seems like you have some faulty logic in there. You use unsigned values, yet check for 0. Your linked page uses signed ints.
There is definitely some room for improvement in the conditionals and logic in the for loops, but the main problem isn't there. I tried commenting out the code that creates the meshes and the time for all of the for loops with all of the if statements clocked at 0 milliseconds.

The real problem is that this code isn't efficient enough for what I'm trying to accomplish:

Code: Select all

                    buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices] = S3DVertex(vector3df(x, y, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x, y+1, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x, y+1, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
Is there anything obviously wrong with it? I don't get it why it's on the magnitude of almost a thousand times slower than Ogre Engine. Are the methods I'm calling inefficient or something? Should I create an array of vertices and indices and push them into the index and vertex lists in one go instead of doing it one at a time?
Marthog
Posts: 31
Joined: Sun Oct 03, 2010 8:33 pm
Contact:

Re: Poor performance while creating meshes manually.

Post by Marthog »

Instead of numVertices use buf->Vertices.size() because it is much clearer what you are doing.

The arrays need to grow several times. Call realloc once and allocate the maximal possible number of vertices and indices. After the mesh generation you can shrink the array so you only have two reallocations.

You need to call buf->recalculateBoundingBox() before calling mesh->recalculateBoundingBox(). I'm sure that this causes your problems with the invisible meshes.

I also don't know how fast GetBlock is.
Rust fanboy
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Poor performance while creating meshes manually.

Post by hendu »

http://sourceforge.net/p/irrlicht/patches/261/

Probably your set_used call is slow. On the unsigned, are you absolutely sure your X, Y, and Z are never negative?
Locien
Posts: 25
Joined: Wed Jul 10, 2013 2:43 am

Re: Poor performance while creating meshes manually.

Post by Locien »

Marthog wrote:Instead of numVertices use buf->Vertices.size() because it is much clearer what you are doing.
I just figured using my own iterator would have better performance.
Marthog wrote: The arrays need to grow several times. Call realloc once and allocate the maximal possible number of vertices and indices. After the mesh generation you can shrink the array so you only have two reallocations.
I changed my code to this. Is this what you meant? I get huge lag spikes(2-5 FPS) using this code and the mesh creation process gets even slower.

Code: Select all

buf->Vertices.reallocate(65530);
    buf->Vertices.set_used(65530);
 
    for (u32 BlockType = 1; BlockType <= 1; ++BlockType )
    {
        NumVertices = 0;
 
        for (s32 z = MinZ; z <  CHUNK_SIZE + MinZ; ++z)
        {
            for (s32 y = MinY; y <  CHUNK_SIZE + MinY; ++y)
            {
                for (s32 x = MinX; x <  CHUNK_SIZE + MinZ; ++x)
                {
                    
                    if(GetBlock(x,y,z) != BlockType) continue;
 
 
                Block = BlockType;
                if (x > 0) Block = GetBlock(x-1,y,z);
 
                if (Block == 0)
                {
                    //buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices] = S3DVertex(vector3df(x, y, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x, y+1, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x, y+1, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                    //x+1
                Block = BlockType;
                if (x < 0 + MaxSize - 1) Block = GetBlock(x+1,y,z);
 
                if (Block == 0)
                {
                    //buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices] = S3DVertex(vector3df(x+1, y, z), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x+1, y, z+1), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                    //y-1
                Block = BlockType;
                if (y > 0) Block = GetBlock(x,y-1,z);
 
                if (Block == 0)
                {
                    //buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y, z), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y, z), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y, z+1), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z+1), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
 
                    //y+1
                Block = BlockType;
                if (y < 0 + MaxSize - 1) Block = GetBlock(x,y+1,z);
 
                if (Block == 0)
                {
                    //buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y+1, z+1), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y+1, z), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                    //z-1
                Block = BlockType;
                if (z > 0) Block = GetBlock(x,y,z-1);
 
                if (Block == 0)
                {
                    //buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y+1, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
 
                    //z+1
                Block = BlockType;
                if (z < 0 + MaxSize - 1) Block = GetBlock(x,y,z+1);
 
                if (Block == 0)
                {
                    //buf->Vertices.set_used(NumVertices+4);
 
                    buf->Vertices[NumVertices] = S3DVertex(vector3df(x, y, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(0, 1));
                    buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(1, 1));
                    buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(1, 0));
                    buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y+1, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(0, 0));
 
                    buf->Indices.push_back(NumVertices);
                    buf->Indices.push_back(NumVertices+1);
                    buf->Indices.push_back(NumVertices+2);
 
                    buf->Indices.push_back(NumVertices+2);
                    buf->Indices.push_back(NumVertices+3);
                    buf->Indices.push_back(NumVertices);
 
                    NumVertices += 4;
                }
 
                buf->Vertices.reallocate(buf->Vertices.size());
                buf->Vertices.set_used(buf->Vertices.size());
                buf->recalculateBoundingBox();
 
                }
 
                
            }
        }
 
    }
Marthog wrote:You need to call buf->recalculateBoundingBox() before calling mesh->recalculateBoundingBox(). I'm sure that this causes your problems with the invisible meshes.
Ah yes, of course. I was using this code earlier but for some reason I seem to have deleted it. Thanks!
Marthog wrote:I also don't know how fast GetBlock is.

Code: Select all

    for(u32 i = 0; i < 100; i++)
    {
        Block = GetBlock(i,i,i);
    }
Takes 0 ticks.
hendu wrote:http://sourceforge.net/p/irrlicht/patches/261/

Probably your set_used call is slow. On the unsigned, are you absolutely sure your X, Y, and Z are never negative?
I'll check it out. You're right, it should be signed and I changed it to s32. That doesn't make the code any faster though.

Thanks for your input though ^^
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: Poor performance while creating meshes manually.

Post by zerochen »

hi,

i guess that doesnt solve your problem but you have a typo error in that line:
for (s32 x = MinX; x < CHUNK_SIZE + MinZ; ++x)

regards
zerochen

edit: also how big is CHUNK_SIZE?
Locien
Posts: 25
Joined: Wed Jul 10, 2013 2:43 am

Re: Poor performance while creating meshes manually.

Post by Locien »

zerochen wrote:hi,

i guess that doesnt solve your problem but you have a typo error in that line:
for (s32 x = MinX; x < CHUNK_SIZE + MinZ; ++x)

regards
zerochen

edit: also how big is CHUNK_SIZE?
Of course! I can't believe I missed such a simple mistake. The speed went up about twenty-fold in some cases after I fixed it. Still it's a bit too slow since it takes up to half a second to render some chunks, which means in the worst case scenario it would take about 2000 seconds to render all of the terrain. CHUNK_SIZE is set to 16. I just made a quick iterator and printed it to the console and the loops are indeed looping exactly 4096 times(CHUNK_SIZE^3) so that's all good. The FPS problem seems to be gone too now. I'm at a steady 180 FPS when all of the chunks are rendered. There's room for optimization but it's not as bad as before.

As you can see the time it takes to create a 16x16x16 chunk is way too long seeing as I want to create 4096 of them in a short amount of time:
Image

I tried to set the amount of used vertices at the maximum in the beginning as buf->Vertices.set_used(65535); and then changing it to buf->Vertices.set_used(buf->Vertices.size()); at the end to reduce the number of times the vertice list has to be resized. This yields awful performance however, FPS drops to 2-5 and the mesh creation process is slowed by the magnitude of 10 times.

I'm just at a loss as to what to do. Does anyone have any idea how to improve on the code further? I would be very greatful. Thank you so much for the help so far as well everyone ^^
Post Reply