From de574026d82a8bedc14783e93b73b5d3083a4af7 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 15 Feb 2023 13:50:37 +0200 Subject: [PATCH 01/13] BLEN-344: Fix area light visibility + normalize intensity. Added data HdLightTokens->normalize for area light. Refactoring: changing to use map of unique_ptr for objects and materials. --- .../sceneDelegate/blenderSceneDelegate.cc | 87 ++++++++++--------- .../render/hydra/sceneDelegate/material.cc | 4 +- .../render/hydra/sceneDelegate/material.h | 12 +-- .../render/hydra/sceneDelegate/object.cc | 10 +-- .../render/hydra/sceneDelegate/object.h | 11 +-- 5 files changed, 64 insertions(+), 60 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index 7fd1c829c768..999084949e8a 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -29,12 +29,13 @@ void BlenderSceneDelegate::set_material(ObjectData &obj_data) return; } SdfPath mat_id = material_id(material); - if (!material_data(mat_id)) { - MaterialData mat_data(material); + MaterialData *mat_data = material_data(mat_id); + if (!mat_data) { + materials[mat_id] = MaterialData::init(material); + mat_data = material_data(mat_id); GetRenderIndex().InsertSprim(HdPrimTypeTokens->material, this, mat_id); - mat_data.export_mtlx(); - materials[mat_id] = mat_data; - LOG(INFO) << "Add material: " << mat_id << ", mtlx=" << mat_data.mtlx_path.GetResolvedPath(); + mat_data->export_mtlx(); + LOG(INFO) << "Add material: " << mat_id << ", mtlx=" << mat_data->mtlx_path.GetResolvedPath(); } obj_data.set_material_id(mat_id); } @@ -90,11 +91,11 @@ void BlenderSceneDelegate::update_collection() continue; } LOG(INFO) << "Remove: " << it->first; - if (it->second.prim_type() == HdPrimTypeTokens->mesh) { + if (it->second->prim_type() == HdPrimTypeTokens->mesh) { index.RemoveRprim(it->first); } - else if (it->second.prim_type() != HdBlenderTokens->empty) { - index.RemoveSprim(it->second.prim_type(), it->first); + else if (it->second->prim_type() != HdBlenderTokens->empty) { + index.RemoveSprim(it->second->prim_type(), it->first); } objects.erase(it); it = objects.begin(); @@ -103,8 +104,8 @@ void BlenderSceneDelegate::update_collection() /* remove unused materials */ std::set available_materials; for (auto &obj : objects) { - if (obj.second.has_data(HdBlenderTokens->materialId)) { - available_materials.insert(obj.second.get_data(HdBlenderTokens->materialId).Get()); + if (obj.second->has_data(HdBlenderTokens->materialId)) { + available_materials.insert(obj.second->get_data(HdBlenderTokens->materialId).Get()); } } for (auto it = materials.begin(); it != materials.end(); ++it) { @@ -125,33 +126,33 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool SdfPath obj_id = object_id(object); ObjectData *obj_data = object_data(obj_id); if (!obj_data) { - ObjectData new_obj_data(object); - new_obj_data.update_visibility(view3d); - if (new_obj_data.prim_type() == HdPrimTypeTokens->mesh) { - LOG(INFO) << "Add mesh object: " << new_obj_data.name() << " id=" << obj_id; - index.InsertRprim(new_obj_data.prim_type(), this, obj_id); - set_material(new_obj_data); + objects[obj_id] = ObjectData::init(object); + obj_data = object_data(obj_id); + obj_data->update_visibility(view3d); + if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { + LOG(INFO) << "Add mesh object: " << obj_data->name() << " id=" << obj_id; + index.InsertRprim(obj_data->prim_type(), this, obj_id); + set_material(*obj_data); } - else if (new_obj_data.type() == OB_LAMP) { - LOG(INFO) << "Add light object: " << new_obj_data.name() << " id=" << obj_id; - index.InsertSprim(new_obj_data.prim_type(), this, obj_id); + else if (obj_data->type() == OB_LAMP) { + LOG(INFO) << "Add light object: " << obj_data->name() << " id=" << obj_id; + index.InsertSprim(obj_data->prim_type(), this, obj_id); } - objects[obj_id] = new_obj_data; return; } if (geometry) { LOG(INFO) << "Full updated: " << obj_id; - ObjectData new_obj_data(object); - new_obj_data.update_visibility(view3d); - if (new_obj_data.prim_type() == HdPrimTypeTokens->mesh) { - set_material(new_obj_data); + objects[obj_id] = ObjectData::init(object); + obj_data = object_data(obj_id); + obj_data->update_visibility(view3d); + if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { + set_material(*obj_data); index.GetChangeTracker().MarkRprimDirty(obj_id, HdChangeTracker::AllDirty); } - else if (new_obj_data.type() == OB_LAMP) { + else if (obj_data->type() == OB_LAMP) { index.GetChangeTracker().MarkSprimDirty(obj_id, HdLight::AllDirty); } - objects[obj_id] = new_obj_data; return; } @@ -179,7 +180,7 @@ ObjectData *BlenderSceneDelegate::object_data(SdfPath const &id) if (it == objects.end()) { return nullptr; } - return &it->second; + return it->second.get(); } MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id) @@ -188,7 +189,7 @@ MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id) if (it == materials.end()) { return nullptr; } - return &it->second; + return it->second.get(); } SdfPath BlenderSceneDelegate::object_id(Object *object) @@ -293,12 +294,12 @@ void BlenderSceneDelegate::update_visibility() /* Check and update visibility */ for (auto &obj : objects) { - if (obj.second.update_visibility(view3d)) { - LOG(INFO) << "Visible changed: " << obj.first.GetAsString() << " " << obj.second.is_visible(); - if (obj.second.prim_type() == HdPrimTypeTokens->mesh) { + if (obj.second->update_visibility(view3d)) { + LOG(INFO) << "Visible changed: " << obj.first.GetAsString() << " " << obj.second->is_visible(); + if (obj.second->prim_type() == HdPrimTypeTokens->mesh) { index.GetChangeTracker().MarkRprimDirty(obj.first, HdChangeTracker::DirtyVisibility); } - else if (obj.second.type() == OB_LAMP) { + else if (obj.second->type() == OB_LAMP) { index.GetChangeTracker().MarkSprimDirty(obj.first, HdLight::DirtyParams); } }; @@ -324,10 +325,10 @@ void BlenderSceneDelegate::update_visibility() HdMeshTopology BlenderSceneDelegate::GetMeshTopology(SdfPath const& id) { LOG(INFO) << "GetMeshTopology: " << id.GetAsString(); - ObjectData &obj_data = objects[id]; + ObjectDataPtr &obj_data = objects[id]; return HdMeshTopology(PxOsdOpenSubdivTokens->catmullClark, HdTokens->rightHanded, - obj_data.get_data(HdBlenderTokens->faceCounts), - obj_data.get_data(HdTokens->pointsIndices)); + obj_data->get_data(HdBlenderTokens->faceCounts), + obj_data->get_data(HdTokens->pointsIndices)); } VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) @@ -342,9 +343,9 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) } } else if (key.GetString() == "MaterialXFilename") { - MaterialData &mat_data = materials[id]; - if (!mat_data.mtlx_path.GetResolvedPath().empty()) { - ret = mat_data.mtlx_path; + MaterialDataPtr &mat_data = materials[id]; + if (!mat_data->mtlx_path.GetResolvedPath().empty()) { + ret = mat_data->mtlx_path; } } else if (key == HdStRenderBufferTokens->stormMsaaSampleCount) { @@ -358,17 +359,17 @@ HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(SdfPath co { LOG(INFO) << "GetPrimvarDescriptors: " << id.GetAsString() << " " << interpolation; HdPrimvarDescriptorVector primvars; - ObjectData &obj_data = objects[id]; + ObjectDataPtr &obj_data = objects[id]; if (interpolation == HdInterpolationVertex) { - if (obj_data.has_data(HdTokens->points)) { + if (obj_data->has_data(HdTokens->points)) { primvars.emplace_back(HdTokens->points, interpolation, HdPrimvarRoleTokens->point); } } else if (interpolation == HdInterpolationFaceVarying) { - if (obj_data.has_data(HdTokens->normals)) { + if (obj_data->has_data(HdTokens->normals)) { primvars.emplace_back(HdTokens->normals, interpolation, HdPrimvarRoleTokens->normal); } - if (obj_data.has_data(HdPrimvarRoleTokens->textureCoordinate)) { + if (obj_data->has_data(HdPrimvarRoleTokens->textureCoordinate)) { primvars.emplace_back(HdPrimvarRoleTokens->textureCoordinate, interpolation, HdPrimvarRoleTokens->textureCoordinate); } } @@ -397,7 +398,7 @@ GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id) { LOG(INFO) << "GetTransform: " << id.GetAsString(); - return objects[id].transform(); + return objects[id]->transform(); } VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken const& key) diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index de3d7e21ee8d..7ad39f8fd182 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -12,9 +12,9 @@ using namespace pxr; namespace blender::render::hydra { -MaterialData::MaterialData() - : material(nullptr) +MaterialDataPtr MaterialData::init(Material *material) { + return std::make_unique(material); } MaterialData::MaterialData(Material *material) diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index 35e8dfd8f3bb..e192ddb0359a 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -3,18 +3,22 @@ #pragma once -#include - #include #include +#include "pxr/base/tf/hashmap.h" #include "DNA_material_types.h" namespace blender::render::hydra { +class MaterialData; +using MaterialDataPtr = std::unique_ptr; +using MaterialDataMap = pxr::TfHashMap; + class MaterialData { public: - MaterialData(); + static MaterialDataPtr init(Material *material); + MaterialData(Material *material); std::string name(); @@ -25,6 +29,4 @@ class MaterialData { Material *material; }; -using MaterialDataMap = std::map; - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/object.cc b/source/blender/render/hydra/sceneDelegate/object.cc index f10c210b3ef6..6e9add690abe 100644 --- a/source/blender/render/hydra/sceneDelegate/object.cc +++ b/source/blender/render/hydra/sceneDelegate/object.cc @@ -30,15 +30,14 @@ using namespace pxr; namespace blender::render::hydra { -ObjectData::ObjectData() - : object(nullptr) - , visible(true) +ObjectDataPtr ObjectData::init(Object *object) { - + return std::make_unique(object); } ObjectData::ObjectData(Object *object) : object(object) + , visible(true) { switch (object->type) { case OB_MESH: @@ -277,7 +276,7 @@ void ObjectData::set_as_light() case LA_SPOT: data[HdLightTokens->shapingConeAngle] = light->spotsize / 2; data[HdLightTokens->shapingConeSoftness] = light->spotblend; - data[UsdLuxTokens->treatAsPoint] = 1; + data[UsdLuxTokens->treatAsPoint] = true; break; case LA_AREA: @@ -302,6 +301,7 @@ void ObjectData::set_as_light() default: break; } + data[HdLightTokens->normalize] = true; break; default: diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index ae3ab672ed86..3be1bda5a353 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -3,12 +3,11 @@ #pragma once -#include - #include #include #include #include "pxr/base/tf/staticTokens.h" +#include "pxr/base/tf/hashmap.h" #include "DNA_object_types.h" @@ -25,10 +24,14 @@ PXR_NAMESPACE_CLOSE_SCOPE namespace blender::render::hydra { +class ObjectData; +using ObjectDataPtr = std::unique_ptr; +using ObjectDataMap = pxr::TfHashMap; class ObjectData { public: - ObjectData(); + static ObjectDataPtr init(Object *object); + ObjectData(Object *object); std::string name(); @@ -57,8 +60,6 @@ public: void set_as_light(); }; -using ObjectDataMap = std::map; - template const T &ObjectData::get_data(pxr::TfToken const &key) { -- 2.30.2 From 07f1eb2b380bfbce08d1e9c71f9d3469ba47ad62 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 16 Feb 2023 09:29:12 +0200 Subject: [PATCH 02/13] Continue refactoring --- source/blender/render/hydra/CMakeLists.txt | 6 + .../blender/render/hydra/sceneDelegate/id.cc | 22 ++++ .../blender/render/hydra/sceneDelegate/id.h | 31 +++++ .../render/hydra/sceneDelegate/light.cc | 79 +++++++++++++ .../render/hydra/sceneDelegate/light.h | 24 ++++ .../render/hydra/sceneDelegate/material.cc | 9 +- .../render/hydra/sceneDelegate/material.h | 8 +- .../render/hydra/sceneDelegate/mesh.cc | 109 ++++++++++++++++++ .../blender/render/hydra/sceneDelegate/mesh.h | 30 +++++ .../render/hydra/sceneDelegate/object.cc | 9 +- .../render/hydra/sceneDelegate/object.h | 15 +-- 11 files changed, 309 insertions(+), 33 deletions(-) create mode 100644 source/blender/render/hydra/sceneDelegate/id.cc create mode 100644 source/blender/render/hydra/sceneDelegate/id.h create mode 100644 source/blender/render/hydra/sceneDelegate/light.cc create mode 100644 source/blender/render/hydra/sceneDelegate/light.h create mode 100644 source/blender/render/hydra/sceneDelegate/mesh.cc create mode 100644 source/blender/render/hydra/sceneDelegate/mesh.h diff --git a/source/blender/render/hydra/CMakeLists.txt b/source/blender/render/hydra/CMakeLists.txt index 1114514a934c..92e3412633dd 100644 --- a/source/blender/render/hydra/CMakeLists.txt +++ b/source/blender/render/hydra/CMakeLists.txt @@ -61,10 +61,16 @@ set(SRC sceneDelegate/blenderSceneDelegate.h sceneDelegate/blenderSceneDelegate.cc + sceneDelegate/id.h + sceneDelegate/id.cc sceneDelegate/object.h sceneDelegate/object.cc sceneDelegate/material.h sceneDelegate/material.cc + sceneDelegate/mesh.h + sceneDelegate/mesh.cc + sceneDelegate/light.h + sceneDelegate/light.cc ) set(LIB diff --git a/source/blender/render/hydra/sceneDelegate/id.cc b/source/blender/render/hydra/sceneDelegate/id.cc new file mode 100644 index 000000000000..ca60b5a3727a --- /dev/null +++ b/source/blender/render/hydra/sceneDelegate/id.cc @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#include "BKE_lib_id.h" + +#include "id.h" + +namespace blender::render::hydra { + +IdData::IdData(ID *id) + : id(id) +{ +} + +std::string IdData::name() +{ + char str[MAX_ID_FULL_NAME]; + BKE_id_full_name_get(str, id, 0); + return str; +} + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/id.h b/source/blender/render/hydra/sceneDelegate/id.h new file mode 100644 index 000000000000..e309e6ef431f --- /dev/null +++ b/source/blender/render/hydra/sceneDelegate/id.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#pragma once + +#include + +#include "DNA_ID.h" + +namespace blender::render::hydra { + +class IdData { + public: + IdData(ID *id); + virtual ~IdData() = default; + + std::string name(); + + virtual pxr::VtValue get_data(pxr::TfToken const &key) = 0; + template const T get_data(pxr::TfToken const &key); + + protected: + ID *id; +}; + +template const T IdData::get_data(pxr::TfToken const &key) +{ + return get_data(key).Get(); +} + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc new file mode 100644 index 000000000000..dd0b659344e4 --- /dev/null +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#include +#include + +#include "BKE_light.h" +#include "DNA_light_types.h" + +#include "light.h" + +using namespace pxr; + +namespace blender::render::hydra { + +LightData::LightData(Object *object) + : ObjectData(object) +{ + Object *object = (Object *)id; + Light *light = (Light *)object->data; + + data[HdLightTokens->intensity] = light->energy; + data[HdLightTokens->color] = GfVec3f(light->r, light->g, light->b); + + switch (light->type) { + case LA_LOCAL: + data[HdLightTokens->radius] = light->area_size / 2; + break; + + case LA_SUN: + data[HdLightTokens->angle] = light->sun_angle * 180.0 / M_PI; + break; + + case LA_SPOT: + data[HdLightTokens->shapingConeAngle] = light->spotsize / 2; + data[HdLightTokens->shapingConeSoftness] = light->spotblend; + data[UsdLuxTokens->treatAsPoint] = true; + break; + + case LA_AREA: + switch (light->area_shape) { + case LA_AREA_SQUARE: + data[HdLightTokens->width] = light->area_size; + data[HdLightTokens->height] = light->area_size; + break; + case LA_AREA_RECT: + data[HdLightTokens->width] = light->area_size; + data[HdLightTokens->height] = light->area_sizey; + break; + + case LA_AREA_DISK: + data[HdLightTokens->radius] = light->area_size / 2; + break; + + case LA_AREA_ELLIPSE: + data[HdLightTokens->radius] = (light->area_size + light->area_sizey) / 4; + break; + + default: + break; + } + data[HdLightTokens->normalize] = true; + break; + + default: + break; + } +} + +VtValue LightData::get_data(TfToken const &key) +{ + auto it = data.find(key); + if (it == data.end()) { + return VtValue(); + } + return it->second; +} + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/light.h b/source/blender/render/hydra/sceneDelegate/light.h new file mode 100644 index 000000000000..3567bc67ffa4 --- /dev/null +++ b/source/blender/render/hydra/sceneDelegate/light.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#pragma once + +#include +#include +#include "pxr/base/tf/hashmap.h" + +#include "object.h" + +namespace blender::render::hydra { + +class LightData: public ObjectData { + public: + LightData(Object *object); + + pxr::VtValue get_data(pxr::TfToken const &key) override; + + private: + std::map data; +}; + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index 7ad39f8fd182..6158d8d130e6 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -18,17 +18,10 @@ MaterialDataPtr MaterialData::init(Material *material) } MaterialData::MaterialData(Material *material) - : material(material) + : IdData((ID *)material) { } -std::string MaterialData::name() -{ - char str[MAX_ID_FULL_NAME]; - BKE_id_full_name_get(str, (ID *)material, 0); - return str; -} - void MaterialData::export_mtlx() { /* Call of python function hydra.export_mtlx() */ diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index e192ddb0359a..e9db68f8e681 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -9,24 +9,22 @@ #include "DNA_material_types.h" +#include "id.h" + namespace blender::render::hydra { class MaterialData; using MaterialDataPtr = std::unique_ptr; using MaterialDataMap = pxr::TfHashMap; -class MaterialData { +class MaterialData: IdData { public: static MaterialDataPtr init(Material *material); MaterialData(Material *material); - std::string name(); void export_mtlx(); pxr::SdfAssetPath mtlx_path; - - private: - Material *material; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/mesh.cc b/source/blender/render/hydra/sceneDelegate/mesh.cc new file mode 100644 index 000000000000..74f19fba6c74 --- /dev/null +++ b/source/blender/render/hydra/sceneDelegate/mesh.cc @@ -0,0 +1,109 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#include +#include + +#include "BKE_mesh.h" +#include "BKE_mesh_runtime.h" +#include "BKE_object.h" +#include "BKE_material.h" + + +#include "mesh.h" + +using namespace pxr; + +namespace blender::render::hydra { + +MeshData::MeshData(Object *object) + : ObjectData(object) +{ + if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && + BLI_listbase_is_empty(&object->modifiers)) { + set_mesh((Mesh *)object->data); + } + else { + Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); + set_mesh(mesh); + BKE_object_to_mesh_clear(object); + } +} + +pxr::VtValue MeshData::get_data(pxr::TfToken const &key) +{ + VtValue ret; + if (key == HdTokens->points) { + ret = vertices; + } + else if (key == HdTokens->normals) { + ret = normals; + } + else if (key == HdPrimvarRoleTokens->textureCoordinate) { + ret = uvs; + } + return ret; +} + +Material *MeshData::material() +{ + Object *object = (Object *)id; + if (BKE_object_material_count_eval(object) == 0) { + return nullptr; + } + return BKE_object_material_get_eval(object, object->actcol); +} + +void MeshData::set_mesh(Mesh *mesh) +{ + BKE_mesh_calc_normals_split(mesh); + int tris_len = BKE_mesh_runtime_looptri_len(mesh); + if (tris_len == 0) { + return; + } + + blender::Span loopTris = mesh->looptris(); + + /* face_vertex_counts */ + face_vertex_counts = VtIntArray(tris_len, 3); + + /* face_vertex_indices */ + blender::Span loops = mesh->loops(); + face_vertex_indices.reserve(loopTris.size() * 3); + for (MLoopTri lt : loopTris) { + face_vertex_indices.push_back(loops[lt.tri[0]].v); + face_vertex_indices.push_back(loops[lt.tri[1]].v); + face_vertex_indices.push_back(loops[lt.tri[2]].v); + } + + /* vertices */ + vertices.reserve(mesh->totvert); + blender::Span verts = mesh->vert_positions(); + for (blender::float3 v : verts) { + vertices.push_back(GfVec3f(v.x, v.y, v.z)); + } + + /* normals */ + const float(*lnors)[3] = (float(*)[3])CustomData_get_layer(&mesh->ldata, CD_NORMAL); + if (lnors) { + normals.reserve(loopTris.size() * 3); + for (MLoopTri lt : loopTris) { + normals.push_back(GfVec3f(lnors[lt.tri[0]])); + normals.push_back(GfVec3f(lnors[lt.tri[1]])); + normals.push_back(GfVec3f(lnors[lt.tri[2]])); + } + } + + /* uvs*/ + const float(*luvs)[2] = (float(*)[2])CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2); + if (luvs) { + uvs.reserve(loopTris.size() * 3); + for (MLoopTri lt : loopTris) { + uvs.push_back(GfVec2f(luvs[lt.tri[0]])); + uvs.push_back(GfVec2f(luvs[lt.tri[1]])); + uvs.push_back(GfVec2f(luvs[lt.tri[2]])); + } + } +} + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/mesh.h b/source/blender/render/hydra/sceneDelegate/mesh.h new file mode 100644 index 000000000000..58bfb0a08701 --- /dev/null +++ b/source/blender/render/hydra/sceneDelegate/mesh.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2011-2022 Blender Foundation */ + +#pragma once + +#include + +#include "object.h" + +namespace blender::render::hydra { + +class MeshData: public ObjectData { +public: + MeshData(Object *object); + + pxr::VtValue get_data(pxr::TfToken const &key) override; + Material *material(); + + pxr::VtIntArray face_vertex_counts; + pxr::VtIntArray face_vertex_indices; + pxr::VtVec3fArray vertices; + pxr::VtVec3fArray normals; + pxr::VtVec2fArray uvs; + pxr::SdfPath material_id; + + private: + void set_mesh(Mesh *mesh); +}; + +} // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/object.cc b/source/blender/render/hydra/sceneDelegate/object.cc index 6e9add690abe..7eb3b542ebf3 100644 --- a/source/blender/render/hydra/sceneDelegate/object.cc +++ b/source/blender/render/hydra/sceneDelegate/object.cc @@ -36,7 +36,7 @@ ObjectDataPtr ObjectData::init(Object *object) } ObjectData::ObjectData(Object *object) - : object(object) + : IdData((ID *)object) , visible(true) { switch (object->type) { @@ -66,13 +66,6 @@ ObjectData::ObjectData(Object *object) } } -std::string ObjectData::name() -{ - char str[MAX_ID_FULL_NAME]; - BKE_id_full_name_get(str, (ID *)object, 0); - return str; -} - int ObjectData::type() { return object->type; diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index 3be1bda5a353..f66ec5af5fe5 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -11,6 +11,7 @@ #include "DNA_object_types.h" +#include "id.h" #include "material.h" PXR_NAMESPACE_OPEN_SCOPE @@ -28,21 +29,18 @@ class ObjectData; using ObjectDataPtr = std::unique_ptr; using ObjectDataMap = pxr::TfHashMap; -class ObjectData { +class ObjectData: public IdData { public: static ObjectDataPtr init(Object *object); ObjectData(Object *object); - std::string name(); int type(); pxr::TfToken prim_type(); pxr::GfMatrix4d transform(); Material *material(); - pxr::VtValue &get_data(pxr::TfToken const &key); - template - const T &get_data(pxr::TfToken const &key); + pxr::VtValue get_data(pxr::TfToken const &key); bool has_data(pxr::TfToken const &key); void set_material_id(pxr::SdfPath const &id); @@ -50,7 +48,6 @@ public: bool is_visible(); private: - Object *object; std::map data; bool visible; @@ -60,10 +57,4 @@ public: void set_as_light(); }; -template -const T &ObjectData::get_data(pxr::TfToken const &key) -{ - return get_data(key).Get(); -} - } // namespace blender::render::hydra -- 2.30.2 From f74cbf28bb9671350717088da2173ff910601636 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 16 Feb 2023 11:14:27 +0200 Subject: [PATCH 03/13] Finished refactoring --- .../sceneDelegate/blenderSceneDelegate.cc | 100 +++--- .../sceneDelegate/blenderSceneDelegate.h | 6 +- .../blender/render/hydra/sceneDelegate/id.h | 11 +- .../render/hydra/sceneDelegate/light.cc | 44 ++- .../render/hydra/sceneDelegate/light.h | 1 + .../render/hydra/sceneDelegate/material.cc | 7 + .../render/hydra/sceneDelegate/material.h | 1 + .../render/hydra/sceneDelegate/mesh.cc | 34 +++ .../blender/render/hydra/sceneDelegate/mesh.h | 14 +- .../render/hydra/sceneDelegate/object.cc | 285 ++---------------- .../render/hydra/sceneDelegate/object.h | 36 +-- 11 files changed, 173 insertions(+), 366 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index 999084949e8a..d8e4be9eaf18 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -21,11 +21,11 @@ BlenderSceneDelegate::BlenderSceneDelegate(HdRenderIndex* parentIndex, SdfPath c { } -void BlenderSceneDelegate::set_material(ObjectData &obj_data) +void BlenderSceneDelegate::set_material(MeshData &mesh_data) { - Material *material = obj_data.material(); + Material *material = mesh_data.material(); if (!material) { - obj_data.set_material_id(SdfPath::EmptyPath()); + mesh_data.material_id = SdfPath::EmptyPath(); return; } SdfPath mat_id = material_id(material); @@ -33,11 +33,11 @@ void BlenderSceneDelegate::set_material(ObjectData &obj_data) if (!mat_data) { materials[mat_id] = MaterialData::init(material); mat_data = material_data(mat_id); - GetRenderIndex().InsertSprim(HdPrimTypeTokens->material, this, mat_id); + GetRenderIndex().InsertSprim(mat_data->prim_type(), this, mat_id); mat_data->export_mtlx(); LOG(INFO) << "Add material: " << mat_id << ", mtlx=" << mat_data->mtlx_path.GetResolvedPath(); } - obj_data.set_material_id(mat_id); + mesh_data.material_id = mat_id; } void BlenderSceneDelegate::update_material(Material *material) @@ -54,9 +54,9 @@ void BlenderSceneDelegate::update_material(Material *material) bool BlenderSceneDelegate::GetVisible(SdfPath const &id) { ObjectData *obj_data = object_data(id); - LOG(INFO) << "GetVisible: " << id.GetAsString() << " " << obj_data->is_visible(); + LOG(INFO) << "GetVisible: " << id.GetAsString() << " " << obj_data->visible; - return obj_data->is_visible(); + return obj_data->visible; } void BlenderSceneDelegate::update_collection() @@ -94,7 +94,7 @@ void BlenderSceneDelegate::update_collection() if (it->second->prim_type() == HdPrimTypeTokens->mesh) { index.RemoveRprim(it->first); } - else if (it->second->prim_type() != HdBlenderTokens->empty) { + else if (!it->second->prim_type().IsEmpty()) { index.RemoveSprim(it->second->prim_type(), it->first); } objects.erase(it); @@ -104,8 +104,9 @@ void BlenderSceneDelegate::update_collection() /* remove unused materials */ std::set available_materials; for (auto &obj : objects) { - if (obj.second->has_data(HdBlenderTokens->materialId)) { - available_materials.insert(obj.second->get_data(HdBlenderTokens->materialId).Get()); + MeshData *m_data = dynamic_cast(obj.second.get()); + if (m_data && !m_data->material_id.IsEmpty()) { + available_materials.insert(m_data->material_id); } } for (auto it = materials.begin(); it != materials.end(); ++it) { @@ -130,9 +131,10 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool obj_data = object_data(obj_id); obj_data->update_visibility(view3d); if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { - LOG(INFO) << "Add mesh object: " << obj_data->name() << " id=" << obj_id; - index.InsertRprim(obj_data->prim_type(), this, obj_id); - set_material(*obj_data); + MeshData *m_data = dynamic_cast(obj_data); + LOG(INFO) << "Add mesh object: " << m_data->name() << " id=" << obj_id; + index.InsertRprim(m_data->prim_type(), this, obj_id); + set_material(*m_data); } else if (obj_data->type() == OB_LAMP) { LOG(INFO) << "Add light object: " << obj_data->name() << " id=" << obj_id; @@ -147,7 +149,7 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool obj_data = object_data(obj_id); obj_data->update_visibility(view3d); if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { - set_material(*obj_data); + set_material(*dynamic_cast(obj_data)); index.GetChangeTracker().MarkRprimDirty(obj_id, HdChangeTracker::AllDirty); } else if (obj_data->type() == OB_LAMP) { @@ -183,6 +185,16 @@ ObjectData *BlenderSceneDelegate::object_data(SdfPath const &id) return it->second.get(); } +MeshData *BlenderSceneDelegate::mesh_data(SdfPath const &id) +{ + return dynamic_cast(object_data(id)); +} + +LightData *BlenderSceneDelegate::light_data(SdfPath const &id) +{ + return dynamic_cast(object_data(id)); +} + MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id) { auto it = materials.find(id); @@ -295,7 +307,7 @@ void BlenderSceneDelegate::update_visibility() /* Check and update visibility */ for (auto &obj : objects) { if (obj.second->update_visibility(view3d)) { - LOG(INFO) << "Visible changed: " << obj.first.GetAsString() << " " << obj.second->is_visible(); + LOG(INFO) << "Visible changed: " << obj.first.GetAsString() << " " << obj.second->visible; if (obj.second->prim_type() == HdPrimTypeTokens->mesh) { index.GetChangeTracker().MarkRprimDirty(obj.first, HdChangeTracker::DirtyVisibility); } @@ -325,10 +337,8 @@ void BlenderSceneDelegate::update_visibility() HdMeshTopology BlenderSceneDelegate::GetMeshTopology(SdfPath const& id) { LOG(INFO) << "GetMeshTopology: " << id.GetAsString(); - ObjectDataPtr &obj_data = objects[id]; - return HdMeshTopology(PxOsdOpenSubdivTokens->catmullClark, HdTokens->rightHanded, - obj_data->get_data(HdBlenderTokens->faceCounts), - obj_data->get_data(HdTokens->pointsIndices)); + MeshData *m_data = mesh_data(id); + return m_data->mesh_topology(); } VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) @@ -338,12 +348,10 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) VtValue ret; ObjectData *obj_data = object_data(id); if (obj_data) { - if (obj_data->has_data(key)) { - ret = obj_data->get_data(key); - } + ret = obj_data->get_data(key); } else if (key.GetString() == "MaterialXFilename") { - MaterialDataPtr &mat_data = materials[id]; + MaterialData *mat_data = material_data(id); if (!mat_data->mtlx_path.GetResolvedPath().empty()) { ret = mat_data->mtlx_path; } @@ -358,34 +366,16 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(SdfPath const& id, HdInterpolation interpolation) { LOG(INFO) << "GetPrimvarDescriptors: " << id.GetAsString() << " " << interpolation; - HdPrimvarDescriptorVector primvars; - ObjectDataPtr &obj_data = objects[id]; - if (interpolation == HdInterpolationVertex) { - if (obj_data->has_data(HdTokens->points)) { - primvars.emplace_back(HdTokens->points, interpolation, HdPrimvarRoleTokens->point); - } - } - else if (interpolation == HdInterpolationFaceVarying) { - if (obj_data->has_data(HdTokens->normals)) { - primvars.emplace_back(HdTokens->normals, interpolation, HdPrimvarRoleTokens->normal); - } - if (obj_data->has_data(HdPrimvarRoleTokens->textureCoordinate)) { - primvars.emplace_back(HdPrimvarRoleTokens->textureCoordinate, interpolation, HdPrimvarRoleTokens->textureCoordinate); - } - } - return primvars; + MeshData *m_data = mesh_data(id); + return m_data->primvar_descriptors(interpolation); } SdfPath BlenderSceneDelegate::GetMaterialId(SdfPath const & rprimId) { - SdfPath ret; - ObjectData *obj_data = object_data(rprimId); - if (obj_data && obj_data->has_data(HdBlenderTokens->materialId)) { - ret = obj_data->get_data(HdBlenderTokens->materialId); - } + MeshData *m_data = mesh_data(rprimId); - LOG(INFO) << "GetMaterialId [" << rprimId.GetAsString() << "] = " << ret.GetAsString(); - return ret; + LOG(INFO) << "GetMaterialId [" << rprimId.GetAsString() << "] = " << m_data->material_id.GetAsString(); + return m_data->material_id; } VtValue BlenderSceneDelegate::GetMaterialResource(SdfPath const& id) @@ -397,25 +387,17 @@ VtValue BlenderSceneDelegate::GetMaterialResource(SdfPath const& id) GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id) { LOG(INFO) << "GetTransform: " << id.GetAsString(); - - return objects[id]->transform(); + return object_data(id)->transform(); } VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken const& key) { LOG(INFO) << "GetLightParamValue: " << id.GetAsString() << " [" << key.GetString() << "]"; - VtValue ret; - ObjectData *obj_data = object_data(id); - if (obj_data) { - if (obj_data->has_data(key)) { - ret = obj_data->get_data(key); - } - else if (key == HdLightTokens->exposure) { - // TODO: temporary value, it should be delivered through Python UI - ret = 1.0f; - } + LightData *l_data = light_data(id); + if (l_data) { + return l_data->get_data(key); } - return ret; + return VtValue(); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h index 77867f2f5c06..3a9cf2cbef64 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h @@ -15,6 +15,8 @@ #include "RNA_blender_cpp.h" #include "object.h" +#include "mesh.h" +#include "light.h" using namespace pxr; @@ -39,13 +41,15 @@ public: private: ObjectData *object_data(SdfPath const &id); + MeshData *mesh_data(SdfPath const &id); + LightData *light_data(SdfPath const &id); MaterialData *material_data(SdfPath const &id); SdfPath object_id(Object *object); SdfPath material_id(Material *material); bool supported_object(Object *object); void add_update_object(Object *object, bool geometry, bool transform, bool shading); - void set_material(ObjectData &obj_data); + void set_material(MeshData &mesh_data); void update_material(Material *material); void update_collection(); void update_visibility(); diff --git a/source/blender/render/hydra/sceneDelegate/id.h b/source/blender/render/hydra/sceneDelegate/id.h index e309e6ef431f..e32dbba0706f 100644 --- a/source/blender/render/hydra/sceneDelegate/id.h +++ b/source/blender/render/hydra/sceneDelegate/id.h @@ -3,7 +3,7 @@ #pragma once -#include +#include #include "DNA_ID.h" @@ -15,17 +15,10 @@ class IdData { virtual ~IdData() = default; std::string name(); - - virtual pxr::VtValue get_data(pxr::TfToken const &key) = 0; - template const T get_data(pxr::TfToken const &key); + virtual pxr::TfToken prim_type() = 0; protected: ID *id; }; -template const T IdData::get_data(pxr::TfToken const &key) -{ - return get_data(key).Get(); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index dd0b659344e4..3ab27e605c2d 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -2,6 +2,7 @@ * Copyright 2011-2022 Blender Foundation */ #include +#include #include #include "BKE_light.h" @@ -16,8 +17,7 @@ namespace blender::render::hydra { LightData::LightData(Object *object) : ObjectData(object) { - Object *object = (Object *)id; - Light *light = (Light *)object->data; + Light *light = (Light *)((Object *)id)->data; data[HdLightTokens->intensity] = light->energy; data[HdLightTokens->color] = GfVec3f(light->r, light->g, light->b); @@ -65,6 +65,46 @@ LightData::LightData(Object *object) default: break; } + + /* TODO: temporary value, it should be delivered through Python UI */ + data[HdLightTokens->exposure] = 1.0f; +} + +pxr::TfToken LightData::prim_type() +{ + Light *light = (Light *)((Object *)id)->data; + TfToken ret; + switch (light->type) { + case LA_LOCAL: + case LA_SPOT: + ret = HdPrimTypeTokens->sphereLight; + break; + + case LA_SUN: + ret = HdPrimTypeTokens->distantLight; + break; + + case LA_AREA: + switch (light->area_shape) { + case LA_AREA_SQUARE: + case LA_AREA_RECT: + ret = HdPrimTypeTokens->rectLight; + break; + + case LA_AREA_DISK: + case LA_AREA_ELLIPSE: + ret = HdPrimTypeTokens->diskLight; + break; + + default: + ret = HdPrimTypeTokens->rectLight; + } + break; + + default: + ret = HdPrimTypeTokens->sphereLight; + } + return ret; } VtValue LightData::get_data(TfToken const &key) diff --git a/source/blender/render/hydra/sceneDelegate/light.h b/source/blender/render/hydra/sceneDelegate/light.h index 3567bc67ffa4..f9c35d03d5a5 100644 --- a/source/blender/render/hydra/sceneDelegate/light.h +++ b/source/blender/render/hydra/sceneDelegate/light.h @@ -15,6 +15,7 @@ class LightData: public ObjectData { public: LightData(Object *object); + pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; private: diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index 6158d8d130e6..42227b0c4d60 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -3,6 +3,8 @@ #include +#include + #include "BKE_material.h" #include "BKE_lib_id.h" @@ -22,6 +24,11 @@ MaterialData::MaterialData(Material *material) { } +TfToken MaterialData::prim_type() +{ + return HdPrimTypeTokens->material; +} + void MaterialData::export_mtlx() { /* Call of python function hydra.export_mtlx() */ diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index e9db68f8e681..4cf79e37a576 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -23,6 +23,7 @@ class MaterialData: IdData { MaterialData(Material *material); + pxr::TfToken prim_type() override; void export_mtlx(); pxr::SdfAssetPath mtlx_path; }; diff --git a/source/blender/render/hydra/sceneDelegate/mesh.cc b/source/blender/render/hydra/sceneDelegate/mesh.cc index 74f19fba6c74..8e262e229d6a 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.cc +++ b/source/blender/render/hydra/sceneDelegate/mesh.cc @@ -30,6 +30,14 @@ MeshData::MeshData(Object *object) } } +pxr::TfToken MeshData::prim_type() +{ + if (face_vertex_counts.empty()) { + return TfToken(); + } + return HdPrimTypeTokens->mesh; +} + pxr::VtValue MeshData::get_data(pxr::TfToken const &key) { VtValue ret; @@ -54,6 +62,32 @@ Material *MeshData::material() return BKE_object_material_get_eval(object, object->actcol); } +pxr::HdMeshTopology MeshData::mesh_topology() +{ + return HdMeshTopology(PxOsdOpenSubdivTokens->catmullClark, HdTokens->rightHanded, + face_vertex_counts, face_vertex_indices); +} + +pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(pxr::HdInterpolation interpolation) +{ + HdPrimvarDescriptorVector primvars; + if (interpolation == HdInterpolationVertex) { + if (!vertices.empty()) { + primvars.emplace_back(HdTokens->points, interpolation, HdPrimvarRoleTokens->point); + } + } + else if (interpolation == HdInterpolationFaceVarying) { + if (!vertices.empty()) { + primvars.emplace_back(HdTokens->normals, interpolation, HdPrimvarRoleTokens->normal); + } + if (!uvs.empty()) { + primvars.emplace_back(HdPrimvarRoleTokens->textureCoordinate, interpolation, + HdPrimvarRoleTokens->textureCoordinate); + } + } + return primvars; +} + void MeshData::set_mesh(Mesh *mesh) { BKE_mesh_calc_normals_split(mesh); diff --git a/source/blender/render/hydra/sceneDelegate/mesh.h b/source/blender/render/hydra/sceneDelegate/mesh.h index 58bfb0a08701..4b9d40cc528a 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.h +++ b/source/blender/render/hydra/sceneDelegate/mesh.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include "object.h" @@ -13,18 +14,23 @@ class MeshData: public ObjectData { public: MeshData(Object *object); + pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; Material *material(); + pxr::HdMeshTopology mesh_topology(); + pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); + + pxr::SdfPath material_id; + + private: + void set_mesh(Mesh *mesh); + pxr::VtIntArray face_vertex_counts; pxr::VtIntArray face_vertex_indices; pxr::VtVec3fArray vertices; pxr::VtVec3fArray normals; pxr::VtVec2fArray uvs; - pxr::SdfPath material_id; - - private: - void set_mesh(Mesh *mesh); }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/object.cc b/source/blender/render/hydra/sceneDelegate/object.cc index 7eb3b542ebf3..31a400722a44 100644 --- a/source/blender/render/hydra/sceneDelegate/object.cc +++ b/source/blender/render/hydra/sceneDelegate/object.cc @@ -1,165 +1,51 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ -#include -#include -#include -#include -#include -#include - -#include "DNA_light_types.h" -#include "DNA_camera_types.h" - #include "BKE_object.h" -#include "BKE_lib_id.h" -#include "BKE_material.h" -#include "BKE_light.h" -#include "BKE_mesh.h" -#include "BKE_mesh_runtime.h" -#include "BKE_layer.h" #include "object.h" +#include "mesh.h" +#include "light.h" #include "../utils.h" -PXR_NAMESPACE_OPEN_SCOPE -TF_DEFINE_PUBLIC_TOKENS(HdBlenderTokens, HD_BLENDER_TOKENS); -PXR_NAMESPACE_CLOSE_SCOPE - using namespace pxr; namespace blender::render::hydra { ObjectDataPtr ObjectData::init(Object *object) { - return std::make_unique(object); + switch (object->type) { + case OB_MESH: + case OB_SURF: + case OB_FONT: + case OB_CURVES: + case OB_CURVES_LEGACY: + case OB_MBALL: + return std::make_unique(object); + + case OB_LAMP: + return std::make_unique(object); + + default: + break; + } + return nullptr; } ObjectData::ObjectData(Object *object) : IdData((ID *)object) , visible(true) { - switch (object->type) { - case OB_MESH: - if (object->mode == OB_MODE_OBJECT && BLI_listbase_is_empty(&object->modifiers)) { - set_as_mesh(); - } - else { - set_as_meshable(); - } - break; - - case OB_SURF: - case OB_FONT: - case OB_CURVES: - case OB_CURVES_LEGACY: - case OB_MBALL: - set_as_meshable(); - break; - - case OB_LAMP: - set_as_light(); - break; - - default: - break; - } } int ObjectData::type() { - return object->type; -} - -TfToken ObjectData::prim_type() -{ - TfToken ret = HdBlenderTokens->empty; - Light *light; - switch (object->type) { - case OB_MESH: - case OB_SURF: - case OB_FONT: - case OB_CURVES: - case OB_CURVES_LEGACY: - case OB_MBALL: - if (!has_data(HdTokens->points)) { - break; - } - ret = HdPrimTypeTokens->mesh; - break; - - case OB_LAMP: - light = (Light *)object->data; - switch (light->type) { - case LA_LOCAL: - case LA_SPOT: - ret = HdPrimTypeTokens->sphereLight; - break; - - case LA_SUN: - ret = HdPrimTypeTokens->distantLight; - break; - - case LA_AREA: - switch (light->area_shape) { - case LA_AREA_SQUARE: - case LA_AREA_RECT: - ret = HdPrimTypeTokens->rectLight; - break; - - case LA_AREA_DISK: - case LA_AREA_ELLIPSE: - ret = HdPrimTypeTokens->diskLight; - break; - - default: - ret = HdPrimTypeTokens->rectLight; - } - break; - - default: - ret = HdPrimTypeTokens->sphereLight; - } - break; - - default: - break; - } - - return ret; + return ((Object *)id)->type; } GfMatrix4d ObjectData::transform() { - return gf_matrix_from_transform(object->object_to_world); -} - -Material *ObjectData::material() -{ - if (BKE_object_material_count_eval(object) == 0) { - return nullptr; - } - return BKE_object_material_get_eval(object, object->actcol); -} - -VtValue &ObjectData::get_data(TfToken const &key) -{ - return data[key]; -} - -bool ObjectData::has_data(TfToken const &key) -{ - return data.find(key) != data.end(); -} - -void ObjectData::set_material_id(SdfPath const &id) -{ - if (id.IsEmpty()) { - data.erase(HdBlenderTokens->materialId); - } - else { - data[HdBlenderTokens->materialId] = id; - } + return gf_matrix_from_transform(((Object *)id)->object_to_world); } bool ObjectData::update_visibility(View3D *view3d) @@ -169,137 +55,8 @@ bool ObjectData::update_visibility(View3D *view3d) } bool prev_visible = visible; - visible = BKE_object_is_visible_in_viewport(view3d, object); + visible = BKE_object_is_visible_in_viewport(view3d, (Object *)id); return visible != prev_visible; } -bool ObjectData::is_visible() -{ - return visible; -} - -void ObjectData::set_as_mesh() -{ - Mesh *mesh = (Mesh *)object->data; - set_mesh(mesh); -} - -void ObjectData::set_as_meshable() -{ - Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); - set_mesh(mesh); - BKE_object_to_mesh_clear(object); -} - -void ObjectData::set_mesh(Mesh *mesh) -{ - BKE_mesh_calc_normals_split(mesh); - int tris_len = BKE_mesh_runtime_looptri_len(mesh); - if (tris_len == 0) { - return; - } - - blender::Span loopTris = mesh->looptris(); - - /* faceVertexCounts */ - data[HdBlenderTokens->faceCounts] = VtIntArray(tris_len, 3); - - /* faceVertexIndices */ - VtIntArray faceVertexIndices; - blender::Span loops = mesh->loops(); - faceVertexIndices.reserve(loopTris.size() * 3); - for (MLoopTri lt : loopTris) { - faceVertexIndices.push_back(loops[lt.tri[0]].v); - faceVertexIndices.push_back(loops[lt.tri[1]].v); - faceVertexIndices.push_back(loops[lt.tri[2]].v); - } - data[HdTokens->pointsIndices] = faceVertexIndices; - - /* vertices */ - VtVec3fArray vertices; - vertices.reserve(mesh->totvert); - blender::Span verts = mesh->vert_positions(); - for (blender::float3 v : verts) { - vertices.push_back(GfVec3f(v.x, v.y, v.z)); - } - data[HdTokens->points] = vertices; - - /* normals */ - const float(*lnors)[3] = (float(*)[3])CustomData_get_layer(&mesh->ldata, CD_NORMAL); - if (lnors) { - VtVec3fArray normals; - normals.reserve(loopTris.size() * 3); - for (MLoopTri lt : loopTris) { - normals.push_back(GfVec3f(lnors[lt.tri[0]])); - normals.push_back(GfVec3f(lnors[lt.tri[1]])); - normals.push_back(GfVec3f(lnors[lt.tri[2]])); - } - data[HdTokens->normals] = normals; - } - - /* UVs*/ - const float(*luvs)[2] = (float(*)[2])CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2); - if (luvs) { - VtVec2fArray uvs; - uvs.reserve(loopTris.size() * 3); - for (MLoopTri lt : loopTris) { - uvs.push_back(GfVec2f(luvs[lt.tri[0]])); - uvs.push_back(GfVec2f(luvs[lt.tri[1]])); - uvs.push_back(GfVec2f(luvs[lt.tri[2]])); - } - data[HdPrimvarRoleTokens->textureCoordinate] = uvs; - } -} - -void ObjectData::set_as_light() -{ - Light *light = (Light *)object->data; - data[HdLightTokens->intensity] = light->energy; - data[HdLightTokens->color] = GfVec3f(light->r, light->g, light->b); - - switch (light->type) { - case LA_LOCAL: - data[HdLightTokens->radius] = light->area_size / 2; - break; - - case LA_SUN: - data[HdLightTokens->angle] = light->sun_angle * 180.0 / M_PI; - break; - - case LA_SPOT: - data[HdLightTokens->shapingConeAngle] = light->spotsize / 2; - data[HdLightTokens->shapingConeSoftness] = light->spotblend; - data[UsdLuxTokens->treatAsPoint] = true; - break; - - case LA_AREA: - switch (light->area_shape) { - case LA_AREA_SQUARE: - data[HdLightTokens->width] = light->area_size; - data[HdLightTokens->height] = light->area_size; - break; - case LA_AREA_RECT: - data[HdLightTokens->width] = light->area_size; - data[HdLightTokens->height] = light->area_sizey; - break; - - case LA_AREA_DISK: - data[HdLightTokens->radius] = light->area_size / 2; - break; - - case LA_AREA_ELLIPSE: - data[HdLightTokens->radius] = (light->area_size + light->area_sizey) / 4; - break; - - default: - break; - } - data[HdLightTokens->normalize] = true; - break; - - default: - break; - } -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index f66ec5af5fe5..5f750d5ba6a9 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -3,26 +3,16 @@ #pragma once -#include -#include #include -#include "pxr/base/tf/staticTokens.h" +#include #include "pxr/base/tf/hashmap.h" #include "DNA_object_types.h" +#include "BKE_layer.h" #include "id.h" #include "material.h" -PXR_NAMESPACE_OPEN_SCOPE -#define HD_BLENDER_TOKENS \ - (materialId) \ - (faceCounts) \ - (empty) - -TF_DECLARE_PUBLIC_TOKENS(HdBlenderTokens, HD_BLENDER_TOKENS); -PXR_NAMESPACE_CLOSE_SCOPE - namespace blender::render::hydra { class ObjectData; @@ -36,25 +26,17 @@ public: ObjectData(Object *object); int type(); - pxr::TfToken prim_type(); pxr::GfMatrix4d transform(); - Material *material(); - - pxr::VtValue get_data(pxr::TfToken const &key); - bool has_data(pxr::TfToken const &key); - - void set_material_id(pxr::SdfPath const &id); + virtual pxr::VtValue get_data(pxr::TfToken const &key) = 0; + template const T get_data(pxr::TfToken const &key); bool update_visibility(View3D *view3d); - bool is_visible(); - private: - std::map data; bool visible; - - void set_as_mesh(); - void set_as_meshable(); - void set_mesh(Mesh *mesh); - void set_as_light(); }; +template const T ObjectData::get_data(pxr::TfToken const &key) +{ + return get_data(key).Get(); +} + } // namespace blender::render::hydra -- 2.30.2 From ecdbcc8e158a675275eb833381a9ddfa15f12d8f Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 16 Feb 2023 18:37:32 +0200 Subject: [PATCH 04/13] Continuing --- .../sceneDelegate/blenderSceneDelegate.cc | 18 ++++++------------ .../blender/render/hydra/sceneDelegate/id.cc | 7 +++++++ source/blender/render/hydra/sceneDelegate/id.h | 8 ++++++++ .../render/hydra/sceneDelegate/light.cc | 12 +++++++++--- .../render/hydra/sceneDelegate/material.cc | 11 +++++++++++ .../render/hydra/sceneDelegate/material.h | 2 ++ .../blender/render/hydra/sceneDelegate/mesh.cc | 8 ++++---- .../render/hydra/sceneDelegate/object.h | 8 +------- 8 files changed, 48 insertions(+), 26 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index d8e4be9eaf18..ece2e017418f 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -345,22 +345,16 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) { LOG(INFO) << "Get: " << id.GetAsString() << " [" << key.GetString() << "]"; - VtValue ret; ObjectData *obj_data = object_data(id); if (obj_data) { - ret = obj_data->get_data(key); + return obj_data->get_data(key); } - else if (key.GetString() == "MaterialXFilename") { - MaterialData *mat_data = material_data(id); - if (!mat_data->mtlx_path.GetResolvedPath().empty()) { - ret = mat_data->mtlx_path; - } + + MaterialData *mat_data = material_data(id); + if (mat_data) { + return mat_data->get_data(key); } - else if (key == HdStRenderBufferTokens->stormMsaaSampleCount) { - // TODO: temporary value, it should be delivered through Python UI - ret = 16; - } - return ret; + return VtValue(); } HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(SdfPath const& id, HdInterpolation interpolation) diff --git a/source/blender/render/hydra/sceneDelegate/id.cc b/source/blender/render/hydra/sceneDelegate/id.cc index ca60b5a3727a..1abaf761d56a 100644 --- a/source/blender/render/hydra/sceneDelegate/id.cc +++ b/source/blender/render/hydra/sceneDelegate/id.cc @@ -5,6 +5,8 @@ #include "id.h" +using namespace pxr; + namespace blender::render::hydra { IdData::IdData(ID *id) @@ -19,4 +21,9 @@ std::string IdData::name() return str; } +VtValue IdData::get_data(TfToken const &key) +{ + return VtValue(); +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/id.h b/source/blender/render/hydra/sceneDelegate/id.h index e32dbba0706f..ef8d2de75f7c 100644 --- a/source/blender/render/hydra/sceneDelegate/id.h +++ b/source/blender/render/hydra/sceneDelegate/id.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include "DNA_ID.h" @@ -16,9 +17,16 @@ class IdData { std::string name(); virtual pxr::TfToken prim_type() = 0; + virtual pxr::VtValue get_data(pxr::TfToken const &key); + template const T get_data(pxr::TfToken const &key); protected: ID *id; }; +template const T IdData::get_data(pxr::TfToken const &key) +{ + return get_data(key).Get(); +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index 3ab27e605c2d..7c634b360fa3 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ +#include + #include #include #include @@ -109,11 +111,15 @@ pxr::TfToken LightData::prim_type() VtValue LightData::get_data(TfToken const &key) { + VtValue ret; auto it = data.find(key); - if (it == data.end()) { - return VtValue(); + if (it != data.end()) { + ret = it->second; } - return it->second; + else if (boost::algorithm::ends_with(key.GetString(), "visibility:transparent")) { + ret = true; + } + return ret; } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index 42227b0c4d60..a4b2f3836d77 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -29,6 +29,17 @@ TfToken MaterialData::prim_type() return HdPrimTypeTokens->material; } +VtValue MaterialData::get_data(TfToken const &key) +{ + VtValue ret; + if (key.GetString() == "MaterialXFilename") { + if (!mtlx_path.GetResolvedPath().empty()) { + ret = mtlx_path; + } + } + return ret; +} + void MaterialData::export_mtlx() { /* Call of python function hydra.export_mtlx() */ diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index 4cf79e37a576..0b38c6325165 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -24,6 +24,8 @@ class MaterialData: IdData { MaterialData(Material *material); pxr::TfToken prim_type() override; + pxr::VtValue get_data(pxr::TfToken const &key) override; + void export_mtlx(); pxr::SdfAssetPath mtlx_path; }; diff --git a/source/blender/render/hydra/sceneDelegate/mesh.cc b/source/blender/render/hydra/sceneDelegate/mesh.cc index 8e262e229d6a..a484a54e899c 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.cc +++ b/source/blender/render/hydra/sceneDelegate/mesh.cc @@ -30,7 +30,7 @@ MeshData::MeshData(Object *object) } } -pxr::TfToken MeshData::prim_type() +TfToken MeshData::prim_type() { if (face_vertex_counts.empty()) { return TfToken(); @@ -38,7 +38,7 @@ pxr::TfToken MeshData::prim_type() return HdPrimTypeTokens->mesh; } -pxr::VtValue MeshData::get_data(pxr::TfToken const &key) +VtValue MeshData::get_data(TfToken const &key) { VtValue ret; if (key == HdTokens->points) { @@ -62,13 +62,13 @@ Material *MeshData::material() return BKE_object_material_get_eval(object, object->actcol); } -pxr::HdMeshTopology MeshData::mesh_topology() +HdMeshTopology MeshData::mesh_topology() { return HdMeshTopology(PxOsdOpenSubdivTokens->catmullClark, HdTokens->rightHanded, face_vertex_counts, face_vertex_indices); } -pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(pxr::HdInterpolation interpolation) +HdPrimvarDescriptorVector MeshData::primvar_descriptors(HdInterpolation interpolation) { HdPrimvarDescriptorVector primvars; if (interpolation == HdInterpolationVertex) { diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index 5f750d5ba6a9..4647594c48e6 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -3,7 +3,6 @@ #pragma once -#include #include #include "pxr/base/tf/hashmap.h" @@ -27,16 +26,11 @@ public: int type(); pxr::GfMatrix4d transform(); - virtual pxr::VtValue get_data(pxr::TfToken const &key) = 0; - template const T get_data(pxr::TfToken const &key); bool update_visibility(View3D *view3d); bool visible; }; -template const T ObjectData::get_data(pxr::TfToken const &key) -{ - return get_data(key).Get(); -} + } // namespace blender::render::hydra -- 2.30.2 From 13ef7269bb59ab638babe6a91dfbdd140bea3ed7 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 16 Feb 2023 19:26:33 +0200 Subject: [PATCH 05/13] Creating insert_prim, remove_prim, mark_prim_dirty, prim_id --- .../blender/render/hydra/sceneDelegate/id.cc | 8 ++++++++ .../blender/render/hydra/sceneDelegate/id.h | 14 +++++++++++++ .../render/hydra/sceneDelegate/light.cc | 12 +++++++++++ .../render/hydra/sceneDelegate/light.h | 3 +++ .../render/hydra/sceneDelegate/material.cc | 20 +++++++++++++++++++ .../render/hydra/sceneDelegate/material.h | 4 ++++ .../render/hydra/sceneDelegate/mesh.cc | 12 +++++++++++ .../blender/render/hydra/sceneDelegate/mesh.h | 6 +++++- .../render/hydra/sceneDelegate/object.cc | 8 ++++++++ .../render/hydra/sceneDelegate/object.h | 3 +++ 10 files changed, 89 insertions(+), 1 deletion(-) diff --git a/source/blender/render/hydra/sceneDelegate/id.cc b/source/blender/render/hydra/sceneDelegate/id.cc index 1abaf761d56a..9b502ea1386d 100644 --- a/source/blender/render/hydra/sceneDelegate/id.cc +++ b/source/blender/render/hydra/sceneDelegate/id.cc @@ -26,4 +26,12 @@ VtValue IdData::get_data(TfToken const &key) return VtValue(); } +pxr::SdfPath IdData::prim_id(pxr::HdSceneDelegate *scene_delegate, char prefix) +{ + /* Making id of object in form like _. Example: M_000002073e369608 */ + char str[32]; + snprintf(str, 32, "%c_%016llx", prefix, (uint64_t)id); + return scene_delegate->GetDelegateID().AppendElementString(str); +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/id.h b/source/blender/render/hydra/sceneDelegate/id.h index ef8d2de75f7c..48b899e3f6da 100644 --- a/source/blender/render/hydra/sceneDelegate/id.h +++ b/source/blender/render/hydra/sceneDelegate/id.h @@ -5,6 +5,7 @@ #include #include +#include #include "DNA_ID.h" @@ -20,8 +21,21 @@ class IdData { virtual pxr::VtValue get_data(pxr::TfToken const &key); template const T get_data(pxr::TfToken const &key); + enum DirtyBits { + DirtyTransform = 1, + DirtyVisibility, + DirtyMaterialId, + AllDirty + }; + + virtual void insert_prim(pxr::HdSceneDelegate *scene_delegate) = 0; + virtual void remove_prim(pxr::HdSceneDelegate *scene_delegate) = 0; + virtual void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) = 0; + virtual pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate) = 0; + protected: ID *id; + pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, char prefix); }; template const T IdData::get_data(pxr::TfToken const &key) diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index 7c634b360fa3..4959b8212e47 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -122,4 +122,16 @@ VtValue LightData::get_data(TfToken const &key) return ret; } +void LightData::insert_prim(pxr::HdSceneDelegate *scene_delegate) +{ +} + +void LightData::remove_prim(pxr::HdSceneDelegate *scene_delegate) +{ +} + +void LightData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +{ +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/light.h b/source/blender/render/hydra/sceneDelegate/light.h index f9c35d03d5a5..fb970c7c9935 100644 --- a/source/blender/render/hydra/sceneDelegate/light.h +++ b/source/blender/render/hydra/sceneDelegate/light.h @@ -17,6 +17,9 @@ class LightData: public ObjectData { pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; + void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; + void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; + void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; private: std::map data; diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index a4b2f3836d77..143acda5012e 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -64,4 +64,24 @@ void MaterialData::export_mtlx() mtlx_path = SdfAssetPath(path, path); } +void MaterialData::insert_prim(pxr::HdSceneDelegate *scene_delegate) +{ +} + +void MaterialData::remove_prim(pxr::HdSceneDelegate *scene_delegate) +{ +} + +void MaterialData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +{ +} + +pxr::SdfPath MaterialData::prim_id(pxr::HdSceneDelegate *scene_delegate) +{ + /* Making id of material in form like M_. Example: M_000002074e812088 */ + char str[32]; + snprintf(str, 32, "M_%016llx", (uint64_t)id); + return scene_delegate->GetDelegateID().AppendElementString(str); +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index 0b38c6325165..beecb0cae8c5 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -25,6 +25,10 @@ class MaterialData: IdData { pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; + void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; + void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; + void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; + pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate) override; void export_mtlx(); pxr::SdfAssetPath mtlx_path; diff --git a/source/blender/render/hydra/sceneDelegate/mesh.cc b/source/blender/render/hydra/sceneDelegate/mesh.cc index a484a54e899c..b6b11631f6ec 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.cc +++ b/source/blender/render/hydra/sceneDelegate/mesh.cc @@ -140,4 +140,16 @@ void MeshData::set_mesh(Mesh *mesh) } } +void MeshData::insert_prim(pxr::HdSceneDelegate *scene_delegate) +{ +} + +void MeshData::remove_prim(pxr::HdSceneDelegate *scene_delegate) +{ +} + +void MeshData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +{ +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/mesh.h b/source/blender/render/hydra/sceneDelegate/mesh.h index 4b9d40cc528a..52d77ff386c5 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.h +++ b/source/blender/render/hydra/sceneDelegate/mesh.h @@ -16,8 +16,12 @@ public: pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; - Material *material(); + void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; + void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; + void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; + + Material *material(); pxr::HdMeshTopology mesh_topology(); pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); diff --git a/source/blender/render/hydra/sceneDelegate/object.cc b/source/blender/render/hydra/sceneDelegate/object.cc index 31a400722a44..e41b241fd789 100644 --- a/source/blender/render/hydra/sceneDelegate/object.cc +++ b/source/blender/render/hydra/sceneDelegate/object.cc @@ -59,4 +59,12 @@ bool ObjectData::update_visibility(View3D *view3d) return visible != prev_visible; } +pxr::SdfPath ObjectData::prim_id(pxr::HdSceneDelegate *scene_delegate) +{ + /* Making id of object in form like O_. Example: O_000002073e369608 */ + char str[32]; + snprintf(str, 32, "O_%016llx", (uint64_t)id); + return scene_delegate->GetDelegateID().AppendElementString(str); +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index 4647594c48e6..5a3691da58c5 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -24,10 +24,13 @@ public: ObjectData(Object *object); + pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate) override; + int type(); pxr::GfMatrix4d transform(); bool update_visibility(View3D *view3d); + bool visible; }; -- 2.30.2 From 98c5d8d9155eb0a50f49e7a6b6c0e33fff516e2c Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Thu, 16 Feb 2023 21:07:14 +0200 Subject: [PATCH 06/13] Using of insert_prim, remove_prim, mark_prim_dirty. Code cleanup. --- .../sceneDelegate/blenderSceneDelegate.cc | 138 +++++------------- .../sceneDelegate/blenderSceneDelegate.h | 2 - .../blender/render/hydra/sceneDelegate/id.cc | 8 - .../blender/render/hydra/sceneDelegate/id.h | 6 +- .../render/hydra/sceneDelegate/light.cc | 26 ++++ .../render/hydra/sceneDelegate/material.cc | 35 ++++- .../render/hydra/sceneDelegate/material.h | 3 +- .../render/hydra/sceneDelegate/mesh.cc | 40 +++++ .../render/hydra/sceneDelegate/object.cc | 17 ++- .../render/hydra/sceneDelegate/object.h | 2 +- 10 files changed, 143 insertions(+), 134 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index ece2e017418f..afff0271407c 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -28,41 +28,33 @@ void BlenderSceneDelegate::set_material(MeshData &mesh_data) mesh_data.material_id = SdfPath::EmptyPath(); return; } - SdfPath mat_id = material_id(material); - MaterialData *mat_data = material_data(mat_id); + SdfPath id = MaterialData::prim_id(this, material); + MaterialData *mat_data = material_data(id); if (!mat_data) { - materials[mat_id] = MaterialData::init(material); - mat_data = material_data(mat_id); - GetRenderIndex().InsertSprim(mat_data->prim_type(), this, mat_id); + materials[id] = MaterialData::init(material); + mat_data = material_data(id); mat_data->export_mtlx(); - LOG(INFO) << "Add material: " << mat_id << ", mtlx=" << mat_data->mtlx_path.GetResolvedPath(); + mat_data->insert_prim(this); } - mesh_data.material_id = mat_id; + mesh_data.material_id = id; } void BlenderSceneDelegate::update_material(Material *material) { - SdfPath mat_id = material_id(material); - MaterialData *mat_data = material_data(mat_id); + MaterialData *mat_data = material_data(MaterialData::prim_id(this, material)); if (mat_data) { mat_data->export_mtlx(); - LOG(INFO) << "Update material: " << mat_id << ", mtlx=" << mat_data->mtlx_path.GetResolvedPath(); - GetRenderIndex().GetChangeTracker().MarkSprimDirty(mat_id, HdMaterial::AllDirty); + mat_data->mark_prim_dirty(this, IdData::DirtyBits::AllDirty); } } bool BlenderSceneDelegate::GetVisible(SdfPath const &id) { - ObjectData *obj_data = object_data(id); - LOG(INFO) << "GetVisible: " << id.GetAsString() << " " << obj_data->visible; - - return obj_data->visible; + return object_data(id)->visible; } void BlenderSceneDelegate::update_collection() { - HdRenderIndex &index = GetRenderIndex(); - /* add new objects */ std::set available_objects; for (auto &inst : b_depsgraph->object_instances) { @@ -73,8 +65,7 @@ void BlenderSceneDelegate::update_collection() if (!supported_object(object)) { continue; } - SdfPath obj_id = object_id(object); - available_objects.insert(obj_id); + available_objects.insert(ObjectData::prim_id(this, object)); if (!is_populated) { add_update_object(object, true, true, true); @@ -90,13 +81,7 @@ void BlenderSceneDelegate::update_collection() if (available_objects.find(it->first) != available_objects.end()) { continue; } - LOG(INFO) << "Remove: " << it->first; - if (it->second->prim_type() == HdPrimTypeTokens->mesh) { - index.RemoveRprim(it->first); - } - else if (!it->second->prim_type().IsEmpty()) { - index.RemoveSprim(it->second->prim_type(), it->first); - } + it->second->remove_prim(this); objects.erase(it); it = objects.begin(); } @@ -113,8 +98,7 @@ void BlenderSceneDelegate::update_collection() if (available_materials.find(it->first) != available_materials.end()) { continue; } - LOG(INFO) << "Remove material: " << it->first; - index.RemoveSprim(HdPrimTypeTokens->material, it->first); + it->second->remove_prim(this); materials.erase(it); it = materials.begin(); } @@ -122,57 +106,39 @@ void BlenderSceneDelegate::update_collection() void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool transform, bool shading) { - HdRenderIndex &index = GetRenderIndex(); - - SdfPath obj_id = object_id(object); - ObjectData *obj_data = object_data(obj_id); + SdfPath id = ObjectData::prim_id(this, object); + ObjectData *obj_data = object_data(id); if (!obj_data) { - objects[obj_id] = ObjectData::init(object); - obj_data = object_data(obj_id); + objects[id] = ObjectData::init(object); + obj_data = object_data(id); obj_data->update_visibility(view3d); - if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { - MeshData *m_data = dynamic_cast(obj_data); - LOG(INFO) << "Add mesh object: " << m_data->name() << " id=" << obj_id; - index.InsertRprim(m_data->prim_type(), this, obj_id); + obj_data->insert_prim(this); + MeshData *m_data = dynamic_cast(obj_data); + if (m_data) { set_material(*m_data); } - else if (obj_data->type() == OB_LAMP) { - LOG(INFO) << "Add light object: " << obj_data->name() << " id=" << obj_id; - index.InsertSprim(obj_data->prim_type(), this, obj_id); - } return; } if (geometry) { - LOG(INFO) << "Full updated: " << obj_id; - objects[obj_id] = ObjectData::init(object); - obj_data = object_data(obj_id); + LOG(INFO) << "Full updated: " << id; + objects[id] = ObjectData::init(object); + obj_data = object_data(id); obj_data->update_visibility(view3d); - if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { - set_material(*dynamic_cast(obj_data)); - index.GetChangeTracker().MarkRprimDirty(obj_id, HdChangeTracker::AllDirty); - } - else if (obj_data->type() == OB_LAMP) { - index.GetChangeTracker().MarkSprimDirty(obj_id, HdLight::AllDirty); + MeshData *m_data = dynamic_cast(obj_data); + if (m_data) { + set_material(*m_data); } + obj_data->mark_prim_dirty(this, IdData::DirtyBits::AllDirty); return; } if (transform) { - LOG(INFO) << "Transform updated: " << obj_id; - if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { - index.GetChangeTracker().MarkRprimDirty(obj_id, HdChangeTracker::DirtyTransform); - } - else if (obj_data->type() == OB_LAMP) { - index.GetChangeTracker().MarkSprimDirty(obj_id, HdLight::DirtyTransform); - } + obj_data->mark_prim_dirty(this, IdData::DirtyBits::DirtyTransform); } if (shading) { - LOG(INFO) << "Shading updated: " << obj_id; - if (obj_data->prim_type() == HdPrimTypeTokens->mesh) { - index.GetChangeTracker().MarkRprimDirty(obj_id, HdChangeTracker::DirtyMaterialId); - } + obj_data->mark_prim_dirty(this, IdData::DirtyBits::DirtyMaterial); } } @@ -204,22 +170,6 @@ MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id) return it->second.get(); } -SdfPath BlenderSceneDelegate::object_id(Object *object) -{ - /* Making id of object in form like O_. Example: O_000002073e369608 */ - char str[32]; - snprintf(str, 32, "O_%016llx", (uint64_t)object); - return GetDelegateID().AppendElementString(str); -} - -SdfPath BlenderSceneDelegate::material_id(Material *material) -{ - /* Making id of material in form like M_. Example: M_000002074e812088 */ - char str[32]; - snprintf(str, 32, "M_%016llx", (uint64_t)material); - return GetDelegateID().AppendElementString(str); -} - bool BlenderSceneDelegate::supported_object(Object *object) { return object->type == OB_MESH || @@ -302,18 +252,10 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, View3D *v3d) void BlenderSceneDelegate::update_visibility() { - HdRenderIndex &index = GetRenderIndex(); - /* Check and update visibility */ for (auto &obj : objects) { if (obj.second->update_visibility(view3d)) { - LOG(INFO) << "Visible changed: " << obj.first.GetAsString() << " " << obj.second->visible; - if (obj.second->prim_type() == HdPrimTypeTokens->mesh) { - index.GetChangeTracker().MarkRprimDirty(obj.first, HdChangeTracker::DirtyVisibility); - } - else if (obj.second->type() == OB_LAMP) { - index.GetChangeTracker().MarkSprimDirty(obj.first, HdLight::DirtyParams); - } + obj.second->mark_prim_dirty(this, IdData::DirtyBits::DirtyVisibility); }; } @@ -327,8 +269,7 @@ void BlenderSceneDelegate::update_visibility() continue; } - SdfPath obj_id = object_id(object); - if (!object_data(obj_id)) { + if (!object_data(ObjectData::prim_id(this, object))) { add_update_object(object, true, true, true); } } @@ -336,15 +277,12 @@ void BlenderSceneDelegate::update_visibility() HdMeshTopology BlenderSceneDelegate::GetMeshTopology(SdfPath const& id) { - LOG(INFO) << "GetMeshTopology: " << id.GetAsString(); MeshData *m_data = mesh_data(id); return m_data->mesh_topology(); } VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) { - LOG(INFO) << "Get: " << id.GetAsString() << " [" << key.GetString() << "]"; - ObjectData *obj_data = object_data(id); if (obj_data) { return obj_data->get_data(key); @@ -359,34 +297,30 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key) HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(SdfPath const& id, HdInterpolation interpolation) { - LOG(INFO) << "GetPrimvarDescriptors: " << id.GetAsString() << " " << interpolation; - MeshData *m_data = mesh_data(id); - return m_data->primvar_descriptors(interpolation); + return mesh_data(id)->primvar_descriptors(interpolation); } SdfPath BlenderSceneDelegate::GetMaterialId(SdfPath const & rprimId) { - MeshData *m_data = mesh_data(rprimId); - - LOG(INFO) << "GetMaterialId [" << rprimId.GetAsString() << "] = " << m_data->material_id.GetAsString(); - return m_data->material_id; + return mesh_data(rprimId)->material_id; } VtValue BlenderSceneDelegate::GetMaterialResource(SdfPath const& id) { - LOG(INFO) << "GetMaterialResource: " << id.GetAsString(); + MaterialData *mat_data = material_data(id); + if (mat_data) { + return mat_data->material_resource(); + } return VtValue(); } GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id) { - LOG(INFO) << "GetTransform: " << id.GetAsString(); return object_data(id)->transform(); } VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken const& key) { - LOG(INFO) << "GetLightParamValue: " << id.GetAsString() << " [" << key.GetString() << "]"; LightData *l_data = light_data(id); if (l_data) { return l_data->get_data(key); diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h index 3a9cf2cbef64..b0157971f0b8 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h @@ -44,8 +44,6 @@ private: MeshData *mesh_data(SdfPath const &id); LightData *light_data(SdfPath const &id); MaterialData *material_data(SdfPath const &id); - SdfPath object_id(Object *object); - SdfPath material_id(Material *material); bool supported_object(Object *object); void add_update_object(Object *object, bool geometry, bool transform, bool shading); diff --git a/source/blender/render/hydra/sceneDelegate/id.cc b/source/blender/render/hydra/sceneDelegate/id.cc index 9b502ea1386d..1abaf761d56a 100644 --- a/source/blender/render/hydra/sceneDelegate/id.cc +++ b/source/blender/render/hydra/sceneDelegate/id.cc @@ -26,12 +26,4 @@ VtValue IdData::get_data(TfToken const &key) return VtValue(); } -pxr::SdfPath IdData::prim_id(pxr::HdSceneDelegate *scene_delegate, char prefix) -{ - /* Making id of object in form like _. Example: M_000002073e369608 */ - char str[32]; - snprintf(str, 32, "%c_%016llx", prefix, (uint64_t)id); - return scene_delegate->GetDelegateID().AppendElementString(str); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/id.h b/source/blender/render/hydra/sceneDelegate/id.h index 48b899e3f6da..d4051cb31e94 100644 --- a/source/blender/render/hydra/sceneDelegate/id.h +++ b/source/blender/render/hydra/sceneDelegate/id.h @@ -21,21 +21,19 @@ class IdData { virtual pxr::VtValue get_data(pxr::TfToken const &key); template const T get_data(pxr::TfToken const &key); - enum DirtyBits { + enum class DirtyBits { DirtyTransform = 1, DirtyVisibility, - DirtyMaterialId, + DirtyMaterial, AllDirty }; virtual void insert_prim(pxr::HdSceneDelegate *scene_delegate) = 0; virtual void remove_prim(pxr::HdSceneDelegate *scene_delegate) = 0; virtual void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) = 0; - virtual pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate) = 0; protected: ID *id; - pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, char prefix); }; template const T IdData::get_data(pxr::TfToken const &key) diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index 4959b8212e47..6136d5cb229b 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -7,6 +7,8 @@ #include #include +#include "glog/logging.h" + #include "BKE_light.h" #include "DNA_light_types.h" @@ -111,6 +113,8 @@ pxr::TfToken LightData::prim_type() VtValue LightData::get_data(TfToken const &key) { + LOG(INFO) << "Get data light: " << name() << " [" << key.GetString() << "]"; + VtValue ret; auto it = data.find(key); if (it != data.end()) { @@ -124,14 +128,36 @@ VtValue LightData::get_data(TfToken const &key) void LightData::insert_prim(pxr::HdSceneDelegate *scene_delegate) { + scene_delegate->GetRenderIndex().InsertSprim( + prim_type(), scene_delegate, prim_id(scene_delegate, (Object *)id)); + LOG(INFO) << "Add light: " << name(); } void LightData::remove_prim(pxr::HdSceneDelegate *scene_delegate) { + scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), prim_id(scene_delegate, (Object *)id)); + LOG(INFO) << "Remove light: " << name(); } void LightData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) { + HdDirtyBits bits = HdLight::Clean; + switch (dirty_bits) { + case DirtyBits::DirtyTransform: + bits = HdLight::DirtyTransform; + break; + case DirtyBits::DirtyVisibility: + bits = HdLight::DirtyParams; + break; + case DirtyBits::AllDirty: + bits = HdLight::AllDirty; + break; + default: + break; + } + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty( + prim_id(scene_delegate, (Object *)id), bits); + LOG(INFO) << "Update light: " << name() << " [" << (int)dirty_bits << "]"; } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index 143acda5012e..31545f44cd1c 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -4,6 +4,9 @@ #include #include +#include + +#include "glog/logging.h" #include "BKE_material.h" #include "BKE_lib_id.h" @@ -19,6 +22,15 @@ MaterialDataPtr MaterialData::init(Material *material) return std::make_unique(material); } +pxr::SdfPath MaterialData::prim_id(pxr::HdSceneDelegate *scene_delegate, Material *material) +{ + /* Making id of material in form like M_. + * Example: M_000002074e812088 */ + char str[32]; + snprintf(str, 32, "M_%016llx", (uint64_t)material); + return scene_delegate->GetDelegateID().AppendElementString(str); +} + MaterialData::MaterialData(Material *material) : IdData((ID *)material) { @@ -40,6 +52,12 @@ VtValue MaterialData::get_data(TfToken const &key) return ret; } +pxr::VtValue MaterialData::material_resource() +{ + /* TODO: Implement return of HdMaterialNetwork */ + return pxr::VtValue(); +} + void MaterialData::export_mtlx() { /* Call of python function hydra.export_mtlx() */ @@ -66,22 +84,23 @@ void MaterialData::export_mtlx() void MaterialData::insert_prim(pxr::HdSceneDelegate *scene_delegate) { + scene_delegate->GetRenderIndex().InsertSprim( + HdPrimTypeTokens->material, scene_delegate, prim_id(scene_delegate, (Material *)id)); + LOG(INFO) << "Add material: " << name() << ", mtlx=" << mtlx_path.GetResolvedPath(); } void MaterialData::remove_prim(pxr::HdSceneDelegate *scene_delegate) { + scene_delegate->GetRenderIndex().RemoveSprim( + HdPrimTypeTokens->material, prim_id(scene_delegate, (Material *)id)); + LOG(INFO) << "Remove material: " << name(); } void MaterialData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) { -} - -pxr::SdfPath MaterialData::prim_id(pxr::HdSceneDelegate *scene_delegate) -{ - /* Making id of material in form like M_. Example: M_000002074e812088 */ - char str[32]; - snprintf(str, 32, "M_%016llx", (uint64_t)id); - return scene_delegate->GetDelegateID().AppendElementString(str); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty( + prim_id(scene_delegate, (Material *)id), HdMaterial::AllDirty); + LOG(INFO) << "Update material: " << name() << ", mtlx=" << mtlx_path.GetResolvedPath(); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index beecb0cae8c5..444739b81142 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -20,6 +20,7 @@ using MaterialDataMap = pxr::TfHashMap #include +#include "glog/logging.h" + #include "BKE_mesh.h" #include "BKE_mesh_runtime.h" #include "BKE_object.h" @@ -142,14 +144,52 @@ void MeshData::set_mesh(Mesh *mesh) void MeshData::insert_prim(pxr::HdSceneDelegate *scene_delegate) { + if (face_vertex_counts.empty()) { + return; + } + + scene_delegate->GetRenderIndex().InsertRprim( + HdPrimTypeTokens->mesh, scene_delegate, prim_id(scene_delegate, (Object *)id)); + LOG(INFO) << "Add mesh: " << name(); } void MeshData::remove_prim(pxr::HdSceneDelegate *scene_delegate) { + SdfPath p_id = prim_id(scene_delegate, (Object *)id); + if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { + return; + } + + scene_delegate->GetRenderIndex().RemoveRprim(p_id); + LOG(INFO) << "Remove mesh: " << name(); } void MeshData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) { + SdfPath p_id = prim_id(scene_delegate, (Object *)id); + if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { + return; + } + + HdDirtyBits bits = HdChangeTracker::Clean; + switch (dirty_bits) { + case DirtyBits::DirtyTransform: + bits = HdChangeTracker::DirtyTransform; + break; + case DirtyBits::DirtyVisibility: + bits = HdChangeTracker::DirtyVisibility; + break; + case DirtyBits::DirtyMaterial: + bits = HdChangeTracker::DirtyMaterialId; + break; + case DirtyBits::AllDirty: + bits = HdChangeTracker::AllDirty; + break; + default: + break; + } + scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits); + LOG(INFO) << "Update mesh: " << name() << " [" << (int)dirty_bits << "]"; } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/object.cc b/source/blender/render/hydra/sceneDelegate/object.cc index e41b241fd789..9f0c3ae11a30 100644 --- a/source/blender/render/hydra/sceneDelegate/object.cc +++ b/source/blender/render/hydra/sceneDelegate/object.cc @@ -32,6 +32,15 @@ ObjectDataPtr ObjectData::init(Object *object) return nullptr; } +pxr::SdfPath ObjectData::prim_id(pxr::HdSceneDelegate *scene_delegate, Object *object) +{ + /* Making id of object in form like O_. Example: + * O_000002073e369608 */ + char str[32]; + snprintf(str, 32, "O_%016llx", (uint64_t)object); + return scene_delegate->GetDelegateID().AppendElementString(str); +} + ObjectData::ObjectData(Object *object) : IdData((ID *)object) , visible(true) @@ -59,12 +68,4 @@ bool ObjectData::update_visibility(View3D *view3d) return visible != prev_visible; } -pxr::SdfPath ObjectData::prim_id(pxr::HdSceneDelegate *scene_delegate) -{ - /* Making id of object in form like O_. Example: O_000002073e369608 */ - char str[32]; - snprintf(str, 32, "O_%016llx", (uint64_t)id); - return scene_delegate->GetDelegateID().AppendElementString(str); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index 5a3691da58c5..e6909297e8eb 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -21,10 +21,10 @@ using ObjectDataMap = pxr::TfHashMap Date: Thu, 16 Feb 2023 21:21:05 +0200 Subject: [PATCH 07/13] Cleanup --- .../render/hydra/sceneDelegate/blenderSceneDelegate.cc | 1 - source/blender/render/hydra/sceneDelegate/id.h | 1 - source/blender/render/hydra/sceneDelegate/light.h | 2 +- source/blender/render/hydra/sceneDelegate/material.cc | 5 ----- source/blender/render/hydra/sceneDelegate/material.h | 1 - source/blender/render/hydra/sceneDelegate/mesh.cc | 8 -------- source/blender/render/hydra/sceneDelegate/mesh.h | 1 - 7 files changed, 1 insertion(+), 18 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index afff0271407c..25d7831b9306 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -121,7 +121,6 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool } if (geometry) { - LOG(INFO) << "Full updated: " << id; objects[id] = ObjectData::init(object); obj_data = object_data(id); obj_data->update_visibility(view3d); diff --git a/source/blender/render/hydra/sceneDelegate/id.h b/source/blender/render/hydra/sceneDelegate/id.h index d4051cb31e94..dbfe10e8a66f 100644 --- a/source/blender/render/hydra/sceneDelegate/id.h +++ b/source/blender/render/hydra/sceneDelegate/id.h @@ -17,7 +17,6 @@ class IdData { virtual ~IdData() = default; std::string name(); - virtual pxr::TfToken prim_type() = 0; virtual pxr::VtValue get_data(pxr::TfToken const &key); template const T get_data(pxr::TfToken const &key); diff --git a/source/blender/render/hydra/sceneDelegate/light.h b/source/blender/render/hydra/sceneDelegate/light.h index fb970c7c9935..28dce7f078c9 100644 --- a/source/blender/render/hydra/sceneDelegate/light.h +++ b/source/blender/render/hydra/sceneDelegate/light.h @@ -15,7 +15,6 @@ class LightData: public ObjectData { public: LightData(Object *object); - pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; @@ -23,6 +22,7 @@ class LightData: public ObjectData { private: std::map data; + pxr::TfToken prim_type(); }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index 31545f44cd1c..419f075a2e47 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -36,11 +36,6 @@ MaterialData::MaterialData(Material *material) { } -TfToken MaterialData::prim_type() -{ - return HdPrimTypeTokens->material; -} - VtValue MaterialData::get_data(TfToken const &key) { VtValue ret; diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index 444739b81142..f89304a46c85 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -24,7 +24,6 @@ class MaterialData: IdData { MaterialData(Material *material); - pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; diff --git a/source/blender/render/hydra/sceneDelegate/mesh.cc b/source/blender/render/hydra/sceneDelegate/mesh.cc index d6eab0cdf6c3..fb4369d64ba0 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.cc +++ b/source/blender/render/hydra/sceneDelegate/mesh.cc @@ -32,14 +32,6 @@ MeshData::MeshData(Object *object) } } -TfToken MeshData::prim_type() -{ - if (face_vertex_counts.empty()) { - return TfToken(); - } - return HdPrimTypeTokens->mesh; -} - VtValue MeshData::get_data(TfToken const &key) { VtValue ret; diff --git a/source/blender/render/hydra/sceneDelegate/mesh.h b/source/blender/render/hydra/sceneDelegate/mesh.h index 52d77ff386c5..bedfd3d2f63e 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.h +++ b/source/blender/render/hydra/sceneDelegate/mesh.h @@ -14,7 +14,6 @@ class MeshData: public ObjectData { public: MeshData(Object *object); - pxr::TfToken prim_type() override; pxr::VtValue get_data(pxr::TfToken const &key) override; void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; -- 2.30.2 From 1a949d73a62311bb43edc16784a41050a75b3b48 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 17 Feb 2023 12:49:50 +0200 Subject: [PATCH 08/13] Code improvements --- .../sceneDelegate/blenderSceneDelegate.cc | 4 +-- .../render/hydra/sceneDelegate/light.cc | 13 +++++---- .../render/hydra/sceneDelegate/material.cc | 28 ++++++++++++------- .../render/hydra/sceneDelegate/material.h | 5 ++-- .../render/hydra/sceneDelegate/mesh.cc | 6 ++-- .../render/hydra/sceneDelegate/object.cc | 2 +- .../render/hydra/sceneDelegate/object.h | 7 ++--- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index 25d7831b9306..56fd926d7172 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -201,10 +201,10 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, View3D *v3d) for (auto &update : b_depsgraph->updates) { BL::ID id = update.id(); - LOG(INFO) << "Update: " << id.name_full() << " " + LOG(INFO) << "Update: " << id.name_full() << " [" << update.is_updated_transform() << update.is_updated_geometry() - << update.is_updated_shading(); + << update.is_updated_shading() << "]"; if (id.is_a(&RNA_Object)) { Object *object = (Object *)id.ptr.data; diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index 6136d5cb229b..b9e759a8829e 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -128,14 +128,15 @@ VtValue LightData::get_data(TfToken const &key) void LightData::insert_prim(pxr::HdSceneDelegate *scene_delegate) { - scene_delegate->GetRenderIndex().InsertSprim( - prim_type(), scene_delegate, prim_id(scene_delegate, (Object *)id)); - LOG(INFO) << "Add light: " << name(); + SdfPath p_id = prim_id(scene_delegate, (Object *)id); + scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); + LOG(INFO) << "Add light: " << name() << " id=" << p_id.GetAsString(); } void LightData::remove_prim(pxr::HdSceneDelegate *scene_delegate) { - scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), prim_id(scene_delegate, (Object *)id)); + SdfPath p_id = prim_id(scene_delegate, (Object *)id); + scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); LOG(INFO) << "Remove light: " << name(); } @@ -155,8 +156,8 @@ void LightData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits default: break; } - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty( - prim_id(scene_delegate, (Object *)id), bits); + SdfPath p_id = prim_id(scene_delegate, (Object *)id); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); LOG(INFO) << "Update light: " << name() << " [" << (int)dirty_bits << "]"; } diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index 419f075a2e47..91885ee00f6e 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -17,7 +17,7 @@ using namespace pxr; namespace blender::render::hydra { -MaterialDataPtr MaterialData::init(Material *material) +std::unique_ptr MaterialData::init(Material *material) { return std::make_unique(material); } @@ -57,11 +57,10 @@ void MaterialData::export_mtlx() { /* Call of python function hydra.export_mtlx() */ - PyObject *module, *dict, *func, *result; - PyGILState_STATE gstate; gstate = PyGILState_Ensure(); + PyObject *module, *dict, *func, *result; module = PyImport_ImportModule("hydra"); dict = PyModule_GetDict(module); func = PyDict_GetItemString(dict, "export_mtlx"); @@ -79,22 +78,31 @@ void MaterialData::export_mtlx() void MaterialData::insert_prim(pxr::HdSceneDelegate *scene_delegate) { - scene_delegate->GetRenderIndex().InsertSprim( - HdPrimTypeTokens->material, scene_delegate, prim_id(scene_delegate, (Material *)id)); - LOG(INFO) << "Add material: " << name() << ", mtlx=" << mtlx_path.GetResolvedPath(); + SdfPath p_id = prim_id(scene_delegate, (Material *)id); + scene_delegate->GetRenderIndex().InsertSprim(HdPrimTypeTokens->material, scene_delegate, p_id); + LOG(INFO) << "Add material: " << name() << " id=" << p_id.GetAsString() + << ", mtlx=" << mtlx_path.GetResolvedPath(); } void MaterialData::remove_prim(pxr::HdSceneDelegate *scene_delegate) { - scene_delegate->GetRenderIndex().RemoveSprim( - HdPrimTypeTokens->material, prim_id(scene_delegate, (Material *)id)); + SdfPath p_id = prim_id(scene_delegate, (Material *)id); + scene_delegate->GetRenderIndex().RemoveSprim(HdPrimTypeTokens->material, p_id); LOG(INFO) << "Remove material: " << name(); } void MaterialData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) { - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty( - prim_id(scene_delegate, (Material *)id), HdMaterial::AllDirty); + HdDirtyBits bits = HdMaterial::Clean; + switch (dirty_bits) { + case DirtyBits::AllDirty: + bits = HdMaterial::AllDirty; + break; + default: + break; + } + SdfPath p_id = prim_id(scene_delegate, (Material *)id); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); LOG(INFO) << "Update material: " << name() << ", mtlx=" << mtlx_path.GetResolvedPath(); } diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index f89304a46c85..4c6366750a78 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -14,12 +14,11 @@ namespace blender::render::hydra { class MaterialData; -using MaterialDataPtr = std::unique_ptr; -using MaterialDataMap = pxr::TfHashMap; +using MaterialDataMap = pxr::TfHashMap, pxr::SdfPath::Hash>; class MaterialData: IdData { public: - static MaterialDataPtr init(Material *material); + static std::unique_ptr init(Material *material); static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, Material *material); MaterialData(Material *material); diff --git a/source/blender/render/hydra/sceneDelegate/mesh.cc b/source/blender/render/hydra/sceneDelegate/mesh.cc index fb4369d64ba0..e3e63a2d3aa0 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.cc +++ b/source/blender/render/hydra/sceneDelegate/mesh.cc @@ -140,9 +140,9 @@ void MeshData::insert_prim(pxr::HdSceneDelegate *scene_delegate) return; } - scene_delegate->GetRenderIndex().InsertRprim( - HdPrimTypeTokens->mesh, scene_delegate, prim_id(scene_delegate, (Object *)id)); - LOG(INFO) << "Add mesh: " << name(); + SdfPath p_id = prim_id(scene_delegate, (Object *)id); + scene_delegate->GetRenderIndex().InsertRprim(HdPrimTypeTokens->mesh, scene_delegate, p_id); + LOG(INFO) << "Add mesh: " << name() << " id=" << p_id.GetAsString(); } void MeshData::remove_prim(pxr::HdSceneDelegate *scene_delegate) diff --git a/source/blender/render/hydra/sceneDelegate/object.cc b/source/blender/render/hydra/sceneDelegate/object.cc index 9f0c3ae11a30..b48187e9aee0 100644 --- a/source/blender/render/hydra/sceneDelegate/object.cc +++ b/source/blender/render/hydra/sceneDelegate/object.cc @@ -12,7 +12,7 @@ using namespace pxr; namespace blender::render::hydra { -ObjectDataPtr ObjectData::init(Object *object) +std::unique_ptr ObjectData::init(Object *object) { switch (object->type) { case OB_MESH: diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index e6909297e8eb..3b18668c65e8 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -15,22 +15,19 @@ namespace blender::render::hydra { class ObjectData; -using ObjectDataPtr = std::unique_ptr; -using ObjectDataMap = pxr::TfHashMap; +using ObjectDataMap = pxr::TfHashMap, pxr::SdfPath::Hash>; class ObjectData: public IdData { public: - static ObjectDataPtr init(Object *object); + static std::unique_ptr init(Object *object); static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, Object *object); ObjectData(Object *object); - int type(); pxr::GfMatrix4d transform(); bool update_visibility(View3D *view3d); - bool visible; }; -- 2.30.2 From debd92a2aa743cf9cda83408f6403b488a1b7d93 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 17 Feb 2023 15:21:37 +0200 Subject: [PATCH 09/13] Making area light invisible --- source/blender/render/hydra/sceneDelegate/light.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index b9e759a8829e..6ddca28da4f7 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -120,8 +120,8 @@ VtValue LightData::get_data(TfToken const &key) if (it != data.end()) { ret = it->second; } - else if (boost::algorithm::ends_with(key.GetString(), "visibility:transparent")) { - ret = true; + else if (boost::algorithm::ends_with(key.GetString(), "visibility:camera")) { + ret = false; } return ret; } -- 2.30.2 From 64cbd38fa776d6c93c5308c09a6510133907bb30 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 17 Feb 2023 16:07:14 +0200 Subject: [PATCH 10/13] Fix after merge --- .../render/hydra/sceneDelegate/blenderSceneDelegate.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index 305a1622b6ed..3559c5df54dd 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -368,12 +368,10 @@ VtValue BlenderSceneDelegate::GetMaterialResource(SdfPath const& id) GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id) { - return objects[id].transform(); if (id == world_id()) { return world_data->transform(GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName()); } - - return objects[id].transform(); + return object_data(id)->transform(); } VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken const& key) -- 2.30.2 From 7e75c09cac115e0e7f14cde41e568bf072b3850d Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 17 Feb 2023 21:15:01 +0200 Subject: [PATCH 11/13] Adjusted WorldData to new design. Fixed issues in World update. --- .../sceneDelegate/blenderSceneDelegate.cc | 85 +++++++++---------- .../sceneDelegate/blenderSceneDelegate.h | 3 +- .../render/hydra/sceneDelegate/world.cc | 74 ++++++++++------ .../render/hydra/sceneDelegate/world.h | 29 +++---- 4 files changed, 102 insertions(+), 89 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index 3559c5df54dd..d6cd0f31a234 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -52,32 +52,31 @@ void BlenderSceneDelegate::update_material(Material *material) } } -void BlenderSceneDelegate::add_update_world(World *world) +void BlenderSceneDelegate::update_world() { - SdfPath world_light_id = world_id(); - - LOG(INFO) << "Add world: " << world_light_id; - - if (!world) { - world_data = nullptr; - GetRenderIndex().RemoveSprim(HdPrimTypeTokens->domeLight, world_light_id); - return; - } - + World *world = (World *)b_depsgraph->scene().world().ptr.data; if (!world_data) { - world_data = make_unique(world, (bContext *)b_context->ptr.data); - GetRenderIndex().InsertSprim(HdPrimTypeTokens->domeLight, this, world_light_id); + if (world) { + world_data = WorldData::init(world, (bContext *)b_context->ptr.data); + world_data->insert_prim(this); + } } else { - world_data = make_unique(world, (bContext *)b_context->ptr.data); - GetRenderIndex().GetChangeTracker().MarkSprimDirty(world_light_id, HdLight::AllDirty); + if (world) { + world_data = WorldData::init(world, (bContext *)b_context->ptr.data); + world_data->mark_prim_dirty(this, IdData::DirtyBits::AllDirty); + } + else { + world_data->remove_prim(this); + world_data = nullptr; + } } } bool BlenderSceneDelegate::GetVisible(SdfPath const &id) { - if (id == world_id()) { - return world_data->is_visible(); + if (id == WorldData::prim_id(this)) { + return true; } return object_data(id)->visible; @@ -182,12 +181,12 @@ ObjectData *BlenderSceneDelegate::object_data(SdfPath const &id) MeshData *BlenderSceneDelegate::mesh_data(SdfPath const &id) { - return dynamic_cast(object_data(id)); + return static_cast(object_data(id)); } LightData *BlenderSceneDelegate::light_data(SdfPath const &id) { - return dynamic_cast(object_data(id)); + return static_cast(object_data(id)); } MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id) @@ -199,11 +198,6 @@ MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id) return it->second.get(); } -SdfPath BlenderSceneDelegate::world_id() -{ - return GetDelegateID().AppendElementString("World"); -} - bool BlenderSceneDelegate::supported_object(Object *object) { return object->type == OB_MESH || @@ -219,16 +213,14 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, BL::Context &b_cont) { LOG(INFO) << "Populate " << is_populated; - view3d = (View3D *)b_cont.space_data().ptr.data; b_depsgraph = &b_deps; b_context = &b_cont; + view3d = (View3D *)b_context->space_data().ptr.data; if (!is_populated) { /* Export initial objects */ update_collection(); - - World *world = (World *)b_depsgraph->scene().world().ptr.data; - add_update_world(world); + update_world(); is_populated = true; return; @@ -237,6 +229,7 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, BL::Context &b_cont) /* Working with updates */ bool do_update_collection = false; bool do_update_visibility = false; + bool do_update_world = false; for (auto &update : b_depsgraph->updates) { BL::ID id = update.id(); @@ -273,23 +266,21 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, BL::Context &b_cont) } if (id.is_a(&RNA_Scene)) { - World *world = (World *)b_depsgraph->scene().world().ptr.data; - add_update_world(world); if (!update.is_updated_geometry() && !update.is_updated_transform() && !update.is_updated_shading()) { do_update_visibility = true; + + Scene *scene = (Scene *)id.ptr.data; + if ((scene->world && !world_data) || (!scene->world && world_data)) { + do_update_world = true; + } } continue; } if (id.is_a(&RNA_World)) { - World *world = (World *)b_depsgraph->scene().world().ptr.data; - add_update_world(world); - continue; - } - - if (id.is_a(&RNA_ShaderNodeTree)) { - World *world = (World *)b_depsgraph->scene().world().ptr.data; - add_update_world(world); + if (update.is_updated_shading()) { + do_update_world = true; + } continue; } } @@ -300,6 +291,10 @@ void BlenderSceneDelegate::Populate(BL::Depsgraph &b_deps, BL::Context &b_cont) if (do_update_visibility) { update_visibility(); } + if (do_update_world) { + update_world(); + } + } void BlenderSceneDelegate::update_visibility() @@ -368,10 +363,14 @@ VtValue BlenderSceneDelegate::GetMaterialResource(SdfPath const& id) GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id) { - if (id == world_id()) { + ObjectData *obj_data = object_data(id); + if (obj_data) { + return obj_data->transform(); + } + if (id == WorldData::prim_id(this)) { return world_data->transform(GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName()); } - return object_data(id)->transform(); + return GfMatrix4d(); } VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken const& key) @@ -380,10 +379,8 @@ VtValue BlenderSceneDelegate::GetLightParamValue(SdfPath const& id, TfToken cons if (l_data) { return l_data->get_data(key); } - if (id == world_id()) { - if (world_data->has_data(key)) { - return world_data->get_data(key); - } + if (id == WorldData::prim_id(this)) { + return world_data->get_data(key); } return VtValue(); } diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h index 92536ea912a6..080eda40e6b6 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.h @@ -45,13 +45,12 @@ private: MeshData *mesh_data(SdfPath const &id); LightData *light_data(SdfPath const &id); MaterialData *material_data(SdfPath const &id); - SdfPath world_id(); bool supported_object(Object *object); void add_update_object(Object *object, bool geometry, bool transform, bool shading); void set_material(MeshData &mesh_data); void update_material(Material *material); - void add_update_world(World *world); + void update_world(); void update_collection(); void update_visibility(); diff --git a/source/blender/render/hydra/sceneDelegate/world.cc b/source/blender/render/hydra/sceneDelegate/world.cc index 2accd814d821..29d05e405826 100644 --- a/source/blender/render/hydra/sceneDelegate/world.cc +++ b/source/blender/render/hydra/sceneDelegate/world.cc @@ -19,31 +19,35 @@ #include "BKE_image.h" #include "NOD_shader.h" +#include "glog/logging.h" + #include "world.h" #include "../utils.h" /* TODO : add custom tftoken "transparency"? */ using namespace pxr; -using namespace std; namespace blender::render::hydra { -WorldData::WorldData() - : b_context(nullptr), - world(nullptr) +std::unique_ptr WorldData::init(World *world, bContext *context) { + return std::make_unique(world, context); } -WorldData::WorldData(World *world, bContext *b_context) - : b_context(b_context), - world(world) +SdfPath WorldData::prim_id(HdSceneDelegate *scene_delegate) { - data.clear(); + return scene_delegate->GetDelegateID().AppendElementString("World"); +} +WorldData::WorldData(World *world, bContext *context) + : IdData((ID *)world) +{ data[UsdLuxTokens->orientToStageUpAxis] = true; if (world->use_nodes) { + /* TODO: Create nodes parsing system */ + bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL); bNodeSocket input_socket = output_node->input_by_identifier("Surface"); bNodeLink const *link = input_socket.directly_linked_links()[0]; @@ -69,16 +73,16 @@ WorldData::WorldData(World *world, bContext *b_context) Image *image = (Image *)color_input_node->id; if (image) { - Main *bmain = CTX_data_main(b_context); - Scene *scene = CTX_data_scene(b_context); + Main *bmain = CTX_data_main(context); + Scene *scene = CTX_data_scene(context); ReportList reports; ImageSaveOptions opts; opts.im_format.imtype = R_IMF_IMTYPE_PNG; - string cached_image_path = cache_image(bmain, scene, image, &tex->iuser, &opts, &reports); - if (!cached_image_path.empty()) { - data[HdLightTokens->textureFile] = SdfAssetPath(cached_image_path, cached_image_path); + std::string image_path = cache_image(bmain, scene, image, &tex->iuser, &opts, &reports); + if (!image_path.empty()) { + data[HdLightTokens->textureFile] = SdfAssetPath(image_path, image_path); } } } @@ -91,35 +95,55 @@ WorldData::WorldData(World *world, bContext *b_context) } } -GfMatrix4d WorldData::transform(string const &renderer_name) +GfMatrix4d WorldData::transform(std::string const &renderer_name) { - GfMatrix4d transform = GfMatrix4d().SetIdentity(); + GfMatrix4d transform = GfMatrix4d(GfRotation(GfVec3d(1.0, 0.0, 0.0), -90), GfVec3d()); - if (has_data(UsdLuxTokens->orientToStageUpAxis)) { - transform *= GfMatrix4d(GfRotation(GfVec3d(1.0, 0.0, 0.0), -90), GfVec3d()); - } /* TODO : do this check via RenderSettings*/ if (renderer_name == "RPR") { transform *= GfMatrix4d(GfRotation(GfVec3d(1.0, 0.0, 0.0), -180), GfVec3d()); transform *= GfMatrix4d(GfRotation(GfVec3d(0.0, 0.0, 1.0), 90.0), GfVec3d()); } - return transform; } -VtValue &WorldData::get_data(TfToken const &key) +VtValue WorldData::get_data(TfToken const &key) { - return data[key]; + VtValue ret; + auto it = data.find(key); + if (it != data.end()) { + ret = it->second; + } + return ret; } -bool WorldData::has_data(TfToken const &key) +void WorldData::insert_prim(HdSceneDelegate *scene_delegate) { - return data.find(key) != data.end(); + SdfPath p_id = prim_id(scene_delegate); + scene_delegate->GetRenderIndex().InsertSprim(HdPrimTypeTokens->domeLight, scene_delegate, p_id); + LOG(INFO) << "Add World: id=" << p_id.GetAsString(); } -bool WorldData::is_visible() +void WorldData::remove_prim(HdSceneDelegate *scene_delegate) { - return true; + SdfPath p_id = prim_id(scene_delegate); + scene_delegate->GetRenderIndex().RemoveSprim(HdPrimTypeTokens->domeLight, p_id); + LOG(INFO) << "Remove World"; +} + +void WorldData::mark_prim_dirty(HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +{ + HdDirtyBits bits = HdLight::Clean; + switch (dirty_bits) { + case DirtyBits::AllDirty: + bits = HdLight::AllDirty; + break; + default: + break; + } + SdfPath p_id = prim_id(scene_delegate); + scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); + LOG(INFO) << "Update World"; } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/sceneDelegate/world.h b/source/blender/render/hydra/sceneDelegate/world.h index b61d5b4824a0..bc16ad4fad81 100644 --- a/source/blender/render/hydra/sceneDelegate/world.h +++ b/source/blender/render/hydra/sceneDelegate/world.h @@ -14,33 +14,26 @@ #include "DNA_view3d_types.h" #include "DNA_world_types.h" +#include "id.h" + namespace blender::render::hydra { -class WorldData { +class WorldData: public IdData { public: - WorldData(); - WorldData(World *world, bContext *b_context); + static std::unique_ptr init(World *world, bContext *context); + static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate); + + WorldData(World *world, bContext *context); - pxr::TfToken prim_type(); pxr::GfMatrix4d transform(std::string const &renderer_name); - pxr::VtValue &get_data(pxr::TfToken const &key); - template - const T &get_data(pxr::TfToken const &key); - bool has_data(pxr::TfToken const &key); - bool is_visible(); + pxr::VtValue get_data(pxr::TfToken const &key) override; + void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; + void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; + void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; - bContext *b_context; - World *world; - private: std::map data; }; -template -const T &WorldData::get_data(pxr::TfToken const &key) -{ - return get_data(key).Get(); -} - } // namespace blender::render::hydra -- 2.30.2 From 03666f871ac2152c2ddc666997bd0396ec2bea54 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sat, 18 Feb 2023 08:08:47 +0200 Subject: [PATCH 12/13] Moved scene_delegate into IdData field --- .../sceneDelegate/blenderSceneDelegate.cc | 36 +++++++++---------- .../blender/render/hydra/sceneDelegate/id.cc | 5 +-- .../blender/render/hydra/sceneDelegate/id.h | 9 ++--- .../render/hydra/sceneDelegate/light.cc | 10 +++--- .../render/hydra/sceneDelegate/light.h | 8 ++--- .../render/hydra/sceneDelegate/material.cc | 14 ++++---- .../render/hydra/sceneDelegate/material.h | 12 ++++--- .../render/hydra/sceneDelegate/mesh.cc | 10 +++--- .../blender/render/hydra/sceneDelegate/mesh.h | 8 ++--- .../render/hydra/sceneDelegate/object.cc | 10 +++--- .../render/hydra/sceneDelegate/object.h | 4 +-- .../render/hydra/sceneDelegate/world.cc | 20 ++++++----- .../render/hydra/sceneDelegate/world.h | 12 +++---- 13 files changed, 82 insertions(+), 76 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc index d6cd0f31a234..12a3dabab13d 100644 --- a/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc +++ b/source/blender/render/hydra/sceneDelegate/blenderSceneDelegate.cc @@ -35,10 +35,10 @@ void BlenderSceneDelegate::set_material(MeshData &mesh_data) SdfPath id = MaterialData::prim_id(this, material); MaterialData *mat_data = material_data(id); if (!mat_data) { - materials[id] = MaterialData::init(material); + materials[id] = MaterialData::init(this, material); mat_data = material_data(id); mat_data->export_mtlx(); - mat_data->insert_prim(this); + mat_data->insert_prim(); } mesh_data.material_id = id; } @@ -48,7 +48,7 @@ void BlenderSceneDelegate::update_material(Material *material) MaterialData *mat_data = material_data(MaterialData::prim_id(this, material)); if (mat_data) { mat_data->export_mtlx(); - mat_data->mark_prim_dirty(this, IdData::DirtyBits::AllDirty); + mat_data->mark_prim_dirty(IdData::DirtyBits::AllDirty); } } @@ -57,17 +57,17 @@ void BlenderSceneDelegate::update_world() World *world = (World *)b_depsgraph->scene().world().ptr.data; if (!world_data) { if (world) { - world_data = WorldData::init(world, (bContext *)b_context->ptr.data); - world_data->insert_prim(this); + world_data = WorldData::init(this, world, (bContext *)b_context->ptr.data); + world_data->insert_prim(); } } else { if (world) { - world_data = WorldData::init(world, (bContext *)b_context->ptr.data); - world_data->mark_prim_dirty(this, IdData::DirtyBits::AllDirty); + world_data = WorldData::init(this, world, (bContext *)b_context->ptr.data); + world_data->mark_prim_dirty(IdData::DirtyBits::AllDirty); } else { - world_data->remove_prim(this); + world_data->remove_prim(); world_data = nullptr; } } @@ -110,7 +110,7 @@ void BlenderSceneDelegate::update_collection() if (available_objects.find(it->first) != available_objects.end()) { continue; } - it->second->remove_prim(this); + it->second->remove_prim(); objects.erase(it); it = objects.begin(); } @@ -127,7 +127,7 @@ void BlenderSceneDelegate::update_collection() if (available_materials.find(it->first) != available_materials.end()) { continue; } - it->second->remove_prim(this); + it->second->remove_prim(); materials.erase(it); it = materials.begin(); } @@ -138,10 +138,10 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool SdfPath id = ObjectData::prim_id(this, object); ObjectData *obj_data = object_data(id); if (!obj_data) { - objects[id] = ObjectData::init(object); + objects[id] = ObjectData::init(this, object); obj_data = object_data(id); obj_data->update_visibility(view3d); - obj_data->insert_prim(this); + obj_data->insert_prim(); MeshData *m_data = dynamic_cast(obj_data); if (m_data) { set_material(*m_data); @@ -150,23 +150,23 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool } if (geometry) { - objects[id] = ObjectData::init(object); + objects[id] = ObjectData::init(this, object); obj_data = object_data(id); obj_data->update_visibility(view3d); MeshData *m_data = dynamic_cast(obj_data); if (m_data) { set_material(*m_data); } - obj_data->mark_prim_dirty(this, IdData::DirtyBits::AllDirty); + obj_data->mark_prim_dirty(IdData::DirtyBits::AllDirty); return; } if (transform) { - obj_data->mark_prim_dirty(this, IdData::DirtyBits::DirtyTransform); + obj_data->mark_prim_dirty(IdData::DirtyBits::DirtyTransform); } if (shading) { - obj_data->mark_prim_dirty(this, IdData::DirtyBits::DirtyMaterial); + obj_data->mark_prim_dirty(IdData::DirtyBits::DirtyMaterial); } } @@ -302,7 +302,7 @@ void BlenderSceneDelegate::update_visibility() /* Check and update visibility */ for (auto &obj : objects) { if (obj.second->update_visibility(view3d)) { - obj.second->mark_prim_dirty(this, IdData::DirtyBits::DirtyVisibility); + obj.second->mark_prim_dirty(IdData::DirtyBits::DirtyVisibility); }; } @@ -368,7 +368,7 @@ GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id) return obj_data->transform(); } if (id == WorldData::prim_id(this)) { - return world_data->transform(GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName()); + return world_data->transform(); } return GfMatrix4d(); } diff --git a/source/blender/render/hydra/sceneDelegate/id.cc b/source/blender/render/hydra/sceneDelegate/id.cc index 1abaf761d56a..54852c34617e 100644 --- a/source/blender/render/hydra/sceneDelegate/id.cc +++ b/source/blender/render/hydra/sceneDelegate/id.cc @@ -9,8 +9,9 @@ using namespace pxr; namespace blender::render::hydra { -IdData::IdData(ID *id) - : id(id) +IdData::IdData(pxr::HdSceneDelegate *scene_delegate, ID *id) + : scene_delegate(scene_delegate) + , id(id) { } diff --git a/source/blender/render/hydra/sceneDelegate/id.h b/source/blender/render/hydra/sceneDelegate/id.h index dbfe10e8a66f..ecd8fe14bcf7 100644 --- a/source/blender/render/hydra/sceneDelegate/id.h +++ b/source/blender/render/hydra/sceneDelegate/id.h @@ -13,7 +13,7 @@ namespace blender::render::hydra { class IdData { public: - IdData(ID *id); + IdData(pxr::HdSceneDelegate *scene_delegate, ID *id); virtual ~IdData() = default; std::string name(); @@ -27,11 +27,12 @@ class IdData { AllDirty }; - virtual void insert_prim(pxr::HdSceneDelegate *scene_delegate) = 0; - virtual void remove_prim(pxr::HdSceneDelegate *scene_delegate) = 0; - virtual void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) = 0; + virtual void insert_prim() = 0; + virtual void remove_prim() = 0; + virtual void mark_prim_dirty(DirtyBits dirty_bits) = 0; protected: + pxr::HdSceneDelegate *scene_delegate; ID *id; }; diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index 6ddca28da4f7..37716de2f41c 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -18,8 +18,8 @@ using namespace pxr; namespace blender::render::hydra { -LightData::LightData(Object *object) - : ObjectData(object) +LightData::LightData(pxr::HdSceneDelegate *scene_delegate, Object *object) + : ObjectData(scene_delegate, object) { Light *light = (Light *)((Object *)id)->data; @@ -126,21 +126,21 @@ VtValue LightData::get_data(TfToken const &key) return ret; } -void LightData::insert_prim(pxr::HdSceneDelegate *scene_delegate) +void LightData::insert_prim() { SdfPath p_id = prim_id(scene_delegate, (Object *)id); scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); LOG(INFO) << "Add light: " << name() << " id=" << p_id.GetAsString(); } -void LightData::remove_prim(pxr::HdSceneDelegate *scene_delegate) +void LightData::remove_prim() { SdfPath p_id = prim_id(scene_delegate, (Object *)id); scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); LOG(INFO) << "Remove light: " << name(); } -void LightData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +void LightData::mark_prim_dirty(DirtyBits dirty_bits) { HdDirtyBits bits = HdLight::Clean; switch (dirty_bits) { diff --git a/source/blender/render/hydra/sceneDelegate/light.h b/source/blender/render/hydra/sceneDelegate/light.h index 28dce7f078c9..794a0deee730 100644 --- a/source/blender/render/hydra/sceneDelegate/light.h +++ b/source/blender/render/hydra/sceneDelegate/light.h @@ -13,12 +13,12 @@ namespace blender::render::hydra { class LightData: public ObjectData { public: - LightData(Object *object); + LightData(pxr::HdSceneDelegate *scene_delegate, Object *object); pxr::VtValue get_data(pxr::TfToken const &key) override; - void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; - void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; - void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; + void insert_prim() override; + void remove_prim() override; + void mark_prim_dirty(DirtyBits dirty_bits) override; private: std::map data; diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index 91885ee00f6e..b2f05fcee69c 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -17,9 +17,9 @@ using namespace pxr; namespace blender::render::hydra { -std::unique_ptr MaterialData::init(Material *material) +std::unique_ptr MaterialData::init(pxr::HdSceneDelegate *scene_delegate, Material *material) { - return std::make_unique(material); + return std::make_unique(scene_delegate, material); } pxr::SdfPath MaterialData::prim_id(pxr::HdSceneDelegate *scene_delegate, Material *material) @@ -31,8 +31,8 @@ pxr::SdfPath MaterialData::prim_id(pxr::HdSceneDelegate *scene_delegate, Materia return scene_delegate->GetDelegateID().AppendElementString(str); } -MaterialData::MaterialData(Material *material) - : IdData((ID *)material) +MaterialData::MaterialData(pxr::HdSceneDelegate *scene_delegate, Material *material) + : IdData(scene_delegate, (ID *)material) { } @@ -76,7 +76,7 @@ void MaterialData::export_mtlx() mtlx_path = SdfAssetPath(path, path); } -void MaterialData::insert_prim(pxr::HdSceneDelegate *scene_delegate) +void MaterialData::insert_prim() { SdfPath p_id = prim_id(scene_delegate, (Material *)id); scene_delegate->GetRenderIndex().InsertSprim(HdPrimTypeTokens->material, scene_delegate, p_id); @@ -84,14 +84,14 @@ void MaterialData::insert_prim(pxr::HdSceneDelegate *scene_delegate) << ", mtlx=" << mtlx_path.GetResolvedPath(); } -void MaterialData::remove_prim(pxr::HdSceneDelegate *scene_delegate) +void MaterialData::remove_prim() { SdfPath p_id = prim_id(scene_delegate, (Material *)id); scene_delegate->GetRenderIndex().RemoveSprim(HdPrimTypeTokens->material, p_id); LOG(INFO) << "Remove material: " << name(); } -void MaterialData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +void MaterialData::mark_prim_dirty(DirtyBits dirty_bits) { HdDirtyBits bits = HdMaterial::Clean; switch (dirty_bits) { diff --git a/source/blender/render/hydra/sceneDelegate/material.h b/source/blender/render/hydra/sceneDelegate/material.h index 4c6366750a78..e0ea0c129b54 100644 --- a/source/blender/render/hydra/sceneDelegate/material.h +++ b/source/blender/render/hydra/sceneDelegate/material.h @@ -18,18 +18,20 @@ using MaterialDataMap = pxr::TfHashMap init(Material *material); + static std::unique_ptr init(pxr::HdSceneDelegate *scene_delegate, Material *material); static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, Material *material); - MaterialData(Material *material); + MaterialData(pxr::HdSceneDelegate *scene_delegate, Material *material); pxr::VtValue get_data(pxr::TfToken const &key) override; - void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; - void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; - void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; + void insert_prim() override; + void remove_prim() override; + void mark_prim_dirty(DirtyBits dirty_bits) override; pxr::VtValue material_resource(); void export_mtlx(); + +private: pxr::SdfAssetPath mtlx_path; }; diff --git a/source/blender/render/hydra/sceneDelegate/mesh.cc b/source/blender/render/hydra/sceneDelegate/mesh.cc index e3e63a2d3aa0..cec931983feb 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.cc +++ b/source/blender/render/hydra/sceneDelegate/mesh.cc @@ -18,8 +18,8 @@ using namespace pxr; namespace blender::render::hydra { -MeshData::MeshData(Object *object) - : ObjectData(object) +MeshData::MeshData(pxr::HdSceneDelegate *scene_delegate, Object *object) + : ObjectData(scene_delegate, object) { if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && BLI_listbase_is_empty(&object->modifiers)) { @@ -134,7 +134,7 @@ void MeshData::set_mesh(Mesh *mesh) } } -void MeshData::insert_prim(pxr::HdSceneDelegate *scene_delegate) +void MeshData::insert_prim() { if (face_vertex_counts.empty()) { return; @@ -145,7 +145,7 @@ void MeshData::insert_prim(pxr::HdSceneDelegate *scene_delegate) LOG(INFO) << "Add mesh: " << name() << " id=" << p_id.GetAsString(); } -void MeshData::remove_prim(pxr::HdSceneDelegate *scene_delegate) +void MeshData::remove_prim() { SdfPath p_id = prim_id(scene_delegate, (Object *)id); if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { @@ -156,7 +156,7 @@ void MeshData::remove_prim(pxr::HdSceneDelegate *scene_delegate) LOG(INFO) << "Remove mesh: " << name(); } -void MeshData::mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +void MeshData::mark_prim_dirty(DirtyBits dirty_bits) { SdfPath p_id = prim_id(scene_delegate, (Object *)id); if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { diff --git a/source/blender/render/hydra/sceneDelegate/mesh.h b/source/blender/render/hydra/sceneDelegate/mesh.h index bedfd3d2f63e..194ed2879cd6 100644 --- a/source/blender/render/hydra/sceneDelegate/mesh.h +++ b/source/blender/render/hydra/sceneDelegate/mesh.h @@ -12,13 +12,13 @@ namespace blender::render::hydra { class MeshData: public ObjectData { public: - MeshData(Object *object); + MeshData(pxr::HdSceneDelegate *scene_delegate, Object *object); pxr::VtValue get_data(pxr::TfToken const &key) override; - void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; - void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; - void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; + void insert_prim() override; + void remove_prim() override; + void mark_prim_dirty(DirtyBits dirty_bits) override; Material *material(); pxr::HdMeshTopology mesh_topology(); diff --git a/source/blender/render/hydra/sceneDelegate/object.cc b/source/blender/render/hydra/sceneDelegate/object.cc index b48187e9aee0..7bb7912b4cc3 100644 --- a/source/blender/render/hydra/sceneDelegate/object.cc +++ b/source/blender/render/hydra/sceneDelegate/object.cc @@ -12,7 +12,7 @@ using namespace pxr; namespace blender::render::hydra { -std::unique_ptr ObjectData::init(Object *object) +std::unique_ptr ObjectData::init(pxr::HdSceneDelegate *scene_delegate, Object *object) { switch (object->type) { case OB_MESH: @@ -21,10 +21,10 @@ std::unique_ptr ObjectData::init(Object *object) case OB_CURVES: case OB_CURVES_LEGACY: case OB_MBALL: - return std::make_unique(object); + return std::make_unique(scene_delegate, object); case OB_LAMP: - return std::make_unique(object); + return std::make_unique(scene_delegate, object); default: break; @@ -41,8 +41,8 @@ pxr::SdfPath ObjectData::prim_id(pxr::HdSceneDelegate *scene_delegate, Object *o return scene_delegate->GetDelegateID().AppendElementString(str); } -ObjectData::ObjectData(Object *object) - : IdData((ID *)object) +ObjectData::ObjectData(pxr::HdSceneDelegate *scene_delegate, Object *object) + : IdData(scene_delegate, (ID *)object) , visible(true) { } diff --git a/source/blender/render/hydra/sceneDelegate/object.h b/source/blender/render/hydra/sceneDelegate/object.h index 3b18668c65e8..dab439d4559b 100644 --- a/source/blender/render/hydra/sceneDelegate/object.h +++ b/source/blender/render/hydra/sceneDelegate/object.h @@ -19,10 +19,10 @@ using ObjectDataMap = pxr::TfHashMap, class ObjectData: public IdData { public: - static std::unique_ptr init(Object *object); + static std::unique_ptr init(pxr::HdSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, Object *object); - ObjectData(Object *object); + ObjectData(pxr::HdSceneDelegate *scene_delegate, Object *object); int type(); pxr::GfMatrix4d transform(); diff --git a/source/blender/render/hydra/sceneDelegate/world.cc b/source/blender/render/hydra/sceneDelegate/world.cc index 29d05e405826..290eca4f5568 100644 --- a/source/blender/render/hydra/sceneDelegate/world.cc +++ b/source/blender/render/hydra/sceneDelegate/world.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "BKE_context.h" @@ -30,9 +31,10 @@ using namespace pxr; namespace blender::render::hydra { -std::unique_ptr WorldData::init(World *world, bContext *context) +std::unique_ptr WorldData::init(pxr::HdSceneDelegate *scene_delegate, + World *world, bContext *context) { - return std::make_unique(world, context); + return std::make_unique(scene_delegate, world, context); } SdfPath WorldData::prim_id(HdSceneDelegate *scene_delegate) @@ -40,8 +42,8 @@ SdfPath WorldData::prim_id(HdSceneDelegate *scene_delegate) return scene_delegate->GetDelegateID().AppendElementString("World"); } -WorldData::WorldData(World *world, bContext *context) - : IdData((ID *)world) +WorldData::WorldData(pxr::HdSceneDelegate *scene_delegate, World *world, bContext *context) + : IdData(scene_delegate, (ID *)world) { data[UsdLuxTokens->orientToStageUpAxis] = true; @@ -95,12 +97,12 @@ WorldData::WorldData(World *world, bContext *context) } } -GfMatrix4d WorldData::transform(std::string const &renderer_name) +GfMatrix4d WorldData::transform() { GfMatrix4d transform = GfMatrix4d(GfRotation(GfVec3d(1.0, 0.0, 0.0), -90), GfVec3d()); /* TODO : do this check via RenderSettings*/ - if (renderer_name == "RPR") { + if (scene_delegate->GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName() == "RPR") { transform *= GfMatrix4d(GfRotation(GfVec3d(1.0, 0.0, 0.0), -180), GfVec3d()); transform *= GfMatrix4d(GfRotation(GfVec3d(0.0, 0.0, 1.0), 90.0), GfVec3d()); } @@ -117,21 +119,21 @@ VtValue WorldData::get_data(TfToken const &key) return ret; } -void WorldData::insert_prim(HdSceneDelegate *scene_delegate) +void WorldData::insert_prim() { SdfPath p_id = prim_id(scene_delegate); scene_delegate->GetRenderIndex().InsertSprim(HdPrimTypeTokens->domeLight, scene_delegate, p_id); LOG(INFO) << "Add World: id=" << p_id.GetAsString(); } -void WorldData::remove_prim(HdSceneDelegate *scene_delegate) +void WorldData::remove_prim() { SdfPath p_id = prim_id(scene_delegate); scene_delegate->GetRenderIndex().RemoveSprim(HdPrimTypeTokens->domeLight, p_id); LOG(INFO) << "Remove World"; } -void WorldData::mark_prim_dirty(HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) +void WorldData::mark_prim_dirty(DirtyBits dirty_bits) { HdDirtyBits bits = HdLight::Clean; switch (dirty_bits) { diff --git a/source/blender/render/hydra/sceneDelegate/world.h b/source/blender/render/hydra/sceneDelegate/world.h index bc16ad4fad81..04a08b29ed09 100644 --- a/source/blender/render/hydra/sceneDelegate/world.h +++ b/source/blender/render/hydra/sceneDelegate/world.h @@ -20,17 +20,17 @@ namespace blender::render::hydra { class WorldData: public IdData { public: - static std::unique_ptr init(World *world, bContext *context); + static std::unique_ptr init(pxr::HdSceneDelegate *scene_delegate, World *world, bContext *context); static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate); - WorldData(World *world, bContext *context); + WorldData(pxr::HdSceneDelegate *scene_delegate, World *world, bContext *context); - pxr::GfMatrix4d transform(std::string const &renderer_name); + pxr::GfMatrix4d transform(); pxr::VtValue get_data(pxr::TfToken const &key) override; - void insert_prim(pxr::HdSceneDelegate *scene_delegate) override; - void remove_prim(pxr::HdSceneDelegate *scene_delegate) override; - void mark_prim_dirty(pxr::HdSceneDelegate *scene_delegate, DirtyBits dirty_bits) override; + void insert_prim() override; + void remove_prim() override; + void mark_prim_dirty(DirtyBits dirty_bits) override; private: std::map data; -- 2.30.2 From f6d144a93804368ed545fea882ef8c44344a4701 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sat, 18 Feb 2023 08:47:31 +0200 Subject: [PATCH 13/13] Improved logging. BLEN-344: Fixed area light visibility. --- source/blender/render/hydra/sceneDelegate/light.cc | 13 +++++++++++-- .../blender/render/hydra/sceneDelegate/material.cc | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/source/blender/render/hydra/sceneDelegate/light.cc b/source/blender/render/hydra/sceneDelegate/light.cc index 37716de2f41c..f3cddffb438b 100644 --- a/source/blender/render/hydra/sceneDelegate/light.cc +++ b/source/blender/render/hydra/sceneDelegate/light.cc @@ -15,6 +15,7 @@ #include "light.h" using namespace pxr; +using namespace boost::algorithm; namespace blender::render::hydra { @@ -120,8 +121,16 @@ VtValue LightData::get_data(TfToken const &key) if (it != data.end()) { ret = it->second; } - else if (boost::algorithm::ends_with(key.GetString(), "visibility:camera")) { - ret = false; + else { + std::string n = key.GetString(); + if (contains(n, "object:visibility:")) { + if (ends_with(n, "camera") || ends_with(n, "shadow")) { + ret = false; + } + else { + ret = true; + } + } } return ret; } diff --git a/source/blender/render/hydra/sceneDelegate/material.cc b/source/blender/render/hydra/sceneDelegate/material.cc index b2f05fcee69c..492157bfa5cd 100644 --- a/source/blender/render/hydra/sceneDelegate/material.cc +++ b/source/blender/render/hydra/sceneDelegate/material.cc @@ -74,14 +74,14 @@ void MaterialData::export_mtlx() PyGILState_Release(gstate); mtlx_path = SdfAssetPath(path, path); + LOG(INFO) << "Material export: " << name() << " mtlx=" << mtlx_path.GetResolvedPath(); } void MaterialData::insert_prim() { SdfPath p_id = prim_id(scene_delegate, (Material *)id); scene_delegate->GetRenderIndex().InsertSprim(HdPrimTypeTokens->material, scene_delegate, p_id); - LOG(INFO) << "Add material: " << name() << " id=" << p_id.GetAsString() - << ", mtlx=" << mtlx_path.GetResolvedPath(); + LOG(INFO) << "Add material: " << name() << " id=" << p_id.GetAsString(); } void MaterialData::remove_prim() -- 2.30.2