renderer_canvas_cull.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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;
  51. Vector2 ysort_pos;
  52. int ysort_index;
  53. int ysort_parent_abs_z_index; // Absolute Z index of parent. Only populated and used when y-sorting.
  54. uint32_t visibility_layer = 0xffffffff;
  55. Vector<Item *> child_items;
  56. struct VisibilityNotifierData {
  57. Rect2 area;
  58. Callable enter_callable;
  59. Callable exit_callable;
  60. bool just_visible = false;
  61. uint64_t visible_in_frame = 0;
  62. SelfList<VisibilityNotifierData> visible_element;
  63. VisibilityNotifierData() :
  64. visible_element(this) {
  65. }
  66. };
  67. VisibilityNotifierData *visibility_notifier = nullptr;
  68. Item() {
  69. children_order_dirty = true;
  70. E = nullptr;
  71. z_index = 0;
  72. modulate = Color(1, 1, 1, 1);
  73. self_modulate = Color(1, 1, 1, 1);
  74. sort_y = false;
  75. use_parent_material = false;
  76. z_relative = true;
  77. index = 0;
  78. ysort_children_count = -1;
  79. ysort_xform = Transform2D();
  80. ysort_pos = Vector2();
  81. ysort_index = 0;
  82. ysort_parent_abs_z_index = 0;
  83. }
  84. };
  85. struct ItemIndexSort {
  86. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  87. return p_left->index < p_right->index;
  88. }
  89. };
  90. struct ItemPtrSort {
  91. _FORCE_INLINE_ bool operator()(const Item *p_left, const Item *p_right) const {
  92. if (Math::is_equal_approx(p_left->ysort_pos.y, p_right->ysort_pos.y)) {
  93. return p_left->ysort_index < p_right->ysort_index;
  94. }
  95. return p_left->ysort_pos.y < p_right->ysort_pos.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. Point2 mirror;
  115. Item *item = nullptr;
  116. bool operator<(const ChildItem &p_item) const {
  117. return item->index < p_item.item->index;
  118. }
  119. };
  120. HashSet<RendererCanvasRender::Light *> lights;
  121. HashSet<RendererCanvasRender::Light *> directional_lights;
  122. HashSet<RendererCanvasRender::LightOccluderInstance *> occluders;
  123. bool children_order_dirty;
  124. Vector<ChildItem> child_items;
  125. Color modulate;
  126. RID parent;
  127. float parent_scale;
  128. int find_item(Item *p_item) {
  129. for (int i = 0; i < child_items.size(); i++) {
  130. if (child_items[i].item == p_item) {
  131. return i;
  132. }
  133. }
  134. return -1;
  135. }
  136. void erase_item(Item *p_item) {
  137. int idx = find_item(p_item);
  138. if (idx >= 0) {
  139. child_items.remove_at(idx);
  140. }
  141. }
  142. Canvas() {
  143. modulate = Color(1, 1, 1, 1);
  144. children_order_dirty = true;
  145. parent_scale = 1.0;
  146. }
  147. };
  148. mutable RID_Owner<Canvas, true> canvas_owner;
  149. RID_Owner<Item, true> canvas_item_owner;
  150. RID_Owner<RendererCanvasRender::Light, true> canvas_light_owner;
  151. bool disable_scale;
  152. bool sdf_used = false;
  153. bool snapping_2d_transforms_to_pixel = false;
  154. PagedAllocator<Item::VisibilityNotifierData> visibility_notifier_allocator;
  155. SelfList<Item::VisibilityNotifierData>::List visibility_notifier_list;
  156. _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 &xform, const Rect2 &p_clip_rect, Rect2 global_rect, const Color &modulate, int p_z, RendererCanvasCull::Item *p_material_owner, bool p_use_canvas_group, RendererCanvasRender::Item *canvas_group_from, const Transform2D &p_xform);
  157. private:
  158. void _render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, Item *p_canvas_item, 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 canvas_cull_mask);
  159. void _cull_canvas_item(Item *p_canvas_item, const Transform2D &p_transform, 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 allow_y_sort, uint32_t canvas_cull_mask);
  160. RendererCanvasRender::Item **z_list;
  161. RendererCanvasRender::Item **z_last_list;
  162. public:
  163. 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 canvas_cull_mask);
  164. bool was_sdf_used();
  165. RID canvas_allocate();
  166. void canvas_initialize(RID p_rid);
  167. void canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring);
  168. void canvas_set_modulate(RID p_canvas, const Color &p_color);
  169. void canvas_set_parent(RID p_canvas, RID p_parent, float p_scale);
  170. void canvas_set_disable_scale(bool p_disable);
  171. RID canvas_item_allocate();
  172. void canvas_item_initialize(RID p_rid);
  173. void canvas_item_set_parent(RID p_item, RID p_parent);
  174. void canvas_item_set_visible(RID p_item, bool p_visible);
  175. void canvas_item_set_light_mask(RID p_item, int p_mask);
  176. void canvas_item_set_visibility_layer(RID p_item, uint32_t p_layer);
  177. uint32_t canvas_item_get_visibility_layer(RID p_item);
  178. void canvas_item_set_transform(RID p_item, const Transform2D &p_transform);
  179. void canvas_item_set_clip(RID p_item, bool p_clip);
  180. void canvas_item_set_distance_field_mode(RID p_item, bool p_enable);
  181. void canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect = Rect2());
  182. void canvas_item_set_modulate(RID p_item, const Color &p_color);
  183. void canvas_item_set_self_modulate(RID p_item, const Color &p_color);
  184. void canvas_item_set_draw_behind_parent(RID p_item, bool p_enable);
  185. void canvas_item_set_update_when_visible(RID p_item, bool p_update);
  186. 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);
  187. 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);
  188. void canvas_item_add_multiline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width = -1.0);
  189. void canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color);
  190. void canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color);
  191. 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);
  192. 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);
  193. 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);
  194. 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));
  195. 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));
  196. 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);
  197. 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());
  198. 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);
  199. 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());
  200. void canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture = RID());
  201. void canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture);
  202. void canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform);
  203. void canvas_item_add_clip_ignore(RID p_item, bool p_ignore);
  204. void canvas_item_add_animation_slice(RID p_item, double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset);
  205. void canvas_item_set_sort_children_by_y(RID p_item, bool p_enable);
  206. void canvas_item_set_z_index(RID p_item, int p_z);
  207. void canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable);
  208. void canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect);
  209. void canvas_item_attach_skeleton(RID p_item, RID p_skeleton);
  210. void canvas_item_clear(RID p_item);
  211. void canvas_item_set_draw_index(RID p_item, int p_index);
  212. void canvas_item_set_material(RID p_item, RID p_material);
  213. void canvas_item_set_use_parent_material(RID p_item, bool p_enable);
  214. 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);
  215. 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);
  216. RID canvas_light_allocate();
  217. void canvas_light_initialize(RID p_rid);
  218. void canvas_light_set_mode(RID p_light, RS::CanvasLightMode p_mode);
  219. void canvas_light_attach_to_canvas(RID p_light, RID p_canvas);
  220. void canvas_light_set_enabled(RID p_light, bool p_enabled);
  221. void canvas_light_set_texture_scale(RID p_light, float p_scale);
  222. void canvas_light_set_transform(RID p_light, const Transform2D &p_transform);
  223. void canvas_light_set_texture(RID p_light, RID p_texture);
  224. void canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset);
  225. void canvas_light_set_color(RID p_light, const Color &p_color);
  226. void canvas_light_set_height(RID p_light, float p_height);
  227. void canvas_light_set_energy(RID p_light, float p_energy);
  228. void canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z);
  229. void canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer);
  230. void canvas_light_set_item_cull_mask(RID p_light, int p_mask);
  231. void canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask);
  232. void canvas_light_set_directional_distance(RID p_light, float p_distance);
  233. void canvas_light_set_blend_mode(RID p_light, RS::CanvasLightBlendMode p_mode);
  234. void canvas_light_set_shadow_enabled(RID p_light, bool p_enabled);
  235. void canvas_light_set_shadow_filter(RID p_light, RS::CanvasLightShadowFilter p_filter);
  236. void canvas_light_set_shadow_color(RID p_light, const Color &p_color);
  237. void canvas_light_set_shadow_smooth(RID p_light, float p_smooth);
  238. RID canvas_light_occluder_allocate();
  239. void canvas_light_occluder_initialize(RID p_rid);
  240. void canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas);
  241. void canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled);
  242. void canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon);
  243. void canvas_light_occluder_set_as_sdf_collision(RID p_occluder, bool p_enable);
  244. void canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform);
  245. void canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask);
  246. RID canvas_occluder_polygon_allocate();
  247. void canvas_occluder_polygon_initialize(RID p_rid);
  248. void canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const Vector<Vector2> &p_shape, bool p_closed);
  249. void canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, RS::CanvasOccluderPolygonCullMode p_mode);
  250. void canvas_set_shadow_texture_size(int p_size);
  251. RID canvas_texture_allocate();
  252. void canvas_texture_initialize(RID p_rid);
  253. void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture);
  254. void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess);
  255. void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter);
  256. void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat);
  257. void canvas_item_set_default_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter);
  258. void canvas_item_set_default_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat);
  259. void update_visibility_notifiers();
  260. bool free(RID p_rid);
  261. RendererCanvasCull();
  262. ~RendererCanvasCull();
  263. };
  264. #endif // RENDERER_CANVAS_CULL_H