visual_server_wrap_mt.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**************************************************************************/
  2. /* visual_server_wrap_mt.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "visual_server_wrap_mt.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. void VisualServerWrapMT::thread_exit() {
  34. exit.set();
  35. }
  36. void VisualServerWrapMT::thread_draw(bool p_swap_buffers, double frame_step) {
  37. visual_server->draw(p_swap_buffers, frame_step);
  38. }
  39. void VisualServerWrapMT::thread_flush() {
  40. }
  41. void VisualServerWrapMT::_thread_callback(void *_instance) {
  42. VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance);
  43. vsmt->thread_loop();
  44. }
  45. void VisualServerWrapMT::thread_loop() {
  46. server_thread = Thread::get_caller_id();
  47. OS::get_singleton()->make_rendering_thread();
  48. visual_server->init();
  49. exit.clear();
  50. draw_thread_up.set();
  51. while (!exit.is_set()) {
  52. // flush commands one by one, until exit is requested
  53. command_queue.wait_and_flush_one();
  54. }
  55. command_queue.flush_all(); // flush all
  56. visual_server->finish();
  57. }
  58. /* EVENT QUEUING */
  59. void VisualServerWrapMT::set_physics_interpolation_enabled(bool p_enabled) {
  60. if (Thread::get_caller_id() != server_thread) {
  61. command_queue.push(visual_server, &VisualServer::set_physics_interpolation_enabled, p_enabled);
  62. } else {
  63. visual_server->set_physics_interpolation_enabled(p_enabled);
  64. }
  65. }
  66. void VisualServerWrapMT::tick() {
  67. if (Thread::get_caller_id() != server_thread) {
  68. command_queue.push(visual_server, &VisualServer::tick);
  69. } else {
  70. visual_server->tick();
  71. }
  72. }
  73. void VisualServerWrapMT::pre_draw(bool p_will_draw) {
  74. if (Thread::get_caller_id() != server_thread) {
  75. command_queue.push(visual_server, &VisualServer::pre_draw, p_will_draw);
  76. } else {
  77. visual_server->pre_draw(p_will_draw);
  78. }
  79. }
  80. void VisualServerWrapMT::sync() {
  81. if (create_thread) {
  82. command_queue.push_and_sync(this, &VisualServerWrapMT::thread_flush);
  83. } else {
  84. command_queue.flush_all(); //flush all pending from other threads
  85. }
  86. }
  87. void VisualServerWrapMT::draw(bool p_swap_buffers, double frame_step) {
  88. if (create_thread) {
  89. command_queue.push(this, &VisualServerWrapMT::thread_draw, p_swap_buffers, frame_step);
  90. } else {
  91. visual_server->draw(p_swap_buffers, frame_step);
  92. }
  93. }
  94. void VisualServerWrapMT::init() {
  95. if (create_thread) {
  96. print_verbose("VisualServerWrapMT: Creating render thread");
  97. OS::get_singleton()->release_rendering_thread();
  98. if (create_thread) {
  99. thread.start(_thread_callback, this);
  100. print_verbose("VisualServerWrapMT: Starting render thread");
  101. }
  102. while (!draw_thread_up.is_set()) {
  103. OS::get_singleton()->delay_usec(1000);
  104. }
  105. print_verbose("VisualServerWrapMT: Finished render thread");
  106. } else {
  107. visual_server->init();
  108. }
  109. }
  110. void VisualServerWrapMT::finish() {
  111. if (create_thread) {
  112. command_queue.push(this, &VisualServerWrapMT::thread_exit);
  113. thread.wait_to_finish();
  114. } else {
  115. visual_server->finish();
  116. }
  117. texture_free_cached_ids();
  118. sky_free_cached_ids();
  119. shader_free_cached_ids();
  120. material_free_cached_ids();
  121. mesh_free_cached_ids();
  122. multimesh_free_cached_ids();
  123. immediate_free_cached_ids();
  124. skeleton_free_cached_ids();
  125. directional_light_free_cached_ids();
  126. omni_light_free_cached_ids();
  127. spot_light_free_cached_ids();
  128. reflection_probe_free_cached_ids();
  129. gi_probe_free_cached_ids();
  130. lightmap_capture_free_cached_ids();
  131. particles_free_cached_ids();
  132. camera_free_cached_ids();
  133. viewport_free_cached_ids();
  134. environment_free_cached_ids();
  135. scenario_free_cached_ids();
  136. instance_free_cached_ids();
  137. canvas_free_cached_ids();
  138. canvas_item_free_cached_ids();
  139. canvas_light_occluder_free_cached_ids();
  140. canvas_occluder_polygon_free_cached_ids();
  141. room_free_cached_ids();
  142. roomgroup_free_cached_ids();
  143. portal_free_cached_ids();
  144. ghost_free_cached_ids();
  145. occluder_instance_free_cached_ids();
  146. occluder_resource_free_cached_ids();
  147. }
  148. void VisualServerWrapMT::set_use_vsync_callback(bool p_enable) {
  149. singleton_mt->call_set_use_vsync(p_enable);
  150. }
  151. VisualServerWrapMT *VisualServerWrapMT::singleton_mt = nullptr;
  152. VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) :
  153. command_queue(p_create_thread) {
  154. singleton_mt = this;
  155. OS::switch_vsync_function = set_use_vsync_callback; //as this goes to another thread, make sure it goes properly
  156. visual_server = p_contained;
  157. create_thread = p_create_thread;
  158. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  159. if (!p_create_thread) {
  160. server_thread = Thread::get_caller_id();
  161. } else {
  162. server_thread = 0;
  163. }
  164. }
  165. VisualServerWrapMT::~VisualServerWrapMT() {
  166. memdelete(visual_server);
  167. //finish();
  168. }