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
8 changed files with 53 additions and 45 deletions
Showing only changes of commit a439e99ab8 - Show all commits

View File

@ -54,12 +54,6 @@ Engine::~Engine()
render_delegate = nullptr; render_delegate = nullptr;
engine = nullptr; engine = nullptr;
hgi = nullptr; hgi = nullptr;
bl_engine = nullptr;
}
bool Engine::is_converged()
{
return render_task_delegate->is_converged();
} }
DagerD marked this conversation as resolved Outdated

no need

no need
float Engine::renderer_percent_done() float Engine::renderer_percent_done()

View File

@ -34,7 +34,6 @@ 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();
protected: protected:
float renderer_percent_done(); float renderer_percent_done();

View File

@ -5,9 +5,38 @@
#include "camera.h" #include "camera.h"
#include "preview_engine.h" #include "preview_engine.h"
#include "BLI_timer.h"
namespace blender::render::hydra { namespace blender::render::hydra {
std::unique_ptr<PreviewEngine> PreviewEngine::preview_engine;
void PreviewEngine::free()
{
if (preview_engine) {
BLI_timer_register((uintptr_t)preview_engine.get(),
DagerD marked this conversation as resolved Outdated

Move this to clear()

Move this to clear()
preview_engine->delete_preview_engine,
&preview_engine,
nullptr,
preview_engine->preview_engine_lifetime,
true);
}
}
std::unique_ptr<PreviewEngine> *PreviewEngine::get(RenderEngine *bl_engine,
char *render_delegate_id)
{
if (!preview_engine) {
preview_engine = std::make_unique<PreviewEngine>(bl_engine, render_delegate_id);
}
if (BLI_timer_is_registered((uintptr_t)preview_engine.get())) {
BLI_timer_unregister((uintptr_t)preview_engine.get());
}
preview_engine->update_bl_engine(bl_engine);
return &preview_engine;
}
void PreviewEngine::sync(Depsgraph *depsgraph, void PreviewEngine::sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) pxr::HdRenderSettingsMap &render_settings)
@ -81,8 +110,8 @@ void PreviewEngine::update_bl_engine(RenderEngine *bl_engine)
double PreviewEngine::delete_preview_engine(uintptr_t uuid, void *user_data) 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); std::unique_ptr<PreviewEngine> *preview_engine = static_cast<std::unique_ptr<PreviewEngine>*>(user_data);
if (preview_engine && preview_engine->get()->is_converged()) { if (preview_engine && preview_engine->get()->render_task_delegate->is_converged()) {
CLOG_INFO(LOG_EN, 1, "Delete preview engine"); CLOG_INFO(LOG_EN, uuid, "Delete preview engine");
preview_engine->reset(); preview_engine->reset();
return -1; return -1;
} }

View File

