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 73 additions and 221 deletions
Showing only changes of commit 6aa6bb0870 - Show all commits

View File

@ -156,7 +156,7 @@ bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id)
} }
InstancerData *i_data = instancer_data(id, true); InstancerData *i_data = instancer_data(id, true);
if (i_data) { if (i_data) {
return i_data->visible; return true;
} }
return object_data(id)->visible; 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()); CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", instancer_id.GetText());
InstancerData *i_data = instancer_data(instancer_id); 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) void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)

View File

@ -12,7 +12,7 @@
namespace blender::render::hydra { namespace blender::render::hydra {
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) 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; return false;
} }
void InstancerData::init() void InstancerData::init() {}
{
ID_LOG(1, "");
write_instances();
}
void InstancerData::insert() void InstancerData::insert() {}
{
ID_LOG(1, "");
scene_delegate_->GetRenderIndex().InsertInstancer(scene_delegate_, prim_id);
}
void InstancerData::remove() void InstancerData::remove()
{ {
@ -63,13 +55,7 @@ void InstancerData::remove()
light_instances_.clear(); light_instances_.clear();
} }
void InstancerData::update() void InstancerData::update() {}
{
ID_LOG(1, "");
write_instances();
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(
prim_id, pxr::HdChangeTracker::AllDirty);
}
pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const 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(); 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<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);
@ -261,29 +131,7 @@ void InstancerData::update_double_sided(MaterialData *mat_data)
} }
} }
pxr::SdfPath InstancerData::object_prim_id(Object *object) const void InstancerData::pre_update()
{
/* Making id of object in form like <prefix>_<pointer in 16 hex digits format> */
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()
{ {
mesh_transforms_.clear(); mesh_transforms_.clear();
for (auto &m_inst : mesh_instances_.values()) { for (auto &m_inst : mesh_instances_.values()) {
@ -292,35 +140,19 @@ void InstancerData::write_instances()
for (auto &l_inst : light_instances_.values()) { for (auto &l_inst : light_instances_.values()) {
l_inst.transforms.clear(); l_inst.transforms.clear();
} }
}
DEGObjectIterSettings s = {0}; void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli)
s.depsgraph = scene_delegate_->depsgraph; {
s.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | if (!ObjectData::is_visible(scene_delegate_, parent_ob, OB_VISIBLE_INSTANCES)) {
DEG_ITER_OBJECT_FLAG_VISIBLE | DEG_ITER_OBJECT_FLAG_DUPLI; return;
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; Object *ob = dupli->ob;
if (!is_instance_supported(ob)) { if (!is_instance_supported(ob)) {
continue; return;
} }
if (!scene_delegate_->shading_settings.use_scene_lights && ob->type == OB_LAMP) { if (!scene_delegate_->shading_settings.use_scene_lights && ob->type == OB_LAMP) {
continue; return;
} }
pxr::SdfPath p_id = object_prim_id(ob); pxr::SdfPath p_id = object_prim_id(ob);
@ -349,10 +181,10 @@ void InstancerData::write_instances()
inst->indices.push_back(mesh_transforms_.size()); inst->indices.push_back(mesh_transforms_.size());
mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat));
} }
} }
ITER_END;
void InstancerData::post_update()
{
/* 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();
@ -362,6 +194,7 @@ void InstancerData::write_instances()
return res; return res;
}); });
/* Insert/remove/update instancer in RenderIndex */
pxr::HdRenderIndex &index = scene_delegate_->GetRenderIndex(); pxr::HdRenderIndex &index = scene_delegate_->GetRenderIndex();
if (mesh_instances_.is_empty()) { if (mesh_instances_.is_empty()) {
if (index.HasInstancer(prim_id)) { if (index.HasInstancer(prim_id)) {
@ -387,6 +220,28 @@ void InstancerData::write_instances()
light_instances_.remove_if([&](auto item) { return item.value.transforms.empty(); }); 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 <prefix>_<pointer in 16 hex digits format> */
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) void InstancerData::update_light_instance(LightInstance &inst)
{ {
auto &render_index = scene_delegate_->GetRenderIndex(); auto &render_index = scene_delegate_->GetRenderIndex();

View File

@ -12,7 +12,7 @@
namespace blender::render::hydra { namespace blender::render::hydra {
class InstancerData : public ObjectData { class InstancerData : public IdData {
struct MeshInstance { struct MeshInstance {
std::unique_ptr<MeshData> data; std::unique_ptr<MeshData> data;
pxr::VtIntArray indices; pxr::VtIntArray indices;
@ -34,12 +34,6 @@ class InstancerData : public ObjectData {
void update() override; void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const 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::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;
@ -48,11 +42,14 @@ class InstancerData : public ObjectData {
void available_materials(Set<pxr::SdfPath> &paths) const; void available_materials(Set<pxr::SdfPath> &paths) const;
void update_double_sided(MaterialData *mat_data); void update_double_sided(MaterialData *mat_data);
void pre_update();

Please add comment why this is done in this way

Please add comment why this is done in this way
void update_instance(Object *parent_ob, DupliObject *dupli);
void post_update();
private: private:
pxr::SdfPath object_prim_id(Object *object) const; pxr::SdfPath object_prim_id(Object *object) const;
pxr::SdfPath light_prim_id(LightInstance const &inst, int index) const; pxr::SdfPath light_prim_id(LightInstance const &inst, int index) const;
int light_prim_id_index(pxr::SdfPath const &id) const; int light_prim_id_index(pxr::SdfPath const &id) const;
void write_instances();
void update_light_instance(LightInstance &inst); void update_light_instance(LightInstance &inst);
MeshInstance *mesh_instance(pxr::SdfPath const &id) const; MeshInstance *mesh_instance(pxr::SdfPath const &id) const;
LightInstance *light_instance(pxr::SdfPath const &id) const; LightInstance *light_instance(pxr::SdfPath const &id) const;