style cleanup
[blender.git] / intern / cycles / kernel / svm / svm_tex_coord.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 /* Texture Coordinate Node */
22
23 __device_inline float3 svm_background_offset(KernelGlobals *kg)
24 {
25         Transform cameratoworld = kernel_data.cam.cameratoworld;
26         return make_float3(cameratoworld.x.w, cameratoworld.y.w, cameratoworld.z.w);
27 }
28
29 __device_inline float3 svm_world_to_ndc(KernelGlobals *kg, ShaderData *sd, float3 P)
30 {
31         if(kernel_data.cam.type != CAMERA_PANORAMA) {
32                 if(sd->object != ~0)
33                         P += svm_background_offset(kg);
34
35                 Transform tfm = kernel_data.cam.worldtondc;
36                 return transform_perspective(&tfm, P);
37         }
38         else {
39                 Transform tfm = kernel_data.cam.worldtocamera;
40
41                 if(sd->object != ~0)
42                         P = normalize(transform_point(&tfm, P));
43                 else
44                         P = normalize(transform_direction(&tfm, P));
45
46                 float2 uv = direction_to_panorama(kg, P);
47
48                 return make_float3(uv.x, uv.y, 0.0f);
49         }
50 }
51
52 __device void svm_node_tex_coord(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
53 {
54         float3 data;
55
56         switch(type) {
57                 case NODE_TEXCO_OBJECT: {
58                         if(sd->object != ~0) {
59                                 data = sd->P;
60                                 object_inverse_position_transform(kg, sd, &data);
61                         }
62                         else
63                                 data = sd->P;
64                         break;
65                 }
66                 case NODE_TEXCO_NORMAL: {
67                         if(sd->object != ~0) {
68                                 data = sd->N;
69                                 object_inverse_normal_transform(kg, sd, &data);
70                         }
71                         else
72                                 data = sd->N;
73                         break;
74                 }
75                 case NODE_TEXCO_CAMERA: {
76                         Transform tfm = kernel_data.cam.worldtocamera;
77
78                         if(sd->object != ~0)
79                                 data = transform_point(&tfm, sd->P);
80                         else
81                                 data = transform_point(&tfm, sd->P + svm_background_offset(kg));
82                         break;
83                 }
84                 case NODE_TEXCO_WINDOW: {
85                         data = svm_world_to_ndc(kg, sd, sd->P);
86                         break;
87                 }
88                 case NODE_TEXCO_REFLECTION: {
89                         if(sd->object != ~0)
90                                 data = 2.0f*dot(sd->N, sd->I)*sd->N - sd->I;
91                         else
92                                 data = sd->I;
93                         break;
94                 }
95         }
96
97         stack_store_float3(stack, out_offset, data);
98 }
99
100 __device void svm_node_tex_coord_bump_dx(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
101 {
102 #ifdef __RAY_DIFFERENTIALS__
103         float3 data;
104
105         switch(type) {
106                 case NODE_TEXCO_OBJECT: {
107                         if(sd->object != ~0) {
108                                 data = sd->P + sd->dP.dx;
109                                 object_inverse_position_transform(kg, sd, &data);
110                         }
111                         else
112                                 data = sd->P + sd->dP.dx;
113                         break;
114                 }
115                 case NODE_TEXCO_NORMAL: {
116                         if(sd->object != ~0) {
117                                 data = sd->N;
118                                 object_inverse_normal_transform(kg, sd, &data);
119                         }
120                         else
121                                 data = sd->N;
122                         break;
123                 }
124                 case NODE_TEXCO_CAMERA: {
125                         Transform tfm = kernel_data.cam.worldtocamera;
126
127                         if(sd->object != ~0)
128                                 data = transform_point(&tfm, sd->P + sd->dP.dx);
129                         else
130                                 data = transform_point(&tfm, sd->P + sd->dP.dx + svm_background_offset(kg));
131                         break;
132                 }
133                 case NODE_TEXCO_WINDOW: {
134                         data = svm_world_to_ndc(kg, sd, sd->P + sd->dP.dx);
135                         break;
136                 }
137                 case NODE_TEXCO_REFLECTION: {
138                         if(sd->object != ~0)
139                                 data = 2.0f*dot(sd->N, sd->I)*sd->N - sd->I;
140                         else
141                                 data = sd->I;
142                         break;
143                 }
144         }
145
146         stack_store_float3(stack, out_offset, data);
147 #else
148         svm_node_tex_coord(kg, sd, stack, type, out_offset);
149 #endif
150 }
151
152 __device void svm_node_tex_coord_bump_dy(KernelGlobals *kg, ShaderData *sd, float *stack, uint type, uint out_offset)
153 {
154 #ifdef __RAY_DIFFERENTIALS__
155         float3 data;
156
157         switch(type) {
158                 case NODE_TEXCO_OBJECT: {
159                         if(sd->object != ~0) {
160                                 data = sd->P + sd->dP.dy;
161                                 object_inverse_position_transform(kg, sd, &data);
162                         }
163                         else
164                                 data = sd->P + sd->dP.dy;
165                         break;
166                 }
167                 case NODE_TEXCO_NORMAL: {
168                         if(sd->object != ~0) {
169                                 data = sd->N;
170                                 object_inverse_normal_transform(kg, sd, &data);
171                         }
172                         else
173                                 data = sd->N;
174                         break;
175                 }
176                 case NODE_TEXCO_CAMERA: {
177                         Transform tfm = kernel_data.cam.worldtocamera;
178
179                         if(sd->object != ~0)
180                                 data = transform_point(&tfm, sd->P + sd->dP.dy);
181                         else
182                                 data = transform_point(&tfm, sd->P + sd->dP.dy + svm_background_offset(kg));
183                         break;
184                 }
185                 case NODE_TEXCO_WINDOW: {
186                         data = svm_world_to_ndc(kg, sd, sd->P + sd->dP.dy);
187                         break;
188                 }
189                 case NODE_TEXCO_REFLECTION: {
190                         if(sd->object != ~0)
191                                 data = 2.0f*dot(sd->N, sd->I)*sd->N - sd->I;
192                         else
193                                 data = sd->I;
194                         break;
195                 }
196         }
197
198         stack_store_float3(stack, out_offset, data);
199 #else
200         svm_node_tex_coord(kg, sd, stack, type, out_offset);
201 #endif
202 }
203
204 CCL_NAMESPACE_END
205