visual_server_raster.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**************************************************************************/
  2. /* visual_server_raster.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 "visual_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 "visual_server_canvas.h"
  36. #include "visual_server_globals.h"
  37. #include "visual_server_scene.h"
  38. // careful, these may run in different threads than the visual server
  39. int VisualServerRaster::changes[2] = { 0 };
  40. /* BLACK BARS */
  41. void VisualServerRaster::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 VisualServerRaster::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 VisualServerRaster::_draw_margins() {
  54. VSG::canvas_render->draw_window_margins(black_margin, black_image);
  55. };
  56. /* FREE */
  57. void VisualServerRaster::free(RID p_rid) {
  58. if (!p_rid.is_valid()) {
  59. ERR_FAIL_MSG("VisualServer attempted to free a NULL RID.");
  60. return;
  61. }
  62. if (VSG::storage->free(p_rid)) {
  63. return;
  64. }
  65. if (VSG::canvas->free(p_rid)) {
  66. return;
  67. }
  68. if (VSG::viewport->free(p_rid)) {
  69. return;
  70. }
  71. if (VSG::scene->free(p_rid)) {
  72. return;
  73. }
  74. if (VSG::scene_render->free(p_rid)) {
  75. return;
  76. }
  77. ERR_FAIL_MSG("RID not found by VisualServer.");
  78. }
  79. /* EVENT QUEUING */
  80. void VisualServerRaster::request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata) {
  81. ERR_FAIL_NULL(p_where);
  82. FrameDrawnCallbacks fdc;
  83. fdc.object = p_where->get_instance_id();
  84. fdc.method = p_method;
  85. fdc.param = p_userdata;
  86. frame_drawn_callbacks.push_back(fdc);
  87. }
  88. void VisualServerRaster::draw(bool p_swap_buffers, double frame_step) {
  89. //needs to be done before changes is reset to 0, to not force the editor to redraw
  90. VS::get_singleton()->emit_signal("frame_pre_draw");
  91. changes[0] = 0;
  92. changes[1] = 0;
  93. VSG::rasterizer->begin_frame(frame_step);
  94. VSG::scene->update_dirty_instances(); //update scene stuff
  95. VSG::viewport->draw_viewports();
  96. VSG::scene->render_probes();
  97. _draw_margins();
  98. VSG::rasterizer->end_frame(p_swap_buffers);
  99. while (frame_drawn_callbacks.front()) {
  100. Object *obj = ObjectDB::get_instance(frame_drawn_callbacks.front()->get().object);
  101. if (obj) {
  102. Variant::CallError ce;
  103. const Variant *v = &frame_drawn_callbacks.front()->get().param;
  104. obj->call(frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  105. if (ce.error != Variant::CallError::CALL_OK) {
  106. String err = Variant::get_call_error_text(obj, frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  107. ERR_PRINT("Error calling frame drawn function: " + err);
  108. }
  109. }
  110. frame_drawn_callbacks.pop_front();
  111. }
  112. VS::get_singleton()->emit_signal("frame_post_draw");
  113. }
  114. void VisualServerRaster::sync() {
  115. }
  116. void VisualServerRaster::set_physics_interpolation_enabled(bool p_enabled) {
  117. VSG::scene->set_physics_interpolation_enabled(p_enabled);
  118. VSG::canvas->set_physics_interpolation_enabled(p_enabled);
  119. }
  120. void VisualServerRaster::tick() {
  121. VSG::scene->tick();
  122. VSG::canvas->tick();
  123. }
  124. void VisualServerRaster::pre_draw(bool p_will_draw) {
  125. VSG::scene->pre_draw(p_will_draw);
  126. }
  127. bool VisualServerRaster::has_changed(ChangedPriority p_priority) const {
  128. switch (p_priority) {
  129. default: {
  130. return (changes[0] > 0) || (changes[1] > 0);
  131. } break;
  132. case CHANGED_PRIORITY_LOW: {
  133. return changes[0] > 0;
  134. } break;
  135. case CHANGED_PRIORITY_HIGH: {
  136. return changes[1] > 0;
  137. } break;
  138. }
  139. }
  140. void VisualServerRaster::init() {
  141. VSG::rasterizer->initialize();
  142. }
  143. void VisualServerRaster::finish() {
  144. if (test_cube.is_valid()) {
  145. free(test_cube);
  146. }
  147. VSG::rasterizer->finalize();
  148. }
  149. /* STATUS INFORMATION */
  150. uint64_t VisualServerRaster::get_render_info(RenderInfo p_info) {
  151. return VSG::storage->get_render_info(p_info);
  152. }
  153. String VisualServerRaster::get_video_adapter_name() const {
  154. return VSG::storage->get_video_adapter_name();
  155. }
  156. String VisualServerRaster::get_video_adapter_vendor() const {
  157. return VSG::storage->get_video_adapter_vendor();
  158. }
  159. /* TESTING */
  160. void VisualServerRaster::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
  161. redraw_request();
  162. VSG::rasterizer->set_boot_image(p_image, p_color, p_scale, p_use_filter);
  163. }
  164. void VisualServerRaster::set_default_clear_color(const Color &p_color) {
  165. VSG::viewport->set_default_clear_color(p_color);
  166. }
  167. void VisualServerRaster::set_shader_time_scale(float p_scale) {
  168. VSG::rasterizer->set_shader_time_scale(p_scale);
  169. }
  170. bool VisualServerRaster::has_feature(Features p_feature) const {
  171. return false;
  172. }
  173. RID VisualServerRaster::get_test_cube() {
  174. if (!test_cube.is_valid()) {
  175. test_cube = _make_test_cube();
  176. }
  177. return test_cube;
  178. }
  179. bool VisualServerRaster::has_os_feature(const String &p_feature) const {
  180. return VSG::storage->has_os_feature(p_feature);
  181. }
  182. void VisualServerRaster::set_debug_generate_wireframes(bool p_generate) {
  183. VSG::storage->set_debug_generate_wireframes(p_generate);
  184. }
  185. void VisualServerRaster::call_set_use_vsync(bool p_enable) {
  186. OS::get_singleton()->_set_use_vsync(p_enable);
  187. }
  188. bool VisualServerRaster::is_low_end() const {
  189. return VSG::rasterizer->is_low_end();
  190. }
  191. VisualServerRaster::VisualServerRaster() {
  192. VSG::canvas = memnew(VisualServerCanvas);
  193. VSG::viewport = memnew(VisualServerViewport);
  194. VSG::scene = memnew(VisualServerScene);
  195. VSG::rasterizer = Rasterizer::create();
  196. VSG::storage = VSG::rasterizer->get_storage();
  197. VSG::canvas_render = VSG::rasterizer->get_canvas();
  198. VSG::scene_render = VSG::rasterizer->get_scene();
  199. for (int i = 0; i < 4; i++) {
  200. black_margin[i] = 0;
  201. black_image[i] = RID();
  202. }
  203. }
  204. VisualServerRaster::~VisualServerRaster() {
  205. memdelete(VSG::canvas);
  206. memdelete(VSG::viewport);
  207. memdelete(VSG::rasterizer);
  208. memdelete(VSG::scene);
  209. }