a2fda12ec8506c500cbe35b811bab306bea9d299
[blender-staging.git] / intern / cycles / render / camera.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 "render/camera.h"
18 #include "render/mesh.h"
19 #include "render/object.h"
20 #include "render/scene.h"
21 #include "render/tables.h"
22
23 #include "device/device.h"
24
25 #include "util/util_foreach.h"
26 #include "util/util_function.h"
27 #include "util/util_math_cdf.h"
28 #include "util/util_vector.h"
29
30 /* needed for calculating differentials */
31 #include "kernel/kernel_compat_cpu.h"
32 #include "kernel/split/kernel_split_data.h"
33 #include "kernel/kernel_globals.h"
34 #include "kernel/kernel_projection.h"
35 #include "kernel/kernel_differential.h"
36 #include "kernel/kernel_montecarlo.h"
37 #include "kernel/kernel_camera.h"
38
39 CCL_NAMESPACE_BEGIN
40
41 static float shutter_curve_eval(float x,
42                                 array<float>& shutter_curve)
43 {
44         if(shutter_curve.size() == 0) {
45                 return 1.0f;
46         }
47
48         x *= shutter_curve.size();
49         int index = (int)x;
50         float frac = x - index;
51         if(index < shutter_curve.size() - 1) {
52                 return lerp(shutter_curve[index], shutter_curve[index + 1], frac);
53         }
54         else {
55                 return shutter_curve[shutter_curve.size() - 1];
56         }
57 }
58
59 NODE_DEFINE(Camera)
60 {
61         NodeType* type = NodeType::add("camera", create);
62
63         SOCKET_FLOAT(shuttertime, "Shutter Time", 1.0f);
64
65         static NodeEnum motion_position_enum;
66         motion_position_enum.insert("start", MOTION_POSITION_START);
67         motion_position_enum.insert("center", MOTION_POSITION_CENTER);
68         motion_position_enum.insert("end", MOTION_POSITION_END);
69         SOCKET_ENUM(motion_position, "Motion Position", motion_position_enum, MOTION_POSITION_CENTER);
70
71         static NodeEnum rolling_shutter_type_enum;
72         rolling_shutter_type_enum.insert("none", ROLLING_SHUTTER_NONE);
73         rolling_shutter_type_enum.insert("top", ROLLING_SHUTTER_TOP);
74         SOCKET_ENUM(rolling_shutter_type, "Rolling Shutter Type", rolling_shutter_type_enum,  ROLLING_SHUTTER_NONE);
75         SOCKET_FLOAT(rolling_shutter_duration, "Rolling Shutter Duration", 0.1f);
76
77         SOCKET_FLOAT_ARRAY(shutter_curve, "Shutter Curve", array<float>());
78
79         SOCKET_FLOAT(aperturesize, "Aperture Size", 0.0f);
80         SOCKET_FLOAT(focaldistance, "Focal Distance", 10.0f);
81         SOCKET_UINT(blades, "Blades", 0);
82         SOCKET_FLOAT(bladesrotation, "Blades Rotation", 0.0f);
83
84         SOCKET_TRANSFORM(matrix, "Matrix", transform_identity());
85
86         SOCKET_FLOAT(aperture_ratio, "Aperture Ratio", 1.0f);
87
88         static NodeEnum type_enum;
89         type_enum.insert("perspective", CAMERA_PERSPECTIVE);
90         type_enum.insert("orthograph", CAMERA_ORTHOGRAPHIC);
91         type_enum.insert("panorama", CAMERA_PANORAMA);
92         SOCKET_ENUM(type, "Type", type_enum, CAMERA_PERSPECTIVE);
93
94         static NodeEnum panorama_type_enum;
95         panorama_type_enum.insert("equirectangular", PANORAMA_EQUIRECTANGULAR);
96         panorama_type_enum.insert("mirrorball", PANORAMA_MIRRORBALL);
97         panorama_type_enum.insert("fisheye_equidistant", PANORAMA_FISHEYE_EQUIDISTANT);
98         panorama_type_enum.insert("fisheye_equisolid", PANORAMA_FISHEYE_EQUISOLID);
99         SOCKET_ENUM(panorama_type, "Panorama Type", panorama_type_enum, PANORAMA_EQUIRECTANGULAR);
100
101         SOCKET_FLOAT(fisheye_fov, "Fisheye FOV", M_PI_F);
102         SOCKET_FLOAT(fisheye_lens, "Fisheye Lens", 10.5f);
103         SOCKET_FLOAT(latitude_min, "Latitude Min", -M_PI_2_F);
104         SOCKET_FLOAT(latitude_max, "Latitude Max", M_PI_2_F);
105         SOCKET_FLOAT(longitude_min, "Longitude Min", -M_PI_F);
106         SOCKET_FLOAT(longitude_max, "Longitude Max", M_PI_F);
107         SOCKET_FLOAT(fov, "FOV", M_PI_4_F);
108         SOCKET_FLOAT(fov_pre, "FOV Pre", M_PI_4_F);
109         SOCKET_FLOAT(fov_post, "FOV Post", M_PI_4_F);
110
111         static NodeEnum stereo_eye_enum;
112         stereo_eye_enum.insert("none", STEREO_NONE);
113         stereo_eye_enum.insert("left", STEREO_LEFT);
114         stereo_eye_enum.insert("right", STEREO_RIGHT);
115         SOCKET_ENUM(stereo_eye, "Stereo Eye", stereo_eye_enum, STEREO_NONE);
116
117         SOCKET_FLOAT(interocular_distance, "Interocular Distance", 0.065f);
118         SOCKET_FLOAT(convergence_distance, "Convergence Distance", 30.0f * 0.065f);
119
120         SOCKET_BOOLEAN(use_pole_merge, "Use Pole Merge", false);
121         SOCKET_FLOAT(pole_merge_angle_from, "Pole Merge Angle From",  60.0f * M_PI_F / 180.0f);
122         SOCKET_FLOAT(pole_merge_angle_to, "Pole Merge Angle To", 75.0f * M_PI_F / 180.0f);
123
124         SOCKET_FLOAT(sensorwidth, "Sensor Width", 0.036f);
125         SOCKET_FLOAT(sensorheight, "Sensor Height", 0.024f);
126
127         SOCKET_FLOAT(nearclip, "Near Clip", 1e-5f);
128         SOCKET_FLOAT(farclip, "Far Clip", 1e5f);
129
130         SOCKET_FLOAT(viewplane.left, "Viewplane Left", 0);
131         SOCKET_FLOAT(viewplane.right, "Viewplane Right", 0);
132         SOCKET_FLOAT(viewplane.bottom, "Viewplane Bottom", 0);
133         SOCKET_FLOAT(viewplane.top, "Viewplane Top", 0);
134
135         SOCKET_FLOAT(border.left, "Border Left", 0);
136         SOCKET_FLOAT(border.right, "Border Right", 0);
137         SOCKET_FLOAT(border.bottom, "Border Bottom", 0);
138         SOCKET_FLOAT(border.top, "Border Top", 0);
139
140         SOCKET_FLOAT(offscreen_dicing_scale, "Offscreen Dicing Scale", 1.0f);
141
142         return type;
143 }
144
145 Camera::Camera()
146 : Node(node_type)
147 {
148         shutter_table_offset = TABLE_OFFSET_INVALID;
149
150         width = 1024;
151         height = 512;
152         resolution = 1;
153
154         motion.pre = transform_identity();
155         motion.post = transform_identity();
156         use_motion = false;
157         use_perspective_motion = false;
158
159         shutter_curve.resize(RAMP_TABLE_SIZE);
160         for(int i = 0; i < shutter_curve.size(); ++i) {
161                 shutter_curve[i] = 1.0f;
162         }
163
164         compute_auto_viewplane();
165
166         screentoworld = transform_identity();
167         rastertoworld = transform_identity();
168         ndctoworld = transform_identity();
169         rastertocamera = transform_identity();
170         cameratoworld = transform_identity();
171         worldtoraster = transform_identity();
172
173         dx = make_float3(0.0f, 0.0f, 0.0f);
174         dy = make_float3(0.0f, 0.0f, 0.0f);
175
176         need_update = true;
177         need_device_update = true;
178         need_flags_update = true;
179         previous_need_motion = -1;
180
181         memset(&kernel_camera, 0, sizeof(kernel_camera));
182 }
183
184 Camera::~Camera()
185 {
186 }
187
188 void Camera::compute_auto_viewplane()
189 {
190         if(type == CAMERA_PANORAMA) {
191                 viewplane.left = 0.0f;
192                 viewplane.right = 1.0f;
193                 viewplane.bottom = 0.0f;
194                 viewplane.top = 1.0f;
195         }
196         else {
197                 float aspect = (float)width/(float)height;
198                 if(width >= height) {
199                         viewplane.left = -aspect;
200                         viewplane.right = aspect;
201                         viewplane.bottom = -1.0f;
202                         viewplane.top = 1.0f;
203                 }
204                 else {
205                         viewplane.left = -1.0f;
206                         viewplane.right = 1.0f;
207                         viewplane.bottom = -1.0f/aspect;
208                         viewplane.top = 1.0f/aspect;
209                 }
210         }
211 }
212
213 void Camera::update(Scene *scene)
214 {
215         Scene::MotionType need_motion = scene->need_motion();
216
217         if(previous_need_motion != need_motion) {
218                 /* scene's motion model could have been changed since previous device
219                  * camera update this could happen for example in case when one render
220                  * layer has got motion pass and another not */
221                 need_device_update = true;
222         }
223
224         if(!need_update)
225                 return;
226
227         /* Full viewport to camera border in the viewport. */
228         Transform fulltoborder = transform_from_viewplane(viewport_camera_border);
229         Transform bordertofull = transform_inverse(fulltoborder);
230
231         /* ndc to raster */
232         Transform ndctoraster = transform_scale(width, height, 1.0f) * bordertofull;
233         Transform full_ndctoraster = transform_scale(full_width, full_height, 1.0f) * bordertofull;
234
235         /* raster to screen */
236         Transform screentondc = fulltoborder * transform_from_viewplane(viewplane);
237
238         Transform screentoraster = ndctoraster * screentondc;
239         Transform rastertoscreen = transform_inverse(screentoraster);
240         Transform full_screentoraster = full_ndctoraster * screentondc;
241         Transform full_rastertoscreen = transform_inverse(full_screentoraster);
242
243         /* screen to camera */
244         Transform cameratoscreen;
245         if(type == CAMERA_PERSPECTIVE)
246                 cameratoscreen = transform_perspective(fov, nearclip, farclip);
247         else if(type == CAMERA_ORTHOGRAPHIC)
248                 cameratoscreen = transform_orthographic(nearclip, farclip);
249         else
250                 cameratoscreen = transform_identity();
251         
252         Transform screentocamera = transform_inverse(cameratoscreen);
253
254         rastertocamera = screentocamera * rastertoscreen;
255         Transform full_rastertocamera = screentocamera * full_rastertoscreen;
256         cameratoraster = screentoraster * cameratoscreen;
257
258         cameratoworld = matrix;
259         screentoworld = cameratoworld * screentocamera;
260         rastertoworld = cameratoworld * rastertocamera;
261         ndctoworld = rastertoworld * ndctoraster;
262
263         /* note we recompose matrices instead of taking inverses of the above, this
264          * is needed to avoid inverting near degenerate matrices that happen due to
265          * precision issues with large scenes */
266         worldtocamera = transform_inverse(matrix);
267         worldtoscreen = cameratoscreen * worldtocamera;
268         worldtondc = screentondc * worldtoscreen;
269         worldtoraster = ndctoraster * worldtondc;
270
271         /* differentials */
272         if(type == CAMERA_ORTHOGRAPHIC) {
273                 dx = transform_direction(&rastertocamera, make_float3(1, 0, 0));
274                 dy = transform_direction(&rastertocamera, make_float3(0, 1, 0));
275                 full_dx = transform_direction(&full_rastertocamera, make_float3(1, 0, 0));
276                 full_dy = transform_direction(&full_rastertocamera, make_float3(0, 1, 0));
277         }
278         else if(type == CAMERA_PERSPECTIVE) {
279                 dx = transform_perspective(&rastertocamera, make_float3(1, 0, 0)) -
280                      transform_perspective(&rastertocamera, make_float3(0, 0, 0));
281                 dy = transform_perspective(&rastertocamera, make_float3(0, 1, 0)) -
282                      transform_perspective(&rastertocamera, make_float3(0, 0, 0));
283                 full_dx = transform_perspective(&full_rastertocamera, make_float3(1, 0, 0)) -
284                      transform_perspective(&full_rastertocamera, make_float3(0, 0, 0));
285                 full_dy = transform_perspective(&full_rastertocamera, make_float3(0, 1, 0)) -
286                      transform_perspective(&full_rastertocamera, make_float3(0, 0, 0));
287         }
288         else {
289                 dx = make_float3(0.0f, 0.0f, 0.0f);
290                 dy = make_float3(0.0f, 0.0f, 0.0f);
291         }
292
293         dx = transform_direction(&cameratoworld, dx);
294         dy = transform_direction(&cameratoworld, dy);
295         full_dx = transform_direction(&cameratoworld, full_dx);
296         full_dy = transform_direction(&cameratoworld, full_dy);
297
298         if(type == CAMERA_PERSPECTIVE) {
299                 float3 v = transform_perspective(&full_rastertocamera, make_float3(full_width, full_height, 1.0f));
300
301                 frustum_right_normal = normalize(make_float3(v.z, 0.0f, -v.x));
302                 frustum_top_normal = normalize(make_float3(0.0f, v.z, -v.y));
303         }
304
305         /* TODO(sergey): Support other types of camera. */
306         if(type == CAMERA_PERSPECTIVE) {
307                 /* TODO(sergey): Move to an utility function and de-duplicate with
308                  * calculation above.
309                  */
310                 Transform screentocamera_pre =
311                         transform_inverse(transform_perspective(fov_pre,
312                                                                 nearclip,
313                                                                 farclip));
314                 Transform screentocamera_post =
315                         transform_inverse(transform_perspective(fov_post,
316                                                                 nearclip,
317                                                                 farclip));
318                 perspective_motion.pre = screentocamera_pre * rastertoscreen;
319                 perspective_motion.post = screentocamera_post * rastertoscreen;
320         }
321
322         /* Compute kernel camera data. */
323         KernelCamera *kcam = &kernel_camera;
324
325         /* store matrices */
326         kcam->screentoworld = screentoworld;
327         kcam->rastertoworld = rastertoworld;
328         kcam->rastertocamera = rastertocamera;
329         kcam->cameratoworld = cameratoworld;
330         kcam->worldtocamera = worldtocamera;
331         kcam->worldtoscreen = worldtoscreen;
332         kcam->worldtoraster = worldtoraster;
333         kcam->worldtondc = worldtondc;
334
335         /* camera motion */
336         kcam->have_motion = 0;
337         kcam->have_perspective_motion = 0;
338
339         if(need_motion == Scene::MOTION_PASS) {
340                 /* TODO(sergey): Support perspective (zoom, fov) motion. */
341                 if(type == CAMERA_PANORAMA) {
342                         if(use_motion) {
343                                 kcam->motion.pre = transform_inverse(motion.pre);
344                                 kcam->motion.post = transform_inverse(motion.post);
345                         }
346                         else {
347                                 kcam->motion.pre = kcam->worldtocamera;
348                                 kcam->motion.post = kcam->worldtocamera;
349                         }
350                 }
351                 else {
352                         if(use_motion) {
353                                 kcam->motion.pre = cameratoraster * transform_inverse(motion.pre);
354                                 kcam->motion.post = cameratoraster * transform_inverse(motion.post);
355                         }
356                         else {
357                                 kcam->motion.pre = worldtoraster;
358                                 kcam->motion.post = worldtoraster;
359                         }
360                 }
361         }
362         else if(need_motion == Scene::MOTION_BLUR) {
363                 if(use_motion) {
364                         transform_motion_decompose(&kcam->motion, &motion, &matrix);
365                         kcam->have_motion = 1;
366                 }
367                 if(use_perspective_motion) {
368                         kcam->perspective_motion = perspective_motion;
369                         kcam->have_perspective_motion = 1;
370                 }
371         }
372
373         /* depth of field */
374         kcam->aperturesize = aperturesize;
375         kcam->focaldistance = focaldistance;
376         kcam->blades = (blades < 3)? 0.0f: blades;
377         kcam->bladesrotation = bladesrotation;
378
379         /* motion blur */
380         kcam->shuttertime = (need_motion == Scene::MOTION_BLUR) ? shuttertime: -1.0f;
381
382         /* type */
383         kcam->type = type;
384
385         /* anamorphic lens bokeh */
386         kcam->inv_aperture_ratio = 1.0f / aperture_ratio;
387
388         /* panorama */
389         kcam->panorama_type = panorama_type;
390         kcam->fisheye_fov = fisheye_fov;
391         kcam->fisheye_lens = fisheye_lens;
392         kcam->equirectangular_range = make_float4(longitude_min - longitude_max, -longitude_min,
393                                                   latitude_min -  latitude_max, -latitude_min + M_PI_2_F);
394
395         switch(stereo_eye) {
396                 case STEREO_LEFT:
397                         kcam->interocular_offset = -interocular_distance * 0.5f;
398                         break;
399                 case STEREO_RIGHT:
400                         kcam->interocular_offset = interocular_distance * 0.5f;
401                         break;
402                 case STEREO_NONE:
403                 default:
404                         kcam->interocular_offset = 0.0f;
405                         break;
406         }
407
408         kcam->convergence_distance = convergence_distance;
409         if(use_pole_merge) {
410                 kcam->pole_merge_angle_from = pole_merge_angle_from;
411                 kcam->pole_merge_angle_to = pole_merge_angle_to;
412         }
413         else {
414                 kcam->pole_merge_angle_from = -1.0f;
415                 kcam->pole_merge_angle_to = -1.0f;
416         }
417
418         /* sensor size */
419         kcam->sensorwidth = sensorwidth;
420         kcam->sensorheight = sensorheight;
421
422         /* render size */
423         kcam->width = width;
424         kcam->height = height;
425         kcam->resolution = resolution;
426
427         /* store differentials */
428         kcam->dx = float3_to_float4(dx);
429         kcam->dy = float3_to_float4(dy);
430
431         /* clipping */
432         kcam->nearclip = nearclip;
433         kcam->cliplength = (farclip == FLT_MAX)? FLT_MAX: farclip - nearclip;
434
435         /* Camera in volume. */
436         kcam->is_inside_volume = 0;
437
438         /* Rolling shutter effect */
439         kcam->rolling_shutter_type = rolling_shutter_type;
440         kcam->rolling_shutter_duration = rolling_shutter_duration;
441
442         /* Set further update flags */
443         need_update = false;
444         need_device_update = true;
445         need_flags_update = true;
446         previous_need_motion = need_motion;
447 }
448
449 void Camera::device_update(Device * /* device */,
450                            DeviceScene *dscene,
451                            Scene *scene)
452 {
453         update(scene);
454
455         if(!need_device_update)
456                 return;
457
458         scene->lookup_tables->remove_table(&shutter_table_offset);
459         if(kernel_camera.shuttertime != -1.0f) {
460                 vector<float> shutter_table;
461                 util_cdf_inverted(SHUTTER_TABLE_SIZE,
462                                   0.0f,
463                                   1.0f,
464                                   function_bind(shutter_curve_eval, _1, shutter_curve),
465                                   false,
466                                   shutter_table);
467                 shutter_table_offset = scene->lookup_tables->add_table(dscene,
468                                                                        shutter_table);
469                 kernel_camera.shutter_table_offset = (int)shutter_table_offset;
470         }
471
472         dscene->data.cam = kernel_camera;
473 }
474
475 void Camera::device_update_volume(Device * /*device*/,
476                                   DeviceScene *dscene,
477                                   Scene *scene)
478 {
479         if(!need_device_update && !need_flags_update) {
480                 return;
481         }
482         KernelCamera *kcam = &dscene->data.cam;
483         BoundBox viewplane_boundbox = viewplane_bounds_get();
484         for(size_t i = 0; i < scene->objects.size(); ++i) {
485                 Object *object = scene->objects[i];
486                 if(object->mesh->has_volume &&
487                    viewplane_boundbox.intersects(object->bounds))
488                 {
489                         /* TODO(sergey): Consider adding more grained check. */
490                         kcam->is_inside_volume = 1;
491                         break;
492                 }
493         }
494         need_device_update = false;
495         need_flags_update = false;
496 }
497
498 void Camera::device_free(Device * /*device*/,
499                          DeviceScene * /*dscene*/,
500                          Scene *scene)
501 {
502         scene->lookup_tables->remove_table(&shutter_table_offset);
503 }
504
505 bool Camera::modified(const Camera& cam)
506 {
507         return !Node::equals(cam);
508 }
509
510 bool Camera::motion_modified(const Camera& cam)
511 {
512         return !((motion == cam.motion) &&
513                  (use_motion == cam.use_motion) &&
514                  (use_perspective_motion == cam.use_perspective_motion));
515 }
516
517 void Camera::tag_update()
518 {
519         need_update = true;
520 }
521
522 float3 Camera::transform_raster_to_world(float raster_x, float raster_y)
523 {
524         float3 D, P;
525         if(type == CAMERA_PERSPECTIVE) {
526                 D = transform_perspective(&rastertocamera,
527                                           make_float3(raster_x, raster_y, 0.0f));
528                 float3 Pclip = normalize(D);
529                 P = make_float3(0.0f, 0.0f, 0.0f);
530                 /* TODO(sergey): Aperture support? */
531                 P = transform_point(&cameratoworld, P);
532                 D = normalize(transform_direction(&cameratoworld, D));
533                 /* TODO(sergey): Clipping is conditional in kernel, and hence it could
534                  * be mistakes in here, currently leading to wrong camera-in-volume
535                  * detection.
536                  */
537                 P += nearclip * D / Pclip.z;
538         }
539         else if(type == CAMERA_ORTHOGRAPHIC) {
540                 D = make_float3(0.0f, 0.0f, 1.0f);
541                 /* TODO(sergey): Aperture support? */
542                 P = transform_perspective(&rastertocamera,
543                                           make_float3(raster_x, raster_y, 0.0f));
544                 P = transform_point(&cameratoworld, P);
545                 D = normalize(transform_direction(&cameratoworld, D));
546         }
547         else {
548                 assert(!"unsupported camera type");
549         }
550         return P;
551 }
552
553 BoundBox Camera::viewplane_bounds_get()
554 {
555         /* TODO(sergey): This is all rather stupid, but is there a way to perform
556          * checks we need in a more clear and smart fasion?
557          */
558         BoundBox bounds = BoundBox::empty;
559
560         if(type == CAMERA_PANORAMA) {
561                 if(use_spherical_stereo == false) {
562                         bounds.grow(make_float3(cameratoworld.x.w,
563                                                 cameratoworld.y.w,
564                                                 cameratoworld.z.w));
565                 }
566                 else {
567                         float half_eye_distance = interocular_distance * 0.5f;
568
569                         bounds.grow(make_float3(cameratoworld.x.w + half_eye_distance,
570                                                 cameratoworld.y.w,
571                                                 cameratoworld.z.w));
572
573                         bounds.grow(make_float3(cameratoworld.z.w,
574                                                 cameratoworld.y.w + half_eye_distance,
575                                                 cameratoworld.z.w));
576
577                         bounds.grow(make_float3(cameratoworld.x.w - half_eye_distance,
578                                                 cameratoworld.y.w,
579                                                 cameratoworld.z.w));
580
581                         bounds.grow(make_float3(cameratoworld.x.w,
582                                                 cameratoworld.y.w - half_eye_distance,
583                                                 cameratoworld.z.w));
584                 }
585         }
586         else {
587                 bounds.grow(transform_raster_to_world(0.0f, 0.0f));
588                 bounds.grow(transform_raster_to_world(0.0f, (float)height));
589                 bounds.grow(transform_raster_to_world((float)width, (float)height));
590                 bounds.grow(transform_raster_to_world((float)width, 0.0f));
591                 if(type == CAMERA_PERSPECTIVE) {
592                         /* Center point has the most distance in local Z axis,
593                          * use it to construct bounding box/
594                          */
595                         bounds.grow(transform_raster_to_world(0.5f*width, 0.5f*height));
596                 }
597         }
598         return bounds;
599 }
600
601 float Camera::world_to_raster_size(float3 P)
602 {
603         float res = 1.0f;
604
605         if(type == CAMERA_ORTHOGRAPHIC) {
606                 res = min(len(full_dx), len(full_dy));
607
608                 if(offscreen_dicing_scale > 1.0f) {
609                         float3 p = transform_perspective(&worldtocamera, P);
610                         float3 v = transform_perspective(&rastertocamera, make_float3(width, height, 0.0f));
611
612                         /* Create point clamped to frustum */
613                         float3 c;
614                         c.x = max(-v.x, min(v.x, p.x));
615                         c.y = max(-v.y, min(v.y, p.y));
616                         c.z = max(0.0f, p.z);
617
618                         float f_dist = len(p - c) / sqrtf((v.x*v.x+v.y*v.y)*0.5f);
619
620                         if(f_dist > 0.0f) {
621                                 res += res * f_dist * (offscreen_dicing_scale - 1.0f);
622                         }
623                 }
624         }
625         else if(type == CAMERA_PERSPECTIVE) {
626                 /* Calculate as if point is directly ahead of the camera. */
627                 float3 raster = make_float3(0.5f*width, 0.5f*height, 0.0f);
628                 float3 Pcamera = transform_perspective(&rastertocamera, raster);
629
630                 /* dDdx */
631                 float3 Ddiff = transform_direction(&cameratoworld, Pcamera);
632                 float3 dx = len_squared(full_dx) < len_squared(full_dy) ? full_dx : full_dy;
633                 float3 dDdx = normalize(Ddiff + dx) - normalize(Ddiff);
634
635                 /* dPdx */
636                 float dist = len(transform_point(&worldtocamera, P));
637                 float3 D = normalize(Ddiff);
638                 res = len(dist*dDdx - dot(dist*dDdx, D)*D);
639
640                 /* Decent approx distance to frustum (doesn't handle corners correctly, but not that big of a deal) */
641                 float f_dist = 0.0f;
642
643                 if(offscreen_dicing_scale > 1.0f) {
644                         float3 p = transform_point(&worldtocamera, P);
645
646                         /* Distance from the four planes */
647                         float r = dot(p, frustum_right_normal);
648                         float t = dot(p, frustum_top_normal);
649                         p = make_float3(-p.x, -p.y, p.z);
650                         float l = dot(p, frustum_right_normal);
651                         float b = dot(p, frustum_top_normal);
652                         p = make_float3(-p.x, -p.y, p.z);
653
654                         if(r <= 0.0f && l <= 0.0f && t <= 0.0f && b <= 0.0f) {
655                                 /* Point is inside frustum */
656                                 f_dist = 0.0f;
657                         }
658                         else if(r > 0.0f && l > 0.0f && t > 0.0f && b > 0.0f) {
659                                 /* Point is behind frustum */
660                                 f_dist = len(p);
661                         }
662                         else {
663                                 /* Point may be behind or off to the side, need to check */
664                                 float3 along_right = make_float3(-frustum_right_normal.z, 0.0f, frustum_right_normal.x);
665                                 float3 along_left = make_float3(frustum_right_normal.z, 0.0f, frustum_right_normal.x);
666                                 float3 along_top = make_float3(0.0f, -frustum_top_normal.z, frustum_top_normal.y);
667                                 float3 along_bottom = make_float3(0.0f, frustum_top_normal.z, frustum_top_normal.y);
668
669                                 float dist[] = {r, l, t, b};
670                                 float3 along[] = {along_right, along_left, along_top, along_bottom};
671
672                                 bool test_o = false;
673
674                                 float *d = dist;
675                                 float3 *a = along;
676                                 for(int i = 0; i < 4; i++, d++, a++) {
677                                         /* Test if we should check this side at all */
678                                         if(*d > 0.0f) {
679                                                 if(dot(p, *a) >= 0.0f) {
680                                                         /* We are in front of the back edge of this side of the frustum */
681                                                         f_dist = max(f_dist, *d);
682                                                 }
683                                                 else {
684                                                         /* Possibly far enough behind the frustum to use distance to origin instead of edge */
685                                                         test_o = true;
686                                                 }
687                                         }
688                                 }
689
690                                 if(test_o) {
691                                         f_dist = (f_dist > 0) ? min(f_dist, len(p)) : len(p);
692                                 }
693                         }
694
695                         if(f_dist > 0.0f) {
696                                 res += len(dDdx - dot(dDdx, D)*D) * f_dist * (offscreen_dicing_scale - 1.0f);
697                         }
698                 }
699         }
700         else if(type == CAMERA_PANORAMA) {
701                 float3 D = transform_point(&worldtocamera, P);
702                 float dist = len(D);
703
704                 Ray ray;
705
706                 /* Distortion can become so great that the results become meaningless, there
707                  * may be a better way to do this, but calculating differentials from the
708                  * point directly ahead seems to produce good enough results. */
709 #if 0
710                 float2 dir = direction_to_panorama(&kernel_camera, normalize(D));
711                 float3 raster = transform_perspective(&cameratoraster, make_float3(dir.x, dir.y, 0.0f));
712
713                 ray.t = 1.0f;
714                 camera_sample_panorama(&kernel_camera, raster.x, raster.y, 0.0f, 0.0f, &ray);
715                 if(ray.t == 0.0f) {
716                         /* No differentials, just use from directly ahead. */
717                         camera_sample_panorama(&kernel_camera, 0.5f*width, 0.5f*height, 0.0f, 0.0f, &ray);
718                 }
719 #else
720                 camera_sample_panorama(&kernel_camera, 0.5f*width, 0.5f*height, 0.0f, 0.0f, &ray);
721 #endif
722
723                 differential_transfer(&ray.dP, ray.dP, ray.D, ray.dD, ray.D, dist);
724
725                 return max(len(ray.dP.dx) * (float(width)/float(full_width)),
726                            len(ray.dP.dy) * (float(height)/float(full_height)));
727         }
728
729         return res;
730 }
731
732 CCL_NAMESPACE_END