visual_server_raster.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*************************************************************************/
  2. /* visual_server_raster.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 "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 = 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 (VSG::storage->free(p_rid))
  59. return;
  60. if (VSG::canvas->free(p_rid))
  61. return;
  62. if (VSG::viewport->free(p_rid))
  63. return;
  64. if (VSG::scene->free(p_rid))
  65. return;
  66. if (VSG::scene_render->free(p_rid))
  67. return;
  68. }
  69. /* EVENT QUEUING */
  70. void VisualServerRaster::request_frame_drawn_callback(Object *p_where, const StringName &p_method, const Variant &p_userdata) {
  71. ERR_FAIL_NULL(p_where);
  72. FrameDrawnCallbacks fdc;
  73. fdc.object = p_where->get_instance_id();
  74. fdc.method = p_method;
  75. fdc.param = p_userdata;
  76. frame_drawn_callbacks.push_back(fdc);
  77. }
  78. void VisualServerRaster::draw(bool p_swap_buffers, double frame_step) {
  79. //needs to be done before changes is reset to 0, to not force the editor to redraw
  80. VS::get_singleton()->emit_signal("frame_pre_draw");
  81. changes = 0;
  82. VSG::rasterizer->begin_frame(frame_step);
  83. VSG::scene->update_dirty_instances(); //update scene stuff
  84. VSG::viewport->draw_viewports();
  85. VSG::scene->render_probes();
  86. _draw_margins();
  87. VSG::rasterizer->end_frame(p_swap_buffers);
  88. while (frame_drawn_callbacks.front()) {
  89. Object *obj = ObjectDB::get_instance(frame_drawn_callbacks.front()->get().object);
  90. if (obj) {
  91. Variant::CallError ce;
  92. const Variant *v = &frame_drawn_callbacks.front()->get().param;
  93. obj->call(frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  94. if (ce.error != Variant::CallError::CALL_OK) {
  95. String err = Variant::get_call_error_text(obj, frame_drawn_callbacks.front()->get().method, &v, 1, ce);
  96. ERR_PRINTS("Error calling frame drawn function: " + err);
  97. }
  98. }
  99. frame_drawn_callbacks.pop_front();
  100. }
  101. VS::get_singleton()->emit_signal("frame_post_draw");
  102. }
  103. void VisualServerRaster::sync() {
  104. }
  105. bool VisualServerRaster::has_changed() const {
  106. return changes > 0;
  107. }
  108. void VisualServerRaster::init() {
  109. VSG::rasterizer->initialize();
  110. }
  111. void VisualServerRaster::finish() {
  112. if (test_cube.is_valid()) {
  113. free(test_cube);
  114. }
  115. VSG::rasterizer->finalize();
  116. }
  117. /* STATUS INFORMATION */
  118. int VisualServerRaster::get_render_info(RenderInfo p_info) {
  119. return VSG::storage->get_render_info(p_info);
  120. }
  121. String VisualServerRaster::get_video_adapter_name() const {
  122. return VSG::storage->get_video_adapter_name();
  123. }
  124. String VisualServerRaster::get_video_adapter_vendor() const {
  125. return VSG::storage->get_video_adapter_vendor();
  126. }
  127. /* TESTING */
  128. void VisualServerRaster::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
  129. redraw_request();
  130. VSG::rasterizer->set_boot_image(p_image, p_color, p_scale, p_use_filter);
  131. }
  132. void VisualServerRaster::set_default_clear_color(const Color &p_color) {
  133. VSG::viewport->set_default_clear_color(p_color);
  134. }
  135. void VisualServerRaster::set_shader_time_scale(float p_scale) {
  136. VSG::rasterizer->set_shader_time_scale(p_scale);
  137. }
  138. bool VisualServerRaster::has_feature(Features p_feature) const {
  139. return false;
  140. }
  141. RID VisualServerRaster::get_test_cube() {
  142. if (!test_cube.is_valid()) {
  143. test_cube = _make_test_cube();
  144. }
  145. return test_cube;
  146. }
  147. bool VisualServerRaster::has_os_feature(const String &p_feature) const {
  148. return VSG::storage->has_os_feature(p_feature);
  149. }
  150. void VisualServerRaster::set_debug_generate_wireframes(bool p_generate) {
  151. VSG::storage->set_debug_generate_wireframes(p_generate);
  152. }
  153. void VisualServerRaster::call_set_use_vsync(bool p_enable) {
  154. OS::get_singleton()->_set_use_vsync(p_enable);
  155. }
  156. bool VisualServerRaster::is_low_end() const {
  157. return VSG::rasterizer->is_low_end();
  158. }
  159. VisualServerRaster::VisualServerRaster() {
  160. VSG::canvas = memnew(VisualServerCanvas);
  161. VSG::viewport = memnew(VisualServerViewport);
  162. VSG::scene = memnew(VisualServerScene);
  163. VSG::rasterizer = Rasterizer::create();
  164. VSG::storage = VSG::rasterizer->get_storage();
  165. VSG::canvas_render = VSG::rasterizer->get_canvas();
  166. VSG::scene_render = VSG::rasterizer->get_scene();
  167. for (int i = 0; i < 4; i++) {
  168. black_margin[i] = 0;
  169. black_image[i] = RID();
  170. }
  171. }
  172. VisualServerRaster::~VisualServerRaster() {
  173. memdelete(VSG::canvas);
  174. memdelete(VSG::viewport);
  175. memdelete(VSG::rasterizer);
  176. memdelete(VSG::scene);
  177. }