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/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 de574193c48e..772275228dd6 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,50 +24,20 @@ 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::init(this, material); - mat_data = material_data(id); - mat_data->export_mtlx(); - mat_data->insert_prim(); - } - mesh_data.material_id = id; -} - -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); - } -} - void BlenderSceneDelegate::update_world() { - Scene *scene = DEG_get_input_scene(depsgraph); 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); } } else { if (world) { - world_data = WorldData::init(this, world, context); - world_data->mark_prim_dirty(IdData::DirtyBits::ALL_DIRTY); + world_data->update(world); } else { - world_data->remove_prim(); + world_data->remove(); world_data = nullptr; } } @@ -83,9 +55,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 = instancer_data(prim_id, true); + if (i_data) { + return i_data->instancer_id; } return pxr::SdfPath(); } @@ -102,43 +74,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); -} - -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, - 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); - } - } - return HdSceneDelegate::SamplePrimvar(id, key, max_sample_count, sample_times, sample_values); + CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText()); + InstancerData *i_data = instancer_data(instancer_id); + return i_data->transform(); } void BlenderSceneDelegate::update_collection(bool remove, bool visibility) @@ -146,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->mark_prim_dirty(IdData::DirtyBits::DIRTY_VISIBILITY); - }; + obj.second->update_visibility(view3d); } } @@ -159,7 +101,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; @@ -171,22 +113,21 @@ 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) { - add_update_instance(data.dupli_object_current); - continue; - } - 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)) { - add_update_object(object, true, true, true); + add_update_object(object); } } ITER_END; @@ -197,7 +138,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(); } @@ -206,70 +147,60 @@ 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) { if (available_materials.find(it->first) != available_materials.end()) { continue; } - it->second->remove_prim(); + it->second->remove(); materials.erase(it); it = materials.begin(); } } } -void BlenderSceneDelegate::add_update_object(Object *object, - bool geometry, - bool transform, - bool shading) +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) { - objects[id] = ObjectData::init(this, object); - obj_data = object_data(id); - obj_data->update_visibility(view3d); - obj_data->insert_prim(); - MeshData *m_data = dynamic_cast(obj_data); - if (m_data) { - set_material(*m_data); - } + if (obj_data) { + obj_data->update(); return; } - - if (geometry) { - objects[id] = ObjectData::init(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); + if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) { return; } - - if (transform) { - obj_data->mark_prim_dirty(IdData::DirtyBits::DIRTY_TRANSFORM); - } - - if (shading) { - obj_data->mark_prim_dirty(IdData::DirtyBits::DIRTY_MATERIAL); - } + objects[id] = ObjectData::create(this, object); + obj_data = object_data(id); + obj_data->update_visibility(view3d); } -void BlenderSceneDelegate::add_update_instance(DupliObject *dupli) +void BlenderSceneDelegate::add_update_instancer(Object *object) { - pxr::SdfPath id = ObjectData::prim_id(this, dupli->ob); - if (!object_data(id)) { - add_update_object(dupli->ob, true, true, true); + pxr::SdfPath id = InstancerData::prim_id(this, object); + InstancerData *i_data = instancer_data(id, true); + if (i_data) { + i_data->update(); + return; } - - MeshData *m_data = mesh_data(id); - m_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) @@ -283,12 +214,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) @@ -300,19 +231,39 @@ MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) return it->second.get(); } +InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool base_prim) +{ + if (base_prim) { + return dynamic_cast(object_data(id)); + } + 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; depsgraph = deps; context = cont; + scene = DEG_get_input_scene(depsgraph); view3d = CTX_wm_view3d(context); if (!is_populated) { /* Export initial objects */ update_collection(false, false); update_world(); - GetRenderIndex().InsertInstancer(this, GetDelegateID().AppendElementString("Instancer")); return; } @@ -320,19 +271,41 @@ 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; + 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) { - 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: { @@ -340,39 +313,21 @@ 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) { - do_update_collection = true; - } - break; - - case ID_SCE: - if (!geometry && !transform && !shading) { - Scene *scene = (Scene *)id; - do_update_visibility = true; - - if ((scene->world && !world_data) || (!scene->world && world_data)) { - do_update_world = true; - } - } - break; - - case ID_WO: - if (shading) { + case ID_WO: { + if (id->recalc & ID_RECALC_SHADING) { do_update_world = true; } - break; + } break; default: break; @@ -380,9 +335,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(); } @@ -398,11 +350,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); } @@ -411,6 +360,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(); } @@ -418,22 +373,23 @@ 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) { - return mesh_data(rprim_id)->material_id; + return mesh_data(rprim_id)->material_id(); } pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id) @@ -452,15 +408,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 cf03fd74f0c7..04dd2f684120 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -10,6 +10,7 @@ #include "CLG_log.h" +#include "instancer.h" #include "light.h" #include "mesh.h" #include "object.h" @@ -17,9 +18,12 @@ 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 }; @@ -44,16 +48,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); - 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, - float *sample_times, - pxr::VtValue *sample_values) override; + pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancer_id) override; EngineType engine_type; @@ -62,17 +57,18 @@ 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(Object *object); - 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 add_update_object(Object *object); + 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/id.cc b/source/blender/render/hydra/scene_delegate/id.cc index c630b2f20f4f..7c0a28be9de2 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -13,14 +13,7 @@ IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id) { } -std::string IdData::name() -{ - 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 43e50e33d931..8d36f8e2d3c1 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -18,22 +18,21 @@ class IdData { 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 void init() = 0; + virtual void insert() = 0; + virtual void remove() = 0; + virtual void update() = 0; - enum class DirtyBits { DIRTY_TRANSFORM = 1, DIRTY_VISIBILITY, DIRTY_MATERIAL, ALL_DIRTY }; - - 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; protected: BlenderSceneDelegate *scene_delegate; ID *id; + 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/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc new file mode 100644 index 000000000000..8c5cd75e72e8 --- /dev/null +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -0,0 +1,194 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#include + +#include "../utils.h" +#include "blender_scene_delegate.h" +#include "instancer.h" + +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; +} + +pxr::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), 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 *)parent_obj)->name, instancer_id.GetText()); +} + +void InstancerData::init() +{ + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); + + set_instances(); + MeshData::init(); +} + +pxr::GfMatrix4d InstancerData::transform() +{ + return pxr::GfMatrix4d(1.0); +} + +bool InstancerData::update_visibility(View3D *view3d) +{ + if (!view3d) { + return false; + } + + bool prev_visible = visible; + visible = BKE_object_is_visible_in_viewport(view3d, parent_obj); + bool ret = visible != prev_visible; + if (ret) { + scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + p_id, pxr::HdChangeTracker::DirtyVisibility); + } + return ret; +} + +pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const +{ + CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); + + if (key == pxr::HdInstancerTokens->instanceTransform) { + return pxr::VtValue(transforms); + } + return MeshData::get_data(key); +} + +pxr::HdPrimvarDescriptorVector InstancerData::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 InstancerData::instance_indices() +{ + pxr::VtIntArray ret(transforms.size()); + for (size_t i = 0; i < ret.size(); ++i) { + ret[i] = i; + } + return ret; +} + +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; + } + 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); + } + free_object_duplilist(lb); + + return id != prev_id; +} + +void InstancerData::insert() +{ + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->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 *)parent_obj)->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 *)parent_obj)->name); + + 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; + } + else if (recalc & ID_RECALC_TRANSFORM || id->recalc & ID_RECALC_TRANSFORM) { + set_instances(); + bits |= pxr::HdChangeTracker::DirtyTransform; + } + if (bits != pxr::HdChangeTracker::Clean) { + scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_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 new file mode 100644 index 000000000000..0e33f1ac017a --- /dev/null +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -0,0 +1,42 @@ +/* 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; + bool update_visibility(View3D *view3d) override; + + pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); + pxr::VtIntArray instance_indices(); + + bool is_base(Object *object) const; + pxr::SdfPath instancer_id; + + private: + Object *parent_obj; + pxr::VtMatrix4dArray transforms; + + bool set_instances(); +}; + +} // 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 ed9a14f76200..9d95abb1b218 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -18,12 +18,21 @@ namespace blender::render::hydra { LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object) : ObjectData(scene_delegate, object) { - Light *light = (Light *)((Object *)id)->data; + CLOG_INFO(LOG_BSD, 2, "%s id=%s", id->name, p_id.GetText()); +} - data[pxr::HdLightTokens->intensity] = scene_delegate->engine_type == - BlenderSceneDelegate::EngineType::PREVIEW ? - light->energy / 1000 : - light->energy; +void LightData::init() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + + Light *light = (Light *)((Object *)id)->data; + data.clear(); + + 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); @@ -112,7 +121,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,41 +142,43 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key) return ret; } -void LightData::insert_prim() +bool LightData::update_visibility(View3D *view3d) { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); + 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_prim() +void LightData::remove() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); + 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::mark_prim_dirty(DirtyBits dirty_bits) +void LightData::update() { /* 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) { + init(); + bits = pxr::HdLight::AllDirty; + } + else if (id->recalc & ID_RECALC_TRANSFORM) { + bits = pxr::HdLight::DirtyTransform; } - 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()); } } // 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 ad3223b22769..12d1988e80ed 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -15,10 +15,12 @@ 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() 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 c460b76b651d..3e20c74ef966 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -20,10 +20,13 @@ namespace blender::render::hydra { -std::unique_ptr MaterialData::init(BlenderSceneDelegate *scene_delegate, - Material *material) +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(); + data->insert(); + return data; } pxr::SdfPath MaterialData::prim_id(BlenderSceneDelegate *scene_delegate, Material *material) @@ -38,40 +41,16 @@ 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); + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); } -pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) +void MaterialData::init() { - 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; -} + CLOG_INFO(LOG_BSD, 2, "%s", id->name); -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(); + material_network_map = pxr::VtValue(); - pxr::HdMaterialNetworkMap material_network_map; - 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(); -} - -void MaterialData::export_mtlx() -{ /* Call of python function hydra.export_mtlx() */ PyGILState_STATE gstate; @@ -97,7 +76,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); @@ -105,37 +84,59 @@ 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_prim() +pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const { - pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); + 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); 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() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); + 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::mark_prim_dirty(DirtyBits dirty_bits) +void MaterialData::update() { - pxr::HdDirtyBits bits = pxr::HdMaterial::Clean; - switch (dirty_bits) { - case DirtyBits::ALL_DIRTY: - bits = pxr::HdMaterial::AllDirty; - break; - 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()); + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + init(); + 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 5e14f00c9b24..7d7e6dfc92df 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -13,24 +13,30 @@ namespace blender::render::hydra { +class MeshData; + class MaterialData : IdData { + friend MeshData; + public: - static std::unique_ptr init(BlenderSceneDelegate *scene_delegate, - Material *material); + 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; + void init() override; + void insert() override; + void remove() override; + void update() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue material_resource(); void export_mtlx(); 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 09bbe3828089..e2d44c017174 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -16,22 +16,12 @@ namespace blender::render::hydra { MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) - : ObjectData(scene_delegate, object) + : ObjectData(scene_delegate, object), mat_data(nullptr) { - 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); - } + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); } -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) { @@ -49,13 +39,14 @@ pxr::VtValue MeshData::get_data(pxr::TfToken const &key) return ret; } -Material *MeshData::material() +bool MeshData::update_visibility(View3D *view3d) { - Object *object = (Object *)id; - if (BKE_object_material_count_eval(object) == 0) { - return nullptr; + bool ret = ObjectData::update_visibility(view3d); + if (ret) { + scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + p_id, pxr::HdChangeTracker::DirtyVisibility); } - return BKE_object_material_get_eval(object, object->actcol); + return ret; } pxr::HdMeshTopology MeshData::mesh_topology() @@ -88,68 +79,22 @@ pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(pxr::HdInterpolatio return primvars; } -pxr::HdPrimvarDescriptorVector MeshData::instancer_primvar_descriptors( - pxr::HdInterpolation interpolation) +pxr::SdfPath MeshData::material_id() { - pxr::HdPrimvarDescriptorVector primvars; - if (interpolation == pxr::HdInterpolationInstance) { - primvars.emplace_back( - pxr::HdInstancerTokens->instanceTransform, interpolation, pxr::HdPrimvarRoleTokens->none); + if (!mat_data) { + return pxr::SdfPath(); } - 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", name().c_str(), 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); + return mat_data->p_id; } 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) { @@ -200,62 +145,100 @@ void MeshData::set_mesh(Mesh *mesh) } } -void MeshData::insert_prim() +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); + + 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); + } + + set_material(); +} + +void MeshData::insert() { if (face_vertex_counts.empty()) { return; } - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); + 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_prim() +void MeshData::remove() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { 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::mark_prim_dirty(DirtyBits dirty_bits) +void MeshData::update() { - pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); + pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; + Object *object = (Object *)id; + if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { + init(); + bits = pxr::HdChangeTracker::AllDirty; + } + else { + if (id->recalc & ID_RECALC_SHADING) { + set_material(); + bits |= pxr::HdChangeTracker::DirtyMaterialId; + } + if (id->recalc & ID_RECALC_TRANSFORM) { + bits |= pxr::HdChangeTracker::DirtyTransform; + } + } + + if (bits == pxr::HdChangeTracker::Clean) { + return; + } + 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; } - - 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 1338e5e599d7..1a2869362a78 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -8,6 +8,7 @@ #include "BKE_duplilist.h" +#include "material.h" #include "object.h" namespace blender::render::hydra { @@ -16,32 +17,20 @@ 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() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; + bool update_visibility(View3D *view3d) override; - void insert_prim() override; - void remove_prim() override; - void mark_prim_dirty(DirtyBits dirty_bits) override; - - Material *material(); pxr::HdMeshTopology mesh_topology(); pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); - 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); + pxr::SdfPath material_id(); - void add_instance(DupliObject *dupli); - - pxr::SdfPath material_id; - pxr::SdfPath instancer_id; - - private: + protected: void set_mesh(Mesh *mesh); + void set_material(); pxr::VtIntArray face_vertex_counts; pxr::VtIntArray face_vertex_indices; @@ -50,6 +39,8 @@ class MeshData : public ObjectData { pxr::VtVec2fArray uvs; pxr::VtMatrix4dArray instances; + + MaterialData *mat_data; }; } // 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 f3386f9c153d..6a120734296d 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -28,24 +28,32 @@ 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) { + std::unique_ptr data; + switch (object->type) { case OB_MESH: case OB_SURF: case OB_FONT: - 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(); + data->insert(); + } + return data; } pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object) @@ -60,11 +68,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) { -} - -int ObjectData::type() -{ - return ((Object *)id)->type; + p_id = prim_id(scene_delegate, object); } pxr::GfMatrix4d ObjectData::transform() diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index c498f73a2100..bdc17566ec43 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -17,14 +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); - int type(); - pxr::GfMatrix4d transform(); - bool update_visibility(View3D *view3d); + 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 9b559a7d8760..993574853c7f 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -28,11 +28,14 @@ namespace blender::render::hydra { -std::unique_ptr WorldData::init(BlenderSceneDelegate *scene_delegate, - World *world, - bContext *context) +std::unique_ptr WorldData::create(BlenderSceneDelegate *scene_delegate, + World *world, + bContext *context) { - return std::make_unique(scene_delegate, world, context); + auto data = std::make_unique(scene_delegate, world, context); + data->init(); + data->insert(); + return data; } pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate) @@ -41,17 +44,39 @@ 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); + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); +} + +void WorldData::init() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + + World *world = (World *)id; + data.clear(); + data[pxr::UsdLuxTokens->orientToStageUpAxis] = true; if (world->use_nodes) { /* 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; } @@ -113,7 +138,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); @@ -123,34 +148,30 @@ pxr::VtValue WorldData::get_data(pxr::TfToken const &key) return ret; } -void WorldData::insert_prim() +void WorldData::insert() { - pxr::SdfPath p_id = prim_id(scene_delegate); + 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_prim() +void WorldData::remove() { - pxr::SdfPath p_id = prim_id(scene_delegate); + 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::mark_prim_dirty(DirtyBits dirty_bits) +void WorldData::update() { - pxr::HdDirtyBits bits = pxr::HdLight::Clean; - switch (dirty_bits) { - case DirtyBits::ALL_DIRTY: - bits = pxr::HdLight::AllDirty; - break; - default: - break; - } - pxr::SdfPath p_id = prim_id(scene_delegate); - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); - CLOG_INFO(LOG_BSD, 2, "Update"); + CLOG_INFO(LOG_BSD, 2, "%s", id->name); + init(); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdLight::AllDirty); +} + +void WorldData::update(World *world) +{ + id = (ID *)world; + 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 5aa170444f80..36ef231894f4 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -20,22 +20,26 @@ namespace blender::render::hydra { class WorldData : public IdData { public: - static std::unique_ptr init(BlenderSceneDelegate *scene_delegate, - World *world, - bContext *context); + 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); + void init() override; + void insert() override; + void remove() override; + void update() override; + void update(World *world); + 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; + bContext *context; }; } // namespace blender::render::hydra