Move BVHType and shadingsystem enums to top-level of Cycles namespace.
[blender.git] / intern / cycles / render / 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 "device.h"
18 #include "light.h"
19 #include "mesh.h"
20 #include "curves.h"
21 #include "object.h"
22 #include "particles.h"
23 #include "scene.h"
24
25 #include "util_foreach.h"
26 #include "util_map.h"
27 #include "util_progress.h"
28 #include "util_types.h"
29 #include "util_vector.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 /* Object */
34
35 Object::Object()
36 {
37         name = "";
38         mesh = NULL;
39         tfm = transform_identity();
40         visibility = ~0;
41         random_id = 0;
42         pass_id = 0;
43         particle_system = NULL;
44         particle_index = 0;
45         bounds = BoundBox::empty;
46         motion.pre = transform_identity();
47         motion.mid = transform_identity();
48         motion.post = transform_identity();
49         use_motion = false;
50         use_holdout = false;
51         dupli_generated = make_float3(0.0f, 0.0f, 0.0f);
52         dupli_uv = make_float2(0.0f, 0.0f);
53 }
54
55 Object::~Object()
56 {
57 }
58
59 void Object::compute_bounds(bool motion_blur)
60 {
61         BoundBox mbounds = mesh->bounds;
62
63         if(motion_blur && use_motion) {
64                 DecompMotionTransform decomp;
65                 transform_motion_decompose(&decomp, &motion, &tfm);
66
67                 bounds = BoundBox::empty;
68
69                 /* todo: this is really terrible. according to pbrt there is a better
70                  * way to find this iteratively, but did not find implementation yet
71                  * or try to implement myself */
72                 for(float t = 0.0f; t < 1.0f; t += (1.0f/128.0f)) {
73                         Transform ttfm;
74
75                         transform_motion_interpolate(&ttfm, &decomp, t);
76                         bounds.grow(mbounds.transformed(&ttfm));
77                 }
78         }
79         else
80                 bounds = mbounds.transformed(&tfm);
81 }
82
83 void Object::apply_transform(bool apply_to_motion)
84 {
85         if(!mesh || tfm == transform_identity())
86                 return;
87         
88         /* triangles */
89         if(mesh->verts.size()) {
90                 /* store matrix to transform later. when accessing these as attributes we
91                  * do not want the transform to be applied for consistency between static
92                  * and dynamic BVH, so we do it on packing. */
93                 mesh->transform_normal = transform_transpose(transform_inverse(tfm));
94
95                 /* apply to mesh vertices */
96                 for(size_t i = 0; i < mesh->verts.size(); i++)
97                         mesh->verts[i] = transform_point(&tfm, mesh->verts[i]);
98                 
99                 if(apply_to_motion) {
100                         Attribute *attr = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
101
102                         if (attr) {
103                                 size_t steps_size = mesh->verts.size() * (mesh->motion_steps - 1);
104                                 float3 *vert_steps = attr->data_float3();
105
106                                 for (size_t i = 0; i < steps_size; i++)
107                                         vert_steps[i] = transform_point(&tfm, vert_steps[i]);
108                         }
109
110                         Attribute *attr_N = mesh->attributes.find(ATTR_STD_MOTION_VERTEX_NORMAL);
111
112                         if(attr_N) {
113                                 Transform ntfm = mesh->transform_normal;
114                                 size_t steps_size = mesh->verts.size() * (mesh->motion_steps - 1);
115                                 float3 *normal_steps = attr_N->data_float3();
116
117                                 for (size_t i = 0; i < steps_size; i++)
118                                         normal_steps[i] = normalize(transform_direction(&ntfm, normal_steps[i]));
119                         }
120                 }
121         }
122
123         /* curves */
124         if(mesh->curve_keys.size()) {
125                 /* compute uniform scale */
126                 float3 c0 = transform_get_column(&tfm, 0);
127                 float3 c1 = transform_get_column(&tfm, 1);
128                 float3 c2 = transform_get_column(&tfm, 2);
129                 float scalar = pow(fabsf(dot(cross(c0, c1), c2)), 1.0f/3.0f);
130
131                 /* apply transform to curve keys */
132                 for(size_t i = 0; i < mesh->curve_keys.size(); i++) {
133                         float3 co = transform_point(&tfm, float4_to_float3(mesh->curve_keys[i]));
134                         float radius = mesh->curve_keys[i].w * scalar;
135
136                         /* scale for curve radius is only correct for uniform scale */
137                         mesh->curve_keys[i] = float3_to_float4(co);
138                         mesh->curve_keys[i].w = radius;
139                 }
140
141                 if(apply_to_motion) {
142                         Attribute *curve_attr = mesh->curve_attributes.find(ATTR_STD_MOTION_VERTEX_POSITION);
143
144                         if (curve_attr) {
145                                 /* apply transform to motion curve keys */
146                                 size_t steps_size = mesh->curve_keys.size() * (mesh->motion_steps - 1);
147                                 float4 *key_steps = curve_attr->data_float4();
148
149                                 for (size_t i = 0; i < steps_size; i++) {
150                                         float3 co = transform_point(&tfm, float4_to_float3(key_steps[i]));
151                                         float radius = key_steps[i].w * scalar;
152
153                                         /* scale for curve radius is only correct for uniform scale */
154                                         key_steps[i] = float3_to_float4(co);
155                                         key_steps[i].w = radius;
156                                 }
157                         }
158                 }
159         }
160
161         /* we keep normals pointing in same direction on negative scale, notify
162          * mesh about this in it (re)calculates normals */
163         if(transform_negative_scale(tfm))
164                 mesh->transform_negative_scaled = true;
165
166         if(bounds.valid()) {
167                 mesh->compute_bounds();
168                 compute_bounds(false);
169         }
170
171         /* tfm is not reset to identity, all code that uses it needs to check the
172            transform_applied boolean */
173 }
174
175 void Object::tag_update(Scene *scene)
176 {
177         if(mesh) {
178                 if(mesh->transform_applied)
179                         mesh->need_update = true;
180
181                 foreach(uint sindex, mesh->used_shaders) {
182                         Shader *shader = scene->shaders[sindex];
183
184                         if(shader->use_mis && shader->has_surface_emission)
185                                 scene->light_manager->need_update = true;
186                 }
187         }
188
189         scene->curve_system_manager->need_update = true;
190         scene->mesh_manager->need_update = true;
191         scene->object_manager->need_update = true;
192 }
193
194 vector<float> Object::motion_times()
195 {
196         /* compute times at which we sample motion for this object */
197         vector<float> times;
198
199         if(!mesh || mesh->motion_steps == 1)
200                 return times;
201
202         int motion_steps = mesh->motion_steps;
203
204         for(int step = 0; step < motion_steps; step++) {
205                 if(step != motion_steps / 2) {
206                         float time = 2.0f * step / (motion_steps - 1) - 1.0f;
207                         times.push_back(time);
208                 }
209         }
210
211         return times;
212 }
213
214 /* Object Manager */
215
216 ObjectManager::ObjectManager()
217 {
218         need_update = true;
219 }
220
221 ObjectManager::~ObjectManager()
222 {
223 }
224
225 void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, uint *object_flag, Progress& progress)
226 {
227         float4 *objects;
228         float4 *objects_vector = NULL;
229         int i = 0;
230         map<Mesh*, float> surface_area_map;
231         map<ParticleSystem*, int> particle_offset;
232         Scene::MotionType need_motion = scene->need_motion(device->info.advanced_shading);
233         bool have_motion = false;
234         bool have_curves = false;
235
236         objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
237         if(need_motion == Scene::MOTION_PASS)
238                 objects_vector = dscene->objects_vector.resize(OBJECT_VECTOR_SIZE*scene->objects.size());
239
240         /* particle system device offsets
241          * 0 is dummy particle, index starts at 1
242          */
243         int numparticles = 1;
244         foreach(ParticleSystem *psys, scene->particle_systems) {
245                 particle_offset[psys] = numparticles;
246                 numparticles += psys->particles.size();
247         }
248
249         foreach(Object *ob, scene->objects) {
250                 Mesh *mesh = ob->mesh;
251                 uint flag = 0;
252
253                 /* compute transformations */
254                 Transform tfm = ob->tfm;
255                 Transform itfm = transform_inverse(tfm);
256
257                 /* compute surface area. for uniform scale we can do avoid the many
258                  * transform calls and share computation for instances */
259                 /* todo: correct for displacement, and move to a better place */
260                 float uniform_scale;
261                 float surface_area = 0.0f;
262                 float pass_id = ob->pass_id;
263                 float random_number = (float)ob->random_id * (1.0f/(float)0xFFFFFFFF);
264                 int particle_index = (ob->particle_system)? ob->particle_index + particle_offset[ob->particle_system]: 0;
265
266                 if(transform_uniform_scale(tfm, uniform_scale)) {
267                         map<Mesh*, float>::iterator it = surface_area_map.find(mesh);
268
269                         if(it == surface_area_map.end()) {
270                                 foreach(Mesh::Triangle& t, mesh->triangles) {
271                                         float3 p1 = mesh->verts[t.v[0]];
272                                         float3 p2 = mesh->verts[t.v[1]];
273                                         float3 p3 = mesh->verts[t.v[2]];
274
275                                         surface_area += triangle_area(p1, p2, p3);
276                                 }
277
278                                 surface_area_map[mesh] = surface_area;
279                         }
280                         else
281                                 surface_area = it->second;
282
283                         surface_area *= uniform_scale;
284                 }
285                 else {
286                         foreach(Mesh::Triangle& t, mesh->triangles) {
287                                 float3 p1 = transform_point(&tfm, mesh->verts[t.v[0]]);
288                                 float3 p2 = transform_point(&tfm, mesh->verts[t.v[1]]);
289                                 float3 p3 = transform_point(&tfm, mesh->verts[t.v[2]]);
290
291                                 surface_area += triangle_area(p1, p2, p3);
292                         }
293                 }
294
295                 /* pack in texture */
296                 int offset = i*OBJECT_SIZE;
297
298                 /* OBJECT_TRANSFORM */
299                 memcpy(&objects[offset], &tfm, sizeof(float4)*3);
300                 /* OBJECT_INVERSE_TRANSFORM */
301                 memcpy(&objects[offset+4], &itfm, sizeof(float4)*3);
302                 /* OBJECT_PROPERTIES */
303                 objects[offset+8] = make_float4(surface_area, pass_id, random_number, __int_as_float(particle_index));
304
305                 if(need_motion == Scene::MOTION_PASS) {
306                         /* motion transformations, is world/object space depending if mesh
307                          * comes with deformed position in object space, or if we transform
308                          * the shading point in world space */
309                         Transform mtfm_pre = ob->motion.pre;
310                         Transform mtfm_post = ob->motion.post;
311
312                         if(!mesh->attributes.find(ATTR_STD_MOTION_VERTEX_POSITION)) {
313                                 mtfm_pre = mtfm_pre * itfm;
314                                 mtfm_post = mtfm_post * itfm;
315                         }
316
317                         memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+0], &mtfm_pre, sizeof(float4)*3);
318                         memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+3], &mtfm_post, sizeof(float4)*3);
319                 }
320 #ifdef __OBJECT_MOTION__
321                 else if(need_motion == Scene::MOTION_BLUR) {
322                         if(ob->use_motion) {
323                                 /* decompose transformations for interpolation */
324                                 DecompMotionTransform decomp;
325
326                                 transform_motion_decompose(&decomp, &ob->motion, &ob->tfm);
327                                 memcpy(&objects[offset], &decomp, sizeof(float4)*8);
328                                 flag |= SD_OBJECT_MOTION;
329                                 have_motion = true;
330                         }
331                 }
332 #endif
333
334                 if(mesh->use_motion_blur)
335                         have_motion = true;
336
337                 /* dupli object coords and motion info */
338                 int totalsteps = mesh->motion_steps;
339                 int numsteps = (totalsteps - 1)/2;
340                 int numverts = mesh->verts.size();
341                 int numkeys = mesh->curve_keys.size();
342
343                 objects[offset+9] = make_float4(ob->dupli_generated[0], ob->dupli_generated[1], ob->dupli_generated[2], __int_as_float(numkeys));
344                 objects[offset+10] = make_float4(ob->dupli_uv[0], ob->dupli_uv[1], __int_as_float(numsteps), __int_as_float(numverts));
345
346                 /* object flag */
347                 if(ob->use_holdout)
348                         flag |= SD_HOLDOUT_MASK;
349                 object_flag[i] = flag;
350
351                 /* have curves */
352                 if(mesh->curves.size())
353                         have_curves = true;
354
355                 i++;
356
357                 if(progress.get_cancel()) return;
358         }
359
360         device->tex_alloc("__objects", dscene->objects);
361         if(need_motion == Scene::MOTION_PASS)
362                 device->tex_alloc("__objects_vector", dscene->objects_vector);
363
364         dscene->data.bvh.have_motion = have_motion;
365         dscene->data.bvh.have_curves = have_curves;
366         dscene->data.bvh.have_instancing = true;
367 }
368
369 void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
370 {
371         if(!need_update)
372                 return;
373         
374         device_free(device, dscene);
375
376         need_update = false;
377
378         if(scene->objects.size() == 0)
379                 return;
380
381         /* object info flag */
382         uint *object_flag = dscene->object_flag.resize(scene->objects.size());
383
384         /* set object transform matrices, before applying static transforms */
385         progress.set_status("Updating Objects", "Copying Transformations to device");
386         device_update_transforms(device, dscene, scene, object_flag, progress);
387
388         if(progress.get_cancel()) return;
389
390         /* prepare for static BVH building */
391         /* todo: do before to support getting object level coords? */
392         if(scene->params.bvh_type == BVHType::BVH_STATIC) {
393                 progress.set_status("Updating Objects", "Applying Static Transformations");
394                 apply_static_transforms(dscene, scene, object_flag, progress);
395         }
396
397         /* allocate object flag */
398         device->tex_alloc("__object_flag", dscene->object_flag);
399 }
400
401 void ObjectManager::device_free(Device *device, DeviceScene *dscene)
402 {
403         device->tex_free(dscene->objects);
404         dscene->objects.clear();
405
406         device->tex_free(dscene->objects_vector);
407         dscene->objects_vector.clear();
408
409         device->tex_free(dscene->object_flag);
410         dscene->object_flag.clear();
411 }
412
413 void ObjectManager::apply_static_transforms(DeviceScene *dscene, Scene *scene, uint *object_flag, Progress& progress)
414 {
415         /* todo: normals and displacement should be done before applying transform! */
416         /* todo: create objects/meshes in right order! */
417
418         /* counter mesh users */
419         map<Mesh*, int> mesh_users;
420 #ifdef __OBJECT_MOTION__
421         Scene::MotionType need_motion = scene->need_motion();
422         bool motion_blur = need_motion == Scene::MOTION_BLUR;
423         bool apply_to_motion = need_motion != Scene::MOTION_PASS;
424 #else
425         bool motion_blur = false;
426 #endif
427         int i = 0;
428         bool have_instancing = false;
429
430         foreach(Object *object, scene->objects) {
431                 map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
432
433                 if(it == mesh_users.end())
434                         mesh_users[object->mesh] = 1;
435                 else
436                         it->second++;
437         }
438
439         if(progress.get_cancel()) return;
440
441         /* apply transforms for objects with single user meshes */
442         foreach(Object *object, scene->objects) {
443                 if(mesh_users[object->mesh] == 1) {
444                         if(!(motion_blur && object->use_motion)) {
445                                 if(!object->mesh->transform_applied) {
446                                         object->apply_transform(apply_to_motion);
447                                         object->mesh->transform_applied = true;
448
449                                         if(progress.get_cancel()) return;
450                                 }
451
452                                 object_flag[i] |= SD_TRANSFORM_APPLIED;
453                         }
454                         else
455                                 have_instancing = true;
456                 }
457                 else
458                         have_instancing = true;
459
460                 i++;
461         }
462
463         dscene->data.bvh.have_instancing = have_instancing;
464 }
465
466 void ObjectManager::tag_update(Scene *scene)
467 {
468         need_update = true;
469         scene->curve_system_manager->need_update = true;
470         scene->mesh_manager->need_update = true;
471         scene->light_manager->need_update = true;
472 }
473
474 CCL_NAMESPACE_END
475