BLEN-299: Export instances #11

Merged
Bogdan Nagirniak merged 7 commits from BLEN-299 into hydra-render 2023-03-05 09:37:12 +01:00
7 changed files with 179 additions and 77 deletions

View File

@ -84,8 +84,6 @@ set(SRC
sceneDelegate/light.cc
sceneDelegate/world.h
sceneDelegate/world.cc
sceneDelegate/instance.h
sceneDelegate/instance.cc
)
set(LIB

View File

@ -79,6 +79,64 @@ bool BlenderSceneDelegate::GetVisible(SdfPath const &id)
return object_data(id)->visible;
}
SdfPath BlenderSceneDelegate::GetInstancerId(SdfPath const &primId)
{
LOG(INFO) << "GetInstancerId: " << primId.GetAsString();
MeshData *m_data = mesh_data(primId);
if (m_data) {
return m_data->instancer_id;
}
return SdfPath();
}
SdfPathVector BlenderSceneDelegate::GetInstancerPrototypes(SdfPath const &instancerId)
{
LOG(INFO) << "GetInstancerPrototypes: " << instancerId.GetString();
SdfPathVector paths;
paths.push_back(instancerId.GetParentPath());
return paths;
}
VtIntArray BlenderSceneDelegate::GetInstanceIndices(SdfPath const &instancerId,
SdfPath const &prototypeId)
{
LOG(INFO) << "GetInstanceIndices: " << instancerId.GetString() << " " << prototypeId.GetString();
MeshData *m_data = mesh_data(prototypeId);
VtIntArray ret = m_data->instance_indices();
return ret;
}
GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(SdfPath const &instancerId)
{
LOG(INFO) << "GetInstancerTransform: " << instancerId.GetString();
// TODO: add a separate object for instancer for cleaner handling code
// Actual instancer transform is get here
return GfMatrix4d(1.0);
}
size_t BlenderSceneDelegate::SampleInstancerTransform(SdfPath const &instancerId, size_t maxSampleCount,
float *sampleTimes, GfMatrix4d *sampleValues)
{
LOG(INFO) << "SampleInstancerTransform: " << instancerId.GetString();
size_t ret = 0;
MeshData *m_data = mesh_data(instancerId.GetParentPath());
ret = m_data->sample_instancer_transform(maxSampleCount, sampleTimes, sampleValues);
return ret;
}
size_t BlenderSceneDelegate::SamplePrimvar(SdfPath const &id, TfToken const &key, size_t maxSampleCount,
float *sampleTimes, VtValue *sampleValues)
{
// 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, maxSampleCount, sampleTimes, sampleValues);
}
}
return HdSceneDelegate::SamplePrimvar(id, key, maxSampleCount, sampleTimes, sampleValues);
}
void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
{
if (visibility) {
@ -92,6 +150,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
/* Export of new visible objects which were not exported before */
std::set<SdfPath> available_objects;
SdfPath id;
DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph;
@ -106,15 +165,15 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
DEG_iterator_objects_end,
&data, Object *, object) {
if (data.dupli_object_current != nullptr) {
InstanceData i_data(this, data.dupli_object_current);
LOG(INFO) << "Instance: " << i_data.name() << " " << i_data.random_id();
add_update_instance(data.dupli_object_current);
continue;
}
if (!ObjectData::supported(object)) {
continue;
}
SdfPath id = ObjectData::prim_id(this, object);
id = ObjectData::prim_id(this, object);
if (remove) {
available_objects.insert(id);
}
@ -192,6 +251,17 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool
}
}
void BlenderSceneDelegate::add_update_instance(DupliObject *dupli)
{
SdfPath id = ObjectData::prim_id(this, dupli->ob);
if (!object_data(id)) {
add_update_object(dupli->ob, true, true, true);
}
MeshData *m_data = mesh_data(id);
m_data->add_instance(dupli);
}
ObjectData *BlenderSceneDelegate::object_data(SdfPath const &id)
{
auto it = objects.find(id);
@ -232,6 +302,7 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
/* Export initial objects */
update_collection(false, false);
update_world();
GetRenderIndex().InsertInstancer(this, GetDelegateID().AppendElementString("Instancer"));
return;
}
@ -312,13 +383,19 @@ void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
HdMeshTopology BlenderSceneDelegate::GetMeshTopology(SdfPath const& id)
{
LOG(INFO) << "GetMeshTopology: " << id.GetString();
MeshData *m_data = mesh_data(id);
return m_data->mesh_topology();
}
VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key)
{
LOG(INFO) << "Get: " << id.GetString() << " " << key.GetString();
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) {
return obj_data->get_data(key);
}
@ -332,7 +409,18 @@ VtValue BlenderSceneDelegate::Get(SdfPath const& id, TfToken const& key)
HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(SdfPath const& id, HdInterpolation interpolation)
{
LOG(INFO) << "GetPrimvarDescriptors: " << id.GetString() << " " << interpolation;
if (mesh_data(id)) {
return mesh_data(id)->primvar_descriptors(interpolation);
}
// TODO: add a separate object for instancer for cleaner handling code
else if (id.GetName() == "Instancer") {
if (MeshData *data = mesh_data(id.GetParentPath())) {
return data->instancer_primvar_descriptors(interpolation);
}
}
HdPrimvarDescriptorVector primvars;
return primvars;
}
SdfPath BlenderSceneDelegate::GetMaterialId(SdfPath const & rprimId)
@ -351,10 +439,17 @@ VtValue BlenderSceneDelegate::GetMaterialResource(SdfPath const& id)
GfMatrix4d BlenderSceneDelegate::GetTransform(SdfPath const& id)
{
LOG(INFO) << "GetTransform: " << id.GetString();
ObjectData *obj_data = object_data(id);
if (obj_data) {
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)) {
return world_data->transform();
}

View File

@ -12,7 +12,6 @@
#include "mesh.h"
#include "light.h"
#include "world.h"
#include "instance.h"
namespace blender::render::hydra {
@ -38,6 +37,14 @@ public:
pxr::SdfPath GetMaterialId(pxr::SdfPath const &rprimId) override;
pxr::VtValue GetMaterialResource(pxr::SdfPath const &materialId) override;
bool GetVisible(pxr::SdfPath const &id) override;
pxr::SdfPath GetInstancerId(pxr::SdfPath const &primId) override;
pxr::SdfPathVector GetInstancerPrototypes(pxr::SdfPath const &instancerId) override;
pxr::VtIntArray GetInstanceIndices(pxr::SdfPath const &instancerId, pxr::SdfPath const &prototypeId) override;
pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancerId);
size_t SampleInstancerTransform(pxr::SdfPath const &instancerId, size_t maxSampleCount,
float *sampleTimes, pxr::GfMatrix4d *sampleValues) override;
size_t SamplePrimvar(pxr::SdfPath const &id, pxr::TfToken const &key, size_t maxSampleCount,
float *sampleTimes, pxr::VtValue *sampleValues) override;
EngineType engine_type;
@ -48,6 +55,7 @@ private:
MaterialData *material_data(pxr::SdfPath const &id);
void add_update_object(Object *object, bool geometry, bool transform, bool shading);
void add_update_instance(DupliObject *dupli);
void set_material(MeshData &mesh_data);
void update_material(Material *material);
void update_world();
@ -61,7 +69,6 @@ private:
ObjectDataMap objects;
MaterialDataMap materials;
std::unique_ptr<WorldData> world_data;
InstanceDataMap instances;
};
} // namespace blender::render::hydra

View File

@ -1,39 +0,0 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include "blenderSceneDelegate.h"
#include "instance.h"
using namespace pxr;
namespace blender::render::hydra {
InstanceData::InstanceData(BlenderSceneDelegate *scene_delegate, DupliObject *dupli)
: IdData(scene_delegate, (ID *)dupli->ob_data)
, dupli(dupli)
{
}
int InstanceData::random_id()
{
return dupli->random_id;
}
VtValue InstanceData::get_data(TfToken const &key)
{
return VtValue();
}
void InstanceData::insert_prim()
{
}
void InstanceData::remove_prim()
{
}
void InstanceData::mark_prim_dirty(DirtyBits dirty_bits)
{
}
} // namespace blender::render::hydra

