rendering_server_default.cpp 15 KB

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