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>();
scene_delegate = std::make_unique<BlenderSceneDelegate>(
render_index.get(),
pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::PREVIEW);
}
Engine::~Engine()
@ -62,6 +57,11 @@ Engine::~Engine()
bl_engine = nullptr;
}
bool Engine::is_converged()
{
return render_task_delegate->is_converged();
}
float Engine::renderer_percent_done()
{
pxr::VtDictionary render_stats = render_delegate->GetRenderStats();

View File

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

View File

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

View File

@ -12,13 +12,13 @@ void PreviewEngine::sync(Depsgraph *depsgraph,
bContext *context,
pxr::HdRenderSettingsMap &render_settings)
{
scene_delegate->clear_data();
for (auto &prim : render_index->GetRprimIds()) {
render_index->RemoveRprim(prim);
render_index->RemoveInstancer(prim);
if (!scene_delegate) {
scene_delegate = std::make_unique<BlenderSceneDelegate>(
render_index.get(),
pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::PREVIEW);
}
scene_delegate->clear();
scene_delegate->populate(depsgraph, context);
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);
}
void PreviewEngine::stop_renderer()
void PreviewEngine::update_bl_engine(RenderEngine *bl_engine)
{
if (render_delegate) {
render_delegate->Stop();
this->bl_engine = bl_engine;
}
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,

View File

@ -14,7 +14,8 @@ class PreviewEngine : public FinalEngine {
bContext *context,
pxr::HdRenderSettingsMap &render_settings) 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:
void update_render_result(const std::string &layer_name,
@ -22,6 +23,9 @@ class PreviewEngine : public FinalEngine {
int height,
std::vector<float> &pixels);
public:
static inline double preview_engine_lifetime = 180.0;
protected:
pxr::HdRenderSettingsMap render_settings;
};

View File

@ -20,20 +20,7 @@
namespace blender::render::hydra {
static double preview_engine_lifetime = 60.0;
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;
}
static std::unique_ptr<PreviewEngine> preview_engine;
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")) {
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)) {
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);
return PyLong_FromVoidPtr(preview_engine);
return PyLong_FromVoidPtr(preview_engine.get());
}
else {
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);
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);
Py_RETURN_NONE;
}

View File

@ -477,10 +477,18 @@ pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
return pxr::VtValue();
}
void BlenderSceneDelegate::clear_data()
void BlenderSceneDelegate::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();
objects.clear();
}
} // namespace blender::render::hydra

View File

@ -29,7 +29,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
~BlenderSceneDelegate() override = default;
void populate(Depsgraph *depsgraph, bContext *context);
void clear_data();
void clear();
// delegate methods
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);
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)

View File

@ -219,7 +219,7 @@ void MeshData::remove_prim()
}
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)

View File

@ -244,6 +244,12 @@ void ViewportEngine::sync(Depsgraph *depsgraph,
bContext *context,
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);
for (auto const &setting : render_settings) {