Rename some functions #25

Merged
Bogdan Nagirniak merged 13 commits from BLEN-388 into hydra-render 2023-04-18 09:07:35 +02:00
21 changed files with 801 additions and 804 deletions
Showing only changes of commit d04fc81610 - Show all commits

View File

@ -46,7 +46,7 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &delegate_id) : bl_eng
Engine::~Engine() Engine::~Engine()
{ {
scene_delegate = nullptr; scene_delegate_ = nullptr;
render_task_delegate = nullptr; render_task_delegate = nullptr;
free_camera_delegate = nullptr; free_camera_delegate = nullptr;
simple_light_task_delegate = nullptr; simple_light_task_delegate = nullptr;

View File

@ -38,12 +38,11 @@ class Engine {
protected: protected:
float renderer_percent_done(); float renderer_percent_done();
protected:
RenderEngine *bl_engine; RenderEngine *bl_engine;
pxr::HdPluginRenderDelegateUniqueHandle render_delegate; pxr::HdPluginRenderDelegateUniqueHandle render_delegate;
std::unique_ptr<pxr::HdRenderIndex> render_index; std::unique_ptr<pxr::HdRenderIndex> render_index;
std::unique_ptr<BlenderSceneDelegate> scene_delegate; std::unique_ptr<BlenderSceneDelegate> scene_delegate_;
std::unique_ptr<RenderTaskDelegate> render_task_delegate; std::unique_ptr<RenderTaskDelegate> render_task_delegate;
std::unique_ptr<pxr::HdxFreeCameraSceneDelegate> free_camera_delegate; std::unique_ptr<pxr::HdxFreeCameraSceneDelegate> free_camera_delegate;
std::unique_ptr<SimpleLightTaskDelegate> simple_light_task_delegate; std::unique_ptr<SimpleLightTaskDelegate> simple_light_task_delegate;

View File

@ -16,11 +16,11 @@ void FinalEngine::sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) pxr::HdRenderSettingsMap &render_settings)
{ {
scene_delegate = std::make_unique<BlenderSceneDelegate>( scene_delegate_ = std::make_unique<BlenderSceneDelegate>(
render_index.get(), render_index.get(),
pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::FINAL); BlenderSceneDelegate::EngineType::FINAL);
scene_delegate->populate(depsgraph, context); scene_delegate_->populate(depsgraph, context);
for (auto const &setting : render_settings) { for (auto const &setting : render_settings) {
render_delegate->SetRenderSetting(setting.first, setting.second); render_delegate->SetRenderSetting(setting.first, setting.second);

View File

@ -56,14 +56,14 @@ void PreviewEngine::sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) pxr::HdRenderSettingsMap &render_settings)
{ {
if (!scene_delegate) { if (!scene_delegate_) {
scene_delegate = std::make_unique<BlenderSceneDelegate>( scene_delegate_ = std::make_unique<BlenderSceneDelegate>(
render_index.get(), render_index.get(),
pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::PREVIEW); BlenderSceneDelegate::EngineType::PREVIEW);
} }
scene_delegate->clear(); scene_delegate_->clear();
scene_delegate->populate(depsgraph, context); scene_delegate_->populate(depsgraph, context);
for (auto const &setting : render_settings) { for (auto const &setting : render_settings) {
render_delegate->SetRenderSetting(setting.first, setting.second); render_delegate->SetRenderSetting(setting.first, setting.second);

View File

@ -18,29 +18,208 @@ BlenderSceneDelegate::BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
BlenderSceneDelegate::EngineType engine_type) BlenderSceneDelegate::EngineType engine_type)
: HdSceneDelegate(parent_index, delegate_id), : HdSceneDelegate(parent_index, delegate_id),
engine_type(engine_type), engine_type(engine_type),
depsgraph(nullptr), depsgraph_(nullptr),
context(nullptr), context_(nullptr),
view3d(nullptr) view3d_(nullptr)
{ {
} }
void BlenderSceneDelegate::update_world() void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
{ {
World *world = scene->world; bool is_populated = depsgraph_ != nullptr;
if (!world_data) {
if (world) { depsgraph_ = deps;
world_data = WorldData::create(this, world, context); context_ = cont;
scene_ = DEG_get_input_scene(depsgraph_);
view3d_ = CTX_wm_view3d(context_);
if (!is_populated) {
/* Export initial objects */
update_collection(false, false);
update_world();
return;
}
/* Working with updates */
bool do_update_collection = false;
bool do_update_visibility = false;
bool do_update_world = false;
unsigned int scene_recalc = ((ID *)scene_)->recalc;
if (scene_recalc) {
/* Checking scene updates */
CLOG_INFO(LOG_BSD,
2,
"Update: %s [%s]",
((ID *)scene_)->name,
std::bitset<32>(scene_recalc).to_string().c_str());
if (scene_recalc & ID_RECALC_BASE_FLAGS) {
do_update_visibility = true;
}
if (scene_recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) {
do_update_collection = true;
}
if (scene_recalc & ID_RECALC_AUDIO_VOLUME) {
if ((scene_->world && !world_data_) || (!scene_->world && world_data_)) {
do_update_world = true;
} }
} }
else { if (do_update_collection || do_update_visibility) {
if (world) { update_collection(do_update_collection, do_update_visibility);
world_data->update(world);
}
else {
world_data->remove();
world_data = nullptr;
} }
} }
/* Checking other objects updates */
DEGIDIterData data = {0};
data.graph = depsgraph_;
data.only_updated = true;
ITER_BEGIN (
DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) {
CLOG_INFO(
LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str());
switch (GS(id->name)) {
case ID_OB: {
Object *object = (Object *)id;
if (!ObjectData::is_supported(object)) {
break;
}
add_update_object(object);
} break;
case ID_MA: {
MaterialData *mat_data = get_material_data(
MaterialData::get_prim_id(this, (Material *)id));
if (mat_data) {
mat_data->update();
}
} break;
case ID_WO: {
if (id->recalc & ID_RECALC_SHADING) {
do_update_world = true;
}
} break;
default:
break;
}
}
ITER_END;
if (do_update_world) {
update_world();
}
}
void BlenderSceneDelegate::clear()
{
for (auto &it : materials_) {
it.second->remove();
}
for (auto &it : objects_) {
it.second->remove();
}
materials_.clear();
objects_.clear();
}
pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
MeshData *m_data = get_mesh_data(id);
return m_data->get_mesh_topology();
}
pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
ObjectData *obj_data = get_object_data(id);
if (obj_data) {
return obj_data->get_transform();
}
if (id == WorldData::get_prim_id(this)) {
return world_data_->get_transform();
}
InstancerData *i_data = get_instancer_data(id);
if (i_data) {
return i_data->get_transform();
}
return pxr::GfMatrix4d();
}
pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key)
{
CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText());
ObjectData *obj_data = get_object_data(id);
if (obj_data) {
return obj_data->get_data(key);
}
MaterialData *mat_data = get_material_data(id);
if (mat_data) {
return mat_data->get_data(key);
}
InstancerData *i_data = get_instancer_data(id);
if (i_data) {
return i_data->get_data(key);
}
return pxr::VtValue();
}
pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
pxr::TfToken const &key)
{
LightData *l_data = get_light_data(id);
if (l_data) {
return l_data->get_data(key);
}
if (id == WorldData::get_prim_id(this)) {
return world_data_->get_data(key);
}
return pxr::VtValue();
}
pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(
pxr::SdfPath const &id, pxr::HdInterpolation interpolation)
{
CLOG_INFO(LOG_BSD, 3, "%s, %d", id.GetText(), interpolation);
MeshData *m_data = get_mesh_data(id);
if (m_data) {
return m_data->get_primvar_descriptors(interpolation);
}
InstancerData *i_data = get_instancer_data(id);
if (i_data) {
return i_data->get_instancer_primvar_descriptors(interpolation);
}
return pxr::HdPrimvarDescriptorVector();
}
pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id)
{
return get_mesh_data(rprim_id)->get_material_id();
}
pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id)
{
MaterialData *mat_data = get_material_data(id);
if (mat_data) {
return mat_data->get_material_resource();
}
return pxr::VtValue();
} }
bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id) bool BlenderSceneDelegate::GetVisible(pxr::SdfPath const &id)
@ -85,12 +264,116 @@ pxr::GfMatrix4d BlenderSceneDelegate::GetInstancerTransform(pxr::SdfPath const &
return i_data->get_transform(); return i_data->get_transform();
} }
ObjectData *BlenderSceneDelegate::get_object_data(pxr::SdfPath const &id)
{
auto it = objects_.find(id);
if (it == objects_.end()) {
return nullptr;
}
return it->second.get();
}
MeshData *BlenderSceneDelegate::get_mesh_data(pxr::SdfPath const &id)
{
return dynamic_cast<MeshData *>(get_object_data(id));
}
LightData *BlenderSceneDelegate::get_light_data(pxr::SdfPath const &id)
{
return dynamic_cast<LightData *>(get_object_data(id));
}
MaterialData *BlenderSceneDelegate::get_material_data(pxr::SdfPath const &id)
{
auto it = materials_.find(id);
if (it == materials_.end()) {
return nullptr;
}
return it->second.get();
}
InstancerData *BlenderSceneDelegate::get_instancer_data(pxr::SdfPath const &id, bool base_prim)
{
if (base_prim) {
return dynamic_cast<InstancerData *>(get_object_data(id));
}
return dynamic_cast<InstancerData *>(get_object_data(id.GetParentPath()));
}
InstancerData *BlenderSceneDelegate::get_instancer_data(Object *object)
{
InstancerData *i_data;
for (auto &it : objects_) {
i_data = dynamic_cast<InstancerData *>(it.second.get());
if (i_data && i_data->is_base(object)) {
return i_data;
}
}
return nullptr;
}
void BlenderSceneDelegate::add_update_object(Object *object)
{
if ((object->transflag & OB_DUPLI) && InstancerData::is_supported(object)) {
add_update_instancer(object);
}
InstancerData *i_data = get_instancer_data(object);
if (i_data) {
i_data->update();
}
pxr::SdfPath id = ObjectData::get_prim_id(this, object);
ObjectData *obj_data = get_object_data(id);
if (obj_data) {
obj_data->update();
return;
}
if (view3d_ && !BKE_object_is_visible_in_viewport(view3d_, object)) {
return;
}
objects_[id] = ObjectData::create(this, object);
obj_data = get_object_data(id);
obj_data->update_visibility(view3d_);
}
void BlenderSceneDelegate::add_update_instancer(Object *object)
{
pxr::SdfPath id = InstancerData::get_prim_id(this, object);
InstancerData *i_data = get_instancer_data(id, true);
if (i_data) {
i_data->update();
return;
}
objects_[id] = InstancerData::create(this, object);
i_data = get_instancer_data(id, true);
i_data->update_visibility(view3d_);
}
void BlenderSceneDelegate::update_world()
{
World *world = scene_->world;
if (!world_data_) {
if (world) {
world_data_ = WorldData::create(this, world, context_);
}
}
else {
if (world) {
world_data_->update(world);
}
else {
world_data_->remove();
world_data_ = nullptr;
}
}
}
void BlenderSceneDelegate::update_collection(bool remove, bool visibility) void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
{ {
if (visibility) { if (visibility) {
/* Check and update visibility */ /* Check and update visibility */
for (auto &obj : objects) { for (auto &obj : objects_) {
obj.second->update_visibility(view3d); obj.second->update_visibility(view3d_);
} }
} }
@ -99,7 +382,7 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
pxr::SdfPath id; pxr::SdfPath id;
DEGObjectIterSettings settings = {0}; DEGObjectIterSettings settings = {0};
settings.depsgraph = depsgraph; settings.depsgraph = depsgraph_;
settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE | settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY | DEG_ITER_OBJECT_FLAG_VISIBLE |
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET; DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET;
DEGObjectIterData data = {0}; DEGObjectIterData data = {0};
@ -134,18 +417,18 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
if (remove) { if (remove) {
/* remove unused objects */ /* remove unused objects */
for (auto it = objects.begin(); it != objects.end(); ++it) { for (auto it = objects_.begin(); it != objects_.end(); ++it) {
if (available_objects.find(it->first) != available_objects.end()) { if (available_objects.find(it->first) != available_objects.end()) {
continue; continue;
} }
it->second->remove(); it->second->remove();
objects.erase(it); objects_.erase(it);
it = objects.begin(); it = objects_.begin();
} }
/* remove unused materials */ /* remove unused materials */
std::set<pxr::SdfPath> available_materials; std::set<pxr::SdfPath> available_materials;
for (auto &obj : objects) { for (auto &obj : objects_) {
MeshData *m_data = dynamic_cast<MeshData *>(obj.second.get()); MeshData *m_data = dynamic_cast<MeshData *>(obj.second.get());
if (!m_data) { if (!m_data) {
continue; continue;
@ -155,297 +438,15 @@ void BlenderSceneDelegate::update_collection(bool remove, bool visibility)
available_materials.insert(mat_id); available_materials.insert(mat_id);
} }
} }
for (auto it = materials.begin(); it != materials.end(); ++it) { for (auto it = materials_.begin(); it != materials_.end(); ++it) {
if (available_materials.find(it->first) != available_materials.end()) { if (available_materials.find(it->first) != available_materials.end()) {
continue; continue;
} }
it->second->remove(); it->second->remove();
materials.erase(it); materials_.erase(it);
it = materials.begin(); it = materials_.begin();
} }
} }
} }
void BlenderSceneDelegate::add_update_object(Object *object)
{
if ((object->transflag & OB_DUPLI) && InstancerData::is_supported(object)) {
add_update_instancer(object);
}
InstancerData *i_data = get_instancer_data(object);
if (i_data) {
i_data->update();
}
pxr::SdfPath id = ObjectData::get_prim_id(this, object);
ObjectData *obj_data = get_object_data(id);
if (obj_data) {
obj_data->update();
return;
}
if (view3d && !BKE_object_is_visible_in_viewport(view3d, object)) {
return;
}
objects[id] = ObjectData::create(this, object);
obj_data = get_object_data(id);
obj_data->update_visibility(view3d);
}
void BlenderSceneDelegate::add_update_instancer(Object *object)
{
pxr::SdfPath id = InstancerData::get_prim_id(this, object);
InstancerData *i_data = get_instancer_data(id, true);
if (i_data) {
i_data->update();
return;
}
objects[id] = InstancerData::create(this, object);
i_data = get_instancer_data(id, true);
i_data->update_visibility(view3d);
}
ObjectData *BlenderSceneDelegate::get_object_data(pxr::SdfPath const &id)
{
auto it = objects.find(id);
if (it == objects.end()) {
return nullptr;
}
return it->second.get();
}
MeshData *BlenderSceneDelegate::get_mesh_data(pxr::SdfPath const &id)
{
return dynamic_cast<MeshData *>(get_object_data(id));
}
LightData *BlenderSceneDelegate::get_light_data(pxr::SdfPath const &id)
{
return dynamic_cast<LightData *>(get_object_data(id));
}
MaterialData *BlenderSceneDelegate::get_material_data(pxr::SdfPath const &id)
{
auto it = materials.find(id);
if (it == materials.end()) {
return nullptr;
}
return it->second.get();
}
InstancerData *BlenderSceneDelegate::get_instancer_data(pxr::SdfPath const &id, bool base_prim)
{
if (base_prim) {
return dynamic_cast<InstancerData *>(get_object_data(id));
}
return dynamic_cast<InstancerData *>(get_object_data(id.GetParentPath()));
}
InstancerData *BlenderSceneDelegate::get_instancer_data(Object *object)
{
InstancerData *i_data;
for (auto &it : objects) {
i_data = dynamic_cast<InstancerData *>(it.second.get());
if (i_data && i_data->is_base(object)) {
return i_data;
}
}
return nullptr;
}
void BlenderSceneDelegate::populate(Depsgraph *deps, bContext *cont)
{
bool is_populated = depsgraph != nullptr;
depsgraph = deps;
context = cont;
scene = DEG_get_input_scene(depsgraph);
view3d = CTX_wm_view3d(context);
if (!is_populated) {
/* Export initial objects */
update_collection(false, false);
update_world();
return;
}
/* Working with updates */
bool do_update_collection = false;
bool do_update_visibility = false;
bool do_update_world = false;
unsigned int scene_recalc = ((ID *)scene)->recalc;
if (scene_recalc) {
/* Checking scene updates */
CLOG_INFO(LOG_BSD,
2,
"Update: %s [%s]",
((ID *)scene)->name,
std::bitset<32>(scene_recalc).to_string().c_str());
if (scene_recalc & ID_RECALC_BASE_FLAGS) {
do_update_visibility = true;
}
if (scene_recalc & (ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY)) {
do_update_collection = true;
}
if (scene_recalc & ID_RECALC_AUDIO_VOLUME) {
if ((scene->world && !world_data) || (!scene->world && world_data)) {
do_update_world = true;
}
}
if (do_update_collection || do_update_visibility) {
update_collection(do_update_collection, do_update_visibility);
}
}
/* Checking other objects updates */
DEGIDIterData data = {0};
data.graph = depsgraph;
data.only_updated = true;
ITER_BEGIN (
DEG_iterator_ids_begin, DEG_iterator_ids_next, DEG_iterator_ids_end, &data, ID *, id) {
CLOG_INFO(
LOG_BSD, 2, "Update: %s [%s]", id->name, std::bitset<32>(id->recalc).to_string().c_str());
switch (GS(id->name)) {
case ID_OB: {
Object *object = (Object *)id;
if (!ObjectData::is_supported(object)) {
break;
}
add_update_object(object);
} break;
case ID_MA: {
MaterialData *mat_data = get_material_data(MaterialData::get_prim_id(this, (Material *)id));
if (mat_data) {
mat_data->update();
}
} break;
case ID_WO: {
if (id->recalc & ID_RECALC_SHADING) {
do_update_world = true;
}
} break;
default:
break;
}
}
ITER_END;
if (do_update_world) {
update_world();
}
}
pxr::HdMeshTopology BlenderSceneDelegate::GetMeshTopology(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
MeshData *m_data = get_mesh_data(id);
return m_data->get_mesh_topology();
}
pxr::VtValue BlenderSceneDelegate::Get(pxr::SdfPath const &id, pxr::TfToken const &key)
{
CLOG_INFO(LOG_BSD, 3, "%s, %s", id.GetText(), key.GetText());
ObjectData *obj_data = get_object_data(id);
if (obj_data) {
return obj_data->get_data(key);
}
MaterialData *mat_data = get_material_data(id);
if (mat_data) {
return mat_data->get_data(key);
}
InstancerData *i_data = get_instancer_data(id);
if (i_data) {
return i_data->get_data(key);
}
return pxr::VtValue();
}
pxr::HdPrimvarDescriptorVector BlenderSceneDelegate::GetPrimvarDescriptors(
pxr::SdfPath const &id, pxr::HdInterpolation interpolation)
{
CLOG_INFO(LOG_BSD, 3, "%s, %d", id.GetText(), interpolation);
MeshData *m_data = get_mesh_data(id);
if (m_data) {
return m_data->get_primvar_descriptors(interpolation);
}
InstancerData *i_data = get_instancer_data(id);
if (i_data) {
return i_data->get_instancer_primvar_descriptors(interpolation);
}
return pxr::HdPrimvarDescriptorVector();
}
pxr::SdfPath BlenderSceneDelegate::GetMaterialId(pxr::SdfPath const &rprim_id)
{
return get_mesh_data(rprim_id)->get_material_id();
}
pxr::VtValue BlenderSceneDelegate::GetMaterialResource(pxr::SdfPath const &id)
{
MaterialData *mat_data = get_material_data(id);
if (mat_data) {
return mat_data->get_material_resource();
}
return pxr::VtValue();
}
pxr::GfMatrix4d BlenderSceneDelegate::GetTransform(pxr::SdfPath const &id)
{
CLOG_INFO(LOG_BSD, 3, "%s", id.GetText());
ObjectData *obj_data = get_object_data(id);
if (obj_data) {
return obj_data->get_transform();
}
if (id == WorldData::get_prim_id(this)) {
return world_data->get_transform();
}
InstancerData *i_data = get_instancer_data(id);
if (i_data) {
return i_data->get_transform();
}
return pxr::GfMatrix4d();
}
pxr::VtValue BlenderSceneDelegate::GetLightParamValue(pxr::SdfPath const &id,
pxr::TfToken const &key)
{
LightData *l_data = get_light_data(id);
if (l_data) {
return l_data->get_data(key);
}
if (id == WorldData::get_prim_id(this)) {
return world_data->get_data(key);
}
return pxr::VtValue();
}
void BlenderSceneDelegate::clear()
{
for (auto &it : materials) {
it.second->remove();
}
for (auto &it : objects) {
it.second->remove();
}
materials.clear();
objects.clear();
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -27,7 +27,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
public: public:
enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW };
BlenderSceneDelegate(pxr::HdRenderIndex *render_index, BlenderSceneDelegate(pxr::HdRenderIndex *parent_index,
pxr::SdfPath const &delegate_id, pxr::SdfPath const &delegate_id,
BlenderSceneDelegate::EngineType engine_type); BlenderSceneDelegate::EngineType engine_type);
~BlenderSceneDelegate() override = default; ~BlenderSceneDelegate() override = default;
@ -35,7 +35,7 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
void populate(Depsgraph *depsgraph, bContext *context); void populate(Depsgraph *depsgraph, bContext *context);
void clear(); void clear();
// delegate methods /* Delegate methods */
pxr::HdMeshTopology GetMeshTopology(pxr::SdfPath const &id) override; pxr::HdMeshTopology GetMeshTopology(pxr::SdfPath const &id) override;
pxr::GfMatrix4d GetTransform(pxr::SdfPath const &id) override; pxr::GfMatrix4d GetTransform(pxr::SdfPath const &id) override;
pxr::VtValue Get(pxr::SdfPath const &id, pxr::TfToken const &key) override; pxr::VtValue Get(pxr::SdfPath const &id, pxr::TfToken const &key) override;
@ -51,7 +51,6 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
pxr::SdfPath const &prototype_id) override; pxr::SdfPath const &prototype_id) override;
pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancer_id) override; pxr::GfMatrix4d GetInstancerTransform(pxr::SdfPath const &instancer_id) override;
public:
EngineType engine_type; EngineType engine_type;
private: private:
@ -67,15 +66,14 @@ class BlenderSceneDelegate : public pxr::HdSceneDelegate {
void update_world(); void update_world();
void update_collection(bool remove, bool visibility); void update_collection(bool remove, bool visibility);
private: Depsgraph *depsgraph_;
Depsgraph *depsgraph; bContext *context_;
bContext *context; Scene *scene_;
Scene *scene; View3D *view3d_;
DagerD marked this conversation as resolved Outdated

= nullptr

`= nullptr`
View3D *view3d;
ObjectDataMap objects; ObjectDataMap objects_;
MaterialDataMap materials; MaterialDataMap materials_;
std::unique_ptr<WorldData> world_data; std::unique_ptr<WorldData> world_data_;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -9,7 +9,7 @@
namespace blender::render::hydra { namespace blender::render::hydra {
IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id) IdData::IdData(BlenderSceneDelegate *scene_delegate, ID *id)
: scene_delegate(scene_delegate), id(id) : scene_delegate_(scene_delegate), id_(id)
{ {
} }

View File

@ -27,9 +27,9 @@ class IdData {
template<class T> const T get_data(pxr::TfToken const &key) const; template<class T> const T get_data(pxr::TfToken const &key) const;
protected: protected:
BlenderSceneDelegate *scene_delegate; BlenderSceneDelegate *scene_delegate_;
ID *id; ID *id_;
pxr::SdfPath p_id; pxr::SdfPath p_id_;
}; };
template<class T> const T IdData::get_data(pxr::TfToken const &key) const template<class T> const T IdData::get_data(pxr::TfToken const &key) const

View File

@ -9,6 +9,15 @@
namespace blender::render::hydra { namespace blender::render::hydra {
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object)
: MeshData(scene_delegate, object), parent_obj_(object)
{
id_ = nullptr;
p_id_ = get_prim_id(scene_delegate, object);
instancer_id = p_id_.AppendElementString("Instancer");
CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj_)->name, instancer_id.GetText());
}
bool InstancerData::is_supported(Object *object) bool InstancerData::is_supported(Object *object)
{ {
switch (object->type) { switch (object->type) {
@ -43,23 +52,75 @@ pxr::SdfPath InstancerData::get_prim_id(BlenderSceneDelegate *scene_delegate, Ob
return scene_delegate->GetDelegateID().AppendElementString(str); return scene_delegate->GetDelegateID().AppendElementString(str);
} }
InstancerData::InstancerData(BlenderSceneDelegate *scene_delegate, Object *object)
: MeshData(scene_delegate, object), parent_obj(object)
{
id = nullptr;
p_id = get_prim_id(scene_delegate, object);
instancer_id = p_id.AppendElementString("Instancer");
CLOG_INFO(LOG_BSD, 2, "%s, instancer_id=%s", ((ID *)parent_obj)->name, instancer_id.GetText());
}
void InstancerData::init() void InstancerData::init()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name); CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name);
set_instances(); set_instances();
MeshData::init(); MeshData::init();
} }
void InstancerData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name);
MeshData::insert();
if (face_vertex_counts_.empty()) {
return;
}
scene_delegate_->GetRenderIndex().InsertInstancer(scene_delegate_, instancer_id);
}
void InstancerData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name);
if (!scene_delegate_->GetRenderIndex().HasInstancer(instancer_id)) {
return;
}
scene_delegate_->GetRenderIndex().RemoveInstancer(instancer_id);
MeshData::remove();
}
void InstancerData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj_)->name);
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
unsigned int recalc = ((ID *)parent_obj_)->recalc;
Object *object = (Object *)id_;
if ((id_->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id_, pxr::HdChangeTracker::AllDirty);
return;
}
if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj_->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
bits |= pxr::HdChangeTracker::AllDirty;
}
else if (recalc & ID_RECALC_TRANSFORM || id_->recalc & ID_RECALC_TRANSFORM) {
set_instances();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits);
}
}
pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const
{
CLOG_INFO(LOG_BSD, 3, "%s [%s]", id_->name, key.GetText());
if (key == pxr::HdInstancerTokens->instanceTransform) {
return pxr::VtValue(transforms_);
}
return MeshData::get_data(key);
}
pxr::GfMatrix4d InstancerData::get_transform() pxr::GfMatrix4d InstancerData::get_transform()
{ {
return pxr::GfMatrix4d(1.0); return pxr::GfMatrix4d(1.0);
@ -72,25 +133,15 @@ bool InstancerData::update_visibility(View3D *view3d)
} }
bool prev_visible = visible; bool prev_visible = visible;
visible = BKE_object_is_visible_in_viewport(view3d, parent_obj); visible = BKE_object_is_visible_in_viewport(view3d, parent_obj_);
bool ret = visible != prev_visible; bool ret = visible != prev_visible;
if (ret) { if (ret) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty( scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id, pxr::HdChangeTracker::DirtyVisibility); p_id_, pxr::HdChangeTracker::DirtyVisibility);
} }
return ret; return ret;
} }
pxr::VtValue InstancerData::get_data(pxr::TfToken const &key) const
{
CLOG_INFO(LOG_BSD, 3, "%s [%s]", id->name, key.GetText());
if (key == pxr::HdInstancerTokens->instanceTransform) {
return pxr::VtValue(transforms);
}
return MeshData::get_data(key);
}
pxr::HdPrimvarDescriptorVector InstancerData::get_instancer_primvar_descriptors( pxr::HdPrimvarDescriptorVector InstancerData::get_instancer_primvar_descriptors(
pxr::HdInterpolation interpolation) pxr::HdInterpolation interpolation)
{ {
@ -104,7 +155,7 @@ pxr::HdPrimvarDescriptorVector InstancerData::get_instancer_primvar_descriptors(
pxr::VtIntArray InstancerData::get_instance_indices() pxr::VtIntArray InstancerData::get_instance_indices()
{ {
pxr::VtIntArray ret(transforms.size()); pxr::VtIntArray ret(transforms_.size());
for (size_t i = 0; i < ret.size(); ++i) { for (size_t i = 0; i < ret.size(); ++i) {
ret[i] = i; ret[i] = i;
} }
@ -113,82 +164,31 @@ pxr::VtIntArray InstancerData::get_instance_indices()
bool InstancerData::is_base(Object *object) const bool InstancerData::is_base(Object *object) const
{ {
return (ID *)object == id; return (ID *)object == id_;
} }
bool InstancerData::set_instances() bool InstancerData::set_instances()
{ {
ID *prev_id = id; ID *prev_id = id_;
id = nullptr; id_ = nullptr;
transforms.clear(); transforms_.clear();
ListBase *lb = object_duplilist(scene_delegate->depsgraph, scene_delegate->scene, parent_obj); ListBase *lb = object_duplilist(scene_delegate_->depsgraph_, scene_delegate_->scene_, parent_obj_);
LISTBASE_FOREACH (DupliObject *, dupli, lb) { LISTBASE_FOREACH (DupliObject *, dupli, lb) {
if (!id) { if (!id_) {
/* TODO: We create instances only for object in first dupli. /* TODO: We create instances only for object in first dupli.
Instances should be created for all objects */ Instances should be created for all objects */
id = (ID *)dupli->ob; id_ = (ID *)dupli->ob;
} }
if (id != (ID *)dupli->ob) { if (id_ != (ID *)dupli->ob) {
continue; continue;
} }
transforms.push_back(gf_matrix_from_transform(dupli->mat)); transforms_.push_back(gf_matrix_from_transform(dupli->mat));
CLOG_INFO( CLOG_INFO(
LOG_BSD, 2, "Instance %s (%s) %d", id->name, ((ID *)dupli->ob)->name, dupli->random_id); LOG_BSD, 2, "Instance %s (%s) %d", id_->name, ((ID *)dupli->ob)->name, dupli->random_id);
} }
free_object_duplilist(lb); free_object_duplilist(lb);
return id != prev_id; return id_ != prev_id;
}
void InstancerData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name);
MeshData::insert();
if (face_vertex_counts.empty()) {
return;
}
scene_delegate->GetRenderIndex().InsertInstancer(scene_delegate, instancer_id);
}
void InstancerData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name);
if (!scene_delegate->GetRenderIndex().HasInstancer(instancer_id)) {
return;
}
scene_delegate->GetRenderIndex().RemoveInstancer(instancer_id);
MeshData::remove();
}
void InstancerData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", ((ID *)parent_obj)->name);
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
unsigned int recalc = ((ID *)parent_obj)->recalc;
Object *object = (Object *)id;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id, pxr::HdChangeTracker::AllDirty);
return;
}
if ((recalc & ID_RECALC_GEOMETRY) || (((ID *)parent_obj->data)->recalc & ID_RECALC_GEOMETRY)) {
init();
bits |= pxr::HdChangeTracker::AllDirty;
}
else if (recalc & ID_RECALC_TRANSFORM || id->recalc & ID_RECALC_TRANSFORM) {
set_instances();
bits |= pxr::HdChangeTracker::DirtyTransform;
}
if (bits != pxr::HdChangeTracker::Clean) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(instancer_id, bits);
}
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -11,13 +11,13 @@ namespace blender::render::hydra {
class InstancerData : public MeshData { class InstancerData : public MeshData {
public: public:
InstancerData(BlenderSceneDelegate *scene_delegate, Object *object);
static bool is_supported(Object *object); static bool is_supported(Object *object);
static std::unique_ptr<InstancerData> create(BlenderSceneDelegate *scene_delegate, static std::unique_ptr<InstancerData> create(BlenderSceneDelegate *scene_delegate,
Object *object); Object *object);
static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate, Object *object);
InstancerData(BlenderSceneDelegate *scene_delegate, Object *object);
void init() override; void init() override;
void insert() override; void insert() override;
void remove() override; void remove() override;
@ -29,15 +29,15 @@ class InstancerData : public MeshData {
pxr::HdPrimvarDescriptorVector get_instancer_primvar_descriptors(pxr::HdInterpolation interpolation); pxr::HdPrimvarDescriptorVector get_instancer_primvar_descriptors(pxr::HdInterpolation interpolation);
pxr::VtIntArray get_instance_indices(); pxr::VtIntArray get_instance_indices();
bool is_base(Object *object) const; bool is_base(Object *object) const;
pxr::SdfPath instancer_id; pxr::SdfPath instancer_id;
private: private:
Object *parent_obj;
pxr::VtMatrix4dArray transforms;
bool set_instances(); bool set_instances();
Object *parent_obj_;
pxr::VtMatrix4dArray transforms_;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -17,72 +17,138 @@ namespace blender::render::hydra {
LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object) LightData::LightData(BlenderSceneDelegate *scene_delegate, Object *object)
: ObjectData(scene_delegate, object) : ObjectData(scene_delegate, object)
{ {
CLOG_INFO(LOG_BSD, 2, "%s id=%s", id->name, p_id.GetText()); CLOG_INFO(LOG_BSD, 2, "%s id=%s", id_->name, p_id_.GetText());
} }
void LightData::init() void LightData::init()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
Light *light = (Light *)((Object *)id)->data; Light *light = (Light *)((Object *)id_)->data;
data.clear(); data_.clear();
float intensity = light->energy; float intensity = light->energy;
if (scene_delegate->engine_type == BlenderSceneDelegate::EngineType::PREVIEW) { if (scene_delegate_->engine_type == BlenderSceneDelegate::EngineType::PREVIEW) {
intensity *= 0.001; intensity *= 0.001;
} }
data[pxr::HdLightTokens->intensity] = intensity; data_[pxr::HdLightTokens->intensity] = intensity;
data[pxr::HdLightTokens->color] = pxr::GfVec3f(light->r, light->g, light->b); data_[pxr::HdLightTokens->color] = pxr::GfVec3f(light->r, light->g, light->b);
switch (light->type) { switch (light->type) {
case LA_LOCAL: case LA_LOCAL:
data[pxr::HdLightTokens->radius] = light->area_size / 2; data_[pxr::HdLightTokens->radius] = light->area_size / 2;
break; break;
case LA_SUN: case LA_SUN:
data[pxr::HdLightTokens->angle] = light->sun_angle * 180.0 / M_PI; data_[pxr::HdLightTokens->angle] = light->sun_angle * 180.0 / M_PI;
break; break;
case LA_SPOT: case LA_SPOT:
data[pxr::HdLightTokens->shapingConeAngle] = light->spotsize / 2; data_[pxr::HdLightTokens->shapingConeAngle] = light->spotsize / 2;
data[pxr::HdLightTokens->shapingConeSoftness] = light->spotblend; data_[pxr::HdLightTokens->shapingConeSoftness] = light->spotblend;
data[pxr::UsdLuxTokens->treatAsPoint] = true; data_[pxr::UsdLuxTokens->treatAsPoint] = true;
break; break;
case LA_AREA: case LA_AREA:
switch (light->area_shape) { switch (light->area_shape) {
case LA_AREA_SQUARE: case LA_AREA_SQUARE:
data[pxr::HdLightTokens->width] = light->area_size; data_[pxr::HdLightTokens->width] = light->area_size;
data[pxr::HdLightTokens->height] = light->area_size; data_[pxr::HdLightTokens->height] = light->area_size;
break; break;
case LA_AREA_RECT: case LA_AREA_RECT:
data[pxr::HdLightTokens->width] = light->area_size; data_[pxr::HdLightTokens->width] = light->area_size;
data[pxr::HdLightTokens->height] = light->area_sizey; data_[pxr::HdLightTokens->height] = light->area_sizey;
break; break;
case LA_AREA_DISK: case LA_AREA_DISK:
data[pxr::HdLightTokens->radius] = light->area_size / 2; data_[pxr::HdLightTokens->radius] = light->area_size / 2;
break; break;
case LA_AREA_ELLIPSE: case LA_AREA_ELLIPSE:
data[pxr::HdLightTokens->radius] = (light->area_size + light->area_sizey) / 4; data_[pxr::HdLightTokens->radius] = (light->area_size + light->area_sizey) / 4;
break; break;
default: default:
break; break;
} }
data[pxr::HdLightTokens->normalize] = true; data_[pxr::HdLightTokens->normalize] = true;
break; break;
default: default:
break; break;
} }
p_type = get_prim_type(light); p_type_ = get_prim_type(light);
/* TODO: temporary value, it should be delivered through Python UI */ /* TODO: temporary value, it should be delivered through Python UI */
data[pxr::HdLightTokens->exposure] = 1.0f; data_[pxr::HdLightTokens->exposure] = 1.0f;
}
void LightData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate_->GetRenderIndex().InsertSprim(p_type_, scene_delegate_, p_id_);
}
void LightData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate_->GetRenderIndex().RemoveSprim(p_type_, p_id_);
}
void LightData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
Light *light = (Light *)((Object *)id_)->data;
if (get_prim_type(light) != p_type_) {
remove();
init();
insert();
return;
}
pxr::HdDirtyBits bits = pxr::HdLight::Clean;
if (id_->recalc & ID_RECALC_GEOMETRY) {
init();
bits = pxr::HdLight::AllDirty;
}
else if (id_->recalc & ID_RECALC_TRANSFORM) {
bits = pxr::HdLight::DirtyTransform;
}
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_, bits);
}
pxr::VtValue LightData::get_data(pxr::TfToken const &key) const
{
pxr::VtValue ret;
auto it = data_.find(key);
if (it != data_.end()) {
ret = it->second;
}
else {
std::string n = key.GetString();
if (boost::algorithm::contains(n, "object:visibility:")) {
if (boost::algorithm::ends_with(n, "camera") || boost::algorithm::ends_with(n, "shadow")) {
ret = false;
}
else {
ret = true;
}
}
}
return ret;
}
bool LightData::update_visibility(View3D *view3d)
{
bool ret = ObjectData::update_visibility(view3d);
if (ret) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_,
pxr::HdLight::DirtyParams);
}
return ret;
} }
pxr::TfToken LightData::get_prim_type(Light *light) pxr::TfToken LightData::get_prim_type(Light *light)
@ -121,70 +187,4 @@ pxr::TfToken LightData::get_prim_type(Light *light)
return ret; return ret;
} }
pxr::VtValue LightData::get_data(pxr::TfToken const &key) const
{
pxr::VtValue ret;
auto it = data.find(key);
if (it != data.end()) {
ret = it->second;
}
else {
std::string n = key.GetString();
if (boost::algorithm::contains(n, "object:visibility:")) {
if (boost::algorithm::ends_with(n, "camera") || boost::algorithm::ends_with(n, "shadow")) {
ret = false;
}
else {
ret = true;
}
}
}
return ret;
}
bool LightData::update_visibility(View3D *view3d)
{
bool ret = ObjectData::update_visibility(view3d);
if (ret) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id,
pxr::HdLight::DirtyParams);
}
return ret;
}
void LightData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertSprim(p_type, scene_delegate, p_id);
}
void LightData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveSprim(p_type, p_id);
}
void LightData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
Light *light = (Light *)((Object *)id)->data;
if (get_prim_type(light) != p_type) {
remove();
init();
insert();
return;
}
pxr::HdDirtyBits bits = pxr::HdLight::Clean;
if (id->recalc & ID_RECALC_GEOMETRY) {
init();
bits = pxr::HdLight::AllDirty;
}
else if (id->recalc & ID_RECALC_TRANSFORM) {
bits = pxr::HdLight::DirtyTransform;
}
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, bits);
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -28,9 +28,8 @@ class LightData : public ObjectData {
private: private:
pxr::TfToken get_prim_type(Light *light); pxr::TfToken get_prim_type(Light *light);
private: std::map<pxr::TfToken, pxr::VtValue> data_;
std::map<pxr::TfToken, pxr::VtValue> data; pxr::TfToken p_type_;
pxr::TfToken p_type;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -20,6 +20,13 @@
namespace blender::render::hydra { namespace blender::render::hydra {
MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material)
: IdData(scene_delegate, (ID *)material)
{
p_id_ = get_prim_id(scene_delegate, material);
CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id_->name, p_id_.GetText());
}
std::unique_ptr<MaterialData> MaterialData::create(BlenderSceneDelegate *scene_delegate, std::unique_ptr<MaterialData> MaterialData::create(BlenderSceneDelegate *scene_delegate,
Material *material) Material *material)
{ {
@ -38,18 +45,11 @@ pxr::SdfPath MaterialData::get_prim_id(BlenderSceneDelegate *scene_delegate, Mat
return scene_delegate->GetDelegateID().AppendElementString(str); return scene_delegate->GetDelegateID().AppendElementString(str);
} }
MaterialData::MaterialData(BlenderSceneDelegate *scene_delegate, Material *material)
: IdData(scene_delegate, (ID *)material)
{
p_id = get_prim_id(scene_delegate, material);
CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText());
}
void MaterialData::init() void MaterialData::init()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
material_network_map = pxr::VtValue(); material_network_map_ = pxr::VtValue();
/* Call of python function hydra.export_mtlx() */ /* Call of python function hydra.export_mtlx() */
@ -62,7 +62,7 @@ void MaterialData::init()
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, id_, &materialptr);
PyObject *material = pyrna_struct_CreatePyObject(&materialptr); PyObject *material = pyrna_struct_CreatePyObject(&materialptr);
result = PyObject_CallFunction(func, "O", material); result = PyObject_CallFunction(func, "O", material);
@ -76,23 +76,44 @@ void MaterialData::init()
Py_DECREF(result); Py_DECREF(result);
} }
else { else {
CLOG_ERROR(LOG_BSD, "Export error for %s", id->name); CLOG_ERROR(LOG_BSD, "Export error for %s", id_->name);
PyErr_Print(); PyErr_Print();
} }
Py_DECREF(module); Py_DECREF(module);
PyGILState_Release(gstate); PyGILState_Release(gstate);
mtlx_path = pxr::SdfAssetPath(path, path); mtlx_path_ = pxr::SdfAssetPath(path, path);
CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", id->name, mtlx_path.GetResolvedPath().c_str()); CLOG_INFO(LOG_BSD, 2, "Export: %s, mtlx=%s", id_->name, mtlx_path_.GetResolvedPath().c_str());
}
void MaterialData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate_->GetRenderIndex().InsertSprim(
pxr::HdPrimTypeTokens->material, scene_delegate_, p_id_);
}
void MaterialData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id_);
}
void MaterialData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
init();
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_,
pxr::HdMaterial::AllDirty);
} }
pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const
{ {
pxr::VtValue ret; pxr::VtValue ret;
if (key.GetString() == "MaterialXFilename") { if (key.GetString() == "MaterialXFilename") {
if (!mtlx_path.GetResolvedPath().empty()) { if (!mtlx_path_.GetResolvedPath().empty()) {
ret = mtlx_path; ret = mtlx_path_;
} }
CLOG_INFO(LOG_BSD, 3, "%s", key.GetText()); CLOG_INFO(LOG_BSD, 3, "%s", key.GetText());
} }
@ -101,42 +122,21 @@ pxr::VtValue MaterialData::get_data(pxr::TfToken const &key) const
pxr::VtValue MaterialData::get_material_resource() pxr::VtValue MaterialData::get_material_resource()
{ {
if (material_network_map.IsEmpty()) { if (material_network_map_.IsEmpty()) {
const std::string &path = mtlx_path.GetResolvedPath(); const std::string &path = mtlx_path_.GetResolvedPath();
if (!path.empty()) { if (!path.empty()) {
pxr::HdRenderDelegate *render_delegate = pxr::HdRenderDelegate *render_delegate =
scene_delegate->GetRenderIndex().GetRenderDelegate(); scene_delegate_->GetRenderIndex().GetRenderDelegate();
pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes(); pxr::TfTokenVector shader_source_types = render_delegate->GetShaderSourceTypes();
pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts(); pxr::TfTokenVector render_contexts = render_delegate->GetMaterialRenderContexts();
pxr::HdMaterialNetworkMap network_map; pxr::HdMaterialNetworkMap network_map;
hdmtlx_convert_to_materialnetworkmap(path, shader_source_types, render_contexts, &network_map); hdmtlx_convert_to_materialnetworkmap(path, shader_source_types, render_contexts, &network_map);
material_network_map = network_map; material_network_map_ = network_map;
} }
} }
return material_network_map; return material_network_map_;
}
void MaterialData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertSprim(
pxr::HdPrimTypeTokens->material, scene_delegate, p_id);
}
void MaterialData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->material, p_id);
}
void MaterialData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
init();
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id,
pxr::HdMaterial::AllDirty);
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -19,12 +19,12 @@ class MaterialData : IdData {
friend MeshData; friend MeshData;
public: public:
MaterialData(BlenderSceneDelegate *scene_delegate, Material *material);
static std::unique_ptr<MaterialData> create(BlenderSceneDelegate *scene_delegate, static std::unique_ptr<MaterialData> create(BlenderSceneDelegate *scene_delegate,
Material *material); Material *material);
static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate, Material *material); static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate, Material *material);
MaterialData(BlenderSceneDelegate *scene_delegate, Material *material);
void init() override; void init() override;
void insert() override; void insert() override;
void remove() override; void remove() override;
@ -34,8 +34,8 @@ class MaterialData : IdData {
pxr::VtValue get_material_resource(); pxr::VtValue get_material_resource();
private: private:
pxr::SdfAssetPath mtlx_path; pxr::SdfAssetPath mtlx_path_;
pxr::VtValue material_network_map; pxr::VtValue material_network_map_;
}; };
using MaterialDataMap = using MaterialDataMap =

View File

@ -16,169 +16,16 @@
namespace blender::render::hydra { namespace blender::render::hydra {
MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object) MeshData::MeshData(BlenderSceneDelegate *scene_delegate, Object *object)
: ObjectData(scene_delegate, object), mat_data(nullptr) : ObjectData(scene_delegate, object), mat_data_(nullptr)
{ {
CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText()); CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id_->name, p_id_.GetText());
}
pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const
{
pxr::VtValue ret;
if (key == pxr::HdTokens->points) {
ret = vertices;
}
else if (key == pxr::HdTokens->normals) {
ret = normals;
}
else if (key == pxr::HdPrimvarRoleTokens->textureCoordinate) {
ret = uvs;
}
else if (key == pxr::HdInstancerTokens->instanceTransform) {
ret = instances;
}
return ret;
}
bool MeshData::update_visibility(View3D *view3d)
{
bool ret = ObjectData::update_visibility(view3d);
if (ret) {
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id, pxr::HdChangeTracker::DirtyVisibility);
}
return ret;
}
pxr::HdMeshTopology MeshData::get_mesh_topology()
{
return pxr::HdMeshTopology(pxr::PxOsdOpenSubdivTokens->none,
pxr::HdTokens->rightHanded,
face_vertex_counts,
face_vertex_indices);
}
pxr::HdPrimvarDescriptorVector MeshData::get_primvar_descriptors(pxr::HdInterpolation interpolation)
{
pxr::HdPrimvarDescriptorVector primvars;
if (interpolation == pxr::HdInterpolationVertex) {
if (!vertices.empty()) {
primvars.emplace_back(pxr::HdTokens->points, interpolation, pxr::HdPrimvarRoleTokens->point);
}
}
else if (interpolation == pxr::HdInterpolationFaceVarying) {
if (!normals.empty()) {
primvars.emplace_back(
pxr::HdTokens->normals, interpolation, pxr::HdPrimvarRoleTokens->normal);
}
if (!uvs.empty()) {
primvars.emplace_back(pxr::HdPrimvarRoleTokens->textureCoordinate,
interpolation,
pxr::HdPrimvarRoleTokens->textureCoordinate);
}
}
return primvars;
}
pxr::SdfPath MeshData::get_material_id()
{
if (!mat_data) {
return pxr::SdfPath();
}
return mat_data->p_id;
}
void MeshData::write_mesh(Mesh *mesh)
{
face_vertex_counts.clear();
face_vertex_indices.clear();
vertices.clear();
normals.clear();
uvs.clear();
BKE_mesh_calc_normals_split(mesh);
int tris_len = BKE_mesh_runtime_looptri_len(mesh);
if (tris_len == 0) {
return;
}
blender::Span<MLoopTri> loopTris = mesh->looptris();
/* face_vertex_counts */
face_vertex_counts = pxr::VtIntArray(tris_len, 3);
/* face_vertex_indices */
blender::Span<int> corner_verts = mesh->corner_verts();
face_vertex_indices.reserve(loopTris.size() * 3);
for (MLoopTri lt : loopTris) {
face_vertex_indices.push_back(corner_verts[lt.tri[0]]);
face_vertex_indices.push_back(corner_verts[lt.tri[1]]);
face_vertex_indices.push_back(corner_verts[lt.tri[2]]);
}
/* vertices */
vertices.reserve(mesh->totvert);
blender::Span<blender::float3> verts = mesh->vert_positions();
for (blender::float3 v : verts) {
vertices.push_back(pxr::GfVec3f(v.x, v.y, v.z));
}
write_normals(mesh);
write_uv_maps(mesh);
}
void MeshData::write_material()
{
Object *object = (Object *)id;
Material *mat = nullptr;
if (BKE_object_material_count_eval(object) > 0) {
mat = BKE_object_material_get_eval(object, object->actcol);
}
if (!mat) {
mat_data = nullptr;
return;
}
pxr::SdfPath id = MaterialData::get_prim_id(scene_delegate, mat);
mat_data = scene_delegate->get_material_data(id);
if (!mat_data) {
scene_delegate->materials[id] = MaterialData::create(scene_delegate, mat);
mat_data = scene_delegate->get_material_data(id);
}
}
void MeshData::write_uv_maps(Mesh *mesh)
{
blender::Span<MLoopTri> loopTris = mesh->looptris();
const float(*luvs)[2] = (float(*)[2])CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2);
if (luvs) {
uvs.reserve(loopTris.size() * 3);
for (MLoopTri lt : loopTris) {
uvs.push_back(pxr::GfVec2f(luvs[lt.tri[0]]));
uvs.push_back(pxr::GfVec2f(luvs[lt.tri[1]]));
uvs.push_back(pxr::GfVec2f(luvs[lt.tri[2]]));
}
}
}
void MeshData::write_normals(Mesh *mesh)
{
blender::Span<MLoopTri> loopTris = mesh->looptris();
const float(*lnors)[3] = (float(*)[3])CustomData_get_layer(&mesh->ldata, CD_NORMAL);
if (lnors) {
normals.reserve(loopTris.size() * 3);
for (MLoopTri lt : loopTris) {
normals.push_back(pxr::GfVec3f(lnors[lt.tri[0]]));
normals.push_back(pxr::GfVec3f(lnors[lt.tri[1]]));
normals.push_back(pxr::GfVec3f(lnors[lt.tri[2]]));
}
}
} }
void MeshData::init() void MeshData::init()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
Object *object = (Object *)id; Object *object = (Object *)id_;
if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT && if (object->type == OB_MESH && object->mode == OB_MODE_OBJECT &&
BLI_listbase_is_empty(&object->modifiers)) { BLI_listbase_is_empty(&object->modifiers)) {
write_mesh((Mesh *)object->data); write_mesh((Mesh *)object->data);
@ -196,38 +43,39 @@ void MeshData::init()
void MeshData::insert() void MeshData::insert()
{ {
if (face_vertex_counts.empty()) { if (face_vertex_counts_.empty()) {
return; return;
} }
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate->GetRenderIndex().InsertRprim(pxr::HdPrimTypeTokens->mesh, scene_delegate, p_id); scene_delegate_->GetRenderIndex().InsertRprim(
pxr::HdPrimTypeTokens->mesh, scene_delegate_, p_id_);
} }
void MeshData::remove() void MeshData::remove()
{ {
if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { if (!scene_delegate_->GetRenderIndex().HasRprim(p_id_)) {
return; return;
} }
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate->GetRenderIndex().RemoveRprim(p_id); scene_delegate_->GetRenderIndex().RemoveRprim(p_id_);
} }
void MeshData::update() 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)) {
init(); init();
bits = pxr::HdChangeTracker::AllDirty; bits = pxr::HdChangeTracker::AllDirty;
} }
else { else {
if (id->recalc & ID_RECALC_SHADING) { if (id_->recalc & ID_RECALC_SHADING) {
write_material(); write_material();
bits |= pxr::HdChangeTracker::DirtyMaterialId; bits |= pxr::HdChangeTracker::DirtyMaterialId;
} }
if (id->recalc & ID_RECALC_TRANSFORM) { if (id_->recalc & ID_RECALC_TRANSFORM) {
bits |= pxr::HdChangeTracker::DirtyTransform; bits |= pxr::HdChangeTracker::DirtyTransform;
} }
} }
@ -236,18 +84,171 @@ void MeshData::update()
return; return;
} }
if (!scene_delegate->GetRenderIndex().HasRprim(p_id)) { if (!scene_delegate_->GetRenderIndex().HasRprim(p_id_)) {
insert(); insert();
return; return;
} }
if (face_vertex_counts.empty()) { if (face_vertex_counts_.empty()) {
/* Remove prim without faces */ /* Remove prim without faces */
remove(); remove();
return; return;
} }
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id, bits); scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(p_id_, bits);
}
pxr::VtValue MeshData::get_data(pxr::TfToken const &key) const
{
pxr::VtValue ret;
if (key == pxr::HdTokens->points) {
ret = vertices_;
}
else if (key == pxr::HdTokens->normals) {
ret = normals_;
}
else if (key == pxr::HdPrimvarRoleTokens->textureCoordinate) {
ret = uvs_;
}
else if (key == pxr::HdInstancerTokens->instanceTransform) {
ret = instances_;
}
return ret;
}
bool MeshData::update_visibility(View3D *view3d)
{
bool ret = ObjectData::update_visibility(view3d);
if (ret) {
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkRprimDirty(
p_id_, pxr::HdChangeTracker::DirtyVisibility);
}
return ret;
}
pxr::HdMeshTopology MeshData::get_mesh_topology()
{
return pxr::HdMeshTopology(pxr::PxOsdOpenSubdivTokens->none,
pxr::HdTokens->rightHanded,
face_vertex_counts_,
face_vertex_indices_);
}
pxr::HdPrimvarDescriptorVector MeshData::get_primvar_descriptors(pxr::HdInterpolation interpolation)
{
pxr::HdPrimvarDescriptorVector primvars;
if (interpolation == pxr::HdInterpolationVertex) {
if (!vertices_.empty()) {
primvars.emplace_back(pxr::HdTokens->points, interpolation, pxr::HdPrimvarRoleTokens->point);
}
}
else if (interpolation == pxr::HdInterpolationFaceVarying) {
if (!normals_.empty()) {
primvars.emplace_back(
pxr::HdTokens->normals, interpolation, pxr::HdPrimvarRoleTokens->normal);
}
if (!uvs_.empty()) {
primvars.emplace_back(pxr::HdPrimvarRoleTokens->textureCoordinate,
interpolation,
pxr::HdPrimvarRoleTokens->textureCoordinate);
}
}
return primvars;
}
pxr::SdfPath MeshData::get_material_id()
{
if (!mat_data_) {
return pxr::SdfPath();
}
return mat_data_->p_id_;
}
void MeshData::write_mesh(Mesh *mesh)
{
face_vertex_counts_.clear();
face_vertex_indices_.clear();
vertices_.clear();
normals_.clear();
uvs_.clear();
BKE_mesh_calc_normals_split(mesh);
int tris_len = BKE_mesh_runtime_looptri_len(mesh);
if (tris_len == 0) {
return;
}
blender::Span<MLoopTri> loopTris = mesh->looptris();
/* face_vertex_counts */
face_vertex_counts_ = pxr::VtIntArray(tris_len, 3);
/* face_vertex_indices */
blender::Span<int> corner_verts = mesh->corner_verts();
face_vertex_indices_.reserve(loopTris.size() * 3);
for (MLoopTri lt : loopTris) {
face_vertex_indices_.push_back(corner_verts[lt.tri[0]]);
face_vertex_indices_.push_back(corner_verts[lt.tri[1]]);
face_vertex_indices_.push_back(corner_verts[lt.tri[2]]);
}
/* vertices */
vertices_.reserve(mesh->totvert);
blender::Span<blender::float3> verts = mesh->vert_positions();
for (blender::float3 v : verts) {
vertices_.push_back(pxr::GfVec3f(v.x, v.y, v.z));
}
write_normals(mesh);
write_uv_maps(mesh);
}
void MeshData::write_material()
{
Object *object = (Object *)id_;
Material *mat = nullptr;
if (BKE_object_material_count_eval(object) > 0) {
mat = BKE_object_material_get_eval(object, object->actcol);
}
if (!mat) {
mat_data_ = nullptr;
return;
}
pxr::SdfPath id = MaterialData::get_prim_id(scene_delegate_, mat);
mat_data_ = scene_delegate_->get_material_data(id);
if (!mat_data_) {
scene_delegate_->materials_[id] = MaterialData::create(scene_delegate_, mat);
mat_data_ = scene_delegate_->get_material_data(id);
}
}
void MeshData::write_uv_maps(Mesh *mesh)
{
blender::Span<MLoopTri> loopTris = mesh->looptris();
const float(*luvs)[2] = (float(*)[2])CustomData_get_layer(&mesh->ldata, CD_PROP_FLOAT2);
if (luvs) {
uvs_.reserve(loopTris.size() * 3);
for (MLoopTri lt : loopTris) {
uvs_.push_back(pxr::GfVec2f(luvs[lt.tri[0]]));
uvs_.push_back(pxr::GfVec2f(luvs[lt.tri[1]]));
uvs_.push_back(pxr::GfVec2f(luvs[lt.tri[2]]));
}
}
}
void MeshData::write_normals(Mesh *mesh)
{
blender::Span<MLoopTri> loopTris = mesh->looptris();
const float(*lnors)[3] = (float(*)[3])CustomData_get_layer(&mesh->ldata, CD_NORMAL);
if (lnors) {
normals_.reserve(loopTris.size() * 3);
for (MLoopTri lt : loopTris) {
normals_.push_back(pxr::GfVec3f(lnors[lt.tri[0]]));
normals_.push_back(pxr::GfVec3f(lnors[lt.tri[1]]));
normals_.push_back(pxr::GfVec3f(lnors[lt.tri[2]]));
}
}
} }
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -35,15 +35,14 @@ class MeshData : public ObjectData {
void write_uv_maps(Mesh *mesh); void write_uv_maps(Mesh *mesh);
void write_normals(Mesh *mesh); void write_normals(Mesh *mesh);
protected: pxr::VtIntArray face_vertex_counts_;
pxr::VtIntArray face_vertex_counts; pxr::VtIntArray face_vertex_indices_;
pxr::VtIntArray face_vertex_indices; pxr::VtVec3fArray vertices_;
pxr::VtVec3fArray vertices; pxr::VtVec2fArray uvs_;
pxr::VtVec2fArray uvs; pxr::VtVec3fArray normals_;
pxr::VtVec3fArray normals; pxr::VtMatrix4dArray instances_;
pxr::VtMatrix4dArray instances;
MaterialData *mat_data; MaterialData *mat_data_;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -11,6 +11,12 @@
namespace blender::render::hydra { namespace blender::render::hydra {
ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object)
: IdData(scene_delegate, (ID *)object), visible(true)
{
p_id_ = get_prim_id(scene_delegate, object);
}
bool ObjectData::is_supported(Object *object) bool ObjectData::is_supported(Object *object)
{ {
switch (object->type) { switch (object->type) {
@ -65,15 +71,9 @@ pxr::SdfPath ObjectData::get_prim_id(BlenderSceneDelegate *scene_delegate, Objec
return scene_delegate->GetDelegateID().AppendElementString(str); return scene_delegate->GetDelegateID().AppendElementString(str);
} }
ObjectData::ObjectData(BlenderSceneDelegate *scene_delegate, Object *object)
: IdData(scene_delegate, (ID *)object), visible(true)
{
p_id = get_prim_id(scene_delegate, object);
}
pxr::GfMatrix4d ObjectData::get_transform() pxr::GfMatrix4d ObjectData::get_transform()
{ {
return gf_matrix_from_transform(((Object *)id)->object_to_world); return gf_matrix_from_transform(((Object *)id_)->object_to_world);
} }
bool ObjectData::update_visibility(View3D *view3d) bool ObjectData::update_visibility(View3D *view3d)
@ -83,7 +83,7 @@ bool ObjectData::update_visibility(View3D *view3d)
} }
bool prev_visible = visible; bool prev_visible = visible;
visible = BKE_object_is_visible_in_viewport(view3d, (Object *)id); visible = BKE_object_is_visible_in_viewport(view3d, (Object *)id_);
return visible != prev_visible; return visible != prev_visible;
} }

View File

@ -16,16 +16,15 @@ namespace blender::render::hydra {
class ObjectData : public IdData { class ObjectData : public IdData {
public: public:
ObjectData(BlenderSceneDelegate *scene_delegate, Object *object);
static bool is_supported(Object *object); static bool is_supported(Object *object);
static std::unique_ptr<ObjectData> create(BlenderSceneDelegate *scene_delegate, Object *object); static std::unique_ptr<ObjectData> create(BlenderSceneDelegate *scene_delegate, Object *object);
static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate, Object *object); static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate, Object *object);
ObjectData(BlenderSceneDelegate *scene_delegate, Object *object);
virtual pxr::GfMatrix4d get_transform(); virtual pxr::GfMatrix4d get_transform();
virtual bool update_visibility(View3D *view3d); virtual bool update_visibility(View3D *view3d);
public:
bool visible; bool visible;
}; };

