From 7c64910a27c3ef445df694147b8b18ba1ca8003a Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Thu, 4 May 2023 00:48:35 +0300 Subject: [PATCH 1/4] recreate PreviewEngine if render_delegate_name changed --- source/blender/render/hydra/preview_engine.cc | 18 ++++++++++++------ source/blender/render/hydra/preview_engine.h | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 12b6cdaccb38..976d24c2a677 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -1,6 +1,9 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ +#include +#include + #include "DEG_depsgraph_query.h" #include "BLI_timer.h" @@ -14,16 +17,16 @@ const double LIFETIME = 180.0; std::unique_ptr PreviewEngine::instance_; PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_id) + const std::string &render_delegate_name) { if (!instance_) { - instance_ = std::make_unique(bl_engine, render_delegate_id); + instance_ = std::make_unique(bl_engine, render_delegate_name); } if (BLI_timer_is_registered((uintptr_t)instance_.get())) { /* Unregister timer while PreviewEngine is working */ BLI_timer_unregister((uintptr_t)instance_.get()); } - instance_->update(bl_engine, render_delegate_id); + instance_->update(bl_engine, render_delegate_name); return instance_.get(); } @@ -96,10 +99,13 @@ double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) return -1; } -void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_delegate_id) +void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_delegate_name) { - this->bl_engine_ = bl_engine; - /* TODO: recreate render_delegate when render_delegate_id is changed */ + bl_engine_ = bl_engine; + if (render_delegate_name != render_delegate_name_) { + render_delegate_->Stop(); + instance_.reset(new PreviewEngine(bl_engine, render_delegate_name)); + } } void PreviewEngine::update_render_result(std::vector &pixels) diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index af613c939fab..65ab19dc455d 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -12,7 +12,7 @@ class PreviewEngine : public FinalEngine { using FinalEngine::FinalEngine; static PreviewEngine *get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_id); + const std::string &render_delegate_name); static void schedule_free(); void sync(Depsgraph *depsgraph, @@ -25,7 +25,7 @@ class PreviewEngine : public FinalEngine { static double free_instance(uintptr_t uuid, void *user_data); static std::unique_ptr instance_; - void update(RenderEngine *bl_engine, const std::string &render_delegate_id); + void update(RenderEngine *bl_engine, const std::string &render_delegate_name); void update_render_result(std::vector &pixels); }; -- 2.30.2 From b69d13c2c470617c8a0a53712c925bb23ed3f4ae Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Thu, 4 May 2023 11:27:57 +0300 Subject: [PATCH 2/4] removed includes --- source/blender/render/hydra/preview_engine.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 976d24c2a677..3b893f6937e1 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -1,9 +1,6 @@ /* SPDX-License-Identifier: Apache-2.0 * Copyright 2011-2022 Blender Foundation */ -#include -#include - #include "DEG_depsgraph_query.h" #include "BLI_timer.h" -- 2.30.2 From 4da45f9f53034648cb34581f100ce68465e05833 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Fri, 5 May 2023 14:04:24 +0300 Subject: [PATCH 3/4] added deletion of pointers --- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 5 +++++ 1 file changed, 5 insertions(+) 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 b1a67394e5e7..3d6b70cb5356 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -187,6 +187,11 @@ void BlenderSceneDelegate::clear() objects_.clear(); instancers_.clear(); materials_.clear(); + + depsgraph = nullptr; + context = nullptr; + scene = nullptr; + view3d = nullptr; } pxr::SdfPath BlenderSceneDelegate::prim_id(ID *id, const char *prefix) const -- 2.30.2 From 1fa080660b08e376bf87653ea0d8f4efc553491f Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 5 May 2023 14:32:06 +0300 Subject: [PATCH 4/4] Added comment and tiny code improvements --- source/blender/render/hydra/engine.cc | 2 +- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 4 +++- .../render/hydra/scene_delegate/blender_scene_delegate.h | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index 72d2dfebbd23..81476b694623 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -25,7 +25,7 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); render_delegate_ = registry.CreateRenderDelegate(pxr::TfToken(render_delegate_name_)); - /* Current USD (23.02) has limited support for Vulkan. To make it works USD should be built + /* USD has limited support for Vulkan. To make it works USD should be built * with PXR_ENABLE_VULKAN_SUPPORT=TRUE which is not possible now */ if (GPU_backend_get_type() == GPU_BACKEND_VULKAN) { BLI_setenv("HGI_ENABLE_VULKAN", "1"); 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 aaa802806310..a637d1ba82e7 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -15,7 +15,7 @@ CLG_LOGREF_DECLARE_GLOBAL(LOG_RENDER_HYDRA_SCENE, "render.hydra.scene"); BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id, BlenderSceneDelegate::EngineType engine_type, - std::string render_delegate_name) + const std::string &render_delegate_name) : HdSceneDelegate(parent_index, delegate_id), engine_type(engine_type), render_delegate_name(render_delegate_name) @@ -258,6 +258,8 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool { pxr::SdfPath p_id; if (child_id) { + /* Getting instancer path id from child Mesh instance (consist with 3 path elements) and + * Light instance (consist with 4 path elements) */ int n = id.GetPathElementCount(); if (n == 3) { p_id = id.GetParentPath(); 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 73cd5f1f1111..1ad18348564e 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -30,7 +30,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id, BlenderSceneDelegate::EngineType engine_type, - std::string render_delegate_name); + const std::string &render_delegate_name); ~BlenderSceneDelegate() override = default; /* Delegate methods */ -- 2.30.2