visual_server_canvas.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**************************************************************************/
  2. /* visual_server_canvas.h */
  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. #ifndef VISUAL_SERVER_CANVAS_H
  31. #define VISUAL_SERVER_CANVAS_H
  32. #include "rasterizer.h"
  33. #include "visual_server_constants.h"
  34. #include "visual_server_viewport.h"
  35. class VisualServerCanvas {
  36. public:
  37. struct Item : public RasterizerCanvas::Item {
  38. RID parent; // canvas it belongs to
  39. List<Item *>::Element *E;
  40. int z_index;
  41. bool z_relative;
  42. bool sort_y;
  43. Color modulate;
  44. Color self_modulate;
  45. bool use_parent_material;
  46. int index;
  47. bool children_order_dirty;
  48. int ysort_children_count;
  49. Color ysort_modulate;
  50. Transform2D ysort_xform;
  51. Vector2 ysort_pos;
  52. int ysort_index;
  53. #ifdef VISUAL_SERVER_CANVAS_DEBUG_ITEM_NAMES
  54. String name;
  55. #endif
  56. Vector<Item *> child_items;
  57. Item() {
  58. children_order_dirty = true;
  59. E = nullptr;
  60. z_index = 0;
  61. modulate = Color(1, 1, 1, 1);
  62. self_modulate = Color(1, 1, 1, 1);
  63. sort_y = false;
  64. use_parent_material = false;
  65. z_relative = true;
  66. index = 0;
  67. ysort_children_count = -1;
  68. ysort_xform = Transform2D();
  69. ysort_pos = Vector2();
  70. ysort_index = 0;
  71. }
  72. };
  73. struct ItemIndexSort {
  74. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  75. return p_left->index < p_right->index;
  76. }
  77. };
  78. struct ItemPtrSort {
  79. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  80. if (Math::is_equal_approx(p_left->ysort_pos.y, p_right->ysort_pos.y)) {
  81. return p_left->ysort_index < p_right->ysort_index;
  82. }
  83. return p_left->ysort_pos.y < p_right->ysort_pos.y;
  84. }
  85. };
  86. struct LightOccluderPolygon : RID_Data {
  87. bool active;
  88. Rect2 aabb;
  89. VS::CanvasOccluderPolygonCullMode cull_mode;
  90. RID occluder;
  91. Set<RasterizerCanvas::LightOccluderInstance *> owners;
  92. LightOccluderPolygon() {
  93. active = false;
  94. cull_mode = VS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  95. }
  96. };
  97. RID_Owner<LightOccluderPolygon> canvas_light_occluder_polygon_owner;
  98. RID_Owner<RasterizerCanvas::LightOccluderInstance> canvas_light_occluder_owner;
  99. struct Canvas : public VisualServerViewport::CanvasBase {
  100. Set<RID> viewports;
  101. struct ChildItem {
  102. Point2 mirror;
  103. Item *item;
  104. bool operator<(const ChildItem &p_item) const {
  105. return item->index < p_item.item->index;
  106. }
  107. };
  108. Set<RasterizerCanvas::Light *> lights;
  109. Set<RasterizerCanvas::LightOccluderInstance *> occluders;
  110. bool children_order_dirty;
  111. Vector<ChildItem> child_items;
  112. Color modulate;
  113. RID parent;
  114. float parent_scale;
  115. int find_item(Item *p_item) {
  116. for (int i = 0; i < child_items.size(); i++) {
  117. if (child_items[i].item == p_item) {
  118. return i;
  119. }
  120. }
  121. return -1;
  122. }
  123. void erase_item(Item *p_item) {
  124. int idx = find_item(p_item);
  125. if (idx >= 0) {
  126. child_items.remove(idx);
  127. }
  128. }
  129. Canvas() {
  130. modulate = Color(1, 1, 1, 1);
  131. children_order_dirty = true;
  132. parent_scale = 1.0;
  133. }
  134. };
  135. mutable RID_Owner<Canvas> canvas_owner;
  136. RID_Owner<Item> canvas_item_owner;
  137. RID_Owner<RasterizerCanvas::Light> canvas_light_owner;
  138. bool disable_scale;
  139. private:
  140. enum CanvasCullMode {
  141. CANVAS_CULL_MODE_ITEM,
  142. CANVAS_CULL_MODE_NODE,
  143. };
  144. CanvasCullMode _canvas_cull_mode = CANVAS_CULL_MODE_NODE;
  145. void _render_canvas_item_tree(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights);
  146. void _light_mask_canvas_items(int p_z, RasterizerCanvas::Item *p_canvas_item, RasterizerCanvas::Light *p_masked_lights, int p_canvas_layer_id);
  147. RasterizerCanvas::Item **z_list;
  148. RasterizerCanvas::Item **z_last_list;
  149. Transform2D _current_camera_transform;
  150. // 3.5 and earlier had no hierarchical culling.
  151. void _render_canvas_item_cull_by_item(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner);
  152. // Hierarchical culling by scene tree node ///////////////////////////////////
  153. void _render_canvas_item_cull_by_node(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner, bool p_enclosed);
  154. void _prepare_tree_bounds(Item *p_root);
  155. void _calculate_canvas_item_bound(Item *p_canvas_item, Rect2 *r_branch_bound);
  156. Transform2D _calculate_item_global_xform(const Item *p_canvas_item);
  157. void _finalize_and_merge_local_bound_to_branch(Item *p_canvas_item, Rect2 *r_branch_bound);
  158. void _merge_local_bound_to_branch(Item *p_canvas_item, Rect2 *r_branch_bound);
  159. // If bounds flags are attempted to be modified multithreaded, the
  160. // tree could become corrupt. Multithread access may not be possible,
  161. // but just in case we use a mutex until proven otherwise.
  162. Mutex _bound_mutex;
  163. void _make_bound_dirty_reparent(Item *p_item);
  164. void _make_bound_dirty(Item *p_item, bool p_changing_visibility = false);
  165. void _make_bound_dirty_down(Item *p_item);
  166. #ifdef VISUAL_SERVER_CANVAS_CHECK_BOUNDS
  167. bool _check_bound_integrity(const Item *p_item);
  168. bool _check_bound_integrity_down(const Item *p_item, bool p_bound_dirty);
  169. void _print_tree(const Item *p_item);
  170. void _print_tree_down(int p_child_id, int p_depth, const Item *p_item, const Item *p_highlight, bool p_hidden = false);
  171. #else
  172. bool _check_bound_integrity(const Item *p_item) { return true; }
  173. #endif
  174. //////////////////////////////////////////////////////////////////////////////
  175. public:
  176. void render_canvas(Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect, int p_canvas_layer_id);
  177. RID canvas_create();
  178. void canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring);
  179. void canvas_set_modulate(RID p_canvas, const Color &p_color);
  180. void canvas_set_parent(RID p_canvas, RID p_parent, float p_scale);
  181. void canvas_set_disable_scale(bool p_disable);
  182. RID canvas_item_create();
  183. void canvas_item_set_parent(RID p_item, RID p_parent);
  184. void canvas_item_set_name(RID p_item, String p_name);
  185. void canvas_item_set_visible(RID p_item, bool p_visible);
  186. void canvas_item_set_light_mask(RID p_item, int p_mask);
  187. void canvas_item_set_transform(RID p_item, const Transform2D &p_transform);
  188. void canvas_item_set_clip(RID p_item, bool p_clip);
  189. void canvas_item_set_distance_field_mode(RID p_item, bool p_enable);
  190. void canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect = Rect2());
  191. void canvas_item_set_modulate(RID p_item, const Color &p_color);
  192. void canvas_item_set_self_modulate(RID p_item, const Color &p_color);
  193. void canvas_item_set_draw_behind_parent(RID p_item, bool p_enable);
  194. void canvas_item_set_use_identity_transform(RID p_item, bool p_enable);
  195. void canvas_item_set_update_when_visible(RID p_item, bool p_update);
  196. void canvas_item_add_line(RID p_item, const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width = 1.0, bool p_antialiased = false);
  197. void canvas_item_add_polyline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  198. void canvas_item_add_multiline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = 1.0, bool p_antialiased = false);
  199. void canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color);
  200. void canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color);
  201. void canvas_item_add_texture_rect(RID p_item, const Rect2 &p_rect, RID p_texture, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, RID p_normal_map = RID());
  202. void canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, RID p_normal_map = RID(), bool p_clip_uv = false);
  203. void canvas_item_add_texture_multirect_region(RID p_item, const Vector<Rect2> &p_rects, RID p_texture, const Vector<Rect2> &p_src_rects, const Color &p_modulate = Color(1, 1, 1), uint32_t p_canvas_rect_flags = 0, RID p_normal_map = RID());
  204. void canvas_item_add_nine_patch(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector2 &p_topleft, const Vector2 &p_bottomright, VS::NinePatchAxisMode p_x_axis_mode = VS::NINE_PATCH_STRETCH, VS::NinePatchAxisMode p_y_axis_mode = VS::NINE_PATCH_STRETCH, bool p_draw_center = true, const Color &p_modulate = Color(1, 1, 1), RID p_normal_map = RID());
  205. void canvas_item_add_primitive(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width = 1.0, RID p_normal_map = RID());
  206. void canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), RID p_texture = RID(), RID p_normal_map = RID(), bool p_antialiased = false);
  207. void canvas_item_add_triangle_array(RID p_item, const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>(), RID p_texture = RID(), int p_count = -1, RID p_normal_map = RID(), bool p_antialiased = false, bool p_antialiasing_use_indices = false);
  208. void canvas_item_add_mesh(RID p_item, const RID &p_mesh, const Transform2D &p_transform = Transform2D(), const Color &p_modulate = Color(1, 1, 1), RID p_texture = RID(), RID p_normal_map = RID());
  209. void canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture = RID(), RID p_normal_map = RID());
  210. void canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture, RID p_normal);
  211. void canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform);
  212. void canvas_item_add_clip_ignore(RID p_item, bool p_ignore);
  213. void canvas_item_set_sort_children_by_y(RID p_item, bool p_enable);
  214. void canvas_item_set_z_index(RID p_item, int p_z);
  215. void canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable);
  216. void canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect);
  217. void canvas_item_clear(RID p_item);
  218. void canvas_item_set_draw_index(RID p_item, int p_index);
  219. void canvas_item_set_material(RID p_item, RID p_material);
  220. void canvas_item_set_use_parent_material(RID p_item, bool p_enable);
  221. void canvas_item_attach_skeleton(RID p_item, RID p_skeleton);
  222. void canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform);
  223. Rect2 _debug_canvas_item_get_rect(RID p_item);
  224. Rect2 _debug_canvas_item_get_local_bound(RID p_item);
  225. void canvas_item_set_interpolated(RID p_item, bool p_interpolated);
  226. void canvas_item_reset_physics_interpolation(RID p_item);
  227. void canvas_item_transform_physics_interpolation(RID p_item, const Transform2D &p_transform);
  228. void _canvas_item_invalidate_local_bound(RID p_item);
  229. void _canvas_item_remove_references(RID p_item, RID p_rid);
  230. RID canvas_light_create();
  231. void canvas_light_attach_to_canvas(RID p_light, RID p_canvas);
  232. void canvas_light_set_enabled(RID p_light, bool p_enabled);
  233. void canvas_light_set_scale(RID p_light, float p_scale);
  234. void canvas_light_set_transform(RID p_light, const Transform2D &p_transform);
  235. void canvas_light_set_texture(RID p_light, RID p_texture);
  236. void canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset);
  237. void canvas_light_set_color(RID p_light, const Color &p_color);
  238. void canvas_light_set_height(RID p_light, float p_height);
  239. void canvas_light_set_energy(RID p_light, float p_energy);
  240. void canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z);
  241. void canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer);
  242. void canvas_light_set_item_cull_mask(RID p_light, int p_mask);
  243. void canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask);
  244. void canvas_light_set_mode(RID p_light, VS::CanvasLightMode p_mode);
  245. void canvas_light_set_shadow_enabled(RID p_light, bool p_enabled);
  246. void canvas_light_set_shadow_buffer_size(RID p_light, int p_size);
  247. void canvas_light_set_shadow_gradient_length(RID p_light, float p_length);
  248. void canvas_light_set_shadow_filter(RID p_light, VS::CanvasLightShadowFilter p_filter);
  249. void canvas_light_set_shadow_color(RID p_light, const Color &p_color);
  250. void canvas_light_set_shadow_smooth(RID p_light, float p_smooth);
  251. void canvas_light_set_interpolated(RID p_light, bool p_interpolated);
  252. void canvas_light_reset_physics_interpolation(RID p_light);
  253. void canvas_light_transform_physics_interpolation(RID p_light, const Transform2D &p_transform);
  254. RID canvas_light_occluder_create();
  255. void canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas);
  256. void canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled);
  257. void canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon);
  258. void canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform);
  259. void canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask);
  260. void canvas_light_occluder_set_interpolated(RID p_occluder, bool p_interpolated);
  261. void canvas_light_occluder_reset_physics_interpolation(RID p_occluder);
  262. void canvas_light_occluder_transform_physics_interpolation(RID p_occluder, const Transform2D &p_transform);
  263. RID canvas_occluder_polygon_create();
  264. void canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape, bool p_closed);
  265. void canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape);
  266. void canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, VS::CanvasOccluderPolygonCullMode p_mode);
  267. bool free(RID p_rid);
  268. // Interpolation
  269. void tick();
  270. void update_interpolation_tick(bool p_process = true);
  271. void set_physics_interpolation_enabled(bool p_enabled) { _interpolation_data.interpolation_enabled = p_enabled; }
  272. struct InterpolationData {
  273. void notify_free_canvas_item(RID p_rid, VisualServerCanvas::Item &r_canvas_item);
  274. void notify_free_canvas_light(RID p_rid, RasterizerCanvas::Light &r_canvas_light);
  275. void notify_free_canvas_light_occluder(RID p_rid, RasterizerCanvas::LightOccluderInstance &r_canvas_light_occluder);
  276. LocalVector<RID> canvas_item_transform_update_lists[2];
  277. LocalVector<RID> *canvas_item_transform_update_list_curr = &canvas_item_transform_update_lists[0];
  278. LocalVector<RID> *canvas_item_transform_update_list_prev = &canvas_item_transform_update_lists[1];
  279. LocalVector<RID> canvas_light_transform_update_lists[2];
  280. LocalVector<RID> *canvas_light_transform_update_list_curr = &canvas_light_transform_update_lists[0];
  281. LocalVector<RID> *canvas_light_transform_update_list_prev = &canvas_light_transform_update_lists[1];
  282. LocalVector<RID> canvas_light_occluder_transform_update_lists[2];
  283. LocalVector<RID> *canvas_light_occluder_transform_update_list_curr = &canvas_light_occluder_transform_update_lists[0];
  284. LocalVector<RID> *canvas_light_occluder_transform_update_list_prev = &canvas_light_occluder_transform_update_lists[1];
  285. bool interpolation_enabled = false;
  286. } _interpolation_data;
  287. VisualServerCanvas();
  288. ~VisualServerCanvas();
  289. };
  290. #endif // VISUAL_SERVER_CANVAS_H