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/blender_scene_delegate.cc b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc index 3ed4b88e5692..19da928efec1 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) @@ -68,21 +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); - } - 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); + return obj_data->get_data(id, key); } MaterialData *mat_data = material_data(id); if (mat_data) { @@ -103,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(); } @@ -131,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(); } @@ -289,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; @@ -296,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); @@ -490,7 +472,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 53e3ca6ed872..8f359e917b24 100644 --- a/source/blender/render/hydra/scene_delegate/curves.cc +++ b/source/blender/render/hydra/scene_delegate/curves.cc @@ -20,7 +20,7 @@ CurvesData::CurvesData(BlenderSceneDelegate *scene_delegate, void CurvesData::init() { - ID_LOG(1, ""); + ID_LOGN(1, ""); Object *object = (Object *)id; write_curves((Curves *)object->data); @@ -30,14 +30,14 @@ 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); } @@ -63,10 +63,10 @@ void CurvesData::update() } scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(prim_id, bits); - ID_LOG(1, ""); + ID_LOGN(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,22 @@ pxr::VtValue CurvesData::get_data(pxr::SdfPath const & /* id */, pxr::TfToken co return pxr::VtValue(); } -bool CurvesData::update_visibility() +pxr::SdfPath CurvesData::material_id() const { - bool ret = ObjectData::update_visibility(); - if (ret) { - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - prim_id, pxr::HdChangeTracker::DirtyVisibility); - ID_LOG(1, ""); + if (!mat_data_) { + return pxr::SdfPath(); } - return ret; + return mat_data_->prim_id; } -pxr::HdBasisCurvesTopology CurvesData::curves_topology(pxr::SdfPath const & /* id */) const +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, pxr::TfToken(), @@ -122,19 +126,15 @@ pxr::HdPrimvarDescriptorVector CurvesData::primvar_descriptors( return primvars; } -pxr::SdfPath CurvesData::material_id() const +void CurvesData::write_materials() { - 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); + 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); } + mat_data_ = get_or_create_material(mat); } void CurvesData::write_curves(Curves *curves) @@ -178,28 +178,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 92e742b4b1ba..39d316db63ac 100644 --- a/source/blender/render/hydra/scene_delegate/curves.h +++ b/source/blender/render/hydra/scene_delegate/curves.h @@ -24,18 +24,19 @@ 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::SdfPath material_id() const override; + void available_materials(Set &paths) 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; + + 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.cc b/source/blender/render/hydra/scene_delegate/id.cc index 1cf8d6b9dd34..ed14c8a6cba4 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -12,9 +12,4 @@ IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const { } -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..9aade0ef6b3d 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(); } }; @@ -38,8 +38,7 @@ 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; @@ -48,12 +47,10 @@ class IdData { 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: " msg, prim_id.GetText(), ##__VA_ARGS__); + +#define ID_LOGN(level, msg, ...) \ CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ level, \ "%s (%s): " msg, \ diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index a7e754ce2ab0..2766d4de7e04 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, ""); + ID_LOGN(1, ""); Light *light = (Light *)((Object *)id)->data; data_.clear(); @@ -97,13 +97,13 @@ void LightData::init() void LightData::insert() { - ID_LOG(1, ""); + ID_LOGN(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); } @@ -128,13 +128,13 @@ void LightData::update() } if (bits != pxr::HdChangeTracker::Clean) { scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits); - ID_LOG(1, ""); + ID_LOGN(1, ""); } } pxr::VtValue LightData::get_data(pxr::TfToken const &key) const { - ID_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); @@ -148,17 +148,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..fb4eb234ce30 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -27,9 +27,8 @@ class LightData : public ObjectData { void update() override; pxr::VtValue get_data(pxr::TfToken const &key) const override; - bool update_visibility() override; - private: + protected: pxr::TfToken prim_type(Light *light); std::map data_; diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index e2aaf54a1789..9c8005874cfc 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -31,7 +31,7 @@ MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, void MaterialData::init() { - ID_LOG(1, ""); + ID_LOGN(1, ""); double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0; export_mtlx(); @@ -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_); } @@ -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/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 81136635efc6..5edae5b175f6 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -27,7 +27,7 @@ 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); @@ -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)); - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s: %d", prim_id.GetText(), 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; } @@ -77,35 +78,45 @@ 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); } } -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(); + return get_data(key); } -bool MeshData::update_visibility() +pxr::SdfPath MeshData::material_id(pxr::SdfPath const &id) const { - 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); + 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); } } - return ret; } pxr::HdMeshTopology MeshData::mesh_topology(pxr::SdfPath const &id) const @@ -139,15 +150,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); @@ -173,16 +175,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); - } - } -} - -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); + ID_LOGN(1, "%d", i); } } } @@ -196,6 +189,16 @@ 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); + m.mat_data = get_or_create_material(mat); + } +} + pxr::SdfPath MeshData::submesh_prim_id(int index) const { char name[16]; @@ -212,7 +215,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(); @@ -275,49 +277,28 @@ 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_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) { + for (; i < submeshes_count_; ++i) { render_index.RemoveRprim(submesh_prim_id(i)); ID_LOG(1, "Remove %d", i); } -} - -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(); - } - } + 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 649056adac0c..6e78d772236b 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -32,26 +32,30 @@ 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::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; 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(); + void update_prims(); pxr::VtVec3fArray vertices_; std::vector submeshes_; + int submeshes_count_ = 0; }; } // 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 e3b4a18b5139..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,21 +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) @@ -78,18 +80,48 @@ 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); } +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() { transform = gf_matrix_from_transform(((Object *)id)->object_to_world); } +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 1a31a0cbf245..31578c8d319e 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,19 @@ 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 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; bool visible = true; protected: - void write_transform(); + 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 ec270c8ba9bf..cba60bc6d8e1 100644 --- a/source/blender/render/hydra/scene_delegate/volume.cc +++ b/source/blender/render/hydra/scene_delegate/volume.cc @@ -27,7 +27,7 @@ VolumeData::VolumeData(BlenderSceneDelegate *scene_delegate, void VolumeData::init() { - ID_LOG(1, ""); + ID_LOGN(1, ""); Volume *volume = (Volume *)((Object *)this->id)->data; Main *main = CTX_data_main(scene_delegate_->context); if (!BKE_volume_load(volume, main)) { @@ -59,22 +59,22 @@ 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); } @@ -100,18 +100,14 @@ void VolumeData::update() } scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(prim_id, bits); - ID_LOG(1, ""); + ID_LOGN(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(); - return pxr::VtValue(pxr::TfToken(name.substr(name.find("VF_") + 3))); - } if (key == pxr::HdVolumeFieldSchemaTokens->fieldIndex) { return pxr::VtValue(0); } @@ -121,20 +117,14 @@ pxr::VtValue VolumeData::get_data(pxr::SdfPath const &id, pxr::TfToken const &ke return pxr::VtValue(); } -bool VolumeData::update_visibility() +pxr::VtValue VolumeData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const { - bool ret = ObjectData::update_visibility(); - if (ret) { - scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - prim_id, pxr::HdChangeTracker::DirtyVisibility); - ID_LOG(1, ""); + if (key == pxr::HdVolumeFieldSchemaTokens->fieldName) { + std::string name = id.GetName(); + return pxr::VtValue(pxr::TfToken(name.substr(name.find("VF_") + 3))); } - return ret; -} -pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const -{ - return field_descriptors_; + return get_data(key); } pxr::SdfPath VolumeData::material_id() const @@ -152,6 +142,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; @@ -160,20 +155,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 diff --git a/source/blender/render/hydra/scene_delegate/volume.h b/source/blender/render/hydra/scene_delegate/volume.h index dba00d2521c4..f58d2106668e 100644 --- a/source/blender/render/hydra/scene_delegate/volume.h +++ b/source/blender/render/hydra/scene_delegate/volume.h @@ -19,16 +19,17 @@ 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::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; pxr::HdVolumeFieldDescriptorVector field_descriptors() const; - pxr::SdfPath material_id() const; - void available_materials(Set &paths) const; + + protected: + void write_materials() override; private: - void write_materials(); - 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 c7f7aa746286..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, nullptr, 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