BLEN-365: Improve creation algorithm of PreviewEngine #21

Merged
Bogdan Nagirniak merged 11 commits from BLEN-365 into hydra-render 2023-04-10 12:44:48 +02:00
11 changed files with 65 additions and 41 deletions
Showing only changes of commit d33f8ef37d - Show all commits

View File

@ -42,11 +42,6 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &delegate_id) : bl_eng
} }
engine = std::make_unique<pxr::HdEngine>(); engine = std::make_unique<pxr::HdEngine>();
scene_delegate = std::make_unique<BlenderSceneDelegate>(
render_index.get(),
pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::PREVIEW);
} }
Engine::~Engine() Engine::~Engine()
@ -62,6 +57,11 @@ Engine::~Engine()
bl_engine = nullptr; bl_engine = nullptr;
DagerD marked this conversation as resolved Outdated

no need

no need
} }
bool Engine::is_converged()
DagerD marked this conversation as resolved Outdated

This function is not needed

This function is not needed
{
return render_task_delegate->is_converged();
}
float Engine::renderer_percent_done() float Engine::renderer_percent_done()
{ {
pxr::VtDictionary render_stats = render_delegate->GetRenderStats(); pxr::VtDictionary render_stats = render_delegate->GetRenderStats();

View File

@ -34,14 +34,14 @@ class Engine {
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) = 0; pxr::HdRenderSettingsMap &render_settings) = 0;
virtual void render(Depsgraph *depsgraph) = 0; virtual void render(Depsgraph *depsgraph) = 0;
bool is_converged();
public:
RenderEngine *bl_engine;
protected: protected:
float renderer_percent_done(); float renderer_percent_done();
protected: protected:
RenderEngine *bl_engine;
pxr::HdPluginRenderDelegateUniqueHandle render_delegate; pxr::HdPluginRenderDelegateUniqueHandle render_delegate;
std::unique_ptr<pxr::HdRenderIndex> render_index; std::unique_ptr<pxr::HdRenderIndex> render_index;
std::unique_ptr<BlenderSceneDelegate> scene_delegate; std::unique_ptr<BlenderSceneDelegate> scene_delegate;

View File

@ -16,6 +16,10 @@ void FinalEngine::sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) pxr::HdRenderSettingsMap &render_settings)
{ {
scene_delegate = std::make_unique<BlenderSceneDelegate>(
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) { for (auto const &setting : render_settings) {

View File

@ -12,13 +12,13 @@ void PreviewEngine::sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) pxr::HdRenderSettingsMap &render_settings)
{ {
scene_delegate->clear_data(); if (!scene_delegate) {
scene_delegate = std::make_unique<BlenderSceneDelegate>(
for (auto &prim : render_index->GetRprimIds()) { render_index.get(),
DagerD marked this conversation as resolved Outdated

Move this to clear()

Move this to clear()
render_index->RemoveRprim(prim); pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"),
render_index->RemoveInstancer(prim); BlenderSceneDelegate::EngineType::PREVIEW);
} }
scene_delegate->clear();
scene_delegate->populate(depsgraph, context); scene_delegate->populate(depsgraph, context);
for (auto const &setting : render_settings) { for (auto const &setting : render_settings) {
@ -73,11 +73,21 @@ void PreviewEngine::render(Depsgraph *depsgraph)
update_render_result(layer_name, res[0], res[1], pixels); update_render_result(layer_name, res[0], res[1], pixels);
} }
void PreviewEngine::stop_renderer() void PreviewEngine::update_bl_engine(RenderEngine *bl_engine)
{ {
if (render_delegate) { this->bl_engine = bl_engine;
render_delegate->Stop();
} }
double PreviewEngine::delete_preview_engine(uintptr_t uuid, void *user_data)
{
std::unique_ptr<PreviewEngine> *preview_engine = static_cast<std::unique_ptr<PreviewEngine>*>(user_data);
if (preview_engine && preview_engine->get()->is_converged()) {
CLOG_INFO(LOG_EN, 1, "Delete preview engine");
preview_engine->reset();
return -1;
}
return preview_engine_lifetime;
} }
void PreviewEngine::update_render_result(const std::string &layer_name, void PreviewEngine::update_render_result(const std::string &layer_name,

View File

@ -14,7 +14,8 @@ class PreviewEngine : public FinalEngine {
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) override; pxr::HdRenderSettingsMap &render_settings) override;
void render(Depsgraph *depsgraph) override; void render(Depsgraph *depsgraph) override;
void stop_renderer(); void update_bl_engine(RenderEngine *bl_engine);
static double delete_preview_engine(uintptr_t uuid, void *user_data);
protected: protected:
void update_render_result(const std::string &layer_name, void update_render_result(const std::string &layer_name,
@ -22,6 +23,9 @@ class PreviewEngine : public FinalEngine {
int height, int height,
std::vector<float> &pixels); std::vector<float> &pixels);
public:
static inline double preview_engine_lifetime = 180.0;
protected: protected:
pxr::HdRenderSettingsMap render_settings; pxr::HdRenderSettingsMap render_settings;
}; };

View File

@ -20,20 +20,7 @@
namespace blender::render::hydra { namespace blender::render::hydra {
static double preview_engine_lifetime = 60.0; static std::unique_ptr<PreviewEngine> preview_engine;
DagerD marked this conversation as resolved Outdated

move to PreviewEngine class as static

move to PreviewEngine class as static
static PreviewEngine *preview_engine;
double delete_preview_engine(uintptr_t uuid, void *user_data)
{
if (preview_engine) {
preview_engine->stop_renderer();
delete preview_engine;
preview_engine = nullptr;
return -1;
}
return preview_engine_lifetime;
}
DagerD marked this conversation as resolved Outdated

Use unique_ptr

Use unique_ptr
static PyObject *init_func(PyObject * /*self*/, PyObject *args) static PyObject *init_func(PyObject * /*self*/, PyObject *args)
{ {
@ -130,15 +117,15 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
} }
else if (STREQ(engine_type, "PREVIEW")) { else if (STREQ(engine_type, "PREVIEW")) {
if (!preview_engine) { if (!preview_engine) {
preview_engine = new PreviewEngine(bl_engine, render_delegate_id); preview_engine = std::make_unique<PreviewEngine>(bl_engine, render_delegate_id);
} }
if (BLI_timer_is_registered(1)) { if (BLI_timer_is_registered(1)) {
BLI_timer_unregister(1); BLI_timer_unregister(1);
} }
preview_engine->bl_engine = bl_engine; preview_engine->update_bl_engine(bl_engine);
CLOG_INFO(LOG_EN, 2, "Engine %016llx %s", engine, engine_type); CLOG_INFO(LOG_EN, 2, "Engine %016llx %s", engine, engine_type);
DagerD marked this conversation as resolved Outdated

This block should be like:
engine = PreviewEngine::get(bl_engine, render_delegate_id);

This block should be like: `engine = PreviewEngine::get(bl_engine, render_delegate_id);`
return PyLong_FromVoidPtr(preview_engine); return PyLong_FromVoidPtr(preview_engine.get());
} }
else { else {
if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) { if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) {
@ -164,7 +151,12 @@ static PyObject *engine_free_func(PyObject * /*self*/, PyObject *args)
Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine);
if (preview_engine) { if (preview_engine) {
BLI_timer_register(1, delete_preview_engine, nullptr, nullptr, preview_engine_lifetime, true); BLI_timer_register(1,
preview_engine->delete_preview_engine,
&preview_engine,
nullptr,
preview_engine->preview_engine_lifetime,
true);
CLOG_INFO(LOG_EN, 2, "Engine %016llx", engine); CLOG_INFO(LOG_EN, 2, "Engine %016llx", engine);
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View File

@ -477,10 +477,18 @@ pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
return pxr::VtValue(); return pxr::VtValue();
} }
void BlenderSceneDelegate::clear_data() void BlenderSceneDelegate::clear()
DagerD marked this conversation as resolved Outdated

Rename to just "clear"

Rename to just "clear"
{ {
objects.clear(); for (auto it = materials.begin(); it != materials.end(); ++it) {
it->second->remove_prim();
}
for (auto it = objects.begin(); it != objects.end(); ++it) {
it->second->remove_prim();
}
materials.clear(); materials.clear();
objects.clear();
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -29,7 +29,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
~BlenderSceneDelegate() override = default; ~BlenderSceneDelegate() override = default;
void populate(Depsgraph *depsgraph, bContext *context); void populate(Depsgraph *depsgraph, bContext *context);
void clear_data(); void clear();
// delegate methods // delegate methods
pxr::HdMeshTopology GetMeshTopology(pxr::SdfPath const &id) override; pxr::HdMeshTopology GetMeshTopology(pxr::SdfPath const &id) override;

View File

@ -120,7 +120,7 @@ void MaterialData::remove_prim()
{ {
pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id);
scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id); scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id);
CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); //CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str());
} }
void MaterialData::mark_prim_dirty(DirtyBits dirty_bits) void MaterialData::mark_prim_dirty(DirtyBits dirty_bits)

View File

@ -219,7 +219,7 @@ void MeshData::remove_prim()
} }
scene_delegate->GetRenderIndex().RemoveRprim(p_id); scene_delegate->GetRenderIndex().RemoveRprim(p_id);
CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str()); //CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str());
} }
void MeshData::mark_prim_dirty(DirtyBits dirty_bits) void MeshData::mark_prim_dirty(DirtyBits dirty_bits)

View File

@ -244,6 +244,12 @@ void ViewportEngine::sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) pxr::HdRenderSettingsMap &render_settings)
{ {
if (!scene_delegate) {
scene_delegate = std::make_unique<BlenderSceneDelegate>(
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) { for (auto const &setting : render_settings) {