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
6 changed files with 54 additions and 128 deletions
Showing only changes of commit e591f4cb35 - Show all commits

View File

@ -56,9 +56,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 = dynamic_cast<InstancerData *>(mesh_data(prim_id));
if (m_data) { if (i_data) {
return m_data->instancer_id; return i_data->instancer_id;
} }
return pxr::SdfPath(); return pxr::SdfPath();
} }
@ -75,16 +75,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::SamplePrimvar(pxr::SdfPath const &id, size_t BlenderSceneDelegate::SamplePrimvar(pxr::SdfPath const &id,
@ -93,14 +92,11 @@ size_t BlenderSceneDelegate::SamplePrimvar(pxr::SdfPath const &id,
float *sample_times, float *sample_times,
pxr::VtValue *sample_values) pxr::VtValue *sample_values)
{ {
// TODO: add a separate object for instancer for cleaner handling code InstancerData *i_data = instancer_data(id);
if (id.GetName() == "Instancer") { if (i_data) {
MeshData *m_data = mesh_data(id.GetParentPath()); return i_data->sample_instancer_primvar(key, max_sample_count, sample_times, sample_values);
if (m_data) {
return m_data->sample_instancer_primvar(key, max_sample_count, sample_times, sample_values);
} }
} return 0;
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)
@ -138,7 +134,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
} }
if (data.dupli_object_current != nullptr) { if (data.dupli_object_current != nullptr) {
add_update_instance(data.dupli_object_current); add_update_instancer(data.dupli_object_current);
continue; continue;
} }
@ -201,15 +197,17 @@ void BlenderSceneDelegate::add_update_object(Object *object)
obj_data->update(); obj_data->update();
} }
void BlenderSceneDelegate::add_update_instance(DupliObject *dupli) void BlenderSceneDelegate::add_update_instancer(DupliObject *dupli)
{ {
pxr::SdfPath id = ObjectData::prim_id(this, dupli->ob); pxr::SdfPath id = InstancerData::prim_id(this, dupli->ob);
if (!object_data(id)) { InstancerData *i_data = (InstancerData *)mesh_data(id);
add_update_object(dupli->ob); if (!i_data) {
objects[id] = InstancerData::create(this, dupli->ob);
i_data = (InstancerData *)mesh_data(id);
// i_data->update_visibility(view3d);
} }
MeshData *m_data = mesh_data(id); i_data->add_instance(dupli);
m_data->add_instance(dupli);
} }
ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id) ObjectData *BlenderSceneDelegate::object_data(pxr::SdfPath const &id)
@ -223,12 +221,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)
@ -240,6 +238,11 @@ MaterialData *BlenderSceneDelegate::material_data(pxr::SdfPath const &id)
return it->second.get(); return it->second.get();
} }
InstancerData *BlenderSceneDelegate::instancer_data(pxr::SdfPath const &id)
{
return dynamic_cast<InstancerData *>(object_data(id.GetParentPath()));
}
void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont) void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
{ {
bool is_populated = depsgraph != nullptr; bool is_populated = depsgraph != nullptr;
@ -336,11 +339,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);
} }
@ -349,6 +349,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();
} }
@ -356,17 +362,18 @@ 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)

View File

@ -14,6 +14,7 @@
#include "mesh.h" #include "mesh.h"
#include "object.h" #include "object.h"
#include "world.h" #include "world.h"
#include "instancer.h"
namespace blender::render::hydra { namespace blender::render::hydra {
@ -46,7 +47,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 SamplePrimvar(pxr::SdfPath const &id, size_t SamplePrimvar(pxr::SdfPath const &id,
pxr::TfToken const &key, pxr::TfToken const &key,
size_t max_sample_count, size_t max_sample_count,
@ -60,9 +61,10 @@ 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);
void add_update_object(Object *object); void add_update_object(Object *object);
void add_update_instance(DupliObject *dupli); void add_update_instancer(DupliObject *dupli);
void update_world(); void update_world();
void update_collection(bool remove, bool visibility); void update_collection(bool remove, bool visibility);

View File

@ -49,10 +49,12 @@ InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *objec
{ {
p_id = prim_id(scene_delegate, object); p_id = prim_id(scene_delegate, object);
instancer_id = p_id.AppendElementString("Instancer"); instancer_id = p_id.AppendElementString("Instancer");
CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", id->name, instancer_id.GetText());
} }
void InstancerData::init() void InstancerData::init()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
MeshData::init(); MeshData::init();
/* USD hides the prototype mesh when instancing in contrary to the Blender, /* USD hides the prototype mesh when instancing in contrary to the Blender,
@ -93,20 +95,12 @@ VtIntArray InstancerData::instance_indices()
return ret; return ret;
} }
size_t InstancerData::sample_instancer_transform(size_t maxSampleCount,
float *sampleTimes,
GfMatrix4d *sampleValues)
{
*sampleTimes = 0.0f;
*sampleValues = GfMatrix4d(1.0);
return 1;
}
size_t InstancerData::sample_instancer_primvar(TfToken const &key, size_t InstancerData::sample_instancer_primvar(TfToken const &key,
size_t maxSampleCount, size_t maxSampleCount,
float *sampleTimes, float *sampleTimes,
VtValue *sampleValues) VtValue *sampleValues)
{ {
CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText());
if (key == HdInstancerTokens->instanceTransform) { if (key == HdInstancerTokens->instanceTransform) {
if (maxSampleCount > 0) { if (maxSampleCount > 0) {
sampleTimes[0] = 0.0f; sampleTimes[0] = 0.0f;
@ -119,7 +113,7 @@ size_t InstancerData::sample_instancer_primvar(TfToken const &key,
void InstancerData::add_instance(DupliObject *dupli) void InstancerData::add_instance(DupliObject *dupli)
{ {
CLOG_INFO(LOG_BSD, 2, "%s - %d", id->name, dupli->random_id); CLOG_INFO(LOG_BSD, 2, "%s [%d]", id->name, dupli->random_id);
transforms.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat)); transforms.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat));
} }

View File

@ -26,7 +26,6 @@ public:
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray instance_indices(); 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 sample_instancer_primvar(pxr::TfToken const &key,
size_t maxSampleCount, size_t maxSampleCount,
float *sampleTimes, float *sampleTimes,

View File

@ -87,66 +87,6 @@ pxr::SdfPath MeshData::material_id()
return mat_data->p_id; return mat_data->p_id;
} }
pxr::HdPrimvarDescriptorVector MeshData::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 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", id->name, 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_counts.clear();
@ -270,8 +210,6 @@ void MeshData::update()
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
Object *object = (Object *)id; Object *object = (Object *)id;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
instancer_id = pxr::SdfPath::EmptyPath();
init(); init();
bits = pxr::HdChangeTracker::AllDirty; bits = pxr::HdChangeTracker::AllDirty;
} }

View File

@ -28,20 +28,6 @@ class MeshData : public ObjectData {
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation); pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::SdfPath material_id(); pxr::SdfPath material_id();
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
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);
pxr::SdfPath instancer_id;
protected: protected:
void set_mesh(Mesh *mesh); void set_mesh(Mesh *mesh);
void set_material(); void set_material();