rendering_server_default.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**************************************************************************/
  2. /* rendering_server_default.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 "rendering_server_default.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/os/os.h"
  34. #include "core/templates/sort_array.h"
  35. #include "renderer_canvas_cull.h"
  36. #include "renderer_scene_cull.h"
  37. #include "rendering_server_globals.h"
  38. // careful, these may run in different threads than the rendering server
  39. int RenderingServerDefault::changes = 0;
  40. /* FREE */
  41. void RenderingServerDefault::_free(RID p_rid) {
  42. if (unlikely(p_rid.is_null())) {
  43. return;
  44. }
  45. if (RSG::utilities->free(p_rid)) {
  46. return;
  47. }
  48. if (RSG::canvas->free(p_rid)) {
  49. return;
  50. }
  51. if (RSG::viewport->free(p_rid)) {
  52. return;
  53. }
  54. if (RSG::scene->free(p_rid)) {
  55. return;
  56. }
  57. }
  58. /* EVENT QUEUING */
  59. void RenderingServerDefault::request_frame_drawn_callback(const Callable &p_callable) {
  60. frame_drawn_callbacks.push_back(p_callable);
  61. }
  62. void RenderingServerDefault::_draw(bool p_swap_buffers, double frame_step) {
  63. //needs to be done before changes is reset to 0, to not force the editor to redraw
  64. RS::get_singleton()->emit_signal(SNAME("frame_pre_draw"));
  65. changes = 0;
  66. RSG::rasterizer->begin_frame(frame_step);
  67. TIMESTAMP_BEGIN()
  68. uint64_t time_usec = OS::get_singleton()->get_ticks_usec();
  69. RSG::scene->update(); //update scenes stuff before updating instances
  70. frame_setup_time = double(OS::get_singleton()->get_ticks_usec() - time_usec) / 1000.0;
  71. RSG::particles_storage->update_particles(); //need to be done after instances are updated (colliders and particle transforms), and colliders are rendered
  72. RSG::scene->render_probes();
  73. RSG::viewport->draw_viewports(p_swap_buffers);
  74. RSG::canvas_render->update();
  75. if (!OS::get_singleton()->get_current_rendering_driver_name().begins_with("opengl3")) {
  76. // Already called for gl_compatibility renderer.
  77. RSG::rasterizer->end_frame(p_swap_buffers);
  78. }
  79. XRServer *xr_server = XRServer::get_singleton();
  80. if (xr_server != nullptr) {
  81. // let our XR server know we're done so we can get our frame timing
  82. xr_server->end_frame();
  83. }
  84. RSG::canvas->update_visibility_notifiers();
  85. RSG::scene->update_visibility_notifiers();
  86. while (frame_drawn_callbacks.front()) {
  87. Callable c = frame_drawn_callbacks.front()->get();
  88. Variant result;
  89. Callable::CallError ce;
  90. c.callp(nullptr, 0, result, ce);
  91. if (ce.error != Callable::CallError::CALL_OK) {
  92. String err = Variant::get_callable_error_text(c, nullptr, 0, ce);
  93. ERR_PRINT("Error calling frame drawn function: " + err);
  94. }
  95. frame_drawn_callbacks.pop_front();
  96. }
  97. RS::get_singleton()->emit_signal(SNAME("frame_post_draw"));
  98. if (RSG::utilities->get_captured_timestamps_count()) {
  99. Vector<FrameProfileArea> new_profile;
  100. if (RSG::utilities->capturing_timestamps) {
  101. new_profile.resize(RSG::utilities->get_captured_timestamps_count());
  102. }
  103. uint64_t base_cpu = RSG::utilities->get_captured_timestamp_cpu_time(0);
  104. uint64_t base_gpu = RSG::utilities->get_captured_timestamp_gpu_time(0);
  105. for (uint32_t i = 0; i < RSG::utilities->get_captured_timestamps_count(); i++) {
  106. uint64_t time_cpu = RSG::utilities->get_captured_timestamp_cpu_time(i);
  107. uint64_t time_gpu = RSG::utilities->get_captured_timestamp_gpu_time(i);
  108. String name = RSG::utilities->get_captured_timestamp_name(i);
  109. if (name.begins_with("vp_")) {
  110. RSG::viewport->handle_timestamp(name, time_cpu, time_gpu);
  111. }
  112. if (RSG::utilities->capturing_timestamps) {
  113. new_profile.write[i].gpu_msec = double((time_gpu - base_gpu) / 1000) / 1000.0;
  114. new_profile.write[i].cpu_msec = double(time_cpu - base_cpu) / 1000.0;
  115. new_profile.write[i].name = RSG::utilities->get_captured_timestamp_name(i);
  116. }
  117. }
  118. frame_profile = new_profile;
  119. }
  120. frame_profile_frame = RSG::utilities->get_captured_timestamps_frame();
  121. if (print_gpu_profile) {
  122. if (print_frame_profile_ticks_from == 0) {
  123. print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();
  124. }
  125. double total_time = 0.0;
  126. for (int i = 0; i < frame_profile.size() - 1; i++) {
  127. String name = frame_profile[i].name;
  128. if (name[0] == '<' || name[0] == '>') {
  129. continue;
  130. }
  131. double time = frame_profile[i + 1].gpu_msec - frame_profile[i].gpu_msec;
  132. if (name[0] != '<' && name[0] != '>') {
  133. if (print_gpu_profile_task_time.has(name)) {
  134. print_gpu_profile_task_time[name] += time;
  135. } else {
  136. print_gpu_profile_task_time[name] = time;
  137. }
  138. }
  139. }
  140. if (frame_profile.size()) {
  141. total_time = frame_profile[frame_profile.size() - 1].gpu_msec;
  142. }
  143. uint64_t ticks_elapsed = OS::get_singleton()->get_ticks_usec() - print_frame_profile_ticks_from;
  144. print_frame_profile_frame_count++;
  145. if (ticks_elapsed > 1000000) {
  146. print_line("GPU PROFILE (total " + rtos(total_time) + "ms): ");
  147. float print_threshold = 0.01;
  148. for (const KeyValue<String, float> &E : print_gpu_profile_task_time) {
  149. double time = E.value / double(print_frame_profile_frame_count);
  150. if (time > print_threshold) {
  151. print_line("\t-" + E.key + ": " + rtos(time) + "ms");
  152. }
  153. }
  154. print_gpu_profile_task_time.clear();
  155. print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();
  156. print_frame_profile_frame_count = 0;
  157. }
  158. }
  159. RSG::utilities->update_memory_info();
  160. }
  161. double RenderingServerDefault::get_frame_setup_time_cpu() const {
  162. return frame_setup_time;
  163. }
  164. bool RenderingServerDefault::has_changed() const {
  165. return changes > 0;
  166. }
  167. void RenderingServerDefault::_init() {
  168. RSG::rasterizer->initialize();
  169. }
  170. void RenderingServerDefault::_finish() {
  171. if (test_cube.is_valid()) {
  172. free(test_cube);
  173. }
  174. RSG::canvas->finalize();
  175. RSG::rasterizer->finalize();
  176. }
  177. void RenderingServerDefault::init() {
  178. if (create_thread) {
  179. print_verbose("RenderingServerWrapMT: Creating render thread");
  180. DisplayServer::get_singleton()->release_rendering_thread();
  181. if (create_thread) {
  182. thread.start(_thread_callback, this);
  183. print_verbose("RenderingServerWrapMT: Starting render thread");
  184. }
  185. while (!draw_thread_up.is_set()) {
  186. OS::get_singleton()->delay_usec(1000);
  187. }
  188. print_verbose("RenderingServerWrapMT: Finished render thread");
  189. } else {
  190. _init();
  191. }
  192. }
  193. void RenderingServerDefault::finish() {
  194. if (create_thread) {
  195. command_queue.push(this, &RenderingServerDefault::_thread_exit);
  196. if (thread.is_started()) {
  197. thread.wait_to_finish();
  198. }
  199. } else {
  200. _finish();
  201. }
  202. }
  203. /* STATUS INFORMATION */
  204. uint64_t RenderingServerDefault::get_rendering_info(RenderingInfo p_info) {
  205. if (p_info == RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME) {
  206. return RSG::viewport->get_total_objects_drawn();
  207. } else if (p_info == RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME) {
  208. return RSG::viewport->get_total_primitives_drawn();
  209. } else if (p_info == RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME) {
  210. return RSG::viewport->get_total_draw_calls_used();
  211. }
  212. return RSG::utilities->get_rendering_info(p_info);
  213. }
  214. RenderingDevice::DeviceType RenderingServerDefault::get_video_adapter_type() const {
  215. return RSG::utilities->get_video_adapter_type();
  216. }
  217. void RenderingServerDefault::set_frame_profiling_enabled(bool p_enable) {
  218. RSG::utilities->capturing_timestamps = p_enable;
  219. }
  220. uint64_t RenderingServerDefault::get_frame_profile_frame() {
  221. return frame_profile_frame;
  222. }
  223. Vector<RenderingServer::FrameProfileArea> RenderingServerDefault::get_frame_profile() {
  224. return frame_profile;
  225. }
  226. /* TESTING */
  227. void RenderingServerDefault::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
  228. redraw_request();
  229. RSG::rasterizer->set_boot_image(p_image, p_color, p_scale, p_use_filter);
  230. }
  231. Color RenderingServerDefault::get_default_clear_color() {
  232. return RSG::texture_storage->get_default_clear_color();
  233. }
  234. void RenderingServerDefault::set_default_clear_color(const Color &p_color) {
  235. RSG::viewport->set_default_clear_color(p_color);
  236. }
  237. bool RenderingServerDefault::has_feature(Features p_feature) const {
  238. return false;
  239. }
  240. void RenderingServerDefault::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
  241. RSG::scene->sdfgi_set_debug_probe_select(p_position, p_dir);
  242. }
  243. void RenderingServerDefault::set_print_gpu_profile(bool p_enable) {
  244. RSG::utilities->capturing_timestamps = p_enable;
  245. print_gpu_profile = p_enable;
  246. }
  247. RID RenderingServerDefault::get_test_cube() {
  248. if (!test_cube.is_valid()) {
  249. test_cube = _make_test_cube();
  250. }
  251. return test_cube;
  252. }
  253. bool RenderingServerDefault::has_os_feature(const String &p_feature) const {
  254. if (RSG::utilities) {
  255. return RSG::utilities->has_os_feature(p_feature);
  256. } else {
  257. return false;
  258. }
  259. }
  260. void RenderingServerDefault::set_debug_generate_wireframes(bool p_generate) {
  261. RSG::utilities->set_debug_generate_wireframes(p_generate);
  262. }
  263. bool RenderingServerDefault::is_low_end() const {
  264. return RendererCompositor::is_low_end();
  265. }
  266. Size2i RenderingServerDefault::get_maximum_viewport_size() const {
  267. if (RSG::utilities) {
  268. return RSG::utilities->get_maximum_viewport_size();
  269. } else {
  270. return Size2i();
  271. }
  272. }
  273. void RenderingServerDefault::_thread_exit() {
  274. exit.set();
  275. }
  276. void RenderingServerDefault::_thread_draw(bool p_swap_buffers, double frame_step) {
  277. _draw(p_swap_buffers, frame_step);
  278. }
  279. void RenderingServerDefault::_thread_flush() {
  280. }
  281. void RenderingServerDefault::_thread_callback(void *_instance) {
  282. RenderingServerDefault *vsmt = reinterpret_cast<RenderingServerDefault *>(_instance);
  283. vsmt->_thread_loop();
  284. }
  285. void RenderingServerDefault::_thread_loop() {
  286. server_thread = Thread::get_caller_id();
  287. DisplayServer::get_singleton()->make_rendering_thread();
  288. _init();
  289. draw_thread_up.set();
  290. while (!exit.is_set()) {
  291. // flush commands one by one, until exit is requested
  292. command_queue.wait_and_flush();
  293. }
  294. command_queue.flush_all(); // flush all
  295. _finish();
  296. }
  297. /* EVENT QUEUING */
  298. void RenderingServerDefault::sync() {
  299. if (create_thread) {
  300. command_queue.push_and_sync(this, &RenderingServerDefault::_thread_flush);
  301. } else {
  302. command_queue.flush_all(); //flush all pending from other threads
  303. }
  304. }
  305. void RenderingServerDefault::draw(bool p_swap_buffers, double frame_step) {
  306. ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Manually triggering the draw function from the RenderingServer can only be done on the main thread. Call this function from the main thread or use call_deferred().");
  307. if (create_thread) {
  308. command_queue.push(this, &RenderingServerDefault::_thread_draw, p_swap_buffers, frame_step);
  309. } else {
  310. _draw(p_swap_buffers, frame_step);
  311. }
  312. }
  313. void RenderingServerDefault::_call_on_render_thread(const Callable &p_callable) {
  314. p_callable.call();
  315. }
  316. RenderingServerDefault::RenderingServerDefault(bool p_create_thread) :
  317. command_queue(p_create_thread) {
  318. RenderingServer::init();
  319. create_thread = p_create_thread;
  320. if (!p_create_thread) {
  321. server_thread = Thread::get_caller_id();
  322. } else {
  323. server_thread = 0;
  324. }
  325. RSG::threaded = p_create_thread;
  326. RSG::canvas = memnew(RendererCanvasCull);
  327. RSG::viewport = memnew(RendererViewport);
  328. RendererSceneCull *sr = memnew(RendererSceneCull);
  329. RSG::camera_attributes = memnew(RendererCameraAttributes);
  330. RSG::scene = sr;
  331. RSG::rasterizer = RendererCompositor::create();
  332. RSG::utilities = RSG::rasterizer->get_utilities();
  333. RSG::light_storage = RSG::rasterizer->get_light_storage();
  334. RSG::material_storage = RSG::rasterizer->get_material_storage();
  335. RSG::mesh_storage = RSG::rasterizer->get_mesh_storage();
  336. RSG::particles_storage = RSG::rasterizer->get_particles_storage();
  337. RSG::texture_storage = RSG::rasterizer->get_texture_storage();
  338. RSG::gi = RSG::rasterizer->get_gi();
  339. RSG::fog = RSG::rasterizer->get_fog();
  340. RSG::canvas_render = RSG::rasterizer->get_canvas();
  341. sr->set_scene_render(RSG::rasterizer->get_scene());
  342. frame_profile_frame = 0;
  343. }
  344. RenderingServerDefault::~RenderingServerDefault() {
  345. memdelete(RSG::canvas);
  346. memdelete(RSG::viewport);
  347. memdelete(RSG::rasterizer);
  348. memdelete(RSG::scene);
  349. memdelete(RSG::camera_attributes);
  350. }