Cycles: Added Cryptomatte output.
[blender.git] / intern / cycles / kernel / geom / geom_object.h
1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14
15 /* Object Primitive
16  *
17  * All mesh and curve primitives are part of an object. The same mesh and curves
18  * may be instanced multiple times by different objects.
19  *
20  * If the mesh is not instanced multiple times, the object will not be explicitly
21  * stored as a primitive in the BVH, rather the bare triangles are curved are
22  * directly primitives in the BVH with world space locations applied, and the object
23  * ID is looked up afterwards. */
24
25 CCL_NAMESPACE_BEGIN
26
27 /* Object attributes, for now a fixed size and contents */
28
29 enum ObjectTransform {
30         OBJECT_TRANSFORM = 0,
31         OBJECT_INVERSE_TRANSFORM = 1,
32 };
33
34 enum ObjectVectorTransform {
35         OBJECT_PASS_MOTION_PRE = 0,
36         OBJECT_PASS_MOTION_POST = 1
37 };
38
39 /* Object to world space transformation */
40
41 ccl_device_inline Transform object_fetch_transform(KernelGlobals *kg, int object, enum ObjectTransform type)
42 {
43         if(type == OBJECT_INVERSE_TRANSFORM) {
44                 return kernel_tex_fetch(__objects, object).itfm;
45         }
46         else {
47                 return kernel_tex_fetch(__objects, object).tfm;
48         }
49 }
50
51 /* Lamp to world space transformation */
52
53 ccl_device_inline Transform lamp_fetch_transform(KernelGlobals *kg, int lamp, bool inverse)
54 {
55         if(inverse) {
56                 return kernel_tex_fetch(__lights, lamp).itfm;
57         }
58         else {
59                 return kernel_tex_fetch(__lights, lamp).tfm;
60         }
61 }
62
63 /* Object to world space transformation for motion vectors */
64
65 ccl_device_inline Transform object_fetch_motion_pass_transform(KernelGlobals *kg, int object, enum ObjectVectorTransform type)
66 {
67         int offset = object*OBJECT_MOTION_PASS_SIZE + (int)type;
68         return kernel_tex_fetch(__object_motion_pass, offset);
69 }
70
71 /* Motion blurred object transformations */
72
73 #ifdef __OBJECT_MOTION__
74 ccl_device_inline Transform object_fetch_transform_motion(KernelGlobals *kg, int object, float time)
75 {
76         const uint motion_offset = kernel_tex_fetch(__objects, object).motion_offset;
77         const ccl_global DecomposedTransform *motion = &kernel_tex_fetch(__object_motion, motion_offset);
78         const uint num_steps = kernel_tex_fetch(__objects, object).numsteps * 2 + 1;
79
80         Transform tfm;
81         transform_motion_array_interpolate(&tfm, motion, num_steps, time);
82
83         return tfm;
84 }
85
86 ccl_device_inline Transform object_fetch_transform_motion_test(KernelGlobals *kg, int object, float time, Transform *itfm)
87 {
88         int object_flag = kernel_tex_fetch(__object_flag, object);
89         if(object_flag & SD_OBJECT_MOTION) {
90                 /* if we do motion blur */
91                 Transform tfm = object_fetch_transform_motion(kg, object, time);
92
93                 if(itfm)
94                         *itfm = transform_quick_inverse(tfm);
95
96                 return tfm;
97         }
98         else {
99                 Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
100                 if(itfm)
101                         *itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
102
103                 return tfm;
104         }
105 }
106 #endif
107
108 /* Transform position from object to world space */
109
110 ccl_device_inline void object_position_transform(KernelGlobals *kg, const ShaderData *sd, float3 *P)
111 {
112 #ifdef __OBJECT_MOTION__
113         *P = transform_point_auto(&sd->ob_tfm, *P);
114 #else
115         Transform tfm = object_fetch_transform(kg, sd->object, OBJECT_TRANSFORM);
116         *P = transform_point(&tfm, *P);
117 #endif
118 }
119
120 /* Transform position from world to object space */
121
122 ccl_device_inline void object_inverse_position_transform(KernelGlobals *kg, const ShaderData *sd, float3 *P)
123 {
124 #ifdef __OBJECT_MOTION__
125         *P = transform_point_auto(&sd->ob_itfm, *P);
126 #else
127         Transform tfm = object_fetch_transform(kg, sd->object, OBJECT_INVERSE_TRANSFORM);
128         *P = transform_point(&tfm, *P);
129 #endif
130 }
131
132 /* Transform normal from world to object space */
133
134 ccl_device_inline void object_inverse_normal_transform(KernelGlobals *kg, const ShaderData *sd, float3 *N)
135 {
136 #ifdef __OBJECT_MOTION__
137         if((sd->object != OBJECT_NONE) || (sd->type == PRIMITIVE_LAMP)) {
138                 *N = normalize(transform_direction_transposed_auto(&sd->ob_tfm, *N));
139         }
140 #else
141         if(sd->object != OBJECT_NONE) {
142                 Transform tfm = object_fetch_transform(kg, sd->object, OBJECT_TRANSFORM);
143                 *N = normalize(transform_direction_transposed(&tfm, *N));
144         }
145         else if(sd->type == PRIMITIVE_LAMP) {
146                 Transform tfm = lamp_fetch_transform(kg, sd->lamp, false);
147                 *N = normalize(transform_direction_transposed(&tfm, *N));
148         }
149 #endif
150 }
151
152 /* Transform normal from object to world space */
153
154 ccl_device_inline void object_normal_transform(KernelGlobals *kg, const ShaderData *sd, float3 *N)
155 {
156 #ifdef __OBJECT_MOTION__
157         *N = normalize(transform_direction_transposed_auto(&sd->ob_itfm, *N));
158 #else
159         Transform tfm = object_fetch_transform(kg, sd->object, OBJECT_INVERSE_TRANSFORM);
160         *N = normalize(transform_direction_transposed(&tfm, *N));
161 #endif
162 }
163
164 /* Transform direction vector from object to world space */
165
166 ccl_device_inline void object_dir_transform(KernelGlobals *kg, const ShaderData *sd, float3 *D)
167 {
168 #ifdef __OBJECT_MOTION__
169         *D = transform_direction_auto(&sd->ob_tfm, *D);
170 #else
171         Transform tfm = object_fetch_transform(kg, sd->object, OBJECT_TRANSFORM);
172         *D = transform_direction(&tfm, *D);
173 #endif
174 }
175
176 /* Transform direction vector from world to object space */
177
178 ccl_device_inline void object_inverse_dir_transform(KernelGlobals *kg, const ShaderData *sd, float3 *D)
179 {
180 #ifdef __OBJECT_MOTION__
181         *D = transform_direction_auto(&sd->ob_itfm, *D);
182 #else
183         Transform tfm = object_fetch_transform(kg, sd->object, OBJECT_INVERSE_TRANSFORM);
184         *D = transform_direction(&tfm, *D);
185 #endif
186 }
187
188 /* Object center position */
189
190 ccl_device_inline float3 object_location(KernelGlobals *kg, const ShaderData *sd)
191 {
192         if(sd->object == OBJECT_NONE)
193                 return make_float3(0.0f, 0.0f, 0.0f);
194
195 #ifdef __OBJECT_MOTION__
196         return make_float3(sd->ob_tfm.x.w, sd->ob_tfm.y.w, sd->ob_tfm.z.w);
197 #else
198         Transform tfm = object_fetch_transform(kg, sd->object, OBJECT_TRANSFORM);
199         return make_float3(tfm.x.w, tfm.y.w, tfm.z.w);
200 #endif
201 }
202
203 /* Total surface area of object */
204
205 ccl_device_inline float object_surface_area(KernelGlobals *kg, int object)
206 {
207         return kernel_tex_fetch(__objects, object).surface_area;
208 }
209
210 /* Pass ID number of object */
211
212 ccl_device_inline float object_pass_id(KernelGlobals *kg, int object)
213 {
214         if(object == OBJECT_NONE)
215                 return 0.0f;
216
217         return kernel_tex_fetch(__objects, object).pass_id;
218 }
219
220 /* Per lamp random number for shader variation */
221
222 ccl_device_inline float lamp_random_number(KernelGlobals *kg, int lamp)
223 {
224         if(lamp == LAMP_NONE)
225                 return 0.0f;
226
227         return kernel_tex_fetch(__lights, lamp).random;
228 }
229
230 /* Per object random number for shader variation */
231
232 ccl_device_inline float object_random_number(KernelGlobals *kg, int object)
233 {
234         if(object == OBJECT_NONE)
235                 return 0.0f;
236
237         return kernel_tex_fetch(__objects, object).random_number;
238 }
239
240 /* Particle ID from which this object was generated */
241
242 ccl_device_inline int object_particle_id(KernelGlobals *kg, int object)
243 {
244         if(object == OBJECT_NONE)
245                 return 0;
246
247         return kernel_tex_fetch(__objects, object).particle_index;
248 }
249
250 /* Generated texture coordinate on surface from where object was instanced */
251
252 ccl_device_inline float3 object_dupli_generated(KernelGlobals *kg, int object)
253 {
254         if(object == OBJECT_NONE)
255                 return make_float3(0.0f, 0.0f, 0.0f);
256
257         const ccl_global KernelObject *kobject = &kernel_tex_fetch(__objects, object);
258         return make_float3(kobject->dupli_generated[0],
259                            kobject->dupli_generated[1],
260                            kobject->dupli_generated[2]);
261 }
262
263 /* UV texture coordinate on surface from where object was instanced */
264
265 ccl_device_inline float3 object_dupli_uv(KernelGlobals *kg, int object)
266 {
267         if(object == OBJECT_NONE)
268                 return make_float3(0.0f, 0.0f, 0.0f);
269
270         const ccl_global KernelObject *kobject = &kernel_tex_fetch(__objects, object);
271         return make_float3(kobject->dupli_uv[0],
272                            kobject->dupli_uv[1],
273                            0.0f);
274 }
275
276 /* Information about mesh for motion blurred triangles and curves */
277
278 ccl_device_inline void object_motion_info(KernelGlobals *kg, int object, int *numsteps, int *numverts, int *numkeys)
279 {
280         if(numkeys) {
281                 *numkeys = kernel_tex_fetch(__objects, object).numkeys;
282         }
283
284         if(numsteps)
285                 *numsteps = kernel_tex_fetch(__objects, object).numsteps;
286         if(numverts)
287                 *numverts = kernel_tex_fetch(__objects, object).numverts;
288 }
289
290 /* Offset to an objects patch map */
291
292 ccl_device_inline uint object_patch_map_offset(KernelGlobals *kg, int object)
293 {
294         if(object == OBJECT_NONE)
295                 return 0;
296
297         return kernel_tex_fetch(__objects, object).patch_map_offset;
298 }
299
300 /* Pass ID for shader */
301
302 ccl_device int shader_pass_id(KernelGlobals *kg, const ShaderData *sd)
303 {
304         return kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).pass_id;
305 }
306
307 /* Cryptomatte ID */
308
309 ccl_device_inline float object_cryptomatte_id(KernelGlobals *kg, int object)
310 {
311         if(object == OBJECT_NONE)
312                 return 0.0f;
313
314         return kernel_tex_fetch(__objects, object).cryptomatte_object;
315 }
316
317 ccl_device_inline float object_cryptomatte_asset_id(KernelGlobals *kg, int object)
318 {
319         if(object == OBJECT_NONE)
320                 return 0;
321
322         return kernel_tex_fetch(__objects, object).cryptomatte_asset;
323 }
324
325 /* Particle data from which object was instanced */
326
327 ccl_device_inline uint particle_index(KernelGlobals *kg, int particle)
328 {
329         return kernel_tex_fetch(__particles, particle).index;
330 }
331
332 ccl_device float particle_age(KernelGlobals *kg, int particle)
333 {
334         return kernel_tex_fetch(__particles, particle).age;
335 }
336
337 ccl_device float particle_lifetime(KernelGlobals *kg, int particle)
338 {
339         return kernel_tex_fetch(__particles, particle).lifetime;
340 }
341
342 ccl_device float particle_size(KernelGlobals *kg, int particle)
343 {
344         return kernel_tex_fetch(__particles, particle).size;
345 }
346
347 ccl_device float4 particle_rotation(KernelGlobals *kg, int particle)
348 {
349         return kernel_tex_fetch(__particles, particle).rotation;
350 }
351
352 ccl_device float3 particle_location(KernelGlobals *kg, int particle)
353 {
354         return float4_to_float3(kernel_tex_fetch(__particles, particle).location);
355 }
356
357 ccl_device float3 particle_velocity(KernelGlobals *kg, int particle)
358 {
359         return float4_to_float3(kernel_tex_fetch(__particles, particle).velocity);
360 }
361
362 ccl_device float3 particle_angular_velocity(KernelGlobals *kg, int particle)
363 {
364         return float4_to_float3(kernel_tex_fetch(__particles, particle).angular_velocity);
365 }
366
367 /* Object intersection in BVH */
368
369 ccl_device_inline float3 bvh_clamp_direction(float3 dir)
370 {
371         /* clamp absolute values by exp2f(-80.0f) to avoid division by zero when calculating inverse direction */
372 #if defined(__KERNEL_SSE__) && defined(__KERNEL_SSE2__)
373         const ssef oopes(8.271806E-25f,8.271806E-25f,8.271806E-25f,0.0f);
374         const ssef mask = _mm_cmpgt_ps(fabs(dir), oopes);
375         const ssef signdir = signmsk(dir.m128) | oopes;
376 #  ifndef __KERNEL_AVX__
377         ssef res = mask & ssef(dir);
378         res = _mm_or_ps(res,_mm_andnot_ps(mask, signdir));
379 #  else
380         ssef res = _mm_blendv_ps(signdir, dir, mask);
381 #  endif
382         return float3(res);
383 #else  /* __KERNEL_SSE__ && __KERNEL_SSE2__ */
384         const float ooeps = 8.271806E-25f;
385         return make_float3((fabsf(dir.x) > ooeps)? dir.x: copysignf(ooeps, dir.x),
386                            (fabsf(dir.y) > ooeps)? dir.y: copysignf(ooeps, dir.y),
387                            (fabsf(dir.z) > ooeps)? dir.z: copysignf(ooeps, dir.z));
388 #endif  /* __KERNEL_SSE__ && __KERNEL_SSE2__ */
389 }
390
391 ccl_device_inline float3 bvh_inverse_direction(float3 dir)
392 {
393         return rcp(dir);
394 }
395
396 /* Transform ray into object space to enter static object in BVH */
397
398 ccl_device_inline float bvh_instance_push(KernelGlobals *kg,
399                                           int object,
400                                           const Ray *ray,
401                                           float3 *P,
402                                           float3 *dir,
403                                           float3 *idir,
404                                           float t)
405 {
406         Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
407
408         *P = transform_point(&tfm, ray->P);
409
410         float len;
411         *dir = bvh_clamp_direction(normalize_len(transform_direction(&tfm, ray->D), &len));
412         *idir = bvh_inverse_direction(*dir);
413
414         if(t != FLT_MAX) {
415                 t *= len;
416         }
417
418         return t;
419 }
420
421 #ifdef __QBVH__
422 /* Same as above, but optimized for QBVH scene intersection,
423  * which needs to modify two max distances.
424  *
425  * TODO(sergey): Investigate if passing NULL instead of t1 gets optimized
426  * so we can avoid having this duplication.
427  */
428 ccl_device_inline void qbvh_instance_push(KernelGlobals *kg,
429                                           int object,
430                                           const Ray *ray,
431                                           float3 *P,
432                                           float3 *dir,
433                                           float3 *idir,
434                                           float *t,
435                                           float *t1)
436 {
437         Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
438
439         *P = transform_point(&tfm, ray->P);
440
441         float len;
442         *dir = bvh_clamp_direction(normalize_len(transform_direction(&tfm, ray->D), &len));
443         *idir = bvh_inverse_direction(*dir);
444
445         if(*t != FLT_MAX)
446                 *t *= len;
447
448         if(*t1 != -FLT_MAX)
449                 *t1 *= len;
450 }
451 #endif
452
453 /* Transorm ray to exit static object in BVH */
454
455 ccl_device_inline float bvh_instance_pop(KernelGlobals *kg,
456                                          int object,
457                                          const Ray *ray,
458                                          float3 *P,
459                                          float3 *dir,
460                                          float3 *idir,
461                                          float t)
462 {
463         if(t != FLT_MAX) {
464                 Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
465                 t /= len(transform_direction(&tfm, ray->D));
466         }
467
468         *P = ray->P;
469         *dir = bvh_clamp_direction(ray->D);
470         *idir = bvh_inverse_direction(*dir);
471
472         return t;
473 }
474
475 /* Same as above, but returns scale factor to apply to multiple intersection distances */
476
477 ccl_device_inline void bvh_instance_pop_factor(KernelGlobals *kg, int object, const Ray *ray, float3 *P, float3 *dir, float3 *idir, float *t_fac)
478 {
479         Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
480         *t_fac = 1.0f / len(transform_direction(&tfm, ray->D));
481
482         *P = ray->P;
483         *dir = bvh_clamp_direction(ray->D);
484         *idir = bvh_inverse_direction(*dir);
485 }
486
487
488 #ifdef __OBJECT_MOTION__
489 /* Transform ray into object space to enter motion blurred object in BVH */
490
491 ccl_device_inline float bvh_instance_motion_push(KernelGlobals *kg,
492                                                 int object,
493                                                 const Ray *ray,
494                                                 float3 *P,
495                                                 float3 *dir,
496                                                 float3 *idir,
497                                                 float t,
498                                                 Transform *itfm)
499 {
500         object_fetch_transform_motion_test(kg, object, ray->time, itfm);
501
502         *P = transform_point(itfm, ray->P);
503
504         float len;
505         *dir = bvh_clamp_direction(normalize_len(transform_direction(itfm, ray->D), &len));
506         *idir = bvh_inverse_direction(*dir);
507
508         if(t != FLT_MAX) {
509                 t *= len;
510         }
511
512         return t;
513 }
514
515 #ifdef __QBVH__
516 /* Same as above, but optimized for QBVH scene intersection,
517  * which needs to modify two max distances.
518  *
519  * TODO(sergey): Investigate if passing NULL instead of t1 gets optimized
520  * so we can avoid having this duplication.
521  */
522 ccl_device_inline void qbvh_instance_motion_push(KernelGlobals *kg,
523                                                  int object,
524                                                  const Ray *ray,
525                                                  float3 *P,
526                                                  float3 *dir,
527                                                  float3 *idir,
528                                                  float *t,
529                                                  float *t1,
530                                                  Transform *itfm)
531 {
532         object_fetch_transform_motion_test(kg, object, ray->time, itfm);
533
534         *P = transform_point(itfm, ray->P);
535
536         float len;
537         *dir = bvh_clamp_direction(normalize_len(transform_direction(itfm, ray->D), &len));
538         *idir = bvh_inverse_direction(*dir);
539
540         if(*t != FLT_MAX)
541                 *t *= len;
542
543         if(*t1 != -FLT_MAX)
544                 *t1 *= len;
545 }
546 #endif
547
548 /* Transorm ray to exit motion blurred object in BVH */
549
550 ccl_device_inline float bvh_instance_motion_pop(KernelGlobals *kg,
551                                                 int object,
552                                                 const Ray *ray,
553                                                 float3 *P,
554                                                 float3 *dir,
555                                                 float3 *idir,
556                                                 float t,
557                                                 Transform *itfm)
558 {
559         if(t != FLT_MAX) {
560                 t /= len(transform_direction(itfm, ray->D));
561         }
562
563         *P = ray->P;
564         *dir = bvh_clamp_direction(ray->D);
565         *idir = bvh_inverse_direction(*dir);
566
567         return t;
568 }
569
570 /* Same as above, but returns scale factor to apply to multiple intersection distances */
571
572 ccl_device_inline void bvh_instance_motion_pop_factor(KernelGlobals *kg,
573                                                       int object,
574                                                       const Ray *ray,
575                                                       float3 *P,
576                                                       float3 *dir,
577                                                       float3 *idir,
578                                                       float *t_fac,
579                                                       Transform *itfm)
580 {
581         *t_fac = 1.0f / len(transform_direction(itfm, ray->D));
582         *P = ray->P;
583         *dir = bvh_clamp_direction(ray->D);
584         *idir = bvh_inverse_direction(*dir);
585 }
586
587 #endif
588
589 /* TODO(sergey): This is only for until we've got OpenCL 2.0
590  * on all devices we consider supported. It'll be replaced with
591  * generic address space.
592  */
593
594 #ifdef __KERNEL_OPENCL__
595 ccl_device_inline void object_position_transform_addrspace(KernelGlobals *kg,
596                                                          const ShaderData *sd,
597                                                          ccl_addr_space float3 *P)
598 {
599         float3 private_P = *P;
600         object_position_transform(kg, sd, &private_P);
601         *P = private_P;
602 }
603
604 ccl_device_inline void object_dir_transform_addrspace(KernelGlobals *kg,
605                                                       const ShaderData *sd,
606                                                       ccl_addr_space float3 *D)
607 {
608         float3 private_D = *D;
609         object_dir_transform(kg, sd, &private_D);
610         *D = private_D;
611 }
612
613 ccl_device_inline void object_normal_transform_addrspace(KernelGlobals *kg,
614                                                          const ShaderData *sd,
615                                                          ccl_addr_space float3 *N)
616 {
617         float3 private_N = *N;
618         object_normal_transform(kg, sd, &private_N);
619         *N = private_N;
620 }
621 #endif
622
623 #ifndef __KERNEL_OPENCL__
624 #  define object_position_transform_auto object_position_transform
625 #  define object_dir_transform_auto object_dir_transform
626 #  define object_normal_transform_auto object_normal_transform
627 #else
628 #  define object_position_transform_auto object_position_transform_addrspace
629 #  define object_dir_transform_auto object_dir_transform_addrspace
630 #  define object_normal_transform_auto object_normal_transform_addrspace
631 #endif
632
633 CCL_NAMESPACE_END