Support several instancers on one object #24

Merged
Bogdan Nagirniak merged 16 commits from BLEN-383 into hydra-render 2023-04-19 02:46:24 +02:00
5 changed files with 53 additions and 70 deletions
Showing only changes of commit f777cf428f - Show all commits

View File

@ -55,9 +55,12 @@ bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id)
pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id) pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id)
{ {
CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText()); CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText());
InstancerData *i_data = instancer_data(prim_id, true); if (prim_id.GetParentPath() == GetDelegateID()) {
return pxr::SdfPath();
}
InstancerData *i_data = instancer_data(prim_id.GetParentPath());
if (i_data) { if (i_data) {
return i_data->instancer_id; return i_data->p_id;
} }
return pxr::SdfPath(); return pxr::SdfPath();
} }
@ -75,7 +78,7 @@ pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &ins
{ {
CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText()); CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText());
InstancerData *i_data = instancer_data(instancer_id); InstancerData *i_data = instancer_data(instancer_id);
return i_data->instance_indices(); return i_data->indices();
} }
pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id) pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id)
@ -381,7 +384,7 @@ pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(
InstancerData *i_data = instancer_data(id); InstancerData *i_data = instancer_data(id);
if (i_data) { if (i_data) {
return i_data->instancer_primvar_descriptors(interpolation); return i_data->primvar_descriptors(interpolation);
} }
return pxr::HdPrimvarDescriptorVector(); return pxr::HdPrimvarDescriptorVector();

View File

@ -26,10 +26,11 @@ class IdData {
virtual pxr::VtValue get_data(pxr::TfToken const &key) const; virtual pxr::VtValue get_data(pxr::TfToken const &key) const;
template<class T> const T get_data(pxr::TfToken const &key) const; template<class T> const T get_data(pxr::TfToken const &key) const;
protected:
BlenderSceneDelegate *scene_delegate;
ID *id; ID *id;
pxr::SdfPath p_id; pxr::SdfPath p_id;
protected:
BlenderSceneDelegate *scene_delegate;
}; };
template<class T> const T IdData::get_data(pxr::TfToken const &key) const template<class T> const T IdData::get_data(pxr::TfToken const &key) const
@ -37,4 +38,7 @@ template<class T> const T IdData::get_data(pxr::TfToken const &key) const
return get_data(key).Get<T>(); return get_data(key).Get<T>();
} }
#define ID_LOG(level, msg, ...) \
CLOG_INFO(LOG_BSD, level, "%s (%s): " msg, p_id.GetText(), id->name, __VA_ARGS__);
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -44,20 +44,16 @@ pxr::SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object
} }
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object) InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object)
: MeshData(scene_delegate, object), parent_obj(object) : ObjectData(scene_delegate, object)
{ {
id = nullptr;
p_id = prim_id(scene_delegate, object); p_id = prim_id(scene_delegate, object);
instancer_id = p_id.AppendElementString("Instancer"); ID_LOG(2, "");
CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj)->name, instancer_id.GetText());
} }
void InstancerData::init() void InstancerData::init()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); ID_LOG(2, "");
set_instances(); set_instances();
MeshData::init();
} }
pxr::GfMatrix4d InstancerData::transform() pxr::GfMatrix4d InstancerData::transform()
@ -71,11 +67,9 @@ bool InstancerData::update_visibility(View3D *view3d)
return false; return false;
} }
bool prev_visible = visible; bool ret = ObjectData::update_visibility(view3d);
visible = BKE_object_is_visible_in_viewport(view3d, parent_obj);
bool ret = visible != prev_visible;
if (ret) { if (ret) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(
p_id, pxr::HdChangeTracker::DirtyVisibility); p_id, pxr::HdChangeTracker::DirtyVisibility);
} }
return ret; return ret;
@ -83,16 +77,15 @@ bool InstancerData::update_visibility(View3D *view3d)
pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const
{ {
CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); ID_LOG(3, "%s", key.GetText());
pxr::VtValue ret;
if (key == pxr::HdInstancerTokens->instanceTransform) { if (key == pxr::HdInstancerTokens->instanceTransform) {
return pxr::VtValue(transforms); ret = transforms;
} }
return MeshData::get_data(key); return ret;
} }
pxr::HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors( pxr::HdPrimvarDescriptorVector InstancerData::primvar_descriptors(pxr::HdInterpolation interpolation)
pxr::HdInterpolation interpolation)
{ {
pxr::HdPrimvarDescriptorVector primvars; pxr::HdPrimvarDescriptorVector primvars;
if (interpolation == pxr::HdInterpolationInstance) { if (interpolation == pxr::HdInterpolationInstance) {
@ -102,7 +95,7 @@ pxr::HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors(
return primvars; return primvars;
} }
pxr::VtIntArray InstancerData::instance_indices() pxr::VtIntArray InstancerData::indices()
{ {
pxr::VtIntArray ret(transforms.size()); pxr::VtIntArray ret(transforms.size());
for (size_t i = 0; i < ret.size(); ++i) { for (size_t i = 0; i < ret.size(); ++i) {
@ -116,78 +109,57 @@ bool InstancerData::is_base(Object *object) const
return (ID *)object == id; return (ID *)object == id;
} }
bool InstancerData::set_instances() void InstancerData::set_instances()
{ {
ID *prev_id = id;
id = nullptr;
transforms.clear(); transforms.clear();
ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, (Object *)id);
LISTBASE_FOREACH (DupliObject *, dupli, lb) { LISTBASE_FOREACH (DupliObject *, dupli, lb) {
if (!id) { if (!m_data) {
/* TODO: We create instances only for object in first dupli. /* TODO: We create instances only for object in first dupli.
Instances should be created for all objects */ Instances should be created for all objects */
id = (ID *)dupli->ob; m_data = ObjectData::create(scene_delegate, dupli->ob);
m_data->p_id = p_id.AppendElementString(m_data->p_id.GetName());
} }
if (id != (ID *)dupli->ob) { if (m_data->id != (ID *)dupli->ob) {
continue; continue;
} }
transforms.push_back(gf_matrix_from_transform(dupli->mat)); transforms.push_back(gf_matrix_from_transform(dupli->mat));
CLOG_INFO( ID_LOG(2, "Instance %s %d", m_data->id->name, dupli->random_id);
LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id);
} }
free_object_duplilist(lb); free_object_duplilist(lb);
return id != prev_id;
} }
void InstancerData::insert() void InstancerData::insert()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); ID_LOG(2, "");
MeshData::insert(); scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, p_id);
m_data->insert();
if (face_vertex_counts.empty()) {
return;
}
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id);
} }
void InstancerData::remove() void InstancerData::remove()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); ID_LOG(2, "");
m_data->remove();
if (!scene_delegate->GetRenderIndex().HasInstancer(instancer_id)) { scene_delegate->GetRenderIndex().RemoveInstancer(p_id);
return;
}
scene_delegate->GetRenderIndex().RemoveInstancer(instancer_id);
MeshData::remove();
} }
void InstancerData::update() void InstancerData::update()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); ID_LOG(2, "");
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
unsigned int recalc = ((ID *)parent_obj)->recalc;
Object *object = (Object *)id; Object *object = (Object *)id;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id, pxr::HdChangeTracker::AllDirty);
return;
}
if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj->data)->recalc & ID_RECALC_GEOMETRY)) {
init(); init();
bits |= pxr::HdChangeTracker::AllDirty; bits |= pxr::HdChangeTracker::AllDirty;
} }
else if (recalc & ID_RECALC_TRANSFORM || id->recalc & ID_RECALC_TRANSFORM) { else if (id->recalc & ID_RECALC_TRANSFORM) {
set_instances(); set_instances();
bits |= pxr::HdChangeTracker::DirtyTransform; bits |= pxr::HdChangeTracker::DirtyTransform;
} }
if (bits != pxr::HdChangeTracker::Clean) { if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits); scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(p_id, bits);
} }
} }

