BLEN-359: Implement updates for instances in viewport #20

Merged
Bogdan Nagirniak merged 20 commits from BLEN-359_1 into hydra-render 2023-04-05 11:47:37 +02:00
18 changed files with 681 additions and 477 deletions

View File

@ -85,6 +85,8 @@ set(SRC
scene_delegate/light.cc scene_delegate/light.cc
scene_delegate/world.h scene_delegate/world.h
scene_delegate/world.cc scene_delegate/world.cc
scene_delegate/instancer.h
scene_delegate/instancer.cc
) )
set(LIB set(LIB

View File

@ -107,7 +107,6 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
RenderEngine *bl_engine = (RenderEngine *)PyLong_AsVoidPtr(pyengine); RenderEngine *bl_engine = (RenderEngine *)PyLong_AsVoidPtr(pyengine);
Engine *engine; Engine *engine;
if (STREQ(engine_type, "VIEWPORT")) { if (STREQ(engine_type, "VIEWPORT")) {
engine = new ViewportEngine(bl_engine, render_delegate_id); engine = new ViewportEngine(bl_engine, render_delegate_id);

View File

@ -1,6 +1,8 @@
/* SPDX-License-Identifier: Apache-2.0 /* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */ * Copyright 2011-2022 Blender Foundation */
#include <bitset>
#include "DEG_depsgraph_query.h" #include "DEG_depsgraph_query.h"
#include "DNA_scene_types.h" #include "DNA_scene_types.h"
@ -22,50 +24,20 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
{ {
} }
void BlenderSceneDelegate::set_material(MeshData &mesh_data)
{
Material *material = mesh_data.material();
if (!material) {
mesh_data.material_id = pxr::SdfPath::EmptyPath();
return;
}
pxr::SdfPath id = MaterialData::prim_id(this, material);
MaterialData *mat_data = material_data(id);
if (!mat_data) {
materials[id] = MaterialData::init(this, material);
mat_data = material_data(id);
mat_data->export_mtlx();
mat_data->insert_prim();
}
mesh_data.material_id = id;
}
void BlenderSceneDelegate::update_material(Material *material)
{
MaterialData *mat_data = material_data(MaterialData::prim_id(this, material));
if (mat_data) {
mat_data->export_mtlx();
mat_data->mark_prim_dirty(IdData::DirtyBits::ALL_DIRTY);
}
}
void BlenderSceneDelegate::update_world() void BlenderSceneDelegate::update_world()
{ {
Scene *scene = DEG_get_input_scene(depsgraph);
World *world = scene->world; World *world = scene->world;
if (!world_data) { if (!world_data) {
if (world) { if (world) {
world_data = WorldData::init(this, world, context); world_data = WorldData::create(this, world, context);
world_data->insert_prim();
} }
} }
else { else {
if (world) { if (world) {
world_data = WorldData::init(this, world, context); world_data->update(world);
world_data->mark_prim_dirty(IdData::DirtyBits::ALL_DIRTY);
} }
else { else {
world_data->remove_prim(); world_data->remove();
world_data = nullptr; world_data = nullptr;
} }
} }
@ -83,9 +55,9 @@ bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id)
pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id) pxr::SdfPath BlenderSceneDelegate::GetInstancerId(pxr::SdfPath const &prim_id)
{ {
CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText()); CLOG_INFO(LOG_BSD, 3, "%s", prim_id.GetText());
MeshData *m_data = mesh_data(prim_id); InstancerData *i_data = instancer_data(prim_id, true);
if (m_data) { if (i_data) {
return m_data->instancer_id; return i_data->instancer_id;
} }
return pxr::SdfPath(); return pxr::SdfPath();
} }
@ -102,43 +74,15 @@ pxr::VtIntArray BlenderSceneDelegate::GetInstanceIndices(pxr::SdfPath const &ins
pxr::SdfPath const &prototype_id) pxr::SdfPath const &prototype_id)
{ {
CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText()); CLOG_INFO(LOG_BSD, 3, "%s, %s", instancer_id.GetText(), prototype_id.GetText());
MeshData *m_data = mesh_data(prototype_id); InstancerData *i_data = instancer_data(instancer_id);
pxr::VtIntArray ret = m_data->instance_indices(); return i_data->instance_indices();
return ret;
} }
pxr::GfMatrix4d BlenderSceneDelegate::get_instancer_transform(pxr::SdfPath const &instancer_id) pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &instancer_id)
{ {
// TODO: add a separate object for instancer for cleaner handling code CLOG_INFO(LOG_BSD, 3, "%s", instancer_id.GetText());
// Actual instancer transform is get here InstancerData *i_data = instancer_data(instancer_id);
return pxr::GfMatrix4d(1.0); return i_data->transform();
}
size_t BlenderSceneDelegate::SampleInstancerTransform(pxr::SdfPath const &instancer_id,
size_t max_sample_count,
float *sample_times,
pxr::GfMatrix4d *sample_values)
{
size_t ret = 0;
MeshData *m_data = mesh_data(instancer_id.GetParentPath());
ret = m_data->sample_instancer_transform(max_sample_count, sample_times, sample_values);
return ret;
}
size_t BlenderSceneDelegate::SamplePrimvar(pxr::SdfPath const &id,
pxr::TfToken const &key,
size_t max_sample_count,
float *sample_times,
pxr::VtValue *sample_values)
{
// TODO: add a separate object for instancer for cleaner handling code
if (id.GetName() == "Instancer") {
MeshData *m_data = mesh_data(id.GetParentPath());
if (m_data) {
return m_data->sample_instancer_primvar(key, max_sample_count, sample_times, sample_values);
}
}
return HdSceneDelegate::SamplePrimvar(id, key, max_sample_count, sample_times, sample_values);
} }
void BlenderSceneDelegate::update_collection(bool remove, bool visibility) void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
@ -146,9 +90,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
if (visibility) { if (visibility) {
/* Check and update visibility */ /* Check and update visibility */
for (auto &obj : objects) { for (auto &obj : objects) {
if (obj.second->update_visibility(view3d)) { obj.second->update_visibility(view3d);
obj.second->mark_prim_dirty(IdData::DirtyBits::DIRTY_VISIBILITY);
};
} }
} }
@ -159,7 +101,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
DEGObjectIterSettings settings = {0}; DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph; settings.depsgraph = depsgraph;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | DEG_ITER_OBJECT_FLAG_DUPLI; DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0}; DEGObjectIterData data = {0};
data.settings = &settings; data.settings = &settings;
data.graph = settings.depsgraph; data.graph = settings.depsgraph;
@ -171,22 +113,21 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
Object *, Object *,
object) { object) {
CLOG_INFO(LOG_BSD, 2, "Add %s", ((ID *)object)->name);
if (!ObjectData::supported(object)) { if (!ObjectData::supported(object)) {
continue; continue;
} }
if (data.dupli_object_current != nullptr) {
add_update_instance(data.dupli_object_current);
continue;
}
id = ObjectData::prim_id(this, object); id = ObjectData::prim_id(this, object);
if (remove) { if (remove) {
available_objects.insert(id); available_objects.insert(id);
if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) {
available_objects.insert(InstancerData::prim_id(this, object));
}
} }
if (!object_data(id)) { if (!object_data(id)) {
add_update_object(object, true, true, true); add_update_object(object);
} }
} }
ITER_END; ITER_END;
@ -197,7 +138,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
if (available_objects.find(it->first) != available_objects.end()) { if (available_objects.find(it->first) != available_objects.end()) {
continue; continue;
} }
it->second->remove_prim(); it->second->remove();
objects.erase(it); objects.erase(it);
it = objects.begin(); it = objects.begin();
} }
@ -206,70 +147,60 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
std::set<pxr::SdfPath> available_materials; std::set<pxr::SdfPath> available_materials;
for (auto &obj : objects) { for (auto &obj : objects) {
MeshData *m_data = dynamic_cast<MeshData *>(obj.second.get()); MeshData *m_data = dynamic_cast<MeshData *>(obj.second.get());
if (m_data && !m_data->material_id.IsEmpty()) { if (!m_data) {
available_materials.insert(m_data->material_id); continue;
}
pxr::SdfPath mat_id = m_data->material_id();
if (!mat_id.IsEmpty()) {
available_materials.insert(mat_id);
} }
} }
for (auto it = materials.begin(); it != materials.end(); ++it) { for (auto it = materials.begin(); it != materials.end(); ++it) {
if (available_materials.find(it->first) != available_materials.end()) { if (available_materials.find(it->first) != available_materials.end()) {
continue; continue;
} }
it->second->remove_prim(); it->second->remove();
materials.erase(it); materials.erase(it);
it = materials.begin(); it = materials.begin();
} }
} }
} }
void BlenderSceneDelegate::add_update_object(Object *object, void BlenderSceneDelegate::add_update_object(Object *object)
bool geometry,
bool transform,
bool shading)
{ {
if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) {
add_update_instancer(object);
}
InstancerData *i_data = instancer_data(object);
if (i_data) {
i_data->update();
}
pxr::SdfPath id = ObjectData::prim_id(this, object); pxr::SdfPath id = ObjectData::prim_id(this, object);
ObjectData *obj_data = object_data(id); ObjectData *obj_data = object_data(id);
if (!obj_data) { if (obj_data) {
objects[id] = ObjectData::init(this, object); obj_data->update();
obj_data = object_data(id);
obj_data->update_visibility(view3d);
obj_data->insert_prim();
MeshData *m_data = dynamic_cast<MeshData *>(obj_data);
if (m_data) {
set_material(*m_data);
}
return; return;
} }
if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) {
if (geometry) {
objects[id] = ObjectData::init(this, object);
obj_data = object_data(id);
obj_data->update_visibility(view3d);
MeshData *m_data = dynamic_cast<MeshData *>(obj_data);
if (m_data) {
set_material(*m_data);
}
obj_data->mark_prim_dirty(IdData::DirtyBits::ALL_DIRTY);
return; return;
} }
objects[id] = ObjectData::create(this, object);
if (transform) { obj_data = object_data(id);
obj_data->mark_prim_dirty(IdData::DirtyBits::DIRTY_TRANSFORM); obj_data->update_visibility(view3d);
} }
if (shading) { void BlenderSceneDelegate::add_update_instancer(Object *object)
obj_data->mark_prim_dirty(IdData::DirtyBits::DIRTY_MATERIAL);
}
}
void BlenderSceneDelegate::add_update_instance(DupliObject *dupli)
{ {
pxr::SdfPath id = ObjectData::prim_id(this, dupli->ob); pxr::SdfPath id = InstancerData::prim_id(this, object);
if (!object_data(id)) { InstancerData *i_data = instancer_data(id, true);
add_update_object(dupli->ob, true, true, true); if (i_data) {
i_data->update();
return;
} }
objects[id] = InstancerData::create(this, object);
MeshData *m_data = mesh_data(id); i_data = instancer_data(id, true);
m_data->add_instance(dupli); i_data->update_visibility(view3d);
} }
ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id)
@ -283,12 +214,12 @@ ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id)
MeshData *BlenderSceneDelegate::mesh_data(pxr::SdfPath const &id) MeshData *BlenderSceneDelegate::mesh_data(pxr::SdfPath const &id)
{ {
return static_cast<MeshData *>(object_data(id)); return dynamic_cast<MeshData *>(object_data(id));
} }
LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id) LightData *BlenderSceneDelegate::light_data(pxr::SdfPath const &id)
{ {
return static_cast<LightData *>(object_data(id)); return dynamic_cast<LightData *>(object_data(id));
} }
MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id) MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id)
@ -300,19 +231,39 @@ MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id)
return it->second.get(); return it->second.get();
} }
InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id, bool base_prim)
{
if (base_prim) {
return dynamic_cast<InstancerData *>(object_data(id));
}
return dynamic_cast<InstancerData *>(object_data(id.GetParentPath()));
}
InstancerData *BlenderSceneDelegate::instancer_data(Object *object)
{
InstancerData *i_data;
for (auto &it : objects) {
i_data = dynamic_cast<InstancerData *>(it.second.get());
if (i_data && i_data->is_base(object)) {
return i_data;
}
}
return nullptr;
}
void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
{ {
bool is_populated = depsgraph != nullptr; bool is_populated = depsgraph != nullptr;
depsgraph = deps; depsgraph = deps;
context = cont; context = cont;
scene = DEG_get_input_scene(depsgraph);
view3d = CTX_wm_view3d(context); view3d = CTX_wm_view3d(context);
if (!is_populated) { if (!is_populated) {
/* Export initial objects */ /* Export initial objects */
update_collection(false, false); update_collection(false, false);
update_world(); update_world();
GetRenderIndex().InsertInstancer(this, GetDelegateID().AppendElementString("Instancer"));
return; return;
} }
@ -320,19 +271,41 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
bool do_update_collection = false; bool do_update_collection = false;
bool do_update_visibility = false; bool do_update_visibility = false;
bool do_update_world = false; bool do_update_world = false;
bool transform, geometry, shading;
unsigned int scene_recalc = ((ID *)scene)->recalc;
if (scene_recalc) {
/* Checking scene updates */
CLOG_INFO(LOG_BSD,
2,
"Update: %s [%s]",
((ID *)scene)->name,
std::bitset<32>(scene_recalc).to_string().c_str());
if (scene_recalc & ID_RECALC_BASE_FLAGS) {
do_update_visibility = true;
}
if (scene_recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) {
do_update_collection = true;
}
if (scene_recalc & ID_RECALC_AUDIO_VOLUME) {
if ((scene->world && !world_data) || (!scene->world && world_data)) {
do_update_world = true;
}
}
if (do_update_collection || do_update_visibility) {
update_collection(do_update_collection, do_update_visibility);
}
}
/* Checking other objects updates */
DEGIDIterData data = {0}; DEGIDIterData data = {0};
data.graph = depsgraph; data.graph = depsgraph;
data.only_updated = true; data.only_updated = true;
ITER_BEGIN ( ITER_BEGIN (
DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) { DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) {
transform = (id->recalc & ID_RECALC_TRANSFORM) != 0; CLOG_INFO(
shading = (id->recalc & (ID_RECALC_SHADING | ID_RECALC_ANIMATION)) != 0; LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str());
geometry = (id->recalc & ID_RECALC_GEOMETRY) != 0;
CLOG_INFO(LOG_BSD, 1, "Update: %s [%d%d%d]", id->name, transform, geometry, shading);
switch (GS(id->name)) { switch (GS(id->name)) {
case ID_OB: { case ID_OB: {
@ -340,39 +313,21 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
if (!ObjectData::supported(object)) { if (!ObjectData::supported(object)) {
break; break;
} }
geometry |= (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY) != 0; add_update_object(object);
add_update_object(object, geometry, transform, shading);
} break; } break;
case ID_MA: case ID_MA: {
if (shading) { MaterialData *mat_data = material_data(MaterialData::prim_id(this, (Material *)id));
Material *material = (Material *)id; if (mat_data) {
update_material(material); mat_data->update();
} }
break; } break;
case ID_GR: case ID_WO: {
if (transform && geometry) { if (id->recalc & ID_RECALC_SHADING) {
do_update_collection = true;
}
break;
case ID_SCE:
if (!geometry && !transform && !shading) {
Scene *scene = (Scene *)id;
do_update_visibility = true;
if ((scene->world && !world_data) || (!scene->world && world_data)) {
do_update_world = true; do_update_world = true;
} }
} } break;
break;
case ID_WO:
if (shading) {
do_update_world = true;
}
break;
default: default:
break; break;
@ -380,9 +335,6 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
} }
ITER_END; ITER_END;
if (do_update_collection || do_update_visibility) {
update_collection(do_update_collection, do_update_visibility);
}
if (do_update_world) { if (do_update_world) {
update_world(); update_world();
} }
@ -398,11 +350,8 @@ pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id
pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key) pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key)
{ {
CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText()); CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText());
ObjectData *obj_data = object_data(id); ObjectData *obj_data = object_data(id);
// TODO: add a separate object for instancer for cleaner handling code
if (!obj_data && id.GetName() == "Instancer") {
obj_data = object_data(id.GetParentPath());
}
if (obj_data) { if (obj_data) {
return obj_data->get_data(key); return obj_data->get_data(key);
} }
@ -411,6 +360,12 @@ pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken cons
if (mat_data) { if (mat_data) {
return mat_data->get_data(key); return mat_data->get_data(key);
} }
InstancerData *i_data = instancer_data(id);
if (i_data) {
return i_data->get_data(key);
}
return pxr::VtValue(); return pxr::VtValue();
} }
@ -418,22 +373,23 @@ pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(
pxr::SdfPath const &id, pxr::HdInterpolation interpolation) pxr::SdfPath const &id, pxr::HdInterpolation interpolation)
{ {
CLOG_INFO(LOG_BSD, 3, "%s, %d", id.GetText(), interpolation); CLOG_INFO(LOG_BSD, 3, "%s, %d", id.GetText(), interpolation);
if (mesh_data(id)) {
return mesh_data(id)->primvar_descriptors(interpolation); MeshData *m_data = mesh_data(id);
if (m_data) {
return m_data->primvar_descriptors(interpolation);
} }
// TODO: add a separate object for instancer for cleaner handling code
else if (id.GetName() == "Instancer") { InstancerData *i_data = instancer_data(id);
if (MeshData *data = mesh_data(id.GetParentPath())) { if (i_data) {
return data->instancer_primvar_descriptors(interpolation); return i_data->instancer_primvar_descriptors(interpolation);
} }
}
pxr::HdPrimvarDescriptorVector primvars; return pxr::HdPrimvarDescriptorVector();
return primvars;
} }
pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id) pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id)
{ {
return mesh_data(rprim_id)->material_id; return mesh_data(rprim_id)->material_id();
} }
pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id) pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id)
@ -452,15 +408,16 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id)
if (obj_data) { if (obj_data) {
return obj_data->transform(); return obj_data->transform();
} }
// TODO: add a separate object for instancer for cleaner handling code
else if (id.GetName() == "Instancer") {
if (MeshData *mesh = mesh_data(id.GetParentPath())) {
return mesh->transform().GetInverse();
}
}
if (id == WorldData::prim_id(this)) { if (id == WorldData::prim_id(this)) {
return world_data->transform(); return world_data->transform();
} }
InstancerData *i_data = instancer_data(id);
if (i_data) {
return i_data->transform();
}
return pxr::GfMatrix4d(); return pxr::GfMatrix4d();
} }

