1 /*
2 * Copyright 2011-2013 Blender Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "render/object.h"
18 #include "device/device.h"
19 #include "render/camera.h"
20 #include "render/curves.h"
21 #include "render/hair.h"
22 #include "render/integrator.h"
23 #include "render/light.h"
24 #include "render/mesh.h"
25 #include "render/particles.h"
26 #include "render/scene.h"
27 #include "render/stats.h"
28 #include "render/volume.h"
29
30 #include "util/util_foreach.h"
31 #include "util/util_logging.h"
32 #include "util/util_map.h"
33 #include "util/util_murmurhash.h"
34 #include "util/util_progress.h"
35 #include "util/util_set.h"
36 #include "util/util_task.h"
37 #include "util/util_vector.h"
38
39 #include "subd/subd_patch_table.h"
40
41 CCL_NAMESPACE_BEGIN
42
43 /* Global state of object transform update. */
44
45 struct UpdateObjectTransformState {
46 /* Global state used by device_update_object_transform().
47 * Common for both threaded and non-threaded update.
48 */
49
50 /* Type of the motion required by the scene settings. */
51 Scene::MotionType need_motion;
52
53 /* Mapping from particle system to a index in packed particle array.
54 * Only used for read.
55 */
56 map<ParticleSystem *, int> particle_offset;
57
58 /* Mesh area.
59 * Used to avoid calculation of mesh area multiple times. Used for both
60 * read and write. Acquire surface_area_lock to keep it all thread safe.
61 */
62 map<Mesh *, float> surface_area_map;
63
64 /* Motion offsets for each object. */
65 array<uint> motion_offset;
66
67 /* Packed object arrays. Those will be filled in. */
68 uint *object_flag;
69 KernelObject *objects;
70 Transform *object_motion_pass;
71 DecomposedTransform *object_motion;
72 float *object_volume_step;
73
74 /* Flags which will be synchronized to Integrator. */
75 bool have_motion;
76 bool have_curves;
77
78 /* ** Scheduling queue. ** */
79
80 Scene *scene;
81
82 /* Some locks to keep everything thread-safe. */
83 thread_spin_lock surface_area_lock;
84
85 /* First unused object index in the queue. */
86 int queue_start_object;
87 };
88
89 /* Object */
90
NODE_DEFINE(Object)91 NODE_DEFINE(Object)
92 {
93 NodeType *type = NodeType::add("object", create);
94
95 SOCKET_NODE(geometry, "Geometry", &Geometry::node_base_type);
96 SOCKET_TRANSFORM(tfm, "Transform", transform_identity());
97 SOCKET_UINT(visibility, "Visibility", ~0);
98 SOCKET_COLOR(color, "Color", make_float3(0.0f, 0.0f, 0.0f));
99 SOCKET_UINT(random_id, "Random ID", 0);
100 SOCKET_INT(pass_id, "Pass ID", 0);
101 SOCKET_BOOLEAN(use_holdout, "Use Holdout", false);
102 SOCKET_BOOLEAN(hide_on_missing_motion, "Hide on Missing Motion", false);
103 SOCKET_POINT(dupli_generated, "Dupli Generated", make_float3(0.0f, 0.0f, 0.0f));
104 SOCKET_POINT2(dupli_uv, "Dupli UV", make_float2(0.0f, 0.0f));
105 SOCKET_TRANSFORM_ARRAY(motion, "Motion", array<Transform>());
106 SOCKET_FLOAT(shadow_terminator_offset, "Terminator Offset", 0.0f);
107
108 SOCKET_BOOLEAN(is_shadow_catcher, "Shadow Catcher", false);
109
110 return type;
111 }
112
Object()113 Object::Object() : Node(node_type)
114 {
115 particle_system = NULL;
116 particle_index = 0;
117 bounds = BoundBox::empty;
118 }
119
~Object()120 Object::~Object()
121 {
122 }
123
update_motion()124 void Object::update_motion()
125 {
126 if (!use_motion()) {
127 return;
128 }
129
130 bool have_motion = false;
131
132 for (size_t i = 0; i < motion.size(); i++) {
133 if (motion[i] == transform_empty()) {
134 if (hide_on_missing_motion) {
135 /* Hide objects that have no valid previous or next
136 * transform, for example particle that stop existing. It
137 * would be better to handle this in the kernel and make
138 * objects invisible outside certain motion steps. */
139 tfm = transform_empty();
140 motion.clear();
141 return;
142 }
143 else {
144 /* Otherwise just copy center motion. */
145 motion[i] = tfm;
146 }
147 }
148
149 /* Test if any of the transforms are actually different. */
150 have_motion = have_motion || motion[i] != tfm;
151 }
152
153 /* Clear motion array if there is no actual motion. */
154 if (!have_motion) {
155 motion.clear();
156 }
157 }
158
compute_bounds(bool motion_blur)159 void Object::compute_bounds(bool motion_blur)
160 {
161 BoundBox mbounds = geometry->bounds;
162
163 if (motion_blur && use_motion()) {
164 array<DecomposedTransform> decomp(motion.size());
165 transform_motion_decompose(decomp.data(), motion.data(), motion.size());
166
167 bounds = BoundBox::empty;
168
169 /* todo: this is really terrible. according to pbrt there is a better
170 * way to find this iteratively, but did not find implementation yet
171 * or try to implement myself */
172 for (float t = 0.0f; t < 1.0f; t += (1.0f / 128.0f)) {
173 Transform ttfm;
174
175 transform_motion_array_interpolate(&ttfm, decomp.data(), motion.size(), t);
176 bounds.grow(mbounds.transformed(&ttfm));
177 }
178 }
179 else {
180 /* No motion blur case. */
181 if (geometry->transform_applied) {
182 bounds = mbounds;
183 }
184 else {
185 bounds = mbounds.transformed(&tfm);
186 }
187 }
188 }
189
apply_transform(bool apply_to_motion)190 void Object::apply_transform(bool apply_to_motion)
191 {
192 if (!geometry || tfm == transform_identity())
193 return;
194
195 geometry->apply_transform(tfm, apply_to_motion);
196
197 /* we keep normals pointing in same direction on negative scale, notify
198 * geometry about this in it (re)calculates normals */
199 if (transform_negative_scale(tfm))
200 geometry->transform_negative_scaled = true;
201
202 if (bounds.valid()) {
203 geometry->compute_bounds();
204 compute_bounds(false);
205 }
206
207 /* tfm is not reset to identity, all code that uses it needs to check the
208 * transform_applied boolean */
209 }
210
tag_update(Scene * scene)211 void Object::tag_update(Scene *scene)
212 {
213 if (geometry) {
214 if (geometry->transform_applied)
215 geometry->need_update = true;
216
217 foreach (Shader *shader, geometry->used_shaders) {
218 if (shader->use_mis && shader->has_surface_emission)
219 scene->light_manager->need_update = true;
220 }
221 }
222
223 scene->camera->need_flags_update = true;
224 scene->geometry_manager->need_update = true;
225 scene->object_manager->need_update = true;
226 }
227
use_motion() const228 bool Object::use_motion() const
229 {
230 return (motion.size() > 1);
231 }
232
motion_time(int step) const233 float Object::motion_time(int step) const
234 {
235 return (use_motion()) ? 2.0f * step / (motion.size() - 1) - 1.0f : 0.0f;
236 }
237
motion_step(float time) const238 int Object::motion_step(float time) const
239 {
240 if (use_motion()) {
241 for (size_t step = 0; step < motion.size(); step++) {
242 if (time == motion_time(step)) {
243 return step;
244 }
245 }
246 }
247
248 return -1;
249 }
250
is_traceable() const251 bool Object::is_traceable() const
252 {
253 /* Mesh itself can be empty,can skip all such objects. */
254 if (!bounds.valid() || bounds.size() == make_float3(0.0f, 0.0f, 0.0f)) {
255 return false;
256 }
257 /* TODO(sergey): Check for mesh vertices/curves. visibility flags. */
258 return true;
259 }
260
visibility_for_tracing() const261 uint Object::visibility_for_tracing() const
262 {
263 uint trace_visibility = visibility;
264 if (is_shadow_catcher) {
265 trace_visibility &= ~PATH_RAY_SHADOW_NON_CATCHER;
266 }
267 else {
268 trace_visibility &= ~PATH_RAY_SHADOW_CATCHER;
269 }
270 return trace_visibility;
271 }
272
compute_volume_step_size() const273 float Object::compute_volume_step_size() const
274 {
275 if (geometry->type != Geometry::MESH && geometry->type != Geometry::VOLUME) {
276 return FLT_MAX;
277 }
278
279 Mesh *mesh = static_cast<Mesh *>(geometry);
280
281 if (!mesh->has_volume) {
282 return FLT_MAX;
283 }
284
285 /* Compute step rate from shaders. */
286 float step_rate = FLT_MAX;
287
288 foreach (Shader *shader, mesh->used_shaders) {
289 if (shader->has_volume) {
290 if ((shader->heterogeneous_volume && shader->has_volume_spatial_varying) ||
291 (shader->has_volume_attribute_dependency)) {
292 step_rate = fminf(shader->volume_step_rate, step_rate);
293 }
294 }
295 }
296
297 if (step_rate == FLT_MAX) {
298 return FLT_MAX;
299 }
300
301 /* Compute step size from voxel grids. */
302 float step_size = FLT_MAX;
303
304 if (geometry->type == Geometry::VOLUME) {
305 Volume *volume = static_cast<Volume *>(geometry);
306
307 foreach (Attribute &attr, volume->attributes.attributes) {
308 if (attr.element == ATTR_ELEMENT_VOXEL) {
309 ImageHandle &handle = attr.data_voxel();
310 const ImageMetaData &metadata = handle.metadata();
311 if (metadata.width == 0 || metadata.height == 0 || metadata.depth == 0) {
312 continue;
313 }
314
315 /* User specified step size. */
316 float voxel_step_size = volume->step_size;
317
318 if (voxel_step_size == 0.0f) {
319 /* Auto detect step size. */
320 float3 size = make_float3(
321 1.0f / metadata.width, 1.0f / metadata.height, 1.0f / metadata.depth);
322
323 /* Step size is transformed from voxel to world space. */
324 Transform voxel_tfm = tfm;
325 if (metadata.use_transform_3d) {
326 voxel_tfm = tfm * transform_inverse(metadata.transform_3d);
327 }
328 voxel_step_size = min3(fabs(transform_direction(&voxel_tfm, size)));
329 }
330 else if (volume->object_space) {
331 /* User specified step size in object space. */
332 float3 size = make_float3(voxel_step_size, voxel_step_size, voxel_step_size);
333 voxel_step_size = min3(fabs(transform_direction(&tfm, size)));
334 }
335
336 if (voxel_step_size > 0.0f) {
337 step_size = fminf(voxel_step_size, step_size);
338 }
339 }
340 }
341 }
342
343 if (step_size == FLT_MAX) {
344 /* Fall back to 1/10th of bounds for procedural volumes. */
345 step_size = 0.1f * average(bounds.size());
346 }
347
348 step_size *= step_rate;
349
350 return step_size;
351 }
352
get_device_index() const353 int Object::get_device_index() const
354 {
355 return index;
356 }
357
358 /* Object Manager */
359
ObjectManager()360 ObjectManager::ObjectManager()
361 {
362 need_update = true;
363 need_flags_update = true;
364 }
365
~ObjectManager()366 ObjectManager::~ObjectManager()
367 {
368 }
369
object_surface_area(UpdateObjectTransformState * state,const Transform & tfm,Geometry * geom)370 static float object_surface_area(UpdateObjectTransformState *state,
371 const Transform &tfm,
372 Geometry *geom)
373 {
374 if (geom->type != Geometry::MESH && geom->type != Geometry::VOLUME) {
375 return 0.0f;
376 }
377
378 Mesh *mesh = static_cast<Mesh *>(geom);
379 if (mesh->has_volume || geom->type == Geometry::VOLUME) {
380 /* Volume density automatically adjust to object scale. */
381 if (geom->type == Geometry::VOLUME && static_cast<Volume *>(geom)->object_space) {
382 const float3 unit = normalize(make_float3(1.0f, 1.0f, 1.0f));
383 return 1.0f / len(transform_direction(&tfm, unit));
384 }
385 else {
386 return 1.0f;
387 }
388 }
389
390 /* Compute surface area. for uniform scale we can do avoid the many
391 * transform calls and share computation for instances.
392 *
393 * TODO(brecht): Correct for displacement, and move to a better place.
394 */
395 float surface_area = 0.0f;
396 float uniform_scale;
397 if (transform_uniform_scale(tfm, uniform_scale)) {
398 map<Mesh *, float>::iterator it;
399
400 /* NOTE: This isn't fully optimal and could in theory lead to multiple
401 * threads calculating area of the same mesh in parallel. However, this
402 * also prevents suspending all the threads when some mesh's area is
403 * not yet known.
404 */
405 state->surface_area_lock.lock();
406 it = state->surface_area_map.find(mesh);
407 state->surface_area_lock.unlock();
408
409 if (it == state->surface_area_map.end()) {
410 size_t num_triangles = mesh->num_triangles();
411 for (size_t j = 0; j < num_triangles; j++) {
412 Mesh::Triangle t = mesh->get_triangle(j);
413 float3 p1 = mesh->verts[t.v[0]];
414 float3 p2 = mesh->verts[t.v[1]];
415 float3 p3 = mesh->verts[t.v[2]];
416
417 surface_area += triangle_area(p1, p2, p3);
418 }
419
420 state->surface_area_lock.lock();
421 state->surface_area_map[mesh] = surface_area;
422 state->surface_area_lock.unlock();
423 }
424 else {
425 surface_area = it->second;
426 }
427
428 surface_area *= uniform_scale;
429 }
430 else {
431 size_t num_triangles = mesh->num_triangles();
432 for (size_t j = 0; j < num_triangles; j++) {
433 Mesh::Triangle t = mesh->get_triangle(j);
434 float3 p1 = transform_point(&tfm, mesh->verts[t.v[0]]);
435 float3 p2 = transform_point(&tfm, mesh->verts[t.v[1]]);
436 float3 p3 = transform_point(&tfm, mesh->verts[t.v[2]]);
437
438 surface_area += triangle_area(p1, p2, p3);
439 }
440 }
441
442 return surface_area;
443 }
444
device_update_object_transform(UpdateObjectTransformState * state,Object * ob)445 void ObjectManager::device_update_object_transform(UpdateObjectTransformState *state, Object *ob)
446 {
447 KernelObject &kobject = state->objects[ob->index];
448 Transform *object_motion_pass = state->object_motion_pass;
449
450 Geometry *geom = ob->geometry;
451 uint flag = 0;
452
453 /* Compute transformations. */
454 Transform tfm = ob->tfm;
455 Transform itfm = transform_inverse(tfm);
456
457 float3 color = ob->color;
458 float pass_id = ob->pass_id;
459 float random_number = (float)ob->random_id * (1.0f / (float)0xFFFFFFFF);
460 int particle_index = (ob->particle_system) ?
461 ob->particle_index + state->particle_offset[ob->particle_system] :
462 0;
463
464 kobject.tfm = tfm;
465 kobject.itfm = itfm;
466 kobject.surface_area = object_surface_area(state, tfm, geom);
467 kobject.color[0] = color.x;
468 kobject.color[1] = color.y;
469 kobject.color[2] = color.z;
470 kobject.pass_id = pass_id;
471 kobject.random_number = random_number;
472 kobject.particle_index = particle_index;
473 kobject.motion_offset = 0;
474
475 if (geom->use_motion_blur) {
476 state->have_motion = true;
477 }
478
479 if (geom->type == Geometry::MESH) {
480 /* TODO: why only mesh? */
481 Mesh *mesh = static_cast<Mesh *>(geom);
482 if (mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)) {
483 flag |= SD_OBJECT_HAS_VERTEX_MOTION;
484 }
485 }
486
487 if (state->need_motion == Scene::MOTION_PASS) {
488 /* Clear motion array if there is no actual motion. */
489 ob->update_motion();
490
491 /* Compute motion transforms. */
492 Transform tfm_pre, tfm_post;
493 if (ob->use_motion()) {
494 tfm_pre = ob->motion[0];
495 tfm_post = ob->motion[ob->motion.size() - 1];
496 }
497 else {
498 tfm_pre = tfm;
499 tfm_post = tfm;
500 }
501
502 /* Motion transformations, is world/object space depending if mesh
503 * comes with deformed position in object space, or if we transform
504 * the shading point in world space. */
505 if (!(flag & SD_OBJECT_HAS_VERTEX_MOTION)) {
506 tfm_pre = tfm_pre * itfm;
507 tfm_post = tfm_post * itfm;
508 }
509
510 int motion_pass_offset = ob->index * OBJECT_MOTION_PASS_SIZE;
511 object_motion_pass[motion_pass_offset + 0] = tfm_pre;
512 object_motion_pass[motion_pass_offset + 1] = tfm_post;
513 }
514 else if (state->need_motion == Scene::MOTION_BLUR) {
515 if (ob->use_motion()) {
516 kobject.motion_offset = state->motion_offset[ob->index];
517
518 /* Decompose transforms for interpolation. */
519 DecomposedTransform *decomp = state->object_motion + kobject.motion_offset;
520 transform_motion_decompose(decomp, ob->motion.data(), ob->motion.size());
521 flag |= SD_OBJECT_MOTION;
522 state->have_motion = true;
523 }
524 }
525
526 /* Dupli object coords and motion info. */
527 kobject.dupli_generated[0] = ob->dupli_generated[0];
528 kobject.dupli_generated[1] = ob->dupli_generated[1];
529 kobject.dupli_generated[2] = ob->dupli_generated[2];
530 kobject.numkeys = (geom->type == Geometry::HAIR) ? static_cast<Hair *>(geom)->curve_keys.size() :
531 0;
532 kobject.dupli_uv[0] = ob->dupli_uv[0];
533 kobject.dupli_uv[1] = ob->dupli_uv[1];
534 int totalsteps = geom->motion_steps;
535 kobject.numsteps = (totalsteps - 1) / 2;
536 kobject.numverts = (geom->type == Geometry::MESH || geom->type == Geometry::VOLUME) ?
537 static_cast<Mesh *>(geom)->verts.size() :
538 0;
539 kobject.patch_map_offset = 0;
540 kobject.attribute_map_offset = 0;
541 uint32_t hash_name = util_murmur_hash3(ob->name.c_str(), ob->name.length(), 0);
542 uint32_t hash_asset = util_murmur_hash3(ob->asset_name.c_str(), ob->asset_name.length(), 0);
543 kobject.cryptomatte_object = util_hash_to_float(hash_name);
544 kobject.cryptomatte_asset = util_hash_to_float(hash_asset);
545 kobject.shadow_terminator_offset = 1.0f / (1.0f - 0.5f * ob->shadow_terminator_offset);
546
547 /* Object flag. */
548 if (ob->use_holdout) {
549 flag |= SD_OBJECT_HOLDOUT_MASK;
550 }
551 state->object_flag[ob->index] = flag;
552 state->object_volume_step[ob->index] = FLT_MAX;
553
554 /* Have curves. */
555 if (geom->type == Geometry::HAIR) {
556 state->have_curves = true;
557 }
558 }
559
device_update_transforms(DeviceScene * dscene,Scene * scene,Progress & progress)560 void ObjectManager::device_update_transforms(DeviceScene *dscene, Scene *scene, Progress &progress)
561 {
562 UpdateObjectTransformState state;
563 state.need_motion = scene->need_motion();
564 state.have_motion = false;
565 state.have_curves = false;
566 state.scene = scene;
567 state.queue_start_object = 0;
568
569 state.objects = dscene->objects.alloc(scene->objects.size());
570 state.object_flag = dscene->object_flag.alloc(scene->objects.size());
571 state.object_volume_step = dscene->object_volume_step.alloc(scene->objects.size());
572 state.object_motion = NULL;
573 state.object_motion_pass = NULL;
574
575 if (state.need_motion == Scene::MOTION_PASS) {
576 state.object_motion_pass = dscene->object_motion_pass.alloc(OBJECT_MOTION_PASS_SIZE *
577 scene->objects.size());
578 }
579 else if (state.need_motion == Scene::MOTION_BLUR) {
580 /* Set object offsets into global object motion array. */
581 uint *motion_offsets = state.motion_offset.resize(scene->objects.size());
582 uint motion_offset = 0;
583
584 foreach (Object *ob, scene->objects) {
585 *motion_offsets = motion_offset;
586 motion_offsets++;
587
588 /* Clear motion array if there is no actual motion. */
589 ob->update_motion();
590 motion_offset += ob->motion.size();
591 }
592
593 state.object_motion = dscene->object_motion.alloc(motion_offset);
594 }
595
596 /* Particle system device offsets
597 * 0 is dummy particle, index starts at 1.
598 */
599 int numparticles = 1;
600 foreach (ParticleSystem *psys, scene->particle_systems) {
601 state.particle_offset[psys] = numparticles;
602 numparticles += psys->particles.size();
603 }
604
605 /* Parallel object update, with grain size to avoid too much threading overhead
606 * for individual objects. */
607 static const int OBJECTS_PER_TASK = 32;
608 parallel_for(blocked_range<size_t>(0, scene->objects.size(), OBJECTS_PER_TASK),
609 [&](const blocked_range<size_t> &r) {
610 for (size_t i = r.begin(); i != r.end(); i++) {
611 Object *ob = state.scene->objects[i];
612 device_update_object_transform(&state, ob);
613 }
614 });
615
616 if (progress.get_cancel()) {
617 return;
618 }
619
620 dscene->objects.copy_to_device();
621 if (state.need_motion == Scene::MOTION_PASS) {
622 dscene->object_motion_pass.copy_to_device();
623 }
624 else if (state.need_motion == Scene::MOTION_BLUR) {
625 dscene->object_motion.copy_to_device();
626 }
627
628 dscene->data.bvh.have_motion = state.have_motion;
629 dscene->data.bvh.have_curves = state.have_curves;
630 }
631
device_update(Device * device,DeviceScene * dscene,Scene * scene,Progress & progress)632 void ObjectManager::device_update(Device *device,
633 DeviceScene *dscene,
634 Scene *scene,
635 Progress &progress)
636 {
637 if (!need_update)
638 return;
639
640 VLOG(1) << "Total " << scene->objects.size() << " objects.";
641
642 device_free(device, dscene);
643
644 if (scene->objects.size() == 0)
645 return;
646
647 {
648 /* Assign object IDs. */
649 scoped_callback_timer timer([scene](double time) {
650 if (scene->update_stats) {
651 scene->update_stats->object.times.add_entry({"device_update (assign index)", time});
652 }
653 });
654
655 int index = 0;
656 foreach (Object *object, scene->objects) {
657 object->index = index++;
658 }
659 }
660
661 {
662 /* set object transform matrices, before applying static transforms */
663 scoped_callback_timer timer([scene](double time) {
664 if (scene->update_stats) {
665 scene->update_stats->object.times.add_entry(
666 {"device_update (copy objects to device)", time});
667 }
668 });
669
670 progress.set_status("Updating Objects", "Copying Transformations to device");
671 device_update_transforms(dscene, scene, progress);
672 }
673
674 if (progress.get_cancel())
675 return;
676
677 /* prepare for static BVH building */
678 /* todo: do before to support getting object level coords? */
679 if (scene->params.bvh_type == SceneParams::BVH_STATIC) {
680 scoped_callback_timer timer([scene](double time) {
681 if (scene->update_stats) {
682 scene->update_stats->object.times.add_entry(
683 {"device_update (apply static transforms)", time});
684 }
685 });
686
687 progress.set_status("Updating Objects", "Applying Static Transformations");
688 apply_static_transforms(dscene, scene, progress);
689 }
690 }
691
device_update_flags(Device *,DeviceScene * dscene,Scene * scene,Progress &,bool bounds_valid)692 void ObjectManager::device_update_flags(
693 Device *, DeviceScene *dscene, Scene *scene, Progress & /*progress*/, bool bounds_valid)
694 {
695 if (!need_update && !need_flags_update)
696 return;
697
698 scoped_callback_timer timer([scene](double time) {
699 if (scene->update_stats) {
700 scene->update_stats->object.times.add_entry({"device_update_flags", time});
701 }
702 });
703
704 need_update = false;
705 need_flags_update = false;
706
707 if (scene->objects.size() == 0)
708 return;
709
710 /* Object info flag. */
711 uint *object_flag = dscene->object_flag.data();
712 float *object_volume_step = dscene->object_volume_step.data();
713
714 /* Object volume intersection. */
715 vector<Object *> volume_objects;
716 bool has_volume_objects = false;
717 foreach (Object *object, scene->objects) {
718 if (object->geometry->has_volume) {
719 if (bounds_valid) {
720 volume_objects.push_back(object);
721 }
722 has_volume_objects = true;
723 object_volume_step[object->index] = object->compute_volume_step_size();
724 }
725 else {
726 object_volume_step[object->index] = FLT_MAX;
727 }
728 }
729
730 foreach (Object *object, scene->objects) {
731 if (object->geometry->has_volume) {
732 object_flag[object->index] |= SD_OBJECT_HAS_VOLUME;
733 object_flag[object->index] &= ~SD_OBJECT_HAS_VOLUME_ATTRIBUTES;
734
735 foreach (Attribute &attr, object->geometry->attributes.attributes) {
736 if (attr.element == ATTR_ELEMENT_VOXEL) {
737 object_flag[object->index] |= SD_OBJECT_HAS_VOLUME_ATTRIBUTES;
738 }
739 }
740 }
741 else {
742 object_flag[object->index] &= ~(SD_OBJECT_HAS_VOLUME | SD_OBJECT_HAS_VOLUME_ATTRIBUTES);
743 }
744
745 if (object->is_shadow_catcher) {
746 object_flag[object->index] |= SD_OBJECT_SHADOW_CATCHER;
747 }
748 else {
749 object_flag[object->index] &= ~SD_OBJECT_SHADOW_CATCHER;
750 }
751
752 if (bounds_valid) {
753 foreach (Object *volume_object, volume_objects) {
754 if (object == volume_object) {
755 continue;
756 }
757 if (object->bounds.intersects(volume_object->bounds)) {
758 object_flag[object->index] |= SD_OBJECT_INTERSECTS_VOLUME;
759 break;
760 }
761 }
762 }
763 else if (has_volume_objects) {
764 /* Not really valid, but can't make more reliable in the case
765 * of bounds not being up to date.
766 */
767 object_flag[object->index] |= SD_OBJECT_INTERSECTS_VOLUME;
768 }
769 }
770
771 /* Copy object flag. */
772 dscene->object_flag.copy_to_device();
773 dscene->object_volume_step.copy_to_device();
774 }
775
device_update_mesh_offsets(Device *,DeviceScene * dscene,Scene * scene)776 void ObjectManager::device_update_mesh_offsets(Device *, DeviceScene *dscene, Scene *scene)
777 {
778 if (dscene->objects.size() == 0) {
779 return;
780 }
781
782 KernelObject *kobjects = dscene->objects.data();
783
784 bool update = false;
785
786 foreach (Object *object, scene->objects) {
787 Geometry *geom = object->geometry;
788
789 if (geom->type == Geometry::MESH) {
790 Mesh *mesh = static_cast<Mesh *>(geom);
791 if (mesh->patch_table) {
792 uint patch_map_offset = 2 * (mesh->patch_table_offset + mesh->patch_table->total_size() -
793 mesh->patch_table->num_nodes * PATCH_NODE_SIZE) -
794 mesh->patch_offset;
795
796 if (kobjects[object->index].patch_map_offset != patch_map_offset) {
797 kobjects[object->index].patch_map_offset = patch_map_offset;
798 update = true;
799 }
800 }
801 }
802
803 if (kobjects[object->index].attribute_map_offset != geom->attr_map_offset) {
804 kobjects[object->index].attribute_map_offset = geom->attr_map_offset;
805 update = true;
806 }
807 }
808
809 if (update) {
810 dscene->objects.copy_to_device();
811 }
812 }
813
device_free(Device *,DeviceScene * dscene)814 void ObjectManager::device_free(Device *, DeviceScene *dscene)
815 {
816 dscene->objects.free();
817 dscene->object_motion_pass.free();
818 dscene->object_motion.free();
819 dscene->object_flag.free();
820 dscene->object_volume_step.free();
821 }
822
apply_static_transforms(DeviceScene * dscene,Scene * scene,Progress & progress)823 void ObjectManager::apply_static_transforms(DeviceScene *dscene, Scene *scene, Progress &progress)
824 {
825 /* todo: normals and displacement should be done before applying transform! */
826 /* todo: create objects/geometry in right order! */
827
828 /* counter geometry users */
829 map<Geometry *, int> geometry_users;
830 Scene::MotionType need_motion = scene->need_motion();
831 bool motion_blur = need_motion == Scene::MOTION_BLUR;
832 bool apply_to_motion = need_motion != Scene::MOTION_PASS;
833 int i = 0;
834
835 foreach (Object *object, scene->objects) {
836 map<Geometry *, int>::iterator it = geometry_users.find(object->geometry);
837
838 if (it == geometry_users.end())
839 geometry_users[object->geometry] = 1;
840 else
841 it->second++;
842 }
843
844 if (progress.get_cancel())
845 return;
846
847 uint *object_flag = dscene->object_flag.data();
848
849 /* apply transforms for objects with single user geometry */
850 foreach (Object *object, scene->objects) {
851 /* Annoying feedback loop here: we can't use is_instanced() because
852 * it'll use uninitialized transform_applied flag.
853 *
854 * Could be solved by moving reference counter to Geometry.
855 */
856 Geometry *geom = object->geometry;
857 bool apply = (geometry_users[geom] == 1) && !geom->has_surface_bssrdf &&
858 !geom->has_true_displacement();
859
860 if (geom->type == Geometry::MESH || geom->type == Geometry::VOLUME) {
861 Mesh *mesh = static_cast<Mesh *>(geom);
862 apply = apply && mesh->subdivision_type == Mesh::SUBDIVISION_NONE;
863 }
864 else if (geom->type == Geometry::HAIR) {
865 /* Can't apply non-uniform scale to curves, this can't be represented by
866 * control points and radius alone. */
867 float scale;
868 apply = apply && transform_uniform_scale(object->tfm, scale);
869 }
870
871 if (apply) {
872 if (!(motion_blur && object->use_motion())) {
873 if (!geom->transform_applied) {
874 object->apply_transform(apply_to_motion);
875 geom->transform_applied = true;
876
877 if (progress.get_cancel())
878 return;
879 }
880
881 object_flag[i] |= SD_OBJECT_TRANSFORM_APPLIED;
882 if (geom->transform_negative_scaled)
883 object_flag[i] |= SD_OBJECT_NEGATIVE_SCALE_APPLIED;
884 }
885 }
886
887 i++;
888 }
889 }
890
tag_update(Scene * scene)891 void ObjectManager::tag_update(Scene *scene)
892 {
893 need_update = true;
894 scene->geometry_manager->need_update = true;
895 scene->light_manager->need_update = true;
896 }
897
get_cryptomatte_objects(Scene * scene)898 string ObjectManager::get_cryptomatte_objects(Scene *scene)
899 {
900 string manifest = "{";
901
902 unordered_set<ustring, ustringHash> objects;
903 foreach (Object *object, scene->objects) {
904 if (objects.count(object->name)) {
905 continue;
906 }
907 objects.insert(object->name);
908 uint32_t hash_name = util_murmur_hash3(object->name.c_str(), object->name.length(), 0);
909 manifest += string_printf("\"%s\":\"%08x\",", object->name.c_str(), hash_name);
910 }
911 manifest[manifest.size() - 1] = '}';
912 return manifest;
913 }
914
get_cryptomatte_assets(Scene * scene)915 string ObjectManager::get_cryptomatte_assets(Scene *scene)
916 {
917 string manifest = "{";
918 unordered_set<ustring, ustringHash> assets;
919 foreach (Object *ob, scene->objects) {
920 if (assets.count(ob->asset_name)) {
921 continue;
922 }
923 assets.insert(ob->asset_name);
924 uint32_t hash_asset = util_murmur_hash3(ob->asset_name.c_str(), ob->asset_name.length(), 0);
925 manifest += string_printf("\"%s\":\"%08x\",", ob->asset_name.c_str(), hash_asset);
926 }
927 manifest[manifest.size() - 1] = '}';
928 return manifest;
929 }
930
931 CCL_NAMESPACE_END
932