move render thread to App class and add ui thread with ui tasks queue

This commit is contained in:
2019-07-09 01:07:13 +02:00
parent f7ead8e157
commit cb6744be44
6 changed files with 239 additions and 194 deletions

View File

@@ -1019,3 +1019,123 @@ void App::rec_loop()
}
}
}
void App::render_thread_main()
{
uint32_t count = 0;
render_thread_id = std::this_thread::get_id();
render_running = true;
while (render_running)
{
std::deque<std::packaged_task<void()>> working_list;
// move the task list locally to free the queue for other threads
{
std::unique_lock<std::mutex> lock(render_task_mutex);
render_cv.wait(lock, [this] { return render_tasklist.empty() && render_running ? false : true; });
working_list = std::move(render_tasklist);
}
// execute the tasks
if (!working_list.empty())
{
async_lock();
while (!working_list.empty())
{
//LOG("render task %d", count);
count++;
working_list.front()();
working_list.pop_front();
}
async_unlock();
}
}
}
void App::ui_thread_main()
{
uint32_t count = 0;
ui_thread_id = std::this_thread::get_id();
ui_running = true;
auto t_start = std::chrono::high_resolution_clock::now();
float t_frame = 0;
float t_fps_counter = 0;
int rendered_frames = 0;
while (ui_running)
{
std::deque<std::packaged_task<void()>> working_list;
// move the task list locally to free the queue for other threads
{
std::unique_lock<std::mutex> lock(ui_task_mutex);
ui_cv.wait(lock, [this] { return ui_tasklist.empty() && ui_running ? false : true; });
working_list = std::move(ui_tasklist);
}
// execute the tasks
if (!working_list.empty())
{
while (!working_list.empty())
{
//LOG("ui task %d", count);
count++;
working_list.front()();
working_list.pop_front();
}
}
auto t_now = std::chrono::high_resolution_clock::now();
float dt = std::chrono::duration<float>(t_now - t_start).count();
// increment timers
t_frame += dt;
t_fps_counter += dt;
if (t_fps_counter > 1.f)
{
}
tick(dt);
if (redraw)
{
update(t_frame);
render_task([this, t_frame]
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
clear();
draw(t_frame);
async_swap();
});
t_frame = 0;
rendered_frames++;
}
}
}
void App::render_thread_start()
{
render_thread = std::thread(&App::render_thread_main, this);
}
void App::render_thread_stop()
{
render_running = false;
render_cv.notify_all();
if (render_thread.joinable())
render_thread.join();
}
void App::ui_thread_start()
{
ui_thread = std::thread(&App::ui_thread_main, this);
}
void App::ui_thread_stop()
{
ui_running = false;
ui_cv.notify_all();
if (ui_thread.joinable())
ui_thread.join();
}