View File

@ -5,11 +5,11 @@
#include "BKE_duplilist.h" #include "BKE_duplilist.h"
#include "mesh.h" #include "object.h"
namespace blender::render::hydra { namespace blender::render::hydra {
class InstancerData : public MeshData { class InstancerData : public ObjectData {
public: public:
static bool supported(Object *object); static bool supported(Object *object);
static std::unique_ptr<InstancerData> create(BlenderSceneDelegate *scene_delegate, static std::unique_ptr<InstancerData> create(BlenderSceneDelegate *scene_delegate,
@ -26,17 +26,17 @@ class InstancerData : public MeshData {
pxr::GfMatrix4d transform() override; pxr::GfMatrix4d transform() override;
bool update_visibility(View3D *view3d) override; bool update_visibility(View3D *view3d) override;
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray instance_indices(); pxr::VtIntArray indices();
bool is_base(Object *object) const; bool is_base(Object *object) const;
pxr::SdfPath instancer_id;
std::unique_ptr<ObjectData> m_data;
private: private:
Object *parent_obj;
pxr::VtMatrix4dArray transforms; pxr::VtMatrix4dArray transforms;
bool set_instances(); void set_instances();
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -84,7 +84,11 @@ bool ObjectData::update_visibility(View3D *view3d)
bool prev_visible = visible; bool prev_visible = visible;
visible = BKE_object_is_visible_in_viewport(view3d, (Object *)id); visible = BKE_object_is_visible_in_viewport(view3d, (Object *)id);
return visible != prev_visible; bool ret = visible != prev_visible;
if (ret) {
ID_LOG(2, "");
}
return ret;
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra