visual_server_wrap_mt.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. if (!draw_pending.decrement()) {
  38. visual_server->draw(p_swap_buffers, frame_step);
  39. }
  40. }
  41. void VisualServerWrapMT::thread_flush() {
  42. draw_pending.decrement();
  43. }
  44. void VisualServerWrapMT::_thread_callback(void *_instance) {
  45. VisualServerWrapMT *vsmt = reinterpret_cast<VisualServerWrapMT *>(_instance);
  46. vsmt->thread_loop();
  47. }
  48. void VisualServerWrapMT::thread_loop() {
  49. server_thread = Thread::get_caller_id();
  50. OS::get_singleton()->make_rendering_thread();
  51. visual_server->init();
  52. exit.clear();
  53. draw_thread_up.set();
  54. while (!exit.is_set()) {
  55. // flush commands one by one, until exit is requested
  56. command_queue.wait_and_flush_one();
  57. }
  58. command_queue.flush_all(); // flush all
  59. visual_server->finish();
  60. }
  61. /* EVENT QUEUING */
  62. void VisualServerWrapMT::sync() {
  63. if (create_thread) {
  64. draw_pending.increment();
  65. command_queue.push_and_sync(this, &VisualServerWrapMT::thread_flush);
  66. } else {
  67. command_queue.flush_all(); //flush all pending from other threads
  68. }
  69. }
  70. void VisualServerWrapMT::draw(bool p_swap_buffers, double frame_step) {
  71. if (create_thread) {
  72. draw_pending.increment();
  73. command_queue.push(this, &VisualServerWrapMT::thread_draw, p_swap_buffers, frame_step);
  74. } else {
  75. visual_server->draw(p_swap_buffers, frame_step);
  76. }
  77. }
  78. void VisualServerWrapMT::init() {
  79. if (create_thread) {
  80. print_verbose("VisualServerWrapMT: Creating render thread");
  81. OS::get_singleton()->release_rendering_thread();
  82. if (create_thread) {
  83. thread.start(_thread_callback, this);
  84. print_verbose("VisualServerWrapMT: Starting render thread");
  85. }
  86. while (!draw_thread_up.is_set()) {
  87. OS::get_singleton()->delay_usec(1000);
  88. }
  89. print_verbose("VisualServerWrapMT: Finished render thread");
  90. } else {
  91. visual_server->init();
  92. }
  93. }
  94. void VisualServerWrapMT::finish() {
  95. if (create_thread) {
  96. command_queue.push(this, &VisualServerWrapMT::thread_exit);
  97. thread.wait_to_finish();
  98. } else {
  99. visual_server->finish();
  100. }
  101. texture_free_cached_ids();
  102. sky_free_cached_ids();
  103. shader_free_cached_ids();
  104. material_free_cached_ids();
  105. mesh_free_cached_ids();
  106. multimesh_free_cached_ids();
  107. immediate_free_cached_ids();
  108. skeleton_free_cached_ids();
  109. directional_light_free_cached_ids();
  110. omni_light_free_cached_ids();
  111. spot_light_free_cached_ids();
  112. reflection_probe_free_cached_ids();
  113. gi_probe_free_cached_ids();
  114. lightmap_capture_free_cached_ids();
  115. particles_free_cached_ids();
  116. camera_free_cached_ids();
  117. viewport_free_cached_ids();
  118. environment_free_cached_ids();
  119. scenario_free_cached_ids();
  120. instance_free_cached_ids();
  121. canvas_free_cached_ids();
  122. canvas_item_free_cached_ids();
  123. canvas_light_occluder_free_cached_ids();
  124. canvas_occluder_polygon_free_cached_ids();
  125. room_free_cached_ids();
  126. roomgroup_free_cached_ids();
  127. portal_free_cached_ids();
  128. ghost_free_cached_ids();
  129. occluder_free_cached_ids();
  130. }
  131. void VisualServerWrapMT::set_use_vsync_callback(bool p_enable) {
  132. singleton_mt->call_set_use_vsync(p_enable);
  133. }
  134. VisualServerWrapMT *VisualServerWrapMT::singleton_mt = nullptr;
  135. VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) :
  136. command_queue(p_create_thread) {
  137. singleton_mt = this;
  138. OS::switch_vsync_function = set_use_vsync_callback; //as this goes to another thread, make sure it goes properly
  139. visual_server = p_contained;
  140. create_thread = p_create_thread;
  141. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  142. if (!p_create_thread) {
  143. server_thread = Thread::get_caller_id();
  144. } else {
  145. server_thread = 0;
  146. }
  147. }
  148. VisualServerWrapMT::~VisualServerWrapMT() {
  149. memdelete(visual_server);
  150. //finish();
  151. }