View File

@ -10,6 +10,7 @@
#include "CLG_log.h" #include "CLG_log.h"
#include "instancer.h"
#include "light.h" #include "light.h"
#include "mesh.h" #include "mesh.h"
#include "object.h" #include "object.h"
@ -20,6 +21,9 @@ namespace blender::render::hydra {
extern struct CLG_LogRef *LOG_BSD; /* BSD - Blender Scene Delegate */ extern struct CLG_LogRef *LOG_BSD; /* BSD - Blender Scene Delegate */
class BlenderSceneDelegate : public pxr::HdSceneDelegate { class BlenderSceneDelegate : public pxr::HdSceneDelegate {
friend MeshData;
friend InstancerData;
public: public:
enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW };
@ -44,16 +48,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
pxr::SdfPathVector GetInstancerPrototypes(pxr::SdfPath const &instancer_id) override; pxr::SdfPathVector GetInstancerPrototypes(pxr::SdfPath const &instancer_id) override;
pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancer_id, pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancer_id,
pxr::SdfPath const &prototype_id) override; pxr::SdfPath const &prototype_id) override;
pxr::GfMatrix4d get_instancer_transform(pxr::SdfPath const &instancer_id); pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancer_id) override;
size_t SampleInstancerTransform(pxr::SdfPath const &instancer_id,
size_t max_sample_count,
float *sample_times,
pxr::GfMatrix4d *sample_values) override;
size_t SamplePrimvar(pxr::SdfPath const &id,
pxr::TfToken const &key,
size_t max_sample_count,
float *sample_times,
pxr::VtValue *sample_values) override;
EngineType engine_type; EngineType engine_type;
@ -62,17 +57,18 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
MeshData *mesh_data(pxr::SdfPath const &id); MeshData *mesh_data(pxr::SdfPath const &id);
LightData *light_data(pxr::SdfPath const &id); LightData *light_data(pxr::SdfPath const &id);
MaterialData *material_data(pxr::SdfPath const &id); MaterialData *material_data(pxr::SdfPath const &id);
InstancerData *instancer_data(pxr::SdfPath const &id, bool base_prim = false);
InstancerData *instancer_data(Object *object);
void add_update_object(Object *object, bool geometry, bool transform, bool shading); void add_update_object(Object *object);
void add_update_instance(DupliObject *dupli); void add_update_instancer(Object *object);
void set_material(MeshData &mesh_data);
void update_material(Material *material);
void update_world(); void update_world();
void update_collection(bool remove, bool visibility); void update_collection(bool remove, bool visibility);
private: private:
Depsgraph *depsgraph; Depsgraph *depsgraph;
bContext *context; bContext *context;
Scene *scene;
View3D *view3d; View3D *view3d;
ObjectDataMap objects; ObjectDataMap objects;

