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
14 changed files with 87 additions and 78 deletions
Showing only changes of commit 1f4e8b2126 - Show all commits

View File

@ -20,8 +20,9 @@ CurvesData::CurvesData(BlenderSceneDelegate *scene_delegate,
void CurvesData::init()
{
ID_LOG(1, "");
ID_LOGN(1, "");
Object *object = (Object *)id;
write_curves((Curves *)object->data);
write_transform();
write_materials();
@ -29,31 +30,30 @@ void CurvesData::init()
void CurvesData::insert()
{
ID_LOG(1, "");
ID_LOGN(1, "");
scene_delegate_->GetRenderIndex().InsertRprim(
pxr::HdPrimTypeTokens->basisCurves, scene_delegate_, prim_id);
}
void CurvesData::remove()
{
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText());
ID_LOG(1, "");
scene_delegate_->GetRenderIndex().RemoveRprim(prim_id);
}
void CurvesData::update()
{
Object *object = (Object *)id;
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if ((object->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)) {
init();
bits = pxr::HdChangeTracker::AllDirty;
}
if (object->id.recalc & ID_RECALC_SHADING) {
if (id->recalc & ID_RECALC_SHADING) {
write_materials();
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
}
if (object->id.recalc & ID_RECALC_TRANSFORM) {
if (id->recalc & ID_RECALC_TRANSFORM) {
write_transform();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
@ -63,7 +63,7 @@ void CurvesData::update()
}
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(prim_id, bits);
ID_LOG(1, "");
ID_LOGN(1, "");
}
pxr::VtValue CurvesData::get_data(pxr::TfToken const &key) const
@ -169,6 +169,7 @@ void CurvesData::write_uv_maps(Curves *curves)
void CurvesData::write_materials()
{
Object *object = (Object *)id;
Material *mat = nullptr;
/* TODO: Using only first material. Add support for multimaterial. */
if (BKE_object_material_count_eval(object) > 0) {

View File

@ -7,8 +7,8 @@
namespace blender::render::hydra {
IdData::IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id)
: prim_id(prim_id), scene_delegate_(scene_delegate)
IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id)
: id(id), prim_id(prim_id), scene_delegate_(scene_delegate)
{
}

View File

@ -30,7 +30,7 @@ class BlenderSceneDelegate;
class IdData {
public:
IdData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id);
IdData(BlenderSceneDelegate *scene_delegate, ID *id, pxr::SdfPath const &prim_id);
virtual ~IdData() = default;
virtual void init() = 0;
@ -40,6 +40,7 @@ class IdData {
virtual pxr::VtValue get_data(pxr::TfToken const &key) const = 0;
ID *id;
pxr::SdfPath prim_id;
protected:
@ -49,4 +50,12 @@ class IdData {
#define ID_LOG(level, msg, ...) \
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, level, "%s: " msg, prim_id.GetText(), ##__VA_ARGS__);
#define ID_LOGN(level, msg, ...) \
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \
level, \
"%s (%s): " msg, \
prim_id.GetText(), \
id->name, \
##__VA_ARGS__);
} // namespace blender::render::hydra

View File

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

View File

@ -24,9 +24,9 @@ LightData::LightData(BlenderSceneDelegate *scene_delegate,
void LightData::init()
{
OBJ_LOG(1, "");
ID_LOGN(1, "");
Light *light = (Light *)object->data;
Light *light = (Light *)((Object *)id)->data;
data_.clear();
switch (light->type) {
@ -97,7 +97,7 @@ void LightData::init()
void LightData::insert()
{
OBJ_LOG(1, "");
ID_LOGN(1, "");
scene_delegate_->GetRenderIndex().InsertSprim(prim_type_, scene_delegate_, prim_id);
}
@ -109,9 +109,10 @@ void LightData::remove()
void LightData::update()
{
Object *object = (Object *)id;
Light *light = (Light *)object->data;
pxr::HdDirtyBits bits = pxr::HdLight::Clean;
if (object->id.recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) {
if (id->recalc & ID_RECALC_GEOMETRY || light->id.recalc & ID_RECALC_GEOMETRY) {
if (prim_type(light) != prim_type_) {
remove();
init();
@ -121,19 +122,19 @@ void LightData::update()
init();
bits = pxr::HdLight::AllDirty;
}
else if (object->id.recalc & ID_RECALC_TRANSFORM) {
else if (id->recalc & ID_RECALC_TRANSFORM) {
write_transform();
bits = pxr::HdLight::DirtyTransform;
}
if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id, bits);
OBJ_LOG(1, "");
ID_LOGN(1, "");
}
}
pxr::VtValue LightData::get_data(pxr::TfToken const &key) const
{
OBJ_LOG(3, "%s", key.GetText());
ID_LOGN(3, "%s", key.GetText());
auto it = data_.find(key);
if (it != data_.end()) {
return pxr::VtValue(it->second);

View File

@ -25,14 +25,14 @@ namespace blender::render::hydra {
MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate,
Material *material,
pxr::SdfPath const &prim_id)
: IdData(scene_delegate, prim_id), material(material)
: IdData(scene_delegate, (ID *)material, prim_id)
{
}
void MaterialData::init()
{
ID_LOG(1, "");
double_sided = (material->blend_flag & MA_BL_CULL_BACKFACE) == 0;
ID_LOGN(1, "");
double_sided = (((Material *)id)->blend_flag & MA_BL_CULL_BACKFACE) == 0;
export_mtlx();
if (scene_delegate_->settings.mx_filename_key.IsEmpty()) {
@ -42,20 +42,20 @@ void MaterialData::init()
void MaterialData::insert()
{
ID_LOG(1, "");
ID_LOGN(1, "");
scene_delegate_->GetRenderIndex().InsertSprim(
pxr::HdPrimTypeTokens->material, scene_delegate_, prim_id);
}
void MaterialData::remove()
{
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText());
ID_LOG(1, "");
scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, prim_id);
}
void MaterialData::update()
{
ID_LOG(1, "");
ID_LOGN(1, "");
bool prev_double_sided = double_sided;
init();
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(prim_id,
@ -74,7 +74,7 @@ void MaterialData::update()
pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const
{
if (key == scene_delegate_->settings.mx_filename_key) {
ID_LOG(3, "%s", key.GetText());
ID_LOGN(3, "%s", key.GetText());
if (!mtlx_path_.GetResolvedPath().empty()) {
return pxr::VtValue(mtlx_path_);
}
@ -105,12 +105,12 @@ void MaterialData::export_mtlx()
func = PyDict_GetItemString(dict, "export_mtlx");
PointerRNA materialptr;
RNA_pointer_create(NULL, &RNA_Material, material, &materialptr);
PyObject *pymaterial = pyrna_struct_CreatePyObject(&materialptr);
RNA_pointer_create(NULL, &RNA_Material, id, &materialptr);
PyObject *material = pyrna_struct_CreatePyObject(&materialptr);
result = PyObject_CallFunction(func, "O", pymaterial);
result = PyObject_CallFunction(func, "O", material);
Py_DECREF(pymaterial);
Py_DECREF(material);
std::string path;
@ -135,7 +135,7 @@ void MaterialData::export_mtlx()
CLOG_ERROR(LOG_RENDER_HYDRA_SCENE,
"Export error for %s (%s): %s",
prim_id.GetText(),
material->id.name,
id->name,
err_str.c_str());
if (traceback) {
PyTraceBack_Print(traceback, PySys_GetObject("stderr"));
@ -149,12 +149,12 @@ void MaterialData::export_mtlx()
PyGILState_Release(gstate);
mtlx_path_ = pxr::SdfAssetPath(path, path);
ID_LOG(1, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str());
ID_LOGN(1, "mtlx=%s", mtlx_path_.GetResolvedPath().c_str());
}
void MaterialData::write_material_network_map()
{
ID_LOG(1, "");
ID_LOGN(1, "");
if (mtlx_path_.GetResolvedPath().empty()) {
material_network_map_ = pxr::VtValue();
return;

View File

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

View File

@ -27,8 +27,9 @@ MeshData::MeshData(BlenderSceneDelegate *scene_delegate,
void MeshData::init()
{
ID_LOG(1, "");
ID_LOGN(1, "");
Object *object = (Object *)id;
Mesh *mesh = BKE_object_to_mesh(nullptr, object, false);
if (mesh) {
write_submeshes(mesh);
@ -48,25 +49,24 @@ void MeshData::remove()
{
for (int i = 0; i < submeshes_.size(); ++i) {
scene_delegate_->GetRenderIndex().RemoveRprim(submesh_prim_id(i));
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s: %d", prim_id.GetText(), i);
ID_LOG(1, "%d", i);
}
}
void MeshData::update()
{
if ((object->id.recalc & ID_RECALC_GEOMETRY) ||
(((ID *)object->data)->recalc & ID_RECALC_GEOMETRY))
{
Object *object = (Object *)id;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
return;
}
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if (object->id.recalc & ID_RECALC_SHADING) {
if (id->recalc & ID_RECALC_SHADING) {
write_materials();
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
}
if (object->id.recalc & ID_RECALC_TRANSFORM) {
if (id->recalc & ID_RECALC_TRANSFORM) {
write_transform();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
@ -77,7 +77,7 @@ void MeshData::update()
for (int i = 0; i < submeshes_.size(); ++i) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(submesh_prim_id(i), bits);
ID_LOG(1, "%d", i);
ID_LOGN(1, "%d", i);
}
}
@ -165,7 +165,7 @@ void MeshData::update_double_sided(MaterialData *mat_data)
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
submesh_prim_id(i),
pxr::HdChangeTracker::DirtyDoubleSided | pxr::HdChangeTracker::DirtyCullStyle);
ID_LOG(1, "%d", i);
ID_LOGN(1, "%d", i);
}
}
}
@ -209,7 +209,7 @@ void MeshData::write_submeshes(Mesh *mesh)
vertices_.clear();
/* Insert base submeshes */
int mat_count = BKE_object_material_count_eval(object);
int mat_count = BKE_object_material_count_eval((Object *)id);
for (int i = 0; i < std::max(mat_count, 1); ++i) {
SubMesh sm;
sm.mat_index = i;
@ -275,11 +275,11 @@ void MeshData::write_submeshes(Mesh *mesh)
pxr::SdfPath p = submesh_prim_id(i);
if (i < sub_meshes_prev_count) {
render_index.GetChangeTracker().MarkRprimDirty(p, pxr::HdChangeTracker::AllDirty);
ID_LOG(1, "Update %d", i);
ID_LOGN(1, "Update %d", i);
}
else {
render_index.InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate_, p);
ID_LOG(1, "Insert %d", i);
ID_LOGN(1, "Insert %d", i);
}
}
for (; i < sub_meshes_prev_count; ++i) {
@ -290,6 +290,7 @@ void MeshData::write_submeshes(Mesh *mesh)
void MeshData::write_materials()
{
Object *object = (Object *)id;
for (int i = 0; i < submeshes_.size(); ++i) {
SubMesh &m = submeshes_[i];
Material *mat = BKE_object_material_get_eval(object, m.mat_index + 1);

View File

@ -41,7 +41,7 @@ class MeshData : public ObjectData {
pxr::HdCullStyle cull_style(pxr::SdfPath const &id) const;
bool double_sided(pxr::SdfPath const &id) const;
void update_double_sided(MaterialData *mat_data);
void available_materials(Set<pxr::SdfPath> &paths) const;
void available_materials(Set<pxr::SdfPath> &paths) const override;
pxr::SdfPathVector submesh_paths() const;
private:

View File

@ -15,7 +15,7 @@ namespace blender::render::hydra {
ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate,
Object *object,
pxr::SdfPath const &prim_id)
: IdData(scene_delegate, prim_id), object(object), transform(pxr::GfMatrix4d(1.0))
: IdData(scene_delegate, (ID *)object, prim_id), transform(pxr::GfMatrix4d(1.0))
{
}
@ -88,7 +88,7 @@ void ObjectData::available_materials(Set<pxr::SdfPath> &paths) const {}
void ObjectData::write_transform()
{
transform = gf_matrix_from_transform(object->object_to_world);
transform = gf_matrix_from_transform(((Object *)id)->object_to_world);
}
void ObjectData::write_materials() {}

View File

@ -33,7 +33,6 @@ class ObjectData : public IdData {
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;
bool visible = true;
@ -46,12 +45,4 @@ using ObjectDataMap = Map<pxr::SdfPath, std::unique_ptr<ObjectData>>;
pxr::GfMatrix4d gf_matrix_from_transform(float m[4][4]);
#define OBJ_LOG(level, msg, ...) \
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, \
level, \
"%s (%s): " msg, \
prim_id.GetText(), \
object ? object->id.name : "", \
##__VA_ARGS__);
} // namespace blender::render::hydra

View File

@ -27,8 +27,8 @@ VolumeData::VolumeData(BlenderSceneDelegate *scene_delegate,
void VolumeData::init()
{
ID_LOG(1, "");
Volume *volume = (Volume *)object->data;
ID_LOGN(1, "");
Volume *volume = (Volume *)((Object *)this->id)->data;
Main *main = CTX_data_main(scene_delegate_->context);
if (!BKE_volume_load(volume, main)) {
return;
@ -59,39 +59,38 @@ void VolumeData::insert()
scene_delegate_->GetRenderIndex().InsertRprim(
pxr::HdPrimTypeTokens->volume, scene_delegate_, prim_id);
ID_LOG(1, "");
ID_LOGN(1, "");
for (auto &desc : field_descriptors_) {
scene_delegate_->GetRenderIndex().InsertBprim(
desc.fieldPrimType, scene_delegate_, desc.fieldId);
ID_LOG(1, "Volume field %s", desc.fieldId.GetText());
ID_LOGN(1, "Volume field %s", desc.fieldId.GetText());
}
}
void VolumeData::remove()
{
for (auto &desc : field_descriptors_) {
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", desc.fieldId.GetText());
ID_LOG(1, "%s", desc.fieldId.GetText());
scene_delegate_->GetRenderIndex().RemoveBprim(desc.fieldPrimType, desc.fieldId);
}
CLOG_INFO(LOG_RENDER_HYDRA_SCENE, 1, "%s", prim_id.GetText());
ID_LOG(1, "");
scene_delegate_->GetRenderIndex().RemoveRprim(prim_id);
}
void VolumeData::update()
{
Object *object = (Object *)id;
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if ((object->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)) {
init();
bits = pxr::HdChangeTracker::AllDirty;
}
if (object->id.recalc & ID_RECALC_SHADING) {
if (id->recalc & ID_RECALC_SHADING) {
write_materials();
bits |= pxr::HdChangeTracker::DirtyMaterialId | pxr::HdChangeTracker::DirtyDoubleSided;
}
if (object->id.recalc & ID_RECALC_TRANSFORM) {
if (id->recalc & ID_RECALC_TRANSFORM) {
write_transform();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
@ -101,7 +100,7 @@ void VolumeData::update()
}
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(prim_id, bits);
ID_LOG(1, "");
ID_LOGN(1, "");
}
pxr::VtValue VolumeData::get_data(pxr::TfToken const &key) const
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.
@ -144,6 +143,7 @@ void VolumeData::available_materials(Set<pxr::SdfPath> &paths) const
void VolumeData::write_materials()
{
Object *object = (Object *)id;
Material *mat = nullptr;
/* TODO: Using only first material. Add support for multimaterial. */
if (BKE_object_material_count_eval(object) > 0) {

View File

@ -134,6 +134,12 @@ void WorldData::init()
write_transform();
}
void WorldData::insert()
{
ID_LOG(1, "");
scene_delegate_->GetRenderIndex().InsertSprim(prim_type_, scene_delegate_, prim_id);
}
void WorldData::update()
{
ID_LOG(1, "");

View File

@ -23,6 +23,7 @@ class WorldData : public LightData {
WorldData(BlenderSceneDelegate *scene_delegate, pxr::SdfPath const &prim_id);
void init() override;
void insert() override;
void update() override;
protected: