Support several instancers on one object #24

Merged
Bogdan Nagirniak merged 16 commits from BLEN-383 into hydra-render 2023-04-19 02:46:24 +02:00
2 changed files with 207 additions and 185 deletions
Showing only changes of commit 4b533ffc20 - Show all commits

View File

@ -24,25 +24,6 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
{
}
void BlenderSceneDelegate::update_world()
{
World *world = scene->world;
if (!world_data) {
if (world) {
world_data = WorldData::create(this, world, context);
}
}
else {
if (world) {
world_data->update(world);
}
else {
world_data->remove();
world_data = nullptr;
}
}
}
bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id)
{
if (id == WorldData::prim_id(this)) {
@ -84,93 +65,6 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &
return i_data->transform();
}
void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
{
if (visibility) {
/* Check and update visibility */
for (auto &it : objects) {
it.second->update_visibility(view3d);
}
for (auto &it : instancers) {
it.second->update_visibility(view3d);
}
}
/* Export of new visible objects which were not exported before */
std::set<pxr::SdfPath> available_objects;
pxr::SdfPath id;
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object) {
CLOG_INFO(LOG_BSD, 2, "Add %s", ((ID *)object)->name);
if (!ObjectData::supported(object)) {
continue;
}
id = ObjectData::prim_id(this, object);
if (remove) {
available_objects.insert(id);
if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) {
available_objects.insert(InstancerData::prim_id(this, object));
}
}
if (!object_data(id)) {
add_update_object(object);
}
}
ITER_END;
if (remove) {
/* remove unused objects */
for (auto it = objects.begin(); it != objects.end(); ++it) {
if (available_objects.find(it->first) != available_objects.end()) {
continue;
}
it->second->remove();
objects.erase(it);
it = objects.begin();
}
/* remove unused materials */
std::set<pxr::SdfPath> available_materials;
for (auto &it : objects) {
MeshData *m_data = dynamic_cast<MeshData *>(it.second.get());
if (!m_data) {
continue;
}
pxr::SdfPath mat_id = m_data->material_id();
if (!mat_id.IsEmpty()) {
available_materials.insert(mat_id);
}
}
for (auto &it : instancers) {
it.second->available_materials(available_materials);
}
for (auto it = materials.begin(); it != materials.end(); ++it) {
if (available_materials.find(it->first) != available_materials.end()) {
continue;
}
it->second->remove();
materials.erase(it);
it = materials.begin();
}
}
}
void BlenderSceneDelegate::add_update_object(Object *object)
{
if ((object->transflag & OB_DUPLI) && InstancerData::supported(object)) {
@ -257,82 +151,11 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
scene = DEG_get_input_scene(depsgraph);
view3d = CTX_wm_view3d(context);
if (!is_populated) {
/* Export initial objects */
update_collection(false, false);
update_world();
return;
if (is_populated) {
check_updates();
}
/* Working with updates */
bool do_update_collection = false;
bool do_update_visibility = false;
bool do_update_world = false;
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};
data.graph = depsgraph;
data.only_updated = true;
ITER_BEGIN (
DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) {
CLOG_INFO(
LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str());
switch (GS(id->name)) {
case ID_OB: {
Object *object = (Object *)id;
if (!ObjectData::supported(object)) {
break;
}
add_update_object(object);
} break;
case ID_MA: {
MaterialData *mat_data = material_data(MaterialData::prim_id(this, (Material *)id));
if (mat_data) {
mat_data->update();
}
} break;
case ID_WO: {
if (id->recalc & ID_RECALC_SHADING) {
do_update_world = true;
}
} break;
default:
break;
}
}
ITER_END;
if (do_update_world) {
else {
add_new_objects();
update_world();
}
}
@ -426,6 +249,201 @@ pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
return pxr::VtValue();
}
void BlenderSceneDelegate::update_world()
{
World *world = scene->world;
if (!world_data) {
if (world) {
world_data = WorldData::create(this, world, context);
}
}
else {
if (world) {
world_data->update(world);
}
else {
world_data->remove();
world_data = nullptr;
}
}
}
void BlenderSceneDelegate::check_updates()
{
/* Working with updates */
bool do_update_collection = false;
bool do_update_visibility = false;
bool do_update_world = false;
unsigned int scene_recalc = scene->id.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;
}
}
}
/* Checking other objects updates */
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) {
CLOG_INFO(
LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str());
switch (GS(id->name)) {
case ID_OB: {
Object *object = (Object *)id;
if (!ObjectData::supported(object)) {
break;
}
add_update_object(object);
} break;
case ID_MA: {
MaterialData *mat_data = material_data(MaterialData::prim_id(this, (Material *)id));
if (mat_data) {
mat_data->update();
}
} break;
case ID_WO: {
if (id->recalc & ID_RECALC_SHADING) {
do_update_world = true;
}
} break;
default:
break;
}
}
ITER_END;
if (do_update_world) {
update_world();
}
if (do_update_collection) {
remove_unused_objects();
}
if (do_update_visibility) {
update_visibility();
}
}
void BlenderSceneDelegate::add_new_objects()
{
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object) {
if (!ObjectData::supported(object)) {
continue;
}
add_update_object(object);
}
ITER_END;
}
void BlenderSceneDelegate::remove_unused_objects()
{
/* Export of new visible objects which were not exported before */
std::set<pxr::SdfPath> available_objects;
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0};
data.settings = &settings;
data.graph = settings.depsgraph;
data.flag = settings.flags;
ITER_BEGIN (DEG_iterator_objects_begin,
DEG_iterator_objects_next,
DEG_iterator_objects_end,
&data,
Object *,
object) {
if (!ObjectData::supported(object)) {
continue;
}
available_objects.insert(ObjectData::prim_id(this, object));
available_objects.insert(InstancerData::prim_id(this, object));
}
ITER_END;
/* remove unused objects */
for (auto it = objects.begin(); it != objects.end(); ++it) {
if (available_objects.find(it->first) != available_objects.end()) {
continue;
}
it->second->remove();
objects.erase(it);
it = objects.begin();
}
/* remove unused materials */
std::set<pxr::SdfPath> available_materials;
for (auto &it : objects) {
MeshData *m_data = dynamic_cast<MeshData *>(it.second.get());
if (!m_data) {
continue;
}
pxr::SdfPath mat_id = m_data->material_id();
if (!mat_id.IsEmpty()) {
available_materials.insert(mat_id);
}
}
for (auto &it : instancers) {
it.second->available_materials(available_materials);
}
for (auto it = materials.begin(); it != materials.end(); ++it) {
if (available_materials.find(it->first) != available_materials.end()) {
continue;
}
it->second->remove();
materials.erase(it);
it = materials.begin();
}
}
void BlenderSceneDelegate::update_visibility()
{
for (auto &it : objects) {
it.second->update_visibility(view3d);
}
for (auto &it : instancers) {
it.second->update_visibility(view3d);
}
}
void BlenderSceneDelegate::clear()
{
for (auto &it : materials) {

View File

@ -32,9 +32,6 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
BlenderSceneDelegate::EngineType engine_type);
~BlenderSceneDelegate() override = default;
void populate(Depsgraph *depsgraph, bContext *context);
void clear();
// delegate methods
pxr::HdMeshTopology GetMeshTopology(pxr::SdfPath const &id) override;
pxr::GfMatrix4d GetTransform(pxr::SdfPath const &id) override;
@ -51,6 +48,9 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
pxr::SdfPath const &prototype_id) override;
pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancer_id) override;
void populate(Depsgraph *depsgraph, bContext *context);
void clear();
EngineType engine_type;
private:
@ -63,7 +63,11 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
void add_update_object(Object *object);
void add_update_instancer(Object *object);
void update_world();
void update_collection(bool remove, bool visibility);
void check_updates();
void add_new_objects();
void remove_unused_objects();
void update_visibility();
private:
Depsgraph *depsgraph;