View File

@ -13,14 +13,7 @@ IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id)
{ {
} }
std::string IdData::name() pxr::VtValue IdData::get_data(pxr::TfToken const &key) const
{
char str[MAX_ID_FULL_NAME];
BKE_id_full_name_get(str, id, 0);
return str;
}
pxr::VtValue IdData::get_data(pxr::TfToken const &key)
{ {
return pxr::VtValue(); return pxr::VtValue();
} }

View File

@ -18,22 +18,21 @@ class IdData {
IdData(BlenderSceneDelegate *scene_delegate, ID *id); IdData(BlenderSceneDelegate *scene_delegate, ID *id);
virtual ~IdData() = default; virtual ~IdData() = default;
std::string name(); virtual void init() = 0;
virtual pxr::VtValue get_data(pxr::TfToken const &key); virtual void insert() = 0;
template<class T> const T get_data(pxr::TfToken const &key); virtual void remove() = 0;
virtual void update() = 0;
enum class DirtyBits { DIRTY_TRANSFORM = 1, DIRTY_VISIBILITY, DIRTY_MATERIAL, ALL_DIRTY }; virtual pxr::VtValue get_data(pxr::TfToken const &key) const;
template<class T> const T get_data(pxr::TfToken const &key) const;
virtual void insert_prim() = 0;
virtual void remove_prim() = 0;
virtual void mark_prim_dirty(DirtyBits dirty_bits) = 0;
protected: protected:
BlenderSceneDelegate *scene_delegate; BlenderSceneDelegate *scene_delegate;
ID *id; ID *id;
pxr::SdfPath p_id;
}; };
template<class T> const T IdData::get_data(pxr::TfToken const &key) template<class T> const T IdData::get_data(pxr::TfToken const &key) const
{ {
return get_data(key).Get<T>(); return get_data(key).Get<T>();
} }