View File

@ -28,6 +28,13 @@
namespace blender::render::hydra { namespace blender::render::hydra {
WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context)
: IdData(scene_delegate, (ID *)world), context_(context)
{
p_id_ = get_prim_id(scene_delegate);
CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id_->name, p_id_.GetText());
}
std::unique_ptr<WorldData> WorldData::create(BlenderSceneDelegate *scene_delegate, std::unique_ptr<WorldData> WorldData::create(BlenderSceneDelegate *scene_delegate,
World *world, World *world,
bContext *context) bContext *context)
@ -43,21 +50,14 @@ pxr::SdfPath WorldData::get_prim_id(BlenderSceneDelegate *scene_delegate)
return scene_delegate->GetDelegateID().AppendElementString("World"); return scene_delegate->GetDelegateID().AppendElementString("World");
} }
WorldData::WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context)
: IdData(scene_delegate, (ID *)world), context(context)
{
p_id = get_prim_id(scene_delegate);
CLOG_INFO(LOG_BSD, 2, "%s, id=%s", id->name, p_id.GetText());
}
void WorldData::init() void WorldData::init()
{ {
CLOG_INFO(LOG_BSD, 2, "%s", id->name); CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
World *world = (World *)id; World *world = (World *)id_;
data.clear(); data_.clear();
data[pxr::UsdLuxTokens->orientToStageUpAxis] = true; data_[pxr::UsdLuxTokens->orientToStageUpAxis] = true;
if (world->use_nodes) { if (world->use_nodes) {
/* TODO: Create nodes parsing system */ /* TODO: Create nodes parsing system */
@ -90,9 +90,9 @@ void WorldData::init()
float const *strength = strength_input.default_value_typed<float>(); float const *strength = strength_input.default_value_typed<float>();
float const *color = color_input.default_value_typed<float>(); float const *color = color_input.default_value_typed<float>();
data[pxr::HdLightTokens->intensity] = strength[1]; data_[pxr::HdLightTokens->intensity] = strength[1];
data[pxr::HdLightTokens->exposure] = 1.0f; data_[pxr::HdLightTokens->exposure] = 1.0f;
data[pxr::HdLightTokens->color] = pxr::GfVec3f(color[0], color[1], color[2]); data_[pxr::HdLightTokens->color] = pxr::GfVec3f(color[0], color[1], color[2]);
if (!color_input.directly_linked_links().is_empty()) { if (!color_input.directly_linked_links().is_empty()) {
bNode *color_input_node = color_input.directly_linked_links()[0]->fromnode; bNode *color_input_node = color_input.directly_linked_links()[0]->fromnode;
@ -101,8 +101,8 @@ void WorldData::init()
Image *image = (Image *)color_input_node->id; Image *image = (Image *)color_input_node->id;
if (image) { if (image) {
Main *bmain = CTX_data_main(context); Main *bmain = CTX_data_main(context_);
Scene *scene = CTX_data_scene(context); Scene *scene = CTX_data_scene(context_);
ReportList reports; ReportList reports;
ImageSaveOptions opts; ImageSaveOptions opts;
@ -110,26 +110,53 @@ void WorldData::init()
std::string image_path = cache_image(bmain, scene, image, &tex->iuser, &opts, &reports); std::string image_path = cache_image(bmain, scene, image, &tex->iuser, &opts, &reports);
if (!image_path.empty()) { if (!image_path.empty()) {
data[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path); data_[pxr::HdLightTokens->textureFile] = pxr::SdfAssetPath(image_path, image_path);
} }
} }
} }
} }
} }
else { else {
data[pxr::HdLightTokens->intensity] = 1.0f; data_[pxr::HdLightTokens->intensity] = 1.0f;
data[pxr::HdLightTokens->exposure] = world->exposure; data_[pxr::HdLightTokens->exposure] = world->exposure;
data[pxr::HdLightTokens->color] = pxr::GfVec3f(world->horr, world->horg, world->horb); data_[pxr::HdLightTokens->color] = pxr::GfVec3f(world->horr, world->horg, world->horb);
} }
} }
void WorldData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate_->GetRenderIndex().InsertSprim(
pxr::HdPrimTypeTokens->domeLight, scene_delegate_, p_id_);
}
void WorldData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
scene_delegate_->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id_);
}
void WorldData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", id_->name);
init();
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id_,
pxr::HdLight::AllDirty);
}
void WorldData::update(World *world)
{
id_ = (ID *)world;
update();
}
pxr::GfMatrix4d WorldData::get_transform() pxr::GfMatrix4d WorldData::get_transform()
{ {
pxr::GfMatrix4d transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90), pxr::GfMatrix4d transform = pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90),
pxr::GfVec3d()); pxr::GfVec3d());
/* TODO : do this check via RenderSettings*/ /* TODO : do this check via RenderSettings*/
if (scene_delegate->GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName() == "RPR") { if (scene_delegate_->GetRenderIndex().GetRenderDelegate()->GetRendererDisplayName() == "RPR") {
transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180), transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -180),
pxr::GfVec3d()); pxr::GfVec3d());
transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0), transform *= pxr::GfMatrix4d(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0),
@ -141,37 +168,11 @@ pxr::GfMatrix4d WorldData::get_transform()
pxr::VtValue WorldData::get_data(pxr::TfToken const &key) const pxr::VtValue WorldData::get_data(pxr::TfToken const &key) const
{ {
pxr::VtValue ret; pxr::VtValue ret;
auto it = data.find(key); auto it = data_.find(key);
if (it != data.end()) { if (it != data_.end()) {
ret = it->second; ret = it->second;
} }
return ret; return ret;
} }
void WorldData::insert()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().InsertSprim(
pxr::HdPrimTypeTokens->domeLight, scene_delegate, p_id);
}
void WorldData::remove()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
scene_delegate->GetRenderIndex().RemoveSprim(pxr::HdPrimTypeTokens->domeLight, p_id);
}
void WorldData::update()
{
CLOG_INFO(LOG_BSD, 2, "%s", id->name);
init();
scene_delegate->GetRenderIndex().GetChangeTracker().MarkSprimDirty(p_id, pxr::HdLight::AllDirty);
}
void WorldData::update(World *world)
{
id = (ID *)world;
update();
}
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -20,13 +20,13 @@ namespace blender::render::hydra {
class WorldData : public IdData { class WorldData : public IdData {
public: public:
WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context);
static std::unique_ptr<WorldData> create(BlenderSceneDelegate *scene_delegate, static std::unique_ptr<WorldData> create(BlenderSceneDelegate *scene_delegate,
World *world, World *world,
bContext *context); bContext *context);
static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate); static pxr::SdfPath get_prim_id(BlenderSceneDelegate *scene_delegate);
WorldData(BlenderSceneDelegate *scene_delegate, World *world, bContext *context);
void init() override; void init() override;
void insert() override; void insert() override;
void remove() override; void remove() override;
@ -37,8 +37,8 @@ class WorldData : public IdData {
pxr::VtValue get_data(pxr::TfToken const &key) const override; pxr::VtValue get_data(pxr::TfToken const &key) const override;
private: private:
std::map<pxr::TfToken, pxr::VtValue> data; std::map<pxr::TfToken, pxr::VtValue> data_;
bContext *context; bContext *context_;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra

View File

@ -217,13 +217,13 @@ void ViewportEngine::sync(Depsgraph *depsgraph,
bContext *context, bContext *context,
pxr::HdRenderSettingsMap &render_settings) pxr::HdRenderSettingsMap &render_settings)
{ {
if (!scene_delegate) { if (!scene_delegate_) {
scene_delegate = std::make_unique<BlenderSceneDelegate>( scene_delegate_ = std::make_unique<BlenderSceneDelegate>(
render_index.get(), render_index.get(),
pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"), pxr::SdfPath::AbsoluteRootPath().AppendElementString("scene"),
BlenderSceneDelegate::EngineType::VIEWPORT); BlenderSceneDelegate::EngineType::VIEWPORT);
} }
scene_delegate->populate(depsgraph, context); scene_delegate_->populate(depsgraph, context);
for (auto const &setting : render_settings) { for (auto const &setting : render_settings) {
render_delegate->SetRenderSetting(setting.first, setting.second); render_delegate->SetRenderSetting(setting.first, setting.second);