I am currently developing a voxel game and I had recently noticed that the game from startup is at around 2GB of memory. Currently I am rendering 14 x 14 chunks as shown:
I did a profile on the game and I noticed that the function call glfwSwapBuffers(...) takes 9,964 m/s to run (76.4%) and as the program's life continued it kept growing. The next function is glDrawElements() which takes 849 m/s to run (8.49%). I am unsure what is causing the bottleneck.
Going back to the high memory usage issue there are two things that seem to be causing high memory. As explained in this thread here: Voxel Game - Lag when camera is inside of the terrain I generate the terrain once and since I posted that question, I create a mesh for each chunk made up of the visible vertices. After that I send each chunk entity to my renderer and it is drawn from these two blocks of code.
for(Chunk[] chunks1D: chunks)
{
for(Chunk chunk: chunks1D)
{
//Model matrix
Matrix4f modelViewMatrix = transformation.getModelViewMatrix(chunk.getEntity(), viewMatrix);
shader.setUniform("modelViewMatrix", modelViewMatrix);
//Render mesh
shader.setMaterialUniform(chunk.getEntity().getMesh().getMaterial());
chunk.getEntity().render();
}
}
which calls this
public Matrix4f getModelViewMatrix(Entity entity, Matrix4f viewMatrix)
{
Vector3f rotation = entity.getRotation();
Vector3f position = entity.getPosition();
float scale = entity.getScale();
modelMatrix.identity().translate(position).
rotateX((float)Math.toRadians(-rotation.x)).
rotateY((float)Math.toRadians(-rotation.y)).
rotateZ((float)Math.toRadians(-rotation.z)).
scale(scale);
//Copy view matrix
Matrix4f currentView = new Matrix4f(viewMatrix);
modelViewMatrix = currentView.mul(modelMatrix);
return modelViewMatrix;
}
and this
public void render()
{
//Bind the VAO
glBindVertexArray(vaoID);
glEnableVertexAttribArray(0); //Vertex Data
glEnableVertexAttribArray(1); //Colour data
glEnableVertexAttribArray(2); //Normal data
//Draw the vertices: mode: Triangles, count, type: integers, offset
glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_INT, 0);
//Restore state
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);
}
From the profile I found two sources of high memory, one for Matrix4f which is due to the getModelViewMatrix(...) function (the memory usage grows over the programs lifespan) and another for float and float[] from the genChunkMesh(...) function as shown in my other thread (Voxel Game - Lag when camera is inside of the terrain) which is called once when a chunk is loaded. It seems as if for both cases that Matrix4f, float[] and float instances are being created by they are never released from memory. :/ I have tried looking everywhere for a solution but I can't find one... I feel my game is optimised well enough for what is being rendered and these problems shouldn't be occurring.
tldr: Simple voxel terrain gen requires high amounts of CPU and the culprit is glfwSwapBuffers which takes 9,694 m/s to run (grows over time). High memory usage also from Matrix4f, float and float[] (floats of which seem to be retained from chunk mesh generation) objects causing the game to use ~2Gb of memory from startup (14 x 14 chunks) each chunk contains its own mesh (containing a VAO which has all of the chunks visible vertices, normals and colours) and two 3D int arrays one for the chunk's blocks and one for the chunk's visible blocks (16 * 16 * 128) each storing the block ID at that location.