rendering_server_raster.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*************************************************************************/
  2. /* rendering_server_raster.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_raster.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #include "core/sort_array.h"
  35. #include "rendering_server_canvas.h"
  36. #include "rendering_server_globals.h"
  37. #include "rendering_server_scene.h"
  38. // careful, these may run in different threads than the visual server
  39. int RenderingServerRaster::changes = 0;
  40. /* BLACK BARS */
  41. void RenderingServerRaster::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) {
  42. black_margin[MARGIN_LEFT] = p_left;
  43. black_margin[MARGIN_TOP] = p_top;
  44. black_margin[MARGIN_RIGHT] = p_right;
  45. black_margin[MARGIN_BOTTOM] = p_bottom;
  46. }
  47. void RenderingServerRaster::black_bars_set_images(RID p_left, RID p_top, RID p_right, RID p_bottom) {
  48. black_image[MARGIN_LEFT] = p_left;
  49. black_image[MARGIN_TOP] = p_top;
  50. black_image[MARGIN_RIGHT] = p_right;
  51. black_image[MARGIN_BOTTOM] = p_bottom;
  52. }
  53. void RenderingServerRaster::_draw_margins() {
  54. RSG::canvas_render->draw_window_margins(black_margin, black_image);
  55. };
  56. /* FREE */
  57. void RenderingServerRaster::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. if (RSG::scene_render->free(p_rid)) {
  71. return;
  72. }
  73. }
  74. /* EVENT QUEUING */
  75. void RenderingServerRaster::request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata) {
  76. ERR_FAIL_NULL(p_where);
  77. FrameDrawnCallbacks fdc;
  78. fdc.object = p_where->get_instance_id();
  79. fdc.method = p_method;
  80. fdc.param = p_userdata;
  81. frame_drawn_callbacks.push_back(fdc);
  82. }
  83. void RenderingServerRaster::draw(bool p_swap_buffers, double frame_step) {
  84. //needs to be done before changes is reset to 0, to not force the editor to redraw
  85. RS::get_singleton()->emit_signal("frame_pre_draw");
  86. changes = 0;
  87. RSG::rasterizer->begin_frame(frame_step);
  88. TIMESTAMP_BEGIN()
  89. RSG::scene_render->update(); //update scenes stuff before updating instances
  90. RSG::scene->update_dirty_instances(); //update scene stuff
  91. RSG::scene->render_probes();
  92. RSG::viewport->draw_viewports();
  93. RSG::canvas_render->update();
  94. _draw_margins();
  95. RSG::rasterizer->end_frame(p_swap_buffers);
  96. while (frame_drawn_callbacks.front()) {
  97. Object *obj = ObjectDB::get_instance(frame_drawn_callbacks.front()->get().object);
  98. if (obj) {
  99. Callable::CallError ce;
  100. const Variant *v = &frame_drawn_callbacks.front()->get().param;
  101. obj->call(frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  102. if (ce.error != Callable::CallError::CALL_OK) {
  103. String err = Variant::get_call_error_text(obj, frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  104. ERR_PRINT("Error calling frame drawn function: " + err);
  105. }
  106. }
  107. frame_drawn_callbacks.pop_front();
  108. }
  109. RS::get_singleton()->emit_signal("frame_post_draw");
  110. if (RSG::storage->get_captured_timestamps_count()) {
  111. Vector<FrameProfileArea> new_profile;
  112. if (RSG::storage->capturing_timestamps) {
  113. new_profile.resize(RSG::storage->get_captured_timestamps_count());
  114. }
  115. uint64_t base_cpu = RSG::storage->get_captured_timestamp_cpu_time(0);
  116. uint64_t base_gpu = RSG::storage->get_captured_timestamp_gpu_time(0);
  117. for (uint32_t i = 0; i < RSG::storage->get_captured_timestamps_count(); i++) {
  118. uint64_t time_cpu = RSG::storage->get_captured_timestamp_cpu_time(i);
  119. uint64_t time_gpu = RSG::storage->get_captured_timestamp_gpu_time(i);
  120. String name = RSG::storage->get_captured_timestamp_name(i);
  121. if (name.begins_with("vp_")) {
  122. RSG::viewport->handle_timestamp(name, time_cpu, time_gpu);
  123. }
  124. if (RSG::storage->capturing_timestamps) {
  125. new_profile.write[i].gpu_msec = float((time_gpu - base_gpu) / 1000) / 1000.0;
  126. new_profile.write[i].cpu_msec = float(time_cpu - base_cpu) / 1000.0;
  127. new_profile.write[i].name = RSG::storage->get_captured_timestamp_name(i);
  128. }
  129. }
  130. frame_profile = new_profile;
  131. }
  132. frame_profile_frame = RSG::storage->get_captured_timestamps_frame();
  133. }
  134. void RenderingServerRaster::sync() {
  135. }
  136. bool RenderingServerRaster::has_changed() const {
  137. return changes > 0;
  138. }
  139. void RenderingServerRaster::init() {
  140. RSG::rasterizer->initialize();
  141. }
  142. void RenderingServerRaster::finish() {
  143. if (test_cube.is_valid()) {
  144. free(test_cube);
  145. }
  146. RSG::rasterizer->finalize();
  147. }
  148. /* STATUS INFORMATION */
  149. int RenderingServerRaster::get_render_info(RenderInfo p_info) {
  150. return RSG::storage->get_render_info(p_info);
  151. }
  152. String RenderingServerRaster::get_video_adapter_name() const {
  153. return RSG::storage->get_video_adapter_name();
  154. }
  155. String RenderingServerRaster::get_video_adapter_vendor() const {
  156. return RSG::storage->get_video_adapter_vendor();
  157. }
  158. void RenderingServerRaster::set_frame_profiling_enabled(bool p_enable) {
  159. RSG::storage->capturing_timestamps = p_enable;
  160. }
  161. uint64_t RenderingServerRaster::get_frame_profile_frame() {
  162. return frame_profile_frame;
  163. }
  164. Vector<RenderingServer::FrameProfileArea> RenderingServerRaster::get_frame_profile() {
  165. return frame_profile;
  166. }
  167. /* TESTING */
  168. void RenderingServerRaster::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
  169. redraw_request();
  170. RSG::rasterizer->set_boot_image(p_image, p_color, p_scale, p_use_filter);
  171. }
  172. void RenderingServerRaster::set_default_clear_color(const Color &p_color) {
  173. RSG::viewport->set_default_clear_color(p_color);
  174. }
  175. bool RenderingServerRaster::has_feature(Features p_feature) const {
  176. return false;
  177. }
  178. void RenderingServerRaster::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
  179. RSG::scene_render->sdfgi_set_debug_probe_select(p_position, p_dir);
  180. }
  181. RID RenderingServerRaster::get_test_cube() {
  182. if (!test_cube.is_valid()) {
  183. test_cube = _make_test_cube();
  184. }
  185. return test_cube;
  186. }
  187. bool RenderingServerRaster::has_os_feature(const String &p_feature) const {
  188. return RSG::storage->has_os_feature(p_feature);
  189. }
  190. void RenderingServerRaster::set_debug_generate_wireframes(bool p_generate) {
  191. RSG::storage->set_debug_generate_wireframes(p_generate);
  192. }
  193. void RenderingServerRaster::call_set_use_vsync(bool p_enable) {
  194. DisplayServer::get_singleton()->_set_use_vsync(p_enable);
  195. }
  196. bool RenderingServerRaster::is_low_end() const {
  197. // FIXME: Commented out when rebasing vulkan branch on master,
  198. // causes a crash, it seems rasterizer is not initialized yet the
  199. // first time it's called.
  200. //return RSG::rasterizer->is_low_end();
  201. return false;
  202. }
  203. RenderingServerRaster::RenderingServerRaster() {
  204. RSG::canvas = memnew(RenderingServerCanvas);
  205. RSG::viewport = memnew(RenderingServerViewport);
  206. RSG::scene = memnew(RenderingServerScene);
  207. RSG::rasterizer = Rasterizer::create();
  208. RSG::storage = RSG::rasterizer->get_storage();
  209. RSG::canvas_render = RSG::rasterizer->get_canvas();
  210. RSG::scene_render = RSG::rasterizer->get_scene();
  211. frame_profile_frame = 0;
  212. for (int i = 0; i < 4; i++) {
  213. black_margin[i] = 0;
  214. black_image[i] = RID();
  215. }
  216. }
  217. RenderingServerRaster::~RenderingServerRaster() {
  218. memdelete(RSG::canvas);
  219. memdelete(RSG::viewport);
  220. memdelete(RSG::rasterizer);
  221. memdelete(RSG::scene);
  222. }