From d0bf1a96188df5abfd98fdf505ab5fa3a5cd8822 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 5 May 2023 18:17:35 +0300 Subject: [PATCH 1/2] Implement GetDoubleSided and GetCullStyle for BlenderSceneDelegate. Initial implementation --- .../hydra/scene_delegate/blender_scene_delegate.cc | 12 ++++++++++++ .../hydra/scene_delegate/blender_scene_delegate.h | 2 ++ .../blender/render/hydra/scene_delegate/material.cc | 6 +++++- .../blender/render/hydra/scene_delegate/material.h | 2 ++ source/blender/render/hydra/scene_delegate/mesh.cc | 10 +++++++++- source/blender/render/hydra/scene_delegate/mesh.h | 3 +++ 6 files changed, 33 insertions(+), 2 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc index a637d1ba82e7..cf5b81e26984 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -122,6 +122,18 @@ bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id) return object_data(id)->visible; } +bool BlenderSceneDelegate::GetDoubleSided(pxr::SdfPath const &id) +{ + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", id.GetText()); + return mesh_data(id)->double_sided(); +} + +pxr::HdCullStyle BlenderSceneDelegate::GetCullStyle(pxr::SdfPath const &id) +{ + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", id.GetText()); + return mesh_data(id)->cull_style; +} + pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id) { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", prim_id.GetText()); diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h index 1ad18348564e..666a0fbc7d34 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -43,6 +43,8 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { pxr::SdfPath GetMaterialId(pxr::SdfPath const &rprim_id) override; pxr::VtValue GetMaterialResource(pxr::SdfPath const &material_id) override; bool GetVisible(pxr::SdfPath const &id) override; + bool GetDoubleSided(pxr::SdfPath const &id) override; + pxr::HdCullStyle GetCullStyle(pxr::SdfPath const &id) override; pxr::SdfPath GetInstancerId(pxr::SdfPath const &prim_id) override; pxr::SdfPathVector GetInstancerPrototypes(pxr::SdfPath const &instancer_id) override; pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancer_id, diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 9033c25cfea5..2acdcbc7b95f 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -30,7 +30,7 @@ MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, void MaterialData::init() { ID_LOG(2, ""); - + double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0; material_network_map_ = pxr::VtValue(); /* Call of python function hydra.export_mtlx() */ @@ -120,9 +120,13 @@ void MaterialData::remove() void MaterialData::update() { ID_LOG(2, ""); + bool prev_double_sided = double_sided; init(); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, pxr::HdMaterial::AllDirty); + if (prev_double_sided != double_sided) { + + } } pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 985e95acb786..f70a7ef11c5f 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -27,6 +27,8 @@ class MaterialData : public IdData { pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_material_resource() const; + bool double_sided = true; + private: pxr::SdfAssetPath mtlx_path_; pxr::VtValue material_network_map_; diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index be5636d08888..c4445cc8aae3 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -74,7 +74,7 @@ void MeshData::update() else { if (id->recalc & ID_RECALC_SHADING) { write_material(); - bits |= pxr::HdChangeTracker::DirtyMaterialId; + bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; } if (id->recalc & ID_RECALC_TRANSFORM) { write_transform(); @@ -164,6 +164,14 @@ pxr::SdfPath MeshData::material_id() const return mat_data_->prim_id; } +bool MeshData::double_sided() const +{ + if (mat_data_) { + return mat_data_->double_sided; + } + return true; +} + void MeshData::write_mesh(Mesh *mesh) { face_vertex_counts_.clear(); diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index da62c1f70003..b920b253170b 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -28,6 +28,9 @@ class MeshData : public ObjectData { pxr::HdMeshTopology mesh_topology() const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::SdfPath material_id() const; + bool double_sided() const; + + pxr::HdCullStyle cull_style = pxr::HdCullStyleBackUnlessDoubleSided; private: void write_mesh(Mesh *mesh); -- 2.30.2 From a929df2082e2f05b05a1e4bf22fa6ad6ee57f78f Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 5 May 2023 18:40:53 +0300 Subject: [PATCH 2/2] Fixed mesh double_sided update when material double_sided is changed --- .../hydra/scene_delegate/blender_scene_delegate.h | 5 +++-- .../blender/render/hydra/scene_delegate/instancer.cc | 7 +++++++ source/blender/render/hydra/scene_delegate/instancer.h | 1 + source/blender/render/hydra/scene_delegate/material.cc | 10 +++++++++- source/blender/render/hydra/scene_delegate/mesh.cc | 10 ++++++++++ source/blender/render/hydra/scene_delegate/mesh.h | 1 + 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h index 666a0fbc7d34..9457b577d842 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -21,8 +21,9 @@ namespace blender::render::hydra { extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE; /* BSD - Blender Scene Delegate */ class BlenderSceneDelegate : public pxr::HdSceneDelegate { - friend ObjectData; /* has access to instances */ - friend MeshData; /* has access to materials */ + friend ObjectData; /* has access to instances */ + friend MeshData; /* has access to materials */ + friend MaterialData; /* has access to objects and instancers */ public: enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 50bc558c20fb..c4f2b3f5bfa0 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -227,6 +227,13 @@ void InstancerData::update_as_parent() prim_id, pxr::HdChangeTracker::AllDirty); } +void InstancerData::update_double_sided(MaterialData *mat_data) +{ + for (auto &it : mesh_instances_) { + it.second.data->update_double_sided(mat_data); + } +} + pxr::SdfPath InstancerData::object_prim_id(Object *object) const { /* Making id of object in form like _ */ diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index cb9410768431..d0454903abf8 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -43,6 +43,7 @@ class InstancerData : public ObjectData { void check_remove(std::set &available_objects); void available_materials(std::set &paths) const; void update_as_parent(); + void update_double_sided(MaterialData *mat_data); private: pxr::SdfPath object_prim_id(Object *object) const; diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 2acdcbc7b95f..cac62eda0be5 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -125,7 +125,15 @@ void MaterialData::update() scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, pxr::HdMaterial::AllDirty); if (prev_double_sided != double_sided) { - + for (auto &it : scene_delegate_->objects_) { + MeshData *m_data = dynamic_cast(it.second.get()); + if (m_data) { + m_data->update_double_sided(this); + } + } + for (auto &it : scene_delegate_->instancers_) { + it.second->update_double_sided(this); + } } } diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index c4445cc8aae3..0bae621a101b 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -119,6 +119,7 @@ bool MeshData::update_visibility() { bool ret = ObjectData::update_visibility(); if (ret) { + ID_LOG(2, ""); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( prim_id, pxr::HdChangeTracker::DirtyVisibility); } @@ -172,6 +173,15 @@ bool MeshData::double_sided() const return true; } +void MeshData::update_double_sided(MaterialData *mat_data) +{ + if (mat_data_ == mat_data) { + ID_LOG(2, ""); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + prim_id, pxr::HdChangeTracker::DirtyDoubleSided); + } +} + void MeshData::write_mesh(Mesh *mesh) { face_vertex_counts_.clear(); diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index b920b253170b..e9a66dd7bdcb 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -29,6 +29,7 @@ class MeshData : public ObjectData { pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::SdfPath material_id() const; bool double_sided() const; + void update_double_sided(MaterialData *mat_data); pxr::HdCullStyle cull_style = pxr::HdCullStyleBackUnlessDoubleSided; -- 2.30.2