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 62 additions and 78 deletions
Showing only changes of commit bf1e52b2e4 - Show all commits

View File

@ -41,20 +41,23 @@ void FinalEngine::render(Depsgraph *depsgraph)
pxr::GfCamera camera = pxr::GfCamera camera =
CameraData(scene->camera, image_res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(border); CameraData(scene->camera, image_res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(border);
pxr::HdTaskSharedPtrVector tasks;
free_camera_delegate_->SetCamera(camera); free_camera_delegate_->SetCamera(camera);
render_task_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); render_task_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1]));
if (light_tasks_delegate_) { if (light_tasks_delegate_) {
light_tasks_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1])); light_tasks_delegate_->set_viewport(pxr::GfVec4d(0, 0, resolution_[0], resolution_[1]));
}
render_task_delegate_->add_aov(pxr::HdAovTokens->color);
render_task_delegate_->add_aov(pxr::HdAovTokens->depth);
render_task_delegate_->bind();
pxr::HdTaskSharedPtrVector tasks;
if (light_tasks_delegate_) {
tasks.push_back(light_tasks_delegate_->skydome_task()); tasks.push_back(light_tasks_delegate_->skydome_task());
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_->add_aov(pxr::HdAovTokens->color);
render_task_delegate_->add_aov(pxr::HdAovTokens->depth);
render_task_delegate_->bind();
engine_->Execute(render_index_.get(), &tasks); engine_->Execute(render_index_.get(), &tasks);
char elapsed_time[32]; char elapsed_time[32];
@ -106,7 +109,7 @@ void FinalEngine::update_render_result()
else if (STREQ(pass->name, "Depth")) { else if (STREQ(pass->name, "Depth")) {
aov_key = pxr::HdAovTokens->depth; aov_key = pxr::HdAovTokens->depth;
} }
render_task_delegate_->get_aov_data(aov_key, pass->ibuf->float_buffer.data); render_task_delegate_->read_aov(aov_key, pass->ibuf->float_buffer.data);
} }
RE_engine_end_result(bl_engine_, result, false, false, false); RE_engine_end_result(bl_engine_, result, false, false, false);

View File

