Browse Source

Restore rendering on a separate thread

m4nu3lf 7 years ago
parent
commit
871c47a2bb

+ 0 - 2
main/main.cpp

@@ -822,8 +822,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) {

+ 6 - 35
servers/visual/visual_server_wrap_mt.cpp

@@ -37,14 +37,7 @@ void VisualServerWrapMT::thread_exit() {
 
 
 void VisualServerWrapMT::thread_draw() {
 void VisualServerWrapMT::thread_draw() {
 
 
-	draw_mutex->lock();
-
-	draw_pending--;
-	bool draw = (draw_pending == 0); // only draw when no more flushes are pending
-
-	draw_mutex->unlock();
-
-	if (draw) {
+	if (!atomic_decrement(&draw_pending)) {
 
 
 		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();
-
-	draw_pending--;
-
-	draw_mutex->unlock();
+	atomic_decrement(&draw_pending);
 }
 }
 
 
 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 */
-
-		/*
-		ERR_FAIL_COND(!draw_mutex);
-		draw_mutex->lock();
-		draw_pending++; //cambiar por un saferefcount
-		draw_mutex->unlock();
-		*/
-		//command_queue.push( this, &VisualServerWrapMT::thread_flush);
+		atomic_increment(&draw_pending);
+		command_queue.push_and_sync(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
-		ERR_FAIL_COND(!draw_mutex);
-		draw_mutex->lock();
-		draw_pending++; //cambiar por un saferefcount
-		draw_mutex->unlock();
-
-		command_queue.push( this, &VisualServerWrapMT::thread_draw);
-		*/
+		atomic_increment(&draw_pending);
+		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();

+ 1 - 2
servers/visual/visual_server_wrap_mt.h

@@ -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;
-	int draw_pending;
+	uint64_t draw_pending;
 	void thread_draw();
 	void thread_draw();
 	void thread_flush();
 	void thread_flush();