From 185b9c908fbb5e147c1f29a51860db123cf23daa Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sat, 13 May 2023 18:22:08 +0300 Subject: [PATCH 01/10] Create possibility to provide render settings via BlenderSceneDelegate. Added settings map to Engine::Engine() --- source/blender/render/hydra/engine.cc | 4 +- source/blender/render/hydra/engine.h | 9 ++- source/blender/render/hydra/preview_engine.cc | 13 ++-- source/blender/render/hydra/preview_engine.h | 7 ++- source/blender/render/hydra/python.cc | 63 ++++++++++--------- 5 files changed, 56 insertions(+), 40 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index 81476b694623..3367c808c2d4 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -17,8 +17,8 @@ namespace blender::render::hydra { CLG_LOGREF_DECLARE_GLOBAL(LOG_RENDER_HYDRA, "render.hydra"); -Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) - : bl_engine_(bl_engine), render_delegate_name_(render_delegate_name) +Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name, SettingsMap &settings) + : bl_engine_(bl_engine), render_delegate_name_(render_delegate_name), settings_(settings) { pxr::HdRendererPluginRegistry ®istry = pxr::HdRendererPluginRegistry::GetInstance(); diff --git a/source/blender/render/hydra/engine.h b/source/blender/render/hydra/engine.h index 318db173f169..74a590f03909 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -23,14 +23,18 @@ namespace blender::render::hydra { extern struct CLG_LogRef *LOG_RENDER_HYDRA; +typedef pxr::TfHashMap SettingsMap; + class Engine { public: - Engine(RenderEngine *bl_engine, const std::string &render_delegate_name); + Engine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings); virtual ~Engine() = default; virtual void sync(Depsgraph *depsgraph, bContext *context, - pxr::HdRenderSettingsMap &render_settings) = 0; + SettingsMap &render_settings) = 0; virtual void render(Depsgraph *depsgraph) = 0; protected: @@ -38,6 +42,7 @@ class Engine { RenderEngine *bl_engine_; std::string render_delegate_name_; + SettingsMap settings_; /* The order is important due to deletion order */ pxr::HgiUniquePtr hgi_; diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 3b893f6937e1..b27b9131e674 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -14,16 +14,17 @@ const double LIFETIME = 180.0; std::unique_ptr PreviewEngine::instance_; PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_name) + const std::string &render_delegate_name, + SettingsMap &settings) { if (!instance_) { - instance_ = std::make_unique(bl_engine, render_delegate_name); + instance_ = std::make_unique(bl_engine, render_delegate_name, settings); } 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_name); + instance_->update(bl_engine, render_delegate_name, settings); return instance_.get(); } @@ -96,12 +97,14 @@ double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) return -1; } -void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_delegate_name) +void PreviewEngine::update(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings) { bl_engine_ = bl_engine; if (render_delegate_name != render_delegate_name_) { render_delegate_->Stop(); - instance_.reset(new PreviewEngine(bl_engine, render_delegate_name)); + instance_.reset(new PreviewEngine(bl_engine, render_delegate_name, settings)); } } diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 65ab19dc455d..8949b4e931f6 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -12,7 +12,8 @@ class PreviewEngine : public FinalEngine { using FinalEngine::FinalEngine; static PreviewEngine *get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_name); + const std::string &render_delegate_name, + SettingsMap &settings); static void schedule_free(); void sync(Depsgraph *depsgraph, @@ -25,7 +26,9 @@ 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_name); + void update(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings); void update_render_result(std::vector &pixels); }; diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 5f6f319f62c1..df7f5beeb177 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -96,34 +96,60 @@ static PyObject *get_render_plugins_func(PyObject * /*self*/, PyObject *args) return ret; } +static SettingsMap get_settings(PyObject *pysettings) +{ + SettingsMap settings; + PyObject *pyiter = PyObject_GetIter(pysettings); + if (pyiter) { + PyObject *pykey, *pyval; + while (pykey = PyIter_Next(pyiter)) { + pxr::TfToken key(PyUnicode_AsUTF8(pykey)); + pyval = PyDict_GetItem(pysettings, pykey); + + if (PyLong_Check(pyval)) { + settings[key] = PyLong_AsLong(pyval); + } + else if (PyFloat_Check(pyval)) { + settings[key] = PyFloat_AsDouble(pyval); + } + else if (PyUnicode_Check(pyval)) { + settings[key] = PyUnicode_AsUTF8(pyval); + } + Py_DECREF(pykey); + } + Py_DECREF(pyiter); + } + return settings; +} + static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args) { - PyObject *pyengine; + PyObject *pyengine, *pysettings; char *engine_type, *render_delegate_id; - if (!PyArg_ParseTuple(args, "Oss", &pyengine, &engine_type, &render_delegate_id)) { + if (!PyArg_ParseTuple(args, "OssO", &pyengine, &engine_type, &render_delegate_id, &pysettings)) { Py_RETURN_NONE; } RenderEngine *bl_engine = (RenderEngine *)PyLong_AsVoidPtr(pyengine); + SettingsMap settings = get_settings(pysettings); Engine *engine; if (STREQ(engine_type, "VIEWPORT")) { - engine = new ViewportEngine(bl_engine, render_delegate_id); + engine = new ViewportEngine(bl_engine, render_delegate_id, settings); } else if (STREQ(engine_type, "PREVIEW")) { - engine = PreviewEngine::get_instance(bl_engine, render_delegate_id); + engine = PreviewEngine::get_instance(bl_engine, render_delegate_id, settings); } else { if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { - engine = new FinalEngineGL(bl_engine, render_delegate_id); + engine = new FinalEngineGL(bl_engine, render_delegate_id, settings); } else { - engine = new FinalEngine(bl_engine, render_delegate_id); + engine = new FinalEngine(bl_engine, render_delegate_id, settings); } } CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx %s", engine, engine_type); - return PyLong_FromVoidPtr(engine); } @@ -157,28 +183,7 @@ static PyObject *engine_sync_func(PyObject * /*self*/, PyObject *args) Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); Depsgraph *depsgraph = (Depsgraph *)PyLong_AsVoidPtr(pydepsgraph); bContext *context = (bContext *)PyLong_AsVoidPtr(pycontext); - - pxr::HdRenderSettingsMap settings; - PyObject *pyiter = PyObject_GetIter(pysettings); - if (pyiter) { - PyObject *pykey, *pyval; - while (pykey = PyIter_Next(pyiter)) { - pxr::TfToken key(PyUnicode_AsUTF8(pykey)); - pyval = PyDict_GetItem(pysettings, pykey); - - if (PyLong_Check(pyval)) { - settings[key] = PyLong_AsLong(pyval); - } - else if (PyFloat_Check(pyval)) { - settings[key] = PyFloat_AsDouble(pyval); - } - else if (PyUnicode_Check(pyval)) { - settings[key] = PyUnicode_AsUTF8(pyval); - } - Py_DECREF(pykey); - } - Py_DECREF(pyiter); - } + SettingsMap settings = get_settings(pysettings); engine->sync(depsgraph, context, settings); -- 2.30.2 From 1ce28eabff9301b8f84cbd191b116b0fd8b0d404 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Sat, 13 May 2023 21:39:53 +0300 Subject: [PATCH 02/10] Moved engine related fields to Engine class --- source/blender/render/hydra/engine.cc | 10 ++++++---- source/blender/render/hydra/engine.h | 16 +++++++-------- source/blender/render/hydra/final_engine.cc | 13 ++++++++---- source/blender/render/hydra/final_engine.h | 4 +++- source/blender/render/hydra/preview_engine.cc | 15 +++++++++----- source/blender/render/hydra/preview_engine.h | 4 +++- .../scene_delegate/blender_scene_delegate.cc | 20 +++++++++---------- .../scene_delegate/blender_scene_delegate.h | 11 ++++------ .../render/hydra/scene_delegate/instancer.cc | 6 ++++-- .../render/hydra/scene_delegate/mesh.cc | 3 ++- .../render/hydra/scene_delegate/world.cc | 3 ++- .../blender/render/hydra/viewport_engine.cc | 13 ++++++++---- source/blender/render/hydra/viewport_engine.h | 4 +++- 13 files changed, 73 insertions(+), 49 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index 3367c808c2d4..da606d7b4d36 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -17,13 +17,15 @@ namespace blender::render::hydra { CLG_LOGREF_DECLARE_GLOBAL(LOG_RENDER_HYDRA, "render.hydra"); -Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name, SettingsMap &settings) - : bl_engine_(bl_engine), render_delegate_name_(render_delegate_name), settings_(settings) +Engine::Engine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings) + : render_delegate_name(render_delegate_name), settings(settings), bl_engine_(bl_engine) { pxr::HdRendererPluginRegistry ®istry = pxr::HdRendererPluginRegistry::GetInstance(); pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - render_delegate_ = registry.CreateRenderDelegate(pxr::TfToken(render_delegate_name_)); + render_delegate_ = registry.CreateRenderDelegate(pxr::TfToken(render_delegate_name)); /* 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 */ @@ -45,7 +47,7 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name, render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("freeCamera")); render_task_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("renderTask")); - if (render_delegate_name_ == "HdStormRendererPlugin") { + if (render_delegate_name == "HdStormRendererPlugin") { simple_light_task_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("simpleLightTask")); diff --git a/source/blender/render/hydra/engine.h b/source/blender/render/hydra/engine.h index 74a590f03909..81acd68a139d 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -27,22 +27,22 @@ typedef pxr::TfHashMap Se class Engine { public: - Engine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings); + enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; + + Engine(RenderEngine *bl_engine, const std::string &render_delegate_name, SettingsMap &settings); virtual ~Engine() = default; - virtual void sync(Depsgraph *depsgraph, - bContext *context, - SettingsMap &render_settings) = 0; + virtual void sync(Depsgraph *depsgraph, bContext *context, SettingsMap &render_settings) = 0; virtual void render(Depsgraph *depsgraph) = 0; + EngineType type; + std::string render_delegate_name; + SettingsMap settings; + protected: float renderer_percent_done(); RenderEngine *bl_engine_; - std::string render_delegate_name_; - SettingsMap settings_; /* The order is important due to deletion order */ pxr::HgiUniquePtr hgi_; diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 9f4bee9c6dd2..401bfc106d23 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -13,15 +13,20 @@ namespace blender::render::hydra { +FinalEngine::FinalEngine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings) + : Engine(bl_engine, render_delegate_name, settings) +{ + type = EngineType::FINAL; +} + void FinalEngine::sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) { scene_delegate_ = std::make_unique( - render_index_.get(), - pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), - BlenderSceneDelegate::EngineType::FINAL, - render_delegate_name_); + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); scene_delegate_->populate(depsgraph, context); for (auto const &setting : render_settings) { diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 585b4a36976e..c37d0da14ad9 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -9,7 +9,9 @@ namespace blender::render::hydra { class FinalEngine : public Engine { public: - using Engine::Engine; + FinalEngine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings); virtual void sync(Depsgraph *depsgraph, bContext *context, diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index b27b9131e674..664478ed3fcd 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -13,6 +13,14 @@ const double LIFETIME = 180.0; std::unique_ptr PreviewEngine::instance_; +PreviewEngine::PreviewEngine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings) + : FinalEngine(bl_engine, render_delegate_name, settings) +{ + type = EngineType::PREVIEW; +} + PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, const std::string &render_delegate_name, SettingsMap &settings) @@ -43,10 +51,7 @@ void PreviewEngine::sync(Depsgraph *depsgraph, { if (!scene_delegate_) { scene_delegate_ = std::make_unique( - render_index_.get(), - pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), - BlenderSceneDelegate::EngineType::PREVIEW, - render_delegate_name_); + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); } scene_delegate_->clear(); scene_delegate_->populate(depsgraph, context); @@ -102,7 +107,7 @@ void PreviewEngine::update(RenderEngine *bl_engine, SettingsMap &settings) { bl_engine_ = bl_engine; - if (render_delegate_name != render_delegate_name_) { + if (render_delegate_name != render_delegate_name) { render_delegate_->Stop(); instance_.reset(new PreviewEngine(bl_engine, render_delegate_name, settings)); } diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 8949b4e931f6..699ba90ac611 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -9,7 +9,9 @@ namespace blender::render::hydra { class PreviewEngine : public FinalEngine { public: - using FinalEngine::FinalEngine; + PreviewEngine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings); static PreviewEngine *get_instance(RenderEngine *bl_engine, const std::string &render_delegate_name, 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 7cc48f1e2b47..17a78c7a019f 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -14,11 +14,8 @@ 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, - const std::string &render_delegate_name) - : HdSceneDelegate(parent_index, delegate_id), - engine_type(engine_type), - render_delegate_name(render_delegate_name) + Engine *engine) + : HdSceneDelegate(parent_index, delegate_id), engine(engine) { } @@ -376,8 +373,8 @@ void BlenderSceneDelegate::check_updates() DEGIDIterData data = {0}; data.graph = depsgraph; data.only_updated = true; - ITER_BEGIN ( - DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { + ITER_BEGIN (DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) + { CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, @@ -451,7 +448,8 @@ void BlenderSceneDelegate::add_new_objects() DEG_iterator_objects_end, &data, Object *, - object) { + object) + { update_objects(object); update_instancers(object); @@ -476,7 +474,8 @@ void BlenderSceneDelegate::remove_unused_objects() DEG_iterator_objects_end, &data, Object *, - object) { + object) + { if (ObjectData::is_supported(object)) { available_objects.insert(object_prim_id(object).GetName()); } @@ -554,7 +553,8 @@ void BlenderSceneDelegate::update_visibility() DEG_iterator_objects_end, &data, Object *, - object) { + object) + { if (!object_data(object_prim_id(object))) { update_objects(object); 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 11aef5cb3bd7..45f5690d5572 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -21,18 +21,17 @@ namespace blender::render::hydra { extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE; +class Engine; + class BlenderSceneDelegate : public pxr::HdSceneDelegate { 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 }; - BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id, - BlenderSceneDelegate::EngineType engine_type, - const std::string &render_delegate_name); + Engine *engine); ~BlenderSceneDelegate() override = default; /* Delegate methods */ @@ -56,13 +55,11 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { void populate(Depsgraph *depsgraph, bContext *context); void clear(); - EngineType engine_type; Depsgraph *depsgraph = nullptr; bContext *context = nullptr; View3D *view3d = nullptr; Scene *scene = nullptr; - - std::string render_delegate_name; + Engine *engine; private: pxr::SdfPath prim_id(ID *id, const char *prefix) const; diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index e10cd9972574..28198b3e630c 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -66,7 +66,8 @@ void InstancerData::update() Object *object = (Object *)id; if (id->recalc & ID_RECALC_GEOMETRY || (object->data && ((ID *)object->data)->recalc & ID_RECALC_GEOMETRY) || - id->recalc & ID_RECALC_TRANSFORM) { + id->recalc & ID_RECALC_TRANSFORM) + { set_instances(); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty( prim_id, pxr::HdChangeTracker::AllDirty); @@ -174,7 +175,8 @@ void InstancerData::check_update(Object *object) if (l_it != light_instances_.end()) { Object *obj = (Object *)l_it->second.data->id; if (obj->id.recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY) || - ((ID *)obj->data)->recalc & ID_RECALC_GEOMETRY) { + ((ID *)obj->data)->recalc & ID_RECALC_GEOMETRY) + { set_instances(); } return; diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index 0bae621a101b..dcb47524b7d3 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -27,7 +27,8 @@ void MeshData::init() Object *object = (Object *)id; if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && - BLI_listbase_is_empty(&object->modifiers)) { + BLI_listbase_is_empty(&object->modifiers)) + { write_mesh((Mesh *)object->data); } else { diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 184555ff0a3b..7b70f8697e4a 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -19,6 +19,7 @@ #include "BLI_path_util.h" #include "NOD_shader.h" +#include "../engine.h" #include "blender_scene_delegate.h" #include "image.h" #include "world.h" @@ -143,7 +144,7 @@ void WorldData::write_transform() transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), pxr::GfVec3d()); /* TODO : do this check via RenderSettings*/ - if (scene_delegate_->render_delegate_name == "HdRprPlugin") { + if (scene_delegate_->engine->render_delegate_name == "HdRprPlugin") { transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), pxr::GfVec3d()); transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 1d2a68b88a29..323fe9cd2741 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -217,16 +217,21 @@ void DrawTexture::free() texture_ = nullptr; } +ViewportEngine::ViewportEngine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings) + : Engine(bl_engine, render_delegate_name, settings) +{ + type = EngineType::VIEWPORT; +} + void ViewportEngine::sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) { if (!scene_delegate_) { scene_delegate_ = std::make_unique( - render_index_.get(), - pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), - BlenderSceneDelegate::EngineType::VIEWPORT, - render_delegate_name_); + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); } scene_delegate_->populate(depsgraph, context); diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index 2f893d71a9d1..7dbd530a3a8f 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -32,7 +32,9 @@ class DrawTexture { class ViewportEngine : public Engine { public: - using Engine::Engine; + ViewportEngine(RenderEngine *bl_engine, + const std::string &render_delegate_name, + SettingsMap &settings); void sync(Depsgraph *depsgraph, bContext *context, -- 2.30.2 From 4529c674dce2f794b210ff31b68fbc1710b18a8f Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 15 May 2023 13:13:40 +0300 Subject: [PATCH 03/10] Added Engine::set_sync_setting(), Engine::set_render_setting(). Adjusted code. --- source/blender/render/hydra/engine.cc | 16 +++- source/blender/render/hydra/engine.h | 11 ++- source/blender/render/hydra/final_engine.cc | 14 +-- source/blender/render/hydra/final_engine.h | 8 +- source/blender/render/hydra/preview_engine.cc | 27 ++---- source/blender/render/hydra/preview_engine.h | 15 +-- source/blender/render/hydra/python.cc | 96 ++++++++++++------- .../blender/render/hydra/viewport_engine.cc | 14 +-- source/blender/render/hydra/viewport_engine.h | 8 +- 9 files changed, 99 insertions(+), 110 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index da606d7b4d36..50fe625969e4 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -17,10 +17,8 @@ namespace blender::render::hydra { CLG_LOGREF_DECLARE_GLOBAL(LOG_RENDER_HYDRA, "render.hydra"); -Engine::Engine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings) - : render_delegate_name(render_delegate_name), settings(settings), bl_engine_(bl_engine) +Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) + : render_delegate_name(render_delegate_name), bl_engine_(bl_engine) { pxr::HdRendererPluginRegistry ®istry = pxr::HdRendererPluginRegistry::GetInstance(); @@ -56,6 +54,16 @@ Engine::Engine(RenderEngine *bl_engine, engine_ = std::make_unique(); } +void Engine::set_sync_setting(const pxr::TfToken &key, const pxr::VtValue &val) +{ + settings[key] = val; +} + +void Engine::set_render_setting(const pxr::TfToken &key, const pxr::VtValue &val) +{ + render_delegate_->SetRenderSetting(key, val); +} + float Engine::renderer_percent_done() { pxr::VtDictionary render_stats = render_delegate_->GetRenderStats(); diff --git a/source/blender/render/hydra/engine.h b/source/blender/render/hydra/engine.h index 81acd68a139d..11f6e3cffaf6 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -23,21 +23,22 @@ namespace blender::render::hydra { extern struct CLG_LogRef *LOG_RENDER_HYDRA; -typedef pxr::TfHashMap SettingsMap; - class Engine { public: enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; - Engine(RenderEngine *bl_engine, const std::string &render_delegate_name, SettingsMap &settings); + Engine(RenderEngine *bl_engine, const std::string &render_delegate_name); virtual ~Engine() = default; - virtual void sync(Depsgraph *depsgraph, bContext *context, SettingsMap &render_settings) = 0; + virtual void sync(Depsgraph *depsgraph, bContext *context) = 0; virtual void render(Depsgraph *depsgraph) = 0; + void set_sync_setting(const pxr::TfToken &key, const pxr::VtValue &val); + void set_render_setting(const pxr::TfToken &key, const pxr::VtValue &val); + EngineType type; std::string render_delegate_name; - SettingsMap settings; + pxr::TfHashMap settings; protected: float renderer_percent_done(); diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 401bfc106d23..10f64d99a469 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -13,25 +13,17 @@ namespace blender::render::hydra { -FinalEngine::FinalEngine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings) - : Engine(bl_engine, render_delegate_name, settings) +FinalEngine::FinalEngine(RenderEngine *bl_engine, const std::string &render_delegate_name) + : Engine(bl_engine, render_delegate_name) { type = EngineType::FINAL; } -void FinalEngine::sync(Depsgraph *depsgraph, - bContext *context, - pxr::HdRenderSettingsMap &render_settings) +void FinalEngine::sync(Depsgraph *depsgraph, bContext *context) { scene_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); scene_delegate_->populate(depsgraph, context); - - for (auto const &setting : render_settings) { - render_delegate_->SetRenderSetting(setting.first, setting.second); - } } void FinalEngine::render(Depsgraph *depsgraph) diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index c37d0da14ad9..f9a4441ad9ae 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -9,13 +9,9 @@ namespace blender::render::hydra { class FinalEngine : public Engine { public: - FinalEngine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings); + FinalEngine(RenderEngine *bl_engine, const std::string &render_delegate_name); - virtual void sync(Depsgraph *depsgraph, - bContext *context, - pxr::HdRenderSettingsMap &render_settings) override; + virtual void sync(Depsgraph *depsgraph, bContext *context) override; virtual void render(Depsgraph *b_depsgraph) override; protected: diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 664478ed3fcd..d87dbb269b72 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -13,26 +13,23 @@ const double LIFETIME = 180.0; std::unique_ptr PreviewEngine::instance_; -PreviewEngine::PreviewEngine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings) - : FinalEngine(bl_engine, render_delegate_name, settings) +PreviewEngine::PreviewEngine(RenderEngine *bl_engine, const std::string &render_delegate_name) + : FinalEngine(bl_engine, render_delegate_name) { type = EngineType::PREVIEW; } PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings) + const std::string &render_delegate_name) { if (!instance_) { - instance_ = std::make_unique(bl_engine, render_delegate_name, settings); + 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_name, settings); + instance_->update(bl_engine, render_delegate_name); return instance_.get(); } @@ -45,9 +42,7 @@ void PreviewEngine::schedule_free() BLI_timer_register((uintptr_t)instance_.get(), free_instance, nullptr, nullptr, LIFETIME, true); } -void PreviewEngine::sync(Depsgraph *depsgraph, - bContext *context, - pxr::HdRenderSettingsMap &render_settings) +void PreviewEngine::sync(Depsgraph *depsgraph, bContext *context) { if (!scene_delegate_) { scene_delegate_ = std::make_unique( @@ -55,10 +50,6 @@ void PreviewEngine::sync(Depsgraph *depsgraph, } scene_delegate_->clear(); scene_delegate_->populate(depsgraph, context); - - for (auto const &setting : render_settings) { - render_delegate_->SetRenderSetting(setting.first, setting.second); - } } void PreviewEngine::render(Depsgraph *depsgraph) @@ -102,14 +93,12 @@ double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) return -1; } -void PreviewEngine::update(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings) +void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_delegate_name) { bl_engine_ = bl_engine; if (render_delegate_name != render_delegate_name) { render_delegate_->Stop(); - instance_.reset(new PreviewEngine(bl_engine, render_delegate_name, settings)); + instance_.reset(new PreviewEngine(bl_engine, render_delegate_name)); } } diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 699ba90ac611..2e8ce474a87d 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -9,18 +9,13 @@ namespace blender::render::hydra { class PreviewEngine : public FinalEngine { public: - PreviewEngine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings); + PreviewEngine(RenderEngine *bl_engine, const std::string &render_delegate_name); static PreviewEngine *get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings); + const std::string &render_delegate_name); static void schedule_free(); - void sync(Depsgraph *depsgraph, - bContext *context, - pxr::HdRenderSettingsMap &render_settings) override; + void sync(Depsgraph *depsgraph, bContext *context) override; void render(Depsgraph *depsgraph) override; private: @@ -28,9 +23,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_name, - SettingsMap &settings); + void update(RenderEngine *bl_engine, const std::string &render_delegate_name); void update_render_result(std::vector &pixels); }; diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index df7f5beeb177..85044c364ca5 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -96,56 +96,29 @@ static PyObject *get_render_plugins_func(PyObject * /*self*/, PyObject *args) return ret; } -static SettingsMap get_settings(PyObject *pysettings) -{ - SettingsMap settings; - PyObject *pyiter = PyObject_GetIter(pysettings); - if (pyiter) { - PyObject *pykey, *pyval; - while (pykey = PyIter_Next(pyiter)) { - pxr::TfToken key(PyUnicode_AsUTF8(pykey)); - pyval = PyDict_GetItem(pysettings, pykey); - - if (PyLong_Check(pyval)) { - settings[key] = PyLong_AsLong(pyval); - } - else if (PyFloat_Check(pyval)) { - settings[key] = PyFloat_AsDouble(pyval); - } - else if (PyUnicode_Check(pyval)) { - settings[key] = PyUnicode_AsUTF8(pyval); - } - Py_DECREF(pykey); - } - Py_DECREF(pyiter); - } - return settings; -} - static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args) { - PyObject *pyengine, *pysettings; + PyObject *pyengine; char *engine_type, *render_delegate_id; - if (!PyArg_ParseTuple(args, "OssO", &pyengine, &engine_type, &render_delegate_id, &pysettings)) { + if (!PyArg_ParseTuple(args, "Oss", &pyengine, &engine_type, &render_delegate_id)) { Py_RETURN_NONE; } RenderEngine *bl_engine = (RenderEngine *)PyLong_AsVoidPtr(pyengine); - SettingsMap settings = get_settings(pysettings); Engine *engine; if (STREQ(engine_type, "VIEWPORT")) { - engine = new ViewportEngine(bl_engine, render_delegate_id, settings); + engine = new ViewportEngine(bl_engine, render_delegate_id); } else if (STREQ(engine_type, "PREVIEW")) { - engine = PreviewEngine::get_instance(bl_engine, render_delegate_id, settings); + engine = PreviewEngine::get_instance(bl_engine, render_delegate_id); } else { if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { - engine = new FinalEngineGL(bl_engine, render_delegate_id, settings); + engine = new FinalEngineGL(bl_engine, render_delegate_id); } else { - engine = new FinalEngine(bl_engine, render_delegate_id, settings); + engine = new FinalEngine(bl_engine, render_delegate_id); } } @@ -175,17 +148,16 @@ static PyObject *engine_free_func(PyObject * /*self*/, PyObject *args) static PyObject *engine_sync_func(PyObject * /*self*/, PyObject *args) { - PyObject *pyengine, *pydepsgraph, *pycontext, *pysettings; - if (!PyArg_ParseTuple(args, "OOOO", &pyengine, &pydepsgraph, &pycontext, &pysettings)) { + PyObject *pyengine, *pydepsgraph, *pycontext; + if (!PyArg_ParseTuple(args, "OOO", &pyengine, &pydepsgraph, &pycontext)) { Py_RETURN_NONE; } Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); Depsgraph *depsgraph = (Depsgraph *)PyLong_AsVoidPtr(pydepsgraph); bContext *context = (bContext *)PyLong_AsVoidPtr(pycontext); - SettingsMap settings = get_settings(pysettings); - engine->sync(depsgraph, context, settings); + engine->sync(depsgraph, context); CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); Py_RETURN_NONE; @@ -231,6 +203,54 @@ static PyObject *engine_view_draw_func(PyObject * /*self*/, PyObject *args) Py_RETURN_NONE; } +static pxr::VtValue get_setting_val(PyObject *pyval) +{ + pxr::VtValue val; + if (PyBool_Check(pyval)) { + val = Py_IsTrue(pyval); + } + else if (PyLong_Check(pyval)) { + val = PyLong_AsLong(pyval); + } + else if (PyFloat_Check(pyval)) { + val = PyFloat_AsDouble(pyval); + } + else if (PyUnicode_Check(pyval)) { + val = PyUnicode_AsUTF8(pyval); + } + return val; +} + +static PyObject *engine_set_sync_setting_func(PyObject * /*self*/, PyObject *args) +{ + PyObject *pyengine, *pyval; + char *key; + if (!PyArg_ParseTuple(args, "OsO", &pyengine, &key, &pyval)) { + Py_RETURN_NONE; + } + + Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); + engine->set_sync_setting(pxr::TfToken(key), get_setting_val(pyval)); + + CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); + Py_RETURN_NONE; +} + +static PyObject *engine_set_render_setting_func(PyObject * /*self*/, PyObject *args) +{ + PyObject *pyengine, *pyval; + char *key; + if (!PyArg_ParseTuple(args, "OsO", &pyengine, &key, &pyval)) { + Py_RETURN_NONE; + } + + Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); + engine->set_render_setting(pxr::TfToken(key), get_setting_val(pyval)); + + CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); + Py_RETURN_NONE; +} + static PyMethodDef methods[] = { {"init", init_func, METH_VARARGS, ""}, {"register_plugins", register_plugins_func, METH_VARARGS, ""}, @@ -241,6 +261,8 @@ static PyMethodDef methods[] = { {"engine_sync", engine_sync_func, METH_VARARGS, ""}, {"engine_render", engine_render_func, METH_VARARGS, ""}, {"engine_view_draw", engine_view_draw_func, METH_VARARGS, ""}, + {"engine_sync_setting", engine_set_sync_setting_func, METH_VARARGS, ""}, + {"engine_render_setting", engine_set_render_setting_func, METH_VARARGS, ""}, {NULL, NULL, 0, NULL}, }; diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 323fe9cd2741..f872802722fe 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -217,27 +217,19 @@ void DrawTexture::free() texture_ = nullptr; } -ViewportEngine::ViewportEngine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings) - : Engine(bl_engine, render_delegate_name, settings) +ViewportEngine::ViewportEngine(RenderEngine *bl_engine, const std::string &render_delegate_name) + : Engine(bl_engine, render_delegate_name) { type = EngineType::VIEWPORT; } -void ViewportEngine::sync(Depsgraph *depsgraph, - bContext *context, - pxr::HdRenderSettingsMap &render_settings) +void ViewportEngine::sync(Depsgraph *depsgraph, bContext *context) { if (!scene_delegate_) { scene_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); } scene_delegate_->populate(depsgraph, context); - - for (auto const &setting : render_settings) { - render_delegate_->SetRenderSetting(setting.first, setting.second); - } } void ViewportEngine::render(Depsgraph *depsgraph) diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index 7dbd530a3a8f..410840400471 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -32,13 +32,9 @@ class DrawTexture { class ViewportEngine : public Engine { public: - ViewportEngine(RenderEngine *bl_engine, - const std::string &render_delegate_name, - SettingsMap &settings); + ViewportEngine(RenderEngine *bl_engine, const std::string &render_delegate_name); - void sync(Depsgraph *depsgraph, - bContext *context, - pxr::HdRenderSettingsMap &render_settings) override; + void sync(Depsgraph *depsgraph, bContext *context) override; void render(Depsgraph *depsgraph) override; void render(Depsgraph *depsgraph, bContext *context); -- 2.30.2 From 5c466ea95fa968a6e8c6242b926ee7e6dcf60b0e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 15 May 2023 15:49:12 +0300 Subject: [PATCH 04/10] Adjusted bpy_hydra.py. Fixes in python.cc --- scripts/modules/bpy_hydra.py | 38 +++++++++++++++++++++------ source/blender/render/hydra/python.cc | 8 +++--- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index 2511d9937ede..f8002df55f5f 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -21,7 +21,13 @@ class CustomHydraRenderEngine(HydraRenderEngine): bpy_hydra.register_plugins(["/path/to/plugin")]) - def get_delegate_settings(self, engine_type): + def get_sync_settings(self, engine_type): + return { + 'setting1': True, + 'setting2': "2", + } + + def get_render_settings(self, engine_type): return { 'setting1': 1, 'setting2': "2", @@ -73,27 +79,43 @@ class HydraRenderEngine(bpy.types.RenderEngine): def unregister(cls): pass - def get_delegate_settings(self, engine_type): + def get_sync_settings(self, engine_type): + return {} + + def get_render_settings(self, engine_type): return {} # final render def update(self, data, depsgraph): engine_type = 'PREVIEW' if self.is_preview else 'FINAL' self.engine_ptr = _bpy_hydra.engine_create(self.as_pointer(), engine_type, self.delegate_id) - delegate_settings = self.get_delegate_settings(engine_type) - _bpy_hydra.engine_sync(self.engine_ptr, depsgraph.as_pointer(), bpy.context.as_pointer(), delegate_settings) + + for key, val in self.get_sync_settings(engine_type).items(): + _bpy_hydra.engine_set_sync_setting(self.engine_ptr, key, val) + + _bpy_hydra.engine_sync(self.engine_ptr, depsgraph.as_pointer(), bpy.context.as_pointer()) def render(self, depsgraph): - if self.engine_ptr: - _bpy_hydra.engine_render(self.engine_ptr, depsgraph.as_pointer()) + if not self.engine_ptr: + return + + for key, val in self.get_render_settings('PREVIEW' if self.is_preview else 'FINAL').items(): + _bpy_hydra.engine_set_render_setting(self.engine_ptr, key, val) + + _bpy_hydra.engine_render(self.engine_ptr, depsgraph.as_pointer()) # viewport render def view_update(self, context, depsgraph): if not self.engine_ptr: self.engine_ptr = _bpy_hydra.engine_create(self.as_pointer(), 'VIEWPORT', self.delegate_id) - delegate_settings = self.get_delegate_settings('VIEWPORT') - _bpy_hydra.engine_sync(self.engine_ptr, depsgraph.as_pointer(), context.as_pointer(), delegate_settings) + for key, val in self.get_sync_settings('VIEWPORT').items(): + _bpy_hydra.engine_set_sync_setting(self.engine_ptr, key, val) + + _bpy_hydra.engine_sync(self.engine_ptr, depsgraph.as_pointer(), context.as_pointer()) + + for key, val in self.get_render_settings('VIEWPORT').items(): + _bpy_hydra.engine_set_render_setting(self.engine_ptr, key, val) def view_draw(self, context, depsgraph): if not self.engine_ptr: diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 85044c364ca5..88bfb3e07869 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -232,7 +232,7 @@ static PyObject *engine_set_sync_setting_func(PyObject * /*self*/, PyObject *arg Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); engine->set_sync_setting(pxr::TfToken(key), get_setting_val(pyval)); - CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); + CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx: %s", engine, key); Py_RETURN_NONE; } @@ -247,7 +247,7 @@ static PyObject *engine_set_render_setting_func(PyObject * /*self*/, PyObject *a Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); engine->set_render_setting(pxr::TfToken(key), get_setting_val(pyval)); - CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx", engine); + CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx: %s", engine, key); Py_RETURN_NONE; } @@ -261,8 +261,8 @@ static PyMethodDef methods[] = { {"engine_sync", engine_sync_func, METH_VARARGS, ""}, {"engine_render", engine_render_func, METH_VARARGS, ""}, {"engine_view_draw", engine_view_draw_func, METH_VARARGS, ""}, - {"engine_sync_setting", engine_set_sync_setting_func, METH_VARARGS, ""}, - {"engine_render_setting", engine_set_render_setting_func, METH_VARARGS, ""}, + {"engine_set_sync_setting", engine_set_sync_setting_func, METH_VARARGS, ""}, + {"engine_set_render_setting", engine_set_render_setting_func, METH_VARARGS, ""}, {NULL, NULL, 0, NULL}, }; -- 2.30.2 From c00f087fa5cb4cfd33cd065be457ea12e6dfe3da Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 15 May 2023 19:57:05 +0300 Subject: [PATCH 05/10] Splitted MaterialData::init() to export_mtlx() and write_material_network_map(). Removed EngineType as not used anymore. --- source/blender/render/hydra/engine.cc | 4 +- source/blender/render/hydra/engine.h | 4 - source/blender/render/hydra/final_engine.cc | 8 -- source/blender/render/hydra/final_engine.h | 2 +- source/blender/render/hydra/preview_engine.cc | 10 -- source/blender/render/hydra/preview_engine.h | 8 +- source/blender/render/hydra/python.cc | 2 +- .../scene_delegate/blender_scene_delegate.cc | 7 + .../scene_delegate/blender_scene_delegate.h | 6 + .../render/hydra/scene_delegate/material.cc | 132 ++++++++++-------- .../render/hydra/scene_delegate/material.h | 3 + .../blender/render/hydra/viewport_engine.cc | 10 -- source/blender/render/hydra/viewport_engine.h | 2 +- 13 files changed, 98 insertions(+), 100 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index 50fe625969e4..c632e0e71d1e 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -43,6 +43,8 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) render_index_.reset(pxr::HdRenderIndex::New(render_delegate_.Get(), hd_drivers)); free_camera_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("freeCamera")); + scene_delegate_ = std::make_unique( + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); render_task_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("renderTask")); if (render_delegate_name == "HdStormRendererPlugin") { @@ -56,7 +58,7 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) void Engine::set_sync_setting(const pxr::TfToken &key, const pxr::VtValue &val) { - settings[key] = val; + scene_delegate_->set_setting(key, val); } void Engine::set_render_setting(const pxr::TfToken &key, const pxr::VtValue &val) diff --git a/source/blender/render/hydra/engine.h b/source/blender/render/hydra/engine.h index 11f6e3cffaf6..a1e186c24fc9 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -25,8 +25,6 @@ extern struct CLG_LogRef *LOG_RENDER_HYDRA; class Engine { public: - enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; - Engine(RenderEngine *bl_engine, const std::string &render_delegate_name); virtual ~Engine() = default; @@ -36,9 +34,7 @@ class Engine { void set_sync_setting(const pxr::TfToken &key, const pxr::VtValue &val); void set_render_setting(const pxr::TfToken &key, const pxr::VtValue &val); - EngineType type; std::string render_delegate_name; - pxr::TfHashMap settings; protected: float renderer_percent_done(); diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 10f64d99a469..b418c0bf64df 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -13,16 +13,8 @@ namespace blender::render::hydra { -FinalEngine::FinalEngine(RenderEngine *bl_engine, const std::string &render_delegate_name) - : Engine(bl_engine, render_delegate_name) -{ - type = EngineType::FINAL; -} - void FinalEngine::sync(Depsgraph *depsgraph, bContext *context) { - scene_delegate_ = std::make_unique( - render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); scene_delegate_->populate(depsgraph, context); } diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index f9a4441ad9ae..681c4a299a09 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -9,7 +9,7 @@ namespace blender::render::hydra { class FinalEngine : public Engine { public: - FinalEngine(RenderEngine *bl_engine, const std::string &render_delegate_name); + using Engine::Engine; virtual void sync(Depsgraph *depsgraph, bContext *context) override; virtual void render(Depsgraph *b_depsgraph) override; diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index d87dbb269b72..204f3f7d49ba 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -13,12 +13,6 @@ const double LIFETIME = 180.0; std::unique_ptr PreviewEngine::instance_; -PreviewEngine::PreviewEngine(RenderEngine *bl_engine, const std::string &render_delegate_name) - : FinalEngine(bl_engine, render_delegate_name) -{ - type = EngineType::PREVIEW; -} - PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, const std::string &render_delegate_name) { @@ -44,10 +38,6 @@ void PreviewEngine::schedule_free() void PreviewEngine::sync(Depsgraph *depsgraph, bContext *context) { - if (!scene_delegate_) { - scene_delegate_ = std::make_unique( - render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); - } scene_delegate_->clear(); scene_delegate_->populate(depsgraph, context); } diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 2e8ce474a87d..3f27e9fb1b49 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -9,8 +9,7 @@ namespace blender::render::hydra { class PreviewEngine : public FinalEngine { public: - PreviewEngine(RenderEngine *bl_engine, const std::string &render_delegate_name); - + using FinalEngine::FinalEngine; static PreviewEngine *get_instance(RenderEngine *bl_engine, const std::string &render_delegate_name); static void schedule_free(); @@ -19,12 +18,13 @@ class PreviewEngine : public FinalEngine { void render(Depsgraph *depsgraph) override; private: - /* Singleton class instance */ 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_name); void update_render_result(std::vector &pixels); + + /* Singleton class instance */ + static std::unique_ptr instance_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 88bfb3e07869..fc449408dcb2 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -216,7 +216,7 @@ static pxr::VtValue get_setting_val(PyObject *pyval) val = PyFloat_AsDouble(pyval); } else if (PyUnicode_Check(pyval)) { - val = PyUnicode_AsUTF8(pyval); + val = std::string(PyUnicode_AsUTF8(pyval)); } return val; } 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 17a78c7a019f..aa853f655105 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -203,6 +203,13 @@ void BlenderSceneDelegate::clear() view3d = nullptr; } +void BlenderSceneDelegate::set_setting(const pxr::TfToken &key, const pxr::VtValue &val) +{ + if (key.GetString() == "MaterialXFilenameKey") { + settings.mx_filename_key = pxr::TfToken(val.Get()); + } +} + pxr::SdfPath BlenderSceneDelegate::prim_id(ID *id, const char *prefix) const { /* Making id of object in form like _ */ 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 45f5690d5572..febb30f87bb8 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -29,6 +29,10 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { friend MaterialData; /* has access to objects and instancers */ public: + struct Settings { + pxr::TfToken mx_filename_key; + }; + BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id, Engine *engine); @@ -54,12 +58,14 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { void populate(Depsgraph *depsgraph, bContext *context); void clear(); + void set_setting(const pxr::TfToken &key, const pxr::VtValue &val); Depsgraph *depsgraph = nullptr; bContext *context = nullptr; View3D *view3d = nullptr; Scene *scene = nullptr; Engine *engine; + Settings settings; private: pxr::SdfPath prim_id(ID *id, const char *prefix) const; diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index cac62eda0be5..15013996f36f 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -14,6 +14,7 @@ #include "RNA_blender_cpp.h" #include "bpy_rna.h" +#include "../engine.h" #include "blender_scene_delegate.h" #include "material.h" #include "mtlx_hydra_adapter.h" @@ -31,8 +32,65 @@ void MaterialData::init() { ID_LOG(2, ""); double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0; - material_network_map_ = pxr::VtValue(); + export_mtlx(); + if (!scene_delegate_->settings.mx_filename_key.IsEmpty()) { + write_material_network_map(); + } +} + +void MaterialData::insert() +{ + ID_LOG(2, ""); + scene_delegate_->GetRenderIndex().InsertSprim( + pxr::HdPrimTypeTokens->material, scene_delegate_, prim_id); +} + +void MaterialData::remove() +{ + CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s", prim_id.GetText()); + scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, prim_id); +} + +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) { + 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); + } + } +} + +pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const +{ + pxr::VtValue ret; + if (key.GetString() == "MaterialXFilename") { + if (!mtlx_path_.GetResolvedPath().empty()) { + ret = mtlx_path_; + } + ID_LOG(3, "%s", key.GetText()); + } + return ret; +} + +pxr::VtValue MaterialData::get_material_resource() const +{ + return material_network_map_; +} + +void MaterialData::export_mtlx() +{ /* Call of python function hydra.export_mtlx() */ PyGILState_STATE gstate; @@ -87,71 +145,25 @@ void MaterialData::init() mtlx_path_ = pxr::SdfAssetPath(path, path); ID_LOG(2, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str()); +} - /* Calculate material network map */ - if (!path.empty()) { - pxr::HdRenderDelegate *render_delegate = scene_delegate_->GetRenderIndex().GetRenderDelegate(); - pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes(); - pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts(); - - pxr::HdMaterialNetworkMap network_map; - hdmtlx_convert_to_materialnetworkmap(path, shader_source_types, render_contexts, &network_map); - - material_network_map_ = network_map; - } - else { +void MaterialData::write_material_network_map() +{ + ID_LOG(2, ""); + if (mtlx_path_.GetResolvedPath().empty()) { material_network_map_ = pxr::VtValue(); + return; } -} -void MaterialData::insert() -{ - ID_LOG(2, ""); - scene_delegate_->GetRenderIndex().InsertSprim( - pxr::HdPrimTypeTokens->material, scene_delegate_, prim_id); -} + pxr::HdRenderDelegate *render_delegate = scene_delegate_->GetRenderIndex().GetRenderDelegate(); + pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes(); + pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts(); -void MaterialData::remove() -{ - CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 2, "%s", prim_id.GetText()); - scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, prim_id); -} + pxr::HdMaterialNetworkMap network_map; + hdmtlx_convert_to_materialnetworkmap( + mtlx_path_.GetResolvedPath(), shader_source_types, render_contexts, &network_map); -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) { - 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); - } - } -} - -pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const -{ - pxr::VtValue ret; - if (key.GetString() == "MaterialXFilename") { - if (!mtlx_path_.GetResolvedPath().empty()) { - ret = mtlx_path_; - } - ID_LOG(3, "%s", key.GetText()); - } - return ret; -} - -pxr::VtValue MaterialData::get_material_resource() const -{ - return material_network_map_; + material_network_map_ = network_map; } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index f70a7ef11c5f..94df4d17b582 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -30,6 +30,9 @@ class MaterialData : public IdData { bool double_sided = true; private: + void export_mtlx(); + void write_material_network_map(); + pxr::SdfAssetPath mtlx_path_; pxr::VtValue material_network_map_; }; diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index f872802722fe..9341aba64f7c 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -217,18 +217,8 @@ void DrawTexture::free() texture_ = nullptr; } -ViewportEngine::ViewportEngine(RenderEngine *bl_engine, const std::string &render_delegate_name) - : Engine(bl_engine, render_delegate_name) -{ - type = EngineType::VIEWPORT; -} - void ViewportEngine::sync(Depsgraph *depsgraph, bContext *context) { - if (!scene_delegate_) { - scene_delegate_ = std::make_unique( - render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), this); - } scene_delegate_->populate(depsgraph, context); } diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index 410840400471..13406259c262 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -32,7 +32,7 @@ class DrawTexture { class ViewportEngine : public Engine { public: - ViewportEngine(RenderEngine *bl_engine, const std::string &render_delegate_name); + using Engine::Engine; void sync(Depsgraph *depsgraph, bContext *context) override; void render(Depsgraph *depsgraph) override; -- 2.30.2 From e294544c4602be0733261a54f208a06c1a46a12d Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 15 May 2023 20:33:48 +0300 Subject: [PATCH 06/10] Code simplification --- source/blender/render/hydra/engine.cc | 6 +++--- source/blender/render/hydra/engine.h | 4 ++-- source/blender/render/hydra/python.cc | 4 ++-- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 4 ++-- .../render/hydra/scene_delegate/blender_scene_delegate.h | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index c632e0e71d1e..0c980af10bac 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -56,14 +56,14 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) engine_ = std::make_unique(); } -void Engine::set_sync_setting(const pxr::TfToken &key, const pxr::VtValue &val) +void Engine::set_sync_setting(const std::string &key, const pxr::VtValue &val) { scene_delegate_->set_setting(key, val); } -void Engine::set_render_setting(const pxr::TfToken &key, const pxr::VtValue &val) +void Engine::set_render_setting(const std::string &key, const pxr::VtValue &val) { - render_delegate_->SetRenderSetting(key, val); + render_delegate_->SetRenderSetting(pxr::TfToken(key), val); } float Engine::renderer_percent_done() diff --git a/source/blender/render/hydra/engine.h b/source/blender/render/hydra/engine.h index a1e186c24fc9..d650e956e2df 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -31,8 +31,8 @@ class Engine { virtual void sync(Depsgraph *depsgraph, bContext *context) = 0; virtual void render(Depsgraph *depsgraph) = 0; - void set_sync_setting(const pxr::TfToken &key, const pxr::VtValue &val); - void set_render_setting(const pxr::TfToken &key, const pxr::VtValue &val); + void set_sync_setting(const std::string &key, const pxr::VtValue &val); + void set_render_setting(const std::string &key, const pxr::VtValue &val); std::string render_delegate_name; diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index fc449408dcb2..1c2c8ea0fc04 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -230,7 +230,7 @@ static PyObject *engine_set_sync_setting_func(PyObject * /*self*/, PyObject *arg } Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); - engine->set_sync_setting(pxr::TfToken(key), get_setting_val(pyval)); + engine->set_sync_setting(key, get_setting_val(pyval)); CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx: %s", engine, key); Py_RETURN_NONE; @@ -245,7 +245,7 @@ static PyObject *engine_set_render_setting_func(PyObject * /*self*/, PyObject *a } Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); - engine->set_render_setting(pxr::TfToken(key), get_setting_val(pyval)); + engine->set_render_setting(key, get_setting_val(pyval)); CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx: %s", engine, key); Py_RETURN_NONE; 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 aa853f655105..264a8d43b6ec 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -203,9 +203,9 @@ void BlenderSceneDelegate::clear() view3d = nullptr; } -void BlenderSceneDelegate::set_setting(const pxr::TfToken &key, const pxr::VtValue &val) +void BlenderSceneDelegate::set_setting(const std::string &key, const pxr::VtValue &val) { - if (key.GetString() == "MaterialXFilenameKey") { + if (key == "MaterialXFilenameKey") { settings.mx_filename_key = pxr::TfToken(val.Get()); } } 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 febb30f87bb8..69ed762c2159 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -58,7 +58,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { void populate(Depsgraph *depsgraph, bContext *context); void clear(); - void set_setting(const pxr::TfToken &key, const pxr::VtValue &val); + void set_setting(const std::string &key, const pxr::VtValue &val); Depsgraph *depsgraph = nullptr; bContext *context = nullptr; -- 2.30.2 From 1e87d9571358d0acd11b9814af0f56fd6a2c40ff Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Mon, 15 May 2023 21:17:45 +0300 Subject: [PATCH 07/10] Fixed using mx_filename_key --- source/blender/render/hydra/scene_delegate/material.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 15013996f36f..cd832a4f1816 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -34,7 +34,7 @@ void MaterialData::init() double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0; export_mtlx(); - if (!scene_delegate_->settings.mx_filename_key.IsEmpty()) { + if (scene_delegate_->settings.mx_filename_key.IsEmpty()) { write_material_network_map(); } } @@ -75,7 +75,7 @@ void MaterialData::update() pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; - if (key.GetString() == "MaterialXFilename") { + if (key == scene_delegate_->settings.mx_filename_key) { if (!mtlx_path_.GetResolvedPath().empty()) { ret = mtlx_path_; } -- 2.30.2 From 62f4c6f028b8e959d5444b2ffd6478cd729d4f34 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 16 May 2023 10:51:03 +0300 Subject: [PATCH 08/10] Refactor: improved working with PreviewEngine, removed sync() from derived engines --- source/blender/render/hydra/engine.cc | 5 +++ source/blender/render/hydra/engine.h | 2 +- source/blender/render/hydra/final_engine.cc | 5 --- source/blender/render/hydra/final_engine.h | 3 +- source/blender/render/hydra/preview_engine.cc | 39 ++++++++----------- source/blender/render/hydra/preview_engine.h | 9 ++--- source/blender/render/hydra/python.cc | 4 +- .../blender/render/hydra/viewport_engine.cc | 5 --- source/blender/render/hydra/viewport_engine.h | 1 - 9 files changed, 28 insertions(+), 45 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index 0c980af10bac..10c50c6b248e 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -56,6 +56,11 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) engine_ = std::make_unique(); } +void Engine::sync(Depsgraph *depsgraph, bContext *context) +{ + scene_delegate_->populate(depsgraph, context); +} + void Engine::set_sync_setting(const std::string &key, const pxr::VtValue &val) { scene_delegate_->set_setting(key, val); diff --git a/source/blender/render/hydra/engine.h b/source/blender/render/hydra/engine.h index d650e956e2df..52e7c4f725a6 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -28,7 +28,7 @@ class Engine { Engine(RenderEngine *bl_engine, const std::string &render_delegate_name); virtual ~Engine() = default; - virtual void sync(Depsgraph *depsgraph, bContext *context) = 0; + void sync(Depsgraph *depsgraph, bContext *context); virtual void render(Depsgraph *depsgraph) = 0; void set_sync_setting(const std::string &key, const pxr::VtValue &val); diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index b418c0bf64df..bdbade2ebb70 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -13,11 +13,6 @@ namespace blender::render::hydra { -void FinalEngine::sync(Depsgraph *depsgraph, bContext *context) -{ - scene_delegate_->populate(depsgraph, context); -} - void FinalEngine::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 681c4a299a09..25e44e8a288a 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -11,8 +11,7 @@ class FinalEngine : public Engine { public: using Engine::Engine; - virtual void sync(Depsgraph *depsgraph, bContext *context) override; - virtual void render(Depsgraph *b_depsgraph) override; + void render(Depsgraph *b_depsgraph) override; protected: void update_render_result(); diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 204f3f7d49ba..06c05fd08f83 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -13,33 +13,35 @@ const double LIFETIME = 180.0; std::unique_ptr PreviewEngine::instance_; -PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_name) +PreviewEngine *PreviewEngine::create(RenderEngine *bl_engine, + const std::string &render_delegate_name) { if (!instance_) { 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()); + else if (instance_->render_delegate_name != render_delegate_name) { + instance_->render_delegate_->Stop(); + instance_ = std::make_unique(bl_engine, render_delegate_name); + } + else { + instance_->bl_engine_ = bl_engine; + } + + if (BLI_timer_is_registered((uintptr_t)&instance_)) { + /* Unregister timer while PreviewEngine is working */ + BLI_timer_unregister((uintptr_t)&instance_); } - instance_->update(bl_engine, render_delegate_name); return instance_.get(); } -void PreviewEngine::schedule_free() +void PreviewEngine::free() { instance_->render_delegate_->Stop(); + instance_->scene_delegate_->clear(); /* Register timer for schedule free PreviewEngine instance */ - BLI_timer_register((uintptr_t)instance_.get(), free_instance, nullptr, nullptr, LIFETIME, true); -} - -void PreviewEngine::sync(Depsgraph *depsgraph, bContext *context) -{ - scene_delegate_->clear(); - scene_delegate_->populate(depsgraph, context); + BLI_timer_register((uintptr_t)&instance_, free_instance, nullptr, nullptr, LIFETIME, true); } void PreviewEngine::render(Depsgraph *depsgraph) @@ -83,15 +85,6 @@ double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) return -1; } -void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_delegate_name) -{ - 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) { RenderResult *result = RE_engine_begin_result( diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 3f27e9fb1b49..986f88f69e83 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -9,18 +9,15 @@ namespace blender::render::hydra { class PreviewEngine : public FinalEngine { public: - using FinalEngine::FinalEngine; - static PreviewEngine *get_instance(RenderEngine *bl_engine, - const std::string &render_delegate_name); - static void schedule_free(); + static PreviewEngine *create(RenderEngine *bl_engine, const std::string &render_delegate_name); + static void free(); - void sync(Depsgraph *depsgraph, bContext *context) override; void render(Depsgraph *depsgraph) override; private: + using FinalEngine::FinalEngine; static double free_instance(uintptr_t uuid, void *user_data); - void update(RenderEngine *bl_engine, const std::string &render_delegate_name); void update_render_result(std::vector &pixels); /* Singleton class instance */ diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index 1c2c8ea0fc04..e87ed49d1cb6 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -111,7 +111,7 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args) engine = new ViewportEngine(bl_engine, render_delegate_id); } else if (STREQ(engine_type, "PREVIEW")) { - engine = PreviewEngine::get_instance(bl_engine, render_delegate_id); + engine = PreviewEngine::create(bl_engine, render_delegate_id); } else { if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { @@ -136,7 +136,7 @@ static PyObject *engine_free_func(PyObject * /*self*/, PyObject *args) Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); PreviewEngine *preview_engine = dynamic_cast(engine); if (preview_engine) { - PreviewEngine::schedule_free(); + PreviewEngine::free(); } else { delete engine; diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 9341aba64f7c..e666e181cc03 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -217,11 +217,6 @@ void DrawTexture::free() texture_ = nullptr; } -void ViewportEngine::sync(Depsgraph *depsgraph, bContext *context) -{ - scene_delegate_->populate(depsgraph, context); -} - void ViewportEngine::render(Depsgraph *depsgraph) { /* Empty function */ diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index 13406259c262..a21ccda581e8 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -34,7 +34,6 @@ class ViewportEngine : public Engine { public: using Engine::Engine; - void sync(Depsgraph *depsgraph, bContext *context) override; void render(Depsgraph *depsgraph) override; void render(Depsgraph *depsgraph, bContext *context); -- 2.30.2 From 5c278033409d84ccedbb97360ef27aff66e6c2c0 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Tue, 16 May 2023 13:34:18 +0300 Subject: [PATCH 09/10] Improvements --- source/blender/render/hydra/preview_engine.cc | 3 ++- source/blender/render/hydra/python.cc | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 06c05fd08f83..93dcd8dea492 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -27,6 +27,8 @@ PreviewEngine *PreviewEngine::create(RenderEngine *bl_engine, instance_->bl_engine_ = bl_engine; } + instance_->scene_delegate_->clear(); + if (BLI_timer_is_registered((uintptr_t)&instance_)) { /* Unregister timer while PreviewEngine is working */ BLI_timer_unregister((uintptr_t)&instance_); @@ -38,7 +40,6 @@ PreviewEngine *PreviewEngine::create(RenderEngine *bl_engine, void PreviewEngine::free() { instance_->render_delegate_->Stop(); - instance_->scene_delegate_->clear(); /* Register timer for schedule free PreviewEngine instance */ BLI_timer_register((uintptr_t)&instance_, free_instance, nullptr, nullptr, LIFETIME, true); diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index e87ed49d1cb6..335bbd283730 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -232,7 +232,7 @@ static PyObject *engine_set_sync_setting_func(PyObject * /*self*/, PyObject *arg Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); engine->set_sync_setting(key, get_setting_val(pyval)); - CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx: %s", engine, key); + CLOG_INFO(LOG_RENDER_HYDRA, 3, "Engine %016llx: %s", engine, key); Py_RETURN_NONE; } @@ -247,7 +247,7 @@ static PyObject *engine_set_render_setting_func(PyObject * /*self*/, PyObject *a Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); engine->set_render_setting(key, get_setting_val(pyval)); - CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx: %s", engine, key); + CLOG_INFO(LOG_RENDER_HYDRA, 3, "Engine %016llx: %s", engine, key); Py_RETURN_NONE; } -- 2.30.2 From e5d8d3f879a1003ca31bf3f8f8aca0be582be83e Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Fri, 19 May 2023 16:55:27 +0300 Subject: [PATCH 10/10] Added explanation of get_sync_settings() and get_render_settings() --- scripts/modules/bpy_hydra.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index f8002df55f5f..3dcc3207927c 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -23,14 +23,13 @@ class CustomHydraRenderEngine(HydraRenderEngine): def get_sync_settings(self, engine_type): return { - 'setting1': True, - 'setting2': "2", + 'MaterialXFilenameKey': "MaterialXFilename", } def get_render_settings(self, engine_type): return { - 'setting1': 1, - 'setting2': "2", + 'enableTinyPrimCulling': True, + 'maxLights': 8, } ``` """ @@ -80,9 +79,18 @@ class HydraRenderEngine(bpy.types.RenderEngine): pass def get_sync_settings(self, engine_type): + """ + Provide settings for Blender scene delegate. Available settings: + `MaterialXFilenameKey` - if provided then MaterialX file will be provided directly to render delegate + without converting to HdMaterialNetwork + """ return {} def get_render_settings(self, engine_type): + """ + Provide render settings for render delegate. List of settings should be available in render delegate + documentation or in `pxr.UsdImagingGL.Engine.GetRendererSettingsList()` + """ return {} # final render -- 2.30.2