I was trying to render a point cloud (>100 million points), I transferred all the data to GPU and just use the buffer name to access it. Everything works fine until I implemented a feature that requires me to transfer all the data back to the RAM.
This is how I pass the data to the GPU:
glBindVertexArray(vao);
// some data
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * GetSize() * 3, &vertices[0], GL_STATIC_DRAW); // pos
// some attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); // pos
...
// some other code
...
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
// Clear the contents in the RAM
vertices.clear();
vertices.shrink_to_fit();
Then, I needed a function at some moment to bring back the data in order to save the changes, this is what I did:
// restore data back to RAM
// this code was called for >4000 times
glBindBuffer(GL_ARRAY_BUFFER, c.vbo);
c.vertices.clear();
c.vertices.resize(c.sizeg);
glGetBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3) * c.sizeg, &c.vertices[0].position.x);
After that, the program will hang everytime I need to render that point cloud.
I removed all other codes to test just this code, and this piece of data transfer code is exactly what caused the lag
It feels like there is a huge amount of data copy inside the GPU each time I try to render the data that caused it to hang. However, after I transferred back the data from GPU to RAM, the data that was originally inside the GPU stay untouched, I thought the rendering should be just as smooth as before but it is not.
Is something changed in the OpenGL state that caused this lag?