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
6 changed files with 45 additions and 69 deletions
Showing only changes of commit 3988b93350 - Show all commits

View File

@ -19,6 +19,8 @@ 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, nullptr, GetDelegateID().AppendElementString("Instancer"));
} }
pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id) pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id)
@ -205,7 +207,7 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
check_updates(); check_updates();
} }
else { else {
add_new_objects(); update_collection();
update_world(); update_world();
} }
} }
@ -422,7 +424,6 @@ void BlenderSceneDelegate::update_world()
void BlenderSceneDelegate::check_updates() void BlenderSceneDelegate::check_updates()
{ {
bool do_update_collection = false; bool do_update_collection = false;
bool do_update_visibility = false;
bool do_update_world = false; bool do_update_world = false;
DEGIDIterData data = {0}; DEGIDIterData data = {0};
@ -440,14 +441,7 @@ void BlenderSceneDelegate::check_updates()
switch (GS(id->name)) { switch (GS(id->name)) {
case ID_OB: { case ID_OB: {
Object *object = (Object *)id; do_update_collection = true;
CLOG_INFO(LOG_RENDER_HYDRA_SCENE,
2,
"Visibility: %s [%s]",
object->id.name,
std::bitset<3>(BKE_object_visibility(object, deg_mode)).to_string().c_str());
update_objects(object);
update_instancers(object);
} break; } break;
case ID_MA: { case ID_MA: {
@ -466,10 +460,9 @@ void BlenderSceneDelegate::check_updates()
case ID_SCE: { 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)) {
do_update_collection = true; do_update_collection = true;
do_update_visibility = true;
} }
if (id->recalc & ID_RECALC_BASE_FLAGS) { if (id->recalc & ID_RECALC_BASE_FLAGS) {
do_update_visibility = true; do_update_collection = true;
} }
if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) {
do_update_collection = true; do_update_collection = true;
@ -491,10 +484,7 @@ void BlenderSceneDelegate::check_updates()
update_world(); update_world();
} }
if (do_update_collection) { if (do_update_collection) {
remove_unused_objects(); update_collection();
}
if (do_update_visibility) {
update_visibility();
} }
} }
@ -527,20 +517,19 @@ void BlenderSceneDelegate::add_new_objects()
} }
ITER_END; ITER_END;
instancer_data_ = std::make_unique<InstancerData>(
this, nullptr, GetDelegateID().AppendElementString("Instancer"));
instancer_data_->init(); instancer_data_->init();
instancer_data_->insert(); instancer_data_->insert();
} }
void BlenderSceneDelegate::remove_unused_objects() void BlenderSceneDelegate::update_collection()
{ {
/* Get available objects */ /* 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 |
DEG_ITER_OBJECT_FLAG_VISIBLE;
DEGObjectIterData data = {0}; DEGObjectIterData data = {0};
data.settings = &settings; data.settings = &settings;
data.graph = settings.depsgraph; data.graph = settings.depsgraph;
@ -553,24 +542,23 @@ void BlenderSceneDelegate::remove_unused_objects()
Object *, Object *,
object) object)
{ {
if (ObjectData::is_supported(object)) { if (!ObjectData::is_supported(object)) {
continue;
}
available_objects.add(object_prim_id(object).GetName()); available_objects.add(object_prim_id(object).GetName());
}
available_objects.add(instancer_prim_id(object).GetName());
}
ITER_END;
/* Remove unused instancers */ pxr::SdfPath id = object_prim_id(object);
instancers_.remove_if([&](auto item) { ObjectData *obj_data = object_data(id);
bool ret = !available_objects.contains(item.key.GetName()); if (obj_data) {
if (ret) { obj_data->update();
item.value->remove();
} }
else { 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 */ /* Remove unused objects */
objects_.remove_if([&](auto item) { objects_.remove_if([&](auto item) {
@ -593,9 +581,7 @@ void BlenderSceneDelegate::remove_unused_objects()
c_data->available_materials(available_materials); c_data->available_materials(available_materials);
} }
} }
for (auto &val : instancers_.values()) { instancer_data_->available_materials(available_materials);
val->available_materials(available_materials);
}
materials_.remove_if([&](auto item) { materials_.remove_if([&](auto item) {
bool ret = !available_materials.contains(item.key); bool ret = !available_materials.contains(item.key);

View File

@ -91,7 +91,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
void update_world(); void update_world();
void check_updates(); void check_updates();
void add_new_objects(); void add_new_objects();
void remove_unused_objects(); void update_collection();
void update_visibility(); void update_visibility();
ObjectDataMap objects_; ObjectDataMap objects_;

View File

@ -76,8 +76,9 @@ std::string cache_or_get_image_file(Image *image, bContext *context, ImageUser *
} }
else { else {
Main *main = CTX_data_main(context); Main *main = CTX_data_main(context);
file_path.reserve(FILE_MAX); char str[FILE_MAX];
BKE_image_user_file_path_ex(main, iuser, image, file_path.data(), false, true); BKE_image_user_file_path_ex(main, iuser, image, str, false, true);
file_path = str;
if (!pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(file_path)) { if (!pxr::HioImageRegistry::GetInstance().IsSupportedImageFile(file_path)) {
file_path = cache_image_file(image, context, iuser, true); file_path = cache_image_file(image, context, iuser, true);

View File

@ -111,19 +111,16 @@ void LightData::remove()
void LightData::update() void LightData::update()
{ {
ID_LOG(1, "");
Object *object = (Object *)id; Object *object = (Object *)id;
Light *light = (Light *)object->data; Light *light = (Light *)object->data;
pxr::HdDirtyBits bits = pxr::HdLight::Clean;
if (id->recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) {
if (prim_type(light) != prim_type_) { if (prim_type(light) != prim_type_) {
remove(); remove();
init(); init();
insert(); insert();
return; return;
} }
pxr::HdDirtyBits bits = pxr::HdLight::Clean;
if (id->recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) {
init(); init();
bits = pxr::HdLight::AllDirty; bits = pxr::HdLight::AllDirty;
} }
@ -133,6 +130,7 @@ void LightData::update()
} }
if (bits != pxr::HdChangeTracker::Clean) { if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits);
ID_LOG(1, "");
} }
} }

View File

@ -299,6 +299,8 @@ void MeshData::write_materials()
} }
pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat);
m.mat_data = scene_delegate_->material_data(p_id);
if (!m.mat_data) {
m.mat_data = scene_delegate_->materials_ m.mat_data = scene_delegate_->materials_
.lookup_or_add(p_id, .lookup_or_add(p_id,
std::make_unique<MaterialData>(scene_delegate_, mat, p_id)) std::make_unique<MaterialData>(scene_delegate_, mat, p_id))
@ -307,5 +309,6 @@ void MeshData::write_materials()
m.mat_data->insert(); m.mat_data->insert();
} }
} }
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -67,23 +67,11 @@ bool ObjectData::is_supported(Object *object)
bool ObjectData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object) bool ObjectData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object)
{ {
eEvaluationMode deg_mode = DEG_get_mode(scene_delegate->depsgraph); eEvaluationMode deg_mode = DEG_get_mode(scene_delegate->depsgraph);
int vis = BKE_object_visibility(object, deg_mode); bool ret = BKE_object_visibility(object, deg_mode) & OB_VISIBLE_SELF;
bool ret = vis & OB_VISIBLE_SELF;
if (deg_mode == DAG_EVAL_VIEWPORT) { if (deg_mode == DAG_EVAL_VIEWPORT) {
ret &= BKE_object_is_visible_in_viewport(scene_delegate->view3d, object); ret &= BKE_object_is_visible_in_viewport(scene_delegate->view3d, object);
} }
else { /* Note: visibility for final render we are taking from depsgraph */
if (ret) {
/* If some of parent object is instancer, then currenct object
* is invisible in Final render */
for (Object *ob = object->parent; ob != nullptr; ob = ob->parent) {
if (ob->transflag & OB_DUPLI) {
ret = false;
break;
}
}
}
}
return ret; return ret;
} }