Cycles: merge of changes from tomato branch.
[blender-staging.git] / source / blender / render / intern / source / initrender.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: 2004/2005/2006 Blender Foundation, full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/render/intern/source/initrender.c
27  *  \ingroup render
28  */
29
30 /* Global includes */
31
32 #include <math.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "PIL_time.h"
40
41 #include "BLI_math.h"
42 #include "BLI_blenlib.h"
43 #include "BLI_jitter.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_camera_types.h"
47 #include "DNA_group_types.h"
48 #include "DNA_image_types.h"
49 #include "DNA_lamp_types.h"
50 #include "DNA_object_types.h"
51 #include "DNA_scene_types.h"
52
53
54 #include "BKE_camera.h"
55 #include "BKE_global.h"
56 #include "BKE_material.h"
57 #include "BKE_object.h"
58 #include "BKE_image.h"
59 #include "BKE_ipo.h"
60 #include "BKE_key.h"
61 #include "BKE_action.h"
62 #include "BKE_writeavi.h"
63 #include "BKE_scene.h"
64
65 #include "IMB_imbuf_types.h"
66 #include "IMB_imbuf.h"
67
68 #ifdef WITH_QUICKTIME
69 #include "quicktime_export.h"
70 #endif
71
72 /* this module */
73 #include "renderpipeline.h"
74 #include "render_types.h"
75
76 #include "rendercore.h"
77 #include "pixelshading.h"
78 #include "zbuf.h"
79
80 /* Own includes */
81 #include "initrender.h"
82
83
84 /* ********************** */
85
86 static void init_render_jit(Render *re)
87 {
88         static float jit[32][2];        /* simple caching */
89         static float mblur_jit[32][2];  /* simple caching */
90         static int lastjit = 0;
91         static int last_mblur_jit = 0;
92         
93         if (lastjit != re->r.osa || last_mblur_jit != re->r.mblur_samples) {
94                 memset(jit, 0, sizeof(jit));
95                 BLI_jitter_init(jit[0], re->r.osa);
96                 
97                 memset(mblur_jit, 0, sizeof(mblur_jit));
98                 BLI_jitter_init(mblur_jit[0], re->r.mblur_samples);
99         }
100         
101         lastjit = re->r.osa;
102         memcpy(re->jit, jit, sizeof(jit));
103         
104         last_mblur_jit = re->r.mblur_samples;
105         memcpy(re->mblur_jit, mblur_jit, sizeof(mblur_jit));
106 }
107
108
109 /* ****************** MASKS and LUTS **************** */
110
111 static float filt_quadratic(float x)
112 {
113         if (x <  0.0f) x = -x;
114         if (x < 0.5f) return 0.75f - (x * x);
115         if (x < 1.5f) return 0.50f * (x - 1.5f) * (x - 1.5f);
116         return 0.0f;
117 }
118
119
120 static float filt_cubic(float x)
121 {
122         float x2 = x * x;
123         
124         if (x <  0.0f) x = -x;
125         
126         if (x < 1.0f) return 0.5f * x * x2 - x2 + 2.0f / 3.0f;
127         if (x < 2.0f) return (2.0f - x) * (2.0f - x) * (2.0f - x) / 6.0f;
128         return 0.0f;
129 }
130
131
132 static float filt_catrom(float x)
133 {
134         float x2 = x * x;
135         
136         if (x <  0.0f) x = -x;
137         if (x < 1.0f) return  1.5f * x2 * x - 2.5f * x2 + 1.0f;
138         if (x < 2.0f) return -0.5f * x2 * x + 2.5f * x2 - 4.0f * x + 2.0f;
139         return 0.0f;
140 }
141
142 static float filt_mitchell(float x) /* Mitchell & Netravali's two-param cubic */
143 {
144         float b = 1.0f / 3.0f, c = 1.0f / 3.0f;
145         float p0 = (  6.0f -  2.0f * b)             / 6.0f;
146         float p2 = (-18.0f + 12.0f * b +  6.0f * c) / 6.0f;
147         float p3 = ( 12.0f -  9.0f * b -  6.0f * c) / 6.0f;
148         float q0 = (          8.0f * b + 24.0f * c) / 6.0f;
149         float q1 = (        -12.0f * b - 48.0f * c) / 6.0f;
150         float q2 = (          6.0f * b + 30.0f * c) / 6.0f;
151         float q3 = (                -b -  6.0f * c) / 6.0f;
152
153         if (x < -2.0f) return 0.0f;
154         if (x < -1.0f) return (q0 - x * (q1 - x * (q2 - x * q3)));
155         if (x < 0.0f) return (p0 + x * x * (p2 - x * p3));
156         if (x < 1.0f) return (p0 + x * x * (p2 + x * p3));
157         if (x < 2.0f) return (q0 + x * (q1 + x * (q2 + x * q3)));
158         return 0.0f;
159 }
160
161 /* x ranges from -1 to 1 */
162 float RE_filter_value(int type, float x)
163 {
164         float gaussfac = 1.6f;
165         
166         x = ABS(x);
167         
168         switch (type) {
169                 case R_FILTER_BOX:
170                         if (x > 1.0f) return 0.0f;
171                         return 1.0f;
172                         
173                 case R_FILTER_TENT:
174                         if (x > 1.0f) return 0.0f;
175                         return 1.0f - x;
176                         
177                 case R_FILTER_GAUSS:
178                         x *= gaussfac;
179                         return (1.0f / expf(x * x) - 1.0f / expf(gaussfac * gaussfac * 2.25f));
180                         
181                 case R_FILTER_MITCH:
182                         return filt_mitchell(x * gaussfac);
183                         
184                 case R_FILTER_QUAD:
185                         return filt_quadratic(x * gaussfac);
186                         
187                 case R_FILTER_CUBIC:
188                         return filt_cubic(x * gaussfac);
189                         
190                 case R_FILTER_CATROM:
191                         return filt_catrom(x * gaussfac);
192         }
193         return 0.0f;
194 }
195
196 static float calc_weight(Render *re, float *weight, int i, int j)
197 {
198         float x, y, dist, totw = 0.0;
199         int a;
200
201         for (a = 0; a < re->osa; a++) {
202                 x = re->jit[a][0] + i;
203                 y = re->jit[a][1] + j;
204                 dist = sqrt(x * x + y * y);
205
206                 weight[a] = 0.0;
207
208                 /* Weighting choices */
209                 switch (re->r.filtertype) {
210                         case R_FILTER_BOX:
211                                 if (i == 0 && j == 0) weight[a] = 1.0;
212                                 break;
213                         
214                         case R_FILTER_TENT:
215                                 if (dist < re->r.gauss)
216                                         weight[a] = re->r.gauss - dist;
217                                 break;
218                         
219                         case R_FILTER_GAUSS:
220                                 x = dist * re->r.gauss;
221                                 weight[a] = (1.0f / expf(x * x) - 1.0f / expf(re->r.gauss * re->r.gauss * 2.25f));
222                                 break;
223                 
224                         case R_FILTER_MITCH:
225                                 weight[a] = filt_mitchell(dist * re->r.gauss);
226                                 break;
227                 
228                         case R_FILTER_QUAD:
229                                 weight[a] = filt_quadratic(dist * re->r.gauss);
230                                 break;
231                         
232                         case R_FILTER_CUBIC:
233                                 weight[a] = filt_cubic(dist * re->r.gauss);
234                                 break;
235                         
236                         case R_FILTER_CATROM:
237                                 weight[a] = filt_catrom(dist * re->r.gauss);
238                                 break;
239                         
240                 }
241                 
242                 totw += weight[a];
243
244         }
245         return totw;
246 }
247
248 void free_sample_tables(Render *re)
249 {
250         int a;
251         
252         if (re->samples) {
253                 for (a = 0; a < 9; a++) {
254                         MEM_freeN(re->samples->fmask1[a]);
255                         MEM_freeN(re->samples->fmask2[a]);
256                 }
257                 
258                 MEM_freeN(re->samples->centmask);
259                 MEM_freeN(re->samples);
260                 re->samples = NULL;
261         }
262 }
263
264 /* based on settings in render, it makes the lookup tables */
265 void make_sample_tables(Render *re)
266 {
267         static int firsttime = 1;
268         SampleTables *st;
269         float flweight[32];
270         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4;
271         int i, j, a;
272
273         /* optimization tables, only once */
274         if (firsttime) {
275                 firsttime = 0;
276         }
277         
278         free_sample_tables(re);
279         
280         init_render_jit(re);    /* needed for mblur too */
281         
282         if (re->osa == 0) {
283                 /* just prevents cpu cycles for larger render and copying */
284                 re->r.filtertype = 0;
285                 return;
286         }
287         
288         st = re->samples = MEM_callocN(sizeof(SampleTables), "sample tables");
289         
290         for (a = 0; a < 9; a++) {
291                 st->fmask1[a] = MEM_callocN(256 * sizeof(float), "initfilt");
292                 st->fmask2[a] = MEM_callocN(256 * sizeof(float), "initfilt");
293         }
294         for (a = 0; a < 256; a++) {
295                 st->cmask[a] = 0;
296                 if (a &   1) st->cmask[a]++;
297                 if (a &   2) st->cmask[a]++;
298                 if (a &   4) st->cmask[a]++;
299                 if (a &   8) st->cmask[a]++;
300                 if (a &  16) st->cmask[a]++;
301                 if (a &  32) st->cmask[a]++;
302                 if (a &  64) st->cmask[a]++;
303                 if (a & 128) st->cmask[a]++;
304         }
305         
306         st->centmask = MEM_mallocN((1 << re->osa), "Initfilt3");
307         
308         for (a = 0; a < 16; a++) {
309                 st->centLut[a] = -0.45f + ((float)a) / 16.0f;
310         }
311
312         /* calculate totw */
313         totw = 0.0;
314         for (j = -1; j < 2; j++) {
315                 for (i = -1; i < 2; i++) {
316                         totw += calc_weight(re, weight, i, j);
317                 }
318         }
319
320         for (j = -1; j < 2; j++) {
321                 for (i = -1; i < 2; i++) {
322                         /* calculate using jit, with offset the weights */
323
324                         memset(weight, 0, sizeof(weight));
325                         calc_weight(re, weight, i, j);
326
327                         for (a = 0; a < 16; a++) flweight[a] = weight[a] * (1.0f / totw);
328
329                         m3 = st->fmask1[3 * (j + 1) + i + 1];
330                         m4 = st->fmask2[3 * (j + 1) + i + 1];
331
332                         for (a = 0; a < 256; a++) {
333                                 if (a &   1) {
334                                         m3[a] += flweight[0];
335                                         m4[a] += flweight[8];
336                                 }
337                                 if (a &   2) {
338                                         m3[a] += flweight[1];
339                                         m4[a] += flweight[9];
340                                 }
341                                 if (a &   4) {
342                                         m3[a] += flweight[2];
343                                         m4[a] += flweight[10];
344                                 }
345                                 if (a &   8) {
346                                         m3[a] += flweight[3];
347                                         m4[a] += flweight[11];
348                                 }
349                                 if (a &  16) {
350                                         m3[a] += flweight[4];
351                                         m4[a] += flweight[12];
352                                 }
353                                 if (a &  32) {
354                                         m3[a] += flweight[5];
355                                         m4[a] += flweight[13];
356                                 }
357                                 if (a &  64) {
358                                         m3[a] += flweight[6];
359                                         m4[a] += flweight[14];
360                                 }
361                                 if (a & 128) {
362                                         m3[a] += flweight[7];
363                                         m4[a] += flweight[15];
364                                 }
365                         }
366                 }
367         }
368
369         /* centmask: the correct subpixel offset per mask */
370
371         fpx1 = MEM_mallocN(256 * sizeof(float), "initgauss4");
372         fpx2 = MEM_mallocN(256 * sizeof(float), "initgauss4");
373         fpy1 = MEM_mallocN(256 * sizeof(float), "initgauss4");
374         fpy2 = MEM_mallocN(256 * sizeof(float), "initgauss4");
375         for (a = 0; a < 256; a++) {
376                 fpx1[a] = fpx2[a] = 0.0;
377                 fpy1[a] = fpy2[a] = 0.0;
378                 if (a & 1) {
379                         fpx1[a] += re->jit[0][0];
380                         fpy1[a] += re->jit[0][1];
381                         fpx2[a] += re->jit[8][0];
382                         fpy2[a] += re->jit[8][1];
383                 }
384                 if (a & 2) {
385                         fpx1[a] += re->jit[1][0];
386                         fpy1[a] += re->jit[1][1];
387                         fpx2[a] += re->jit[9][0];
388                         fpy2[a] += re->jit[9][1];
389                 }
390                 if (a & 4) {
391                         fpx1[a] += re->jit[2][0];
392                         fpy1[a] += re->jit[2][1];
393                         fpx2[a] += re->jit[10][0];
394                         fpy2[a] += re->jit[10][1];
395                 }
396                 if (a & 8) {
397                         fpx1[a] += re->jit[3][0];
398                         fpy1[a] += re->jit[3][1];
399                         fpx2[a] += re->jit[11][0];
400                         fpy2[a] += re->jit[11][1];
401                 }
402                 if (a & 16) {
403                         fpx1[a] += re->jit[4][0];
404                         fpy1[a] += re->jit[4][1];
405                         fpx2[a] += re->jit[12][0];
406                         fpy2[a] += re->jit[12][1];
407                 }
408                 if (a & 32) {
409                         fpx1[a] += re->jit[5][0];
410                         fpy1[a] += re->jit[5][1];
411                         fpx2[a] += re->jit[13][0];
412                         fpy2[a] += re->jit[13][1];
413                 }
414                 if (a & 64) {
415                         fpx1[a] += re->jit[6][0];
416                         fpy1[a] += re->jit[6][1];
417                         fpx2[a] += re->jit[14][0];
418                         fpy2[a] += re->jit[14][1];
419                 }
420                 if (a & 128) {
421                         fpx1[a] += re->jit[7][0];
422                         fpy1[a] += re->jit[7][1];
423                         fpx2[a] += re->jit[15][0];
424                         fpy2[a] += re->jit[15][1];
425                 }
426         }
427
428         for (a = (1 << re->osa) - 1; a > 0; a--) {
429                 val = st->cmask[a & 255] + st->cmask[a >> 8];
430                 i = 8 + (15.9f * (fpy1[a & 255] + fpy2[a >> 8]) / val);
431                 CLAMP(i, 0, 15);
432                 j = 8 + (15.9f * (fpx1[a & 255] + fpx2[a >> 8]) / val);
433                 CLAMP(j, 0, 15);
434                 i = j + (i << 4);
435                 st->centmask[a] = i;
436         }
437
438         MEM_freeN(fpx1);
439         MEM_freeN(fpx2);
440         MEM_freeN(fpy1);
441         MEM_freeN(fpy2);
442 }
443
444
445 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
446
447 struct Object *RE_GetCamera(Render *re)
448 {
449         return re->camera_override ? re->camera_override : re->scene->camera;
450 }
451
452 static void re_camera_params_get(Render *re, CameraParams *params, Object *cam_ob)
453 {
454         copy_m4_m4(re->winmat, params->winmat);
455
456         re->clipsta = params->clipsta;
457         re->clipend = params->clipend;
458
459         re->ycor = params->ycor;
460         re->viewdx = params->viewdx;
461         re->viewdy = params->viewdy;
462         re->viewplane = params->viewplane;
463
464         BKE_camera_object_mode(&re->r, cam_ob);
465 }
466
467 void RE_SetEnvmapCamera(Render *re, Object *cam_ob, float viewscale, float clipsta, float clipend)
468 {
469         CameraParams params;
470
471         /* setup parameters */
472         BKE_camera_params_init(&params);
473         BKE_camera_params_from_object(&params, cam_ob);
474
475         params.lens = 16.0f * viewscale;
476         params.sensor_x = 32.0f;
477         params.sensor_y = 32.0f;
478         params.sensor_fit = CAMERA_SENSOR_FIT_AUTO;
479         params.clipsta = clipsta;
480         params.clipend = clipend;
481         
482         /* compute matrix, viewplane, .. */
483         BKE_camera_params_compute_viewplane(&params, re->winx, re->winy, 1.0f, 1.0f);
484         BKE_camera_params_compute_matrix(&params);
485
486         /* extract results */
487         re_camera_params_get(re, &params, cam_ob);
488 }
489
490 /* call this after InitState() */
491 /* per render, there's one persistent viewplane. Parts will set their own viewplanes */
492 void RE_SetCamera(Render *re, Object *cam_ob)
493 {
494         CameraParams params;
495
496         /* setup parameters */
497         BKE_camera_params_init(&params);
498         BKE_camera_params_from_object(&params, cam_ob);
499
500         params.use_fields = (re->r.mode & R_FIELDS);
501         params.field_second = (re->flag & R_SEC_FIELD);
502         params.field_odd = (re->r.mode & R_ODDFIELD);
503
504         /* compute matrix, viewplane, .. */
505         BKE_camera_params_compute_viewplane(&params, re->winx, re->winy, re->r.xasp, re->r.yasp);
506         BKE_camera_params_compute_matrix(&params);
507
508         /* extract results */
509         re_camera_params_get(re, &params, cam_ob);
510 }
511
512 void RE_SetPixelSize(Render *re, float pixsize)
513 {
514         re->viewdx = pixsize;
515         re->viewdy = re->ycor * pixsize;
516 }
517
518 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[][4])
519 {
520         re->r.cfra = frame;
521         RE_SetCamera(re, camera);
522         copy_m4_m4(mat, re->winmat);
523 }
524
525 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */
526
527
528 void freeparts(Render *re)
529 {
530         RenderPart *part = re->parts.first;
531         
532         while (part) {
533                 if (part->rectp) MEM_freeN(part->rectp);
534                 if (part->rectz) MEM_freeN(part->rectz);
535                 part = part->next;
536         }
537         BLI_freelistN(&re->parts);
538 }
539
540 void initparts(Render *re, int do_crop)
541 {
542         int nr, xd, yd, partx, party, xparts, yparts;
543         int xminb, xmaxb, yminb, ymaxb;
544         
545         freeparts(re);
546         
547         /* this is render info for caller, is not reset when parts are freed! */
548         re->i.totpart = 0;
549         re->i.curpart = 0;
550         re->i.partsdone = FALSE;
551         
552         /* just for readable code.. */
553         xminb = re->disprect.xmin;
554         yminb = re->disprect.ymin;
555         xmaxb = re->disprect.xmax;
556         ymaxb = re->disprect.ymax;
557         
558         xparts = re->r.xparts;
559         yparts = re->r.yparts;
560         
561         /* mininum part size, but for exr tile saving it was checked already */
562         if (!(re->r.scemode & (R_EXR_TILE_FILE | R_FULL_SAMPLE))) {
563                 if (re->r.mode & R_PANORAMA) {
564                         if (ceil(re->rectx / (float)xparts) < 8)
565                                 xparts = 1 + re->rectx / 8;
566                 }
567                 else
568                 if (ceil(re->rectx / (float)xparts) < 64)
569                         xparts = 1 + re->rectx / 64;
570                 
571                 if (ceil(re->recty / (float)yparts) < 64)
572                         yparts = 1 + re->recty / 64;
573         }
574         
575         /* part size */
576         partx = ceil(re->rectx / (float)xparts);
577         party = ceil(re->recty / (float)yparts);
578         
579         re->xparts = xparts;
580         re->yparts = yparts;
581         re->partx = partx;
582         re->party = party;
583         
584         /* calculate rotation factor of 1 pixel */
585         if (re->r.mode & R_PANORAMA)
586                 re->panophi = panorama_pixel_rot(re);
587         
588         for (nr = 0; nr < xparts * yparts; nr++) {
589                 rcti disprect;
590                 int rectx, recty;
591                 
592                 xd = (nr % xparts);
593                 yd = (nr - xd) / xparts;
594                 
595                 disprect.xmin = xminb + xd * partx;
596                 disprect.ymin = yminb + yd * party;
597                 
598                 /* ensure we cover the entire picture, so last parts go to end */
599                 if (xd < xparts - 1) {
600                         disprect.xmax = disprect.xmin + partx;
601                         if (disprect.xmax > xmaxb)
602                                 disprect.xmax = xmaxb;
603                 }
604                 else disprect.xmax = xmaxb;
605                 
606                 if (yd < yparts - 1) {
607                         disprect.ymax = disprect.ymin + party;
608                         if (disprect.ymax > ymaxb)
609                                 disprect.ymax = ymaxb;
610                 }
611                 else disprect.ymax = ymaxb;
612                 
613                 rectx = BLI_RCT_SIZE_X(&disprect);
614                 recty = BLI_RCT_SIZE_Y(&disprect);
615                 
616                 /* so, now can we add this part? */
617                 if (rectx > 0 && recty > 0) {
618                         RenderPart *pa = MEM_callocN(sizeof(RenderPart), "new part");
619                         
620                         /* Non-box filters need 2 pixels extra to work */
621                         if (do_crop && (re->r.filtertype || (re->r.mode & R_EDGE))) {
622                                 pa->crop = 2;
623                                 disprect.xmin -= pa->crop;
624                                 disprect.ymin -= pa->crop;
625                                 disprect.xmax += pa->crop;
626                                 disprect.ymax += pa->crop;
627                                 rectx += 2 * pa->crop;
628                                 recty += 2 * pa->crop;
629                         }
630                         pa->disprect = disprect;
631                         pa->rectx = rectx;
632                         pa->recty = recty;
633
634                         BLI_addtail(&re->parts, pa);
635                         re->i.totpart++;
636                 }
637         }
638 }
639
640
641