Hydra tasks refactor #73

Merged
Bogdan Nagirniak merged 8 commits from hydra-tasks-refactor into hydra-render 2023-07-25 22:13:33 +02:00
5 changed files with 83 additions and 92 deletions
Showing only changes of commit 442608fa27 - Show all commits

View File

@ -22,7 +22,7 @@ namespace blender::render::hydra {
void FinalEngine::render(Depsgraph *depsgraph)
{
prepare_for_render(depsgraph);
render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color);
render_task_delegate_->add_aov(pxr::HdAovTokens->color);
engine_->Execute(render_index_.get(), &tasks_);
@ -48,11 +48,11 @@ void FinalEngine::render(Depsgraph *depsgraph)
break;
}
render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data());
render_task_delegate_->get_aov_data(pxr::HdAovTokens->color, pixels.data());
update_render_result();
}
render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data());
render_task_delegate_->get_aov_data(pxr::HdAovTokens->color, pixels.data());
update_render_result();
}
@ -104,14 +104,14 @@ 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(free_camera_delegate_->GetCameraId());
render_task_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1]));
if (light_tasks_delegate_) {
light_tasks_delegate_->set_camera_and_viewport(
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());
tasks_.push_back(render_task_delegate_->task());
render_images_.emplace(
"Combined",

View File

@ -15,7 +15,7 @@ namespace blender::render::hydra {
void PreviewEngine::render(Depsgraph *depsgraph)
{
prepare_for_render(depsgraph);
render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color);
render_task_delegate_->add_aov(pxr::HdAovTokens->color);
engine_->Execute(render_index_.get(), &tasks_);
@ -29,11 +29,11 @@ void PreviewEngine::render(Depsgraph *depsgraph)
break;
}
render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data());
render_task_delegate_->get_aov_data(pxr::HdAovTokens->color, pixels.data());
update_render_result(pixels);
}
render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data());
render_task_delegate_->get_aov_data(pxr::HdAovTokens->color, pixels.data());
update_render_result(pixels);
}

View File