View File

@ -0,0 +1,194 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <pxr/base/gf/vec2f.h>
#include "../utils.h"
#include "blender_scene_delegate.h"
#include "instancer.h"
namespace blender::render::hydra {
bool InstancerData::supported(Object *object)
{
switch (object->type) {
case OB_MESH:
case OB_SURF:
case OB_FONT:
case OB_CURVES_LEGACY:
case OB_MBALL:
return true;
default:
break;
}
return false;
}
std::unique_ptr<InstancerData> InstancerData::create(BlenderSceneDelegate *scene_delegate,
Object *object)
{
auto data = std::make_unique<InstancerData>(scene_delegate, object);
data->init();
data->insert();
return data;
}
pxr::SdfPath InstancerData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object)
{
/* Making id of instancer in form like I_<pointer in 16 hex digits format>. Example:
* I_000002073e369608 */
char str[32];
snprintf(str, 32, "I_%016llx", (uint64_t)object);
return scene_delegate->GetDelegateID().AppendElementString(str);
}
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object)
: MeshData(scene_delegate, object), parent_obj(object)
{
id = nullptr;
p_id = prim_id(scene_delegate, object);
instancer_id = p_id.AppendElementString("Instancer");
CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj)->name, instancer_id.GetText());
}
void InstancerData::init()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name);
set_instances();
MeshData::init();
}
pxr::GfMatrix4d InstancerData::transform()
{
return pxr::GfMatrix4d(1.0);
}
bool InstancerData::update_visibility(View3D *view3d)
{
if (!view3d) {
return false;
}
bool prev_visible = visible;
visible = BKE_object_is_visible_in_viewport(view3d, parent_obj);
bool ret = visible != prev_visible;
if (ret) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id, pxr::HdChangeTracker::DirtyVisibility);
}
return ret;
}
pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const
{
CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText());
if (key == pxr::HdInstancerTokens->instanceTransform) {
return pxr::VtValue(transforms);
}
return MeshData::get_data(key);
}
pxr::HdPrimvarDescriptorVector InstancerData::instancer_primvar_descriptors(
pxr::HdInterpolation interpolation)
{
pxr::HdPrimvarDescriptorVector primvars;
if (interpolation == pxr::HdInterpolationInstance) {
primvars.emplace_back(
pxr::HdInstancerTokens->instanceTransform, interpolation, pxr::HdPrimvarRoleTokens->none);
}
return primvars;
}
pxr::VtIntArray InstancerData::instance_indices()
{
pxr::VtIntArray ret(transforms.size());
for (size_t i = 0; i < ret.size(); ++i) {
ret[i] = i;
}
return ret;
}
bool InstancerData::is_base(Object *object) const
{
return (ID *)object == id;
}
bool InstancerData::set_instances()
{
ID *prev_id = id;
id = nullptr;
transforms.clear();
ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj);
LISTBASE_FOREACH (DupliObject *, dupli, lb) {
if (!id) {
/* TODO: We create instances only for object in first dupli.
Instances should be created for all objects */
id = (ID *)dupli->ob;
}
if (id != (ID *)dupli->ob) {
continue;
}
transforms.push_back(gf_matrix_from_transform(dupli->mat));
CLOG_INFO(
LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id);
}
free_object_duplilist(lb);
return id != prev_id;
}
void InstancerData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name);
MeshData::insert();
if (face_vertex_counts.empty()) {
return;
}
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id);
}
void InstancerData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name);
if (!scene_delegate->GetRenderIndex().HasInstancer(instancer_id)) {
return;
}
scene_delegate->GetRenderIndex().RemoveInstancer(instancer_id);
MeshData::remove();
}
void InstancerData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name);
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
unsigned int recalc = ((ID *)parent_obj)->recalc;
Object *object = (Object *)id;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id, pxr::HdChangeTracker::AllDirty);
return;
}
if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
bits |= pxr::HdChangeTracker::AllDirty;
}
else if (recalc & ID_RECALC_TRANSFORM || id->recalc & ID_RECALC_TRANSFORM) {
set_instances();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits);
}
}
} // namespace blender::render::hydra

View File

@ -0,0 +1,42 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include "BKE_duplilist.h"
#include "mesh.h"
namespace blender::render::hydra {
class InstancerData : public MeshData {
public:
static bool supported(Object *object);
static std::unique_ptr<InstancerData> create(BlenderSceneDelegate *scene_delegate,
Object *object);
static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object);
InstancerData(BlenderSceneDelegate *scene_delegate, Object *object);
void init() override;
void insert() override;
void remove() override;
void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override;
pxr::GfMatrix4d transform() override;
bool update_visibility(View3D *view3d) override;
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray instance_indices();
bool is_base(Object *object) const;
pxr::SdfPath instancer_id;
private:
Object *parent_obj;
pxr::VtMatrix4dArray transforms;
bool set_instances();
};
} // namespace blender::render::hydra

View File

@ -18,12 +18,21 @@ namespace blender::render::hydra {
LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object) LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object)
: ObjectData(scene_delegate, object) : ObjectData(scene_delegate, object)
{ {
Light *light = (Light *)((Object *)id)->data; CLOG_INFO(LOG_BSD, 2, "%s id=%s", id->name, p_id.GetText());
}
data[pxr::HdLightTokens->intensity] = scene_delegate->engine_type == void LightData::init()
BlenderSceneDelegate::EngineType::PREVIEW ? {
light->energy / 1000 : CLOG_INFO(LOG_BSD, 2, "%s", id->name);
light->energy;
Light *light = (Light *)((Object *)id)->data;
data.clear();
float intensity = light->energy;
if (scene_delegate->engine_type == BlenderSceneDelegate::EngineType::PREVIEW) {
intensity *= 0.001;
}
data[pxr::HdLightTokens->intensity] = intensity;
data[pxr::HdLightTokens->color] = pxr::GfVec3f(light->r, light->g, light->b); data[pxr::HdLightTokens->color] = pxr::GfVec3f(light->r, light->g, light->b);
@ -112,7 +121,7 @@ pxr::TfToken LightData::prim_type()
return ret; return ret;
} }
pxr::VtValue LightData::get_data(pxr::TfToken const &key) pxr::VtValue LightData::get_data(pxr::TfToken const &key) const
{ {
pxr::VtValue ret; pxr::VtValue ret;
auto it = data.find(key); auto it = data.find(key);
@ -133,41 +142,43 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key)
return ret; return ret;
} }
void LightData::insert_prim() bool LightData::update_visibility(View3D *view3d)
{ {
pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); bool ret = ObjectData::update_visibility(view3d);
if (ret) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id,
pxr::HdLight::DirtyParams);
}
return ret;
}
void LightData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id); scene_delegate->GetRenderIndex().InsertSprim(prim_type(), scene_delegate, p_id);
CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str());
} }
void LightData::remove_prim() void LightData::remove()
{ {
pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id); scene_delegate->GetRenderIndex().RemoveSprim(prim_type(), p_id);
CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str());
} }
void LightData::mark_prim_dirty(DirtyBits dirty_bits) void LightData::update()
{ {
/* TODO: prim_type was changed we have to do remove..add light */ /* TODO: prim_type was changed we have to do remove..add light */
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
pxr::HdDirtyBits bits = pxr::HdLight::Clean; pxr::HdDirtyBits bits = pxr::HdLight::Clean;
switch (dirty_bits) { if (id->recalc & ID_RECALC_GEOMETRY) {
case DirtyBits::DIRTY_TRANSFORM: init();
bits = pxr::HdLight::DirtyTransform;
break;
case DirtyBits::DIRTY_VISIBILITY:
bits = pxr::HdLight::DirtyParams;
break;
case DirtyBits::ALL_DIRTY:
bits = pxr::HdLight::AllDirty; bits = pxr::HdLight::AllDirty;
break;
default:
break;
} }
pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); else if (id->recalc & ID_RECALC_TRANSFORM) {
bits = pxr::HdLight::DirtyTransform;
}
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits);
CLOG_INFO(LOG_BSD, 2, "Update: [%d] %s", dirty_bits, name().c_str());
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -15,10 +15,12 @@ class LightData : public ObjectData {
public: public:
LightData(BlenderSceneDelegate *scene_delegate, Object *object); LightData(BlenderSceneDelegate *scene_delegate, Object *object);
pxr::VtValue get_data(pxr::TfToken const &key) override; void init() override;
void insert_prim() override; void insert() override;
void remove_prim() override; void remove() override;
void mark_prim_dirty(DirtyBits dirty_bits) override; void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility(View3D *view3d) override;
private: private:
std::map<pxr::TfToken, pxr::VtValue> data; std::map<pxr::TfToken, pxr::VtValue> data;

View File

@ -20,10 +20,13 @@
namespace blender::render::hydra { namespace blender::render::hydra {
std::unique_ptr<MaterialData> MaterialData::init(BlenderSceneDelegate *scene_delegate, std::unique_ptr<MaterialData> MaterialData::create(BlenderSceneDelegate *scene_delegate,
Material *material) Material *material)
{ {
return std::make_unique<MaterialData>(scene_delegate, material); auto data = std::make_unique<MaterialData>(scene_delegate, material);
data->init();
data->insert();
return data;
} }
pxr::SdfPath MaterialData::prim_id(BlenderSceneDelegate *scene_delegate, Material *material) pxr::SdfPath MaterialData::prim_id(BlenderSceneDelegate *scene_delegate, Material *material)
@ -38,40 +41,16 @@ pxr::SdfPath MaterialData::prim_id(BlenderSceneDelegate *scene_delegate, Materia
MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material) MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material)
: IdData(scene_delegate, (ID *)material) : IdData(scene_delegate, (ID *)material)
{ {
p_id = prim_id(scene_delegate, material);
CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText());
} }
pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) void MaterialData::init()
{ {
pxr::VtValue ret; CLOG_INFO(LOG_BSD, 2, "%s", id->name);
if (key.GetString() == "MaterialXFilename") {
if (!mtlx_path.GetResolvedPath().empty()) {
ret = mtlx_path;
}
CLOG_INFO(LOG_BSD, 3, "%s", key.GetText());
}
return ret;
}
pxr::VtValue MaterialData::material_resource() material_network_map = pxr::VtValue();
{
std::string const &path = mtlx_path.GetResolvedPath();
if (!path.empty()) {
pxr::HdRenderDelegate *render_delegate = scene_delegate->GetRenderIndex().GetRenderDelegate();
pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes();
pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts();
pxr::HdMaterialNetworkMap material_network_map;
HdMtlxConvertToMaterialNetworkMap(
path, shader_source_types, render_contexts, &material_network_map);
CLOG_INFO(LOG_BSD, 3, "%s", path.c_str());
return pxr::VtValue(material_network_map);
}
return pxr::VtValue();
}
void MaterialData::export_mtlx()
{
/* Call of python function hydra.export_mtlx() */ /* Call of python function hydra.export_mtlx() */
PyGILState_STATE gstate; PyGILState_STATE gstate;
@ -97,7 +76,7 @@ void MaterialData::export_mtlx()
Py_DECREF(result); Py_DECREF(result);
} }
else { else {
CLOG_ERROR(LOG_BSD, "Export error for %s", name().c_str()); CLOG_ERROR(LOG_BSD, "Export error for %s", id->name);
PyErr_Print(); PyErr_Print();
} }
Py_DECREF(module); Py_DECREF(module);
@ -105,37 +84,59 @@ void MaterialData::export_mtlx()
PyGILState_Release(gstate); PyGILState_Release(gstate);
mtlx_path = pxr::SdfAssetPath(path, path); mtlx_path = pxr::SdfAssetPath(path, path);
CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", name().c_str(), mtlx_path.GetResolvedPath().c_str()); CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", id->name, mtlx_path.GetResolvedPath().c_str());
} }
void MaterialData::insert_prim() pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const
{ {
pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); pxr::VtValue ret;
if (key.GetString() == "MaterialXFilename") {
if (!mtlx_path.GetResolvedPath().empty()) {
ret = mtlx_path;
}
CLOG_INFO(LOG_BSD, 3, "%s", key.GetText());
}
return ret;
}
pxr::VtValue MaterialData::material_resource()
{
if (material_network_map.IsEmpty()) {
const std::string &path = mtlx_path.GetResolvedPath();
if (!path.empty()) {
pxr::HdRenderDelegate *render_delegate =
scene_delegate->GetRenderIndex().GetRenderDelegate();
pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes();
pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts();
pxr::HdMaterialNetworkMap network_map;
HdMtlxConvertToMaterialNetworkMap(path, shader_source_types, render_contexts, &network_map);
material_network_map = network_map;
}
}
return material_network_map;
}
void MaterialData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertSprim( scene_delegate->GetRenderIndex().InsertSprim(
pxr::HdPrimTypeTokens->material, scene_delegate, p_id); pxr::HdPrimTypeTokens->material, scene_delegate, p_id);
CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str());
} }
void MaterialData::remove_prim() void MaterialData::remove()
{ {
pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id); scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id);
CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str());
} }
void MaterialData::mark_prim_dirty(DirtyBits dirty_bits) void MaterialData::update()
{ {
pxr::HdDirtyBits bits = pxr::HdMaterial::Clean; CLOG_INFO(LOG_BSD, 2, "%s", id->name);
switch (dirty_bits) { init();
case DirtyBits::ALL_DIRTY: scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id,
bits = pxr::HdMaterial::AllDirty; pxr::HdMaterial::AllDirty);
break;
default:
break;
}
pxr::SdfPath p_id = prim_id(scene_delegate, (Material *)id);
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits);
CLOG_INFO(LOG_BSD, 2, "Update: %s, mtlx=%s", name().c_str(), mtlx_path.GetResolvedPath().c_str());
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -13,24 +13,30 @@
namespace blender::render::hydra { namespace blender::render::hydra {
class MeshData;
class MaterialData : IdData { class MaterialData : IdData {
friend MeshData;
public: public:
static std::unique_ptr<MaterialData> init(BlenderSceneDelegate *scene_delegate, static std::unique_ptr<MaterialData> create(BlenderSceneDelegate *scene_delegate,
Material *material); Material *material);
static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Material *material); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Material *material);
MaterialData(BlenderSceneDelegate *scene_delegate, Material *material); MaterialData(BlenderSceneDelegate *scene_delegate, Material *material);
pxr::VtValue get_data(pxr::TfToken const &key) override; void init() override;
void insert_prim() override; void insert() override;
void remove_prim() override; void remove() override;
void mark_prim_dirty(DirtyBits dirty_bits) override; void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override;
pxr::VtValue material_resource(); pxr::VtValue material_resource();
void export_mtlx(); void export_mtlx();
private: private:
pxr::SdfAssetPath mtlx_path; pxr::SdfAssetPath mtlx_path;
pxr::VtValue material_network_map;
}; };
using MaterialDataMap = using MaterialDataMap =

View File

@ -16,22 +16,12 @@
namespace blender::render::hydra { namespace blender::render::hydra {
MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object)
: ObjectData(scene_delegate, object) : ObjectData(scene_delegate, object), mat_data(nullptr)
{ {
if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText());
BLI_listbase_is_empty(&object->modifiers)) {
set_mesh((Mesh *)object->data);
}
else {
Mesh *mesh = BKE_object_to_mesh(nullptr, object, false);
if (mesh) {
set_mesh(mesh);
}
BKE_object_to_mesh_clear(object);
}
} }
pxr::VtValue MeshData::get_data(pxr::TfToken const &key) pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const
{ {
pxr::VtValue ret; pxr::VtValue ret;
if (key == pxr::HdTokens->points) { if (key == pxr::HdTokens->points) {
@ -49,13 +39,14 @@ pxr::VtValue MeshData::get_data(pxr::TfToken const &key)
return ret; return ret;
} }
Material *MeshData::material() bool MeshData::update_visibility(View3D *view3d)
{ {
Object *object = (Object *)id; bool ret = ObjectData::update_visibility(view3d);
if (BKE_object_material_count_eval(object) == 0) { if (ret) {
return nullptr; scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id, pxr::HdChangeTracker::DirtyVisibility);
} }
return BKE_object_material_get_eval(object, object->actcol); return ret;
} }
pxr::HdMeshTopology MeshData::mesh_topology() pxr::HdMeshTopology MeshData::mesh_topology()
@ -88,68 +79,22 @@ pxr::HdPrimvarDescriptorVector MeshData::primvar_descriptors(pxr::HdInterpolatio
return primvars; return primvars;
} }
pxr::HdPrimvarDescriptorVector MeshData::instancer_primvar_descriptors( pxr::SdfPath MeshData::material_id()
pxr::HdInterpolation interpolation)
{ {
pxr::HdPrimvarDescriptorVector primvars; if (!mat_data) {
if (interpolation == pxr::HdInterpolationInstance) { return pxr::SdfPath();
primvars.emplace_back(
pxr::HdInstancerTokens->instanceTransform, interpolation, pxr::HdPrimvarRoleTokens->none);
} }
return primvars; return mat_data->p_id;
}
pxr::VtIntArray MeshData::instance_indices()
{
pxr::VtIntArray ret(instances.size());
for (size_t i = 0; i < ret.size(); ++i) {
ret[i] = i;
}
return ret;
}
size_t MeshData::sample_instancer_transform(size_t max_sample_count,
float *sample_times,
pxr::GfMatrix4d *sample_values)
{
*sample_times = 0.0f;
*sample_values = pxr::GfMatrix4d(1.0);
return 1;
}
size_t MeshData::sample_instancer_primvar(pxr::TfToken const &key,
size_t max_sample_count,
float *sample_times,
pxr::VtValue *sample_values)
{
if (key == pxr::HdInstancerTokens->instanceTransform) {
if (max_sample_count > 0) {
sample_times[0] = 0.0f;
sample_values[0] = instances;
return 1;
}
}
return 0;
}
void MeshData::add_instance(DupliObject *dupli)
{
if (instancer_id.IsEmpty()) {
instancer_id = prim_id(scene_delegate, (Object *)id).AppendElementString("Instancer");
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id);
CLOG_INFO(LOG_BSD, 2, "Instancer: %s, id=%s", name().c_str(), instancer_id.GetText());
}
if (instances.empty()) {
// USD hides the prototype mesh when instancing in contrary to the Blender, so we must add it
// back implicitly
instances.push_back(pxr::GfMatrix4d(1.0));
}
instances.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat));
CLOG_INFO(LOG_BSD, 2, "%s - %d", instancer_id.GetText(), dupli->random_id);
} }
void MeshData::set_mesh(Mesh *mesh) void MeshData::set_mesh(Mesh *mesh)
{ {
face_vertex_counts.clear();
face_vertex_indices.clear();
vertices.clear();
normals.clear();
uvs.clear();
BKE_mesh_calc_normals_split(mesh); BKE_mesh_calc_normals_split(mesh);
int tris_len = BKE_mesh_runtime_looptri_len(mesh); int tris_len = BKE_mesh_runtime_looptri_len(mesh);
if (tris_len == 0) { if (tris_len == 0) {
@ -200,62 +145,100 @@ void MeshData::set_mesh(Mesh *mesh)
} }
} }
void MeshData::insert_prim() void MeshData::set_material()
{
Object *object = (Object *)id;
Material *mat = nullptr;
if (BKE_object_material_count_eval(object) > 0) {
mat = BKE_object_material_get_eval(object, object->actcol);
}
if (!mat) {
mat_data = nullptr;
return;
}
pxr::SdfPath id = MaterialData::prim_id(scene_delegate, mat);
mat_data = scene_delegate->material_data(id);
if (!mat_data) {
scene_delegate->materials[id] = MaterialData::create(scene_delegate, mat);
mat_data = scene_delegate->material_data(id);
}
}
void MeshData::init()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
Object *object = (Object *)id;
if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT &&
BLI_listbase_is_empty(&object->modifiers)) {
set_mesh((Mesh *)object->data);
}
else {
Mesh *mesh = BKE_object_to_mesh(nullptr, object, false);
if (mesh) {
set_mesh(mesh);
}
BKE_object_to_mesh_clear(object);
}
set_material();
}
void MeshData::insert()
{ {
if (face_vertex_counts.empty()) { if (face_vertex_counts.empty()) {
return; return;
} }
pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate, p_id); scene_delegate->GetRenderIndex().InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate, p_id);
CLOG_INFO(LOG_BSD, 2, "Add: %s id=%s", name().c_str(), p_id.GetString().c_str());
} }
void MeshData::remove_prim() void MeshData::remove()
{ {
pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id);
if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) {
return; return;
} }
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveRprim(p_id); scene_delegate->GetRenderIndex().RemoveRprim(p_id);
CLOG_INFO(LOG_BSD, 2, "Remove: %s", name().c_str());
} }
void MeshData::mark_prim_dirty(DirtyBits dirty_bits) void MeshData::update()
{ {
pxr::SdfPath p_id = prim_id(scene_delegate, (Object *)id); pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
Object *object = (Object *)id;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
bits = pxr::HdChangeTracker::AllDirty;
}
else {
if (id->recalc & ID_RECALC_SHADING) {
set_material();
bits |= pxr::HdChangeTracker::DirtyMaterialId;
}
if (id->recalc & ID_RECALC_TRANSFORM) {
bits |= pxr::HdChangeTracker::DirtyTransform;
}
}
if (bits == pxr::HdChangeTracker::Clean) {
return;
}
if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) {
/* Trying to insert prim */ insert();
insert_prim();
return; return;
} }
if (face_vertex_counts.empty()) { if (face_vertex_counts.empty()) {
/* Remove prim without faces */ /* Remove prim without faces */
remove_prim(); remove();
return; return;
} }
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
switch (dirty_bits) {
case DirtyBits::DIRTY_TRANSFORM:
bits = pxr::HdChangeTracker::DirtyTransform;
break;
case DirtyBits::DIRTY_VISIBILITY:
bits = pxr::HdChangeTracker::DirtyVisibility;
break;
case DirtyBits::DIRTY_MATERIAL:
bits = pxr::HdChangeTracker::DirtyMaterialId;
break;
case DirtyBits::ALL_DIRTY:
bits = pxr::HdChangeTracker::AllDirty;
break;
default:
break;
}
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits); scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits);
CLOG_INFO(LOG_BSD, 2, "Update: [%d] %s", dirty_bits, name().c_str());
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -8,6 +8,7 @@
#include "BKE_duplilist.h" #include "BKE_duplilist.h"
#include "material.h"
#include "object.h" #include "object.h"
namespace blender::render::hydra { namespace blender::render::hydra {
@ -16,32 +17,20 @@ class MeshData : public ObjectData {
public: public:
MeshData(BlenderSceneDelegate *scene_delegate, Object *object); MeshData(BlenderSceneDelegate *scene_delegate, Object *object);
pxr::VtValue get_data(pxr::TfToken const &key) override; void init() override;
void insert() override;
void remove() override;
void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility(View3D *view3d) override;
void insert_prim() override;
void remove_prim() override;
void mark_prim_dirty(DirtyBits dirty_bits) override;
Material *material();
pxr::HdMeshTopology mesh_topology(); pxr::HdMeshTopology mesh_topology();
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::SdfPath material_id();
pxr::VtIntArray instance_indices();
size_t sample_instancer_transform(size_t max_sample_count,
float *sample_times,
pxr::GfMatrix4d *sample_values);
size_t sample_instancer_primvar(pxr::TfToken const &key,
size_t max_sample_count,
float *sample_times,
pxr::VtValue *sample_values);
void add_instance(DupliObject *dupli); protected:
pxr::SdfPath material_id;
pxr::SdfPath instancer_id;
private:
void set_mesh(Mesh *mesh); void set_mesh(Mesh *mesh);
void set_material();
pxr::VtIntArray face_vertex_counts; pxr::VtIntArray face_vertex_counts;
pxr::VtIntArray face_vertex_indices; pxr::VtIntArray face_vertex_indices;
@ -50,6 +39,8 @@ class MeshData : public ObjectData {
pxr::VtVec2fArray uvs; pxr::VtVec2fArray uvs;
pxr::VtMatrix4dArray instances; pxr::VtMatrix4dArray instances;
MaterialData *mat_data;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -28,24 +28,32 @@ bool ObjectData::supported(Object *object)
return false; return false;
} }
std::unique_ptr<ObjectData> ObjectData::init(BlenderSceneDelegate *scene_delegate, Object *object) std::unique_ptr<ObjectData> ObjectData::create(BlenderSceneDelegate *scene_delegate,
Object *object)
{ {
std::unique_ptr<ObjectData> data;
switch (object->type) { switch (object->type) {
case OB_MESH: case OB_MESH:
case OB_SURF: case OB_SURF:
case OB_FONT: case OB_FONT:
case OB_CURVES:
case OB_CURVES_LEGACY: case OB_CURVES_LEGACY:
case OB_MBALL: case OB_MBALL:
return std::make_unique<MeshData>(scene_delegate, object); data = std::make_unique<MeshData>(scene_delegate, object);
break;
case OB_LAMP: case OB_LAMP:
return std::make_unique<LightData>(scene_delegate, object); data = std::make_unique<LightData>(scene_delegate, object);
break;
default: default:
break; break;
} }
return nullptr; if (data) {
data->init();
data->insert();
}
return data;
} }
pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object) pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *object)
@ -60,11 +68,7 @@ pxr::SdfPath ObjectData::prim_id(BlenderSceneDelegate *scene_delegate, Object *o
ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object) ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object)
: IdData(scene_delegate, (ID *)object), visible(true) : IdData(scene_delegate, (ID *)object), visible(true)
{ {
} p_id = prim_id(scene_delegate, object);
int ObjectData::type()
{
return ((Object *)id)->type;
} }
pxr::GfMatrix4d ObjectData::transform() pxr::GfMatrix4d ObjectData::transform()

