From 2e97a39e96d8c443e3c84efc12ef38e159a4e8c0 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 25 Apr 2023 12:41:58 +0300 Subject: [PATCH 1/8] added render_delegate_name to engine and BlenderSceneDelegate --- source/blender/render/hydra/engine.cc | 7 ++++--- source/blender/render/hydra/engine.h | 4 +++- source/blender/render/hydra/final_engine.cc | 3 ++- source/blender/render/hydra/preview_engine.cc | 3 ++- .../render/hydra/scene_delegate/blender_scene_delegate.cc | 7 +++++-- .../render/hydra/scene_delegate/blender_scene_delegate.h | 5 ++++- source/blender/render/hydra/scene_delegate/world.cc | 2 +- source/blender/render/hydra/viewport_engine.cc | 3 ++- 8 files changed, 23 insertions(+), 11 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index fdcbf0903785..7b6d8573db98 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -17,12 +17,13 @@ namespace blender::render::hydra { CLG_LOGREF_DECLARE_GLOBAL(LOG_RENDER_HYDRA, "render.hydra"); -Engine::Engine(RenderEngine *bl_engine, const std::string &delegate_id) : bl_engine_(bl_engine) +Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name) + : bl_engine_(bl_engine), render_delegate_name(render_delegate_name) { pxr::HdRendererPluginRegistry ®istry = pxr::HdRendererPluginRegistry::GetInstance(); pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - render_delegate_ = registry.CreateRenderDelegate(pxr::TfToken(delegate_id)); + 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 * with PXR_ENABLE_VULKAN_SUPPORT=TRUE which is not possible now */ @@ -44,7 +45,7 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &delegate_id) : bl_eng 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_->GetRendererDisplayName() == "GL") { + 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 2bd760c1537d..04f48f788f42 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -25,7 +25,7 @@ extern struct CLG_LogRef *LOG_RENDER_HYDRA; /* EN - Engine */ class Engine { public: - Engine(RenderEngine *bl_engine, const std::string &render_delegate_id); + Engine(RenderEngine *bl_engine, const std::string &render_delegate_name); virtual ~Engine() = default; virtual void sync(Depsgraph *depsgraph, @@ -33,6 +33,8 @@ class Engine { pxr::HdRenderSettingsMap &render_settings) = 0; virtual void render(Depsgraph *depsgraph) = 0; + std::string render_delegate_name; + protected: float renderer_percent_done(); diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 768934b9ffca..e02d9f37b422 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -20,7 +20,8 @@ void FinalEngine::sync(Depsgraph *depsgraph, scene_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), - BlenderSceneDelegate::EngineType::FINAL); + BlenderSceneDelegate::EngineType::FINAL, + render_delegate_name); scene_delegate_->populate(depsgraph, context); for (auto const &setting : render_settings) { diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 4992c67f9636..760aa0080128 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -44,7 +44,8 @@ void PreviewEngine::sync(Depsgraph *depsgraph, scene_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), - BlenderSceneDelegate::EngineType::PREVIEW); + BlenderSceneDelegate::EngineType::PREVIEW, + render_delegate_name); } scene_delegate_->clear(); scene_delegate_->populate(depsgraph, context); 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 bb6614f2be8a..95f6f4fd4030 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -15,8 +15,11 @@ 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) - : HdSceneDelegate(parent_index, delegate_id), engine_type(engine_type) + BlenderSceneDelegate::EngineType engine_type, + std::string render_delegate_name) + : HdSceneDelegate(parent_index, delegate_id), + engine_type(engine_type), + render_delegate_name(render_delegate_name) { } 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 de0e67d6c904..9e0b2f6e7c81 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -28,7 +28,8 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id, - BlenderSceneDelegate::EngineType engine_type); + BlenderSceneDelegate::EngineType engine_type, + std::string render_delegate_name); ~BlenderSceneDelegate() override = default; /* Delegate methods */ @@ -56,6 +57,8 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { View3D *view3d = nullptr; Scene *scene = nullptr; + std::string render_delegate_name; + private: pxr::SdfPath prim_id(ID *id, const char *prefix) const; pxr::SdfPath object_prim_id(Object *object) const; diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 08f7a326db10..12f72c7084eb 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -162,7 +162,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_->GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName() == "RPR") { + if (scene_delegate_->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 802f2d9f3c1a..b4ad73321fba 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -225,7 +225,8 @@ void ViewportEngine::sync(Depsgraph *depsgraph, scene_delegate_ = std::make_unique( render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), - BlenderSceneDelegate::EngineType::VIEWPORT); + BlenderSceneDelegate::EngineType::VIEWPORT, + render_delegate_name); } scene_delegate_->populate(depsgraph, context); -- 2.30.2 From 987c707c7dacf1531d100b3c615f19566b2d3b16 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 25 Apr 2023 14:03:39 +0300 Subject: [PATCH 2/8] improved code with FinalEngine::get_resolution() --- source/blender/render/hydra/final_engine.cc | 27 ++++++--------------- source/blender/render/hydra/final_engine.h | 2 +- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index e02d9f37b422..597fbe0a5b10 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -39,13 +39,7 @@ void FinalEngine::render(Depsgraph *depsgraph) std::string layer_name = view_layer->name; const RenderData &r = scene->r; - pxr::GfVec4f border(0, 0, 1, 1); - if (r.mode & R_BORDER) { - border = pxr::GfVec4f(r.border.xmin, - r.border.ymin, - r.border.xmax - r.border.xmin, - r.border.ymax - r.border.ymin); - } + pxr::GfVec4f border = get_resolution(scene); pxr::GfVec2i image_res(r.xsch * r.size / 100, r.ysch * r.size / 100); pxr::GfVec2i res(int(image_res[0] * border[2]), int(image_res[1] * border[3])); pxr::GfCamera camera = @@ -106,16 +100,15 @@ void FinalEngine::render(Depsgraph *depsgraph) update_render_result(render_images, layer_name, res[0], res[1]); } -pxr::GfVec2i FinalEngine::get_resolution(Scene *scene) +pxr::GfVec4f FinalEngine::get_resolution(const Scene *scene) { - RenderData &r = scene->r; + const RenderData &r = scene->r; float border_w = 1.0, border_h = 1.0; if (r.mode & R_BORDER) { border_w = r.border.xmax - r.border.xmin; border_h = r.border.ymax - r.border.ymin; } - return pxr::GfVec2i(int(r.xsch * border_w * r.size / 100), - int(r.ysch * border_h * r.size / 100)); + return pxr::GfVec4f(r.border.xmin, r.border.ymin, border_w, border_h); } void FinalEngine::update_render_result(std::map> &render_images, @@ -157,15 +150,9 @@ void FinalEngineGL::render(Depsgraph *depsgraph) std::string layer_name = view_layer->name; const RenderData &r = scene->r; - pxr::GfVec4f border(0, 0, 1, 1); - if (r.mode & R_BORDER) { - border = pxr::GfVec4f(r.border.xmin, - r.border.ymin, - r.border.xmax - r.border.xmin, - r.border.ymax - r.border.ymin); - } - pxr::GfVec2i image_res = {r.xsch * r.size / 100, r.ysch * r.size / 100}; - pxr::GfVec2i res = {int(image_res[0] * border[2]), int(image_res[1] * border[3])}; + pxr::GfVec4f border = get_resolution(scene); + pxr::GfVec2i image_res(r.xsch * r.size / 100, r.ysch * r.size / 100); + pxr::GfVec2i res(int(image_res[0] * border[2]), int(image_res[1] * border[3])); pxr::GfCamera camera = CameraData(scene->camera, image_res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(border); diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 388500a38710..c153e07242c5 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -17,7 +17,7 @@ class FinalEngine : public Engine { virtual void render(Depsgraph *b_depsgraph) override; protected: - pxr::GfVec2i get_resolution(Scene *scene); + pxr::GfVec4f get_resolution(const Scene *scene); void update_render_result(std::map> &render_images, const std::string &layer_name, int width, -- 2.30.2 From 7484aefea29a9bed5008d712eb3f67c950c97dea Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 25 Apr 2023 15:50:46 +0300 Subject: [PATCH 3/8] moved duplicated code to FinalEngine::prepare_for_render --- source/blender/render/hydra/final_engine.cc | 95 +++++++-------------- source/blender/render/hydra/final_engine.h | 7 ++ 2 files changed, 39 insertions(+), 63 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 597fbe0a5b10..4894f47e02ee 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -31,42 +31,12 @@ void FinalEngine::sync(Depsgraph *depsgraph, void FinalEngine::render(Depsgraph *depsgraph) { - const Scene *scene = DEG_get_evaluated_scene(depsgraph); - const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph); - - std::string scene_name(MAX_ID_FULL_NAME, 0); - BKE_id_full_name_get(scene_name.data(), (ID *)scene, 0); - std::string layer_name = view_layer->name; - - const RenderData &r = scene->r; - pxr::GfVec4f border = get_resolution(scene); - pxr::GfVec2i image_res(r.xsch * r.size / 100, r.ysch * r.size / 100); - pxr::GfVec2i res(int(image_res[0] * border[2]), int(image_res[1] * border[3])); - pxr::GfCamera camera = - CameraData(scene->camera, image_res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(border); - - free_camera_delegate_->SetCamera(camera); - render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), - pxr::GfVec4d(0, 0, res[0], res[1])); - render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); - if (simple_light_task_delegate_) { - simple_light_task_delegate_->set_camera_path(free_camera_delegate_->GetCameraId()); - } - - pxr::HdTaskSharedPtrVector tasks; - if (simple_light_task_delegate_) { - tasks.push_back(simple_light_task_delegate_->get_task()); - } - tasks.push_back(render_task_delegate_->get_task()); - - std::map> render_images{ - {"Combined", std::vector(res[0] * res[1] * 4)}}; /* 4 - number of channels. */ - std::vector &pixels = render_images["Combined"]; + prepare_for_render(depsgraph); { /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - engine_->Execute(render_index_.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks_); } char elapsed_time[32]; @@ -84,7 +54,7 @@ void FinalEngine::render(Depsgraph *depsgraph) elapsed_time, sizeof(elapsed_time), PIL_check_seconds_timer() - time_begin); notify_status(percent_done / 100.0, - scene_name + ": " + layer_name, + scene_name_ + ": " + layer_name_, std::string("Render Time: ") + elapsed_time + " | Done: " + std::to_string(int(percent_done)) + "%"); @@ -92,12 +62,12 @@ void FinalEngine::render(Depsgraph *depsgraph) break; } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(render_images, layer_name, res[0], res[1]); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_.data()); + update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(render_images, layer_name, res[0], res[1]); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_.data()); + update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); } pxr::GfVec4f FinalEngine::get_resolution(const Scene *scene) @@ -140,46 +110,45 @@ void FinalEngine::notify_status(float progress, const std::string &title, const RE_engine_update_stats(bl_engine_, title.c_str(), info.c_str()); } -void FinalEngineGL::render(Depsgraph *depsgraph) +void FinalEngine::prepare_for_render(Depsgraph *depsgraph) { const Scene *scene = DEG_get_evaluated_scene(depsgraph); const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph); - std::string scene_name(MAX_ID_FULL_NAME, 0); - BKE_id_full_name_get(scene_name.data(), (ID *)scene, 0); - std::string layer_name = view_layer->name; + BKE_id_full_name_get(scene_name_.data(), (ID *)scene, 0); + layer_name_ = view_layer->name; const RenderData &r = scene->r; pxr::GfVec4f border = get_resolution(scene); pxr::GfVec2i image_res(r.xsch * r.size / 100, r.ysch * r.size / 100); - pxr::GfVec2i res(int(image_res[0] * border[2]), int(image_res[1] * border[3])); + resolution_ = pxr::GfVec2i(int(image_res[0] * border[2]), int(image_res[1] * border[3])); pxr::GfCamera camera = CameraData(scene->camera, image_res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(border); free_camera_delegate_->SetCamera(camera); - render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), - pxr::GfVec4d(0, 0, res[0], res[1])); + render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); + render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); + if (simple_light_task_delegate_) { simple_light_task_delegate_->set_camera_path(free_camera_delegate_->GetCameraId()); + tasks_.push_back(simple_light_task_delegate_->get_task()); } + tasks_.push_back(render_task_delegate_->get_task()); - pxr::HdTaskSharedPtrVector tasks; - if (simple_light_task_delegate_) { - /* TODO: Uncomment this and fix GL error: - * invalid operation, reported from void __cdecl - * pxrInternal_v0_22__pxrReserved__::HgiGLResourceBindings::BindResources(void) */ - // tasks.push_back(simple_light_task_delegate_->get_task()); - } - tasks.push_back(render_task_delegate_->get_task()); + render_images_.emplace( + "Combined", + std::vector(resolution_[0] * resolution_[1] * 4)); /* 4 - number of channels. */ + pixels_ = render_images_["Combined"]; +} - std::map> render_images{ - {"Combined", std::vector(res[0] * res[1] * 4)}}; /* 4 - number of channels. */ - std::vector &pixels = render_images["Combined"]; +void FinalEngineGL::render(Depsgraph *depsgraph) +{ + prepare_for_render(depsgraph); GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_hdyra_render_final"); GPUTexture *texture = GPU_texture_create_2d("tex_hydra_render_final", - res[0], - res[1], + resolution_[0], + resolution_[1], 1, GPU_RGBA32F, GPU_TEXTURE_USAGE_GENERAL, @@ -195,7 +164,7 @@ void FinalEngineGL::render(Depsgraph *depsgraph) { /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - engine_->Execute(render_index_.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks_); } char elapsed_time[32]; @@ -213,7 +182,7 @@ void FinalEngineGL::render(Depsgraph *depsgraph) elapsed_time, sizeof(elapsed_time), PIL_check_seconds_timer() - time_begin); notify_status(percent_done / 100.0, - scene_name + ": " + layer_name, + scene_name_ + ": " + layer_name_, std::string("Render Time: ") + elapsed_time + " | Done: " + std::to_string(int(percent_done)) + "%"); @@ -222,15 +191,15 @@ void FinalEngineGL::render(Depsgraph *depsgraph) } void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); - memcpy(pixels.data(), data, pixels.size() * sizeof(float)); + memcpy(pixels_.data(), data, pixels_.size() * sizeof(float)); MEM_freeN(data); - update_render_result(render_images, layer_name, res[0], res[1]); + update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); } void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); - memcpy(pixels.data(), data, pixels.size() * sizeof(float)); + memcpy(pixels_.data(), data, pixels_.size() * sizeof(float)); MEM_freeN(data); - update_render_result(render_images, layer_name, res[0], res[1]); + update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); GPU_framebuffer_free(framebuffer); GPU_texture_free(texture); diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index c153e07242c5..b9d30f2e5fe1 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -23,8 +23,15 @@ class FinalEngine : public Engine { int width, int height); void notify_status(float progress, const std::string &title, const std::string &info); + void prepare_for_render(Depsgraph *depsgraph); pxr::HdRenderSettingsMap render_settings_; + pxr::HdTaskSharedPtrVector tasks_; + std::string scene_name_; + std::string layer_name_; + std::map> render_images_; + pxr::GfVec2i resolution_; + std::vector pixels_; }; class FinalEngineGL : public FinalEngine { -- 2.30.2 From 550a29f5e4c9ac13bbf00ce5b600fc4e77ff4331 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 25 Apr 2023 16:36:55 +0300 Subject: [PATCH 4/8] fix FinalEngine::pixels member --- source/blender/render/hydra/final_engine.cc | 10 +++++----- source/blender/render/hydra/final_engine.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 4894f47e02ee..81260eccfaf3 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -62,11 +62,11 @@ void FinalEngine::render(Depsgraph *depsgraph) break; } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_.data()); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_.data()); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); } @@ -138,7 +138,7 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) render_images_.emplace( "Combined", std::vector(resolution_[0] * resolution_[1] * 4)); /* 4 - number of channels. */ - pixels_ = render_images_["Combined"]; + pixels_ = &render_images_["Combined"]; } void FinalEngineGL::render(Depsgraph *depsgraph) @@ -191,13 +191,13 @@ void FinalEngineGL::render(Depsgraph *depsgraph) } void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); - memcpy(pixels_.data(), data, pixels_.size() * sizeof(float)); + memcpy(pixels_->data(), data, pixels_->size() * sizeof(float)); MEM_freeN(data); update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); } void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); - memcpy(pixels_.data(), data, pixels_.size() * sizeof(float)); + memcpy(pixels_->data(), data, pixels_->size() * sizeof(float)); MEM_freeN(data); update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index b9d30f2e5fe1..3cce56f3dd56 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -31,7 +31,7 @@ class FinalEngine : public Engine { std::string layer_name_; std::map> render_images_; pxr::GfVec2i resolution_; - std::vector pixels_; + std::vector *pixels_ = nullptr; }; class FinalEngineGL : public FinalEngine { -- 2.30.2 From 6b25f1ea48eca20864133df3cd93436e0b3a087c Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 25 Apr 2023 17:31:54 +0300 Subject: [PATCH 5/8] PreviewEngine simplified with FinalEngine::prepare_for_render --- source/blender/render/hydra/preview_engine.cc | 39 +++++-------------- source/blender/render/hydra/preview_engine.h | 2 +- 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 760aa0080128..8411b6e4d894 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -57,33 +57,12 @@ void PreviewEngine::sync(Depsgraph *depsgraph, void PreviewEngine::render(Depsgraph *depsgraph) { - const Scene *scene = DEG_get_evaluated_scene(depsgraph); - const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph); - - std::string layer_name = view_layer->name; - pxr::GfVec2i res(scene->r.xsch, scene->r.ysch); - - pxr::GfCamera camera = - CameraData(scene->camera, res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(pxr::GfVec4f(0, 0, 1, 1)); - - free_camera_delegate_->SetCamera(camera); - render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), - pxr::GfVec4d(0, 0, res[0], res[1])); - render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); - - pxr::HdTaskSharedPtrVector tasks; - if (simple_light_task_delegate_) { - tasks.push_back(simple_light_task_delegate_->get_task()); - } - tasks.push_back(render_task_delegate_->get_task()); - - std::vector pixels = std::vector(res[0] * res[1] * - 4); /* 4 - number of channels. */ + prepare_for_render(depsgraph); { /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - engine_->Execute(render_index_.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks_); } while (true) { @@ -94,13 +73,13 @@ void PreviewEngine::render(Depsgraph *depsgraph) if (render_task_delegate_->is_converged()) { break; } - - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(layer_name, res[0], res[1], pixels); + + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); + update_render_result(layer_name_, resolution_[0], resolution_[1], pixels_); } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(layer_name, res[0], res[1], pixels); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); + update_render_result(layer_name_, resolution_[0], resolution_[1], pixels_); } double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) @@ -124,14 +103,14 @@ void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_de void PreviewEngine::update_render_result(const std::string &layer_name, int width, int height, - std::vector &pixels) + std::vector *pixels) { RenderResult *result = RE_engine_begin_result( bl_engine_, 0, 0, width, height, layer_name.c_str(), nullptr); RenderLayer *layer = (RenderLayer *)result->layers.first; RenderPass *pass = (RenderPass *)layer->passes.first; - memcpy(pass->rect, pixels.data(), sizeof(float) * pass->rectx * pass->recty * pass->channels); + memcpy(pass->rect, pixels->data(), sizeof(float) * pass->rectx * pass->recty * pass->channels); RE_engine_end_result(bl_engine_, result, false, false, false); } diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 64e969eca368..f20144692e97 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -29,7 +29,7 @@ class PreviewEngine : public FinalEngine { void update_render_result(const std::string &layer_name, int width, int height, - std::vector &pixels); + std::vector *pixels); }; } // namespace blender::render::hydra -- 2.30.2 From 6d39382b1c657d66770f79f69740a00da85099c6 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Tue, 25 Apr 2023 18:06:30 +0300 Subject: [PATCH 6/8] make format --- source/blender/render/hydra/final_engine.cc | 3 ++- source/blender/render/hydra/preview_engine.cc | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 81260eccfaf3..48f053da1010 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -126,7 +126,8 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) CameraData(scene->camera, image_res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(border); free_camera_delegate_->SetCamera(camera); - render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); + render_task_delegate_->set_camera_and_viewport( + free_camera_delegate_->GetCameraId(), pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); if (simple_light_task_delegate_) { diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 8411b6e4d894..e618b7f2ec7b 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -73,7 +73,7 @@ void PreviewEngine::render(Depsgraph *depsgraph) if (render_task_delegate_->is_converged()) { break; } - + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); update_render_result(layer_name_, resolution_[0], resolution_[1], pixels_); } -- 2.30.2 From 648e5e566cadad4d7777e47790ba4c22b3f6351f Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Wed, 26 Apr 2023 12:13:12 +0300 Subject: [PATCH 7/8] fix review comments --- source/blender/render/hydra/engine.cc | 6 +-- source/blender/render/hydra/engine.h | 3 +- source/blender/render/hydra/final_engine.cc | 38 +++++++++---------- source/blender/render/hydra/final_engine.h | 8 +--- source/blender/render/hydra/preview_engine.cc | 21 +++++----- source/blender/render/hydra/preview_engine.h | 5 +-- .../blender/render/hydra/viewport_engine.cc | 2 +- 7 files changed, 37 insertions(+), 46 deletions(-) diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index 7b6d8573db98..72d2dfebbd23 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -18,12 +18,12 @@ 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) + : bl_engine_(bl_engine), render_delegate_name_(render_delegate_name) { 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_)); /* Current USD (23.02) 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 +45,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 04f48f788f42..72e42589ceff 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -33,12 +33,11 @@ class Engine { pxr::HdRenderSettingsMap &render_settings) = 0; virtual void render(Depsgraph *depsgraph) = 0; - std::string render_delegate_name; - protected: float renderer_percent_done(); RenderEngine *bl_engine_; + std::string render_delegate_name_; /* 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 48f053da1010..16a92b8cd7f6 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -21,7 +21,7 @@ void FinalEngine::sync(Depsgraph *depsgraph, render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::FINAL, - render_delegate_name); + render_delegate_name_); scene_delegate_->populate(depsgraph, context); for (auto const &setting : render_settings) { @@ -33,6 +33,8 @@ void FinalEngine::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); + std::vector &pixels = render_images_["Combined"]; + { /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); @@ -62,15 +64,15 @@ void FinalEngine::render(Depsgraph *depsgraph) break; } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); - update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + update_render_result(); } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); - update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + update_render_result(); } -pxr::GfVec4f FinalEngine::get_resolution(const Scene *scene) +pxr::GfVec4f FinalEngine::get_render_border(const Scene *scene) { const RenderData &r = scene->r; float border_w = 1.0, border_h = 1.0; @@ -81,19 +83,16 @@ pxr::GfVec4f FinalEngine::get_resolution(const Scene *scene) return pxr::GfVec4f(r.border.xmin, r.border.ymin, border_w, border_h); } -void FinalEngine::update_render_result(std::map> &render_images, - const std::string &layer_name, - int width, - int height) +void FinalEngine::update_render_result() { RenderResult *result = RE_engine_begin_result( - bl_engine_, 0, 0, width, height, layer_name.c_str(), nullptr); + bl_engine_, 0, 0, resolution_[0], resolution_[1], layer_name_.c_str(), nullptr); /* TODO: only for the first render layer */ RenderLayer *layer = (RenderLayer *)result->layers.first; for (RenderPass *pass = (RenderPass *)layer->passes.first; pass != nullptr; pass = pass->next) { - auto it_image = render_images.find(pass->name); - if (it_image == render_images.end()) { + auto it_image = render_images_.find(pass->name); + if (it_image == render_images_.end()) { continue; } memcpy(pass->rect, @@ -119,7 +118,7 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) layer_name_ = view_layer->name; const RenderData &r = scene->r; - pxr::GfVec4f border = get_resolution(scene); + pxr::GfVec4f border = get_render_border(scene); pxr::GfVec2i image_res(r.xsch * r.size / 100, r.ysch * r.size / 100); resolution_ = pxr::GfVec2i(int(image_res[0] * border[2]), int(image_res[1] * border[3])); pxr::GfCamera camera = @@ -139,13 +138,14 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) render_images_.emplace( "Combined", std::vector(resolution_[0] * resolution_[1] * 4)); /* 4 - number of channels. */ - pixels_ = &render_images_["Combined"]; } void FinalEngineGL::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); + std::vector &pixels = render_images_["Combined"]; + GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_hdyra_render_final"); GPUTexture *texture = GPU_texture_create_2d("tex_hydra_render_final", resolution_[0], @@ -192,15 +192,15 @@ void FinalEngineGL::render(Depsgraph *depsgraph) } void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); - memcpy(pixels_->data(), data, pixels_->size() * sizeof(float)); + memcpy(pixels.data(), data, pixels.size() * sizeof(float)); MEM_freeN(data); - update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); + update_render_result(); } void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); - memcpy(pixels_->data(), data, pixels_->size() * sizeof(float)); + memcpy(pixels.data(), data, pixels.size() * sizeof(float)); MEM_freeN(data); - update_render_result(render_images_, layer_name_, resolution_[0], resolution_[1]); + update_render_result(); GPU_framebuffer_free(framebuffer); GPU_texture_free(texture); diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 3cce56f3dd56..2d3f78dfe594 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -17,11 +17,8 @@ class FinalEngine : public Engine { virtual void render(Depsgraph *b_depsgraph) override; protected: - pxr::GfVec4f get_resolution(const Scene *scene); - void update_render_result(std::map> &render_images, - const std::string &layer_name, - int width, - int height); + pxr::GfVec4f get_render_border(const Scene *scene); + void update_render_result(); void notify_status(float progress, const std::string &title, const std::string &info); void prepare_for_render(Depsgraph *depsgraph); @@ -31,7 +28,6 @@ class FinalEngine : public Engine { std::string layer_name_; std::map> render_images_; pxr::GfVec2i resolution_; - std::vector *pixels_ = nullptr; }; class FinalEngineGL : public FinalEngine { diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index e618b7f2ec7b..12b6cdaccb38 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -45,7 +45,7 @@ void PreviewEngine::sync(Depsgraph *depsgraph, render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::PREVIEW, - render_delegate_name); + render_delegate_name_); } scene_delegate_->clear(); scene_delegate_->populate(depsgraph, context); @@ -59,6 +59,8 @@ void PreviewEngine::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); + std::vector &pixels = render_images_["Combined"]; + { /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); @@ -74,12 +76,12 @@ void PreviewEngine::render(Depsgraph *depsgraph) break; } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); - update_render_result(layer_name_, resolution_[0], resolution_[1], pixels_); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + update_render_result(pixels); } - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels_->data()); - update_render_result(layer_name_, resolution_[0], resolution_[1], pixels_); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + update_render_result(pixels); } double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) @@ -100,17 +102,14 @@ void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_de /* TODO: recreate render_delegate when render_delegate_id is changed */ } -void PreviewEngine::update_render_result(const std::string &layer_name, - int width, - int height, - std::vector *pixels) +void PreviewEngine::update_render_result(std::vector &pixels) { RenderResult *result = RE_engine_begin_result( - bl_engine_, 0, 0, width, height, layer_name.c_str(), nullptr); + bl_engine_, 0, 0, resolution_[0], resolution_[1], layer_name_.c_str(), nullptr); RenderLayer *layer = (RenderLayer *)result->layers.first; RenderPass *pass = (RenderPass *)layer->passes.first; - memcpy(pass->rect, pixels->data(), sizeof(float) * pass->rectx * pass->recty * pass->channels); + memcpy(pass->rect, pixels.data(), sizeof(float) * pass->rectx * pass->recty * pass->channels); RE_engine_end_result(bl_engine_, result, false, false, false); } diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index f20144692e97..af613c939fab 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -26,10 +26,7 @@ class PreviewEngine : public FinalEngine { static std::unique_ptr instance_; void update(RenderEngine *bl_engine, const std::string &render_delegate_id); - void update_render_result(const std::string &layer_name, - int width, - int height, - std::vector *pixels); + void update_render_result(std::vector &pixels); }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index b4ad73321fba..1d2a68b88a29 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -226,7 +226,7 @@ void ViewportEngine::sync(Depsgraph *depsgraph, render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::VIEWPORT, - render_delegate_name); + render_delegate_name_); } scene_delegate_->populate(depsgraph, context); -- 2.30.2 From ce7bf9f4c2996cd98eab151c6a96e507178962a3 Mon Sep 17 00:00:00 2001 From: Bogdan Nagirniak Date: Wed, 26 Apr 2023 13:48:45 +0300 Subject: [PATCH 8/8] Moved get_render_border code to prepare_for_render --- source/blender/render/hydra/final_engine.cc | 19 +++++++------------ source/blender/render/hydra/final_engine.h | 1 - 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 16a92b8cd7f6..9f4bee9c6dd2 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -72,17 +72,6 @@ void FinalEngine::render(Depsgraph *depsgraph) update_render_result(); } -pxr::GfVec4f FinalEngine::get_render_border(const Scene *scene) -{ - const RenderData &r = scene->r; - float border_w = 1.0, border_h = 1.0; - if (r.mode & R_BORDER) { - border_w = r.border.xmax - r.border.xmin; - border_h = r.border.ymax - r.border.ymin; - } - return pxr::GfVec4f(r.border.xmin, r.border.ymin, border_w, border_h); -} - void FinalEngine::update_render_result() { RenderResult *result = RE_engine_begin_result( @@ -118,7 +107,13 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) layer_name_ = view_layer->name; const RenderData &r = scene->r; - pxr::GfVec4f border = get_render_border(scene); + pxr::GfVec4f border(0, 0, 1, 1); + if (r.mode & R_BORDER) { + border.Set(r.border.xmin, + r.border.ymin, + r.border.xmax - r.border.xmin, + r.border.ymax - r.border.ymin); + } pxr::GfVec2i image_res(r.xsch * r.size / 100, r.ysch * r.size / 100); resolution_ = pxr::GfVec2i(int(image_res[0] * border[2]), int(image_res[1] * border[3])); pxr::GfCamera camera = diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 2d3f78dfe594..585b4a36976e 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -17,7 +17,6 @@ class FinalEngine : public Engine { virtual void render(Depsgraph *b_depsgraph) override; protected: - pxr::GfVec4f get_render_border(const Scene *scene); void update_render_result(); void notify_status(float progress, const std::string &title, const std::string &info); void prepare_for_render(Depsgraph *depsgraph); -- 2.30.2