| 
									
										
										
										
											2023-01-10 15:26:54 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  visual_server_wrap_mt.cpp                                             */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | #include "visual_server_wrap_mt.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/os/os.h"
 | 
					
						
							|  |  |  | #include "core/project_settings.h"
 | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::thread_exit() { | 
					
						
							| 
									
										
										
										
											2021-01-31 13:34:42 +01:00
										 |  |  | 	exit.set(); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 08:11:43 +02:00
										 |  |  | void VisualServerWrapMT::thread_draw(bool p_swap_buffers, double frame_step) { | 
					
						
							| 
									
										
										
										
											2020-01-30 22:22:36 +01:00
										 |  |  | 	visual_server->draw(p_swap_buffers, frame_step); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::thread_flush() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::_thread_callback(void *_instance) { | 
					
						
							|  |  |  | 	VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	vsmt->thread_loop(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::thread_loop() { | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 	server_thread = Thread::get_caller_id(); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	OS::get_singleton()->make_rendering_thread(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	visual_server->init(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-31 13:34:42 +01:00
										 |  |  | 	exit.clear(); | 
					
						
							|  |  |  | 	draw_thread_up.set(); | 
					
						
							|  |  |  | 	while (!exit.is_set()) { | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 		// flush commands one by one, until exit is requested
 | 
					
						
							|  |  |  | 		command_queue.wait_and_flush_one(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	command_queue.flush_all(); // flush all
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	visual_server->finish(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* EVENT QUEUING */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-09 15:53:23 +00:00
										 |  |  | void VisualServerWrapMT::set_physics_interpolation_enabled(bool p_enabled) { | 
					
						
							|  |  |  | 	if (Thread::get_caller_id() != server_thread) { | 
					
						
							|  |  |  | 		command_queue.push(visual_server, &VisualServer::set_physics_interpolation_enabled, p_enabled); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		visual_server->set_physics_interpolation_enabled(p_enabled); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::tick() { | 
					
						
							|  |  |  | 	if (Thread::get_caller_id() != server_thread) { | 
					
						
							|  |  |  | 		command_queue.push(visual_server, &VisualServer::tick); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		visual_server->tick(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::pre_draw(bool p_will_draw) { | 
					
						
							|  |  |  | 	if (Thread::get_caller_id() != server_thread) { | 
					
						
							|  |  |  | 		command_queue.push(visual_server, &VisualServer::pre_draw, p_will_draw); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		visual_server->pre_draw(p_will_draw); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | void VisualServerWrapMT::sync() { | 
					
						
							|  |  |  | 	if (create_thread) { | 
					
						
							| 
									
										
										
										
											2017-10-06 23:24:23 +01:00
										 |  |  | 		command_queue.push_and_sync(this, &VisualServerWrapMT::thread_flush); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		command_queue.flush_all(); //flush all pending from other threads
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 08:11:43 +02:00
										 |  |  | void VisualServerWrapMT::draw(bool p_swap_buffers, double frame_step) { | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	if (create_thread) { | 
					
						
							| 
									
										
										
										
											2018-05-09 08:11:43 +02:00
										 |  |  | 		command_queue.push(this, &VisualServerWrapMT::thread_draw, p_swap_buffers, frame_step); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-05-09 08:11:43 +02:00
										 |  |  | 		visual_server->draw(p_swap_buffers, frame_step); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::init() { | 
					
						
							|  |  |  | 	if (create_thread) { | 
					
						
							| 
									
										
										
										
											2018-08-24 09:35:07 +02:00
										 |  |  | 		print_verbose("VisualServerWrapMT: Creating render thread"); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 		OS::get_singleton()->release_rendering_thread(); | 
					
						
							|  |  |  | 		if (create_thread) { | 
					
						
							| 
									
										
										
										
											2021-01-27 20:10:10 +01:00
										 |  |  | 			thread.start(_thread_callback, this); | 
					
						
							| 
									
										
										
										
											2018-08-24 09:35:07 +02:00
										 |  |  | 			print_verbose("VisualServerWrapMT: Starting render thread"); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-01-31 13:34:42 +01:00
										 |  |  | 		while (!draw_thread_up.is_set()) { | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 			OS::get_singleton()->delay_usec(1000); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-24 09:35:07 +02:00
										 |  |  | 		print_verbose("VisualServerWrapMT: Finished render thread"); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		visual_server->init(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void VisualServerWrapMT::finish() { | 
					
						
							| 
									
										
										
										
											2021-01-27 20:10:10 +01:00
										 |  |  | 	if (create_thread) { | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 		command_queue.push(this, &VisualServerWrapMT::thread_exit); | 
					
						
							| 
									
										
										
										
											2021-01-27 20:10:10 +01:00
										 |  |  | 		thread.wait_to_finish(); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		visual_server->finish(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 23:34:01 -03:00
										 |  |  | 	texture_free_cached_ids(); | 
					
						
							| 
									
										
										
										
											2019-10-13 09:31:00 -07:00
										 |  |  | 	sky_free_cached_ids(); | 
					
						
							| 
									
										
										
										
											2017-11-09 23:34:01 -03:00
										 |  |  | 	shader_free_cached_ids(); | 
					
						
							|  |  |  | 	material_free_cached_ids(); | 
					
						
							|  |  |  | 	mesh_free_cached_ids(); | 
					
						
							|  |  |  | 	multimesh_free_cached_ids(); | 
					
						
							|  |  |  | 	immediate_free_cached_ids(); | 
					
						
							|  |  |  | 	skeleton_free_cached_ids(); | 
					
						
							|  |  |  | 	directional_light_free_cached_ids(); | 
					
						
							|  |  |  | 	omni_light_free_cached_ids(); | 
					
						
							|  |  |  | 	spot_light_free_cached_ids(); | 
					
						
							|  |  |  | 	reflection_probe_free_cached_ids(); | 
					
						
							|  |  |  | 	gi_probe_free_cached_ids(); | 
					
						
							| 
									
										
										
										
											2019-10-13 09:31:00 -07:00
										 |  |  | 	lightmap_capture_free_cached_ids(); | 
					
						
							| 
									
										
										
										
											2017-11-09 23:34:01 -03:00
										 |  |  | 	particles_free_cached_ids(); | 
					
						
							|  |  |  | 	camera_free_cached_ids(); | 
					
						
							|  |  |  | 	viewport_free_cached_ids(); | 
					
						
							|  |  |  | 	environment_free_cached_ids(); | 
					
						
							|  |  |  | 	scenario_free_cached_ids(); | 
					
						
							|  |  |  | 	instance_free_cached_ids(); | 
					
						
							|  |  |  | 	canvas_free_cached_ids(); | 
					
						
							|  |  |  | 	canvas_item_free_cached_ids(); | 
					
						
							|  |  |  | 	canvas_light_occluder_free_cached_ids(); | 
					
						
							|  |  |  | 	canvas_occluder_polygon_free_cached_ids(); | 
					
						
							| 
									
										
										
										
											2021-02-04 10:43:08 +00:00
										 |  |  | 	room_free_cached_ids(); | 
					
						
							|  |  |  | 	roomgroup_free_cached_ids(); | 
					
						
							|  |  |  | 	portal_free_cached_ids(); | 
					
						
							|  |  |  | 	ghost_free_cached_ids(); | 
					
						
							| 
									
										
										
										
											2022-02-11 14:36:02 +00:00
										 |  |  | 	occluder_instance_free_cached_ids(); | 
					
						
							|  |  |  | 	occluder_resource_free_cached_ids(); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 17:09:25 -03:00
										 |  |  | void VisualServerWrapMT::set_use_vsync_callback(bool p_enable) { | 
					
						
							|  |  |  | 	singleton_mt->call_set_use_vsync(p_enable); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-04 16:00:45 +02:00
										 |  |  | VisualServerWrapMT *VisualServerWrapMT::singleton_mt = nullptr; | 
					
						
							| 
									
										
										
										
											2017-12-16 17:09:25 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 21:36:34 +01:00
										 |  |  | VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) : | 
					
						
							|  |  |  | 		command_queue(p_create_thread) { | 
					
						
							| 
									
										
										
										
											2017-12-16 17:09:25 -03:00
										 |  |  | 	singleton_mt = this; | 
					
						
							|  |  |  | 	OS::switch_vsync_function = set_use_vsync_callback; //as this goes to another thread, make sure it goes properly
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	visual_server = p_contained; | 
					
						
							|  |  |  | 	create_thread = p_create_thread; | 
					
						
							| 
									
										
										
										
											2017-07-17 21:05:38 -03:00
										 |  |  | 	pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc"); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!p_create_thread) { | 
					
						
							| 
									
										
										
										
											2017-08-07 17:17:31 +07:00
										 |  |  | 		server_thread = Thread::get_caller_id(); | 
					
						
							| 
									
										
										
										
											2017-06-09 00:23:50 -03:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		server_thread = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | VisualServerWrapMT::~VisualServerWrapMT() { | 
					
						
							|  |  |  | 	memdelete(visual_server); | 
					
						
							|  |  |  | 	//finish();
 | 
					
						
							|  |  |  | } |