View File

@ -17,14 +17,13 @@ namespace blender::render::hydra {
class ObjectData : public IdData { class ObjectData : public IdData {
public: public:
static bool supported(Object *object); static bool supported(Object *object);
static std::unique_ptr<ObjectData> init(BlenderSceneDelegate *scene_delegate, Object *object); static std::unique_ptr<ObjectData> create(BlenderSceneDelegate *scene_delegate, Object *object);
static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate, Object *object);
ObjectData(BlenderSceneDelegate *scene_delegate, Object *object); ObjectData(BlenderSceneDelegate *scene_delegate, Object *object);
int type(); virtual pxr::GfMatrix4d transform();
pxr::GfMatrix4d transform(); virtual bool update_visibility(View3D *view3d);
bool update_visibility(View3D *view3d);
bool visible; bool visible;
}; };

View File

@ -28,11 +28,14 @@
namespace blender::render::hydra { namespace blender::render::hydra {
std::unique_ptr<WorldData> WorldData::init(BlenderSceneDelegate *scene_delegate, std::unique_ptr<WorldData> WorldData::create(BlenderSceneDelegate *scene_delegate,
World *world, World *world,
bContext *context) bContext *context)
{ {
return std::make_unique<WorldData>(scene_delegate, world, context); auto data = std::make_unique<WorldData>(scene_delegate, world, context);
data->init();
data->insert();
return data;
} }
pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate) pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate)
@ -41,17 +44,39 @@ pxr::SdfPath WorldData::prim_id(BlenderSceneDelegate *scene_delegate)
} }
WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context) WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context)
: IdData(scene_delegate, (ID *)world) : IdData(scene_delegate, (ID *)world), context(context)
{ {
p_id = prim_id(scene_delegate);
CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText());
}
void WorldData::init()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
World *world = (World *)id;
data.clear();
data[pxr::UsdLuxTokens->orientToStageUpAxis] = true; data[pxr::UsdLuxTokens->orientToStageUpAxis] = true;
if (world->use_nodes) { if (world->use_nodes) {
/* TODO: Create nodes parsing system */ /* TODO: Create nodes parsing system */
bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL); bNode *output_node = ntreeShaderOutputNode(world->nodetree, SHD_OUTPUT_ALL);
bNodeSocket input_socket = output_node->input_by_identifier("Surface"); blender::Span<bNodeSocket *> input_sockets = output_node->input_sockets();
bNodeLink const *link = input_socket.directly_linked_links()[0]; bNodeSocket *input_socket = nullptr;
if (input_socket.directly_linked_links().is_empty()) {
for (auto socket : input_sockets) {
if (STREQ(socket->name, "Surface")) {
input_socket = socket;
break;
}
}
if (!input_socket) {
return;
}
bNodeLink const *link = input_socket->directly_linked_links()[0];
if (input_socket->directly_linked_links().is_empty()) {
return; return;
} }
@ -113,7 +138,7 @@ pxr::GfMatrix4d WorldData::transform()
return transform; return transform;
} }
pxr::VtValue WorldData::get_data(pxr::TfToken const &key) pxr::VtValue WorldData::get_data(pxr::TfToken const &key) const
{ {
pxr::VtValue ret; pxr::VtValue ret;
auto it = data.find(key); auto it = data.find(key);
@ -123,34 +148,30 @@ pxr::VtValue WorldData::get_data(pxr::TfToken const &key)
return ret; return ret;
} }
void WorldData::insert_prim() void WorldData::insert()
{ {
pxr::SdfPath p_id = prim_id(scene_delegate); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertSprim( scene_delegate->GetRenderIndex().InsertSprim(
pxr::HdPrimTypeTokens->domeLight, scene_delegate, p_id); pxr::HdPrimTypeTokens->domeLight, scene_delegate, p_id);
CLOG_INFO(LOG_BSD, 2, "Add: id=%s", p_id.GetText());
} }
void WorldData::remove_prim() void WorldData::remove()
{ {
pxr::SdfPath p_id = prim_id(scene_delegate); CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id); scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id);
CLOG_INFO(LOG_BSD, 2, "Remove");
} }
void WorldData::mark_prim_dirty(DirtyBits dirty_bits) void WorldData::update()
{ {
pxr::HdDirtyBits bits = pxr::HdLight::Clean; CLOG_INFO(LOG_BSD, 2, "%s", id->name);
switch (dirty_bits) { init();
case DirtyBits::ALL_DIRTY: scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdLight::AllDirty);
bits = pxr::HdLight::AllDirty;
break;
default:
break;
} }
pxr::SdfPath p_id = prim_id(scene_delegate);
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits); void WorldData::update(World *world)
CLOG_INFO(LOG_BSD, 2, "Update"); {
id = (ID *)world;
update();
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -20,22 +20,26 @@ namespace blender::render::hydra {
class WorldData : public IdData { class WorldData : public IdData {
public: public:
static std::unique_ptr<WorldData> init(BlenderSceneDelegate *scene_delegate, static std::unique_ptr<WorldData> create(BlenderSceneDelegate *scene_delegate,
World *world, World *world,
bContext *context); bContext *context);
static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate); static pxr::SdfPath prim_id(BlenderSceneDelegate *scene_delegate);
WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context); WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context);
void init() override;
void insert() override;
void remove() override;
void update() override;
void update(World *world);
pxr::GfMatrix4d transform(); pxr::GfMatrix4d transform();
pxr::VtValue get_data(pxr::TfToken const &key) override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
void insert_prim() override;
void remove_prim() override;
void mark_prim_dirty(DirtyBits dirty_bits) override;
private: private:
std::map<pxr::TfToken, pxr::VtValue> data; std::map<pxr::TfToken, pxr::VtValue> data;
bContext *context;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra