Attempt to fix #34871: random CUDA 3.5 compile failure due to recent change.
[blender.git] / intern / cycles / kernel / kernel_camera.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 /* Perspective Camera */
22
23 __device float2 camera_sample_aperture(KernelGlobals *kg, float u, float v)
24 {
25         float blades = kernel_data.cam.blades;
26
27         if(blades == 0.0f) {
28                 /* sample disk */
29                 return concentric_sample_disk(u, v);
30         }
31         else {
32                 /* sample polygon */
33                 float rotation = kernel_data.cam.bladesrotation;
34                 return regular_polygon_sample(blades, rotation, u, v);
35         }
36 }
37
38 __device void camera_sample_perspective(KernelGlobals *kg, float raster_x, float raster_y, float lens_u, float lens_v, Ray *ray)
39 {
40         /* create ray form raster position */
41         Transform rastertocamera = kernel_data.cam.rastertocamera;
42         float3 Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
43
44         ray->P = make_float3(0.0f, 0.0f, 0.0f);
45         ray->D = Pcamera;
46
47         /* modify ray for depth of field */
48         float aperturesize = kernel_data.cam.aperturesize;
49
50         if(aperturesize > 0.0f) {
51                 /* sample point on aperture */
52                 float2 lensuv = camera_sample_aperture(kg, lens_u, lens_v)*aperturesize;
53
54                 /* compute point on plane of focus */
55                 float ft = kernel_data.cam.focaldistance/ray->D.z;
56                 float3 Pfocus = ray->D*ft;
57
58                 /* update ray for effect of lens */
59                 ray->P = make_float3(lensuv.x, lensuv.y, 0.0f);
60                 ray->D = normalize(Pfocus - ray->P);
61         }
62
63         /* transform ray from camera to world */
64         Transform cameratoworld = kernel_data.cam.cameratoworld;
65
66 #ifdef __CAMERA_MOTION__
67         if(kernel_data.cam.have_motion)
68                 transform_motion_interpolate(&cameratoworld, (const DecompMotionTransform*)&kernel_data.cam.motion, ray->time);
69 #endif
70
71         ray->P = transform_point(&cameratoworld, ray->P);
72         ray->D = transform_direction(&cameratoworld, ray->D);
73         ray->D = normalize(ray->D);
74
75 #ifdef __RAY_DIFFERENTIALS__
76         /* ray differential */
77         float3 Ddiff = transform_direction(&cameratoworld, Pcamera);
78
79         ray->dP.dx = make_float3(0.0f, 0.0f, 0.0f);
80         ray->dP.dy = make_float3(0.0f, 0.0f, 0.0f);
81
82         ray->dD.dx = normalize(Ddiff + float4_to_float3(kernel_data.cam.dx)) - normalize(Ddiff);
83         ray->dD.dy = normalize(Ddiff + float4_to_float3(kernel_data.cam.dy)) - normalize(Ddiff);
84 #endif
85
86 #ifdef __CAMERA_CLIPPING__
87         /* clipping */
88         ray->P += kernel_data.cam.nearclip*ray->D;
89         ray->t = kernel_data.cam.cliplength;
90 #else
91         ray->t = FLT_MAX;
92 #endif
93 }
94
95 /* Orthographic Camera */
96
97 __device void camera_sample_orthographic(KernelGlobals *kg, float raster_x, float raster_y, float lens_u, float lens_v, Ray *ray)
98 {
99         /* create ray form raster position */
100         Transform rastertocamera = kernel_data.cam.rastertocamera;
101         float3 Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
102
103         ray->P = Pcamera;
104         ray->D = make_float3(0.0f, 0.0f, 1.0f);
105
106         /* modify ray for depth of field */
107         float aperturesize = kernel_data.cam.aperturesize;
108
109         if(aperturesize > 0.0f) {
110                 /* sample point on aperture */
111                 float2 lensuv = camera_sample_aperture(kg, lens_u, lens_v)*aperturesize;
112
113                 /* compute point on plane of focus */
114                 float ft = kernel_data.cam.focaldistance/ray->D.z;
115                 float3 Pfocus = ray->D*ft;
116
117                 /* update ray for effect of lens */
118                 float3 lensuvw = make_float3(lensuv.x, lensuv.y, 0.0f);
119
120                 ray->P += lensuvw;
121                 ray->D = normalize(Pfocus - lensuvw);
122         }
123
124         /* transform ray from camera to world */
125         Transform cameratoworld = kernel_data.cam.cameratoworld;
126
127 #ifdef __CAMERA_MOTION__
128         if(kernel_data.cam.have_motion)
129                 transform_motion_interpolate(&cameratoworld, (const DecompMotionTransform*)&kernel_data.cam.motion, ray->time);
130 #endif
131
132         ray->P = transform_point(&cameratoworld, ray->P);
133         ray->D = transform_direction(&cameratoworld, ray->D);
134         ray->D = normalize(ray->D);
135
136 #ifdef __RAY_DIFFERENTIALS__
137         /* ray differential */
138         ray->dP.dx = float4_to_float3(kernel_data.cam.dx);
139         ray->dP.dy = float4_to_float3(kernel_data.cam.dy);
140
141         ray->dD.dx = make_float3(0.0f, 0.0f, 0.0f);
142         ray->dD.dy = make_float3(0.0f, 0.0f, 0.0f);
143 #endif
144
145 #ifdef __CAMERA_CLIPPING__
146         /* clipping */
147         ray->t = kernel_data.cam.cliplength;
148 #else
149         ray->t = FLT_MAX;
150 #endif
151 }
152
153 /* Panorama Camera */
154
155 __device void camera_sample_panorama(KernelGlobals *kg, float raster_x, float raster_y, float lens_u, float lens_v, Ray *ray)
156 {
157         Transform rastertocamera = kernel_data.cam.rastertocamera;
158         float3 Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
159
160         /* create ray form raster position */
161         ray->P = make_float3(0.0f, 0.0f, 0.0f);
162
163 #ifdef __CAMERA_CLIPPING__
164         /* clipping */
165         ray->t = kernel_data.cam.cliplength;
166 #else
167         ray->t = FLT_MAX;
168 #endif
169
170         ray->D = panorama_to_direction(kg, Pcamera.x, Pcamera.y);
171
172         /* modify ray for depth of field */
173         float aperturesize = kernel_data.cam.aperturesize;
174
175         if(aperturesize > 0.0f) {
176                 /* sample point on aperture */
177                 float2 lensuv = camera_sample_aperture(kg, lens_u, lens_v)*aperturesize;
178
179                 /* compute point on plane of focus */
180                 float3 D = normalize(ray->D);
181                 float3 Pfocus = D * kernel_data.cam.focaldistance;
182
183                 /* calculate orthonormal coordinates perpendicular to D */
184                 float3 U, V;
185                 make_orthonormals(D, &U, &V);
186
187                 /* update ray for effect of lens */
188                 ray->P = U * lensuv.x + V * lensuv.y;
189                 ray->D = normalize(Pfocus - ray->P);
190         }
191
192         /* indicates ray should not receive any light, outside of the lens */
193         if(len_squared(ray->D) == 0.0f) {
194                 ray->t = 0.0f;
195                 return;
196         }
197
198         /* transform ray from camera to world */
199         Transform cameratoworld = kernel_data.cam.cameratoworld;
200
201 #ifdef __CAMERA_MOTION__
202         if(kernel_data.cam.have_motion)
203                 transform_motion_interpolate(&cameratoworld, (const DecompMotionTransform*)&kernel_data.cam.motion, ray->time);
204 #endif
205
206         ray->P = transform_point(&cameratoworld, ray->P);
207         ray->D = transform_direction(&cameratoworld, ray->D);
208         ray->D = normalize(ray->D);
209
210 #ifdef __RAY_DIFFERENTIALS__
211         /* ray differential */
212         ray->dP.dx = make_float3(0.0f, 0.0f, 0.0f);
213         ray->dP.dy = make_float3(0.0f, 0.0f, 0.0f);
214
215         Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x + 1.0f, raster_y, 0.0f));
216         ray->dD.dx = normalize(transform_direction(&cameratoworld, panorama_to_direction(kg, Pcamera.x, Pcamera.y))) - ray->D;
217
218         Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y + 1.0f, 0.0f));
219         ray->dD.dy = normalize(transform_direction(&cameratoworld, panorama_to_direction(kg, Pcamera.x, Pcamera.y))) - ray->D;
220 #endif
221 }
222
223 /* Common */
224
225 __device void camera_sample(KernelGlobals *kg, int x, int y, float filter_u, float filter_v,
226         float lens_u, float lens_v, float time, Ray *ray)
227 {
228         /* pixel filter */
229         int filter_table_offset = kernel_data.film.filter_table_offset;
230         float raster_x = x + lookup_table_read(kg, filter_u, filter_table_offset, FILTER_TABLE_SIZE);
231         float raster_y = y + lookup_table_read(kg, filter_v, filter_table_offset, FILTER_TABLE_SIZE);
232
233 #ifdef __CAMERA_MOTION__
234         /* motion blur */
235         if(kernel_data.cam.shuttertime == -1.0f)
236                 ray->time = TIME_INVALID;
237         else
238                 ray->time = 0.5f + 0.5f*(time - 0.5f)*kernel_data.cam.shuttertime;
239 #endif
240
241         /* sample */
242         if(kernel_data.cam.type == CAMERA_PERSPECTIVE)
243                 camera_sample_perspective(kg, raster_x, raster_y, lens_u, lens_v, ray);
244         else if(kernel_data.cam.type == CAMERA_ORTHOGRAPHIC)
245                 camera_sample_orthographic(kg, raster_x, raster_y, lens_u, lens_v, ray);
246         else
247                 camera_sample_panorama(kg, raster_x, raster_y, lens_u, lens_v, ray);
248 }
249
250 /* Utilities */
251
252 __device_inline float camera_distance(KernelGlobals *kg, float3 P)
253 {
254         Transform cameratoworld = kernel_data.cam.cameratoworld;
255         float3 camP = make_float3(cameratoworld.x.w, cameratoworld.y.w, cameratoworld.z.w);
256
257         if(kernel_data.cam.type == CAMERA_ORTHOGRAPHIC) {
258                 float3 camD = make_float3(cameratoworld.x.z, cameratoworld.y.z, cameratoworld.z.z);
259                 return fabsf(dot((P - camP), camD));
260         }
261         else
262                 return len(P - camP);
263 }
264
265 CCL_NAMESPACE_END
266