Make object visibility and instancing creation to be calculated via depsgraph #57

Merged
Bogdan Nagirniak merged 16 commits from BLEN-442 into hydra-render 2023-07-08 10:09:53 +02:00
3 changed files with 126 additions and 11 deletions
Showing only changes of commit 05baf7b646 - Show all commits

View File

@ -31,6 +31,7 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
Engine *engine) Engine *engine)
: HdSceneDelegate(parent_index, delegate_id), engine(engine) : HdSceneDelegate(parent_index, delegate_id), engine(engine)
{ {
instancer_data_ = std::make_unique<InstancerData>(this, instancer_prim_id());
} }
pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id) pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id)
@ -433,18 +434,19 @@ void BlenderSceneDelegate::check_updates()
void BlenderSceneDelegate::update_collection() void BlenderSceneDelegate::update_collection()
{ {
/* Add or update available objects */
Set<std::string> available_objects; Set<std::string> available_objects;
DEGObjectIterSettings settings = {0}; DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph; 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 |

use macro DEG_OBJECT_ITER_FOR_RENDER_ENGINE_FLAGS here

use macro `DEG_OBJECT_ITER_FOR_RENDER_ENGINE_FLAGS` here
DEG_ITER_OBJECT_FLAG_VISIBLE; DEG_ITER_OBJECT_FLAG_VISIBLE | DEG_ITER_OBJECT_FLAG_DUPLI;
DEGObjectIterData data = {0}; DEGObjectIterData data = {0};
data.settings = &settings; data.settings = &settings;
data.graph = settings.depsgraph; data.graph = settings.depsgraph;
data.flag = settings.flags; data.flag = settings.flags;
instancer_data_->pre_update();
ITER_BEGIN (DEG_iterator_objects_begin, ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next, DEG_iterator_objects_next,
DEG_iterator_objects_end, DEG_iterator_objects_end,
@ -452,6 +454,11 @@ void BlenderSceneDelegate::update_collection()
Object *, Object *,
object) object)
{ {
if (data.dupli_object_current) {
instancer_data_->update_instance(data.dupli_parent, data.dupli_object_current);
continue;
}
if (!ObjectData::is_supported(object)) { if (!ObjectData::is_supported(object)) {
continue; continue;
} }
@ -477,14 +484,7 @@ void BlenderSceneDelegate::update_collection()
} }
ITER_END; ITER_END;
if (!instancer_data_) { instancer_data_->post_update();
instancer_data_ = std::make_unique<InstancerData>(this, instancer_prim_id());
instancer_data_->init();
instancer_data_->insert();
}
else {
instancer_data_->update();
}
/* Remove unused objects */ /* Remove unused objects */
objects_.remove_if([&](auto item) { objects_.remove_if([&](auto item) {

View File

@ -51,12 +51,16 @@ void InstancerData::remove()
for (auto &m_inst : mesh_instances_.values()) { for (auto &m_inst : mesh_instances_.values()) {
m_inst.data->remove(); 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()) { for (auto &l_inst : light_instances_.values()) {
l_inst.transforms.clear(); l_inst.transforms.clear();
update_light_instance(l_inst); update_light_instance(l_inst);
} }
light_instances_.clear();
} }
void InstancerData::update() void InstancerData::update()
@ -104,6 +108,94 @@ bool InstancerData::update_visibility()
return ret; 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<LightData>(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<MeshData>(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 pxr::GfMatrix4d InstancerData::get_transform(pxr::SdfPath const &id) const
{ {
LightInstance *l_inst = light_instance(id); LightInstance *l_inst = light_instance(id);
@ -260,6 +352,7 @@ void InstancerData::write_instances()
} }
ITER_END; ITER_END;
/* Remove mesh intances without indices */ /* Remove mesh intances without indices */
mesh_instances_.remove_if([&](auto item) { mesh_instances_.remove_if([&](auto item) {
bool res = item.value.indices.empty(); bool res = item.value.indices.empty();
@ -269,6 +362,24 @@ void InstancerData::write_instances()
return res; 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 */ /* Update light intances and remove instances without transforms */
for (auto &l_inst : light_instances_.values()) { for (auto &l_inst : light_instances_.values()) {
update_light_instance(l_inst); update_light_instance(l_inst);

View File

@ -36,6 +36,10 @@ class InstancerData : public ObjectData {
pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility() 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::GfMatrix4d get_transform(pxr::SdfPath const &id) const;
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const;
pxr::VtIntArray indices(pxr::SdfPath const &id) const; pxr::VtIntArray indices(pxr::SdfPath const &id) const;

Please add comment why this is done in this way

Please add comment why this is done in this way