Create possibility to provide render settings via BlenderSceneDelegate #41

Merged
Bogdan Nagirniak merged 10 commits from BLEN-349 into hydra-render 2023-05-19 20:19:17 +02:00
5 changed files with 56 additions and 40 deletions
Showing only changes of commit 185b9c908f - Show all commits

View File

@ -17,8 +17,8 @@ namespace blender::render::hydra {
CLG_LOGREF_DECLARE_GLOBAL(LOG_RENDER_HYDRA, "render.hydra");
Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name)
: bl_engine_(bl_engine), render_delegate_name_(render_delegate_name)
Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name, SettingsMap &settings)
: bl_engine_(bl_engine), render_delegate_name_(render_delegate_name), settings_(settings)
{
pxr::HdRendererPluginRegistry &registry = pxr::HdRendererPluginRegistry::GetInstance();

View File

@ -23,14 +23,18 @@ namespace blender::render::hydra {
extern struct CLG_LogRef *LOG_RENDER_HYDRA;
typedef pxr::TfHashMap<pxr::TfToken, pxr::VtValue, pxr::TfToken::HashFunctor> SettingsMap;
class Engine {
public:
Engine(RenderEngine *bl_engine, const std::string &render_delegate_name);
Engine(RenderEngine *bl_engine,
const std::string &render_delegate_name,
SettingsMap &settings);
virtual ~Engine() = default;
virtual void sync(Depsgraph *depsgraph,
bContext *context,
pxr::HdRenderSettingsMap &render_settings) = 0;
SettingsMap &render_settings) = 0;
virtual void render(Depsgraph *depsgraph) = 0;
protected:
@ -38,6 +42,7 @@ class Engine {
RenderEngine *bl_engine_;
std::string render_delegate_name_;
SettingsMap settings_;
/* The order is important due to deletion order */
pxr::HgiUniquePtr hgi_;

View File

@ -14,16 +14,17 @@ const double LIFETIME = 180.0;
std::unique_ptr<PreviewEngine> PreviewEngine::instance_;
PreviewEngine *PreviewEngine::get_instance(RenderEngine *bl_engine,
const std::string &render_delegate_name)
const std::string &render_delegate_name,
SettingsMap &settings)
{
if (!instance_) {
instance_ = std::make_unique<PreviewEngine>(bl_engine, render_delegate_name);
instance_ = std::make_unique<PreviewEngine>(bl_engine, render_delegate_name, settings);
}
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_name);
instance_->update(bl_engine, render_delegate_name, settings);
return instance_.get();
}
@ -96,12 +97,14 @@ double PreviewEngine::free_instance(uintptr_t uuid, void *user_data)
return -1;
}
void PreviewEngine::update(RenderEngine *bl_engine, const std::string &render_delegate_name)
void PreviewEngine::update(RenderEngine *bl_engine,
const std::string &render_delegate_name,
SettingsMap &settings)
{
bl_engine_ = bl_engine;
if (render_delegate_name != render_delegate_name_) {
render_delegate_->Stop();
instance_.reset(new PreviewEngine(bl_engine, render_delegate_name));
instance_.reset(new PreviewEngine(bl_engine, render_delegate_name, settings));
}
}

View File

@ -12,7 +12,8 @@ class PreviewEngine : public FinalEngine {
using FinalEngine::FinalEngine;
static PreviewEngine *get_instance(RenderEngine *bl_engine,
const std::string &render_delegate_name);
const std::string &render_delegate_name,
SettingsMap &settings);
static void schedule_free();
Review

Why did you move it here?

Why did you move it here?
Review

PreviewEngine should be created via PreviewEngine::create() therefore constructor moved to private section.

