[#11706] Camera IPO Lag
[blender.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
29 /* Global includes */
30
31 #include <math.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdio.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "PIL_time.h"
39
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_jitter.h"
43
44 #include "MTC_matrixops.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 #include "BKE_utildefines.h"
54 #include "BKE_global.h"
55 #include "BKE_material.h"
56 #include "BKE_object.h"
57 #include "BKE_image.h"
58 #include "BKE_ipo.h"
59 #include "BKE_key.h"
60 #include "BKE_action.h"
61 #include "BKE_writeavi.h"
62 #include "BKE_scene.h"
63
64 #include "IMB_imbuf_types.h"
65 #include "IMB_imbuf.h"
66
67 #ifdef WITH_QUICKTIME
68 #include "quicktime_export.h"
69 #endif
70
71 /* this module */
72 #include "renderpipeline.h"
73 #include "render_types.h"
74
75 #include "rendercore.h"
76 #include "pixelshading.h"
77 #include "zbuf.h"
78
79 /* Own includes */
80 #include "initrender.h"
81
82
83 /* ********************** */
84
85 static void init_render_jit(Render *re)
86 {
87         static float jit[32][2];        /* simple caching */
88         static int lastjit= 0;
89         
90         if(lastjit!=re->r.osa) {
91                 memset(jit, 0, sizeof(jit));
92                 BLI_initjit(jit[0], re->r.osa);
93         }
94         
95         lastjit= re->r.osa;
96         memcpy(re->jit, jit, sizeof(jit));
97 }
98
99
100 /* ****************** MASKS and LUTS **************** */
101
102 static float filt_quadratic(float x)
103 {
104     if (x <  0.0f) x = -x;
105     if (x < 0.5f) return 0.75f-(x*x);
106     if (x < 1.5f) return 0.50f*(x-1.5f)*(x-1.5f);
107     return 0.0f;
108 }
109
110
111 static float filt_cubic(float x)
112 {
113         float x2= x*x;
114         
115     if (x <  0.0f) x = -x;
116         
117     if (x < 1.0f) return 0.5*x*x2 - x2 + 2.0f/3.0f;
118     if (x < 2.0f) return (2.0-x)*(2.0-x)*(2.0-x)/6.0f;
119     return 0.0f;
120 }
121
122
123 static float filt_catrom(float x)
124 {
125         float x2= x*x;
126         
127     if (x <  0.0f) x = -x;
128     if (x < 1.0f) return  1.5f*x2*x - 2.5f*x2  + 1.0f;
129     if (x < 2.0f) return -0.5f*x2*x + 2.5*x2 - 4.0f*x + 2.0f;
130     return 0.0f;
131 }
132
133 static float filt_mitchell(float x)     /* Mitchell & Netravali's two-param cubic */
134 {
135         float b = 1.0f/3.0f, c = 1.0f/3.0f;
136         float p0 = (  6.0 -  2.0*b         ) / 6.0;
137         float p2 = (-18.0 + 12.0*b +  6.0*c) / 6.0;
138         float p3 = ( 12.0 -  9.0*b -  6.0*c) / 6.0;
139         float q0 = (       8.0*b + 24.0*c) / 6.0;
140         float q1 = (      - 12.0*b - 48.0*c) / 6.0;
141         float q2 = (         6.0*b + 30.0*c) / 6.0;
142         float q3 = (       -     b -  6.0*c) / 6.0;
143
144         if (x<-2.0) return 0.0;
145         if (x<-1.0) return (q0-x*(q1-x*(q2-x*q3)));
146         if (x< 0.0) return (p0+x*x*(p2-x*p3));
147         if (x< 1.0) return (p0+x*x*(p2+x*p3));
148         if (x< 2.0) return (q0+x*(q1+x*(q2+x*q3)));
149         return 0.0;
150 }
151
152 /* x ranges from -1 to 1 */
153 float RE_filter_value(int type, float x)
154 {
155         float gaussfac= 1.6f;
156         
157         x= ABS(x);
158         
159         switch(type) {
160                 case R_FILTER_BOX:
161                         if(x>1.0) return 0.0f;
162                         return 1.0;
163                         
164                 case R_FILTER_TENT:
165                         if(x>1.0) return 0.0f;
166                         return 1.0f-x;
167                         
168                 case R_FILTER_GAUSS:
169                         x*= gaussfac;
170                         return (1.0/exp(x*x) - 1.0/exp(gaussfac*gaussfac*2.25));
171                         
172                 case R_FILTER_MITCH:
173                         return filt_mitchell(x*gaussfac);
174                         
175                 case R_FILTER_QUAD:
176                         return filt_quadratic(x*gaussfac);
177                         
178                 case R_FILTER_CUBIC:
179                         return filt_cubic(x*gaussfac);
180                         
181                 case R_FILTER_CATROM:
182                         return filt_catrom(x*gaussfac);
183         }
184         return 0.0f;
185 }
186
187 static float calc_weight(Render *re, float *weight, int i, int j)
188 {
189         float x, y, dist, totw= 0.0;
190         int a;
191
192         for(a=0; a<re->osa; a++) {
193                 x= re->jit[a][0] + i;
194                 y= re->jit[a][1] + j;
195                 dist= sqrt(x*x+y*y);
196
197                 weight[a]= 0.0;
198
199                 /* Weighting choices */
200                 switch(re->r.filtertype) {
201                 case R_FILTER_BOX:
202                         if(i==0 && j==0) weight[a]= 1.0;
203                         break;
204                         
205                 case R_FILTER_TENT:
206                         if(dist < re->r.gauss)
207                                 weight[a]= re->r.gauss - dist;
208                         break;
209                         
210                 case R_FILTER_GAUSS:
211                         x = dist*re->r.gauss;
212                         weight[a]= (1.0/exp(x*x) - 1.0/exp(re->r.gauss*re->r.gauss*2.25));
213                         break;
214                 
215                 case R_FILTER_MITCH:
216                         weight[a]= filt_mitchell(dist*re->r.gauss);
217                         break;
218                 
219                 case R_FILTER_QUAD:
220                         weight[a]= filt_quadratic(dist*re->r.gauss);
221                         break;
222                         
223                 case R_FILTER_CUBIC:
224                         weight[a]= filt_cubic(dist*re->r.gauss);
225                         break;
226                         
227                 case R_FILTER_CATROM:
228                         weight[a]= filt_catrom(dist*re->r.gauss);
229                         break;
230                         
231                 }
232                 
233                 totw+= weight[a];
234
235         }
236         return totw;
237 }
238
239 void free_sample_tables(Render *re)
240 {
241         int a;
242         
243         if(re->samples) {
244                 for(a=0; a<9; a++) {
245                         MEM_freeN(re->samples->fmask1[a]);
246                         MEM_freeN(re->samples->fmask2[a]);
247                 }
248                 
249                 MEM_freeN(re->samples->centmask);
250                 MEM_freeN(re->samples);
251                 re->samples= NULL;
252         }
253 }
254
255 /* based on settings in render, it makes the lookup tables */
256 void make_sample_tables(Render *re)
257 {
258         static int firsttime= 1;
259         SampleTables *st;
260         float flweight[32], fmask[256];
261         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4;
262         int i, j, a;
263
264         /* optimization tables, only once */
265         if(firsttime) {
266                 firsttime= 0;
267         }
268         
269         free_sample_tables(re);
270         
271         init_render_jit(re);    /* needed for mblur too */
272         
273         if(re->osa==0) {
274                 /* just prevents cpu cycles for larger render and copying */
275                 re->r.filtertype= 0;
276                 return;
277         }
278         
279         st= re->samples= MEM_callocN(sizeof(SampleTables), "sample tables");
280         
281         for(a=0; a<9;a++) {
282                 st->fmask1[a]= MEM_callocN(256*sizeof(float), "initfilt");
283                 st->fmask2[a]= MEM_callocN(256*sizeof(float), "initfilt");
284         }
285         for(a=0; a<256; a++) {
286                 st->cmask[a]= 0;
287                 if(a &   1) st->cmask[a]++;
288                 if(a &   2) st->cmask[a]++;
289                 if(a &   4) st->cmask[a]++;
290                 if(a &   8) st->cmask[a]++;
291                 if(a &  16) st->cmask[a]++;
292                 if(a &  32) st->cmask[a]++;
293                 if(a &  64) st->cmask[a]++;
294                 if(a & 128) st->cmask[a]++;
295         }
296         
297         st->centmask= MEM_mallocN((1<<re->osa), "Initfilt3");
298         
299         for(a=0; a<16; a++) {
300                 st->centLut[a]= -0.45+((float)a)/16.0;
301         }
302
303         val= 1.0/((float)re->osa);
304         for(a=0; a<256; a++) {
305                 fmask[a]= ((float)st->cmask[a])*val;
306         }
307
308         /* calculate totw */
309         totw= 0.0;
310         for(j= -1; j<2; j++) {
311                 for(i= -1; i<2; i++) {
312                         totw+= calc_weight(re, weight, i, j);
313                 }
314         }
315
316         for(j= -1; j<2; j++) {
317                 for(i= -1; i<2; i++) {
318                         /* calculate using jit, with offset the weights */
319
320                         memset(weight, 0, sizeof(weight));
321                         calc_weight(re, weight, i, j);
322
323                         for(a=0; a<16; a++) flweight[a]= weight[a]*(1.0/totw);
324
325                         m3= st->fmask1[ 3*(j+1)+i+1 ];
326                         m4= st->fmask2[ 3*(j+1)+i+1 ];
327
328                         for(a=0; a<256; a++) {
329                                 if(a &   1) {
330                                         m3[a]+= flweight[0];
331                                         m4[a]+= flweight[8];
332                                 }
333                                 if(a &   2) {
334                                         m3[a]+= flweight[1];
335                                         m4[a]+= flweight[9];
336                                 }
337                                 if(a &   4) {
338                                         m3[a]+= flweight[2];
339                                         m4[a]+= flweight[10];
340                                 }
341                                 if(a &   8) {
342                                         m3[a]+= flweight[3];
343                                         m4[a]+= flweight[11];
344                                 }
345                                 if(a &  16) {
346                                         m3[a]+= flweight[4];
347                                         m4[a]+= flweight[12];
348                                 }
349                                 if(a &  32) {
350                                         m3[a]+= flweight[5];
351                                         m4[a]+= flweight[13];
352                                 }
353                                 if(a &  64) {
354                                         m3[a]+= flweight[6];
355                                         m4[a]+= flweight[14];
356                                 }
357                                 if(a & 128) {
358                                         m3[a]+= flweight[7];
359                                         m4[a]+= flweight[15];
360                                 }
361                         }
362                 }
363         }
364
365         /* centmask: the correct subpixel offset per mask */
366
367         fpx1= MEM_mallocN(256*sizeof(float), "initgauss4");
368         fpx2= MEM_mallocN(256*sizeof(float), "initgauss4");
369         fpy1= MEM_mallocN(256*sizeof(float), "initgauss4");
370         fpy2= MEM_mallocN(256*sizeof(float), "initgauss4");
371         for(a=0; a<256; a++) {
372                 fpx1[a]= fpx2[a]= 0.0;
373                 fpy1[a]= fpy2[a]= 0.0;
374                 if(a & 1) {
375                         fpx1[a]+= re->jit[0][0];
376                         fpy1[a]+= re->jit[0][1];
377                         fpx2[a]+= re->jit[8][0];
378                         fpy2[a]+= re->jit[8][1];
379                 }
380                 if(a & 2) {
381                         fpx1[a]+= re->jit[1][0];
382                         fpy1[a]+= re->jit[1][1];
383                         fpx2[a]+= re->jit[9][0];
384                         fpy2[a]+= re->jit[9][1];
385                 }
386                 if(a & 4) {
387                         fpx1[a]+= re->jit[2][0];
388                         fpy1[a]+= re->jit[2][1];
389                         fpx2[a]+= re->jit[10][0];
390                         fpy2[a]+= re->jit[10][1];
391                 }
392                 if(a & 8) {
393                         fpx1[a]+= re->jit[3][0];
394                         fpy1[a]+= re->jit[3][1];
395                         fpx2[a]+= re->jit[11][0];
396                         fpy2[a]+= re->jit[11][1];
397                 }
398                 if(a & 16) {
399                         fpx1[a]+= re->jit[4][0];
400                         fpy1[a]+= re->jit[4][1];
401                         fpx2[a]+= re->jit[12][0];
402                         fpy2[a]+= re->jit[12][1];
403                 }
404                 if(a & 32) {
405                         fpx1[a]+= re->jit[5][0];
406                         fpy1[a]+= re->jit[5][1];
407                         fpx2[a]+= re->jit[13][0];
408                         fpy2[a]+= re->jit[13][1];
409                 }
410                 if(a & 64) {
411                         fpx1[a]+= re->jit[6][0];
412                         fpy1[a]+= re->jit[6][1];
413                         fpx2[a]+= re->jit[14][0];
414                         fpy2[a]+= re->jit[14][1];
415                 }
416                 if(a & 128) {
417                         fpx1[a]+= re->jit[7][0];
418                         fpy1[a]+= re->jit[7][1];
419                         fpx2[a]+= re->jit[15][0];
420                         fpy2[a]+= re->jit[15][1];
421                 }
422         }
423
424         for(a= (1<<re->osa)-1; a>0; a--) {
425                 val= st->cmask[a & 255] + st->cmask[a>>8];
426                 i= 8+(15.9*(fpy1[a & 255]+fpy2[a>>8])/val);
427                 CLAMP(i, 0, 15);
428                 j= 8+(15.9*(fpx1[a & 255]+fpx2[a>>8])/val);
429                 CLAMP(j, 0, 15);
430                 i= j + (i<<4);
431                 st->centmask[a]= i;
432         }
433
434         MEM_freeN(fpx1);
435         MEM_freeN(fpx2);
436         MEM_freeN(fpy1);
437         MEM_freeN(fpy2);
438 }
439
440
441 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
442
443 /* call this after InitState() */
444 /* per render, there's one persistant viewplane. Parts will set their own viewplanes */
445 void RE_SetCamera(Render *re, Object *camera)
446 {
447         Camera *cam=NULL;
448         rctf viewplane;
449         float pixsize, clipsta, clipend;
450         float lens, shiftx=0.0, shifty=0.0, winside;
451         
452         /* question mark */
453         re->ycor= ( (float)re->r.yasp)/( (float)re->r.xasp);
454         if(re->r.mode & R_FIELDS)
455                 re->ycor *= 2.0f;
456         
457         if(camera->type==OB_CAMERA) {
458                 cam= camera->data;
459                 
460                 if(cam->type==CAM_ORTHO) re->r.mode |= R_ORTHO;
461                 
462                 /* solve this too... all time depending stuff is in convertblender.c?
463                  * Need to update the camera early because it's used for projection matrices
464                  * and other stuff BEFORE the animation update loop is done 
465                  * */
466                 if(cam->ipo) {
467                         calc_ipo(cam->ipo, frame_to_float(re->r.cfra));
468                         execute_ipo(&cam->id, cam->ipo);
469                 }
470                 lens= cam->lens;
471                 shiftx=cam->shiftx;
472                 shifty=cam->shifty;
473
474                 clipsta= cam->clipsta;
475                 clipend= cam->clipend;
476         }
477         else if(camera->type==OB_LAMP) {
478                 Lamp *la= camera->data;
479                 float fac= cos( M_PI*la->spotsize/360.0 );
480                 float phi= acos(fac);
481                 
482                 lens= 16.0*fac/sin(phi);
483                 if(lens==0.0f)
484                         lens= 35.0;
485                 clipsta= la->clipsta;
486                 clipend= la->clipend;
487         }
488         else {  /* envmap exception... */
489                 lens= re->lens;
490                 if(lens==0.0f)
491                         lens= 16.0;
492                 
493                 clipsta= re->clipsta;
494                 clipend= re->clipend;
495                 if(clipsta==0.0f || clipend==0.0f) {
496                         clipsta= 0.1f;
497                         clipend= 1000.0f;
498                 }
499         }
500
501         /* ortho only with camera available */
502         if(cam && (re->r.mode & R_ORTHO)) {
503                 if( (re->r.xasp*re->winx) >= (re->r.yasp*re->winy) ) {
504                         re->viewfac= re->winx;
505                 }
506                 else {
507                         re->viewfac= re->ycor*re->winy;
508                 }
509                 /* ortho_scale == 1.0 means exact 1 to 1 mapping */
510                 pixsize= cam->ortho_scale/re->viewfac;
511         }
512         else {
513                 if( (re->r.xasp*re->winx) >= (re->r.yasp*re->winy) ) {
514                         re->viewfac= (re->winx*lens)/32.0;
515                 }
516                 else {
517                         re->viewfac= re->ycor*(re->winy*lens)/32.0;
518                 }
519                 
520                 pixsize= clipsta/re->viewfac;
521         }
522         
523         /* viewplane fully centered, zbuffer fills in jittered between -.5 and +.5 */
524         winside= MAX2(re->winx, re->winy);
525         viewplane.xmin= -0.5f*(float)re->winx + shiftx*winside; 
526         viewplane.ymin= -0.5f*re->ycor*(float)re->winy + shifty*winside;
527         viewplane.xmax=  0.5f*(float)re->winx + shiftx*winside; 
528         viewplane.ymax=  0.5f*re->ycor*(float)re->winy + shifty*winside; 
529
530         if(re->flag & R_SEC_FIELD) {
531                 if(re->r.mode & R_ODDFIELD) {
532                         viewplane.ymin-= .5*re->ycor;
533                         viewplane.ymax-= .5*re->ycor;
534                 }
535                 else {
536                         viewplane.ymin+= .5*re->ycor;
537                         viewplane.ymax+= .5*re->ycor;
538                 }
539         }
540         /* the window matrix is used for clipping, and not changed during OSA steps */
541         /* using an offset of +0.5 here would give clip errors on edges */
542         viewplane.xmin= pixsize*(viewplane.xmin);
543         viewplane.xmax= pixsize*(viewplane.xmax);
544         viewplane.ymin= pixsize*(viewplane.ymin);
545         viewplane.ymax= pixsize*(viewplane.ymax);
546         
547         re->viewdx= pixsize;
548         re->viewdy= re->ycor*pixsize;
549
550         if(re->r.mode & R_ORTHO)
551                 RE_SetOrtho(re, &viewplane, clipsta, clipend);
552         else 
553                 RE_SetWindow(re, &viewplane, clipsta, clipend);
554
555 }
556
557 void RE_SetPixelSize(Render *re, float pixsize)
558 {
559         re->viewdx= pixsize;
560         re->viewdy= re->ycor*pixsize;
561 }
562
563 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[][4])
564 {
565         re->r.cfra= frame;
566         RE_SetCamera(re, camera);
567         Mat4CpyMat4(mat, re->winmat);
568 }
569
570 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */
571
572
573 void freeparts(Render *re)
574 {
575         RenderPart *part= re->parts.first;
576         
577         while(part) {
578                 if(part->rectp) MEM_freeN(part->rectp);
579                 if(part->rectz) MEM_freeN(part->rectz);
580                 part= part->next;
581         }
582         BLI_freelistN(&re->parts);
583 }
584
585 void initparts(Render *re)
586 {
587         int nr, xd, yd, partx, party, xparts, yparts;
588         int xminb, xmaxb, yminb, ymaxb;
589         
590         freeparts(re);
591         
592         /* this is render info for caller, is not reset when parts are freed! */
593         re->i.totpart= 0;
594         re->i.curpart= 0;
595         re->i.partsdone= 0;
596         
597         /* just for readable code.. */
598         xminb= re->disprect.xmin;
599         yminb= re->disprect.ymin;
600         xmaxb= re->disprect.xmax;
601         ymaxb= re->disprect.ymax;
602         
603         xparts= re->r.xparts;
604         yparts= re->r.yparts;
605         
606         /* mininum part size, but for exr tile saving it was checked already */
607         if(!(re->r.scemode & R_EXR_TILE_FILE)) {
608                 if(re->r.mode & R_PANORAMA) {
609                         if(ceil(re->rectx/(float)xparts) < 8) 
610                                 xparts= 1 + re->rectx/8;
611                 }
612                 else
613                         if(ceil(re->rectx/(float)xparts) < 64) 
614                                 xparts= 1 + re->rectx/64;
615                 
616                 if(ceil(re->recty/(float)yparts) < 64) 
617                         yparts= 1 + re->recty/64;
618         }
619         
620         /* part size */
621         partx= ceil(re->rectx/(float)xparts);
622         party= ceil(re->recty/(float)yparts);
623         
624         re->xparts= xparts;
625         re->yparts= yparts;
626         re->partx= partx;
627         re->party= party;
628         
629         /* calculate rotation factor of 1 pixel */
630         if(re->r.mode & R_PANORAMA)
631                 re->panophi= panorama_pixel_rot(re);
632         
633         for(nr=0; nr<xparts*yparts; nr++) {
634                 rcti disprect;
635                 int rectx, recty;
636                 
637                 xd= (nr % xparts);
638                 yd= (nr-xd)/xparts;
639                 
640                 disprect.xmin= xminb+ xd*partx;
641                 disprect.ymin= yminb+ yd*party;
642                 
643                 /* ensure we cover the entire picture, so last parts go to end */
644                 if(xd<xparts-1) {
645                         disprect.xmax= disprect.xmin + partx;
646                         if(disprect.xmax > xmaxb)
647                                 disprect.xmax = xmaxb;
648                 }
649                 else disprect.xmax= xmaxb;
650                 
651                 if(yd<yparts-1) {
652                         disprect.ymax= disprect.ymin + party;
653                         if(disprect.ymax > ymaxb)
654                                 disprect.ymax = ymaxb;
655                 }
656                 else disprect.ymax= ymaxb;
657                 
658                 rectx= disprect.xmax - disprect.xmin;
659                 recty= disprect.ymax - disprect.ymin;
660                 
661                 /* so, now can we add this part? */
662                 if(rectx>0 && recty>0) {
663                         RenderPart *pa= MEM_callocN(sizeof(RenderPart), "new part");
664                         
665                         /* Non-box filters need 2 pixels extra to work */
666                         if((re->r.filtertype || (re->r.mode & R_EDGE))) {
667                                 pa->crop= 2;
668                                 disprect.xmin -= pa->crop;
669                                 disprect.ymin -= pa->crop;
670                                 disprect.xmax += pa->crop;
671                                 disprect.ymax += pa->crop;
672                                 rectx+= 2*pa->crop;
673                                 recty+= 2*pa->crop;
674                         }
675                         pa->disprect= disprect;
676                         pa->rectx= rectx;
677                         pa->recty= recty;
678
679                         BLI_addtail(&re->parts, pa);
680                         re->i.totpart++;
681                 }
682         }
683 }
684
685
686