diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc index a424b54e11e1..3ffa32bf4e92 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -3,6 +3,7 @@ #include +#include "BKE_object.h" #include "DEG_depsgraph_query.h" #include "DNA_scene_types.h" @@ -305,22 +306,26 @@ void BlenderSceneDelegate::update_objects(Object *object) if (!ObjectData::is_supported(object)) { return; } + pxr::SdfPath id = object_prim_id(object); ObjectData *obj_data = object_data(id); if (obj_data) { obj_data->update_parent(); obj_data->update(); + obj_data->update_visibility(); return; } - if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) { + + if (!ObjectData::is_visible(this, object)) { + /* Do not export new object if it is invisible */ return; } + objects_[id] = ObjectData::create(this, object, id); obj_data = object_data(id); obj_data->update_parent(); obj_data->init(); obj_data->insert(); - obj_data->update_visibility(); } void BlenderSceneDelegate::update_instancers(Object *object) @@ -333,26 +338,30 @@ void BlenderSceneDelegate::update_instancers(Object *object) pxr::SdfPath id = instancer_prim_id(object); InstancerData *i_data = instancer_data(id); if (i_data) { - if (object->transflag & OB_DUPLI) { - i_data->update(); - } - else { + if ((object->transflag & OB_DUPLI) == 0) { + /* Object isn't instancer anymore and should be removed */ i_data->remove(); instancers_.erase(id); + return; } + + i_data->update(); return; } + if ((object->transflag & OB_DUPLI) == 0) { return; } - if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) { + + if (!InstancerData::is_visible(this, object)) { + /* Do not export new instancer if it is invisible */ return; } + instancers_[id] = std::make_unique(this, object, id); i_data = instancer_data(id); i_data->init(); i_data->insert(); - i_data->update_visibility(); } void BlenderSceneDelegate::update_world() @@ -385,6 +394,8 @@ void BlenderSceneDelegate::check_updates() DEGIDIterData data = {0}; data.graph = depsgraph; data.only_updated = true; + eEvaluationMode deg_mode = DEG_get_mode(depsgraph); + ITER_BEGIN (DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, @@ -396,6 +407,11 @@ void BlenderSceneDelegate::check_updates() switch (GS(id->name)) { case ID_OB: { Object *object = (Object *)id; + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, + 2, + "Visibility: %s [%s]", + object->id.name, + std::bitset<3>(BKE_object_visibility(object, deg_mode)).to_string().c_str()); update_objects(object); update_instancers(object); } break; @@ -414,6 +430,10 @@ void BlenderSceneDelegate::check_updates() } break; case ID_SCE: { + if (id->recalc & ID_RECALC_COPY_ON_WRITE && !(id->recalc & ID_RECALC_SELECT)) { + do_update_collection = true; + do_update_visibility = true; + } if (id->recalc & ID_RECALC_BASE_FLAGS) { do_update_visibility = true; } @@ -448,12 +468,13 @@ void BlenderSceneDelegate::add_new_objects() { DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; - settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | - DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; + settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; DEGObjectIterData data = {0}; data.settings = &settings; data.graph = settings.depsgraph; data.flag = settings.flags; + eEvaluationMode deg_mode = DEG_get_mode(depsgraph); + ITER_BEGIN (DEG_iterator_objects_begin, DEG_iterator_objects_next, DEG_iterator_objects_end, @@ -461,7 +482,11 @@ void BlenderSceneDelegate::add_new_objects() Object *, object) { - + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, + 2, + "Visibility: %s [%s]", + object->id.name, + std::bitset<3>(BKE_object_visibility(object, deg_mode)).to_string().c_str()); update_objects(object); update_instancers(object); } @@ -480,6 +505,7 @@ void BlenderSceneDelegate::remove_unused_objects() data.settings = &settings; data.graph = settings.depsgraph; data.flag = settings.flags; + ITER_BEGIN (DEG_iterator_objects_begin, DEG_iterator_objects_next, DEG_iterator_objects_end, @@ -539,6 +565,7 @@ void BlenderSceneDelegate::remove_unused_objects() void BlenderSceneDelegate::update_visibility() { + /* Updating visibility of existing objects/instancers */ for (auto &it : objects_) { it.second->update_visibility(); } @@ -546,15 +573,16 @@ void BlenderSceneDelegate::update_visibility() it.second->update_visibility(); } - /* Add objects which were invisible before and not added yet */ + /* Add objects/instancers which were invisible before and not added yet */ DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; - settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | - DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; + settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; DEGObjectIterData data = {0}; data.settings = &settings; data.graph = settings.depsgraph; data.flag = settings.flags; + eEvaluationMode deg_mode = DEG_get_mode(depsgraph); + ITER_BEGIN (DEG_iterator_objects_begin, DEG_iterator_objects_next, DEG_iterator_objects_end, @@ -562,7 +590,11 @@ void BlenderSceneDelegate::update_visibility() Object *, object) { - + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, + 2, + "Visibility: %s [%s]", + object->id.name, + std::bitset<3>(BKE_object_visibility(object, deg_mode)).to_string().c_str()); if (!object_data(object_prim_id(object))) { update_objects(object); } diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 1a60741bb1b5..6f0177b4299e 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -4,6 +4,9 @@ #include #include +#include "BKE_object.h" +#include "DEG_depsgraph_query.h" + #include "blender_scene_delegate.h" #include "instancer.h" @@ -33,6 +36,29 @@ bool InstancerData::is_supported(Object *object) return false; } +bool InstancerData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object) +{ + eEvaluationMode deg_mode = DEG_get_mode(scene_delegate->depsgraph); + int vis = BKE_object_visibility(object, deg_mode); + bool ret = vis & OB_VISIBLE_INSTANCES; + if (deg_mode == DAG_EVAL_VIEWPORT) { + ret &= BKE_object_is_visible_in_viewport(scene_delegate->view3d, object); + } + else { + if (ret) { + /* If some of parent object is instancer, then currenct object as instancer + * is invisible in Final render */ + for (Object *ob = object->parent; ob != nullptr; ob = ob->parent) { + if (ob->transflag & OB_DUPLI) { + ret = false; + break; + } + } + } + } + return ret; +} + void InstancerData::init() { ID_LOG(2, ""); @@ -62,7 +88,6 @@ void InstancerData::remove() void InstancerData::update() { ID_LOG(2, ""); - Object *object = (Object *)id; if (id->recalc & ID_RECALC_GEOMETRY || (object->data && ((ID *)object->data)->recalc & ID_RECALC_GEOMETRY) || @@ -86,7 +111,10 @@ pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const bool InstancerData::update_visibility() { - bool ret = ObjectData::update_visibility(); + bool prev_visible = visible; + visible = is_visible(scene_delegate_, (Object *)id); + bool ret = visible != prev_visible; + if (ret) { auto &change_tracker = scene_delegate_->GetRenderIndex().GetChangeTracker(); change_tracker.MarkInstancerDirty(prim_id, pxr::HdChangeTracker::DirtyVisibility); @@ -164,6 +192,14 @@ void InstancerData::check_update(Object *object) pxr::SdfPath path = object_prim_id(object); MeshInstance *m_inst = mesh_instance(path); if (m_inst) { + if (!is_instance_visible(object)) { + m_inst->data->remove(); + mesh_instances_.erase(path); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( + prim_id, pxr::HdChangeTracker::AllDirty); + return; + } + m_inst->data->update(); if (m_inst->data->id->recalc & ID_RECALC_TRANSFORM) { @@ -176,6 +212,13 @@ void InstancerData::check_update(Object *object) LightInstance *l_inst = light_instance(path); if (l_inst) { + if (!is_instance_visible(object)) { + l_inst->transforms.clear(); + update_light_instance(*l_inst); + light_instances_.erase(path); + return; + } + Object *obj = (Object *)l_inst->data->id; if (obj->id.recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY) || ((ID *)obj->data)->recalc & ID_RECALC_GEOMETRY) @@ -184,6 +227,28 @@ void InstancerData::check_update(Object *object) } return; } + + /* Checking if object wasn't added to instances before */ + if (is_supported(object) && is_instance_visible(object)) { + bool do_write_instances = false; + ListBase *lb = object_duplilist( + scene_delegate_->depsgraph, scene_delegate_->scene, (Object *)id); + LISTBASE_FOREACH (DupliObject *, dupli, lb) { + if (dupli->ob == object) { + do_write_instances = true; + break; + } + } + free_object_duplilist(lb); + + if (do_write_instances) { + write_instances(); + if (!mesh_instances_.empty()) { + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( + prim_id, pxr::HdChangeTracker::AllDirty); + } + } + } } void InstancerData::check_remove(std::set &available_objects) @@ -237,6 +302,22 @@ void InstancerData::update_double_sided(MaterialData *mat_data) } } +bool InstancerData::is_instance_visible(Object *object) +{ + eEvaluationMode deg_mode = DEG_get_mode(scene_delegate_->depsgraph); + int vis = BKE_object_visibility(object, deg_mode); + bool ret = vis & OB_VISIBLE_SELF; + if (deg_mode == DAG_EVAL_VIEWPORT) { + if (!ret && ((object->transflag & OB_DUPLI) == 0 || + (object->transflag & OB_DUPLI && + object->duplicator_visibility_flag & OB_DUPLI_FLAG_VIEWPORT))) + { + ret = true; + } + } + return ret; +} + pxr::SdfPath InstancerData::object_prim_id(Object *object) const { /* Making id of object in form like _ */ @@ -273,7 +354,7 @@ void InstancerData::write_instances() scene_delegate_->depsgraph, scene_delegate_->scene, (Object *)id); LISTBASE_FOREACH (DupliObject *, dupli, lb) { Object *ob = dupli->ob; - if (!is_supported(ob)) { + if (!is_supported(ob) || !is_instance_visible(ob)) { continue; } diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 1dfb1e98c4b8..97ea2ebe7b21 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -25,6 +25,7 @@ class InstancerData : public ObjectData { public: InstancerData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id); static bool is_supported(Object *object); + static bool is_visible(BlenderSceneDelegate *scene_delegate, Object *object); void init() override; void insert() override; @@ -46,6 +47,7 @@ class InstancerData : public ObjectData { void update_double_sided(MaterialData *mat_data); private: + bool is_instance_visible(Object *object); pxr::SdfPath object_prim_id(Object *object) const; pxr::SdfPath light_prim_id(LightInstance const &inst, int index) const; int light_prim_id_index(pxr::SdfPath const &id) const; diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 3e750df0c72b..8bb14cdae268 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -2,6 +2,7 @@ * Copyright 2011-2022 Blender Foundation */ #include "BKE_object.h" +#include "DEG_depsgraph_query.h" #include "blender_scene_delegate.h" #include "light.h" @@ -59,21 +60,36 @@ bool ObjectData::is_supported(Object *object) return false; } -bool ObjectData::update_visibility() +bool ObjectData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object) { - if (!scene_delegate_->view3d) { - return false; + eEvaluationMode deg_mode = DEG_get_mode(scene_delegate->depsgraph); + int vis = BKE_object_visibility(object, deg_mode); + bool ret = vis & OB_VISIBLE_SELF; + if (deg_mode == DAG_EVAL_VIEWPORT) { + ret &= BKE_object_is_visible_in_viewport(scene_delegate->view3d, object); } - - bool prev_visible = visible; - visible = BKE_object_is_visible_in_viewport(scene_delegate_->view3d, (Object *)id); - bool ret = visible != prev_visible; - if (ret) { - ID_LOG(2, ""); + else { + if (ret) { + /* If some of parent object is instancer, then currenct object + * is invisible in Final render */ + for (Object *ob = object->parent; ob != nullptr; ob = ob->parent) { + if (ob->transflag & OB_DUPLI) { + ret = false; + break; + } + } + } } return ret; } +bool ObjectData::update_visibility() +{ + bool prev_visible = visible; + visible = is_visible(scene_delegate_, (Object *)id); + return visible != prev_visible; +} + void ObjectData::update_parent() { Object *object = (Object *)id; @@ -87,7 +103,6 @@ void ObjectData::update_parent() scene_delegate_->instancer_prim_id(ob)); if (i_data) { i_data->update_as_parent(); - break; } } } diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index b6d5e4e48d5e..6ed40aa579a3 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -22,6 +22,7 @@ class ObjectData : public IdData { Object *object, pxr::SdfPath const &prim_id); static bool is_supported(Object *object); + static bool is_visible(BlenderSceneDelegate *scene_delegate, Object *object); virtual bool update_visibility(); void update_parent();