`PreviewEngine` should be created via `PreviewEngine::create()` therefore constructor moved to private section.
void sync(Depsgraph *depsgraph,
@ -25,7 +26,9 @@ class PreviewEngine : public FinalEngine {
static double free_instance(uintptr_t uuid, void *user_data);
static std::unique_ptr<PreviewEngine> instance_;
void update(RenderEngine *bl_engine, const std::string &render_delegate_name);
void update(RenderEngine *bl_engine,
const std::string &render_delegate_name,
SettingsMap &settings);
void update_render_result(std::vector<float> &pixels);
};

View File

@ -96,34 +96,60 @@ static PyObject *get_render_plugins_func(PyObject * /*self*/, PyObject *args)
return ret;
}
static SettingsMap get_settings(PyObject *pysettings)
{
SettingsMap settings;
PyObject *pyiter = PyObject_GetIter(pysettings);
if (pyiter) {
PyObject *pykey, *pyval;
while (pykey = PyIter_Next(pyiter)) {
pxr::TfToken key(PyUnicode_AsUTF8(pykey));
pyval = PyDict_GetItem(pysettings, pykey);
if (PyLong_Check(pyval)) {
settings[key] = PyLong_AsLong(pyval);
}
else if (PyFloat_Check(pyval)) {
settings[key] = PyFloat_AsDouble(pyval);
}
else if (PyUnicode_Check(pyval)) {
settings[key] = PyUnicode_AsUTF8(pyval);
}
Py_DECREF(pykey);
}
Py_DECREF(pyiter);
}
return settings;
}
static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
{
PyObject *pyengine;
PyObject *pyengine, *pysettings;
char *engine_type, *render_delegate_id;
if (!PyArg_ParseTuple(args, "Oss", &pyengine, &engine_type, &render_delegate_id)) {
if (!PyArg_ParseTuple(args, "OssO", &pyengine, &engine_type, &render_delegate_id, &pysettings)) {
Py_RETURN_NONE;
}
RenderEngine *bl_engine = (RenderEngine *)PyLong_AsVoidPtr(pyengine);
SettingsMap settings = get_settings(pysettings);
Engine *engine;
if (STREQ(engine_type, "VIEWPORT")) {
engine = new ViewportEngine(bl_engine, render_delegate_id);
engine = new ViewportEngine(bl_engine, render_delegate_id, settings);
}
else if (STREQ(engine_type, "PREVIEW")) {
engine = PreviewEngine::get_instance(bl_engine, render_delegate_id);
engine = PreviewEngine::get_instance(bl_engine, render_delegate_id, settings);
}
else {
if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) {
engine = new FinalEngineGL(bl_engine, render_delegate_id);
engine = new FinalEngineGL(bl_engine, render_delegate_id, settings);
}
else {
engine = new FinalEngine(bl_engine, render_delegate_id);
engine = new FinalEngine(bl_engine, render_delegate_id, settings);
}
}
CLOG_INFO(LOG_RENDER_HYDRA, 2, "Engine %016llx %s", engine, engine_type);
return PyLong_FromVoidPtr(engine);
}
@ -157,28 +183,7 @@ static PyObject *engine_sync_func(PyObject * /*self*/, PyObject *args)
Engine *engine = (Engine *)PyLong_AsVoidPtr(pyengine);
Depsgraph *depsgraph = (Depsgraph *)PyLong_AsVoidPtr(pydepsgraph);
bContext *context = (bContext *)PyLong_AsVoidPtr(pycontext);
pxr::HdRenderSettingsMap settings;
PyObject *pyiter = PyObject_GetIter(pysettings);
if (pyiter) {
PyObject *pykey, *pyval;
while (pykey = PyIter_Next(pyiter)) {
pxr::TfToken key(PyUnicode_AsUTF8(pykey));
pyval = PyDict_GetItem(pysettings, pykey);
if (PyLong_Check(pyval)) {
settings[key] = PyLong_AsLong(pyval);
}
else if (PyFloat_Check(pyval)) {
settings[key] = PyFloat_AsDouble(pyval);
}
else if (PyUnicode_Check(pyval)) {
settings[key] = PyUnicode_AsUTF8(pyval);
}
Py_DECREF(pykey);
}
Py_DECREF(pyiter);
}
SettingsMap settings = get_settings(pysettings);
engine->sync(depsgraph, context, settings);