@ -1,8 +1,6 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <iostream>
#include <pxr/imaging/hd/renderBuffer.h>
#include <pxr/imaging/hd/renderDelegate.h>
#include <pxr/imaging/hdx/renderTask.h>
@ -15,74 +13,58 @@ 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<pxr::HdxRenderTask>(this, render_task_id);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id,
pxr::HdChangeTracker::DirtyCollection);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id,
pxr::HdChangeTracker::DirtyRenderTags);
task_id_ = GetDelegateID().AppendElementString("task");
GetRenderIndex().InsertTask<pxr::HdxRenderTask>(this, task_id_);
task_params_.enableLighting = true;
task_params_.alphaThreshold = 0.1f;
}
pxr::SdfPath RenderTaskDelegate::get_task_id() const
{
return GetDelegateID().AppendElementString("task");
}
pxr::SdfPath RenderTaskDelegate::get_aov_id(pxr::TfToken const &aov) const
pxr::SdfPath RenderTaskDelegate::buffer_id(pxr::TfToken const &aov) const
{
return GetDelegateID().AppendElementString("aov_" + aov.GetString());
}
bool RenderTaskDelegate::is_converged()
{
pxr::HdTaskSharedPtr renderTask = GetRenderIndex().GetTask(get_task_id());
return ((pxr::HdxRenderTask &)*renderTask).IsConverged();
return ((pxr::HdxRenderTask *)task().get())->IsConverged();
}
void RenderTaskDelegate::set_renderer_aov(pxr::TfToken const &aov)
void RenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
{
pxr::HdAovDescriptor aov_desc = GetRenderIndex().GetRenderDelegate()->GetDefaultAovDescriptor(
aov);
pxr::HdRenderBufferDescriptor desc(
pxr::GfVec3i(task_params_.viewport[2], task_params_.viewport[3], 1),
auto &render_index = GetRenderIndex();
pxr::SdfPath buf_id = buffer_id(aov_key);
if (buffer_descriptors_.find(buf_id) != buffer_descriptors_.end()) {
return;
}
pxr::HdAovDescriptor aov_desc = render_index.GetRenderDelegate()->GetDefaultAovDescriptor(
aov_key);
int w = task_params_.viewport[2] - task_params_.viewport[0];
int h = task_params_.viewport[3] - task_params_.viewport[1];
render_index.InsertBprim(pxr::HdPrimTypeTokens->renderBuffer, this, buf_id);
buffer_descriptors_[buf_id] = pxr::HdRenderBufferDescriptor(
pxr::GfVec3i(w, h, 1),
aov_desc.format,
aov_desc.multiSampled);
pxr::SdfPath buffer_id = get_aov_id(aov);
if (buffer_descriptors_.find(buffer_id) == buffer_descriptors_.end()) {
GetRenderIndex().InsertBprim(pxr::HdPrimTypeTokens->renderBuffer, this, buffer_id);
buffer_descriptors_[buffer_id] = desc;
GetRenderIndex().GetChangeTracker().MarkBprimDirty(buffer_id,
pxr::HdRenderBuffer::DirtyDescription);
pxr::HdRenderPassAovBinding binding;
binding.aovName = aov;
binding.renderBufferId = buffer_id;
binding.aovName = aov_key;
binding.renderBufferId = buf_id;
binding.aovSettings = aov_desc.aovSettings;
task_params_.aovBindings.push_back(binding);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(get_task_id(),
pxr::HdChangeTracker::DirtyParams);
}
else if (buffer_descriptors_[buffer_id] != desc) {
buffer_descriptors_[buffer_id] = desc;
GetRenderIndex().GetChangeTracker().MarkBprimDirty(buffer_id,
pxr::HdRenderBuffer::DirtyDescription);
}
render_index.GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams);
}
pxr::HdRenderBuffer *RenderTaskDelegate::get_renderer_aov(pxr::TfToken const &aov)
pxr::HdRenderBuffer *RenderTaskDelegate::get_aov(pxr::TfToken const &aov_key)
{
return (pxr::HdRenderBuffer *)(GetRenderIndex().GetBprim(pxr::HdPrimTypeTokens->renderBuffer,
get_aov_id(aov)));
return (pxr::HdRenderBuffer *)GetRenderIndex().GetBprim(pxr::HdPrimTypeTokens->renderBuffer,
buffer_id(aov_key));
}
void RenderTaskDelegate::get_renderer_aov_data(pxr::TfToken const &aov, void *data)
void RenderTaskDelegate::get_aov_data(pxr::TfToken const &aov, void *data)
{
pxr::HdRenderBuffer *buffer = get_renderer_aov(aov);
pxr::HdRenderBuffer *buffer = get_aov(aov);
void *buf_data = buffer->Map();
memcpy(data,
buf_data,
@ -90,19 +72,32 @@ void RenderTaskDelegate::get_renderer_aov_data(pxr::TfToken const &aov, void *da
buffer->Unmap();
}
pxr::HdTaskSharedPtr RenderTaskDelegate::get_task()
pxr::HdTaskSharedPtr RenderTaskDelegate::task()
{
return GetRenderIndex().GetTask(get_task_id());
return GetRenderIndex().GetTask(task_id_);
}
void RenderTaskDelegate::set_camera_and_viewport(pxr::SdfPath const &camera_id,
pxr::GfVec4d const &viewport)
void RenderTaskDelegate::set_camera(pxr::SdfPath const &camera_id)
{
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(),
pxr::HdChangeTracker::DirtyParams);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams);
}
void RenderTaskDelegate::set_viewport(pxr::GfVec4d const &viewport)
{
if (task_params_.viewport == viewport) {
return;
}
auto &render_index = GetRenderIndex();
task_params_.viewport = viewport;
render_index.GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams);
int w = viewport[2] - viewport[0];
int h = viewport[3] - viewport[1];
for (auto &it : buffer_descriptors_) {
it.second.dimensions = pxr::GfVec3i(w, h, 1);
render_index.GetChangeTracker().MarkBprimDirty(it.first,
pxr::HdRenderBuffer::DirtyDescription);
}
}
@ -112,12 +107,8 @@ pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const & /*id*/, pxr::TfToken c
return pxr::VtValue(task_params_);
}
if (key == pxr::HdTokens->collection) {
pxr::HdRprimCollection rprim_collection(pxr::HdTokens->geometry,
pxr::HdReprSelector(pxr::HdReprTokens->smoothHull),
false,
pxr::TfToken());
rprim_collection.SetRootPath(pxr::SdfPath::AbsoluteRootPath());
return pxr::VtValue(rprim_collection);
return pxr::VtValue(pxr::HdRprimCollection(
pxr::HdTokens->geometry, pxr::HdReprSelector(pxr::HdReprTokens->smoothHull)));
}
return pxr::VtValue();
}
@ -127,7 +118,7 @@ pxr::HdRenderBufferDescriptor RenderTaskDelegate::GetRenderBufferDescriptor(pxr:
return buffer_descriptors_[id];
}
pxr::TfTokenVector RenderTaskDelegate::GetTaskRenderTags(pxr::SdfPath const & /*task_id*/)
pxr::TfTokenVector RenderTaskDelegate::GetTaskRenderTags(pxr::SdfPath const & /*id*/)
{
return {pxr::HdRenderTagTokens->geometry};
}

