From cc456bf8d438edd4e1f688f6f3693d686aec967d Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 17:55:23 +0300 Subject: [PATCH 01/16] Refactored materials --- .../scene_delegate/blender_scene_delegate.cc | 26 +++++++++---------- .../scene_delegate/blender_scene_delegate.h | 8 ++++++ .../render/hydra/scene_delegate/material.h | 4 +-- .../render/hydra/scene_delegate/mesh.cc | 4 +-- 4 files changed, 24 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 dedab58950f0..5af21a30453d 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -217,13 +217,11 @@ void BlenderSceneDelegate::clear() for (auto &it : instancers_) { it.second->remove(); } - for (auto &it : materials_) { - it.second->remove(); - } + objects_.clear(); instancers_.clear(); - materials_.clear(); + materials_.clear_and_shrink(); depsgraph = nullptr; context = nullptr; @@ -297,11 +295,11 @@ LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) const MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) const { - auto it = materials_.find(id); - if (it == materials_.end()) { + const std::unique_ptr *value = materials_.lookup_ptr(id); + if (value == nullptr) { return nullptr; } - return it->second.get(); + return value->get(); } InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool child_id) const @@ -585,14 +583,14 @@ void BlenderSceneDelegate::remove_unused_objects() for (auto &it : instancers_) { it.second->available_materials(available_materials); } - for (auto it = materials_.begin(); it != materials_.end(); ++it) { - if (available_materials.find(it->first) != available_materials.end()) { - continue; - } - it->second->remove(); - materials_.erase(it); - it = materials_.begin(); + + materials_.remove_if([&](auto item) { + bool ret = available_materials.find(item.key) == available_materials.end(); + if (ret){ + item.value->remove(); } + return ret; + }); } void BlenderSceneDelegate::update_visibility() 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 cc36e4b7f05e..53d8b10bb785 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -8,6 +8,7 @@ #include "BKE_context.h" #include "DEG_depsgraph.h" +#include "BLI_hash.hh" #include "CLG_log.h" @@ -18,6 +19,13 @@ #include "object.h" #include "world.h" +template<> struct blender::DefaultHash { + uint64_t operator()(const pxr::SdfPath &value) const + { + return pxr::SdfPath::Hash()(value); + } +}; + namespace blender::render::hydra { extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE; diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 94df4d17b582..5488b3b3ee2e 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -8,6 +8,7 @@ #include #include "DNA_material_types.h" +#include "BLI_map.hh" #include "id.h" @@ -37,7 +38,6 @@ class MaterialData : public IdData { pxr::VtValue material_network_map_; }; -using MaterialDataMap = - pxr::TfHashMap, pxr::SdfPath::Hash>; +using MaterialDataMap = Map>; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 8c8ef8bb86f5..9a82da08f249 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -304,8 +304,8 @@ void MeshData::write_materials() pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); m.mat_data = scene_delegate_->material_data(p_id); if (!m.mat_data) { - scene_delegate_->materials_[p_id] = std::make_unique( - scene_delegate_, mat, p_id); + scene_delegate_->materials_.add_overwrite(p_id, + std::make_unique(scene_delegate_, mat, p_id)); m.mat_data = scene_delegate_->material_data(p_id); m.mat_data->init(); m.mat_data->insert(); -- 2.30.2 From 757555fe17b565e6ec1936085b216b7990944bcf Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 18:11:07 +0300 Subject: [PATCH 02/16] Fixed materials for curves. --- source/blender/render/hydra/scene_delegate/curves.cc | 2 +- source/blender/render/hydra/scene_delegate/material.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 983a58c133e1..5d988526f03e 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -199,7 +199,7 @@ void CurvesData::write_material() pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); mat_data_ = scene_delegate_->material_data(p_id); if (!mat_data_) { - scene_delegate_->materials_[p_id] = std::make_unique(scene_delegate_, mat, p_id); + scene_delegate_->materials_.add_overwrite(p_id, std::make_unique(scene_delegate_, mat, p_id)); mat_data_ = scene_delegate_->material_data(p_id); mat_data_->init(); mat_data_->insert(); diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index cd832a4f1816..80a79e01d19d 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -60,8 +60,8 @@ void MaterialData::update() scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, pxr::HdMaterial::AllDirty); if (prev_double_sided != double_sided) { - for (auto &it : scene_delegate_->objects_) { - MeshData *m_data = dynamic_cast(it.second.get()); + for (auto &it : scene_delegate_->objects_.values()) { + MeshData *m_data = dynamic_cast(it.get()); if (m_data) { m_data->update_double_sided(this); } -- 2.30.2 From 5061ccded6cec119290b394e8ac947dcba3346c8 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 18:11:28 +0300 Subject: [PATCH 03/16] Refactored objects --- .../scene_delegate/blender_scene_delegate.cc | 36 +++++++++---------- .../render/hydra/scene_delegate/object.h | 3 +- 2 files changed, 20 insertions(+), 19 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 5af21a30453d..f471a4c13311 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -211,8 +211,8 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) void BlenderSceneDelegate::clear() { - for (auto &it : objects_) { - it.second->remove(); + for (auto &it : objects_.values()) { + it->remove(); } for (auto &it : instancers_) { it.second->remove(); @@ -267,9 +267,9 @@ pxr::SdfPath BlenderSceneDelegate::world_prim_id() const ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) const { pxr::SdfPath p_id = (id.GetName().find("SM_") == 0) ? id.GetParentPath() : id; - auto it = objects_.find(p_id); - if (it != objects_.end()) { - return it->second.get(); + const std::unique_ptr *value = objects_.lookup_ptr(p_id); + if (value != nullptr) { + return value->get(); } InstancerData *i_data = instancer_data(p_id, true); if (i_data) { @@ -347,7 +347,7 @@ void BlenderSceneDelegate::update_objects(Object *object) return; } - objects_[id] = ObjectData::create(this, object, id); + objects_.add_overwrite(id, ObjectData::create(this, object, id)); obj_data = object_data(id); obj_data->update_parent(); obj_data->init(); @@ -559,23 +559,23 @@ void BlenderSceneDelegate::remove_unused_objects() } /* Remove unused objects */ - for (auto it = objects_.begin(); it != objects_.end(); ++it) { - if (available_objects.find(it->first.GetName()) != available_objects.end()) { - continue; - } - it->second->remove(); - objects_.erase(it); - it = objects_.begin(); + objects_.remove_if([&](auto item) { + bool ret = available_objects.find(item.key.GetName()) == available_objects.end(); + if (ret){ + item.value->remove(); } + return ret; + }); + /* Remove unused materials */ std::set available_materials; - for (auto &it : objects_) { - MeshData *m_data = dynamic_cast(it.second.get()); + for (auto &it : objects_.values()) { + MeshData *m_data = dynamic_cast(it.get()); if (m_data) { m_data->available_materials(available_materials); } - CurvesData *c_data = dynamic_cast(it.second.get()); + CurvesData *c_data = dynamic_cast(it.get()); if (c_data) { c_data->available_materials(available_materials); } @@ -596,8 +596,8 @@ void BlenderSceneDelegate::remove_unused_objects() void BlenderSceneDelegate::update_visibility() { /* Updating visibility of existing objects/instancers */ - for (auto &it : objects_) { - it.second->update_visibility(); + for (auto &it : objects_.values()) { + it->update_visibility(); } for (auto &it : instancers_) { it.second->update_visibility(); diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 6ed40aa579a3..7639cfc9a652 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -8,6 +8,7 @@ #include "BKE_layer.h" #include "DNA_object_types.h" +#include "BLI_map.hh" #include "id.h" #include "material.h" @@ -37,7 +38,7 @@ class ObjectData : public IdData { }; using ObjectDataMap = - pxr::TfHashMap, pxr::SdfPath::Hash>; + Map>; pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]); -- 2.30.2 From e999ebdaf267bde14ce6ee655c0eacfdcfd19b48 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 18:22:30 +0300 Subject: [PATCH 04/16] Refactored instancers --- .../scene_delegate/blender_scene_delegate.cc | 44 +++++++++---------- .../render/hydra/scene_delegate/instancer.h | 3 +- .../render/hydra/scene_delegate/material.cc | 4 +- 3 files changed, 26 insertions(+), 25 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 f471a4c13311..ce4286d744e9 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -214,8 +214,8 @@ void BlenderSceneDelegate::clear() for (auto &it : objects_.values()) { it->remove(); } - for (auto &it : instancers_) { - it.second->remove(); + for (auto &it : instancers_.values()) { + it->remove(); } @@ -320,9 +320,9 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool p_id = id; } - auto it = instancers_.find(p_id); - if (it != instancers_.end()) { - return it->second.get(); + const std::unique_ptr *value = instancers_.lookup_ptr(p_id); + if (value != nullptr) { + return value->get(); } return nullptr; } @@ -357,8 +357,8 @@ void BlenderSceneDelegate::update_objects(Object *object) void BlenderSceneDelegate::update_instancers(Object *object) { /* Check object inside instancers */ - for (auto &it : instancers_) { - it.second->check_update(object); + for (auto &it : instancers_.values()) { + it->check_update(object); } pxr::SdfPath id = instancer_prim_id(object); @@ -367,7 +367,7 @@ void BlenderSceneDelegate::update_instancers(Object *object) if ((object->transflag & OB_DUPLI) == 0) { /* Object isn't instancer anymore and should be removed */ i_data->remove(); - instancers_.erase(id); + instancers_.remove(id); return; } @@ -384,7 +384,7 @@ void BlenderSceneDelegate::update_instancers(Object *object) return; } - instancers_[id] = std::make_unique(this, object, id); + instancers_.add_overwrite(id, std::make_unique(this, object, id)); i_data = instancer_data(id); i_data->init(); i_data->insert(); @@ -547,16 +547,16 @@ void BlenderSceneDelegate::remove_unused_objects() ITER_END; /* Remove unused instancers */ - for (auto it = instancers_.begin(); it != instancers_.end(); ++it) { - if (available_objects.find(it->first.GetName()) != available_objects.end()) { - /* Remove objects from instancers */ - it->second->check_remove(available_objects); - continue; - } - it->second->remove(); - instancers_.erase(it); - it = instancers_.begin(); + instancers_.remove_if([&](auto item) { + bool ret = available_objects.find(item.key.GetName()) == available_objects.end(); + if (ret){ + item.value->remove(); } + else{ + item.value->check_remove(available_objects); + } + return ret; + }); /* Remove unused objects */ objects_.remove_if([&](auto item) { @@ -580,8 +580,8 @@ void BlenderSceneDelegate::remove_unused_objects() c_data->available_materials(available_materials); } } - for (auto &it : instancers_) { - it.second->available_materials(available_materials); + for (auto &it : instancers_.values()) { + it->available_materials(available_materials); } materials_.remove_if([&](auto item) { @@ -599,8 +599,8 @@ void BlenderSceneDelegate::update_visibility() for (auto &it : objects_.values()) { it->update_visibility(); } - for (auto &it : instancers_) { - it.second->update_visibility(); + for (auto &it : instancers_.values()) { + it->update_visibility(); } /* Add objects/instancers which were invisible before and not added yet */ diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 97ea2ebe7b21..a4a70d663e5a 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -4,6 +4,7 @@ #pragma once #include "BKE_duplilist.h" +#include "BLI_map.hh" #include "light.h" #include "mesh.h" @@ -62,6 +63,6 @@ class InstancerData : public ObjectData { }; using InstancerDataMap = - pxr::TfHashMap, pxr::SdfPath::Hash>; + Map>; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 80a79e01d19d..808b2aacd26c 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -66,8 +66,8 @@ void MaterialData::update() m_data->update_double_sided(this); } } - for (auto &it : scene_delegate_->instancers_) { - it.second->update_double_sided(this); + for (auto &it : scene_delegate_->instancers_.values()) { + it->update_double_sided(this); } } } -- 2.30.2 From f202f145df2cbe7f978809699d2708050ad69246 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 19:08:32 +0300 Subject: [PATCH 05/16] Refactored mesh_instances --- .../scene_delegate/blender_scene_delegate.h | 8 --- .../blender/render/hydra/scene_delegate/id.h | 8 +++ .../render/hydra/scene_delegate/instancer.cc | 71 +++++++++---------- .../render/hydra/scene_delegate/instancer.h | 2 +- 4 files changed, 42 insertions(+), 47 deletions(-) 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 53d8b10bb785..cc36e4b7f05e 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -8,7 +8,6 @@ #include "BKE_context.h" #include "DEG_depsgraph.h" -#include "BLI_hash.hh" #include "CLG_log.h" @@ -19,13 +18,6 @@ #include "object.h" #include "world.h" -template<> struct blender::DefaultHash { - uint64_t operator()(const pxr::SdfPath &value) const - { - return pxr::SdfPath::Hash()(value); - } -}; - namespace blender::render::hydra { extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE; diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index cdb5a6a7f232..2d10ee1e3c58 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -8,6 +8,14 @@ #include #include "DNA_ID.h" +#include "BLI_hash.hh" + +template<> struct blender::DefaultHash { + uint64_t operator()(const pxr::SdfPath &value) const + { + return pxr::SdfPath::Hash()(value); + } +}; 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 6f0177b4299e..c1d1453d5fc8 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -74,8 +74,8 @@ void InstancerData::insert() void InstancerData::remove() { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s", prim_id.GetText()); - for (auto &it : mesh_instances_) { - it.second.data->remove(); + for (auto &it : mesh_instances_.values()) { + it.data->remove(); } scene_delegate_->GetRenderIndex().RemoveInstancer(prim_id); @@ -118,9 +118,9 @@ bool InstancerData::update_visibility() if (ret) { auto &change_tracker = scene_delegate_->GetRenderIndex().GetChangeTracker(); change_tracker.MarkInstancerDirty(prim_id, pxr::HdChangeTracker::DirtyVisibility); - for (auto &it : mesh_instances_) { - it.second.data->visible = visible; - for (auto &p : it.second.data->submesh_paths()) { + for (auto &it : mesh_instances_.values()) { + it.data->visible = visible; + for (auto &p : it.data->submesh_paths()) { change_tracker.MarkRprimDirty(p, pxr::HdChangeTracker::DirtyVisibility); } } @@ -179,8 +179,8 @@ ObjectData *InstancerData::object_data(pxr::SdfPath const &id) const pxr::SdfPathVector InstancerData::prototypes() const { pxr::SdfPathVector paths; - for (auto &it : mesh_instances_) { - for (auto &p : it.second.data->submesh_paths()) { + for (auto &it : mesh_instances_.values()) { + for (auto &p : it.data->submesh_paths()) { paths.push_back(p); } } @@ -194,7 +194,7 @@ void InstancerData::check_update(Object *object) if (m_inst) { if (!is_instance_visible(object)) { m_inst->data->remove(); - mesh_instances_.erase(path); + mesh_instances_.remove(path); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( prim_id, pxr::HdChangeTracker::AllDirty); return; @@ -243,7 +243,7 @@ void InstancerData::check_update(Object *object) if (do_write_instances) { write_instances(); - if (!mesh_instances_.empty()) { + if (!mesh_instances_.is_empty()) { scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( prim_id, pxr::HdChangeTracker::AllDirty); } @@ -254,13 +254,13 @@ void InstancerData::check_update(Object *object) void InstancerData::check_remove(std::set &available_objects) { bool ret = false; - for (auto it = mesh_instances_.begin(); it != mesh_instances_.end(); ++it) { - if (available_objects.find(it->first.GetName()) != available_objects.end()) { + for (auto it : mesh_instances_.items()) { + if (available_objects.find(it.key.GetName()) != available_objects.end()) { continue; } - it->second.data->remove(); - mesh_instances_.erase(it); - it = mesh_instances_.begin(); + it.value.data->remove(); + mesh_instances_.remove(it.key); + auto it = mesh_instances_.items().begin(); ret = true; } if (ret) { @@ -283,8 +283,8 @@ void InstancerData::check_remove(std::set &available_objects) void InstancerData::available_materials(std::set &paths) const { - for (auto &it : mesh_instances_) { - ((MeshData *)it.second.data.get())->available_materials(paths); + for (auto &it : mesh_instances_.values()) { + ((MeshData *)it.data.get())->available_materials(paths); } } @@ -297,8 +297,8 @@ void InstancerData::update_as_parent() void InstancerData::update_double_sided(MaterialData *mat_data) { - for (auto &it : mesh_instances_) { - it.second.data->update_double_sided(mat_data); + for (auto &it : mesh_instances_.values()) { + it.data->update_double_sided(mat_data); } } @@ -343,8 +343,8 @@ int InstancerData::light_prim_id_index(pxr::SdfPath const &id) const void InstancerData::write_instances() { mesh_transforms_.clear(); - for (auto &it : mesh_instances_) { - it.second.indices.clear(); + for (auto &it : mesh_instances_.values()) { + it.indices.clear(); } for (auto &it : light_instances_) { it.second.transforms.clear(); @@ -372,27 +372,22 @@ void InstancerData::write_instances() else { MeshInstance *inst = mesh_instance(p_id); if (!inst) { - inst = &mesh_instances_[p_id]; - inst->data = std::make_unique(scene_delegate_, ob, p_id); - inst->data->init(); - inst->data->insert(); + inst = mesh_instances_.lookup_ptr(p_id); + if (inst) { + inst->data = std::make_unique(scene_delegate_, ob, p_id); + inst->data->init(); + inst->data->insert(); + 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)); + } } - 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)); } } free_object_duplilist(lb); /* Remove mesh intances without indices */ - for (auto it = mesh_instances_.begin(); it != mesh_instances_.end(); ++it) { - if (!it->second.indices.empty()) { - continue; - } - it->second.data->remove(); - mesh_instances_.erase(it); - it = mesh_instances_.begin(); - } + mesh_instances_.remove_if([&](auto item) {return item.value.indices.empty();}); /* Update light intances and remove instances without transforms */ for (auto it = light_instances_.begin(); it != light_instances_.end(); ++it) { @@ -461,11 +456,11 @@ void InstancerData::update_light_instance(LightInstance &inst) InstancerData::MeshInstance *InstancerData::mesh_instance(pxr::SdfPath const &id) const { - auto it = mesh_instances_.find(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); - if (it == mesh_instances_.end()) { + auto it = mesh_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); + if (it == nullptr) { return nullptr; } - return const_cast(&it->second); + return const_cast(it); } InstancerData::LightInstance *InstancerData::light_instance(pxr::SdfPath const &id) const diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index a4a70d663e5a..a272e93ba475 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -57,7 +57,7 @@ class InstancerData : public ObjectData { MeshInstance *mesh_instance(pxr::SdfPath const &id) const; LightInstance *light_instance(pxr::SdfPath const &id) const; - pxr::TfHashMap mesh_instances_; + Map mesh_instances_; pxr::TfHashMap light_instances_; pxr::VtMatrix4dArray mesh_transforms_; }; -- 2.30.2 From 9c06b97fa2df3308682c08dfced8cfbf05d81e73 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 21:54:15 +0300 Subject: [PATCH 06/16] Refactored light_instances and mesh_instances --- .../render/hydra/scene_delegate/instancer.cc | 72 +++++++++---------- .../render/hydra/scene_delegate/instancer.h | 2 +- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index c1d1453d5fc8..b7df8e87d246 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -79,9 +79,9 @@ void InstancerData::remove() } scene_delegate_->GetRenderIndex().RemoveInstancer(prim_id); - for (auto &it : light_instances_) { - it.second.transforms.clear(); - update_light_instance(it.second); + for (auto &it : light_instances_.values()) { + it.transforms.clear(); + update_light_instance(it); } } @@ -125,10 +125,10 @@ bool InstancerData::update_visibility() } } char name[16]; - for (auto &it : light_instances_) { - for (int i = 0; i < it.second.count; ++i) { + for (auto &it : light_instances_.values()) { + for (int i = 0; i < it.count; ++i) { snprintf(name, 16, "L_%08x", i); - change_tracker.MarkRprimDirty(it.second.data->prim_id.AppendElementString(name), + change_tracker.MarkRprimDirty(it.data->prim_id.AppendElementString(name), pxr::HdChangeTracker::DirtyVisibility); } } @@ -215,7 +215,7 @@ void InstancerData::check_update(Object *object) if (!is_instance_visible(object)) { l_inst->transforms.clear(); update_light_instance(*l_inst); - light_instances_.erase(path); + light_instances_.remove(path); return; } @@ -254,31 +254,30 @@ void InstancerData::check_update(Object *object) void InstancerData::check_remove(std::set &available_objects) { bool ret = false; - for (auto it : mesh_instances_.items()) { - if (available_objects.find(it.key.GetName()) != available_objects.end()) { - continue; - } - it.value.data->remove(); - mesh_instances_.remove(it.key); - auto it = mesh_instances_.items().begin(); + + mesh_instances_.remove_if([&](auto item) { + bool res = available_objects.find(item.key.GetName()) == available_objects.end(); + if (res){ + item.value.data->remove(); ret = true; - } + }; + return res; + }); + if (ret) { write_instances(); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( prim_id, pxr::HdChangeTracker::AllDirty); } - for (auto it = light_instances_.begin(); it != light_instances_.end(); ++it) { - if (available_objects.find(it->first.GetName()) != available_objects.end()) { - continue; - } - it->second.transforms.clear(); - update_light_instance(it->second); - - light_instances_.erase(it); - it = light_instances_.begin(); - } + light_instances_.remove_if([&](auto item) { + bool res = available_objects.find(item.key.GetName()) == available_objects.end(); + if (res){ + item.value.transforms.clear(); + update_light_instance(item.value); + }; + return res; + }); } void InstancerData::available_materials(std::set &paths) const @@ -346,8 +345,8 @@ void InstancerData::write_instances() for (auto &it : mesh_instances_.values()) { it.indices.clear(); } - for (auto &it : light_instances_) { - it.second.transforms.clear(); + for (auto &it : light_instances_.values()) { + it.transforms.clear(); } ListBase *lb = object_duplilist( @@ -362,7 +361,7 @@ void InstancerData::write_instances() if (ob->type == OB_LAMP) { LightInstance *inst = light_instance(p_id); if (!inst) { - inst = &light_instances_[p_id]; + inst = light_instances_.lookup_ptr(p_id); inst->data = std::make_unique(scene_delegate_, ob, p_id); inst->data->init(); } @@ -390,13 +389,10 @@ void InstancerData::write_instances() mesh_instances_.remove_if([&](auto item) {return item.value.indices.empty();}); /* Update light intances and remove instances without transforms */ - for (auto it = light_instances_.begin(); it != light_instances_.end(); ++it) { - update_light_instance(it->second); - if (it->second.transforms.empty()) { - light_instances_.erase(it); - it = light_instances_.begin(); - } - } + light_instances_.remove_if([&](auto item) { + update_light_instance(item.value); + return item.value.transforms.empty(); + }); } void InstancerData::update_light_instance(LightInstance &inst) @@ -465,11 +461,11 @@ InstancerData::MeshInstance *InstancerData::mesh_instance(pxr::SdfPath const &id InstancerData::LightInstance *InstancerData::light_instance(pxr::SdfPath const &id) const { - auto it = light_instances_.find(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); - if (it == light_instances_.end()) { + auto it = light_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); + if (it == nullptr) { return nullptr; } - return const_cast(&it->second); + return const_cast(it); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index a272e93ba475..c019ce496063 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -58,7 +58,7 @@ class InstancerData : public ObjectData { LightInstance *light_instance(pxr::SdfPath const &id) const; Map mesh_instances_; - pxr::TfHashMap light_instances_; + Map light_instances_; pxr::VtMatrix4dArray mesh_transforms_; }; -- 2.30.2 From f603bf3e0284d126828c99c78eef443d71045c22 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 22:10:14 +0300 Subject: [PATCH 07/16] Make format --- .../scene_delegate/blender_scene_delegate.cc | 36 +++++++++---------- .../render/hydra/scene_delegate/curves.cc | 3 +- .../blender/render/hydra/scene_delegate/id.h | 2 +- .../render/hydra/scene_delegate/instancer.cc | 26 +++++++------- .../render/hydra/scene_delegate/instancer.h | 3 +- .../render/hydra/scene_delegate/material.h | 2 +- .../render/hydra/scene_delegate/mesh.cc | 4 +-- .../render/hydra/scene_delegate/object.h | 5 ++- 8 files changed, 39 insertions(+), 42 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 ce4286d744e9..ca79469a6b90 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -218,7 +218,6 @@ void BlenderSceneDelegate::clear() it->remove(); } - objects_.clear(); instancers_.clear(); materials_.clear_and_shrink(); @@ -548,26 +547,25 @@ void BlenderSceneDelegate::remove_unused_objects() /* Remove unused instancers */ instancers_.remove_if([&](auto item) { - bool ret = available_objects.find(item.key.GetName()) == available_objects.end(); - if (ret){ - item.value->remove(); - } - else{ - item.value->check_remove(available_objects); - } - return ret; + bool ret = available_objects.find(item.key.GetName()) == available_objects.end(); + if (ret) { + item.value->remove(); + } + else { + item.value->check_remove(available_objects); + } + return ret; }); /* Remove unused objects */ - objects_.remove_if([&](auto item) { + objects_.remove_if([&](auto item) { bool ret = available_objects.find(item.key.GetName()) == available_objects.end(); - if (ret){ - item.value->remove(); - } - return ret; + if (ret) { + item.value->remove(); + } + return ret; }); - /* Remove unused materials */ std::set available_materials; for (auto &it : objects_.values()) { @@ -586,10 +584,10 @@ void BlenderSceneDelegate::remove_unused_objects() materials_.remove_if([&](auto item) { bool ret = available_materials.find(item.key) == available_materials.end(); - if (ret){ - item.value->remove(); - } - return ret; + if (ret) { + item.value->remove(); + } + return ret; }); } diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 5d988526f03e..415e5a2b8229 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -199,7 +199,8 @@ void CurvesData::write_material() pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); mat_data_ = scene_delegate_->material_data(p_id); if (!mat_data_) { - scene_delegate_->materials_.add_overwrite(p_id, std::make_unique(scene_delegate_, mat, p_id)); + scene_delegate_->materials_.add_overwrite( + p_id, std::make_unique(scene_delegate_, mat, p_id)); mat_data_ = scene_delegate_->material_data(p_id); mat_data_->init(); mat_data_->insert(); diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 2d10ee1e3c58..c210236e2ba3 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -7,8 +7,8 @@ #include #include -#include "DNA_ID.h" #include "BLI_hash.hh" +#include "DNA_ID.h" template<> struct blender::DefaultHash { uint64_t operator()(const pxr::SdfPath &value) const diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index b7df8e87d246..63985372c860 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -257,11 +257,11 @@ void InstancerData::check_remove(std::set &available_objects) mesh_instances_.remove_if([&](auto item) { bool res = available_objects.find(item.key.GetName()) == available_objects.end(); - if (res){ - item.value.data->remove(); - ret = true; - }; - return res; + if (res) { + item.value.data->remove(); + ret = true; + }; + return res; }); if (ret) { @@ -272,11 +272,11 @@ void InstancerData::check_remove(std::set &available_objects) light_instances_.remove_if([&](auto item) { bool res = available_objects.find(item.key.GetName()) == available_objects.end(); - if (res){ - item.value.transforms.clear(); - update_light_instance(item.value); - }; - return res; + if (res) { + item.value.transforms.clear(); + update_light_instance(item.value); + }; + return res; }); } @@ -386,12 +386,12 @@ void InstancerData::write_instances() free_object_duplilist(lb); /* Remove mesh intances without indices */ - mesh_instances_.remove_if([&](auto item) {return item.value.indices.empty();}); + mesh_instances_.remove_if([&](auto item) { return item.value.indices.empty(); }); /* Update light intances and remove instances without transforms */ light_instances_.remove_if([&](auto item) { - update_light_instance(item.value); - return item.value.transforms.empty(); + update_light_instance(item.value); + return item.value.transforms.empty(); }); } diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index c019ce496063..8af648ceffc5 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -62,7 +62,6 @@ class InstancerData : public ObjectData { pxr::VtMatrix4dArray mesh_transforms_; }; -using InstancerDataMap = - Map>; +using InstancerDataMap = Map>; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 5488b3b3ee2e..fd87cd332414 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -7,8 +7,8 @@ #include #include -#include "DNA_material_types.h" #include "BLI_map.hh" +#include "DNA_material_types.h" #include "id.h" diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 9a82da08f249..348ed6b3a9bf 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -304,8 +304,8 @@ void MeshData::write_materials() pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); m.mat_data = scene_delegate_->material_data(p_id); if (!m.mat_data) { - scene_delegate_->materials_.add_overwrite(p_id, - std::make_unique(scene_delegate_, mat, p_id)); + scene_delegate_->materials_.add_overwrite( + p_id, std::make_unique(scene_delegate_, mat, p_id)); m.mat_data = scene_delegate_->material_data(p_id); m.mat_data->init(); m.mat_data->insert(); diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 7639cfc9a652..79b2150cdb7f 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -7,8 +7,8 @@ #include #include "BKE_layer.h" -#include "DNA_object_types.h" #include "BLI_map.hh" +#include "DNA_object_types.h" #include "id.h" #include "material.h" @@ -37,8 +37,7 @@ class ObjectData : public IdData { Object *parent_ = nullptr; }; -using ObjectDataMap = - Map>; +using ObjectDataMap = Map>; pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]); -- 2.30.2 From 5df34751f55951d9ab9f0b84188932093d6d9ad5 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Mon, 29 May 2023 22:17:11 +0300 Subject: [PATCH 08/16] Reverted mistaken changes. --- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 ca79469a6b90..adbd433ea729 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -217,10 +217,13 @@ void BlenderSceneDelegate::clear() for (auto &it : instancers_.values()) { it->remove(); } + for (auto &it : materials_.values()) { + it->remove(); + } objects_.clear(); instancers_.clear(); - materials_.clear_and_shrink(); + materials_.clear(); depsgraph = nullptr; context = nullptr; -- 2.30.2 From 49013bffef95c46ff87364a95c510d192a31a694 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Tue, 30 May 2023 14:57:01 +0300 Subject: [PATCH 09/16] Used add_new instead add_overwrite --- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 4 ++-- source/blender/render/hydra/scene_delegate/curves.cc | 2 +- source/blender/render/hydra/scene_delegate/mesh.cc | 2 +- 3 files changed, 4 insertions(+), 4 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 adbd433ea729..d2f10824a6f0 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -349,7 +349,7 @@ void BlenderSceneDelegate::update_objects(Object *object) return; } - objects_.add_overwrite(id, ObjectData::create(this, object, id)); + objects_.add_new(id, ObjectData::create(this, object, id)); obj_data = object_data(id); obj_data->update_parent(); obj_data->init(); @@ -386,7 +386,7 @@ void BlenderSceneDelegate::update_instancers(Object *object) return; } - instancers_.add_overwrite(id, std::make_unique(this, object, id)); + instancers_.add_new(id, std::make_unique(this, object, id)); i_data = instancer_data(id); i_data->init(); i_data->insert(); diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 415e5a2b8229..bb50a0a04271 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -199,7 +199,7 @@ void CurvesData::write_material() pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); mat_data_ = scene_delegate_->material_data(p_id); if (!mat_data_) { - scene_delegate_->materials_.add_overwrite( + scene_delegate_->materials_.add_new( p_id, std::make_unique(scene_delegate_, mat, p_id)); mat_data_ = scene_delegate_->material_data(p_id); mat_data_->init(); diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 348ed6b3a9bf..250f005c51b5 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -304,7 +304,7 @@ void MeshData::write_materials() pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); m.mat_data = scene_delegate_->material_data(p_id); if (!m.mat_data) { - scene_delegate_->materials_.add_overwrite( + scene_delegate_->materials_.add_new( p_id, std::make_unique(scene_delegate_, mat, p_id)); m.mat_data = scene_delegate_->material_data(p_id); m.mat_data->init(); -- 2.30.2 From a735cd3b659f53c9a6c0c82fb893f32d4bac6f0a Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Tue, 30 May 2023 15:20:44 +0300 Subject: [PATCH 10/16] Added missed .data->remove() --- source/blender/render/hydra/scene_delegate/instancer.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 63985372c860..197bfa8131c7 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -386,7 +386,13 @@ void InstancerData::write_instances() free_object_duplilist(lb); /* Remove mesh intances without indices */ - mesh_instances_.remove_if([&](auto item) { return item.value.indices.empty(); }); + mesh_instances_.remove_if([&](auto item) { + bool res = item.value.indices.empty(); + if (res){ + item.value.data->remove(); + } + return item.value.indices.empty(); + }); /* Update light intances and remove instances without transforms */ light_instances_.remove_if([&](auto item) { -- 2.30.2 From c47f08f2e15c37c65f6ca627ce503178809a7baf Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Tue, 30 May 2023 21:31:06 +0300 Subject: [PATCH 11/16] Revert logic, fixed mesh and light instances. --- .../render/hydra/scene_delegate/instancer.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 197bfa8131c7..2db31df01541 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -361,7 +361,7 @@ void InstancerData::write_instances() if (ob->type == OB_LAMP) { LightInstance *inst = light_instance(p_id); if (!inst) { - inst = light_instances_.lookup_ptr(p_id); + inst = &light_instances_.lookup_or_add_default(p_id); inst->data = std::make_unique(scene_delegate_, ob, p_id); inst->data->init(); } @@ -371,16 +371,14 @@ void InstancerData::write_instances() else { MeshInstance *inst = mesh_instance(p_id); if (!inst) { - inst = mesh_instances_.lookup_ptr(p_id); - if (inst) { - inst->data = std::make_unique(scene_delegate_, ob, p_id); - inst->data->init(); - inst->data->insert(); - 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 = &mesh_instances_.lookup_or_add_default(p_id); + inst->data = std::make_unique(scene_delegate_, ob, p_id); + inst->data->init(); + inst->data->insert(); } + 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)); } } free_object_duplilist(lb); -- 2.30.2 From f850776aef94088446586ee23fdb46375f527ef0 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Tue, 30 May 2023 21:35:09 +0300 Subject: [PATCH 12/16] Code improvement and make format --- .../blender/render/hydra/scene_delegate/instancer.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 2db31df01541..1affcfeb584d 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -385,11 +385,11 @@ void InstancerData::write_instances() /* Remove mesh intances without indices */ mesh_instances_.remove_if([&](auto item) { - bool res = item.value.indices.empty(); - if (res){ - item.value.data->remove(); - } - return item.value.indices.empty(); + bool res = item.value.indices.empty(); + if (res) { + item.value.data->remove(); + } + return res; }); /* Update light intances and remove instances without transforms */ -- 2.30.2 From 4ecac046cc063a327c6decbc112421217812f527 Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Wed, 31 May 2023 13:38:15 +0300 Subject: [PATCH 13/16] Changed naming 'it' to 'val'. --- .../scene_delegate/blender_scene_delegate.cc | 52 +++++++++--------- .../render/hydra/scene_delegate/instancer.cc | 54 +++++++++---------- .../render/hydra/scene_delegate/material.cc | 8 +-- 3 files changed, 57 insertions(+), 57 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 d2f10824a6f0..fa4ccb151f05 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -211,14 +211,14 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) void BlenderSceneDelegate::clear() { - for (auto &it : objects_.values()) { - it->remove(); + for (auto &val : objects_.values()) { + val->remove(); } - for (auto &it : instancers_.values()) { - it->remove(); + for (auto &val : instancers_.values()) { + val->remove(); } - for (auto &it : materials_.values()) { - it->remove(); + for (auto &val : materials_.values()) { + val->remove(); } objects_.clear(); @@ -269,9 +269,9 @@ pxr::SdfPath BlenderSceneDelegate::world_prim_id() const ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) const { pxr::SdfPath p_id = (id.GetName().find("SM_") == 0) ? id.GetParentPath() : id; - const std::unique_ptr *value = objects_.lookup_ptr(p_id); - if (value != nullptr) { - return value->get(); + const std::unique_ptr *val = objects_.lookup_ptr(p_id); + if (val != nullptr) { + return val->get(); } InstancerData *i_data = instancer_data(p_id, true); if (i_data) { @@ -297,11 +297,11 @@ LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) const MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) const { - const std::unique_ptr *value = materials_.lookup_ptr(id); - if (value == nullptr) { + const std::unique_ptr *val = materials_.lookup_ptr(id); + if (!val) { return nullptr; } - return value->get(); + return val->get(); } InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool child_id) const @@ -322,9 +322,9 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool p_id = id; } - const std::unique_ptr *value = instancers_.lookup_ptr(p_id); - if (value != nullptr) { - return value->get(); + const std::unique_ptr *val = instancers_.lookup_ptr(p_id); + if (val != nullptr) { + return val->get(); } return nullptr; } @@ -359,8 +359,8 @@ void BlenderSceneDelegate::update_objects(Object *object) void BlenderSceneDelegate::update_instancers(Object *object) { /* Check object inside instancers */ - for (auto &it : instancers_.values()) { - it->check_update(object); + for (auto &val : instancers_.values()) { + val->check_update(object); } pxr::SdfPath id = instancer_prim_id(object); @@ -571,18 +571,18 @@ void BlenderSceneDelegate::remove_unused_objects() /* Remove unused materials */ std::set available_materials; - for (auto &it : objects_.values()) { - MeshData *m_data = dynamic_cast(it.get()); + for (auto &val : objects_.values()) { + MeshData *m_data = dynamic_cast(val.get()); if (m_data) { m_data->available_materials(available_materials); } - CurvesData *c_data = dynamic_cast(it.get()); + CurvesData *c_data = dynamic_cast(val.get()); if (c_data) { c_data->available_materials(available_materials); } } - for (auto &it : instancers_.values()) { - it->available_materials(available_materials); + for (auto &val : instancers_.values()) { + val->available_materials(available_materials); } materials_.remove_if([&](auto item) { @@ -597,11 +597,11 @@ void BlenderSceneDelegate::remove_unused_objects() void BlenderSceneDelegate::update_visibility() { /* Updating visibility of existing objects/instancers */ - for (auto &it : objects_.values()) { - it->update_visibility(); + for (auto &val : objects_.values()) { + val->update_visibility(); } - for (auto &it : instancers_.values()) { - it->update_visibility(); + for (auto &val : instancers_.values()) { + val->update_visibility(); } /* Add objects/instancers which were invisible before and not added yet */ diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 1affcfeb584d..ce117d9811fd 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -74,14 +74,14 @@ void InstancerData::insert() void InstancerData::remove() { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s", prim_id.GetText()); - for (auto &it : mesh_instances_.values()) { - it.data->remove(); + for (auto &val : mesh_instances_.values()) { + val.data->remove(); } scene_delegate_->GetRenderIndex().RemoveInstancer(prim_id); - for (auto &it : light_instances_.values()) { - it.transforms.clear(); - update_light_instance(it); + for (auto &val : light_instances_.values()) { + val.transforms.clear(); + update_light_instance(val); } } @@ -118,17 +118,17 @@ bool InstancerData::update_visibility() if (ret) { auto &change_tracker = scene_delegate_->GetRenderIndex().GetChangeTracker(); change_tracker.MarkInstancerDirty(prim_id, pxr::HdChangeTracker::DirtyVisibility); - for (auto &it : mesh_instances_.values()) { - it.data->visible = visible; - for (auto &p : it.data->submesh_paths()) { + for (auto &val : mesh_instances_.values()) { + val.data->visible = visible; + for (auto &p : val.data->submesh_paths()) { change_tracker.MarkRprimDirty(p, pxr::HdChangeTracker::DirtyVisibility); } } char name[16]; - for (auto &it : light_instances_.values()) { - for (int i = 0; i < it.count; ++i) { + for (auto &val : light_instances_.values()) { + for (int i = 0; i < val.count; ++i) { snprintf(name, 16, "L_%08x", i); - change_tracker.MarkRprimDirty(it.data->prim_id.AppendElementString(name), + change_tracker.MarkRprimDirty(val.data->prim_id.AppendElementString(name), pxr::HdChangeTracker::DirtyVisibility); } } @@ -179,8 +179,8 @@ ObjectData *InstancerData::object_data(pxr::SdfPath const &id) const pxr::SdfPathVector InstancerData::prototypes() const { pxr::SdfPathVector paths; - for (auto &it : mesh_instances_.values()) { - for (auto &p : it.data->submesh_paths()) { + for (auto &val : mesh_instances_.values()) { + for (auto &p : val.data->submesh_paths()) { paths.push_back(p); } } @@ -282,8 +282,8 @@ void InstancerData::check_remove(std::set &available_objects) void InstancerData::available_materials(std::set &paths) const { - for (auto &it : mesh_instances_.values()) { - ((MeshData *)it.data.get())->available_materials(paths); + for (auto &val : mesh_instances_.values()) { + ((MeshData *)val.data.get())->available_materials(paths); } } @@ -296,8 +296,8 @@ void InstancerData::update_as_parent() void InstancerData::update_double_sided(MaterialData *mat_data) { - for (auto &it : mesh_instances_.values()) { - it.data->update_double_sided(mat_data); + for (auto &val : mesh_instances_.values()) { + val.data->update_double_sided(mat_data); } } @@ -342,11 +342,11 @@ int InstancerData::light_prim_id_index(pxr::SdfPath const &id) const void InstancerData::write_instances() { mesh_transforms_.clear(); - for (auto &it : mesh_instances_.values()) { - it.indices.clear(); + for (auto &val : mesh_instances_.values()) { + val.indices.clear(); } - for (auto &it : light_instances_.values()) { - it.transforms.clear(); + for (auto &val : light_instances_.values()) { + val.transforms.clear(); } ListBase *lb = object_duplilist( @@ -456,20 +456,20 @@ void InstancerData::update_light_instance(LightInstance &inst) InstancerData::MeshInstance *InstancerData::mesh_instance(pxr::SdfPath const &id) const { - auto it = mesh_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); - if (it == nullptr) { + auto val = mesh_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); + if (!val) { return nullptr; } - return const_cast(it); + return const_cast(val); } InstancerData::LightInstance *InstancerData::light_instance(pxr::SdfPath const &id) const { - auto it = light_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); - if (it == nullptr) { + auto val = light_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); + if (!val) { return nullptr; } - return const_cast(it); + return const_cast(val); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 808b2aacd26c..07a0d20f3bbd 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -60,14 +60,14 @@ void MaterialData::update() scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, pxr::HdMaterial::AllDirty); if (prev_double_sided != double_sided) { - for (auto &it : scene_delegate_->objects_.values()) { - MeshData *m_data = dynamic_cast(it.get()); + for (auto &val : scene_delegate_->objects_.values()) { + MeshData *m_data = dynamic_cast(val.get()); if (m_data) { m_data->update_double_sided(this); } } - for (auto &it : scene_delegate_->instancers_.values()) { - it->update_double_sided(this); + for (auto &val : scene_delegate_->instancers_.values()) { + val->update_double_sided(this); } } } -- 2.30.2 From 3f4cfa89c9557b9da51cae6eb42ba32d58d9188b Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Wed, 31 May 2023 23:21:02 +0300 Subject: [PATCH 14/16] Refactored to obj_data, m_data, mat_data, i_data, l_inst, m_inst. --- .../scene_delegate/blender_scene_delegate.cc | 34 +++++------ .../render/hydra/scene_delegate/instancer.cc | 56 ++++++++++--------- .../render/hydra/scene_delegate/material.cc | 8 +-- 3 files changed, 50 insertions(+), 48 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 fa4ccb151f05..4b4e3a79ac26 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -211,14 +211,14 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) void BlenderSceneDelegate::clear() { - for (auto &val : objects_.values()) { - val->remove(); + for (auto &obj_data : objects_.values()) { + obj_data->remove(); } - for (auto &val : instancers_.values()) { - val->remove(); + for (auto &i_data : instancers_.values()) { + i_data->remove(); } - for (auto &val : materials_.values()) { - val->remove(); + for (auto &mat_data : materials_.values()) { + mat_data->remove(); } objects_.clear(); @@ -269,9 +269,9 @@ pxr::SdfPath BlenderSceneDelegate::world_prim_id() const ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) const { pxr::SdfPath p_id = (id.GetName().find("SM_") == 0) ? id.GetParentPath() : id; - const std::unique_ptr *val = objects_.lookup_ptr(p_id); - if (val != nullptr) { - return val->get(); + auto obj_data = objects_.lookup_ptr(p_id); + if (obj_data) { + return obj_data->get(); } InstancerData *i_data = instancer_data(p_id, true); if (i_data) { @@ -297,11 +297,11 @@ LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) const MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) const { - const std::unique_ptr *val = materials_.lookup_ptr(id); - if (!val) { + auto mat_data = materials_.lookup_ptr(id); + if (!mat_data) { return nullptr; } - return val->get(); + return mat_data->get(); } InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool child_id) const @@ -322,9 +322,9 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool p_id = id; } - const std::unique_ptr *val = instancers_.lookup_ptr(p_id); - if (val != nullptr) { - return val->get(); + auto i_data = instancers_.lookup_ptr(p_id); + if (i_data) { + return i_data->get(); } return nullptr; } @@ -359,8 +359,8 @@ void BlenderSceneDelegate::update_objects(Object *object) void BlenderSceneDelegate::update_instancers(Object *object) { /* Check object inside instancers */ - for (auto &val : instancers_.values()) { - val->check_update(object); + for (auto &i_data : instancers_.values()) { + i_data->check_update(object); } pxr::SdfPath id = instancer_prim_id(object); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index ce117d9811fd..389719e37cfd 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -74,14 +74,14 @@ void InstancerData::insert() void InstancerData::remove() { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s", prim_id.GetText()); - for (auto &val : mesh_instances_.values()) { - val.data->remove(); + for (auto &m_inst : mesh_instances_.values()) { + m_inst.data->remove(); } scene_delegate_->GetRenderIndex().RemoveInstancer(prim_id); - for (auto &val : light_instances_.values()) { - val.transforms.clear(); - update_light_instance(val); + for (auto &l_inst : light_instances_.values()) { + l_inst.transforms.clear(); + update_light_instance(l_inst); } } @@ -118,17 +118,17 @@ bool InstancerData::update_visibility() if (ret) { auto &change_tracker = scene_delegate_->GetRenderIndex().GetChangeTracker(); change_tracker.MarkInstancerDirty(prim_id, pxr::HdChangeTracker::DirtyVisibility); - for (auto &val : mesh_instances_.values()) { - val.data->visible = visible; - for (auto &p : val.data->submesh_paths()) { + 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 &val : light_instances_.values()) { - for (int i = 0; i < val.count; ++i) { + for (auto &l_inst : light_instances_.values()) { + for (int i = 0; i < l_inst.count; ++i) { snprintf(name, 16, "L_%08x", i); - change_tracker.MarkRprimDirty(val.data->prim_id.AppendElementString(name), + change_tracker.MarkRprimDirty(l_inst.data->prim_id.AppendElementString(name), pxr::HdChangeTracker::DirtyVisibility); } } @@ -179,8 +179,8 @@ ObjectData *InstancerData::object_data(pxr::SdfPath const &id) const pxr::SdfPathVector InstancerData::prototypes() const { pxr::SdfPathVector paths; - for (auto &val : mesh_instances_.values()) { - for (auto &p : val.data->submesh_paths()) { + for (auto &m_inst : mesh_instances_.values()) { + for (auto &p : m_inst.data->submesh_paths()) { paths.push_back(p); } } @@ -282,8 +282,8 @@ void InstancerData::check_remove(std::set &available_objects) void InstancerData::available_materials(std::set &paths) const { - for (auto &val : mesh_instances_.values()) { - ((MeshData *)val.data.get())->available_materials(paths); + for (auto &m_inst : mesh_instances_.values()) { + ((MeshData *)m_inst.data.get())->available_materials(paths); } } @@ -296,8 +296,8 @@ void InstancerData::update_as_parent() void InstancerData::update_double_sided(MaterialData *mat_data) { - for (auto &val : mesh_instances_.values()) { - val.data->update_double_sided(mat_data); + for (auto &m_inst : mesh_instances_.values()) { + m_inst.data->update_double_sided(mat_data); } } @@ -342,11 +342,11 @@ int InstancerData::light_prim_id_index(pxr::SdfPath const &id) const void InstancerData::write_instances() { mesh_transforms_.clear(); - for (auto &val : mesh_instances_.values()) { - val.indices.clear(); + for (auto &m_inst : mesh_instances_.values()) { + m_inst.indices.clear(); } - for (auto &val : light_instances_.values()) { - val.transforms.clear(); + for (auto &l_inst : light_instances_.values()) { + l_inst.transforms.clear(); } ListBase *lb = object_duplilist( @@ -456,20 +456,22 @@ void InstancerData::update_light_instance(LightInstance &inst) InstancerData::MeshInstance *InstancerData::mesh_instance(pxr::SdfPath const &id) const { - auto val = mesh_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); - if (!val) { + auto m_inst = mesh_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : + id); + if (!m_inst) { return nullptr; } - return const_cast(val); + return const_cast(m_inst); } InstancerData::LightInstance *InstancerData::light_instance(pxr::SdfPath const &id) const { - auto val = light_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : id); - if (!val) { + auto l_inst = light_instances_.lookup_ptr(id.GetPathElementCount() == 4 ? id.GetParentPath() : + id); + if (!l_inst) { return nullptr; } - return const_cast(val); + return const_cast(l_inst); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 07a0d20f3bbd..827f31660d97 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -60,14 +60,14 @@ void MaterialData::update() scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, pxr::HdMaterial::AllDirty); if (prev_double_sided != double_sided) { - for (auto &val : scene_delegate_->objects_.values()) { - MeshData *m_data = dynamic_cast(val.get()); + for (auto &obj_data : scene_delegate_->objects_.values()) { + MeshData *m_data = dynamic_cast(obj_data.get()); if (m_data) { m_data->update_double_sided(this); } } - for (auto &val : scene_delegate_->instancers_.values()) { - val->update_double_sided(this); + for (auto &i_data : scene_delegate_->instancers_.values()) { + i_data->update_double_sided(this); } } } -- 2.30.2 From a9e68d81b38f669cef87dc72111a63b5fb3cbf4a Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Thu, 1 Jun 2023 09:53:14 +0300 Subject: [PATCH 15/16] Refactored std::set to blender::Set Make format --- .../hydra/scene_delegate/blender_scene_delegate.cc | 14 +++++++------- .../hydra/scene_delegate/blender_scene_delegate.h | 1 + .../blender/render/hydra/scene_delegate/curves.cc | 4 ++-- .../blender/render/hydra/scene_delegate/curves.h | 3 ++- .../render/hydra/scene_delegate/instancer.cc | 8 ++++---- .../render/hydra/scene_delegate/instancer.h | 5 +++-- source/blender/render/hydra/scene_delegate/mesh.cc | 4 ++-- source/blender/render/hydra/scene_delegate/mesh.h | 3 ++- 8 files changed, 23 insertions(+), 19 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 4b4e3a79ac26..c68f852e133e 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -524,7 +524,7 @@ void BlenderSceneDelegate::add_new_objects() void BlenderSceneDelegate::remove_unused_objects() { /* Get available objects */ - std::set available_objects; + Set available_objects; DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; @@ -542,15 +542,15 @@ void BlenderSceneDelegate::remove_unused_objects() object) { if (ObjectData::is_supported(object)) { - available_objects.insert(object_prim_id(object).GetName()); + available_objects.add(object_prim_id(object).GetName()); } - available_objects.insert(instancer_prim_id(object).GetName()); + available_objects.add(instancer_prim_id(object).GetName()); } ITER_END; /* Remove unused instancers */ instancers_.remove_if([&](auto item) { - bool ret = available_objects.find(item.key.GetName()) == available_objects.end(); + bool ret = !available_objects.contains(item.key.GetName()); if (ret) { item.value->remove(); } @@ -562,7 +562,7 @@ void BlenderSceneDelegate::remove_unused_objects() /* Remove unused objects */ objects_.remove_if([&](auto item) { - bool ret = available_objects.find(item.key.GetName()) == available_objects.end(); + bool ret = !available_objects.contains(item.key.GetName()); if (ret) { item.value->remove(); } @@ -570,7 +570,7 @@ void BlenderSceneDelegate::remove_unused_objects() }); /* Remove unused materials */ - std::set available_materials; + Set available_materials; for (auto &val : objects_.values()) { MeshData *m_data = dynamic_cast(val.get()); if (m_data) { @@ -586,7 +586,7 @@ void BlenderSceneDelegate::remove_unused_objects() } materials_.remove_if([&](auto item) { - bool ret = available_materials.find(item.key) == available_materials.end(); + bool ret = !available_materials.contains(item.key); if (ret) { item.value->remove(); } 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 cc36e4b7f05e..16f659d99b3e 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -11,6 +11,7 @@ #include "CLG_log.h" +#include "BLI_set.hh" #include "curves.h" #include "instancer.h" #include "light.h" diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index bb50a0a04271..7b13c093d7e3 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -134,10 +134,10 @@ pxr::SdfPath CurvesData::material_id() const return mat_data_->prim_id; } -void CurvesData::available_materials(std::set &paths) const +void CurvesData::available_materials(Set &paths) const { if (mat_data_ && !mat_data_->prim_id.IsEmpty()) { - paths.insert(mat_data_->prim_id); + paths.add(mat_data_->prim_id); } } diff --git a/source/blender/render/hydra/scene_delegate/curves.h b/source/blender/render/hydra/scene_delegate/curves.h index 3fe26b5b2449..47a2199bbaf1 100644 --- a/source/blender/render/hydra/scene_delegate/curves.h +++ b/source/blender/render/hydra/scene_delegate/curves.h @@ -9,6 +9,7 @@ #include "BKE_duplilist.h" #include "DNA_Curves_types.h" +#include "BLI_set.hh" #include "material.h" #include "object.h" @@ -29,7 +30,7 @@ class CurvesData : public ObjectData { pxr::HdBasisCurvesTopology curves_topology(pxr::SdfPath const &id) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::SdfPath material_id() const; - void available_materials(std::set &paths) const; + void available_materials(Set &paths) const; private: void write_curves(Curves *curves); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 389719e37cfd..db4384df1e9a 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -251,12 +251,12 @@ void InstancerData::check_update(Object *object) } } -void InstancerData::check_remove(std::set &available_objects) +void InstancerData::check_remove(Set &available_objects) { bool ret = false; mesh_instances_.remove_if([&](auto item) { - bool res = available_objects.find(item.key.GetName()) == available_objects.end(); + bool res = !available_objects.contains(item.key.GetName()); if (res) { item.value.data->remove(); ret = true; @@ -271,7 +271,7 @@ void InstancerData::check_remove(std::set &available_objects) } light_instances_.remove_if([&](auto item) { - bool res = available_objects.find(item.key.GetName()) == available_objects.end(); + bool res = !available_objects.contains(item.key.GetName()); if (res) { item.value.transforms.clear(); update_light_instance(item.value); @@ -280,7 +280,7 @@ void InstancerData::check_remove(std::set &available_objects) }); } -void InstancerData::available_materials(std::set &paths) const +void InstancerData::available_materials(Set &paths) const { for (auto &m_inst : mesh_instances_.values()) { ((MeshData *)m_inst.data.get())->available_materials(paths); diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 8af648ceffc5..b8a1d127d64c 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -5,6 +5,7 @@ #include "BKE_duplilist.h" #include "BLI_map.hh" +#include "BLI_set.hh" #include "light.h" #include "mesh.h" @@ -42,8 +43,8 @@ class InstancerData : public ObjectData { ObjectData *object_data(pxr::SdfPath const &id) const; pxr::SdfPathVector prototypes() const; void check_update(Object *object); - void check_remove(std::set &available_objects); - void available_materials(std::set &paths) const; + void check_remove(Set &available_objects); + void available_materials(Set &paths) const; void update_as_parent(); void update_double_sided(MaterialData *mat_data); diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 250f005c51b5..d5f8d6c6ac15 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -173,11 +173,11 @@ void MeshData::update_double_sided(MaterialData *mat_data) } } -void MeshData::available_materials(std::set &paths) const +void MeshData::available_materials(Set &paths) const { for (auto &sm : submeshes_) { if (sm.mat_data && !sm.mat_data->prim_id.IsEmpty()) { - paths.insert(sm.mat_data->prim_id); + paths.add(sm.mat_data->prim_id); } } } diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 750e9c2bdf39..95f0ba04ed2a 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -7,6 +7,7 @@ #include #include "BKE_duplilist.h" +#include "BLI_set.hh" #include "material.h" #include "object.h" @@ -39,7 +40,7 @@ class MeshData : public ObjectData { pxr::SdfPath material_id(pxr::SdfPath const &id) const; bool double_sided(pxr::SdfPath const &id) const; void update_double_sided(MaterialData *mat_data); - void available_materials(std::set &paths) const; + void available_materials(Set &paths) const; pxr::SdfPathVector submesh_paths() const; pxr::HdCullStyle cull_style = pxr::HdCullStyleBackUnlessDoubleSided; -- 2.30.2 From 445e628bf343a21bbe5605634ce5d1d753e1205c Mon Sep 17 00:00:00 2001 From: Vasyl-Pidhirskyi Date: Thu, 1 Jun 2023 16:45:19 +0300 Subject: [PATCH 16/16] Refactored add_new to lookup_or_add Make format. --- .../scene_delegate/blender_scene_delegate.cc | 3 +-- .../blender/render/hydra/scene_delegate/mesh.cc | 15 +++++++-------- 2 files changed, 8 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 c68f852e133e..a11d8be990cf 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -386,8 +386,7 @@ void BlenderSceneDelegate::update_instancers(Object *object) return; } - instancers_.add_new(id, std::make_unique(this, object, id)); - i_data = instancer_data(id); + i_data = instancers_.lookup_or_add(id, std::make_unique(this, object, id)).get(); i_data->init(); i_data->insert(); } diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index d5f8d6c6ac15..36f6050dfd71 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -301,15 +301,14 @@ void MeshData::write_materials() m.mat_data = nullptr; continue; } + pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); - m.mat_data = scene_delegate_->material_data(p_id); - if (!m.mat_data) { - scene_delegate_->materials_.add_new( - p_id, std::make_unique(scene_delegate_, mat, p_id)); - m.mat_data = scene_delegate_->material_data(p_id); - m.mat_data->init(); - m.mat_data->insert(); - } + 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(); } } -- 2.30.2