Hydra code cleanup #81

Merged
Bogdan Nagirniak merged 7 commits from hydra-code-cleanup into hydra-render 2023-08-01 21:35:28 +02:00
13 changed files with 135 additions and 62 deletions
Showing only changes of commit 18d1fbc2bf - Show all commits

View File

@ -28,6 +28,7 @@ class CustomHydraRenderEngine(bpy.types.HydraRenderEngine):
return {
'myBoolean': True,
'myValue': 8,
'aovToken:Depth': "depth",
}
# Settings used by the synchronization process.
@ -43,6 +44,11 @@ class CustomHydraRenderEngine(bpy.types.HydraRenderEngine):
super().update(data, depsgraph)
# Do extra work here
def update_render_passes(self, scene, render_layer):
if render_layer.use_pass_z:
self.register_pass(scene, render_layer, 'Depth', 1, 'Z', 'VALUE')
# Registration
def register():
bpy.utils.register_class(CustomHydraRenderEngine)

View File

@ -1283,6 +1283,7 @@ class HydraRenderEngine(RenderEngine):
# Final render.
def update(self, data, depsgraph):
import _bpy_hydra
import bpy
engine_type = 'PREVIEW' if self.is_preview else 'FINAL'
if not self.engine_ptr:
@ -1293,7 +1294,7 @@ class HydraRenderEngine(RenderEngine):
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_update(self.engine_ptr, depsgraph, None)
_bpy_hydra.engine_update(self.engine_ptr, depsgraph, bpy.context)
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)

View File

@ -45,11 +45,11 @@ class InstancerData : public IdData {
void update_double_sided(MaterialData *mat_data);
/* Following update functions are working together:
pre_update()
update_instance()
update_instance()
...
post_update() */
* pre_update()
* update_instance()
* update_instance()
* ...
* post_update() */
void pre_update();
void update_instance(DupliObject *dupli);
void post_update();

View File

@ -33,8 +33,6 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name)
pxr::TF_PY_ALLOW_THREADS_IN_SCOPE();
/* 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 */
if (GPU_backend_get_type() == GPU_BACKEND_VULKAN) {
BLI_setenv("HGI_ENABLE_VULKAN", "1");
}
@ -122,7 +120,13 @@ void Engine::set_sync_setting(const std::string &key, const pxr::VtValue &val)
void Engine::set_render_setting(const std::string &key, const pxr::VtValue &val)
{
if (STRPREFIX(key.c_str(), "aovToken:")) {
render_delegate_settings_.aovs.add_new(key.substr(key.find(":") + 1),
pxr::TfToken(val.UncheckedGet<std::string>()));
}
else {
render_delegate_->SetRenderSetting(pxr::TfToken(key), val);
}
}
float Engine::renderer_percent_done()

View File

