rendering_server_default.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*************************************************************************/
  2. /* rendering_server_default.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "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 visual server
  39. int RenderingServerDefault::changes = 0;
  40. /* BLACK BARS */
  41. void RenderingServerDefault::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) {
  42. black_margin[SIDE_LEFT] = p_left;
  43. black_margin[SIDE_TOP] = p_top;
  44. black_margin[SIDE_RIGHT] = p_right;
  45. black_margin[SIDE_BOTTOM] = p_bottom;
  46. }
  47. void RenderingServerDefault::black_bars_set_images(RID p_left, RID p_top, RID p_right, RID p_bottom) {
  48. black_image[SIDE_LEFT] = p_left;
  49. black_image[SIDE_TOP] = p_top;
  50. black_image[SIDE_RIGHT] = p_right;
  51. black_image[SIDE_BOTTOM] = p_bottom;
  52. }
  53. void RenderingServerDefault::_draw_margins() {
  54. RSG::canvas_render->draw_window_margins(black_margin, black_image);
  55. };
  56. /* FREE */
  57. void RenderingServerDefault::_free(RID p_rid) {
  58. if (RSG::storage->free(p_rid)) {
  59. return;
  60. }
  61. if (RSG::canvas->free(p_rid)) {
  62. return;
  63. }
  64. if (RSG::viewport->free(p_rid)) {
  65. return;
  66. }
  67. if (RSG::scene->free(p_rid)) {
  68. return;
  69. }
  70. }
  71. /* EVENT QUEUING */
  72. void RenderingServerDefault::request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata) {
  73. ERR_FAIL_NULL(p_where);
  74. FrameDrawnCallbacks fdc;
  75. fdc.object = p_where->get_instance_id();
  76. fdc.method = p_method;
  77. fdc.param = p_userdata;
  78. frame_drawn_callbacks.push_back(fdc);
  79. }
  80. void RenderingServerDefault::_draw(bool p_swap_buffers, double frame_step) {
  81. //needs to be done before changes is reset to 0, to not force the editor to redraw
  82. RS::get_singleton()->emit_signal("frame_pre_draw");
  83. changes = 0;
  84. RSG::rasterizer->begin_frame(frame_step);
  85. TIMESTAMP_BEGIN()
  86. uint64_t time_usec = OS::get_singleton()->get_ticks_usec();
  87. RSG::scene->update(); //update scenes stuff before updating instances
  88. frame_setup_time = double(OS::get_singleton()->get_ticks_usec() - time_usec) / 1000.0;
  89. RSG::storage->update_particles(); //need to be done after instances are updated (colliders and particle transforms), and colliders are rendered
  90. RSG::scene->render_probes();
  91. RSG::viewport->draw_viewports();
  92. RSG::canvas_render->update();
  93. _draw_margins();
  94. RSG::rasterizer->end_frame(p_swap_buffers);
  95. while (frame_drawn_callbacks.front()) {
  96. Object *obj = ObjectDB::get_instance(frame_drawn_callbacks.front()->get().object);
  97. if (obj) {
  98. Callable::CallError ce;
  99. const Variant *v = &frame_drawn_callbacks.front()->get().param;
  100. obj->call(frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  101. if (ce.error != Callable::CallError::CALL_OK) {
  102. String err = Variant::get_call_error_text(obj, frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  103. ERR_PRINT("Error calling frame drawn function: " + err);
  104. }
  105. }
  106. frame_drawn_callbacks.pop_front();
  107. }
  108. RS::get_singleton()->emit_signal("frame_post_draw");
  109. if (RSG::storage->get_captured_timestamps_count()) {
  110. Vector<FrameProfileArea> new_profile;
  111. if (RSG::storage->capturing_timestamps) {
  112. new_profile.resize(RSG::storage->get_captured_timestamps_count());
  113. }
  114. uint64_t base_cpu = RSG::storage->get_captured_timestamp_cpu_time(0);
  115. uint64_t base_gpu = RSG::storage->get_captured_timestamp_gpu_time(0);
  116. for (uint32_t i = 0; i < RSG::storage->get_captured_timestamps_count(); i++) {
  117. uint64_t time_cpu = RSG::storage->get_captured_timestamp_cpu_time(i);
  118. uint64_t time_gpu = RSG::storage->get_captured_timestamp_gpu_time(i);
  119. String name = RSG::storage->get_captured_timestamp_name(i);
  120. if (name.begins_with("vp_")) {
  121. RSG::viewport->handle_timestamp(name, time_cpu, time_gpu);
  122. }
  123. if (RSG::storage->capturing_timestamps) {
  124. new_profile.write[i].gpu_msec = float((time_gpu - base_gpu) / 1000) / 1000.0;
  125. new_profile.write[i].cpu_msec = float(time_cpu - base_cpu) / 1000.0;
  126. new_profile.write[i].name = RSG::storage->get_captured_timestamp_name(i);
  127. }
  128. }
  129. frame_profile = new_profile;
  130. }
  131. frame_profile_frame = RSG::storage->get_captured_timestamps_frame();
  132. if (print_gpu_profile) {
  133. if (print_frame_profile_ticks_from == 0) {
  134. print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();
  135. }
  136. float total_time = 0.0;
  137. for (int i = 0; i < frame_profile.size() - 1; i++) {
  138. String name = frame_profile[i].name;
  139. if (name[0] == '<' || name[0] == '>') {
  140. continue;
  141. }
  142. float time = frame_profile[i + 1].gpu_msec - frame_profile[i].gpu_msec;
  143. if (name[0] != '<' && name[0] != '>') {
  144. if (print_gpu_profile_task_time.has(name)) {
  145. print_gpu_profile_task_time[name] += time;
  146. } else {
  147. print_gpu_profile_task_time[name] = time;
  148. }
  149. }
  150. }
  151. if (frame_profile.size()) {
  152. total_time = frame_profile[frame_profile.size() - 1].gpu_msec;
  153. }
  154. uint64_t ticks_elapsed = OS::get_singleton()->get_ticks_usec() - print_frame_profile_ticks_from;
  155. print_frame_profile_frame_count++;
  156. if (ticks_elapsed > 1000000) {
  157. print_line("GPU PROFILE (total " + rtos(total_time) + "ms): ");
  158. float print_threshold = 0.01;
  159. for (OrderedHashMap<String, float>::Element E = print_gpu_profile_task_time.front(); E; E = E.next()) {
  160. float time = E.value() / float(print_frame_profile_frame_count);
  161. if (time > print_threshold) {
  162. print_line("\t-" + E.key() + ": " + rtos(time) + "ms");
  163. }
  164. }
  165. print_gpu_profile_task_time.clear();
  166. print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();
  167. print_frame_profile_frame_count = 0;
  168. }
  169. }
  170. }
  171. float RenderingServerDefault::get_frame_setup_time_cpu() const {
  172. return frame_setup_time;
  173. }
  174. bool RenderingServerDefault::has_changed() const {
  175. return changes > 0;
  176. }
  177. void RenderingServerDefault::_init() {
  178. RSG::rasterizer->initialize();
  179. }
  180. void RenderingServerDefault::_finish() {
  181. if (test_cube.is_valid()) {
  182. free(test_cube);
  183. }
  184. RSG::rasterizer->finalize();
  185. }
  186. void RenderingServerDefault::init() {
  187. if (create_thread) {
  188. print_verbose("RenderingServerWrapMT: Creating render thread");
  189. DisplayServer::get_singleton()->release_rendering_thread();
  190. if (create_thread) {
  191. thread.start(_thread_callback, this);
  192. print_verbose("RenderingServerWrapMT: Starting render thread");
  193. }
  194. while (!draw_thread_up.is_set()) {
  195. OS::get_singleton()->delay_usec(1000);
  196. }
  197. print_verbose("RenderingServerWrapMT: Finished render thread");
  198. } else {
  199. _init();
  200. }
  201. }
  202. void RenderingServerDefault::finish() {
  203. if (create_thread) {
  204. command_queue.push(this, &RenderingServerDefault::_thread_exit);
  205. thread.wait_to_finish();
  206. } else {
  207. _finish();
  208. }
  209. }
  210. /* STATUS INFORMATION */
  211. uint64_t RenderingServerDefault::get_render_info(RenderInfo p_info) {
  212. return RSG::storage->get_render_info(p_info);
  213. }
  214. String RenderingServerDefault::get_video_adapter_name() const {
  215. return RSG::storage->get_video_adapter_name();
  216. }
  217. String RenderingServerDefault::get_video_adapter_vendor() const {
  218. return RSG::storage->get_video_adapter_vendor();
  219. }
  220. void RenderingServerDefault::set_frame_profiling_enabled(bool p_enable) {
  221. RSG::storage->capturing_timestamps = p_enable;
  222. }
  223. uint64_t RenderingServerDefault::get_frame_profile_frame() {
  224. return frame_profile_frame;
  225. }
  226. Vector<RenderingServer::FrameProfileArea> RenderingServerDefault::get_frame_profile() {
  227. return frame_profile;
  228. }
  229. /* TESTING */
  230. void RenderingServerDefault::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
  231. redraw_request();
  232. RSG::rasterizer->set_boot_image(p_image, p_color, p_scale, p_use_filter);
  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::storage->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. return RSG::storage->has_os_feature(p_feature);
  255. }
  256. void RenderingServerDefault::set_debug_generate_wireframes(bool p_generate) {
  257. RSG::storage->set_debug_generate_wireframes(p_generate);
  258. }
  259. bool RenderingServerDefault::is_low_end() const {
  260. // FIXME: Commented out when rebasing vulkan branch on master,
  261. // causes a crash, it seems rasterizer is not initialized yet the
  262. // first time it's called.
  263. //return RSG::rasterizer->is_low_end();
  264. return false;
  265. }
  266. void RenderingServerDefault::_thread_exit() {
  267. exit.set();
  268. }
  269. void RenderingServerDefault::_thread_draw(bool p_swap_buffers, double frame_step) {
  270. if (!draw_pending.decrement()) {
  271. _draw(p_swap_buffers, frame_step);
  272. }
  273. }
  274. void RenderingServerDefault::_thread_flush() {
  275. draw_pending.decrement();
  276. }
  277. void RenderingServerDefault::_thread_callback(void *_instance) {
  278. RenderingServerDefault *vsmt = reinterpret_cast<RenderingServerDefault *>(_instance);
  279. vsmt->_thread_loop();
  280. }
  281. void RenderingServerDefault::_thread_loop() {
  282. server_thread = Thread::get_caller_id();
  283. DisplayServer::get_singleton()->make_rendering_thread();
  284. _init();
  285. draw_thread_up.set();
  286. while (!exit.is_set()) {
  287. // flush commands one by one, until exit is requested
  288. command_queue.wait_and_flush_one();
  289. }
  290. command_queue.flush_all(); // flush all
  291. _finish();
  292. }
  293. /* EVENT QUEUING */
  294. void RenderingServerDefault::sync() {
  295. if (create_thread) {
  296. draw_pending.increment();
  297. command_queue.push_and_sync(this, &RenderingServerDefault::_thread_flush);
  298. } else {
  299. command_queue.flush_all(); //flush all pending from other threads
  300. }
  301. }
  302. void RenderingServerDefault::draw(bool p_swap_buffers, double frame_step) {
  303. if (create_thread) {
  304. draw_pending.increment();
  305. command_queue.push(this, &RenderingServerDefault::_thread_draw, p_swap_buffers, frame_step);
  306. } else {
  307. _draw(p_swap_buffers, frame_step);
  308. }
  309. }
  310. RenderingServerDefault::RenderingServerDefault(bool p_create_thread) :
  311. command_queue(p_create_thread) {
  312. create_thread = p_create_thread;
  313. if (!p_create_thread) {
  314. server_thread = Thread::get_caller_id();
  315. } else {
  316. server_thread = 0;
  317. }
  318. RSG::canvas = memnew(RendererCanvasCull);
  319. RSG::viewport = memnew(RendererViewport);
  320. RendererSceneCull *sr = memnew(RendererSceneCull);
  321. RSG::scene = sr;
  322. RSG::rasterizer = RendererCompositor::create();
  323. RSG::storage = RSG::rasterizer->get_storage();
  324. RSG::canvas_render = RSG::rasterizer->get_canvas();
  325. sr->set_scene_render(RSG::rasterizer->get_scene());
  326. frame_profile_frame = 0;
  327. for (int i = 0; i < 4; i++) {
  328. black_margin[i] = 0;
  329. black_image[i] = RID();
  330. }
  331. }
  332. RenderingServerDefault::~RenderingServerDefault() {
  333. memdelete(RSG::canvas);
  334. memdelete(RSG::viewport);
  335. memdelete(RSG::rasterizer);
  336. memdelete(RSG::scene);
  337. }