@ -10,6 +10,9 @@ namespace blender::render::hydra {
class PreviewEngine : public FinalEngine { class PreviewEngine : public FinalEngine {
public: public:
using FinalEngine::FinalEngine; using FinalEngine::FinalEngine;
static void free();
static std::unique_ptr<PreviewEngine> *get(RenderEngine *bl_engine = nullptr,
char *render_delegate_id = nullptr);
void sync(Depsgraph *depsgraph, void sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) override; pxr::HdRenderSettingsMap &render_settings) override;
@ -27,6 +30,7 @@ class PreviewEngine : public FinalEngine {
static inline double preview_engine_lifetime = 180.0; static inline double preview_engine_lifetime = 180.0;
protected: protected:
static std::unique_ptr<PreviewEngine> preview_engine;
pxr::HdRenderSettingsMap render_settings; pxr::HdRenderSettingsMap render_settings;
}; };

View File

@ -20,8 +20,6 @@
namespace blender::render::hydra { namespace blender::render::hydra {
static std::unique_ptr<PreviewEngine> preview_engine;
static PyObject *init_func(PyObject * /*self*/, PyObject *args) static PyObject *init_func(PyObject * /*self*/, PyObject *args)
DagerD marked this conversation as resolved Outdated

move to PreviewEngine class as static

move to PreviewEngine class as static
{ {
DagerD marked this conversation as resolved Outdated

Use unique_ptr

Use unique_ptr
CLOG_INFO(LOG_EN, 1, "Init"); CLOG_INFO(LOG_EN, 1, "Init");
@ -116,16 +114,7 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
engine = new ViewportEngine(bl_engine, render_delegate_id); engine = new ViewportEngine(bl_engine, render_delegate_id);
} }
else if (STREQ(engine_type, "PREVIEW")) { else if (STREQ(engine_type, "PREVIEW")) {
if (!preview_engine) { engine = PreviewEngine::get(bl_engine, render_delegate_id)->get();
preview_engine = std::make_unique<PreviewEngine>(bl_engine, render_delegate_id);
}
if (BLI_timer_is_registered(1)) {
BLI_timer_unregister(1);
}
preview_engine->update_bl_engine(bl_engine);
CLOG_INFO(LOG_EN, 2, "Engine %016llx %s", engine, engine_type);
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) {
@ -149,15 +138,9 @@ static PyObject *engine_free_func(PyObject * /*self*/, PyObject *args)
} }
DagerD marked this conversation as resolved Outdated

Add PreviewEngine::update_engine() instead public usage of bl_engine

Add PreviewEngine::update_engine() instead public usage of bl_engine
Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine); Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine);
PreviewEngine *preview_engine = dynamic_cast<PreviewEngine *>(engine);
if (preview_engine) { if (preview_engine) {
BLI_timer_register(1, preview_engine->free();
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; Py_RETURN_NONE;
} }

View File

@ -436,12 +436,12 @@ pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
void BlenderSceneDelegate::clear() void BlenderSceneDelegate::clear()
{ {
for (auto it = materials.begin(); it != materials.end(); ++it) { for (auto &it : materials) {
DagerD marked this conversation as resolved Outdated
for (auto &it : materials) {
  it.second->remove();
}
``` for (auto &it : materials) { it.second->remove(); } ```
it->second->remove(); it.second->remove();
} }
for (auto it = objects.begin(); it != objects.end(); ++it) { for (auto &it : objects) {
it->second->remove(); it.second->remove();
} }
materials.clear(); materials.clear();

View File

@ -7,7 +7,6 @@
#include <pxr/imaging/hd/tokens.h> #include <pxr/imaging/hd/tokens.h>
#include <pxr/usd/usdLux/tokens.h> #include <pxr/usd/usdLux/tokens.h>
#include "BKE_light.h"
#include "DNA_light_types.h" #include "DNA_light_types.h"
#include "blender_scene_delegate.h" #include "blender_scene_delegate.h"
@ -80,17 +79,16 @@ void LightData::init()
break; break;
} }
p_type = light->type; p_type = prim_type(light);
p_shape = light->area_shape;
/* TODO: temporary value, it should be delivered through Python UI */ /* TODO: temporary value, it should be delivered through Python UI */
data[pxr::HdLightTokens->exposure] = 1.0f; data[pxr::HdLightTokens->exposure] = 1.0f;
} }
pxr::TfToken LightData::prim_type() pxr::TfToken LightData::prim_type(Light *light)
{ {
pxr::TfToken ret; pxr::TfToken ret;
switch (p_type) { switch (light->type) {
case LA_LOCAL: case LA_LOCAL:
case LA_SPOT: case LA_SPOT:
ret = pxr::HdPrimTypeTokens->sphereLight; ret = pxr::HdPrimTypeTokens->sphereLight;
@ -101,7 +99,7 @@ pxr::TfToken LightData::prim_type()
break; break;
case LA_AREA: case LA_AREA:
switch (p_shape) { switch (light->area_shape) {
case LA_AREA_SQUARE: case LA_AREA_SQUARE:
case LA_AREA_RECT: case LA_AREA_RECT:
ret = pxr::HdPrimTypeTokens->rectLight; ret = pxr::HdPrimTypeTokens->rectLight;
@ -157,13 +155,13 @@ bool LightData::update_visibility(View3D *view3d)
void LightData::insert() void LightData::insert()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); scene_delegate->GetRenderIndex().InsertSprim(p_type, scene_delegate, p_id);
} }
void LightData::remove() void LightData::remove()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); scene_delegate->GetRenderIndex().RemoveSprim(p_type, p_id);
} }
void LightData::update() void LightData::update()
@ -171,7 +169,7 @@ void LightData::update()
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
Light *light = (Light *)((Object *)id)->data; Light *light = (Light *)((Object *)id)->data;
if (light->type != p_type || light->area_shape != p_shape) { if (prim_type(light) != p_type) {
remove(); remove();
init(); init();
insert(); insert();

View File

@ -7,6 +7,8 @@
#include <pxr/usd/sdf/assetPath.h> #include <pxr/usd/sdf/assetPath.h>
#include <pxr/usd/sdf/path.h> #include <pxr/usd/sdf/path.h>
#include "BKE_light.h"
#include "object.h" #include "object.h"
namespace blender::render::hydra { namespace blender::render::hydra {
@ -24,9 +26,8 @@ class LightData : public ObjectData {
private: private:
std::map<pxr::TfToken, pxr::VtValue> data; std::map<pxr::TfToken, pxr::VtValue> data;
DagerD marked this conversation as resolved Outdated

pxr::TfToken p_type;
make prim_type() with Light * parameter

`pxr::TfToken p_type;` make prim_type() with `Light *` parameter
short p_type; pxr::TfToken p_type;
short p_shape; pxr::TfToken prim_type(Light *light);
pxr::TfToken prim_type();
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra