Code cleanup: fix some vs2012 compiler warnings
[blender.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, centmasksize;
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         centmasksize = (1 << re->osa);
307         st->centmask = MEM_mallocN(centmasksize, "Initfilt3");
308         
309         for (a = 0; a < 16; a++) {
310                 st->centLut[a] = -0.45f + ((float)a) / 16.0f;
311         }
312
313         /* calculate totw */
314         totw = 0.0;
315         for (j = -1; j < 2; j++) {
316                 for (i = -1; i < 2; i++) {
317                         totw += calc_weight(re, weight, i, j);
318                 }
319         }
320
321         for (j = -1; j < 2; j++) {
322                 for (i = -1; i < 2; i++) {
323                         /* calculate using jit, with offset the weights */
324
325                         memset(weight, 0, sizeof(weight));
326                         calc_weight(re, weight, i, j);
327
328                         for (a = 0; a < 16; a++) flweight[a] = weight[a] * (1.0f / totw);
329
330                         m3 = st->fmask1[3 * (j + 1) + i + 1];
331                         m4 = st->fmask2[3 * (j + 1) + i + 1];
332
333                         for (a = 0; a < 256; a++) {
334                                 if (a &   1) {
335                                         m3[a] += flweight[0];
336                                         m4[a] += flweight[8];
337                                 }
338                                 if (a &   2) {
339                                         m3[a] += flweight[1];
340                                         m4[a] += flweight[9];
341                                 }
342                                 if (a &   4) {
343                                         m3[a] += flweight[2];
344                                         m4[a] += flweight[10];
345                                 }
346                                 if (a &   8) {
347                                         m3[a] += flweight[3];
348                                         m4[a] += flweight[11];
349                                 }
350                                 if (a &  16) {
351                                         m3[a] += flweight[4];
352                                         m4[a] += flweight[12];
353                                 }
354                                 if (a &  32) {
355                                         m3[a] += flweight[5];
356                                         m4[a] += flweight[13];
357                                 }
358                                 if (a &  64) {
359                                         m3[a] += flweight[6];
360                                         m4[a] += flweight[14];
361                                 }
362                                 if (a & 128) {
363                                         m3[a] += flweight[7];
364                                         m4[a] += flweight[15];
365                                 }
366                         }
367                 }
368         }
369
370         /* centmask: the correct subpixel offset per mask */
371
372         fpx1 = MEM_mallocN(256 * sizeof(float), "initgauss4");
373         fpx2 = MEM_mallocN(256 * sizeof(float), "initgauss4");
374         fpy1 = MEM_mallocN(256 * sizeof(float), "initgauss4");
375         fpy2 = MEM_mallocN(256 * sizeof(float), "initgauss4");
376         for (a = 0; a < 256; a++) {
377                 fpx1[a] = fpx2[a] = 0.0;
378                 fpy1[a] = fpy2[a] = 0.0;
379                 if (a & 1) {
380                         fpx1[a] += re->jit[0][0];
381                         fpy1[a] += re->jit[0][1];
382                         fpx2[a] += re->jit[8][0];
383                         fpy2[a] += re->jit[8][1];
384                 }
385                 if (a & 2) {
386                         fpx1[a] += re->jit[1][0];
387                         fpy1[a] += re->jit[1][1];
388                         fpx2[a] += re->jit[9][0];
389                         fpy2[a] += re->jit[9][1];
390                 }
391                 if (a & 4) {
392                         fpx1[a] += re->jit[2][0];
393                         fpy1[a] += re->jit[2][1];
394                         fpx2[a] += re->jit[10][0];
395                         fpy2[a] += re->jit[10][1];
396                 }
397                 if (a & 8) {
398                         fpx1[a] += re->jit[3][0];
399                         fpy1[a] += re->jit[3][1];
400                         fpx2[a] += re->jit[11][0];
401                         fpy2[a] += re->jit[11][1];
402                 }
403                 if (a & 16) {
404                         fpx1[a] += re->jit[4][0];
405                         fpy1[a] += re->jit[4][1];
406                         fpx2[a] += re->jit[12][0];
407                         fpy2[a] += re->jit[12][1];
408                 }
409                 if (a & 32) {
410                         fpx1[a] += re->jit[5][0];
411                         fpy1[a] += re->jit[5][1];
412                         fpx2[a] += re->jit[13][0];
413                         fpy2[a] += re->jit[13][1];
414                 }
415                 if (a & 64) {
416                         fpx1[a] += re->jit[6][0];
417                         fpy1[a] += re->jit[6][1];
418                         fpx2[a] += re->jit[14][0];
419                         fpy2[a] += re->jit[14][1];
420                 }
421                 if (a & 128) {
422                         fpx1[a] += re->jit[7][0];
423                         fpy1[a] += re->jit[7][1];
424                         fpx2[a] += re->jit[15][0];
425                         fpy2[a] += re->jit[15][1];
426                 }
427         }
428
429         for (a = centmasksize - 1; a > 0; a--) {
430                 val = st->cmask[a & 255] + st->cmask[a >> 8];
431                 i = 8 + (15.9f * (fpy1[a & 255] + fpy2[a >> 8]) / val);
432                 CLAMP(i, 0, 15);
433                 j = 8 + (15.9f * (fpx1[a & 255] + fpx2[a >> 8]) / val);
434                 CLAMP(j, 0, 15);
435                 i = j + (i << 4);
436                 st->centmask[a] = i;
437         }
438
439         MEM_freeN(fpx1);
440         MEM_freeN(fpx2);
441         MEM_freeN(fpy1);
442         MEM_freeN(fpy2);
443 }
444
445
446 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
447
448 struct Object *RE_GetCamera(Render *re)
449 {
450         return re->camera_override ? re->camera_override : re->scene->camera;
451 }
452
453 static void re_camera_params_get(Render *re, CameraParams *params, Object *cam_ob)
454 {
455         copy_m4_m4(re->winmat, params->winmat);
456
457         re->clipsta = params->clipsta;
458         re->clipend = params->clipend;
459
460         re->ycor = params->ycor;
461         re->viewdx = params->viewdx;
462         re->viewdy = params->viewdy;
463         re->viewplane = params->viewplane;
464
465         BKE_camera_object_mode(&re->r, cam_ob);
466 }
467
468 void RE_SetEnvmapCamera(Render *re, Object *cam_ob, float viewscale, float clipsta, float clipend)
469 {
470         CameraParams params;
471
472         /* setup parameters */
473         BKE_camera_params_init(&params);
474         BKE_camera_params_from_object(&params, cam_ob);
475
476         params.lens = 16.0f * viewscale;
477         params.sensor_x = 32.0f;
478         params.sensor_y = 32.0f;
479         params.sensor_fit = CAMERA_SENSOR_FIT_AUTO;
480         params.clipsta = clipsta;
481         params.clipend = clipend;
482         
483         /* compute matrix, viewplane, .. */
484         BKE_camera_params_compute_viewplane(&params, re->winx, re->winy, 1.0f, 1.0f);
485         BKE_camera_params_compute_matrix(&params);
486
487         /* extract results */
488         re_camera_params_get(re, &params, cam_ob);
489 }
490
491 /* call this after InitState() */
492 /* per render, there's one persistent viewplane. Parts will set their own viewplanes */
493 void RE_SetCamera(Render *re, Object *cam_ob)
494 {
495         CameraParams params;
496
497         /* setup parameters */
498         BKE_camera_params_init(&params);
499         BKE_camera_params_from_object(&params, cam_ob);
500
501         params.use_fields = (re->r.mode & R_FIELDS);
502         params.field_second = (re->flag & R_SEC_FIELD);
503         params.field_odd = (re->r.mode & R_ODDFIELD);
504
505         /* compute matrix, viewplane, .. */
506         BKE_camera_params_compute_viewplane(&params, re->winx, re->winy, re->r.xasp, re->r.yasp);
507         BKE_camera_params_compute_matrix(&params);
508
509         /* extract results */
510         re_camera_params_get(re, &params, cam_ob);
511 }
512
513 void RE_SetPixelSize(Render *re, float pixsize)
514 {
515         re->viewdx = pixsize;
516         re->viewdy = re->ycor * pixsize;
517 }
518
519 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[4][4])
520 {
521         re->r.cfra = frame;
522         RE_SetCamera(re, camera);
523         copy_m4_m4(mat, re->winmat);
524 }
525
526 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */
527
528
529 void RE_parts_free(Render *re)
530 {
531         RenderPart *part = re->parts.first;
532         
533         while (part) {
534                 if (part->rectp) MEM_freeN(part->rectp);
535                 if (part->rectz) MEM_freeN(part->rectz);
536                 part = part->next;
537         }
538         BLI_freelistN(&re->parts);
539 }
540
541 void RE_parts_clamp(Render *re)
542 {
543         /* part size */
544         re->partx = min_ii(re->r.tilex, re->rectx);
545         re->party = min_ii(re->r.tiley, re->recty);
546 }
547
548 void RE_parts_init(Render *re, int do_crop)
549 {
550         int nr, xd, yd, partx, party, xparts, yparts;
551         int xminb, xmaxb, yminb, ymaxb;
552         
553         RE_parts_free(re);
554         
555         /* this is render info for caller, is not reset when parts are freed! */
556         re->i.totpart = 0;
557         re->i.curpart = 0;
558         re->i.partsdone = FALSE;
559         
560         /* just for readable code.. */
561         xminb = re->disprect.xmin;
562         yminb = re->disprect.ymin;
563         xmaxb = re->disprect.xmax;
564         ymaxb = re->disprect.ymax;
565         
566         RE_parts_clamp(re);
567
568         partx = re->partx;
569         party = re->party;
570         /* part count */
571         xparts = (re->rectx + partx - 1) / partx;
572         yparts = (re->recty + party - 1) / party;
573         
574         /* calculate rotation factor of 1 pixel */
575         if (re->r.mode & R_PANORAMA)
576                 re->panophi = panorama_pixel_rot(re);
577         
578         for (nr = 0; nr < xparts * yparts; nr++) {
579                 rcti disprect;
580                 int rectx, recty;
581                 
582                 xd = (nr % xparts);
583                 yd = (nr - xd) / xparts;
584                 
585                 disprect.xmin = xminb + xd * partx;
586                 disprect.ymin = yminb + yd * party;
587                 
588                 /* ensure we cover the entire picture, so last parts go to end */
589                 if (xd < xparts - 1) {
590                         disprect.xmax = disprect.xmin + partx;
591                         if (disprect.xmax > xmaxb)
592                                 disprect.xmax = xmaxb;
593                 }
594                 else disprect.xmax = xmaxb;
595                 
596                 if (yd < yparts - 1) {
597                         disprect.ymax = disprect.ymin + party;
598                         if (disprect.ymax > ymaxb)
599                                 disprect.ymax = ymaxb;
600                 }
601                 else disprect.ymax = ymaxb;
602                 
603                 rectx = BLI_rcti_size_x(&disprect);
604                 recty = BLI_rcti_size_y(&disprect);
605                 
606                 /* so, now can we add this part? */
607                 if (rectx > 0 && recty > 0) {
608                         RenderPart *pa = MEM_callocN(sizeof(RenderPart), "new part");
609                         
610                         /* Non-box filters need 2 pixels extra to work */
611                         if (do_crop && (re->r.filtertype || (re->r.mode & R_EDGE))) {
612                                 pa->crop = 2;
613                                 disprect.xmin -= pa->crop;
614                                 disprect.ymin -= pa->crop;
615                                 disprect.xmax += pa->crop;
616                                 disprect.ymax += pa->crop;
617                                 rectx += 2 * pa->crop;
618                                 recty += 2 * pa->crop;
619                         }
620                         pa->disprect = disprect;
621                         pa->rectx = rectx;
622                         pa->recty = recty;
623
624                         BLI_addtail(&re->parts, pa);
625                         re->i.totpart++;
626                 }
627         }
628 }
629
630
631