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