View File

@ -1,29 +0,0 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#pragma once
#include "BKE_duplilist.h"
#include "id.h"
namespace blender::render::hydra {
class InstanceData: public IdData {
public:
InstanceData(BlenderSceneDelegate *scene_delegate, DupliObject *dupli);
int random_id();
pxr::VtValue get_data(pxr::TfToken const &key) override;
void insert_prim() override;
void remove_prim() override;
void mark_prim_dirty(DirtyBits dirty_bits) override;
public:
DupliObject *dupli;
};
using InstanceDataMap = pxr::TfHashMap<pxr::SdfPath, std::unique_ptr<InstanceData>, pxr::SdfPath::Hash>;
} // namespace blender::render::hydra

View File

@ -13,6 +13,7 @@
#include "blenderSceneDelegate.h"
#include "mesh.h"
#include "../utils.h"
using namespace pxr;
@ -44,6 +45,9 @@ VtValue MeshData::get_data(TfToken const &key)
else if (key == HdPrimvarRoleTokens->textureCoordinate) {
ret = uvs;
}
else if (key == HdInstancerTokens->instanceTransform) {
ret = instances;
}
return ret;
}
@ -71,7 +75,7 @@ HdPrimvarDescriptorVector MeshData::primvar_descriptors(HdInterpolation interpol
}
}
else if (interpolation == HdInterpolationFaceVarying) {
if (!vertices.empty()) {
if (!normals.empty()) {
primvars.emplace_back(HdTokens->normals, interpolation, HdPrimvarRoleTokens->normal);
}
if (!uvs.empty()) {
@ -82,6 +86,59 @@ HdPrimvarDescriptorVector MeshData::primvar_descriptors(HdInterpolation interpol
return primvars;
}
HdPrimvarDescriptorVector MeshData::instancer_primvar_descriptors(HdInterpolation interpolation)
{
HdPrimvarDescriptorVector primvars;
if (interpolation == HdInterpolationInstance) {
primvars.emplace_back(HdInstancerTokens->instanceTransform, interpolation,
HdPrimvarRoleTokens->none);
}
return primvars;
}
VtIntArray MeshData::instance_indices()
{
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 maxSampleCount, float *sampleTimes, GfMatrix4d *sampleValues)
{
*sampleTimes = 0.0f;
*sampleValues = GfMatrix4d(1.0);
return 1;
}
size_t MeshData::sample_instancer_primvar(TfToken const &key, size_t maxSampleCount, float *sampleTimes, VtValue *sampleValues)
{
if (key == HdInstancerTokens->instanceTransform) {
if (maxSampleCount > 0) {
sampleTimes[0] = 0.0f;
sampleValues[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);
LOG(INFO) << "Add instancer: " << name() << " id=" << instancer_id.GetAsString();
}
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(GfMatrix4d(1.0));
}
instances.push_back(transform().GetInverse() * gf_matrix_from_transform(dupli->mat));
LOG(INFO) << "Add instance: " << instancer_id.GetAsString() << " " << dupli->random_id;
}
void MeshData::set_mesh(Mesh *mesh)
{
BKE_mesh_calc_normals_split(mesh);

View File

@ -6,6 +6,8 @@
#include <pxr/base/vt/array.h>
#include <pxr/imaging/hd/sceneDelegate.h>
#include "BKE_duplilist.h"
#include "object.h"
namespace blender::render::hydra {
@ -23,8 +25,15 @@ public:
Material *material();
pxr::HdMeshTopology mesh_topology();
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::HdPrimvarDescriptorVector instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
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 maxSampleCount, float *sampleTimes, pxr::VtValue *sampleValues);
void add_instance(DupliObject *dupli);
pxr::SdfPath material_id;
pxr::SdfPath instancer_id;
private:
void set_mesh(Mesh *mesh);
@ -34,6 +43,10 @@ public:
pxr::VtVec3fArray vertices;
pxr::VtVec3fArray normals;
pxr::VtVec2fArray uvs;
pxr::VtMatrix4dArray instances;
};
} // namespace blender::render::hydra