@ -29,6 +29,10 @@ namespace blender::render::hydra {
extern struct CLG_LogRef *LOG_HYDRA_RENDER;
struct RenderDelegateSettings {
Map<std::string, pxr::TfToken> aovs;
};
class Engine {
protected:
std::string render_delegate_name_;
@ -47,6 +51,7 @@ class Engine {
std::unique_ptr<io::hydra::HydraSceneDelegate> hydra_scene_delegate_;
std::unique_ptr<io::hydra::USDSceneDelegate> usd_scene_delegate_;
RenderDelegateSettings render_delegate_settings_;
std::unique_ptr<RenderTaskDelegate> render_task_delegate_;
std::unique_ptr<pxr::HdxFreeCameraSceneDelegate> free_camera_delegate_;
std::unique_ptr<LightTasksDelegate> light_tasks_delegate_;

View File

@ -51,9 +51,18 @@ void FinalEngine::render()
}
render_task_delegate_->add_aov(pxr::HdAovTokens->color);
if (bl_engine_->type->flag & RE_USE_GPU_CONTEXT) {
render_task_delegate_->add_aov(pxr::HdAovTokens->depth);
}
render_task_delegate_->bind();
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) {
pxr::TfToken *aov_token = render_delegate_settings_.aovs.lookup_ptr(pass->name);
if (aov_token) {
render_task_delegate_->add_aov(*aov_token);
}
}
pxr::HdTaskSharedPtrVector tasks;
if (light_tasks_delegate_) {
@ -63,6 +72,9 @@ void FinalEngine::render()
tasks.push_back(light_tasks_delegate_->simple_task());
}
tasks.push_back(render_task_delegate_->task());
render_task_delegate_->bind();
engine_->Execute(render_index_.get(), &tasks);
char elapsed_time[32];
@ -108,14 +120,10 @@ void FinalEngine::update_render_result(int width, int height, const char *layer_
if (rlayer) {
LISTBASE_FOREACH (RenderPass *, rpass, &rlayer->passes) {
pxr::TfToken aov_key;
if (STREQ(rpass->name, "Combined")) {
aov_key = pxr::HdAovTokens->color;
pxr::TfToken *aov_token = render_delegate_settings_.aovs.lookup_ptr(rpass->name);
if (aov_token) {
render_task_delegate_->read_aov(*aov_token, rpass->ibuf->float_buffer.data);
}
else if (STREQ(rpass->name, "Depth")) {
aov_key = pxr::HdAovTokens->depth;
}
render_task_delegate_->read_aov(aov_key, rpass->ibuf->float_buffer.data);
}
}

View File

@ -2,6 +2,7 @@
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation */
#include "light_tasks_delegate.h"
#include "engine.h"
namespace blender::render::hydra {
@ -13,10 +14,15 @@ LightTasksDelegate::LightTasksDelegate(pxr::HdRenderIndex *parent_index,
GetRenderIndex().InsertTask<pxr::HdxSimpleLightTask>(this, simple_task_id_);
skydome_task_id_ = GetDelegateID().AppendElementString("skydomeTask");
GetRenderIndex().InsertTask<pxr::HdxSkydomeTask>(this, skydome_task_id_);
CLOG_INFO(LOG_RENDER_HYDRA, 1, "%s", simple_task_id_.GetText());
CLOG_INFO(LOG_RENDER_HYDRA, 1, "%s", skydome_task_id_.GetText());
}
pxr::VtValue LightTasksDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key)
{
CLOG_INFO(LOG_RENDER_HYDRA, 3, "%s, %s", id.GetText(), key.GetText());
if (key == pxr::HdTokens->params) {
if (id == simple_task_id_) {
return pxr::VtValue(simple_task_params_);
@ -36,8 +42,8 @@ pxr::HdTaskSharedPtr LightTasksDelegate::simple_task()
pxr::HdTaskSharedPtr LightTasksDelegate::skydome_task()
{
/* Note that this task is intended to be the first "Render Task",
so that the AOV's are properly cleared, however it
does not spawn a HdRenderPass. */
* so that the AOV's are properly cleared, however it
* does not spawn a HdRenderPass. */
return GetRenderIndex().GetTask(skydome_task_id_);
}

View File

@ -13,6 +13,9 @@
#include "MEM_guardedalloc.h"
#include "engine.h"
#include "render_task_delegate.h"
namespace blender::render::hydra {
RenderTaskDelegate::RenderTaskDelegate(pxr::HdRenderIndex *parent_index,
@ -24,10 +27,14 @@ RenderTaskDelegate::RenderTaskDelegate(pxr::HdRenderIndex *parent_index,
task_params_.enableLighting = true;
task_params_.alphaThreshold = 0.1f;
CLOG_INFO(LOG_RENDER_HYDRA, 1, "%s", task_id_.GetText());
}
pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const & /*id*/, pxr::TfToken const &key)
pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key)
{
CLOG_INFO(LOG_RENDER_HYDRA, 3, "%s, %s", id.GetText(), key.GetText());
if (key == pxr::HdTokens->params) {
return pxr::VtValue(task_params_);
}
@ -38,13 +45,17 @@ pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const & /*id*/, pxr::TfToken c
return pxr::VtValue();
}
pxr::TfTokenVector RenderTaskDelegate::GetTaskRenderTags(pxr::SdfPath const & /*id*/)
pxr::TfTokenVector RenderTaskDelegate::GetTaskRenderTags(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_RENDER_HYDRA, 3, "%s", id.GetText());
return {pxr::HdRenderTagTokens->geometry};
}
pxr::HdRenderBufferDescriptor RenderTaskDelegate::GetRenderBufferDescriptor(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_RENDER_HYDRA, 3, "%s", id.GetText());
return buffer_descriptors_[id];
}
@ -95,6 +106,10 @@ void RenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
pxr::HdAovDescriptor aov_desc = render_index.GetRenderDelegate()->GetDefaultAovDescriptor(
aov_key);
if (aov_desc.format == pxr::HdFormatInvalid) {
return;
}
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);
@ -105,8 +120,11 @@ void RenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
binding.aovName = aov_key;
binding.renderBufferId = buf_id;
binding.aovSettings = aov_desc.aovSettings;
binding.clearValue = pxr::VtValue(pxr::GfVec4f(0));
task_params_.aovBindings.push_back(binding);
render_index.GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams);
CLOG_INFO(LOG_RENDER_HYDRA, 1, "%s", aov_key.GetText());
}
void RenderTaskDelegate::read_aov(pxr::TfToken const &aov_key, void *data)
@ -130,8 +148,11 @@ void RenderTaskDelegate::read_aov(pxr::TfToken const &aov_key, GPUTexture *textu
if (!buffer) {
return;
}
eGPUDataFormat format = buffer->GetFormat() == pxr::HdFormat::HdFormatFloat16Vec4 ?
GPU_DATA_HALF_FLOAT :
GPU_DATA_FLOAT;
void *buf_data = buffer->Map();
GPU_texture_update(texture, GPU_DATA_FLOAT, buf_data);
GPU_texture_update(texture, format, buf_data);
buffer->Unmap();
}
@ -144,6 +165,17 @@ pxr::SdfPath RenderTaskDelegate::buffer_id(pxr::TfToken const &aov_key) const
return GetDelegateID().AppendElementString("aov_" + aov_key.GetString());
}
GPURenderTaskDelegate::~GPURenderTaskDelegate()
{
unbind();
if (tex_color_) {
GPU_texture_free(tex_color_);
}
if (tex_depth_) {
GPU_texture_free(tex_depth_);
}
}
void GPURenderTaskDelegate::set_viewport(pxr::GfVec4d const &viewport)
{
if (task_params_.viewport == viewport) {
@ -174,7 +206,7 @@ void GPURenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
tex = &tex_color_;
}
else if (aov_key == pxr::HdAovTokens->depth) {
format = GPU_DEPTH32F_STENCIL8;
format = GPU_DEPTH_COMPONENT32F;
tex = &tex_depth_;
}
else {
@ -192,6 +224,8 @@ void GPURenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
format,
GPU_TEXTURE_USAGE_GENERAL,
nullptr);
CLOG_INFO(LOG_RENDER_HYDRA, 1, "%s", aov_key.GetText());
}
void GPURenderTaskDelegate::read_aov(pxr::TfToken const &aov_key, void *data)
@ -251,22 +285,33 @@ void GPURenderTaskDelegate::bind()
/* Workaround missing/buggy VAOs in hgiGL and hdSt. For OpenGL compatibility
* profile this is not a problem, but for core profile it is. */
if (GPU_backend_get_type() == GPU_BACKEND_OPENGL) {
if (VAO_ == 0) {
if (VAO_ == 0 && GPU_backend_get_type() == GPU_BACKEND_OPENGL) {
glGenVertexArrays(1, &VAO_);
}
glBindVertexArray(VAO_);
}
}
void GPURenderTaskDelegate::unbind()
{
if (GPU_backend_get_type() == GPU_BACKEND_OPENGL) {
if (VAO_) {
glDeleteVertexArrays(1, &VAO_);
VAO_ = 0;
}
if (framebuffer_) {
GPU_framebuffer_free(framebuffer_);
GPU_texture_free(tex_color_);
GPU_texture_free(tex_depth_);
framebuffer_ = nullptr;
}
}
GPUTexture *GPURenderTaskDelegate::aov_texture(pxr::TfToken const &aov_key)
{
if (aov_key == pxr::HdAovTokens->color) {
return tex_color_;
}
if (aov_key == pxr::HdAovTokens->depth) {
return tex_depth_;
}
return nullptr;
}
} // namespace blender::render::hydra

