Merge pull request #11895 from m4nu3lf/rendering/separate_thread

Restore rendering on a separate thread
This commit is contained in:
Juan Linietsky 2017-11-21 14:31:14 -03:00 committed by GitHub
commit 640856f4d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 361 additions and 1250 deletions

View file

@ -76,6 +76,30 @@ CommandQueueMT::SyncSemaphore *CommandQueueMT::_alloc_sync_sem() {
return &sync_sems[idx]; return &sync_sems[idx];
} }
bool CommandQueueMT::dealloc_one() {
tryagain:
if (dealloc_ptr == write_ptr) {
// The queue is empty
return false;
}
uint32_t size = *(uint32_t *)&command_mem[dealloc_ptr];
if (size == 0) {
// End of command buffer wrap down
dealloc_ptr = 0;
goto tryagain;
}
if (size & 1) {
// Still used, nothing can be deallocated
return false;
}
dealloc_ptr += (size >> 1) + sizeof(uint32_t);
return true;
}
CommandQueueMT::CommandQueueMT(bool p_sync) { CommandQueueMT::CommandQueueMT(bool p_sync) {
read_ptr = 0; read_ptr = 0;

File diff suppressed because it is too large Load diff

View file

@ -832,8 +832,6 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true); OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true);
if (rtm == -1) { if (rtm == -1) {
rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE); rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE);
if (rtm >= 1) //hack for now
rtm = 1;
} }
if (rtm >= 0 && rtm < 3) { if (rtm >= 0 && rtm < 3) {

View file

@ -61,6 +61,7 @@
if (m_type##_id_pool.size() == 0) { \ if (m_type##_id_pool.size() == 0) { \
int ret; \ int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, &ret); \ command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, &ret); \
SYNC_DEBUG \
} \ } \
rid = m_type##_id_pool.front()->get(); \ rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \ m_type##_id_pool.pop_front(); \
@ -91,6 +92,7 @@
if (m_type##_id_pool.size() == 0) { \ if (m_type##_id_pool.size() == 0) { \
int ret; \ int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, &ret); \ command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, &ret); \
SYNC_DEBUG \
} \ } \
rid = m_type##_id_pool.front()->get(); \ rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \ m_type##_id_pool.pop_front(); \
@ -121,6 +123,7 @@
if (m_type##_id_pool.size() == 0) { \ if (m_type##_id_pool.size() == 0) { \
int ret; \ int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, &ret); \ command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, &ret); \
SYNC_DEBUG \
} \ } \
rid = m_type##_id_pool.front()->get(); \ rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \ m_type##_id_pool.pop_front(); \
@ -151,6 +154,7 @@
if (m_type##_id_pool.size() == 0) { \ if (m_type##_id_pool.size() == 0) { \
int ret; \ int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, &ret); \ command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, &ret); \
SYNC_DEBUG \
} \ } \
rid = m_type##_id_pool.front()->get(); \ rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \ m_type##_id_pool.pop_front(); \
@ -181,6 +185,7 @@
if (m_type##_id_pool.size() == 0) { \ if (m_type##_id_pool.size() == 0) { \
int ret; \ int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, p4, &ret); \ command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, p4, &ret); \
SYNC_DEBUG \
} \ } \
rid = m_type##_id_pool.front()->get(); \ rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \ m_type##_id_pool.pop_front(); \
@ -211,6 +216,7 @@
if (m_type##_id_pool.size() == 0) { \ if (m_type##_id_pool.size() == 0) { \
int ret; \ int ret; \
command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, p4, p5, &ret); \ command_queue.push_and_ret(this, &ServerNameWrapMT::m_type##allocn, p1, p2, p3, p4, p5, &ret); \
SYNC_DEBUG \
} \ } \
rid = m_type##_id_pool.front()->get(); \ rid = m_type##_id_pool.front()->get(); \
m_type##_id_pool.pop_front(); \ m_type##_id_pool.pop_front(); \
@ -255,6 +261,7 @@
virtual void m_type() { \ virtual void m_type() { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type); \ command_queue.push_and_sync(server_name, &ServerName::m_type); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(); \ server_name->m_type(); \
} \ } \
@ -264,6 +271,7 @@
virtual void m_type() const { \ virtual void m_type() const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type); \ command_queue.push_and_sync(server_name, &ServerName::m_type); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(); \ server_name->m_type(); \
} \ } \
@ -299,6 +307,7 @@
virtual void m_type(m_arg1 p1) { \ virtual void m_type(m_arg1 p1) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1); \ server_name->m_type(p1); \
} \ } \
@ -308,6 +317,7 @@
virtual void m_type(m_arg1 p1) const { \ virtual void m_type(m_arg1 p1) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1); \ server_name->m_type(p1); \
} \ } \
@ -359,6 +369,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2) { \ virtual void m_type(m_arg1 p1, m_arg2 p2) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2); \ server_name->m_type(p1, p2); \
} \ } \
@ -368,6 +379,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2) const { \ virtual void m_type(m_arg1 p1, m_arg2 p2) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2); \ server_name->m_type(p1, p2); \
} \ } \
@ -408,6 +420,7 @@
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
m_r ret; \ m_r ret; \
command_queue.push_and_ret(server_name, &ServerName::m_type, p1, p2, p3, &ret); \ command_queue.push_and_ret(server_name, &ServerName::m_type, p1, p2, p3, &ret); \
SYNC_DEBUG \
return ret; \ return ret; \
} else { \ } else { \
return server_name->m_type(p1, p2, p3); \ return server_name->m_type(p1, p2, p3); \
@ -418,6 +431,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3) { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3); \ server_name->m_type(p1, p2, p3); \
} \ } \
@ -427,6 +441,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3) const { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3); \ server_name->m_type(p1, p2, p3); \
} \ } \
@ -478,6 +493,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4) { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4); \ server_name->m_type(p1, p2, p3, p4); \
} \ } \
@ -487,6 +503,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4) const { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4); \ server_name->m_type(p1, p2, p3, p4); \
} \ } \
@ -538,6 +555,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5) { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5); \ server_name->m_type(p1, p2, p3, p4, p5); \
} \ } \
@ -547,6 +565,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5) const { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5); \ server_name->m_type(p1, p2, p3, p4, p5); \
} \ } \
@ -587,6 +606,7 @@
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
m_r ret; \ m_r ret; \
command_queue.push_and_ret(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, &ret); \ command_queue.push_and_ret(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, &ret); \
SYNC_DEBUG \
return ret; \ return ret; \
} else { \ } else { \
return server_name->m_type(p1, p2, p3, p4, p5, p6); \ return server_name->m_type(p1, p2, p3, p4, p5, p6); \
@ -597,6 +617,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6) { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5, p6); \ server_name->m_type(p1, p2, p3, p4, p5, p6); \
} \ } \
@ -606,6 +627,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6) const { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5, p6); \ server_name->m_type(p1, p2, p3, p4, p5, p6); \
} \ } \
@ -657,6 +679,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7) { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5, p6, p7); \ server_name->m_type(p1, p2, p3, p4, p5, p6, p7); \
} \ } \
@ -666,6 +689,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7) const { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5, p6, p7); \ server_name->m_type(p1, p2, p3, p4, p5, p6, p7); \
} \ } \
@ -717,6 +741,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7, m_arg8 p8) { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7, m_arg8 p8) { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7, p8); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7, p8); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5, p6, p7, p8); \ server_name->m_type(p1, p2, p3, p4, p5, p6, p7, p8); \
} \ } \
@ -726,6 +751,7 @@
virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7, m_arg8 p8) const { \ virtual void m_type(m_arg1 p1, m_arg2 p2, m_arg3 p3, m_arg4 p4, m_arg5 p5, m_arg6 p6, m_arg7 p7, m_arg8 p8) const { \
if (Thread::get_caller_id() != server_thread) { \ if (Thread::get_caller_id() != server_thread) { \
command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7, p8); \ command_queue.push_and_sync(server_name, &ServerName::m_type, p1, p2, p3, p4, p5, p6, p7, p8); \
SYNC_DEBUG \
} else { \ } else { \
server_name->m_type(p1, p2, p3, p4, p5, p6, p7, p8); \ server_name->m_type(p1, p2, p3, p4, p5, p6, p7, p8); \
} \ } \

View file

@ -37,14 +37,7 @@ void VisualServerWrapMT::thread_exit() {
void VisualServerWrapMT::thread_draw() { void VisualServerWrapMT::thread_draw() {
draw_mutex->lock(); if (!atomic_decrement(&draw_pending)) {
draw_pending--;
bool draw = (draw_pending == 0); // only draw when no more flushes are pending
draw_mutex->unlock();
if (draw) {
visual_server->draw(); visual_server->draw();
} }
@ -52,11 +45,7 @@ void VisualServerWrapMT::thread_draw() {
void VisualServerWrapMT::thread_flush() { void VisualServerWrapMT::thread_flush() {
draw_mutex->lock(); atomic_decrement(&draw_pending);
draw_pending--;
draw_mutex->unlock();
} }
void VisualServerWrapMT::_thread_callback(void *_instance) { void VisualServerWrapMT::_thread_callback(void *_instance) {
@ -92,15 +81,8 @@ void VisualServerWrapMT::sync() {
if (create_thread) { if (create_thread) {
/* TODO: sync with the thread */ atomic_increment(&draw_pending);
command_queue.push_and_sync(this, &VisualServerWrapMT::thread_flush);
/*
ERR_FAIL_COND(!draw_mutex);
draw_mutex->lock();
draw_pending++; //cambiar por un saferefcount
draw_mutex->unlock();
*/
//command_queue.push( this, &VisualServerWrapMT::thread_flush);
} else { } else {
command_queue.flush_all(); //flush all pending from other threads command_queue.flush_all(); //flush all pending from other threads
@ -111,14 +93,8 @@ void VisualServerWrapMT::draw() {
if (create_thread) { if (create_thread) {
/* TODO: Make it draw atomic_increment(&draw_pending);
ERR_FAIL_COND(!draw_mutex); command_queue.push(this, &VisualServerWrapMT::thread_draw);
draw_mutex->lock();
draw_pending++; //cambiar por un saferefcount
draw_mutex->unlock();
command_queue.push( this, &VisualServerWrapMT::thread_draw);
*/
} else { } else {
visual_server->draw(); visual_server->draw();
@ -129,7 +105,6 @@ void VisualServerWrapMT::init() {
if (create_thread) { if (create_thread) {
draw_mutex = Mutex::create();
print_line("CREATING RENDER THREAD"); print_line("CREATING RENDER THREAD");
OS::get_singleton()->release_rendering_thread(); OS::get_singleton()->release_rendering_thread();
if (create_thread) { if (create_thread) {
@ -181,9 +156,6 @@ void VisualServerWrapMT::finish() {
canvas_item_free_cached_ids(); canvas_item_free_cached_ids();
canvas_light_occluder_free_cached_ids(); canvas_light_occluder_free_cached_ids();
canvas_occluder_polygon_free_cached_ids(); canvas_occluder_polygon_free_cached_ids();
if (draw_mutex)
memdelete(draw_mutex);
} }
VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread)
@ -192,7 +164,6 @@ VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_
visual_server = p_contained; visual_server = p_contained;
create_thread = p_create_thread; create_thread = p_create_thread;
thread = NULL; thread = NULL;
draw_mutex = NULL;
draw_pending = 0; draw_pending = 0;
draw_thread_up = false; draw_thread_up = false;
alloc_mutex = Mutex::create(); alloc_mutex = Mutex::create();

View file

@ -52,8 +52,7 @@ class VisualServerWrapMT : public VisualServer {
volatile bool draw_thread_up; volatile bool draw_thread_up;
bool create_thread; bool create_thread;
Mutex *draw_mutex; uint64_t draw_pending;
int draw_pending;
void thread_draw(); void thread_draw();
void thread_flush(); void thread_flush();