diff --git a/scripts/modules/bpy_hydra.py b/scripts/modules/bpy_hydra.py index 649351288d58..83684f4dd233 100644 --- a/scripts/modules/bpy_hydra.py +++ b/scripts/modules/bpy_hydra.py @@ -31,6 +31,10 @@ class CustomHydraRenderEngine(HydraRenderEngine): 'enableTinyPrimCulling': True, 'maxLights': 8, } + + def update_render_passes(self, scene, render_layer): + if render_layer.use_pass_z: + self.register_pass(scene, render_layer, 'Depth', 1, 'Z', 'VALUE') ``` """ @@ -141,6 +145,10 @@ class HydraRenderEngine(bpy.types.RenderEngine): _bpy_hydra.engine_view_draw(self.engine_ptr, depsgraph.as_pointer(), context.as_pointer()) + def update_render_passes(self, scene, render_layer): + """ Register supported AOVs for render. Must be overridden in child classes. """ + pass + def export_mtlx(material): """ Exports material to .mtlx file. It is called from Blender source code. """ diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index ebc6d91c9218..6e3c11376562 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -11,22 +11,28 @@ #include "GPU_framebuffer.h" #include "GPU_texture.h" #include "PIL_time.h" - +#include "BLI_map.hh" #include "IMB_imbuf_types.h" #include "camera.h" #include "final_engine.h" +PXR_NAMESPACE_OPEN_SCOPE +TF_DEFINE_PRIVATE_TOKENS(tokens_, (worldCoordinate)); +PXR_NAMESPACE_CLOSE_SCOPE + namespace blender::render::hydra { void FinalEngine::render(Depsgraph *depsgraph) { prepare_for_render(depsgraph); - render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); + for (auto &it : render_images_) { + render_task_delegate_->set_renderer_aov(aovs_map_.lookup(it.first)); + } + engine_->Execute(render_index_.get(), &tasks_); - std::vector &pixels = render_images_["Combined"]; char elapsed_time[32]; double time_begin = PIL_check_seconds_timer(); float percent_done = 0.0; @@ -47,12 +53,14 @@ void FinalEngine::render(Depsgraph *depsgraph) if (render_task_delegate_->is_converged()) { break; } - - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + for (auto &it : render_images_) { + render_task_delegate_->get_renderer_aov_data(aovs_map_.lookup(it.first), it.second.data()); + } update_render_result(); } - - render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + for (auto &it : render_images_) { + render_task_delegate_->get_renderer_aov_data(aovs_map_.lookup(it.first), it.second.data()); + } update_render_result(); } @@ -111,11 +119,25 @@ void FinalEngine::prepare_for_render(Depsgraph *depsgraph) free_camera_delegate_->GetCameraId(), pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); tasks_ = light_tasks_delegate_->get_tasks(); } - tasks_.push_back(render_task_delegate_->get_task()); + for (auto &task : render_task_delegate_->get_tasks()) { + tasks_.push_back(task); + } - render_images_.emplace( - "Combined", - std::vector(resolution_[0] * resolution_[1] * 4)); /* 4 - number of channels. */ + aovs_map_.add_new("Combined", pxr::HdAovTokens->color); + aovs_map_.add_new("Depth", pxr::HdAovTokens->depth); + + if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) { + aovs_map_.add_new("Normal", pxr::HdAovTokens->normal); + aovs_map_.add_new("Position", pxr::tokens_->worldCoordinate); + } + + RenderResult *result = RE_engine_get_result(bl_engine_); + RenderLayer *layer = (RenderLayer *)result->layers.first; + for (RenderPass *pass = (RenderPass *)layer->passes.first; pass != nullptr; pass = pass->next) { + if (aovs_map_.lookup_key_ptr(pass->name)) { + render_images_.emplace(pass->name, std::vector(pass->rectx * pass->recty * pass->channels)); + } + } } void FinalEngineGPU::render(Depsgraph *depsgraph) @@ -134,7 +156,7 @@ void FinalEngineGPU::render(Depsgraph *depsgraph) resolution_[0], resolution_[1], 1, - GPU_DEPTH32F_STENCIL8, + GPU_DEPTH_COMPONENT32F, GPU_TEXTURE_USAGE_GENERAL, nullptr); GPU_texture_filter_mode(tex_color, true); @@ -159,10 +181,10 @@ void FinalEngineGPU::render(Depsgraph *depsgraph) engine_->Execute(render_index_.get(), &tasks_); - std::vector &pixels = render_images_["Combined"]; char elapsed_time[32]; double time_begin = PIL_check_seconds_timer(); float percent_done = 0.0; + void *data; while (true) { if (RE_engine_test_break(bl_engine_)) { @@ -182,17 +204,38 @@ void FinalEngineGPU::render(Depsgraph *depsgraph) if (render_task_delegate_->is_converged()) { break; } + + for (auto &it : render_images_) { + if (it.first == "Comdined") { + data = GPU_texture_read(tex_color, GPU_DATA_FLOAT, 0); + memcpy(it.second.data(), data, it.second.size() * sizeof(float)); + MEM_freeN(data); + update_render_result(); - void *data = GPU_texture_read(tex_color, GPU_DATA_FLOAT, 0); - memcpy(pixels.data(), data, pixels.size() * sizeof(float)); - MEM_freeN(data); - update_render_result(); + } + if (it.first == "Depth") { + data = GPU_texture_read(tex_depth, GPU_DATA_FLOAT, 0); + memcpy(it.second.data(), data, it.second.size() * sizeof(float)); + MEM_freeN(data); + update_render_result(); + } + } } - void *data = GPU_texture_read(tex_color, GPU_DATA_FLOAT, 0); - memcpy(pixels.data(), data, pixels.size() * sizeof(float)); - MEM_freeN(data); - update_render_result(); + for (auto &it : render_images_) { + if (it.first == "Combined") { + data = GPU_texture_read(tex_color, GPU_DATA_FLOAT, 0); + memcpy(it.second.data(), data, it.second.size() * sizeof(float)); + MEM_freeN(data); + update_render_result(); + } + if (it.first == "Depth") { + data = GPU_texture_read(tex_depth, GPU_DATA_FLOAT, 0); + memcpy(it.second.data(), data, it.second.size() * sizeof(float)); + MEM_freeN(data); + update_render_result(); + } + } glDeleteVertexArrays(1, &VAO); GPU_framebuffer_free(framebuffer); diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 2ab44bbadaa5..5ef78693d2a5 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -24,6 +24,7 @@ class FinalEngine : public Engine { std::string layer_name_; std::map> render_images_; pxr::GfVec2i resolution_; + Map aovs_map_; }; class FinalEngineGPU : public FinalEngine { diff --git a/source/blender/render/hydra/render_task_delegate.cc b/source/blender/render/hydra/render_task_delegate.cc index 5351be086ca2..677bdb188ed3 100644 --- a/source/blender/render/hydra/render_task_delegate.cc +++ b/source/blender/render/hydra/render_task_delegate.cc @@ -15,20 +15,28 @@ RenderTaskDelegate::RenderTaskDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id) : pxr::HdSceneDelegate(parent_index, delegate_id) { - pxr::SdfPath render_task_id = get_task_id(); - GetRenderIndex().InsertTask(this, render_task_id); - GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id, + render_task_id_ = GetDelegateID().AppendElementString("task"); + GetRenderIndex().InsertTask(this, render_task_id_); + GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id_, pxr::HdChangeTracker::DirtyCollection); - GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id, + GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id_, pxr::HdChangeTracker::DirtyRenderTags); + task_params_.enableLighting = true; task_params_.alphaThreshold = 0.1f; + + aov_task_id_ = GetDelegateID().AppendElementString("aovTask"); + GetRenderIndex().InsertTask(this, aov_task_id_); + GetRenderIndex().GetChangeTracker().MarkTaskDirty(aov_task_id_, + pxr::HdChangeTracker::DirtyCollection); + GetRenderIndex().GetChangeTracker().MarkTaskDirty(aov_task_id_, + pxr::HdChangeTracker::DirtyRenderTags); } -pxr::SdfPath RenderTaskDelegate::get_task_id() const +pxr::HdTaskSharedPtrVector RenderTaskDelegate::get_tasks() { - return GetDelegateID().AppendElementString("task"); + return {GetRenderIndex().GetTask(aov_task_id_), GetRenderIndex().GetTask(render_task_id_)}; } pxr::SdfPath RenderTaskDelegate::get_aov_id(pxr::TfToken const &aov) const @@ -38,7 +46,7 @@ pxr::SdfPath RenderTaskDelegate::get_aov_id(pxr::TfToken const &aov) const bool RenderTaskDelegate::is_converged() { - pxr::HdTaskSharedPtr renderTask = GetRenderIndex().GetTask(get_task_id()); + pxr::HdTaskSharedPtr renderTask = GetRenderIndex().GetTask(render_task_id_); return ((pxr::HdxRenderTask &)*renderTask).IsConverged(); } @@ -64,7 +72,7 @@ void RenderTaskDelegate::set_renderer_aov(pxr::TfToken const &aov) binding.aovSettings = aov_desc.aovSettings; task_params_.aovBindings.push_back(binding); - GetRenderIndex().GetChangeTracker().MarkTaskDirty(get_task_id(), + GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id_, pxr::HdChangeTracker::DirtyParams); } else if (buffer_descriptors_[buffer_id] != desc) { @@ -90,26 +98,27 @@ void RenderTaskDelegate::get_renderer_aov_data(pxr::TfToken const &aov, void *da buffer->Unmap(); } -pxr::HdTaskSharedPtr RenderTaskDelegate::get_task() -{ - return GetRenderIndex().GetTask(get_task_id()); -} - void RenderTaskDelegate::set_camera_and_viewport(pxr::SdfPath const &camera_id, pxr::GfVec4d const &viewport) { if (task_params_.viewport != viewport || task_params_.camera != camera_id) { task_params_.viewport = viewport; task_params_.camera = camera_id; - GetRenderIndex().GetChangeTracker().MarkTaskDirty(get_task_id(), + GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id_, pxr::HdChangeTracker::DirtyParams); } } -pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const & /*id*/, pxr::TfToken const &key) +pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) { if (key == pxr::HdTokens->params) { - return pxr::VtValue(task_params_); + if (id == aov_task_id_) { + aov_task_params_.aovName = pxr::HdAovTokens->depth; + return pxr::VtValue(aov_task_params_); + } + else if (id == render_task_id_) { + return pxr::VtValue(task_params_); + } } if (key == pxr::HdTokens->collection) { pxr::HdRprimCollection rprim_collection(pxr::HdTokens->geometry, diff --git a/source/blender/render/hydra/render_task_delegate.h b/source/blender/render/hydra/render_task_delegate.h index f8a1a956816c..1c59eb6b516c 100644 --- a/source/blender/render/hydra/render_task_delegate.h +++ b/source/blender/render/hydra/render_task_delegate.h @@ -5,6 +5,7 @@ #include #include +#include namespace blender::render::hydra { @@ -13,7 +14,7 @@ class RenderTaskDelegate : public pxr::HdSceneDelegate { RenderTaskDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id); ~RenderTaskDelegate() override = default; - pxr::SdfPath get_task_id() const; + pxr::HdTaskSharedPtrVector get_tasks(); pxr::SdfPath get_aov_id(pxr::TfToken const &aov) const; bool is_converged(); @@ -21,7 +22,6 @@ class RenderTaskDelegate : public pxr::HdSceneDelegate { pxr::HdRenderBuffer *get_renderer_aov(pxr::TfToken const &id); void get_renderer_aov_data(pxr::TfToken const &id, void *buf); - pxr::HdTaskSharedPtr get_task(); void set_camera_and_viewport(pxr::SdfPath const &cameraId, pxr::GfVec4d const &viewport); /* Delegate methods */ @@ -30,6 +30,9 @@ class RenderTaskDelegate : public pxr::HdSceneDelegate { pxr::TfTokenVector GetTaskRenderTags(pxr::SdfPath const &taskId) override; private: + pxr::SdfPath aov_task_id_; + pxr::SdfPath render_task_id_; + pxr::HdxVisualizeAovTaskParams aov_task_params_; pxr::HdxRenderTaskParams task_params_; pxr::TfHashMap buffer_descriptors_; diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index b16c4ad776ae..685b0d27a0ca 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -256,8 +256,10 @@ void ViewportEngine::render(Depsgraph * /* depsgraph */, bContext *context) if (light_tasks_delegate_) { tasks = light_tasks_delegate_->get_tasks(); } - tasks.push_back(render_task_delegate_->get_task()); - + for (auto &task : render_task_delegate_->get_tasks()) { + tasks.push_back(task); + } + engine_->Execute(render_index_.get(), &tasks); if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) {