I have created a c++ compute program using GLFW OpenGL that calculates the simple physics of gravity on particles over a pre-defined time period, saving the calculated results to a standard GL_ARRAY_BUFFER. This is setup in a standard way:
glGenBuffers(1, &vboSTATE);
glBindBuffer(GL_ARRAY_BUFFER, vboSTATE);
int bufferSize = (pCount * (4 * sizeof(GLfloat))) * (totalSteps);
glBufferData(GL_ARRAY_BUFFER, bufferSize, NULL, GL_DYNAMIC_DRAW);
The data is saved using glCopyBufferSubData to get the data transferred between GPU buffers.
Once the simulation time has passed, the results (positions) are rendered with a simple draw call as follows, looping through the recorded data in steps:
glDrawArrays(GL_POINTS, step, particleCount);
"step" being the physics step to render in the current frame. There is a pause before the rendering loop starts and this is linear in relation to the particle count. Using 1 particle, the delay seems to disappear, 100 and the delay is about 5 secs, 1000 particles and the delay is in the region of a minute or so and may even hang and crash the program.
The only thing I notice is that the buffer size is increasing with the number of particles in the simulation but it is not anything I would think of crazy - less than 1mb for 100 particles.
I had a previous program that would calculate and render in real-time within the same frame but the number of particles was limited (I am looking to get thousands at the end), but no delay was experienced. This has occured when I moved to this post-solving display style program.
Any ideas?? Thanks,
This issue was solved by using the glFinish() after each copy operation to ensure the data was actually copied. I found the solution with the help of the suggestions provided by the community here. This does increase the loop times considerably but at least you know for sure openGL is finished writing and the data is ready to use...
Thanks everyone!