I have a problem with a simple game loop. As my rendering and iteration functions together take 10 ms, the time between update and swapBuffers measuers around 140 ms. What is causing this. Is there a straightforward way to fix this.
Could it be related to the fact that each rect() (in a for loop) calls glDrawElements() and it somehow ques the whole process.
void game::startup()
{
startup();
glfwSwapInterval(1);
}
void game::render(double currentTime)
{
static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };
static const GLfloat one = 1.0f;
glClearBufferfv(GL_COLOR, 0, green);
glClearBufferfv(GL_DEPTH, 0, &one);
auto t1 = std::chrono::high_resolution_clock::now();
for (size_t y = 0; y < game_board.getWorldHeight(); ++y) {
for (size_t x = 0; x < golf_board.getWorldWidth(); ++x) {
rect(x * cell_size, y * cell_size, cell_size, cell_size, game_board.get_state(y * game_board.getWorldWidth() + x));
}
}
auto t2 = std::chrono::high_resolution_clock::now();
game_board.iterate(1);
auto t3 = std::chrono::high_resolution_clock::now();
float rendertime = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count() / 1000.0f;
float generatetime = std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2).count() / 1000.0f;
std::chrono::milliseconds ms_perframe(1000 / 60); // About 60 fps
auto cur_time = std::chrono::high_resolution_clock::now();
float sleep_time = std::chrono::duration_cast<std::chrono::microseconds>(t1 + ms_perframe - t2).count() / 1000.0f;
std::this_thread::sleep_for(t1 + ms_perframe - t3);
cout << "render time (ms)" << rendertime <<"\n"
<< "generate time (ms)" <<generatetime << "\n"
<< "sleeping time (ms)" << sleep_time << "\n";
}
Output (board size: 256 x 128 = 32768 rectangles drawn per frame):
render time (ms) 8.1
generate time (ms) 1.3
sleeping time (ms) 6.6
SWAP TIME (ms) 146
application run function rendering part:
startup();
do
{
render(glfwGetTime());
auto t1 = std::chrono::high_resolution_clock::now();
glfwSwapBuffers(window);
auto t2 = std::chrono::high_resolution_clock::now();
float swap_time =
std::chrono::duration_cast<std::chrono::microseconds>(t2-t1).count() / 1000.0f;
std::cout << "SWAP TIME (ms)" << swap_time << "\n";
glfwPollEvents();
running &= (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_RELEASE);
running &= (glfwWindowShouldClose(window) != GL_TRUE);
} while (running);
shutdown();
glfwDestroyWindow(window);
glfwTerminate();
rectangle draw function:
rect(int x, int y, int w, int h, int state) {
// input is top left corner of rectangle, width and height of rectange
and the state of unit
GLfloat fstate = (float)state * 0.5f;
GLfloat vertices[] = {
x, y, fstate, // Top-left
x + w, y, fstate, // Top-right
x + w, y + h, fstate, // Bottom-right
x, y + h, fstate // Bottom-left
};
glBufferSubData(GL_ARRAY_BUFFER,0, sizeof(vertices),vertices);
glUseProgram(shaderProgram);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, mvp);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
glfwSwapInterval(1);
toglfwSwapInterval(0);
and let us know what effect, if any, that has. \$\endgroup\$ – Dan Aug 23 '17 at 18:15