2017-06-09 11:23:50 +08:00
|
|
|
/*************************************************************************/
|
2020-03-28 20:19:05 +08:00
|
|
|
/* rendering_server_wrap_mt.cpp */
|
2017-06-09 11:23:50 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2017-06-09 11:23:50 +08:00
|
|
|
/*************************************************************************/
|
2020-01-01 18:16:22 +08:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-06-09 11:23:50 +08:00
|
|
|
/* */
|
|
|
|
/* 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 07:50:27 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
#include "rendering_server_wrap_mt.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/project_settings.h"
|
2020-03-03 21:36:29 +08:00
|
|
|
#include "servers/display_server.h"
|
2017-06-09 11:23:50 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::thread_exit() {
|
2017-06-09 11:23:50 +08:00
|
|
|
exit = true;
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::thread_draw(bool p_swap_buffers, double frame_step) {
|
2017-10-07 06:24:23 +08:00
|
|
|
if (!atomic_decrement(&draw_pending)) {
|
2020-03-28 02:21:27 +08:00
|
|
|
rendering_server->draw(p_swap_buffers, frame_step);
|
2017-06-09 11:23:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::thread_flush() {
|
2017-10-07 06:24:23 +08:00
|
|
|
atomic_decrement(&draw_pending);
|
2017-06-09 11:23:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::_thread_callback(void *_instance) {
|
|
|
|
RenderingServerWrapMT *vsmt = reinterpret_cast<RenderingServerWrapMT *>(_instance);
|
2017-06-09 11:23:50 +08:00
|
|
|
|
|
|
|
vsmt->thread_loop();
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::thread_loop() {
|
2017-08-07 18:17:31 +08:00
|
|
|
server_thread = Thread::get_caller_id();
|
2017-06-09 11:23:50 +08:00
|
|
|
|
2020-03-03 21:36:29 +08:00
|
|
|
DisplayServer::get_singleton()->make_rendering_thread();
|
2017-06-09 11:23:50 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
rendering_server->init();
|
2017-06-09 11:23:50 +08:00
|
|
|
|
|
|
|
exit = false;
|
|
|
|
draw_thread_up = true;
|
|
|
|
while (!exit) {
|
|
|
|
// flush commands one by one, until exit is requested
|
|
|
|
command_queue.wait_and_flush_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
command_queue.flush_all(); // flush all
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
rendering_server->finish();
|
2017-06-09 11:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EVENT QUEUING */
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::sync() {
|
2017-06-09 11:23:50 +08:00
|
|
|
if (create_thread) {
|
2017-10-07 06:24:23 +08:00
|
|
|
atomic_increment(&draw_pending);
|
2020-03-28 02:21:27 +08:00
|
|
|
command_queue.push_and_sync(this, &RenderingServerWrapMT::thread_flush);
|
2017-06-09 11:23:50 +08:00
|
|
|
} else {
|
|
|
|
command_queue.flush_all(); //flush all pending from other threads
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::draw(bool p_swap_buffers, double frame_step) {
|
2017-06-09 11:23:50 +08:00
|
|
|
if (create_thread) {
|
2017-10-07 06:24:23 +08:00
|
|
|
atomic_increment(&draw_pending);
|
2020-03-28 02:21:27 +08:00
|
|
|
command_queue.push(this, &RenderingServerWrapMT::thread_draw, p_swap_buffers, frame_step);
|
2017-06-09 11:23:50 +08:00
|
|
|
} else {
|
2020-03-28 02:21:27 +08:00
|
|
|
rendering_server->draw(p_swap_buffers, frame_step);
|
2017-06-09 11:23:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::init() {
|
2017-06-09 11:23:50 +08:00
|
|
|
if (create_thread) {
|
2020-03-28 02:21:27 +08:00
|
|
|
print_verbose("RenderingServerWrapMT: Creating render thread");
|
2020-03-03 21:36:29 +08:00
|
|
|
DisplayServer::get_singleton()->release_rendering_thread();
|
2017-06-09 11:23:50 +08:00
|
|
|
if (create_thread) {
|
|
|
|
thread = Thread::create(_thread_callback, this);
|
2020-03-28 02:21:27 +08:00
|
|
|
print_verbose("RenderingServerWrapMT: Starting render thread");
|
2017-06-09 11:23:50 +08:00
|
|
|
}
|
|
|
|
while (!draw_thread_up) {
|
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
}
|
2020-03-28 02:21:27 +08:00
|
|
|
print_verbose("RenderingServerWrapMT: Finished render thread");
|
2017-06-09 11:23:50 +08:00
|
|
|
} else {
|
2020-03-28 02:21:27 +08:00
|
|
|
rendering_server->init();
|
2017-06-09 11:23:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::finish() {
|
2019-10-14 00:31:00 +08:00
|
|
|
sky_free_cached_ids();
|
2017-11-10 10:34:01 +08: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();
|
2020-05-01 20:34:23 +08:00
|
|
|
lightmap_free_cached_ids();
|
2017-11-10 10:34:01 +08:00
|
|
|
particles_free_cached_ids();
|
|
|
|
camera_free_cached_ids();
|
|
|
|
viewport_free_cached_ids();
|
|
|
|
environment_free_cached_ids();
|
2020-01-14 02:37:24 +08:00
|
|
|
camera_effects_free_cached_ids();
|
2017-11-10 10:34:01 +08:00
|
|
|
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();
|
2020-04-08 01:12:07 +08:00
|
|
|
|
|
|
|
if (thread) {
|
|
|
|
command_queue.push(this, &RenderingServerWrapMT::thread_exit);
|
|
|
|
Thread::wait_to_finish(thread);
|
|
|
|
memdelete(thread);
|
|
|
|
|
|
|
|
thread = nullptr;
|
|
|
|
} else {
|
|
|
|
rendering_server->finish();
|
|
|
|
}
|
2017-06-09 11:23:50 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void RenderingServerWrapMT::set_use_vsync_callback(bool p_enable) {
|
2017-12-17 04:09:25 +08:00
|
|
|
singleton_mt->call_set_use_vsync(p_enable);
|
|
|
|
}
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
RenderingServerWrapMT *RenderingServerWrapMT::singleton_mt = nullptr;
|
2017-12-17 04:09:25 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
RenderingServerWrapMT::RenderingServerWrapMT(RenderingServer *p_contained, bool p_create_thread) :
|
2017-12-07 04:36:34 +08:00
|
|
|
command_queue(p_create_thread) {
|
2017-12-17 04:09:25 +08:00
|
|
|
singleton_mt = this;
|
2020-03-03 21:36:29 +08:00
|
|
|
DisplayServer::switch_vsync_function = set_use_vsync_callback; //as this goes to another thread, make sure it goes properly
|
2017-12-17 04:09:25 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
rendering_server = p_contained;
|
2017-06-09 11:23:50 +08:00
|
|
|
create_thread = p_create_thread;
|
2020-04-02 07:20:12 +08:00
|
|
|
thread = nullptr;
|
2017-06-09 11:23:50 +08:00
|
|
|
draw_pending = 0;
|
|
|
|
draw_thread_up = false;
|
2017-07-18 08:05:38 +08:00
|
|
|
pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
|
2017-06-09 11:23:50 +08:00
|
|
|
|
|
|
|
if (!p_create_thread) {
|
2017-08-07 18:17:31 +08:00
|
|
|
server_thread = Thread::get_caller_id();
|
2017-06-09 11:23:50 +08:00
|
|
|
} else {
|
|
|
|
server_thread = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
RenderingServerWrapMT::~RenderingServerWrapMT() {
|
|
|
|
memdelete(rendering_server);
|
2017-06-09 11:23:50 +08:00
|
|
|
//finish();
|
|
|
|
}
|