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/camera.h"
18 #include "render/graph.h"
19 #include "render/integrator.h"
20 #include "render/light.h"
21 #include "render/mesh.h"
22 #include "render/nodes.h"
23 #include "render/object.h"
24 #include "render/particles.h"
25 #include "render/scene.h"
26 #include "render/shader.h"
27
28 #include "blender/blender_object_cull.h"
29 #include "blender/blender_sync.h"
30 #include "blender/blender_util.h"
31
32 #include "util/util_foreach.h"
33 #include "util/util_hash.h"
34 #include "util/util_logging.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 /* Utilities */
39
BKE_object_is_modified(BL::Object & b_ob)40 bool BlenderSync::BKE_object_is_modified(BL::Object &b_ob)
41 {
42 /* test if we can instance or if the object is modified */
43 if (b_ob.type() == BL::Object::type_META) {
44 /* multi-user and dupli metaballs are fused, can't instance */
45 return true;
46 }
47 else if (ccl::BKE_object_is_modified(b_ob, b_scene, preview)) {
48 /* modifiers */
49 return true;
50 }
51 else {
52 /* object level material links */
53 BL::Object::material_slots_iterator slot;
54 for (b_ob.material_slots.begin(slot); slot != b_ob.material_slots.end(); ++slot)
55 if (slot->link() == BL::MaterialSlot::link_OBJECT)
56 return true;
57 }
58
59 return false;
60 }
61
object_is_geometry(BL::Object & b_ob)62 bool BlenderSync::object_is_geometry(BL::Object &b_ob)
63 {
64 BL::ID b_ob_data = b_ob.data();
65
66 if (!b_ob_data) {
67 return false;
68 }
69
70 BL::Object::type_enum type = b_ob.type();
71
72 if (type == BL::Object::type_VOLUME || type == BL::Object::type_HAIR) {
73 /* Will be exported attached to mesh. */
74 return true;
75 }
76 else if (type == BL::Object::type_CURVE) {
77 /* Skip exporting curves without faces, overhead can be
78 * significant if there are many for path animation. */
79 BL::Curve b_curve(b_ob_data);
80
81 return (b_curve.bevel_object() || b_curve.extrude() != 0.0f || b_curve.bevel_depth() != 0.0f ||
82 b_curve.dimensions() == BL::Curve::dimensions_2D || b_ob.modifiers.length());
83 }
84 else {
85 return (b_ob_data.is_a(&RNA_Mesh) || b_ob_data.is_a(&RNA_Curve) ||
86 b_ob_data.is_a(&RNA_MetaBall));
87 }
88 }
89
object_is_light(BL::Object & b_ob)90 bool BlenderSync::object_is_light(BL::Object &b_ob)
91 {
92 BL::ID b_ob_data = b_ob.data();
93
94 return (b_ob_data && b_ob_data.is_a(&RNA_Light));
95 }
96
97 /* Object */
98
sync_object(BL::Depsgraph & b_depsgraph,BL::ViewLayer & b_view_layer,BL::DepsgraphObjectInstance & b_instance,float motion_time,bool use_particle_hair,bool show_lights,BlenderObjectCulling & culling,bool * use_portal)99 Object *BlenderSync::sync_object(BL::Depsgraph &b_depsgraph,
100 BL::ViewLayer &b_view_layer,
101 BL::DepsgraphObjectInstance &b_instance,
102 float motion_time,
103 bool use_particle_hair,
104 bool show_lights,
105 BlenderObjectCulling &culling,
106 bool *use_portal)
107 {
108 const bool is_instance = b_instance.is_instance();
109 BL::Object b_ob = b_instance.object();
110 BL::Object b_parent = is_instance ? b_instance.parent() : b_instance.object();
111 BL::Object b_ob_instance = is_instance ? b_instance.instance_object() : b_ob;
112 const bool motion = motion_time != 0.0f;
113 /*const*/ Transform tfm = get_transform(b_ob.matrix_world());
114 int *persistent_id = NULL;
115 BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id_array;
116 if (is_instance) {
117 persistent_id_array = b_instance.persistent_id();
118 persistent_id = persistent_id_array.data;
119 }
120
121 /* light is handled separately */
122 if (!motion && object_is_light(b_ob)) {
123 if (!show_lights) {
124 return NULL;
125 }
126
127 /* TODO: don't use lights for excluded layers used as mask layer,
128 * when dynamic overrides are back. */
129 #if 0
130 if (!((layer_flag & view_layer.holdout_layer) && (layer_flag & view_layer.exclude_layer)))
131 #endif
132 {
133 sync_light(b_parent,
134 persistent_id,
135 b_ob,
136 b_ob_instance,
137 is_instance ? b_instance.random_id() : 0,
138 tfm,
139 use_portal);
140 }
141
142 return NULL;
143 }
144
145 /* only interested in object that we can create meshes from */
146 if (!object_is_geometry(b_ob)) {
147 return NULL;
148 }
149
150 /* Perform object culling. */
151 if (culling.test(scene, b_ob, tfm)) {
152 return NULL;
153 }
154
155 /* Visibility flags for both parent and child. */
156 PointerRNA cobject = RNA_pointer_get(&b_ob.ptr, "cycles");
157 bool use_holdout = get_boolean(cobject, "is_holdout") ||
158 b_parent.holdout_get(PointerRNA_NULL, b_view_layer);
159 uint visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL_VISIBILITY;
160
161 if (b_parent.ptr.data != b_ob.ptr.data) {
162 visibility &= object_ray_visibility(b_parent);
163 }
164
165 /* TODO: make holdout objects on excluded layer invisible for non-camera rays. */
166 #if 0
167 if (use_holdout && (layer_flag & view_layer.exclude_layer)) {
168 visibility &= ~(PATH_RAY_ALL_VISIBILITY - PATH_RAY_CAMERA);
169 }
170 #endif
171
172 /* Clear camera visibility for indirect only objects. */
173 bool use_indirect_only = !use_holdout &&
174 b_parent.indirect_only_get(PointerRNA_NULL, b_view_layer);
175 if (use_indirect_only) {
176 visibility &= ~PATH_RAY_CAMERA;
177 }
178
179 /* Don't export completely invisible objects. */
180 if (visibility == 0) {
181 return NULL;
182 }
183
184 /* key to lookup object */
185 ObjectKey key(b_parent, persistent_id, b_ob_instance, use_particle_hair);
186 Object *object;
187
188 /* motion vector case */
189 if (motion) {
190 object = object_map.find(key);
191
192 if (object && object->use_motion()) {
193 /* Set transform at matching motion time step. */
194 int time_index = object->motion_step(motion_time);
195 if (time_index >= 0) {
196 object->motion[time_index] = tfm;
197 }
198
199 /* mesh deformation */
200 if (object->geometry)
201 sync_geometry_motion(b_depsgraph, b_ob, object, motion_time, use_particle_hair);
202 }
203
204 return object;
205 }
206
207 /* test if we need to sync */
208 bool object_updated = false;
209
210 if (object_map.add_or_update(&object, b_ob, b_parent, key))
211 object_updated = true;
212
213 /* mesh sync */
214 object->geometry = sync_geometry(
215 b_depsgraph, b_ob, b_ob_instance, object_updated, use_particle_hair);
216
217 /* special case not tracked by object update flags */
218
219 /* holdout */
220 if (use_holdout != object->use_holdout) {
221 object->use_holdout = use_holdout;
222 scene->object_manager->tag_update(scene);
223 object_updated = true;
224 }
225
226 if (visibility != object->visibility) {
227 object->visibility = visibility;
228 object_updated = true;
229 }
230
231 bool is_shadow_catcher = get_boolean(cobject, "is_shadow_catcher");
232 if (is_shadow_catcher != object->is_shadow_catcher) {
233 object->is_shadow_catcher = is_shadow_catcher;
234 object_updated = true;
235 }
236
237 float shadow_terminator_offset = get_float(cobject, "shadow_terminator_offset");
238 if (shadow_terminator_offset != object->shadow_terminator_offset) {
239 object->shadow_terminator_offset = shadow_terminator_offset;
240 object_updated = true;
241 }
242
243 /* sync the asset name for Cryptomatte */
244 BL::Object parent = b_ob.parent();
245 ustring parent_name;
246 if (parent) {
247 while (parent.parent()) {
248 parent = parent.parent();
249 }
250 parent_name = parent.name();
251 }
252 else {
253 parent_name = b_ob.name();
254 }
255 if (object->asset_name != parent_name) {
256 object->asset_name = parent_name;
257 object_updated = true;
258 }
259
260 /* object sync
261 * transform comparison should not be needed, but duplis don't work perfect
262 * in the depsgraph and may not signal changes, so this is a workaround */
263 if (object_updated || (object->geometry && object->geometry->need_update) ||
264 tfm != object->tfm) {
265 object->name = b_ob.name().c_str();
266 object->pass_id = b_ob.pass_index();
267 object->color = get_float3(b_ob.color());
268 object->tfm = tfm;
269 object->motion.clear();
270
271 /* motion blur */
272 Scene::MotionType need_motion = scene->need_motion();
273 if (need_motion != Scene::MOTION_NONE && object->geometry) {
274 Geometry *geom = object->geometry;
275 geom->use_motion_blur = false;
276 geom->motion_steps = 0;
277
278 uint motion_steps;
279
280 if (need_motion == Scene::MOTION_BLUR) {
281 motion_steps = object_motion_steps(b_parent, b_ob, Object::MAX_MOTION_STEPS);
282 geom->motion_steps = motion_steps;
283 if (motion_steps && object_use_deform_motion(b_parent, b_ob)) {
284 geom->use_motion_blur = true;
285 }
286 }
287 else {
288 motion_steps = 3;
289 geom->motion_steps = motion_steps;
290 }
291
292 object->motion.clear();
293 object->motion.resize(motion_steps, transform_empty());
294
295 if (motion_steps) {
296 object->motion[motion_steps / 2] = tfm;
297
298 for (size_t step = 0; step < motion_steps; step++) {
299 motion_times.insert(object->motion_time(step));
300 }
301 }
302 }
303
304 /* dupli texture coordinates and random_id */
305 if (is_instance) {
306 object->dupli_generated = 0.5f * get_float3(b_instance.orco()) -
307 make_float3(0.5f, 0.5f, 0.5f);
308 object->dupli_uv = get_float2(b_instance.uv());
309 object->random_id = b_instance.random_id();
310 }
311 else {
312 object->dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
313 object->dupli_uv = make_float2(0.0f, 0.0f);
314 object->random_id = hash_uint2(hash_string(object->name.c_str()), 0);
315 }
316
317 object->tag_update(scene);
318 }
319
320 if (is_instance) {
321 /* Sync possible particle data. */
322 sync_dupli_particle(b_parent, b_instance, object);
323 }
324
325 return object;
326 }
327
328 /* Object Loop */
329
sync_objects(BL::Depsgraph & b_depsgraph,BL::SpaceView3D & b_v3d,float motion_time)330 void BlenderSync::sync_objects(BL::Depsgraph &b_depsgraph,
331 BL::SpaceView3D &b_v3d,
332 float motion_time)
333 {
334 /* layer data */
335 bool motion = motion_time != 0.0f;
336
337 if (!motion) {
338 /* prepare for sync */
339 light_map.pre_sync();
340 geometry_map.pre_sync();
341 object_map.pre_sync();
342 particle_system_map.pre_sync();
343 motion_times.clear();
344 }
345 else {
346 geometry_motion_synced.clear();
347 }
348
349 /* initialize culling */
350 BlenderObjectCulling culling(scene, b_scene);
351
352 /* object loop */
353 bool cancel = false;
354 bool use_portal = false;
355 const bool show_lights = BlenderViewportParameters(b_v3d).use_scene_lights;
356
357 BL::ViewLayer b_view_layer = b_depsgraph.view_layer_eval();
358
359 BL::Depsgraph::object_instances_iterator b_instance_iter;
360 for (b_depsgraph.object_instances.begin(b_instance_iter);
361 b_instance_iter != b_depsgraph.object_instances.end() && !cancel;
362 ++b_instance_iter) {
363 BL::DepsgraphObjectInstance b_instance = *b_instance_iter;
364 BL::Object b_ob = b_instance.object();
365
366 /* Viewport visibility. */
367 const bool show_in_viewport = !b_v3d || b_ob.visible_in_viewport_get(b_v3d);
368 if (show_in_viewport == false) {
369 continue;
370 }
371
372 /* Load per-object culling data. */
373 culling.init_object(scene, b_ob);
374
375 /* Object itself. */
376 if (b_instance.show_self()) {
377 sync_object(b_depsgraph,
378 b_view_layer,
379 b_instance,
380 motion_time,
381 false,
382 show_lights,
383 culling,
384 &use_portal);
385 }
386
387 /* Particle hair as separate object. */
388 if (b_instance.show_particles() && object_has_particle_hair(b_ob)) {
389 sync_object(b_depsgraph,
390 b_view_layer,
391 b_instance,
392 motion_time,
393 true,
394 show_lights,
395 culling,
396 &use_portal);
397 }
398
399 cancel = progress.get_cancel();
400 }
401
402 progress.set_sync_status("");
403
404 if (!cancel && !motion) {
405 sync_background_light(b_v3d, use_portal);
406
407 /* handle removed data and modified pointers */
408 light_map.post_sync();
409 geometry_map.post_sync();
410 object_map.post_sync();
411 particle_system_map.post_sync();
412 }
413
414 if (motion)
415 geometry_motion_synced.clear();
416 }
417
sync_motion(BL::RenderSettings & b_render,BL::Depsgraph & b_depsgraph,BL::SpaceView3D & b_v3d,BL::Object & b_override,int width,int height,void ** python_thread_state)418 void BlenderSync::sync_motion(BL::RenderSettings &b_render,
419 BL::Depsgraph &b_depsgraph,
420 BL::SpaceView3D &b_v3d,
421 BL::Object &b_override,
422 int width,
423 int height,
424 void **python_thread_state)
425 {
426 if (scene->need_motion() == Scene::MOTION_NONE)
427 return;
428
429 /* get camera object here to deal with camera switch */
430 BL::Object b_cam = b_scene.camera();
431 if (b_override)
432 b_cam = b_override;
433
434 Camera prevcam = *(scene->camera);
435
436 int frame_center = b_scene.frame_current();
437 float subframe_center = b_scene.frame_subframe();
438 float frame_center_delta = 0.0f;
439
440 if (scene->need_motion() != Scene::MOTION_PASS &&
441 scene->camera->motion_position != Camera::MOTION_POSITION_CENTER) {
442 float shuttertime = scene->camera->shuttertime;
443 if (scene->camera->motion_position == Camera::MOTION_POSITION_END) {
444 frame_center_delta = -shuttertime * 0.5f;
445 }
446 else {
447 assert(scene->camera->motion_position == Camera::MOTION_POSITION_START);
448 frame_center_delta = shuttertime * 0.5f;
449 }
450
451 float time = frame_center + subframe_center + frame_center_delta;
452 int frame = (int)floorf(time);
453 float subframe = time - frame;
454 python_thread_state_restore(python_thread_state);
455 b_engine.frame_set(frame, subframe);
456 python_thread_state_save(python_thread_state);
457 if (b_cam) {
458 sync_camera_motion(b_render, b_cam, width, height, 0.0f);
459 }
460 sync_objects(b_depsgraph, b_v3d, 0.0f);
461 }
462
463 /* Insert motion times from camera. Motion times from other objects
464 * have already been added in a sync_objects call. */
465 if (b_cam) {
466 uint camera_motion_steps = object_motion_steps(b_cam, b_cam);
467 for (size_t step = 0; step < camera_motion_steps; step++) {
468 motion_times.insert(scene->camera->motion_time(step));
469 }
470 }
471
472 /* note iteration over motion_times set happens in sorted order */
473 foreach (float relative_time, motion_times) {
474 /* center time is already handled. */
475 if (relative_time == 0.0f) {
476 continue;
477 }
478
479 VLOG(1) << "Synchronizing motion for the relative time " << relative_time << ".";
480
481 /* fixed shutter time to get previous and next frame for motion pass */
482 float shuttertime = scene->motion_shutter_time();
483
484 /* compute frame and subframe time */
485 float time = frame_center + subframe_center + frame_center_delta +
486 relative_time * shuttertime * 0.5f;
487 int frame = (int)floorf(time);
488 float subframe = time - frame;
489
490 /* change frame */
491 python_thread_state_restore(python_thread_state);
492 b_engine.frame_set(frame, subframe);
493 python_thread_state_save(python_thread_state);
494
495 /* Syncs camera motion if relative_time is one of the camera's motion times. */
496 sync_camera_motion(b_render, b_cam, width, height, relative_time);
497
498 /* sync object */
499 sync_objects(b_depsgraph, b_v3d, relative_time);
500 }
501
502 /* we need to set the python thread state again because this
503 * function assumes it is being executed from python and will
504 * try to save the thread state */
505 python_thread_state_restore(python_thread_state);
506 b_engine.frame_set(frame_center, subframe_center);
507 python_thread_state_save(python_thread_state);
508
509 /* tag camera for motion update */
510 if (scene->camera->motion_modified(prevcam))
511 scene->camera->tag_update();
512 }
513
514 CCL_NAMESPACE_END
515