Merging r46111 through r46136 from trunk into soc-2011-tomato
[blender-staging.git] / intern / cycles / kernel / kernel_object.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 CCL_NAMESPACE_BEGIN
20
21 enum ObjectTransform {
22         OBJECT_TRANSFORM = 0,
23         OBJECT_INVERSE_TRANSFORM = 3,
24         OBJECT_PROPERTIES = 6,
25         OBJECT_TRANSFORM_MOTION_PRE = 8,
26         OBJECT_TRANSFORM_MOTION_POST = 12
27 };
28
29 __device_inline Transform object_fetch_transform(KernelGlobals *kg, int object, float time, enum ObjectTransform type)
30 {
31         Transform tfm;
32
33 #ifdef __MOTION__
34         /* if we do motion blur */
35         if(time != TIME_INVALID) {
36                 int offset = object*OBJECT_SIZE + (int)OBJECT_TRANSFORM_MOTION_PRE;
37                 float4 have_motion = kernel_tex_fetch(__objects, offset + 0);
38
39                 /* if this object have motion */
40                 if(have_motion.x != FLT_MAX) {
41                         /* fetch motion transforms */
42                         MotionTransform motion;
43
44                         motion.pre.x = have_motion;
45                         motion.pre.y = kernel_tex_fetch(__objects, offset + 1);
46                         motion.pre.z = kernel_tex_fetch(__objects, offset + 2);
47                         motion.pre.w = kernel_tex_fetch(__objects, offset + 3);
48
49                         motion.post.x = kernel_tex_fetch(__objects, offset + 4);
50                         motion.post.y = kernel_tex_fetch(__objects, offset + 5);
51                         motion.post.z = kernel_tex_fetch(__objects, offset + 6);
52                         motion.post.w = kernel_tex_fetch(__objects, offset + 7);
53
54                         /* interpolate (todo: do only once per object) */
55                         transform_motion_interpolate(&tfm, &motion, time);
56
57                         /* invert */
58                         if(type == OBJECT_INVERSE_TRANSFORM)
59                                 tfm = transform_quick_inverse(tfm);
60
61                         return tfm;
62                 }
63         }
64 #endif
65
66         int offset = object*OBJECT_SIZE + (int)type;
67
68         tfm.x = kernel_tex_fetch(__objects, offset + 0);
69         tfm.y = kernel_tex_fetch(__objects, offset + 1);
70         tfm.z = kernel_tex_fetch(__objects, offset + 2);
71         tfm.w = make_float4(0.0f, 0.0f, 0.0f, 1.0f);
72
73         return tfm;
74 }
75
76 __device_inline void object_position_transform(KernelGlobals *kg, ShaderData *sd, float3 *P)
77 {
78 #ifdef __MOTION__
79         *P = transform_point(&sd->ob_tfm, *P);
80 #else
81         Transform tfm = object_fetch_transform(kg, sd->object, TIME_INVALID, OBJECT_TRANSFORM);
82         *P = transform_point(&tfm, *P);
83 #endif
84 }
85
86 __device_inline void object_normal_transform(KernelGlobals *kg, ShaderData *sd, float3 *N)
87 {
88 #ifdef __MOTION__
89         *N = normalize(transform_direction_transposed(&sd->ob_itfm, *N));
90 #else
91         Transform tfm = object_fetch_transform(kg, sd->object, TIME_INVALID, OBJECT_INVERSE_TRANSFORM);
92         *N = normalize(transform_direction_transposed(&tfm, *N));
93 #endif
94 }
95
96 __device_inline void object_dir_transform(KernelGlobals *kg, ShaderData *sd, float3 *D)
97 {
98 #ifdef __MOTION__
99         *D = transform_direction(&sd->ob_tfm, *D);
100 #else
101         Transform tfm = object_fetch_transform(kg, sd->object, 0.0f, OBJECT_TRANSFORM);
102         *D = transform_direction(&tfm, *D);
103 #endif
104 }
105
106 __device_inline float object_surface_area(KernelGlobals *kg, int object)
107 {
108         int offset = object*OBJECT_SIZE + OBJECT_PROPERTIES;
109         float4 f = kernel_tex_fetch(__objects, offset);
110         return f.x;
111 }
112
113 __device_inline float object_pass_id(KernelGlobals *kg, int object)
114 {
115         if(object == ~0)
116                 return 0.0f;
117
118         int offset = object*OBJECT_SIZE + OBJECT_PROPERTIES;
119         float4 f = kernel_tex_fetch(__objects, offset);
120         return f.y;
121 }
122
123 CCL_NAMESPACE_END
124