@ -107,7 +107,7 @@ void RenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
render_index.GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams); render_index.GetChangeTracker().MarkTaskDirty(task_id_, pxr::HdChangeTracker::DirtyParams);
} }
void RenderTaskDelegate::get_aov_data(pxr::TfToken const &aov_key, void *data) void RenderTaskDelegate::read_aov(pxr::TfToken const &aov_key, void *data)
{ {
pxr::HdRenderBuffer *buffer = (pxr::HdRenderBuffer *)GetRenderIndex().GetBprim( pxr::HdRenderBuffer *buffer = (pxr::HdRenderBuffer *)GetRenderIndex().GetBprim(
pxr::HdPrimTypeTokens->renderBuffer, buffer_id(aov_key)); pxr::HdPrimTypeTokens->renderBuffer, buffer_id(aov_key));
@ -141,10 +141,12 @@ void GPURenderTaskDelegate::set_viewport(pxr::GfVec4d const &viewport)
if (tex_color_) { if (tex_color_) {
GPU_texture_free(tex_color_); GPU_texture_free(tex_color_);
tex_color_ = nullptr;
add_aov(pxr::HdAovTokens->color); add_aov(pxr::HdAovTokens->color);
} }
if (tex_depth_) { if (tex_depth_) {
GPU_texture_free(tex_depth_); GPU_texture_free(tex_depth_);
tex_depth_ = nullptr;
add_aov(pxr::HdAovTokens->depth); add_aov(pxr::HdAovTokens->depth);
} }
} }
@ -165,6 +167,10 @@ void GPURenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
return; return;
} }
if (*tex) {
return;
}
*tex = GPU_texture_create_2d(("tex_render_hydra_" + aov_key.GetString()).c_str(), *tex = GPU_texture_create_2d(("tex_render_hydra_" + aov_key.GetString()).c_str(),
task_params_.viewport[2] - task_params_.viewport[0], task_params_.viewport[2] - task_params_.viewport[0],
task_params_.viewport[3] - task_params_.viewport[1], task_params_.viewport[3] - task_params_.viewport[1],
@ -174,7 +180,7 @@ void GPURenderTaskDelegate::add_aov(pxr::TfToken const &aov_key)
nullptr); nullptr);
} }
void GPURenderTaskDelegate::get_aov_data(pxr::TfToken const &aov_key, void *data) void GPURenderTaskDelegate::read_aov(pxr::TfToken const &aov_key, void *data)
{ {
GPUTexture *tex = nullptr; GPUTexture *tex = nullptr;
int c; int c;
@ -218,7 +224,7 @@ void GPURenderTaskDelegate::bind()
void GPURenderTaskDelegate::unbind() void GPURenderTaskDelegate::unbind()
{ {
glDeleteVertexArrays(1, &VAO_); //glDeleteVertexArrays(1, &VAO_);
GPU_framebuffer_free(framebuffer_); GPU_framebuffer_free(framebuffer_);
GPU_texture_free(tex_color_); GPU_texture_free(tex_color_);
GPU_texture_free(tex_depth_); GPU_texture_free(tex_depth_);

View File

@ -26,7 +26,7 @@ class RenderTaskDelegate : public pxr::HdSceneDelegate {
void set_camera(pxr::SdfPath const &camera_id); void set_camera(pxr::SdfPath const &camera_id);
virtual void set_viewport(pxr::GfVec4d const &viewport); virtual void set_viewport(pxr::GfVec4d const &viewport);
virtual void add_aov(pxr::TfToken const &aov_key); virtual void add_aov(pxr::TfToken const &aov_key);
virtual void get_aov_data(pxr::TfToken const &aov_key, void *data); virtual void read_aov(pxr::TfToken const &aov_key, void *data);
virtual void bind(); virtual void bind();
virtual void unbind(); virtual void unbind();
@ -45,7 +45,7 @@ class GPURenderTaskDelegate : public RenderTaskDelegate {
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;
void get_aov_data(pxr::TfToken const &aov_key, void *data) override; void read_aov(pxr::TfToken const &aov_key, void *data) override;
void bind() override; void bind() override;
void unbind() override; void unbind() override;

View File

@ -5,19 +5,15 @@
#include <pxr/imaging/glf/drawTarget.h> #include <pxr/imaging/glf/drawTarget.h>
#include <pxr/usd/usdGeom/camera.h> #include <pxr/usd/usdGeom/camera.h>
#include "DNA_camera_types.h"
#include "DNA_screen_types.h"
#include "DNA_vec_types.h" /* this include must be before BKE_camera.h due to "rctf" type */ #include "DNA_vec_types.h" /* this include must be before BKE_camera.h due to "rctf" type */
#include "DNA_camera_types.h"
#include "DNA_screen_types.h"
#include "BKE_camera.h" #include "BKE_camera.h"
#include "BLI_math_matrix.h" #include "BLI_math_matrix.h"
#include "BLI_timecode.h" #include "BLI_timecode.h"
#include "DEG_depsgraph_query.h" #include "DEG_depsgraph_query.h"
#include "GPU_matrix.h" #include "GPU_matrix.h"
#include "PIL_time.h" #include "PIL_time.h"
#include "camera.h" #include "camera.h"
@ -136,7 +132,7 @@ pxr::GfCamera ViewSettings::gf_camera()
(float)border[3] / screen_height)); (float)border[3] / screen_height));
} }
DrawTexture::DrawTexture() : texture_(nullptr), width_(0), height_(0), channels_(4) DrawTexture::DrawTexture()
{ {
float coords[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; float coords[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0};
@ -154,67 +150,50 @@ DrawTexture::DrawTexture() : texture_(nullptr), width_(0), height_(0), channels_
DrawTexture::~DrawTexture() DrawTexture::~DrawTexture()
{ {
if (texture_) { if (texture_) {
free(); GPU_texture_free(texture_);
} }
GPU_batch_discard(batch_); GPU_batch_discard(batch_);
} }
void DrawTexture::set_buffer(pxr::HdRenderBuffer *buffer) void DrawTexture::write_data(int width, int height, const void *data)
{ {
if (!texture_) { if (texture_ && width == GPU_texture_width(texture_) && height == GPU_texture_height(texture_)) {
create(buffer);
return;
}
if (width_ != buffer->GetWidth() || height_ != buffer->GetHeight()) {
free();
create(buffer);
return;
}
void *data = buffer->Map();
GPU_texture_update(texture_, GPU_DATA_FLOAT, data); GPU_texture_update(texture_, GPU_DATA_FLOAT, data);
buffer->Unmap(); return;
} }
void DrawTexture::draw(GPUShader *shader, float x, float y) if (texture_ ) {
{ GPU_texture_free(texture_);
int slot = GPU_shader_get_sampler_binding(shader, "image");
GPU_texture_bind(texture_, slot);
GPU_shader_uniform_1i(shader, "image", slot);
GPU_matrix_push();
GPU_matrix_translate_2f(x, y);
GPU_matrix_scale_2f(width_, height_);
GPU_batch_set_shader(batch_, shader);
GPU_batch_draw(batch_);
GPU_matrix_pop();
} }
void DrawTexture::create(pxr::HdRenderBuffer *buffer)
{
width_ = buffer->GetWidth();
height_ = buffer->GetHeight();
channels_ = pxr::HdGetComponentCount(buffer->GetFormat());
void *data = buffer->Map();
texture_ = GPU_texture_create_2d("tex_hydra_render_viewport", texture_ = GPU_texture_create_2d("tex_hydra_render_viewport",
width_, width,
height_, height,
1, 1,
GPU_RGBA16F, GPU_RGBA16F,
GPU_TEXTURE_USAGE_GENERAL, GPU_TEXTURE_USAGE_GENERAL,
(float *)data); (float *)data);
buffer->Unmap();
GPU_texture_filter_mode(texture_, true); GPU_texture_filter_mode(texture_, true);
GPU_texture_mipmap_mode(texture_, true, true); GPU_texture_mipmap_mode(texture_, true, true);
} }
void DrawTexture::free() void DrawTexture::draw(GPUShader *shader, const pxr::GfVec4d &viewport)
{ {
GPU_texture_free(texture_); draw(shader, texture_, viewport);
texture_ = nullptr; }
void DrawTexture::draw(GPUShader *shader, GPUTexture *tex, const pxr::GfVec4d &viewport)
{
int slot = GPU_shader_get_sampler_binding(shader, "image");
GPU_texture_bind(tex, slot);
GPU_shader_uniform_1i(shader, "image", slot);
GPU_matrix_push();
GPU_matrix_translate_2f(viewport[0], viewport[1]);
GPU_matrix_scale_2f(viewport[2] - viewport[0], viewport[3] - viewport[1]);
GPU_batch_set_shader(batch_, shader);
GPU_batch_draw(batch_);
GPU_matrix_pop();
} }
void ViewportEngine::render(Depsgraph * /* depsgraph */) void ViewportEngine::render(Depsgraph * /* depsgraph */)
@ -231,16 +210,14 @@ void ViewportEngine::render(Depsgraph * /* depsgraph */, bContext *context)
pxr::GfCamera gf_camera = view_settings.gf_camera(); pxr::GfCamera gf_camera = view_settings.gf_camera();
free_camera_delegate_->SetCamera(gf_camera); free_camera_delegate_->SetCamera(gf_camera);
render_task_delegate_->set_viewport(pxr::GfVec4d(view_settings.border[0],
view_settings.border[1],
view_settings.border[2],
view_settings.border[3]));
if (light_tasks_delegate_) { pxr::GfVec4d viewport(view_settings.border[0],
light_tasks_delegate_->set_viewport(pxr::GfVec4d(view_settings.border[0],
view_settings.border[1], view_settings.border[1],
view_settings.border[2], view_settings.border[2],
view_settings.border[3])); view_settings.border[3]);
render_task_delegate_->set_viewport(viewport);
if (light_tasks_delegate_) {
light_tasks_delegate_->set_viewport(viewport);
} }
if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) { if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) {
@ -256,12 +233,13 @@ 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());
engine_->Execute(render_index_.get(), &tasks); engine_->Execute(render_index_.get(), &tasks);
if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) { if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) {
//draw_texture_.set_buffer(render_task_delegate_->get_aov(pxr::HdAovTokens->color)); std::vector<float> data(view_settings.width() * view_settings.height() * 4, 0.0f);
draw_texture_.draw(shader, view_settings.border[0], view_settings.border[1]); render_task_delegate_->read_aov(pxr::HdAovTokens->color, data.data());
draw_texture_.write_data(view_settings.width(), view_settings.height(), data.data());
draw_texture_.draw(shader, viewport);
} }
GPU_shader_unbind(); GPU_shader_unbind();

View File

@ -18,16 +18,13 @@ class DrawTexture {
DrawTexture(); DrawTexture();
~DrawTexture(); ~DrawTexture();
void set_buffer(pxr::HdRenderBuffer *buffer); void write_data(int width, int height, const void *data);
void draw(GPUShader *shader, float x, float y); void draw(GPUShader *shader, const pxr::GfVec4d &viewport);
void draw(GPUShader *shader, GPUTexture *tex, const pxr::GfVec4d &viewport);
private: private:
void create(pxr::HdRenderBuffer *buffer); GPUTexture *texture_ = nullptr;
void free();
GPUTexture *texture_;
GPUBatch *batch_; GPUBatch *batch_;
int width_, height_, channels_;
}; };
class ViewportEngine : public Engine { class ViewportEngine : public Engine {