renderer_canvas_cull.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**************************************************************************/
  2. /* renderer_canvas_cull.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 RENDERER_CANVAS_CULL_H
  31. #define RENDERER_CANVAS_CULL_H
  32. #include "core/templates/paged_allocator.h"
  33. #include "renderer_compositor.h"
  34. #include "renderer_viewport.h"
  35. class RendererCanvasCull {
  36. public:
  37. struct Item : public RendererCanvasRender::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; // Relative to y-sorted subtree's root item (identity for such root). Its `origin.y` is used for sorting.
  51. int ysort_index;
  52. int ysort_parent_abs_z_index; // Absolute Z index of parent. Only populated and used when y-sorting.
  53. uint32_t visibility_layer = 0xffffffff;
  54. Vector<Item *> child_items;
  55. struct VisibilityNotifierData {
  56. Rect2 area;
  57. Callable enter_callable;
  58. Callable exit_callable;
  59. bool just_visible = false;
  60. uint64_t visible_in_frame = 0;
  61. SelfList<VisibilityNotifierData> visible_element;
  62. VisibilityNotifierData() :
  63. visible_element(this) {
  64. }
  65. };
  66. VisibilityNotifierData *visibility_notifier = nullptr;
  67. Item() {
  68. children_order_dirty = true;
  69. E = nullptr;
  70. z_index = 0;
  71. modulate = Color(1, 1, 1, 1);
  72. self_modulate = Color(1, 1, 1, 1);
  73. sort_y = false;
  74. use_parent_material = false;
  75. z_relative = true;
  76. index = 0;
  77. ysort_children_count = -1;
  78. ysort_xform = Transform2D();
  79. ysort_index = 0;
  80. ysort_parent_abs_z_index = 0;
  81. }
  82. };
  83. struct ItemIndexSort {
  84. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  85. return p_left->index < p_right->index;
  86. }
  87. };
  88. struct ItemYSort {
  89. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  90. const real_t left_y = p_left->ysort_xform.columns[2].y;
  91. const real_t right_y = p_right->ysort_xform.columns[2].y;
  92. if (Math::is_equal_approx(left_y, right_y)) {
  93. return p_left->ysort_index < p_right->ysort_index;
  94. }
  95. return left_y < right_y;
  96. }
  97. };
  98. struct LightOccluderPolygon {
  99. bool active;
  100. Rect2 aabb;
  101. RS::CanvasOccluderPolygonCullMode cull_mode;
  102. RID occluder;
  103. HashSet<RendererCanvasRender::LightOccluderInstance *> owners;
  104. LightOccluderPolygon() {
  105. active = false;
  106. cull_mode = RS::CANVAS_OCCLUDER_POLYGON_CULL_DISABLED;
  107. }
  108. };
  109. RID_Owner<LightOccluderPolygon, true> canvas_light_occluder_polygon_owner;
  110. RID_Owner<RendererCanvasRender::LightOccluderInstance, true> canvas_light_occluder_owner;
  111. struct Canvas : public RendererViewport::CanvasBase {
  112. HashSet<RID> viewports;
  113. struct ChildItem {
  114. Item *item = nullptr;
  115. bool operator<(const ChildItem &p_item) const {
  116. return item->index < p_item.item->index;
  117. }
  118. };
  119. HashSet<RendererCanvasRender::Light *> lights;
  120. HashSet<RendererCanvasRender::Light *> directional_lights;
  121. HashSet<RendererCanvasRender::LightOccluderInstance *> occluders;
  122. bool children_order_dirty;
  123. Vector<ChildItem> child_items;
  124. Color modulate;
  125. RID parent;
  126. float parent_scale;
  127. int find_item(Item *p_item) {
  128. for (int i = 0; i < child_items.size(); i++) {
  129. if (child_items[i].item == p_item) {
  130. return i;
  131. }
  132. }
  133. return -1;
  134. }
  135. void erase_item(Item *p_item) {
  136. int idx = find_item(p_item);
  137. if (idx >= 0) {
  138. child_items.remove_at(idx);
  139. }
  140. }
  141. Canvas() {
  142. modulate = Color(1, 1, 1, 1);
  143. children_order_dirty = true;
  144. parent_scale = 1.0;
  145. }
  146. };
  147. mutable RID_Owner<Canvas, true> canvas_owner;
  148. RID_Owner<Item, true> canvas_item_owner;
  149. RID_Owner<RendererCanvasRender::Light, true> canvas_light_owner;
  150. template <typename T>
  151. void _free_rids(T &p_owner, const char *p_type);
  152. bool disable_scale;
  153. bool sdf_used = false;
  154. bool snapping_2d_transforms_to_pixel = false;
  155. bool debug_redraw = false;
  156. double debug_redraw_time = 0;
  157. Color debug_redraw_color;
  158. PagedAllocator<Item::VisibilityNotifierData> visibility_notifier_allocator;
  159. SelfList<Item::VisibilityNotifierData>::List visibility_notifier_list;
  160. _FORCE_INLINE_ void _attach_canvas_item_for_draw(Item *ci, Item *p_canvas_clip, RendererCanvasRender::Item **r_z_list, RendererCanvasRender::Item **r_z_last_list, const Transform2D &p_transform, const Rect2 &p_clip_rect, Rect2 p_global_rect, const Color &modulate, int p_z, RendererCanvasCull::Item *p_material_owner, bool p_use_canvas_group, RendererCanvasRender::Item *r_canvas_group_from);
  161. private:
  162. void _render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, uint32_t p_canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info = nullptr);
  163. void _cull_canvas_item(Item *p_canvas_item, const Transform2D &p_parent_xform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RendererCanvasRender::Item **r_z_list, RendererCanvasRender::Item **r_z_last_list, Item *p_canvas_clip, Item *p_material_owner, bool p_is_already_y_sorted, uint32_t p_canvas_cull_mask, const Point2 &p_repeat_size, int p_repeat_times, RendererCanvasRender::Item *p_repeat_source_item);
  164. void _collect_ysort_children(RendererCanvasCull::Item *p_canvas_item, RendererCanvasCull::Item *p_material_owner, const Color &p_modulate, RendererCanvasCull::Item **r_items, int &r_index, int p_z);
  165. int _count_ysort_children(RendererCanvasCull::Item *p_canvas_item);
  166. void _mark_ysort_dirty(RendererCanvasCull::Item *ysort_owner);
  167. static constexpr int z_range = RS::CANVAS_ITEM_Z_MAX - RS::CANVAS_ITEM_Z_MIN + 1;
  168. RendererCanvasRender::Item **z_list;
  169. RendererCanvasRender::Item **z_last_list;
  170. public:
  171. void render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, const Rect2 &p_clip_rect, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_transforms_to_pixel, bool p_snap_2d_vertices_to_pixel, uint32_t p_canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info = nullptr);
  172. bool was_sdf_used();
  173. RID canvas_allocate();
  174. void canvas_initialize(RID p_rid);
  175. void canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring);
  176. void canvas_set_item_repeat(RID p_item, const Point2 &p_repeat_size, int p_repeat_times);
  177. void canvas_set_modulate(RID p_canvas, const Color &p_color);
  178. void canvas_set_parent(RID p_canvas, RID p_parent, float p_scale);
  179. void canvas_set_disable_scale(bool p_disable);
  180. RID canvas_item_allocate();
  181. void canvas_item_initialize(RID p_rid);
  182. void canvas_item_set_parent(RID p_item, RID p_parent);
  183. void canvas_item_set_visible(RID p_item, bool p_visible);
  184. void canvas_item_set_light_mask(RID p_item, int p_mask);
  185. void canvas_item_set_visibility_layer(RID p_item, uint32_t p_layer);
  186. uint32_t canvas_item_get_visibility_layer(RID p_item);
  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_update_when_visible(RID p_item, bool p_update);
  195. 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);
  196. 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);
  197. 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);
  198. void canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color, bool p_antialiased);
  199. void canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color, bool p_antialiased);
  200. 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);
  201. 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, bool p_clip_uv = false);
  202. void canvas_item_add_msdf_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), int p_outline_size = 0, float p_px_range = 1.0, float p_scale = 1.0);
  203. void canvas_item_add_lcd_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));
  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, RS::NinePatchAxisMode p_x_axis_mode = RS::NINE_PATCH_STRETCH, RS::NinePatchAxisMode p_y_axis_mode = RS::NINE_PATCH_STRETCH, bool p_draw_center = true, const Color &p_modulate = Color(1, 1, 1));
  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);
  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());
  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);
  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());
  209. void canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture = RID());
  210. void canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture);
  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_add_animation_slice(RID p_item, double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset);
  214. void canvas_item_set_sort_children_by_y(RID p_item, bool p_enable);
  215. void canvas_item_set_z_index(RID p_item, int p_z);
  216. void canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable);
  217. void canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect);
  218. void canvas_item_attach_skeleton(RID p_item, RID p_skeleton);
  219. void canvas_item_clear(RID p_item);
  220. void canvas_item_set_draw_index(RID p_item, int p_index);
  221. void canvas_item_set_material(RID p_item, RID p_material);
  222. void canvas_item_set_use_parent_material(RID p_item, bool p_enable);
  223. void canvas_item_set_visibility_notifier(RID p_item, bool p_enable, const Rect2 &p_area, const Callable &p_enter_callable, const Callable &p_exit_callable);
  224. void canvas_item_set_canvas_group_mode(RID p_item, RS::CanvasGroupMode p_mode, float p_clear_margin = 5.0, bool p_fit_empty = false, float p_fit_margin = 0.0, bool p_blur_mipmaps = false);
  225. void canvas_item_set_debug_redraw(bool p_enabled);
  226. bool canvas_item_get_debug_redraw() const;
  227. void canvas_item_set_interpolated(RID p_item, bool p_interpolated);
  228. void canvas_item_reset_physics_interpolation(RID p_item);
  229. void canvas_item_transform_physics_interpolation(RID p_item, const Transform2D &p_transform);
  230. RID canvas_light_allocate();
  231. void canvas_light_initialize(RID p_rid);
  232. void canvas_light_set_mode(RID p_light, RS::CanvasLightMode p_mode);
  233. void canvas_light_attach_to_canvas(RID p_light, RID p_canvas);
  234. void canvas_light_set_enabled(RID p_light, bool p_enabled);
  235. void canvas_light_set_texture_scale(RID p_light, float p_scale);
  236. void canvas_light_set_transform(RID p_light, const Transform2D &p_transform);
  237. void canvas_light_set_texture(RID p_light, RID p_texture);
  238. void canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset);
  239. void canvas_light_set_color(RID p_light, const Color &p_color);
  240. void canvas_light_set_height(RID p_light, float p_height);
  241. void canvas_light_set_energy(RID p_light, float p_energy);
  242. void canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z);
  243. void canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer);
  244. void canvas_light_set_item_cull_mask(RID p_light, int p_mask);
  245. void canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask);
  246. void canvas_light_set_directional_distance(RID p_light, float p_distance);
  247. void canvas_light_set_blend_mode(RID p_light, RS::CanvasLightBlendMode p_mode);
  248. void canvas_light_set_shadow_enabled(RID p_light, bool p_enabled);
  249. void canvas_light_set_shadow_filter(RID p_light, RS::CanvasLightShadowFilter p_filter);
  250. void canvas_light_set_shadow_color(RID p_light, const Color &p_color);
  251. void canvas_light_set_shadow_smooth(RID p_light, float p_smooth);
  252. void canvas_light_set_interpolated(RID p_light, bool p_interpolated);
  253. void canvas_light_reset_physics_interpolation(RID p_light);
  254. void canvas_light_transform_physics_interpolation(RID p_light, const Transform2D &p_transform);
  255. RID canvas_light_occluder_allocate();
  256. void canvas_light_occluder_initialize(RID p_rid);
  257. void canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas);
  258. void canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled);
  259. void canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon);
  260. void canvas_light_occluder_set_as_sdf_collision(RID p_occluder, bool p_enable);
  261. void canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform);
  262. void canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask);
  263. void canvas_light_occluder_set_interpolated(RID p_occluder, bool p_interpolated);
  264. void canvas_light_occluder_reset_physics_interpolation(RID p_occluder);
  265. void canvas_light_occluder_transform_physics_interpolation(RID p_occluder, const Transform2D &p_transform);
  266. RID canvas_occluder_polygon_allocate();
  267. void canvas_occluder_polygon_initialize(RID p_rid);
  268. void canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const Vector<Vector2> &p_shape, bool p_closed);
  269. void canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, RS::CanvasOccluderPolygonCullMode p_mode);
  270. void canvas_set_shadow_texture_size(int p_size);
  271. RID canvas_texture_allocate();
  272. void canvas_texture_initialize(RID p_rid);
  273. void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture);
  274. void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess);
  275. void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter);
  276. void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat);
  277. void canvas_item_set_default_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter);
  278. void canvas_item_set_default_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat);
  279. void update_visibility_notifiers();
  280. Rect2 _debug_canvas_item_get_rect(RID p_item);
  281. bool free(RID p_rid);
  282. void finalize();
  283. /* INTERPOLATION */
  284. void tick();
  285. void update_interpolation_tick(bool p_process = true);
  286. void set_physics_interpolation_enabled(bool p_enabled) { _interpolation_data.interpolation_enabled = p_enabled; }
  287. struct InterpolationData {
  288. void notify_free_canvas_item(RID p_rid, RendererCanvasCull::Item &r_canvas_item);
  289. void notify_free_canvas_light(RID p_rid, RendererCanvasRender::Light &r_canvas_light);
  290. void notify_free_canvas_light_occluder(RID p_rid, RendererCanvasRender::LightOccluderInstance &r_canvas_light_occluder);
  291. LocalVector<RID> canvas_item_transform_update_lists[2];
  292. LocalVector<RID> *canvas_item_transform_update_list_curr = &canvas_item_transform_update_lists[0];
  293. LocalVector<RID> *canvas_item_transform_update_list_prev = &canvas_item_transform_update_lists[1];
  294. LocalVector<RID> canvas_light_transform_update_lists[2];
  295. LocalVector<RID> *canvas_light_transform_update_list_curr = &canvas_light_transform_update_lists[0];
  296. LocalVector<RID> *canvas_light_transform_update_list_prev = &canvas_light_transform_update_lists[1];
  297. LocalVector<RID> canvas_light_occluder_transform_update_lists[2];
  298. LocalVector<RID> *canvas_light_occluder_transform_update_list_curr = &canvas_light_occluder_transform_update_lists[0];
  299. LocalVector<RID> *canvas_light_occluder_transform_update_list_prev = &canvas_light_occluder_transform_update_lists[1];
  300. bool interpolation_enabled = false;
  301. } _interpolation_data;
  302. RendererCanvasCull();
  303. ~RendererCanvasCull();
  304. };
  305. #endif // RENDERER_CANVAS_CULL_H