From d6339d554723356d2057837f7852f960981db071 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sun, 16 Jul 2023 12:51:53 +0300 Subject: [PATCH 1/9] Basic refactoring --- .../scene_delegate/blender_scene_delegate.cc | 10 +---- .../render/hydra/scene_delegate/curves.cc | 26 ++++--------- .../render/hydra/scene_delegate/curves.h | 7 ++-- .../blender/render/hydra/scene_delegate/id.cc | 9 +---- .../blender/render/hydra/scene_delegate/id.h | 22 +++-------- .../render/hydra/scene_delegate/instancer.cc | 16 ++++---- .../render/hydra/scene_delegate/light.cc | 18 ++------- .../render/hydra/scene_delegate/light.h | 1 - .../render/hydra/scene_delegate/material.cc | 14 +++---- .../render/hydra/scene_delegate/material.h | 1 + .../render/hydra/scene_delegate/mesh.cc | 37 +++++++------------ .../render/hydra/scene_delegate/mesh.h | 4 +- .../render/hydra/scene_delegate/object.cc | 15 +++++--- .../render/hydra/scene_delegate/object.h | 9 ++++- .../render/hydra/scene_delegate/volume.cc | 27 ++++---------- .../render/hydra/scene_delegate/volume.h | 5 +-- .../render/hydra/scene_delegate/world.cc | 2 +- 17 files changed, 80 insertions(+), 143 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 3ed4b88e5692..b79e5632ec8e 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -45,7 +45,7 @@ pxr::HdBasisCurvesTopology BlenderSceneDelegate::GetBasisCurvesTopology(pxr::Sdf { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", id.GetText()); CurvesData *c_data = curves_data(id); - return c_data->curves_topology(id); + return c_data->topology(); }; pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id) @@ -72,14 +72,6 @@ pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken cons if (m_data) { return m_data->get_data(id, key); } - CurvesData *c_data = curves_data(id); - if (c_data) { - return c_data->get_data(id, key); - } - VolumeData *v_data = volume_data(id); - if (v_data) { - return v_data->get_data(id, key); - } ObjectData *obj_data = object_data(id); if (obj_data) { return obj_data->get_data(key); diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 53e3ca6ed872..19391e27b13d 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -22,7 +22,6 @@ void CurvesData::init() { ID_LOG(1, ""); - Object *object = (Object *)id; write_curves((Curves *)object->data); write_transform(); write_materials(); @@ -43,17 +42,18 @@ void CurvesData::remove() void CurvesData::update() { - Object *object = (Object *)id; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { + if ((object->id.recalc & ID_RECALC_GEOMETRY) || + (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) + { init(); bits = pxr::HdChangeTracker::AllDirty; } - if (id->recalc & ID_RECALC_SHADING) { + if (object->id.recalc & ID_RECALC_SHADING) { write_materials(); bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; } - if (id->recalc & ID_RECALC_TRANSFORM) { + if (object->id.recalc & ID_RECALC_TRANSFORM) { write_transform(); bits |= pxr::HdChangeTracker::DirtyTransform; } @@ -66,7 +66,7 @@ void CurvesData::update() ID_LOG(1, ""); } -pxr::VtValue CurvesData::get_data(pxr::SdfPath const & /* id */, pxr::TfToken const &key) const +pxr::VtValue CurvesData::get_data(pxr::TfToken const &key) const { if (key == pxr::HdTokens->points) { return pxr::VtValue(vertices_); @@ -80,18 +80,7 @@ pxr::VtValue CurvesData::get_data(pxr::SdfPath const & /* id */, pxr::TfToken co return pxr::VtValue(); } -bool CurvesData::update_visibility() -{ - bool ret = ObjectData::update_visibility(); - if (ret) { - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - prim_id, pxr::HdChangeTracker::DirtyVisibility); - ID_LOG(1, ""); - } - return ret; -} - -pxr::HdBasisCurvesTopology CurvesData::curves_topology(pxr::SdfPath const & /* id */) const +pxr::HdBasisCurvesTopology CurvesData::topology() const { return pxr::HdBasisCurvesTopology(pxr::HdTokens->linear, pxr::TfToken(), @@ -180,7 +169,6 @@ void CurvesData::write_uv_maps(Curves *curves) void CurvesData::write_materials() { - Object *object = (Object *)id; Material *mat = nullptr; /* TODO: Using only first material. Add support for multimaterial. */ if (BKE_object_material_count_eval(object) > 0) { diff --git a/source/blender/render/hydra/scene_delegate/curves.h b/source/blender/render/hydra/scene_delegate/curves.h index 92e742b4b1ba..ac237edbfe8f 100644 --- a/source/blender/render/hydra/scene_delegate/curves.h +++ b/source/blender/render/hydra/scene_delegate/curves.h @@ -24,13 +24,12 @@ class CurvesData : public ObjectData { void remove() override; void update() override; - pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; - bool update_visibility() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; - pxr::HdBasisCurvesTopology curves_topology(pxr::SdfPath const &id) const; + pxr::HdBasisCurvesTopology topology() const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::SdfPath material_id() const; - void available_materials(Set &paths) const; + void available_materials(Set &paths) const override; private: void write_curves(Curves *curves); diff --git a/source/blender/render/hydra/scene_delegate/id.cc b/source/blender/render/hydra/scene_delegate/id.cc index 1cf8d6b9dd34..c2c6d4b6d46d 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -7,14 +7,9 @@ namespace blender::render::hydra { -IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id) - : id(id), prim_id(prim_id), scene_delegate_(scene_delegate) +IdData::IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) + : prim_id(prim_id), scene_delegate_(scene_delegate) { } -pxr::VtValue IdData::get_data(pxr::TfToken const & /*key*/) const -{ - return pxr::VtValue(); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 089871c0927a..b2a2c3de580e 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -13,14 +13,14 @@ template<> struct blender::DefaultHash { uint64_t operator()(const pxr::SdfPath &value) const { - return pxr::SdfPath::Hash()(value); + return (uint64_t)value.GetHash(); } }; template<> struct blender::DefaultHash { uint64_t operator()(const pxr::TfToken &value) const { - return pxr::TfHash()(value); + return (uint64_t)value.Hash(); } }; @@ -30,7 +30,7 @@ class BlenderSceneDelegate; class IdData { public: - IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id); + IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); virtual ~IdData() = default; virtual void init() = 0; @@ -38,27 +38,15 @@ class IdData { virtual void remove() = 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; + virtual pxr::VtValue get_data(pxr::TfToken const &key) const = 0; - ID *id; pxr::SdfPath prim_id; protected: BlenderSceneDelegate *scene_delegate_; }; -template const T IdData::get_data(pxr::TfToken const &key) const -{ - return get_data(key).Get(); -} - #define ID_LOG(level, msg, ...) \ - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ - level, \ - "%s (%s): " msg, \ - prim_id.GetText(), \ - id ? id->name : "", \ - ##__VA_ARGS__); + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, level, "%s: " msg, prim_id.GetText(), ##__VA_ARGS__); } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index c0c6b5a5c794..319d87dcb438 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -12,7 +12,7 @@ namespace blender::render::hydra { InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, nullptr, prim_id) + : IdData(scene_delegate, prim_id) { } @@ -163,7 +163,7 @@ void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli) inst->data = std::make_unique(scene_delegate_, ob, p_id); inst->data->init(); } - ID_LOG(2, "Light %s %d", inst->data->id->name, (int)inst->transforms.size()); + ID_LOG(2, "Light %s %d", inst->data->object->id.name, (int)inst->transforms.size()); inst->transforms.push_back(gf_matrix_from_transform(dupli->mat)); } else { @@ -177,7 +177,7 @@ void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli) else { inst->data->update(); } - ID_LOG(2, "Mesh %s %d", inst->data->id->name, (int)mesh_transforms_.size()); + ID_LOG(2, "Mesh %s %d", inst->data->object->id.name, (int)mesh_transforms_.size()); inst->indices.push_back(mesh_transforms_.size()); mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); } @@ -260,7 +260,7 @@ void InstancerData::update_light_instance(LightInstance &inst) } /* Update current light instances */ - if (inst.data->prim_type((Light *)((Object *)l_data.id)->data) != l_data.prim_type_) { + if (inst.data->prim_type((Light *)l_data.object->data) != l_data.prim_type_) { /* Recreate instances when prim_type was changed */ for (i = 0; i < inst.count; ++i) { p = light_prim_id(inst, i); @@ -271,13 +271,13 @@ void InstancerData::update_light_instance(LightInstance &inst) for (i = 0; i < inst.count; ++i) { p = light_prim_id(inst, i); render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p); - ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.id->name); + ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.object->id.name); } } else { /* Update light instances*/ pxr::HdDirtyBits bits = pxr::HdLight::DirtyTransform; - Object *obj = (Object *)inst.data->id; + Object *obj = inst.data->object; if (obj->id.recalc & ID_RECALC_GEOMETRY || ((ID *)obj->data)->recalc & ID_RECALC_GEOMETRY) { l_data.init(); bits = pxr::HdLight::AllDirty; @@ -285,7 +285,7 @@ void InstancerData::update_light_instance(LightInstance &inst) for (i = 0; i < inst.count; ++i) { p = light_prim_id(inst, i); render_index.GetChangeTracker().MarkSprimDirty(p, bits); - ID_LOG(2, "Update %s (%s)", p.GetText(), l_data.id->name); + ID_LOG(2, "Update %s (%s)", p.GetText(), l_data.object->id.name); } } @@ -293,7 +293,7 @@ void InstancerData::update_light_instance(LightInstance &inst) while (inst.count < inst.transforms.size()) { p = light_prim_id(inst, inst.count); render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p); - ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.id->name); + ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.object->id.name); ++inst.count; } } diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index a7e754ce2ab0..c2584c339eb3 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -26,7 +26,7 @@ void LightData::init() { ID_LOG(1, ""); - Light *light = (Light *)((Object *)id)->data; + Light *light = (Light *)object->data; data_.clear(); switch (light->type) { @@ -109,10 +109,9 @@ void LightData::remove() void LightData::update() { - Object *object = (Object *)id; Light *light = (Light *)object->data; pxr::HdDirtyBits bits = pxr::HdLight::Clean; - if (id->recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) { + if (object->id.recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) { if (prim_type(light) != prim_type_) { remove(); init(); @@ -122,7 +121,7 @@ void LightData::update() init(); bits = pxr::HdLight::AllDirty; } - else if (id->recalc & ID_RECALC_TRANSFORM) { + else if (object->id.recalc & ID_RECALC_TRANSFORM) { write_transform(); bits = pxr::HdLight::DirtyTransform; } @@ -148,17 +147,6 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key) const return pxr::VtValue(); } -bool LightData::update_visibility() -{ - bool ret = ObjectData::update_visibility(); - if (ret) { - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, - pxr::HdLight::DirtyParams); - ID_LOG(1, ""); - } - return ret; -} - pxr::TfToken LightData::prim_type(Light *light) { switch (light->type) { diff --git a/source/blender/render/hydra/scene_delegate/light.h b/source/blender/render/hydra/scene_delegate/light.h index 215b82c773c9..77a3286659e4 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -27,7 +27,6 @@ class LightData : public ObjectData { void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; - bool update_visibility() override; private: pxr::TfToken prim_type(Light *light); diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index e2aaf54a1789..b82995254035 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -25,14 +25,14 @@ namespace blender::render::hydra { MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, (ID *)material, prim_id) + : IdData(scene_delegate, prim_id), material(material) { } void MaterialData::init() { ID_LOG(1, ""); - double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0; + double_sided = (material->blend_flag & MA_BL_CULL_BACKFACE) == 0; export_mtlx(); if (scene_delegate_->settings.mx_filename_key.IsEmpty()) { @@ -105,12 +105,12 @@ void MaterialData::export_mtlx() func = PyDict_GetItemString(dict, "export_mtlx"); PointerRNA materialptr; - RNA_pointer_create(NULL, &RNA_Material, id, &materialptr); - PyObject *material = pyrna_struct_CreatePyObject(&materialptr); + RNA_pointer_create(NULL, &RNA_Material, material, &materialptr); + PyObject *pymaterial = pyrna_struct_CreatePyObject(&materialptr); - result = PyObject_CallFunction(func, "O", material); + result = PyObject_CallFunction(func, "O", pymaterial); - Py_DECREF(material); + Py_DECREF(pymaterial); std::string path; @@ -135,7 +135,7 @@ void MaterialData::export_mtlx() CLOG_ERROR(LOG_RENDER_HYDRA_SCENE, "Export error for %s (%s): %s", prim_id.GetText(), - id->name, + material->id.name, err_str.c_str()); if (traceback) { PyTraceBack_Print(traceback, PySys_GetObject("stderr")); diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index b366ed548c42..da13f16abec0 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -29,6 +29,7 @@ class MaterialData : public IdData { pxr::VtValue get_material_resource() const; pxr::HdCullStyle cull_style() const; + Material *material; bool double_sided = true; private: diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 81136635efc6..04660cca95a8 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -29,7 +29,6 @@ void MeshData::init() { ID_LOG(1, ""); - Object *object = (Object *)id; Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); if (mesh) { write_submeshes(mesh); @@ -55,18 +54,19 @@ void MeshData::remove() void MeshData::update() { - Object *object = (Object *)id; - if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { + if ((object->id.recalc & ID_RECALC_GEOMETRY) || + (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) + { init(); return; } pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - if (id->recalc & ID_RECALC_SHADING) { + if (object->id.recalc & ID_RECALC_SHADING) { write_materials(); bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; } - if (id->recalc & ID_RECALC_TRANSFORM) { + if (object->id.recalc & ID_RECALC_TRANSFORM) { write_transform(); bits |= pxr::HdChangeTracker::DirtyTransform; } @@ -81,31 +81,23 @@ void MeshData::update() } } -pxr::VtValue MeshData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const +pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const { if (key == pxr::HdTokens->points) { return pxr::VtValue(vertices_); } - else if (key == pxr::HdTokens->normals) { + return pxr::VtValue(); +} + +pxr::VtValue MeshData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const +{ + if (key == pxr::HdTokens->normals) { return pxr::VtValue(submesh(id).normals); } else if (key == pxr::_tokens->st) { return pxr::VtValue(submesh(id).uvs); } - return pxr::VtValue(); -} - -bool MeshData::update_visibility() -{ - bool ret = ObjectData::update_visibility(); - if (ret) { - for (int i = 0; i < submeshes_.size(); ++i) { - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - submesh_prim_id(i), pxr::HdChangeTracker::DirtyVisibility); - ID_LOG(1, "%d", i); - } - } - return ret; + return get_data(key); } pxr::HdMeshTopology MeshData::mesh_topology(pxr::SdfPath const &id) const @@ -217,7 +209,7 @@ void MeshData::write_submeshes(Mesh *mesh) vertices_.clear(); /* Insert base submeshes */ - int mat_count = BKE_object_material_count_eval((Object *)id); + int mat_count = BKE_object_material_count_eval(object); for (int i = 0; i < std::max(mat_count, 1); ++i) { SubMesh sm; sm.mat_index = i; @@ -298,7 +290,6 @@ void MeshData::write_submeshes(Mesh *mesh) void MeshData::write_materials() { - Object *object = (Object *)id; for (int i = 0; i < submeshes_.size(); ++i) { SubMesh &m = submeshes_[i]; Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 649056adac0c..b0f3e9425cbd 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -32,8 +32,8 @@ class MeshData : public ObjectData { void remove() override; void update() override; - pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; - bool update_visibility() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; + pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const override; pxr::HdMeshTopology mesh_topology(pxr::SdfPath const &id) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index e3b4a18b5139..131fcbeff626 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -15,7 +15,7 @@ namespace blender::render::hydra { ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, (ID *)object, prim_id), transform(pxr::GfMatrix4d(1.0)) + : IdData(scene_delegate, prim_id), object(object), transform(pxr::GfMatrix4d(1.0)) { } @@ -43,6 +43,7 @@ std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_deleg data = std::make_unique(scene_delegate, object, prim_id); break; default: + BLI_assert_unreachable(); break; } return data; @@ -78,18 +79,20 @@ bool ObjectData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object return ret; } -bool ObjectData::update_visibility() +pxr::VtValue ObjectData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const { - bool prev_visible = visible; - visible = is_visible(scene_delegate_, (Object *)id); - return visible != prev_visible; + return get_data(key); } +void ObjectData::available_materials(Set &paths) const {} + void ObjectData::write_transform() { - transform = gf_matrix_from_transform(((Object *)id)->object_to_world); + transform = gf_matrix_from_transform(object->object_to_world); } +void ObjectData::write_materials() {} + pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]) { pxr::GfMatrix4d ret; diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 1a31a0cbf245..52b202419579 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -9,6 +9,7 @@ #include "BKE_layer.h" #include "BKE_object.h" #include "BLI_map.hh" +#include "BLI_set.hh" #include "DNA_object_types.h" #include "id.h" @@ -28,13 +29,17 @@ class ObjectData : public IdData { Object *object, int mode = OB_VISIBLE_SELF); - virtual bool update_visibility(); + using IdData::get_data; + virtual pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; + virtual void available_materials(Set &paths) const; + Object *object; pxr::GfMatrix4d transform; bool visible = true; protected: - void write_transform(); + virtual void write_transform(); + virtual void write_materials(); }; using ObjectDataMap = Map>; diff --git a/source/blender/render/hydra/scene_delegate/volume.cc b/source/blender/render/hydra/scene_delegate/volume.cc index ec270c8ba9bf..682bffb12b90 100644 --- a/source/blender/render/hydra/scene_delegate/volume.cc +++ b/source/blender/render/hydra/scene_delegate/volume.cc @@ -28,7 +28,7 @@ VolumeData::VolumeData(BlenderSceneDelegate *scene_delegate, void VolumeData::init() { ID_LOG(1, ""); - Volume *volume = (Volume *)((Object *)this->id)->data; + Volume *volume = (Volume *)object->data; Main *main = CTX_data_main(scene_delegate_->context); if (!BKE_volume_load(volume, main)) { return; @@ -80,17 +80,18 @@ void VolumeData::remove() void VolumeData::update() { - Object *object = (Object *)id; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { + if ((object->id.recalc & ID_RECALC_GEOMETRY) || + (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) + { init(); bits = pxr::HdChangeTracker::AllDirty; } - if (id->recalc & ID_RECALC_SHADING) { + if (object->id.recalc & ID_RECALC_SHADING) { write_materials(); bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; } - if (id->recalc & ID_RECALC_TRANSFORM) { + if (object->id.recalc & ID_RECALC_TRANSFORM) { write_transform(); bits |= pxr::HdChangeTracker::DirtyTransform; } @@ -103,13 +104,13 @@ void VolumeData::update() ID_LOG(1, ""); } -pxr::VtValue VolumeData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const +pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const { if (key == pxr::HdVolumeFieldSchemaTokens->filePath) { return pxr::VtValue(pxr::SdfAssetPath(filepath_, filepath_)); } if (key == pxr::HdVolumeFieldSchemaTokens->fieldName) { - std::string name = id.GetName(); + std::string name = prim_id.GetName(); return pxr::VtValue(pxr::TfToken(name.substr(name.find("VF_") + 3))); } if (key == pxr::HdVolumeFieldSchemaTokens->fieldIndex) { @@ -121,17 +122,6 @@ pxr::VtValue VolumeData::get_data(pxr::SdfPath const &id, pxr::TfToken const &ke return pxr::VtValue(); } -bool VolumeData::update_visibility() -{ - bool ret = ObjectData::update_visibility(); - if (ret) { - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - prim_id, pxr::HdChangeTracker::DirtyVisibility); - ID_LOG(1, ""); - } - return ret; -} - pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const { return field_descriptors_; @@ -154,7 +144,6 @@ void VolumeData::available_materials(Set &paths) const void VolumeData::write_materials() { - Object *object = (Object *)id; Material *mat = nullptr; /* TODO: Using only first material. Add support for multimaterial. */ if (BKE_object_material_count_eval(object) > 0) { diff --git a/source/blender/render/hydra/scene_delegate/volume.h b/source/blender/render/hydra/scene_delegate/volume.h index dba00d2521c4..9bd47c27d3a7 100644 --- a/source/blender/render/hydra/scene_delegate/volume.h +++ b/source/blender/render/hydra/scene_delegate/volume.h @@ -19,12 +19,11 @@ class VolumeData : public ObjectData { void remove() override; void update() override; - pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; - bool update_visibility() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::HdVolumeFieldDescriptorVector field_descriptors() const; pxr::SdfPath material_id() const; - void available_materials(Set &paths) const; + void available_materials(Set &paths) const override; private: void write_materials(); diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index c7f7aa746286..f2e14b1c82d0 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -33,7 +33,7 @@ namespace blender::render::hydra { WorldData::WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, nullptr, prim_id) + : IdData(scene_delegate, prim_id) { } -- 2.30.2 From 84e11ae4307fe2faa08456be924c96d9b9489e17 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 17 Jul 2023 14:02:43 +0300 Subject: [PATCH 2/9] Finished refactoring light and world. Fixed bug with PreviewEngine --- source/blender/render/hydra/preview_engine.cc | 6 +-- .../render/hydra/scene_delegate/light.cc | 10 ++-- .../render/hydra/scene_delegate/light.h | 2 +- .../render/hydra/scene_delegate/object.h | 8 +++ .../render/hydra/scene_delegate/world.cc | 53 +++++-------------- .../render/hydra/scene_delegate/world.h | 17 ++---- 6 files changed, 33 insertions(+), 63 deletions(-) diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index bfe3572df3f2..48a9d5073fe0 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -10,11 +10,9 @@ namespace blender::render::hydra { void PreviewEngine::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); + render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); - { - /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ - engine_->Execute(render_index_.get(), &tasks_); - } + engine_->Execute(render_index_.get(), &tasks_); std::vector &pixels = render_images_["Combined"]; while (true) { diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index c2584c339eb3..1d7d6202a28f 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -24,7 +24,7 @@ LightData::LightData(BlenderSceneDelegate *scene_delegate, void LightData::init() { - ID_LOG(1, ""); + OBJ_LOG(1, ""); Light *light = (Light *)object->data; data_.clear(); @@ -97,13 +97,13 @@ void LightData::init() void LightData::insert() { - ID_LOG(1, ""); + OBJ_LOG(1, ""); scene_delegate_->GetRenderIndex().InsertSprim(prim_type_, scene_delegate_, prim_id); } void LightData::remove() { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText()); + ID_LOG(1, ""); scene_delegate_->GetRenderIndex().RemoveSprim(prim_type_, prim_id); } @@ -127,13 +127,13 @@ void LightData::update() } if (bits != pxr::HdChangeTracker::Clean) { scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits); - ID_LOG(1, ""); + OBJ_LOG(1, ""); } } pxr::VtValue LightData::get_data(pxr::TfToken const &key) const { - ID_LOG(3, "%s", key.GetText()); + OBJ_LOG(3, "%s", key.GetText()); auto it = data_.find(key); if (it != data_.end()) { return pxr::VtValue(it->second); diff --git a/source/blender/render/hydra/scene_delegate/light.h b/source/blender/render/hydra/scene_delegate/light.h index 77a3286659e4..fb4eb234ce30 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -28,7 +28,7 @@ class LightData : public ObjectData { pxr::VtValue get_data(pxr::TfToken const &key) const override; - private: + protected: pxr::TfToken prim_type(Light *light); std::map data_; diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 52b202419579..0aefabbcf210 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -46,4 +46,12 @@ using ObjectDataMap = Map>; pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]); +#define OBJ_LOG(level, msg, ...) \ + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ + level, \ + "%s (%s): " msg, \ + prim_id.GetText(), \ + object ? object->id.name : "", \ + ##__VA_ARGS__); + } // 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 f2e14b1c82d0..998a84def18e 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -33,14 +33,13 @@ namespace blender::render::hydra { WorldData::WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, prim_id) + : LightData(scene_delegate, nullptr, prim_id) { + prim_type_ = pxr::HdPrimTypeTokens->domeLight; } void WorldData::init() { - write_transform(); - data_.clear(); data_[pxr::UsdLuxTokens->orientToStageUpAxis] = true; @@ -51,7 +50,7 @@ void WorldData::init() if (scene_delegate_->shading_settings.use_scene_world) { World *world = scene_delegate_->scene->world; - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s: %s", prim_id.GetText(), world->id.name); + ID_LOG(1, "%s", world->id.name); exposure = world->exposure; if (world->use_nodes) { @@ -115,21 +114,13 @@ void WorldData::init() } } else { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, - 1, - "%s: studiolight: %s", - prim_id.GetText(), - scene_delegate_->shading_settings.studiolight_name.c_str()); + ID_LOG(1, "studiolight: %s", scene_delegate_->shading_settings.studiolight_name.c_str()); StudioLight *sl = BKE_studiolight_find( scene_delegate_->shading_settings.studiolight_name.c_str(), STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE); if (sl != NULL && sl->flag & STUDIOLIGHT_TYPE_WORLD) { texture_file = pxr::SdfAssetPath(sl->filepath, sl->filepath); - transform *= pxr::GfMatrix4d( - pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, -1.0), - RAD2DEGF(scene_delegate_->shading_settings.studiolight_rotation)), - pxr::GfVec3d()); /* coefficient to follow Cycles result */ intensity = scene_delegate_->shading_settings.studiolight_intensity / 2; } @@ -139,45 +130,27 @@ void WorldData::init() data_[pxr::HdLightTokens->exposure] = exposure; data_[pxr::HdLightTokens->color] = color; data_[pxr::HdLightTokens->textureFile] = texture_file; -} -void WorldData::insert() -{ - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText()); - scene_delegate_->GetRenderIndex().InsertSprim( - pxr::HdPrimTypeTokens->domeLight, scene_delegate_, prim_id); -} - -void WorldData::remove() -{ - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText()); - scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, prim_id); + write_transform(); } void WorldData::update() { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText()); + ID_LOG(1, ""); init(); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, pxr::HdLight::AllDirty); } -pxr::VtValue WorldData::get_data(pxr::TfToken const &key) const -{ - auto it = data_.find(key); - if (it != data_.end()) { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s: %s", prim_id.GetText(), key.GetText()); - return pxr::VtValue(it->second); - } - return pxr::VtValue(); -} - void WorldData::write_transform() { - transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), pxr::GfVec3d()); - - transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), pxr::GfVec3d()); - transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), pxr::GfVec3d()); + transform = pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), 90.0)) * + pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0)); + if (!scene_delegate_->shading_settings.use_scene_world) { + transform *= pxr::GfMatrix4d().SetRotate( + pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, -1.0), + RAD2DEGF(scene_delegate_->shading_settings.studiolight_rotation))); + } } } // 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 c6183ab0c1c7..e7c48dd250a7 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -14,28 +14,19 @@ #include "DNA_view3d_types.h" #include "DNA_world_types.h" -#include "id.h" +#include "light.h" namespace blender::render::hydra { -class WorldData : public IdData { +class WorldData : public LightData { public: WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); void init() override; - void insert() override; - void remove() override; void update() override; - void update(World *world); - pxr::VtValue get_data(pxr::TfToken const &key) const override; - - pxr::GfMatrix4d transform; - - private: - void write_transform(); - - std::map data_; + protected: + void write_transform() override; }; } // namespace blender::render::hydra -- 2.30.2 From 1f4e8b212693f3115c99a975938ed2e71f9e69d0 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 17 Jul 2023 20:25:18 +0300 Subject: [PATCH 3/9] Reverting some changes. Improved logging --- .../render/hydra/scene_delegate/curves.cc | 19 +++++++------ .../blender/render/hydra/scene_delegate/id.cc | 4 +-- .../blender/render/hydra/scene_delegate/id.h | 11 +++++++- .../render/hydra/scene_delegate/instancer.cc | 16 +++++------ .../render/hydra/scene_delegate/light.cc | 15 +++++----- .../render/hydra/scene_delegate/material.cc | 28 +++++++++---------- .../render/hydra/scene_delegate/material.h | 1 - .../render/hydra/scene_delegate/mesh.cc | 25 +++++++++-------- .../render/hydra/scene_delegate/mesh.h | 2 +- .../render/hydra/scene_delegate/object.cc | 4 +-- .../render/hydra/scene_delegate/object.h | 9 ------ .../render/hydra/scene_delegate/volume.cc | 24 ++++++++-------- .../render/hydra/scene_delegate/world.cc | 6 ++++ .../render/hydra/scene_delegate/world.h | 1 + 14 files changed, 87 insertions(+), 78 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 19391e27b13d..73ae29ff17f7 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -20,8 +20,9 @@ CurvesData::CurvesData(BlenderSceneDelegate *scene_delegate, void CurvesData::init() { - ID_LOG(1, ""); + ID_LOGN(1, ""); + Object *object = (Object *)id; write_curves((Curves *)object->data); write_transform(); write_materials(); @@ -29,31 +30,30 @@ void CurvesData::init() void CurvesData::insert() { - ID_LOG(1, ""); + ID_LOGN(1, ""); scene_delegate_->GetRenderIndex().InsertRprim( pxr::HdPrimTypeTokens->basisCurves, scene_delegate_, prim_id); } void CurvesData::remove() { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText()); + ID_LOG(1, ""); scene_delegate_->GetRenderIndex().RemoveRprim(prim_id); } void CurvesData::update() { + Object *object = (Object *)id; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - if ((object->id.recalc & ID_RECALC_GEOMETRY) || - (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) - { + if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { init(); bits = pxr::HdChangeTracker::AllDirty; } - if (object->id.recalc & ID_RECALC_SHADING) { + if (id->recalc & ID_RECALC_SHADING) { write_materials(); bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; } - if (object->id.recalc & ID_RECALC_TRANSFORM) { + if (id->recalc & ID_RECALC_TRANSFORM) { write_transform(); bits |= pxr::HdChangeTracker::DirtyTransform; } @@ -63,7 +63,7 @@ void CurvesData::update() } scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(prim_id, bits); - ID_LOG(1, ""); + ID_LOGN(1, ""); } pxr::VtValue CurvesData::get_data(pxr::TfToken const &key) const @@ -169,6 +169,7 @@ void CurvesData::write_uv_maps(Curves *curves) void CurvesData::write_materials() { + Object *object = (Object *)id; Material *mat = nullptr; /* TODO: Using only first material. Add support for multimaterial. */ if (BKE_object_material_count_eval(object) > 0) { diff --git a/source/blender/render/hydra/scene_delegate/id.cc b/source/blender/render/hydra/scene_delegate/id.cc index c2c6d4b6d46d..ed14c8a6cba4 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -7,8 +7,8 @@ namespace blender::render::hydra { -IdData::IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) - : prim_id(prim_id), scene_delegate_(scene_delegate) +IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id) + : id(id), prim_id(prim_id), scene_delegate_(scene_delegate) { } diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index b2a2c3de580e..6be2238051bb 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -30,7 +30,7 @@ class BlenderSceneDelegate; class IdData { public: - IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); + IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id); virtual ~IdData() = default; virtual void init() = 0; @@ -40,6 +40,7 @@ class IdData { virtual pxr::VtValue get_data(pxr::TfToken const &key) const = 0; + ID *id; pxr::SdfPath prim_id; protected: @@ -49,4 +50,12 @@ class IdData { #define ID_LOG(level, msg, ...) \ CLOG_INFO(LOG_RENDER_HYDRA_SCENE, level, "%s: " msg, prim_id.GetText(), ##__VA_ARGS__); +#define ID_LOGN(level, msg, ...) \ + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ + level, \ + "%s (%s): " msg, \ + prim_id.GetText(), \ + id->name, \ + ##__VA_ARGS__); + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 319d87dcb438..c0c6b5a5c794 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -12,7 +12,7 @@ namespace blender::render::hydra { InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, prim_id) + : IdData(scene_delegate, nullptr, prim_id) { } @@ -163,7 +163,7 @@ void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli) inst->data = std::make_unique(scene_delegate_, ob, p_id); inst->data->init(); } - ID_LOG(2, "Light %s %d", inst->data->object->id.name, (int)inst->transforms.size()); + ID_LOG(2, "Light %s %d", inst->data->id->name, (int)inst->transforms.size()); inst->transforms.push_back(gf_matrix_from_transform(dupli->mat)); } else { @@ -177,7 +177,7 @@ void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli) else { inst->data->update(); } - ID_LOG(2, "Mesh %s %d", inst->data->object->id.name, (int)mesh_transforms_.size()); + ID_LOG(2, "Mesh %s %d", inst->data->id->name, (int)mesh_transforms_.size()); inst->indices.push_back(mesh_transforms_.size()); mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); } @@ -260,7 +260,7 @@ void InstancerData::update_light_instance(LightInstance &inst) } /* Update current light instances */ - if (inst.data->prim_type((Light *)l_data.object->data) != l_data.prim_type_) { + if (inst.data->prim_type((Light *)((Object *)l_data.id)->data) != l_data.prim_type_) { /* Recreate instances when prim_type was changed */ for (i = 0; i < inst.count; ++i) { p = light_prim_id(inst, i); @@ -271,13 +271,13 @@ void InstancerData::update_light_instance(LightInstance &inst) for (i = 0; i < inst.count; ++i) { p = light_prim_id(inst, i); render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p); - ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.object->id.name); + ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.id->name); } } else { /* Update light instances*/ pxr::HdDirtyBits bits = pxr::HdLight::DirtyTransform; - Object *obj = inst.data->object; + Object *obj = (Object *)inst.data->id; if (obj->id.recalc & ID_RECALC_GEOMETRY || ((ID *)obj->data)->recalc & ID_RECALC_GEOMETRY) { l_data.init(); bits = pxr::HdLight::AllDirty; @@ -285,7 +285,7 @@ void InstancerData::update_light_instance(LightInstance &inst) for (i = 0; i < inst.count; ++i) { p = light_prim_id(inst, i); render_index.GetChangeTracker().MarkSprimDirty(p, bits); - ID_LOG(2, "Update %s (%s)", p.GetText(), l_data.object->id.name); + ID_LOG(2, "Update %s (%s)", p.GetText(), l_data.id->name); } } @@ -293,7 +293,7 @@ void InstancerData::update_light_instance(LightInstance &inst) while (inst.count < inst.transforms.size()) { p = light_prim_id(inst, inst.count); render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p); - ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.object->id.name); + ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.id->name); ++inst.count; } } diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index 1d7d6202a28f..2766d4de7e04 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -24,9 +24,9 @@ LightData::LightData(BlenderSceneDelegate *scene_delegate, void LightData::init() { - OBJ_LOG(1, ""); + ID_LOGN(1, ""); - Light *light = (Light *)object->data; + Light *light = (Light *)((Object *)id)->data; data_.clear(); switch (light->type) { @@ -97,7 +97,7 @@ void LightData::init() void LightData::insert() { - OBJ_LOG(1, ""); + ID_LOGN(1, ""); scene_delegate_->GetRenderIndex().InsertSprim(prim_type_, scene_delegate_, prim_id); } @@ -109,9 +109,10 @@ void LightData::remove() void LightData::update() { + Object *object = (Object *)id; Light *light = (Light *)object->data; pxr::HdDirtyBits bits = pxr::HdLight::Clean; - if (object->id.recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) { + if (id->recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) { if (prim_type(light) != prim_type_) { remove(); init(); @@ -121,19 +122,19 @@ void LightData::update() init(); bits = pxr::HdLight::AllDirty; } - else if (object->id.recalc & ID_RECALC_TRANSFORM) { + else if (id->recalc & ID_RECALC_TRANSFORM) { write_transform(); bits = pxr::HdLight::DirtyTransform; } if (bits != pxr::HdChangeTracker::Clean) { scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits); - OBJ_LOG(1, ""); + ID_LOGN(1, ""); } } pxr::VtValue LightData::get_data(pxr::TfToken const &key) const { - OBJ_LOG(3, "%s", key.GetText()); + ID_LOGN(3, "%s", key.GetText()); auto it = data_.find(key); if (it != data_.end()) { return pxr::VtValue(it->second); diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index b82995254035..9c8005874cfc 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -25,14 +25,14 @@ namespace blender::render::hydra { MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, prim_id), material(material) + : IdData(scene_delegate, (ID *)material, prim_id) { } void MaterialData::init() { - ID_LOG(1, ""); - double_sided = (material->blend_flag & MA_BL_CULL_BACKFACE) == 0; + ID_LOGN(1, ""); + double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0; export_mtlx(); if (scene_delegate_->settings.mx_filename_key.IsEmpty()) { @@ -42,20 +42,20 @@ void MaterialData::init() void MaterialData::insert() { - ID_LOG(1, ""); + ID_LOGN(1, ""); scene_delegate_->GetRenderIndex().InsertSprim( pxr::HdPrimTypeTokens->material, scene_delegate_, prim_id); } void MaterialData::remove() { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText()); + ID_LOG(1, ""); scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, prim_id); } void MaterialData::update() { - ID_LOG(1, ""); + ID_LOGN(1, ""); bool prev_double_sided = double_sided; init(); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, @@ -74,7 +74,7 @@ void MaterialData::update() pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const { if (key == scene_delegate_->settings.mx_filename_key) { - ID_LOG(3, "%s", key.GetText()); + ID_LOGN(3, "%s", key.GetText()); if (!mtlx_path_.GetResolvedPath().empty()) { return pxr::VtValue(mtlx_path_); } @@ -105,12 +105,12 @@ void MaterialData::export_mtlx() func = PyDict_GetItemString(dict, "export_mtlx"); PointerRNA materialptr; - RNA_pointer_create(NULL, &RNA_Material, material, &materialptr); - PyObject *pymaterial = pyrna_struct_CreatePyObject(&materialptr); + RNA_pointer_create(NULL, &RNA_Material, id, &materialptr); + PyObject *material = pyrna_struct_CreatePyObject(&materialptr); - result = PyObject_CallFunction(func, "O", pymaterial); + result = PyObject_CallFunction(func, "O", material); - Py_DECREF(pymaterial); + Py_DECREF(material); std::string path; @@ -135,7 +135,7 @@ void MaterialData::export_mtlx() CLOG_ERROR(LOG_RENDER_HYDRA_SCENE, "Export error for %s (%s): %s", prim_id.GetText(), - material->id.name, + id->name, err_str.c_str()); if (traceback) { PyTraceBack_Print(traceback, PySys_GetObject("stderr")); @@ -149,12 +149,12 @@ void MaterialData::export_mtlx() PyGILState_Release(gstate); mtlx_path_ = pxr::SdfAssetPath(path, path); - ID_LOG(1, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str()); + ID_LOGN(1, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str()); } void MaterialData::write_material_network_map() { - ID_LOG(1, ""); + ID_LOGN(1, ""); if (mtlx_path_.GetResolvedPath().empty()) { material_network_map_ = pxr::VtValue(); return; diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index da13f16abec0..b366ed548c42 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -29,7 +29,6 @@ class MaterialData : public IdData { pxr::VtValue get_material_resource() const; pxr::HdCullStyle cull_style() const; - Material *material; bool double_sided = true; private: diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 04660cca95a8..d40cf372018f 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -27,8 +27,9 @@ MeshData::MeshData(BlenderSceneDelegate *scene_delegate, void MeshData::init() { - ID_LOG(1, ""); + ID_LOGN(1, ""); + Object *object = (Object *)id; Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); if (mesh) { write_submeshes(mesh); @@ -48,25 +49,24 @@ void MeshData::remove() { for (int i = 0; i < submeshes_.size(); ++i) { scene_delegate_->GetRenderIndex().RemoveRprim(submesh_prim_id(i)); - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s: %d", prim_id.GetText(), i); + ID_LOG(1, "%d", i); } } void MeshData::update() { - if ((object->id.recalc & ID_RECALC_GEOMETRY) || - (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) - { + Object *object = (Object *)id; + if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { init(); return; } pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - if (object->id.recalc & ID_RECALC_SHADING) { + if (id->recalc & ID_RECALC_SHADING) { write_materials(); bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; } - if (object->id.recalc & ID_RECALC_TRANSFORM) { + if (id->recalc & ID_RECALC_TRANSFORM) { write_transform(); bits |= pxr::HdChangeTracker::DirtyTransform; } @@ -77,7 +77,7 @@ void MeshData::update() for (int i = 0; i < submeshes_.size(); ++i) { scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(submesh_prim_id(i), bits); - ID_LOG(1, "%d", i); + ID_LOGN(1, "%d", i); } } @@ -165,7 +165,7 @@ void MeshData::update_double_sided(MaterialData *mat_data) scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( submesh_prim_id(i), pxr::HdChangeTracker::DirtyDoubleSided | pxr::HdChangeTracker::DirtyCullStyle); - ID_LOG(1, "%d", i); + ID_LOGN(1, "%d", i); } } } @@ -209,7 +209,7 @@ void MeshData::write_submeshes(Mesh *mesh) vertices_.clear(); /* Insert base submeshes */ - int mat_count = BKE_object_material_count_eval(object); + int mat_count = BKE_object_material_count_eval((Object *)id); for (int i = 0; i < std::max(mat_count, 1); ++i) { SubMesh sm; sm.mat_index = i; @@ -275,11 +275,11 @@ void MeshData::write_submeshes(Mesh *mesh) pxr::SdfPath p = submesh_prim_id(i); if (i < sub_meshes_prev_count) { render_index.GetChangeTracker().MarkRprimDirty(p, pxr::HdChangeTracker::AllDirty); - ID_LOG(1, "Update %d", i); + ID_LOGN(1, "Update %d", i); } else { render_index.InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate_, p); - ID_LOG(1, "Insert %d", i); + ID_LOGN(1, "Insert %d", i); } } for (; i < sub_meshes_prev_count; ++i) { @@ -290,6 +290,7 @@ void MeshData::write_submeshes(Mesh *mesh) void MeshData::write_materials() { + Object *object = (Object *)id; for (int i = 0; i < submeshes_.size(); ++i) { SubMesh &m = submeshes_[i]; Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index b0f3e9425cbd..28d080467f95 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -41,7 +41,7 @@ class MeshData : public ObjectData { pxr::HdCullStyle cull_style(pxr::SdfPath const &id) const; bool double_sided(pxr::SdfPath const &id) const; void update_double_sided(MaterialData *mat_data); - void available_materials(Set &paths) const; + void available_materials(Set &paths) const override; pxr::SdfPathVector submesh_paths() const; private: diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 131fcbeff626..3eea72f69392 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -15,7 +15,7 @@ namespace blender::render::hydra { ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object, pxr::SdfPath const &prim_id) - : IdData(scene_delegate, prim_id), object(object), transform(pxr::GfMatrix4d(1.0)) + : IdData(scene_delegate, (ID *)object, prim_id), transform(pxr::GfMatrix4d(1.0)) { } @@ -88,7 +88,7 @@ void ObjectData::available_materials(Set &paths) const {} void ObjectData::write_transform() { - transform = gf_matrix_from_transform(object->object_to_world); + transform = gf_matrix_from_transform(((Object *)id)->object_to_world); } void ObjectData::write_materials() {} diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 0aefabbcf210..4b8ea121af09 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -33,7 +33,6 @@ class ObjectData : public IdData { virtual pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; virtual void available_materials(Set &paths) const; - Object *object; pxr::GfMatrix4d transform; bool visible = true; @@ -46,12 +45,4 @@ using ObjectDataMap = Map>; pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]); -#define OBJ_LOG(level, msg, ...) \ - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ - level, \ - "%s (%s): " msg, \ - prim_id.GetText(), \ - object ? object->id.name : "", \ - ##__VA_ARGS__); - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/volume.cc b/source/blender/render/hydra/scene_delegate/volume.cc index 682bffb12b90..340bfd720549 100644 --- a/source/blender/render/hydra/scene_delegate/volume.cc +++ b/source/blender/render/hydra/scene_delegate/volume.cc @@ -27,8 +27,8 @@ VolumeData::VolumeData(BlenderSceneDelegate *scene_delegate, void VolumeData::init() { - ID_LOG(1, ""); - Volume *volume = (Volume *)object->data; + ID_LOGN(1, ""); + Volume *volume = (Volume *)((Object *)this->id)->data; Main *main = CTX_data_main(scene_delegate_->context); if (!BKE_volume_load(volume, main)) { return; @@ -59,39 +59,38 @@ void VolumeData::insert() scene_delegate_->GetRenderIndex().InsertRprim( pxr::HdPrimTypeTokens->volume, scene_delegate_, prim_id); - ID_LOG(1, ""); + ID_LOGN(1, ""); for (auto &desc : field_descriptors_) { scene_delegate_->GetRenderIndex().InsertBprim( desc.fieldPrimType, scene_delegate_, desc.fieldId); - ID_LOG(1, "Volume field %s", desc.fieldId.GetText()); + ID_LOGN(1, "Volume field %s", desc.fieldId.GetText()); } } void VolumeData::remove() { for (auto &desc : field_descriptors_) { - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", desc.fieldId.GetText()); + ID_LOG(1, "%s", desc.fieldId.GetText()); scene_delegate_->GetRenderIndex().RemoveBprim(desc.fieldPrimType, desc.fieldId); } - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText()); + ID_LOG(1, ""); scene_delegate_->GetRenderIndex().RemoveRprim(prim_id); } void VolumeData::update() { + Object *object = (Object *)id; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - if ((object->id.recalc & ID_RECALC_GEOMETRY) || - (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) - { + if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { init(); bits = pxr::HdChangeTracker::AllDirty; } - if (object->id.recalc & ID_RECALC_SHADING) { + if (id->recalc & ID_RECALC_SHADING) { write_materials(); bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; } - if (object->id.recalc & ID_RECALC_TRANSFORM) { + if (id->recalc & ID_RECALC_TRANSFORM) { write_transform(); bits |= pxr::HdChangeTracker::DirtyTransform; } @@ -101,7 +100,7 @@ void VolumeData::update() } scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(prim_id, bits); - ID_LOG(1, ""); + ID_LOGN(1, ""); } pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const @@ -144,6 +143,7 @@ void VolumeData::available_materials(Set &paths) const void VolumeData::write_materials() { + Object *object = (Object *)id; Material *mat = nullptr; /* TODO: Using only first material. Add support for multimaterial. */ if (BKE_object_material_count_eval(object) > 0) { diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 998a84def18e..6db5b9ec4a66 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -134,6 +134,12 @@ void WorldData::init() write_transform(); } +void WorldData::insert() +{ + ID_LOG(1, ""); + scene_delegate_->GetRenderIndex().InsertSprim(prim_type_, scene_delegate_, prim_id); +} + void WorldData::update() { ID_LOG(1, ""); diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index e7c48dd250a7..da0981b53990 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -23,6 +23,7 @@ class WorldData : public LightData { WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); void init() override; + void insert() override; void update() override; protected: -- 2.30.2 From 9d3765ff91e7532ae55b9043c0bfe24773e3788e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 17 Jul 2023 20:59:45 +0300 Subject: [PATCH 4/9] Moved material related functions to ObjectData as virtual. Adjusted code by reordering some functions --- .../render/hydra/scene_delegate/curves.cc | 66 +++++++-------- .../render/hydra/scene_delegate/curves.h | 8 +- .../blender/render/hydra/scene_delegate/id.h | 2 +- .../render/hydra/scene_delegate/mesh.cc | 84 +++++++++---------- .../render/hydra/scene_delegate/mesh.h | 8 +- .../render/hydra/scene_delegate/object.cc | 10 +++ .../render/hydra/scene_delegate/object.h | 2 + .../render/hydra/scene_delegate/volume.cc | 10 +-- .../render/hydra/scene_delegate/volume.h | 11 +-- .../render/hydra/scene_delegate/world.cc | 6 -- .../render/hydra/scene_delegate/world.h | 1 - 11 files changed, 109 insertions(+), 99 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 73ae29ff17f7..568803b75476 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -80,6 +80,21 @@ pxr::VtValue CurvesData::get_data(pxr::TfToken const &key) const return pxr::VtValue(); } +pxr::SdfPath CurvesData::material_id() const +{ + if (!mat_data_) { + return pxr::SdfPath(); + } + return mat_data_->prim_id; +} + +void CurvesData::available_materials(Set &paths) const +{ + if (mat_data_ && !mat_data_->prim_id.IsEmpty()) { + paths.add(mat_data_->prim_id); + } +} + pxr::HdBasisCurvesTopology CurvesData::topology() const { return pxr::HdBasisCurvesTopology(pxr::HdTokens->linear, @@ -111,18 +126,27 @@ pxr::HdPrimvarDescriptorVector CurvesData::primvar_descriptors( return primvars; } -pxr::SdfPath CurvesData::material_id() const +void CurvesData::write_materials() { - if (!mat_data_) { - return pxr::SdfPath(); + Object *object = (Object *)id; + Material *mat = nullptr; + /* TODO: Using only first material. Add support for multimaterial. */ + if (BKE_object_material_count_eval(object) > 0) { + mat = BKE_object_material_get_eval(object, 0); } - return mat_data_->prim_id; -} -void CurvesData::available_materials(Set &paths) const -{ - if (mat_data_ && !mat_data_->prim_id.IsEmpty()) { - paths.add(mat_data_->prim_id); + if (!mat) { + mat_data_ = nullptr; + return; + } + pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); + mat_data_ = scene_delegate_->material_data(p_id); + if (!mat_data_) { + scene_delegate_->materials_.add_new( + p_id, std::make_unique(scene_delegate_, mat, p_id)); + mat_data_ = scene_delegate_->material_data(p_id); + mat_data_->init(); + mat_data_->insert(); } } @@ -167,28 +191,4 @@ void CurvesData::write_uv_maps(Curves *curves) } } -void CurvesData::write_materials() -{ - Object *object = (Object *)id; - Material *mat = nullptr; - /* TODO: Using only first material. Add support for multimaterial. */ - if (BKE_object_material_count_eval(object) > 0) { - mat = BKE_object_material_get_eval(object, 0); - } - - if (!mat) { - mat_data_ = nullptr; - return; - } - pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); - mat_data_ = scene_delegate_->material_data(p_id); - if (!mat_data_) { - scene_delegate_->materials_.add_new( - p_id, std::make_unique(scene_delegate_, mat, p_id)); - mat_data_ = scene_delegate_->material_data(p_id); - mat_data_->init(); - mat_data_->insert(); - } -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/curves.h b/source/blender/render/hydra/scene_delegate/curves.h index ac237edbfe8f..39d316db63ac 100644 --- a/source/blender/render/hydra/scene_delegate/curves.h +++ b/source/blender/render/hydra/scene_delegate/curves.h @@ -25,16 +25,18 @@ class CurvesData : public ObjectData { void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; + pxr::SdfPath material_id() const override; + void available_materials(Set &paths) const override; pxr::HdBasisCurvesTopology topology() const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; - pxr::SdfPath material_id() const; - void available_materials(Set &paths) const override; + + protected: + void write_materials() override; private: void write_curves(Curves *curves); void write_uv_maps(Curves *curves); - void write_materials(); pxr::VtIntArray curve_vertex_counts_; pxr::VtVec3fArray vertices_; diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 6be2238051bb..9aade0ef6b3d 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -55,7 +55,7 @@ class IdData { level, \ "%s (%s): " msg, \ prim_id.GetText(), \ - id->name, \ + id ? id->name : "", \ ##__VA_ARGS__); } // 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 d40cf372018f..47794bfe1950 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -100,6 +100,24 @@ pxr::VtValue MeshData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) return get_data(key); } +pxr::SdfPath MeshData::material_id(pxr::SdfPath const &id) const +{ + const SubMesh &sm = submesh(id); + if (!sm.mat_data) { + return pxr::SdfPath(); + } + return sm.mat_data->prim_id; +} + +void MeshData::available_materials(Set &paths) const +{ + for (auto &sm : submeshes_) { + if (sm.mat_data && !sm.mat_data->prim_id.IsEmpty()) { + paths.add(sm.mat_data->prim_id); + } + } +} + pxr::HdMeshTopology MeshData::mesh_topology(pxr::SdfPath const &id) const { const SubMesh &sm = submesh(id); @@ -131,15 +149,6 @@ pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors( return primvars; } -pxr::SdfPath MeshData::material_id(pxr::SdfPath const &id) const -{ - const SubMesh &sm = submesh(id); - if (!sm.mat_data) { - return pxr::SdfPath(); - } - return sm.mat_data->prim_id; -} - pxr::HdCullStyle MeshData::cull_style(pxr::SdfPath const &id) const { const SubMesh &sm = submesh(id); @@ -170,15 +179,6 @@ void MeshData::update_double_sided(MaterialData *mat_data) } } -void MeshData::available_materials(Set &paths) const -{ - for (auto &sm : submeshes_) { - if (sm.mat_data && !sm.mat_data->prim_id.IsEmpty()) { - paths.add(sm.mat_data->prim_id); - } - } -} - pxr::SdfPathVector MeshData::submesh_paths() const { pxr::SdfPathVector ret; @@ -188,6 +188,30 @@ pxr::SdfPathVector MeshData::submesh_paths() const return ret; } +void MeshData::write_materials() +{ + Object *object = (Object *)id; + for (int i = 0; i < submeshes_.size(); ++i) { + SubMesh &m = submeshes_[i]; + Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); + if (!mat) { + m.mat_data = nullptr; + continue; + } + + pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); + m.mat_data = scene_delegate_->material_data(p_id); + if (!m.mat_data) { + m.mat_data = scene_delegate_->materials_ + .lookup_or_add(p_id, + std::make_unique(scene_delegate_, mat, p_id)) + .get(); + m.mat_data->init(); + m.mat_data->insert(); + } + } +} + pxr::SdfPath MeshData::submesh_prim_id(int index) const { char name[16]; @@ -288,28 +312,4 @@ void MeshData::write_submeshes(Mesh *mesh) } } -void MeshData::write_materials() -{ - Object *object = (Object *)id; - for (int i = 0; i < submeshes_.size(); ++i) { - SubMesh &m = submeshes_[i]; - Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); - if (!mat) { - m.mat_data = nullptr; - continue; - } - - pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); - m.mat_data = scene_delegate_->material_data(p_id); - if (!m.mat_data) { - m.mat_data = scene_delegate_->materials_ - .lookup_or_add(p_id, - std::make_unique(scene_delegate_, mat, p_id)) - .get(); - m.mat_data->init(); - m.mat_data->insert(); - } - } -} - } // 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 28d080467f95..2c79bdfc5722 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -34,21 +34,23 @@ class MeshData : public ObjectData { pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const override; + pxr::SdfPath material_id(pxr::SdfPath const &id) const override; + void available_materials(Set &paths) const override; pxr::HdMeshTopology mesh_topology(pxr::SdfPath const &id) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; - pxr::SdfPath material_id(pxr::SdfPath const &id) const; pxr::HdCullStyle cull_style(pxr::SdfPath const &id) const; bool double_sided(pxr::SdfPath const &id) const; void update_double_sided(MaterialData *mat_data); - void available_materials(Set &paths) const override; pxr::SdfPathVector submesh_paths() const; + protected: + void write_materials() override; + private: pxr::SdfPath submesh_prim_id(int index) const; const SubMesh &submesh(pxr::SdfPath const &id) const; void write_submeshes(Mesh *mesh); - void write_materials(); pxr::VtVec3fArray vertices_; std::vector submeshes_; diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 3eea72f69392..26d151c292e3 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -84,6 +84,16 @@ pxr::VtValue ObjectData::get_data(pxr::SdfPath const &id, pxr::TfToken const &ke return get_data(key); } +pxr::SdfPath ObjectData::material_id() const +{ + return pxr::SdfPath(); +} + +pxr::SdfPath ObjectData::material_id(pxr::SdfPath const &id) const +{ + return material_id(); +} + void ObjectData::available_materials(Set &paths) const {} void ObjectData::write_transform() diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index 4b8ea121af09..db0bd29f3ade 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -31,6 +31,8 @@ class ObjectData : public IdData { using IdData::get_data; virtual pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; + virtual pxr::SdfPath material_id() const; + virtual pxr::SdfPath material_id(pxr::SdfPath const &id) const; virtual void available_materials(Set &paths) const; pxr::GfMatrix4d transform; diff --git a/source/blender/render/hydra/scene_delegate/volume.cc b/source/blender/render/hydra/scene_delegate/volume.cc index 340bfd720549..c211399a9070 100644 --- a/source/blender/render/hydra/scene_delegate/volume.cc +++ b/source/blender/render/hydra/scene_delegate/volume.cc @@ -121,11 +121,6 @@ pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const return pxr::VtValue(); } -pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const -{ - return field_descriptors_; -} - pxr::SdfPath VolumeData::material_id() const { if (!mat_data_) { @@ -141,6 +136,11 @@ void VolumeData::available_materials(Set &paths) const } } +pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const +{ + return field_descriptors_; +} + void VolumeData::write_materials() { Object *object = (Object *)id; diff --git a/source/blender/render/hydra/scene_delegate/volume.h b/source/blender/render/hydra/scene_delegate/volume.h index 9bd47c27d3a7..c60b6cc87f68 100644 --- a/source/blender/render/hydra/scene_delegate/volume.h +++ b/source/blender/render/hydra/scene_delegate/volume.h @@ -20,14 +20,15 @@ class VolumeData : public ObjectData { void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; - - pxr::HdVolumeFieldDescriptorVector field_descriptors() const; - pxr::SdfPath material_id() const; + pxr::SdfPath material_id() const override; void available_materials(Set &paths) const override; - private: - void write_materials(); + pxr::HdVolumeFieldDescriptorVector field_descriptors() const; + protected: + void write_materials() override; + + private: std::string filepath_; pxr::HdVolumeFieldDescriptorVector field_descriptors_; MaterialData *mat_data_ = nullptr; diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 6db5b9ec4a66..998a84def18e 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -134,12 +134,6 @@ void WorldData::init() write_transform(); } -void WorldData::insert() -{ - ID_LOG(1, ""); - scene_delegate_->GetRenderIndex().InsertSprim(prim_type_, scene_delegate_, prim_id); -} - void WorldData::update() { ID_LOG(1, ""); diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index da0981b53990..e7c48dd250a7 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -23,7 +23,6 @@ class WorldData : public LightData { WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id); void init() override; - void insert() override; void update() override; protected: -- 2.30.2 From 70cd9f9ea45b74bd798e2dac3b47a34d507ab78f Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 17 Jul 2023 21:11:03 +0300 Subject: [PATCH 5/9] Simplified BlenderSceneDelegate --- .../scene_delegate/blender_scene_delegate.cc | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 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 b79e5632ec8e..d56cf05bed01 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -68,13 +68,9 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id) pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s, %s", id.GetText(), key.GetText()); - MeshData *m_data = mesh_data(id); - if (m_data) { - return m_data->get_data(id, key); - } ObjectData *obj_data = object_data(id); if (obj_data) { - return obj_data->get_data(key); + return obj_data->get_data(id, key); } MaterialData *mat_data = material_data(id); if (mat_data) { @@ -95,9 +91,6 @@ pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id, if (l_data) { return l_data->get_data(key); } - if (id == world_prim_id()) { - return world_data_->get_data(key); - } return pxr::VtValue(); } @@ -123,17 +116,9 @@ pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors( pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id) { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", rprim_id.GetText()); - MeshData *m_data = mesh_data(rprim_id); - if (m_data) { - return m_data->material_id(rprim_id); - } - CurvesData *c_data = curves_data(rprim_id); - if (c_data) { - return c_data->material_id(); - } - VolumeData *v_data = volume_data(rprim_id); - if (v_data) { - return v_data->material_id(); + ObjectData *obj_data = object_data(rprim_id); + if (obj_data) { + return obj_data->material_id(rprim_id); } return pxr::SdfPath(); } @@ -312,6 +297,9 @@ VolumeData *BlenderSceneDelegate::volume_data(pxr::SdfPath const &id) const LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) const { + if (id == world_prim_id()) { + return world_data_.get(); + } return dynamic_cast(object_data(id)); } -- 2.30.2 From 18930cf5a9a6b0bd09fcd4fbbbbd7cf74dc94309 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 17 Jul 2023 21:26:01 +0300 Subject: [PATCH 6/9] Improved working with MeshData --- .../render/hydra/scene_delegate/mesh.cc | 21 +++++++++++-------- .../render/hydra/scene_delegate/mesh.h | 2 ++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 47794bfe1950..fb0d0f4f119e 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -42,15 +42,15 @@ void MeshData::init() void MeshData::insert() { - /* Empty, because insertion of rprims happen in write_submeshes() */ + ID_LOGN(1, ""); + update_prims(); } void MeshData::remove() { - for (int i = 0; i < submeshes_.size(); ++i) { - scene_delegate_->GetRenderIndex().RemoveRprim(submesh_prim_id(i)); - ID_LOG(1, "%d", i); - } + ID_LOG(1, ""); + submeshes_.clear(); + update_prims(); } void MeshData::update() @@ -58,6 +58,7 @@ void MeshData::update() Object *object = (Object *)id; if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { init(); + update_prims(); return; } @@ -228,7 +229,6 @@ const MeshData::SubMesh &MeshData::submesh(pxr::SdfPath const &id) const void MeshData::write_submeshes(Mesh *mesh) { - int sub_meshes_prev_count = submeshes_.size(); submeshes_.clear(); vertices_.clear(); @@ -291,13 +291,15 @@ void MeshData::write_submeshes(Mesh *mesh) vertices_.push_back(pxr::GfVec3f(v.x, v.y, v.z)); } } +} - /* Update prims in render index */ +void MeshData::update_prims() +{ auto &render_index = scene_delegate_->GetRenderIndex(); int i; for (i = 0; i < submeshes_.size(); ++i) { pxr::SdfPath p = submesh_prim_id(i); - if (i < sub_meshes_prev_count) { + if (i < submeshes_count_) { render_index.GetChangeTracker().MarkRprimDirty(p, pxr::HdChangeTracker::AllDirty); ID_LOGN(1, "Update %d", i); } @@ -306,10 +308,11 @@ void MeshData::write_submeshes(Mesh *mesh) ID_LOGN(1, "Insert %d", i); } } - for (; i < sub_meshes_prev_count; ++i) { + for (; i < submeshes_count_; ++i) { render_index.RemoveRprim(submesh_prim_id(i)); ID_LOG(1, "Remove %d", i); } + submeshes_count_ = submeshes_.size(); } } // 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 2c79bdfc5722..6e78d772236b 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -51,9 +51,11 @@ class MeshData : public ObjectData { pxr::SdfPath submesh_prim_id(int index) const; const SubMesh &submesh(pxr::SdfPath const &id) const; void write_submeshes(Mesh *mesh); + void update_prims(); pxr::VtVec3fArray vertices_; std::vector submeshes_; + int submeshes_count_ = 0; }; } // namespace blender::render::hydra -- 2.30.2 From 83403c15a4539784fafaa5c72387a919c5704cdd Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 17 Jul 2023 22:01:33 +0300 Subject: [PATCH 7/9] Added ObjectData::get_or_create_material(), adjusted code --- .../scene_delegate/blender_scene_delegate.cc | 1 - .../scene_delegate/blender_scene_delegate.h | 5 +-- .../render/hydra/scene_delegate/curves.cc | 15 +-------- .../render/hydra/scene_delegate/mesh.cc | 16 +--------- .../render/hydra/scene_delegate/object.cc | 31 +++++++++++++++---- .../render/hydra/scene_delegate/object.h | 1 + .../render/hydra/scene_delegate/volume.cc | 15 +-------- 7 files changed, 30 insertions(+), 54 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 d56cf05bed01..0f6e5f840ed0 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -470,7 +470,6 @@ void BlenderSceneDelegate::update_collection() } else { obj_data = objects_.lookup_or_add(id, ObjectData::create(this, object, id)).get(); - obj_data->init(); obj_data->insert(); } } 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 a745c87fc3df..a6f7cee1e202 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -28,10 +28,7 @@ extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE; class Engine; class BlenderSceneDelegate : public pxr::HdSceneDelegate { - friend ObjectData; /* has access to instances */ - friend CurvesData; /* has access to materials */ - friend VolumeData; /* has access to materials */ - friend MeshData; /* has access to materials */ + friend ObjectData; /* has access to materials */ friend MaterialData; /* has access to objects and instancers */ public: diff --git a/source/blender/render/hydra/scene_delegate/curves.cc b/source/blender/render/hydra/scene_delegate/curves.cc index 568803b75476..8f359e917b24 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -134,20 +134,7 @@ void CurvesData::write_materials() if (BKE_object_material_count_eval(object) > 0) { mat = BKE_object_material_get_eval(object, 0); } - - if (!mat) { - mat_data_ = nullptr; - return; - } - pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); - mat_data_ = scene_delegate_->material_data(p_id); - if (!mat_data_) { - scene_delegate_->materials_.add_new( - p_id, std::make_unique(scene_delegate_, mat, p_id)); - mat_data_ = scene_delegate_->material_data(p_id); - mat_data_->init(); - mat_data_->insert(); - } + mat_data_ = get_or_create_material(mat); } void CurvesData::write_curves(Curves *curves) diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index fb0d0f4f119e..5edae5b175f6 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -195,21 +195,7 @@ void MeshData::write_materials() for (int i = 0; i < submeshes_.size(); ++i) { SubMesh &m = submeshes_[i]; Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); - if (!mat) { - m.mat_data = nullptr; - continue; - } - - pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); - m.mat_data = scene_delegate_->material_data(p_id); - if (!m.mat_data) { - m.mat_data = scene_delegate_->materials_ - .lookup_or_add(p_id, - std::make_unique(scene_delegate_, mat, p_id)) - .get(); - m.mat_data->init(); - m.mat_data->insert(); - } + m.mat_data = get_or_create_material(mat); } } diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 26d151c292e3..80a7a3ab30f9 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -23,7 +23,7 @@ std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_deleg Object *object, pxr::SdfPath const &prim_id) { - std::unique_ptr data; + std::unique_ptr obj_data; switch (object->type) { case OB_MESH: @@ -31,22 +31,23 @@ std::unique_ptr ObjectData::create(BlenderSceneDelegate *scene_deleg case OB_FONT: case OB_CURVES_LEGACY: case OB_MBALL: - data = std::make_unique(scene_delegate, object, prim_id); + obj_data = std::make_unique(scene_delegate, object, prim_id); break; case OB_CURVES: - data = std::make_unique(scene_delegate, object, prim_id); + obj_data = std::make_unique(scene_delegate, object, prim_id); break; case OB_LAMP: - data = std::make_unique(scene_delegate, object, prim_id); + obj_data = std::make_unique(scene_delegate, object, prim_id); break; case OB_VOLUME: - data = std::make_unique(scene_delegate, object, prim_id); + obj_data = std::make_unique(scene_delegate, object, prim_id); break; default: BLI_assert_unreachable(); break; } - return data; + obj_data->init(); + return obj_data; } bool ObjectData::is_supported(Object *object) @@ -103,6 +104,24 @@ void ObjectData::write_transform() void ObjectData::write_materials() {} +MaterialData *ObjectData::get_or_create_material(Material *mat) +{ + if (!mat) { + return nullptr; + } + + pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); + MaterialData *mat_data = scene_delegate_->material_data(p_id); + if (!mat_data) { + scene_delegate_->materials_.add_new( + p_id, std::make_unique(scene_delegate_, mat, p_id)); + mat_data = scene_delegate_->material_data(p_id); + mat_data->init(); + mat_data->insert(); + } + return mat_data; +} + pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]) { pxr::GfMatrix4d ret; diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index db0bd29f3ade..31578c8d319e 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -41,6 +41,7 @@ class ObjectData : public IdData { protected: virtual void write_transform(); virtual void write_materials(); + MaterialData *get_or_create_material(Material *mat); }; using ObjectDataMap = Map>; diff --git a/source/blender/render/hydra/scene_delegate/volume.cc b/source/blender/render/hydra/scene_delegate/volume.cc index c211399a9070..20561d54755c 100644 --- a/source/blender/render/hydra/scene_delegate/volume.cc +++ b/source/blender/render/hydra/scene_delegate/volume.cc @@ -149,20 +149,7 @@ void VolumeData::write_materials() if (BKE_object_material_count_eval(object) > 0) { mat = BKE_object_material_get_eval(object, 0); } - - if (!mat) { - mat_data_ = nullptr; - return; - } - pxr::SdfPath p_id = scene_delegate_->material_prim_id(mat); - mat_data_ = scene_delegate_->material_data(p_id); - if (!mat_data_) { - scene_delegate_->materials_.add_new( - p_id, std::make_unique(scene_delegate_, mat, p_id)); - mat_data_ = scene_delegate_->material_data(p_id); - mat_data_->init(); - mat_data_->insert(); - } + mat_data_ = get_or_create_material(mat); } } // namespace blender::render::hydra -- 2.30.2 From cfbd8d115d67a8971c80a1d10637f12203853fa7 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 18 Jul 2023 14:41:05 +0300 Subject: [PATCH 8/9] Moved world_data_ return to object_data() --- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 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 0f6e5f840ed0..19da928efec1 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -266,6 +266,10 @@ pxr::SdfPath BlenderSceneDelegate::world_prim_id() const ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) const { + if (id == world_prim_id()) { + return world_data_.get(); + } + pxr::SdfPath p_id = (id.GetName().find("SM_") == 0 || id.GetName().find("VF_") == 0) ? id.GetParentPath() : id; @@ -273,6 +277,7 @@ ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) const if (obj_data) { return obj_data->get(); } + InstancerData *i_data = instancer_data(p_id, true); if (i_data) { return i_data->object_data(id); @@ -297,9 +302,6 @@ VolumeData *BlenderSceneDelegate::volume_data(pxr::SdfPath const &id) const LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) const { - if (id == world_prim_id()) { - return world_data_.get(); - } return dynamic_cast(object_data(id)); } -- 2.30.2 From 54aa648ee22a3a4cf9de9af82069b8d62c37fcb2 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 18 Jul 2023 18:02:30 +0300 Subject: [PATCH 9/9] Fixed volumes --- .../blender/render/hydra/scene_delegate/volume.cc | 14 ++++++++++---- .../blender/render/hydra/scene_delegate/volume.h | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/volume.cc b/source/blender/render/hydra/scene_delegate/volume.cc index 20561d54755c..cba60bc6d8e1 100644 --- a/source/blender/render/hydra/scene_delegate/volume.cc +++ b/source/blender/render/hydra/scene_delegate/volume.cc @@ -108,10 +108,6 @@ pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const if (key == pxr::HdVolumeFieldSchemaTokens->filePath) { return pxr::VtValue(pxr::SdfAssetPath(filepath_, filepath_)); } - if (key == pxr::HdVolumeFieldSchemaTokens->fieldName) { - std::string name = prim_id.GetName(); - return pxr::VtValue(pxr::TfToken(name.substr(name.find("VF_") + 3))); - } if (key == pxr::HdVolumeFieldSchemaTokens->fieldIndex) { return pxr::VtValue(0); } @@ -121,6 +117,16 @@ pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const return pxr::VtValue(); } +pxr::VtValue VolumeData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const +{ + if (key == pxr::HdVolumeFieldSchemaTokens->fieldName) { + std::string name = id.GetName(); + return pxr::VtValue(pxr::TfToken(name.substr(name.find("VF_") + 3))); + } + + return get_data(key); +} + pxr::SdfPath VolumeData::material_id() const { if (!mat_data_) { diff --git a/source/blender/render/hydra/scene_delegate/volume.h b/source/blender/render/hydra/scene_delegate/volume.h index c60b6cc87f68..f58d2106668e 100644 --- a/source/blender/render/hydra/scene_delegate/volume.h +++ b/source/blender/render/hydra/scene_delegate/volume.h @@ -20,6 +20,7 @@ class VolumeData : public ObjectData { void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; + pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const override; pxr::SdfPath material_id() const override; void available_materials(Set &paths) const override; -- 2.30.2