View File

@ -30,8 +30,8 @@ class RenderTaskDelegate : public pxr::HdSceneDelegate {
pxr::HdRenderBufferDescriptor GetRenderBufferDescriptor(pxr::SdfPath const &id) override;
pxr::HdTaskSharedPtr task();
bool is_converged();
void set_camera(pxr::SdfPath const &camera_id);
virtual bool is_converged();
virtual void set_viewport(pxr::GfVec4d const &viewport);
virtual void add_aov(pxr::TfToken const &aov_key);
virtual void read_aov(pxr::TfToken const &aov_key, void *data);
@ -52,6 +52,7 @@ class GPURenderTaskDelegate : public RenderTaskDelegate {
public:
using RenderTaskDelegate::RenderTaskDelegate;
~GPURenderTaskDelegate() override;
void set_viewport(pxr::GfVec4d const &viewport) override;
void add_aov(pxr::TfToken const &aov_key) override;
@ -59,6 +60,7 @@ class GPURenderTaskDelegate : public RenderTaskDelegate {
void read_aov(pxr::TfToken const &aov_key, GPUTexture *texture) override;
void bind() override;
void unbind() override;
GPUTexture *aov_texture(pxr::TfToken const &aov_key);
};
} // namespace blender::render::hydra

View File

View File

@ -183,18 +183,16 @@ void DrawTexture::write_data(int width, int height, const void *data)
width,
height,
1,
GPU_RGBA16F,
GPU_RGBA32F,
GPU_TEXTURE_USAGE_GENERAL,
(float *)data);
}
void DrawTexture::draw(GPUShader *shader, const pxr::GfVec4d &viewport)
{
draw(shader, texture_, viewport);
}
void DrawTexture::draw(GPUShader *shader, GPUTexture *tex, const pxr::GfVec4d &viewport)
void DrawTexture::draw(GPUShader *shader, const pxr::GfVec4d &viewport, GPUTexture *tex)
{
if (!tex) {
tex = texture_;
}
int slot = GPU_shader_get_sampler_binding(shader, "image");
GPU_texture_bind(tex, slot);
GPU_shader_uniform_1i(shader, "image", slot);
@ -231,20 +229,8 @@ void ViewportEngine::render()
light_tasks_delegate_->set_viewport(viewport);
}
if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) {
render_task_delegate_->add_aov(pxr::HdAovTokens->color);
}
/* Workaround missing/buggy VAOs in hgiGL and hdSt. For OpenGL compatibility
* profile this is not a problem, but for core profile it is. */
GLuint VAO;
if (GPU_backend_get_type() == GPU_BACKEND_OPENGL) {
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
}
GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_IMAGE);
GPU_shader_bind(shader);
render_task_delegate_->add_aov(pxr::HdAovTokens->depth);
pxr::HdTaskSharedPtrVector tasks;
if (light_tasks_delegate_) {
@ -254,9 +240,24 @@ void ViewportEngine::render()
tasks.push_back(light_tasks_delegate_->simple_task());
}
tasks.push_back(render_task_delegate_->task());
GPUFrameBuffer *view_framebuffer = GPU_framebuffer_active_get();
render_task_delegate_->bind();
engine_->Execute(render_index_.get(), &tasks);
if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) {
render_task_delegate_->unbind();
GPU_framebuffer_bind(view_framebuffer);
GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_IMAGE);
GPU_shader_bind(shader);
GPURenderTaskDelegate *gpu_task = dynamic_cast<GPURenderTaskDelegate *>(
render_task_delegate_.get());
if (gpu_task) {
draw_texture_.draw(shader, viewport, gpu_task->aov_texture(pxr::HdAovTokens->color));
}
else {
draw_texture_.write_data(view_settings.width(), view_settings.height(), nullptr);
render_task_delegate_->read_aov(pxr::HdAovTokens->color, draw_texture_.texture());
draw_texture_.draw(shader, viewport);
@ -264,10 +265,6 @@ void ViewportEngine::render()
GPU_shader_unbind();
if (GPU_backend_get_type() == GPU_BACKEND_OPENGL) {
glDeleteVertexArrays(1, &VAO);
}
if (renderer_percent_done() == 0.0f) {
time_begin_ = PIL_check_seconds_timer();
}

View File

@ -23,8 +23,7 @@ class DrawTexture {
~DrawTexture();
void write_data(int width, int height, const void *data);
void draw(GPUShader *shader, const pxr::GfVec4d &viewport);
void draw(GPUShader *shader, GPUTexture *tex, const pxr::GfVec4d &viewport);
void draw(GPUShader *shader, const pxr::GfVec4d &viewport, GPUTexture *tex = nullptr);
GPUTexture *texture() const;
private: