From 931f2fc6e617d5ce7d72dfea865c09cb105f0abd Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 22 Jun 2023 14:17:47 +0300 Subject: [PATCH 01/13] Make instancers creation via depsgraph. Initial changes. --- .../scene_delegate/blender_scene_delegate.cc | 19 ++++++++---- .../scene_delegate/blender_scene_delegate.h | 1 + .../blender/render/hydra/scene_delegate/id.h | 3 +- .../render/hydra/scene_delegate/instancer.cc | 30 +++++++++++++++---- 4 files changed, 40 insertions(+), 13 deletions(-) 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 4326fc6bf008..53f47749c6b1 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -326,10 +326,13 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool p_id = id; } - auto i_data = instancers_.lookup_ptr(p_id); - if (i_data) { - return i_data->get(); + if (instancer_data_ && p_id == instancer_data_->prim_id) { + return instancer_data_.get(); } + //auto i_data = instancers_.lookup_ptr(p_id); + //if (i_data) { + // return i_data->get(); + //} return nullptr; } @@ -499,7 +502,8 @@ void BlenderSceneDelegate::add_new_objects() { DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; - settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; + settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | + DEG_ITER_OBJECT_FLAG_VISIBLE; DEGObjectIterData data = {0}; data.settings = &settings; data.graph = settings.depsgraph; @@ -519,9 +523,14 @@ void BlenderSceneDelegate::add_new_objects() object->id.name, std::bitset<3>(BKE_object_visibility(object, deg_mode)).to_string().c_str()); update_objects(object); - update_instancers(object); + //update_instancers(object); } ITER_END; + + instancer_data_ = std::make_unique( + this, nullptr, GetDelegateID().AppendElementString("Instancer")); + instancer_data_->init(); + instancer_data_->insert(); } void BlenderSceneDelegate::remove_unused_objects() diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h index c7b1468408e4..2cc98ebb76a5 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -97,6 +97,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { ObjectDataMap objects_; MaterialDataMap materials_; InstancerDataMap instancers_; + std::unique_ptr instancer_data_; std::unique_ptr world_data_; }; diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index ccfba5a61584..720ddd7bdd7a 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -56,9 +56,8 @@ template const T IdData::get_data(pxr::TfToken const &key) const #define ID_LOG(level, msg, ...) \ CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ level, \ - "%s (%s): " msg, \ + "%s: " msg, \ prim_id.GetText(), \ - id->name, \ ##__VA_ARGS__); } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 3db5626e89e5..4306ff150108 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -349,11 +349,27 @@ void InstancerData::write_instances() l_inst.transforms.clear(); } - ListBase *lb = object_duplilist( - scene_delegate_->depsgraph, scene_delegate_->scene, (Object *)id); - LISTBASE_FOREACH (DupliObject *, dupli, lb) { + DEGObjectIterSettings s = {0}; + s.depsgraph = scene_delegate_->depsgraph; + s.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | + DEG_ITER_OBJECT_FLAG_VISIBLE | DEG_ITER_OBJECT_FLAG_DUPLI; + DEGObjectIterData d = {0}; + d.settings = &s; + d.graph = s.depsgraph; + d.flag = s.flags; + ITER_BEGIN (DEG_iterator_objects_begin, + DEG_iterator_objects_next, + DEG_iterator_objects_end, + &d, + Object *, + object) + { + if (d.dupli_object_current == nullptr) { + continue; + } + DupliObject *dupli = d.dupli_object_current; Object *ob = dupli->ob; - if (!is_supported(ob) || !is_instance_visible(ob)) { + if (!is_supported(ob)) { continue; } @@ -381,7 +397,7 @@ void InstancerData::write_instances() mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); } } - free_object_duplilist(lb); + ITER_END; /* Remove mesh intances without indices */ mesh_instances_.remove_if([&](auto item) { @@ -393,8 +409,10 @@ void InstancerData::write_instances() }); /* Update light intances and remove instances without transforms */ + for (auto &l_inst : light_instances_.values()) { + update_light_instance(l_inst); + } light_instances_.remove_if([&](auto item) { - update_light_instance(item.value); return item.value.transforms.empty(); }); } -- 2.30.2 From 3988b9335021e8d2efae2854ad74febc32a38872 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 22 Jun 2023 20:49:47 +0300 Subject: [PATCH 02/13] Implemented update_collection(), fixed memory leak. --- .../scene_delegate/blender_scene_delegate.cc | 60 +++++++------------ .../scene_delegate/blender_scene_delegate.h | 2 +- .../render/hydra/scene_delegate/image.cc | 5 +- .../render/hydra/scene_delegate/light.cc | 16 +++-- .../render/hydra/scene_delegate/mesh.cc | 15 +++-- .../render/hydra/scene_delegate/object.cc | 16 +---- 6 files changed, 45 insertions(+), 69 deletions(-) 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 53f47749c6b1..fd125ff64909 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -19,6 +19,8 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, Engine *engine) : HdSceneDelegate(parent_index, delegate_id), engine(engine) { + instancer_data_ = std::make_unique( + this, nullptr, GetDelegateID().AppendElementString("Instancer")); } pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id) @@ -205,7 +207,7 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) check_updates(); } else { - add_new_objects(); + update_collection(); update_world(); } } @@ -422,7 +424,6 @@ void BlenderSceneDelegate::update_world() void BlenderSceneDelegate::check_updates() { bool do_update_collection = false; - bool do_update_visibility = false; bool do_update_world = false; DEGIDIterData data = {0}; @@ -440,14 +441,7 @@ 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); + do_update_collection = true; } break; case ID_MA: { @@ -466,10 +460,9 @@ void BlenderSceneDelegate::check_updates() 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; + do_update_collection = true; } if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { do_update_collection = true; @@ -491,10 +484,7 @@ void BlenderSceneDelegate::check_updates() update_world(); } if (do_update_collection) { - remove_unused_objects(); - } - if (do_update_visibility) { - update_visibility(); + update_collection(); } } @@ -527,20 +517,19 @@ void BlenderSceneDelegate::add_new_objects() } ITER_END; - instancer_data_ = std::make_unique( - this, nullptr, GetDelegateID().AppendElementString("Instancer")); instancer_data_->init(); instancer_data_->insert(); } -void BlenderSceneDelegate::remove_unused_objects() +void BlenderSceneDelegate::update_collection() { - /* Get available objects */ + /* Add or update available objects */ Set available_objects; DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; - settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; + settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | + DEG_ITER_OBJECT_FLAG_VISIBLE; DEGObjectIterData data = {0}; data.settings = &settings; data.graph = settings.depsgraph; @@ -553,24 +542,23 @@ void BlenderSceneDelegate::remove_unused_objects() Object *, object) { - if (ObjectData::is_supported(object)) { - available_objects.add(object_prim_id(object).GetName()); + if (!ObjectData::is_supported(object)) { + continue; } - available_objects.add(instancer_prim_id(object).GetName()); - } - ITER_END; + available_objects.add(object_prim_id(object).GetName()); - /* Remove unused instancers */ - instancers_.remove_if([&](auto item) { - bool ret = !available_objects.contains(item.key.GetName()); - if (ret) { - item.value->remove(); + pxr::SdfPath id = object_prim_id(object); + ObjectData *obj_data = object_data(id); + if (obj_data) { + obj_data->update(); } else { - item.value->check_remove(available_objects); + obj_data = objects_.lookup_or_add(id, ObjectData::create(this, object, id)).get(); + obj_data->init(); + obj_data->insert(); } - return ret; - }); + } + ITER_END; /* Remove unused objects */ objects_.remove_if([&](auto item) { @@ -593,9 +581,7 @@ void BlenderSceneDelegate::remove_unused_objects() c_data->available_materials(available_materials); } } - for (auto &val : instancers_.values()) { - val->available_materials(available_materials); - } + instancer_data_->available_materials(available_materials); materials_.remove_if([&](auto item) { bool ret = !available_materials.contains(item.key); diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h index 2cc98ebb76a5..83dbf9784f82 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -91,7 +91,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { void update_world(); void check_updates(); void add_new_objects(); - void remove_unused_objects(); + void update_collection(); void update_visibility(); ObjectDataMap objects_; diff --git a/source/blender/render/hydra/scene_delegate/image.cc b/source/blender/render/hydra/scene_delegate/image.cc index c82731eb788f..340fb34cf424 100644 --- a/source/blender/render/hydra/scene_delegate/image.cc +++ b/source/blender/render/hydra/scene_delegate/image.cc @@ -76,8 +76,9 @@ std::string cache_or_get_image_file(Image *image, bContext *context, ImageUser * } else { Main *main = CTX_data_main(context); - file_path.reserve(FILE_MAX); - BKE_image_user_file_path_ex(main, iuser, image, file_path.data(), false, true); + char str[FILE_MAX]; + BKE_image_user_file_path_ex(main, iuser, image, str, false, true); + file_path = str; if (!pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(file_path)) { file_path = cache_image_file(image, context, iuser, true); diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index 7c1ae2f7dd30..6d225ec46f46 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -111,19 +111,16 @@ void LightData::remove() void LightData::update() { - ID_LOG(1, ""); - Object *object = (Object *)id; Light *light = (Light *)object->data; - if (prim_type(light) != prim_type_) { - remove(); - init(); - insert(); - return; - } - pxr::HdDirtyBits bits = pxr::HdLight::Clean; if (id->recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) { + if (prim_type(light) != prim_type_) { + remove(); + init(); + insert(); + return; + } init(); bits = pxr::HdLight::AllDirty; } @@ -133,6 +130,7 @@ void LightData::update() } if (bits != pxr::HdChangeTracker::Clean) { scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits); + ID_LOG(1, ""); } } diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index f2bf1235b2de..0f766a6e5a7b 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -299,12 +299,15 @@ void MeshData::write_materials() } pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); - m.mat_data = scene_delegate_->materials_ - .lookup_or_add(p_id, - std::make_unique(scene_delegate_, mat, p_id)) - .get(); - m.mat_data->init(); - m.mat_data->insert(); + m.mat_data = scene_delegate_->material_data(p_id); + if (!m.mat_data) { + m.mat_data = scene_delegate_->materials_ + .lookup_or_add(p_id, + std::make_unique(scene_delegate_, mat, p_id)) + .get(); + m.mat_data->init(); + m.mat_data->insert(); + } } } diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 48d0865eb730..2bebac19a24f 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -67,23 +67,11 @@ bool ObjectData::is_supported(Object *object) bool ObjectData::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_SELF; + bool ret = BKE_object_visibility(object, deg_mode) & OB_VISIBLE_SELF; 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 - * is invisible in Final render */ - for (Object *ob = object->parent; ob != nullptr; ob = ob->parent) { - if (ob->transflag & OB_DUPLI) { - ret = false; - break; - } - } - } - } + /* Note: visibility for final render we are taking from depsgraph */ return ret; } -- 2.30.2 From 58c2e57538f6af78392a0a320ec586173be1b783 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 22 Jun 2023 21:21:07 +0300 Subject: [PATCH 03/13] Removing unused functionality --- .../scene_delegate/blender_scene_delegate.cc | 163 ++---------------- .../scene_delegate/blender_scene_delegate.h | 7 +- .../render/hydra/scene_delegate/instancer.cc | 6 +- .../render/hydra/scene_delegate/instancer.h | 2 +- .../render/hydra/scene_delegate/material.cc | 4 +- .../render/hydra/scene_delegate/object.cc | 18 -- .../render/hydra/scene_delegate/object.h | 3 - 7 files changed, 15 insertions(+), 188 deletions(-) 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 fd125ff64909..5142094e7086 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -19,8 +19,6 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, Engine *engine) : HdSceneDelegate(parent_index, delegate_id), engine(engine) { - instancer_data_ = std::make_unique( - this, nullptr, GetDelegateID().AppendElementString("Instancer")); } pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id) @@ -217,15 +215,11 @@ void BlenderSceneDelegate::clear() for (auto &obj_data : objects_.values()) { obj_data->remove(); } - for (auto &i_data : instancers_.values()) { - i_data->remove(); - } + objects_.clear(); + instancer_data_->remove(); for (auto &mat_data : materials_.values()) { mat_data->remove(); } - - objects_.clear(); - instancers_.clear(); materials_.clear(); depsgraph = nullptr; @@ -262,9 +256,9 @@ pxr::SdfPath BlenderSceneDelegate::material_prim_id(Material *mat) const return prim_id((ID *)mat, "M"); } -pxr::SdfPath BlenderSceneDelegate::instancer_prim_id(Object *object) const +pxr::SdfPath BlenderSceneDelegate::instancer_prim_id() const { - return prim_id((ID *)object, "I"); + return GetDelegateID().AppendElementString("Instancer"); } pxr::SdfPath BlenderSceneDelegate::world_prim_id() const @@ -331,75 +325,9 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool if (instancer_data_ && p_id == instancer_data_->prim_id) { return instancer_data_.get(); } - //auto i_data = instancers_.lookup_ptr(p_id); - //if (i_data) { - // return i_data->get(); - //} return nullptr; } -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 (!ObjectData::is_visible(this, object)) { - /* Do not export new object if it is invisible */ - return; - } - - objects_.add_new(id, ObjectData::create(this, object, id)); - obj_data = object_data(id); - obj_data->update_parent(); - obj_data->init(); - obj_data->insert(); -} - -void BlenderSceneDelegate::update_instancers(Object *object) -{ - /* Check object inside instancers */ - for (auto &i_data : instancers_.values()) { - i_data->check_update(object); - } - - pxr::SdfPath id = instancer_prim_id(object); - InstancerData *i_data = instancer_data(id); - if (i_data) { - if ((object->transflag & OB_DUPLI) == 0) { - /* Object isn't instancer anymore and should be removed */ - i_data->remove(); - instancers_.remove(id); - return; - } - - i_data->update(); - return; - } - - if ((object->transflag & OB_DUPLI) == 0) { - return; - } - - if (!InstancerData::is_visible(this, object)) { - /* Do not export new instancer if it is invisible */ - return; - } - - i_data = instancers_.lookup_or_add(id, std::make_unique(this, object, id)).get(); - i_data->init(); - i_data->insert(); -} - void BlenderSceneDelegate::update_world() { World *world = scene->world; @@ -429,8 +357,6 @@ 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, @@ -488,39 +414,6 @@ void BlenderSceneDelegate::check_updates() } } -void BlenderSceneDelegate::add_new_objects() -{ - DEGObjectIterSettings settings = {0}; - settings.depsgraph = depsgraph; - settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | - DEG_ITER_OBJECT_FLAG_VISIBLE; - 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, - &data, - 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); - } - ITER_END; - - instancer_data_->init(); - instancer_data_->insert(); -} - void BlenderSceneDelegate::update_collection() { /* Add or update available objects */ @@ -560,6 +453,12 @@ void BlenderSceneDelegate::update_collection() } ITER_END; + if (!instancer_data_) { + instancer_data_ = std::make_unique(this, instancer_prim_id()); + instancer_data_->init(); + instancer_data_->insert(); + } + /* Remove unused objects */ objects_.remove_if([&](auto item) { bool ret = !available_objects.contains(item.key.GetName()); @@ -592,46 +491,4 @@ void BlenderSceneDelegate::update_collection() }); } -void BlenderSceneDelegate::update_visibility() -{ - /* Updating visibility of existing objects/instancers */ - for (auto &val : objects_.values()) { - val->update_visibility(); - } - for (auto &val : instancers_.values()) { - val->update_visibility(); - } - - /* 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_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, - &data, - 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); - } - if (!instancer_data(instancer_prim_id(object))) { - update_instancers(object); - } - } - ITER_END; -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h index 83dbf9784f82..10bd5d7ceecf 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -76,7 +76,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { pxr::SdfPath prim_id(ID *id, const char *prefix) const; pxr::SdfPath object_prim_id(Object *object) const; pxr::SdfPath material_prim_id(Material *mat) const; - pxr::SdfPath instancer_prim_id(Object *object) const; + pxr::SdfPath instancer_prim_id() const; pxr::SdfPath world_prim_id() const; ObjectData *object_data(pxr::SdfPath const &id) const; @@ -86,17 +86,12 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { MaterialData *material_data(pxr::SdfPath const &id) const; InstancerData *instancer_data(pxr::SdfPath const &id, bool child_id = false) const; - void update_objects(Object *object); - void update_instancers(Object *object); void update_world(); void check_updates(); - void add_new_objects(); void update_collection(); - void update_visibility(); ObjectDataMap objects_; MaterialDataMap materials_; - InstancerDataMap instancers_; std::unique_ptr instancer_data_; std::unique_ptr world_data_; }; diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 4306ff150108..65b7d18c03d5 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -12,10 +12,8 @@ namespace blender::render::hydra { -InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, - Object *object, - pxr::SdfPath const &prim_id) - : ObjectData(scene_delegate, object, prim_id) +InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) + : ObjectData(scene_delegate, nullptr, prim_id) { } diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index b8a1d127d64c..623780d70ac8 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -25,7 +25,7 @@ class InstancerData : public ObjectData { }; public: - InstancerData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id); + InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); static bool is_supported(Object *object); static bool is_visible(BlenderSceneDelegate *scene_delegate, Object *object); diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index f2a65531e5c0..2ea1bd861ace 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -67,9 +67,7 @@ void MaterialData::update() m_data->update_double_sided(this); } } - for (auto &i_data : scene_delegate_->instancers_.values()) { - i_data->update_double_sided(this); - } + scene_delegate_->instancer_data_->update_double_sided(this); } } diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 2bebac19a24f..9549eebb8b38 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -82,24 +82,6 @@ bool ObjectData::update_visibility() return visible != prev_visible; } -void ObjectData::update_parent() -{ - Object *object = (Object *)id; - if (parent_ != object->parent) { - ID_LOG(1, ""); - parent_ = object->parent; - - /* Looking for corresponded instancer and update it as parent */ - for (Object *ob = parent_; ob != nullptr; ob = ob->parent) { - InstancerData *i_data = scene_delegate_->instancer_data( - scene_delegate_->instancer_prim_id(ob)); - if (i_data) { - i_data->update_as_parent(); - } - } - } -} - void ObjectData::write_transform() { transform = gf_matrix_from_transform(((Object *)id)->object_to_world); diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 79b2150cdb7f..db3703f02df8 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -26,15 +26,12 @@ class ObjectData : public IdData { static bool is_visible(BlenderSceneDelegate *scene_delegate, Object *object); virtual bool update_visibility(); - void update_parent(); pxr::GfMatrix4d transform; bool visible = true; protected: void write_transform(); - - Object *parent_ = nullptr; }; using ObjectDataMap = Map>; -- 2.30.2 From e13eb5da52e1ad5a453635568600f5b8fb12f176 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 23 Jun 2023 10:49:13 +0300 Subject: [PATCH 04/13] Fixed instances updates + instances visibility --- .../scene_delegate/blender_scene_delegate.cc | 6 ++++++ .../render/hydra/scene_delegate/instancer.cc | 19 +++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) 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 5142094e7086..98c4a86ad131 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -438,6 +438,9 @@ void BlenderSceneDelegate::update_collection() if (!ObjectData::is_supported(object)) { continue; } + if (!ObjectData::is_visible(this, object)) { + continue; + } available_objects.add(object_prim_id(object).GetName()); pxr::SdfPath id = object_prim_id(object); @@ -458,6 +461,9 @@ void BlenderSceneDelegate::update_collection() instancer_data_->init(); instancer_data_->insert(); } + else { + instancer_data_->update(); + } /* Remove unused objects */ objects_.remove_if([&](auto item) { diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 65b7d18c03d5..f27f8b96eee1 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -86,15 +86,9 @@ void InstancerData::remove() void InstancerData::update() { ID_LOG(1, ""); - Object *object = (Object *)id; - if (id->recalc & ID_RECALC_GEOMETRY || - (object->data && ((ID *)object->data)->recalc & ID_RECALC_GEOMETRY) || - id->recalc & ID_RECALC_TRANSFORM) - { - write_instances(); - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( - prim_id, pxr::HdChangeTracker::AllDirty); - } + write_instances(); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( + prim_id, pxr::HdChangeTracker::AllDirty); } pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const @@ -370,7 +364,9 @@ void InstancerData::write_instances() if (!is_supported(ob)) { continue; } - + if (!ObjectData::is_visible(scene_delegate_, d.dupli_parent)) { + continue; + } pxr::SdfPath p_id = object_prim_id(ob); if (ob->type == OB_LAMP) { LightInstance *inst = light_instance(p_id); @@ -390,6 +386,9 @@ void InstancerData::write_instances() inst->data->init(); inst->data->insert(); } + else { + inst->data->update(); + } ID_LOG(2, "Mesh %s %d", inst->data->id->name, mesh_transforms_.size()); inst->indices.push_back(mesh_transforms_.size()); mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); -- 2.30.2 From 88be7d3cd92314d0da16b90499a3fd0e5acc1b5b Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 23 Jun 2023 12:05:38 +0300 Subject: [PATCH 05/13] Removed unused code. Adjusted ID_LOG --- .../blender/render/hydra/scene_delegate/id.h | 3 +- .../render/hydra/scene_delegate/instancer.cc | 143 +----------------- .../render/hydra/scene_delegate/instancer.h | 9 +- 3 files changed, 5 insertions(+), 150 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 720ddd7bdd7a..089871c0927a 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -56,8 +56,9 @@ template const T IdData::get_data(pxr::TfToken const &key) const #define ID_LOG(level, msg, ...) \ CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ level, \ - "%s: " msg, \ + "%s (%s): " msg, \ prim_id.GetText(), \ + id ? id->name : "", \ ##__VA_ARGS__); } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index f27f8b96eee1..1f3a6d91de4d 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -17,7 +17,7 @@ InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath { } -bool InstancerData::is_supported(Object *object) +bool InstancerData::is_instance_supported(Object *object) { switch (object->type) { case OB_MESH: @@ -34,29 +34,6 @@ 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(1, ""); @@ -179,99 +156,6 @@ pxr::SdfPathVector InstancerData::prototypes() const return paths; } -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_.remove(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) { - write_instances(); - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( - prim_id, pxr::HdChangeTracker::AllDirty); - } - return; - } - - 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_.remove(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) - { - write_instances(); - } - 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_.is_empty()) { - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( - prim_id, pxr::HdChangeTracker::AllDirty); - } - } - } -} - -void InstancerData::check_remove(Set &available_objects) -{ - bool ret = false; - - mesh_instances_.remove_if([&](auto item) { - bool res = !available_objects.contains(item.key.GetName()); - if (res) { - item.value.data->remove(); - ret = true; - }; - return res; - }); - - if (ret) { - write_instances(); - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( - prim_id, pxr::HdChangeTracker::AllDirty); - } - - light_instances_.remove_if([&](auto item) { - bool res = !available_objects.contains(item.key.GetName()); - if (res) { - item.value.transforms.clear(); - update_light_instance(item.value); - }; - return res; - }); -} - void InstancerData::available_materials(Set &paths) const { for (auto &m_inst : mesh_instances_.values()) { @@ -279,13 +163,6 @@ void InstancerData::available_materials(Set &paths) const } } -void InstancerData::update_as_parent() -{ - write_instances(); - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( - prim_id, pxr::HdChangeTracker::AllDirty); -} - void InstancerData::update_double_sided(MaterialData *mat_data) { for (auto &m_inst : mesh_instances_.values()) { @@ -293,22 +170,6 @@ 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 _ */ @@ -361,7 +222,7 @@ void InstancerData::write_instances() } DupliObject *dupli = d.dupli_object_current; Object *ob = dupli->ob; - if (!is_supported(ob)) { + if (!is_instance_supported(ob)) { continue; } if (!ObjectData::is_visible(scene_delegate_, d.dupli_parent)) { diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 623780d70ac8..62b7490a539a 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -26,8 +26,7 @@ class InstancerData : public ObjectData { public: InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); - static bool is_supported(Object *object); - static bool is_visible(BlenderSceneDelegate *scene_delegate, Object *object); + static bool is_instance_supported(Object *object); void init() override; void insert() override; @@ -42,14 +41,10 @@ class InstancerData : public ObjectData { pxr::VtIntArray indices(pxr::SdfPath const &id) const; ObjectData *object_data(pxr::SdfPath const &id) const; pxr::SdfPathVector prototypes() const; - void check_update(Object *object); - void check_remove(Set &available_objects); void available_materials(Set &paths) const; - void update_as_parent(); 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; @@ -63,6 +58,4 @@ class InstancerData : public ObjectData { pxr::VtMatrix4dArray mesh_transforms_; }; -using InstancerDataMap = Map>; - } // namespace blender::render::hydra -- 2.30.2 From 01f072063bd460241b0ad8162dcdf4b041633e13 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 23 Jun 2023 13:25:22 +0300 Subject: [PATCH 06/13] make format + improvements in image.cc --- source/blender/render/hydra/preview_engine.cc | 4 +++- source/blender/render/hydra/scene_delegate/image.cc | 4 +--- source/blender/render/hydra/scene_delegate/instancer.cc | 4 +--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 651154cbf4a2..70ee57a12a11 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -39,7 +39,9 @@ void PreviewEngine::update_render_result(std::vector &pixels) RenderLayer *layer = (RenderLayer *)result->layers.first; RenderPass *pass = (RenderPass *)layer->passes.first; - memcpy(pass->buffer.data, pixels.data(), sizeof(float) * pass->rectx * pass->recty * pass->channels); + memcpy(pass->buffer.data, + pixels.data(), + sizeof(float) * pass->rectx * pass->recty * pass->channels); RE_engine_end_result(bl_engine_, result, false, false, false); } diff --git a/source/blender/render/hydra/scene_delegate/image.cc b/source/blender/render/hydra/scene_delegate/image.cc index 340fb34cf424..3bbf39bd9b4b 100644 --- a/source/blender/render/hydra/scene_delegate/image.cc +++ b/source/blender/render/hydra/scene_delegate/image.cc @@ -60,8 +60,6 @@ static std::string cache_image_file(Image *image, } } BKE_image_save_options_free(&opts); - - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s -> %s", image->id.name, file_path.c_str()); return file_path; } @@ -94,7 +92,7 @@ std::string cache_image_color(float color[4]) char name[128]; snprintf(name, sizeof(name), - "color_%02x-%02x-%02x.hdr", + "color_%02x%02x%02x.hdr", int(color[0] * 255), int(color[1] * 255), int(color[2] * 255)); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 1f3a6d91de4d..5c9486247d9a 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -270,9 +270,7 @@ void InstancerData::write_instances() for (auto &l_inst : light_instances_.values()) { update_light_instance(l_inst); } - light_instances_.remove_if([&](auto item) { - return item.value.transforms.empty(); - }); + light_instances_.remove_if([&](auto item) { return item.value.transforms.empty(); }); } void InstancerData::update_light_instance(LightInstance &inst) -- 2.30.2 From a87f3ce062aaa70271083872f9b0069f97d353aa Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 30 Jun 2023 10:50:00 +0300 Subject: [PATCH 07/13] Fix after merging --- .../scene_delegate/blender_scene_delegate.cc | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) 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 1bf6f2a667fb..f7a5fef1e5a3 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -373,12 +373,7 @@ void BlenderSceneDelegate::check_updates() } if (set_light_shading_settings()) { - if (shading_settings.use_scene_lights) { - add_new_objects(); - } - else { - do_update_collection = true; - } + do_update_collection = true; } DEGIDIterData data = {0}; @@ -411,19 +406,15 @@ void BlenderSceneDelegate::check_updates() } break; case ID_SCE: { - if (id->recalc & ID_RECALC_COPY_ON_WRITE && !(id->recalc & ID_RECALC_SELECT)) { + if ((id->recalc & ID_RECALC_COPY_ON_WRITE && !(id->recalc & ID_RECALC_SELECT)) || + id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_BASE_FLAGS)) + { do_update_collection = true; } - if (id->recalc & ID_RECALC_BASE_FLAGS) { - do_update_collection = true; - } - if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { - do_update_collection = true; - } - if (id->recalc & ID_RECALC_AUDIO_VOLUME) { - if ((scene->world && !world_data_) || (!scene->world && world_data_)) { - do_update_world = true; - } + if (id->recalc & ID_RECALC_AUDIO_VOLUME && + ((scene->world && !world_data_) || (!scene->world && world_data_))) + { + do_update_world = true; } } break; -- 2.30.2 From a5aedb3e22bd3dcf2fc3e0f67342f095b87fed0c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 30 Jun 2023 17:36:19 +0300 Subject: [PATCH 08/13] Fixed instance visibility --- .../render/hydra/scene_delegate/instancer.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index a969e526a547..e46a074e2434 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -202,6 +202,15 @@ void InstancerData::write_instances() l_inst.transforms.clear(); } + auto is_instance_visible = [&](Object *parent_ob) { + eEvaluationMode deg_mode = DEG_get_mode(scene_delegate_->depsgraph); + bool ret = BKE_object_visibility(parent_ob, deg_mode) & OB_VISIBLE_INSTANCES; + if (deg_mode == DAG_EVAL_VIEWPORT) { + ret &= BKE_object_is_visible_in_viewport(scene_delegate_->view3d, parent_ob); + } + return ret; + }; + DEGObjectIterSettings s = {0}; s.depsgraph = scene_delegate_->depsgraph; s.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | @@ -217,17 +226,17 @@ void InstancerData::write_instances() Object *, object) { - if (d.dupli_object_current == nullptr) { + DupliObject *dupli = d.dupli_object_current; + if (dupli == nullptr) { + continue; + } + if (!is_instance_visible(d.dupli_parent)) { continue; } - DupliObject *dupli = d.dupli_object_current; Object *ob = dupli->ob; if (!is_instance_supported(ob)) { continue; } - if (!ObjectData::is_visible(scene_delegate_, d.dupli_parent)) { - continue; - } if (!scene_delegate_->shading_settings.use_scene_lights && ob->type == OB_LAMP) { continue; } -- 2.30.2 From 8a40fc54ea92e77c845079ae6f1e0dfc3df588f4 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 30 Jun 2023 21:46:39 +0300 Subject: [PATCH 09/13] Improved working with instances visibility --- .../hydra/scene_delegate/blender_scene_delegate.cc | 1 - source/blender/render/hydra/scene_delegate/curves.cc | 1 - .../blender/render/hydra/scene_delegate/instancer.cc | 12 +----------- source/blender/render/hydra/scene_delegate/mesh.cc | 1 - source/blender/render/hydra/scene_delegate/object.cc | 5 ++--- source/blender/render/hydra/scene_delegate/object.h | 5 ++++- 6 files changed, 7 insertions(+), 18 deletions(-) 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 f7a5fef1e5a3..9fb24bacf634 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -3,7 +3,6 @@ #include -#include "BKE_object.h" #include "BLI_set.hh" #include "DEG_depsgraph_query.h" #include "DNA_scene_types.h" diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 3de712c6cf0e..da2411652ebc 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -6,7 +6,6 @@ #include "BKE_customdata.h" #include "BKE_material.h" -#include "BKE_object.h" #include "blender_scene_delegate.h" diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index e46a074e2434..a4dafd0b43dd 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -4,7 +4,6 @@ #include #include -#include "BKE_object.h" #include "DEG_depsgraph_query.h" #include "blender_scene_delegate.h" @@ -202,15 +201,6 @@ void InstancerData::write_instances() l_inst.transforms.clear(); } - auto is_instance_visible = [&](Object *parent_ob) { - eEvaluationMode deg_mode = DEG_get_mode(scene_delegate_->depsgraph); - bool ret = BKE_object_visibility(parent_ob, deg_mode) & OB_VISIBLE_INSTANCES; - if (deg_mode == DAG_EVAL_VIEWPORT) { - ret &= BKE_object_is_visible_in_viewport(scene_delegate_->view3d, parent_ob); - } - return ret; - }; - DEGObjectIterSettings s = {0}; s.depsgraph = scene_delegate_->depsgraph; s.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | @@ -230,7 +220,7 @@ void InstancerData::write_instances() if (dupli == nullptr) { continue; } - if (!is_instance_visible(d.dupli_parent)) { + if (!ObjectData::is_visible(scene_delegate_, d.dupli_parent, OB_VISIBLE_INSTANCES)) { continue; } Object *ob = dupli->ob; diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 0f766a6e5a7b..312e343028a6 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -8,7 +8,6 @@ #include "BKE_material.h" #include "BKE_mesh.hh" #include "BKE_mesh_runtime.h" -#include "BKE_object.h" #include "blender_scene_delegate.h" #include "mesh.h" diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 9549eebb8b38..bff88c92ffaf 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ -#include "BKE_object.h" #include "DEG_depsgraph_query.h" #include "blender_scene_delegate.h" @@ -64,10 +63,10 @@ bool ObjectData::is_supported(Object *object) return false; } -bool ObjectData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object) +bool ObjectData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object, int mode) { eEvaluationMode deg_mode = DEG_get_mode(scene_delegate->depsgraph); - bool ret = BKE_object_visibility(object, deg_mode) & OB_VISIBLE_SELF; + bool ret = BKE_object_visibility(object, deg_mode) & mode; if (deg_mode == DAG_EVAL_VIEWPORT) { ret &= BKE_object_is_visible_in_viewport(scene_delegate->view3d, object); } diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index db3703f02df8..1a31a0cbf245 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -7,6 +7,7 @@ #include #include "BKE_layer.h" +#include "BKE_object.h" #include "BLI_map.hh" #include "DNA_object_types.h" @@ -23,7 +24,9 @@ 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); + static bool is_visible(BlenderSceneDelegate *scene_delegate, + Object *object, + int mode = OB_VISIBLE_SELF); virtual bool update_visibility(); -- 2.30.2 From 05baf7b64638deb08efaf77b91a5a3b47ca2f159 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sat, 1 Jul 2023 03:15:06 +0300 Subject: [PATCH 10/13] Instancing creation optimization --- .../scene_delegate/blender_scene_delegate.cc | 20 ++-- .../render/hydra/scene_delegate/instancer.cc | 113 +++++++++++++++++- .../render/hydra/scene_delegate/instancer.h | 4 + 3 files changed, 126 insertions(+), 11 deletions(-) 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 9fb24bacf634..916436bf47d2 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -31,6 +31,7 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, Engine *engine) : HdSceneDelegate(parent_index, delegate_id), engine(engine) { + instancer_data_ = std::make_unique(this, instancer_prim_id()); } pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id) @@ -433,18 +434,19 @@ void BlenderSceneDelegate::check_updates() void BlenderSceneDelegate::update_collection() { - /* Add or update available objects */ Set available_objects; DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | - DEG_ITER_OBJECT_FLAG_VISIBLE; + DEG_ITER_OBJECT_FLAG_VISIBLE | DEG_ITER_OBJECT_FLAG_DUPLI; DEGObjectIterData data = {0}; data.settings = &settings; data.graph = settings.depsgraph; data.flag = settings.flags; + instancer_data_->pre_update(); + ITER_BEGIN (DEG_iterator_objects_begin, DEG_iterator_objects_next, DEG_iterator_objects_end, @@ -452,6 +454,11 @@ void BlenderSceneDelegate::update_collection() Object *, object) { + if (data.dupli_object_current) { + instancer_data_->update_instance(data.dupli_parent, data.dupli_object_current); + continue; + } + if (!ObjectData::is_supported(object)) { continue; } @@ -477,14 +484,7 @@ void BlenderSceneDelegate::update_collection() } ITER_END; - if (!instancer_data_) { - instancer_data_ = std::make_unique(this, instancer_prim_id()); - instancer_data_->init(); - instancer_data_->insert(); - } - else { - instancer_data_->update(); - } + instancer_data_->post_update(); /* Remove unused objects */ objects_.remove_if([&](auto item) { diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index a4dafd0b43dd..7a145340d210 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -51,12 +51,16 @@ void InstancerData::remove() for (auto &m_inst : mesh_instances_.values()) { m_inst.data->remove(); } - scene_delegate_->GetRenderIndex().RemoveInstancer(prim_id); + if (!mesh_instances_.is_empty()) { + scene_delegate_->GetRenderIndex().RemoveInstancer(prim_id); + } + mesh_instances_.clear(); for (auto &l_inst : light_instances_.values()) { l_inst.transforms.clear(); update_light_instance(l_inst); } + light_instances_.clear(); } void InstancerData::update() @@ -104,6 +108,94 @@ bool InstancerData::update_visibility() return ret; } +void InstancerData::pre_update() +{ + mesh_transforms_.clear(); + for (auto &m_inst : mesh_instances_.values()) { + m_inst.indices.clear(); + } + for (auto &l_inst : light_instances_.values()) { + l_inst.transforms.clear(); + } +} + +void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli) +{ + if (!ObjectData::is_visible(scene_delegate_, parent_ob, OB_VISIBLE_INSTANCES)) { + return; + } + Object *ob = dupli->ob; + if (!is_instance_supported(ob)) { + return; + } + if (!scene_delegate_->shading_settings.use_scene_lights && ob->type == OB_LAMP) { + return; + } + + pxr::SdfPath p_id = object_prim_id(ob); + if (ob->type == OB_LAMP) { + LightInstance *inst = light_instance(p_id); + if (!inst) { + inst = &light_instances_.lookup_or_add_default(p_id); + inst->data = std::make_unique(scene_delegate_, ob, p_id); + inst->data->init(); + } + ID_LOG(2, "Light %s %d", inst->data->id->name, inst->transforms.size()); + inst->transforms.push_back(gf_matrix_from_transform(dupli->mat)); + } + else { + MeshInstance *inst = mesh_instance(p_id); + if (!inst) { + inst = &mesh_instances_.lookup_or_add_default(p_id); + inst->data = std::make_unique(scene_delegate_, ob, p_id); + inst->data->init(); + inst->data->insert(); + } + else { + inst->data->update(); + } + ID_LOG(2, "Mesh %s %d", inst->data->id->name, mesh_transforms_.size()); + inst->indices.push_back(mesh_transforms_.size()); + mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); + } +} + +void InstancerData::post_update() +{ + /* Remove mesh intances without indices */ + mesh_instances_.remove_if([&](auto item) { + bool res = item.value.indices.empty(); + if (res) { + item.value.data->remove(); + } + return res; + }); + + pxr::HdRenderIndex &index = scene_delegate_->GetRenderIndex(); + if (mesh_instances_.is_empty()) { + if (index.HasInstancer(prim_id)) { + index.RemoveInstancer(prim_id); + ID_LOG(1, "Remove instancer"); + } + } + else { + if (index.HasInstancer(prim_id)) { + index.GetChangeTracker().MarkInstancerDirty(prim_id, pxr::HdChangeTracker::AllDirty); + ID_LOG(1, "Update instancer"); + } + else { + index.InsertInstancer(scene_delegate_, prim_id); + ID_LOG(1, "Insert instancer"); + } + } + + /* Update light intances and remove instances without transforms */ + for (auto &l_inst : light_instances_.values()) { + update_light_instance(l_inst); + } + light_instances_.remove_if([&](auto item) { return item.value.transforms.empty(); }); +} + pxr::GfMatrix4d InstancerData::get_transform(pxr::SdfPath const &id) const { LightInstance *l_inst = light_instance(id); @@ -260,6 +352,7 @@ void InstancerData::write_instances() } ITER_END; + /* Remove mesh intances without indices */ mesh_instances_.remove_if([&](auto item) { bool res = item.value.indices.empty(); @@ -269,6 +362,24 @@ void InstancerData::write_instances() return res; }); + pxr::HdRenderIndex &index = scene_delegate_->GetRenderIndex(); + if (mesh_instances_.is_empty()) { + if (index.HasInstancer(prim_id)) { + index.RemoveInstancer(prim_id); + ID_LOG(1, "Remove instancer"); + } + } + else { + if (index.HasInstancer(prim_id)) { + index.GetChangeTracker().MarkInstancerDirty(prim_id, pxr::HdChangeTracker::AllDirty); + ID_LOG(1, "Update instancer"); + } + else { + index.InsertInstancer(scene_delegate_, prim_id); + ID_LOG(1, "Insert instancer"); + } + } + /* Update light intances and remove instances without transforms */ for (auto &l_inst : light_instances_.values()) { update_light_instance(l_inst); diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 62b7490a539a..385843d5de4f 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -36,6 +36,10 @@ class InstancerData : public ObjectData { pxr::VtValue get_data(pxr::TfToken const &key) const override; bool update_visibility() override; + void pre_update(); + void update_instance(Object *parent_ob, DupliObject *dupli); + void post_update(); + pxr::GfMatrix4d get_transform(pxr::SdfPath const &id) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::VtIntArray indices(pxr::SdfPath const &id) const; -- 2.30.2 From 6aa6bb087096928deacb43cf6ab4a0589c8b342a Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sat, 1 Jul 2023 03:28:21 +0300 Subject: [PATCH 11/13] Removed unused code --- .../scene_delegate/blender_scene_delegate.cc | 4 +- .../render/hydra/scene_delegate/instancer.cc | 277 +++++------------- .../render/hydra/scene_delegate/instancer.h | 13 +- 3 files changed, 73 insertions(+), 221 deletions(-) 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 916436bf47d2..6226bd29f076 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -156,7 +156,7 @@ bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id) } InstancerData *i_data = instancer_data(id, true); if (i_data) { - return i_data->visible; + return true; } return object_data(id)->visible; } @@ -202,7 +202,7 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const & { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", instancer_id.GetText()); InstancerData *i_data = instancer_data(instancer_id); - return i_data->transform; + return i_data->get_transform(instancer_id); } void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 7a145340d210..4fdef5b147b9 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -12,7 +12,7 @@ namespace blender::render::hydra { InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) - : ObjectData(scene_delegate, nullptr, prim_id) + : IdData(scene_delegate, nullptr, prim_id) { } @@ -33,17 +33,9 @@ bool InstancerData::is_instance_supported(Object *object) return false; } -void InstancerData::init() -{ - ID_LOG(1, ""); - write_instances(); -} +void InstancerData::init() {} -void InstancerData::insert() -{ - ID_LOG(1, ""); - scene_delegate_->GetRenderIndex().InsertInstancer(scene_delegate_, prim_id); -} +void InstancerData::insert() {} void InstancerData::remove() { @@ -63,13 +55,7 @@ void InstancerData::remove() light_instances_.clear(); } -void InstancerData::update() -{ - ID_LOG(1, ""); - write_instances(); - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( - prim_id, pxr::HdChangeTracker::AllDirty); -} +void InstancerData::update() {} pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const { @@ -80,122 +66,6 @@ pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const return pxr::VtValue(); } -bool InstancerData::update_visibility() -{ - bool prev_visible = visible; - visible = is_visible(scene_delegate_, (Object *)id); - bool ret = visible != prev_visible; - - if (ret) { - ID_LOG(1, ""); - auto &change_tracker = scene_delegate_->GetRenderIndex().GetChangeTracker(); - change_tracker.MarkInstancerDirty(prim_id, pxr::HdChangeTracker::DirtyVisibility); - for (auto &m_inst : mesh_instances_.values()) { - m_inst.data->visible = visible; - for (auto &p : m_inst.data->submesh_paths()) { - change_tracker.MarkRprimDirty(p, pxr::HdChangeTracker::DirtyVisibility); - } - } - char name[16]; - for (auto &l_inst : light_instances_.values()) { - for (int i = 0; i < l_inst.count; ++i) { - snprintf(name, sizeof(name), "L_%08x", i); - change_tracker.MarkRprimDirty(l_inst.data->prim_id.AppendElementString(name), - pxr::HdChangeTracker::DirtyVisibility); - } - } - } - return ret; -} - -void InstancerData::pre_update() -{ - mesh_transforms_.clear(); - for (auto &m_inst : mesh_instances_.values()) { - m_inst.indices.clear(); - } - for (auto &l_inst : light_instances_.values()) { - l_inst.transforms.clear(); - } -} - -void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli) -{ - if (!ObjectData::is_visible(scene_delegate_, parent_ob, OB_VISIBLE_INSTANCES)) { - return; - } - Object *ob = dupli->ob; - if (!is_instance_supported(ob)) { - return; - } - if (!scene_delegate_->shading_settings.use_scene_lights && ob->type == OB_LAMP) { - return; - } - - pxr::SdfPath p_id = object_prim_id(ob); - if (ob->type == OB_LAMP) { - LightInstance *inst = light_instance(p_id); - if (!inst) { - inst = &light_instances_.lookup_or_add_default(p_id); - inst->data = std::make_unique(scene_delegate_, ob, p_id); - inst->data->init(); - } - ID_LOG(2, "Light %s %d", inst->data->id->name, inst->transforms.size()); - inst->transforms.push_back(gf_matrix_from_transform(dupli->mat)); - } - else { - MeshInstance *inst = mesh_instance(p_id); - if (!inst) { - inst = &mesh_instances_.lookup_or_add_default(p_id); - inst->data = std::make_unique(scene_delegate_, ob, p_id); - inst->data->init(); - inst->data->insert(); - } - else { - inst->data->update(); - } - ID_LOG(2, "Mesh %s %d", inst->data->id->name, mesh_transforms_.size()); - inst->indices.push_back(mesh_transforms_.size()); - mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); - } -} - -void InstancerData::post_update() -{ - /* Remove mesh intances without indices */ - mesh_instances_.remove_if([&](auto item) { - bool res = item.value.indices.empty(); - if (res) { - item.value.data->remove(); - } - return res; - }); - - pxr::HdRenderIndex &index = scene_delegate_->GetRenderIndex(); - if (mesh_instances_.is_empty()) { - if (index.HasInstancer(prim_id)) { - index.RemoveInstancer(prim_id); - ID_LOG(1, "Remove instancer"); - } - } - else { - if (index.HasInstancer(prim_id)) { - index.GetChangeTracker().MarkInstancerDirty(prim_id, pxr::HdChangeTracker::AllDirty); - ID_LOG(1, "Update instancer"); - } - else { - index.InsertInstancer(scene_delegate_, prim_id); - ID_LOG(1, "Insert instancer"); - } - } - - /* Update light intances and remove instances without transforms */ - for (auto &l_inst : light_instances_.values()) { - update_light_instance(l_inst); - } - light_instances_.remove_if([&](auto item) { return item.value.transforms.empty(); }); -} - pxr::GfMatrix4d InstancerData::get_transform(pxr::SdfPath const &id) const { LightInstance *l_inst = light_instance(id); @@ -261,29 +131,7 @@ void InstancerData::update_double_sided(MaterialData *mat_data) } } -pxr::SdfPath InstancerData::object_prim_id(Object *object) const -{ - /* Making id of object in form like _ */ - char name[32]; - snprintf(name, sizeof(name), "O_%016llx", (uint64_t)object); - return prim_id.AppendElementString(name); -} - -pxr::SdfPath InstancerData::light_prim_id(LightInstance const &inst, int index) const -{ - char name[16]; - snprintf(name, sizeof(name), "L_%08x", index); - return inst.data->prim_id.AppendElementString(name); -} - -int InstancerData::light_prim_id_index(pxr::SdfPath const &id) const -{ - int index; - sscanf(id.GetName().c_str(), "L_%x", &index); - return index; -} - -void InstancerData::write_instances() +void InstancerData::pre_update() { mesh_transforms_.clear(); for (auto &m_inst : mesh_instances_.values()) { @@ -292,67 +140,51 @@ void InstancerData::write_instances() for (auto &l_inst : light_instances_.values()) { l_inst.transforms.clear(); } +} - DEGObjectIterSettings s = {0}; - s.depsgraph = scene_delegate_->depsgraph; - s.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | - DEG_ITER_OBJECT_FLAG_VISIBLE | DEG_ITER_OBJECT_FLAG_DUPLI; - DEGObjectIterData d = {0}; - d.settings = &s; - d.graph = s.depsgraph; - d.flag = s.flags; - ITER_BEGIN (DEG_iterator_objects_begin, - DEG_iterator_objects_next, - DEG_iterator_objects_end, - &d, - Object *, - object) - { - DupliObject *dupli = d.dupli_object_current; - if (dupli == nullptr) { - continue; - } - if (!ObjectData::is_visible(scene_delegate_, d.dupli_parent, OB_VISIBLE_INSTANCES)) { - continue; - } - Object *ob = dupli->ob; - if (!is_instance_supported(ob)) { - continue; - } - if (!scene_delegate_->shading_settings.use_scene_lights && ob->type == OB_LAMP) { - continue; - } +void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli) +{ + if (!ObjectData::is_visible(scene_delegate_, parent_ob, OB_VISIBLE_INSTANCES)) { + return; + } + Object *ob = dupli->ob; + if (!is_instance_supported(ob)) { + return; + } + if (!scene_delegate_->shading_settings.use_scene_lights && ob->type == OB_LAMP) { + return; + } - pxr::SdfPath p_id = object_prim_id(ob); - if (ob->type == OB_LAMP) { - LightInstance *inst = light_instance(p_id); - if (!inst) { - inst = &light_instances_.lookup_or_add_default(p_id); - inst->data = std::make_unique(scene_delegate_, ob, p_id); - inst->data->init(); - } - ID_LOG(2, "Light %s %d", inst->data->id->name, inst->transforms.size()); - inst->transforms.push_back(gf_matrix_from_transform(dupli->mat)); + pxr::SdfPath p_id = object_prim_id(ob); + if (ob->type == OB_LAMP) { + LightInstance *inst = light_instance(p_id); + if (!inst) { + inst = &light_instances_.lookup_or_add_default(p_id); + inst->data = std::make_unique(scene_delegate_, ob, p_id); + inst->data->init(); + } + ID_LOG(2, "Light %s %d", inst->data->id->name, inst->transforms.size()); + inst->transforms.push_back(gf_matrix_from_transform(dupli->mat)); + } + else { + MeshInstance *inst = mesh_instance(p_id); + if (!inst) { + inst = &mesh_instances_.lookup_or_add_default(p_id); + inst->data = std::make_unique(scene_delegate_, ob, p_id); + inst->data->init(); + inst->data->insert(); } else { - MeshInstance *inst = mesh_instance(p_id); - if (!inst) { - inst = &mesh_instances_.lookup_or_add_default(p_id); - inst->data = std::make_unique(scene_delegate_, ob, p_id); - inst->data->init(); - inst->data->insert(); - } - else { - inst->data->update(); - } - ID_LOG(2, "Mesh %s %d", inst->data->id->name, mesh_transforms_.size()); - inst->indices.push_back(mesh_transforms_.size()); - mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); + inst->data->update(); } + ID_LOG(2, "Mesh %s %d", inst->data->id->name, mesh_transforms_.size()); + inst->indices.push_back(mesh_transforms_.size()); + mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); } - ITER_END; - +} +void InstancerData::post_update() +{ /* Remove mesh intances without indices */ mesh_instances_.remove_if([&](auto item) { bool res = item.value.indices.empty(); @@ -362,6 +194,7 @@ void InstancerData::write_instances() return res; }); + /* Insert/remove/update instancer in RenderIndex */ pxr::HdRenderIndex &index = scene_delegate_->GetRenderIndex(); if (mesh_instances_.is_empty()) { if (index.HasInstancer(prim_id)) { @@ -387,6 +220,28 @@ void InstancerData::write_instances() light_instances_.remove_if([&](auto item) { return item.value.transforms.empty(); }); } +pxr::SdfPath InstancerData::object_prim_id(Object *object) const +{ + /* Making id of object in form like _ */ + char name[32]; + snprintf(name, sizeof(name), "O_%016llx", (uint64_t)object); + return prim_id.AppendElementString(name); +} + +pxr::SdfPath InstancerData::light_prim_id(LightInstance const &inst, int index) const +{ + char name[16]; + snprintf(name, sizeof(name), "L_%08x", index); + return inst.data->prim_id.AppendElementString(name); +} + +int InstancerData::light_prim_id_index(pxr::SdfPath const &id) const +{ + int index; + sscanf(id.GetName().c_str(), "L_%x", &index); + return index; +} + void InstancerData::update_light_instance(LightInstance &inst) { auto &render_index = scene_delegate_->GetRenderIndex(); diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 385843d5de4f..a061e350daf1 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -12,7 +12,7 @@ namespace blender::render::hydra { -class InstancerData : public ObjectData { +class InstancerData : public IdData { struct MeshInstance { std::unique_ptr data; pxr::VtIntArray indices; @@ -34,12 +34,6 @@ class InstancerData : public ObjectData { void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; - bool update_visibility() override; - - void pre_update(); - void update_instance(Object *parent_ob, DupliObject *dupli); - void post_update(); - pxr::GfMatrix4d get_transform(pxr::SdfPath const &id) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::VtIntArray indices(pxr::SdfPath const &id) const; @@ -48,11 +42,14 @@ class InstancerData : public ObjectData { void available_materials(Set &paths) const; void update_double_sided(MaterialData *mat_data); + void pre_update(); + void update_instance(Object *parent_ob, DupliObject *dupli); + void post_update(); + private: 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; - void write_instances(); void update_light_instance(LightInstance &inst); MeshInstance *mesh_instance(pxr::SdfPath const &id) const; LightInstance *light_instance(pxr::SdfPath const &id) const; -- 2.30.2 From 961a406367f71609a4a3a1d88c5df6101a366845 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 3 Jul 2023 20:46:13 +0300 Subject: [PATCH 12/13] Fixed issue with light instances. Fixed code review and added comments. --- .../scene_delegate/blender_scene_delegate.cc | 3 +-- .../blender/render/hydra/scene_delegate/image.cc | 2 +- .../render/hydra/scene_delegate/instancer.cc | 15 ++++++++------- .../render/hydra/scene_delegate/instancer.h | 6 ++++++ .../blender/render/hydra/scene_delegate/world.cc | 7 ++----- 5 files changed, 18 insertions(+), 15 deletions(-) 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 6226bd29f076..4678a0fc3b64 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -438,8 +438,7 @@ void BlenderSceneDelegate::update_collection() DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; - settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | - DEG_ITER_OBJECT_FLAG_VISIBLE | DEG_ITER_OBJECT_FLAG_DUPLI; + settings.flags = DEG_OBJECT_ITER_FOR_RENDER_ENGINE_FLAGS; DEGObjectIterData data = {0}; data.settings = &settings; data.graph = settings.depsgraph; diff --git a/source/blender/render/hydra/scene_delegate/image.cc b/source/blender/render/hydra/scene_delegate/image.cc index 6b1c067cb1f6..7f8f4a28c3d3 100644 --- a/source/blender/render/hydra/scene_delegate/image.cc +++ b/source/blender/render/hydra/scene_delegate/image.cc @@ -45,7 +45,7 @@ static std::string cache_image_file(Image *image, BKE_image_path_ext_from_imformat(&scene->r.im_format, &r_ext); opts.im_format = scene->r.im_format; } - + snprintf(file_name, sizeof(file_name), "img_%016llx%s", (uintptr_t)image, r_ext); file_path = get_cache_file(file_name); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 4fdef5b147b9..7dd8e9401dbc 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -194,10 +194,17 @@ void InstancerData::post_update() return res; }); + /* Update light intances and remove instances without transforms */ + for (auto &l_inst : light_instances_.values()) { + update_light_instance(l_inst); + } + light_instances_.remove_if([&](auto item) { return item.value.transforms.empty(); }); + /* Insert/remove/update instancer in RenderIndex */ pxr::HdRenderIndex &index = scene_delegate_->GetRenderIndex(); if (mesh_instances_.is_empty()) { - if (index.HasInstancer(prim_id)) { + /* Important: removing instancer when light_instances_ are empty too */ + if (index.HasInstancer(prim_id) && light_instances_.is_empty()) { index.RemoveInstancer(prim_id); ID_LOG(1, "Remove instancer"); } @@ -212,12 +219,6 @@ void InstancerData::post_update() ID_LOG(1, "Insert instancer"); } } - - /* Update light intances and remove instances without transforms */ - for (auto &l_inst : light_instances_.values()) { - update_light_instance(l_inst); - } - light_instances_.remove_if([&](auto item) { return item.value.transforms.empty(); }); } pxr::SdfPath InstancerData::object_prim_id(Object *object) const diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index a061e350daf1..75c42efb2109 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -42,6 +42,12 @@ class InstancerData : public IdData { void available_materials(Set &paths) const; void update_double_sided(MaterialData *mat_data); + /* Following update functions are working together: + pre_update() + update_instance() + update_instance() + ... + post_update() */ void pre_update(); void update_instance(Object *parent_ob, DupliObject *dupli); void post_update(); diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 511756cd3936..c7f7aa746286 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -176,11 +176,8 @@ void WorldData::write_transform() { transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), pxr::GfVec3d()); - transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), - pxr::GfVec3d()); - transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), - pxr::GfVec3d()); - + transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), pxr::GfVec3d()); + transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), pxr::GfVec3d()); } } // namespace blender::render::hydra -- 2.30.2 From 16cbfb1c4181f7af857fceab181a680bd371968c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 4 Jul 2023 12:03:43 +0300 Subject: [PATCH 13/13] Code improvements after merge --- .../hydra/scene_delegate/blender_scene_delegate.cc | 10 +++++----- .../hydra/scene_delegate/blender_scene_delegate.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) 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 a64759e5b5c3..acf81a5916ab 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -323,6 +323,11 @@ CurvesData *BlenderSceneDelegate::curves_data(pxr::SdfPath const &id) const return dynamic_cast(object_data(id)); } +VolumeData *BlenderSceneDelegate::volume_data(pxr::SdfPath const &id) const +{ + return dynamic_cast(object_data(id)); +} + LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) const { return dynamic_cast(object_data(id)); @@ -337,11 +342,6 @@ MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) const return mat_data->get(); } -VolumeData *BlenderSceneDelegate::volume_data(pxr::SdfPath const &id) const -{ - return dynamic_cast(object_data(id)); -} - InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool child_id) const { pxr::SdfPath p_id; diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h index 1470a28021ad..156b220b23ab 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -97,10 +97,10 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { ObjectData *object_data(pxr::SdfPath const &id) const; MeshData *mesh_data(pxr::SdfPath const &id) const; CurvesData *curves_data(pxr::SdfPath const &id) const; + VolumeData *volume_data(pxr::SdfPath const &id) const; LightData *light_data(pxr::SdfPath const &id) const; MaterialData *material_data(pxr::SdfPath const &id) const; InstancerData *instancer_data(pxr::SdfPath const &id, bool child_id = false) const; - VolumeData *volume_data(pxr::SdfPath const &id) const; void update_world(); void check_updates(); -- 2.30.2