Support more AOVs #78

Merged
Bogdan Nagirniak merged 11 commits from DagerD/blender:BLEN-462_3 into hydra-render 2023-07-29 14:45:16 +02:00
6 changed files with 65 additions and 40 deletions
Showing only changes of commit 199b37d7c3 - Show all commits

View File

@ -87,7 +87,7 @@ void Engine::sync(Depsgraph *depsgraph, bContext *context)
hydra_scene_delegate_ = std::make_unique<io::hydra::HydraSceneDelegate>( hydra_scene_delegate_ = std::make_unique<io::hydra::HydraSceneDelegate>(
render_index_.get(), scene_path, scene_delegate_settings_); render_index_.get(), scene_path, scene_delegate_settings_);
} }
hydra_scene_delegate_->populate(depsgraph, CTX_wm_view3d(context)); hydra_scene_delegate_->populate(depsgraph, context ? CTX_wm_view3d(context) : nullptr);
} }
else { else {
/* Slow USD export for reference. */ /* Slow USD export for reference. */

View File

@ -66,8 +66,6 @@ void FinalEngine::render(Depsgraph *depsgraph)
} }
} }
render_task_delegate_->bind();
pxr::HdTaskSharedPtrVector tasks; pxr::HdTaskSharedPtrVector tasks;
if (light_tasks_delegate_) { if (light_tasks_delegate_) {
if (scene->r.alphamode != R_ALPHAPREMUL) { if (scene->r.alphamode != R_ALPHAPREMUL) {
@ -76,6 +74,9 @@ void FinalEngine::render(Depsgraph *depsgraph)
tasks.push_back(light_tasks_delegate_->simple_task()); tasks.push_back(light_tasks_delegate_->simple_task());
} }
tasks.push_back(render_task_delegate_->task()); tasks.push_back(render_task_delegate_->task());
render_task_delegate_->bind();
engine_->Execute(render_index_.get(), &tasks); engine_->Execute(render_index_.get(), &tasks);
char elapsed_time[32]; char elapsed_time[32];

View File

@ -120,6 +120,7 @@ void RenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
binding.aovName = aov_key; binding.aovName = aov_key;
binding.renderBufferId = buf_id; binding.renderBufferId = buf_id;
binding.aovSettings = aov_desc.aovSettings; binding.aovSettings = aov_desc.aovSettings;
binding.clearValue = pxr::VtValue(pxr::GfVec4f(0));
task_params_.aovBindings.push_back(binding); task_params_.aovBindings.push_back(binding);
render_index.GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams); render_index.GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams);
@ -147,8 +148,11 @@ void RenderTaskDelegate::read_aov(pxr::TfToken const &aov_key, GPUTexture *textu
if (!buffer) { if (!buffer) {
return; return;
} }
eGPUDataFormat format = buffer->GetFormat() == pxr::HdFormat::HdFormatFloat16Vec4 ?
GPU_DATA_HALF_FLOAT :
GPU_DATA_FLOAT;
void *buf_data = buffer->Map(); void *buf_data = buffer->Map();
GPU_texture_update(texture, GPU_DATA_FLOAT, buf_data); GPU_texture_update(texture, format, buf_data);
buffer->Unmap(); buffer->Unmap();
} }
@ -161,6 +165,17 @@ pxr::SdfPath RenderTaskDelegate::buffer_id(pxr::TfToken const &aov_key) const
return GetDelegateID().AppendElementString("aov_" + aov_key.GetString()); 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) void GPURenderTaskDelegate::set_viewport(pxr::GfVec4d const &viewport)
{ {
if (task_params_.viewport == viewport) { if (task_params_.viewport == viewport) {
@ -270,22 +285,33 @@ void GPURenderTaskDelegate::bind()
/* Workaround missing/buggy VAOs in hgiGL and hdSt. For OpenGL compatibility /* Workaround missing/buggy VAOs in hgiGL and hdSt. For OpenGL compatibility
* profile this is not a problem, but for core profile it is. */ * profile this is not a problem, but for core profile it is. */
if (GPU_backend_get_type() == GPU_BACKEND_OPENGL) { if (VAO_ == 0 && GPU_backend_get_type() == GPU_BACKEND_OPENGL) {
if (VAO_ == 0) { glGenVertexArrays(1, &VAO_);
glGenVertexArrays(1, &VAO_);
}
glBindVertexArray(VAO_); glBindVertexArray(VAO_);
} }
} }
void GPURenderTaskDelegate::unbind() void GPURenderTaskDelegate::unbind()
{ {
if (GPU_backend_get_type() == GPU_BACKEND_OPENGL) { if (VAO_) {
glDeleteVertexArrays(1, &VAO_); glDeleteVertexArrays(1, &VAO_);
VAO_ = 0;
} }
GPU_framebuffer_free(framebuffer_); if (framebuffer_) {
GPU_texture_free(tex_color_); GPU_framebuffer_free(framebuffer_);
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 } // namespace blender::render::hydra

View File

@ -52,6 +52,7 @@ class GPURenderTaskDelegate : public RenderTaskDelegate {
DagerD marked this conversation as resolved Outdated

Looks like aov_task doesn't need.

Looks like aov_task doesn't need.
public: public:
using RenderTaskDelegate::RenderTaskDelegate; using RenderTaskDelegate::RenderTaskDelegate;
~GPURenderTaskDelegate() override;
void set_viewport(pxr::GfVec4d const &viewport) override; void set_viewport(pxr::GfVec4d const &viewport) override;
void add_aov(pxr::TfToken const &aov_key) 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 read_aov(pxr::TfToken const &aov_key, GPUTexture *texture) override;
void bind() override; void bind() override;
void unbind() override; void unbind() override;
GPUTexture *aov_texture(pxr::TfToken const &aov_key);
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -183,18 +183,16 @@ void DrawTexture::write_data(int width, int height, const void *data)
width, width,
height, height,
1, 1,
GPU_RGBA16F, GPU_RGBA32F,
GPU_TEXTURE_USAGE_GENERAL, GPU_TEXTURE_USAGE_GENERAL,
(float *)data); (float *)data);
} }
void DrawTexture::draw(GPUShader *shader, const pxr::GfVec4d &viewport) void DrawTexture::draw(GPUShader *shader, const pxr::GfVec4d &viewport, GPUTexture *tex)
{
draw(shader, texture_, viewport);
}
void DrawTexture::draw(GPUShader *shader, GPUTexture *tex, const pxr::GfVec4d &viewport)
{ {
if (!tex) {
tex = texture_;
}
int slot = GPU_shader_get_sampler_binding(shader, "image"); int slot = GPU_shader_get_sampler_binding(shader, "image");
GPU_texture_bind(tex, slot); GPU_texture_bind(tex, slot);
GPU_shader_uniform_1i(shader, "image", slot); GPU_shader_uniform_1i(shader, "image", slot);
@ -237,20 +235,8 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
light_tasks_delegate_->set_viewport(viewport); light_tasks_delegate_->set_viewport(viewport);
} }
if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) { render_task_delegate_->add_aov(pxr::HdAovTokens->color);
render_task_delegate_->add_aov(pxr::HdAovTokens->color); render_task_delegate_->add_aov(pxr::HdAovTokens->depth);
}
/* 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);
pxr::HdTaskSharedPtrVector tasks; pxr::HdTaskSharedPtrVector tasks;
if (light_tasks_delegate_) { if (light_tasks_delegate_) {
@ -260,9 +246,24 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
tasks.push_back(light_tasks_delegate_->simple_task()); tasks.push_back(light_tasks_delegate_->simple_task());
} }
tasks.push_back(render_task_delegate_->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); 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); draw_texture_.write_data(view_settings.width(), view_settings.height(), nullptr);
render_task_delegate_->read_aov(pxr::HdAovTokens->color, draw_texture_.texture()); render_task_delegate_->read_aov(pxr::HdAovTokens->color, draw_texture_.texture());
draw_texture_.draw(shader, viewport); draw_texture_.draw(shader, viewport);
@ -270,10 +271,6 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
GPU_shader_unbind(); GPU_shader_unbind();
if (GPU_backend_get_type() == GPU_BACKEND_OPENGL) {
glDeleteVertexArrays(1, &VAO);
}
if (renderer_percent_done() == 0.0f) { if (renderer_percent_done() == 0.0f) {
time_begin_ = PIL_check_seconds_timer(); time_begin_ = PIL_check_seconds_timer();
} }

View File

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