View File

@ -13,23 +13,23 @@ 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::SdfPath get_aov_id(pxr::TfToken const &aov) const;
bool is_converged();
void set_renderer_aov(pxr::TfToken const &aovId);
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 */
pxr::VtValue Get(pxr::SdfPath const &id, pxr::TfToken const &key) override;
pxr::TfTokenVector GetTaskRenderTags(pxr::SdfPath const &id) override;
pxr::HdRenderBufferDescriptor GetRenderBufferDescriptor(pxr::SdfPath const &id) override;
pxr::TfTokenVector GetTaskRenderTags(pxr::SdfPath const &taskId) override;
pxr::HdTaskSharedPtr task();
void set_camera(pxr::SdfPath const &camera_id);
void set_viewport(pxr::GfVec4d const &viewport);
void add_aov(pxr::TfToken const &aov_key);
pxr::HdRenderBuffer *get_aov(pxr::TfToken const &aov_key);
void get_aov_data(pxr::TfToken const &aov_key, void *data);
bool is_converged();
private:
pxr::SdfPath buffer_id(pxr::TfToken const &aov) const;
pxr::SdfPath task_id_;
pxr::HdxRenderTaskParams task_params_;
pxr::TfHashMap<pxr::SdfPath, pxr::HdRenderBufferDescriptor, pxr::SdfPath::Hash>
buffer_descriptors_;

View File

@ -231,8 +231,8 @@ void ViewportEngine::render(Depsgraph * /* depsgraph */, bContext *context)
pxr::GfCamera gf_camera = view_settings.gf_camera();
free_camera_delegate_->SetCamera(gf_camera);
render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(),
pxr::GfVec4d(view_settings.border[0],
render_task_delegate_->set_camera(free_camera_delegate_->GetCameraId());
render_task_delegate_->set_viewport(pxr::GfVec4d(view_settings.border[0],
view_settings.border[1],
view_settings.border[2],
view_settings.border[3]));
@ -246,7 +246,7 @@ void ViewportEngine::render(Depsgraph * /* depsgraph */, bContext *context)
}
if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) {
render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color);
render_task_delegate_->add_aov(pxr::HdAovTokens->color);
}
GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_IMAGE);
@ -256,12 +256,12 @@ 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());
tasks.push_back(render_task_delegate_->task());
engine_->Execute(render_index_.get(), &tasks);
if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) {
draw_texture_.set_buffer(render_task_delegate_->get_renderer_aov(pxr::HdAovTokens->color));
draw_texture_.set_buffer(render_task_delegate_->get_aov(pxr::HdAovTokens->color));
draw_texture_.draw(shader, view_settings.border[0], view_settings.border[1]);
}