diff options
author | reduz <reduzio@gmail.com> | 2021-06-16 15:43:02 -0300 |
---|---|---|
committer | reduz <reduzio@gmail.com> | 2021-06-16 18:50:39 -0300 |
commit | 6e98c4cd502949fc3659c882ac671a69457251b4 (patch) | |
tree | 5191d90e436da24e7eb69ca14694740cba1b70e2 /servers/rendering | |
parent | 341cb8da311698d685b390524d0f20795f1774d7 (diff) |
Refactor VisibilityNotifier3D
* This is the 3D counterpart to #49632
* Implemented a bit different as 3D works using instancing
After merged, both 2D and 3D classes will most likely be renamed in a separate PR to DisplayNotifier2D/3D.
Diffstat (limited to 'servers/rendering')
-rw-r--r-- | servers/rendering/rasterizer_dummy.h | 15 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/renderer_storage_rd.cpp | 63 | ||||
-rw-r--r-- | servers/rendering/renderer_rd/renderer_storage_rd.h | 19 | ||||
-rw-r--r-- | servers/rendering/renderer_scene.h | 1 | ||||
-rw-r--r-- | servers/rendering/renderer_scene_cull.cpp | 45 | ||||
-rw-r--r-- | servers/rendering/renderer_scene_cull.h | 19 | ||||
-rw-r--r-- | servers/rendering/renderer_storage.h | 8 | ||||
-rw-r--r-- | servers/rendering/rendering_server_default.cpp | 1 | ||||
-rw-r--r-- | servers/rendering/rendering_server_default.h | 8 |
9 files changed, 175 insertions, 4 deletions
diff --git a/servers/rendering/rasterizer_dummy.h b/servers/rendering/rasterizer_dummy.h index b44088e822..d4c25b6253 100644 --- a/servers/rendering/rasterizer_dummy.h +++ b/servers/rendering/rasterizer_dummy.h @@ -621,9 +621,18 @@ public: bool particles_collision_is_heightfield(RID p_particles_collision) const override { return false; } RID particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const override { return RID(); } - RID particles_collision_instance_create(RID p_collision) override { return RID(); }; - void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) override{}; - void particles_collision_instance_set_active(RID p_collision_instance, bool p_active) override{}; + RID particles_collision_instance_create(RID p_collision) override { return RID(); } + void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) override {} + void particles_collision_instance_set_active(RID p_collision_instance, bool p_active) override {} + + /* VISIBILITY NOTIFIER */ + virtual RID visibility_notifier_allocate() override { return RID(); } + virtual void visibility_notifier_initialize(RID p_notifier) override {} + virtual void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) override {} + virtual void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) override {} + + virtual AABB visibility_notifier_get_aabb(RID p_notifier) const override { return AABB(); } + virtual void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) override {} /* GLOBAL VARIABLES */ diff --git a/servers/rendering/renderer_rd/renderer_storage_rd.cpp b/servers/rendering/renderer_rd/renderer_storage_rd.cpp index 64be54115f..246b5f0e4c 100644 --- a/servers/rendering/renderer_rd/renderer_storage_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_storage_rd.cpp @@ -5535,6 +5535,59 @@ void RendererStorageRD::particles_collision_instance_set_active(RID p_collision_ pci->active = p_active; } +/* VISIBILITY NOTIFIER */ + +RID RendererStorageRD::visibility_notifier_allocate() { + return visibility_notifier_owner.allocate_rid(); +} +void RendererStorageRD::visibility_notifier_initialize(RID p_notifier) { + visibility_notifier_owner.initialize_rid(p_notifier, VisibilityNotifier()); +} +void RendererStorageRD::visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) { + VisibilityNotifier *vn = visibility_notifier_owner.getornull(p_notifier); + ERR_FAIL_COND(!vn); + vn->aabb = p_aabb; + vn->dependency.changed_notify(DEPENDENCY_CHANGED_AABB); +} +void RendererStorageRD::visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) { + VisibilityNotifier *vn = visibility_notifier_owner.getornull(p_notifier); + ERR_FAIL_COND(!vn); + vn->enter_callback = p_enter_callbable; + vn->exit_callback = p_exit_callable; +} + +AABB RendererStorageRD::visibility_notifier_get_aabb(RID p_notifier) const { + const VisibilityNotifier *vn = visibility_notifier_owner.getornull(p_notifier); + ERR_FAIL_COND_V(!vn, AABB()); + return vn->aabb; +} +void RendererStorageRD::visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) { + VisibilityNotifier *vn = visibility_notifier_owner.getornull(p_notifier); + ERR_FAIL_COND(!vn); + + if (p_enter) { + if (!vn->enter_callback.is_null()) { + if (p_deferred) { + vn->enter_callback.call_deferred(nullptr, 0); + } else { + Variant r; + Callable::CallError ce; + vn->enter_callback.call(nullptr, 0, r, ce); + } + } + } else { + if (!vn->exit_callback.is_null()) { + if (p_deferred) { + vn->exit_callback.call_deferred(nullptr, 0); + } else { + Variant r; + Callable::CallError ce; + vn->exit_callback.call(nullptr, 0, r, ce); + } + } + } +} + /* SKELETON API */ RID RendererStorageRD::skeleton_allocate() { @@ -7590,6 +7643,9 @@ void RendererStorageRD::base_update_dependency(RID p_base, DependencyTracker *p_ } else if (particles_collision_owner.owns(p_base)) { ParticlesCollision *pc = particles_collision_owner.getornull(p_base); p_instance->update_dependency(&pc->dependency); + } else if (visibility_notifier_owner.owns(p_base)) { + VisibilityNotifier *vn = visibility_notifier_owner.getornull(p_base); + p_instance->update_dependency(&vn->dependency); } } @@ -7628,6 +7684,9 @@ RS::InstanceType RendererStorageRD::get_base_type(RID p_rid) const { if (particles_collision_owner.owns(p_rid)) { return RS::INSTANCE_PARTICLES_COLLISION; } + if (visibility_notifier_owner.owns(p_rid)) { + return RS::INSTANCE_VISIBLITY_NOTIFIER; + } return RS::INSTANCE_NONE; } @@ -8704,6 +8763,10 @@ bool RendererStorageRD::free(RID p_rid) { } particles_collision->dependency.deleted_notify(p_rid); particles_collision_owner.free(p_rid); + } else if (visibility_notifier_owner.owns(p_rid)) { + VisibilityNotifier *vn = visibility_notifier_owner.getornull(p_rid); + vn->dependency.deleted_notify(p_rid); + visibility_notifier_owner.free(p_rid); } else if (particles_collision_instance_owner.owns(p_rid)) { particles_collision_instance_owner.free(p_rid); } else if (render_target_owner.owns(p_rid)) { diff --git a/servers/rendering/renderer_rd/renderer_storage_rd.h b/servers/rendering/renderer_rd/renderer_storage_rd.h index 28a1044705..ab470cb3a6 100644 --- a/servers/rendering/renderer_rd/renderer_storage_rd.h +++ b/servers/rendering/renderer_rd/renderer_storage_rd.h @@ -947,6 +947,17 @@ private: mutable RID_Owner<ParticlesCollisionInstance> particles_collision_instance_owner; + /* visibility_notifier */ + + struct VisibilityNotifier { + AABB aabb; + Callable enter_callback; + Callable exit_callback; + Dependency dependency; + }; + + mutable RID_Owner<VisibilityNotifier> visibility_notifier_owner; + /* Skeleton */ struct Skeleton { @@ -2253,6 +2264,14 @@ public: virtual bool particles_collision_is_heightfield(RID p_particles_collision) const; RID particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const; + virtual RID visibility_notifier_allocate(); + virtual void visibility_notifier_initialize(RID p_notifier); + virtual void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb); + virtual void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable); + + virtual AABB visibility_notifier_get_aabb(RID p_notifier) const; + virtual void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred); + //used from 2D and 3D virtual RID particles_collision_instance_create(RID p_collision); virtual void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform); diff --git a/servers/rendering/renderer_scene.h b/servers/rendering/renderer_scene.h index 3b25498ed8..d71425f465 100644 --- a/servers/rendering/renderer_scene.h +++ b/servers/rendering/renderer_scene.h @@ -207,6 +207,7 @@ public: virtual void update() = 0; virtual void render_probes() = 0; + virtual void update_visibility_notifiers() = 0; virtual bool free(RID p_rid) = 0; diff --git a/servers/rendering/renderer_scene_cull.cpp b/servers/rendering/renderer_scene_cull.cpp index 2c66b22089..60c7dbf3b7 100644 --- a/servers/rendering/renderer_scene_cull.cpp +++ b/servers/rendering/renderer_scene_cull.cpp @@ -506,6 +506,9 @@ void RendererSceneCull::instance_set_base(RID p_instance, RID p_base) { InstanceParticlesCollisionData *collision = static_cast<InstanceParticlesCollisionData *>(instance->base_data); RSG::storage->free(collision->instance); } break; + case RS::INSTANCE_VISIBLITY_NOTIFIER: { + //none + } break; case RS::INSTANCE_REFLECTION_PROBE: { InstanceReflectionProbeData *reflection_probe = static_cast<InstanceReflectionProbeData *>(instance->base_data); scene_render->free(reflection_probe->instance); @@ -615,6 +618,11 @@ void RendererSceneCull::instance_set_base(RID p_instance, RID p_base) { RSG::storage->particles_collision_instance_set_active(collision->instance, instance->visible); instance->base_data = collision; } break; + case RS::INSTANCE_VISIBLITY_NOTIFIER: { + InstanceVisibilityNotifierData *vnd = memnew(InstanceVisibilityNotifierData); + vnd->base = p_base; + instance->base_data = vnd; + } break; case RS::INSTANCE_REFLECTION_PROBE: { InstanceReflectionProbeData *reflection_probe = memnew(InstanceReflectionProbeData); reflection_probe->owner = instance; @@ -1549,6 +1557,9 @@ void RendererSceneCull::_update_instance(Instance *p_instance) { case RS::INSTANCE_VOXEL_GI: { idata.instance_data_rid = static_cast<InstanceVoxelGIData *>(p_instance->base_data)->probe_instance.get_id(); } break; + case RS::INSTANCE_VISIBLITY_NOTIFIER: { + idata.visibility_notifier = static_cast<InstanceVisibilityNotifierData *>(p_instance->base_data); + } break; default: { } } @@ -1758,6 +1769,9 @@ void RendererSceneCull::_update_instance_aabb(Instance *p_instance) { new_aabb = RSG::storage->particles_collision_get_aabb(p_instance->base); } break; + case RenderingServer::INSTANCE_VISIBLITY_NOTIFIER: { + new_aabb = RSG::storage->visibility_notifier_get_aabb(p_instance->base); + } break; case RenderingServer::INSTANCE_LIGHT: { new_aabb = RSG::storage->light_get_aabb(p_instance->base); @@ -2613,6 +2627,15 @@ void RendererSceneCull::_scene_cull(CullData &cull_data, InstanceCullResult &cul } else if (base_type == RS::INSTANCE_LIGHTMAP) { cull_result.lightmaps.push_back(RID::from_uint64(idata.instance_data_rid)); + } else if (base_type == RS::INSTANCE_VISIBLITY_NOTIFIER) { + InstanceVisibilityNotifierData *vnd = idata.visibility_notifier; + if (!vnd->list_element.in_list()) { + visible_notifier_list_lock.lock(); + visible_notifier_list.add(&vnd->list_element); + visible_notifier_list_lock.unlock(); + vnd->just_visible = true; + } + vnd->visible_in_frame = RSG::rasterizer->get_frame_number(); } else if (((1 << base_type) & RS::INSTANCE_GEOMETRY_MASK) && !(idata.flags & InstanceData::FLAG_CAST_SHADOWS_ONLY)) { bool keep = true; @@ -3849,6 +3872,28 @@ TypedArray<Image> RendererSceneCull::bake_render_uv2(RID p_base, const Vector<RI return scene_render->bake_render_uv2(p_base, p_material_overrides, p_image_size); } +void RendererSceneCull::update_visibility_notifiers() { + SelfList<InstanceVisibilityNotifierData> *E = visible_notifier_list.first(); + while (E) { + SelfList<InstanceVisibilityNotifierData> *N = E->next(); + + InstanceVisibilityNotifierData *visibility_notifier = E->self(); + if (visibility_notifier->just_visible) { + visibility_notifier->just_visible = false; + + RSG::storage->visibility_notifier_call(visibility_notifier->base, true, RSG::threaded); + } else { + if (visibility_notifier->visible_in_frame != RSG::rasterizer->get_frame_number()) { + visible_notifier_list.remove(E); + + RSG::storage->visibility_notifier_call(visibility_notifier->base, false, RSG::threaded); + } + } + + E = N; + } +} + /*******************************/ /* Passthrough to Scene Render */ /*******************************/ diff --git a/servers/rendering/renderer_scene_cull.h b/servers/rendering/renderer_scene_cull.h index bdfbea95a2..d9466d8b04 100644 --- a/servers/rendering/renderer_scene_cull.h +++ b/servers/rendering/renderer_scene_cull.h @@ -118,6 +118,8 @@ public: virtual void occluder_initialize(RID p_occluder); virtual void occluder_set_mesh(RID p_occluder, const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices); + /* VISIBILITY NOTIFIER API */ + RendererSceneOcclusionCull *dummy_occlusion_culling; /* SCENARIO API */ @@ -243,6 +245,8 @@ public: } }; + struct InstanceVisibilityNotifierData; + struct InstanceData { // Store instance pointer as well as common instance processing information, // to make processing more cache friendly. @@ -271,6 +275,7 @@ public: union { uint64_t instance_data_rid; RendererSceneRender::GeometryInstance *instance_geometry; + InstanceVisibilityNotifierData *visibility_notifier; }; Instance *instance = nullptr; int32_t parent_array_index = -1; @@ -611,6 +616,18 @@ public: RID instance; }; + struct InstanceVisibilityNotifierData : public InstanceBaseData { + bool just_visible = false; + uint64_t visible_in_frame = 0; + RID base; + SelfList<InstanceVisibilityNotifierData> list_element; + InstanceVisibilityNotifierData() : + list_element(this) {} + }; + + SpinLock visible_notifier_list_lock; + SelfList<InstanceVisibilityNotifierData>::List visible_notifier_list; + struct InstanceLightData : public InstanceBaseData { RID instance; uint64_t last_version; @@ -1123,6 +1140,8 @@ public: void set_scene_render(RendererSceneRender *p_scene_render); + virtual void update_visibility_notifiers(); + RendererSceneCull(); virtual ~RendererSceneCull(); }; diff --git a/servers/rendering/renderer_storage.h b/servers/rendering/renderer_storage.h index e6b9ac15ff..f22c582f48 100644 --- a/servers/rendering/renderer_storage.h +++ b/servers/rendering/renderer_storage.h @@ -557,6 +557,14 @@ public: virtual bool particles_collision_is_heightfield(RID p_particles_collision) const = 0; virtual RID particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const = 0; + virtual RID visibility_notifier_allocate() = 0; + virtual void visibility_notifier_initialize(RID p_notifier) = 0; + virtual void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) = 0; + virtual void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) = 0; + + virtual AABB visibility_notifier_get_aabb(RID p_notifier) const = 0; + virtual void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) = 0; + //used from 2D and 3D virtual RID particles_collision_instance_create(RID p_collision) = 0; virtual void particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) = 0; diff --git a/servers/rendering/rendering_server_default.cpp b/servers/rendering/rendering_server_default.cpp index 95aa05f6b3..629d212b69 100644 --- a/servers/rendering/rendering_server_default.cpp +++ b/servers/rendering/rendering_server_default.cpp @@ -118,6 +118,7 @@ void RenderingServerDefault::_draw(bool p_swap_buffers, double frame_step) { RSG::rasterizer->end_frame(p_swap_buffers); RSG::canvas->update_visibility_notifiers(); + RSG::scene->update_visibility_notifiers(); while (frame_drawn_callbacks.front()) { Object *obj = ObjectDB::get_instance(frame_drawn_callbacks.front()->get().object); diff --git a/servers/rendering/rendering_server_default.h b/servers/rendering/rendering_server_default.h index 73373c97bb..9404374287 100644 --- a/servers/rendering/rendering_server_default.h +++ b/servers/rendering/rendering_server_default.h @@ -529,6 +529,12 @@ public: FUNC1(particles_collision_height_field_update, RID) FUNC2(particles_collision_set_height_field_resolution, RID, ParticlesCollisionHeightfieldResolution) + /* VISIBILITY_NOTIFIER */ + + FUNCRIDSPLIT(visibility_notifier) + FUNC2(visibility_notifier_set_aabb, RID, const AABB &) + FUNC3(visibility_notifier_set_callbacks, RID, const Callable &, const Callable &) + #undef server_name #undef ServerName //from now on, calls forwarded to this singleton @@ -549,7 +555,7 @@ public: /* OCCLUDER */ FUNCRIDSPLIT(occluder) - FUNC3(occluder_set_mesh, RID, const PackedVector3Array &, const PackedInt32Array &); + FUNC3(occluder_set_mesh, RID, const PackedVector3Array &, const PackedInt32Array &) #undef server_name #undef ServerName |