BLEN-343: Create PreviewEngine #8

Merged
Bogdan Nagirniak merged 5 commits from BLEN-343 into hydra-render 2023-03-03 14:55:28 +01:00
11 changed files with 140 additions and 5 deletions
Showing only changes of commit 3d3f51fa4b - Show all commits

View File

@ -46,7 +46,7 @@ class HydraRenderEngine(bpy.types.RenderEngine):
delegate_settings = self.get_delegate_settings(engine_type) delegate_settings = self.get_delegate_settings(engine_type)
_hydra.engine_sync(self.engine_ptr, depsgraph.as_pointer(), bpy.context.as_pointer(), delegate_settings) _hydra.engine_sync(self.engine_ptr, depsgraph.as_pointer(), bpy.context.as_pointer(), delegate_settings)
_hydra.engine_render(self.engine_ptr, depsgraph.as_pointer()) _hydra.engine_render(self.engine_ptr, depsgraph.as_pointer(), engine_type)
Review

revert this

revert this
# viewport render # viewport render
def view_update(self, context, depsgraph): def view_update(self, context, depsgraph):

View File

@ -50,6 +50,8 @@ set(SRC
engine.cc engine.cc
finalEngine.h finalEngine.h
finalEngine.cc finalEngine.cc
previewEngine.h
previewEngine.cc
viewportEngine.h viewportEngine.h
viewportEngine.cc viewportEngine.cc
camera.h camera.h

View File

@ -27,6 +27,7 @@ public:
virtual ~Engine(); virtual ~Engine();
DagerD marked this conversation as resolved
Review

move to BlenderSceneDelegate class

move to BlenderSceneDelegate class
virtual void sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) = 0; virtual void sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) = 0;
virtual void render(BL::Depsgraph &b_depsgraph) = 0;
protected: protected:
float getRendererPercentDone(); float getRendererPercentDone();

View File

@ -19,7 +19,7 @@ using namespace pxr;
namespace blender::render::hydra { namespace blender::render::hydra {
void FinalEngine::sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) void FinalEngine::sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, HdRenderSettingsMap &renderSettings)
{ {
sceneDelegate = std::make_unique<BlenderSceneDelegate>(renderIndex.get(), sceneDelegate = std::make_unique<BlenderSceneDelegate>(renderIndex.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene")); SdfPath::AbsoluteRootPath().AppendElementString("scene"));

View File

@ -13,7 +13,7 @@ class FinalEngine : public Engine {
public: public:
using Engine::Engine; using Engine::Engine;
void sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) override; void sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) override;
virtual void render(BL::Depsgraph &b_depsgraph); virtual void render(BL::Depsgraph &b_depsgraph) override;
protected: protected:
pxr::GfVec2i get_resolution(BL::RenderSettings b_render); pxr::GfVec2i get_resolution(BL::RenderSettings b_render);

View File

@ -0,0 +1,81 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <pxr/usdImaging/usdAppUtils/camera.h>
#include "previewEngine.h"
#include "camera.h"
using namespace pxr;
using namespace std;
namespace blender::render::hydra {
void PreviewEngine::sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, HdRenderSettingsMap &renderSettings)
{
is_synced = false;
sceneDelegate = std::make_unique<BlenderSceneDelegate>(renderIndex.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene"));
sceneDelegate->populate((Depsgraph *)b_depsgraph.ptr.data, (bContext *)b_context.ptr.data);
for (auto const& setting : renderSettings) {
renderDelegate->SetRenderSetting(setting.first, setting.second);
}
is_synced = true;
}
void PreviewEngine::render(BL::Depsgraph &b_depsgraph)
{
if (!is_synced) {
return;
DagerD marked this conversation as resolved
Review

check if (!sceneDelegate) instead is_synced

check `if (!sceneDelegate)` instead is_synced
}
BL::Scene b_scene = b_depsgraph.scene();
string layerName = b_depsgraph.view_layer().name();
GfVec2i buffer_res {b_scene.render().resolution_x(),
b_scene.render().resolution_y()};
GfCamera gfCamera = CameraData((Object *)b_scene.camera().ptr.data, buffer_res, GfVec4f(0, 0, 1, 1)).gf_camera(GfVec4f(0, 0, 1, 1));
freeCameraDelegate->SetCamera(gfCamera);
renderTaskDelegate->SetCameraAndViewport(freeCameraDelegate->GetCameraId(), GfVec4d(0, 0, buffer_res[0], buffer_res[1]));
renderTaskDelegate->SetRendererAov(HdAovTokens->color);
HdTaskSharedPtrVector tasks = renderTaskDelegate->GetTasks();
vector<float> pixels = vector<float>(buffer_res[0] * buffer_res[1] * 4); // 4 - number of channels
{
// Release the GIL before calling into hydra, in case any hydra plugins call into python.
TF_PY_ALLOW_THREADS_IN_SCOPE();
engine->Execute(renderIndex.get(), &tasks);
}
while (true) {
if (b_engine.test_break()) {
break;
}
if (renderTaskDelegate->IsConverged()) {
break;
}
renderTaskDelegate->GetRendererAovData(HdAovTokens->color, pixels.data());
updateRenderResult(layerName, buffer_res[0], buffer_res[1], pixels);
}
renderTaskDelegate->GetRendererAovData(HdAovTokens->color, pixels.data());
updateRenderResult(layerName, buffer_res[0], buffer_res[1], pixels);
}
void PreviewEngine::updateRenderResult(const string &layerName, int width, int height, vector<float> &pixels)
{
BL::RenderResult b_result = b_engine.begin_result(0, 0, width, height, layerName.c_str(), NULL);
BL::CollectionRef b_passes = b_result.layers[0].passes;
b_passes[0].rect(pixels.data());
b_engine.end_result(b_result, false, false, false);
}
} // namespace blender::render::hydra

View File

@ -0,0 +1,28 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include "engine.h"
namespace blender::render::hydra {
class PreviewEngine : public Engine {
DagerD marked this conversation as resolved
Review

Maybe inherit from FinalEngine?

Maybe inherit from FinalEngine?
public:
using Engine::Engine;
void sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) override;
void render(BL::Depsgraph &b_depsgraph) override;
protected:
pxr::GfVec2i get_resolution(BL::RenderSettings b_render);
void updateRenderResult(const std::string &layerName, int width, int height, std::vector<float> &pixels);
void notifyStatus(float progress, const std::string &title, const std::string &info);
protected:
HdRenderSettingsMap renderSettings;
private:
bool is_synced;
};
} // namespace blender::render::hydra

View File

@ -16,6 +16,7 @@
#include "finalEngine.h" #include "finalEngine.h"
#include "viewportEngine.h" #include "viewportEngine.h"
#include "previewEngine.h"
using namespace std; using namespace std;
@ -130,9 +131,13 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
BL::RenderEngine b_engine(engineptr); BL::RenderEngine b_engine(engineptr);
Engine *engine; Engine *engine;
if (string(engineType) == "VIEWPORT") { if (string(engineType) == "VIEWPORT") {
engine = new ViewportEngine(b_engine, delegateId); engine = new ViewportEngine(b_engine, delegateId);
} }
else if (string(engineType) == "PREVIEW") {
engine = new PreviewEngine(b_engine, delegateId);
}
else { else {
if (b_engine.bl_use_gpu_context()) { if (b_engine.bl_use_gpu_context()) {
engine = new FinalEngineGL(b_engine, delegateId); engine = new FinalEngineGL(b_engine, delegateId);
@ -204,11 +209,20 @@ static PyObject *engine_sync_func(PyObject * /*self*/, PyObject *args)
static PyObject *engine_render_func(PyObject * /*self*/, PyObject *args) static PyObject *engine_render_func(PyObject * /*self*/, PyObject *args)
{ {
PyObject *pyengine, *pydepsgraph; PyObject *pyengine, *pydepsgraph;
if (!PyArg_ParseTuple(args, "OO", &pyengine, &pydepsgraph)) { char *engineType;
if (!PyArg_ParseTuple(args, "OOs", &pyengine, &pydepsgraph, &engineType)) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
FinalEngine *engine = (FinalEngine *)PyLong_AsVoidPtr(pyengine); Engine *engine;
if (string(engineType) == "PREVIEW") {
engine = (PreviewEngine *)PyLong_AsVoidPtr(pyengine);
}
else {
engine = (FinalEngine *)PyLong_AsVoidPtr(pyengine);
}
PointerRNA depsgraphptr; PointerRNA depsgraphptr;
RNA_pointer_create(NULL, &RNA_Depsgraph, (ID *)PyLong_AsVoidPtr(pydepsgraph), &depsgraphptr); RNA_pointer_create(NULL, &RNA_Depsgraph, (ID *)PyLong_AsVoidPtr(pydepsgraph), &depsgraphptr);

View File

@ -27,6 +27,10 @@ void BlenderSceneDelegate::set_material(MeshData &mesh_data)
mesh_data.material_id = SdfPath::EmptyPath(); mesh_data.material_id = SdfPath::EmptyPath();
return; return;
} }
if (!material->flag) {
mesh_data.material_id = SdfPath::EmptyPath();
return;
}
SdfPath id = MaterialData::prim_id(this, material); SdfPath id = MaterialData::prim_id(this, material);
MaterialData *mat_data = material_data(id); MaterialData *mat_data = material_data(id);
if (!mat_data) { if (!mat_data) {

View File

@ -307,6 +307,10 @@ void ViewportEngine::viewDraw(BL::Depsgraph &b_depsgraph, BL::Context &b_context
} }
} }
void ViewportEngine::render(BL::Depsgraph& b_depsgraph)
{
}
void ViewportEngine::notifyStatus(const string &info, const string &status) void ViewportEngine::notifyStatus(const string &info, const string &status)
{ {
b_engine.update_stats(status.c_str(), info.c_str()); b_engine.update_stats(status.c_str(), info.c_str());

View File

@ -34,6 +34,7 @@ public:
using Engine::Engine; using Engine::Engine;
void sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) override; void sync(BL::Depsgraph &b_depsgraph, BL::Context &b_context, pxr::HdRenderSettingsMap &renderSettings) override;
void viewDraw(BL::Depsgraph &b_depsgraph, BL::Context &b_context); void viewDraw(BL::Depsgraph &b_depsgraph, BL::Context &b_context);
void render(BL::Depsgraph &b_depsgraph) override;
private: private:
void notifyStatus(const std::string &title, const std::string &info); void notifyStatus(const std::string &title, const std::string &info);