Blender V5.3
scene_index.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "scene_index.hh"
6
7#include <pxr/imaging/hd/light.h>
8#include <pxr/imaging/hd/material.h>
9#include <pxr/imaging/hd/renderDelegate.h>
10#include <pxr/imaging/hd/retainedDataSource.h>
11#include <pxr/imaging/hd/tokens.h>
12#include <pxr/imaging/hd/xformSchema.h>
13
14#include "BLI_utildefines.hh"
15
16#include "BKE_layer.hh"
17#include "BKE_object.hh"
18
20
21#include "DNA_ID.h"
22#include "DNA_material_types.h"
23#include "DNA_node_types.h"
24#include "DNA_object_types.h"
25#include "DNA_scene_types.h"
26#include "DNA_world_types.h"
27
28#include "curves.hh"
29#include "instancer.hh"
30#include "light.hh"
31#include "mesh.hh"
32#include "populate_context.hh"
33#include "util.hh"
34#include "volume.hh"
35#include "volume_modifier.hh"
36#include "world.hh"
37
38namespace blender::io::hydra {
39
41
42bool HydraSceneIndex::object_is_supported(const Object *object) const
43{
44 switch (object->type) {
45 case OB_MESH:
46 case OB_SURF:
47 case OB_FONT:
49 case OB_MBALL:
50 case OB_LAMP:
51 case OB_CURVES:
52 case OB_VOLUME:
53 return true;
54 default:
55 return false;
56 }
57}
58
59static bool object_render_as_geometry(const Object *object)
60{
62}
63
64bool HydraSceneIndex::object_is_visible(Depsgraph *depsgraph,
65 const View3D *view3d,
66 const Object *object,
67 const int mode) const
68{
69 const eEvaluationMode deg_mode = DEG_get_mode(depsgraph);
70 const bool is_visible = BKE_object_visibility(object, deg_mode) & mode;
71 return (deg_mode == DAG_EVAL_VIEWPORT) ?
72 is_visible && BKE_object_is_visible_in_viewport(view3d, object) :
73 is_visible;
74}
75
76HydraSceneIndex::HydraSceneIndex(const pxr::SdfPath &root_path,
77 pxr::HdRenderDelegate *render_delegate,
78 const bool use_materialx)
79 : retained_(pxr::HdRetainedSceneIndex::New()),
80 root_path_(root_path),
81 render_delegate_(render_delegate),
82 use_materialx_(use_materialx)
83{
84 if (render_delegate_) {
85 material_render_contexts_ = render_delegate_->GetMaterialRenderContexts();
86 shader_source_types_ = render_delegate_->GetShaderSourceTypes();
87 }
88}
89
94
95void HydraSceneIndex::populate(Depsgraph *depsgraph, View3D *view3d)
96{
97 Main *bmain = DEG_get_bmain(depsgraph);
98 Scene *scene = DEG_get_evaluated_scene(depsgraph);
99
100 /* Gather recalc flags. Note that we can not check recalc flags on pointers
101 * in emitted object and material maps, as those datablocks might have been
102 * deleted in the meantime. */
103 Map<const ID *, int> id_recalc;
104 {
105 DEGIDIterData iter_data = {nullptr};
106 iter_data.graph = depsgraph;
107 iter_data.only_updated = true;
108 ITER_BEGIN (
110 {
111 id_recalc.add(id, id->recalc);
112 }
113 ITER_END;
114 }
115 auto has_recalc = [&](const ID *id, const int flag) {
116 const int *recalc = id_recalc.lookup_ptr(id);
117 return recalc && (*recalc & flag) != 0;
118 };
119
120 /* Drop materials marked for update, we'll recreate them. */
121 emitted_materials_.remove_if([&](const auto &item) {
122 return has_recalc(&item.key->id, ID_RECALC_ALL) ||
123 has_recalc(&item.key->nodetree->id, ID_RECALC_ALL);
124 });
125
126 /* Find geometry marked for update. */
127 Set<const ID *> dirty_instance_geometries;
128 for (const auto &item : instance_geometries_by_object_.items()) {
129 if (has_recalc(&item.key->id, ID_RECALC_GEOMETRY)) {
130 for (const ID *geom : item.value) {
131 dirty_instance_geometries.add(geom);
132 }
133 }
134 }
135
136 /* Drop emitted geometry that was updated. */
137 emitted_geometry_.remove_if([&](const auto &item) {
138 return has_recalc(item.key.id, ID_RECALC_GEOMETRY) ||
139 dirty_instance_geometries.contains(item.key.id);
140 });
141
142 PopulateContext ctx(depsgraph,
143 bmain,
144 scene,
145 view3d,
146 root_path_,
147 use_materialx_,
148 Span<pxr::TfToken>(material_render_contexts_),
149 Span<pxr::TfToken>(shader_source_types_),
150 emitted_materials_,
151 emitted_geometry_,
152 instance_geometries_by_object_,
153 emitted_paths_);
154
155 DEGObjectIterSettings settings = {nullptr};
156 settings.depsgraph = depsgraph;
158 DEGObjectIterData data = {nullptr};
159 data.settings = &settings;
160 data.graph = settings.depsgraph;
161 data.flag = settings.flags;
162
166 &data,
167 Object *,
168 object)
169 {
170 /* TODO: backface culling is on material in Blender but on geometry in USD.
171 * Currently we are not correctly updating geometry for changes to that. */
172 const bool updated_geometry = (object->id.recalc & ID_RECALC_GEOMETRY) != 0 ||
173 (object->data && object->data->recalc != 0);
174 const bool updated_transform = (object->id.recalc & ID_RECALC_TRANSFORM) != 0;
175
176 /* Remove outdated geometry emitted by this object. */
177 if (updated_geometry && !data.dupli_object_current) {
178 if (const EmittedObject *prev = emitted_objects_.lookup_ptr(object)) {
179 for (const EmittedGeometryKey &k : prev->geometry_keys) {
180 emitted_geometry_.remove(k);
181 }
182 }
183 }
184
185 if (!data.dupli_object_current && !updated_geometry) {
186 if (EmittedObject *cached = emitted_objects_.lookup_ptr(object)) {
187 if (!updated_transform) {
188 /* Reuse the previous emission as-is. */
189 for (const EmittedObjectPrim &item : cached->prims) {
190 ctx.new_paths.add(item.path);
191 }
192 for (const Material *mat : cached->materials) {
193 ctx.get_or_create_material(mat);
194 }
195 for (const EmittedGeometryKey &k : cached->geometry_keys) {
196 ctx.used_emitted_geometry.add(k);
197 }
198 ctx.new_emitted_objects.add(object, *cached);
199 continue;
200 }
201 /* Fast transform overlay and re-compose for transform only update. */
202 const pxr::GfMatrix4d obj_xform = gf_matrix_from_transform(
203 object->object_to_world().ptr());
204 for (EmittedObjectPrim &item : cached->prims) {
205 if (!item.base_data_source) {
206 continue;
207 }
208 const pxr::GfMatrix4d new_xform = item.geometry_xform * obj_xform;
209 item.xform_overlay = pxr::HdRetainedContainerDataSource::New(
210 pxr::HdXformSchema::GetSchemaToken(),
211 pxr::HdXformSchema::Builder()
212 .SetMatrix(
213 pxr::HdRetainedTypedSampledDataSource<pxr::GfMatrix4d>::New(new_xform))
214 .SetResetXformStack(pxr::HdRetainedTypedSampledDataSource<bool>::New(false))
215 .Build());
216 ctx.new_paths.add(item.path);
217 ctx.to_add.push_back({item.path, item.type, item.compose()});
218 ctx.to_dirty.push_back({item.path, pxr::HdXformSchema::GetDefaultLocator()});
219 }
220 for (const Material *mat : cached->materials) {
221 ctx.get_or_create_material(mat);
222 }
223 for (const EmittedGeometryKey &k : cached->geometry_keys) {
224 ctx.used_emitted_geometry.add(k);
225 }
226 ctx.new_emitted_objects.add(object, *cached);
227 continue;
228 }
229 }
230
231 if (data.dupli_object_current) {
232 /* Note `object` can be temporary on the stack, here we get the stable pointer. */
233 Object *source = data.dupli_object_current->ob;
234 if (!DEG_iterator_dupli_is_visible(data.dupli_object_current, DEG_get_mode(depsgraph))) {
235 continue;
236 }
237
238 /* Lights and volumes aren't gprims and don't support instancer prototypes.
239 * So we emit a new prim per dupli. */
240 if (source->type == OB_LAMP) {
241 emit_light_dupli(ctx, source, data.dupli_object_current->mat);
242 continue;
243 }
244 if (source->type == OB_VOLUME) {
245 emit_volume_dupli(ctx, source, data.dupli_object_current->mat);
246 continue;
247 }
248
249 if (!object_render_as_geometry(source)) {
250 continue;
251 }
252
253 /* Unique object info that works for geometry sets too. */
254 const BObjectInfo info{object, source, object->data};
255
256 /* Emit prototype once. */
257 if (!ctx.proto_range_by_source.contains(source)) {
258 const int proto_start = ctx.all_proto_paths.size();
259 if (source->type == OB_CURVES) {
260 emit_curves_proto(ctx, info);
261 }
262 else {
263 emit_mesh_proto(ctx, info);
264 emit_hair_proto(ctx, source);
265 }
266 ctx.proto_range_by_source.add(
267 source, IndexRange(proto_start, ctx.all_proto_paths.size() - proto_start));
268 }
269
270 ctx.add_instance(source, data.dupli_object_current->mat);
271 continue;
272 }
273
274 if (!object_is_supported(object)) {
275 continue;
276 }
277
278 EmittedObject emitted;
279
280 /* Emit hair particles separately. */
281 emit_hair_for_object(ctx, object, emitted);
282
283 if (!object_is_visible(depsgraph, view3d, object, OB_VISIBLE_SELF)) {
284 if (!emitted.prims.is_empty()) {
285 ctx.new_emitted_objects.add(object, std::move(emitted));
286 }
287 continue;
288 }
289
290 const BObjectInfo info{object, object, object->data};
291 if (object->type == OB_LAMP) {
292 emit_light_object(ctx, object, emitted);
293 }
294 else if (emit_volume_object(ctx, object, emitted)) {
295 /* Emitted as volume prim instead. */
296 }
297 else if (object->type == OB_CURVES) {
298 emit_curves_object(ctx, info, emitted);
299 }
300 else {
301 emit_mesh_object(ctx, info, emitted);
302 }
303
304 if (!emitted.prims.is_empty()) {
305 ctx.new_emitted_objects.add(object, std::move(emitted));
306 }
307 }
308 ITER_END;
309
310 /* Emit instancer prims. */
311 if (!ctx.all_proto_paths.is_empty() && !ctx.instance_transforms.empty()) {
312 const pxr::SdfPath instancer_path = ctx.instancer_prim_id();
313 pxr::HdContainerDataSourceHandle prim_ds = build_instancer_prim_data_source(
314 ctx.all_proto_paths.as_span(), ctx.per_proto_indices.as_span(), ctx.instance_transforms);
315 ctx.emit_prim(instancer_path, pxr::HdPrimTypeTokens->instancer, prim_ds);
316 }
317
318 /* Drop unused materials, geometry, instances. */
319 emitted_materials_.remove_if(
320 [&](const auto &item) { return !ctx.used_emitted_materials.contains(item.key); });
321 emitted_geometry_.remove_if(
322 [&](const auto &item) { return !ctx.used_emitted_geometry.contains(item.key); });
323 instance_geometries_by_object_.remove_if(
324 [&](const auto &item) { return !ctx.used_instance_sources.contains(item.key); });
325
326 /* Keep used and newly added material prims. */
327 for (const auto &item : emitted_materials_.items()) {
328 if (item.value.data_source) {
329 ctx.new_paths.add(item.value.path);
330 }
331 }
332 for (const Material *material : ctx.new_emitted_materials) {
333 const EmittedMaterial *entry = emitted_materials_.lookup_ptr(material);
334 if (entry && entry->data_source) {
335 ctx.emit_prim(entry->path, pxr::HdPrimTypeTokens->material, entry->data_source);
336 }
337 }
338
339 /* Emit world. */
340 {
341 const pxr::SdfPath world_path = root_path_.AppendElementString("World");
342 const bool world_shading_changed = scene->world &&
343 (has_recalc(&scene->world->id, ID_RECALC_ALL) ||
344 has_recalc(&scene->world->nodetree->id, ID_RECALC_ALL));
345 emitted_world_.emit(ctx, bmain, scene, view3d, world_path, world_shading_changed);
346 }
347
348 /* Add, remove and dirty prims as needed. */
349 pxr::HdSceneIndexObserver::RemovedPrimEntries to_remove;
350 for (const pxr::SdfPath &path : emitted_paths_) {
351 if (!ctx.new_paths.contains(path)) {
352 to_remove.push_back({path});
353 }
354 }
355
356 if (!to_remove.empty()) {
357 retained_->RemovePrims(to_remove);
358 }
359 if (!ctx.to_add.empty()) {
360 retained_->AddPrims(ctx.to_add);
361 }
362 if (!ctx.to_dirty.empty()) {
363 retained_->DirtyPrims(ctx.to_dirty);
364 }
365
366 emitted_paths_ = std::move(ctx.new_paths);
367 emitted_objects_ = std::move(ctx.new_emitted_objects);
368}
369
371{
372 if (!emitted_paths_.is_empty()) {
373 pxr::HdSceneIndexObserver::RemovedPrimEntries to_remove;
374 for (const pxr::SdfPath &path : emitted_paths_) {
375 to_remove.push_back({path});
376 }
377 retained_->RemovePrims(to_remove);
378 emitted_paths_.clear();
379 }
380 emitted_objects_.clear();
381 emitted_materials_.clear();
382 emitted_geometry_.clear();
383 instance_geometries_by_object_.clear();
384 emitted_world_.clear();
385}
386
387} // namespace blender::io::hydra
General operations, lookup, etc. for blender objects.
#define ITER_END
#define ITER_BEGIN(callback_begin, callback_next, callback_end, _data_in, _type, _instance)
#define DEG_OBJECT_ITER_FOR_RENDER_ENGINE_FLAGS
ID and Library types, which are fundamental for SDNA.
Object is a sort of wrapper for general info.
#define ELEM(...)
constexpr IndexRange()=default
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
int64_t remove_if(Predicate &&predicate)
Definition BLI_set.hh:534
void populate(Depsgraph *depsgraph, View3D *view3d)
HydraSceneIndex(const pxr::SdfPath &root_path, pxr::HdRenderDelegate *render_delegate, bool use_materialx)
void emit_hair_proto(PopulateContext &ctx, Object *source)
void emit_hair_for_object(PopulateContext &ctx, Object *object, EmittedObject &emitted)
void emit_mesh_proto(PopulateContext &ctx, const BObjectInfo &info)
void emit_curves_proto(PopulateContext &ctx, const BObjectInfo &info)
void emit_mesh_object(PopulateContext &ctx, const BObjectInfo &info, EmittedObject &emitted)
bool emit_volume_object(PopulateContext &ctx, const Object *object, EmittedObject &emitted)
static bool object_render_as_geometry(const Object *object)
CLG_LOGREF_DECLARE_GLOBAL(LOG_HYDRA_SCENE_INDEX, "hydra.scene_index")
CLG_LogRef * LOG_HYDRA_SCENE_INDEX
void emit_light_dupli(PopulateContext &ctx, const Object *source, const float dupli_mat[4][4])
pxr::GfMatrix4d gf_matrix_from_transform(const float m[4][4])
Definition util.hh:20
void emit_volume_dupli(PopulateContext &ctx, const Object *source, const float dupli_mat[4][4])
void emit_light_object(PopulateContext &ctx, const Object *object, EmittedObject &emitted)
pxr::HdContainerDataSourceHandle build_instancer_prim_data_source(Span< pxr::SdfPath > prototypes, Span< pxr::VtIntArray > instance_indices, const pxr::VtMatrix4dArray &transforms)
Definition instancer.cc:32
void emit_curves_object(PopulateContext &ctx, const BObjectInfo &info, EmittedObject &emitted)
void DEG_iterator_ids_end(BLI_Iterator *iter)
void DEG_iterator_objects_next(BLI_Iterator *iter)
Main * DEG_get_bmain(const Depsgraph *graph)
BMesh const char void * data
int BKE_object_visibility(const Object *ob, int dag_eval_mode)
Definition object.cc:2063
void DEG_iterator_ids_next(BLI_Iterator *iter)
@ DAG_EVAL_VIEWPORT
void DEG_iterator_objects_end(BLI_Iterator *iter)
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
bool BKE_object_is_visible_in_viewport(const View3D *v3d, const Object *ob)
bool DEG_iterator_dupli_is_visible(const DupliObject *dupli, eEvaluationMode eval_mode)
@ ID_RECALC_ALL
Definition DNA_ID.h:1190
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void DEG_iterator_ids_begin(BLI_Iterator *iter, DEGIDIterData *data)
void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
@ OB_VISIBLE_SELF
struct World * world
pxr::HdContainerDataSourceHandle data_source
pxr::HdContainerDataSourceHandle base_data_source
pxr::HdContainerDataSourceHandle compose() const
pxr::HdContainerDataSourceHandle xform_overlay
Vector< EmittedObjectPrim > prims
Set< EmittedGeometryKey > used_emitted_geometry
Map< const Object *, EmittedObject > new_emitted_objects
void add_instance(const Object *source, const float dupli_mat[4][4])
Set< const Object * > used_instance_sources
pxr::HdRetainedSceneIndex::AddedPrimEntries to_add
Vector< const Material * > new_emitted_materials
Vector< pxr::VtIntArray > per_proto_indices
const EmittedMaterial * get_or_create_material(const Material *material)
Map< const Object *, IndexRange > proto_range_by_source
Set< const Material * > used_emitted_materials
pxr::HdSceneIndexObserver::DirtiedPrimEntries to_dirty
void emit_prim(const pxr::SdfPath &path, const pxr::TfToken &type, const pxr::HdContainerDataSourceHandle &ds)