diff --git a/source/blender/render/hydra/camera.cc b/source/blender/render/hydra/camera.cc index f2201e53518d..b9917061fdd6 100644 --- a/source/blender/render/hydra/camera.cc +++ b/source/blender/render/hydra/camera.cc @@ -9,15 +9,92 @@ namespace blender::render::hydra { +CameraData::CameraData(bContext *context) +{ + View3D *view3d = CTX_wm_view3d(context); + RegionView3D *region_data = (RegionView3D *)CTX_wm_region_data(context); + ARegion *region = CTX_wm_region(context); + + /* This constant was found experimentally, didn't find such option in + * context.view3d or context.region_data. */ + float VIEWPORT_SENSOR_SIZE = 72.0; + + pxr::GfVec2i res(region->winx, region->winy); + float ratio = (float)res[0] / res[1]; + transform_ = gf_matrix_from_transform(region_data->viewmat).GetInverse(); + + switch (region_data->persp) { + case RV3D_PERSP: { + mode_ = CAM_PERSP; + clip_range_ = pxr::GfRange1f(view3d->clip_start, view3d->clip_end); + lens_shift_ = pxr::GfVec2f(0.0, 0.0); + focal_length_ = view3d->lens; + + if (ratio > 1.0) { + sensor_size_ = pxr::GfVec2f(VIEWPORT_SENSOR_SIZE, VIEWPORT_SENSOR_SIZE / ratio); + } + else { + sensor_size_ = pxr::GfVec2f(VIEWPORT_SENSOR_SIZE * ratio, VIEWPORT_SENSOR_SIZE); + } + break; + } + + case RV3D_ORTHO: { + mode_ = CAM_ORTHO; + lens_shift_ = pxr::GfVec2f(0.0f, 0.0f); + + float o_size = region_data->dist * VIEWPORT_SENSOR_SIZE / view3d->lens; + float o_depth = view3d->clip_end; + + clip_range_ = pxr::GfRange1f(-o_depth * 0.5, o_depth * 0.5); + + if (ratio > 1.0f) { + ortho_size_ = pxr::GfVec2f(o_size, o_size / ratio); + } + else { + ortho_size_ = pxr::GfVec2f(o_size * ratio, o_size); + } + break; + } + + case RV3D_CAMOB: { + pxr::GfMatrix4d mat = transform_; + *this = CameraData(view3d->camera, res, pxr::GfVec4f(0, 0, 1, 1)); + transform_ = mat; + + /* This formula was taken from previous plugin with corresponded comment. + * See blender/intern/cycles/blender/blender_camera.cpp:blender_camera_from_view (look + * for 1.41421f). */ + float zoom = 4.0 / pow((pow(2.0, 0.5) + region_data->camzoom / 50.0), 2); + + /* Updating l_shift due to viewport zoom and view_camera_offset + * view_camera_offset should be multiplied by 2. */ + lens_shift_ = pxr::GfVec2f((lens_shift_[0] + region_data->camdx * 2) / zoom, + (lens_shift_[1] + region_data->camdy * 2) / zoom); + + if (mode_ == CAM_ORTHO) { + ortho_size_ *= zoom; + } + else { + sensor_size_ *= zoom; + } + break; + } + + default: + break; + } +} + CameraData::CameraData(Object *camera_obj, pxr::GfVec2i res, pxr::GfVec4f tile) { Camera *camera = (Camera *)camera_obj->data; float t_pos[2] = {tile[0], tile[1]}; float t_size[2] = {tile[2], tile[3]}; - transform = gf_matrix_from_transform(camera_obj->object_to_world); - clip_range = pxr::GfRange1f(camera->clip_start, camera->clip_end); - mode = camera->type; + transform_ = gf_matrix_from_transform(camera_obj->object_to_world); + clip_range_ = pxr::GfRange1f(camera->clip_start, camera->clip_end); + mode_ = camera->type; if (camera->dof.flag & CAM_DOF_ENABLED) { float focus_distance; @@ -28,11 +105,11 @@ CameraData::CameraData(Object *camera_obj, pxr::GfVec2i res, pxr::GfVec4f tile) pxr::GfVec3f obj_pos(camera->dof.focus_object->object_to_world[0][3], camera->dof.focus_object->object_to_world[1][3], camera->dof.focus_object->object_to_world[2][3]); - pxr::GfVec3f cam_pos(transform[0][3], transform[1][3], transform[2][3]); + pxr::GfVec3f cam_pos(transform_[0][3], transform_[1][3], transform_[2][3]); focus_distance = (obj_pos - cam_pos).GetLength(); } - dof_data = std::tuple( + dof_data_ = std::tuple( std::max(focus_distance, 0.001f), camera->dof.aperture_fstop, camera->dof.aperture_blades); } @@ -40,184 +117,112 @@ CameraData::CameraData(Object *camera_obj, pxr::GfVec2i res, pxr::GfVec4f tile) switch (camera->sensor_fit) { case CAMERA_SENSOR_FIT_VERT: - lens_shift = pxr::GfVec2f(camera->shiftx / ratio, camera->shifty); + lens_shift_ = pxr::GfVec2f(camera->shiftx / ratio, camera->shifty); break; case CAMERA_SENSOR_FIT_HOR: - lens_shift = pxr::GfVec2f(camera->shiftx, camera->shifty * ratio); + lens_shift_ = pxr::GfVec2f(camera->shiftx, camera->shifty * ratio); break; case CAMERA_SENSOR_FIT_AUTO: if (ratio > 1.0f) { - lens_shift = pxr::GfVec2f(camera->shiftx, camera->shifty * ratio); + lens_shift_ = pxr::GfVec2f(camera->shiftx, camera->shifty * ratio); } else { - lens_shift = pxr::GfVec2f(camera->shiftx / ratio, camera->shifty); + lens_shift_ = pxr::GfVec2f(camera->shiftx / ratio, camera->shifty); } break; default: - lens_shift = pxr::GfVec2f(camera->shiftx, camera->shifty); + lens_shift_ = pxr::GfVec2f(camera->shiftx, camera->shifty); break; } - lens_shift = pxr::GfVec2f( - lens_shift[0] / t_size[0] + (t_pos[0] + t_size[0] * 0.5 - 0.5) / t_size[0], - lens_shift[1] / t_size[1] + (t_pos[1] + t_size[1] * 0.5 - 0.5) / t_size[1]); + lens_shift_ = pxr::GfVec2f( + lens_shift_[0] / t_size[0] + (t_pos[0] + t_size[0] * 0.5 - 0.5) / t_size[0], + lens_shift_[1] / t_size[1] + (t_pos[1] + t_size[1] * 0.5 - 0.5) / t_size[1]); switch (camera->type) { case CAM_PERSP: - focal_length = camera->lens; + focal_length_ = camera->lens; switch (camera->sensor_fit) { case CAMERA_SENSOR_FIT_VERT: - sensor_size = pxr::GfVec2f(camera->sensor_y * ratio, camera->sensor_y); + sensor_size_ = pxr::GfVec2f(camera->sensor_y * ratio, camera->sensor_y); break; case CAMERA_SENSOR_FIT_HOR: - sensor_size = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); + sensor_size_ = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); break; case CAMERA_SENSOR_FIT_AUTO: if (ratio > 1.0f) { - sensor_size = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); + sensor_size_ = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); } else { - sensor_size = pxr::GfVec2f(camera->sensor_x * ratio, camera->sensor_x); + sensor_size_ = pxr::GfVec2f(camera->sensor_x * ratio, camera->sensor_x); } break; default: - sensor_size = pxr::GfVec2f(camera->sensor_x, camera->sensor_y); + sensor_size_ = pxr::GfVec2f(camera->sensor_x, camera->sensor_y); break; } - sensor_size = pxr::GfVec2f(sensor_size[0] * t_size[0], sensor_size[1] * t_size[1]); + sensor_size_ = pxr::GfVec2f(sensor_size_[0] * t_size[0], sensor_size_[1] * t_size[1]); break; case CAM_ORTHO: - focal_length = 0.0f; + focal_length_ = 0.0f; switch (camera->sensor_fit) { case CAMERA_SENSOR_FIT_VERT: - ortho_size = pxr::GfVec2f(camera->ortho_scale * ratio, camera->ortho_scale); + ortho_size_ = pxr::GfVec2f(camera->ortho_scale * ratio, camera->ortho_scale); break; case CAMERA_SENSOR_FIT_HOR: - ortho_size = pxr::GfVec2f(camera->ortho_scale, camera->ortho_scale / ratio); + ortho_size_ = pxr::GfVec2f(camera->ortho_scale, camera->ortho_scale / ratio); break; case CAMERA_SENSOR_FIT_AUTO: if (ratio > 1.0f) { - ortho_size = pxr::GfVec2f(camera->ortho_scale, camera->ortho_scale / ratio); + ortho_size_ = pxr::GfVec2f(camera->ortho_scale, camera->ortho_scale / ratio); } else { - ortho_size = pxr::GfVec2f(camera->ortho_scale * ratio, camera->ortho_scale); + ortho_size_ = pxr::GfVec2f(camera->ortho_scale * ratio, camera->ortho_scale); } break; default: - ortho_size = pxr::GfVec2f(camera->ortho_scale, camera->ortho_scale); + ortho_size_ = pxr::GfVec2f(camera->ortho_scale, camera->ortho_scale); break; } - ortho_size = pxr::GfVec2f(ortho_size[0] * t_size[0], ortho_size[1] * t_size[1]); + ortho_size_ = pxr::GfVec2f(ortho_size_[0] * t_size[0], ortho_size_[1] * t_size[1]); break; case CAM_PANO: /* TODO: Recheck parameters for PANO camera */ - focal_length = camera->lens; + focal_length_ = camera->lens; switch (camera->sensor_fit) { case CAMERA_SENSOR_FIT_VERT: - sensor_size = pxr::GfVec2f(camera->sensor_y * ratio, camera->sensor_y); + sensor_size_ = pxr::GfVec2f(camera->sensor_y * ratio, camera->sensor_y); break; case CAMERA_SENSOR_FIT_HOR: - sensor_size = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); + sensor_size_ = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); break; case CAMERA_SENSOR_FIT_AUTO: if (ratio > 1.0f) { - sensor_size = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); + sensor_size_ = pxr::GfVec2f(camera->sensor_x, camera->sensor_x / ratio); } else { - sensor_size = pxr::GfVec2f(camera->sensor_x * ratio, camera->sensor_x); + sensor_size_ = pxr::GfVec2f(camera->sensor_x * ratio, camera->sensor_x); } break; default: - sensor_size = pxr::GfVec2f(camera->sensor_x, camera->sensor_y); + sensor_size_ = pxr::GfVec2f(camera->sensor_x, camera->sensor_y); break; } - sensor_size = pxr::GfVec2f(sensor_size[0] * t_size[0], sensor_size[1] * t_size[1]); + sensor_size_ = pxr::GfVec2f(sensor_size_[0] * t_size[0], sensor_size_[1] * t_size[1]); default: - focal_length = camera->lens; - sensor_size = pxr::GfVec2f(camera->sensor_y * ratio, camera->sensor_y); + focal_length_ = camera->lens; + sensor_size_ = pxr::GfVec2f(camera->sensor_y * ratio, camera->sensor_y); } } -CameraData::CameraData(bContext *context) +pxr::GfCamera CameraData::gf_camera() { - View3D *view3d = CTX_wm_view3d(context); - RegionView3D *region_data = (RegionView3D *)CTX_wm_region_data(context); - ARegion *region = CTX_wm_region(context); - - // this constant was found experimentally, didn't find such option in - // context.view3d or context.region_data - float VIEWPORT_SENSOR_SIZE = 72.0; - - pxr::GfVec2i res(region->winx, region->winy); - float ratio = (float)res[0] / res[1]; - transform = gf_matrix_from_transform(region_data->viewmat).GetInverse(); - - switch (region_data->persp) { - case RV3D_PERSP: { - mode = CAM_PERSP; - clip_range = pxr::GfRange1f(view3d->clip_start, view3d->clip_end); - lens_shift = pxr::GfVec2f(0.0, 0.0); - focal_length = view3d->lens; - - if (ratio > 1.0) { - sensor_size = pxr::GfVec2f(VIEWPORT_SENSOR_SIZE, VIEWPORT_SENSOR_SIZE / ratio); - } - else { - sensor_size = pxr::GfVec2f(VIEWPORT_SENSOR_SIZE * ratio, VIEWPORT_SENSOR_SIZE); - } - break; - } - - case RV3D_ORTHO: { - mode = CAM_ORTHO; - lens_shift = pxr::GfVec2f(0.0f, 0.0f); - - float o_size = region_data->dist * VIEWPORT_SENSOR_SIZE / view3d->lens; - float o_depth = view3d->clip_end; - - clip_range = pxr::GfRange1f(-o_depth * 0.5, o_depth * 0.5); - - if (ratio > 1.0f) { - ortho_size = pxr::GfVec2f(o_size, o_size / ratio); - } - else { - ortho_size = pxr::GfVec2f(o_size * ratio, o_size); - } - break; - } - - case RV3D_CAMOB: { - pxr::GfMatrix4d mat = transform; - *this = CameraData(view3d->camera, res, pxr::GfVec4f(0, 0, 1, 1)); - transform = mat; - - // This formula was taken from previous plugin with corresponded comment - // See blender/intern/cycles/blender/blender_camera.cpp:blender_camera_from_view (look - // for 1.41421f) - float zoom = 4.0 / pow((pow(2.0, 0.5) + region_data->camzoom / 50.0), 2); - - // Updating l_shift due to viewport zoom and view_camera_offset - // view_camera_offset should be multiplied by 2 - lens_shift = pxr::GfVec2f((lens_shift[0] + region_data->camdx * 2) / zoom, - (lens_shift[1] + region_data->camdy * 2) / zoom); - - if (mode == CAM_ORTHO) { - ortho_size *= zoom; - } - else { - sensor_size *= zoom; - } - break; - } - - default: - break; - } + return gf_camera(pxr::GfVec4f(0, 0, 1, 1)); } pxr::GfCamera CameraData::gf_camera(pxr::GfVec4f tile) @@ -226,19 +231,19 @@ pxr::GfCamera CameraData::gf_camera(pxr::GfVec4f tile) pxr::GfCamera gf_camera = pxr::GfCamera(); - gf_camera.SetClippingRange(clip_range); + gf_camera.SetClippingRange(clip_range_); - float l_shift[2] = {(lens_shift[0] + t_pos[0] + t_size[0] * 0.5f - 0.5f) / t_size[0], - (lens_shift[1] + t_pos[1] + t_size[1] * 0.5f - 0.5f) / t_size[1]}; + float l_shift[2] = {(lens_shift_[0] + t_pos[0] + t_size[0] * 0.5f - 0.5f) / t_size[0], + (lens_shift_[1] + t_pos[1] + t_size[1] * 0.5f - 0.5f) / t_size[1]}; - switch (mode) { + switch (mode_) { case CAM_PERSP: case CAM_PANO: { /* TODO: store panoramic camera settings */ gf_camera.SetProjection(pxr::GfCamera::Projection::Perspective); - gf_camera.SetFocalLength(focal_length); + gf_camera.SetFocalLength(focal_length_); - float s_size[2] = {sensor_size[0] * t_size[0], sensor_size[1] * t_size[1]}; + float s_size[2] = {sensor_size_[0] * t_size[0], sensor_size_[1] * t_size[1]}; gf_camera.SetHorizontalAperture(s_size[0]); gf_camera.SetVerticalAperture(s_size[1]); @@ -250,9 +255,9 @@ pxr::GfCamera CameraData::gf_camera(pxr::GfVec4f tile) case CAM_ORTHO: { gf_camera.SetProjection(pxr::GfCamera::Projection::Orthographic); - // Use tenths of a world unit accorging to USD docs - // https://graphics.pixar.com/usd/docs/api/class_gf_camera.html - float o_size[2] = {ortho_size[0] * t_size[0] * 10, ortho_size[1] * t_size[1] * 10}; + /* Use tenths of a world unit accorging to USD docs + * https://graphics.pixar.com/usd/docs/api/class_gf_camera.html */ + float o_size[2] = {ortho_size_[0] * t_size[0] * 10, ortho_size_[1] * t_size[1] * 10}; gf_camera.SetHorizontalAperture(o_size[0]); gf_camera.SetVerticalAperture(o_size[1]); @@ -265,13 +270,8 @@ pxr::GfCamera CameraData::gf_camera(pxr::GfVec4f tile) break; } - gf_camera.SetTransform(transform); + gf_camera.SetTransform(transform_); return gf_camera; } -pxr::GfCamera CameraData::gf_camera() -{ - return gf_camera(pxr::GfVec4f(0, 0, 1, 1)); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/camera.h b/source/blender/render/hydra/camera.h index 9e23146b5a60..6e3a482ae007 100644 --- a/source/blender/render/hydra/camera.h +++ b/source/blender/render/hydra/camera.h @@ -23,14 +23,14 @@ class CameraData { pxr::GfCamera gf_camera(pxr::GfVec4f tile); private: - int mode; - pxr::GfRange1f clip_range; - float focal_length; - pxr::GfVec2f sensor_size; - pxr::GfMatrix4d transform; - pxr::GfVec2f lens_shift; - pxr::GfVec2f ortho_size; - std::tuple dof_data; + int mode_; + pxr::GfRange1f clip_range_; + float focal_length_; + pxr::GfVec2f sensor_size_; + pxr::GfMatrix4d transform_; + pxr::GfVec2f lens_shift_; + pxr::GfVec2f ortho_size_; + std::tuple dof_data_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/engine.cc b/source/blender/render/hydra/engine.cc index 6452e1ddf866..26026dad49ea 100644 --- a/source/blender/render/hydra/engine.cc +++ b/source/blender/render/hydra/engine.cc @@ -14,51 +14,39 @@ namespace blender::render::hydra { CLG_LOGREF_DECLARE_GLOBAL(LOG_EN, "rhd.en"); -Engine::Engine(RenderEngine *bl_engine, const std::string &delegate_id) : bl_engine(bl_engine) +Engine::Engine(RenderEngine *bl_engine, const std::string &delegate_id) : bl_engine_(bl_engine) { pxr::HdRendererPluginRegistry ®istry = pxr::HdRendererPluginRegistry::GetInstance(); pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - render_delegate = registry.CreateRenderDelegate(pxr::TfToken(delegate_id)); + render_delegate_ = registry.CreateRenderDelegate(pxr::TfToken(delegate_id)); pxr::HdDriverVector hd_drivers; if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { - hgi = pxr::Hgi::CreatePlatformDefaultHgi(); - hgi_driver.name = pxr::HgiTokens->renderDriver; - hgi_driver.driver = pxr::VtValue(hgi.get()); + hgi_ = pxr::Hgi::CreatePlatformDefaultHgi(); + hgi_driver_.name = pxr::HgiTokens->renderDriver; + hgi_driver_.driver = pxr::VtValue(hgi_.get()); - hd_drivers.push_back(&hgi_driver); + hd_drivers.push_back(&hgi_driver_); } - render_index.reset(pxr::HdRenderIndex::New(render_delegate.Get(), hd_drivers)); - free_camera_delegate = std::make_unique( - render_index.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("freeCamera")); - render_task_delegate = std::make_unique( - render_index.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("renderTask")); - if (render_delegate->GetRendererDisplayName() == "GL") { - simple_light_task_delegate = std::make_unique( - render_index.get(), + render_index_.reset(pxr::HdRenderIndex::New(render_delegate_.Get(), hd_drivers)); + free_camera_delegate_ = std::make_unique( + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("freeCamera")); + render_task_delegate_ = std::make_unique( + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("renderTask")); + if (render_delegate_->GetRendererDisplayName() == "GL") { + simple_light_task_delegate_ = std::make_unique( + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("simpleLightTask")); } - engine = std::make_unique(); -} - -Engine::~Engine() -{ - scene_delegate = nullptr; - render_task_delegate = nullptr; - free_camera_delegate = nullptr; - simple_light_task_delegate = nullptr; - render_index = nullptr; - render_delegate = nullptr; - engine = nullptr; - hgi = nullptr; + engine_ = std::make_unique(); } float Engine::renderer_percent_done() { - pxr::VtDictionary render_stats = render_delegate->GetRenderStats(); + pxr::VtDictionary render_stats = render_delegate_->GetRenderStats(); auto it = render_stats.find("percentDone"); if (it == render_stats.end()) { return 0.0; diff --git a/source/blender/render/hydra/engine.h b/source/blender/render/hydra/engine.h index 3e6a5ad95056..4de015e51ee1 100644 --- a/source/blender/render/hydra/engine.h +++ b/source/blender/render/hydra/engine.h @@ -5,8 +5,6 @@ #include -#include - #include #include #include @@ -28,7 +26,7 @@ extern struct CLG_LogRef *LOG_EN; /* EN - Engine */ class Engine { public: Engine(RenderEngine *bl_engine, const std::string &render_delegate_id); - virtual ~Engine(); + virtual ~Engine() = default; virtual void sync(Depsgraph *depsgraph, bContext *context, @@ -38,19 +36,19 @@ class Engine { protected: float renderer_percent_done(); - protected: - RenderEngine *bl_engine; + RenderEngine *bl_engine_; - pxr::HdPluginRenderDelegateUniqueHandle render_delegate; - std::unique_ptr render_index; - std::unique_ptr scene_delegate; - std::unique_ptr render_task_delegate; - std::unique_ptr free_camera_delegate; - std::unique_ptr simple_light_task_delegate; - std::unique_ptr engine; + /* The order is important due to deletion order */ + pxr::HdPluginRenderDelegateUniqueHandle render_delegate_; + std::unique_ptr render_index_; + std::unique_ptr scene_delegate_; + std::unique_ptr render_task_delegate_; + std::unique_ptr free_camera_delegate_; + std::unique_ptr simple_light_task_delegate_; + std::unique_ptr engine_; - pxr::HgiUniquePtr hgi; - pxr::HdDriver hgi_driver; + pxr::HgiUniquePtr hgi_; + pxr::HdDriver hgi_driver_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/final_engine.cc b/source/blender/render/hydra/final_engine.cc index 54e55fbbfa0f..f2b8286fc04c 100644 --- a/source/blender/render/hydra/final_engine.cc +++ b/source/blender/render/hydra/final_engine.cc @@ -16,27 +16,27 @@ void FinalEngine::sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) { - scene_delegate = std::make_unique( - render_index.get(), + scene_delegate_ = std::make_unique( + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::FINAL); - scene_delegate->populate(depsgraph, context); + scene_delegate_->populate(depsgraph, context); for (auto const &setting : render_settings) { - render_delegate->SetRenderSetting(setting.first, setting.second); + render_delegate_->SetRenderSetting(setting.first, setting.second); } } void FinalEngine::render(Depsgraph *depsgraph) { - Scene *scene = DEG_get_input_scene(depsgraph); - ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph); + const Scene *scene = DEG_get_evaluated_scene(depsgraph); + const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph); std::string scene_name(MAX_ID_FULL_NAME, 0); BKE_id_full_name_get(scene_name.data(), (ID *)scene, 0); std::string layer_name = view_layer->name; - RenderData &r = scene->r; + const RenderData &r = scene->r; pxr::GfVec4f border(0, 0, 1, 1); if (r.mode & R_BORDER) { border = pxr::GfVec4f(r.border.xmin, @@ -49,19 +49,19 @@ void FinalEngine::render(Depsgraph *depsgraph) pxr::GfCamera camera = 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, res[0], res[1])); - render_task_delegate->set_renderer_aov(pxr::HdAovTokens->color); - if (simple_light_task_delegate) { - simple_light_task_delegate->set_camera_path(free_camera_delegate->GetCameraId()); + free_camera_delegate_->SetCamera(camera); + render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), + pxr::GfVec4d(0, 0, res[0], res[1])); + render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); + if (simple_light_task_delegate_) { + simple_light_task_delegate_->set_camera_path(free_camera_delegate_->GetCameraId()); } pxr::HdTaskSharedPtrVector tasks; - if (simple_light_task_delegate) { - tasks.push_back(simple_light_task_delegate->get_task()); + if (simple_light_task_delegate_) { + tasks.push_back(simple_light_task_delegate_->get_task()); } - tasks.push_back(render_task_delegate->get_task()); + tasks.push_back(render_task_delegate_->get_task()); std::chrono::time_point time_begin = std::chrono::steady_clock::now(), time_current; @@ -70,17 +70,17 @@ void FinalEngine::render(Depsgraph *depsgraph) float percent_done = 0.0; std::map> render_images{ - {"Combined", std::vector(res[0] * res[1] * 4)}}; // 4 - number of channels + {"Combined", std::vector(res[0] * res[1] * 4)}}; /* 4 - number of channels. */ std::vector &pixels = render_images["Combined"]; { - // Release the GIL before calling into hydra, in case any hydra plugins call into python. + /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - engine->Execute(render_index.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks); } while (true) { - if (RE_engine_test_break(bl_engine)) { + if (RE_engine_test_break(bl_engine_)) { break; } @@ -94,15 +94,15 @@ void FinalEngine::render(Depsgraph *depsgraph) "Render Time: " + format_duration(elapsed_time) + " | Done: " + std::to_string(int(percent_done)) + "%"); - if (render_task_delegate->is_converged()) { + if (render_task_delegate_->is_converged()) { break; } - render_task_delegate->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); update_render_result(render_images, layer_name, res[0], res[1]); } - render_task_delegate->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); update_render_result(render_images, layer_name, res[0], res[1]); } @@ -124,7 +124,7 @@ void FinalEngine::update_render_result(std::map> int height) { RenderResult *result = RE_engine_begin_result( - bl_engine, 0, 0, width, height, layer_name.c_str(), nullptr); + bl_engine_, 0, 0, width, height, layer_name.c_str(), nullptr); /* TODO: only for the first render layer */ RenderLayer *layer = (RenderLayer *)result->layers.first; @@ -138,25 +138,25 @@ void FinalEngine::update_render_result(std::map> sizeof(float) * pass->rectx * pass->recty * pass->channels); } - RE_engine_end_result(bl_engine, result, false, false, false); + RE_engine_end_result(bl_engine_, result, false, false, false); } void FinalEngine::notify_status(float progress, const std::string &title, const std::string &info) { - RE_engine_update_progress(bl_engine, progress); - RE_engine_update_stats(bl_engine, title.c_str(), info.c_str()); + RE_engine_update_progress(bl_engine_, progress); + RE_engine_update_stats(bl_engine_, title.c_str(), info.c_str()); } void FinalEngineGL::render(Depsgraph *depsgraph) { - Scene *scene = DEG_get_input_scene(depsgraph); - ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph); + const Scene *scene = DEG_get_evaluated_scene(depsgraph); + const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph); std::string scene_name(MAX_ID_FULL_NAME, 0); BKE_id_full_name_get(scene_name.data(), (ID *)scene, 0); std::string layer_name = view_layer->name; - RenderData &r = scene->r; + const RenderData &r = scene->r; pxr::GfVec4f border(0, 0, 1, 1); if (r.mode & R_BORDER) { border = pxr::GfVec4f(r.border.xmin, @@ -169,21 +169,21 @@ void FinalEngineGL::render(Depsgraph *depsgraph) pxr::GfCamera camera = 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, res[0], res[1])); - if (simple_light_task_delegate) { - simple_light_task_delegate->set_camera_path(free_camera_delegate->GetCameraId()); + free_camera_delegate_->SetCamera(camera); + render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), + pxr::GfVec4d(0, 0, res[0], res[1])); + if (simple_light_task_delegate_) { + simple_light_task_delegate_->set_camera_path(free_camera_delegate_->GetCameraId()); } pxr::HdTaskSharedPtrVector tasks; - if (simple_light_task_delegate) { + if (simple_light_task_delegate_) { /* TODO: Uncomment this and fix GL error: - invalid operation, reported from void __cdecl - pxrInternal_v0_22__pxrReserved__::HgiGLResourceBindings::BindResources(void) */ - // tasks.push_back(simple_light_task_delegate->get_task()); + * invalid operation, reported from void __cdecl + * pxrInternal_v0_22__pxrReserved__::HgiGLResourceBindings::BindResources(void) */ + // tasks.push_back(simple_light_task_delegate_->get_task()); } - tasks.push_back(render_task_delegate->get_task()); + tasks.push_back(render_task_delegate_->get_task()); std::chrono::time_point time_begin = std::chrono::steady_clock::now(), time_current; @@ -192,11 +192,11 @@ void FinalEngineGL::render(Depsgraph *depsgraph) float percent_done = 0.0; std::map> render_images{ - {"Combined", std::vector(res[0] * res[1] * 4)}}; // 4 - number of channels + {"Combined", std::vector(res[0] * res[1] * 4)}}; /* 4 - number of channels. */ std::vector &pixels = render_images["Combined"]; - GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fbHydraRenderFinal"); - GPUTexture *texture = GPU_texture_create_2d("texHydraRenderFinal", + GPUFrameBuffer *framebuffer = GPU_framebuffer_create("fb_hdyra_render_final"); + GPUTexture *texture = GPU_texture_create_2d("tex_hydra_render_final", res[0], res[1], 1, @@ -212,13 +212,13 @@ void FinalEngineGL::render(Depsgraph *depsgraph) GPU_framebuffer_clear_color_depth(framebuffer, clear_color, 1.0); { - // Release the GIL before calling into hydra, in case any hydra plugins call into python. + /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - engine->Execute(render_index.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks); } while (true) { - if (RE_engine_test_break(bl_engine)) { + if (RE_engine_test_break(bl_engine_)) { break; } @@ -232,7 +232,7 @@ void FinalEngineGL::render(Depsgraph *depsgraph) "Render Time: " + format_duration(elapsed_time) + " | Done: " + std::to_string(int(percent_done)) + "%"); - if (render_task_delegate->is_converged()) { + if (render_task_delegate_->is_converged()) { break; } diff --git a/source/blender/render/hydra/final_engine.h b/source/blender/render/hydra/final_engine.h index 28c1cf6c7cec..17473ad5ec74 100644 --- a/source/blender/render/hydra/final_engine.h +++ b/source/blender/render/hydra/final_engine.h @@ -12,6 +12,7 @@ namespace blender::render::hydra { class FinalEngine : public Engine { public: using Engine::Engine; + virtual void sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) override; @@ -25,13 +26,13 @@ class FinalEngine : public Engine { int height); void notify_status(float progress, const std::string &title, const std::string &info); - protected: - pxr::HdRenderSettingsMap render_settings; + pxr::HdRenderSettingsMap render_settings_; }; class FinalEngineGL : public FinalEngine { public: using FinalEngine::FinalEngine; + void render(Depsgraph *depsgraph) override; }; diff --git a/source/blender/render/hydra/preview_engine.cc b/source/blender/render/hydra/preview_engine.cc index ffb9891952e2..08b1f5b90c7d 100644 --- a/source/blender/render/hydra/preview_engine.cc +++ b/source/blender/render/hydra/preview_engine.cc @@ -3,77 +3,61 @@ #include "DEG_depsgraph_query.h" +#include "BLI_timer.h" #include "camera.h" #include "preview_engine.h" -#include "BLI_timer.h" namespace blender::render::hydra { const double LIFETIME = 180.0; -std::unique_ptr PreviewEngine::instance; +std::unique_ptr PreviewEngine::instance_; + +PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, + const std::string &render_delegate_id) +{ + if (!instance_) { + instance_ = std::make_unique(bl_engine, render_delegate_id); + } + if (BLI_timer_is_registered((uintptr_t)instance_.get())) { + /* Unregister timer while PreviewEngine is working */ + BLI_timer_unregister((uintptr_t)instance_.get()); + } + instance_->update(bl_engine, render_delegate_id); + + return instance_.get(); +} void PreviewEngine::schedule_free() { - instance->render_delegate->Stop(); + instance_->render_delegate_->Stop(); /* Register timer for schedule free PreviewEngine instance */ - BLI_timer_register((uintptr_t)instance.get(), - free_instance, - nullptr, - nullptr, - LIFETIME, - true); -} - -PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine, const std::string &render_delegate_id) -{ - if (!instance) { - instance = std::make_unique(bl_engine, render_delegate_id); - } - if (BLI_timer_is_registered((uintptr_t)instance.get())) { - /* Unregister timer while PreviewEngine is working */ - BLI_timer_unregister((uintptr_t)instance.get()); - } - instance->update(bl_engine, render_delegate_id); - - return instance.get(); -} - -double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) -{ - if (!instance->render_task_delegate->is_converged()) { - /* Restart timer if render isn't completed */ - return LIFETIME; - } - - CLOG_INFO(LOG_EN, 2, ""); - instance = nullptr; - return -1; + BLI_timer_register((uintptr_t)instance_.get(), free_instance, nullptr, nullptr, LIFETIME, true); } void PreviewEngine::sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) { - if (!scene_delegate) { - scene_delegate = std::make_unique( - render_index.get(), + if (!scene_delegate_) { + scene_delegate_ = std::make_unique( + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::PREVIEW); } - scene_delegate->clear(); - scene_delegate->populate(depsgraph, context); + scene_delegate_->clear(); + scene_delegate_->populate(depsgraph, context); for (auto const &setting : render_settings) { - render_delegate->SetRenderSetting(setting.first, setting.second); + render_delegate_->SetRenderSetting(setting.first, setting.second); } } void PreviewEngine::render(Depsgraph *depsgraph) { - Scene *scene = DEG_get_input_scene(depsgraph); - ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph); + const Scene *scene = DEG_get_evaluated_scene(depsgraph); + const ViewLayer *view_layer = DEG_get_evaluated_view_layer(depsgraph); std::string layer_name = view_layer->name; pxr::GfVec2i res(scene->r.xsch, scene->r.ysch); @@ -81,45 +65,58 @@ void PreviewEngine::render(Depsgraph *depsgraph) pxr::GfCamera camera = CameraData(scene->camera, res, pxr::GfVec4f(0, 0, 1, 1)).gf_camera(pxr::GfVec4f(0, 0, 1, 1)); - free_camera_delegate->SetCamera(camera); - render_task_delegate->set_camera_and_viewport(free_camera_delegate->GetCameraId(), - pxr::GfVec4d(0, 0, res[0], res[1])); - render_task_delegate->set_renderer_aov(pxr::HdAovTokens->color); + free_camera_delegate_->SetCamera(camera); + render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), + pxr::GfVec4d(0, 0, res[0], res[1])); + render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); pxr::HdTaskSharedPtrVector tasks; - if (simple_light_task_delegate) { - tasks.push_back(simple_light_task_delegate->get_task()); + if (simple_light_task_delegate_) { + tasks.push_back(simple_light_task_delegate_->get_task()); } - tasks.push_back(render_task_delegate->get_task()); + tasks.push_back(render_task_delegate_->get_task()); - std::vector pixels = std::vector(res[0] * res[1] * 4); // 4 - number of channels + std::vector pixels = std::vector(res[0] * res[1] * + 4); /* 4 - number of channels. */ { - // Release the GIL before calling into hydra, in case any hydra plugins call into python. + /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - engine->Execute(render_index.get(), &tasks); + engine_->Execute(render_index_.get(), &tasks); } while (true) { - if (RE_engine_test_break(bl_engine)) { + if (RE_engine_test_break(bl_engine_)) { break; } - if (render_task_delegate->is_converged()) { + if (render_task_delegate_->is_converged()) { break; } - render_task_delegate->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); update_render_result(layer_name, res[0], res[1], pixels); } - render_task_delegate->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); + render_task_delegate_->get_renderer_aov_data(pxr::HdAovTokens->color, pixels.data()); update_render_result(layer_name, res[0], res[1], pixels); } +double PreviewEngine::free_instance(uintptr_t uuid, void *user_data) +{ + if (!instance_->render_task_delegate_->is_converged()) { + /* Restart timer if render isn't completed */ + return LIFETIME; + } + + CLOG_INFO(LOG_EN, 2, ""); + instance_ = nullptr; + return -1; +} + void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_delegate_id) { - this->bl_engine = bl_engine; + this->bl_engine_ = bl_engine; /* TODO: recreate render_delegate when render_delegate_id is changed */ } @@ -129,13 +126,13 @@ void PreviewEngine::update_render_result(const std::string &layer_name, std::vector &pixels) { RenderResult *result = RE_engine_begin_result( - bl_engine, 0, 0, width, height, layer_name.c_str(), nullptr); + bl_engine_, 0, 0, width, height, layer_name.c_str(), nullptr); RenderLayer *layer = (RenderLayer *)result->layers.first; RenderPass *pass = (RenderPass *)layer->passes.first; memcpy(pass->rect, pixels.data(), sizeof(float) * pass->rectx * pass->recty * pass->channels); - RE_engine_end_result(bl_engine, result, false, false, false); + RE_engine_end_result(bl_engine_, result, false, false, false); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/preview_engine.h b/source/blender/render/hydra/preview_engine.h index 97ee9da93a3e..64e969eca368 100644 --- a/source/blender/render/hydra/preview_engine.h +++ b/source/blender/render/hydra/preview_engine.h @@ -8,28 +8,28 @@ namespace blender::render::hydra { class PreviewEngine : public FinalEngine { - private: - /* Singleton class instance */ - static std::unique_ptr instance; - static double free_instance(uintptr_t uuid, void *user_data); - public: - static PreviewEngine *get_instance(RenderEngine *bl_engine, const std::string &render_delegate_id); + using FinalEngine::FinalEngine; + + static PreviewEngine *get_instance(RenderEngine *bl_engine, + const std::string &render_delegate_id); static void schedule_free(); - using FinalEngine::FinalEngine; void sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) override; void render(Depsgraph *depsgraph) override; private: + /* Singleton class instance */ + static double free_instance(uintptr_t uuid, void *user_data); + static std::unique_ptr instance_; + void update(RenderEngine *bl_engine, const std::string &render_delegate_id); void update_render_result(const std::string &layer_name, int width, int height, std::vector &pixels); - }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/python.cc b/source/blender/render/hydra/python.cc index b97a31b15555..e445ead763a2 100644 --- a/source/blender/render/hydra/python.cc +++ b/source/blender/render/hydra/python.cc @@ -236,8 +236,8 @@ static PyMethodDef methods[] = { {"engine_create", engine_create_func, METH_VARARGS, ""}, {"engine_free", engine_free_func, METH_VARARGS, ""}, - {"engine_render", engine_render_func, METH_VARARGS, ""}, {"engine_sync", engine_sync_func, METH_VARARGS, ""}, + {"engine_render", engine_render_func, METH_VARARGS, ""}, {"engine_view_draw", engine_view_draw_func, METH_VARARGS, ""}, {NULL, NULL, 0, NULL}, diff --git a/source/blender/render/hydra/render_task_delegate.cc b/source/blender/render/hydra/render_task_delegate.cc index 250b86619da8..d5348aef4d7b 100644 --- a/source/blender/render/hydra/render_task_delegate.cc +++ b/source/blender/render/hydra/render_task_delegate.cc @@ -22,8 +22,8 @@ RenderTaskDelegate::RenderTaskDelegate(pxr::HdRenderIndex *parent_index, GetRenderIndex().GetChangeTracker().MarkTaskDirty(render_task_id, pxr::HdChangeTracker::DirtyRenderTags); - task_params.enableLighting = true; - task_params.alphaThreshold = 0.1f; + task_params_.enableLighting = true; + task_params_.alphaThreshold = 0.1f; } pxr::SdfPath RenderTaskDelegate::get_task_id() const @@ -36,32 +36,6 @@ pxr::SdfPath RenderTaskDelegate::get_aov_id(pxr::TfToken const &aov) const return GetDelegateID().AppendElementString("aov_" + aov.GetString()); } -pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) -{ - if (key == pxr::HdTokens->params) { - 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::HdRenderBufferDescriptor RenderTaskDelegate::GetRenderBufferDescriptor(pxr::SdfPath const &id) -{ - return buffer_descriptors[id]; -} - -pxr::TfTokenVector RenderTaskDelegate::GetTaskRenderTags(pxr::SdfPath const &task_id) -{ - return {pxr::HdRenderTagTokens->geometry}; -} - bool RenderTaskDelegate::is_converged() { pxr::HdTaskSharedPtr renderTask = GetRenderIndex().GetTask(get_task_id()); @@ -73,14 +47,14 @@ void RenderTaskDelegate::set_renderer_aov(pxr::TfToken const &aov) pxr::HdAovDescriptor aov_desc = GetRenderIndex().GetRenderDelegate()->GetDefaultAovDescriptor( aov); pxr::HdRenderBufferDescriptor desc( - pxr::GfVec3i(task_params.viewport[2], task_params.viewport[3], 1), + pxr::GfVec3i(task_params_.viewport[2], task_params_.viewport[3], 1), aov_desc.format, aov_desc.multiSampled); pxr::SdfPath buffer_id = get_aov_id(aov); - if (buffer_descriptors.find(buffer_id) == buffer_descriptors.end()) { + if (buffer_descriptors_.find(buffer_id) == buffer_descriptors_.end()) { GetRenderIndex().InsertBprim(pxr::HdPrimTypeTokens->renderBuffer, this, buffer_id); - buffer_descriptors[buffer_id] = desc; + buffer_descriptors_[buffer_id] = desc; GetRenderIndex().GetChangeTracker().MarkBprimDirty(buffer_id, pxr::HdRenderBuffer::DirtyDescription); @@ -88,13 +62,13 @@ void RenderTaskDelegate::set_renderer_aov(pxr::TfToken const &aov) binding.aovName = aov; binding.renderBufferId = buffer_id; binding.aovSettings = aov_desc.aovSettings; - task_params.aovBindings.push_back(binding); + 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; + else if (buffer_descriptors_[buffer_id] != desc) { + buffer_descriptors_[buffer_id] = desc; GetRenderIndex().GetChangeTracker().MarkBprimDirty(buffer_id, pxr::HdRenderBuffer::DirtyDescription); } @@ -124,12 +98,38 @@ pxr::HdTaskSharedPtr RenderTaskDelegate::get_task() 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; + 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); } } +pxr::VtValue RenderTaskDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) +{ + if (key == pxr::HdTokens->params) { + 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::HdRenderBufferDescriptor RenderTaskDelegate::GetRenderBufferDescriptor(pxr::SdfPath const &id) +{ + return buffer_descriptors_[id]; +} + +pxr::TfTokenVector RenderTaskDelegate::GetTaskRenderTags(pxr::SdfPath const &task_id) +{ + return {pxr::HdRenderTagTokens->geometry}; +} + } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/render_task_delegate.h b/source/blender/render/hydra/render_task_delegate.h index f7119fc83e2a..f8a1a956816c 100644 --- a/source/blender/render/hydra/render_task_delegate.h +++ b/source/blender/render/hydra/render_task_delegate.h @@ -16,10 +16,6 @@ class RenderTaskDelegate : public pxr::HdSceneDelegate { pxr::SdfPath get_task_id() const; pxr::SdfPath get_aov_id(pxr::TfToken const &aov) const; - pxr::VtValue Get(pxr::SdfPath const &id, pxr::TfToken const &key) override; - pxr::HdRenderBufferDescriptor GetRenderBufferDescriptor(pxr::SdfPath const &id) override; - pxr::TfTokenVector GetTaskRenderTags(pxr::SdfPath const &taskId) override; - bool is_converged(); void set_renderer_aov(pxr::TfToken const &aovId); pxr::HdRenderBuffer *get_renderer_aov(pxr::TfToken const &id); @@ -28,10 +24,15 @@ class RenderTaskDelegate : public pxr::HdSceneDelegate { 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::HdRenderBufferDescriptor GetRenderBufferDescriptor(pxr::SdfPath const &id) override; + pxr::TfTokenVector GetTaskRenderTags(pxr::SdfPath const &taskId) override; + private: - pxr::HdxRenderTaskParams task_params; + pxr::HdxRenderTaskParams task_params_; pxr::TfHashMap - buffer_descriptors; + buffer_descriptors_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc index 38346736b41e..b86bf0ab200b 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.cc @@ -16,31 +16,205 @@ CLG_LOGREF_DECLARE_GLOBAL(LOG_BSD, "rhd.bsd"); BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id, BlenderSceneDelegate::EngineType engine_type) - : HdSceneDelegate(parent_index, delegate_id), - engine_type(engine_type), - depsgraph(nullptr), - context(nullptr), - view3d(nullptr) + : HdSceneDelegate(parent_index, delegate_id), engine_type(engine_type) { } -void BlenderSceneDelegate::update_world() +void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) { - World *world = scene->world; - if (!world_data) { - if (world) { - world_data = WorldData::create(this, world, context); + bool is_populated = depsgraph_ != nullptr; + + depsgraph_ = deps; + context_ = cont; + scene_ = DEG_get_input_scene(depsgraph_); + view3d_ = CTX_wm_view3d(context_); + + if (!is_populated) { + /* Export initial objects */ + update_collection(false, false); + update_world(); + return; + } + + /* Working with updates */ + bool do_update_collection = false; + bool do_update_visibility = false; + bool do_update_world = false; + + unsigned int scene_recalc = ((ID *)scene_)->recalc; + if (scene_recalc) { + /* Checking scene updates */ + CLOG_INFO(LOG_BSD, + 2, + "Update: %s [%s]", + ((ID *)scene_)->name, + std::bitset<32>(scene_recalc).to_string().c_str()); + + if (scene_recalc & ID_RECALC_BASE_FLAGS) { + do_update_visibility = true; + } + if (scene_recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { + do_update_collection = true; + } + if (scene_recalc & ID_RECALC_AUDIO_VOLUME) { + if ((scene_->world && !world_data_) || (!scene_->world && world_data_)) { + do_update_world = true; + } + } + if (do_update_collection || do_update_visibility) { + update_collection(do_update_collection, do_update_visibility); } } - else { - if (world) { - world_data->update(world); - } - else { - world_data->remove(); - world_data = nullptr; + + /* Checking other objects updates */ + DEGIDIterData data = {0}; + data.graph = depsgraph_; + data.only_updated = true; + ITER_BEGIN ( + DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { + + CLOG_INFO( + LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str()); + + switch (GS(id->name)) { + case ID_OB: { + Object *object = (Object *)id; + if (!ObjectData::is_supported(object)) { + break; + } + add_update_object(object); + } break; + + case ID_MA: { + MaterialData *mat_data = material_data(MaterialData::prim_id(this, (Material *)id)); + if (mat_data) { + mat_data->update(); + } + } break; + + case ID_WO: { + if (id->recalc & ID_RECALC_SHADING) { + do_update_world = true; + } + } break; + + default: + break; } } + ITER_END; + + if (do_update_world) { + update_world(); + } +} + +void BlenderSceneDelegate::clear() +{ + for (auto &it : materials_) { + it.second->remove(); + } + + for (auto &it : objects_) { + it.second->remove(); + } + + materials_.clear(); + objects_.clear(); +} + +pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id) +{ + CLOG_INFO(LOG_BSD, 3, "%s", id.GetText()); + MeshData *m_data = mesh_data(id); + return m_data->mesh_topology(); +} + +pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id) +{ + CLOG_INFO(LOG_BSD, 3, "%s", id.GetText()); + ObjectData *obj_data = object_data(id); + if (obj_data) { + return obj_data->transform(); + } + + if (id == WorldData::prim_id(this)) { + return world_data_->transform(); + } + + InstancerData *i_data = instancer_data(id); + if (i_data) { + return i_data->transform(); + } + + return pxr::GfMatrix4d(); +} + +pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) +{ + CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText()); + + ObjectData *obj_data = object_data(id); + if (obj_data) { + return obj_data->get_data(key); + } + + MaterialData *mat_data = material_data(id); + if (mat_data) { + return mat_data->get_data(key); + } + + InstancerData *i_data = instancer_data(id); + if (i_data) { + return i_data->get_data(key); + } + + return pxr::VtValue(); +} + +pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id, + pxr::TfToken const &key) +{ + LightData *l_data = light_data(id); + if (l_data) { + return l_data->get_data(key); + } + if (id == WorldData::prim_id(this)) { + return world_data_->get_data(key); + } + return pxr::VtValue(); +} + +pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors( + pxr::SdfPath const &id, pxr::HdInterpolation interpolation) +{ + CLOG_INFO(LOG_BSD, 3, "%s, %d", id.GetText(), interpolation); + + MeshData *m_data = mesh_data(id); + if (m_data) { + return m_data->primvar_descriptors(interpolation); + } + + InstancerData *i_data = instancer_data(id); + if (i_data) { + return i_data->instancer_primvar_descriptors(interpolation); + } + + return pxr::HdPrimvarDescriptorVector(); +} + +pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id) +{ + return mesh_data(rprim_id)->material_id(); +} + +pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id) +{ + MaterialData *mat_data = material_data(id); + if (mat_data) { + return mat_data->get_material_resource(); + } + return pxr::VtValue(); } bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id) @@ -85,128 +259,10 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const & return i_data->transform(); } -void BlenderSceneDelegate::update_collection(bool remove, bool visibility) -{ - if (visibility) { - /* Check and update visibility */ - for (auto &obj : objects) { - obj.second->update_visibility(view3d); - } - } - - /* Export of new visible objects which were not exported before */ - std::set available_objects; - pxr::SdfPath id; - - DEGObjectIterSettings settings = {0}; - settings.depsgraph = depsgraph; - settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | - DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; - DEGObjectIterData data = {0}; - data.settings = &settings; - data.graph = settings.depsgraph; - data.flag = settings.flags; - ITER_BEGIN (DEG_iterator_objects_begin, - DEG_iterator_objects_next, - DEG_iterator_objects_end, - &data, - Object *, - object) { - - CLOG_INFO(LOG_BSD, 2, "Add %s", ((ID *)object)->name); - if (!ObjectData::supported(object)) { - continue; - } - - id = ObjectData::prim_id(this, object); - if (remove) { - available_objects.insert(id); - if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) { - available_objects.insert(InstancerData::prim_id(this, object)); - } - } - - if (!object_data(id)) { - add_update_object(object); - } - } - ITER_END; - - if (remove) { - /* remove unused objects */ - for (auto it = objects.begin(); it != objects.end(); ++it) { - if (available_objects.find(it->first) != available_objects.end()) { - continue; - } - it->second->remove(); - objects.erase(it); - it = objects.begin(); - } - - /* remove unused materials */ - std::set available_materials; - for (auto &obj : objects) { - MeshData *m_data = dynamic_cast(obj.second.get()); - if (!m_data) { - continue; - } - pxr::SdfPath mat_id = m_data->material_id(); - if (!mat_id.IsEmpty()) { - available_materials.insert(mat_id); - } - } - for (auto it = materials.begin(); it != materials.end(); ++it) { - if (available_materials.find(it->first) != available_materials.end()) { - continue; - } - it->second->remove(); - materials.erase(it); - it = materials.begin(); - } - } -} - -void BlenderSceneDelegate::add_update_object(Object *object) -{ - if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) { - add_update_instancer(object); - } - InstancerData *i_data = instancer_data(object); - if (i_data) { - i_data->update(); - } - - pxr::SdfPath id = ObjectData::prim_id(this, object); - ObjectData *obj_data = object_data(id); - if (obj_data) { - obj_data->update(); - return; - } - if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) { - return; - } - objects[id] = ObjectData::create(this, object); - obj_data = object_data(id); - obj_data->update_visibility(view3d); -} - -void BlenderSceneDelegate::add_update_instancer(Object *object) -{ - pxr::SdfPath id = InstancerData::prim_id(this, object); - InstancerData *i_data = instancer_data(id, true); - if (i_data) { - i_data->update(); - return; - } - objects[id] = InstancerData::create(this, object); - i_data = instancer_data(id, true); - i_data->update_visibility(view3d); -} - ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) { - auto it = objects.find(id); - if (it == objects.end()) { + auto it = objects_.find(id); + if (it == objects_.end()) { return nullptr; } return it->second.get(); @@ -224,8 +280,8 @@ LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) { - auto it = materials.find(id); - if (it == materials.end()) { + auto it = materials_.find(id); + if (it == materials_.end()) { return nullptr; } return it->second.get(); @@ -242,7 +298,7 @@ InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool InstancerData *BlenderSceneDelegate::instancer_data(Object *object) { InstancerData *i_data; - for (auto &it : objects) { + for (auto &it : objects_) { i_data = dynamic_cast(it.second.get()); if (i_data && i_data->is_base(object)) { return i_data; @@ -251,201 +307,141 @@ InstancerData *BlenderSceneDelegate::instancer_data(Object *object) return nullptr; } -void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) +void BlenderSceneDelegate::add_update_object(Object *object) { - bool is_populated = depsgraph != nullptr; + if ((object->transflag & OB_DUPLI) && InstancerData::is_supported(object)) { + add_update_instancer(object); + } + InstancerData *i_data = instancer_data(object); + if (i_data) { + i_data->update(); + } - depsgraph = deps; - context = cont; - scene = DEG_get_input_scene(depsgraph); - view3d = CTX_wm_view3d(context); - - if (!is_populated) { - /* Export initial objects */ - update_collection(false, false); - update_world(); + pxr::SdfPath id = ObjectData::prim_id(this, object); + ObjectData *obj_data = object_data(id); + if (obj_data) { + obj_data->update(); return; } + if (view3d_ && !BKE_object_is_visible_in_viewport(view3d_, object)) { + return; + } + objects_[id] = ObjectData::create(this, object); + obj_data = object_data(id); + obj_data->update_visibility(view3d_); +} - /* Working with updates */ - bool do_update_collection = false; - bool do_update_visibility = false; - bool do_update_world = false; +void BlenderSceneDelegate::add_update_instancer(Object *object) +{ + pxr::SdfPath id = InstancerData::prim_id(this, object); + InstancerData *i_data = instancer_data(id, true); + if (i_data) { + i_data->update(); + return; + } + objects_[id] = InstancerData::create(this, object); + i_data = instancer_data(id, true); + i_data->update_visibility(view3d_); +} - unsigned int scene_recalc = ((ID *)scene)->recalc; - if (scene_recalc) { - /* Checking scene updates */ - CLOG_INFO(LOG_BSD, - 2, - "Update: %s [%s]", - ((ID *)scene)->name, - std::bitset<32>(scene_recalc).to_string().c_str()); +void BlenderSceneDelegate::update_world() +{ + World *world = scene_->world; + if (!world_data_) { + if (world) { + world_data_ = WorldData::create(this, world, context_); + } + } + else { + if (world) { + world_data_->update(world); + } + else { + world_data_->remove(); + world_data_ = nullptr; + } + } +} - if (scene_recalc & ID_RECALC_BASE_FLAGS) { - do_update_visibility = true; - } - if (scene_recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) { - do_update_collection = true; - } - if (scene_recalc & ID_RECALC_AUDIO_VOLUME) { - if ((scene->world && !world_data) || (!scene->world && world_data)) { - do_update_world = true; - } - } - if (do_update_collection || do_update_visibility) { - update_collection(do_update_collection, do_update_visibility); +void BlenderSceneDelegate::update_collection(bool remove, bool visibility) +{ + if (visibility) { + /* Check and update visibility */ + for (auto &obj : objects_) { + obj.second->update_visibility(view3d_); } } - /* Checking other objects updates */ - DEGIDIterData data = {0}; - data.graph = depsgraph; - data.only_updated = true; - ITER_BEGIN ( - DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { + /* Export of new visible objects which were not exported before */ + std::set available_objects; + pxr::SdfPath id; - CLOG_INFO( - LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str()); + DEGObjectIterSettings settings = {0}; + settings.depsgraph = depsgraph_; + settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | + DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; + DEGObjectIterData data = {0}; + data.settings = &settings; + data.graph = settings.depsgraph; + data.flag = settings.flags; + ITER_BEGIN (DEG_iterator_objects_begin, + DEG_iterator_objects_next, + DEG_iterator_objects_end, + &data, + Object *, + object) { - switch (GS(id->name)) { - case ID_OB: { - Object *object = (Object *)id; - if (!ObjectData::supported(object)) { - break; - } - add_update_object(object); - } break; + CLOG_INFO(LOG_BSD, 2, "Add %s", ((ID *)object)->name); + if (!ObjectData::is_supported(object)) { + continue; + } - case ID_MA: { - MaterialData *mat_data = material_data(MaterialData::prim_id(this, (Material *)id)); - if (mat_data) { - mat_data->update(); - } - } break; + id = ObjectData::prim_id(this, object); + if (remove) { + available_objects.insert(id); + if ((object->transflag & OB_DUPLI) && InstancerData::is_supported(object)) { + available_objects.insert(InstancerData::prim_id(this, object)); + } + } - case ID_WO: { - if (id->recalc & ID_RECALC_SHADING) { - do_update_world = true; - } - } break; - - default: - break; + if (!object_data(id)) { + add_update_object(object); } } ITER_END; - if (do_update_world) { - update_world(); + if (remove) { + /* remove unused objects */ + for (auto it = objects_.begin(); it != objects_.end(); ++it) { + if (available_objects.find(it->first) != available_objects.end()) { + continue; + } + it->second->remove(); + objects_.erase(it); + it = objects_.begin(); + } + + /* remove unused materials */ + std::set available_materials; + for (auto &obj : objects_) { + MeshData *m_data = dynamic_cast(obj.second.get()); + if (!m_data) { + continue; + } + pxr::SdfPath mat_id = m_data->material_id(); + if (!mat_id.IsEmpty()) { + available_materials.insert(mat_id); + } + } + for (auto it = materials_.begin(); it != materials_.end(); ++it) { + if (available_materials.find(it->first) != available_materials.end()) { + continue; + } + it->second->remove(); + materials_.erase(it); + it = materials_.begin(); + } } } -pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id) -{ - CLOG_INFO(LOG_BSD, 3, "%s", id.GetText()); - MeshData *m_data = mesh_data(id); - return m_data->mesh_topology(); -} - -pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) -{ - CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText()); - - ObjectData *obj_data = object_data(id); - if (obj_data) { - return obj_data->get_data(key); - } - - MaterialData *mat_data = material_data(id); - if (mat_data) { - return mat_data->get_data(key); - } - - InstancerData *i_data = instancer_data(id); - if (i_data) { - return i_data->get_data(key); - } - - return pxr::VtValue(); -} - -pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors( - pxr::SdfPath const &id, pxr::HdInterpolation interpolation) -{ - CLOG_INFO(LOG_BSD, 3, "%s, %d", id.GetText(), interpolation); - - MeshData *m_data = mesh_data(id); - if (m_data) { - return m_data->primvar_descriptors(interpolation); - } - - InstancerData *i_data = instancer_data(id); - if (i_data) { - return i_data->instancer_primvar_descriptors(interpolation); - } - - return pxr::HdPrimvarDescriptorVector(); -} - -pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id) -{ - return mesh_data(rprim_id)->material_id(); -} - -pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id) -{ - MaterialData *mat_data = material_data(id); - if (mat_data) { - return mat_data->material_resource(); - } - return pxr::VtValue(); -} - -pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id) -{ - CLOG_INFO(LOG_BSD, 3, "%s", id.GetText()); - ObjectData *obj_data = object_data(id); - if (obj_data) { - return obj_data->transform(); - } - - if (id == WorldData::prim_id(this)) { - return world_data->transform(); - } - - InstancerData *i_data = instancer_data(id); - if (i_data) { - return i_data->transform(); - } - - return pxr::GfMatrix4d(); -} - -pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id, - pxr::TfToken const &key) -{ - LightData *l_data = light_data(id); - if (l_data) { - return l_data->get_data(key); - } - if (id == WorldData::prim_id(this)) { - return world_data->get_data(key); - } - return pxr::VtValue(); -} - -void BlenderSceneDelegate::clear() -{ - for (auto &it : materials) { - it.second->remove(); - } - - for (auto &it : objects) { - it.second->remove(); - } - - materials.clear(); - objects.clear(); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h index 0ee8bb3d0f21..48c84e987e14 100644 --- a/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h +++ b/source/blender/render/hydra/scene_delegate/blender_scene_delegate.h @@ -27,7 +27,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { public: enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; - BlenderSceneDelegate(pxr::HdRenderIndex *render_index, + BlenderSceneDelegate(pxr::HdRenderIndex *parent_index, pxr::SdfPath const &delegate_id, BlenderSceneDelegate::EngineType engine_type); ~BlenderSceneDelegate() override = default; @@ -35,7 +35,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { void populate(Depsgraph *depsgraph, bContext *context); void clear(); - // delegate methods + /* Delegate methods */ pxr::HdMeshTopology GetMeshTopology(pxr::SdfPath const &id) override; pxr::GfMatrix4d GetTransform(pxr::SdfPath const &id) override; pxr::VtValue Get(pxr::SdfPath const &id, pxr::TfToken const &key) override; @@ -66,15 +66,14 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate { void update_world(); void update_collection(bool remove, bool visibility); - private: - Depsgraph *depsgraph; - bContext *context; - Scene *scene; - View3D *view3d; + Depsgraph *depsgraph_ = nullptr; + bContext *context_ = nullptr; + View3D *view3d_ = nullptr; + Scene *scene_ = nullptr; - ObjectDataMap objects; - MaterialDataMap materials; - std::unique_ptr world_data; + ObjectDataMap objects_; + MaterialDataMap materials_; + std::unique_ptr world_data_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/id.cc b/source/blender/render/hydra/scene_delegate/id.cc index 7c0a28be9de2..c2f048d796e0 100644 --- a/source/blender/render/hydra/scene_delegate/id.cc +++ b/source/blender/render/hydra/scene_delegate/id.cc @@ -9,7 +9,7 @@ namespace blender::render::hydra { IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id) - : scene_delegate(scene_delegate), id(id) + : scene_delegate_(scene_delegate), id_(id) { } diff --git a/source/blender/render/hydra/scene_delegate/id.h b/source/blender/render/hydra/scene_delegate/id.h index 8d36f8e2d3c1..5ece38101c9b 100644 --- a/source/blender/render/hydra/scene_delegate/id.h +++ b/source/blender/render/hydra/scene_delegate/id.h @@ -27,9 +27,9 @@ class IdData { template const T get_data(pxr::TfToken const &key) const; protected: - BlenderSceneDelegate *scene_delegate; - ID *id; - pxr::SdfPath p_id; + BlenderSceneDelegate *scene_delegate_; + ID *id_; + pxr::SdfPath p_id_; }; template const T IdData::get_data(pxr::TfToken const &key) const diff --git a/source/blender/render/hydra/scene_delegate/instancer.cc b/source/blender/render/hydra/scene_delegate/instancer.cc index 8c5cd75e72e8..8e85c641841f 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.cc +++ b/source/blender/render/hydra/scene_delegate/instancer.cc @@ -9,7 +9,16 @@ namespace blender::render::hydra { -bool InstancerData::supported(Object *object) +InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object) + : MeshData(scene_delegate, object), parent_obj_(object) +{ + id_ = nullptr; + p_id_ = prim_id(scene_delegate, object); + instancer_id = p_id_.AppendElementString("Instancer"); + CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj_)->name, instancer_id.GetText()); +} + +bool InstancerData::is_supported(Object *object) { switch (object->type) { case OB_MESH: @@ -43,23 +52,75 @@ pxr::SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object return scene_delegate->GetDelegateID().AppendElementString(str); } -InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object) - : MeshData(scene_delegate, object), parent_obj(object) -{ - id = nullptr; - p_id = prim_id(scene_delegate, object); - instancer_id = p_id.AppendElementString("Instancer"); - CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj)->name, instancer_id.GetText()); -} - void InstancerData::init() { - CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name); set_instances(); MeshData::init(); } +void InstancerData::insert() +{ + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name); + MeshData::insert(); + + if (face_vertex_counts_.empty()) { + return; + } + scene_delegate_->GetRenderIndex().InsertInstancer(scene_delegate_, instancer_id); +} + +void InstancerData::remove() +{ + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name); + + if (!scene_delegate_->GetRenderIndex().HasInstancer(instancer_id)) { + return; + } + scene_delegate_->GetRenderIndex().RemoveInstancer(instancer_id); + + MeshData::remove(); +} + +void InstancerData::update() +{ + CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name); + + pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; + unsigned int recalc = ((ID *)parent_obj_)->recalc; + + Object *object = (Object *)id_; + if ((id_->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { + init(); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + p_id_, pxr::HdChangeTracker::AllDirty); + return; + } + + if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj_->data)->recalc & ID_RECALC_GEOMETRY)) { + init(); + bits |= pxr::HdChangeTracker::AllDirty; + } + else if (recalc & ID_RECALC_TRANSFORM || id_->recalc & ID_RECALC_TRANSFORM) { + set_instances(); + bits |= pxr::HdChangeTracker::DirtyTransform; + } + if (bits != pxr::HdChangeTracker::Clean) { + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits); + } +} + +pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const +{ + CLOG_INFO(LOG_BSD, 3, "%s [%s]", id_->name, key.GetText()); + + if (key == pxr::HdInstancerTokens->instanceTransform) { + return pxr::VtValue(transforms_); + } + return MeshData::get_data(key); +} + pxr::GfMatrix4d InstancerData::transform() { return pxr::GfMatrix4d(1.0); @@ -72,25 +133,15 @@ bool InstancerData::update_visibility(View3D *view3d) } bool prev_visible = visible; - visible = BKE_object_is_visible_in_viewport(view3d, parent_obj); + visible = BKE_object_is_visible_in_viewport(view3d, parent_obj_); bool ret = visible != prev_visible; if (ret) { - scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - p_id, pxr::HdChangeTracker::DirtyVisibility); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + p_id_, pxr::HdChangeTracker::DirtyVisibility); } return ret; } -pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const -{ - CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText()); - - if (key == pxr::HdInstancerTokens->instanceTransform) { - return pxr::VtValue(transforms); - } - return MeshData::get_data(key); -} - pxr::HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors( pxr::HdInterpolation interpolation) { @@ -104,7 +155,7 @@ pxr::HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors( pxr::VtIntArray InstancerData::instance_indices() { - pxr::VtIntArray ret(transforms.size()); + pxr::VtIntArray ret(transforms_.size()); for (size_t i = 0; i < ret.size(); ++i) { ret[i] = i; } @@ -113,82 +164,32 @@ pxr::VtIntArray InstancerData::instance_indices() bool InstancerData::is_base(Object *object) const { - return (ID *)object == id; + return (ID *)object == id_; } bool InstancerData::set_instances() { - ID *prev_id = id; - id = nullptr; - transforms.clear(); - ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); + ID *prev_id = id_; + id_ = nullptr; + transforms_.clear(); + ListBase *lb = object_duplilist( + scene_delegate_->depsgraph_, scene_delegate_->scene_, parent_obj_); LISTBASE_FOREACH (DupliObject *, dupli, lb) { - if (!id) { + if (!id_) { /* TODO: We create instances only for object in first dupli. Instances should be created for all objects */ - id = (ID *)dupli->ob; + id_ = (ID *)dupli->ob; } - if (id != (ID *)dupli->ob) { + if (id_ != (ID *)dupli->ob) { continue; } - transforms.push_back(gf_matrix_from_transform(dupli->mat)); + transforms_.push_back(gf_matrix_from_transform(dupli->mat)); CLOG_INFO( - LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id); + LOG_BSD, 2, "Instance %s (%s) %d", id_->name, ((ID *)dupli->ob)->name, dupli->random_id); } free_object_duplilist(lb); - return id != prev_id; -} - -void InstancerData::insert() -{ - CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); - MeshData::insert(); - - if (face_vertex_counts.empty()) { - return; - } - scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id); -} - -void InstancerData::remove() -{ - CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); - - if (!scene_delegate->GetRenderIndex().HasInstancer(instancer_id)) { - return; - } - scene_delegate->GetRenderIndex().RemoveInstancer(instancer_id); - - MeshData::remove(); -} - -void InstancerData::update() -{ - CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); - - pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - unsigned int recalc = ((ID *)parent_obj)->recalc; - - Object *object = (Object *)id; - if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { - init(); - scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - p_id, pxr::HdChangeTracker::AllDirty); - return; - } - - if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj->data)->recalc & ID_RECALC_GEOMETRY)) { - init(); - bits |= pxr::HdChangeTracker::AllDirty; - } - else if (recalc & ID_RECALC_TRANSFORM || id->recalc & ID_RECALC_TRANSFORM) { - set_instances(); - bits |= pxr::HdChangeTracker::DirtyTransform; - } - if (bits != pxr::HdChangeTracker::Clean) { - scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits); - } + return id_ != prev_id; } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/instancer.h b/source/blender/render/hydra/scene_delegate/instancer.h index 0e33f1ac017a..cdff32c7b62b 100644 --- a/source/blender/render/hydra/scene_delegate/instancer.h +++ b/source/blender/render/hydra/scene_delegate/instancer.h @@ -11,32 +11,33 @@ namespace blender::render::hydra { class InstancerData : public MeshData { public: - static bool supported(Object *object); + InstancerData(BlenderSceneDelegate *scene_delegate, Object *object); + + static bool is_supported(Object *object); static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object); - InstancerData(BlenderSceneDelegate *scene_delegate, Object *object); - void init() override; void insert() override; void remove() override; void update() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::GfMatrix4d transform() override; bool update_visibility(View3D *view3d) override; pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::VtIntArray instance_indices(); - bool is_base(Object *object) const; + pxr::SdfPath instancer_id; private: - Object *parent_obj; - pxr::VtMatrix4dArray transforms; - bool set_instances(); + + Object *parent_obj_; + pxr::VtMatrix4dArray transforms_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/light.cc b/source/blender/render/hydra/scene_delegate/light.cc index 3b104e7f11e1..ca99694a57de 100644 --- a/source/blender/render/hydra/scene_delegate/light.cc +++ b/source/blender/render/hydra/scene_delegate/light.cc @@ -17,72 +17,138 @@ namespace blender::render::hydra { LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object) : ObjectData(scene_delegate, object) { - CLOG_INFO(LOG_BSD, 2, "%s id=%s", id->name, p_id.GetText()); + CLOG_INFO(LOG_BSD, 2, "%s id=%s", id_->name, p_id_.GetText()); } void LightData::init() { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); - Light *light = (Light *)((Object *)id)->data; - data.clear(); + Light *light = (Light *)((Object *)id_)->data; + data_.clear(); float intensity = light->energy; - if (scene_delegate->engine_type == BlenderSceneDelegate::EngineType::PREVIEW) { + if (scene_delegate_->engine_type == BlenderSceneDelegate::EngineType::PREVIEW) { intensity *= 0.001; } - data[pxr::HdLightTokens->intensity] = intensity; + data_[pxr::HdLightTokens->intensity] = intensity; - data[pxr::HdLightTokens->color] = pxr::GfVec3f(light->r, light->g, light->b); + data_[pxr::HdLightTokens->color] = pxr::GfVec3f(light->r, light->g, light->b); switch (light->type) { case LA_LOCAL: - data[pxr::HdLightTokens->radius] = light->area_size / 2; + data_[pxr::HdLightTokens->radius] = light->area_size / 2; break; case LA_SUN: - data[pxr::HdLightTokens->angle] = light->sun_angle * 180.0 / M_PI; + data_[pxr::HdLightTokens->angle] = light->sun_angle * 180.0 / M_PI; break; case LA_SPOT: - data[pxr::HdLightTokens->shapingConeAngle] = light->spotsize / 2; - data[pxr::HdLightTokens->shapingConeSoftness] = light->spotblend; - data[pxr::UsdLuxTokens->treatAsPoint] = true; + data_[pxr::HdLightTokens->shapingConeAngle] = light->spotsize / 2; + data_[pxr::HdLightTokens->shapingConeSoftness] = light->spotblend; + data_[pxr::UsdLuxTokens->treatAsPoint] = true; break; case LA_AREA: switch (light->area_shape) { case LA_AREA_SQUARE: - data[pxr::HdLightTokens->width] = light->area_size; - data[pxr::HdLightTokens->height] = light->area_size; + data_[pxr::HdLightTokens->width] = light->area_size; + data_[pxr::HdLightTokens->height] = light->area_size; break; case LA_AREA_RECT: - data[pxr::HdLightTokens->width] = light->area_size; - data[pxr::HdLightTokens->height] = light->area_sizey; + data_[pxr::HdLightTokens->width] = light->area_size; + data_[pxr::HdLightTokens->height] = light->area_sizey; break; case LA_AREA_DISK: - data[pxr::HdLightTokens->radius] = light->area_size / 2; + data_[pxr::HdLightTokens->radius] = light->area_size / 2; break; case LA_AREA_ELLIPSE: - data[pxr::HdLightTokens->radius] = (light->area_size + light->area_sizey) / 4; + data_[pxr::HdLightTokens->radius] = (light->area_size + light->area_sizey) / 4; break; default: break; } - data[pxr::HdLightTokens->normalize] = true; + data_[pxr::HdLightTokens->normalize] = true; break; default: break; } - p_type = prim_type(light); + p_type_ = prim_type(light); /* TODO: temporary value, it should be delivered through Python UI */ - data[pxr::HdLightTokens->exposure] = 1.0f; + data_[pxr::HdLightTokens->exposure] = 1.0f; +} + +void LightData::insert() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().InsertSprim(p_type_, scene_delegate_, p_id_); +} + +void LightData::remove() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().RemoveSprim(p_type_, p_id_); +} + +void LightData::update() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + + Light *light = (Light *)((Object *)id_)->data; + if (prim_type(light) != p_type_) { + remove(); + init(); + insert(); + return; + } + + pxr::HdDirtyBits bits = pxr::HdLight::Clean; + if (id_->recalc & ID_RECALC_GEOMETRY) { + init(); + bits = pxr::HdLight::AllDirty; + } + else if (id_->recalc & ID_RECALC_TRANSFORM) { + bits = pxr::HdLight::DirtyTransform; + } + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_, bits); +} + +pxr::VtValue LightData::get_data(pxr::TfToken const &key) const +{ + pxr::VtValue ret; + auto it = data_.find(key); + if (it != data_.end()) { + ret = it->second; + } + else { + std::string n = key.GetString(); + if (boost::algorithm::contains(n, "object:visibility:")) { + if (boost::algorithm::ends_with(n, "camera") || boost::algorithm::ends_with(n, "shadow")) { + ret = false; + } + else { + ret = true; + } + } + } + return ret; +} + +bool LightData::update_visibility(View3D *view3d) +{ + bool ret = ObjectData::update_visibility(view3d); + if (ret) { + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_, + pxr::HdLight::DirtyParams); + } + return ret; } pxr::TfToken LightData::prim_type(Light *light) @@ -121,70 +187,4 @@ pxr::TfToken LightData::prim_type(Light *light) return ret; } -pxr::VtValue LightData::get_data(pxr::TfToken const &key) const -{ - pxr::VtValue ret; - auto it = data.find(key); - if (it != data.end()) { - ret = it->second; - } - else { - std::string n = key.GetString(); - if (boost::algorithm::contains(n, "object:visibility:")) { - if (boost::algorithm::ends_with(n, "camera") || boost::algorithm::ends_with(n, "shadow")) { - ret = false; - } - else { - ret = true; - } - } - } - return ret; -} - -bool LightData::update_visibility(View3D *view3d) -{ - bool ret = ObjectData::update_visibility(view3d); - if (ret) { - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, - pxr::HdLight::DirtyParams); - } - return ret; -} - -void LightData::insert() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().InsertSprim(p_type, scene_delegate, p_id); -} - -void LightData::remove() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().RemoveSprim(p_type, p_id); -} - -void LightData::update() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - - Light *light = (Light *)((Object *)id)->data; - if (prim_type(light) != p_type) { - remove(); - init(); - insert(); - return; - } - - pxr::HdDirtyBits bits = pxr::HdLight::Clean; - if (id->recalc & ID_RECALC_GEOMETRY) { - init(); - bits = pxr::HdLight::AllDirty; - } - else if (id->recalc & ID_RECALC_TRANSFORM) { - bits = pxr::HdLight::DirtyTransform; - } - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/light.h b/source/blender/render/hydra/scene_delegate/light.h index f351450c084b..4aa91040e761 100644 --- a/source/blender/render/hydra/scene_delegate/light.h +++ b/source/blender/render/hydra/scene_delegate/light.h @@ -21,13 +21,15 @@ class LightData : public ObjectData { void insert() override; void remove() override; void update() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; bool update_visibility(View3D *view3d) override; private: - std::map data; - pxr::TfToken p_type; pxr::TfToken prim_type(Light *light); + + std::map data_; + pxr::TfToken p_type_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/material.cc b/source/blender/render/hydra/scene_delegate/material.cc index 3e20c74ef966..d13b011e89b3 100644 --- a/source/blender/render/hydra/scene_delegate/material.cc +++ b/source/blender/render/hydra/scene_delegate/material.cc @@ -20,6 +20,13 @@ namespace blender::render::hydra { +MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material) + : IdData(scene_delegate, (ID *)material) +{ + p_id_ = prim_id(scene_delegate, material); + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id_->name, p_id_.GetText()); +} + std::unique_ptr MaterialData::create(BlenderSceneDelegate *scene_delegate, Material *material) { @@ -38,18 +45,11 @@ pxr::SdfPath MaterialData::prim_id(BlenderSceneDelegate *scene_delegate, Materia return scene_delegate->GetDelegateID().AppendElementString(str); } -MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material) - : IdData(scene_delegate, (ID *)material) -{ - p_id = prim_id(scene_delegate, material); - CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); -} - void MaterialData::init() { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); - material_network_map = pxr::VtValue(); + material_network_map_ = pxr::VtValue(); /* Call of python function hydra.export_mtlx() */ @@ -62,7 +62,7 @@ void MaterialData::init() func = PyDict_GetItemString(dict, "export_mtlx"); PointerRNA materialptr; - RNA_pointer_create(NULL, &RNA_Material, id, &materialptr); + RNA_pointer_create(NULL, &RNA_Material, id_, &materialptr); PyObject *material = pyrna_struct_CreatePyObject(&materialptr); result = PyObject_CallFunction(func, "O", material); @@ -76,67 +76,68 @@ void MaterialData::init() Py_DECREF(result); } else { - CLOG_ERROR(LOG_BSD, "Export error for %s", id->name); + CLOG_ERROR(LOG_BSD, "Export error for %s", id_->name); PyErr_Print(); } Py_DECREF(module); PyGILState_Release(gstate); - mtlx_path = pxr::SdfAssetPath(path, path); - CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", id->name, mtlx_path.GetResolvedPath().c_str()); + mtlx_path_ = pxr::SdfAssetPath(path, path); + CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", id_->name, mtlx_path_.GetResolvedPath().c_str()); +} + +void MaterialData::insert() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().InsertSprim( + pxr::HdPrimTypeTokens->material, scene_delegate_, p_id_); +} + +void MaterialData::remove() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id_); +} + +void MaterialData::update() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + init(); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_, + pxr::HdMaterial::AllDirty); } pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; if (key.GetString() == "MaterialXFilename") { - if (!mtlx_path.GetResolvedPath().empty()) { - ret = mtlx_path; + if (!mtlx_path_.GetResolvedPath().empty()) { + ret = mtlx_path_; } CLOG_INFO(LOG_BSD, 3, "%s", key.GetText()); } return ret; } -pxr::VtValue MaterialData::material_resource() +pxr::VtValue MaterialData::get_material_resource() { - if (material_network_map.IsEmpty()) { - const std::string &path = mtlx_path.GetResolvedPath(); + if (material_network_map_.IsEmpty()) { + const std::string &path = mtlx_path_.GetResolvedPath(); if (!path.empty()) { pxr::HdRenderDelegate *render_delegate = - scene_delegate->GetRenderIndex().GetRenderDelegate(); + scene_delegate_->GetRenderIndex().GetRenderDelegate(); pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes(); pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts(); pxr::HdMaterialNetworkMap network_map; - HdMtlxConvertToMaterialNetworkMap(path, shader_source_types, render_contexts, &network_map); + hdmtlx_convert_to_materialnetworkmap( + path, shader_source_types, render_contexts, &network_map); - material_network_map = network_map; + material_network_map_ = network_map; } } - return material_network_map; -} - -void MaterialData::insert() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().InsertSprim( - pxr::HdPrimTypeTokens->material, scene_delegate, p_id); -} - -void MaterialData::remove() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id); -} - -void MaterialData::update() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - init(); - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, - pxr::HdMaterial::AllDirty); + return material_network_map_; } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/material.h b/source/blender/render/hydra/scene_delegate/material.h index 7d7e6dfc92df..1389fec8ade1 100644 --- a/source/blender/render/hydra/scene_delegate/material.h +++ b/source/blender/render/hydra/scene_delegate/material.h @@ -19,24 +19,23 @@ class MaterialData : IdData { friend MeshData; public: + MaterialData(BlenderSceneDelegate *scene_delegate, Material *material); + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Material *material); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Material *material); - MaterialData(BlenderSceneDelegate *scene_delegate, Material *material); - void init() override; void insert() override; void remove() override; void update() override; - pxr::VtValue get_data(pxr::TfToken const &key) const override; - pxr::VtValue material_resource(); - void export_mtlx(); + pxr::VtValue get_data(pxr::TfToken const &key) const override; + pxr::VtValue get_material_resource(); private: - pxr::SdfAssetPath mtlx_path; - pxr::VtValue material_network_map; + pxr::SdfAssetPath mtlx_path_; + pxr::VtValue material_network_map_; }; using MaterialDataMap = diff --git a/source/blender/render/hydra/scene_delegate/mesh.cc b/source/blender/render/hydra/scene_delegate/mesh.cc index e2d44c017174..5c3340962277 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.cc +++ b/source/blender/render/hydra/scene_delegate/mesh.cc @@ -16,25 +16,102 @@ namespace blender::render::hydra { MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) - : ObjectData(scene_delegate, object), mat_data(nullptr) + : ObjectData(scene_delegate, object), mat_data_(nullptr) { - CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id_->name, p_id_.GetText()); +} + +void MeshData::init() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + + Object *object = (Object *)id_; + if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && + BLI_listbase_is_empty(&object->modifiers)) { + write_mesh((Mesh *)object->data); + } + else { + Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); + if (mesh) { + write_mesh(mesh); + } + BKE_object_to_mesh_clear(object); + } + + write_material(); +} + +void MeshData::insert() +{ + if (face_vertex_counts_.empty()) { + return; + } + + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().InsertRprim( + pxr::HdPrimTypeTokens->mesh, scene_delegate_, p_id_); +} + +void MeshData::remove() +{ + if (!scene_delegate_->GetRenderIndex().HasRprim(p_id_)) { + return; + } + + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().RemoveRprim(p_id_); +} + +void MeshData::update() +{ + pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; + Object *object = (Object *)id_; + if ((id_->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { + init(); + bits = pxr::HdChangeTracker::AllDirty; + } + else { + if (id_->recalc & ID_RECALC_SHADING) { + write_material(); + bits |= pxr::HdChangeTracker::DirtyMaterialId; + } + if (id_->recalc & ID_RECALC_TRANSFORM) { + bits |= pxr::HdChangeTracker::DirtyTransform; + } + } + + if (bits == pxr::HdChangeTracker::Clean) { + return; + } + + if (!scene_delegate_->GetRenderIndex().HasRprim(p_id_)) { + insert(); + return; + } + + if (face_vertex_counts_.empty()) { + /* Remove prim without faces */ + remove(); + return; + } + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id_, bits); } pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; if (key == pxr::HdTokens->points) { - ret = vertices; + ret = vertices_; } else if (key == pxr::HdTokens->normals) { - ret = normals; + ret = normals_; } else if (key == pxr::HdPrimvarRoleTokens->textureCoordinate) { - ret = uvs; + ret = uvs_; } else if (key == pxr::HdInstancerTokens->instanceTransform) { - ret = instances; + ret = instances_; } return ret; } @@ -43,8 +120,8 @@ bool MeshData::update_visibility(View3D *view3d) { bool ret = ObjectData::update_visibility(view3d); if (ret) { - scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( - p_id, pxr::HdChangeTracker::DirtyVisibility); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty( + p_id_, pxr::HdChangeTracker::DirtyVisibility); } return ret; } @@ -53,24 +130,24 @@ pxr::HdMeshTopology MeshData::mesh_topology() { return pxr::HdMeshTopology(pxr::PxOsdOpenSubdivTokens->none, pxr::HdTokens->rightHanded, - face_vertex_counts, - face_vertex_indices); + face_vertex_counts_, + face_vertex_indices_); } pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(pxr::HdInterpolation interpolation) { pxr::HdPrimvarDescriptorVector primvars; if (interpolation == pxr::HdInterpolationVertex) { - if (!vertices.empty()) { + if (!vertices_.empty()) { primvars.emplace_back(pxr::HdTokens->points, interpolation, pxr::HdPrimvarRoleTokens->point); } } else if (interpolation == pxr::HdInterpolationFaceVarying) { - if (!normals.empty()) { + if (!normals_.empty()) { primvars.emplace_back( pxr::HdTokens->normals, interpolation, pxr::HdPrimvarRoleTokens->normal); } - if (!uvs.empty()) { + if (!uvs_.empty()) { primvars.emplace_back(pxr::HdPrimvarRoleTokens->textureCoordinate, interpolation, pxr::HdPrimvarRoleTokens->textureCoordinate); @@ -81,19 +158,19 @@ pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(pxr::HdInterpolatio pxr::SdfPath MeshData::material_id() { - if (!mat_data) { + if (!mat_data_) { return pxr::SdfPath(); } - return mat_data->p_id; + return mat_data_->p_id_; } -void MeshData::set_mesh(Mesh *mesh) +void MeshData::write_mesh(Mesh *mesh) { - face_vertex_counts.clear(); - face_vertex_indices.clear(); - vertices.clear(); - normals.clear(); - uvs.clear(); + face_vertex_counts_.clear(); + face_vertex_indices_.clear(); + vertices_.clear(); + normals_.clear(); + uvs_.clear(); BKE_mesh_calc_normals_split(mesh); int tris_len = BKE_mesh_runtime_looptri_len(mesh); @@ -104,141 +181,74 @@ void MeshData::set_mesh(Mesh *mesh) blender::Span loopTris = mesh->looptris(); /* face_vertex_counts */ - face_vertex_counts = pxr::VtIntArray(tris_len, 3); + face_vertex_counts_ = pxr::VtIntArray(tris_len, 3); /* face_vertex_indices */ blender::Span corner_verts = mesh->corner_verts(); - face_vertex_indices.reserve(loopTris.size() * 3); + face_vertex_indices_.reserve(loopTris.size() * 3); for (MLoopTri lt : loopTris) { - face_vertex_indices.push_back(corner_verts[lt.tri[0]]); - face_vertex_indices.push_back(corner_verts[lt.tri[1]]); - face_vertex_indices.push_back(corner_verts[lt.tri[2]]); + face_vertex_indices_.push_back(corner_verts[lt.tri[0]]); + face_vertex_indices_.push_back(corner_verts[lt.tri[1]]); + face_vertex_indices_.push_back(corner_verts[lt.tri[2]]); } /* vertices */ - vertices.reserve(mesh->totvert); + vertices_.reserve(mesh->totvert); blender::Span verts = mesh->vert_positions(); for (blender::float3 v : verts) { - vertices.push_back(pxr::GfVec3f(v.x, v.y, v.z)); + vertices_.push_back(pxr::GfVec3f(v.x, v.y, v.z)); } - /* normals */ - const float(*lnors)[3] = (float(*)[3])CustomData_get_layer(&mesh->ldata, CD_NORMAL); - if (lnors) { - normals.reserve(loopTris.size() * 3); - for (MLoopTri lt : loopTris) { - normals.push_back(pxr::GfVec3f(lnors[lt.tri[0]])); - normals.push_back(pxr::GfVec3f(lnors[lt.tri[1]])); - normals.push_back(pxr::GfVec3f(lnors[lt.tri[2]])); - } - } - - /* uvs*/ - const float(*luvs)[2] = (float(*)[2])CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2); - if (luvs) { - uvs.reserve(loopTris.size() * 3); - for (MLoopTri lt : loopTris) { - uvs.push_back(pxr::GfVec2f(luvs[lt.tri[0]])); - uvs.push_back(pxr::GfVec2f(luvs[lt.tri[1]])); - uvs.push_back(pxr::GfVec2f(luvs[lt.tri[2]])); - } - } + write_normals(mesh); + write_uv_maps(mesh); } -void MeshData::set_material() +void MeshData::write_material() { - Object *object = (Object *)id; + Object *object = (Object *)id_; Material *mat = nullptr; if (BKE_object_material_count_eval(object) > 0) { mat = BKE_object_material_get_eval(object, object->actcol); } if (!mat) { - mat_data = nullptr; + mat_data_ = nullptr; return; } - pxr::SdfPath id = MaterialData::prim_id(scene_delegate, mat); - mat_data = scene_delegate->material_data(id); - if (!mat_data) { - scene_delegate->materials[id] = MaterialData::create(scene_delegate, mat); - mat_data = scene_delegate->material_data(id); + pxr::SdfPath id = MaterialData::prim_id(scene_delegate_, mat); + mat_data_ = scene_delegate_->material_data(id); + if (!mat_data_) { + scene_delegate_->materials_[id] = MaterialData::create(scene_delegate_, mat); + mat_data_ = scene_delegate_->material_data(id); } } -void MeshData::init() +void MeshData::write_uv_maps(Mesh *mesh) { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - - Object *object = (Object *)id; - if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && - BLI_listbase_is_empty(&object->modifiers)) { - set_mesh((Mesh *)object->data); - } - else { - Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); - if (mesh) { - set_mesh(mesh); - } - BKE_object_to_mesh_clear(object); - } - - set_material(); -} - -void MeshData::insert() -{ - if (face_vertex_counts.empty()) { - return; - } - - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate, p_id); -} - -void MeshData::remove() -{ - if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { - return; - } - - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().RemoveRprim(p_id); -} - -void MeshData::update() -{ - pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; - Object *object = (Object *)id; - if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { - init(); - bits = pxr::HdChangeTracker::AllDirty; - } - else { - if (id->recalc & ID_RECALC_SHADING) { - set_material(); - bits |= pxr::HdChangeTracker::DirtyMaterialId; - } - if (id->recalc & ID_RECALC_TRANSFORM) { - bits |= pxr::HdChangeTracker::DirtyTransform; + blender::Span loopTris = mesh->looptris(); + const float(*luvs)[2] = (float(*)[2])CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2); + if (luvs) { + uvs_.reserve(loopTris.size() * 3); + for (MLoopTri lt : loopTris) { + uvs_.push_back(pxr::GfVec2f(luvs[lt.tri[0]])); + uvs_.push_back(pxr::GfVec2f(luvs[lt.tri[1]])); + uvs_.push_back(pxr::GfVec2f(luvs[lt.tri[2]])); } } +} - if (bits == pxr::HdChangeTracker::Clean) { - return; +void MeshData::write_normals(Mesh *mesh) +{ + blender::Span loopTris = mesh->looptris(); + const float(*lnors)[3] = (float(*)[3])CustomData_get_layer(&mesh->ldata, CD_NORMAL); + if (lnors) { + normals_.reserve(loopTris.size() * 3); + for (MLoopTri lt : loopTris) { + normals_.push_back(pxr::GfVec3f(lnors[lt.tri[0]])); + normals_.push_back(pxr::GfVec3f(lnors[lt.tri[1]])); + normals_.push_back(pxr::GfVec3f(lnors[lt.tri[2]])); + } } - - if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { - insert(); - return; - } - - if (face_vertex_counts.empty()) { - /* Remove prim without faces */ - remove(); - return; - } - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/mesh.h b/source/blender/render/hydra/scene_delegate/mesh.h index 1a2869362a78..c710c7317e8d 100644 --- a/source/blender/render/hydra/scene_delegate/mesh.h +++ b/source/blender/render/hydra/scene_delegate/mesh.h @@ -21,6 +21,7 @@ class MeshData : public ObjectData { void insert() override; void remove() override; void update() override; + pxr::VtValue get_data(pxr::TfToken const &key) const override; bool update_visibility(View3D *view3d) override; @@ -29,18 +30,19 @@ class MeshData : public ObjectData { pxr::SdfPath material_id(); protected: - void set_mesh(Mesh *mesh); - void set_material(); + void write_mesh(Mesh *mesh); + void write_material(); + void write_uv_maps(Mesh *mesh); + void write_normals(Mesh *mesh); - pxr::VtIntArray face_vertex_counts; - pxr::VtIntArray face_vertex_indices; - pxr::VtVec3fArray vertices; - pxr::VtVec3fArray normals; - pxr::VtVec2fArray uvs; + pxr::VtIntArray face_vertex_counts_; + pxr::VtIntArray face_vertex_indices_; + pxr::VtVec3fArray vertices_; + pxr::VtVec2fArray uvs_; + pxr::VtVec3fArray normals_; + pxr::VtMatrix4dArray instances_; - pxr::VtMatrix4dArray instances; - - MaterialData *mat_data; + MaterialData *mat_data_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.cc b/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.cc index 2a7d1e717098..58836be947cc 100644 --- a/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.cc +++ b/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.cc @@ -22,10 +22,10 @@ namespace mx = MaterialX; -void HdMtlxConvertToMaterialNetworkMap(std::string const &mtlx_path, - pxr::TfTokenVector const &shader_source_types, - pxr::TfTokenVector const &render_contexts, - pxr::HdMaterialNetworkMap *out) +void hdmtlx_convert_to_materialnetworkmap(std::string const &mtlx_path, + pxr::TfTokenVector const &shader_source_types, + pxr::TfTokenVector const &render_contexts, + pxr::HdMaterialNetworkMap *out) { if (mtlx_path.empty()) { return; diff --git a/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.h b/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.h index 9ffd92895c2b..741f288ffb66 100644 --- a/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.h +++ b/source/blender/render/hydra/scene_delegate/mtlx_hydra_adapter.h @@ -10,7 +10,7 @@ struct pxr::HdMaterialNetworkMap; -void HdMtlxConvertToMaterialNetworkMap(std::string const &mtlx_path, - pxr::TfTokenVector const &shader_source_types, - pxr::TfTokenVector const &render_contexts, - pxr::HdMaterialNetworkMap *out); +void hdmtlx_convert_to_materialnetworkmap(std::string const &mtlx_path, + pxr::TfTokenVector const &shader_source_types, + pxr::TfTokenVector const &render_contexts, + pxr::HdMaterialNetworkMap *out); diff --git a/source/blender/render/hydra/scene_delegate/object.cc b/source/blender/render/hydra/scene_delegate/object.cc index 6a120734296d..241c047d451e 100644 --- a/source/blender/render/hydra/scene_delegate/object.cc +++ b/source/blender/render/hydra/scene_delegate/object.cc @@ -11,7 +11,13 @@ namespace blender::render::hydra { -bool ObjectData::supported(Object *object) +ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object) + : IdData(scene_delegate, (ID *)object), visible(true) +{ + p_id_ = prim_id(scene_delegate, object); +} + +bool ObjectData::is_supported(Object *object) { switch (object->type) { case OB_MESH: @@ -65,15 +71,9 @@ pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *o return scene_delegate->GetDelegateID().AppendElementString(str); } -ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object) - : IdData(scene_delegate, (ID *)object), visible(true) -{ - p_id = prim_id(scene_delegate, object); -} - pxr::GfMatrix4d ObjectData::transform() { - return gf_matrix_from_transform(((Object *)id)->object_to_world); + return gf_matrix_from_transform(((Object *)id_)->object_to_world); } bool ObjectData::update_visibility(View3D *view3d) @@ -83,7 +83,7 @@ bool ObjectData::update_visibility(View3D *view3d) } bool prev_visible = visible; - visible = BKE_object_is_visible_in_viewport(view3d, (Object *)id); + visible = BKE_object_is_visible_in_viewport(view3d, (Object *)id_); return visible != prev_visible; } diff --git a/source/blender/render/hydra/scene_delegate/object.h b/source/blender/render/hydra/scene_delegate/object.h index bdc17566ec43..4a60b5d64c59 100644 --- a/source/blender/render/hydra/scene_delegate/object.h +++ b/source/blender/render/hydra/scene_delegate/object.h @@ -16,12 +16,12 @@ namespace blender::render::hydra { class ObjectData : public IdData { public: - static bool supported(Object *object); + ObjectData(BlenderSceneDelegate *scene_delegate, Object *object); + + static bool is_supported(Object *object); static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object); - ObjectData(BlenderSceneDelegate *scene_delegate, Object *object); - virtual pxr::GfMatrix4d transform(); virtual bool update_visibility(View3D *view3d); diff --git a/source/blender/render/hydra/scene_delegate/world.cc b/source/blender/render/hydra/scene_delegate/world.cc index 993574853c7f..e712d076c336 100644 --- a/source/blender/render/hydra/scene_delegate/world.cc +++ b/source/blender/render/hydra/scene_delegate/world.cc @@ -28,6 +28,13 @@ namespace blender::render::hydra { +WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context) + : IdData(scene_delegate, (ID *)world), context_(context) +{ + p_id_ = prim_id(scene_delegate); + CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id_->name, p_id_.GetText()); +} + std::unique_ptr WorldData::create(BlenderSceneDelegate *scene_delegate, World *world, bContext *context) @@ -43,21 +50,14 @@ pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate) return scene_delegate->GetDelegateID().AppendElementString("World"); } -WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context) - : IdData(scene_delegate, (ID *)world), context(context) -{ - p_id = prim_id(scene_delegate); - CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); -} - void WorldData::init() { - CLOG_INFO(LOG_BSD, 2, "%s", id->name); + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); - World *world = (World *)id; - data.clear(); + World *world = (World *)id_; + data_.clear(); - data[pxr::UsdLuxTokens->orientToStageUpAxis] = true; + data_[pxr::UsdLuxTokens->orientToStageUpAxis] = true; if (world->use_nodes) { /* TODO: Create nodes parsing system */ @@ -90,9 +90,9 @@ void WorldData::init() float const *strength = strength_input.default_value_typed(); float const *color = color_input.default_value_typed(); - data[pxr::HdLightTokens->intensity] = strength[1]; - data[pxr::HdLightTokens->exposure] = 1.0f; - data[pxr::HdLightTokens->color] = pxr::GfVec3f(color[0], color[1], color[2]); + data_[pxr::HdLightTokens->intensity] = strength[1]; + data_[pxr::HdLightTokens->exposure] = 1.0f; + data_[pxr::HdLightTokens->color] = pxr::GfVec3f(color[0], color[1], color[2]); if (!color_input.directly_linked_links().is_empty()) { bNode *color_input_node = color_input.directly_linked_links()[0]->fromnode; @@ -101,8 +101,8 @@ void WorldData::init() Image *image = (Image *)color_input_node->id; if (image) { - Main *bmain = CTX_data_main(context); - Scene *scene = CTX_data_scene(context); + Main *bmain = CTX_data_main(context_); + Scene *scene = CTX_data_scene(context_); ReportList reports; ImageSaveOptions opts; @@ -110,26 +110,53 @@ void WorldData::init() std::string image_path = cache_image(bmain, scene, image, &tex->iuser, &opts, &reports); if (!image_path.empty()) { - data[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path); + data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path); } } } } } else { - data[pxr::HdLightTokens->intensity] = 1.0f; - data[pxr::HdLightTokens->exposure] = world->exposure; - data[pxr::HdLightTokens->color] = pxr::GfVec3f(world->horr, world->horg, world->horb); + data_[pxr::HdLightTokens->intensity] = 1.0f; + data_[pxr::HdLightTokens->exposure] = world->exposure; + data_[pxr::HdLightTokens->color] = pxr::GfVec3f(world->horr, world->horg, world->horb); } } +void WorldData::insert() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().InsertSprim( + pxr::HdPrimTypeTokens->domeLight, scene_delegate_, p_id_); +} + +void WorldData::remove() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id_); +} + +void WorldData::update() +{ + CLOG_INFO(LOG_BSD, 2, "%s", id_->name); + init(); + scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_, + pxr::HdLight::AllDirty); +} + +void WorldData::update(World *world) +{ + id_ = (ID *)world; + update(); +} + pxr::GfMatrix4d WorldData::transform() { pxr::GfMatrix4d transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), pxr::GfVec3d()); /* TODO : do this check via RenderSettings*/ - if (scene_delegate->GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName() == "RPR") { + if (scene_delegate_->GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName() == "RPR") { transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), pxr::GfVec3d()); transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), @@ -141,37 +168,11 @@ pxr::GfMatrix4d WorldData::transform() pxr::VtValue WorldData::get_data(pxr::TfToken const &key) const { pxr::VtValue ret; - auto it = data.find(key); - if (it != data.end()) { + auto it = data_.find(key); + if (it != data_.end()) { ret = it->second; } return ret; } -void WorldData::insert() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().InsertSprim( - pxr::HdPrimTypeTokens->domeLight, scene_delegate, p_id); -} - -void WorldData::remove() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id); -} - -void WorldData::update() -{ - CLOG_INFO(LOG_BSD, 2, "%s", id->name); - init(); - scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdLight::AllDirty); -} - -void WorldData::update(World *world) -{ - id = (ID *)world; - update(); -} - } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/scene_delegate/world.h b/source/blender/render/hydra/scene_delegate/world.h index 36ef231894f4..71924a7862bc 100644 --- a/source/blender/render/hydra/scene_delegate/world.h +++ b/source/blender/render/hydra/scene_delegate/world.h @@ -20,13 +20,13 @@ namespace blender::render::hydra { class WorldData : public IdData { public: + WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); + static std::unique_ptr create(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate); - WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); - void init() override; void insert() override; void remove() override; @@ -34,12 +34,11 @@ class WorldData : public IdData { void update(World *world); pxr::GfMatrix4d transform(); - pxr::VtValue get_data(pxr::TfToken const &key) const override; private: - std::map data; - bContext *context; + std::map data_; + bContext *context_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/simple_light_task_delegate.cc b/source/blender/render/hydra/simple_light_task_delegate.cc index 712e3c5e2cca..6bd7fc0cecfc 100644 --- a/source/blender/render/hydra/simple_light_task_delegate.cc +++ b/source/blender/render/hydra/simple_light_task_delegate.cc @@ -27,13 +27,13 @@ pxr::HdTaskSharedPtr SimpleLightTaskDelegate::get_task() void SimpleLightTaskDelegate::set_camera_path(pxr::SdfPath const &camera_path) { - task_params.cameraPath = camera_path; + task_params_.cameraPath = camera_path; } pxr::VtValue SimpleLightTaskDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) { if (key == pxr::HdTokens->params) { - return pxr::VtValue(task_params); + return pxr::VtValue(task_params_); } return pxr::VtValue(); } diff --git a/source/blender/render/hydra/simple_light_task_delegate.h b/source/blender/render/hydra/simple_light_task_delegate.h index 4c8f8ed20b27..376876b7f17b 100644 --- a/source/blender/render/hydra/simple_light_task_delegate.h +++ b/source/blender/render/hydra/simple_light_task_delegate.h @@ -18,10 +18,11 @@ class SimpleLightTaskDelegate : public pxr::HdSceneDelegate { pxr::HdTaskSharedPtr get_task(); void set_camera_path(pxr::SdfPath const &); + /* Delegate methods */ pxr::VtValue Get(pxr::SdfPath const &id, pxr::TfToken const &key) override; private: - pxr::HdxSimpleLightTaskParams task_params; + pxr::HdxSimpleLightTaskParams task_params_; }; } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/viewport_engine.cc b/source/blender/render/hydra/viewport_engine.cc index 7984ca892a22..f3db317a9a8b 100644 --- a/source/blender/render/hydra/viewport_engine.cc +++ b/source/blender/render/hydra/viewport_engine.cc @@ -5,15 +5,17 @@ #include #include -// clang-format off #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 "BKE_camera.h" + #include "BLI_math_matrix.h" + #include "DEG_depsgraph_query.h" + #include "GPU_matrix.h" -// clang-format on #include "camera.h" #include "utils.h" @@ -47,7 +49,7 @@ ViewSettings::ViewSettings(bContext *context) : camera_data(context) Scene *scene = CTX_data_scene(context); - // getting render border + /* Getting render border. */ int x1 = 0, y1 = 0; int x2 = screen_width, y2 = screen_height; @@ -73,7 +75,7 @@ ViewSettings::ViewSettings(bContext *context) : camera_data(context) } } - // getting camera view region + /* Getting camera view region. */ float x1_f = std::min( {screen_points[0][0], screen_points[1][0], screen_points[2][0], screen_points[3][0]}); float x2_f = std::max( @@ -83,7 +85,7 @@ ViewSettings::ViewSettings(bContext *context) : camera_data(context) float y2_f = std::max( {screen_points[0][1], screen_points[1][1], screen_points[2][1], screen_points[3][1]}); - // adjusting region to border + /* Adjusting region to border. */ float x = x1_f, y = y1_f; float dx = x2_f - x1_f, dy = y2_f - y1_f; @@ -92,7 +94,7 @@ ViewSettings::ViewSettings(bContext *context) : camera_data(context) y1 = y + scene->r.border.ymin * dy; y2 = y + scene->r.border.ymax * dy; - // adjusting to region screen resolution + /* Adjusting to region screen resolution. */ x1 = std::max(std::min(x1, screen_width), 0); x2 = std::max(std::min(x2, screen_width), 0); y1 = std::max(std::min(y1, screen_height), 0); @@ -132,7 +134,7 @@ pxr::GfCamera ViewSettings::gf_camera() (float)border[3] / screen_height)); } -DrawTexture::DrawTexture() : texture(nullptr), width(0), height(0), channels(4) +DrawTexture::DrawTexture() : texture_(nullptr), width_(0), height_(0), channels_(4) { float coords[8] = {0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0}; @@ -144,92 +146,97 @@ DrawTexture::DrawTexture() : texture(nullptr), width(0), height(0), channels(4) GPU_vertbuf_attr_fill(vbo, 0, coords); GPU_vertbuf_attr_fill(vbo, 1, coords); - batch = GPU_batch_create_ex(GPU_PRIM_TRI_FAN, vbo, nullptr, GPU_BATCH_OWNS_VBO); + batch_ = GPU_batch_create_ex(GPU_PRIM_TRI_FAN, vbo, nullptr, GPU_BATCH_OWNS_VBO); } DrawTexture::~DrawTexture() { - if (texture) { + if (texture_) { free(); } - GPU_batch_discard(batch); + GPU_batch_discard(batch_); } void DrawTexture::set_buffer(pxr::HdRenderBuffer *buffer) { - if (!texture) { + if (!texture_) { create(buffer); return; } - if (width != buffer->GetWidth() || height != buffer->GetHeight()) { + 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(); } -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("texHydraRenderViewport", - width, - height, - 1, - GPU_RGBA16F, - GPU_TEXTURE_USAGE_GENERAL, - (float *)data); - buffer->Unmap(); - - GPU_texture_filter_mode(texture, true); - GPU_texture_mipmap_mode(texture, true, true); -} - -void DrawTexture::free() -{ - GPU_texture_free(texture); - texture = nullptr; -} - void DrawTexture::draw(GPUShader *shader, float x, float y) { int slot = GPU_shader_get_sampler_binding(shader, "image"); - GPU_texture_bind(texture, slot); + 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_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", + width_, + height_, + 1, + GPU_RGBA16F, + GPU_TEXTURE_USAGE_GENERAL, + (float *)data); + buffer->Unmap(); + + GPU_texture_filter_mode(texture_, true); + GPU_texture_mipmap_mode(texture_, true, true); +} + +void DrawTexture::free() +{ + GPU_texture_free(texture_); + texture_ = nullptr; +} + void ViewportEngine::sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) { - if (!scene_delegate) { - scene_delegate = std::make_unique( - render_index.get(), + if (!scene_delegate_) { + scene_delegate_ = std::make_unique( + render_index_.get(), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::VIEWPORT); } - scene_delegate->populate(depsgraph, context); + scene_delegate_->populate(depsgraph, context); for (auto const &setting : render_settings) { - render_delegate->SetRenderSetting(setting.first, setting.second); + render_delegate_->SetRenderSetting(setting.first, setting.second); } } +void ViewportEngine::render(Depsgraph *depsgraph) +{ + /* Empty function */ +} + void ViewportEngine::render(Depsgraph *depsgraph, bContext *context) { ViewSettings view_settings(context); @@ -238,41 +245,41 @@ 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], - view_settings.border[1], - view_settings.border[2], - view_settings.border[3])); - if (simple_light_task_delegate) { - simple_light_task_delegate->set_camera_path(free_camera_delegate->GetCameraId()); + free_camera_delegate_->SetCamera(gf_camera); + render_task_delegate_->set_camera_and_viewport(free_camera_delegate_->GetCameraId(), + pxr::GfVec4d(view_settings.border[0], + view_settings.border[1], + view_settings.border[2], + view_settings.border[3])); + if (simple_light_task_delegate_) { + simple_light_task_delegate_->set_camera_path(free_camera_delegate_->GetCameraId()); } - if ((bl_engine->type->flag & RE_USE_GPU_CONTEXT) == 0) { - render_task_delegate->set_renderer_aov(pxr::HdAovTokens->color); + if ((bl_engine_->type->flag & RE_USE_GPU_CONTEXT) == 0) { + render_task_delegate_->set_renderer_aov(pxr::HdAovTokens->color); } if (renderer_percent_done() == 0.0f) { - time_begin = std::chrono::steady_clock::now(); + time_begin_ = std::chrono::steady_clock::now(); } GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_3D_IMAGE); GPU_shader_bind(shader); pxr::HdTaskSharedPtrVector tasks; - if (simple_light_task_delegate) { - tasks.push_back(simple_light_task_delegate->get_task()); + if (simple_light_task_delegate_) { + tasks.push_back(simple_light_task_delegate_->get_task()); } - tasks.push_back(render_task_delegate->get_task()); + tasks.push_back(render_task_delegate_->get_task()); { /* Release the GIL before calling into hydra, in case any hydra plugins call into python. */ pxr::TF_PY_ALLOW_THREADS_IN_SCOPE(); - engine->Execute(render_index.get(), &tasks); + 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.draw(shader, view_settings.border[0], view_settings.border[1]); + 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_.draw(shader, view_settings.border[0], view_settings.border[1]); } } @@ -281,29 +288,24 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context) std::chrono::time_point time_current = std::chrono::steady_clock::now(); std::chrono::milliseconds elapsed_time = std::chrono::duration_cast( - time_current - time_begin); + time_current - time_begin_); std::string formatted_time = format_duration(elapsed_time); - if (!render_task_delegate->is_converged()) { + if (!render_task_delegate_->is_converged()) { notify_status("Time: " + formatted_time + " | Done: " + std::to_string(int(renderer_percent_done())) + "%", "Render"); - bl_engine->flag |= RE_ENGINE_DO_DRAW; + bl_engine_->flag |= RE_ENGINE_DO_DRAW; } else { notify_status(("Time: " + formatted_time).c_str(), "Rendering Done"); } } -void ViewportEngine::render(Depsgraph *depsgraph) -{ - /* Empty function */ -} - void ViewportEngine::notify_status(const std::string &info, const std::string &status) { - RE_engine_update_stats(bl_engine, status.c_str(), info.c_str()); + RE_engine_update_stats(bl_engine_, status.c_str(), info.c_str()); } } // namespace blender::render::hydra diff --git a/source/blender/render/hydra/viewport_engine.h b/source/blender/render/hydra/viewport_engine.h index 154842fc7021..a362cf21bfba 100644 --- a/source/blender/render/hydra/viewport_engine.h +++ b/source/blender/render/hydra/viewport_engine.h @@ -19,6 +19,7 @@ class DrawTexture { public: DrawTexture(); ~DrawTexture(); + void set_buffer(pxr::HdRenderBuffer *buffer); void draw(GPUShader *shader, float x, float y); @@ -26,14 +27,15 @@ class DrawTexture { void create(pxr::HdRenderBuffer *buffer); void free(); - GPUTexture *texture; - GPUBatch *batch; - int width, height, channels; + GPUTexture *texture_; + GPUBatch *batch_; + int width_, height_, channels_; }; class ViewportEngine : public Engine { public: using Engine::Engine; + void sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &render_settings) override; @@ -44,9 +46,9 @@ class ViewportEngine : public Engine { void notify_status(const std::string &title, const std::string &info); private: - std::chrono::time_point time_begin; + std::chrono::time_point time_begin_; - DrawTexture draw_texture; + DrawTexture draw_texture_; }; } // namespace blender::render::hydra