From a9a0e405a28458b2715f88854835179f1a85569b Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 27 Mar 2023 17:46:11 +0300 Subject: [PATCH 01/20] BLEN-359: Implement updates of instances in viewport. Added p_id to IdData. Removed SampleInstancerTransform as not used. --- .../hydra/scene_delegate/blender_scene_delegate.cc | 11 ----------- .../hydra/scene_delegate/blender_scene_delegate.h | 4 ---- source/blender/render/hydra/scene_delegate/id.h | 1 + source/blender/render/hydra/scene_delegate/light.cc | 3 --- .../blender/render/hydra/scene_delegate/material.cc | 4 +--- source/blender/render/hydra/scene_delegate/mesh.cc | 3 --- source/blender/render/hydra/scene_delegate/object.cc | 1 + source/blender/render/hydra/scene_delegate/world.cc | 5 ++--- 8 files changed, 5 insertions(+), 27 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 de574193c48e..bc1d650d40fd 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -114,17 +114,6 @@ pxr::GfMatrix4d BlenderSceneDelegate::get_instancer_transform(pxr::SdfPath const return pxr::GfMatrix4d(1.0); } -size_t BlenderSceneDelegate::SampleInstancerTransform(pxr::SdfPath const &instancer_id, - size_t max_sample_count, - float *sample_times, - pxr::GfMatrix4d *sample_values) -{ - size_t ret = 0; - MeshData *m_data = mesh_data(instancer_id.GetParentPath()); - ret = m_data->sample_instancer_transform(max_sample_count, sample_times, sample_values); - return ret; -} - size_t BlenderSceneDelegate::SamplePrimvar(pxr::SdfPath const &id, pxr::TfToken const &key, size_t max_sample_count, 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 cf03fd74f0c7..0aa1d91af679 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -45,10 +45,6 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancer_id, pxr::SdfPath const &prototype_id) override; pxr::GfMatrix4d get_instancer_transform(pxr::SdfPath const &instancer_id); - size_t SampleInstancerTransform(pxr::SdfPath const &instancer_id, - size_t max_sample_count, - float *sample_times, - pxr::GfMatrix4d *sample_values) override; size_t SamplePrimvar(pxr::SdfPath const &id, pxr::TfToken const &key, size_t max_sample_count, diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 43e50e33d931..38cc07b5fe6a 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -31,6 +31,7 @@ class IdData { protected: BlenderSceneDelegate *scene_delegate; ID *id; + pxr::SdfPath p_id; }; template const T IdData::get_data(pxr::TfToken const &key) diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index ed9a14f76200..ffcb65c487fb 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -135,14 +135,12 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key) void LightData::insert_prim() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } void LightData::remove_prim() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } @@ -165,7 +163,6 @@ void LightData::mark_prim_dirty(DirtyBits dirty_bits) default: break; } - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); CLOG_INFO(LOG_BSD, 2, "Update: [%d] %s", dirty_bits, name().c_str()); } diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index c460b76b651d..67bd42b12433 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -38,6 +38,7 @@ pxr::SdfPath MaterialData::prim_id(BlenderSceneDelegate *scene_delegate, Materia MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material) : IdData(scene_delegate, (ID *)material) { + p_id = prim_id(scene_delegate, material); } pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) @@ -110,7 +111,6 @@ void MaterialData::export_mtlx() void MaterialData::insert_prim() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); scene_delegate->GetRenderIndex().InsertSprim( pxr::HdPrimTypeTokens->material, scene_delegate, p_id); CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); @@ -118,7 +118,6 @@ void MaterialData::insert_prim() void MaterialData::remove_prim() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id); CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } @@ -133,7 +132,6 @@ void MaterialData::mark_prim_dirty(DirtyBits dirty_bits) default: break; } - pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); CLOG_INFO(LOG_BSD, 2, "Update: %s, mtlx=%s", name().c_str(), mtlx_path.GetResolvedPath().c_str()); } diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 09bbe3828089..241d7765ac31 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -206,14 +206,12 @@ void MeshData::insert_prim() return; } - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); scene_delegate->GetRenderIndex().InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate, p_id); CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } void MeshData::remove_prim() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { return; } @@ -224,7 +222,6 @@ void MeshData::remove_prim() void MeshData::mark_prim_dirty(DirtyBits dirty_bits) { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { /* Trying to insert prim */ insert_prim(); diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index f3386f9c153d..11242e721407 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -60,6 +60,7 @@ pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *o ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object) : IdData(scene_delegate, (ID *)object), visible(true) { + p_id = prim_id(scene_delegate, object); } int ObjectData::type() diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 9b559a7d8760..f973e70073c2 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -43,6 +43,8 @@ pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate) WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context) : IdData(scene_delegate, (ID *)world) { + p_id = prim_id(scene_delegate); + data[pxr::UsdLuxTokens->orientToStageUpAxis] = true; if (world->use_nodes) { @@ -125,7 +127,6 @@ pxr::VtValue WorldData::get_data(pxr::TfToken const &key) void WorldData::insert_prim() { - pxr::SdfPath p_id = prim_id(scene_delegate); scene_delegate->GetRenderIndex().InsertSprim( pxr::HdPrimTypeTokens->domeLight, scene_delegate, p_id); CLOG_INFO(LOG_BSD, 2, "Add: id=%s", p_id.GetText()); @@ -133,7 +134,6 @@ void WorldData::insert_prim() void WorldData::remove_prim() { - pxr::SdfPath p_id = prim_id(scene_delegate); scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id); CLOG_INFO(LOG_BSD, 2, "Remove"); } @@ -148,7 +148,6 @@ void WorldData::mark_prim_dirty(DirtyBits dirty_bits) default: break; } - pxr::SdfPath p_id = prim_id(scene_delegate); scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); CLOG_INFO(LOG_BSD, 2, "Update"); } -- 2.30.2 From 1e462a8aff0d02b9425bb0af8ddca7d1908d51d6 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 28 Mar 2023 06:34:02 +0300 Subject: [PATCH 02/20] Refactoring export process --- .../scene_delegate/blender_scene_delegate.cc | 34 +++++++++---------- .../scene_delegate/blender_scene_delegate.h | 2 ++ .../blender/render/hydra/scene_delegate/id.cc | 12 +++++-- .../blender/render/hydra/scene_delegate/id.h | 21 +++++++----- .../render/hydra/scene_delegate/light.cc | 12 ++++--- .../render/hydra/scene_delegate/light.h | 9 ++--- .../render/hydra/scene_delegate/material.cc | 19 ++++++++--- .../render/hydra/scene_delegate/material.h | 12 ++++--- .../render/hydra/scene_delegate/mesh.cc | 16 +++++---- .../render/hydra/scene_delegate/mesh.h | 9 ++--- .../render/hydra/scene_delegate/object.cc | 7 +++- .../render/hydra/scene_delegate/object.h | 4 ++- .../render/hydra/scene_delegate/world.cc | 19 ++++++++--- .../render/hydra/scene_delegate/world.h | 13 ++++--- 14 files changed, 121 insertions(+), 68 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 bc1d650d40fd..d15d7bff778c 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -32,10 +32,10 @@ void BlenderSceneDelegate::set_material(MeshData &mesh_data) pxr::SdfPath id = MaterialData::prim_id(this, material); MaterialData *mat_data = material_data(id); if (!mat_data) { - materials[id] = MaterialData::init(this, material); + materials[id] = MaterialData::create(this, material); mat_data = material_data(id); mat_data->export_mtlx(); - mat_data->insert_prim(); + mat_data->insert(); } mesh_data.material_id = id; } @@ -45,7 +45,7 @@ void BlenderSceneDelegate::update_material(Material *material) MaterialData *mat_data = material_data(MaterialData::prim_id(this, material)); if (mat_data) { mat_data->export_mtlx(); - mat_data->mark_prim_dirty(IdData::DirtyBits::ALL_DIRTY); + mat_data->update(IdData::DirtyBits::ALL_DIRTY); } } @@ -55,17 +55,17 @@ void BlenderSceneDelegate::update_world() World *world = scene->world; if (!world_data) { if (world) { - world_data = WorldData::init(this, world, context); - world_data->insert_prim(); + world_data = WorldData::create(this, world, context); + world_data->insert(); } } else { if (world) { - world_data = WorldData::init(this, world, context); - world_data->mark_prim_dirty(IdData::DirtyBits::ALL_DIRTY); + world_data = WorldData::create(this, world, context); + world_data->update(IdData::DirtyBits::ALL_DIRTY); } else { - world_data->remove_prim(); + world_data->remove(); world_data = nullptr; } } @@ -136,7 +136,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) /* Check and update visibility */ for (auto &obj : objects) { if (obj.second->update_visibility(view3d)) { - obj.second->mark_prim_dirty(IdData::DirtyBits::DIRTY_VISIBILITY); + obj.second->update(IdData::DirtyBits::DIRTY_VISIBILITY); }; } } @@ -186,7 +186,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) if (available_objects.find(it->first) != available_objects.end()) { continue; } - it->second->remove_prim(); + it->second->remove(); objects.erase(it); it = objects.begin(); } @@ -203,7 +203,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) if (available_materials.find(it->first) != available_materials.end()) { continue; } - it->second->remove_prim(); + it->second->remove(); materials.erase(it); it = materials.begin(); } @@ -218,10 +218,10 @@ void BlenderSceneDelegate::add_update_object(Object *object, pxr::SdfPath id = ObjectData::prim_id(this, object); ObjectData *obj_data = object_data(id); if (!obj_data) { - objects[id] = ObjectData::init(this, object); + objects[id] = ObjectData::create(this, object); obj_data = object_data(id); obj_data->update_visibility(view3d); - obj_data->insert_prim(); + obj_data->insert(); MeshData *m_data = dynamic_cast(obj_data); if (m_data) { set_material(*m_data); @@ -230,23 +230,23 @@ void BlenderSceneDelegate::add_update_object(Object *object, } if (geometry) { - objects[id] = ObjectData::init(this, object); + objects[id] = ObjectData::create(this, object); obj_data = object_data(id); obj_data->update_visibility(view3d); MeshData *m_data = dynamic_cast(obj_data); if (m_data) { set_material(*m_data); } - obj_data->mark_prim_dirty(IdData::DirtyBits::ALL_DIRTY); + obj_data->update(IdData::DirtyBits::ALL_DIRTY); return; } if (transform) { - obj_data->mark_prim_dirty(IdData::DirtyBits::DIRTY_TRANSFORM); + obj_data->update(IdData::DirtyBits::DIRTY_TRANSFORM); } if (shading) { - obj_data->mark_prim_dirty(IdData::DirtyBits::DIRTY_MATERIAL); + obj_data->update(IdData::DirtyBits::DIRTY_MATERIAL); } } 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 0aa1d91af679..96989a1877f3 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -20,6 +20,8 @@ namespace blender::render::hydra { extern struct CLG_LogRef *LOG_BSD; /* BSD - Blender Scene Delegate */ class BlenderSceneDelegate : public pxr::HdSceneDelegate { + friend MeshData; + public: enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; diff --git a/source/blender/render/hydra/scene_delegate/id.cc b/source/blender/render/hydra/scene_delegate/id.cc index c630b2f20f4f..09f470750975 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -11,16 +11,24 @@ namespace blender::render::hydra { IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id) : scene_delegate(scene_delegate), id(id) { + //p_id = prim_id(); + //init(); + //insert(); } -std::string IdData::name() +IdData::~IdData() +{ + //remove(); +} + +std::string IdData::name() const { char str[MAX_ID_FULL_NAME]; BKE_id_full_name_get(str, id, 0); return str; } -pxr::VtValue IdData::get_data(pxr::TfToken const &key) +pxr::VtValue IdData::get_data(pxr::TfToken const &key) const { return pxr::VtValue(); } diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 38cc07b5fe6a..e0eff7bd1330 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -16,17 +16,20 @@ class BlenderSceneDelegate; class IdData { public: IdData(BlenderSceneDelegate *scene_delegate, ID *id); - virtual ~IdData() = default; - - std::string name(); - virtual pxr::VtValue get_data(pxr::TfToken const &key); - template const T get_data(pxr::TfToken const &key); + virtual ~IdData(); enum class DirtyBits { DIRTY_TRANSFORM = 1, DIRTY_VISIBILITY, DIRTY_MATERIAL, ALL_DIRTY }; + virtual pxr::SdfPath prim_id() const = 0; + virtual void init() = 0; + virtual void insert() = 0; + virtual void remove() = 0; + virtual void update(DirtyBits dirty_bits) = 0; - virtual void insert_prim() = 0; - virtual void remove_prim() = 0; - virtual void mark_prim_dirty(DirtyBits dirty_bits) = 0; + virtual pxr::VtValue get_data(pxr::TfToken const &key) const; + template const T get_data(pxr::TfToken const &key) const; + + + std::string name() const; protected: BlenderSceneDelegate *scene_delegate; @@ -34,7 +37,7 @@ class IdData { pxr::SdfPath p_id; }; -template const T IdData::get_data(pxr::TfToken const &key) +template const T IdData::get_data(pxr::TfToken const &key) const { return get_data(key).Get(); } diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index ffcb65c487fb..bd35280cb202 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -112,7 +112,7 @@ pxr::TfToken LightData::prim_type() return ret; } -pxr::VtValue LightData::get_data(pxr::TfToken const &key) +pxr::VtValue LightData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; auto it = data.find(key); @@ -133,19 +133,23 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key) return ret; } -void LightData::insert_prim() +void LightData::init() +{ +} + +void LightData::insert() { scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } -void LightData::remove_prim() +void LightData::remove() { scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } -void LightData::mark_prim_dirty(DirtyBits dirty_bits) +void LightData::update(DirtyBits dirty_bits) { /* TODO: prim_type was changed we have to do remove..add light */ diff --git a/source/blender/render/hydra/scene_delegate/light.h b/source/blender/render/hydra/scene_delegate/light.h index ad3223b22769..1eff5b010c7f 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -15,10 +15,11 @@ class LightData : public ObjectData { public: LightData(BlenderSceneDelegate *scene_delegate, Object *object); - pxr::VtValue get_data(pxr::TfToken const &key) override; - void insert_prim() override; - void remove_prim() override; - void mark_prim_dirty(DirtyBits dirty_bits) override; + void init() override; + void insert() override; + void remove() override; + void update(DirtyBits dirty_bits) override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; private: std::map data; diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 67bd42b12433..b2dc02b2cb08 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -20,7 +20,7 @@ namespace blender::render::hydra { -std::unique_ptr MaterialData::init(BlenderSceneDelegate *scene_delegate, +std::unique_ptr MaterialData::create(BlenderSceneDelegate *scene_delegate, Material *material) { return std::make_unique(scene_delegate, material); @@ -41,7 +41,16 @@ MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *mater p_id = prim_id(scene_delegate, material); } -pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) +pxr::SdfPath MaterialData::prim_id() const +{ + return pxr::SdfPath(); +} + +void MaterialData::init() +{ +} + +pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; if (key.GetString() == "MaterialXFilename") { @@ -109,20 +118,20 @@ void MaterialData::export_mtlx() CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", name().c_str(), mtlx_path.GetResolvedPath().c_str()); } -void MaterialData::insert_prim() +void MaterialData::insert() { scene_delegate->GetRenderIndex().InsertSprim( pxr::HdPrimTypeTokens->material, scene_delegate, p_id); CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } -void MaterialData::remove_prim() +void MaterialData::remove() { scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id); CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } -void MaterialData::mark_prim_dirty(DirtyBits dirty_bits) +void MaterialData::update(DirtyBits dirty_bits) { pxr::HdDirtyBits bits = pxr::HdMaterial::Clean; switch (dirty_bits) { diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 5e14f00c9b24..cf0c56affae7 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -15,16 +15,18 @@ namespace blender::render::hydra { class MaterialData : IdData { public: - static std::unique_ptr init(BlenderSceneDelegate *scene_delegate, + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Material *material); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Material *material); MaterialData(BlenderSceneDelegate *scene_delegate, Material *material); - pxr::VtValue get_data(pxr::TfToken const &key) override; - void insert_prim() override; - void remove_prim() override; - void mark_prim_dirty(DirtyBits dirty_bits) override; + pxr::SdfPath prim_id() const override; + void init() override; + void insert() override; + void remove() override; + void update(DirtyBits dirty_bits) override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue material_resource(); void export_mtlx(); diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 241d7765ac31..c598ecdc3b55 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -31,7 +31,7 @@ MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) } } -pxr::VtValue MeshData::get_data(pxr::TfToken const &key) +pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; if (key == pxr::HdTokens->points) { @@ -200,7 +200,11 @@ void MeshData::set_mesh(Mesh *mesh) } } -void MeshData::insert_prim() +void MeshData::init() +{ +} + +void MeshData::insert() { if (face_vertex_counts.empty()) { return; @@ -210,7 +214,7 @@ void MeshData::insert_prim() CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } -void MeshData::remove_prim() +void MeshData::remove() { if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { return; @@ -220,17 +224,17 @@ void MeshData::remove_prim() CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } -void MeshData::mark_prim_dirty(DirtyBits dirty_bits) +void MeshData::update(DirtyBits dirty_bits) { if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { /* Trying to insert prim */ - insert_prim(); + insert(); return; } if (face_vertex_counts.empty()) { /* Remove prim without faces */ - remove_prim(); + remove(); return; } diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 1338e5e599d7..db8173173fb5 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -16,11 +16,12 @@ class MeshData : public ObjectData { public: MeshData(BlenderSceneDelegate *scene_delegate, Object *object); - pxr::VtValue get_data(pxr::TfToken const &key) override; + void init() override; + void insert() override; + void remove() override; + void update(DirtyBits dirty_bits) override; - void insert_prim() override; - void remove_prim() override; - void mark_prim_dirty(DirtyBits dirty_bits) override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; Material *material(); pxr::HdMeshTopology mesh_topology(); diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 11242e721407..0571268156d6 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -28,7 +28,7 @@ bool ObjectData::supported(Object *object) return false; } -std::unique_ptr ObjectData::init(BlenderSceneDelegate *scene_delegate, Object *object) +std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_delegate, Object *object) { switch (object->type) { case OB_MESH: @@ -63,6 +63,11 @@ ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object) p_id = prim_id(scene_delegate, object); } +pxr::SdfPath ObjectData::prim_id() const +{ + return pxr::SdfPath(); +} + int ObjectData::type() { return ((Object *)id)->type; diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index c498f73a2100..b36c767e5d27 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -17,11 +17,13 @@ namespace blender::render::hydra { class ObjectData : public IdData { public: static bool supported(Object *object); - static std::unique_ptr init(BlenderSceneDelegate *scene_delegate, Object *object); + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object); ObjectData(BlenderSceneDelegate *scene_delegate, Object *object); + pxr::SdfPath prim_id() const override; + int type(); pxr::GfMatrix4d transform(); bool update_visibility(View3D *view3d); diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index f973e70073c2..3d75fefb2514 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -28,7 +28,7 @@ namespace blender::render::hydra { -std::unique_ptr WorldData::init(BlenderSceneDelegate *scene_delegate, +std::unique_ptr WorldData::create(BlenderSceneDelegate *scene_delegate, World *world, bContext *context) { @@ -100,6 +100,15 @@ WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContex } } +pxr::SdfPath WorldData::prim_id() const +{ + return pxr::SdfPath(); +} + +void WorldData::init() +{ +} + pxr::GfMatrix4d WorldData::transform() { pxr::GfMatrix4d transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), @@ -115,7 +124,7 @@ pxr::GfMatrix4d WorldData::transform() return transform; } -pxr::VtValue WorldData::get_data(pxr::TfToken const &key) +pxr::VtValue WorldData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; auto it = data.find(key); @@ -125,20 +134,20 @@ pxr::VtValue WorldData::get_data(pxr::TfToken const &key) return ret; } -void WorldData::insert_prim() +void WorldData::insert() { scene_delegate->GetRenderIndex().InsertSprim( pxr::HdPrimTypeTokens->domeLight, scene_delegate, p_id); CLOG_INFO(LOG_BSD, 2, "Add: id=%s", p_id.GetText()); } -void WorldData::remove_prim() +void WorldData::remove() { scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id); CLOG_INFO(LOG_BSD, 2, "Remove"); } -void WorldData::mark_prim_dirty(DirtyBits dirty_bits) +void WorldData::update(DirtyBits dirty_bits) { pxr::HdDirtyBits bits = pxr::HdLight::Clean; switch (dirty_bits) { diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index 5aa170444f80..995115486ec7 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -20,19 +20,22 @@ namespace blender::render::hydra { class WorldData : public IdData { public: - static std::unique_ptr init(BlenderSceneDelegate *scene_delegate, + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate); WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); + pxr::SdfPath prim_id() const override; + void init() override; + void insert() override; + void remove() override; + void update(DirtyBits dirty_bits) override; + pxr::GfMatrix4d transform(); - pxr::VtValue get_data(pxr::TfToken const &key) override; - void insert_prim() override; - void remove_prim() override; - void mark_prim_dirty(DirtyBits dirty_bits) override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; private: std::map data; -- 2.30.2 From b4e44a88e6506ad299d25d9b636fb7bcfd52a21c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 28 Mar 2023 10:56:33 +0300 Subject: [PATCH 03/20] Implementation of new functions --- .../blender/render/hydra/scene_delegate/id.cc | 8 ------ .../blender/render/hydra/scene_delegate/id.h | 4 +-- .../render/hydra/scene_delegate/light.cc | 10 +++++--- .../render/hydra/scene_delegate/material.cc | 10 +++----- .../render/hydra/scene_delegate/material.h | 1 - .../render/hydra/scene_delegate/mesh.cc | 25 +++++++++++-------- .../render/hydra/scene_delegate/object.cc | 18 +++++++------ .../render/hydra/scene_delegate/object.h | 2 -- .../render/hydra/scene_delegate/world.cc | 22 ++++++++-------- .../render/hydra/scene_delegate/world.h | 2 +- 10 files changed, 47 insertions(+), 55 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/id.cc b/source/blender/render/hydra/scene_delegate/id.cc index 09f470750975..ffc6c8660c55 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -11,14 +11,6 @@ namespace blender::render::hydra { IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id) : scene_delegate(scene_delegate), id(id) { - //p_id = prim_id(); - //init(); - //insert(); -} - -IdData::~IdData() -{ - //remove(); } std::string IdData::name() const diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index e0eff7bd1330..8fcc5f811bd4 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -16,10 +16,9 @@ class BlenderSceneDelegate; class IdData { public: IdData(BlenderSceneDelegate *scene_delegate, ID *id); - virtual ~IdData(); + virtual ~IdData() = default; enum class DirtyBits { DIRTY_TRANSFORM = 1, DIRTY_VISIBILITY, DIRTY_MATERIAL, ALL_DIRTY }; - virtual pxr::SdfPath prim_id() const = 0; virtual void init() = 0; virtual void insert() = 0; virtual void remove() = 0; @@ -28,7 +27,6 @@ class IdData { virtual pxr::VtValue get_data(pxr::TfToken const &key) const; template const T get_data(pxr::TfToken const &key) const; - std::string name() const; protected: diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index bd35280cb202..c3158392b129 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -18,6 +18,12 @@ namespace blender::render::hydra { LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object) : ObjectData(scene_delegate, object) { +} + +void LightData::init() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + Light *light = (Light *)((Object *)id)->data; data[pxr::HdLightTokens->intensity] = scene_delegate->engine_type == @@ -133,10 +139,6 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key) const return ret; } -void LightData::init() -{ -} - void LightData::insert() { scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index b2dc02b2cb08..8f1964f5e634 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -23,7 +23,9 @@ namespace blender::render::hydra { std::unique_ptr MaterialData::create(BlenderSceneDelegate *scene_delegate, Material *material) { - return std::make_unique(scene_delegate, material); + auto data = std::make_unique(scene_delegate, material); + data->init(); + return data; } pxr::SdfPath MaterialData::prim_id(BlenderSceneDelegate *scene_delegate, Material *material) @@ -41,13 +43,9 @@ MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *mater p_id = prim_id(scene_delegate, material); } -pxr::SdfPath MaterialData::prim_id() const -{ - return pxr::SdfPath(); -} - void MaterialData::init() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); } pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index cf0c56affae7..87cd20612c30 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -21,7 +21,6 @@ class MaterialData : IdData { MaterialData(BlenderSceneDelegate *scene_delegate, Material *material); - pxr::SdfPath prim_id() const override; void init() override; void insert() override; void remove() override; diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index c598ecdc3b55..1d0c47dc6d26 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -18,17 +18,6 @@ namespace blender::render::hydra { MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) : ObjectData(scene_delegate, object) { - if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && - BLI_listbase_is_empty(&object->modifiers)) { - set_mesh((Mesh *)object->data); - } - else { - Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); - if (mesh) { - set_mesh(mesh); - } - BKE_object_to_mesh_clear(object); - } } pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const @@ -202,6 +191,20 @@ void MeshData::set_mesh(Mesh *mesh) void MeshData::init() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + + Object *object = (Object *)id; + if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && + BLI_listbase_is_empty(&object->modifiers)) { + set_mesh((Mesh *)object->data); + } + else { + Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); + if (mesh) { + set_mesh(mesh); + } + BKE_object_to_mesh_clear(object); + } } void MeshData::insert() diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 0571268156d6..df57f8d9726d 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -30,6 +30,8 @@ bool ObjectData::supported(Object *object) std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_delegate, Object *object) { + std::unique_ptr data; + switch (object->type) { case OB_MESH: case OB_SURF: @@ -37,15 +39,20 @@ std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_deleg case OB_CURVES: case OB_CURVES_LEGACY: case OB_MBALL: - return std::make_unique(scene_delegate, object); + data = std::make_unique(scene_delegate, object); + break; case OB_LAMP: - return std::make_unique(scene_delegate, object); + data = std::make_unique(scene_delegate, object); + break; default: break; } - return nullptr; + if (data) { + data->init(); + } + return data; } pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object) @@ -63,11 +70,6 @@ ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object) p_id = prim_id(scene_delegate, object); } -pxr::SdfPath ObjectData::prim_id() const -{ - return pxr::SdfPath(); -} - int ObjectData::type() { return ((Object *)id)->type; diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index b36c767e5d27..0013f744928b 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -22,8 +22,6 @@ class ObjectData : public IdData { ObjectData(BlenderSceneDelegate *scene_delegate, Object *object); - pxr::SdfPath prim_id() const override; - int type(); pxr::GfMatrix4d transform(); bool update_visibility(View3D *view3d); diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 3d75fefb2514..4ed7e5fe4778 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -32,7 +32,9 @@ std::unique_ptr WorldData::create(BlenderSceneDelegate *scene_delegat World *world, bContext *context) { - return std::make_unique(scene_delegate, world, context); + auto data = std::make_unique(scene_delegate, world, context); + data->init(); + return data; } pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate) @@ -41,9 +43,16 @@ pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate) } WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context) - : IdData(scene_delegate, (ID *)world) + : IdData(scene_delegate, (ID *)world), context(context) { p_id = prim_id(scene_delegate); +} + +void WorldData::init() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + + World *world = (World *)id; data[pxr::UsdLuxTokens->orientToStageUpAxis] = true; @@ -100,15 +109,6 @@ WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContex } } -pxr::SdfPath WorldData::prim_id() const -{ - return pxr::SdfPath(); -} - -void WorldData::init() -{ -} - pxr::GfMatrix4d WorldData::transform() { pxr::GfMatrix4d transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index 995115486ec7..f3ba09594f7e 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -27,7 +27,6 @@ class WorldData : public IdData { WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); - pxr::SdfPath prim_id() const override; void init() override; void insert() override; void remove() override; @@ -39,6 +38,7 @@ class WorldData : public IdData { private: std::map data; + bContext *context; }; } // namespace blender::render::hydra -- 2.30.2 From 42233ebc7a77a75302efbdcc6fa50eddf767de39 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 28 Mar 2023 16:45:07 +0300 Subject: [PATCH 04/20] Adjusted material.cc, world.cc. Fixed searching Surface socket in world nodetree. --- .../scene_delegate/blender_scene_delegate.cc | 5 +-- .../render/hydra/scene_delegate/material.cc | 21 ++++------ .../render/hydra/scene_delegate/world.cc | 42 ++++++++++++------- .../render/hydra/scene_delegate/world.h | 1 + 4 files changed, 37 insertions(+), 32 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 d15d7bff778c..e1563ee0fb3d 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -34,7 +34,6 @@ void BlenderSceneDelegate::set_material(MeshData &mesh_data) if (!mat_data) { materials[id] = MaterialData::create(this, material); mat_data = material_data(id); - mat_data->export_mtlx(); mat_data->insert(); } mesh_data.material_id = id; @@ -44,7 +43,6 @@ void BlenderSceneDelegate::update_material(Material *material) { MaterialData *mat_data = material_data(MaterialData::prim_id(this, material)); if (mat_data) { - mat_data->export_mtlx(); mat_data->update(IdData::DirtyBits::ALL_DIRTY); } } @@ -61,8 +59,7 @@ void BlenderSceneDelegate::update_world() } else { if (world) { - world_data = WorldData::create(this, world, context); - world_data->update(IdData::DirtyBits::ALL_DIRTY); + world_data->update(world); } else { world_data->remove(); diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 8f1964f5e634..80befdbe6907 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -41,11 +41,13 @@ MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *mater : IdData(scene_delegate, (ID *)material) { p_id = prim_id(scene_delegate, material); + CLOG_INFO(LOG_BSD, 2, "%s - %s", id->name, p_id.GetText()); } void MaterialData::init() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); + export_mtlx(); } pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const @@ -72,7 +74,6 @@ pxr::VtValue MaterialData::material_resource() HdMtlxConvertToMaterialNetworkMap( path, shader_source_types, render_contexts, &material_network_map); - CLOG_INFO(LOG_BSD, 3, "%s", path.c_str()); return pxr::VtValue(material_network_map); } return pxr::VtValue(); @@ -118,29 +119,23 @@ void MaterialData::export_mtlx() void MaterialData::insert() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().InsertSprim( pxr::HdPrimTypeTokens->material, scene_delegate, p_id); - CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } void MaterialData::remove() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id); - CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } void MaterialData::update(DirtyBits dirty_bits) { - pxr::HdDirtyBits bits = pxr::HdMaterial::Clean; - switch (dirty_bits) { - case DirtyBits::ALL_DIRTY: - bits = pxr::HdMaterial::AllDirty; - break; - default: - break; - } - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); - CLOG_INFO(LOG_BSD, 2, "Update: %s, mtlx=%s", name().c_str(), mtlx_path.GetResolvedPath().c_str()); + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + mtlx_path = pxr::SdfAssetPath("", ""); + init(); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdMaterial::AllDirty); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 4ed7e5fe4778..5ee5cc37de31 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -46,6 +46,7 @@ WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContex : IdData(scene_delegate, (ID *)world), context(context) { p_id = prim_id(scene_delegate); + CLOG_INFO(LOG_BSD, 2, "%s - %s", id->name, p_id.GetText()); } void WorldData::init() @@ -60,9 +61,20 @@ void WorldData::init() /* TODO: Create nodes parsing system */ bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL); - bNodeSocket input_socket = output_node->input_by_identifier("Surface"); - bNodeLink const *link = input_socket.directly_linked_links()[0]; - if (input_socket.directly_linked_links().is_empty()) { + blender::Span input_sockets = output_node->input_sockets(); + bNodeSocket *input_socket = nullptr; + + for (auto socket : input_sockets) { + if (STREQ(socket->name, "Surface")) { + input_socket = socket; + break; + } + } + if (!input_socket) { + return; + } + bNodeLink const *link = input_socket->directly_linked_links()[0]; + if (input_socket->directly_linked_links().is_empty()) { return; } @@ -136,29 +148,29 @@ pxr::VtValue WorldData::get_data(pxr::TfToken const &key) const void WorldData::insert() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().InsertSprim( pxr::HdPrimTypeTokens->domeLight, scene_delegate, p_id); - CLOG_INFO(LOG_BSD, 2, "Add: id=%s", p_id.GetText()); } void WorldData::remove() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id); - CLOG_INFO(LOG_BSD, 2, "Remove"); } void WorldData::update(DirtyBits dirty_bits) { - pxr::HdDirtyBits bits = pxr::HdLight::Clean; - switch (dirty_bits) { - case DirtyBits::ALL_DIRTY: - bits = pxr::HdLight::AllDirty; - break; - default: - break; - } - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); - CLOG_INFO(LOG_BSD, 2, "Update"); + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + data.clear(); + init(); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdLight::AllDirty); +} + +void WorldData::update(World *world) +{ + id = (ID *)world; + update(DirtyBits::ALL_DIRTY); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index f3ba09594f7e..4d7e1692ebe3 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -31,6 +31,7 @@ class WorldData : public IdData { void insert() override; void remove() override; void update(DirtyBits dirty_bits) override; + void update(World *world); pxr::GfMatrix4d transform(); -- 2.30.2 From 8336945eaf8c11f08c44046a72ffa79cd913baf5 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 28 Mar 2023 18:31:54 +0300 Subject: [PATCH 05/20] Adjusting mesh.cc and light.cc --- .../scene_delegate/blender_scene_delegate.cc | 39 ++++++------ .../render/hydra/scene_delegate/light.cc | 37 ++++++----- .../render/hydra/scene_delegate/light.h | 1 + .../render/hydra/scene_delegate/material.cc | 2 +- .../render/hydra/scene_delegate/mesh.cc | 61 ++++++++++++------- .../render/hydra/scene_delegate/mesh.h | 2 +- .../render/hydra/scene_delegate/object.cc | 5 -- .../render/hydra/scene_delegate/object.h | 3 +- .../render/hydra/scene_delegate/world.cc | 2 +- 9 files changed, 85 insertions(+), 67 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 e1563ee0fb3d..83831e8d5ede 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -219,32 +219,29 @@ void BlenderSceneDelegate::add_update_object(Object *object, obj_data = object_data(id); obj_data->update_visibility(view3d); obj_data->insert(); - MeshData *m_data = dynamic_cast(obj_data); - if (m_data) { - set_material(*m_data); - } return; } - if (geometry) { - objects[id] = ObjectData::create(this, object); - obj_data = object_data(id); - obj_data->update_visibility(view3d); - MeshData *m_data = dynamic_cast(obj_data); - if (m_data) { - set_material(*m_data); - } - obj_data->update(IdData::DirtyBits::ALL_DIRTY); - return; - } + obj_data->update(IdData::DirtyBits::ALL_DIRTY); + //if (geometry) { + // objects[id] = ObjectData::create(this, object); + // obj_data = object_data(id); + // obj_data->update_visibility(view3d); + // MeshData *m_data = dynamic_cast(obj_data); + // if (m_data) { + // set_material(*m_data); + // } + // obj_data->update(IdData::DirtyBits::ALL_DIRTY); + // return; + //} - if (transform) { - obj_data->update(IdData::DirtyBits::DIRTY_TRANSFORM); - } + //if (transform) { + // obj_data->update(IdData::DirtyBits::DIRTY_TRANSFORM); + //} - if (shading) { - obj_data->update(IdData::DirtyBits::DIRTY_MATERIAL); - } + //if (shading) { + // obj_data->update(IdData::DirtyBits::DIRTY_MATERIAL); + //} } void BlenderSceneDelegate::add_update_instance(DupliObject *dupli) diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index c3158392b129..24d1efb74033 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -18,6 +18,7 @@ namespace blender::render::hydra { LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object) : ObjectData(scene_delegate, object) { + CLOG_INFO(LOG_BSD, 2, "%s id=%s", id->name, p_id.GetText()); } void LightData::init() @@ -139,38 +140,44 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key) const return ret; } +bool LightData::update_visibility(View3D *view3d) +{ + bool ret = ObjectData::update_visibility(view3d); + if (ret) { + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, + pxr::HdLight::DirtyParams); + } + return ret; +} + void LightData::insert() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); - CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } void LightData::remove() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); - CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } void LightData::update(DirtyBits dirty_bits) { /* TODO: prim_type was changed we have to do remove..add light */ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + pxr::HdDirtyBits bits = pxr::HdLight::Clean; - switch (dirty_bits) { - case DirtyBits::DIRTY_TRANSFORM: - bits = pxr::HdLight::DirtyTransform; - break; - case DirtyBits::DIRTY_VISIBILITY: - bits = pxr::HdLight::DirtyParams; - break; - case DirtyBits::ALL_DIRTY: - bits = pxr::HdLight::AllDirty; - break; - default: - break; + if (id->recalc & ID_RECALC_GEOMETRY) { + data.clear(); + init(); + bits = pxr::HdLight::AllDirty; + } + else if (id->recalc & ID_RECALC_TRANSFORM) { + bits = pxr::HdLight::DirtyTransform; } scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); - CLOG_INFO(LOG_BSD, 2, "Update: [%d] %s", dirty_bits, name().c_str()); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/light.h b/source/blender/render/hydra/scene_delegate/light.h index 1eff5b010c7f..850247d41671 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -20,6 +20,7 @@ class LightData : public ObjectData { void remove() override; void update(DirtyBits dirty_bits) override; pxr::VtValue get_data(pxr::TfToken const &key) const override; + bool update_visibility(View3D *view3d) override; private: std::map data; diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 80befdbe6907..1f6cef0e8769 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -41,7 +41,7 @@ MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *mater : IdData(scene_delegate, (ID *)material) { p_id = prim_id(scene_delegate, material); - CLOG_INFO(LOG_BSD, 2, "%s - %s", id->name, p_id.GetText()); + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); } void MaterialData::init() diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 1d0c47dc6d26..1b999bbd282c 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -18,6 +18,7 @@ namespace blender::render::hydra { MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) : ObjectData(scene_delegate, object) { + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); } pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const @@ -38,6 +39,16 @@ pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const return ret; } +bool MeshData::update_visibility(View3D *view3d) +{ + bool ret = ObjectData::update_visibility(view3d); + if (ret) { + scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + p_id, pxr::HdChangeTracker::DirtyVisibility); + } + return ret; +} + Material *MeshData::material() { Object *object = (Object *)id; @@ -205,6 +216,8 @@ void MeshData::init() } BKE_object_to_mesh_clear(object); } + + scene_delegate->set_material(*this); } void MeshData::insert() @@ -213,8 +226,8 @@ void MeshData::insert() return; } + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate, p_id); - CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str()); } void MeshData::remove() @@ -223,12 +236,36 @@ void MeshData::remove() return; } + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().RemoveRprim(p_id); - CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); } void MeshData::update(DirtyBits dirty_bits) { + pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; + Object *object = (Object *)id; + if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { + material_id = pxr::SdfPath::EmptyPath(); + instancer_id = pxr::SdfPath::EmptyPath(); + face_vertex_counts.clear(); + face_vertex_indices.clear(); + vertices.clear(); + normals.clear(); + uvs.clear(); + + init(); + bits = pxr::HdChangeTracker::AllDirty; + } + else { + if (id->recalc & ID_RECALC_SHADING) { + scene_delegate->set_material(*this); + bits |= pxr::HdChangeTracker::DirtyMaterialId; + } + if (id->recalc & ID_RECALC_TRANSFORM) { + bits |= pxr::HdChangeTracker::DirtyTransform; + } + } + if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { /* Trying to insert prim */ insert(); @@ -240,26 +277,8 @@ void MeshData::update(DirtyBits dirty_bits) remove(); return; } - - pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - switch (dirty_bits) { - case DirtyBits::DIRTY_TRANSFORM: - bits = pxr::HdChangeTracker::DirtyTransform; - break; - case DirtyBits::DIRTY_VISIBILITY: - bits = pxr::HdChangeTracker::DirtyVisibility; - break; - case DirtyBits::DIRTY_MATERIAL: - bits = pxr::HdChangeTracker::DirtyMaterialId; - break; - case DirtyBits::ALL_DIRTY: - bits = pxr::HdChangeTracker::AllDirty; - break; - default: - break; - } + CLOG_INFO(LOG_BSD, 2, "%s", id->name); scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits); - CLOG_INFO(LOG_BSD, 2, "Update: [%d] %s", dirty_bits, name().c_str()); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index db8173173fb5..9df0f588ed53 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -20,8 +20,8 @@ class MeshData : public ObjectData { void insert() override; void remove() override; void update(DirtyBits dirty_bits) override; - pxr::VtValue get_data(pxr::TfToken const &key) const override; + bool update_visibility(View3D *view3d) override; Material *material(); pxr::HdMeshTopology mesh_topology(); diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index df57f8d9726d..d8df0227fafe 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -70,11 +70,6 @@ ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object) p_id = prim_id(scene_delegate, object); } -int ObjectData::type() -{ - return ((Object *)id)->type; -} - pxr::GfMatrix4d ObjectData::transform() { return gf_matrix_from_transform(((Object *)id)->object_to_world); diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 0013f744928b..9834a03b0fcd 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -22,9 +22,8 @@ class ObjectData : public IdData { ObjectData(BlenderSceneDelegate *scene_delegate, Object *object); - int type(); pxr::GfMatrix4d transform(); - bool update_visibility(View3D *view3d); + virtual bool update_visibility(View3D *view3d); bool visible; }; diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 5ee5cc37de31..abf8e5d7a898 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -46,7 +46,7 @@ WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContex : IdData(scene_delegate, (ID *)world), context(context) { p_id = prim_id(scene_delegate); - CLOG_INFO(LOG_BSD, 2, "%s - %s", id->name, p_id.GetText()); + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); } void WorldData::init() -- 2.30.2 From 3976164cca1c38b779594dfb6412a72f6a64cd71 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 28 Mar 2023 19:08:13 +0300 Subject: [PATCH 06/20] Code cleanup, simplified IdData::update() --- .../scene_delegate/blender_scene_delegate.cc | 29 ++----------------- .../blender/render/hydra/scene_delegate/id.h | 3 +- .../render/hydra/scene_delegate/light.cc | 2 +- .../render/hydra/scene_delegate/light.h | 2 +- .../render/hydra/scene_delegate/material.cc | 3 +- .../render/hydra/scene_delegate/material.h | 2 +- .../render/hydra/scene_delegate/mesh.cc | 2 +- .../render/hydra/scene_delegate/mesh.h | 2 +- .../render/hydra/scene_delegate/object.cc | 1 + .../render/hydra/scene_delegate/world.cc | 5 ++-- .../render/hydra/scene_delegate/world.h | 2 +- 11 files changed, 16 insertions(+), 37 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 83831e8d5ede..28c1c175a69c 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -33,8 +33,6 @@ void BlenderSceneDelegate::set_material(MeshData &mesh_data) MaterialData *mat_data = material_data(id); if (!mat_data) { materials[id] = MaterialData::create(this, material); - mat_data = material_data(id); - mat_data->insert(); } mesh_data.material_id = id; } @@ -43,7 +41,7 @@ void BlenderSceneDelegate::update_material(Material *material) { MaterialData *mat_data = material_data(MaterialData::prim_id(this, material)); if (mat_data) { - mat_data->update(IdData::DirtyBits::ALL_DIRTY); + mat_data->update(); } } @@ -54,7 +52,6 @@ void BlenderSceneDelegate::update_world() if (!world_data) { if (world) { world_data = WorldData::create(this, world, context); - world_data->insert(); } } else { @@ -133,7 +130,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) /* Check and update visibility */ for (auto &obj : objects) { if (obj.second->update_visibility(view3d)) { - obj.second->update(IdData::DirtyBits::DIRTY_VISIBILITY); + obj.second->update(); }; } } @@ -218,30 +215,10 @@ void BlenderSceneDelegate::add_update_object(Object *object, objects[id] = ObjectData::create(this, object); obj_data = object_data(id); obj_data->update_visibility(view3d); - obj_data->insert(); return; } - obj_data->update(IdData::DirtyBits::ALL_DIRTY); - //if (geometry) { - // objects[id] = ObjectData::create(this, object); - // obj_data = object_data(id); - // obj_data->update_visibility(view3d); - // MeshData *m_data = dynamic_cast(obj_data); - // if (m_data) { - // set_material(*m_data); - // } - // obj_data->update(IdData::DirtyBits::ALL_DIRTY); - // return; - //} - - //if (transform) { - // obj_data->update(IdData::DirtyBits::DIRTY_TRANSFORM); - //} - - //if (shading) { - // obj_data->update(IdData::DirtyBits::DIRTY_MATERIAL); - //} + obj_data->update(); } void BlenderSceneDelegate::add_update_instance(DupliObject *dupli) diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 8fcc5f811bd4..f2bb145f4d85 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -18,11 +18,10 @@ class IdData { IdData(BlenderSceneDelegate *scene_delegate, ID *id); virtual ~IdData() = default; - enum class DirtyBits { DIRTY_TRANSFORM = 1, DIRTY_VISIBILITY, DIRTY_MATERIAL, ALL_DIRTY }; virtual void init() = 0; virtual void insert() = 0; virtual void remove() = 0; - virtual void update(DirtyBits dirty_bits) = 0; + virtual void update() = 0; virtual pxr::VtValue get_data(pxr::TfToken const &key) const; template const T get_data(pxr::TfToken const &key) const; diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index 24d1efb74033..e0b57a8a3671 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -162,7 +162,7 @@ void LightData::remove() scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); } -void LightData::update(DirtyBits dirty_bits) +void LightData::update() { /* TODO: prim_type was changed we have to do remove..add light */ diff --git a/source/blender/render/hydra/scene_delegate/light.h b/source/blender/render/hydra/scene_delegate/light.h index 850247d41671..12d1988e80ed 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -18,7 +18,7 @@ class LightData : public ObjectData { void init() override; void insert() override; void remove() override; - void update(DirtyBits dirty_bits) override; + void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; bool update_visibility(View3D *view3d) override; diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 1f6cef0e8769..10a5bda2ba64 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -25,6 +25,7 @@ std::unique_ptr MaterialData::create(BlenderSceneDelegate *scene_d { auto data = std::make_unique(scene_delegate, material); data->init(); + data->insert(); return data; } @@ -130,7 +131,7 @@ void MaterialData::remove() scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id); } -void MaterialData::update(DirtyBits dirty_bits) +void MaterialData::update() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); mtlx_path = pxr::SdfAssetPath("", ""); diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 87cd20612c30..9c42cf57c577 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -24,7 +24,7 @@ class MaterialData : IdData { void init() override; void insert() override; void remove() override; - void update(DirtyBits dirty_bits) override; + void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue material_resource(); diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 1b999bbd282c..41ceb1621430 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -240,7 +240,7 @@ void MeshData::remove() scene_delegate->GetRenderIndex().RemoveRprim(p_id); } -void MeshData::update(DirtyBits dirty_bits) +void MeshData::update() { pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; Object *object = (Object *)id; diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 9df0f588ed53..274cf16a11a7 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -19,7 +19,7 @@ class MeshData : public ObjectData { void init() override; void insert() override; void remove() override; - void update(DirtyBits dirty_bits) override; + void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; bool update_visibility(View3D *view3d) override; diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index d8df0227fafe..58eeb9a0a6d3 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -51,6 +51,7 @@ std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_deleg } if (data) { data->init(); + data->insert(); } return data; } diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index abf8e5d7a898..04c57e6099d9 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -34,6 +34,7 @@ std::unique_ptr WorldData::create(BlenderSceneDelegate *scene_delegat { auto data = std::make_unique(scene_delegate, world, context); data->init(); + data->insert(); return data; } @@ -159,7 +160,7 @@ void WorldData::remove() scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id); } -void WorldData::update(DirtyBits dirty_bits) +void WorldData::update() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); data.clear(); @@ -170,7 +171,7 @@ void WorldData::update(DirtyBits dirty_bits) void WorldData::update(World *world) { id = (ID *)world; - update(DirtyBits::ALL_DIRTY); + update(); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index 4d7e1692ebe3..c7e40b169936 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -30,7 +30,7 @@ class WorldData : public IdData { void init() override; void insert() override; void remove() override; - void update(DirtyBits dirty_bits) override; + void update() override; void update(World *world); pxr::GfMatrix4d transform(); -- 2.30.2 From a26d88dc62fa600e647a0696f146e21874e31e65 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 29 Mar 2023 12:52:15 +0300 Subject: [PATCH 07/20] Moved set_mateial(0 to MeshData --- .../scene_delegate/blender_scene_delegate.cc | 25 +++------- .../scene_delegate/blender_scene_delegate.h | 1 - .../blender/render/hydra/scene_delegate/id.cc | 7 --- .../blender/render/hydra/scene_delegate/id.h | 2 - .../render/hydra/scene_delegate/material.cc | 4 +- .../render/hydra/scene_delegate/material.h | 4 ++ .../render/hydra/scene_delegate/mesh.cc | 47 +++++++++++++------ .../render/hydra/scene_delegate/mesh.h | 8 +++- 8 files changed, 51 insertions(+), 47 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 28c1c175a69c..0f6639df7546 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -22,21 +22,6 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, { } -void BlenderSceneDelegate::set_material(MeshData &mesh_data) -{ - Material *material = mesh_data.material(); - if (!material) { - mesh_data.material_id = pxr::SdfPath::EmptyPath(); - return; - } - pxr::SdfPath id = MaterialData::prim_id(this, material); - MaterialData *mat_data = material_data(id); - if (!mat_data) { - materials[id] = MaterialData::create(this, material); - } - mesh_data.material_id = id; -} - void BlenderSceneDelegate::update_material(Material *material) { MaterialData *mat_data = material_data(MaterialData::prim_id(this, material)); @@ -189,8 +174,12 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) std::set available_materials; for (auto &obj : objects) { MeshData *m_data = dynamic_cast(obj.second.get()); - if (m_data && !m_data->material_id.IsEmpty()) { - available_materials.insert(m_data->material_id); + if (!m_data) { + continue; + } + pxr::SdfPath mat_id = m_data->material_id(); + if (!mat_id.IsEmpty()) { + available_materials.insert(mat_id); } } for (auto it = materials.begin(); it != materials.end(); ++it) { @@ -393,7 +382,7 @@ pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors( pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id) { - return mesh_data(rprim_id)->material_id; + return mesh_data(rprim_id)->material_id(); } pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id) 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 96989a1877f3..6971483b0059 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -63,7 +63,6 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { void add_update_object(Object *object, bool geometry, bool transform, bool shading); void add_update_instance(DupliObject *dupli); - void set_material(MeshData &mesh_data); void update_material(Material *material); void update_world(); void update_collection(bool remove, bool visibility); diff --git a/source/blender/render/hydra/scene_delegate/id.cc b/source/blender/render/hydra/scene_delegate/id.cc index ffc6c8660c55..7c0a28be9de2 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -13,13 +13,6 @@ IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id) { } -std::string IdData::name() const -{ - char str[MAX_ID_FULL_NAME]; - BKE_id_full_name_get(str, id, 0); - return str; -} - pxr::VtValue IdData::get_data(pxr::TfToken const &key) const { return pxr::VtValue(); diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index f2bb145f4d85..8d36f8e2d3c1 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -26,8 +26,6 @@ class IdData { virtual pxr::VtValue get_data(pxr::TfToken const &key) const; template const T get_data(pxr::TfToken const &key) const; - std::string name() const; - protected: BlenderSceneDelegate *scene_delegate; ID *id; diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 10a5bda2ba64..3b1fe60f75e8 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -107,7 +107,7 @@ void MaterialData::export_mtlx() Py_DECREF(result); } else { - CLOG_ERROR(LOG_BSD, "Export error for %s", name().c_str()); + CLOG_ERROR(LOG_BSD, "Export error for %s", id->name); PyErr_Print(); } Py_DECREF(module); @@ -115,7 +115,7 @@ void MaterialData::export_mtlx() PyGILState_Release(gstate); mtlx_path = pxr::SdfAssetPath(path, path); - CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", name().c_str(), mtlx_path.GetResolvedPath().c_str()); + CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", id->name, mtlx_path.GetResolvedPath().c_str()); } void MaterialData::insert() diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 9c42cf57c577..5e2ce6ae4b75 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -13,7 +13,11 @@ namespace blender::render::hydra { +class MeshData; + class MaterialData : IdData { + friend MeshData; + public: static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Material *material); diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 41ceb1621430..9f3e99285615 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -16,7 +16,7 @@ namespace blender::render::hydra { MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) - : ObjectData(scene_delegate, object) + : ObjectData(scene_delegate, object), mat_data(nullptr) { CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); } @@ -49,15 +49,6 @@ bool MeshData::update_visibility(View3D *view3d) return ret; } -Material *MeshData::material() -{ - Object *object = (Object *)id; - if (BKE_object_material_count_eval(object) == 0) { - return nullptr; - } - return BKE_object_material_get_eval(object, object->actcol); -} - pxr::HdMeshTopology MeshData::mesh_topology() { return pxr::HdMeshTopology(pxr::PxOsdOpenSubdivTokens->none, @@ -88,6 +79,14 @@ pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(pxr::HdInterpolatio return primvars; } +pxr::SdfPath MeshData::material_id() +{ + if (!mat_data) { + return pxr::SdfPath(); + } + return mat_data->p_id; +} + pxr::HdPrimvarDescriptorVector MeshData::instancer_primvar_descriptors( pxr::HdInterpolation interpolation) { @@ -137,7 +136,7 @@ void MeshData::add_instance(DupliObject *dupli) if (instancer_id.IsEmpty()) { instancer_id = prim_id(scene_delegate, (Object *)id).AppendElementString("Instancer"); scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id); - CLOG_INFO(LOG_BSD, 2, "Instancer: %s, id=%s", name().c_str(), instancer_id.GetText()); + CLOG_INFO(LOG_BSD, 2, "Instancer: %s, id=%s", id->name, instancer_id.GetText()); } if (instances.empty()) { // USD hides the prototype mesh when instancing in contrary to the Blender, so we must add it @@ -200,6 +199,26 @@ void MeshData::set_mesh(Mesh *mesh) } } +void MeshData::set_material() +{ + Object *object = (Object *)id; + Material *mat = nullptr; + if (BKE_object_material_count_eval(object) > 0) { + mat = BKE_object_material_get_eval(object, object->actcol); + } + + if (!mat) { + mat_data = nullptr; + return; + } + pxr::SdfPath id = MaterialData::prim_id(scene_delegate, mat); + mat_data = scene_delegate->material_data(id); + if (!mat_data) { + scene_delegate->materials[id] = MaterialData::create(scene_delegate, mat); + mat_data = scene_delegate->material_data(id); + } +} + void MeshData::init() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); @@ -217,7 +236,7 @@ void MeshData::init() BKE_object_to_mesh_clear(object); } - scene_delegate->set_material(*this); + set_material(); } void MeshData::insert() @@ -245,7 +264,6 @@ void MeshData::update() pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; Object *object = (Object *)id; if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { - material_id = pxr::SdfPath::EmptyPath(); instancer_id = pxr::SdfPath::EmptyPath(); face_vertex_counts.clear(); face_vertex_indices.clear(); @@ -258,7 +276,7 @@ void MeshData::update() } else { if (id->recalc & ID_RECALC_SHADING) { - scene_delegate->set_material(*this); + set_material(); bits |= pxr::HdChangeTracker::DirtyMaterialId; } if (id->recalc & ID_RECALC_TRANSFORM) { @@ -267,7 +285,6 @@ void MeshData::update() } if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { - /* Trying to insert prim */ insert(); return; } diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 274cf16a11a7..9fa3f0f22dbc 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -9,6 +9,7 @@ #include "BKE_duplilist.h" #include "object.h" +#include "material.h" namespace blender::render::hydra { @@ -23,9 +24,10 @@ class MeshData : public ObjectData { pxr::VtValue get_data(pxr::TfToken const &key) const override; bool update_visibility(View3D *view3d) override; - Material *material(); pxr::HdMeshTopology mesh_topology(); pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); + pxr::SdfPath material_id(); + pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::VtIntArray instance_indices(); size_t sample_instancer_transform(size_t max_sample_count, @@ -38,11 +40,11 @@ class MeshData : public ObjectData { void add_instance(DupliObject *dupli); - pxr::SdfPath material_id; pxr::SdfPath instancer_id; private: void set_mesh(Mesh *mesh); + void set_material(); pxr::VtIntArray face_vertex_counts; pxr::VtIntArray face_vertex_indices; @@ -51,6 +53,8 @@ class MeshData : public ObjectData { pxr::VtVec2fArray uvs; pxr::VtMatrix4dArray instances; + + MaterialData *mat_data; }; } // namespace blender::render::hydra -- 2.30.2 From 3251d36305050f6d5f05cc4b4b0d58d41f60402c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 29 Mar 2023 15:31:35 +0300 Subject: [PATCH 08/20] Improved working with updates. Code simplification --- .../scene_delegate/blender_scene_delegate.cc | 67 ++++++++----------- .../scene_delegate/blender_scene_delegate.h | 3 +- 2 files changed, 29 insertions(+), 41 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 0f6639df7546..c441d4ba4d1f 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ +#include + #include "DEG_depsgraph_query.h" #include "DNA_scene_types.h" @@ -22,14 +24,6 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, { } -void BlenderSceneDelegate::update_material(Material *material) -{ - MaterialData *mat_data = material_data(MaterialData::prim_id(this, material)); - if (mat_data) { - mat_data->update(); - } -} - void BlenderSceneDelegate::update_world() { Scene *scene = DEG_get_input_scene(depsgraph); @@ -154,7 +148,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) } if (!object_data(id)) { - add_update_object(object, true, true, true); + add_update_object(object); } } ITER_END; @@ -193,10 +187,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) } } -void BlenderSceneDelegate::add_update_object(Object *object, - bool geometry, - bool transform, - bool shading) +void BlenderSceneDelegate::add_update_object(Object *object) { pxr::SdfPath id = ObjectData::prim_id(this, object); ObjectData *obj_data = object_data(id); @@ -214,7 +205,7 @@ void BlenderSceneDelegate::add_update_instance(DupliObject *dupli) { pxr::SdfPath id = ObjectData::prim_id(this, dupli->ob); if (!object_data(id)) { - add_update_object(dupli->ob, true, true, true); + add_update_object(dupli->ob); } MeshData *m_data = mesh_data(id); @@ -269,7 +260,6 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) bool do_update_collection = false; bool do_update_visibility = false; bool do_update_world = false; - bool transform, geometry, shading; DEGIDIterData data = {0}; data.graph = depsgraph; @@ -277,11 +267,7 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) ITER_BEGIN ( DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { - transform = (id->recalc & ID_RECALC_TRANSFORM) != 0; - shading = (id->recalc & (ID_RECALC_SHADING | ID_RECALC_ANIMATION)) != 0; - geometry = (id->recalc & ID_RECALC_GEOMETRY) != 0; - - CLOG_INFO(LOG_BSD, 1, "Update: %s [%d%d%d]", id->name, transform, geometry, shading); + CLOG_INFO(LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str()); switch (GS(id->name)) { case ID_OB: { @@ -289,39 +275,42 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) if (!ObjectData::supported(object)) { break; } - geometry |= (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY) != 0; - add_update_object(object, geometry, transform, shading); + add_update_object(object); } break; - case ID_MA: - if (shading) { - Material *material = (Material *)id; - update_material(material); + case ID_MA: { + MaterialData *mat_data = material_data(MaterialData::prim_id(this, (Material *)id)); + if (mat_data) { + mat_data->update(); } - break; + } break; - case ID_GR: - if (transform && geometry) { + //case ID_GR: + // if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { + // do_update_collection = true; + // } + // break; + + case ID_SCE: { + if (id->recalc & ID_RECALC_BASE_FLAGS) { + do_update_visibility = true; + } + if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { do_update_collection = true; } - break; - - case ID_SCE: - if (!geometry && !transform && !shading) { + if (id->recalc & ID_RECALC_AUDIO_VOLUME) { Scene *scene = (Scene *)id; - do_update_visibility = true; - if ((scene->world && !world_data) || (!scene->world && world_data)) { do_update_world = true; } } - break; + } break; - case ID_WO: - if (shading) { + case ID_WO: { + if (id->recalc & ID_RECALC_SHADING) { do_update_world = true; } - break; + } break; default: break; 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 6971483b0059..77618d2b519e 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -61,9 +61,8 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { LightData *light_data(pxr::SdfPath const &id); MaterialData *material_data(pxr::SdfPath const &id); - void add_update_object(Object *object, bool geometry, bool transform, bool shading); + void add_update_object(Object *object); void add_update_instance(DupliObject *dupli); - void update_material(Material *material); void update_world(); void update_collection(bool remove, bool visibility); -- 2.30.2 From 310a758f360be695f4ad2b6cbeecea87f1d4ef5e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 29 Mar 2023 17:46:29 +0300 Subject: [PATCH 09/20] Added instancer.h/cc. Improvements in other objects export. --- source/blender/render/hydra/CMakeLists.txt | 2 + .../render/hydra/scene_delegate/instancer.cc | 157 ++++++++++++++++++ .../render/hydra/scene_delegate/instancer.h | 43 +++++ .../render/hydra/scene_delegate/light.cc | 11 +- .../render/hydra/scene_delegate/material.cc | 64 ++++--- .../render/hydra/scene_delegate/material.h | 1 + .../render/hydra/scene_delegate/mesh.cc | 11 +- .../render/hydra/scene_delegate/mesh.h | 2 +- .../render/hydra/scene_delegate/object.cc | 1 - .../render/hydra/scene_delegate/object.h | 2 +- .../render/hydra/scene_delegate/world.cc | 2 +- 11 files changed, 249 insertions(+), 47 deletions(-) create mode 100644 source/blender/render/hydra/scene_delegate/instancer.cc create mode 100644 source/blender/render/hydra/scene_delegate/instancer.h diff --git a/source/blender/render/hydra/CMakeLists.txt b/source/blender/render/hydra/CMakeLists.txt index 45c518363b0e..fa3c6861fb17 100644 --- a/source/blender/render/hydra/CMakeLists.txt +++ b/source/blender/render/hydra/CMakeLists.txt @@ -85,6 +85,8 @@ set(SRC scene_delegate/light.cc scene_delegate/world.h scene_delegate/world.cc + scene_delegate/instancer.h + scene_delegate/instancer.cc ) set(LIB diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc new file mode 100644 index 000000000000..7413f1bea0ff --- /dev/null +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -0,0 +1,157 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#include + +#include "blender_scene_delegate.h" +#include "instancer.h" +#include "../utils.h" + +using namespace pxr; + +namespace blender::render::hydra { + +bool InstancerData::supported(Object *object) +{ + switch (object->type) { + case OB_MESH: + case OB_SURF: + case OB_FONT: + case OB_CURVES_LEGACY: + case OB_MBALL: + return true; + + default: + break; + } + return false; +} + +std::unique_ptr InstancerData::create(BlenderSceneDelegate *scene_delegate, Object *object) +{ + auto data = std::make_unique(scene_delegate, object); + data->init(); + data->insert(); + return data; +} + +SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object) +{ + /* Making id of instancer in form like I_. Example: + * I_000002073e369608 */ + char str[32]; + snprintf(str, 32, "I_%016llx", (uint64_t)object); + return scene_delegate->GetDelegateID().AppendElementString(str); +} + +InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object) + : MeshData(scene_delegate, object) +{ + p_id = prim_id(scene_delegate, object); + instancer_id = p_id.AppendElementString("Instancer"); +} + +void InstancerData::init() +{ + MeshData::init(); + + /* USD hides the prototype mesh when instancing in contrary to the Blender, + so we must add it back implicitly */ + transforms.clear(); + transforms.push_back(GfMatrix4d(1.0)); +} + +GfMatrix4d InstancerData::transform() +{ + return GfMatrix4d(1.0); +} + +VtValue InstancerData::get_data(TfToken const &key) const +{ + if (key == HdInstancerTokens->instanceTransform) { + return VtValue(transforms); + } + return MeshData::get_data(key); +} + +HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors(HdInterpolation interpolation) +{ + HdPrimvarDescriptorVector primvars; + if (interpolation == HdInterpolationInstance) { + primvars.emplace_back( + HdInstancerTokens->instanceTransform, interpolation, HdPrimvarRoleTokens->none); + } + return primvars; +} + +VtIntArray InstancerData::instance_indices() +{ + VtIntArray ret(transforms.size()); + for (size_t i = 0; i < ret.size(); ++i) { + ret[i] = i; + } + return ret; +} + +size_t InstancerData::sample_instancer_transform(size_t maxSampleCount, + float *sampleTimes, + GfMatrix4d *sampleValues) +{ + *sampleTimes = 0.0f; + *sampleValues = GfMatrix4d(1.0); + return 1; +} + +size_t InstancerData::sample_instancer_primvar(TfToken const &key, + size_t maxSampleCount, + float *sampleTimes, + VtValue *sampleValues) +{ + if (key == HdInstancerTokens->instanceTransform) { + if (maxSampleCount > 0) { + sampleTimes[0] = 0.0f; + sampleValues[0] = transforms; + return 1; + } + } + return 0; +} + +void InstancerData::add_instance(DupliObject *dupli) +{ + CLOG_INFO(LOG_BSD, 2, "%s - %d", id->name, dupli->random_id); + transforms.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat)); +} + +void InstancerData::insert() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + MeshData::insert(); + + if (face_vertex_counts.empty()) { + return; + } + + scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id); +} + +void InstancerData::remove() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + + if (!scene_delegate->GetRenderIndex().HasInstancer(instancer_id)) { + return; + } + scene_delegate->GetRenderIndex().RemoveInstancer(instancer_id); + + MeshData::remove(); +} + +void InstancerData::update() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + + MeshData::update(); +} + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h new file mode 100644 index 000000000000..0c4872e0ea43 --- /dev/null +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#pragma once + +#include "BKE_duplilist.h" + +#include "mesh.h" + +namespace blender::render::hydra { + +class InstancerData: public MeshData { +public: + static bool supported(Object *object); + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Object *object); + static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object); + + InstancerData(BlenderSceneDelegate *scene_delegate, Object *object); + + void init() override; + void insert() override; + void remove() override; + void update() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; + pxr::GfMatrix4d transform() override; + + pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); + pxr::VtIntArray instance_indices(); + size_t sample_instancer_transform(size_t maxSampleCount, float *sampleTimes, pxr::GfMatrix4d *sampleValues); + size_t sample_instancer_primvar(pxr::TfToken const &key, + size_t maxSampleCount, + float *sampleTimes, + pxr::VtValue *sampleValues); + + void add_instance(DupliObject *dupli); + + pxr::SdfPath instancer_id; + + private: + pxr::VtMatrix4dArray transforms; +}; + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index e0b57a8a3671..281e2c8ec5f8 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -26,11 +26,13 @@ void LightData::init() CLOG_INFO(LOG_BSD, 2, "%s", id->name); Light *light = (Light *)((Object *)id)->data; + data.clear(); - data[pxr::HdLightTokens->intensity] = scene_delegate->engine_type == - BlenderSceneDelegate::EngineType::PREVIEW ? - light->energy / 1000 : - light->energy; + float intensity = light->energy; + if (scene_delegate->engine_type == BlenderSceneDelegate::EngineType::PREVIEW) { + intensity *= 0.001; + } + data[pxr::HdLightTokens->intensity] = intensity; data[pxr::HdLightTokens->color] = pxr::GfVec3f(light->r, light->g, light->b); @@ -170,7 +172,6 @@ void LightData::update() pxr::HdDirtyBits bits = pxr::HdLight::Clean; if (id->recalc & ID_RECALC_GEOMETRY) { - data.clear(); init(); bits = pxr::HdLight::AllDirty; } diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 3b1fe60f75e8..42e4706f999c 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -48,40 +48,9 @@ MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *mater void MaterialData::init() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); - export_mtlx(); -} -pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const -{ - pxr::VtValue ret; - if (key.GetString() == "MaterialXFilename") { - if (!mtlx_path.GetResolvedPath().empty()) { - ret = mtlx_path; - } - CLOG_INFO(LOG_BSD, 3, "%s", key.GetText()); - } - return ret; -} + material_network_map = pxr::VtValue(); -pxr::VtValue MaterialData::material_resource() -{ - std::string const &path = mtlx_path.GetResolvedPath(); - if (!path.empty()) { - pxr::HdRenderDelegate *render_delegate = scene_delegate->GetRenderIndex().GetRenderDelegate(); - pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes(); - pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts(); - - pxr::HdMaterialNetworkMap material_network_map; - HdMtlxConvertToMaterialNetworkMap( - path, shader_source_types, render_contexts, &material_network_map); - - return pxr::VtValue(material_network_map); - } - return pxr::VtValue(); -} - -void MaterialData::export_mtlx() -{ /* Call of python function hydra.export_mtlx() */ PyGILState_STATE gstate; @@ -118,6 +87,36 @@ void MaterialData::export_mtlx() CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", id->name, mtlx_path.GetResolvedPath().c_str()); } +pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const +{ + pxr::VtValue ret; + if (key.GetString() == "MaterialXFilename") { + if (!mtlx_path.GetResolvedPath().empty()) { + ret = mtlx_path; + } + CLOG_INFO(LOG_BSD, 3, "%s", key.GetText()); + } + return ret; +} + +pxr::VtValue MaterialData::material_resource() +{ + if (material_network_map.IsEmpty()) { + const std::string &path = mtlx_path.GetResolvedPath(); + if (!path.empty()) { + pxr::HdRenderDelegate *render_delegate = scene_delegate->GetRenderIndex().GetRenderDelegate(); + pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes(); + pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts(); + + pxr::HdMaterialNetworkMap network_map; + HdMtlxConvertToMaterialNetworkMap(path, shader_source_types, render_contexts, &network_map); + + material_network_map = network_map; + } + } + return material_network_map; +} + void MaterialData::insert() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); @@ -134,7 +133,6 @@ void MaterialData::remove() void MaterialData::update() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); - mtlx_path = pxr::SdfAssetPath("", ""); init(); scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdMaterial::AllDirty); } diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 5e2ce6ae4b75..7d147d4ac7eb 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -36,6 +36,7 @@ class MaterialData : IdData { private: pxr::SdfAssetPath mtlx_path; + pxr::VtValue material_network_map; }; using MaterialDataMap = diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 9f3e99285615..ecdbaa3a9ea1 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -149,6 +149,12 @@ void MeshData::add_instance(DupliObject *dupli) void MeshData::set_mesh(Mesh *mesh) { + face_vertex_counts.clear(); + face_vertex_indices.clear(); + vertices.clear(); + normals.clear(); + uvs.clear(); + BKE_mesh_calc_normals_split(mesh); int tris_len = BKE_mesh_runtime_looptri_len(mesh); if (tris_len == 0) { @@ -265,11 +271,6 @@ void MeshData::update() Object *object = (Object *)id; if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { instancer_id = pxr::SdfPath::EmptyPath(); - face_vertex_counts.clear(); - face_vertex_indices.clear(); - vertices.clear(); - normals.clear(); - uvs.clear(); init(); bits = pxr::HdChangeTracker::AllDirty; diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 9fa3f0f22dbc..b5caf92f611b 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -42,7 +42,7 @@ class MeshData : public ObjectData { pxr::SdfPath instancer_id; - private: + protected: void set_mesh(Mesh *mesh); void set_material(); diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 58eeb9a0a6d3..39b9b483b605 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -36,7 +36,6 @@ std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_deleg case OB_MESH: case OB_SURF: case OB_FONT: - case OB_CURVES: case OB_CURVES_LEGACY: case OB_MBALL: data = std::make_unique(scene_delegate, object); diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 9834a03b0fcd..bdc17566ec43 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -22,7 +22,7 @@ class ObjectData : public IdData { ObjectData(BlenderSceneDelegate *scene_delegate, Object *object); - pxr::GfMatrix4d transform(); + virtual pxr::GfMatrix4d transform(); virtual bool update_visibility(View3D *view3d); bool visible; diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 04c57e6099d9..f01469d01850 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -55,6 +55,7 @@ void WorldData::init() CLOG_INFO(LOG_BSD, 2, "%s", id->name); World *world = (World *)id; + data.clear(); data[pxr::UsdLuxTokens->orientToStageUpAxis] = true; @@ -163,7 +164,6 @@ void WorldData::remove() void WorldData::update() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); - data.clear(); init(); scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdLight::AllDirty); } -- 2.30.2 From e591f4cb35fa6fb62153c3766cfbfc56b3747ad1 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 29 Mar 2023 19:32:29 +0300 Subject: [PATCH 10/20] Moving to use InstancerData --- .../scene_delegate/blender_scene_delegate.cc | 85 ++++++++++--------- .../scene_delegate/blender_scene_delegate.h | 6 +- .../render/hydra/scene_delegate/instancer.cc | 14 +-- .../render/hydra/scene_delegate/instancer.h | 1 - .../render/hydra/scene_delegate/mesh.cc | 62 -------------- .../render/hydra/scene_delegate/mesh.h | 14 --- 6 files changed, 54 insertions(+), 128 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 c441d4ba4d1f..cd9125ce599c 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -56,9 +56,9 @@ bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id) pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id) { CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText()); - MeshData *m_data = mesh_data(prim_id); - if (m_data) { - return m_data->instancer_id; + InstancerData *i_data = dynamic_cast(mesh_data(prim_id)); + if (i_data) { + return i_data->instancer_id; } return pxr::SdfPath(); } @@ -75,16 +75,15 @@ pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &ins pxr::SdfPath const &prototype_id) { CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText()); - MeshData *m_data = mesh_data(prototype_id); - pxr::VtIntArray ret = m_data->instance_indices(); - return ret; + InstancerData *i_data = instancer_data(instancer_id); + return i_data->instance_indices(); } -pxr::GfMatrix4d BlenderSceneDelegate::get_instancer_transform(pxr::SdfPath const &instancer_id) +pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id) { - // TODO: add a separate object for instancer for cleaner handling code - // Actual instancer transform is get here - return pxr::GfMatrix4d(1.0); + CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText()); + InstancerData *i_data = instancer_data(instancer_id); + return i_data->transform(); } size_t BlenderSceneDelegate::SamplePrimvar(pxr::SdfPath const &id, @@ -93,14 +92,11 @@ size_t BlenderSceneDelegate::SamplePrimvar(pxr::SdfPath const &id, float *sample_times, pxr::VtValue *sample_values) { - // TODO: add a separate object for instancer for cleaner handling code - if (id.GetName() == "Instancer") { - MeshData *m_data = mesh_data(id.GetParentPath()); - if (m_data) { - return m_data->sample_instancer_primvar(key, max_sample_count, sample_times, sample_values); - } + InstancerData *i_data = instancer_data(id); + if (i_data) { + return i_data->sample_instancer_primvar(key, max_sample_count, sample_times, sample_values); } - return HdSceneDelegate::SamplePrimvar(id, key, max_sample_count, sample_times, sample_values); + return 0; } void BlenderSceneDelegate::update_collection(bool remove, bool visibility) @@ -138,7 +134,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) } if (data.dupli_object_current != nullptr) { - add_update_instance(data.dupli_object_current); + add_update_instancer(data.dupli_object_current); continue; } @@ -201,15 +197,17 @@ void BlenderSceneDelegate::add_update_object(Object *object) obj_data->update(); } -void BlenderSceneDelegate::add_update_instance(DupliObject *dupli) +void BlenderSceneDelegate::add_update_instancer(DupliObject *dupli) { - pxr::SdfPath id = ObjectData::prim_id(this, dupli->ob); - if (!object_data(id)) { - add_update_object(dupli->ob); + pxr::SdfPath id = InstancerData::prim_id(this, dupli->ob); + InstancerData *i_data = (InstancerData *)mesh_data(id); + if (!i_data) { + objects[id] = InstancerData::create(this, dupli->ob); + i_data = (InstancerData *)mesh_data(id); + // i_data->update_visibility(view3d); } - MeshData *m_data = mesh_data(id); - m_data->add_instance(dupli); + i_data->add_instance(dupli); } ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) @@ -223,12 +221,12 @@ ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) MeshData *BlenderSceneDelegate::mesh_data(pxr::SdfPath const &id) { - return static_cast(object_data(id)); + return dynamic_cast(object_data(id)); } LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) { - return static_cast(object_data(id)); + return dynamic_cast(object_data(id)); } MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) @@ -240,6 +238,11 @@ MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) return it->second.get(); } +InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id) +{ + return dynamic_cast(object_data(id.GetParentPath())); +} + void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) { bool is_populated = depsgraph != nullptr; @@ -336,11 +339,8 @@ pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) { CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText()); + ObjectData *obj_data = object_data(id); - // TODO: add a separate object for instancer for cleaner handling code - if (!obj_data && id.GetName() == "Instancer") { - obj_data = object_data(id.GetParentPath()); - } if (obj_data) { return obj_data->get_data(key); } @@ -349,6 +349,12 @@ pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken cons if (mat_data) { return mat_data->get_data(key); } + + InstancerData *i_data = instancer_data(id); + if (i_data) { + return i_data->get_data(key); + } + return pxr::VtValue(); } @@ -356,17 +362,18 @@ pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors( pxr::SdfPath const &id, pxr::HdInterpolation interpolation) { CLOG_INFO(LOG_BSD, 3, "%s, %d", id.GetText(), interpolation); - if (mesh_data(id)) { - return mesh_data(id)->primvar_descriptors(interpolation); + + MeshData *m_data = mesh_data(id); + if (m_data) { + return m_data->primvar_descriptors(interpolation); } - // TODO: add a separate object for instancer for cleaner handling code - else if (id.GetName() == "Instancer") { - if (MeshData *data = mesh_data(id.GetParentPath())) { - return data->instancer_primvar_descriptors(interpolation); - } + + InstancerData *i_data = instancer_data(id); + if (i_data) { + return i_data->instancer_primvar_descriptors(interpolation); } - pxr::HdPrimvarDescriptorVector primvars; - return primvars; + + return pxr::HdPrimvarDescriptorVector(); } pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id) 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 77618d2b519e..e746cd14aa87 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -14,6 +14,7 @@ #include "mesh.h" #include "object.h" #include "world.h" +#include "instancer.h" namespace blender::render::hydra { @@ -46,7 +47,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { pxr::SdfPathVector GetInstancerPrototypes(pxr::SdfPath const &instancer_id) override; pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancer_id, pxr::SdfPath const &prototype_id) override; - pxr::GfMatrix4d get_instancer_transform(pxr::SdfPath const &instancer_id); + pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancer_id) override; size_t SamplePrimvar(pxr::SdfPath const &id, pxr::TfToken const &key, size_t max_sample_count, @@ -60,9 +61,10 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { MeshData *mesh_data(pxr::SdfPath const &id); LightData *light_data(pxr::SdfPath const &id); MaterialData *material_data(pxr::SdfPath const &id); + InstancerData *instancer_data(pxr::SdfPath const &id); void add_update_object(Object *object); - void add_update_instance(DupliObject *dupli); + void add_update_instancer(DupliObject *dupli); void update_world(); void update_collection(bool remove, bool visibility); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 7413f1bea0ff..35452e4a67c1 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -49,10 +49,12 @@ InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *objec { p_id = prim_id(scene_delegate, object); instancer_id = p_id.AppendElementString("Instancer"); + CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", id->name, instancer_id.GetText()); } void InstancerData::init() { + CLOG_INFO(LOG_BSD, 2, "%s", id->name); MeshData::init(); /* USD hides the prototype mesh when instancing in contrary to the Blender, @@ -93,20 +95,12 @@ VtIntArray InstancerData::instance_indices() return ret; } -size_t InstancerData::sample_instancer_transform(size_t maxSampleCount, - float *sampleTimes, - GfMatrix4d *sampleValues) -{ - *sampleTimes = 0.0f; - *sampleValues = GfMatrix4d(1.0); - return 1; -} - size_t InstancerData::sample_instancer_primvar(TfToken const &key, size_t maxSampleCount, float *sampleTimes, VtValue *sampleValues) { + CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); if (key == HdInstancerTokens->instanceTransform) { if (maxSampleCount > 0) { sampleTimes[0] = 0.0f; @@ -119,7 +113,7 @@ size_t InstancerData::sample_instancer_primvar(TfToken const &key, void InstancerData::add_instance(DupliObject *dupli) { - CLOG_INFO(LOG_BSD, 2, "%s - %d", id->name, dupli->random_id); + CLOG_INFO(LOG_BSD, 2, "%s [%d]", id->name, dupli->random_id); transforms.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat)); } diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 0c4872e0ea43..3da474a2a7b3 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -26,7 +26,6 @@ public: pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::VtIntArray instance_indices(); - size_t sample_instancer_transform(size_t maxSampleCount, float *sampleTimes, pxr::GfMatrix4d *sampleValues); size_t sample_instancer_primvar(pxr::TfToken const &key, size_t maxSampleCount, float *sampleTimes, diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index ecdbaa3a9ea1..03226e6cbf6d 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -87,66 +87,6 @@ pxr::SdfPath MeshData::material_id() return mat_data->p_id; } -pxr::HdPrimvarDescriptorVector MeshData::instancer_primvar_descriptors( - pxr::HdInterpolation interpolation) -{ - pxr::HdPrimvarDescriptorVector primvars; - if (interpolation == pxr::HdInterpolationInstance) { - primvars.emplace_back( - pxr::HdInstancerTokens->instanceTransform, interpolation, pxr::HdPrimvarRoleTokens->none); - } - return primvars; -} - -pxr::VtIntArray MeshData::instance_indices() -{ - pxr::VtIntArray ret(instances.size()); - for (size_t i = 0; i < ret.size(); ++i) { - ret[i] = i; - } - return ret; -} - -size_t MeshData::sample_instancer_transform(size_t max_sample_count, - float *sample_times, - pxr::GfMatrix4d *sample_values) -{ - *sample_times = 0.0f; - *sample_values = pxr::GfMatrix4d(1.0); - return 1; -} - -size_t MeshData::sample_instancer_primvar(pxr::TfToken const &key, - size_t max_sample_count, - float *sample_times, - pxr::VtValue *sample_values) -{ - if (key == pxr::HdInstancerTokens->instanceTransform) { - if (max_sample_count > 0) { - sample_times[0] = 0.0f; - sample_values[0] = instances; - return 1; - } - } - return 0; -} - -void MeshData::add_instance(DupliObject *dupli) -{ - if (instancer_id.IsEmpty()) { - instancer_id = prim_id(scene_delegate, (Object *)id).AppendElementString("Instancer"); - scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id); - CLOG_INFO(LOG_BSD, 2, "Instancer: %s, id=%s", id->name, instancer_id.GetText()); - } - if (instances.empty()) { - // USD hides the prototype mesh when instancing in contrary to the Blender, so we must add it - // back implicitly - instances.push_back(pxr::GfMatrix4d(1.0)); - } - instances.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat)); - CLOG_INFO(LOG_BSD, 2, "%s - %d", instancer_id.GetText(), dupli->random_id); -} - void MeshData::set_mesh(Mesh *mesh) { face_vertex_counts.clear(); @@ -270,8 +210,6 @@ void MeshData::update() pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; Object *object = (Object *)id; if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { - instancer_id = pxr::SdfPath::EmptyPath(); - init(); bits = pxr::HdChangeTracker::AllDirty; } diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index b5caf92f611b..4d06a4a138cb 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -28,20 +28,6 @@ class MeshData : public ObjectData { pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); pxr::SdfPath material_id(); - pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); - pxr::VtIntArray instance_indices(); - size_t sample_instancer_transform(size_t max_sample_count, - float *sample_times, - pxr::GfMatrix4d *sample_values); - size_t sample_instancer_primvar(pxr::TfToken const &key, - size_t max_sample_count, - float *sample_times, - pxr::VtValue *sample_values); - - void add_instance(DupliObject *dupli); - - pxr::SdfPath instancer_id; - protected: void set_mesh(Mesh *mesh); void set_material(); -- 2.30.2 From 54b843eb6d960ffb676e9f050314d0673da35de0 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 29 Mar 2023 20:49:07 +0300 Subject: [PATCH 11/20] Fixing instances transforms --- .../scene_delegate/blender_scene_delegate.cc | 9 +++++--- .../scene_delegate/blender_scene_delegate.h | 2 +- .../render/hydra/scene_delegate/instancer.cc | 22 ++++++++++++++----- .../render/hydra/scene_delegate/instancer.h | 1 + 4 files changed, 24 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 cd9125ce599c..fcb312e1c465 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -200,10 +200,10 @@ void BlenderSceneDelegate::add_update_object(Object *object) void BlenderSceneDelegate::add_update_instancer(DupliObject *dupli) { pxr::SdfPath id = InstancerData::prim_id(this, dupli->ob); - InstancerData *i_data = (InstancerData *)mesh_data(id); + InstancerData *i_data = instancer_data(id, true); if (!i_data) { objects[id] = InstancerData::create(this, dupli->ob); - i_data = (InstancerData *)mesh_data(id); + i_data = instancer_data(id, true); // i_data->update_visibility(view3d); } @@ -238,8 +238,11 @@ MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) return it->second.get(); } -InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id) +InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool base_mesh) { + if (base_mesh) { + return dynamic_cast(object_data(id)); + } return dynamic_cast(object_data(id.GetParentPath())); } 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 e746cd14aa87..f398621879bf 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -61,7 +61,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { MeshData *mesh_data(pxr::SdfPath const &id); LightData *light_data(pxr::SdfPath const &id); MaterialData *material_data(pxr::SdfPath const &id); - InstancerData *instancer_data(pxr::SdfPath const &id); + InstancerData *instancer_data(pxr::SdfPath const &id, bool base_mesh=false); void add_update_object(Object *object); void add_update_instancer(DupliObject *dupli); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 35452e4a67c1..feb7c9a80d7b 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -57,26 +57,36 @@ void InstancerData::init() CLOG_INFO(LOG_BSD, 2, "%s", id->name); MeshData::init(); - /* USD hides the prototype mesh when instancing in contrary to the Blender, - so we must add it back implicitly */ transforms.clear(); - transforms.push_back(GfMatrix4d(1.0)); } GfMatrix4d InstancerData::transform() { - return GfMatrix4d(1.0); + /* transform of the first instance */ + return transforms[0]; } VtValue InstancerData::get_data(TfToken const &key) const { + CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); + if (key == HdInstancerTokens->instanceTransform) { + pxr::VtMatrix4dArray t = transforms; + /* USD hides the prototype mesh when instancing in contrary to the Blender, + so we must add it back implicitly */ + t[0] = pxr::GfMatrix4d(1.0); return VtValue(transforms); } return MeshData::get_data(key); } -HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors(HdInterpolation interpolation) +pxr::GfMatrix4d InstancerData::instancer_transform() +{ + return pxr::GfMatrix4d(1.0); +} + +HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors( + HdInterpolation interpolation) { HdPrimvarDescriptorVector primvars; if (interpolation == HdInterpolationInstance) { @@ -114,7 +124,7 @@ size_t InstancerData::sample_instancer_primvar(TfToken const &key, void InstancerData::add_instance(DupliObject *dupli) { CLOG_INFO(LOG_BSD, 2, "%s [%d]", id->name, dupli->random_id); - transforms.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat)); + transforms.push_back(/*ObjectData::transform().GetInverse() **/ gf_matrix_from_transform(dupli->mat)); } void InstancerData::insert() diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 3da474a2a7b3..7304406db270 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -24,6 +24,7 @@ public: pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::GfMatrix4d transform() override; + pxr::GfMatrix4d instancer_transform(); pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::VtIntArray instance_indices(); size_t sample_instancer_primvar(pxr::TfToken const &key, -- 2.30.2 From aa3c52a37525aa25d0def4330467fd0bcd7a4c33 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 30 Mar 2023 12:14:58 +0300 Subject: [PATCH 12/20] Continuing --- .../scene_delegate/blender_scene_delegate.cc | 5 ++- .../render/hydra/scene_delegate/instancer.cc | 38 +++++++++++++++---- .../render/hydra/scene_delegate/instancer.h | 4 +- 3 files changed, 37 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 fcb312e1c465..4be45b5b3b9a 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -134,6 +134,9 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) } if (data.dupli_object_current != nullptr) { + if (!InstancerData::supported(data.dupli_object_current->ob)) { + continue; + } add_update_instancer(data.dupli_object_current); continue; } @@ -204,7 +207,7 @@ void BlenderSceneDelegate::add_update_instancer(DupliObject *dupli) if (!i_data) { objects[id] = InstancerData::create(this, dupli->ob); i_data = instancer_data(id, true); - // i_data->update_visibility(view3d); + i_data->update_visibility(view3d); } i_data->add_instance(dupli); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index feb7c9a80d7b..904cacc704f8 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -66,16 +66,28 @@ GfMatrix4d InstancerData::transform() return transforms[0]; } +bool InstancerData::update_visibility(View3D *view3d) +{ + if (!view3d) { + return false; + } + + Object *obj = (Object *)id; + if (!obj->parent) { + return false; + } + + bool prev_visible = visible; + visible = BKE_object_is_visible_in_viewport(view3d, obj->parent); + return visible != prev_visible; +} + VtValue InstancerData::get_data(TfToken const &key) const { CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); if (key == HdInstancerTokens->instanceTransform) { - pxr::VtMatrix4dArray t = transforms; - /* USD hides the prototype mesh when instancing in contrary to the Blender, - so we must add it back implicitly */ - t[0] = pxr::GfMatrix4d(1.0); - return VtValue(transforms); + return VtValue(calc_instance_transforms()); } return MeshData::get_data(key); } @@ -124,7 +136,7 @@ size_t InstancerData::sample_instancer_primvar(TfToken const &key, void InstancerData::add_instance(DupliObject *dupli) { CLOG_INFO(LOG_BSD, 2, "%s [%d]", id->name, dupli->random_id); - transforms.push_back(/*ObjectData::transform().GetInverse() **/ gf_matrix_from_transform(dupli->mat)); + transforms.push_back(gf_matrix_from_transform(dupli->mat)); } void InstancerData::insert() @@ -135,7 +147,6 @@ void InstancerData::insert() if (face_vertex_counts.empty()) { return; } - scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id); } @@ -154,8 +165,19 @@ void InstancerData::remove() void InstancerData::update() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); - MeshData::update(); } +pxr::VtMatrix4dArray InstancerData::calc_instance_transforms() const +{ + pxr::VtMatrix4dArray ret; + /* USD hides the prototype mesh when instancing in contrary to the Blender, + so we must add it back implicitly */ + ret.push_back(pxr::GfMatrix4d(1.0)); + for (size_t i = 1; i < transforms.size(); ++i) { + ret.push_back(transforms[0].GetInverse() * transforms[i]); + } + return ret; +} + } // 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 7304406db270..21450a4ab2e3 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -12,7 +12,7 @@ namespace blender::render::hydra { class InstancerData: public MeshData { public: static bool supported(Object *object); - static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Object *object); + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object); InstancerData(BlenderSceneDelegate *scene_delegate, Object *object); @@ -23,6 +23,7 @@ public: void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::GfMatrix4d transform() override; + bool update_visibility(View3D *view3d) override; pxr::GfMatrix4d instancer_transform(); pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); @@ -37,6 +38,7 @@ public: pxr::SdfPath instancer_id; private: + pxr::VtMatrix4dArray calc_instance_transforms() const; pxr::VtMatrix4dArray transforms; }; -- 2.30.2 From 42592f2356578ddfbc54aeac189e893396163a09 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 30 Mar 2023 19:32:29 +0300 Subject: [PATCH 13/20] Fixed positioning of instances. Simplified code. --- .../scene_delegate/blender_scene_delegate.cc | 32 +++++----------- .../scene_delegate/blender_scene_delegate.h | 7 +--- .../render/hydra/scene_delegate/instancer.cc | 38 ++----------------- .../render/hydra/scene_delegate/instancer.h | 6 --- 4 files changed, 14 insertions(+), 69 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 4be45b5b3b9a..1ac9dd1d8e72 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -56,7 +56,7 @@ bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id) pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id) { CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText()); - InstancerData *i_data = dynamic_cast(mesh_data(prim_id)); + InstancerData *i_data = instancer_data(prim_id, true); if (i_data) { return i_data->instancer_id; } @@ -86,19 +86,6 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const & return i_data->transform(); } -size_t BlenderSceneDelegate::SamplePrimvar(pxr::SdfPath const &id, - pxr::TfToken const &key, - size_t max_sample_count, - float *sample_times, - pxr::VtValue *sample_values) -{ - InstancerData *i_data = instancer_data(id); - if (i_data) { - return i_data->sample_instancer_primvar(key, max_sample_count, sample_times, sample_values); - } - return 0; -} - void BlenderSceneDelegate::update_collection(bool remove, bool visibility) { if (visibility) { @@ -241,9 +228,9 @@ MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) return it->second.get(); } -InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool base_mesh) +InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool base_prim) { - if (base_mesh) { + if (base_prim) { return dynamic_cast(object_data(id)); } return dynamic_cast(object_data(id.GetParentPath())); @@ -403,15 +390,16 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id) if (obj_data) { return obj_data->transform(); } - // TODO: add a separate object for instancer for cleaner handling code - else if (id.GetName() == "Instancer") { - if (MeshData *mesh = mesh_data(id.GetParentPath())) { - return mesh->transform().GetInverse(); - } - } + if (id == WorldData::prim_id(this)) { return world_data->transform(); } + + InstancerData *i_data = instancer_data(id); + if (i_data) { + return i_data->transform(); + } + return pxr::GfMatrix4d(); } 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 f398621879bf..84de32f31996 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -48,11 +48,6 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancer_id, pxr::SdfPath const &prototype_id) override; pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancer_id) override; - size_t SamplePrimvar(pxr::SdfPath const &id, - pxr::TfToken const &key, - size_t max_sample_count, - float *sample_times, - pxr::VtValue *sample_values) override; EngineType engine_type; @@ -61,7 +56,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { MeshData *mesh_data(pxr::SdfPath const &id); LightData *light_data(pxr::SdfPath const &id); MaterialData *material_data(pxr::SdfPath const &id); - InstancerData *instancer_data(pxr::SdfPath const &id, bool base_mesh=false); + InstancerData *instancer_data(pxr::SdfPath const &id, bool base_prim=false); void add_update_object(Object *object); void add_update_instancer(DupliObject *dupli); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 904cacc704f8..8747049fdf14 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -62,8 +62,9 @@ void InstancerData::init() GfMatrix4d InstancerData::transform() { + return pxr::GfMatrix4d(1.0); /* transform of the first instance */ - return transforms[0]; + //return transforms[0]; } bool InstancerData::update_visibility(View3D *view3d) @@ -87,16 +88,11 @@ VtValue InstancerData::get_data(TfToken const &key) const CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); if (key == HdInstancerTokens->instanceTransform) { - return VtValue(calc_instance_transforms()); + return VtValue(transforms); } return MeshData::get_data(key); } -pxr::GfMatrix4d InstancerData::instancer_transform() -{ - return pxr::GfMatrix4d(1.0); -} - HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors( HdInterpolation interpolation) { @@ -117,22 +113,6 @@ VtIntArray InstancerData::instance_indices() return ret; } -size_t InstancerData::sample_instancer_primvar(TfToken const &key, - size_t maxSampleCount, - float *sampleTimes, - VtValue *sampleValues) -{ - CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); - if (key == HdInstancerTokens->instanceTransform) { - if (maxSampleCount > 0) { - sampleTimes[0] = 0.0f; - sampleValues[0] = transforms; - return 1; - } - } - return 0; -} - void InstancerData::add_instance(DupliObject *dupli) { CLOG_INFO(LOG_BSD, 2, "%s [%d]", id->name, dupli->random_id); @@ -168,16 +148,4 @@ void InstancerData::update() MeshData::update(); } -pxr::VtMatrix4dArray InstancerData::calc_instance_transforms() const -{ - pxr::VtMatrix4dArray ret; - /* USD hides the prototype mesh when instancing in contrary to the Blender, - so we must add it back implicitly */ - ret.push_back(pxr::GfMatrix4d(1.0)); - for (size_t i = 1; i < transforms.size(); ++i) { - ret.push_back(transforms[0].GetInverse() * transforms[i]); - } - return ret; -} - } // 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 21450a4ab2e3..5439cc023435 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -25,20 +25,14 @@ public: pxr::GfMatrix4d transform() override; bool update_visibility(View3D *view3d) override; - pxr::GfMatrix4d instancer_transform(); pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::VtIntArray instance_indices(); - size_t sample_instancer_primvar(pxr::TfToken const &key, - size_t maxSampleCount, - float *sampleTimes, - pxr::VtValue *sampleValues); void add_instance(DupliObject *dupli); pxr::SdfPath instancer_id; private: - pxr::VtMatrix4dArray calc_instance_transforms() const; pxr::VtMatrix4dArray transforms; }; -- 2.30.2 From 73200fac43467b1dc75bfe0a8bbd4efe8eec353b Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 30 Mar 2023 20:52:01 +0300 Subject: [PATCH 14/20] Moved to use parent object in InstancerData. Formatted code --- source/blender/render/hydra/python.cc | 3 +- .../scene_delegate/blender_scene_delegate.cc | 54 +++++++++---------- .../scene_delegate/blender_scene_delegate.h | 10 ++-- .../render/hydra/scene_delegate/instancer.cc | 38 ++++++------- .../render/hydra/scene_delegate/instancer.h | 12 ++--- .../render/hydra/scene_delegate/light.cc | 2 +- .../render/hydra/scene_delegate/material.cc | 8 +-- .../render/hydra/scene_delegate/material.h | 2 +- .../render/hydra/scene_delegate/mesh.h | 2 +- .../render/hydra/scene_delegate/object.cc | 3 +- .../render/hydra/scene_delegate/world.cc | 4 +- .../render/hydra/scene_delegate/world.h | 4 +- 12 files changed, 70 insertions(+), 72 deletions(-) diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 9401a76a8f91..858e00b3ba3b 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -106,7 +106,6 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args) } RenderEngine *bl_engine = (RenderEngine *)PyLong_AsVoidPtr(pyengine); - Engine *engine; if (STREQ(engine_type, "VIEWPORT")) { @@ -136,7 +135,7 @@ static PyObject *engine_free_func(PyObject * /*self*/, PyObject *args) Py_RETURN_NONE; } - Engine *engine = (Engine *) PyLong_AsVoidPtr(pyengine); + Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); delete engine; CLOG_INFO(LOG_EN, 2, "Engine %016llx", engine); 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 1ac9dd1d8e72..6e080b3e681c 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -26,7 +26,6 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, void BlenderSceneDelegate::update_world() { - Scene *scene = DEG_get_input_scene(depsgraph); World *world = scene->world; if (!world_data) { if (world) { @@ -104,7 +103,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) DEGObjectIterSettings settings = {0}; settings.depsgraph = depsgraph; settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | - DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | DEG_ITER_OBJECT_FLAG_DUPLI; + DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; DEGObjectIterData data = {0}; data.settings = &settings; data.graph = settings.depsgraph; @@ -116,16 +115,12 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) Object *, object) { + CLOG_INFO(LOG_BSD, 2, "Add %s", ((ID *)object)->name); if (!ObjectData::supported(object)) { continue; } - - if (data.dupli_object_current != nullptr) { - if (!InstancerData::supported(data.dupli_object_current->ob)) { - continue; - } - add_update_instancer(data.dupli_object_current); - continue; + if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) { + add_update_instancer(object); } id = ObjectData::prim_id(this, object); @@ -177,27 +172,24 @@ void BlenderSceneDelegate::add_update_object(Object *object) { pxr::SdfPath id = ObjectData::prim_id(this, object); ObjectData *obj_data = object_data(id); - if (!obj_data) { - objects[id] = ObjectData::create(this, object); - obj_data = object_data(id); - obj_data->update_visibility(view3d); - return; + if (obj_data) { + obj_data->update(); } - - obj_data->update(); + objects[id] = ObjectData::create(this, object); + obj_data = object_data(id); + obj_data->update_visibility(view3d); } -void BlenderSceneDelegate::add_update_instancer(DupliObject *dupli) +void BlenderSceneDelegate::add_update_instancer(Object *object) { - pxr::SdfPath id = InstancerData::prim_id(this, dupli->ob); + pxr::SdfPath id = InstancerData::prim_id(this, object); InstancerData *i_data = instancer_data(id, true); - if (!i_data) { - objects[id] = InstancerData::create(this, dupli->ob); - i_data = instancer_data(id, true); - i_data->update_visibility(view3d); + if (i_data) { + i_data->update(); } - - i_data->add_instance(dupli); + objects[id] = InstancerData::create(this, object); + i_data = instancer_data(id, true); + i_data->update_visibility(view3d); } ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) @@ -242,6 +234,7 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) depsgraph = deps; context = cont; + scene = DEG_get_input_scene(depsgraph); view3d = CTX_wm_view3d(context); if (!is_populated) { @@ -263,7 +256,8 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) ITER_BEGIN ( DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { - CLOG_INFO(LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str()); + CLOG_INFO( + LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str()); switch (GS(id->name)) { case ID_OB: { @@ -281,11 +275,11 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) } } break; - //case ID_GR: - // if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { - // do_update_collection = true; - // } - // break; + // case ID_GR: + // if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { + // do_update_collection = true; + // } + // break; case ID_SCE: { if (id->recalc & ID_RECALC_BASE_FLAGS) { 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 84de32f31996..7a02b034fdd0 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -10,18 +10,19 @@ #include "CLG_log.h" +#include "instancer.h" #include "light.h" #include "mesh.h" #include "object.h" #include "world.h" -#include "instancer.h" namespace blender::render::hydra { -extern struct CLG_LogRef *LOG_BSD; /* BSD - Blender Scene Delegate */ +extern struct CLG_LogRef *LOG_BSD; /* BSD - Blender Scene Delegate */ class BlenderSceneDelegate : public pxr::HdSceneDelegate { friend MeshData; + friend InstancerData; public: enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; @@ -56,16 +57,17 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { MeshData *mesh_data(pxr::SdfPath const &id); LightData *light_data(pxr::SdfPath const &id); MaterialData *material_data(pxr::SdfPath const &id); - InstancerData *instancer_data(pxr::SdfPath const &id, bool base_prim=false); + InstancerData *instancer_data(pxr::SdfPath const &id, bool base_prim = false); void add_update_object(Object *object); - void add_update_instancer(DupliObject *dupli); + void add_update_instancer(Object *object); void update_world(); void update_collection(bool remove, bool visibility); private: Depsgraph *depsgraph; bContext *context; + Scene *scene; View3D *view3d; ObjectDataMap objects; diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 8747049fdf14..8d30c99e7010 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -3,9 +3,9 @@ #include +#include "../utils.h" #include "blender_scene_delegate.h" #include "instancer.h" -#include "../utils.h" using namespace pxr; @@ -27,7 +27,8 @@ bool InstancerData::supported(Object *object) return false; } -std::unique_ptr InstancerData::create(BlenderSceneDelegate *scene_delegate, Object *object) +std::unique_ptr InstancerData::create(BlenderSceneDelegate *scene_delegate, + Object *object) { auto data = std::make_unique(scene_delegate, object); data->init(); @@ -45,26 +46,36 @@ SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object *obj } InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object) - : MeshData(scene_delegate, object) + : MeshData(scene_delegate, object), parent_obj(object) { p_id = prim_id(scene_delegate, object); instancer_id = p_id.AppendElementString("Instancer"); CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", id->name, instancer_id.GetText()); + id = nullptr; } void InstancerData::init() { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - MeshData::init(); + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); + id = nullptr; transforms.clear(); + ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); + LISTBASE_FOREACH (DupliObject *, dupli, lb) { + if (!id) { + id = (ID *)dupli->ob; + } + transforms.push_back(gf_matrix_from_transform(dupli->mat)); + CLOG_INFO(LOG_BSD, 2, "Instance %s %d", id->name, dupli->random_id); + } + free_object_duplilist(lb); + + MeshData::init(); } GfMatrix4d InstancerData::transform() { return pxr::GfMatrix4d(1.0); - /* transform of the first instance */ - //return transforms[0]; } bool InstancerData::update_visibility(View3D *view3d) @@ -73,13 +84,8 @@ bool InstancerData::update_visibility(View3D *view3d) return false; } - Object *obj = (Object *)id; - if (!obj->parent) { - return false; - } - bool prev_visible = visible; - visible = BKE_object_is_visible_in_viewport(view3d, obj->parent); + visible = BKE_object_is_visible_in_viewport(view3d, parent_obj); return visible != prev_visible; } @@ -113,12 +119,6 @@ VtIntArray InstancerData::instance_indices() return ret; } -void InstancerData::add_instance(DupliObject *dupli) -{ - CLOG_INFO(LOG_BSD, 2, "%s [%d]", id->name, dupli->random_id); - transforms.push_back(gf_matrix_from_transform(dupli->mat)); -} - void InstancerData::insert() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 5439cc023435..1b634f2ff05b 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -9,10 +9,11 @@ namespace blender::render::hydra { -class InstancerData: public MeshData { -public: +class InstancerData : public MeshData { + public: static bool supported(Object *object); - static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Object *object); + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, + Object *object); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object); InstancerData(BlenderSceneDelegate *scene_delegate, Object *object); @@ -28,12 +29,11 @@ public: pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::VtIntArray instance_indices(); - void add_instance(DupliObject *dupli); - pxr::SdfPath instancer_id; private: + Object *parent_obj; pxr::VtMatrix4dArray transforms; }; -} // namespace blender::render::hydra +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index 281e2c8ec5f8..9d95abb1b218 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -147,7 +147,7 @@ bool LightData::update_visibility(View3D *view3d) bool ret = ObjectData::update_visibility(view3d); if (ret) { scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, - pxr::HdLight::DirtyParams); + pxr::HdLight::DirtyParams); } return ret; } diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 42e4706f999c..3e20c74ef966 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -21,7 +21,7 @@ namespace blender::render::hydra { std::unique_ptr MaterialData::create(BlenderSceneDelegate *scene_delegate, - Material *material) + Material *material) { auto data = std::make_unique(scene_delegate, material); data->init(); @@ -104,7 +104,8 @@ pxr::VtValue MaterialData::material_resource() if (material_network_map.IsEmpty()) { const std::string &path = mtlx_path.GetResolvedPath(); if (!path.empty()) { - pxr::HdRenderDelegate *render_delegate = scene_delegate->GetRenderIndex().GetRenderDelegate(); + pxr::HdRenderDelegate *render_delegate = + scene_delegate->GetRenderIndex().GetRenderDelegate(); pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes(); pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts(); @@ -134,7 +135,8 @@ void MaterialData::update() { CLOG_INFO(LOG_BSD, 2, "%s", id->name); init(); - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdMaterial::AllDirty); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, + pxr::HdMaterial::AllDirty); } } // 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 7d147d4ac7eb..7d7e6dfc92df 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -20,7 +20,7 @@ class MaterialData : IdData { public: static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, - Material *material); + Material *material); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Material *material); MaterialData(BlenderSceneDelegate *scene_delegate, Material *material); diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 4d06a4a138cb..1a2869362a78 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -8,8 +8,8 @@ #include "BKE_duplilist.h" -#include "object.h" #include "material.h" +#include "object.h" namespace blender::render::hydra { diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 39b9b483b605..6a120734296d 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -28,7 +28,8 @@ bool ObjectData::supported(Object *object) return false; } -std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_delegate, Object *object) +std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_delegate, + Object *object) { std::unique_ptr data; diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index f01469d01850..993574853c7f 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -29,8 +29,8 @@ namespace blender::render::hydra { std::unique_ptr WorldData::create(BlenderSceneDelegate *scene_delegate, - World *world, - bContext *context) + World *world, + bContext *context) { auto data = std::make_unique(scene_delegate, world, context); data->init(); diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index c7e40b169936..36ef231894f4 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -21,8 +21,8 @@ namespace blender::render::hydra { class WorldData : public IdData { public: static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, - World *world, - bContext *context); + World *world, + bContext *context); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate); WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); -- 2.30.2 From 5bc6d011bbe4890a4ee162e105511f1dc02227e3 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 31 Mar 2023 10:00:12 +0300 Subject: [PATCH 15/20] Implement instancer's updates of basic mesh --- .../scene_delegate/blender_scene_delegate.cc | 11 +++++-- .../render/hydra/scene_delegate/instancer.cc | 33 ++++++++++++++++--- .../render/hydra/scene_delegate/mesh.cc | 4 +++ 3 files changed, 40 insertions(+), 8 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 6e080b3e681c..e6c723a442b3 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -119,9 +119,6 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) if (!ObjectData::supported(object)) { continue; } - if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) { - add_update_instancer(object); - } id = ObjectData::prim_id(this, object); if (remove) { @@ -170,10 +167,17 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) void BlenderSceneDelegate::add_update_object(Object *object) { + if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) { + add_update_instancer(object); + } pxr::SdfPath id = ObjectData::prim_id(this, object); ObjectData *obj_data = object_data(id); if (obj_data) { obj_data->update(); + return; + } + if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) { + return; } objects[id] = ObjectData::create(this, object); obj_data = object_data(id); @@ -186,6 +190,7 @@ void BlenderSceneDelegate::add_update_instancer(Object *object) InstancerData *i_data = instancer_data(id, true); if (i_data) { i_data->update(); + return; } objects[id] = InstancerData::create(this, object); i_data = instancer_data(id, true); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 8d30c99e7010..13e8f60b1d56 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -63,10 +63,12 @@ void InstancerData::init() ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); LISTBASE_FOREACH (DupliObject *, dupli, lb) { if (!id) { + /* TODO: We create instances only for object in first dupli. + Instances should be created for all objects */ id = (ID *)dupli->ob; } transforms.push_back(gf_matrix_from_transform(dupli->mat)); - CLOG_INFO(LOG_BSD, 2, "Instance %s %d", id->name, dupli->random_id); + CLOG_INFO(LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id); } free_object_duplilist(lb); @@ -121,7 +123,7 @@ VtIntArray InstancerData::instance_indices() void InstancerData::insert() { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); MeshData::insert(); if (face_vertex_counts.empty()) { @@ -132,7 +134,7 @@ void InstancerData::insert() void InstancerData::remove() { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); if (!scene_delegate->GetRenderIndex().HasInstancer(instancer_id)) { return; @@ -144,8 +146,29 @@ void InstancerData::remove() void InstancerData::update() { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - MeshData::update(); + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); + + pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; + unsigned int recalc = ((ID *)parent_obj)->recalc; + + if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj->data)->recalc & ID_RECALC_GEOMETRY)) { + init(); + bits = pxr::HdChangeTracker::AllDirty; + } + else if (recalc & ID_RECALC_TRANSFORM) { + transforms.clear(); + ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); + LISTBASE_FOREACH (DupliObject *, dupli, lb) { + transforms.push_back(gf_matrix_from_transform(dupli->mat)); + } + free_object_duplilist(lb); + + bits |= pxr::HdChangeTracker::DirtyTransform; + } + if (bits != pxr::HdChangeTracker::Clean) { + scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits); + } + //scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits); } } // 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 03226e6cbf6d..e2d44c017174 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -223,6 +223,10 @@ void MeshData::update() } } + if (bits == pxr::HdChangeTracker::Clean) { + return; + } + if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { insert(); return; -- 2.30.2 From 91b7b09d0008c90bd6b886a714f6ee9a59cda209 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 31 Mar 2023 12:12:12 +0300 Subject: [PATCH 16/20] Fixed update of base mesh in Instancer --- .../scene_delegate/blender_scene_delegate.cc | 17 +++++++++++++++++ .../scene_delegate/blender_scene_delegate.h | 1 + .../render/hydra/scene_delegate/instancer.cc | 14 +++++++++++++- .../render/hydra/scene_delegate/instancer.h | 1 + 4 files changed, 32 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 e6c723a442b3..437690e8a648 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -170,6 +170,11 @@ void BlenderSceneDelegate::add_update_object(Object *object) if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) { add_update_instancer(object); } + InstancerData *i_data = instancer_data(object); + if (i_data) { + i_data->update(); + } + pxr::SdfPath id = ObjectData::prim_id(this, object); ObjectData *obj_data = object_data(id); if (obj_data) { @@ -233,6 +238,18 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool return dynamic_cast(object_data(id.GetParentPath())); } +InstancerData *BlenderSceneDelegate::instancer_data(Object *object) +{ + InstancerData *i_data; + for (auto &it : objects) { + i_data = dynamic_cast(it.second.get()); + if (i_data && i_data->is_base(object)) { + return i_data; + } + } + return nullptr; +} + void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) { bool is_populated = depsgraph != nullptr; 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 7a02b034fdd0..04dd2f684120 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -58,6 +58,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { LightData *light_data(pxr::SdfPath const &id); MaterialData *material_data(pxr::SdfPath const &id); InstancerData *instancer_data(pxr::SdfPath const &id, bool base_prim = false); + InstancerData *instancer_data(Object *object); void add_update_object(Object *object); void add_update_instancer(Object *object); diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 13e8f60b1d56..a363e65f19b0 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -121,6 +121,11 @@ VtIntArray InstancerData::instance_indices() return ret; } +bool InstancerData::is_base(Object *object) const +{ + return (ID *)object == id; +} + void InstancerData::insert() { CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); @@ -151,6 +156,14 @@ void InstancerData::update() pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; unsigned int recalc = ((ID *)parent_obj)->recalc; + Object *object = (Object *)id; + 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(); bits = pxr::HdChangeTracker::AllDirty; @@ -168,7 +181,6 @@ void InstancerData::update() if (bits != pxr::HdChangeTracker::Clean) { scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits); } - //scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits); } } // 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 1b634f2ff05b..72096960776a 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -29,6 +29,7 @@ class InstancerData : public MeshData { pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::VtIntArray instance_indices(); + bool is_base(Object *object) const; pxr::SdfPath instancer_id; private: -- 2.30.2 From e04a3c6340fe8e58c10ea69f7f56b7122d90951e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 31 Mar 2023 13:51:29 +0300 Subject: [PATCH 17/20] Fixed transform update of base mesh --- .../render/hydra/scene_delegate/instancer.cc | 52 ++++++++++--------- .../render/hydra/scene_delegate/instancer.h | 2 + 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index a363e65f19b0..74d47188e816 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -48,30 +48,17 @@ SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object *obj InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object) : MeshData(scene_delegate, object), parent_obj(object) { + id = nullptr; p_id = prim_id(scene_delegate, object); instancer_id = p_id.AppendElementString("Instancer"); - CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", id->name, instancer_id.GetText()); - id = nullptr; + CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj)->name, instancer_id.GetText()); } void InstancerData::init() { CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); - id = nullptr; - transforms.clear(); - ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); - LISTBASE_FOREACH (DupliObject *, dupli, lb) { - if (!id) { - /* TODO: We create instances only for object in first dupli. - Instances should be created for all objects */ - id = (ID *)dupli->ob; - } - transforms.push_back(gf_matrix_from_transform(dupli->mat)); - CLOG_INFO(LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id); - } - free_object_duplilist(lb); - + set_instances(); MeshData::init(); } @@ -126,6 +113,27 @@ bool InstancerData::is_base(Object *object) const return (ID *)object == id; } +bool InstancerData::set_instances() +{ + ID *prev_id = id; + id = nullptr; + transforms.clear(); + ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); + LISTBASE_FOREACH (DupliObject *, dupli, lb) { + if (!id) { + /* TODO: We create instances only for object in first dupli. + Instances should be created for all objects */ + id = (ID *)dupli->ob; + } + transforms.push_back(gf_matrix_from_transform(dupli->mat)); + CLOG_INFO( + LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id); + } + free_object_duplilist(lb); + + return id != prev_id; +} + void InstancerData::insert() { CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); @@ -166,16 +174,10 @@ void InstancerData::update() if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj->data)->recalc & ID_RECALC_GEOMETRY)) { init(); - bits = pxr::HdChangeTracker::AllDirty; + bits |= pxr::HdChangeTracker::AllDirty; } - else if (recalc & ID_RECALC_TRANSFORM) { - transforms.clear(); - ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); - LISTBASE_FOREACH (DupliObject *, dupli, lb) { - transforms.push_back(gf_matrix_from_transform(dupli->mat)); - } - free_object_duplilist(lb); - + else if (recalc & ID_RECALC_TRANSFORM || id->recalc & ID_RECALC_TRANSFORM) { + set_instances(); bits |= pxr::HdChangeTracker::DirtyTransform; } if (bits != pxr::HdChangeTracker::Clean) { diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 72096960776a..0e33f1ac017a 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -35,6 +35,8 @@ class InstancerData : public MeshData { private: Object *parent_obj; pxr::VtMatrix4dArray transforms; + + bool set_instances(); }; } // namespace blender::render::hydra -- 2.30.2 From 5f57b0dbd3e7f631e28c5900d5295a16b6b30e56 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 31 Mar 2023 13:58:25 +0300 Subject: [PATCH 18/20] Fixed visibility update --- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 4 +--- source/blender/render/hydra/scene_delegate/instancer.cc | 7 ++++++- 2 files changed, 7 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 437690e8a648..2855a3c163b8 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -90,9 +90,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) if (visibility) { /* Check and update visibility */ for (auto &obj : objects) { - if (obj.second->update_visibility(view3d)) { - obj.second->update(); - }; + obj.second->update_visibility(view3d); } } diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 74d47188e816..14b84dbd0da8 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -75,7 +75,12 @@ bool InstancerData::update_visibility(View3D *view3d) bool prev_visible = visible; visible = BKE_object_is_visible_in_viewport(view3d, parent_obj); - return visible != prev_visible; + bool ret = visible != prev_visible; + if (ret) { + scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + p_id, pxr::HdChangeTracker::DirtyVisibility); + } + return ret; } VtValue InstancerData::get_data(TfToken const &key) const -- 2.30.2 From 60d68d8bd7210a9da2064e6571a7f49b7d720338 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 31 Mar 2023 14:55:28 +0300 Subject: [PATCH 19/20] Fixed instancer remove --- .../scene_delegate/blender_scene_delegate.cc | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 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 2855a3c163b8..a6c226251c6a 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -121,6 +121,9 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility) id = ObjectData::prim_id(this, object); if (remove) { available_objects.insert(id); + if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) { + available_objects.insert(InstancerData::prim_id(this, object)); + } } if (!object_data(id)) { @@ -261,7 +264,6 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) /* Export initial objects */ update_collection(false, false); update_world(); - GetRenderIndex().InsertInstancer(this, GetDelegateID().AppendElementString("Instancer")); return; } @@ -270,14 +272,37 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) bool do_update_visibility = false; bool do_update_world = false; + unsigned int scene_recalc = ((ID *)scene)->recalc; + if (scene_recalc) { + /* Checking scene updates */ + CLOG_INFO(LOG_BSD, 2, "Update: %s [%s]", ((ID *)scene)->name, + std::bitset<32>(scene_recalc).to_string().c_str()); + + if (scene_recalc & ID_RECALC_BASE_FLAGS) { + do_update_visibility = true; + } + if (scene_recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { + do_update_collection = true; + } + if (scene_recalc & ID_RECALC_AUDIO_VOLUME) { + if ((scene->world && !world_data) || (!scene->world && world_data)) { + do_update_world = true; + } + } + if (do_update_collection || do_update_visibility) { + update_collection(do_update_collection, do_update_visibility); + } + } + + /* Checking other objects updates */ DEGIDIterData data = {0}; data.graph = depsgraph; data.only_updated = true; ITER_BEGIN ( DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { - CLOG_INFO( - LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str()); + CLOG_INFO(LOG_BSD, 2, "Update: %s [%s]", id->name, + std::bitset<32>(id->recalc).to_string().c_str()); switch (GS(id->name)) { case ID_OB: { @@ -295,27 +320,6 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) } } break; - // case ID_GR: - // if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { - // do_update_collection = true; - // } - // break; - - case ID_SCE: { - if (id->recalc & ID_RECALC_BASE_FLAGS) { - do_update_visibility = true; - } - if (id->recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { - do_update_collection = true; - } - if (id->recalc & ID_RECALC_AUDIO_VOLUME) { - Scene *scene = (Scene *)id; - if ((scene->world && !world_data) || (!scene->world && world_data)) { - do_update_world = true; - } - } - } break; - case ID_WO: { if (id->recalc & ID_RECALC_SHADING) { do_update_world = true; @@ -328,9 +332,6 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) } ITER_END; - if (do_update_collection || do_update_visibility) { - update_collection(do_update_collection, do_update_visibility); - } if (do_update_world) { update_world(); } -- 2.30.2 From dba77f7fdefd4ba72af3e3e79410e9a794e25b8e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 5 Apr 2023 12:44:04 +0300 Subject: [PATCH 20/20] Fixed code style: removed "using namespace pxr", call "make format". Fixed adding only one object instance. --- .../scene_delegate/blender_scene_delegate.cc | 9 ++++-- .../render/hydra/scene_delegate/instancer.cc | 29 ++++++++++--------- 2 files changed, 21 insertions(+), 17 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 a6c226251c6a..772275228dd6 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -275,7 +275,10 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) unsigned int scene_recalc = ((ID *)scene)->recalc; if (scene_recalc) { /* Checking scene updates */ - CLOG_INFO(LOG_BSD, 2, "Update: %s [%s]", ((ID *)scene)->name, + CLOG_INFO(LOG_BSD, + 2, + "Update: %s [%s]", + ((ID *)scene)->name, std::bitset<32>(scene_recalc).to_string().c_str()); if (scene_recalc & ID_RECALC_BASE_FLAGS) { @@ -301,8 +304,8 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) ITER_BEGIN ( DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { - CLOG_INFO(LOG_BSD, 2, "Update: %s [%s]", id->name, - std::bitset<32>(id->recalc).to_string().c_str()); + CLOG_INFO( + LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str()); switch (GS(id->name)) { case ID_OB: { diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 14b84dbd0da8..8c5cd75e72e8 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -7,8 +7,6 @@ #include "blender_scene_delegate.h" #include "instancer.h" -using namespace pxr; - namespace blender::render::hydra { bool InstancerData::supported(Object *object) @@ -36,7 +34,7 @@ std::unique_ptr InstancerData::create(BlenderSceneDelegate *scene return data; } -SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object) +pxr::SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object) { /* Making id of instancer in form like I_. Example: * I_000002073e369608 */ @@ -62,7 +60,7 @@ void InstancerData::init() MeshData::init(); } -GfMatrix4d InstancerData::transform() +pxr::GfMatrix4d InstancerData::transform() { return pxr::GfMatrix4d(1.0); } @@ -83,30 +81,30 @@ bool InstancerData::update_visibility(View3D *view3d) return ret; } -VtValue InstancerData::get_data(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()); - if (key == HdInstancerTokens->instanceTransform) { - return VtValue(transforms); + if (key == pxr::HdInstancerTokens->instanceTransform) { + return pxr::VtValue(transforms); } return MeshData::get_data(key); } -HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors( - HdInterpolation interpolation) +pxr::HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors( + pxr::HdInterpolation interpolation) { - HdPrimvarDescriptorVector primvars; - if (interpolation == HdInterpolationInstance) { + pxr::HdPrimvarDescriptorVector primvars; + if (interpolation == pxr::HdInterpolationInstance) { primvars.emplace_back( - HdInstancerTokens->instanceTransform, interpolation, HdPrimvarRoleTokens->none); + pxr::HdInstancerTokens->instanceTransform, interpolation, pxr::HdPrimvarRoleTokens->none); } return primvars; } -VtIntArray InstancerData::instance_indices() +pxr::VtIntArray InstancerData::instance_indices() { - VtIntArray ret(transforms.size()); + pxr::VtIntArray ret(transforms.size()); for (size_t i = 0; i < ret.size(); ++i) { ret[i] = i; } @@ -130,6 +128,9 @@ bool InstancerData::set_instances() Instances should be created for all objects */ id = (ID *)dupli->ob; } + if (id != (ID *)dupli->ob) { + continue; + } transforms.push_back(gf_matrix_from_transform(dupli->mat)); CLOG_INFO( LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id); -- 2.30.2