visual_server_wrap_mt.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 = true;
  35. }
  36. void VisualServerWrapMT::thread_draw(bool p_swap_buffers, double frame_step) {
  37. if (!atomic_decrement(&draw_pending)) {
  38. visual_server->draw(p_swap_buffers, frame_step);
  39. }
  40. }
  41. void VisualServerWrapMT::thread_flush() {
  42. atomic_decrement(&draw_pending);
  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 = false;
  53. draw_thread_up = true;
  54. while (!exit) {
  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. atomic_increment(&draw_pending);
  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. atomic_increment(&draw_pending);
  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 = Thread::create(_thread_callback, this);
  84. print_verbose("VisualServerWrapMT: Starting render thread");
  85. }
  86. while (!draw_thread_up) {
  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 (thread) {
  96. command_queue.push(this, &VisualServerWrapMT::thread_exit);
  97. Thread::wait_to_finish(thread);
  98. memdelete(thread);
  99. thread = NULL;
  100. } else {
  101. visual_server->finish();
  102. }
  103. texture_free_cached_ids();
  104. shader_free_cached_ids();
  105. material_free_cached_ids();
  106. mesh_free_cached_ids();
  107. multimesh_free_cached_ids();
  108. immediate_free_cached_ids();
  109. skeleton_free_cached_ids();
  110. directional_light_free_cached_ids();
  111. omni_light_free_cached_ids();
  112. spot_light_free_cached_ids();
  113. reflection_probe_free_cached_ids();
  114. gi_probe_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. }
  126. void VisualServerWrapMT::set_use_vsync_callback(bool p_enable) {
  127. singleton_mt->call_set_use_vsync(p_enable);
  128. }
  129. VisualServerWrapMT *VisualServerWrapMT::singleton_mt = NULL;
  130. VisualServerWrapMT::VisualServerWrapMT(VisualServer *p_contained, bool p_create_thread) :
  131. command_queue(p_create_thread) {
  132. singleton_mt = this;
  133. OS::switch_vsync_function = set_use_vsync_callback; //as this goes to another thread, make sure it goes properly
  134. visual_server = p_contained;
  135. create_thread = p_create_thread;
  136. thread = NULL;
  137. draw_pending = 0;
  138. draw_thread_up = false;
  139. alloc_mutex = Mutex::create();
  140. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  141. if (!p_create_thread) {
  142. server_thread = Thread::get_caller_id();
  143. } else {
  144. server_thread = 0;
  145. }
  146. }
  147. VisualServerWrapMT::~VisualServerWrapMT() {
  148. memdelete(visual_server);
  149. memdelete(alloc_mutex);
  150. //finish();
  151. }