diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index fdcbf0903785..72d2dfebbd23 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..72e42589ceff 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, @@ -37,6 +37,7 @@ class Engine { 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 768934b9ffca..9f4bee9c6dd2 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) { @@ -30,48 +31,14 @@ 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); + prepare_for_render(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(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::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"]; + 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(); - engine_->Execute(render_index_.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks_); } char elapsed_time[32]; @@ -89,7 +56,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)) + "%"); @@ -98,38 +65,23 @@ void FinalEngine::render(Depsgraph *depsgraph) } render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(render_images, layer_name, res[0], res[1]); + update_render_result(); } render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(render_images, layer_name, res[0], res[1]); + update_render_result(); } -pxr::GfVec2i FinalEngine::get_resolution(Scene *scene) -{ - 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)); -} - -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, @@ -146,52 +98,53 @@ 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(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); + 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}; - pxr::GfVec2i res = {int(image_res[0] * border[2]), int(image_res[1] * border[3])}; + 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 = 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. */ +} - 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); + + 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", - res[0], - res[1], + resolution_[0], + resolution_[1], 1, GPU_RGBA32F, GPU_TEXTURE_USAGE_GENERAL, @@ -207,7 +160,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]; @@ -225,7 +178,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)) + "%"); @@ -236,13 +189,13 @@ void FinalEngineGL::render(Depsgraph *depsgraph) void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); 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(); } void *data = GPU_texture_read(texture, GPU_DATA_FLOAT, 0); 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(); 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 388500a38710..585b4a36976e 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -17,14 +17,16 @@ class FinalEngine : public Engine { virtual void render(Depsgraph *b_depsgraph) override; protected: - pxr::GfVec2i get_resolution(Scene *scene); - void update_render_result(std::map> &render_images, - const std::string &layer_name, - int width, - int height); + void update_render_result(); 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_; }; class FinalEngineGL : public FinalEngine { diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index 4992c67f9636..12b6cdaccb38 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); @@ -56,33 +57,14 @@ 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); + prepare_for_render(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. */ + 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(); - engine_->Execute(render_index_.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks_); } while (true) { @@ -95,11 +77,11 @@ void PreviewEngine::render(Depsgraph *depsgraph) } render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(layer_name, res[0], res[1], pixels); + update_render_result(pixels); } render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); - update_render_result(layer_name, res[0], res[1], pixels); + update_render_result(pixels); } double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) @@ -120,13 +102,10 @@ 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; diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 64e969eca368..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/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..1d2a68b88a29 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);