Refactor code after PR #57 #67

Merged
Bogdan Nagirniak merged 9 commits from hydra-object_data-refactor into hydra-render 2023-07-20 22:34:34 +02:00
17 changed files with 80 additions and 143 deletions
Showing only changes of commit d6339d5547 - Show all commits

View File

@ -45,7 +45,7 @@ pxr::HdBasisCurvesTopology BlenderSceneDelegate::GetBasisCurvesTopology(pxr::Sdf
{ {
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", id.GetText()); CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 3, "%s", id.GetText());
CurvesData *c_data = curves_data(id); CurvesData *c_data = curves_data(id);
return c_data->curves_topology(id); return c_data->topology();
}; };
pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id) pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id)
@ -72,14 +72,6 @@ pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken cons
if (m_data) { if (m_data) {
return m_data->get_data(id, key); return m_data->get_data(id, key);
} }
CurvesData *c_data = curves_data(id);
if (c_data) {
return c_data->get_data(id, key);
}
VolumeData *v_data = volume_data(id);
if (v_data) {
return v_data->get_data(id, key);
}
ObjectData *obj_data = object_data(id); ObjectData *obj_data = object_data(id);
if (obj_data) { if (obj_data) {
return obj_data->get_data(key); return obj_data->get_data(key);

View File

@ -22,7 +22,6 @@ void CurvesData::init()
{ {
ID_LOG(1, ""); ID_LOG(1, "");
Object *object = (Object *)id;
write_curves((Curves *)object->data); write_curves((Curves *)object->data);
write_transform(); write_transform();
write_materials(); write_materials();
@ -43,17 +42,18 @@ void CurvesData::remove()
void CurvesData::update() void CurvesData::update()
{ {
Object *object = (Object *)id;
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { if ((object->id.recalc & ID_RECALC_GEOMETRY) ||
(((ID *)object->data)->recalc & ID_RECALC_GEOMETRY))
{
init(); init();
bits = pxr::HdChangeTracker::AllDirty; bits = pxr::HdChangeTracker::AllDirty;
} }
if (id->recalc & ID_RECALC_SHADING) { if (object->id.recalc & ID_RECALC_SHADING) {
write_materials(); write_materials();
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
} }
if (id->recalc & ID_RECALC_TRANSFORM) { if (object->id.recalc & ID_RECALC_TRANSFORM) {
write_transform(); write_transform();
bits |= pxr::HdChangeTracker::DirtyTransform; bits |= pxr::HdChangeTracker::DirtyTransform;
} }
@ -66,7 +66,7 @@ void CurvesData::update()
ID_LOG(1, ""); ID_LOG(1, "");
} }
pxr::VtValue CurvesData::get_data(pxr::SdfPath const & /* id */, pxr::TfToken const &key) const pxr::VtValue CurvesData::get_data(pxr::TfToken const &key) const
{ {
if (key == pxr::HdTokens->points) { if (key == pxr::HdTokens->points) {
return pxr::VtValue(vertices_); return pxr::VtValue(vertices_);
@ -80,18 +80,7 @@ pxr::VtValue CurvesData::get_data(pxr::SdfPath const & /* id */, pxr::TfToken co
return pxr::VtValue(); return pxr::VtValue();
} }
bool CurvesData::update_visibility() pxr::HdBasisCurvesTopology CurvesData::topology() const
{
bool ret = ObjectData::update_visibility();
if (ret) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
prim_id, pxr::HdChangeTracker::DirtyVisibility);
ID_LOG(1, "");
}
return ret;
}
pxr::HdBasisCurvesTopology CurvesData::curves_topology(pxr::SdfPath const & /* id */) const
{ {
return pxr::HdBasisCurvesTopology(pxr::HdTokens->linear, return pxr::HdBasisCurvesTopology(pxr::HdTokens->linear,
pxr::TfToken(), pxr::TfToken(),
@ -180,7 +169,6 @@ void CurvesData::write_uv_maps(Curves *curves)
void CurvesData::write_materials() void CurvesData::write_materials()
{ {
Object *object = (Object *)id;
Material *mat = nullptr; Material *mat = nullptr;
/* TODO: Using only first material. Add support for multimaterial. */ /* TODO: Using only first material. Add support for multimaterial. */
if (BKE_object_material_count_eval(object) > 0) { if (BKE_object_material_count_eval(object) > 0) {

View File

@ -24,13 +24,12 @@ class CurvesData : public ObjectData {
void remove() override; void remove() override;
void update() override; void update() override;
pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility() override;
pxr::HdBasisCurvesTopology curves_topology(pxr::SdfPath const &id) const; pxr::HdBasisCurvesTopology topology() const;
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const;
pxr::SdfPath material_id() const; pxr::SdfPath material_id() const;
void available_materials(Set<pxr::SdfPath> &paths) const; void available_materials(Set<pxr::SdfPath> &paths) const override;
private: private:
void write_curves(Curves *curves); void write_curves(Curves *curves);

View File

@ -7,14 +7,9 @@
namespace blender::render::hydra { namespace blender::render::hydra {
IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id) IdData::IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id)
: id(id), prim_id(prim_id), scene_delegate_(scene_delegate) : prim_id(prim_id), scene_delegate_(scene_delegate)
{ {
} }
pxr::VtValue IdData::get_data(pxr::TfToken const & /*key*/) const
{
return pxr::VtValue();
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -13,14 +13,14 @@
template<> struct blender::DefaultHash<pxr::SdfPath> { template<> struct blender::DefaultHash<pxr::SdfPath> {
uint64_t operator()(const pxr::SdfPath &value) const uint64_t operator()(const pxr::SdfPath &value) const
{ {
return pxr::SdfPath::Hash()(value); return (uint64_t)value.GetHash();
} }
}; };
template<> struct blender::DefaultHash<pxr::TfToken> { template<> struct blender::DefaultHash<pxr::TfToken> {
uint64_t operator()(const pxr::TfToken &value) const uint64_t operator()(const pxr::TfToken &value) const
{ {
return pxr::TfHash()(value); return (uint64_t)value.Hash();
} }
}; };
@ -30,7 +30,7 @@ class BlenderSceneDelegate;
class IdData { class IdData {
public: public:
IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id); IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id);
virtual ~IdData() = default; virtual ~IdData() = default;
virtual void init() = 0; virtual void init() = 0;
@ -38,27 +38,15 @@ class IdData {
virtual void remove() = 0; virtual void remove() = 0;
virtual void update() = 0; virtual void update() = 0;
virtual pxr::VtValue get_data(pxr::TfToken const &key) const; virtual pxr::VtValue get_data(pxr::TfToken const &key) const = 0;
template<class T> const T get_data(pxr::TfToken const &key) const;
ID *id;
pxr::SdfPath prim_id; pxr::SdfPath prim_id;
protected: protected:
BlenderSceneDelegate *scene_delegate_; BlenderSceneDelegate *scene_delegate_;
}; };
template<class T> const T IdData::get_data(pxr::TfToken const &key) const
{
return get_data(key).Get<T>();
}
#define ID_LOG(level, msg, ...) \ #define ID_LOG(level, msg, ...) \
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \ CLOG_INFO(LOG_RENDER_HYDRA_SCENE, level, "%s: " msg, prim_id.GetText(), ##__VA_ARGS__);
level, \
"%s (%s): " msg, \
prim_id.GetText(), \
id ? id->name : "", \
##__VA_ARGS__);
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -12,7 +12,7 @@
namespace blender::render::hydra { namespace blender::render::hydra {
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id)
: IdData(scene_delegate, nullptr, prim_id) : IdData(scene_delegate, prim_id)
{ {
} }
@ -163,7 +163,7 @@ void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli)
inst->data = std::make_unique<LightData>(scene_delegate_, ob, p_id); inst->data = std::make_unique<LightData>(scene_delegate_, ob, p_id);
inst->data->init(); inst->data->init();
} }
ID_LOG(2, "Light %s %d", inst->data->id->name, (int)inst->transforms.size()); ID_LOG(2, "Light %s %d", inst->data->object->id.name, (int)inst->transforms.size());
inst->transforms.push_back(gf_matrix_from_transform(dupli->mat)); inst->transforms.push_back(gf_matrix_from_transform(dupli->mat));
} }
else { else {
@ -177,7 +177,7 @@ void InstancerData::update_instance(Object *parent_ob, DupliObject *dupli)
else { else {
inst->data->update(); inst->data->update();
} }
ID_LOG(2, "Mesh %s %d", inst->data->id->name, (int)mesh_transforms_.size()); ID_LOG(2, "Mesh %s %d", inst->data->object->id.name, (int)mesh_transforms_.size());
inst->indices.push_back(mesh_transforms_.size()); inst->indices.push_back(mesh_transforms_.size());
mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat)); mesh_transforms_.push_back(gf_matrix_from_transform(dupli->mat));
} }
@ -260,7 +260,7 @@ void InstancerData::update_light_instance(LightInstance &inst)
} }
/* Update current light instances */ /* Update current light instances */
if (inst.data->prim_type((Light *)((Object *)l_data.id)->data) != l_data.prim_type_) { if (inst.data->prim_type((Light *)l_data.object->data) != l_data.prim_type_) {
/* Recreate instances when prim_type was changed */ /* Recreate instances when prim_type was changed */
for (i = 0; i < inst.count; ++i) { for (i = 0; i < inst.count; ++i) {
p = light_prim_id(inst, i); p = light_prim_id(inst, i);
@ -271,13 +271,13 @@ void InstancerData::update_light_instance(LightInstance &inst)
for (i = 0; i < inst.count; ++i) { for (i = 0; i < inst.count; ++i) {
p = light_prim_id(inst, i); p = light_prim_id(inst, i);
render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p); render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p);
ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.id->name); ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.object->id.name);
} }
} }
else { else {
/* Update light instances*/ /* Update light instances*/
pxr::HdDirtyBits bits = pxr::HdLight::DirtyTransform; pxr::HdDirtyBits bits = pxr::HdLight::DirtyTransform;
Object *obj = (Object *)inst.data->id; Object *obj = inst.data->object;
if (obj->id.recalc & ID_RECALC_GEOMETRY || ((ID *)obj->data)->recalc & ID_RECALC_GEOMETRY) { if (obj->id.recalc & ID_RECALC_GEOMETRY || ((ID *)obj->data)->recalc & ID_RECALC_GEOMETRY) {
l_data.init(); l_data.init();
bits = pxr::HdLight::AllDirty; bits = pxr::HdLight::AllDirty;
@ -285,7 +285,7 @@ void InstancerData::update_light_instance(LightInstance &inst)
for (i = 0; i < inst.count; ++i) { for (i = 0; i < inst.count; ++i) {
p = light_prim_id(inst, i); p = light_prim_id(inst, i);
render_index.GetChangeTracker().MarkSprimDirty(p, bits); render_index.GetChangeTracker().MarkSprimDirty(p, bits);
ID_LOG(2, "Update %s (%s)", p.GetText(), l_data.id->name); ID_LOG(2, "Update %s (%s)", p.GetText(), l_data.object->id.name);
} }
} }
@ -293,7 +293,7 @@ void InstancerData::update_light_instance(LightInstance &inst)
while (inst.count < inst.transforms.size()) { while (inst.count < inst.transforms.size()) {
p = light_prim_id(inst, inst.count); p = light_prim_id(inst, inst.count);
render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p); render_index.InsertSprim(l_data.prim_type_, scene_delegate_, p);
ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.id->name); ID_LOG(2, "Insert %s (%s)", p.GetText(), l_data.object->id.name);
++inst.count; ++inst.count;
} }
} }

View File

@ -26,7 +26,7 @@ void LightData::init()
{ {
ID_LOG(1, ""); ID_LOG(1, "");
Light *light = (Light *)((Object *)id)->data; Light *light = (Light *)object->data;
data_.clear(); data_.clear();
switch (light->type) { switch (light->type) {
@ -109,10 +109,9 @@ void LightData::remove()
void LightData::update() void LightData::update()
{ {
Object *object = (Object *)id;
Light *light = (Light *)object->data; Light *light = (Light *)object->data;
pxr::HdDirtyBits bits = pxr::HdLight::Clean; pxr::HdDirtyBits bits = pxr::HdLight::Clean;
if (id->recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) { if (object->id.recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) {
if (prim_type(light) != prim_type_) { if (prim_type(light) != prim_type_) {
remove(); remove();
init(); init();
@ -122,7 +121,7 @@ void LightData::update()
init(); init();
bits = pxr::HdLight::AllDirty; bits = pxr::HdLight::AllDirty;
} }
else if (id->recalc & ID_RECALC_TRANSFORM) { else if (object->id.recalc & ID_RECALC_TRANSFORM) {
write_transform(); write_transform();
bits = pxr::HdLight::DirtyTransform; bits = pxr::HdLight::DirtyTransform;
} }
@ -148,17 +147,6 @@ pxr::VtValue LightData::get_data(pxr::TfToken const &key) const
return pxr::VtValue(); return pxr::VtValue();
} }
bool LightData::update_visibility()
{
bool ret = ObjectData::update_visibility();
if (ret) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id,
pxr::HdLight::DirtyParams);
ID_LOG(1, "");
}
return ret;
}
pxr::TfToken LightData::prim_type(Light *light) pxr::TfToken LightData::prim_type(Light *light)
{ {
switch (light->type) { switch (light->type) {

View File

@ -27,7 +27,6 @@ class LightData : public ObjectData {
void update() override; void update() override;
pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility() override;
private: private:
pxr::TfToken prim_type(Light *light); pxr::TfToken prim_type(Light *light);

View File

@ -25,14 +25,14 @@ namespace blender::render::hydra {
MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate,
Material *material, Material *material,
pxr::SdfPath const &prim_id) pxr::SdfPath const &prim_id)
: IdData(scene_delegate, (ID *)material, prim_id) : IdData(scene_delegate, prim_id), material(material)
{ {
} }
void MaterialData::init() void MaterialData::init()
{ {
ID_LOG(1, ""); ID_LOG(1, "");
double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0; double_sided = (material->blend_flag & MA_BL_CULL_BACKFACE) == 0;
export_mtlx(); export_mtlx();
if (scene_delegate_->settings.mx_filename_key.IsEmpty()) { if (scene_delegate_->settings.mx_filename_key.IsEmpty()) {
@ -105,12 +105,12 @@ void MaterialData::export_mtlx()
func = PyDict_GetItemString(dict, "export_mtlx"); func = PyDict_GetItemString(dict, "export_mtlx");
PointerRNA materialptr; PointerRNA materialptr;
RNA_pointer_create(NULL, &RNA_Material, id, &materialptr); RNA_pointer_create(NULL, &RNA_Material, material, &materialptr);
PyObject *material = pyrna_struct_CreatePyObject(&materialptr); PyObject *pymaterial = pyrna_struct_CreatePyObject(&materialptr);
result = PyObject_CallFunction(func, "O", material); result = PyObject_CallFunction(func, "O", pymaterial);
Py_DECREF(material); Py_DECREF(pymaterial);
std::string path; std::string path;
@ -135,7 +135,7 @@ void MaterialData::export_mtlx()
CLOG_ERROR(LOG_RENDER_HYDRA_SCENE, CLOG_ERROR(LOG_RENDER_HYDRA_SCENE,
"Export error for %s (%s): %s", "Export error for %s (%s): %s",
prim_id.GetText(), prim_id.GetText(),
id->name, material->id.name,
err_str.c_str()); err_str.c_str());
if (traceback) { if (traceback) {
PyTraceBack_Print(traceback, PySys_GetObject("stderr")); PyTraceBack_Print(traceback, PySys_GetObject("stderr"));

View File

@ -29,6 +29,7 @@ class MaterialData : public IdData {
pxr::VtValue get_material_resource() const; pxr::VtValue get_material_resource() const;
pxr::HdCullStyle cull_style() const; pxr::HdCullStyle cull_style() const;
Material *material;
bool double_sided = true; bool double_sided = true;
private: private:

View File

@ -29,7 +29,6 @@ void MeshData::init()
{ {
ID_LOG(1, ""); ID_LOG(1, "");
Object *object = (Object *)id;
Mesh *mesh = BKE_object_to_mesh(nullptr, object, false); Mesh *mesh = BKE_object_to_mesh(nullptr, object, false);
if (mesh) { if (mesh) {
write_submeshes(mesh); write_submeshes(mesh);
@ -55,18 +54,19 @@ void MeshData::remove()
void MeshData::update() void MeshData::update()
{ {
Object *object = (Object *)id; if ((object->id.recalc & ID_RECALC_GEOMETRY) ||
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY))
{
init(); init();
return; return;
} }
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if (id->recalc & ID_RECALC_SHADING) { if (object->id.recalc & ID_RECALC_SHADING) {
write_materials(); write_materials();
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
} }
if (id->recalc & ID_RECALC_TRANSFORM) { if (object->id.recalc & ID_RECALC_TRANSFORM) {
write_transform(); write_transform();
bits |= pxr::HdChangeTracker::DirtyTransform; bits |= pxr::HdChangeTracker::DirtyTransform;
} }
@ -81,31 +81,23 @@ void MeshData::update()
} }
} }
pxr::VtValue MeshData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const
{ {
if (key == pxr::HdTokens->points) { if (key == pxr::HdTokens->points) {
return pxr::VtValue(vertices_); return pxr::VtValue(vertices_);
} }
else if (key == pxr::HdTokens->normals) { return pxr::VtValue();
}
pxr::VtValue MeshData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const
{
if (key == pxr::HdTokens->normals) {
return pxr::VtValue(submesh(id).normals); return pxr::VtValue(submesh(id).normals);
} }
else if (key == pxr::_tokens->st) { else if (key == pxr::_tokens->st) {
return pxr::VtValue(submesh(id).uvs); return pxr::VtValue(submesh(id).uvs);
} }
return pxr::VtValue(); return get_data(key);
}
bool MeshData::update_visibility()
{
bool ret = ObjectData::update_visibility();
if (ret) {
for (int i = 0; i < submeshes_.size(); ++i) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
submesh_prim_id(i), pxr::HdChangeTracker::DirtyVisibility);
ID_LOG(1, "%d", i);
}
}
return ret;
} }
pxr::HdMeshTopology MeshData::mesh_topology(pxr::SdfPath const &id) const pxr::HdMeshTopology MeshData::mesh_topology(pxr::SdfPath const &id) const
@ -217,7 +209,7 @@ void MeshData::write_submeshes(Mesh *mesh)
vertices_.clear(); vertices_.clear();
/* Insert base submeshes */ /* Insert base submeshes */
int mat_count = BKE_object_material_count_eval((Object *)id); int mat_count = BKE_object_material_count_eval(object);
for (int i = 0; i < std::max(mat_count, 1); ++i) { for (int i = 0; i < std::max(mat_count, 1); ++i) {
SubMesh sm; SubMesh sm;
sm.mat_index = i; sm.mat_index = i;
@ -298,7 +290,6 @@ void MeshData::write_submeshes(Mesh *mesh)
void MeshData::write_materials() void MeshData::write_materials()
{ {
Object *object = (Object *)id;
for (int i = 0; i < submeshes_.size(); ++i) { for (int i = 0; i < submeshes_.size(); ++i) {
SubMesh &m = submeshes_[i]; SubMesh &m = submeshes_[i];
Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1); Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1);

View File

@ -32,8 +32,8 @@ class MeshData : public ObjectData {
void remove() override; void remove() override;
void update() override; void update() override;
pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility() override; pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const override;
pxr::HdMeshTopology mesh_topology(pxr::SdfPath const &id) const; pxr::HdMeshTopology mesh_topology(pxr::SdfPath const &id) const;
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const;

View File

@ -15,7 +15,7 @@ namespace blender::render::hydra {
ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate,
Object *object, Object *object,
pxr::SdfPath const &prim_id) pxr::SdfPath const &prim_id)
: IdData(scene_delegate, (ID *)object, prim_id), transform(pxr::GfMatrix4d(1.0)) : IdData(scene_delegate, prim_id), object(object), transform(pxr::GfMatrix4d(1.0))
{ {
} }
@ -43,6 +43,7 @@ std::unique_ptr<ObjectData> ObjectData::create(BlenderSceneDelegate *scene_deleg
data = std::make_unique<VolumeData>(scene_delegate, object, prim_id); data = std::make_unique<VolumeData>(scene_delegate, object, prim_id);
break; break;
default: default:
BLI_assert_unreachable();
break; break;
} }
return data; return data;
@ -78,18 +79,20 @@ bool ObjectData::is_visible(BlenderSceneDelegate *scene_delegate, Object *object
return ret; return ret;
} }
bool ObjectData::update_visibility() pxr::VtValue ObjectData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const
{ {
bool prev_visible = visible; return get_data(key);
visible = is_visible(scene_delegate_, (Object *)id);
return visible != prev_visible;
} }
void ObjectData::available_materials(Set<pxr::SdfPath> &paths) const {}
void ObjectData::write_transform() void ObjectData::write_transform()
{ {
transform = gf_matrix_from_transform(((Object *)id)->object_to_world); transform = gf_matrix_from_transform(object->object_to_world);
} }
void ObjectData::write_materials() {}
pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]) pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4])
{ {
pxr::GfMatrix4d ret; pxr::GfMatrix4d ret;

View File

@ -9,6 +9,7 @@
#include "BKE_layer.h" #include "BKE_layer.h"
#include "BKE_object.h" #include "BKE_object.h"
#include "BLI_map.hh" #include "BLI_map.hh"
#include "BLI_set.hh"
#include "DNA_object_types.h" #include "DNA_object_types.h"
#include "id.h" #include "id.h"
@ -28,13 +29,17 @@ class ObjectData : public IdData {
Object *object, Object *object,
int mode = OB_VISIBLE_SELF); int mode = OB_VISIBLE_SELF);
virtual bool update_visibility(); using IdData::get_data;
virtual pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const;
virtual void available_materials(Set<pxr::SdfPath> &paths) const;
Object *object;
pxr::GfMatrix4d transform; pxr::GfMatrix4d transform;
bool visible = true; bool visible = true;
protected: protected:
void write_transform(); virtual void write_transform();
virtual void write_materials();
}; };
using ObjectDataMap = Map<pxr::SdfPath, std::unique_ptr<ObjectData>>; using ObjectDataMap = Map<pxr::SdfPath, std::unique_ptr<ObjectData>>;

View File

@ -28,7 +28,7 @@ VolumeData::VolumeData(BlenderSceneDelegate *scene_delegate,
void VolumeData::init() void VolumeData::init()
{ {
ID_LOG(1, ""); ID_LOG(1, "");
Volume *volume = (Volume *)((Object *)this->id)->data; Volume *volume = (Volume *)object->data;
Main *main = CTX_data_main(scene_delegate_->context); Main *main = CTX_data_main(scene_delegate_->context);
if (!BKE_volume_load(volume, main)) { if (!BKE_volume_load(volume, main)) {
return; return;
@ -80,17 +80,18 @@ void VolumeData::remove()
void VolumeData::update() void VolumeData::update()
{ {
Object *object = (Object *)id;
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { if ((object->id.recalc & ID_RECALC_GEOMETRY) ||
(((ID *)object->data)->recalc & ID_RECALC_GEOMETRY))
{
init(); init();
bits = pxr::HdChangeTracker::AllDirty; bits = pxr::HdChangeTracker::AllDirty;
} }
if (id->recalc & ID_RECALC_SHADING) { if (object->id.recalc & ID_RECALC_SHADING) {
write_materials(); write_materials();
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided; bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
} }
if (id->recalc & ID_RECALC_TRANSFORM) { if (object->id.recalc & ID_RECALC_TRANSFORM) {
write_transform(); write_transform();
bits |= pxr::HdChangeTracker::DirtyTransform; bits |= pxr::HdChangeTracker::DirtyTransform;
} }
@ -103,13 +104,13 @@ void VolumeData::update()
ID_LOG(1, ""); ID_LOG(1, "");
} }
BogdanNagirniak marked this conversation as resolved
Review

Must be get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const since id here is field name.

Also change back prim_id.GetName(); -> id.GetName();

Must be `get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const` since `id` here is field name. Also change back `prim_id.GetName();` -> `id.GetName();`
Review

No, VolumeData::get_data(pxr::TfToken const &key) should be here. By default

pxr::VtValue ObjectData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const
{
  return get_data(key);
}
No, `VolumeData::get_data(pxr::TfToken const &key)` should be here. By default ``` pxr::VtValue ObjectData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const { return get_data(key); } ```
Review

Yes, you are right. Fixed.

Yes, you are right. Fixed.
pxr::VtValue VolumeData::get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const
{ {
if (key == pxr::HdVolumeFieldSchemaTokens->filePath) { if (key == pxr::HdVolumeFieldSchemaTokens->filePath) {
return pxr::VtValue(pxr::SdfAssetPath(filepath_, filepath_)); return pxr::VtValue(pxr::SdfAssetPath(filepath_, filepath_));
} }
if (key == pxr::HdVolumeFieldSchemaTokens->fieldName) { if (key == pxr::HdVolumeFieldSchemaTokens->fieldName) {
std::string name = id.GetName(); std::string name = prim_id.GetName();
return pxr::VtValue(pxr::TfToken(name.substr(name.find("VF_") + 3))); return pxr::VtValue(pxr::TfToken(name.substr(name.find("VF_") + 3)));
} }
if (key == pxr::HdVolumeFieldSchemaTokens->fieldIndex) { if (key == pxr::HdVolumeFieldSchemaTokens->fieldIndex) {
@ -121,17 +122,6 @@ pxr::VtValue VolumeData::get_data(pxr::SdfPath const &id, pxr::TfToken const &ke
return pxr::VtValue(); return pxr::VtValue();
} }
bool VolumeData::update_visibility()
{
bool ret = ObjectData::update_visibility();
if (ret) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
prim_id, pxr::HdChangeTracker::DirtyVisibility);
ID_LOG(1, "");
}
return ret;
}
pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const pxr::HdVolumeFieldDescriptorVector VolumeData::field_descriptors() const
{ {
return field_descriptors_; return field_descriptors_;
@ -154,7 +144,6 @@ void VolumeData::available_materials(Set<pxr::SdfPath> &paths) const
void VolumeData::write_materials() void VolumeData::write_materials()
{ {
Object *object = (Object *)id;
Material *mat = nullptr; Material *mat = nullptr;
/* TODO: Using only first material. Add support for multimaterial. */ /* TODO: Using only first material. Add support for multimaterial. */
if (BKE_object_material_count_eval(object) > 0) { if (BKE_object_material_count_eval(object) > 0) {

View File

@ -19,12 +19,11 @@ class VolumeData : public ObjectData {
void remove() override; void remove() override;
void update() override; void update() override;
pxr::VtValue get_data(pxr::SdfPath const &id, pxr::TfToken const &key) const; pxr::VtValue get_data(pxr::TfToken const &key) const override;
bool update_visibility() override;
pxr::HdVolumeFieldDescriptorVector field_descriptors() const; pxr::HdVolumeFieldDescriptorVector field_descriptors() const;
pxr::SdfPath material_id() const; pxr::SdfPath material_id() const;
void available_materials(Set<pxr::SdfPath> &paths) const; void available_materials(Set<pxr::SdfPath> &paths) const override;
private: private:
void write_materials(); void write_materials();

View File

@ -33,7 +33,7 @@
namespace blender::render::hydra { namespace blender::render::hydra {
WorldData::WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id) WorldData::WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id)
: IdData(scene_delegate, nullptr, prim_id) : IdData(scene_delegate, prim_id)
{ {
} }