diff options
Diffstat (limited to 'servers/rendering/renderer_canvas_cull.cpp')
-rw-r--r-- | servers/rendering/renderer_canvas_cull.cpp | 369 |
1 files changed, 231 insertions, 138 deletions
diff --git a/servers/rendering/renderer_canvas_cull.cpp b/servers/rendering/renderer_canvas_cull.cpp index 67003a6f64..ec0a8347f8 100644 --- a/servers/rendering/renderer_canvas_cull.cpp +++ b/servers/rendering/renderer_canvas_cull.cpp @@ -44,10 +44,10 @@ void RendererCanvasCull::_render_canvas_item_tree(RID p_to_render_target, Canvas memset(z_last_list, 0, z_range * sizeof(RendererCanvasRender::Item *)); for (int i = 0; i < p_child_item_count; i++) { - _cull_canvas_item(p_child_items[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr); + _cull_canvas_item(p_child_items[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, true); } if (p_canvas_item) { - _cull_canvas_item(p_canvas_item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr); + _cull_canvas_item(p_canvas_item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, true); } RendererCanvasRender::Item *list = nullptr; @@ -97,105 +97,14 @@ void _collect_ysort_children(RendererCanvasCull::Item *p_canvas_item, Transform2 } } -void _mark_ysort_dirty(RendererCanvasCull::Item *ysort_owner, RID_PtrOwner<RendererCanvasCull::Item, true> &canvas_item_owner) { +void _mark_ysort_dirty(RendererCanvasCull::Item *ysort_owner, RID_Owner<RendererCanvasCull::Item, true> &canvas_item_owner) { do { ysort_owner->ysort_children_count = -1; ysort_owner = canvas_item_owner.owns(ysort_owner->parent) ? canvas_item_owner.getornull(ysort_owner->parent) : nullptr; } while (ysort_owner && ysort_owner->sort_y); } -void RendererCanvasCull::_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 **z_list, RendererCanvasRender::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner) { - Item *ci = p_canvas_item; - - if (!ci->visible) { - return; - } - - if (ci->children_order_dirty) { - ci->child_items.sort_custom<ItemIndexSort>(); - ci->children_order_dirty = false; - } - - Rect2 rect = ci->get_rect(); - Transform2D xform = ci->xform; - if (snapping_2d_transforms_to_pixel) { - xform.elements[2] = xform.elements[2].floor(); - } - xform = p_transform * xform; - - Rect2 global_rect = xform.xform(rect); - global_rect.position += p_clip_rect.position; - - if (ci->use_parent_material && p_material_owner) { - ci->material_owner = p_material_owner; - } else { - p_material_owner = ci; - ci->material_owner = nullptr; - } - - Color modulate(ci->modulate.r * p_modulate.r, ci->modulate.g * p_modulate.g, ci->modulate.b * p_modulate.b, ci->modulate.a * p_modulate.a); - - if (modulate.a < 0.007) { - return; - } - - int child_item_count = ci->child_items.size(); - Item **child_items = ci->child_items.ptrw(); - - if (ci->clip) { - if (p_canvas_clip != nullptr) { - ci->final_clip_rect = p_canvas_clip->final_clip_rect.intersection(global_rect); - } else { - ci->final_clip_rect = global_rect; - } - ci->final_clip_rect.position = ci->final_clip_rect.position.round(); - ci->final_clip_rect.size = ci->final_clip_rect.size.round(); - ci->final_clip_owner = ci; - - } else { - ci->final_clip_owner = p_canvas_clip; - } - - if (ci->sort_y) { - if (ci->ysort_children_count == -1) { - ci->ysort_children_count = 0; - _collect_ysort_children(ci, Transform2D(), p_material_owner, nullptr, ci->ysort_children_count); - } - - child_item_count = ci->ysort_children_count; - child_items = (Item **)alloca(child_item_count * sizeof(Item *)); - - int i = 0; - _collect_ysort_children(ci, Transform2D(), p_material_owner, child_items, i); - - SortArray<Item *, ItemPtrSort> sorter; - sorter.sort(child_items, child_item_count); - } - - if (ci->z_relative) { - p_z = CLAMP(p_z + ci->z_index, RS::CANVAS_ITEM_Z_MIN, RS::CANVAS_ITEM_Z_MAX); - } else { - p_z = ci->z_index; - } - - RendererCanvasRender::Item *canvas_group_from = nullptr; - bool use_canvas_group = ci->canvas_group != nullptr && (ci->canvas_group->fit_empty || ci->commands != nullptr); - if (use_canvas_group) { - int zidx = p_z - RS::CANVAS_ITEM_Z_MIN; - canvas_group_from = z_last_list[zidx]; - } - - for (int i = 0; i < child_item_count; i++) { - if ((!child_items[i]->behind && !use_canvas_group) || (ci->sort_y && child_items[i]->sort_y)) { - continue; - } - if (ci->sort_y) { - _cull_canvas_item(child_items[i], xform * child_items[i]->ysort_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner); - } else { - _cull_canvas_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner); - } - } - +void RendererCanvasCull::_attach_canvas_item_for_draw(RendererCanvasCull::Item *ci, RendererCanvasCull::Item *p_canvas_clip, RendererCanvasRender::Item **z_list, RendererCanvasRender::Item **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 use_canvas_group, RendererCanvasRender::Item *canvas_group_from, const Transform2D &p_xform) { if (ci->copy_back_buffer) { ci->copy_back_buffer->screen_rect = xform.xform(ci->copy_back_buffer->rect).intersection(p_clip_rect); } @@ -229,7 +138,7 @@ void RendererCanvasCull::_cull_canvas_item(Item *p_canvas_item, const Transform2 // We have two choices now, if user has drawn something, we must assume users wants to draw the "mask", so compute the size based on this. // If nothing has been drawn, we just take it over and draw it ourselves. if (ci->canvas_group->fit_empty && (ci->commands == nullptr || - (ci->commands->next == nullptr && ci->commands->type == Item::Command::TYPE_RECT && (static_cast<Item::CommandRect *>(ci->commands)->flags & RendererCanvasRender::CANVAS_RECT_IS_GROUP)))) { + (ci->commands->next == nullptr && ci->commands->type == RendererCanvasCull::Item::Command::TYPE_RECT && (static_cast<RendererCanvasCull::Item::CommandRect *>(ci->commands)->flags & RendererCanvasRender::CANVAS_RECT_IS_GROUP)))) { // No commands, or sole command is the one used to draw, so we (re)create the draw command. ci->clear(); @@ -264,42 +173,163 @@ void RendererCanvasCull::_cull_canvas_item(Item *p_canvas_item, const Transform2 } } - if (ci->update_when_visible) { - RenderingServerDefault::redraw_request(); + if (((ci->commands != nullptr || ci->visibility_notifier) && p_clip_rect.intersects(global_rect, true)) || ci->vp_render || ci->copy_back_buffer) { + //something to draw? + + if (ci->update_when_visible) { + RenderingServerDefault::redraw_request(); + } + + if (ci->commands != nullptr) { + ci->final_transform = xform; + ci->final_modulate = Color(modulate.r * ci->self_modulate.r, modulate.g * ci->self_modulate.g, modulate.b * ci->self_modulate.b, modulate.a * ci->self_modulate.a); + ci->global_rect_cache = global_rect; + ci->global_rect_cache.position -= p_clip_rect.position; + ci->light_masked = false; + + int zidx = p_z - RS::CANVAS_ITEM_Z_MIN; + + if (z_last_list[zidx]) { + z_last_list[zidx]->next = ci; + z_last_list[zidx] = ci; + + } else { + z_list[zidx] = ci; + z_last_list[zidx] = ci; + } + + ci->z_final = p_z; + + ci->next = nullptr; + } + + if (ci->visibility_notifier) { + if (!ci->visibility_notifier->visible_element.in_list()) { + visibility_notifier_list.add(&ci->visibility_notifier->visible_element); + ci->visibility_notifier->just_visible = true; + } + + ci->visibility_notifier->visible_in_frame = RSG::rasterizer->get_frame_number(); + } } +} - if ((ci->commands != nullptr && p_clip_rect.intersects(global_rect, true)) || ci->vp_render || ci->copy_back_buffer) { - //something to draw? - ci->final_transform = xform; - ci->final_modulate = Color(modulate.r * ci->self_modulate.r, modulate.g * ci->self_modulate.g, modulate.b * ci->self_modulate.b, modulate.a * ci->self_modulate.a); - ci->global_rect_cache = global_rect; - ci->global_rect_cache.position -= p_clip_rect.position; - ci->light_masked = false; +void RendererCanvasCull::_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 **z_list, RendererCanvasRender::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner, bool allow_y_sort) { + Item *ci = p_canvas_item; - int zidx = p_z - RS::CANVAS_ITEM_Z_MIN; + if (!ci->visible) { + return; + } - if (z_last_list[zidx]) { - z_last_list[zidx]->next = ci; - z_last_list[zidx] = ci; + if (ci->children_order_dirty) { + ci->child_items.sort_custom<ItemIndexSort>(); + ci->children_order_dirty = false; + } - } else { - z_list[zidx] = ci; - z_last_list[zidx] = ci; + Rect2 rect = ci->get_rect(); + + if (ci->visibility_notifier) { + if (ci->visibility_notifier->area.size != Vector2()) { + rect = rect.merge(ci->visibility_notifier->area); } + } - ci->z_final = p_z; + Transform2D xform = ci->xform; + if (snapping_2d_transforms_to_pixel) { + xform.elements[2] = xform.elements[2].floor(); + } + xform = p_transform * xform; - ci->next = nullptr; + Rect2 global_rect = xform.xform(rect); + global_rect.position += p_clip_rect.position; + + if (ci->use_parent_material && p_material_owner) { + ci->material_owner = p_material_owner; + } else { + p_material_owner = ci; + ci->material_owner = nullptr; } - for (int i = 0; i < child_item_count; i++) { - if (child_items[i]->behind || use_canvas_group || (ci->sort_y && child_items[i]->sort_y)) { - continue; + Color modulate(ci->modulate.r * p_modulate.r, ci->modulate.g * p_modulate.g, ci->modulate.b * p_modulate.b, ci->modulate.a * p_modulate.a); + + if (modulate.a < 0.007) { + return; + } + + int child_item_count = ci->child_items.size(); + Item **child_items = ci->child_items.ptrw(); + + if (ci->clip) { + if (p_canvas_clip != nullptr) { + ci->final_clip_rect = p_canvas_clip->final_clip_rect.intersection(global_rect); + } else { + ci->final_clip_rect = global_rect; } - if (ci->sort_y) { - _cull_canvas_item(child_items[i], xform * child_items[i]->ysort_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner); + ci->final_clip_rect.position = ci->final_clip_rect.position.round(); + ci->final_clip_rect.size = ci->final_clip_rect.size.round(); + ci->final_clip_owner = ci; + + } else { + ci->final_clip_owner = p_canvas_clip; + } + + if (ci->z_relative) { + p_z = CLAMP(p_z + ci->z_index, RS::CANVAS_ITEM_Z_MIN, RS::CANVAS_ITEM_Z_MAX); + } else { + p_z = ci->z_index; + } + + if (ci->sort_y) { + if (allow_y_sort) { + if (ci->ysort_children_count == -1) { + ci->ysort_children_count = 0; + _collect_ysort_children(ci, Transform2D(), p_material_owner, nullptr, ci->ysort_children_count); + } + + child_item_count = ci->ysort_children_count + 1; + child_items = (Item **)alloca(child_item_count * sizeof(Item *)); + + child_items[0] = ci; + int i = 1; + _collect_ysort_children(ci, Transform2D(), p_material_owner, child_items, i); + ci->ysort_xform = ci->xform.affine_inverse(); + + SortArray<Item *, ItemPtrSort> sorter; + sorter.sort(child_items, child_item_count); + + for (i = 0; i < child_item_count; i++) { + _cull_canvas_item(child_items[i], xform * child_items[i]->ysort_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner, false); + } } else { - _cull_canvas_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner); + RendererCanvasRender::Item *canvas_group_from = nullptr; + bool use_canvas_group = ci->canvas_group != nullptr && (ci->canvas_group->fit_empty || ci->commands != nullptr); + if (use_canvas_group) { + int zidx = p_z - RS::CANVAS_ITEM_Z_MIN; + canvas_group_from = z_last_list[zidx]; + } + + _attach_canvas_item_for_draw(ci, p_canvas_clip, z_list, z_last_list, xform, p_clip_rect, global_rect, modulate, p_z, p_material_owner, use_canvas_group, canvas_group_from, xform); + } + } else { + RendererCanvasRender::Item *canvas_group_from = nullptr; + bool use_canvas_group = ci->canvas_group != nullptr && (ci->canvas_group->fit_empty || ci->commands != nullptr); + if (use_canvas_group) { + int zidx = p_z - RS::CANVAS_ITEM_Z_MIN; + canvas_group_from = z_last_list[zidx]; + } + + for (int i = 0; i < child_item_count; i++) { + if (!child_items[i]->behind && !use_canvas_group) { + continue; + } + _cull_canvas_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner, true); + } + _attach_canvas_item_for_draw(ci, p_canvas_clip, z_list, z_last_list, xform, p_clip_rect, global_rect, modulate, p_z, p_material_owner, use_canvas_group, canvas_group_from, xform); + for (int i = 0; i < child_item_count; i++) { + if (child_items[i]->behind || use_canvas_group) { + continue; + } + _cull_canvas_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner, true); } } } @@ -362,8 +392,7 @@ RID RendererCanvasCull::canvas_allocate() { return canvas_owner.allocate_rid(); } void RendererCanvasCull::canvas_initialize(RID p_rid) { - Canvas *canvas = memnew(Canvas); - canvas_owner.initialize_rid(p_rid, canvas); + canvas_owner.initialize_rid(p_rid); } void RendererCanvasCull::canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring) { @@ -399,8 +428,7 @@ RID RendererCanvasCull::canvas_item_allocate() { return canvas_item_owner.allocate_rid(); } void RendererCanvasCull::canvas_item_initialize(RID p_rid) { - Item *canvas_item = memnew(Item); - canvas_item_owner.initialize_rid(p_rid, canvas_item); + canvas_item_owner.initialize_rid(p_rid); } void RendererCanvasCull::canvas_item_set_parent(RID p_item, RID p_parent) { @@ -973,6 +1001,18 @@ void RendererCanvasCull::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) ci->ignore = p_ignore; } +void RendererCanvasCull::canvas_item_add_animation_slice(RID p_item, double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset) { + Item *canvas_item = canvas_item_owner.getornull(p_item); + ERR_FAIL_COND(!canvas_item); + + Item::CommandAnimationSlice *as = canvas_item->alloc_command<Item::CommandAnimationSlice>(); + ERR_FAIL_COND(!as); + as->animation_length = p_animation_length; + as->slice_begin = p_slice_begin; + as->slice_end = p_slice_end; + as->offset = p_offset; +} + void RendererCanvasCull::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) { Item *canvas_item = canvas_item_owner.getornull(p_item); ERR_FAIL_COND(!canvas_item); @@ -1084,6 +1124,26 @@ void RendererCanvasCull::canvas_item_set_use_parent_material(RID p_item, bool p_ canvas_item->use_parent_material = p_enable; } +void RendererCanvasCull::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) { + Item *canvas_item = canvas_item_owner.getornull(p_item); + ERR_FAIL_COND(!canvas_item); + + if (p_enable) { + if (!canvas_item->visibility_notifier) { + canvas_item->visibility_notifier = visibility_notifier_allocator.alloc(); + } + canvas_item->visibility_notifier->area = p_area; + canvas_item->visibility_notifier->enter_callable = p_enter_callable; + canvas_item->visibility_notifier->exit_callable = p_exit_callable; + + } else { + if (canvas_item->visibility_notifier) { + visibility_notifier_allocator.free(canvas_item->visibility_notifier); + canvas_item->visibility_notifier = nullptr; + } + } +} + void RendererCanvasCull::canvas_item_set_canvas_group_mode(RID p_item, RS::CanvasGroupMode p_mode, float p_clear_margin, bool p_fit_empty, float p_fit_margin, bool p_blur_mipmaps) { Item *canvas_item = canvas_item_owner.getornull(p_item); ERR_FAIL_COND(!canvas_item); @@ -1109,9 +1169,9 @@ RID RendererCanvasCull::canvas_light_allocate() { return canvas_light_owner.allocate_rid(); } void RendererCanvasCull::canvas_light_initialize(RID p_rid) { - RendererCanvasRender::Light *clight = memnew(RendererCanvasRender::Light); + canvas_light_owner.initialize_rid(p_rid); + RendererCanvasRender::Light *clight = canvas_light_owner.getornull(p_rid); clight->light_internal = RSG::canvas_render->light_create(); - return canvas_light_owner.initialize_rid(p_rid, clight); } void RendererCanvasCull::canvas_light_set_mode(RID p_light, RS::CanvasLightMode p_mode) { @@ -1305,9 +1365,7 @@ RID RendererCanvasCull::canvas_light_occluder_allocate() { return canvas_light_occluder_owner.allocate_rid(); } void RendererCanvasCull::canvas_light_occluder_initialize(RID p_rid) { - RendererCanvasRender::LightOccluderInstance *occluder = memnew(RendererCanvasRender::LightOccluderInstance); - - return canvas_light_occluder_owner.initialize_rid(p_rid, occluder); + return canvas_light_occluder_owner.initialize_rid(p_rid); } void RendererCanvasCull::canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas) { @@ -1389,9 +1447,9 @@ RID RendererCanvasCull::canvas_occluder_polygon_allocate() { return canvas_light_occluder_polygon_owner.allocate_rid(); } void RendererCanvasCull::canvas_occluder_polygon_initialize(RID p_rid) { - LightOccluderPolygon *occluder_poly = memnew(LightOccluderPolygon); + canvas_light_occluder_polygon_owner.initialize_rid(p_rid); + LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.getornull(p_rid); occluder_poly->occluder = RSG::canvas_render->occluder_polygon_create(); - return canvas_light_occluder_polygon_owner.initialize_rid(p_rid, occluder_poly); } void RendererCanvasCull::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const Vector<Vector2> &p_shape, bool p_closed) { @@ -1466,6 +1524,44 @@ void RendererCanvasCull::canvas_item_set_default_texture_repeat(RID p_item, RS:: ci->texture_repeat = p_repeat; } +void RendererCanvasCull::update_visibility_notifiers() { + SelfList<Item::VisibilityNotifierData> *E = visibility_notifier_list.first(); + while (E) { + SelfList<Item::VisibilityNotifierData> *N = E->next(); + + Item::VisibilityNotifierData *visibility_notifier = E->self(); + if (visibility_notifier->just_visible) { + visibility_notifier->just_visible = false; + + if (!visibility_notifier->enter_callable.is_null()) { + if (RSG::threaded) { + visibility_notifier->enter_callable.call_deferred(nullptr, 0); + } else { + Callable::CallError ce; + Variant ret; + visibility_notifier->enter_callable.call(nullptr, 0, ret, ce); + } + } + } else { + if (visibility_notifier->visible_in_frame != RSG::rasterizer->get_frame_number()) { + visibility_notifier_list.remove(E); + + if (!visibility_notifier->exit_callable.is_null()) { + if (RSG::threaded) { + visibility_notifier->exit_callable.call_deferred(nullptr, 0); + } else { + Callable::CallError ce; + Variant ret; + visibility_notifier->exit_callable.call(nullptr, 0, ret, ce); + } + } + } + } + + E = N; + } +} + bool RendererCanvasCull::free(RID p_rid) { if (canvas_owner.owns(p_rid)) { Canvas *canvas = canvas_owner.getornull(p_rid); @@ -1496,8 +1592,6 @@ bool RendererCanvasCull::free(RID p_rid) { canvas_owner.free(p_rid); - memdelete(canvas); - } else if (canvas_item_owner.owns(p_rid)) { Item *canvas_item = canvas_item_owner.getornull(p_rid); ERR_FAIL_COND_V(!canvas_item, true); @@ -1520,6 +1614,10 @@ bool RendererCanvasCull::free(RID p_rid) { canvas_item->child_items[i]->parent = RID(); } + if (canvas_item->visibility_notifier != nullptr) { + visibility_notifier_allocator.free(canvas_item->visibility_notifier); + } + /* if (canvas_item->material) { canvas_item->material->owners.erase(canvas_item); @@ -1528,8 +1626,6 @@ bool RendererCanvasCull::free(RID p_rid) { canvas_item_owner.free(p_rid); - memdelete(canvas_item); - } else if (canvas_light_owner.owns(p_rid)) { RendererCanvasRender::Light *canvas_light = canvas_light_owner.getornull(p_rid); ERR_FAIL_COND_V(!canvas_light, true); @@ -1544,7 +1640,6 @@ bool RendererCanvasCull::free(RID p_rid) { RSG::canvas_render->free(canvas_light->light_internal); canvas_light_owner.free(p_rid); - memdelete(canvas_light); } else if (canvas_light_occluder_owner.owns(p_rid)) { RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.getornull(p_rid); @@ -1563,7 +1658,6 @@ bool RendererCanvasCull::free(RID p_rid) { } canvas_light_occluder_owner.free(p_rid); - memdelete(occluder); } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) { LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.getornull(p_rid); @@ -1576,7 +1670,6 @@ bool RendererCanvasCull::free(RID p_rid) { } canvas_light_occluder_polygon_owner.free(p_rid); - memdelete(occluder_poly); } else { return false; } |