rendering_server_default.cpp 16 KB

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