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 67 additions and 175 deletions
Showing only changes of commit 31873bbf9e - Show all commits

View File

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

View File

@ -78,30 +78,12 @@ bool BlenderSceneDelegate::GetVisible(SdfPath const &id)
return object_data(id)->visible;
}
GfRange3d BlenderSceneDelegate::GetExtent(SdfPath const &id)
{
LOG(INFO) << "GetExtent: " << id.GetString();
return GfRange3d();
}
bool BlenderSceneDelegate::GetDoubleSided(SdfPath const &id)
{
LOG(INFO) << "GetDoubleSided: " << id.GetString();
return false;
}
HdCullStyle BlenderSceneDelegate::GetCullStyle(SdfPath const &id)
{
LOG(INFO) << "GetCullStyle: " << id.GetString();
return HdCullStyle();
}
SdfPath BlenderSceneDelegate::GetInstancerId(SdfPath const &primId)
{
ObjectData *obj_data = object_data(primId);
LOG(INFO) << "GetInstancerId: " << primId.GetAsString();
if (obj_data && obj_data->name() == "Cube") {
return GetDelegateID().AppendElementString("Instancer");
MeshData *m_data = mesh_data(primId);
if (m_data) {
return m_data->instancer_id;
}
return SdfPath();
}
@ -110,8 +92,7 @@ SdfPathVector BlenderSceneDelegate::GetInstancerPrototypes(SdfPath const &instan
{
LOG(INFO) << "GetInstancerPrototypes: " << instancerId.GetString();
SdfPathVector paths;
paths.push_back(GetDelegateID().AppendElementString("Inst1"));
paths.push_back(GetDelegateID().AppendElementString("Inst2"));
paths.push_back(instancerId.GetParentPath());
return paths;
}
@ -119,7 +100,19 @@ VtIntArray BlenderSceneDelegate::GetInstanceIndices(SdfPath const &instancerId,
SdfPath const &prototypeId)
{
LOG(INFO) << "GetInstanceIndices: " << instancerId.GetString() << " " << prototypeId.GetString();
return VtIntArray();
MeshData *m_data = mesh_data(prototypeId);
VtIntArray ret = m_data->instance_indices();
return ret;
}
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;
}
void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
@ -238,18 +231,13 @@ void BlenderSceneDelegate::add_update_object(Object *object, bool geometry, bool
void BlenderSceneDelegate::add_update_instance(DupliObject *dupli, bool transform)
{
SdfPath id = InstanceData::prim_id(this, dupli);
InstanceData *i_data = instance_data(id);
if (!i_data) {
instances[id] = InstanceData::init(this, dupli);
i_data = instance_data(id);
i_data->insert_prim();
return;
SdfPath id = ObjectData::prim_id(this, dupli->ob);
if (!object_data(id)) {
add_update_object(dupli->ob, true, true, true);
}
if (transform) {
i_data->mark_prim_dirty(IdData::DirtyBits::DirtyTransform);
}
MeshData *m_data = mesh_data(id);
m_data->add_instance(dupli);
}
ObjectData *BlenderSceneDelegate::object_data(SdfPath const &id)
@ -280,15 +268,6 @@ MaterialData *BlenderSceneDelegate::material_data(SdfPath const &id)
return it->second.get();
}
InstanceData *BlenderSceneDelegate::instance_data(SdfPath const &id)
{
auto it = instances.find(id);
if (it == instances.end()) {
return nullptr;
}
return it->second.get();
}
void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
{
bool is_populated = depsgraph != nullptr;

View File

@ -12,7 +12,6 @@
#include "mesh.h"
#include "light.h"
#include "world.h"
#include "instance.h"
namespace blender::render::hydra {
@ -32,19 +31,17 @@ 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::GfRange3d GetExtent(pxr::SdfPath const &id) override;
bool GetDoubleSided(pxr::SdfPath const &id) override;
pxr::HdCullStyle GetCullStyle(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;
size_t SampleInstancerTransform(pxr::SdfPath const &instancerId, size_t maxSampleCount,
float *sampleTimes, pxr::GfMatrix4d *sampleValues) override;
private:
ObjectData *object_data(pxr::SdfPath const &id);
MeshData *mesh_data(pxr::SdfPath const &id);
LightData *light_data(pxr::SdfPath const &id);
MaterialData *material_data(pxr::SdfPath const &id);
InstanceData *instance_data(pxr::SdfPath const &id);
void add_update_object(Object *object, bool geometry, bool transform, bool shading);
void add_update_instance(DupliObject *dupli, bool transform);
@ -61,7 +58,6 @@ private:
ObjectDataMap objects;
MaterialDataMap materials;
std::unique_ptr<WorldData> world_data;
InstanceDataMap instances;
};
} // namespace blender::render::hydra

View File

@ -1,88 +0,0 @@
/* SPDX-License-Identifier: Apache-2.0
* Copyright 2011-2022 Blender Foundation */
#include <pxr/imaging/hd/instancer.h>
#include "glog/logging.h"
#include "../utils.h"
#include "instance.h"
using namespace pxr;
namespace blender::render::hydra {
std::unique_ptr<InstanceData> InstanceData::init(pxr::HdSceneDelegate *scene_delegate,
DupliObject *dupli)
{
return std::make_unique<InstanceData>(scene_delegate, dupli);
}
pxr::SdfPath InstanceData::prim_id(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli)
{
char str[16];
snprintf(str, 16, "I_%08lx", dupli->random_id);
return scene_delegate->GetDelegateID().AppendElementString(str);
}
InstanceData::InstanceData(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli)
: IdData(scene_delegate, (ID *)dupli->ob)
, dupli(dupli)
{
}
pxr::GfMatrix4d InstanceData::transform()
{
SdfPath p_id = prim_id(scene_delegate, dupli);
LOG(INFO) << "Transform instance: " << name() << " " << p_id.GetName();
return gf_matrix_from_transform(dupli->mat);
}
VtValue InstanceData::get_data(TfToken const &key)
{
SdfPath p_id = prim_id(scene_delegate, dupli);
LOG(INFO) << "Get data instance: " << name() << " " << p_id.GetName()
<< " [" << key.GetString() << "]";
return VtValue();
}
void InstanceData::insert_prim()
{
SdfPath p_id = prim_id(scene_delegate, dupli);
scene_delegate->GetRenderIndex().InsertSprim(HdPrimTypeTokens->instance, scene_delegate, p_id);
LOG(INFO) << "Add instance: " << name() << " id=" << p_id.GetAsString();
}
void InstanceData::remove_prim()
{
SdfPath p_id = prim_id(scene_delegate, dupli);
scene_delegate->GetRenderIndex().RemoveSprim(HdPrimTypeTokens->instance, p_id);
LOG(INFO) << "Remove instance: " << name() << " id=" << p_id.GetAsString();
}
void InstanceData::mark_prim_dirty(DirtyBits dirty_bits)
{
SdfPath p_id = prim_id(scene_delegate, dupli);
HdDirtyBits bits = HdChangeTracker::Clean;
switch (dirty_bits) {
case DirtyBits::DirtyTransform:
bits = HdChangeTracker::DirtyTransform;
break;
case DirtyBits::DirtyVisibility:
bits = HdChangeTracker::DirtyVisibility;
break;
case DirtyBits::DirtyMaterial:
bits = HdChangeTracker::DirtyMaterialId;
break;
case DirtyBits::AllDirty:
bits = HdChangeTracker::AllDirty;
break;
default:
break;
}
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits);
LOG(INFO) << "Update mesh: " << name() << " [" << (int)dirty_bits << "]";
}
} // namespace blender::render::hydra

View File

@ -1,34 +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:
static std::unique_ptr<InstanceData> init(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli);
static pxr::SdfPath prim_id(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli);
InstanceData(pxr::HdSceneDelegate *scene_delegate, DupliObject *dupli);
pxr::GfMatrix4d transform();
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

@ -12,6 +12,7 @@
#include "BKE_material.h"
#include "mesh.h"
#include "../utils.h"
using namespace pxr;
@ -70,7 +71,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()) {
@ -81,6 +82,36 @@ HdPrimvarDescriptorVector MeshData::primvar_descriptors(HdInterpolation interpol
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)
{
size_t n = std::min(instances.size(), maxSampleCount);
for (size_t i = 0; i < n; ++i) {
sampleTimes[i] = 0.0f;
sampleValues[i] = gf_matrix_from_transform(instances[i]->mat);
}
return n;
}
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();
}
instances.push_back(dupli);
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,6 +25,10 @@ public:
Material *material();
pxr::HdMeshTopology mesh_topology();
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray instance_indices();
size_t sample_instancer_transform(size_t maxSampleCount, float *sampleTimes, pxr::GfMatrix4d *sampleValues);
void add_instance(DupliObject *dupli);
pxr::SdfPath material_id;
pxr::SdfPath instancer_id;
@ -35,6 +41,10 @@ public:
pxr::VtVec3fArray vertices;
pxr::VtVec3fArray normals;
pxr::VtVec2fArray uvs;
std::vector<DupliObject *> instances;
};
} // namespace blender::render::hydra