BLEN-367: Fix code style #14

Merged
Bogdan Nagirniak merged 11 commits from BLEN-367 into hydra-render 2023-03-15 09:42:17 +01:00
30 changed files with 403 additions and 307 deletions
Showing only changes of commit ad5d8da198 - Show all commits

View File

@ -1,9 +1,9 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include "BKE_context.h"
#include "DNA_camera_types.h"
#include "DNA_screen_types.h"
#include "BKE_context.h"
#include "camera.h"
#include "utils.h"
@ -35,9 +35,8 @@ CameraData::CameraData(Object *camera_obj, GfVec2i res, GfVec4f tile)
focus_distance = (obj_pos - cam_pos).GetLength();
}
dof_data = std::tuple(std::max(focus_distance, 0.001f),
camera->dof.aperture_fstop,
camera->dof.aperture_blades);
dof_data = std::tuple(
std::max(focus_distance, 0.001f), camera->dof.aperture_fstop, camera->dof.aperture_blades);
}
float ratio = (float)res[0] / res[1];

View File

@ -1,11 +1,11 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <pxr/base/plug/plugin.h>
#include <pxr/base/plug/registry.h>
#include <pxr/imaging/hd/rendererPluginRegistry.h>
#include <pxr/imaging/hdSt/renderDelegate.h>
#include <pxr/imaging/hgi/tokens.h>
#include <pxr/base/plug/plugin.h>
#include <pxr/base/plug/registry.h>
#include <pxr/usd/usdGeom/tokens.h>
#include "glog/logging.h"
@ -16,8 +16,7 @@ using namespace pxr;
namespace blender::render::hydra {
Engine::Engine(RenderEngine *bl_engine, const std::string &delegateId)
: bl_engine(bl_engine)
Engine::Engine(RenderEngine *bl_engine, const std::string &delegateId) : bl_engine(bl_engine)
{
HdRendererPluginRegistry &registry = HdRendererPluginRegistry::GetInstance();

View File

@ -7,16 +7,16 @@
#include <Python.h>
#include <pxr/imaging/hd/driver.h>
#include <pxr/imaging/hd/engine.h>
#include <pxr/imaging/hd/pluginRenderDelegateUniqueHandle.h>
#include <pxr/imaging/hd/driver.h>
#include <pxr/imaging/hdx/freeCameraSceneDelegate.h>
#include <pxr/imaging/hgi/hgi.h>
#include "RE_engine.h"
#include "sceneDelegate/blenderSceneDelegate.h"
#include "renderTaskDelegate.h"
#include "sceneDelegate/blenderSceneDelegate.h"
#include "simpleLightTaskDelegate.h"
namespace blender::render::hydra {
@ -26,7 +26,9 @@ public:
Engine(RenderEngine *bl_engine, const std::string &render_delegate_id);
virtual ~Engine();
virtual void sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &renderSettings) = 0;
virtual void sync(Depsgraph *depsgraph,
bContext *context,
pxr::HdRenderSettingsMap &renderSettings) = 0;
virtual void render(Depsgraph *depsgraph) = 0;
protected:

View File

@ -3,13 +3,13 @@
#include <pxr/imaging/glf/drawTarget.h>
#include "DEG_depsgraph_query.h"
#include "BKE_lib_id.h"
#include "DEG_depsgraph_query.h"
#include "glog/logging.h"
#include "finalEngine.h"
#include "camera.h"
#include "finalEngine.h"
#include "utils.h"
using namespace std;
@ -17,10 +17,14 @@ using namespace pxr;
namespace blender::render::hydra {
void FinalEngine::sync(Depsgraph *depsgraph, bContext *context, HdRenderSettingsMap &renderSettings)
void FinalEngine::sync(Depsgraph *depsgraph,
bContext *context,
HdRenderSettingsMap &renderSettings)
{
scene_delegate = std::make_unique<BlenderSceneDelegate>(render_index.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::Final);
scene_delegate = std::make_unique<BlenderSceneDelegate>(
render_index.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::Final);
scene_delegate->populate(depsgraph, context);
for (auto const &setting : renderSettings) {
@ -40,15 +44,18 @@ void FinalEngine::render(Depsgraph *depsgraph)
RenderData &r = scene->r;
GfVec4f border(0, 0, 1, 1);
if (r.mode & R_BORDER) {
border = GfVec4f(r.border.xmin, r.border.ymin,
r.border.xmax - r.border.xmin, r.border.ymax - r.border.ymin);
border = GfVec4f(r.border.xmin,
r.border.ymin,
r.border.xmax - r.border.xmin,
r.border.ymax - r.border.ymin);
}
GfVec2i image_res(r.xsch * r.size / 100, r.ysch * r.size / 100);
GfVec2i res(int(image_res[0] * border[2]), int(image_res[1] * border[3]));
GfCamera camera = CameraData(scene->camera, image_res, GfVec4f(0, 0, 1, 1)).gf_camera(border);
free_camera_delegate->SetCamera(camera);
render_task_delegate->SetCameraAndViewport(free_camera_delegate->GetCameraId(), GfVec4d(0, 0, res[0], res[1]));
render_task_delegate->SetCameraAndViewport(free_camera_delegate->GetCameraId(),
GfVec4d(0, 0, res[0], res[1]));
render_task_delegate->SetRendererAov(HdAovTokens->color);
if (simple_light_task_delegate) {
simple_light_task_delegate->SetCameraPath(free_camera_delegate->GetCameraId());
@ -84,8 +91,10 @@ void FinalEngine::render(Depsgraph *depsgraph)
timeCurrent = chrono::steady_clock::now();
elapsedTime = chrono::duration_cast<chrono::milliseconds>(timeCurrent - timeBegin);
notify_status(percentDone / 100.0, scene_name + ": " + layer_name,
"Render Time: " + format_duration(elapsedTime) + " | Done: " + to_string(int(percentDone)) + "%");
notify_status(percentDone / 100.0,
scene_name + ": " + layer_name,
"Render Time: " + format_duration(elapsedTime) +
" | Done: " + to_string(int(percentDone)) + "%");
if (render_task_delegate->IsConverged()) {
break;
@ -110,9 +119,13 @@ GfVec2i FinalEngine::get_resolution(Scene *scene)
return GfVec2i(int(r.xsch * border_w * r.size / 100), int(r.ysch * border_h * r.size / 100));
}
void FinalEngine::updateRenderResult(map<string, vector<float>>& renderImages, const string &layerName, int width, int height)
void FinalEngine::updateRenderResult(map<string, vector<float>> &renderImages,
const string &layerName,
int width,
int height)
{
RenderResult *result = RE_engine_begin_result(bl_engine, 0, 0, width, height, layerName.c_str(), nullptr);
RenderResult *result = RE_engine_begin_result(
bl_engine, 0, 0, width, height, layerName.c_str(), nullptr);
/* TODO: only for the first render layer */
RenderLayer *layer = (RenderLayer *)result->layers.first;
@ -121,7 +134,8 @@ void FinalEngine::updateRenderResult(map<string, vector<float>>& renderImages, c
if (it_image == renderImages.end()) {
continue;
}
memcpy(pass->rect, it_image->second.data(),
memcpy(pass->rect,
it_image->second.data(),
sizeof(float) * pass->rectx * pass->recty * pass->channels);
}
@ -146,14 +160,15 @@ void FinalEngineGL::render(Depsgraph *depsgraph)
RenderData &r = scene->r;
GfVec4f border(0, 0, 1, 1);
if (r.mode & R_BORDER) {
border = GfVec4f(r.border.xmin, r.border.ymin,
r.border.xmax - r.border.xmin, r.border.ymax - r.border.ymin);
border = GfVec4f(r.border.xmin,
r.border.ymin,
r.border.xmax - r.border.xmin,
r.border.ymax - r.border.ymin);
}
GfVec2i image_res = {r.xsch * r.size / 100, r.ysch * r.size / 100};
GfVec2i res = {int(image_res[0] * border[2]), int(image_res[1] * border[3])};
GfCamera camera = CameraData(scene->camera, image_res, GfVec4f(0, 0, 1, 1)).gf_camera(border);
free_camera_delegate->SetCamera(camera);
render_task_delegate->SetCameraAndViewport(free_camera_delegate->GetCameraId(),
GfVec4d(0, 0, res[0], res[1]));
@ -164,7 +179,8 @@ void FinalEngineGL::render(Depsgraph *depsgraph)
HdTaskSharedPtrVector tasks;
if (simple_light_task_delegate) {
/* TODO: Uncomment this and fix GL error:
invalid operation, reported from void __cdecl pxrInternal_v0_22__pxrReserved__::HgiGLResourceBindings::BindResources(void) */
invalid operation, reported from void __cdecl
pxrInternal_v0_22__pxrReserved__::HgiGLResourceBindings::BindResources(void) */
// tasks.push_back(simple_light_task_delegate->GetTask());
}
tasks.push_back(render_task_delegate->GetTask());

View File

@ -12,12 +12,17 @@ namespace blender::render::hydra {
class FinalEngine : public Engine {
public:
using Engine::Engine;
virtual void sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &renderSettings) override;
virtual void sync(Depsgraph *depsgraph,
bContext *context,
pxr::HdRenderSettingsMap &renderSettings) override;
virtual void render(Depsgraph *b_depsgraph) override;
protected:
pxr::GfVec2i get_resolution(Scene *scene);
void updateRenderResult(std::map<std::string, std::vector<float>> &render_images, const std::string &layerName, int width, int height);
void updateRenderResult(std::map<std::string, std::vector<float>> &render_images,
const std::string &layerName,
int width,
int height);
void notify_status(float progress, const std::string &title, const std::string &info);
protected:

View File

@ -3,18 +3,22 @@
#include "DEG_depsgraph_query.h"
#include "previewEngine.h"
#include "camera.h"
#include "previewEngine.h"
using namespace pxr;
using namespace std;
namespace blender::render::hydra {
void PreviewEngine::sync(Depsgraph *depsgraph, bContext *context, HdRenderSettingsMap &renderSettings)
void PreviewEngine::sync(Depsgraph *depsgraph,
bContext *context,
HdRenderSettingsMap &renderSettings)
{
scene_delegate = make_unique<BlenderSceneDelegate>(render_index.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::Preview);
scene_delegate = make_unique<BlenderSceneDelegate>(
render_index.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::Preview);
scene_delegate->populate(depsgraph, context);
for (auto const &setting : renderSettings) {
@ -30,10 +34,12 @@ void PreviewEngine::render(Depsgraph *depsgraph)
string layerName = view_layer->name;
GfVec2i res(scene->r.xsch, scene->r.ysch);
GfCamera camera = CameraData(scene->camera, res, GfVec4f(0, 0, 1, 1)).gf_camera(GfVec4f(0, 0, 1, 1));
GfCamera camera =
CameraData(scene->camera, res, GfVec4f(0, 0, 1, 1)).gf_camera(GfVec4f(0, 0, 1, 1));
free_camera_delegate->SetCamera(camera);
render_task_delegate->SetCameraAndViewport(free_camera_delegate->GetCameraId(), GfVec4d(0, 0, res[0], res[1]));
render_task_delegate->SetCameraAndViewport(free_camera_delegate->GetCameraId(),
GfVec4d(0, 0, res[0], res[1]));
render_task_delegate->SetRendererAov(HdAovTokens->color);
HdTaskSharedPtrVector tasks;
@ -67,9 +73,13 @@ void PreviewEngine::render(Depsgraph *depsgraph)
updateRenderResult(layerName, res[0], res[1], pixels);
}
void PreviewEngine::updateRenderResult(const string &layerName, int width, int height, vector<float> &pixels)
void PreviewEngine::updateRenderResult(const string &layerName,
int width,
int height,
vector<float> &pixels)
{
RenderResult *result = RE_engine_begin_result(bl_engine, 0, 0, width, height, layerName.c_str(), nullptr);
RenderResult *result = RE_engine_begin_result(
bl_engine, 0, 0, width, height, layerName.c_str(), nullptr);
RenderLayer *layer = (RenderLayer *)result->layers.first;
RenderPass *pass = (RenderPass *)layer->passes.first;

View File

@ -10,11 +10,16 @@ namespace blender::render::hydra {
class PreviewEngine : public FinalEngine {
public:
using FinalEngine::FinalEngine;
void sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &renderSettings) override;
void sync(Depsgraph *depsgraph,
bContext *context,
pxr::HdRenderSettingsMap &renderSettings) override;
void render(Depsgraph *depsgraph) override;
protected:
void updateRenderResult(const std::string &layerName, int width, int height, std::vector<float> &pixels);
void updateRenderResult(const std::string &layerName,
int width,
int height,
std::vector<float> &pixels);
protected:
HdRenderSettingsMap renderSettings;

View File

@ -14,9 +14,9 @@
#include "glog/logging.h"
#include "finalEngine.h"
#include "viewportEngine.h"
#include "previewEngine.h"
#include "utils.h"
#include "viewportEngine.h"
using namespace std;
@ -47,7 +47,8 @@ static PyObject *init_func(PyObject * /*self*/, PyObject *args)
{
LOG(INFO) << "init_func";
pxr::PlugRegistry::GetInstance().RegisterPlugins(string(BKE_appdir_program_dir()) + "/blender.shared/usd");
pxr::PlugRegistry::GetInstance().RegisterPlugins(string(BKE_appdir_program_dir()) +
"/blender.shared/usd");
setup_usd_mtlx_environment();
@ -108,8 +109,10 @@ static PyObject *get_render_plugins_func(PyObject * /*self*/, PyObject *args)
PyDict_SetItemString(descr, "id", val = PyUnicode_FromString(plugin_ids[i].GetText()));
Py_DECREF(val);
PyDict_SetItemString(descr, "name",
val = PyUnicode_FromString(UsdImagingGLEngine::GetRendererDisplayName(plugin_ids[i]).c_str()));
PyDict_SetItemString(descr,
"name",
val = PyUnicode_FromString(
UsdImagingGLEngine::GetRendererDisplayName(plugin_ids[i]).c_str()));
Py_DECREF(val);
string plugin_name = plugin_ids[i];
@ -222,8 +225,7 @@ static PyObject *engine_render_func(PyObject * /*self*/, PyObject *args)
Depsgraph *depsgraph = (Depsgraph *)PyLong_AsVoidPtr(pydepsgraph);
/* Allow Blender to execute other Python scripts. */
Py_BEGIN_ALLOW_THREADS
engine->render(depsgraph);
Py_BEGIN_ALLOW_THREADS engine->render(depsgraph);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
@ -241,8 +243,7 @@ static PyObject *engine_view_draw_func(PyObject * /*self*/, PyObject *args)
bContext *context = (bContext *)PyLong_AsVoidPtr(pycontext);
/* Allow Blender to execute other Python scripts. */
Py_BEGIN_ALLOW_THREADS
engine->render(depsgraph, context);
Py_BEGIN_ALLOW_THREADS engine->render(depsgraph, context);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;

View File

@ -18,8 +18,10 @@ RenderTaskDelegate::RenderTaskDelegate(HdRenderIndex* parentIndex, SdfPath const
{
SdfPath renderTaskId = GetTaskID();
GetRenderIndex().InsertTask<HdxRenderTask>(this, renderTaskId);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(renderTaskId, HdChangeTracker::DirtyCollection);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(renderTaskId, HdChangeTracker::DirtyRenderTags);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(renderTaskId,
HdChangeTracker::DirtyCollection);
GetRenderIndex().GetChangeTracker().MarkTaskDirty(renderTaskId,
HdChangeTracker::DirtyRenderTags);
taskParams.enableLighting = true;
taskParams.alphaThreshold = 0.1f;
@ -42,7 +44,8 @@ VtValue RenderTaskDelegate::Get(SdfPath const& id, TfToken const& key)
return VtValue(taskParams);
}
if (key == HdTokens->collection) {
HdRprimCollection rprimCollection(HdTokens->geometry, HdReprSelector(HdReprTokens->smoothHull), false, TfToken());
HdRprimCollection rprimCollection(
HdTokens->geometry, HdReprSelector(HdReprTokens->smoothHull), false, TfToken());
rprimCollection.SetRootPath(SdfPath::AbsoluteRootPath());
return VtValue(rprimCollection);
}
@ -71,7 +74,8 @@ void RenderTaskDelegate::SetRendererAov(TfToken const &aov)
{
HdAovDescriptor aovDesc = GetRenderIndex().GetRenderDelegate()->GetDefaultAovDescriptor(aov);
HdRenderBufferDescriptor desc(GfVec3i(taskParams.viewport[2], taskParams.viewport[3], 1),
aovDesc.format, aovDesc.multiSampled);
aovDesc.format,
aovDesc.multiSampled);
SdfPath bufferId = GetAovID(aov);
if (bufferDescriptors.find(bufferId) == bufferDescriptors.end()) {
@ -95,14 +99,17 @@ void RenderTaskDelegate::SetRendererAov(TfToken const &aov)
HdRenderBuffer *RenderTaskDelegate::GetRendererAov(TfToken const &aov)
{
return (HdRenderBuffer *)(GetRenderIndex().GetBprim(HdPrimTypeTokens->renderBuffer, GetAovID(aov)));
return (
HdRenderBuffer *)(GetRenderIndex().GetBprim(HdPrimTypeTokens->renderBuffer, GetAovID(aov)));
}
void RenderTaskDelegate::GetRendererAovData(TfToken const &aov, void *data)
{
HdRenderBuffer *buffer = GetRendererAov(aov);
void *bufData = buffer->Map();
memcpy(data, bufData, buffer->GetWidth() * buffer->GetHeight() * HdDataSizeOfFormat(buffer->GetFormat()));
memcpy(data,
bufData,
buffer->GetWidth() * buffer->GetHeight() * HdDataSizeOfFormat(buffer->GetFormat()));
buffer->Unmap();
}
@ -120,5 +127,4 @@ void RenderTaskDelegate::SetCameraAndViewport(SdfPath const &cameraId, GfVec4d c
}
}
} // namespace blender::render::hydra

View File

@ -3,15 +3,14 @@
#pragma once
#include <pxr/imaging/hdx/renderSetupTask.h>
#include <pxr/imaging/hd/sceneDelegate.h>
#include <pxr/imaging/hdx/renderSetupTask.h>
using namespace pxr;
namespace blender::render::hydra {
class RenderTaskDelegate : public HdSceneDelegate
{
class RenderTaskDelegate : public HdSceneDelegate {
public:
RenderTaskDelegate(HdRenderIndex *parentIndex, SdfPath const &delegateID);
~RenderTaskDelegate() override = default;

View File

@ -13,12 +13,14 @@ using namespace std;
namespace blender::render::hydra {
BlenderSceneDelegate::BlenderSceneDelegate(HdRenderIndex* parentIndex, SdfPath const& delegateID, BlenderSceneDelegate::EngineType engine_type)
: HdSceneDelegate(parentIndex, delegateID)
, engine_type(engine_type)
, depsgraph(nullptr)
, context(nullptr)
, view3d(nullptr)
BlenderSceneDelegate::BlenderSceneDelegate(HdRenderIndex *parentIndex,
SdfPath const &delegateID,
BlenderSceneDelegate::EngineType engine_type)
: HdSceneDelegate(parentIndex, delegateID),
engine_type(engine_type),
depsgraph(nullptr),
context(nullptr),
view3d(nullptr)
{
}
@ -115,8 +117,10 @@ GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(SdfPath const &instancerI
return GfMatrix4d(1.0);
}
size_t BlenderSceneDelegate::SampleInstancerTransform(SdfPath const &instancerId, size_t maxSampleCount,
float *sampleTimes, GfMatrix4d *sampleValues)
size_t BlenderSceneDelegate::SampleInstancerTransform(SdfPath const &instancerId,
size_t maxSampleCount,
float *sampleTimes,
GfMatrix4d *sampleValues)
{
LOG(INFO) << "SampleInstancerTransform: " << instancerId.GetString();
size_t ret = 0;
@ -125,8 +129,11 @@ size_t BlenderSceneDelegate::SampleInstancerTransform(SdfPath const &instancerId
return ret;
}
size_t BlenderSceneDelegate::SamplePrimvar(SdfPath const &id, TfToken const &key, size_t maxSampleCount,
float *sampleTimes, VtValue *sampleValues)
size_t BlenderSceneDelegate::SamplePrimvar(SdfPath const &id,
TfToken const &key,
size_t maxSampleCount,
float *sampleTimes,
VtValue *sampleValues)
{
// TODO: add a separate object for instancer for cleaner handling code
if (id.GetName() == "Instancer") {
@ -164,7 +171,9 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data, Object *, object) {
&data,
Object *,
object) {
if (!ObjectData::supported(object)) {
continue;
@ -216,7 +225,10 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
}
}
void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool transform, bool shading)
void BlenderSceneDelegate::add_update_object(Object *object,
bool geometry,
bool transform,
bool shading)
{
SdfPath id = ObjectData::prim_id(this, object);
ObjectData *obj_data = object_data(id);
@ -317,10 +329,8 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
DEGIDIterData data = {0};
data.graph = depsgraph;
data.only_updated = true;
ITER_BEGIN (DEG_iterator_ids_begin,
DEG_iterator_ids_next,
DEG_iterator_ids_end,
&data, ID *, id) {
ITER_BEGIN (
DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) {
transform = (id->recalc & ID_RECALC_TRANSFORM) != 0;
shading = (id->recalc & (ID_RECALC_SHADING | ID_RECALC_ANIMATION)) != 0;
@ -336,8 +346,7 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
}
geometry |= (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY) != 0;
add_update_object(object, geometry, transform, shading);
}
break;
} break;
case ID_MA:
if (shading) {
@ -409,7 +418,8 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key)
return VtValue();
}
HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(SdfPath const& id, HdInterpolation interpolation)
HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(
SdfPath const &id, HdInterpolation interpolation)
{
LOG(INFO) << "GetPrimvarDescriptors: " << id.GetString() << " " << interpolation;
if (mesh_data(id)) {

View File

@ -8,22 +8,20 @@
#include "BKE_context.h"
#include "DEG_depsgraph.h"
#include "object.h"
#include "mesh.h"
#include "light.h"
#include "mesh.h"
#include "object.h"
#include "world.h"
namespace blender::render::hydra {
class BlenderSceneDelegate : public pxr::HdSceneDelegate {
public:
enum class EngineType {
Viewport = 1,
Final,
Preview
};
enum class EngineType { Viewport = 1, Final, Preview };
BlenderSceneDelegate(pxr::HdRenderIndex *render_index, pxr::SdfPath const &delegateId, BlenderSceneDelegate::EngineType engine_type);
BlenderSceneDelegate(pxr::HdRenderIndex *render_index,
pxr::SdfPath const &delegateId,
BlenderSceneDelegate::EngineType engine_type);
~BlenderSceneDelegate() override = default;
void populate(Depsgraph *depsgraph, bContext *context);
@ -33,18 +31,25 @@ public:
pxr::GfMatrix4d GetTransform(pxr::SdfPath const &id) override;
pxr::VtValue Get(pxr::SdfPath const &id, pxr::TfToken const &key) override;
pxr::VtValue GetLightParamValue(pxr::SdfPath const &id, pxr::TfToken const &key) override;
pxr::HdPrimvarDescriptorVector GetPrimvarDescriptors(pxr::SdfPath const &id, pxr::HdInterpolation interpolation) override;
pxr::HdPrimvarDescriptorVector GetPrimvarDescriptors(
pxr::SdfPath const &id, pxr::HdInterpolation interpolation) override;
pxr::SdfPath GetMaterialId(pxr::SdfPath const &rprimId) override;
pxr::VtValue GetMaterialResource(pxr::SdfPath const &materialId) override;
bool GetVisible(pxr::SdfPath const &id) override;
pxr::SdfPath GetInstancerId(pxr::SdfPath const &primId) override;
pxr::SdfPathVector GetInstancerPrototypes(pxr::SdfPath const &instancerId) override;
pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancerId, pxr::SdfPath const &prototypeId) override;
pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancerId,
pxr::SdfPath const &prototypeId) override;
pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancerId);
size_t SampleInstancerTransform(pxr::SdfPath const &instancerId, size_t maxSampleCount,
float *sampleTimes, pxr::GfMatrix4d *sampleValues) override;
size_t SamplePrimvar(pxr::SdfPath const &id, pxr::TfToken const &key, size_t maxSampleCount,
float *sampleTimes, pxr::VtValue *sampleValues) override;
size_t SampleInstancerTransform(pxr::SdfPath const &instancerId,
size_t maxSampleCount,
float *sampleTimes,
pxr::GfMatrix4d *sampleValues) override;
size_t SamplePrimvar(pxr::SdfPath const &id,
pxr::TfToken const &key,
size_t maxSampleCount,
float *sampleTimes,
pxr::VtValue *sampleValues) override;
EngineType engine_type;

View File

@ -11,8 +11,7 @@ using namespace pxr;
namespace blender::render::hydra {
IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id)
: scene_delegate(scene_delegate)
, id(id)
: scene_delegate(scene_delegate), id(id)
{
}

View File

@ -3,8 +3,8 @@
#pragma once
#include <pxr/base/vt/value.h>
#include <pxr/base/tf/token.h>
#include <pxr/base/vt/value.h>
#include <pxr/imaging/hd/sceneDelegate.h>
#include "DNA_ID.h"
@ -22,12 +22,7 @@ class IdData {
virtual pxr::VtValue get_data(pxr::TfToken const &key);
template<class T> const T get_data(pxr::TfToken const &key);
enum class DirtyBits {
DirtyTransform = 1,
DirtyVisibility,
DirtyMaterial,
AllDirty
};
enum class DirtyBits { DirtyTransform = 1, DirtyVisibility, DirtyMaterial, AllDirty };
virtual void insert_prim() = 0;
virtual void remove_prim() = 0;

View File

@ -25,9 +25,10 @@ LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object)
{
Light *light = (Light *)((Object *)id)->data;
data[HdLightTokens->intensity] = scene_delegate->engine_type == BlenderSceneDelegate::EngineType::Preview
? light->energy / 1000
: light->energy;
data[HdLightTokens->intensity] = scene_delegate->engine_type ==
BlenderSceneDelegate::EngineType::Preview ?
light->energy / 1000 :
light->energy;
data[HdLightTokens->color] = GfVec3f(light->r, light->g, light->b);

View File

@ -3,9 +3,9 @@
#pragma once
#include "pxr/base/tf/hashmap.h"
#include <pxr/usd/sdf/assetPath.h>
#include <pxr/usd/sdf/path.h>
#include "pxr/base/tf/hashmap.h"
#include "object.h"

View File

@ -3,14 +3,14 @@
#include <Python.h>
#include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hd/material.h>
#include <pxr/imaging/hd/renderDelegate.h>
#include <pxr/imaging/hd/tokens.h>
#include "glog/logging.h"
#include "BKE_material.h"
#include "BKE_lib_id.h"
#include "BKE_material.h"
#include "blenderSceneDelegate.h"
#include "material.h"
@ -20,7 +20,8 @@ using namespace pxr;
namespace blender::render::hydra {
std::unique_ptr<MaterialData> MaterialData::init(BlenderSceneDelegate *scene_delegate, Material *material)
std::unique_ptr<MaterialData> MaterialData::init(BlenderSceneDelegate *scene_delegate,
Material *material)
{
return std::make_unique<MaterialData>(scene_delegate, material);
}

View File

@ -3,9 +3,9 @@
#pragma once
#include "pxr/base/tf/hashmap.h"
#include <pxr/usd/sdf/assetPath.h>
#include <pxr/usd/sdf/path.h>
#include "pxr/base/tf/hashmap.h"
#include "DNA_material_types.h"
@ -15,7 +15,8 @@ namespace blender::render::hydra {
class MaterialData : IdData {
public:
static std::unique_ptr<MaterialData> init(BlenderSceneDelegate *scene_delegate, Material *material);
static std::unique_ptr<MaterialData> init(BlenderSceneDelegate *scene_delegate,
Material *material);
static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Material *material);
MaterialData(BlenderSceneDelegate *scene_delegate, Material *material);
@ -32,6 +33,7 @@ private:
pxr::SdfAssetPath mtlx_path;
};
using MaterialDataMap = pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<MaterialData>, pxr::SdfPath::Hash>;
using MaterialDataMap =
pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<MaterialData>, pxr::SdfPath::Hash>;
} // namespace blender::render::hydra

View File

@ -6,14 +6,14 @@
#include "glog/logging.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
#include "BKE_mesh_runtime.h"
#include "BKE_object.h"
#include "BKE_material.h"
#include "../utils.h"
#include "blenderSceneDelegate.h"
#include "mesh.h"
#include "../utils.h"
using namespace pxr;
@ -64,8 +64,10 @@ Material *MeshData::material()
HdMeshTopology MeshData::mesh_topology()
{
return HdMeshTopology(PxOsdOpenSubdivTokens->catmullClark, HdTokens->rightHanded,
face_vertex_counts, face_vertex_indices);
return HdMeshTopology(PxOsdOpenSubdivTokens->catmullClark,
HdTokens->rightHanded,
face_vertex_counts,
face_vertex_indices);
}
HdPrimvarDescriptorVector MeshData::primvar_descriptors(HdInterpolation interpolation)
@ -81,7 +83,8 @@ HdPrimvarDescriptorVector MeshData::primvar_descriptors(HdInterpolation interpol
primvars.emplace_back(HdTokens->normals, interpolation, HdPrimvarRoleTokens->normal);
}
if (!uvs.empty()) {
primvars.emplace_back(HdPrimvarRoleTokens->textureCoordinate, interpolation,
primvars.emplace_back(HdPrimvarRoleTokens->textureCoordinate,
interpolation,
HdPrimvarRoleTokens->textureCoordinate);
}
}
@ -92,8 +95,8 @@ HdPrimvarDescriptorVector MeshData::instancer_primvar_descriptors(HdInterpolatio
{
HdPrimvarDescriptorVector primvars;
if (interpolation == HdInterpolationInstance) {
primvars.emplace_back(HdInstancerTokens->instanceTransform, interpolation,
HdPrimvarRoleTokens->none);
primvars.emplace_back(
HdInstancerTokens->instanceTransform, interpolation, HdPrimvarRoleTokens->none);
}
return primvars;
}
@ -107,14 +110,19 @@ VtIntArray MeshData::instance_indices()
return ret;
}
size_t MeshData::sample_instancer_transform(size_t maxSampleCount, float *sampleTimes, GfMatrix4d *sampleValues)
size_t MeshData::sample_instancer_transform(size_t maxSampleCount,
float *sampleTimes,
GfMatrix4d *sampleValues)
{
*sampleTimes = 0.0f;
*sampleValues = GfMatrix4d(1.0);
return 1;
}
size_t MeshData::sample_instancer_primvar(TfToken const &key, size_t maxSampleCount, float *sampleTimes, VtValue *sampleValues)
size_t MeshData::sample_instancer_primvar(TfToken const &key,
size_t maxSampleCount,
float *sampleTimes,
VtValue *sampleValues)
{
if (key == HdInstancerTokens->instanceTransform) {
if (maxSampleCount > 0) {
@ -134,7 +142,8 @@ void MeshData::add_instance(DupliObject *dupli)
LOG(INFO) << "Add instancer: " << name() << " id=" << instancer_id.GetAsString();
}
if (instances.empty()) {
// USD hides the prototype mesh when instancing in contrary to the Blender, so we must add it back implicitly
// USD hides the prototype mesh when instancing in contrary to the Blender, so we must add it
// back implicitly
instances.push_back(GfMatrix4d(1.0));
}
instances.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat));

View File

@ -27,8 +27,13 @@ public:
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray instance_indices();
size_t sample_instancer_transform(size_t maxSampleCount, float *sampleTimes, pxr::GfMatrix4d *sampleValues);
size_t sample_instancer_primvar(pxr::TfToken const &key, size_t maxSampleCount, float *sampleTimes, pxr::VtValue *sampleValues);
size_t sample_instancer_transform(size_t maxSampleCount,
float *sampleTimes,
pxr::GfMatrix4d *sampleValues);
size_t sample_instancer_primvar(pxr::TfToken const &key,
size_t maxSampleCount,
float *sampleTimes,
pxr::VtValue *sampleValues);
void add_instance(DupliObject *dupli);
@ -47,6 +52,4 @@ public:
pxr::VtMatrix4dArray instances;
};
} // namespace blender::render::hydra

View File

@ -3,11 +3,11 @@
#include "BKE_object.h"
#include "blenderSceneDelegate.h"
#include "object.h"
#include "mesh.h"
#include "light.h"
#include "../utils.h"
#include "blenderSceneDelegate.h"
#include "light.h"
#include "mesh.h"
#include "object.h"
using namespace pxr;
@ -60,8 +60,7 @@ pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *o
}
ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object)
: IdData(scene_delegate, (ID *)object)
, visible(true)
: IdData(scene_delegate, (ID *)object), visible(true)
{
}

View File

@ -3,11 +3,11 @@
#pragma once
#include <pxr/base/gf/matrix4d.h>
#include "pxr/base/tf/hashmap.h"
#include <pxr/base/gf/matrix4d.h>
#include "DNA_object_types.h"
#include "BKE_layer.h"
#include "DNA_object_types.h"
#include "id.h"
#include "material.h"
@ -29,6 +29,7 @@ public:
bool visible;
};
using ObjectDataMap = pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<ObjectData>, pxr::SdfPath::Hash>;
using ObjectDataMap =
pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<ObjectData>, pxr::SdfPath::Hash>;
} // namespace blender::render::hydra

View File

@ -3,28 +3,28 @@
#include <filesystem>
#include <pxr/base/vt/array.h>
#include <pxr/base/gf/vec2f.h>
#include <pxr/base/gf/rotation.h>
#include <pxr/base/gf/vec2f.h>
#include <pxr/base/vt/array.h>
#include <pxr/imaging/hd/light.h>
#include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hd/renderDelegate.h>
#include <pxr/imaging/hd/tokens.h>
#include <pxr/usd/usdLux/tokens.h>
#include "BKE_context.h"
#include "DNA_node_types.h"
#include "DNA_windowmanager_types.h"
#include "BKE_image.h"
#include "BKE_node.h"
#include "BKE_node_runtime.hh"
#include "BKE_image.h"
#include "NOD_shader.h"
#include "glog/logging.h"
#include "../utils.h"
#include "blenderSceneDelegate.h"
#include "world.h"
#include "../utils.h"
/* TODO : add custom tftoken "transparency"? */
@ -33,7 +33,8 @@ using namespace pxr;
namespace blender::render::hydra {
std::unique_ptr<WorldData> WorldData::init(BlenderSceneDelegate *scene_delegate,
World *world, bContext *context)
World *world,
bContext *context)
{
return std::make_unique<WorldData>(scene_delegate, world, context);
}

View File

@ -5,11 +5,11 @@
#include <map>
#include "pxr/base/tf/staticTokens.h"
#include <pxr/base/gf/matrix4d.h>
#include <pxr/base/vt/value.h>
#include <pxr/usd/sdf/assetPath.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/base/vt/value.h>
#include "pxr/base/tf/staticTokens.h"
#include "DNA_view3d_types.h"
#include "DNA_world_types.h"
@ -20,7 +20,9 @@ namespace blender::render::hydra {
class WorldData : public IdData {
public:
static std::unique_ptr<WorldData> init(BlenderSceneDelegate *scene_delegate, World *world, bContext *context);
static std::unique_ptr<WorldData> init(BlenderSceneDelegate *scene_delegate,
World *world,
bContext *context);
static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate);
WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context);

View File

@ -1,15 +1,15 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <sstream>
#include <filesystem>
#include <sstream>
#include <pxr/base/tf/stringUtils.h>
#include "BKE_appdir.h"
#include "BKE_image_save.h"
#include "BLI_string.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "DNA_camera_types.h"
@ -22,11 +22,22 @@ namespace blender::render::hydra {
GfMatrix4d gf_matrix_from_transform(float m[4][4])
{
return GfMatrix4d(
m[0][0], m[0][1], m[0][2], m[0][3],
m[1][0], m[1][1], m[1][2], m[1][3],
m[2][0], m[2][1], m[2][2], m[2][3],
m[3][0], m[3][1], m[3][2], m[3][3]);
return GfMatrix4d(m[0][0],
m[0][1],
m[0][2],
m[0][3],
m[1][0],
m[1][1],
m[1][2],
m[1][3],
m[2][0],
m[2][1],
m[2][2],
m[2][3],
m[3][0],
m[3][1],
m[3][2],
m[3][3]);
}
string format_duration(chrono::milliseconds millisecs)
@ -101,7 +112,8 @@ string cache_image(Main *bmain,
void set_env_paths(string const &name, vector<string> path_dirs)
{
const char *env = BLI_getenv(name.c_str());;
const char *env = BLI_getenv(name.c_str());
;
stringstream ss;
int i = 0;
for (string &s : path_dirs) {

View File

@ -7,18 +7,18 @@
#include <pxr/imaging/glf/drawTarget.h>
#include <pxr/usd/usdGeom/camera.h>
#include "DEG_depsgraph_query.h"
#include "BKE_camera.h"
#include "BLI_math_matrix.h"
#include "DEG_depsgraph_query.h"
#include "DNA_camera_types.h"
#include "DNA_screen_types.h"
#include "BKE_camera.h"
#include "GPU_shader.h"
#include "glog/logging.h"
#include "viewportEngine.h"
#include "camera.h"
#include "utils.h"
#include "viewportEngine.h"
using namespace std;
using namespace pxr;
@ -40,8 +40,7 @@ struct ViewSettings {
GfVec4i border;
};
ViewSettings::ViewSettings(bContext *context)
: camera_data(context)
ViewSettings::ViewSettings(bContext *context) : camera_data(context)
{
View3D *view3d = CTX_wm_view3d(context);
RegionView3D *region_data = (RegionView3D *)CTX_wm_region_data(context);
@ -65,21 +64,28 @@ ViewSettings::ViewSettings(bContext *context)
float screen_points[4][2];
for (int i = 0; i < 4; i++) {
float world_location[] = {camera_points[i][0], camera_points[i][1], camera_points[i][2], 1.0f};
float world_location[] = {
camera_points[i][0], camera_points[i][1], camera_points[i][2], 1.0f};
mul_m4_v4(camera_obj->object_to_world, world_location);
mul_m4_v4(region_data->persmat, world_location);
if (world_location[3] > 0.0) {
screen_points[i][0] = screen_width * 0.5f + screen_width * 0.5f * (world_location[0] / world_location[3]);
screen_points[i][1] = screen_height * 0.5f + screen_height * 0.5f * (world_location[1] / world_location[3]);
screen_points[i][0] = screen_width * 0.5f +
screen_width * 0.5f * (world_location[0] / world_location[3]);
screen_points[i][1] = screen_height * 0.5f +
screen_height * 0.5f * (world_location[1] / world_location[3]);
}
}
// getting camera view region
float x1_f = min({screen_points[0][0], screen_points[1][0], screen_points[2][0], screen_points[3][0]});
float x2_f = max({screen_points[0][0], screen_points[1][0], screen_points[2][0], screen_points[3][0]});
float y1_f = min({screen_points[0][1], screen_points[1][1], screen_points[2][1], screen_points[3][1]});
float y2_f = max({screen_points[0][1], screen_points[1][1], screen_points[2][1], screen_points[3][1]});
float x1_f = min(
{screen_points[0][0], screen_points[1][0], screen_points[2][0], screen_points[3][0]});
float x2_f = max(
{screen_points[0][0], screen_points[1][0], screen_points[2][0], screen_points[3][0]});
float y1_f = min(
{screen_points[0][1], screen_points[1][1], screen_points[2][1], screen_points[3][1]});
float y2_f = max(
{screen_points[0][1], screen_points[1][1], screen_points[2][1], screen_points[3][1]});
// adjusting region to border
float x = x1_f, y = y1_f;
@ -124,16 +130,13 @@ int ViewSettings::height()
GfCamera ViewSettings::gf_camera()
{
return camera_data.gf_camera(GfVec4f(
(float)border[0] / screen_width, (float)border[1] / screen_height,
(float)border[2] / screen_width, (float)border[3] / screen_height));
return camera_data.gf_camera(GfVec4f((float)border[0] / screen_width,
(float)border[1] / screen_height,
(float)border[2] / screen_width,
(float)border[3] / screen_height));
}
GLTexture::GLTexture()
: textureId(0)
, width(0)
, height(0)
, channels(4)
GLTexture::GLTexture() : textureId(0), width(0), height(0), channels(4)
{
}
@ -239,11 +242,15 @@ void GLTexture::draw(GLfloat x, GLfloat y)
glDeleteVertexArrays(1, &vertex_array);
}
void ViewportEngine::sync(Depsgraph *depsgraph, bContext *context, HdRenderSettingsMap &renderSettings)
void ViewportEngine::sync(Depsgraph *depsgraph,
bContext *context,
HdRenderSettingsMap &renderSettings)
{
if (!scene_delegate) {
scene_delegate = std::make_unique<BlenderSceneDelegate>(render_index.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene"), BlenderSceneDelegate::EngineType::Viewport);
scene_delegate = std::make_unique<BlenderSceneDelegate>(
render_index.get(),
SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::Viewport);
}
scene_delegate->populate(depsgraph, context);
@ -262,7 +269,10 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
GfCamera gfCamera = viewSettings.gf_camera();
free_camera_delegate->SetCamera(gfCamera);
render_task_delegate->SetCameraAndViewport(free_camera_delegate->GetCameraId(),
GfVec4d(viewSettings.border[0], viewSettings.border[1], viewSettings.border[2], viewSettings.border[3]));
GfVec4d(viewSettings.border[0],
viewSettings.border[1],
viewSettings.border[2],
viewSettings.border[3]));
if (simple_light_task_delegate) {
simple_light_task_delegate->SetCameraPath(free_camera_delegate->GetCameraId());
}
@ -298,12 +308,15 @@ void ViewportEngine::render(Depsgraph *depsgraph, bContext *context)
GPU_shader_unbind();
chrono::time_point<chrono::steady_clock> timeCurrent = chrono::steady_clock::now();
chrono::milliseconds elapsedTime = chrono::duration_cast<chrono::milliseconds>(timeCurrent - timeBegin);
chrono::milliseconds elapsedTime = chrono::duration_cast<chrono::milliseconds>(timeCurrent -
timeBegin);
string formattedTime = format_duration(elapsedTime);
if (!render_task_delegate->IsConverged()) {
notify_status("Time: " + formattedTime + " | Done: " + to_string(int(renderer_percent_done())) + "%", "Render");
notify_status("Time: " + formattedTime +
" | Done: " + to_string(int(renderer_percent_done())) + "%",
"Render");
bl_engine->flag |= RE_ENGINE_DO_DRAW;
}
else {

View File

@ -13,8 +13,7 @@
namespace blender::render::hydra {
class GLTexture
{
class GLTexture {
public:
GLTexture();
~GLTexture();
@ -32,7 +31,9 @@ private:
class ViewportEngine : public Engine {
public:
using Engine::Engine;
void sync(Depsgraph *depsgraph, bContext *context, pxr::HdRenderSettingsMap &renderSettings) override;
void sync(Depsgraph *depsgraph,
bContext *context,
pxr::HdRenderSettingsMap &renderSettings) override;
void render(Depsgraph *depsgraph) override;
void render(Depsgraph *depsgraph, bContext *context);