Cycles: add object level holdout property, to replace Z mask layer.
[blender.git] / intern / cycles / blender / blender_object.cpp
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/integrator.h"
19 #include "render/graph.h"
20 #include "render/light.h"
21 #include "render/mesh.h"
22 #include "render/object.h"
23 #include "render/scene.h"
24 #include "render/nodes.h"
25 #include "render/particles.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
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
62 bool BlenderSync::object_is_mesh(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         if(b_ob.type() == BL::Object::type_CURVE) {
71                 /* Skip exporting curves without faces, overhead can be
72                  * significant if there are many for path animation. */
73                 BL::Curve b_curve(b_ob.data());
74
75                 return (b_curve.bevel_object() ||
76                         b_curve.extrude() != 0.0f ||
77                         b_curve.bevel_depth() != 0.0f ||
78                         b_curve.dimensions() == BL::Curve::dimensions_2D ||
79                         b_ob.modifiers.length());
80         }
81         else {
82                 return (b_ob_data.is_a(&RNA_Mesh) ||
83                         b_ob_data.is_a(&RNA_Curve) ||
84                         b_ob_data.is_a(&RNA_MetaBall));
85         }
86 }
87
88 bool BlenderSync::object_is_light(BL::Object& b_ob)
89 {
90         BL::ID b_ob_data = b_ob.data();
91
92         return (b_ob_data && b_ob_data.is_a(&RNA_Lamp));
93 }
94
95 static uint object_ray_visibility(BL::Object& b_ob)
96 {
97         PointerRNA cvisibility = RNA_pointer_get(&b_ob.ptr, "cycles_visibility");
98         uint flag = 0;
99
100         flag |= get_boolean(cvisibility, "camera")? PATH_RAY_CAMERA: 0;
101         flag |= get_boolean(cvisibility, "diffuse")? PATH_RAY_DIFFUSE: 0;
102         flag |= get_boolean(cvisibility, "glossy")? PATH_RAY_GLOSSY: 0;
103         flag |= get_boolean(cvisibility, "transmission")? PATH_RAY_TRANSMIT: 0;
104         flag |= get_boolean(cvisibility, "shadow")? PATH_RAY_SHADOW: 0;
105         flag |= get_boolean(cvisibility, "scatter")? PATH_RAY_VOLUME_SCATTER: 0;
106
107         return flag;
108 }
109
110 /* Light */
111
112 void BlenderSync::sync_light(BL::Object& b_parent,
113                              int persistent_id[OBJECT_PERSISTENT_ID_SIZE],
114                              BL::Object& b_ob,
115                              BL::Object& b_ob_instance,
116                              int random_id,
117                              Transform& tfm,
118                              bool *use_portal)
119 {
120         /* test if we need to sync */
121         Light *light;
122         ObjectKey key(b_parent, persistent_id, b_ob_instance);
123
124         if(!light_map.sync(&light, b_ob, b_parent, key)) {
125                 if(light->is_portal)
126                         *use_portal = true;
127                 return;
128         }
129         
130         BL::Lamp b_lamp(b_ob.data());
131
132         /* type */
133         switch(b_lamp.type()) {
134                 case BL::Lamp::type_POINT: {
135                         BL::PointLamp b_point_lamp(b_lamp);
136                         light->size = b_point_lamp.shadow_soft_size();
137                         light->type = LIGHT_POINT;
138                         break;
139                 }
140                 case BL::Lamp::type_SPOT: {
141                         BL::SpotLamp b_spot_lamp(b_lamp);
142                         light->size = b_spot_lamp.shadow_soft_size();
143                         light->type = LIGHT_SPOT;
144                         light->spot_angle = b_spot_lamp.spot_size();
145                         light->spot_smooth = b_spot_lamp.spot_blend();
146                         break;
147                 }
148                 case BL::Lamp::type_HEMI: {
149                         light->type = LIGHT_DISTANT;
150                         light->size = 0.0f;
151                         break;
152                 }
153                 case BL::Lamp::type_SUN: {
154                         BL::SunLamp b_sun_lamp(b_lamp);
155                         light->size = b_sun_lamp.shadow_soft_size();
156                         light->type = LIGHT_DISTANT;
157                         break;
158                 }
159                 case BL::Lamp::type_AREA: {
160                         BL::AreaLamp b_area_lamp(b_lamp);
161                         light->size = 1.0f;
162                         light->axisu = transform_get_column(&tfm, 0);
163                         light->axisv = transform_get_column(&tfm, 1);
164                         light->sizeu = b_area_lamp.size();
165                         if(b_area_lamp.shape() == BL::AreaLamp::shape_RECTANGLE)
166                                 light->sizev = b_area_lamp.size_y();
167                         else
168                                 light->sizev = light->sizeu;
169                         light->type = LIGHT_AREA;
170                         break;
171                 }
172         }
173
174         /* location and (inverted!) direction */
175         light->co = transform_get_column(&tfm, 3);
176         light->dir = -transform_get_column(&tfm, 2);
177         light->tfm = tfm;
178
179         /* shader */
180         vector<Shader*> used_shaders;
181         find_shader(b_lamp, used_shaders, scene->default_light);
182         light->shader = used_shaders[0];
183
184         /* shadow */
185         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
186         PointerRNA clamp = RNA_pointer_get(&b_lamp.ptr, "cycles");
187         light->cast_shadow = get_boolean(clamp, "cast_shadow");
188         light->use_mis = get_boolean(clamp, "use_multiple_importance_sampling");
189         
190         int samples = get_int(clamp, "samples");
191         if(get_boolean(cscene, "use_square_samples"))
192                 light->samples = samples * samples;
193         else
194                 light->samples = samples;
195
196         light->max_bounces = get_int(clamp, "max_bounces");
197
198         if(b_ob != b_ob_instance) {
199                 light->random_id = random_id;
200         }
201         else {
202                 light->random_id = hash_int_2d(hash_string(b_ob.name().c_str()), 0);
203         }
204
205         if(light->type == LIGHT_AREA)
206                 light->is_portal = get_boolean(clamp, "is_portal");
207         else
208                 light->is_portal = false;
209
210         if(light->is_portal)
211                 *use_portal = true;
212
213         /* visibility */
214         uint visibility = object_ray_visibility(b_ob);
215         light->use_diffuse = (visibility & PATH_RAY_DIFFUSE) != 0;
216         light->use_glossy = (visibility & PATH_RAY_GLOSSY) != 0;
217         light->use_transmission = (visibility & PATH_RAY_TRANSMIT) != 0;
218         light->use_scatter = (visibility & PATH_RAY_VOLUME_SCATTER) != 0;
219
220         /* tag */
221         light->tag_update(scene);
222 }
223
224 void BlenderSync::sync_background_light(bool use_portal)
225 {
226         BL::World b_world = b_scene.world();
227
228         if(b_world) {
229                 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
230                 PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
231                 bool sample_as_light = get_boolean(cworld, "sample_as_light");
232
233                 if(sample_as_light || use_portal) {
234                         /* test if we need to sync */
235                         Light *light;
236                         ObjectKey key(b_world, 0, b_world);
237
238                         if(light_map.sync(&light, b_world, b_world, key) ||
239                             world_recalc ||
240                             b_world.ptr.data != world_map)
241                         {
242                                 light->type = LIGHT_BACKGROUND;
243                                 light->map_resolution  = get_int(cworld, "sample_map_resolution");
244                                 light->shader = scene->default_background;
245                                 light->use_mis = sample_as_light;
246                                 light->max_bounces = get_int(cworld, "max_bounces");
247
248                                 int samples = get_int(cworld, "samples");
249                                 if(get_boolean(cscene, "use_square_samples"))
250                                         light->samples = samples * samples;
251                                 else
252                                         light->samples = samples;
253
254                                 light->tag_update(scene);
255                                 light_map.set_recalc(b_world);
256                         }
257                 }
258         }
259
260         world_map = b_world.ptr.data;
261         world_recalc = false;
262 }
263
264 /* Object */
265
266 Object *BlenderSync::sync_object(BL::Depsgraph::duplis_iterator& b_dupli_iter,
267                                  uint layer_flag,
268                                  float motion_time,
269                                  bool hide_tris,
270                                  BlenderObjectCulling& culling,
271                                  bool *use_portal)
272 {
273         const bool is_instance = b_dupli_iter->is_instance();
274         BL::Object b_ob = b_dupli_iter->object();
275         BL::Object b_parent = is_instance ? b_dupli_iter->parent()
276                                           : b_dupli_iter->object();
277         BL::Object b_ob_instance = is_instance ? b_dupli_iter->instance_object()
278                                                : b_ob;
279         const bool motion = motion_time != 0.0f;
280         /*const*/ Transform tfm = get_transform(b_ob.matrix_world());
281         int *persistent_id = NULL;
282         BL::Array<int, OBJECT_PERSISTENT_ID_SIZE> persistent_id_array;
283         if(is_instance) {
284                 persistent_id_array = b_dupli_iter->persistent_id();
285                 persistent_id = persistent_id_array.data;
286         }
287
288         /* light is handled separately */
289         if(object_is_light(b_ob)) {
290                 /* don't use lamps for excluded layers used as mask layer */
291                 if(!motion && !((layer_flag & view_layer.holdout_layer) &&
292                                 (layer_flag & view_layer.exclude_layer)))
293                 {
294                         sync_light(b_parent,
295                                    persistent_id,
296                                    b_ob,
297                                    b_ob_instance,
298                                    is_instance ? b_dupli_iter->random_id() : 0,
299                                    tfm,
300                                    use_portal);
301                 }
302
303                 return NULL;
304         }
305
306         /* only interested in object that we can create meshes from */
307         if(!object_is_mesh(b_ob)) {
308                 return NULL;
309         }
310
311         /* Perform object culling. */
312         if(culling.test(scene, b_ob, tfm)) {
313                 return NULL;
314         }
315
316         /* Visibility flags for both parent and child. */
317         PointerRNA cobject = RNA_pointer_get(&b_ob.ptr, "cycles");
318         bool use_holdout = (layer_flag & view_layer.holdout_layer) != 0 ||
319                            get_boolean(cobject, "is_holdout");
320         uint visibility = object_ray_visibility(b_ob) & PATH_RAY_ALL_VISIBILITY;
321
322         if(b_parent.ptr.data != b_ob.ptr.data) {
323                 visibility &= object_ray_visibility(b_parent);
324         }
325
326         /* Make holdout objects on excluded layer invisible for non-camera rays. */
327         if(use_holdout && (layer_flag & view_layer.exclude_layer)) {
328                 visibility &= ~(PATH_RAY_ALL_VISIBILITY - PATH_RAY_CAMERA);
329         }
330
331         /* Hide objects not on render layer from camera rays. */
332         if(!(layer_flag & view_layer.layer)) {
333                 visibility &= ~PATH_RAY_CAMERA;
334         }
335
336         /* Don't export completely invisible objects. */
337         if(visibility == 0) {
338                 return NULL;
339         }
340
341         /* key to lookup object */
342         ObjectKey key(b_parent, persistent_id, b_ob_instance);
343         Object *object;
344
345         /* motion vector case */
346         if(motion) {
347                 object = object_map.find(key);
348
349                 if(object && (scene->need_motion() == Scene::MOTION_PASS ||
350                               object_use_motion(b_parent, b_ob)))
351                 {
352                         /* object transformation */
353                         if(tfm != object->tfm) {
354                                 VLOG(1) << "Object " << b_ob.name() << " motion detected.";
355                                 if(motion_time == -1.0f || motion_time == 1.0f) {
356                                         object->use_motion = true;
357                                 }
358                         }
359
360                         if(motion_time == -1.0f) {
361                                 object->motion.pre = tfm;
362                         }
363                         else if(motion_time == 1.0f) {
364                                 object->motion.post = tfm;
365                         }
366
367                         /* mesh deformation */
368                         if(object->mesh)
369                                 sync_mesh_motion(b_ob, object, motion_time);
370                 }
371
372                 return object;
373         }
374
375         /* test if we need to sync */
376         bool object_updated = false;
377
378         if(object_map.sync(&object, b_ob, b_parent, key))
379                 object_updated = true;
380         
381         /* mesh sync */
382         object->mesh = sync_mesh(b_ob, b_ob_instance, object_updated, hide_tris);
383
384         /* special case not tracked by object update flags */
385
386         /* holdout */
387         if(use_holdout != object->use_holdout) {
388                 object->use_holdout = use_holdout;
389                 scene->object_manager->tag_update(scene);
390                 object_updated = true;
391         }
392
393         if(visibility != object->visibility) {
394                 object->visibility = visibility;
395                 object_updated = true;
396         }
397
398         bool is_shadow_catcher = get_boolean(cobject, "is_shadow_catcher");
399         if(is_shadow_catcher != object->is_shadow_catcher) {
400                 object->is_shadow_catcher = is_shadow_catcher;
401                 object_updated = true;
402         }
403
404         /* object sync
405          * transform comparison should not be needed, but duplis don't work perfect
406          * in the depsgraph and may not signal changes, so this is a workaround */
407         if(object_updated || (object->mesh && object->mesh->need_update) || tfm != object->tfm) {
408                 object->name = b_ob.name().c_str();
409                 object->pass_id = b_ob.pass_index();
410                 object->tfm = tfm;
411                 object->motion.pre = transform_empty();
412                 object->motion.post = transform_empty();
413                 object->use_motion = false;
414
415                 /* motion blur */
416                 if(scene->need_motion() == Scene::MOTION_BLUR && object->mesh) {
417                         Mesh *mesh = object->mesh;
418
419                         mesh->use_motion_blur = false;
420
421                         if(object_use_motion(b_parent, b_ob)) {
422                                 if(object_use_deform_motion(b_parent, b_ob)) {
423                                         mesh->motion_steps = object_motion_steps(b_ob);
424                                         mesh->use_motion_blur = true;
425                                 }
426
427                                 vector<float> times = object->motion_times();
428                                 foreach(float time, times)
429                                         motion_times.insert(time);
430                         }
431                 }
432
433                 /* dupli texture coordinates and random_id */
434                 if(is_instance) {
435                         object->dupli_generated = 0.5f*get_float3(b_dupli_iter->orco()) - make_float3(0.5f, 0.5f, 0.5f);
436                         object->dupli_uv = get_float2(b_dupli_iter->uv());
437                         object->random_id = b_dupli_iter->random_id();
438                 }
439                 else {
440                         object->dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
441                         object->dupli_uv = make_float2(0.0f, 0.0f);
442                         object->random_id =  hash_int_2d(hash_string(object->name.c_str()), 0);
443                 }
444
445                 object->tag_update(scene);
446         }
447
448         return object;
449 }
450
451 static bool object_render_hide_original(BL::Object::type_enum ob_type,
452                                         BL::Object::dupli_type_enum dupli_type)
453 {
454         /* metaball exception, they duplicate self */
455         if(ob_type == BL::Object::type_META)
456                 return false;
457
458         return (dupli_type == BL::Object::dupli_type_VERTS ||
459                 dupli_type == BL::Object::dupli_type_FACES ||
460                 dupli_type == BL::Object::dupli_type_FRAMES);
461 }
462
463 static bool object_render_hide(BL::Object& b_ob,
464                                bool top_level,
465                                bool parent_hide,
466                                bool& hide_triangles)
467 {
468         /* check if we should render or hide particle emitter */
469         BL::Object::particle_systems_iterator b_psys;
470
471         bool hair_present = false;
472         bool show_emitter = false;
473         bool hide_emitter = false;
474         bool hide_as_dupli_parent = false;
475         bool hide_as_dupli_child_original = false;
476
477         for(b_ob.particle_systems.begin(b_psys); b_psys != b_ob.particle_systems.end(); ++b_psys) {
478                 if((b_psys->settings().render_type() == BL::ParticleSettings::render_type_PATH) &&
479                    (b_psys->settings().type()==BL::ParticleSettings::type_HAIR))
480                         hair_present = true;
481
482                 if(b_psys->settings().use_render_emitter())
483                         show_emitter = true;
484                 else
485                         hide_emitter = true;
486         }
487
488         if(show_emitter)
489                 hide_emitter = false;
490
491         /* duplicators hidden by default, except dupliframes which duplicate self */
492         if(b_ob.is_duplicator())
493                 if(top_level || b_ob.dupli_type() != BL::Object::dupli_type_FRAMES)
494                         hide_as_dupli_parent = true;
495
496         /* hide original object for duplis */
497         BL::Object parent = b_ob.parent();
498         while(parent) {
499                 if(object_render_hide_original(b_ob.type(),
500                                                parent.dupli_type()))
501                 {
502                         if(parent_hide) {
503                                 hide_as_dupli_child_original = true;
504                                 break;
505                         }
506                 }
507                 parent = parent.parent();
508         }
509         
510         hide_triangles = hide_emitter;
511
512         if(show_emitter) {
513                 return false;
514         }
515         else if(hair_present) {
516                 return hide_as_dupli_child_original;
517         }
518         else {
519                 return (hide_as_dupli_parent || hide_as_dupli_child_original);
520         }
521 }
522
523 /* Object Loop */
524
525 void BlenderSync::sync_objects(float motion_time)
526 {
527         /* layer data */
528         bool motion = motion_time != 0.0f;
529         
530         if(!motion) {
531                 /* prepare for sync */
532                 light_map.pre_sync();
533                 mesh_map.pre_sync();
534                 object_map.pre_sync();
535                 particle_system_map.pre_sync();
536                 motion_times.clear();
537         }
538         else {
539                 mesh_motion_synced.clear();
540         }
541
542         /* initialize culling */
543         BlenderObjectCulling culling(scene, b_scene);
544
545         /* object loop */
546         bool cancel = false;
547         bool use_portal = false;
548
549         BL::Depsgraph::duplis_iterator b_dupli_iter;
550         for(b_depsgraph.duplis.begin(b_dupli_iter);
551             b_dupli_iter != b_depsgraph.duplis.end() && !cancel;
552             ++b_dupli_iter)
553         {
554                 BL::Object b_ob = b_dupli_iter->object();
555                 if(!b_ob.is_visible()) {
556                         continue;
557                 }
558
559                 progress.set_sync_status("Synchronizing object", b_ob.name());
560
561                 /* load per-object culling data */
562                 culling.init_object(scene, b_ob);
563
564                 /* test if object needs to be hidden */
565                 bool hide_tris;
566
567                  if(!object_render_hide(b_ob, true, true, hide_tris)) {
568                         /* object itself */
569                         sync_object(b_dupli_iter,
570                                     ~(0), /* until we get rid of layers */
571                                     motion_time,
572                                     hide_tris,
573                                     culling,
574                                     &use_portal);
575                  }
576
577                 cancel = progress.get_cancel();
578         }
579
580         progress.set_sync_status("");
581
582         if(!cancel && !motion) {
583                 sync_background_light(use_portal);
584
585                 /* handle removed data and modified pointers */
586                 if(light_map.post_sync())
587                         scene->light_manager->tag_update(scene);
588                 if(mesh_map.post_sync())
589                         scene->mesh_manager->tag_update(scene);
590                 if(object_map.post_sync())
591                         scene->object_manager->tag_update(scene);
592                 if(particle_system_map.post_sync())
593                         scene->particle_system_manager->tag_update(scene);
594         }
595
596         if(motion)
597                 mesh_motion_synced.clear();
598 }
599
600 void BlenderSync::sync_motion(BL::RenderSettings& b_render,
601                               BL::Object& b_override,
602                               int width, int height,
603                               void **python_thread_state)
604 {
605         if(scene->need_motion() == Scene::MOTION_NONE)
606                 return;
607
608         /* get camera object here to deal with camera switch */
609         BL::Object b_cam = b_scene.camera();
610         if(b_override)
611                 b_cam = b_override;
612
613         Camera prevcam = *(scene->camera);
614
615         int frame_center = b_scene.frame_current();
616         float subframe_center = b_scene.frame_subframe();
617         float frame_center_delta = 0.0f;
618
619         if(scene->need_motion() != Scene::MOTION_PASS &&
620            scene->camera->motion_position != Camera::MOTION_POSITION_CENTER)
621         {
622                 float shuttertime = scene->camera->shuttertime;
623                 if(scene->camera->motion_position == Camera::MOTION_POSITION_END) {
624                         frame_center_delta = -shuttertime * 0.5f;
625                 }
626                 else {
627                         assert(scene->camera->motion_position == Camera::MOTION_POSITION_START);
628                         frame_center_delta = shuttertime * 0.5f;
629                 }
630                 float time = frame_center + subframe_center + frame_center_delta;
631                 int frame = (int)floorf(time);
632                 float subframe = time - frame;
633                 python_thread_state_restore(python_thread_state);
634                 b_engine.frame_set(frame, subframe);
635                 python_thread_state_save(python_thread_state);
636                 sync_camera_motion(b_render, b_cam, width, height, 0.0f);
637                 sync_objects(0.0f);
638         }
639
640         /* always sample these times for camera motion */
641         motion_times.insert(-1.0f);
642         motion_times.insert(1.0f);
643
644         /* note iteration over motion_times set happens in sorted order */
645         foreach(float relative_time, motion_times) {
646                 VLOG(1) << "Synchronizing motion for the relative time "
647                         << relative_time << ".";
648
649                 /* fixed shutter time to get previous and next frame for motion pass */
650                 float shuttertime = scene->motion_shutter_time();
651
652                 /* compute frame and subframe time */
653                 float time = frame_center + subframe_center + frame_center_delta + relative_time * shuttertime * 0.5f;
654                 int frame = (int)floorf(time);
655                 float subframe = time - frame;
656
657                 /* change frame */
658                 python_thread_state_restore(python_thread_state);
659                 b_engine.frame_set(frame, subframe);
660                 python_thread_state_save(python_thread_state);
661
662                 /* sync camera, only supports two times at the moment */
663                 if(relative_time == -1.0f || relative_time == 1.0f) {
664                         sync_camera_motion(b_render,
665                                            b_cam,
666                                            width, height,
667                                            relative_time);
668                 }
669
670                 /* sync object */
671                 sync_objects(relative_time);
672         }
673
674         /* we need to set the python thread state again because this
675          * function assumes it is being executed from python and will
676          * try to save the thread state */
677         python_thread_state_restore(python_thread_state);
678         b_engine.frame_set(frame_center, subframe_center);
679         python_thread_state_save(python_thread_state);
680
681         /* tag camera for motion update */
682         if(scene->camera->motion_modified(prevcam))
683                 scene->camera->tag_update();
684 }
685
686 CCL_NAMESPACE_END
687